Class: Amp::Core::Repositories::Git::PackFileIndexV1

Inherits:
PackFileIndex
  • Object
show all
Defined in:
lib/amp-git/repo_format/packfile_index.rb

Overview

This class is for the older version of index files. They are structured as simply a fanout table and a list of [offset] entries. There’s a checksum at the bottom for verification.

Initialization only reads in the fanout table, and the file is left open.

Constant Summary collapse

ENTRY_TABLE_START =
256 * 4
ENTRY_TABLE_ENTRY_SIZE =
20 + 4
ENTRY_TABLE_FORMAT =
"Na20"

Constants inherited from PackFileIndex

Amp::Core::Repositories::Git::PackFileIndex::FANOUT_TABLE_SIZE

Instance Attribute Summary

Attributes inherited from PackFileIndex

#fanout, #size

Instance Method Summary collapse

Methods inherited from PackFileIndex

#initialize, parse, #search_range_for_hash

Constructor Details

This class inherits a constructor from Amp::Core::Repositories::Git::PackFileIndex

Instance Method Details

#offset_for_hash(hsh) ⇒ Fixnum?

Looks up the offset in a packfile of a given hash. nil is returned if the sha1 isn’t found.

Parameters:

  • hsh (String)

    a binary, sha-1 hash of the desired object

Returns:

  • (Fixnum, nil)

    the offset into the corresponding packfile at which you can find the object

Raises:



111
112
113
114
115
116
117
118
119
120
121
# File 'lib/amp-git/repo_format/packfile_index.rb', line 111

def offset_for_hash(hsh)
  range = search_range_for_hash hsh
  @fp.seek(FANOUT_TABLE_START + range.begin * ENTRY_TABLE_ENTRY_SIZE, IO::SEEK_SET)
  # linear search for now.
  # TODO: binary search!
  range.each do |_|
    offset, sha1 = @fp.read(ENTRY_TABLE_ENTRY_SIZE).unpack(ENTRY_TABLE_FORMAT)
    return offset if sha1 == hsh
  end
  raise PackFileIndexLookupError.new("Couldn't find the hash #{hsh.inspect} in the packfile index.")
end