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

169 lines
4.1 KiB
Ruby
Raw Normal View History

2015-09-11 14:41:01 +05:30
# CommitRange makes it easier to work with commit ranges
#
# Examples:
#
2015-12-23 02:04:40 +05:30
# range = CommitRange.new('f3f85602...e86e1013', project)
2015-09-11 14:41:01 +05:30
# range.exclude_start? # => false
# range.reference_title # => "Commits f3f85602 through e86e1013"
# range.to_s # => "f3f85602...e86e1013"
#
2015-12-23 02:04:40 +05:30
# range = CommitRange.new('f3f856029bc5f966c5a7ee24cf7efefdd20e6019..e86e1013709735be5bb767e2b228930c543f25ae', project)
2015-09-11 14:41:01 +05:30
# range.exclude_start? # => true
# range.reference_title # => "Commits f3f85602^ through e86e1013"
# range.to_param # => {from: "f3f856029bc5f966c5a7ee24cf7efefdd20e6019^", to: "e86e1013709735be5bb767e2b228930c543f25ae"}
# range.to_s # => "f3f85602..e86e1013"
#
2015-12-23 02:04:40 +05:30
# # Assuming the specified project has a repository containing both commits:
2015-09-11 14:41:01 +05:30
# range.valid_commits? # => true
#
class CommitRange
include ActiveModel::Conversion
include Referable
2015-12-23 02:04:40 +05:30
attr_reader :commit_from, :notation, :commit_to
attr_reader :ref_from, :ref_to
2015-09-11 14:41:01 +05:30
# Optional Project model
attr_accessor :project
2015-12-23 02:04:40 +05:30
# The beginning and ending refs can be named or SHAs, and
2015-09-11 14:41:01 +05:30
# the range notation can be double- or triple-dot.
2015-12-23 02:04:40 +05:30
REF_PATTERN = /[0-9a-zA-Z][0-9a-zA-Z_.-]*[0-9a-zA-Z\^]/
PATTERN = /#{REF_PATTERN}\.{2,3}#{REF_PATTERN}/
# In text references, the beginning and ending refs can only be SHAs
2016-04-02 18:10:28 +05:30
# between 7 and 40 hex characters.
STRICT_PATTERN = /\h{7,40}\.{2,3}\h{7,40}/
2015-09-11 14:41:01 +05:30
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{
(?:#{Project.reference_pattern}#{reference_prefix})?
2015-12-23 02:04:40 +05:30
(?<commit_range>#{STRICT_PATTERN})
2015-09-11 14:41:01 +05:30
}x
end
2015-12-23 02:04:40 +05:30
def self.link_reference_pattern
super("compare", /(?<commit_range>#{PATTERN})/)
end
2015-09-11 14:41:01 +05:30
# Initialize a CommitRange
#
# range_string - The String commit range.
# project - An optional Project model.
#
# Raises ArgumentError if `range_string` does not match `PATTERN`.
2015-12-23 02:04:40 +05:30
def initialize(range_string, project)
@project = project
2015-09-11 14:41:01 +05:30
range_string.strip!
2015-12-23 02:04:40 +05:30
unless range_string =~ /\A#{PATTERN}\z/
2015-09-11 14:41:01 +05:30
raise ArgumentError, "invalid CommitRange string format: #{range_string}"
end
2015-12-23 02:04:40 +05:30
@ref_from, @notation, @ref_to = range_string.split(/(\.{2,3})/, 2)
2015-09-11 14:41:01 +05:30
2015-12-23 02:04:40 +05:30
if project.valid_repo?
@commit_from = project.commit(@ref_from)
@commit_to = project.commit(@ref_to)
end
if valid_commits?
@ref_from = Commit.truncate_sha(sha_from) if sha_from.start_with?(@ref_from)
@ref_to = Commit.truncate_sha(sha_to) if sha_to.start_with?(@ref_to)
end
2015-09-11 14:41:01 +05:30
end
def inspect
%(#<#{self.class}:#{object_id} #{to_s}>)
end
def to_s
2015-12-23 02:04:40 +05:30
sha_from + notation + sha_to
2015-09-11 14:41:01 +05:30
end
2015-12-23 02:04:40 +05:30
alias_method :id, :to_s
2015-09-11 14:41:01 +05:30
def to_reference(from_project = nil)
2015-12-23 02:04:40 +05:30
if cross_project_reference?(from_project)
project.to_reference + self.class.reference_prefix + self.id
else
self.id
end
end
def reference_link_text(from_project = nil)
reference = ref_from + notation + ref_to
2015-09-11 14:41:01 +05:30
if cross_project_reference?(from_project)
2015-12-23 02:04:40 +05:30
reference = project.to_reference + self.class.reference_prefix + reference
2015-09-11 14:41:01 +05:30
end
reference
end
# Returns a String for use in a link's title attribute
def reference_title
2015-12-23 02:04:40 +05:30
"Commits #{sha_start} through #{sha_to}"
2015-09-11 14:41:01 +05:30
end
# Return a Hash of parameters for passing to a URL helper
#
# See `namespace_project_compare_url`
def to_param
2015-12-23 02:04:40 +05:30
{ from: sha_start, to: sha_to }
2015-09-11 14:41:01 +05:30
end
def exclude_start?
2015-12-23 02:04:40 +05:30
@notation == '..'
2015-09-11 14:41:01 +05:30
end
# Check if both the starting and ending commit IDs exist in a project's
# repository
2015-12-23 02:04:40 +05:30
def valid_commits?
commit_start.present? && commit_end.present?
2015-09-11 14:41:01 +05:30
end
def persisted?
true
end
2015-12-23 02:04:40 +05:30
def sha_from
return nil unless @commit_from
@commit_from.id
end
def sha_to
return nil unless @commit_to
@commit_to.id
2015-09-11 14:41:01 +05:30
end
2015-12-23 02:04:40 +05:30
def sha_start
return nil unless sha_from
exclude_start? ? sha_from + '^' : sha_from
2015-09-11 14:41:01 +05:30
end
2015-12-23 02:04:40 +05:30
def commit_start
return nil unless sha_start
2015-09-11 14:41:01 +05:30
2015-12-23 02:04:40 +05:30
if exclude_start?
@commit_start ||= project.commit(sha_start)
else
commit_from
end
2015-09-11 14:41:01 +05:30
end
2015-12-23 02:04:40 +05:30
alias_method :sha_end, :sha_to
alias_method :commit_end, :commit_to
2015-09-11 14:41:01 +05:30
end