added Matthias and Simon to credits for the gitrb code
[rubygit.git] / lib / git / raw / repository.rb
blobbd5e971d0aaa3577e932b2b99bb9e8213242185a
2 # converted from the gitrb project
4 # authors: 
5 #    Matthias Lederhofer <matled@gmx.net>
6 #    Simon 'corecode' Schubert <corecode@fs.ei.tum.de>
8 # provides native ruby access to git objects and pack files
11 require 'git/raw/internal/object'
12 require 'git/raw/internal/pack'
13 require 'git/raw/internal/loose'
14 require 'git/raw/object'
16 module Git
17   module Raw
18     
19     class Repository
20       def initialize(git_dir)
21         @git_dir = git_dir
22         @loose = Raw::Internal::LooseStorage.new(git_path("objects"))
23         @packs = []
24         initpacks
25       end
27       def show
28         @packs.each do |p|
29           puts p.name
30           puts
31           p.each_sha1 do |s|
32             puts "**#{p[s].type}**"
33             if p[s].type.to_s == 'commit'
34               puts s.unpack('H*')
35               puts p[s].content
36             end
37           end
38           puts
39         end
40       end
42       def object(sha)
43         o = get_raw_object_by_sha1(sha)
44         c = Git::Raw::Object.from_raw(o)
45       end
46             
47       def cat_file(sha)
48         object(sha).raw_content
49       end
50       
51       def log(sha, count = 30)
52         output = ''
53         i = 0
55         while sha && (i < count) do
56           o = get_raw_object_by_sha1(sha)
57           c = Git::Raw::Object.from_raw(o)
58           
59           output += "commit #{sha}\n"
60           output += o.content + "\n"
62           sha = c.parent.first
63           i += 1
64         end
65         
66         output
67       end
68       
69       def get_object_by_sha1(sha1)
70         r = get_raw_object_by_sha1(sha1)
71         return nil if !r
72         Object.from_raw(r, self)
73       end
75       def get_raw_object_by_sha1(sha1)
76         sha1 = [sha1].pack("H*")
78         # try packs
79         @packs.each do |pack|
80           o = pack[sha1]
81           return o if o
82         end
84         # try loose storage
85         o = @loose[sha1]
86         return o if o
88         # try packs again, maybe the object got packed in the meantime
89         initpacks
90         @packs.each do |pack|
91           o = pack[sha1]
92           return o if o
93         end
95         nil
96       end
98       protected
99       
100         def git_path(path)
101           return "#@git_dir/#{path}"
102         end
104       private 
105       
106         def initpacks
107           @packs.each do |pack|
108             pack.close
109           end
110           @packs = []
111           Dir.open(git_path("objects/pack/")) do |dir|
112             dir.each do |entry|
113               if entry =~ /\.pack$/i
114                 @packs << Git::Raw::Internal::PackStorage.new(git_path("objects/pack/" \
115                                                                   + entry))
116               end
117             end
118           end
119         end
120       
121     end
122     
123   end