Class: Grit::Tag

Inherits:
Ref
  • Object
show all
Defined in:
lib/grit/tag.rb

Instance Attribute Summary

Attributes inherited from Ref

#commit, #name

Class Method Summary collapse

Methods inherited from Ref

#initialize, #inspect

Constructor Details

This class inherits a constructor from Grit::Ref

Class Method Details

.commit_from_sha(repo, id) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/grit/tag.rb', line 52

def self.commit_from_sha(repo, id)
  git_ruby_repo = GitRuby::Repository.new(repo.path)
  object = git_ruby_repo.get_object_by_sha1(id)

  if object.type == :commit
    Commit.create(repo, :id => id)
  elsif object.type == :tag
    Commit.create(repo, :id => object.object)
  else
    raise "Unknown object type."
  end
end

.find_all(repo, options = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/grit/tag.rb', line 4

def self.find_all(repo, options = {})
  refs = []
  already = {}

  Dir.chdir(repo.path) do
    files = Dir.glob(prefix + '/**/*')

    files.each do |ref|
      next if !File.file?(ref)

      id = File.read(ref).chomp
      name = ref.sub("#{prefix}/", '')
      commit = commit_from_sha(repo, id)

      if !already[name]
        refs << self.new(name, commit)
        already[name] = true
      end
    end

    if File.file?('packed-refs')
      lines = File.readlines('packed-refs')
      lines.each_with_index do |line, i|
        if m = /^(\w{40}) (.*?)$/.match(line)
          next if !Regexp.new('^' + prefix).match(m[2])
          name = m[2].sub("#{prefix}/", '')

          # Annotated tags in packed-refs include a reference
          # to the commit object on the following line.
          next_line = lines[i+1]
          if next_line && next_line[0] == ?^
            commit = Commit.create(repo, :id => next_line[1..-1].chomp)
          else
            commit = commit_from_sha(repo, m[1])
          end

          if !already[name]
            refs << self.new(name, commit)
            already[name] = true
          end
        end
      end
    end
  end

  refs
end