readObjectFromPath method

Future<GitObjectResult> readObjectFromPath(
  1. String filePath
)

Implementation

Future<GitObjectResult> readObjectFromPath(String filePath) async {
  // FIXME: Handle zlib and fs exceptions
  var contents = await _fs.file(filePath).readAsBytes();
  var raw = zlib.decode(contents) as Uint8List;

  // Read Object Type
  var x = raw.indexOf(asciiHelper.space);
  if (x == -1) {
    return GitObjectResult.fail(GitObjectCorruptedMissingType());
  }
  var fmt = raw.sublistView(0, x);

  // Read and validate object size
  var y = raw.indexOf(0x0, x);
  if (y == -1) {
    return GitObjectResult.fail(GitObjectCorruptedMissingSize());
  }

  var size = int.tryParse(ascii.decode(raw.sublistView(x, y)));
  if (size == null) {
    return GitObjectResult.fail(GitObjectCorruptedInvalidIntSize());
  }

  if (size != (raw.length - y - 1)) {
    return GitObjectResult.fail(GitObjectCorruptedBadSize());
  }

  var fmtStr = ascii.decode(fmt);
  var rawData = raw.sublistView(y + 1);
  return createObject(fmtStr, rawData, filePath);
}