write method

  1. @override
Future<void> write(
  1. String name,
  2. GitObjectId id, {
  3. GitObjectId? expected,
})
override

Points name at id. With expected (use GitObjectId.zero for "must not exist yet") the update is refused with a GitRefConflict unless the ref is there now.

Implementation

@override
Future<void> write(
  String name,
  GitObjectId id, {
  GitObjectId? expected,
}) async {
  final target = _loose(name);
  target.parent.createSync(recursive: true);
  final lock = File('${target.path}.lock');
  try {
    lock.createSync(exclusive: true);
  } on FileSystemException {
    throw GitRefConflict(name, expected: expected, actual: await read(name));
  }
  try {
    if (expected != null) {
      final actual = await read(name);
      final matches = expected.isZero ? actual == null : actual == expected;
      if (!matches) {
        throw GitRefConflict(name, expected: expected, actual: actual);
      }
    }
    lock.writeAsStringSync('${id.hex}\n', flush: true);
    lock.renameSync(target.path);
  } finally {
    if (lock.existsSync()) lock.deleteSync();
  }
}