Class: Revision

Inherits:
Object
  • Object
show all
Defined in:
lib/revision.rb

Defined Under Namespace

Classes: Ancestor, HintedError, Parent, Ref, Upstream

Constant Summary collapse

InvalidObject =
Class.new(StandardError)
INVALID_NAME =
/
  ^\.
| \/\.
| \.\.
| \/$
| \.lock$
| @\{
| [\x00-\x20*:?\[\\^~\x7f]
/x
PARENT =
/^(.+)\^(\d*)$/
ANCESTOR =
/^(.+)~(\d+)$/
UPSTREAM =
/^(.*)@\{u(pstream)?\}$/i
HEAD =
"HEAD"
REF_ALIASES =
{
  "@" => HEAD,
  ""  => HEAD
}
COMMIT =
"commit"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repo, expression) ⇒ Revision

Returns a new instance of Revision.



78
79
80
81
82
83
# File 'lib/revision.rb', line 78

def initialize(repo, expression)
  @repo   = repo
  @expr   = expression
  @query  = Revision.parse(@expr)
  @errors = []
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



76
77
78
# File 'lib/revision.rb', line 76

def errors
  @errors
end

Class Method Details

.parse(revision) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/revision.rb', line 55

def self.parse(revision)
  if match = PARENT.match(revision)
    rev = Revision.parse(match[1])
    n = (match[2] == "") ? 1 : match[2].to_i
    rev ? Parent.new(rev, n) : nil
  elsif match = ANCESTOR.match(revision)
    rev = Revision.parse(match[1])
    rev ? Ancestor.new(rev, match[2].to_i) : nil
  elsif match = UPSTREAM.match(revision)
    rev = Revision.parse(match[1])
    rev ? Upstream.new(rev) : nil
  elsif Revision.valid_ref?(revision)
    name = REF_ALIASES[revision] || revision
    Ref.new(name)
  end
end

.valid_ref?(revision) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/revision.rb', line 72

def self.valid_ref?(revision)
  INVALID_NAME =~ revision ? false : true
end

Instance Method Details

#commit_parent(oid, n = 1) ⇒ Object



108
109
110
111
112
113
114
115
# File 'lib/revision.rb', line 108

def commit_parent(oid, n = 1)
  return nil unless oid

  commit = load_typed_object(oid, COMMIT)
  return nil unless commit

  commit.parents[n - 1]
end

#read_ref(name) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/revision.rb', line 94

def read_ref(name)
  oid = @repo.refs.read_ref(name)
  return oid if oid

  candidates = @repo.database.prefix_match(name)
  return candidates.first if candidates.size == 1

  if candidates.size > 1
    log_ambiguous_sha1(name, candidates)
  end

  nil
end

#resolve(type = nil) ⇒ Object

Raises:



85
86
87
88
89
90
91
92
# File 'lib/revision.rb', line 85

def resolve(type = nil)
  oid = @query&.resolve(self)
  oid = nil if type and not load_typed_object(oid, type)

  return oid if oid

  raise InvalidObject, "Not a valid object name: '#{ @expr }'."
end

#upstream(branch) ⇒ Object



117
118
119
120
# File 'lib/revision.rb', line 117

def upstream(branch)
  branch = @repo.refs.current_ref.short_name if branch == HEAD
  @repo.remotes.get_upstream(branch)
end