commit method

Future<GitCommit> commit({
  1. required String message,
  2. required GitAuthor author,
  3. GitAuthor? committer,
  4. bool addAll = false,
})

Implementation

Future<GitCommit> commit({
  required String message,
  required GitAuthor author,
  GitAuthor? committer,
  bool addAll = false,
}) async {
  committer ??= author;

  var index = await indexStorage.readIndex();

  if (addAll) {
    await addDirectoryToIndex(index, workTree, recursive: true);
    await indexStorage.writeIndex(index);
  }

  var treeHash = await writeTree(index);
  if (treeHash == null) {
    throw Exception('WTF, there is nothing to add?');
  }
  var parents = <GitHash>[];

  var headRef = await head();
  if (headRef != null) {
    var parentRef = await resolveReference(headRef);
    if (parentRef != null && parentRef.isHash) {
      parents.add(parentRef.hash!);
    }
  }

  var commit = GitCommit.create(
    author: author,
    committer: committer,
    parents: parents,
    message: message,
    treeHash: treeHash,
  );
  var hash = await objStorage.writeObject(commit);

  // Update the ref of the current branch
  var branchName = await currentBranch();
  if (branchName == null) {
    var h = await head();
    if (h == null) {
      throw Exception('Could not update current branch');
    }
    var target = h.target!;
    assert(target.isBranch());
    branchName = target.branchName();
  }

  var newRef = Reference.hash(ReferenceName.branch(branchName!), hash);

  await refStorage.saveRef(newRef);

  return commit;
}