version: introduce HeadVersion

This commit is contained in:
Vlad Shablinsky 2016-06-18 20:33:03 +03:00 committed by Xu Cheng
parent 90e84453f9
commit 80489dcb49
No known key found for this signature in database
GPG Key ID: C2A3860FA0B459CE

View File

@ -179,6 +179,18 @@ class Version
end
end
def self.create(val)
unless val.respond_to?(:to_str)
raise TypeError, "Version value must be a string; got a #{val.class} (#{val})"
end
if val.to_str.start_with?("HEAD")
HeadVersion.new(val)
else
Version.new(val)
end
end
def initialize(val)
if val.respond_to?(:to_str)
@version = val.to_str
@ -192,7 +204,7 @@ class Version
end
def head?
version == "HEAD"
false
end
def <=>(other)
@ -200,6 +212,7 @@ class Version
return 0 if version == other.version
return 1 if head? && !other.head?
return -1 if !head? && other.head?
return 0 if head? && other.head?
ltokens = tokens
rtokens = other.tokens
@ -379,3 +392,25 @@ class Version
return m.captures.first unless m.nil?
end
end
class HeadVersion < Version
attr_reader :commit
def initialize(val)
super
@commit = @version[/^HEAD-(.+)$/, 1]
end
def update_commit(commit)
@commit = commit
@version = if commit
"HEAD-#{commit}"
else
"HEAD"
end
end
def head?
true
end
end