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

503 lines
12 KiB
Ruby
Raw Normal View History

2018-03-17 18:26:18 +05:30
# coding: utf-8
2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2014-09-02 18:07:02 +05:30
class Commit
extend ActiveModel::Naming
2017-09-10 17:25:29 +05:30
extend Gitlab::Cache::RequestCache
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
2018-03-27 19:54:05 +05:30
include ::Gitlab::Utils::StrongMemoize
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
2017-09-10 17:25:29 +05:30
attr_accessor :project, :author
2018-03-17 18:26:18 +05:30
attr_accessor :redacted_description_html
attr_accessor :redacted_title_html
2018-03-27 19:54:05 +05:30
attr_reader :gpg_commit
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
2018-03-17 18:26:18 +05:30
MIN_SHA_LENGTH = Gitlab::Git::Commit::MIN_SHA_LENGTH
COMMIT_SHA_PATTERN = /\h{#{MIN_SHA_LENGTH},40}/.freeze
2018-05-09 12:01:36 +05:30
# Used by GFM to match and present link extensions on node texts and hrefs.
LINK_EXTENSION_PATTERN = /(patch)/.freeze
2018-03-17 18:26:18 +05:30
def banzai_render_context(field)
2018-05-09 12:01:36 +05:30
pipeline = field == :description ? :commit_description : :single_line
context = { pipeline: pipeline, project: self.project }
2018-03-17 18:26:18 +05:30
context[:author] = self.author if self.author
context
end
2017-08-17 22:00:37 +05:30
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
2018-03-17 18:26:18 +05:30
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
2015-04-26 12:48:37 +05:30
# Truncate sha to 8 characters
def truncate_sha(sha)
2018-03-17 18:26:18 +05:30
sha[0..MIN_SHA_LENGTH]
2015-04-26 12:48:37 +05:30
end
2016-06-02 11:05:42 +05:30
def max_diff_options
{
max_files: DIFF_HARD_LIMIT_FILES,
2017-09-10 17:25:29 +05:30
max_lines: DIFF_HARD_LIMIT_LINES
2016-06-02 11:05:42 +05:30
}
end
2017-08-17 22:00:37 +05:30
def from_hash(hash, project)
2017-09-10 17:25:29 +05:30
raw_commit = Gitlab::Git::Commit.new(project.repository.raw, hash)
new(raw_commit, project)
2017-08-17 22:00:37 +05:30
end
def valid_hash?(key)
!!(/\A#{COMMIT_SHA_PATTERN}\z/ =~ key)
end
2018-03-17 18:26:18 +05:30
def lazy(project, oid)
BatchLoader.for({ project: project, oid: oid }).batch do |items, loader|
items_by_project = items.group_by { |i| i[:project] }
items_by_project.each do |project, commit_ids|
oids = commit_ids.map { |i| i[:oid] }
project.repository.commits_by(oids: oids).each do |commit|
loader.call({ project: commit.project, oid: commit.id }, commit) if commit
end
end
end
end
2018-10-15 14:42:47 +05:30
def parent_class
::Project
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
2018-03-17 18:26:18 +05:30
@statuses = {}
2018-03-27 19:54:05 +05:30
@gpg_commit = Gitlab::Gpg::Commit.new(self) if project
2014-09-02 18:07:02 +05:30
end
def id
2018-03-17 18:26:18 +05:30
raw.id
end
def project_id
project.id
2014-09-02 18:07:02 +05:30
end
2015-09-11 14:41:01 +05:30
def ==(other)
2018-03-17 18:26:18 +05:30
other.is_a?(self.class) && raw == other.raw
2015-09-11 14:41:01 +05:30
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})?
2018-03-17 18:26:18 +05:30
(?<commit>#{COMMIT_SHA_PATTERN})
2015-09-11 14:41:01 +05:30
}x
end
2015-12-23 02:04:40 +05:30
def self.link_reference_pattern
2018-05-09 12:01:36 +05:30
@link_reference_pattern ||=
super("commit", /(?<commit>#{COMMIT_SHA_PATTERN})?(\.(?<extension>#{LINK_EXTENSION_PATTERN}))?/)
2015-12-23 02:04:40 +05:30
end
2018-03-17 18:26:18 +05:30
def to_reference(from = nil, full: false)
commit_reference(from, id, full: full)
2015-12-23 02:04:40 +05:30
end
2018-03-17 18:26:18 +05:30
def reference_link_text(from = nil, full: false)
commit_reference(from, 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
2017-09-10 17:25:29 +05:30
# after 80 characters + `...`
2014-09-02 18:07:02 +05:30
def title
2017-09-10 17:25:29 +05:30
return full_title if full_title.length < 100
full_title.truncate(81, separator: ' ', omission: '…')
2016-09-13 17:45:13 +05:30
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
2017-09-10 17:25:29 +05:30
@full_title ||=
2017-08-17 22:00:37 +05:30
if safe_message.blank?
no_commit_message
else
2018-05-09 12:01:36 +05:30
safe_message.split(/[\r\n]/, 2).first
2017-08-17 22:00:37 +05:30
end
2014-09-02 18:07:02 +05:30
end
2017-09-10 17:25:29 +05:30
# Returns full commit message if title is truncated (greater than 99 characters)
# otherwise returns commit message without first line
2014-09-02 18:07:02 +05:30
def description
2017-09-10 17:25:29 +05:30
return safe_message if full_title.length >= 100
safe_message.split("\n", 2)[1].try(:chomp)
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
2018-11-08 19:23:39 +05:30
def lazy_author
BatchLoader.for(author_email.downcase).batch do |emails, loader|
# A Hash that maps user Emails to the corresponding User objects. The
# Emails at this point are the _primary_ Emails of the Users.
users_for_emails = User
.by_any_email(emails)
.each_with_object({}) { |user, hash| hash[user.email] = user }
users_for_ids = users_for_emails
.values
.each_with_object({}) { |user, hash| hash[user.id] = user }
# Some commits may have used an alternative Email address. In this case we
# need to query the "emails" table to map those addresses to User objects.
Email
.where(email: emails - users_for_emails.keys)
.pluck(:email, :user_id)
.each { |(email, id)| users_for_emails[email] = users_for_ids[id] }
users_for_emails.each { |email, user| loader.call(email, user) }
end
end
2015-04-26 12:48:37 +05:30
def author
2018-11-08 19:23:39 +05:30
# We use __sync so that we get the actual objects back (including an actual
# nil), instead of a wrapper, as returning a wrapped nil breaks a lot of
# code.
lazy_author.__sync
2015-04-26 12:48:37 +05:30
end
2017-09-10 17:25:29 +05:30
request_cache(:author) { author_email.downcase }
2015-04-26 12:48:37 +05:30
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
2018-03-27 19:54:05 +05:30
@parents ||= parent_ids.map { |oid| Commit.lazy(project, oid) }
2015-10-24 18:46:33 +05:30
end
def parent
2018-03-27 19:54:05 +05:30
strong_memoize(:parent) do
project.commit_by(oid: self.parent_id) if self.parent_id
end
2015-10-24 18:46:33 +05:30
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
def notes_with_associations
2018-05-09 12:01:36 +05:30
notes.includes(:author, :award_emoji)
end
2018-03-17 18:26:18 +05:30
def merge_requests
@merge_requests ||= project.merge_requests.by_commit_sha(sha)
end
def method_missing(method, *args, &block)
@raw.__send__(method, *args, &block) # rubocop:disable GitlabSecurity/PublicSend
2014-09-02 18:07:02 +05:30
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
def short_id
2018-03-17 18:26:18 +05:30
@raw.short_id(MIN_SHA_LENGTH)
2015-04-26 12:48:37 +05:30
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
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)
2017-08-17 22:00:37 +05:30
return @statuses[ref] if @statuses.key?(ref)
2018-03-17 18:26:18 +05:30
@statuses[ref] = project.pipelines.latest_status_per_commit(id, ref)[id]
end
def set_status_for_ref(ref, status)
@statuses[ref] = status
2015-04-26 12:48:37 +05:30
end
2015-12-23 02:04:40 +05:30
2017-09-10 17:25:29 +05:30
def signature
return @signature if defined?(@signature)
@signature = gpg_commit.signature
end
delegate :has_signature?, to: :gpg_commit
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
2018-03-17 18:26:18 +05:30
def cherry_pick_description(user)
2018-11-18 11:00:15 +05:30
message_body = ["(cherry picked from commit #{sha})"]
2018-03-17 18:26:18 +05:30
if merged_merge_request?(user)
commits_in_merge_request = merged_merge_request(user).commits
if commits_in_merge_request.present?
2018-11-18 11:00:15 +05:30
message_body << ""
2018-03-17 18:26:18 +05:30
commits_in_merge_request.reverse.each do |commit_in_merge|
2018-11-18 11:00:15 +05:30
message_body << "#{commit_in_merge.short_id} #{commit_in_merge.title}"
2018-03-17 18:26:18 +05:30
end
end
end
2018-11-18 11:00:15 +05:30
message_body.join("\n")
2018-03-17 18:26:18 +05:30
end
def cherry_pick_message(user)
%Q{#{message}\n\n#{cherry_pick_description(user)}}
end
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
2018-03-17 18:26:18 +05:30
def has_been_reverted?(current_user, notes_association = nil)
ext = all_references(current_user)
2018-03-17 18:26:18 +05:30
notes_association ||= notes_with_associations
2018-03-17 18:26:18 +05:30
notes_association.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
2018-03-17 18:26:18 +05:30
# uri_type('not/found') # => nil
2016-06-22 15:30:34 +05:30
#
# Returns a symbol
def uri_type(path)
2018-03-17 18:26:18 +05:30
entry = @raw.tree_entry(path)
return unless entry
2016-06-22 15:30:34 +05:30
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
end
2016-09-13 17:45:13 +05:30
def raw_diffs(*args)
2018-03-17 18:26:18 +05:30
raw.diffs(*args)
2016-09-13 17:45:13 +05:30
end
2017-09-10 17:25:29 +05:30
def raw_deltas
2018-03-17 18:26:18 +05:30
@deltas ||= raw.deltas
2017-09-10 17:25:29 +05:30
end
2018-03-17 18:26:18 +05:30
def diffs(diff_options = {})
2016-09-13 17:45:13 +05:30
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
2018-10-15 14:42:47 +05:30
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
2017-08-17 22:00:37 +05:30
WIP_REGEX = /\A\s*(((?i)(\[WIP\]|WIP:|WIP)\s|WIP$))|(fixup!|squash!)\s/.freeze
def work_in_progress?
!!(title =~ WIP_REGEX)
end
2018-03-27 19:54:05 +05:30
def merged_merge_request?(user)
!!merged_merge_request(user)
end
2015-12-23 02:04:40 +05:30
private
2018-03-17 18:26:18 +05:30
def commit_reference(from, referable_commit_id, full: false)
reference = project.to_reference(from, full: full)
2017-08-17 22:00:37 +05:30
if reference.present?
"#{reference}#{self.class.reference_prefix}#{referable_commit_id}"
else
referable_commit_id
end
end
2015-12-23 02:04:40 +05:30
def repo_changes
changes = { added: [], modified: [], removed: [] }
2017-09-10 17:25:29 +05:30
raw_deltas.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_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