Class: CommitRange

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Conversion, Referable
Defined in:
app/models/commit_range.rb

Overview

CommitRange makes it easier to work with commit ranges

Examples:

range = CommitRange.new('f3f85602...e86e1013', project)
range.exclude_start?  # => false
range.reference_title # => "Commits f3f85602 through e86e1013"
range.to_s            # => "f3f85602...e86e1013"

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"

# Assuming the specified project has a repository containing both commits:
range.valid_commits? # => true

Constant Summary collapse

REF_PATTERN =

The beginning and ending refs can be named or SHAs, and the range notation can be double- or triple-dot.

/[0-9a-zA-Z][0-9a-zA-Z_.-]*[0-9a-zA-Z\^]/
PATTERN =
/#{REF_PATTERN}\.{2,3}#{REF_PATTERN}/
STRICT_PATTERN =

In text references, the beginning and ending refs can only be SHAs between 7 and 40 hex characters.

/\h{7,40}\.{2,3}\h{7,40}/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(range_string, project) ⇒ CommitRange

Initialize a CommitRange

range_string - The String commit range. project - An optional Project model.

Raises ArgumentError if `range_string` does not match `PATTERN`.


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/models/commit_range.rb', line 62

def initialize(range_string, project)
  @project = project

  range_string.strip!

  unless range_string =~ /\A#{PATTERN}\z/
    raise ArgumentError, "invalid CommitRange string format: #{range_string}"
  end

  @ref_from, @notation, @ref_to = range_string.split(/(\.{2,3})/, 2)

  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
end

Instance Attribute Details

#commit_fromObject (readonly)

Returns the value of attribute commit_from


23
24
25
# File 'app/models/commit_range.rb', line 23

def commit_from
  @commit_from
end

#commit_toObject (readonly) Also known as: commit_end

Returns the value of attribute commit_to


23
24
25
# File 'app/models/commit_range.rb', line 23

def commit_to
  @commit_to
end

#notationObject (readonly)

Returns the value of attribute notation


23
24
25
# File 'app/models/commit_range.rb', line 23

def notation
  @notation
end

#projectObject

Optional Project model


27
28
29
# File 'app/models/commit_range.rb', line 27

def project
  @project
end

#ref_fromObject (readonly)

Returns the value of attribute ref_from


24
25
26
# File 'app/models/commit_range.rb', line 24

def ref_from
  @ref_from
end

#ref_toObject (readonly)

Returns the value of attribute ref_to


24
25
26
# File 'app/models/commit_range.rb', line 24

def ref_to
  @ref_to
end

Class Method Details


52
53
54
# File 'app/models/commit_range.rb', line 52

def self.link_reference_pattern
  @link_reference_pattern ||= super("compare", /(?<commit_range>#{PATTERN})/)
end

.reference_patternObject

Pattern used to extract commit range references from text

This pattern supports cross-project references.


45
46
47
48
49
50
# File 'app/models/commit_range.rb', line 45

def self.reference_pattern
  @reference_pattern ||= %r{
    (?:#{Project.reference_pattern}#{reference_prefix})?
    (?<commit_range>#{STRICT_PATTERN})
  }x
end

.reference_prefixObject


38
39
40
# File 'app/models/commit_range.rb', line 38

def self.reference_prefix
  '@'
end

Instance Method Details

#commit_startObject


156
157
158
159
160
161
162
163
164
# File 'app/models/commit_range.rb', line 156

def commit_start
  return nil unless sha_start

  if exclude_start?
    @commit_start ||= project.commit(sha_start)
  else
    commit_from
  end
end

#exclude_start?Boolean

Returns:

  • (Boolean)

124
125
126
# File 'app/models/commit_range.rb', line 124

def exclude_start?
  @notation == '..'
end

#inspectObject


84
85
86
# File 'app/models/commit_range.rb', line 84

def inspect
  %(#<#{self.class}:#{object_id} #{to_s}>)
end

#persisted?Boolean

Returns:

  • (Boolean)

134
135
136
# File 'app/models/commit_range.rb', line 134

def persisted?
  true
end

102
103
104
105
106
107
108
109
110
# File 'app/models/commit_range.rb', line 102

def reference_link_text(from_project = nil)
  reference = ref_from + notation + ref_to

  if cross_project_reference?(from_project)
    reference = project.to_reference + self.class.reference_prefix + reference
  end

  reference
end

#reference_titleObject

Returns a String for use in a link's title attribute


113
114
115
# File 'app/models/commit_range.rb', line 113

def reference_title
  "Commits #{sha_start} through #{sha_to}"
end

#sha_fromObject


138
139
140
141
142
# File 'app/models/commit_range.rb', line 138

def sha_from
  return nil unless @commit_from

  @commit_from.id
end

#sha_startObject


150
151
152
153
154
# File 'app/models/commit_range.rb', line 150

def sha_start
  return nil unless sha_from

  exclude_start? ? sha_from + '^' : sha_from
end

#sha_toObject Also known as: sha_end


144
145
146
147
148
# File 'app/models/commit_range.rb', line 144

def sha_to
  return nil unless @commit_to

  @commit_to.id
end

#to_paramObject

Return a Hash of parameters for passing to a URL helper

See `namespace_project_compare_url`


120
121
122
# File 'app/models/commit_range.rb', line 120

def to_param
  { from: sha_start, to: sha_to }
end

#to_reference(from_project = nil) ⇒ Object


94
95
96
97
98
99
100
# File 'app/models/commit_range.rb', line 94

def to_reference(from_project = nil)
  if cross_project_reference?(from_project)
    project.to_reference + self.class.reference_prefix + self.id
  else
    self.id
  end
end

#to_sObject Also known as: id


88
89
90
# File 'app/models/commit_range.rb', line 88

def to_s
  sha_from + notation + sha_to
end

#valid_commits?Boolean

Check if both the starting and ending commit IDs exist in a project's repository

Returns:

  • (Boolean)

130
131
132
# File 'app/models/commit_range.rb', line 130

def valid_commits?
  commit_start.present? && commit_end.present?
end