2014-09-02 18:07:02 +05:30
|
|
|
class Commit
|
|
|
|
extend ActiveModel::Naming
|
2015-09-11 14:41:01 +05:30
|
|
|
|
|
|
|
include ActiveModel::Conversion
|
2017-08-17 22:00:37 +05:30
|
|
|
include Noteable
|
2015-09-11 14:41:01 +05:30
|
|
|
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
|
2016-06-16 23:09:34 +05:30
|
|
|
|
|
|
|
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
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
# The SHA can be between 7 and 40 hex characters.
|
|
|
|
COMMIT_SHA_PATTERN = '\h{7,40}'.freeze
|
|
|
|
|
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|
|
2017-08-17 22:00:37 +05:30
|
|
|
if commit.is_a?(Commit)
|
2015-04-26 12:48:37 +05:30
|
|
|
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
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
def from_hash(hash, project)
|
|
|
|
new(Gitlab::Git::Commit.new(hash), project)
|
|
|
|
end
|
|
|
|
|
|
|
|
def valid_hash?(key)
|
|
|
|
!!(/\A#{COMMIT_SHA_PATTERN}\z/ =~ key)
|
|
|
|
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
|
|
|
|
#
|
|
|
|
# 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
|
2017-08-17 22:00:37 +05:30
|
|
|
@link_reference_pattern ||= super("commit", /(?<commit>#{COMMIT_SHA_PATTERN})/)
|
2015-12-23 02:04:40 +05:30
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def to_reference(from_project = nil, full: false)
|
|
|
|
commit_reference(from_project, id, full: full)
|
2015-12-23 02:04:40 +05:30
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def reference_link_text(from_project = nil, full: false)
|
|
|
|
commit_reference(from_project, short_id, full: full)
|
2015-09-11 14:41:01 +05:30
|
|
|
end
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
def diff_line_count
|
2017-08-17 22:00:37 +05:30
|
|
|
@diff_line_count ||= Commit.diff_line_count(raw_diffs)
|
2014-09-02 18:07:02 +05:30
|
|
|
@diff_line_count
|
|
|
|
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
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
@full_title =
|
|
|
|
if safe_message.blank?
|
|
|
|
no_commit_message
|
|
|
|
else
|
|
|
|
safe_message.split("\n", 2).first
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
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
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def discussion_notes
|
|
|
|
notes.non_diff_notes
|
|
|
|
end
|
|
|
|
|
2016-06-16 23:09:34 +05:30
|
|
|
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
|
|
|
|
|
2016-06-16 23:09:34 +05:30
|
|
|
def pipelines
|
2016-11-24 13:41:30 +05:30
|
|
|
project.pipelines.where(sha: sha)
|
2015-10-24 18:46:33 +05:30
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def last_pipeline
|
|
|
|
@last_pipeline ||= pipelines.last
|
|
|
|
end
|
|
|
|
|
2016-11-24 13:41:30 +05:30
|
|
|
def status(ref = nil)
|
|
|
|
@statuses ||= {}
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
return @statuses[ref] if @statuses.key?(ref)
|
|
|
|
|
|
|
|
@statuses[ref] = pipelines.latest_status(ref)
|
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-16 23:09:34 +05:30
|
|
|
|
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
|
|
|
|
2017-01-15 13:20:01 +05:30
|
|
|
def revert_description(user)
|
|
|
|
if merged_merge_request?(user)
|
|
|
|
"This reverts merge request #{merged_merge_request(user).to_reference}"
|
2016-04-02 18:10:28 +05:30
|
|
|
else
|
|
|
|
"This reverts commit #{sha}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-01-15 13:20:01 +05:30
|
|
|
def revert_message(user)
|
|
|
|
%Q{Revert "#{title.strip}"\n\n#{revert_description(user)}}
|
2016-04-02 18:10:28 +05:30
|
|
|
end
|
|
|
|
|
2017-01-15 13:20:01 +05:30
|
|
|
def reverts_commit?(commit, user)
|
|
|
|
description? && description.include?(commit.revert_description(user))
|
2016-04-02 18:10:28 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def merge_commit?
|
|
|
|
parents.size > 1
|
|
|
|
end
|
|
|
|
|
2017-01-15 13:20:01 +05:30
|
|
|
def merged_merge_request(current_user)
|
|
|
|
# Memoize with per-user access check
|
|
|
|
@merged_merge_request_hash ||= Hash.new do |hash, user|
|
|
|
|
hash[user] = merged_merge_request_no_cache(user)
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2017-01-15 13:20:01 +05:30
|
|
|
@merged_merge_request_hash[current_user]
|
2016-04-02 18:10:28 +05:30
|
|
|
end
|
|
|
|
|
2017-01-15 13:20:01 +05:30
|
|
|
def has_been_reverted?(current_user, noteable = self)
|
2016-06-16 23:09:34 +05:30
|
|
|
ext = all_references(current_user)
|
|
|
|
|
|
|
|
noteable.notes_with_associations.system.each do |note|
|
|
|
|
note.all_references(current_user, extractor: ext)
|
|
|
|
end
|
|
|
|
|
2017-01-15 13:20:01 +05:30
|
|
|
ext.commits.any? { |commit_ref| commit_ref.reverts_commit?(self, current_user) }
|
2016-04-02 18:10:28 +05:30
|
|
|
end
|
|
|
|
|
2017-01-15 13:20:01 +05:30
|
|
|
def change_type_title(user)
|
|
|
|
merged_merge_request?(user) ? 'merge request' : 'commit'
|
2016-06-02 11:05:42 +05:30
|
|
|
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
|
2017-08-17 22:00:37 +05:30
|
|
|
blob = ::Blob.decorate(Gitlab::Git::Blob.new(name: entry[:name]), @project)
|
2016-08-24 12:49:21 +05:30
|
|
|
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)
|
2017-08-17 22:00:37 +05:30
|
|
|
use_gitaly = Gitlab::GitalyClient.feature_enabled?(:commit_raw_diffs)
|
|
|
|
deltas_only = args.last.is_a?(Hash) && args.last[:deltas_only]
|
|
|
|
|
|
|
|
if use_gitaly && !deltas_only
|
|
|
|
Gitlab::GitalyClient::Commit.diff_from_parent(self, *args)
|
|
|
|
else
|
|
|
|
raw.diffs(*args)
|
|
|
|
end
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def diffs(diff_options = nil)
|
|
|
|
Gitlab::Diff::FileCollection::Commit.new(self, diff_options: diff_options)
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def persisted?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def touch
|
|
|
|
# no-op but needs to be defined since #persisted? is defined
|
|
|
|
end
|
|
|
|
|
|
|
|
WIP_REGEX = /\A\s*(((?i)(\[WIP\]|WIP:|WIP)\s|WIP$))|(fixup!|squash!)\s/.freeze
|
|
|
|
|
|
|
|
def work_in_progress?
|
|
|
|
!!(title =~ WIP_REGEX)
|
|
|
|
end
|
|
|
|
|
2015-12-23 02:04:40 +05:30
|
|
|
private
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def commit_reference(from_project, referable_commit_id, full: false)
|
|
|
|
reference = project.to_reference(from_project, full: full)
|
|
|
|
|
|
|
|
if reference.present?
|
|
|
|
"#{reference}#{self.class.reference_prefix}#{referable_commit_id}"
|
|
|
|
else
|
|
|
|
referable_commit_id
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
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
|
2017-01-15 13:20:01 +05:30
|
|
|
|
|
|
|
def merged_merge_request?(user)
|
|
|
|
!!merged_merge_request(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
def merged_merge_request_no_cache(user)
|
|
|
|
MergeRequestsFinder.new(user, project_id: project.id).find_by(merge_commit_sha: id) if merge_commit?
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|