createOrUpdateBranch method

Future<String?> createOrUpdateBranch(
  1. String branchName,
  2. String treeSha,
  3. String commitMessage
)

Returns the SHA for the new commit if one is created.

null if the branch is not updated.

Implementation

Future<String?> createOrUpdateBranch(
  String branchName,
  String treeSha,
  String commitMessage,
) async {
  requireArgumentNotNullOrEmpty(branchName, 'branchName');
  requireArgumentValidSha1(treeSha, 'treeSha');

  final targetBranchRef = await branchReference(branchName);

  String? newCommitSha;

  if (targetBranchRef == null) {
    newCommitSha = await commitTree(treeSha, commitMessage);
  } else {
    newCommitSha =
        await _updateBranch(targetBranchRef.sha, treeSha, commitMessage);
  }

  if (newCommitSha == null) {
    return null;
  }

  assert(isValidSha(newCommitSha));

  final branchRef = 'refs/heads/$branchName';

  // TODO: if update-ref fails should we leave the new commit dangling?
  // or at least log so the user can go clean up?
  await runCommand(['update-ref', branchRef, newCommitSha]);
  return newCommitSha;
}