2018-07-25 09:30:33 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2011-11-20 20:32:12 +00:00
|
|
|
class Commit
|
2012-01-28 14:47:55 +00:00
|
|
|
extend ActiveModel::Naming
|
2017-07-18 09:48:48 +00:00
|
|
|
extend Gitlab::Cache::RequestCache
|
2015-05-03 03:11:21 +00:00
|
|
|
|
|
|
|
include ActiveModel::Conversion
|
2017-03-10 01:29:11 +00:00
|
|
|
include Noteable
|
2015-04-21 13:23:20 +00:00
|
|
|
include Participable
|
2015-10-14 14:20:11 +00:00
|
|
|
include Mentionable
|
2015-05-03 03:11:21 +00:00
|
|
|
include Referable
|
|
|
|
include StaticModel
|
2019-01-28 12:12:30 +00:00
|
|
|
include Presentable
|
2018-03-05 09:37:16 +00:00
|
|
|
include ::Gitlab::Utils::StrongMemoize
|
2019-12-11 15:07:38 +00:00
|
|
|
include ActsAsPaginatedDiff
|
2019-06-05 04:59:48 +00:00
|
|
|
include CacheMarkdownField
|
2013-05-30 23:16:49 +00:00
|
|
|
|
2015-10-22 13:40:02 +00:00
|
|
|
attr_mentionable :safe_message, pipeline: :single_line
|
2016-05-26 11:38:28 +00:00
|
|
|
|
|
|
|
participant :author
|
|
|
|
participant :committer
|
|
|
|
participant :notes_with_associations
|
2011-11-28 09:46:41 +00:00
|
|
|
|
2017-08-23 16:53:29 +00:00
|
|
|
attr_accessor :redacted_description_html
|
|
|
|
attr_accessor :redacted_title_html
|
2018-09-04 12:51:02 +00:00
|
|
|
attr_accessor :redacted_full_title_html
|
2020-02-07 00:09:12 +00:00
|
|
|
attr_reader :container
|
2020-02-04 15:08:40 +00:00
|
|
|
|
|
|
|
delegate :repository, to: :container
|
|
|
|
delegate :project, to: :repository, allow_nil: true
|
2015-04-21 13:09:15 +00:00
|
|
|
|
2016-03-03 17:38:44 +00:00
|
|
|
DIFF_SAFE_LINES = Gitlab::Git::DiffCollection::DEFAULT_LIMITS[:max_lines]
|
2014-07-15 15:28:21 +00:00
|
|
|
|
2013-09-04 07:33:09 +00:00
|
|
|
# Commits above this size will not be rendered in HTML
|
2016-04-19 09:48:10 +00:00
|
|
|
DIFF_HARD_LIMIT_FILES = 1000
|
|
|
|
DIFF_HARD_LIMIT_LINES = 50000
|
2012-10-25 09:16:14 +00:00
|
|
|
|
2017-11-22 14:48:09 +00:00
|
|
|
MIN_SHA_LENGTH = Gitlab::Git::Commit::MIN_SHA_LENGTH
|
2017-09-22 09:30:37 +00:00
|
|
|
COMMIT_SHA_PATTERN = /\h{#{MIN_SHA_LENGTH},40}/.freeze
|
2019-08-28 05:48:24 +00:00
|
|
|
EXACT_COMMIT_SHA_PATTERN = /\A#{COMMIT_SHA_PATTERN}\z/.freeze
|
2018-04-06 15:13:18 +00:00
|
|
|
# Used by GFM to match and present link extensions on node texts and hrefs.
|
|
|
|
LINK_EXTENSION_PATTERN = /(patch)/.freeze
|
2016-12-08 14:54:45 +00:00
|
|
|
|
2019-06-05 04:59:48 +00:00
|
|
|
cache_markdown_field :title, pipeline: :single_line
|
|
|
|
cache_markdown_field :full_title, pipeline: :single_line
|
|
|
|
cache_markdown_field :description, pipeline: :commit_description
|
2017-08-23 16:53:29 +00:00
|
|
|
|
2014-02-03 16:13:24 +00:00
|
|
|
class << self
|
2020-02-04 15:08:40 +00:00
|
|
|
def decorate(commits, container)
|
2014-10-10 14:39:29 +00:00
|
|
|
commits.map do |commit|
|
2017-02-22 17:25:50 +00:00
|
|
|
if commit.is_a?(Commit)
|
2014-10-10 14:39:29 +00:00
|
|
|
commit
|
|
|
|
else
|
2020-02-04 15:08:40 +00:00
|
|
|
self.new(commit, container)
|
2014-10-10 14:39:29 +00:00
|
|
|
end
|
|
|
|
end
|
2014-02-03 16:13:24 +00:00
|
|
|
end
|
2013-04-01 13:04:35 +00:00
|
|
|
|
2014-02-03 16:13:24 +00:00
|
|
|
# Calculate number of lines to render for diffs
|
|
|
|
def diff_line_count(diffs)
|
2016-03-03 17:38:44 +00:00
|
|
|
diffs.reduce(0) { |sum, d| sum + Gitlab::Git::Util.count_lines(d.diff) }
|
2014-02-03 16:13:24 +00:00
|
|
|
end
|
2014-10-10 12:39:48 +00:00
|
|
|
|
2017-11-18 14:06:55 +00:00
|
|
|
def order_by(collection:, order_by:, sort:)
|
|
|
|
return collection unless %w[email name commits].include?(order_by)
|
|
|
|
return collection unless %w[asc desc].include?(sort)
|
|
|
|
|
|
|
|
collection.sort do |a, b|
|
|
|
|
operands = [a, b].tap { |o| o.reverse! if sort == 'desc' }
|
|
|
|
|
|
|
|
attr1, attr2 = operands.first.public_send(order_by), operands.second.public_send(order_by) # rubocop:disable PublicSend
|
|
|
|
|
|
|
|
# use case insensitive comparison for string values
|
|
|
|
order_by.in?(%w[email name]) ? attr1.casecmp(attr2) : attr1 <=> attr2
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-10-10 13:30:14 +00:00
|
|
|
# Truncate sha to 8 characters
|
2014-10-10 12:39:48 +00:00
|
|
|
def truncate_sha(sha)
|
2017-09-22 09:30:37 +00:00
|
|
|
sha[0..MIN_SHA_LENGTH]
|
2014-10-10 12:39:48 +00:00
|
|
|
end
|
2016-03-03 17:38:44 +00:00
|
|
|
|
|
|
|
def max_diff_options
|
|
|
|
{
|
|
|
|
max_files: DIFF_HARD_LIMIT_FILES,
|
2017-05-03 11:22:03 +00:00
|
|
|
max_lines: DIFF_HARD_LIMIT_LINES
|
2016-03-03 17:38:44 +00:00
|
|
|
}
|
|
|
|
end
|
2016-11-24 14:07:44 +00:00
|
|
|
|
2020-02-04 15:08:40 +00:00
|
|
|
def from_hash(hash, container)
|
|
|
|
raw_commit = Gitlab::Git::Commit.new(container.repository.raw, hash)
|
|
|
|
new(raw_commit, container)
|
2016-11-24 14:07:44 +00:00
|
|
|
end
|
2016-12-08 14:54:45 +00:00
|
|
|
|
|
|
|
def valid_hash?(key)
|
2019-08-28 05:48:24 +00:00
|
|
|
!!(EXACT_COMMIT_SHA_PATTERN =~ key)
|
2016-12-08 14:54:45 +00:00
|
|
|
end
|
2017-12-05 13:15:30 +00:00
|
|
|
|
2020-02-04 15:08:40 +00:00
|
|
|
def lazy(container, oid)
|
|
|
|
BatchLoader.for({ container: container, oid: oid }).batch(replace_methods: false) do |items, loader|
|
|
|
|
items_by_container = items.group_by { |i| i[:container] }
|
2017-12-05 13:15:30 +00:00
|
|
|
|
2020-02-04 15:08:40 +00:00
|
|
|
items_by_container.each do |container, commit_ids|
|
2017-12-05 13:15:30 +00:00
|
|
|
oids = commit_ids.map { |i| i[:oid] }
|
|
|
|
|
2020-02-04 15:08:40 +00:00
|
|
|
container.repository.commits_by(oids: oids).each do |commit|
|
|
|
|
loader.call({ container: commit.container, oid: commit.id }, commit) if commit
|
2017-12-05 13:15:30 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-05-01 12:39:44 +00:00
|
|
|
|
|
|
|
def parent_class
|
|
|
|
::Project
|
|
|
|
end
|
2013-09-04 07:33:09 +00:00
|
|
|
end
|
|
|
|
|
2013-03-31 15:49:58 +00:00
|
|
|
attr_accessor :raw
|
2012-09-27 06:20:36 +00:00
|
|
|
|
2020-02-04 15:08:40 +00:00
|
|
|
def initialize(raw_commit, container)
|
2013-01-04 22:35:38 +00:00
|
|
|
raise "Nil as raw commit passed" unless raw_commit
|
|
|
|
|
2013-03-31 15:49:58 +00:00
|
|
|
@raw = raw_commit
|
2020-02-04 15:08:40 +00:00
|
|
|
@container = container
|
2012-11-22 19:49:01 +00:00
|
|
|
end
|
2013-03-21 20:50:18 +00:00
|
|
|
|
2019-09-30 15:08:09 +00:00
|
|
|
delegate \
|
|
|
|
:pipelines,
|
|
|
|
:last_pipeline,
|
|
|
|
:latest_pipeline,
|
|
|
|
:latest_pipeline_for_project,
|
|
|
|
:set_latest_pipeline_for_ref,
|
|
|
|
:status,
|
|
|
|
to: :with_pipeline
|
|
|
|
|
|
|
|
def with_pipeline
|
|
|
|
@with_pipeline ||= CommitWithPipeline.new(self)
|
|
|
|
end
|
|
|
|
|
2013-03-31 16:00:45 +00:00
|
|
|
def id
|
2017-11-03 13:16:43 +00:00
|
|
|
raw.id
|
2013-03-31 16:00:45 +00:00
|
|
|
end
|
|
|
|
|
2018-02-13 12:41:35 +00:00
|
|
|
def project_id
|
2020-02-04 15:08:40 +00:00
|
|
|
project&.id
|
2018-02-13 12:41:35 +00:00
|
|
|
end
|
|
|
|
|
2015-05-03 02:41:37 +00:00
|
|
|
def ==(other)
|
2017-12-05 13:15:30 +00:00
|
|
|
other.is_a?(self.class) && raw == other.raw
|
2015-05-03 02:41:37 +00:00
|
|
|
end
|
|
|
|
|
2015-05-14 20:59:39 +00:00
|
|
|
def self.reference_prefix
|
|
|
|
'@'
|
|
|
|
end
|
|
|
|
|
2019-08-28 05:48:24 +00:00
|
|
|
def self.reference_valid?(reference)
|
|
|
|
!!(reference =~ EXACT_COMMIT_SHA_PATTERN)
|
|
|
|
end
|
|
|
|
|
2015-05-14 20:59:39 +00:00
|
|
|
# Pattern used to extract commit references from text
|
|
|
|
#
|
|
|
|
# This pattern supports cross-project references.
|
|
|
|
def self.reference_pattern
|
2016-03-24 15:41:48 +00:00
|
|
|
@reference_pattern ||= %r{
|
2015-05-15 20:10:55 +00:00
|
|
|
(?:#{Project.reference_pattern}#{reference_prefix})?
|
2017-09-22 09:30:37 +00:00
|
|
|
(?<commit>#{COMMIT_SHA_PATTERN})
|
2015-05-15 20:10:55 +00:00
|
|
|
}x
|
2015-05-14 20:59:39 +00:00
|
|
|
end
|
|
|
|
|
2015-11-30 20:14:46 +00:00
|
|
|
def self.link_reference_pattern
|
2018-04-06 15:13:18 +00:00
|
|
|
@link_reference_pattern ||=
|
|
|
|
super("commit", /(?<commit>#{COMMIT_SHA_PATTERN})?(\.(?<extension>#{LINK_EXTENSION_PATTERN}))?/)
|
2015-11-30 20:14:46 +00:00
|
|
|
end
|
|
|
|
|
2017-11-22 13:20:35 +00:00
|
|
|
def to_reference(from = nil, full: false)
|
|
|
|
commit_reference(from, id, full: full)
|
2015-12-01 11:58:45 +00:00
|
|
|
end
|
|
|
|
|
2017-11-22 13:20:35 +00:00
|
|
|
def reference_link_text(from = nil, full: false)
|
|
|
|
commit_reference(from, short_id, full: full)
|
2015-05-03 03:11:21 +00:00
|
|
|
end
|
|
|
|
|
2013-09-04 07:33:09 +00:00
|
|
|
def diff_line_count
|
2017-02-22 00:51:36 +00:00
|
|
|
@diff_line_count ||= Commit.diff_line_count(raw_diffs)
|
2013-09-04 07:33:09 +00:00
|
|
|
@diff_line_count
|
|
|
|
end
|
|
|
|
|
2013-03-31 20:48:12 +00:00
|
|
|
# Returns the commits title.
|
|
|
|
#
|
|
|
|
# Usually, the commit title is the first line of the commit message.
|
2013-08-12 19:28:31 +00:00
|
|
|
# In case this first line is longer than 100 characters, it is cut off
|
2017-04-21 15:31:18 +00:00
|
|
|
# after 80 characters + `...`
|
2013-03-31 20:48:12 +00:00
|
|
|
def title
|
2017-04-21 15:31:18 +00:00
|
|
|
return full_title if full_title.length < 100
|
|
|
|
|
2018-12-04 17:59:48 +00:00
|
|
|
# Use three dots instead of the ellipsis Unicode character because
|
|
|
|
# some clients show the raw Unicode value in the merge commit.
|
|
|
|
full_title.truncate(81, separator: ' ', omission: '...')
|
2016-07-26 04:49:06 +00:00
|
|
|
end
|
2013-03-31 20:48:12 +00:00
|
|
|
|
2016-07-26 04:49:06 +00:00
|
|
|
# Returns the full commits title
|
|
|
|
def full_title
|
2017-04-21 15:31:18 +00:00
|
|
|
@full_title ||=
|
2017-02-22 15:10:32 +00:00
|
|
|
if safe_message.blank?
|
|
|
|
no_commit_message
|
|
|
|
else
|
2017-07-25 11:56:09 +00:00
|
|
|
safe_message.split(/[\r\n]/, 2).first
|
2017-02-22 15:10:32 +00:00
|
|
|
end
|
2013-03-31 20:48:12 +00:00
|
|
|
end
|
|
|
|
|
2017-04-21 15:31:18 +00:00
|
|
|
# Returns full commit message if title is truncated (greater than 99 characters)
|
|
|
|
# otherwise returns commit message without first line
|
2013-03-31 20:48:12 +00:00
|
|
|
def description
|
2017-04-21 15:31:18 +00:00
|
|
|
return safe_message if full_title.length >= 100
|
2018-08-21 09:53:43 +00:00
|
|
|
return no_commit_message if safe_message.blank?
|
2013-03-31 20:48:12 +00:00
|
|
|
|
2018-08-21 09:53:43 +00:00
|
|
|
safe_message.split("\n", 2)[1].try(:chomp)
|
2017-04-21 15:31:18 +00:00
|
|
|
end
|
2017-06-16 14:00:58 +00:00
|
|
|
|
2014-02-25 13:54:30 +00:00
|
|
|
def description?
|
|
|
|
description.present?
|
2013-03-31 20:48:12 +00:00
|
|
|
end
|
|
|
|
|
2015-12-07 13:13:06 +00:00
|
|
|
def hook_attrs(with_changed_files: false)
|
2015-12-07 12:11:15 +00:00
|
|
|
data = {
|
2014-09-15 07:10:35 +00:00
|
|
|
id: id,
|
|
|
|
message: safe_message,
|
2020-02-21 15:09:05 +00:00
|
|
|
title: title,
|
2014-09-15 07:10:35 +00:00
|
|
|
timestamp: committed_date.xmlschema,
|
2016-04-13 09:25:42 +00:00
|
|
|
url: Gitlab::UrlBuilder.build(self),
|
2014-09-15 07:10:35 +00:00
|
|
|
author: {
|
|
|
|
name: author_name,
|
|
|
|
email: author_email
|
|
|
|
}
|
|
|
|
}
|
2015-12-07 12:11:15 +00:00
|
|
|
|
|
|
|
if with_changed_files
|
2015-12-07 13:13:06 +00:00
|
|
|
data.merge!(repo_changes)
|
2015-12-07 12:11:15 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
data
|
2014-09-15 07:10:35 +00:00
|
|
|
end
|
|
|
|
|
2018-05-07 16:22:07 +00:00
|
|
|
def lazy_author
|
|
|
|
BatchLoader.for(author_email.downcase).batch do |emails, loader|
|
2019-12-07 00:07:51 +00:00
|
|
|
users = User.by_any_email(emails, confirmed: true).includes(:emails)
|
2018-11-14 18:42:36 +00:00
|
|
|
|
|
|
|
emails.each do |email|
|
|
|
|
user = users.find { |u| u.any_email?(email) }
|
|
|
|
|
|
|
|
loader.call(email, user)
|
|
|
|
end
|
2018-05-07 16:22:07 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-03-27 11:16:34 +00:00
|
|
|
def author
|
2019-10-14 18:06:24 +00:00
|
|
|
strong_memoize(:author) do
|
|
|
|
lazy_author&.itself
|
|
|
|
end
|
2015-03-27 11:16:34 +00:00
|
|
|
end
|
2017-07-18 10:04:20 +00:00
|
|
|
request_cache(:author) { author_email.downcase }
|
2015-03-27 11:16:34 +00:00
|
|
|
|
2019-12-07 00:07:51 +00:00
|
|
|
def committer(confirmed: true)
|
|
|
|
@committer ||= User.find_by_any_email(committer_email, confirmed: confirmed)
|
2015-03-27 11:16:34 +00:00
|
|
|
end
|
|
|
|
|
2015-10-20 12:23:56 +00:00
|
|
|
def parents
|
2020-02-04 15:08:40 +00:00
|
|
|
@parents ||= parent_ids.map { |oid| Commit.lazy(container, oid) }
|
2015-10-20 12:23:56 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def parent
|
2018-03-05 09:37:16 +00:00
|
|
|
strong_memoize(:parent) do
|
2020-02-04 15:08:40 +00:00
|
|
|
container.commit_by(oid: self.parent_id) if self.parent_id
|
2018-03-05 09:37:16 +00:00
|
|
|
end
|
2015-10-20 12:23:56 +00:00
|
|
|
end
|
|
|
|
|
2015-04-21 13:15:49 +00:00
|
|
|
def notes
|
2020-02-04 15:08:40 +00:00
|
|
|
container.notes.for_commit_id(self.id)
|
2015-04-15 13:56:21 +00:00
|
|
|
end
|
|
|
|
|
2019-12-10 15:07:52 +00:00
|
|
|
def user_mentions
|
|
|
|
CommitUserMention.where(commit_id: self.id)
|
|
|
|
end
|
|
|
|
|
2017-03-10 01:29:11 +00:00
|
|
|
def discussion_notes
|
|
|
|
notes.non_diff_notes
|
|
|
|
end
|
|
|
|
|
2016-05-26 11:38:28 +00:00
|
|
|
def notes_with_associations
|
2018-04-19 05:02:04 +00:00
|
|
|
notes.includes(:author, :award_emoji)
|
2016-05-26 11:38:28 +00:00
|
|
|
end
|
|
|
|
|
2017-12-05 13:15:30 +00:00
|
|
|
def method_missing(method, *args, &block)
|
|
|
|
@raw.__send__(method, *args, &block) # rubocop:disable GitlabSecurity/PublicSend
|
2013-03-21 20:50:18 +00:00
|
|
|
end
|
2013-03-31 20:48:12 +00:00
|
|
|
|
2015-05-23 22:38:44 +00:00
|
|
|
def respond_to_missing?(method, include_private = false)
|
|
|
|
@raw.respond_to?(method, include_private) || super
|
2013-03-31 20:48:12 +00:00
|
|
|
end
|
2014-09-25 09:19:26 +00:00
|
|
|
|
2014-10-10 13:30:14 +00:00
|
|
|
def short_id
|
2017-09-22 09:30:37 +00:00
|
|
|
@raw.short_id(MIN_SHA_LENGTH)
|
2014-10-10 13:30:14 +00:00
|
|
|
end
|
|
|
|
|
2016-06-20 16:51:48 +00:00
|
|
|
def diff_refs
|
|
|
|
Gitlab::Diff::DiffRefs.new(
|
2016-08-19 14:05:47 +00:00
|
|
|
base_sha: self.parent_id || Gitlab::Git::BLANK_SHA,
|
2016-06-20 16:51:48 +00:00
|
|
|
head_sha: self.sha
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2020-02-07 00:09:12 +00:00
|
|
|
def has_signature?
|
|
|
|
signature_type && signature_type != :NONE
|
|
|
|
end
|
|
|
|
|
|
|
|
def raw_signature_type
|
|
|
|
strong_memoize(:raw_signature_type) do
|
|
|
|
next unless @raw.instance_of?(Gitlab::Git::Commit)
|
|
|
|
|
|
|
|
@raw.raw_commit.signature_type if defined? @raw.raw_commit.signature_type
|
|
|
|
end
|
|
|
|
end
|
2017-02-15 00:01:30 +00:00
|
|
|
|
2020-02-07 00:09:12 +00:00
|
|
|
def signature_type
|
|
|
|
@signature_type ||= raw_signature_type || :NONE
|
2017-02-15 00:01:30 +00:00
|
|
|
end
|
|
|
|
|
2020-02-07 00:09:12 +00:00
|
|
|
def signature
|
|
|
|
strong_memoize(:signature) do
|
|
|
|
case signature_type
|
|
|
|
when :PGP
|
|
|
|
Gitlab::Gpg::Commit.new(self).signature
|
|
|
|
when :X509
|
|
|
|
Gitlab::X509::Commit.new(self).signature
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-07-25 07:40:23 +00:00
|
|
|
|
2016-02-03 23:28:40 +00:00
|
|
|
def revert_branch_name
|
2016-02-12 15:08:42 +00:00
|
|
|
"revert-#{short_id}"
|
2016-02-03 23:28:40 +00:00
|
|
|
end
|
2016-05-26 11:38:28 +00:00
|
|
|
|
2016-04-18 07:39:07 +00:00
|
|
|
def cherry_pick_branch_name
|
2020-02-04 15:08:40 +00:00
|
|
|
repository.next_branch("cherry-pick-#{short_id}", mild: true)
|
2016-04-18 07:39:07 +00:00
|
|
|
end
|
2016-02-03 23:28:40 +00:00
|
|
|
|
2017-08-17 18:26:06 +00:00
|
|
|
def cherry_pick_description(user)
|
2018-07-25 09:30:33 +00:00
|
|
|
message_body = ["(cherry picked from commit #{sha})"]
|
2017-08-15 17:35:28 +00:00
|
|
|
|
2017-08-17 18:26:06 +00:00
|
|
|
if merged_merge_request?(user)
|
|
|
|
commits_in_merge_request = merged_merge_request(user).commits
|
2017-08-10 19:16:49 +00:00
|
|
|
|
2017-08-17 18:26:06 +00:00
|
|
|
if commits_in_merge_request.present?
|
2018-07-25 09:30:33 +00:00
|
|
|
message_body << ""
|
2017-08-10 19:16:49 +00:00
|
|
|
|
2019-07-24 21:08:25 +00:00
|
|
|
commits_in_merge_request.reverse_each do |commit_in_merge|
|
2018-07-25 09:30:33 +00:00
|
|
|
message_body << "#{commit_in_merge.short_id} #{commit_in_merge.title}"
|
2017-08-10 19:16:49 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-08-15 17:57:40 +00:00
|
|
|
|
2018-07-25 09:30:33 +00:00
|
|
|
message_body.join("\n")
|
2017-08-15 17:35:28 +00:00
|
|
|
end
|
2017-08-10 19:16:49 +00:00
|
|
|
|
2017-08-17 18:26:06 +00:00
|
|
|
def cherry_pick_message(user)
|
|
|
|
%Q{#{message}\n\n#{cherry_pick_description(user)}}
|
2017-08-10 19:16:49 +00:00
|
|
|
end
|
|
|
|
|
2016-11-29 13:47:43 +00:00
|
|
|
def revert_description(user)
|
|
|
|
if merged_merge_request?(user)
|
|
|
|
"This reverts merge request #{merged_merge_request(user).to_reference}"
|
2016-02-15 03:36:30 +00:00
|
|
|
else
|
|
|
|
"This reverts commit #{sha}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-11-29 13:47:43 +00:00
|
|
|
def revert_message(user)
|
2017-02-22 22:39:43 +00:00
|
|
|
%Q{Revert "#{title.strip}"\n\n#{revert_description(user)}}
|
2016-02-15 03:36:30 +00:00
|
|
|
end
|
2016-02-11 15:03:13 +00:00
|
|
|
|
2016-11-29 13:47:43 +00:00
|
|
|
def reverts_commit?(commit, user)
|
|
|
|
description? && description.include?(commit.revert_description(user))
|
2016-02-03 23:28:40 +00:00
|
|
|
end
|
|
|
|
|
2016-02-09 19:50:25 +00:00
|
|
|
def merge_commit?
|
2019-02-06 12:33:11 +00:00
|
|
|
parent_ids.size > 1
|
2016-02-05 23:49:45 +00:00
|
|
|
end
|
|
|
|
|
2016-11-29 13:47:43 +00:00
|
|
|
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
|
2016-12-14 13:00:06 +00:00
|
|
|
|
2016-11-29 13:47:43 +00:00
|
|
|
@merged_merge_request_hash[current_user]
|
2016-02-08 20:37:27 +00:00
|
|
|
end
|
|
|
|
|
2018-01-11 16:20:13 +00:00
|
|
|
def has_been_reverted?(current_user, notes_association = nil)
|
2020-03-11 00:09:09 +00:00
|
|
|
ext = Gitlab::ReferenceExtractor.new(project, current_user)
|
2018-01-11 16:20:13 +00:00
|
|
|
notes_association ||= notes_with_associations
|
2016-05-26 11:38:28 +00:00
|
|
|
|
2018-01-11 16:20:13 +00:00
|
|
|
notes_association.system.each do |note|
|
2016-05-26 11:38:28 +00:00
|
|
|
note.all_references(current_user, extractor: ext)
|
|
|
|
end
|
|
|
|
|
2016-11-29 13:47:43 +00:00
|
|
|
ext.commits.any? { |commit_ref| commit_ref.reverts_commit?(self, current_user) }
|
2016-02-15 03:36:30 +00:00
|
|
|
end
|
|
|
|
|
2016-11-29 13:47:43 +00:00
|
|
|
def change_type_title(user)
|
|
|
|
merged_merge_request?(user) ? 'merge request' : 'commit'
|
2016-04-18 07:39:07 +00:00
|
|
|
end
|
|
|
|
|
2016-06-21 14:53:16 +00:00
|
|
|
# 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
|
2018-01-15 09:39:06 +00:00
|
|
|
# uri_type('not/found') # => nil
|
2016-06-21 14:53:16 +00:00
|
|
|
#
|
|
|
|
# Returns a symbol
|
|
|
|
def uri_type(path)
|
2018-01-15 09:39:06 +00:00
|
|
|
entry = @raw.tree_entry(path)
|
|
|
|
return unless entry
|
|
|
|
|
2016-06-21 14:53:16 +00:00
|
|
|
if entry[:type] == :blob
|
2020-02-04 15:08:40 +00:00
|
|
|
blob = ::Blob.decorate(Gitlab::Git::Blob.new(name: entry[:name]), container)
|
2019-10-09 12:06:13 +00:00
|
|
|
blob.image? || blob.video? || blob.audio? ? :raw : :blob
|
2016-06-21 14:53:16 +00:00
|
|
|
else
|
|
|
|
entry[:type]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-27 17:00:34 +00:00
|
|
|
def raw_diffs(*args)
|
2017-07-25 21:26:52 +00:00
|
|
|
raw.diffs(*args)
|
2016-07-27 17:00:34 +00:00
|
|
|
end
|
|
|
|
|
2017-05-05 14:55:12 +00:00
|
|
|
def raw_deltas
|
2017-07-25 21:26:52 +00:00
|
|
|
@deltas ||= raw.deltas
|
2017-05-05 14:55:12 +00:00
|
|
|
end
|
2017-05-09 11:48:21 +00:00
|
|
|
|
2017-11-03 13:16:43 +00:00
|
|
|
def diffs(diff_options = {})
|
2016-07-26 07:21:42 +00:00
|
|
|
Gitlab::Diff::FileCollection::Commit.new(self, diff_options: diff_options)
|
|
|
|
end
|
|
|
|
|
2016-04-18 08:27:51 +00:00
|
|
|
def persisted?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2018-07-21 08:52:07 +00:00
|
|
|
def to_ability_name
|
|
|
|
model_name.singular
|
|
|
|
end
|
|
|
|
|
2016-04-18 08:27:51 +00:00
|
|
|
def touch
|
|
|
|
# no-op but needs to be defined since #persisted? is defined
|
|
|
|
end
|
|
|
|
|
2018-04-29 06:23:59 +00:00
|
|
|
def touch_later
|
|
|
|
# No-op.
|
|
|
|
# This method is called by ActiveRecord.
|
|
|
|
# We don't want to do anything for `Commit` model, so this is empty.
|
|
|
|
end
|
|
|
|
|
2020-07-08 21:09:09 +00:00
|
|
|
# WIP is deprecated in favor of Draft. Currently both options are supported
|
|
|
|
# https://gitlab.com/gitlab-org/gitlab/-/issues/227426
|
|
|
|
DRAFT_REGEX = /\A\s*#{Regexp.union(Gitlab::Regex.merge_request_wip, Gitlab::Regex.merge_request_draft)}|(fixup!|squash!)\s/.freeze
|
2016-12-15 20:48:26 +00:00
|
|
|
|
|
|
|
def work_in_progress?
|
2020-07-08 21:09:09 +00:00
|
|
|
!!(title =~ DRAFT_REGEX)
|
2016-12-15 20:48:26 +00:00
|
|
|
end
|
|
|
|
|
2018-02-09 15:32:42 +00:00
|
|
|
def merged_merge_request?(user)
|
|
|
|
!!merged_merge_request(user)
|
|
|
|
end
|
|
|
|
|
2018-12-28 10:44:25 +00:00
|
|
|
def cache_key
|
|
|
|
"commit:#{sha}"
|
|
|
|
end
|
|
|
|
|
2019-11-28 15:06:57 +00:00
|
|
|
def expire_note_etag_cache
|
|
|
|
super
|
|
|
|
|
|
|
|
expire_note_etag_cache_for_related_mrs
|
|
|
|
end
|
|
|
|
|
2015-12-04 14:23:21 +00:00
|
|
|
private
|
|
|
|
|
2019-11-28 15:06:57 +00:00
|
|
|
def expire_note_etag_cache_for_related_mrs
|
|
|
|
MergeRequest.includes(target_project: :namespace).by_commit_sha(id).find_each do |mr|
|
|
|
|
mr.expire_note_etag_cache
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-11-22 13:20:35 +00:00
|
|
|
def commit_reference(from, referable_commit_id, full: false)
|
2020-02-13 15:08:52 +00:00
|
|
|
base = container.to_reference_base(from, full: full)
|
2016-11-02 23:49:13 +00:00
|
|
|
|
2020-01-29 12:09:08 +00:00
|
|
|
if base.present?
|
|
|
|
"#{base}#{self.class.reference_prefix}#{referable_commit_id}"
|
2016-11-02 23:49:13 +00:00
|
|
|
else
|
|
|
|
referable_commit_id
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-12-04 14:23:21 +00:00
|
|
|
def repo_changes
|
|
|
|
changes = { added: [], modified: [], removed: [] }
|
|
|
|
|
2017-05-09 10:27:17 +00:00
|
|
|
raw_deltas.each do |diff|
|
2015-12-07 13:13:06 +00:00
|
|
|
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
|
2015-12-04 14:23:21 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
changes
|
|
|
|
end
|
2016-11-29 13:47:43 +00:00
|
|
|
|
|
|
|
def merged_merge_request_no_cache(user)
|
2020-02-04 15:08:40 +00:00
|
|
|
MergeRequestsFinder.new(user, project_id: project_id).find_by(merge_commit_sha: id) if merge_commit?
|
2016-11-29 13:47:43 +00:00
|
|
|
end
|
2011-11-20 20:32:12 +00:00
|
|
|
end
|