runGit function

Future<GitResult> runGit(
  1. List<String> arguments, {
  2. String? workingDirectory,
  3. WayfinderCancellation? cancel,
  4. Map<String, String>? environment,
  5. Duration timeout = wayfinderGitTimeout,
  6. int maxOutputBytes = wayfinderGitMaxOutputBytes,
  7. GitProcessStarter start = startGitProcess,
})

The default GitRunner: runs Git with wayfinderGitOptions in wayfinderGitEnvironment of environment, by default the process's, with stdin closed. Before a command that may reach a remote, it reads core.sshCommand with one more hardened git config call (wayfinderConfiguredSshCommand), so the user's SSH command still runs in batch mode.

start starts each process, startGitProcess by default; a host passes its own process layer here and keeps the rest of this hardening. Stdout is kept as bytes up to maxOutputBytes: a command that writes more is stopped and throws the git-output-too-large WayfinderException rather than return a cut blob. Only the last 64 KiB of stderr are kept.

A command that writes no output for timeout is stopped and throws git-timeout; cancel stops it the same way and throws WayfinderException.cancelled(). Stopping kills the command with the processes it started (GitProcess.kill).

Once a command exits, its stdout is read to its end, and its stderr until it closes or a second passes without any, because a helper the command left running, such as an SSH connection master, can hold stderr open. Both must end within ten seconds of the exit, or timeout when that is shorter. A stdout still open then throws git-output-incomplete, since a result never holds part of it; a stderr still being written then is cut. Either way GitProcess.kill is asked to stop those helpers, and cancel still stops the read.

Implementation

Future<GitResult> runGit(
  List<String> arguments, {
  String? workingDirectory,
  WayfinderCancellation? cancel,
  Map<String, String>? environment,
  Duration timeout = wayfinderGitTimeout,
  int maxOutputBytes = wayfinderGitMaxOutputBytes,
  GitProcessStarter start = startGitProcess,
}) async {
  if (maxOutputBytes <= 0) {
    throw ArgumentError.value(
      maxOutputBytes,
      'maxOutputBytes',
      'must be positive',
    );
  }
  throwIfCancelled(cancel);
  final inherited = environment ?? Platform.environment;
  Future<GitResult> run(List<String> arguments, {String? sshCommand}) => _run(
    arguments,
    workingDirectory: workingDirectory,
    environment: wayfinderGitEnvironment(inherited, sshCommand: sshCommand),
    cancel: cancel,
    timeout: timeout,
    maxOutputBytes: maxOutputBytes,
    start: start,
  );
  final sshCommand = await wayfinderConfiguredSshCommand(
    arguments,
    workingDirectory: workingDirectory,
    environment: inherited,
    run: run,
  );
  return run(arguments, sshCommand: sshCommand);
}