serialize method

Uint8List serialize()

Implementation

Uint8List serialize() {
  // Do we support this version of the index?
  if (versionNo != 2) {
    throw Exception(
        'Git Index version $versionNo cannot be serialized. Only version 2 is supported');
  }

  var writer = ByteDataWriter();

  // Header
  writer.write(_indexSignature);
  writer.writeUint32(versionNo);
  writer.writeUint32(entries.length);

  // Entries
  entries.sort((a, b) => a.path.compareTo(b.path));
  entries.forEach((e) => writer.write(e.serialize()));

  // Footer
  var hash = GitHash.compute(writer.toBytes());
  writer.write(hash.bytes);

  return writer.toBytes();
}