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

274 lines
6.3 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
2015-10-24 18:46:33 +05:30
participant :author, :committer, :notes
2015-09-11 14:41:01 +05:30
attr_accessor :project
2014-09-02 18:07:02 +05:30
# Safe amount of changes (files and lines) in one commit to render
# Used to prevent 500 error on huge commits by suppressing diff
#
# User can force display of diff above this size
2015-04-26 12:48:37 +05:30
DIFF_SAFE_FILES = 100 unless defined?(DIFF_SAFE_FILES)
DIFF_SAFE_LINES = 5000 unless defined?(DIFF_SAFE_LINES)
2014-09-02 18:07:02 +05:30
# Commits above this size will not be rendered in HTML
2015-04-26 12:48:37 +05:30
DIFF_HARD_LIMIT_FILES = 1000 unless defined?(DIFF_HARD_LIMIT_FILES)
DIFF_HARD_LIMIT_LINES = 50000 unless defined?(DIFF_HARD_LIMIT_LINES)
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)
diffs.reduce(0) { |sum, d| sum + d.diff.lines.count }
end
2015-04-26 12:48:37 +05:30
# Truncate sha to 8 characters
def truncate_sha(sha)
sha[0..7]
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
%r{
(?:#{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-04-02 18:10:28 +05:30
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
@diff_line_count ||= Commit::diff_line_count(self.diffs)
@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
title = safe_message
return no_commit_message if title.blank?
2015-04-26 12:48:37 +05:30
title_end = title.index("\n")
2014-09-02 18:07:02 +05:30
if (!title_end && title.length > 100) || (title_end && title_end > 100)
2015-04-26 12:48:37 +05:30
title[0..79] << ""
2014-09-02 18:07:02 +05:30
else
2015-04-26 12:48:37 +05:30
title.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)
2015-04-26 12:48:37 +05:30
path_with_namespace = project.path_with_namespace
2015-12-23 02:04:40 +05:30
data = {
2015-04-26 12:48:37 +05:30
id: id,
message: safe_message,
timestamp: committed_date.xmlschema,
url: "#{Gitlab.config.gitlab.url}/#{path_with_namespace}/commit/#{id}",
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
2015-12-23 02:04:40 +05:30
@author ||= User.find_by_any_email(author_email.downcase)
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 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
2015-10-24 18:46:33 +05:30
def ci_commit
project.ci_commit(sha)
end
def status
ci_commit.try(:status) || :not_found
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
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
%Q{Revert "#{title}"\n\n#{revert_description}}
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)
Gitlab::ReferenceExtractor.lazily do
noteable.notes.system.flat_map do |note|
note.all_references(current_user).commits
end
end.any? { |commit_ref| commit_ref.reverts_commit?(self) }
end
2015-12-23 02:04:40 +05:30
private
def repo_changes
changes = { added: [], modified: [], removed: [] }
diffs.each do |diff|
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