Class Grit::GitRuby::Internal::LooseStorage
In: lib/grit/git-ruby/internal/loose.rb
Parent: Object

Methods

Public Class methods

simply figure out the sha

[Source]

    # File lib/grit/git-ruby/internal/loose.rb, line 87
87:         def self.calculate_sha(content, type)
88:           size = content.length.to_s
89:           verify_header(type, size)
90:           header = "#{type} #{size}\0"
91:           store = header + content
92:                     
93:           Digest::SHA1.hexdigest(store)
94:         end

[Source]

    # File lib/grit/git-ruby/internal/loose.rb, line 23
23:         def initialize(directory)
24:           @directory = directory
25:         end

[Source]

     # File lib/grit/git-ruby/internal/loose.rb, line 96
 96:         def self.verify_header(type, size)
 97:           if !%w(blob tree commit tag).include?(type) || size !~ /^\d+$/
 98:             raise LooseObjectError, "invalid object header"
 99:           end
100:         end

Public Instance methods

[Source]

    # File lib/grit/git-ruby/internal/loose.rb, line 27
27:         def [](sha1)
28:           sha1 = sha1.unpack("H*")[0]
29:           begin
30:             return nil unless sha1[0...2] && sha1[2..39]
31:             path = @directory + '/' + sha1[0...2] + '/' + sha1[2..39]
32:             get_raw_object(File.open(path, 'rb').read)
33:           rescue Errno::ENOENT
34:             nil
35:           end
36:         end

[Source]

    # File lib/grit/git-ruby/internal/loose.rb, line 38
38:         def get_raw_object(buf)
39:           if buf.length < 2
40:             raise LooseObjectError, "object file too small"
41:           end
42: 
43:           if legacy_loose_object?(buf)
44:             content = Zlib::Inflate.inflate(buf)
45:             header, content = content.split(/\0/, 2)
46:             if !header || !content
47:               raise LooseObjectError, "invalid object header"
48:             end
49:             type, size = header.split(/ /, 2)
50:             if !%w(blob tree commit tag).include?(type) || size !~ /^\d+$/
51:               raise LooseObjectError, "invalid object header"
52:             end
53:             type = type.to_sym
54:             size = size.to_i
55:           else
56:             type, size, used = unpack_object_header_gently(buf)
57:             content = Zlib::Inflate.inflate(buf[used..-1])
58:           end
59:           raise LooseObjectError, "size mismatch" if content.length != size
60:           return RawObject.new(type, content)
61:         end

currently, I‘m using the legacy format because it‘s easier to do this function takes content and a type and writes out the loose object and returns a sha

[Source]

    # File lib/grit/git-ruby/internal/loose.rb, line 65
65:         def put_raw_object(content, type)
66:           size = content.length.to_s
67:           LooseStorage.verify_header(type, size)
68:           
69:           header = "#{type} #{size}\0"
70:           store = header + content
71:                     
72:           sha1 = Digest::SHA1.hexdigest(store)
73:           path = @directory+'/'+sha1[0...2]+'/'+sha1[2..40]
74:           
75:           if !File.exists?(path)
76:             content = Zlib::Deflate.deflate(store)
77:           
78:             FileUtils.mkdir_p(@directory+'/'+sha1[0...2])
79:             File.open(path, 'wb') do |f|
80:               f.write content
81:             end
82:           end
83:           return sha1
84:         end

[Validate]