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