getLatestCommitId method

Future<String> getLatestCommitId({
  1. required String base,
  2. required String head,
  3. required String path,
  4. required int line,
})

git log {base}..{head} --format=%H -n 1 -L {line},+1:{path}

If there is no commit between {base} and {head}: ''

If a commit exists between {base} and {head}: e.g.: '6d87458a8089ae5d025b087bfb9b0809d2060411'

Implementation

Future<String> getLatestCommitId({
  required String base,
  required String head,
  required String path,
  required int line,
}) async {
  final result = await _runner.run([
    'log',
    '$base..$head',
    '--format=%H',
    '-n',
    '1',
    '-L',
    '$line,+1:$path',
  ]);

  final regex = RegExp(r'^([\d|a-z]{40})');
  final match = regex.firstMatch(result);
  if (match == null) {
    return '';
  }

  return match.group(1) ?? '';
}