2018-07-25 05:30:33 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-04-21 18:04:20 -04:00
|
|
|
# CommitRange makes it easier to work with commit ranges
|
|
|
|
#
|
|
|
|
# Examples:
|
|
|
|
#
|
2015-11-30 15:10:52 -05:00
|
|
|
# range = CommitRange.new('f3f85602...e86e1013', project)
|
2015-04-25 14:58:49 -04:00
|
|
|
# range.exclude_start? # => false
|
|
|
|
# range.to_s # => "f3f85602...e86e1013"
|
2015-04-21 18:04:20 -04:00
|
|
|
#
|
2015-11-30 15:10:52 -05:00
|
|
|
# range = CommitRange.new('f3f856029bc5f966c5a7ee24cf7efefdd20e6019..e86e1013709735be5bb767e2b228930c543f25ae', project)
|
2015-04-25 14:58:49 -04:00
|
|
|
# range.exclude_start? # => true
|
|
|
|
# range.to_param # => {from: "f3f856029bc5f966c5a7ee24cf7efefdd20e6019^", to: "e86e1013709735be5bb767e2b228930c543f25ae"}
|
|
|
|
# range.to_s # => "f3f85602..e86e1013"
|
2015-04-21 18:04:20 -04:00
|
|
|
#
|
2015-12-01 06:49:22 -05:00
|
|
|
# # Assuming the specified project has a repository containing both commits:
|
2015-04-21 18:04:20 -04:00
|
|
|
# range.valid_commits? # => true
|
|
|
|
#
|
|
|
|
class CommitRange
|
|
|
|
include ActiveModel::Conversion
|
2015-05-02 23:11:21 -04:00
|
|
|
include Referable
|
2015-04-21 18:04:20 -04:00
|
|
|
|
2015-11-30 15:10:52 -05:00
|
|
|
attr_reader :commit_from, :notation, :commit_to
|
2015-11-30 15:36:13 -05:00
|
|
|
attr_reader :ref_from, :ref_to
|
2015-04-21 18:04:20 -04:00
|
|
|
|
2016-06-04 11:15:08 -04:00
|
|
|
# The Project model
|
2015-04-21 18:04:20 -04:00
|
|
|
attr_accessor :project
|
|
|
|
|
2015-11-30 15:36:13 -05:00
|
|
|
# The beginning and ending refs can be named or SHAs, and
|
2015-05-14 16:59:39 -04:00
|
|
|
# the range notation can be double- or triple-dot.
|
2015-11-30 15:36:13 -05:00
|
|
|
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-01-29 19:49:07 -05:00
|
|
|
# between 7 and 40 hex characters.
|
|
|
|
STRICT_PATTERN = /\h{7,40}\.{2,3}\h{7,40}/
|
2015-04-21 18:04:20 -04:00
|
|
|
|
2015-05-14 16:59:39 -04:00
|
|
|
def self.reference_prefix
|
|
|
|
'@'
|
|
|
|
end
|
|
|
|
|
|
|
|
# Pattern used to extract commit range references from text
|
|
|
|
#
|
|
|
|
# This pattern supports cross-project references.
|
|
|
|
def self.reference_pattern
|
2016-03-24 11:41:48 -04:00
|
|
|
@reference_pattern ||= %r{
|
2015-12-01 09:51:27 -05:00
|
|
|
(?:#{Project.reference_pattern}#{reference_prefix})?
|
|
|
|
(?<commit_range>#{STRICT_PATTERN})
|
2015-05-14 16:59:39 -04:00
|
|
|
}x
|
|
|
|
end
|
|
|
|
|
2015-11-30 15:14:46 -05:00
|
|
|
def self.link_reference_pattern
|
2016-03-24 11:41:48 -04:00
|
|
|
@link_reference_pattern ||= super("compare", /(?<commit_range>#{PATTERN})/)
|
2015-11-30 15:14:46 -05:00
|
|
|
end
|
|
|
|
|
2015-04-21 18:04:20 -04:00
|
|
|
# Initialize a CommitRange
|
|
|
|
#
|
|
|
|
# range_string - The String commit range.
|
2016-06-04 11:15:08 -04:00
|
|
|
# project - The Project model.
|
2015-04-21 18:04:20 -04:00
|
|
|
#
|
|
|
|
# Raises ArgumentError if `range_string` does not match `PATTERN`.
|
2015-11-30 15:10:52 -05:00
|
|
|
def initialize(range_string, project)
|
|
|
|
@project = project
|
|
|
|
|
2016-05-02 12:15:25 -04:00
|
|
|
range_string = range_string.strip
|
2015-04-21 18:04:20 -04:00
|
|
|
|
2015-12-01 06:49:22 -05:00
|
|
|
unless range_string =~ /\A#{PATTERN}\z/
|
2015-04-21 18:04:20 -04:00
|
|
|
raise ArgumentError, "invalid CommitRange string format: #{range_string}"
|
|
|
|
end
|
|
|
|
|
2015-11-30 15:36:13 -05:00
|
|
|
@ref_from, @notation, @ref_to = range_string.split(/(\.{2,3})/, 2)
|
2015-04-21 18:04:20 -04:00
|
|
|
|
2015-11-30 15:10:52 -05:00
|
|
|
if project.valid_repo?
|
2015-11-30 15:36:13 -05:00
|
|
|
@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)
|
2015-11-30 15:10:52 -05:00
|
|
|
end
|
2015-04-21 18:04:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def inspect
|
2016-10-03 10:09:57 -04:00
|
|
|
%(#<#{self.class}:#{object_id} #{self}>)
|
2015-04-21 18:04:20 -04:00
|
|
|
end
|
|
|
|
|
2015-04-23 13:52:35 -04:00
|
|
|
def to_s
|
2015-11-30 15:10:52 -05:00
|
|
|
sha_from + notation + sha_to
|
2015-04-21 18:04:20 -04:00
|
|
|
end
|
|
|
|
|
2015-11-30 15:10:52 -05:00
|
|
|
alias_method :id, :to_s
|
|
|
|
|
2017-11-22 08:20:35 -05:00
|
|
|
def to_reference(from = nil, full: false)
|
|
|
|
project_reference = project.to_reference(from, full: full)
|
2016-11-02 19:49:13 -04:00
|
|
|
|
|
|
|
if project_reference.present?
|
|
|
|
project_reference + self.class.reference_prefix + self.id
|
2015-12-01 06:58:45 -05:00
|
|
|
else
|
|
|
|
self.id
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-11-22 08:20:35 -05:00
|
|
|
def reference_link_text(from = nil)
|
|
|
|
project_reference = project.to_reference(from)
|
2016-11-02 19:49:13 -04:00
|
|
|
reference = ref_from + notation + ref_to
|
2015-05-12 17:58:29 -04:00
|
|
|
|
2016-11-02 19:49:13 -04:00
|
|
|
if project_reference.present?
|
|
|
|
project_reference + self.class.reference_prefix + reference
|
|
|
|
else
|
|
|
|
reference
|
2015-05-02 23:11:21 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-04-21 18:04:20 -04:00
|
|
|
# Return a Hash of parameters for passing to a URL helper
|
|
|
|
#
|
|
|
|
# See `namespace_project_compare_url`
|
|
|
|
def to_param
|
2015-11-30 15:10:52 -05:00
|
|
|
{ from: sha_start, to: sha_to }
|
2015-04-21 18:04:20 -04:00
|
|
|
end
|
|
|
|
|
2015-04-25 14:58:49 -04:00
|
|
|
def exclude_start?
|
2015-11-30 15:36:13 -05:00
|
|
|
@notation == '..'
|
2015-04-21 18:04:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Check if both the starting and ending commit IDs exist in a project's
|
|
|
|
# repository
|
2015-11-30 15:10:52 -05:00
|
|
|
def valid_commits?
|
|
|
|
commit_start.present? && commit_end.present?
|
2015-04-21 18:04:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def persisted?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2015-11-30 15:10:52 -05:00
|
|
|
def sha_from
|
2019-02-08 07:19:53 -05:00
|
|
|
return unless @commit_from
|
2015-11-30 15:10:52 -05:00
|
|
|
|
|
|
|
@commit_from.id
|
2015-04-23 13:43:21 -04:00
|
|
|
end
|
2015-04-21 18:04:20 -04:00
|
|
|
|
2015-11-30 15:10:52 -05:00
|
|
|
def sha_to
|
2019-02-08 07:19:53 -05:00
|
|
|
return unless @commit_to
|
2015-11-30 15:10:52 -05:00
|
|
|
|
|
|
|
@commit_to.id
|
|
|
|
end
|
|
|
|
|
|
|
|
def sha_start
|
2019-02-08 07:19:53 -05:00
|
|
|
return unless sha_from
|
2015-11-30 15:10:52 -05:00
|
|
|
|
|
|
|
exclude_start? ? sha_from + '^' : sha_from
|
2015-04-21 18:04:20 -04:00
|
|
|
end
|
|
|
|
|
2015-11-30 15:10:52 -05:00
|
|
|
def commit_start
|
2019-02-08 07:19:53 -05:00
|
|
|
return unless sha_start
|
2015-04-21 18:04:20 -04:00
|
|
|
|
2015-11-30 15:10:52 -05:00
|
|
|
if exclude_start?
|
|
|
|
@commit_start ||= project.commit(sha_start)
|
|
|
|
else
|
|
|
|
commit_from
|
|
|
|
end
|
2015-04-21 18:04:20 -04:00
|
|
|
end
|
2015-11-30 15:10:52 -05:00
|
|
|
|
|
|
|
alias_method :sha_end, :sha_to
|
|
|
|
alias_method :commit_end, :commit_to
|
2015-04-21 18:04:20 -04:00
|
|
|
end
|