Class: Grit::GitRuby::Internal::PackStorage

Inherits:
Object
  • Object
show all
Defined in:
lib/grit/git-ruby/internal/pack.rb

Constant Summary

OBJ_OFS_DELTA =
6
OBJ_REF_DELTA =
7
FanOutCount =
256
SHA1Size =
20
IdxOffsetSize =
4
OffsetSize =
4
ExtendedOffsetSize =
8
CrcSize =
4
OffsetStart =
FanOutCount * IdxOffsetSize
SHA1Start =
OffsetStart + OffsetSize
EntrySize =
OffsetSize + SHA1Size
EntrySizeV2 =
SHA1Size + CrcSize + OffsetSize

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ PackStorage



40
41
42
43
44
45
46
47
# File 'lib/grit/git-ruby/internal/pack.rb', line 40

def initialize(file)
  if file =~ /\.idx$/
    file = file[0...-3] + 'pack'
  end
  @name = file
  @cache = {}
  init_pack
end

Instance Method Details

#[](sha1) ⇒ Object



118
119
120
121
122
123
124
125
126
127
# File 'lib/grit/git-ruby/internal/pack.rb', line 118

def [](sha1)
  if obj = @cache[sha1]
    return obj
  end

  offset = find_object(sha1)
  return nil if !offset
  @cache[sha1] = obj = parse_object(offset)
  return obj
end

#cache_objectsObject



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/grit/git-ruby/internal/pack.rb', line 91

def cache_objects
  @cache = {}
  with_packfile do |packfile|
    each_entry do |sha, offset|
      data, type = unpack_object(packfile, offset, {:caching => true})
      if data
        @cache[sha] = RawObject.new(OBJ_TYPES[type], data)
      end
    end
  end
end

#closeObject



107
108
109
# File 'lib/grit/git-ruby/internal/pack.rb', line 107

def close
  # shouldnt be anything open now
end

#each_entryObject



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/grit/git-ruby/internal/pack.rb', line 143

def each_entry
  with_idx do |idx|
    if @version == 2
      data = read_data_v2(idx)
      data.each do |sha1, crc, offset|
        yield sha1, offset
      end
    else
      pos = OffsetStart
      @size.times do
        offset = idx[pos,OffsetSize].unpack('N')[0]
        sha1 = idx[pos+OffsetSize,SHA1Size]
        pos += EntrySize
        yield sha1, offset
      end
    end
  end
end

#each_sha1Object



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/grit/git-ruby/internal/pack.rb', line 183

def each_sha1
  with_idx do |idx|
    if @version == 2
      data = read_data_v2(idx)
      data.each do |sha1, crc, offset|
        yield sha1
      end
    else
      pos = SHA1Start
      @size.times do
        sha1 = idx[pos,SHA1Size]
        pos += EntrySize
        yield sha1
      end
    end
  end
end

#find_object_in_index(idx, sha1) ⇒ Object



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/grit/git-ruby/internal/pack.rb', line 201

def find_object_in_index(idx, sha1)
  slot = sha1.getord(0)
  return nil if !slot
  first, last = @offsets[slot,2]
  while first < last
    mid = (first + last) / 2
    if @version == 2
      midsha1 = idx[OffsetStart + (mid * SHA1Size), SHA1Size]
      cmp = midsha1 <=> sha1

      if cmp < 0
        first = mid + 1
      elsif cmp > 0
        last = mid
      else
        pos = OffsetStart + (@size * (SHA1Size + CrcSize)) + (mid * OffsetSize)
        offset = idx[pos, OffsetSize].unpack('N')[0]
        if offset & 0x80000000 > 0
          offset &= 0x7fffffff
          pos = OffsetStart + (@size * (SHA1Size + CrcSize + OffsetSize)) + (offset * ExtendedOffsetSize)
          words = idx[pos, ExtendedOffsetSize].unpack('NN')
          offset = (words[0] << 32) | words[1]
        end
        return offset
      end
    else
      midsha1 = idx[SHA1Start + mid * EntrySize,SHA1Size]
      cmp = midsha1 <=> sha1

      if cmp < 0
        first = mid + 1
      elsif cmp > 0
        last = mid
      else
        pos = OffsetStart + mid * EntrySize
        offset = idx[pos,OffsetSize].unpack('N')[0]
        return offset
      end
    end
  end
  nil
end

#get_shasObject

given an index file, list out the shas that it's packfile contains



112
113
114
115
116
# File 'lib/grit/git-ruby/internal/pack.rb', line 112

def get_shas
  shas = []
  each_sha1 { |sha| shas << sha.unpack("H*")[0] }
  shas
end

#init_packObject



129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/grit/git-ruby/internal/pack.rb', line 129

def init_pack
  with_idx do |idx|
    @offsets = [0]
    FanOutCount.times do |i|
      pos = idx[i * IdxOffsetSize,IdxOffsetSize].unpack('N')[0]
      if pos < @offsets[i]
        raise PackFormatError, "pack #@name has discontinuous index #{i}"
      end
      @offsets << pos
    end
    @size = @offsets[-1]
  end
end

#nameObject



103
104
105
# File 'lib/grit/git-ruby/internal/pack.rb', line 103

def name
  @name
end

#with_idx(index_file = nil) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/grit/git-ruby/internal/pack.rb', line 49

def with_idx(index_file = nil)
  index_file ||= @name[0...-4] + 'idx'

  begin
    idxfile = File.open(index_file, 'rb')
  rescue Errno::ENOENT => boom
    # file went away. bail out without yielding.
    return
  end

  # read header
  sig = idxfile.read(4)
  ver = idxfile.read(4).unpack("N")[0]

  if sig == PACK_IDX_SIGNATURE
    if(ver != 2)
      raise PackFormatError, "pack #@name has unknown pack file version #{ver}"
    end
    @version = 2
  else
    @version = 1
  end

  idx = FileWindow.new(idxfile, @version)
  yield idx
  idx.unmap
ensure
  idxfile.close if idxfile
end

#with_packfileObject



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/grit/git-ruby/internal/pack.rb', line 79

def with_packfile
  begin
    packfile = File.open(@name, 'rb')
  rescue Errno::ENOENT
    # file went away. bail out without yielding.
    return
  end
  yield packfile
ensure
  packfile.close if packfile
end