isBehindUpstream method

Future<bool> isBehindUpstream({
  1. String remote = 'origin',
  2. String? branch,
})

Determine if the local git repository is behind on commits from it's remote branch.

Implementation

Future<bool> isBehindUpstream({
  String remote = 'origin',
  String? branch,
}) async {
  await remoteUpdate();

  final localBranch = branch ?? await currentBranchName;
  final remoteBranch = '$remote/$localBranch';
  final arguments = [
    'rev-list',
    '--left-right',
    '--count',
    '$remoteBranch...$localBranch',
  ];

  final processResult = await executeCommand(
    arguments: arguments,
  );
  final leftRightCounts = (processResult.stdout as String)
      .split('\t')
      .map<int>(int.parse)
      .toList();
  final behindCount = leftRightCounts[0];
  final aheadCount = leftRightCounts[1];
  final isBehind = behindCount > 0;

  logger.trace(
    '[GIT] Local branch `$localBranch` is behind remote branch `$remoteBranch` '
    'by $behindCount commit(s) and ahead by $aheadCount.',
  );

  return isBehind;
}