2015-04-21 18:04:20 -04:00
|
|
|
# CommitRange makes it easier to work with commit ranges
|
|
|
|
#
|
|
|
|
# Examples:
|
|
|
|
#
|
|
|
|
# range = CommitRange.new('f3f85602...e86e1013')
|
2015-04-25 14:58:49 -04:00
|
|
|
# range.exclude_start? # => false
|
2015-04-21 18:04:20 -04:00
|
|
|
# range.reference_title # => "Commits f3f85602 through e86e1013"
|
2015-04-25 14:58:49 -04:00
|
|
|
# range.to_s # => "f3f85602...e86e1013"
|
2015-04-21 18:04:20 -04:00
|
|
|
#
|
|
|
|
# range = CommitRange.new('f3f856029bc5f966c5a7ee24cf7efefdd20e6019..e86e1013709735be5bb767e2b228930c543f25ae')
|
2015-04-25 14:58:49 -04:00
|
|
|
# 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 18:04:20 -04:00
|
|
|
#
|
|
|
|
# # Assuming `project` is a Project with a repository containing both commits:
|
|
|
|
# range.project = project
|
|
|
|
# range.valid_commits? # => true
|
|
|
|
#
|
|
|
|
class CommitRange
|
|
|
|
include ActiveModel::Conversion
|
|
|
|
|
|
|
|
attr_reader :sha_from, :notation, :sha_to
|
|
|
|
|
|
|
|
# Optional Project model
|
|
|
|
attr_accessor :project
|
|
|
|
|
2015-04-25 14:58:49 -04:00
|
|
|
# See `exclude_start?`
|
|
|
|
attr_reader :exclude_start
|
2015-04-21 18:04:20 -04:00
|
|
|
|
|
|
|
# The beginning and ending SHA sums can be between 6 and 40 hex characters,
|
|
|
|
# and the range selection can be double- or triple-dot.
|
|
|
|
PATTERN = /\h{6,40}\.{2,3}\h{6,40}/
|
|
|
|
|
|
|
|
# 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 = nil)
|
|
|
|
range_string.strip!
|
|
|
|
|
|
|
|
unless range_string.match(/\A#{PATTERN}\z/)
|
|
|
|
raise ArgumentError, "invalid CommitRange string format: #{range_string}"
|
|
|
|
end
|
|
|
|
|
2015-04-25 14:58:49 -04:00
|
|
|
@exclude_start = !range_string.include?('...')
|
2015-04-21 18:04:20 -04:00
|
|
|
@sha_from, @notation, @sha_to = range_string.split(/(\.{2,3})/, 2)
|
|
|
|
|
|
|
|
@project = project
|
|
|
|
end
|
|
|
|
|
|
|
|
def inspect
|
|
|
|
%(#<#{self.class}:#{object_id} #{to_s}>)
|
|
|
|
end
|
|
|
|
|
2015-04-23 13:52:35 -04:00
|
|
|
def to_s
|
|
|
|
"#{sha_from[0..7]}#{notation}#{sha_to[0..7]}"
|
2015-04-21 18:04:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Returns a String for use in a link's title attribute
|
|
|
|
def reference_title
|
2015-04-25 14:59:47 -04:00
|
|
|
"Commits #{suffixed_sha_from} through #{sha_to}"
|
2015-04-21 18:04:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Return a Hash of parameters for passing to a URL helper
|
|
|
|
#
|
|
|
|
# See `namespace_project_compare_url`
|
|
|
|
def to_param
|
2015-04-25 14:59:47 -04:00
|
|
|
{ from: suffixed_sha_from, to: sha_to }
|
2015-04-21 18:04:20 -04:00
|
|
|
end
|
|
|
|
|
2015-04-25 14:58:49 -04:00
|
|
|
def exclude_start?
|
|
|
|
exclude_start
|
2015-04-21 18:04:20 -04: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?(project = project)
|
|
|
|
return nil unless project.present?
|
|
|
|
return false unless project.valid_repo?
|
|
|
|
|
2015-04-23 13:43:21 -04:00
|
|
|
commit_from.present? && commit_to.present?
|
2015-04-21 18:04:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def persisted?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2015-04-23 13:43:21 -04:00
|
|
|
def commit_from
|
2015-04-25 14:59:47 -04:00
|
|
|
@commit_from ||= project.repository.commit(suffixed_sha_from)
|
2015-04-23 13:43:21 -04:00
|
|
|
end
|
2015-04-21 18:04:20 -04:00
|
|
|
|
2015-04-23 13:43:21 -04:00
|
|
|
def commit_to
|
|
|
|
@commit_to ||= project.repository.commit(sha_to)
|
2015-04-21 18:04:20 -04:00
|
|
|
end
|
|
|
|
|
2015-04-23 13:43:21 -04:00
|
|
|
private
|
2015-04-21 18:04:20 -04:00
|
|
|
|
2015-04-25 14:59:47 -04:00
|
|
|
def suffixed_sha_from
|
2015-04-25 14:58:49 -04:00
|
|
|
sha_from + (exclude_start? ? '^' : '')
|
2015-04-21 18:04:20 -04:00
|
|
|
end
|
|
|
|
end
|