internal/loose,pack: test code to list all objects
[git/ruby-binding.git] / git.rb
blobd1d92fb306cf0164525159af69bb24d47e714bd0
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
15       Object.from_raw(r, self)
16     end
18     def get_raw_object_by_sha1(sha1)
19       sha1 = [sha1].pack("H*")
20       packs = Dir.glob(@git_dir + '/objects/pack/pack-*.pack')
22       # try packs
23       packs.each do |pack|
24         storage = Git::Internal::PackStorage.new(pack)
25         o = storage[sha1]
26         return o if o
27       end
29       # try loose storage
30       storage = Git::Internal::LooseStorage.new(@git_dir+'/objects')
31       o = storage[sha1]
32       return o if o
34       # try packs again, maybe the object got packed in the meantime
35       packs.each do |pack|
36         storage = Git::Internal::PackStorage.new(pack)
37         o = storage[sha1]
38         return o if o
39       end
41       return nil
42     end
43   end
44 end