gitlab-org--gitlab-foss/app/models/commit_range.rb

160 lines
3.8 KiB
Ruby
Raw Normal View History

2015-04-21 22:04:20 +00:00
# CommitRange makes it easier to work with commit ranges
#
# Examples:
#
# range = CommitRange.new('f3f85602...e86e1013', project)
# range.exclude_start? # => false
2015-04-21 22:04:20 +00:00
# range.reference_title # => "Commits f3f85602 through e86e1013"
# range.to_s # => "f3f85602...e86e1013"
2015-04-21 22:04:20 +00:00
#
# range = CommitRange.new('f3f856029bc5f966c5a7ee24cf7efefdd20e6019..e86e1013709735be5bb767e2b228930c543f25ae', project)
# range.exclude_start? # => true
# range.reference_title # => "Commits f3f85602^ through e86e1013"
# range.to_param # => {from: "f3f856029bc5f966c5a7ee24cf7efefdd20e6019^", to: "e86e1013709735be5bb767e2b228930c543f25ae"}
# range.to_s # => "f3f85602..e86e1013"
2015-04-21 22:04:20 +00:00
#
# # Assuming `project` is a Project with a repository containing both commits:
# range.project = project
# range.valid_commits? # => true
#
class CommitRange
include ActiveModel::Conversion
include Referable
2015-04-21 22:04:20 +00:00
attr_reader :commit_from, :notation, :commit_to
2015-04-21 22:04:20 +00:00
# Optional Project model
attr_accessor :project
# See `exclude_start?`
attr_reader :exclude_start
2015-04-21 22:04:20 +00:00
# The beginning and ending SHAs can be between 6 and 40 hex characters, and
# the range notation can be double- or triple-dot.
2015-04-21 22:04:20 +00:00
PATTERN = /\h{6,40}\.{2,3}\h{6,40}/
def self.reference_prefix
'@'
end
# Pattern used to extract commit range references from text
#
# This pattern supports cross-project references.
def self.reference_pattern
%r{
#{link_reference_pattern} |
(?:
(?:#{Project.reference_pattern}#{reference_prefix})?
(?<commit_range>#{PATTERN})
)
}x
end
def self.link_reference_pattern
super("compare", /(?<commit_range>#{PATTERN})/)
end
2015-04-21 22:04:20 +00:00
# Initialize a CommitRange
#
# range_string - The String commit range.
# project - An optional Project model.
#
# Raises ArgumentError if `range_string` does not match `PATTERN`.
def initialize(range_string, project)
@project = project
2015-04-21 22:04:20 +00:00
range_string.strip!
unless range_string.match(/\A#{PATTERN}\z/)
raise ArgumentError, "invalid CommitRange string format: #{range_string}"
end
ref_from, @notation, ref_to = range_string.split(/(\.{2,3})/, 2)
2015-04-21 22:04:20 +00:00
@exclude_start = @notation == '..'
if project.valid_repo?
@commit_from = project.commit(ref_from)
@commit_to = project.commit(ref_to)
end
2015-04-21 22:04:20 +00:00
end
def inspect
%(#<#{self.class}:#{object_id} #{to_s}>)
end
2015-04-23 17:52:35 +00:00
def to_s
sha_from + notation + sha_to
2015-04-21 22:04:20 +00:00
end
alias_method :id, :to_s
def to_reference(from_project = nil)
reference = Commit.truncate_sha(sha_from) + notation + Commit.truncate_sha(sha_to)
if cross_project_reference?(from_project)
reference = project.to_reference + self.class.reference_prefix + reference
end
reference
end
2015-04-21 22:04:20 +00:00
# Returns a String for use in a link's title attribute
def reference_title
"Commits #{sha_start} through #{sha_to}"
2015-04-21 22:04:20 +00:00
end
# Return a Hash of parameters for passing to a URL helper
#
# See `namespace_project_compare_url`
def to_param
{ from: sha_start, to: sha_to }
2015-04-21 22:04:20 +00:00
end
def exclude_start?
exclude_start
2015-04-21 22:04:20 +00:00
end
# Check if both the starting and ending commit IDs exist in a project's
# repository
#
# project - An optional Project to check (default: `project`)
def valid_commits?
commit_start.present? && commit_end.present?
2015-04-21 22:04:20 +00:00
end
def persisted?
true
end
def sha_from
return nil unless @commit_from
@commit_from.id
2015-04-23 17:43:21 +00:00
end
2015-04-21 22:04:20 +00:00
def sha_to
return nil unless @commit_to
@commit_to.id
end
def sha_start
return nil unless sha_from
exclude_start? ? sha_from + '^' : sha_from
2015-04-21 22:04:20 +00:00
end
def commit_start
return nil unless sha_start
2015-04-21 22:04:20 +00:00
if exclude_start?
@commit_start ||= project.commit(sha_start)
else
commit_from
end
2015-04-21 22:04:20 +00:00
end
alias_method :sha_end, :sha_to
alias_method :commit_end, :commit_to
2015-04-21 22:04:20 +00:00
end