GitIndex.decode constructor

GitIndex.decode(
  1. Uint8List bytes
)

Implementation

GitIndex.decode(Uint8List bytes) {
  var reader = ByteDataReader(endian: Endian.big, copy: false);
  reader.add(bytes);

  // Read 12 byte header
  var sig = reader.read(4);
  if (sig.length != 4) {
    throw GitIndexCorruptedException('Invalid Signature lenght');
  }

  if (!_listEq(sig, _indexSignature)) {
    throw GitIndexCorruptedException('Invalid signature $sig');
  }

  versionNo = reader.readUint32();
  if (versionNo <= 1 || versionNo > 4) {
    throw Exception('GitIndexError: Version number not supported $versionNo');
  }

  // Read Index Entries
  var numEntries = reader.readUint32();
  for (var i = 0; i < numEntries; i++) {
    var lastEntry = i == 0 ? null : entries[i - 1];
    var entry =
        GitIndexEntry.fromBytes(versionNo, bytes.length, reader, lastEntry);
    entries.add(entry);
  }

  // Read Extensions
  List<int> extensionHeader;
  while (true) {
    extensionHeader = reader.read(4);
    if (!_parseExtension(extensionHeader, reader)) {
      break;
    }
  }

  var hashBytesBuilder = BytesBuilder(copy: false);
  hashBytesBuilder..add(extensionHeader)..add(reader.read(16));

  var expectedHash = GitHash.fromBytes(hashBytesBuilder.toBytes());
  var actualHash = GitHash.compute(bytes.sublistView(0, bytes.length - 20));
  if (expectedHash != actualHash) {
    print('ExpectedHash: $expectedHash');
    print('ActualHash:  $actualHash');
    throw GitIndexCorruptedException('Invalid Hash');
  }
}