From a8e6656481fecce33339ab1e9ad42a6b9f0ed1bf Mon Sep 17 00:00:00 2001 From: Robert Speicher Date: Tue, 21 Apr 2015 18:04:20 -0400 Subject: Add CommitRange class --- app/models/commit_range.rb | 128 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 app/models/commit_range.rb (limited to 'app/models/commit_range.rb') diff --git a/app/models/commit_range.rb b/app/models/commit_range.rb new file mode 100644 index 00000000000..b3712aed056 --- /dev/null +++ b/app/models/commit_range.rb @@ -0,0 +1,128 @@ +# CommitRange makes it easier to work with commit ranges +# +# Examples: +# +# range = CommitRange.new('f3f85602...e86e1013') +# range.inclusive? # => false +# range.to_s # => "f3f85602...e86e1013" +# range.reference_title # => "Commits f3f85602 through e86e1013" +# +# range = CommitRange.new('f3f856029bc5f966c5a7ee24cf7efefdd20e6019..e86e1013709735be5bb767e2b228930c543f25ae') +# range.inclusive? # => true +# range.to_s # => "f3f85602..e86e1013" +# range.to_param # => {from: "f3f856029bc5f966c5a7ee24cf7efefdd20e6019^", to: "e86e1013709735be5bb767e2b228930c543f25ae"} +# +# # Assuming `project` is a Project with a repository containing both commits: +# range.project = project +# range.valid_commits? # => true +# range.to_a # => [#, #] +# +class CommitRange + include ActiveModel::Conversion + + attr_reader :sha_from, :notation, :sha_to + + # Optional Project model + attr_accessor :project + + # See `inclusive?` + attr_reader :inclusive + + # 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 + + @inclusive = range_string !~ /\.{3}/ + @sha_from, @notation, @sha_to = range_string.split(/(\.{2,3})/, 2) + + @project = project + + @_commit_map = {} + end + + def inspect + %(#<#{self.class}:#{object_id} #{to_s}>) + end + + # Returns an Array of Commit objects, where the first value is the starting + # commit, and the second value is the ending commit + # + # Returns `[nil, nil]` if `valid_commits?` is falsey + def to_a + if valid_commits? + [commit(sha_from), commit(sha_to)] + else + [nil, nil] + end + end + + def to_s(short: true) + if short + "#{sha_from[0..7]}#{notation}#{sha_to[0..7]}" + else + "#{sha_from}#{notation}#{sha_to}" + end + end + + # Returns a String for use in a link's title attribute + def reference_title + "Commits #{sha_from} through #{sha_to}" + end + + # Return a Hash of parameters for passing to a URL helper + # + # See `namespace_project_compare_url` + def to_param + { from: sha_from_as_param, to: sha_to } + end + + # Check if the range is inclusive + # + # We consider a CommitRange "inclusive" when it uses the two-dot syntax. + def inclusive? + inclusive + 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? + + commit(sha_from).present? && commit(sha_to).present? + end + + def persisted? + true + end + + private + + def sha_from_as_param + sha_from + (inclusive? ? '^' : '') + end + + def commit(sha) + unless @_commit_map[sha] + # FIXME (rspeicher): Law of Demeter + @_commit_map[sha] = project.repository.commit(sha) + end + + @_commit_map[sha] + end +end -- cgit v1.2.1 From 81a21e57961356a0924b7b1529696f79eded4630 Mon Sep 17 00:00:00 2001 From: Robert Speicher Date: Thu, 23 Apr 2015 13:43:21 -0400 Subject: CommitRange improvements --- app/models/commit_range.rb | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) (limited to 'app/models/commit_range.rb') diff --git a/app/models/commit_range.rb b/app/models/commit_range.rb index b3712aed056..19b1a218e45 100644 --- a/app/models/commit_range.rb +++ b/app/models/commit_range.rb @@ -45,12 +45,10 @@ class CommitRange raise ArgumentError, "invalid CommitRange string format: #{range_string}" end - @inclusive = range_string !~ /\.{3}/ + @inclusive = !range_string.include?('...') @sha_from, @notation, @sha_to = range_string.split(/(\.{2,3})/, 2) @project = project - - @_commit_map = {} end def inspect @@ -63,7 +61,7 @@ class CommitRange # Returns `[nil, nil]` if `valid_commits?` is falsey def to_a if valid_commits? - [commit(sha_from), commit(sha_to)] + [commit_from, commit_to] else [nil, nil] end @@ -104,25 +102,24 @@ class CommitRange return nil unless project.present? return false unless project.valid_repo? - commit(sha_from).present? && commit(sha_to).present? + commit_from.present? && commit_to.present? end def persisted? true end - private + def commit_from + @commit_from ||= project.repository.commit(sha_from_as_param) + end - def sha_from_as_param - sha_from + (inclusive? ? '^' : '') + def commit_to + @commit_to ||= project.repository.commit(sha_to) end - def commit(sha) - unless @_commit_map[sha] - # FIXME (rspeicher): Law of Demeter - @_commit_map[sha] = project.repository.commit(sha) - end + private - @_commit_map[sha] + def sha_from_as_param + sha_from + (inclusive? ? '^' : '') end end -- cgit v1.2.1 From c11d3c5789f2d77059b6d132af7b9fda9651ede5 Mon Sep 17 00:00:00 2001 From: Robert Speicher Date: Thu, 23 Apr 2015 13:52:35 -0400 Subject: Remove param from CommitRange#to_s --- app/models/commit_range.rb | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) (limited to 'app/models/commit_range.rb') diff --git a/app/models/commit_range.rb b/app/models/commit_range.rb index 19b1a218e45..66daa072c9e 100644 --- a/app/models/commit_range.rb +++ b/app/models/commit_range.rb @@ -67,12 +67,8 @@ class CommitRange end end - def to_s(short: true) - if short - "#{sha_from[0..7]}#{notation}#{sha_to[0..7]}" - else - "#{sha_from}#{notation}#{sha_to}" - end + def to_s + "#{sha_from[0..7]}#{notation}#{sha_to[0..7]}" end # Returns a String for use in a link's title attribute -- cgit v1.2.1 From 2403a28b3f201702b252b88ed6020a0c3f8511bf Mon Sep 17 00:00:00 2001 From: Robert Speicher Date: Thu, 23 Apr 2015 14:00:23 -0400 Subject: Include caret in CommitRange#reference_title --- app/models/commit_range.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'app/models/commit_range.rb') diff --git a/app/models/commit_range.rb b/app/models/commit_range.rb index 66daa072c9e..dce1bfcc198 100644 --- a/app/models/commit_range.rb +++ b/app/models/commit_range.rb @@ -73,7 +73,7 @@ class CommitRange # Returns a String for use in a link's title attribute def reference_title - "Commits #{sha_from} through #{sha_to}" + "Commits #{sha_from_as_param} through #{sha_to}" end # Return a Hash of parameters for passing to a URL helper -- cgit v1.2.1 From 92c681a53c5565eddae51dc0bede07541c3eb0c5 Mon Sep 17 00:00:00 2001 From: Robert Speicher Date: Sat, 25 Apr 2015 14:45:56 -0400 Subject: Remove CommitRange#to_a --- app/models/commit_range.rb | 13 ------------- 1 file changed, 13 deletions(-) (limited to 'app/models/commit_range.rb') diff --git a/app/models/commit_range.rb b/app/models/commit_range.rb index dce1bfcc198..1b4b12e70e9 100644 --- a/app/models/commit_range.rb +++ b/app/models/commit_range.rb @@ -15,7 +15,6 @@ # # Assuming `project` is a Project with a repository containing both commits: # range.project = project # range.valid_commits? # => true -# range.to_a # => [#, #] # class CommitRange include ActiveModel::Conversion @@ -55,18 +54,6 @@ class CommitRange %(#<#{self.class}:#{object_id} #{to_s}>) end - # Returns an Array of Commit objects, where the first value is the starting - # commit, and the second value is the ending commit - # - # Returns `[nil, nil]` if `valid_commits?` is falsey - def to_a - if valid_commits? - [commit_from, commit_to] - else - [nil, nil] - end - end - def to_s "#{sha_from[0..7]}#{notation}#{sha_to[0..7]}" end -- cgit v1.2.1 From 165cacce163366fa3b362bb5005ed9ede346a8e1 Mon Sep 17 00:00:00 2001 From: Robert Speicher Date: Sat, 25 Apr 2015 14:58:49 -0400 Subject: Rename `CommitRange#inclusive?` to `#exclude_start?` --- app/models/commit_range.rb | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) (limited to 'app/models/commit_range.rb') diff --git a/app/models/commit_range.rb b/app/models/commit_range.rb index 1b4b12e70e9..67ea4155aec 100644 --- a/app/models/commit_range.rb +++ b/app/models/commit_range.rb @@ -3,14 +3,15 @@ # Examples: # # range = CommitRange.new('f3f85602...e86e1013') -# range.inclusive? # => false -# range.to_s # => "f3f85602...e86e1013" +# range.exclude_start? # => false # range.reference_title # => "Commits f3f85602 through e86e1013" +# range.to_s # => "f3f85602...e86e1013" # # range = CommitRange.new('f3f856029bc5f966c5a7ee24cf7efefdd20e6019..e86e1013709735be5bb767e2b228930c543f25ae') -# range.inclusive? # => true -# range.to_s # => "f3f85602..e86e1013" -# range.to_param # => {from: "f3f856029bc5f966c5a7ee24cf7efefdd20e6019^", to: "e86e1013709735be5bb767e2b228930c543f25ae"} +# range.exclude_start? # => true +# range.reference_title # => "Commits f3f85602^ through e86e1013" +# range.to_param # => {from: "f3f856029bc5f966c5a7ee24cf7efefdd20e6019^", to: "e86e1013709735be5bb767e2b228930c543f25ae"} +# range.to_s # => "f3f85602..e86e1013" # # # Assuming `project` is a Project with a repository containing both commits: # range.project = project @@ -24,8 +25,8 @@ class CommitRange # Optional Project model attr_accessor :project - # See `inclusive?` - attr_reader :inclusive + # See `exclude_start?` + attr_reader :exclude_start # The beginning and ending SHA sums can be between 6 and 40 hex characters, # and the range selection can be double- or triple-dot. @@ -44,7 +45,7 @@ class CommitRange raise ArgumentError, "invalid CommitRange string format: #{range_string}" end - @inclusive = !range_string.include?('...') + @exclude_start = !range_string.include?('...') @sha_from, @notation, @sha_to = range_string.split(/(\.{2,3})/, 2) @project = project @@ -70,11 +71,8 @@ class CommitRange { from: sha_from_as_param, to: sha_to } end - # Check if the range is inclusive - # - # We consider a CommitRange "inclusive" when it uses the two-dot syntax. - def inclusive? - inclusive + def exclude_start? + exclude_start end # Check if both the starting and ending commit IDs exist in a project's @@ -103,6 +101,6 @@ class CommitRange private def sha_from_as_param - sha_from + (inclusive? ? '^' : '') + sha_from + (exclude_start? ? '^' : '') end end -- cgit v1.2.1 From 682ec038ac686cd8e0e7161a4a576dc764b61b11 Mon Sep 17 00:00:00 2001 From: Robert Speicher Date: Sat, 25 Apr 2015 14:59:47 -0400 Subject: Rename `CommitRange#sha_from_as_param` to `#suffixed_sha_from` --- app/models/commit_range.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'app/models/commit_range.rb') diff --git a/app/models/commit_range.rb b/app/models/commit_range.rb index 67ea4155aec..e6456198264 100644 --- a/app/models/commit_range.rb +++ b/app/models/commit_range.rb @@ -61,14 +61,14 @@ class CommitRange # Returns a String for use in a link's title attribute def reference_title - "Commits #{sha_from_as_param} through #{sha_to}" + "Commits #{suffixed_sha_from} through #{sha_to}" end # Return a Hash of parameters for passing to a URL helper # # See `namespace_project_compare_url` def to_param - { from: sha_from_as_param, to: sha_to } + { from: suffixed_sha_from, to: sha_to } end def exclude_start? @@ -91,7 +91,7 @@ class CommitRange end def commit_from - @commit_from ||= project.repository.commit(sha_from_as_param) + @commit_from ||= project.repository.commit(suffixed_sha_from) end def commit_to @@ -100,7 +100,7 @@ class CommitRange private - def sha_from_as_param + def suffixed_sha_from sha_from + (exclude_start? ? '^' : '') end end -- cgit v1.2.1