runGit function
Runs git with args in repoDir using runner and returns the trimmed
stdout.
Throws when git exits non-zero, unless allowFailure is set (used for
commands like git merge --abort/git rebase --abort that legitimately
fail when there is nothing to abort).
Shared by do review and do publish so their rollback paths use one
git runner instead of drifting copies.
Set trimmed to false when the raw stdout matters — the columns of
git status --porcelain are positional and a leading space is meaningful
(» M file« is modified but unstaged), so trimming corrupts the first line.
Implementation
Future<String> runGit(
ProcessRunner runner,
List<String> args, {
required Directory repoDir,
bool allowFailure = false,
bool trimmed = true,
}) async {
final result = await runner('git', args, workingDirectory: repoDir.path);
if (!allowFailure && result.exitCode != 0) {
final stderrStr = result.stderr?.toString().trim() ?? '';
final stdoutStr = result.stdout?.toString().trim() ?? '';
final detail = stderrStr.isNotEmpty ? stderrStr : stdoutStr;
throw Exception(
cError(
'git ${args.join(' ')} failed in '
'${path.basename(repoDir.path)}: $detail',
),
);
}
final out = result.stdout?.toString() ?? '';
return trimmed ? out.trim() : out;
}