Class: Pkg::Util::Git_tag

Inherits:
Object
  • Object
show all
Defined in:
lib/packaging/util/git_tags.rb

Constant Summary collapse

GIT =
Pkg::Util::Tool::GIT
DEVNULL =
Pkg::Util::OS::DEVNULL
SHA1 =

A SHA1 sum is 20 characters long, but Git will match on the first ~8 or so. And 8 is long enough for fun test sums like ‘cafebeef’ or ‘deadfeed`.

/[0-9A-F]{8,20}/i

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(address, reference) ⇒ Git_tag

Returns a new instance of Git_tag.



13
14
15
16
17
# File 'lib/packaging/util/git_tags.rb', line 13

def initialize(address, reference)
  @address = address
  @ref = reference
  parse_ref!
end

Instance Attribute Details

#addressObject (readonly)

Returns the value of attribute address.



3
4
5
# File 'lib/packaging/util/git_tags.rb', line 3

def address
  @address
end

#refObject (readonly)

Returns the value of attribute ref.



3
4
5
# File 'lib/packaging/util/git_tags.rb', line 3

def ref
  @ref
end

#ref_nameObject (readonly)

Returns the value of attribute ref_name.



3
4
5
# File 'lib/packaging/util/git_tags.rb', line 3

def ref_name
  @ref_name
end

#ref_typeObject (readonly)

Returns the value of attribute ref_type.



3
4
5
# File 'lib/packaging/util/git_tags.rb', line 3

def ref_type
  @ref_type
end

Instance Method Details

#branch?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/packaging/util/git_tags.rb', line 61

def branch?
  ref_type.downcase == "heads"
end

#branch_nameObject



52
53
54
# File 'lib/packaging/util/git_tags.rb', line 52

def branch_name
  branch? ? ref_name : nil
end

#fetch_full_refObject

Fetch the full ref using ls-remote, this should raise an error if it returns non-zero because that means this ref doesn’t exist in the repo



45
46
47
48
49
50
# File 'lib/packaging/util/git_tags.rb', line 45

def fetch_full_ref
  stdout, = Pkg::Util::Execution.capture3("#{GIT} ls-remote --tags --heads --exit-code #{address} #{ref}")
  stdout.split.last
rescue RuntimeError => e
  raise "ERROR : Not a ref or sha!\n#{e}"
end

#parse_ref!Object

Parse ref in one of three ways: if the ref is already in a good format just grab the ref type from the string. if it’s not, check if it’s a sha, if that is true then list it as a sha. finally if it’s neither of those fetch the full ref and parse that.



23
24
25
26
27
28
29
30
31
# File 'lib/packaging/util/git_tags.rb', line 23

def parse_ref!
  if ref?
    split_ref(ref)
  elsif sha?
    @ref_type = "sha"
  else
    split_ref(fetch_full_ref)
  end
end

#ref?Boolean

Returns:

  • (Boolean)


56
57
58
59
# File 'lib/packaging/util/git_tags.rb', line 56

def ref?
  %x(#{GIT} check-ref-format #{ref} >#{DEVNULL} 2>&1)
  $?.success?
end

#sha?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/packaging/util/git_tags.rb', line 69

def sha?
  !!(ref =~ SHA1)
end

#split_ref(ref) ⇒ Object

Split the ref based on slashes, set ref_name and ref_type based on the last two items from the split. i.e. refs/tags/1.1.1 would return:

@ref_name => 1.1.1       @ref_type => tags


36
37
38
39
40
41
# File 'lib/packaging/util/git_tags.rb', line 36

def split_ref(ref)
  ref_parts = ref.split('/', 3)
  @ref_name = ref_parts.pop
  @ref_type = ref_parts.pop
  [@ref_type, @ref_name]
end

#tag?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/packaging/util/git_tags.rb', line 65

def tag?
  ref_type.downcase == "tags"
end