runGit function

Future<ProcessResult> runGit(
  1. List<String> arguments,
  2. {bool throwOnError = true,
  3. bool echoOutput = false,
  4. String? processWorkingDir}
)

Run git with the provided arguments.

If echoOutput is true, the output of the git command will be echoed. Note: echoOutput true will also cause the returned ProcessResult to have null values for ProcessResult.stdout and ProcessResult.stderr.

Use processWorkingDir to set the working directory for the created Git process. If omitted or null, the default (Directory.current) is used.

Implementation

Future<ProcessResult> runGit(
  List<String> arguments, {
  bool throwOnError = true,
  bool echoOutput = false,
  String? processWorkingDir,
}) async {
  final pr = await Process.start(
    'git',
    arguments,
    workingDirectory: processWorkingDir,
    runInShell: true,
    mode: echoOutput ? ProcessStartMode.inheritStdio : ProcessStartMode.normal,
  );

  final results = await Future.wait([
    pr.exitCode,
    if (!echoOutput) pr.stdout.transform(const SystemEncoding().decoder).join(),
    if (!echoOutput) pr.stderr.transform(const SystemEncoding().decoder).join(),
  ]);

  final result = ProcessResult(
    pr.pid,
    results[0] as int,
    echoOutput ? null : results[1] as String,
    echoOutput ? null : results[2] as String,
  );

  if (throwOnError) {
    _throwIfProcessFailed(result, 'git', arguments);
  }
  return result;
}