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.



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

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.



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

def errors
  @errors
end

Class Method Details

.parse(revision) ⇒ Object



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

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)


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

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

Instance Method Details

#commit_parent(oid, n = 1) ⇒ Object



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

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



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

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:



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

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



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

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