robinluckey-grit 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,382 @@
1
+ #
2
+ # converted from the gitrb project
3
+ #
4
+ # authors:
5
+ # Matthias Lederhofer <matled@gmx.net>
6
+ # Simon 'corecode' Schubert <corecode@fs.ei.tum.de>
7
+ # Scott Chacon <schacon@gmail.com>
8
+ #
9
+ # provides native ruby access to git objects and pack files
10
+ #
11
+
12
+ require 'zlib'
13
+ require 'grit/git-ruby/internal/raw_object'
14
+ require 'grit/git-ruby/internal/file_window'
15
+
16
+ PACK_SIGNATURE = "PACK"
17
+ PACK_IDX_SIGNATURE = "\377tOc"
18
+
19
+ module Grit
20
+ module GitRuby
21
+ module Internal
22
+ class PackFormatError < StandardError
23
+ end
24
+
25
+ class PackStorage
26
+ OBJ_OFS_DELTA = 6
27
+ OBJ_REF_DELTA = 7
28
+
29
+ FanOutCount = 256
30
+ SHA1Size = 20
31
+ IdxOffsetSize = 4
32
+ OffsetSize = 4
33
+ CrcSize = 4
34
+ OffsetStart = FanOutCount * IdxOffsetSize
35
+ SHA1Start = OffsetStart + OffsetSize
36
+ EntrySize = OffsetSize + SHA1Size
37
+ EntrySizeV2 = SHA1Size + CrcSize + OffsetSize
38
+
39
+ def initialize(file)
40
+ if file =~ /\.idx$/
41
+ file = file[0...-3] + 'pack'
42
+ end
43
+ @name = file
44
+ @cache = {}
45
+ init_pack
46
+ end
47
+
48
+ def with_idx(index_file = nil)
49
+ if !index_file
50
+ index_file = @name
51
+ idxfile = File.open(@name[0...-4]+'idx', 'rb')
52
+ else
53
+ idxfile = File.open(index_file, 'rb')
54
+ end
55
+
56
+ # read header
57
+ sig = idxfile.read(4)
58
+ ver = idxfile.read(4).unpack("N")[0]
59
+
60
+ if sig == PACK_IDX_SIGNATURE
61
+ if(ver != 2)
62
+ raise PackFormatError, "pack #@name has unknown pack file version #{ver}"
63
+ end
64
+ @version = 2
65
+ else
66
+ @version = 1
67
+ end
68
+
69
+ idx = FileWindow.new(idxfile, @version)
70
+ yield idx
71
+ idx.unmap
72
+ idxfile.close
73
+ end
74
+
75
+ def with_packfile
76
+ packfile = File.open(@name, 'rb')
77
+ yield packfile
78
+ packfile.close
79
+ end
80
+
81
+ def cache_objects
82
+ @cache = {}
83
+ with_packfile do |packfile|
84
+ each_entry do |sha, offset|
85
+ data, type = unpack_object(packfile, offset, {:caching => true})
86
+ if data
87
+ @cache[sha] = RawObject.new(OBJ_TYPES[type], data)
88
+ end
89
+ end
90
+ end
91
+ end
92
+
93
+ def name
94
+ @name
95
+ end
96
+
97
+ def close
98
+ # shouldnt be anything open now
99
+ end
100
+
101
+ # given an index file, list out the shas that it's packfile contains
102
+ def get_shas
103
+ shas = []
104
+ each_sha1 { |sha| shas << sha.unpack("H*")[0] }
105
+ shas
106
+ end
107
+
108
+ def [](sha1)
109
+ if obj = @cache[sha1]
110
+ return obj
111
+ end
112
+
113
+ offset = find_object(sha1)
114
+ return nil if !offset
115
+ @cache[sha1] = obj = parse_object(offset)
116
+ return obj
117
+ end
118
+
119
+ def init_pack
120
+ with_idx do |idx|
121
+ @offsets = [0]
122
+ FanOutCount.times do |i|
123
+ pos = idx[i * IdxOffsetSize,IdxOffsetSize].unpack('N')[0]
124
+ if pos < @offsets[i]
125
+ raise PackFormatError, "pack #@name has discontinuous index #{i}"
126
+ end
127
+ @offsets << pos
128
+ end
129
+ @size = @offsets[-1]
130
+ end
131
+ end
132
+
133
+ def each_entry
134
+ with_idx do |idx|
135
+ if @version == 2
136
+ data = read_data_v2(idx)
137
+ data.each do |sha1, crc, offset|
138
+ yield sha1, offset
139
+ end
140
+ else
141
+ pos = OffsetStart
142
+ @size.times do
143
+ offset = idx[pos,OffsetSize].unpack('N')[0]
144
+ sha1 = idx[pos+OffsetSize,SHA1Size]
145
+ pos += EntrySize
146
+ yield sha1, offset
147
+ end
148
+ end
149
+ end
150
+ end
151
+
152
+ def read_data_v2(idx)
153
+ data = []
154
+ pos = OffsetStart
155
+ @size.times do |i|
156
+ data[i] = [idx[pos,SHA1Size], 0, 0]
157
+ pos += SHA1Size
158
+ end
159
+ @size.times do |i|
160
+ crc = idx[pos,CrcSize]
161
+ data[i][1] = crc
162
+ pos += CrcSize
163
+ end
164
+ @size.times do |i|
165
+ offset = idx[pos,OffsetSize].unpack('N')[0]
166
+ data[i][2] = offset
167
+ pos += OffsetSize
168
+ end
169
+ data
170
+ end
171
+ private :read_data_v2
172
+
173
+ def each_sha1
174
+ with_idx do |idx|
175
+ if @version == 2
176
+ data = read_data_v2(idx)
177
+ data.each do |sha1, crc, offset|
178
+ yield sha1
179
+ end
180
+ else
181
+ pos = SHA1Start
182
+ @size.times do
183
+ sha1 = idx[pos,SHA1Size]
184
+ pos += EntrySize
185
+ yield sha1
186
+ end
187
+ end
188
+ end
189
+ end
190
+
191
+ def find_object_in_index(idx, sha1)
192
+ slot = sha1.getord(0)
193
+ return nil if !slot
194
+ first, last = @offsets[slot,2]
195
+ while first < last
196
+ mid = (first + last) / 2
197
+ if @version == 2
198
+ midsha1 = idx[OffsetStart + (mid * SHA1Size), SHA1Size]
199
+ cmp = midsha1 <=> sha1
200
+
201
+ if cmp < 0
202
+ first = mid + 1
203
+ elsif cmp > 0
204
+ last = mid
205
+ else
206
+ pos = OffsetStart + (@size * (SHA1Size + CrcSize)) + (mid * OffsetSize)
207
+ offset = idx[pos, OffsetSize].unpack('N')[0]
208
+ return offset
209
+ end
210
+ else
211
+ midsha1 = idx[SHA1Start + mid * EntrySize,SHA1Size]
212
+ cmp = midsha1 <=> sha1
213
+
214
+ if cmp < 0
215
+ first = mid + 1
216
+ elsif cmp > 0
217
+ last = mid
218
+ else
219
+ pos = OffsetStart + mid * EntrySize
220
+ offset = idx[pos,OffsetSize].unpack('N')[0]
221
+ return offset
222
+ end
223
+ end
224
+ end
225
+ nil
226
+ end
227
+
228
+ def find_object(sha1)
229
+ obj = nil
230
+ with_idx do |idx|
231
+ obj = find_object_in_index(idx, sha1)
232
+ end
233
+ obj
234
+ end
235
+ private :find_object
236
+
237
+ def parse_object(offset)
238
+ obj = nil
239
+ with_packfile do |packfile|
240
+ data, type = unpack_object(packfile, offset)
241
+ obj = RawObject.new(OBJ_TYPES[type], data)
242
+ end
243
+ obj
244
+ end
245
+ protected :parse_object
246
+
247
+ def unpack_object(packfile, offset, options = {})
248
+ obj_offset = offset
249
+ packfile.seek(offset)
250
+
251
+ c = packfile.read(1).getord(0)
252
+ size = c & 0xf
253
+ type = (c >> 4) & 7
254
+ shift = 4
255
+ offset += 1
256
+ while c & 0x80 != 0
257
+ c = packfile.read(1).getord(0)
258
+ size |= ((c & 0x7f) << shift)
259
+ shift += 7
260
+ offset += 1
261
+ end
262
+
263
+ return [false, false] if !(type == OBJ_COMMIT || type == OBJ_TREE) && options[:caching]
264
+
265
+ case type
266
+ when OBJ_OFS_DELTA, OBJ_REF_DELTA
267
+ data, type = unpack_deltified(packfile, type, offset, obj_offset, size, options)
268
+ #puts type
269
+ when OBJ_COMMIT, OBJ_TREE, OBJ_BLOB, OBJ_TAG
270
+ data = unpack_compressed(offset, size)
271
+ else
272
+ raise PackFormatError, "invalid type #{type}"
273
+ end
274
+ [data, type]
275
+ end
276
+ private :unpack_object
277
+
278
+ def unpack_deltified(packfile, type, offset, obj_offset, size, options = {})
279
+ packfile.seek(offset)
280
+ data = packfile.read(SHA1Size)
281
+
282
+ if type == OBJ_OFS_DELTA
283
+ i = 0
284
+ c = data.getord(i)
285
+ base_offset = c & 0x7f
286
+ while c & 0x80 != 0
287
+ c = data.getord(i += 1)
288
+ base_offset += 1
289
+ base_offset <<= 7
290
+ base_offset |= c & 0x7f
291
+ end
292
+ base_offset = obj_offset - base_offset
293
+ offset += i + 1
294
+ else
295
+ base_offset = find_object(data)
296
+ offset += SHA1Size
297
+ end
298
+
299
+ base, type = unpack_object(packfile, base_offset)
300
+
301
+ return [false, false] if !(type == OBJ_COMMIT || type == OBJ_TREE) && options[:caching]
302
+
303
+ delta = unpack_compressed(offset, size)
304
+ [patch_delta(base, delta), type]
305
+ end
306
+ private :unpack_deltified
307
+
308
+ def unpack_compressed(offset, destsize)
309
+ outdata = ""
310
+ with_packfile do |packfile|
311
+ packfile.seek(offset)
312
+ zstr = Zlib::Inflate.new
313
+ while outdata.size < destsize
314
+ indata = packfile.read(4096)
315
+ if indata.size == 0
316
+ raise PackFormatError, 'error reading pack data'
317
+ end
318
+ outdata += zstr.inflate(indata)
319
+ end
320
+ if outdata.size > destsize
321
+ raise PackFormatError, 'error reading pack data'
322
+ end
323
+ zstr.close
324
+ end
325
+ outdata
326
+ end
327
+ private :unpack_compressed
328
+
329
+ def patch_delta(base, delta)
330
+ src_size, pos = patch_delta_header_size(delta, 0)
331
+ if src_size != base.size
332
+ raise PackFormatError, 'invalid delta data'
333
+ end
334
+
335
+ dest_size, pos = patch_delta_header_size(delta, pos)
336
+ dest = ""
337
+ while pos < delta.size
338
+ c = delta.getord(pos)
339
+ pos += 1
340
+ if c & 0x80 != 0
341
+ pos -= 1
342
+ cp_off = cp_size = 0
343
+ cp_off = delta.getord(pos += 1) if c & 0x01 != 0
344
+ cp_off |= delta.getord(pos += 1) << 8 if c & 0x02 != 0
345
+ cp_off |= delta.getord(pos += 1) << 16 if c & 0x04 != 0
346
+ cp_off |= delta.getord(pos += 1) << 24 if c & 0x08 != 0
347
+ cp_size = delta.getord(pos += 1) if c & 0x10 != 0
348
+ cp_size |= delta.getord(pos += 1) << 8 if c & 0x20 != 0
349
+ cp_size |= delta.getord(pos += 1) << 16 if c & 0x40 != 0
350
+ cp_size = 0x10000 if cp_size == 0
351
+ pos += 1
352
+ dest += base[cp_off,cp_size]
353
+ elsif c != 0
354
+ dest += delta[pos,c]
355
+ pos += c
356
+ else
357
+ raise PackFormatError, 'invalid delta data'
358
+ end
359
+ end
360
+ dest
361
+ end
362
+ private :patch_delta
363
+
364
+ def patch_delta_header_size(delta, pos)
365
+ size = 0
366
+ shift = 0
367
+ begin
368
+ c = delta.getord(pos)
369
+ if c == nil
370
+ raise PackFormatError, 'invalid delta header'
371
+ end
372
+ pos += 1
373
+ size |= (c & 0x7f) << shift
374
+ shift += 7
375
+ end while c & 0x80 != 0
376
+ [size, pos]
377
+ end
378
+ private :patch_delta_header_size
379
+ end
380
+ end
381
+ end
382
+ end
@@ -0,0 +1,37 @@
1
+ #
2
+ # converted from the gitrb project
3
+ #
4
+ # authors:
5
+ # Matthias Lederhofer <matled@gmx.net>
6
+ # Simon 'corecode' Schubert <corecode@fs.ei.tum.de>
7
+ #
8
+ # provides native ruby access to git objects and pack files
9
+ #
10
+
11
+ require 'digest/sha1'
12
+
13
+ module Grit
14
+ module GitRuby
15
+ module Internal
16
+ OBJ_NONE = 0
17
+ OBJ_COMMIT = 1
18
+ OBJ_TREE = 2
19
+ OBJ_BLOB = 3
20
+ OBJ_TAG = 4
21
+
22
+ OBJ_TYPES = [nil, :commit, :tree, :blob, :tag].freeze
23
+
24
+ class RawObject
25
+ attr_accessor :type, :content
26
+ def initialize(type, content)
27
+ @type = type
28
+ @content = content
29
+ end
30
+
31
+ def sha1
32
+ Digest::SHA1.digest("%s %d\0" % [@type, @content.length] + @content)
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,325 @@
1
+ #
2
+ # converted from the gitrb project
3
+ #
4
+ # authors:
5
+ # Matthias Lederhofer <matled@gmx.net>
6
+ # Simon 'corecode' Schubert <corecode@fs.ei.tum.de>
7
+ # Scott Chacon <schacon@gmail.com>
8
+ #
9
+ # provides native ruby access to git objects and pack files
10
+ #
11
+
12
+ # These classes translate the raw binary data kept in the sha encoded files
13
+ # into parsed data that can then be used in another fashion
14
+ require 'stringio'
15
+
16
+ module Grit
17
+ module GitRuby
18
+
19
+ # class for author/committer/tagger lines
20
+ class UserInfo
21
+ attr_accessor :name, :email, :date, :offset
22
+
23
+ def initialize(str)
24
+ m = /^(.*?) <(.*)> (\d+) ([+-])0*(\d+?)$/.match(str)
25
+ if !m
26
+ raise RuntimeError, "invalid header '%s' in commit" % str
27
+ end
28
+ @name = m[1]
29
+ @email = m[2]
30
+ @date = Time.at(Integer(m[3]))
31
+ @offset = (m[4] == "-" ? -1 : 1)*Integer(m[5])
32
+ end
33
+
34
+ def to_s
35
+ "%s <%s> %s %+05d" % [@name, @email, @date.to_i, @offset]
36
+ end
37
+ end
38
+
39
+ # base class for all git objects (blob, tree, commit, tag)
40
+ class Object
41
+ attr_accessor :repository
42
+
43
+ def Object.from_raw(rawobject, repository = nil)
44
+ case rawobject.type
45
+ when :blob
46
+ return Blob.from_raw(rawobject, repository)
47
+ when :tree
48
+ return Tree.from_raw(rawobject, repository)
49
+ when :commit
50
+ return Commit.from_raw(rawobject, repository)
51
+ when :tag
52
+ return Tag.from_raw(rawobject, repository)
53
+ else
54
+ raise RuntimeError, "got invalid object-type"
55
+ end
56
+ end
57
+
58
+ def initialize
59
+ raise NotImplemented, "abstract class"
60
+ end
61
+
62
+ def type
63
+ raise NotImplemented, "abstract class"
64
+ end
65
+
66
+ def raw_content
67
+ raise NotImplemented, "abstract class"
68
+ end
69
+
70
+ def sha1
71
+ Digest::SHA1.hexdigest("%s %d\0" % \
72
+ [self.type, self.raw_content.length] + \
73
+ self.raw_content)
74
+ end
75
+ end
76
+
77
+ class Blob < Object
78
+ attr_accessor :content
79
+
80
+ def self.from_raw(rawobject, repository)
81
+ new(rawobject.content)
82
+ end
83
+
84
+ def initialize(content, repository=nil)
85
+ @content = content
86
+ @repository = repository
87
+ end
88
+
89
+ def type
90
+ :blob
91
+ end
92
+
93
+ def raw_content
94
+ @content
95
+ end
96
+ end
97
+
98
+ class DirectoryEntry
99
+ S_IFMT = 00170000
100
+ S_IFLNK = 0120000
101
+ S_IFREG = 0100000
102
+ S_IFDIR = 0040000
103
+
104
+ attr_accessor :mode, :name, :sha1
105
+ def initialize(mode, filename, sha1o)
106
+ @mode = 0
107
+ mode.each_byte do |i|
108
+ @mode = (@mode << 3) | (i-'0'[0])
109
+ end
110
+ @name = filename
111
+ @sha1 = sha1o
112
+ if ![S_IFLNK, S_IFDIR, S_IFREG].include?(@mode & S_IFMT)
113
+ raise RuntimeError, "unknown type for directory entry"
114
+ end
115
+ end
116
+
117
+ def type
118
+ case @mode & S_IFMT
119
+ when S_IFLNK
120
+ @type = :link
121
+ when S_IFDIR
122
+ @type = :directory
123
+ when S_IFREG
124
+ @type = :file
125
+ else
126
+ raise RuntimeError, "unknown type for directory entry"
127
+ end
128
+ end
129
+
130
+ def type=(type)
131
+ case @type
132
+ when :link
133
+ @mode = (@mode & ~S_IFMT) | S_IFLNK
134
+ when :directory
135
+ @mode = (@mode & ~S_IFMT) | S_IFDIR
136
+ when :file
137
+ @mode = (@mode & ~S_IFMT) | S_IFREG
138
+ else
139
+ raise RuntimeError, "invalid type"
140
+ end
141
+ end
142
+
143
+ def format_type
144
+ case type
145
+ when :link
146
+ 'link'
147
+ when :directory
148
+ 'tree'
149
+ when :file
150
+ 'blob'
151
+ end
152
+ end
153
+
154
+ def format_mode
155
+ "%06o" % @mode
156
+ end
157
+
158
+ def raw
159
+ "%o %s\0%s" % [@mode, @name, [@sha1].pack("H*")]
160
+ end
161
+ end
162
+
163
+
164
+ def self.read_bytes_until(io, char)
165
+ string = ''
166
+ if RUBY_VERSION > '1.9'
167
+ while ((next_char = io.getc) != char) && !io.eof
168
+ string += next_char
169
+ end
170
+ else
171
+ while ((next_char = io.getc.chr) != char) && !io.eof
172
+ string += next_char
173
+ end
174
+ end
175
+ string
176
+ end
177
+
178
+
179
+ class Tree < Object
180
+ attr_accessor :entry
181
+
182
+ def self.from_raw(rawobject, repository=nil)
183
+ raw = StringIO.new(rawobject.content)
184
+
185
+ entries = []
186
+ while !raw.eof?
187
+ mode = Grit::GitRuby.read_bytes_until(raw, ' ')
188
+ file_name = Grit::GitRuby.read_bytes_until(raw, "\0")
189
+ raw_sha = raw.read(20)
190
+ sha = raw_sha.unpack("H*").first
191
+
192
+ entries << DirectoryEntry.new(mode, file_name, sha)
193
+ end
194
+ new(entries, repository)
195
+ end
196
+
197
+ def initialize(entries=[], repository = nil)
198
+ @entry = entries
199
+ @repository = repository
200
+ end
201
+
202
+ def type
203
+ :tree
204
+ end
205
+
206
+ def raw_content
207
+ # TODO: sort correctly
208
+ #@entry.sort { |a,b| a.name <=> b.name }.
209
+ @entry.collect { |e| [[e.format_mode, e.format_type, e.sha1].join(' '), e.name].join("\t") }.join("\n")
210
+ end
211
+
212
+ def actual_raw
213
+ #@entry.collect { |e| e.raw.join(' '), e.name].join("\t") }.join("\n")
214
+ end
215
+ end
216
+
217
+ class Commit < Object
218
+ attr_accessor :author, :committer, :tree, :parent, :message, :headers
219
+
220
+ def self.from_raw(rawobject, repository=nil)
221
+ parent = []
222
+ tree = author = committer = nil
223
+
224
+ headers, message = rawobject.content.split(/\n\n/, 2)
225
+ all_headers = headers.split(/\n/).map { |header| header.split(/ /, 2) }
226
+ all_headers.each do |key, value|
227
+ case key
228
+ when "tree"
229
+ tree = value
230
+ when "parent"
231
+ parent.push(value)
232
+ when "author"
233
+ author = UserInfo.new(value)
234
+ when "committer"
235
+ committer = UserInfo.new(value)
236
+ else
237
+ warn "unknown header '%s' in commit %s" % \
238
+ [key, rawobject.sha1.unpack("H*")[0]]
239
+ end
240
+ end
241
+ if not tree && author && committer
242
+ raise RuntimeError, "incomplete raw commit object"
243
+ end
244
+ new(tree, parent, author, committer, message, headers, repository)
245
+ end
246
+
247
+ def initialize(tree, parent, author, committer, message, headers, repository=nil)
248
+ @tree = tree
249
+ @author = author
250
+ @parent = parent
251
+ @committer = committer
252
+ @message = message
253
+ @headers = headers
254
+ @repository = repository
255
+ end
256
+
257
+ def type
258
+ :commit
259
+ end
260
+
261
+ def raw_content
262
+ "tree %s\n%sauthor %s\ncommitter %s\n\n" % [
263
+ @tree,
264
+ @parent.collect { |i| "parent %s\n" % i }.join,
265
+ @author, @committer] + @message
266
+ end
267
+
268
+ def raw_log(sha)
269
+ output = "commit #{sha}\n"
270
+ output += @headers + "\n\n"
271
+ output += @message.split("\n").map { |l| ' ' + l }.join("\n") + "\n\n"
272
+ end
273
+
274
+ end
275
+
276
+ class Tag < Object
277
+ attr_accessor :object, :type, :tag, :tagger, :message
278
+
279
+ def self.from_raw(rawobject, repository=nil)
280
+ headers, message = rawobject.content.split(/\n\n/, 2)
281
+ headers = headers.split(/\n/).map { |header| header.split(/ /, 2) }
282
+ headers.each do |key, value|
283
+ case key
284
+ when "object"
285
+ object = value
286
+ when "type"
287
+ if !["blob", "tree", "commit", "tag"].include?(value)
288
+ raise RuntimeError, "invalid type in tag"
289
+ end
290
+ type = value.to_sym
291
+ when "tag"
292
+ tag = value
293
+ when "tagger"
294
+ tagger = UserInfo.new(value)
295
+ else
296
+ warn "unknown header '%s' in tag" % \
297
+ [key, rawobject.sha1.unpack("H*")[0]]
298
+ end
299
+ if not object && type && tag && tagger
300
+ raise RuntimeError, "incomplete raw tag object"
301
+ end
302
+ end
303
+ new(object, type, tag, tagger, repository)
304
+ end
305
+
306
+ def initialize(object, type, tag, tagger, repository=nil)
307
+ @object = object
308
+ @type = type
309
+ @tag = tag
310
+ @tagger = tagger
311
+ @repository = repository
312
+ end
313
+
314
+ def raw_content
315
+ "object %s\ntype %s\ntag %s\ntagger %s\n\n" % \
316
+ [@object, @type, @tag, @tagger] + @message
317
+ end
318
+
319
+ def type
320
+ :tag
321
+ end
322
+ end
323
+
324
+ end
325
+ end