git.rb: whitespace fuckup
[git/ruby-binding.git] / git.rb
blobaa52a28d4f3b6008ad0239313394630d24651bdf
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       @loose = Internal::LooseStorage.new(git_path("objects"))
11       @packs = []
12       initpacks
13     end
15     def git_path(path)
16       return "#@git_dir/#{path}"
17     end
19     def get_object_by_sha1(sha1)
20       r = get_raw_object_by_sha1(sha1)
21       return nil if !r
22       Object.from_raw(r, self)
23     end
25     def get_raw_object_by_sha1(sha1)
26       sha1 = [sha1].pack("H*")
28       # try packs
29       @packs.each do |pack|
30         o = pack[sha1]
31         return o if o
32       end
34       # try loose storage
35       o = @loose[sha1]
36       return o if o
38       # try packs again, maybe the object got packed in the meantime
39       initpacks
40       @packs.each do |pack|
41         o = pack[sha1]
42         return o if o
43       end
45       nil
46     end
48     def initpacks
49       @packs.each do |pack|
50         pack.close
51       end
52       @packs = []
53       Dir.open(git_path("objects/pack/")) do |dir|
54         dir.each do |entry|
55           if entry =~ /\.pack$/i
56             @packs << Git::Internal::PackStorage.new(git_path("objects/pack/" \
57                                                               + entry))
58           end
59         end
60       end
61     end
62   end
63 end
65 if $0 == __FILE__
66   require 'git'
67   r = Git::Repository.new(ARGV[0])
68   ARGV[1..-1].each do |sha1|
69     o = r.get_object_by_sha1(sha1)
70     if !o
71       puts 'no such object'
72       next
73     end
74     puts o.type
75     case o.type
76     when :blob
77       puts o.content
78     when :tree
79       puts o.entry.collect { |e| "%s %s" % [e.sha1, e.name] }.join("\n")
80     when :commit
81       puts o.raw_content
82     when :tag
83       puts o.raw_content
84     end
85   end
86 end