Don't qualify with .rb extension.
[git/ruby-binding.git] / git.rb
blob71f9b9ef9a09302208b877acf6b85c548623695b
1 require 'git/internal/object'
2 require 'git/internal/pack'
3 require 'git/internal/loose'
5 module Git
6   class Repository
7     def initialize(git_dir)
8       @git_dir = git_dir
9     end
11     def get_object_by_sha1(sha1)
12       r = get_raw_object_by_sha1(sha1)
13       return nil if !r
15       case r.type
16       when :blob
17         return Blob.new(r, self)
18       when :tree
19         return Tree.new(r, self)
20       when :commit
21         return Commit.new(r, self)
22       when :tag
23         return Tag.new(r, self)
24       else
25         raise RuntimeError, "got invalid object-type"
26       end
27     end
29     def get_raw_object_by_sha1(sha1)
30       sha1 = [sha1].pack("H*")
31       packs = Dir.glob(@git_dir + '/objects/pack/pack-*.pack')
33       # try packs
34       packs.each do |pack|
35         storage = Git::Internal::PackStorage.new(pack)
36         o = storage[sha1]
37         return o if o
38       end
40       # try loose storage
41       storage = Git::Internal::LooseStorage.new(@git_dir+'/objects')
42       o = storage[sha1]
43       return o if o
45       # try packs again, maybe the object got packed in the meantime
46       packs.each do |pack|
47         storage = Git::Internal::PackStorage.new(pack)
48         o = storage[sha1]
49         return o if o
50       end
52       return nil
53     end
54   end
55 end