debian-mirror-gitlab/app/models/commit.rb

350 lines
7.9 KiB
Ruby
Raw Normal View History

2014-09-02 18:07:02 +05:30
class Commit
extend ActiveModel::Naming
2015-09-11 14:41:01 +05:30
include ActiveModel::Conversion
include Participable
2015-10-24 18:46:33 +05:30
include Mentionable
2015-09-11 14:41:01 +05:30
include Referable
include StaticModel
2014-09-02 18:07:02 +05:30
2015-12-23 02:04:40 +05:30
attr_mentionable :safe_message, pipeline: :single_line
participant :author
participant :committer
participant :notes_with_associations
2015-09-11 14:41:01 +05:30
attr_accessor :project
2014-09-02 18:07:02 +05:30
2016-06-02 11:05:42 +05:30
DIFF_SAFE_LINES = Gitlab::Git::DiffCollection::DEFAULT_LIMITS[:max_lines]
2014-09-02 18:07:02 +05:30
# Commits above this size will not be rendered in HTML
2016-06-02 11:05:42 +05:30
DIFF_HARD_LIMIT_FILES = 1000
DIFF_HARD_LIMIT_LINES = 50000
2014-09-02 18:07:02 +05:30
class << self
2015-09-11 14:41:01 +05:30
def decorate(commits, project)
2015-04-26 12:48:37 +05:30
commits.map do |commit|
if commit.kind_of?(Commit)
commit
else
2015-09-11 14:41:01 +05:30
self.new(commit, project)
2015-04-26 12:48:37 +05:30
end
end
2014-09-02 18:07:02 +05:30
end
# Calculate number of lines to render for diffs
def diff_line_count(diffs)
2016-06-02 11:05:42 +05:30
diffs.reduce(0) { |sum, d| sum + Gitlab::Git::Util.count_lines(d.diff) }
2014-09-02 18:07:02 +05:30
end
2015-04-26 12:48:37 +05:30
# Truncate sha to 8 characters
def truncate_sha(sha)
sha[0..7]
end
2016-06-02 11:05:42 +05:30
def max_diff_options
{
max_files: DIFF_HARD_LIMIT_FILES,
max_lines: DIFF_HARD_LIMIT_LINES,
}
end
2014-09-02 18:07:02 +05:30
end
attr_accessor :raw
2015-09-11 14:41:01 +05:30
def initialize(raw_commit, project)
2014-09-02 18:07:02 +05:30
raise "Nil as raw commit passed" unless raw_commit
@raw = raw_commit
2015-09-11 14:41:01 +05:30
@project = project
2014-09-02 18:07:02 +05:30
end
def id
@raw.id
end
2015-09-11 14:41:01 +05:30
def ==(other)
(self.class === other) && (raw == other.raw)
end
def self.reference_prefix
'@'
end
# Pattern used to extract commit references from text
#
2016-04-02 18:10:28 +05:30
# The SHA can be between 7 and 40 hex characters.
2015-09-11 14:41:01 +05:30
#
# This pattern supports cross-project references.
def self.reference_pattern
2016-06-02 11:05:42 +05:30
@reference_pattern ||= %r{
2015-09-11 14:41:01 +05:30
(?:#{Project.reference_pattern}#{reference_prefix})?
2016-04-02 18:10:28 +05:30
(?<commit>\h{7,40})
2015-09-11 14:41:01 +05:30
}x
end
2015-12-23 02:04:40 +05:30
def self.link_reference_pattern
2016-06-02 11:05:42 +05:30
@link_reference_pattern ||= super("commit", /(?<commit>\h{7,40})/)
2015-12-23 02:04:40 +05:30
end
2015-09-11 14:41:01 +05:30
def to_reference(from_project = nil)
if cross_project_reference?(from_project)
2015-12-23 02:04:40 +05:30
project.to_reference + self.class.reference_prefix + self.id
2015-09-11 14:41:01 +05:30
else
2015-12-23 02:04:40 +05:30
self.id
end
end
def reference_link_text(from_project = nil)
if cross_project_reference?(from_project)
project.to_reference + self.class.reference_prefix + self.short_id
else
self.short_id
2015-09-11 14:41:01 +05:30
end
end
2014-09-02 18:07:02 +05:30
def diff_line_count
2016-09-13 17:45:13 +05:30
@diff_line_count ||= Commit::diff_line_count(raw_diffs)
2014-09-02 18:07:02 +05:30
@diff_line_count
end
# Returns a string describing the commit for use in a link title
#
# Example
#
# "Commit: Alex Denisov - Project git clone panel"
def link_title
"Commit: #{author_name} - #{title}"
end
# Returns the commits title.
#
# Usually, the commit title is the first line of the commit message.
# In case this first line is longer than 100 characters, it is cut off
# after 80 characters and ellipses (`&hellp;`) are appended.
def title
2016-09-13 17:45:13 +05:30
full_title.length > 100 ? full_title[0..79] << "" : full_title
end
2014-09-02 18:07:02 +05:30
2016-09-13 17:45:13 +05:30
# Returns the full commits title
def full_title
return @full_title if @full_title
2014-09-02 18:07:02 +05:30
2016-09-13 17:45:13 +05:30
if safe_message.blank?
@full_title = no_commit_message
2014-09-02 18:07:02 +05:30
else
2016-09-13 17:45:13 +05:30
@full_title = safe_message.split("\n", 2).first
2014-09-02 18:07:02 +05:30
end
end
# Returns the commits description
#
# cut off, ellipses (`&hellp;`) are prepended to the commit message.
def description
2015-04-26 12:48:37 +05:30
title_end = safe_message.index("\n")
@description ||=
if (!title_end && safe_message.length > 100) || (title_end && title_end > 100)
"" << safe_message[80..-1]
else
safe_message.split("\n", 2)[1].try(:chomp)
end
2014-09-02 18:07:02 +05:30
end
def description?
description.present?
end
2015-12-23 02:04:40 +05:30
def hook_attrs(with_changed_files: false)
data = {
2015-04-26 12:48:37 +05:30
id: id,
message: safe_message,
timestamp: committed_date.xmlschema,
2016-06-02 11:05:42 +05:30
url: Gitlab::UrlBuilder.build(self),
2015-04-26 12:48:37 +05:30
author: {
name: author_name,
email: author_email
}
}
2015-12-23 02:04:40 +05:30
if with_changed_files
data.merge!(repo_changes)
end
data
2015-04-26 12:48:37 +05:30
end
2014-09-02 18:07:02 +05:30
# Discover issues should be closed when this commit is pushed to a project's
# default branch.
2015-09-11 14:41:01 +05:30
def closes_issues(current_user = self.committer)
2015-04-26 12:48:37 +05:30
Gitlab::ClosingIssueExtractor.new(project, current_user).closed_by_message(safe_message)
2014-09-02 18:07:02 +05:30
end
2015-04-26 12:48:37 +05:30
def author
2016-09-13 17:45:13 +05:30
if RequestStore.active?
key = "commit_author:#{author_email.downcase}"
# nil is a valid value since no author may exist in the system
if RequestStore.store.has_key?(key)
@author = RequestStore.store[key]
else
@author = find_author_by_any_email
RequestStore.store[key] = @author
end
else
@author ||= find_author_by_any_email
end
2015-04-26 12:48:37 +05:30
end
def committer
2015-12-23 02:04:40 +05:30
@committer ||= User.find_by_any_email(committer_email.downcase)
2015-09-11 14:41:01 +05:30
end
2015-10-24 18:46:33 +05:30
def parents
@parents ||= parent_ids.map { |id| project.commit(id) }
end
def parent
@parent ||= project.commit(self.parent_id) if self.parent_id
end
2015-09-11 14:41:01 +05:30
def notes
project.notes.for_commit_id(self.id)
2014-09-02 18:07:02 +05:30
end
def notes_with_associations
notes.includes(:author)
end
2014-09-02 18:07:02 +05:30
def method_missing(m, *args, &block)
@raw.send(m, *args, &block)
end
2015-09-11 14:41:01 +05:30
def respond_to_missing?(method, include_private = false)
@raw.respond_to?(method, include_private) || super
2014-09-02 18:07:02 +05:30
end
2015-04-26 12:48:37 +05:30
# Truncate sha to 8 characters
def short_id
@raw.short_id(7)
end
2016-08-24 12:49:21 +05:30
def diff_refs
Gitlab::Diff::DiffRefs.new(
2016-09-13 17:45:13 +05:30
base_sha: self.parent_id || Gitlab::Git::BLANK_SHA,
2016-08-24 12:49:21 +05:30
head_sha: self.sha
)
end
def pipelines
@pipeline ||= project.pipelines.where(sha: sha)
2015-10-24 18:46:33 +05:30
end
def status
2016-06-02 11:05:42 +05:30
return @status if defined?(@status)
@status ||= pipelines.status
2015-04-26 12:48:37 +05:30
end
2015-12-23 02:04:40 +05:30
2016-04-02 18:10:28 +05:30
def revert_branch_name
"revert-#{short_id}"
end
2016-06-02 11:05:42 +05:30
def cherry_pick_branch_name
project.repository.next_branch("cherry-pick-#{short_id}", mild: true)
end
2016-04-02 18:10:28 +05:30
def revert_description
if merged_merge_request
"This reverts merge request #{merged_merge_request.to_reference}"
else
"This reverts commit #{sha}"
end
end
def revert_message
2016-06-02 11:05:42 +05:30
%Q{Revert "#{title.strip}"\n\n#{revert_description}}
2016-04-02 18:10:28 +05:30
end
def reverts_commit?(commit)
description? && description.include?(commit.revert_description)
end
def merge_commit?
parents.size > 1
end
def merged_merge_request
return @merged_merge_request if defined?(@merged_merge_request)
@merged_merge_request = project.merge_requests.find_by(merge_commit_sha: id) if merge_commit?
end
def has_been_reverted?(current_user = nil, noteable = self)
ext = all_references(current_user)
noteable.notes_with_associations.system.each do |note|
note.all_references(current_user, extractor: ext)
end
ext.commits.any? { |commit_ref| commit_ref.reverts_commit?(self) }
2016-04-02 18:10:28 +05:30
end
2016-06-02 11:05:42 +05:30
def change_type_title
merged_merge_request ? 'merge request' : 'commit'
end
2016-06-22 15:30:34 +05:30
# Get the URI type of the given path
#
# Used to build URLs to files in the repository in GFM.
#
# path - String path to check
#
# Examples:
#
# uri_type('doc/README.md') # => :blob
# uri_type('doc/logo.png') # => :raw
# uri_type('doc/api') # => :tree
# uri_type('not/found') # => :nil
#
# Returns a symbol
def uri_type(path)
entry = @raw.tree.path(path)
if entry[:type] == :blob
2016-08-24 12:49:21 +05:30
blob = ::Blob.decorate(Gitlab::Git::Blob.new(name: entry[:name]))
blob.image? || blob.video? ? :raw : :blob
2016-06-22 15:30:34 +05:30
else
entry[:type]
end
rescue Rugged::TreeError
nil
end
2016-09-13 17:45:13 +05:30
def raw_diffs(*args)
raw.diffs(*args)
end
def diffs(diff_options = nil)
Gitlab::Diff::FileCollection::Commit.new(self, diff_options: diff_options)
end
2015-12-23 02:04:40 +05:30
private
2016-09-13 17:45:13 +05:30
def find_author_by_any_email
User.find_by_any_email(author_email.downcase)
end
2015-12-23 02:04:40 +05:30
def repo_changes
changes = { added: [], modified: [], removed: [] }
2016-09-13 17:45:13 +05:30
raw_diffs(deltas_only: true).each do |diff|
2015-12-23 02:04:40 +05:30
if diff.deleted_file
changes[:removed] << diff.old_path
elsif diff.renamed_file || diff.new_file
changes[:added] << diff.new_path
else
changes[:modified] << diff.new_path
end
end
changes
end
2014-09-02 18:07:02 +05:30
end