mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
Closes https://github.com/flutter/flutter/issues/74165. The original issue called for, on Windows, telling `CYGWIN` to use `=noglob`, to work around some git operation errors that happen when using non-native Git. There ... was no great way to do this with the existing codebase without, IMO, adding lots of confusing code. So, I refactored all the calls of: - before: `processUtils.<method>(['git', ...args], ...params)` - after: `git.<method>([...args], ...params)` ... and implicitly add the new environment variables, if `Platform.isWindows`. Did some minor test cleanup and process execution cleanup while I was at it.
132 lines
4.5 KiB
Dart
132 lines
4.5 KiB
Dart
// Copyright 2014 The Flutter Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
// found in the LICENSE file.
|
|
|
|
import 'base/io.dart';
|
|
import 'base/platform.dart';
|
|
import 'base/process.dart';
|
|
import 'convert.dart';
|
|
|
|
/// Wrapper around the command-line `git` executable on the host.
|
|
interface class Git {
|
|
/// Creates a wrapper that executes `git` using [runProcessWith].
|
|
Git({
|
|
required Platform currentPlatform,
|
|
required ProcessUtils runProcessWith,
|
|
String executable = 'git',
|
|
}) : _platform = currentPlatform,
|
|
_processUtils = runProcessWith,
|
|
_pathToGitExecutable = executable;
|
|
|
|
final String _pathToGitExecutable;
|
|
final Platform _platform;
|
|
final ProcessUtils _processUtils;
|
|
|
|
/// Returns the result of `git log <arguments>`.
|
|
///
|
|
/// Automatically injects the arguments `-c log.showSignature=false` in order
|
|
/// to ignore user settings that will break the expected output for this call;
|
|
/// otherwise this call is identical to using [Git.runSync] directly.
|
|
RunResult logSync(List<String> arguments, {String? workingDirectory}) {
|
|
assert(arguments.isEmpty || arguments.first != 'log');
|
|
return runSync([
|
|
..._ignoreLogShowSignature,
|
|
'log',
|
|
...arguments,
|
|
], workingDirectory: workingDirectory);
|
|
}
|
|
|
|
static const _ignoreLogShowSignature = ['-c', 'log.showSignature=false'];
|
|
|
|
/// Spawns a child process to run `git`.
|
|
///
|
|
/// The arguments are the same as [ProcessUtils.run], except:
|
|
///
|
|
/// - [arguments] does _not_ include the executable (it is implicit);
|
|
/// - [environment] may include additional (implicit) platform-specific variables
|
|
Future<RunResult> run(
|
|
List<String> arguments, {
|
|
bool throwOnError = false,
|
|
RunResultChecker? allowedFailures,
|
|
String? workingDirectory,
|
|
bool allowReentrantFlutter = false,
|
|
Map<String, String>? environment,
|
|
Duration? timeout,
|
|
int timeoutRetries = 0,
|
|
}) {
|
|
return _processUtils.run(
|
|
[_pathToGitExecutable, ...arguments],
|
|
throwOnError: throwOnError,
|
|
allowedFailures: allowedFailures,
|
|
workingDirectory: workingDirectory,
|
|
allowReentrantFlutter: allowReentrantFlutter,
|
|
environment: {if (_platform.isWindows) ..._useNoGlobCygwinGit, ...?environment},
|
|
timeout: timeout,
|
|
timeoutRetries: timeoutRetries,
|
|
);
|
|
}
|
|
|
|
/// Runs a command using `git` and blocks waiting for its result.
|
|
///
|
|
/// The arguments are the same as [ProcessUtils.runSync], except:
|
|
///
|
|
/// - [arguments] does _not_ include the executable (it is implicit);
|
|
/// - [environment] may include additional (implicit) platform-specific variables
|
|
RunResult runSync(
|
|
List<String> arguments, {
|
|
bool throwOnError = false,
|
|
bool verboseExceptions = false,
|
|
RunResultChecker? allowedFailures,
|
|
bool hideStdout = false,
|
|
String? workingDirectory,
|
|
Map<String, String>? environment,
|
|
bool allowReentrantFlutter = false,
|
|
Encoding encoding = systemEncoding,
|
|
}) {
|
|
return _processUtils.runSync(
|
|
[_pathToGitExecutable, ...arguments],
|
|
throwOnError: throwOnError,
|
|
verboseExceptions: verboseExceptions,
|
|
allowedFailures: allowedFailures,
|
|
hideStdout: hideStdout,
|
|
workingDirectory: workingDirectory,
|
|
environment: {if (_platform.isWindows) ..._useNoGlobCygwinGit, ...?environment},
|
|
allowReentrantFlutter: allowReentrantFlutter,
|
|
encoding: encoding,
|
|
);
|
|
}
|
|
|
|
/// Spawns a child process to run `git` and streams the result to stdout/err.
|
|
///
|
|
/// The arguments are the same as [ProcessUtils.stream], except:
|
|
///
|
|
/// - [arguments] does _not_ include the executable (it is implicit);
|
|
/// - [environment] may include additional (implicit) platform-specific variables
|
|
Future<int> stream(
|
|
List<String> arguments, {
|
|
String? workingDirectory,
|
|
bool allowReentrantFlutter = false,
|
|
String prefix = '',
|
|
bool trace = false,
|
|
RegExp? filter,
|
|
RegExp? stdoutErrorMatcher,
|
|
StringConverter? mapFunction,
|
|
Map<String, String>? environment,
|
|
}) {
|
|
assert(arguments.isEmpty || arguments.first != 'git');
|
|
return _processUtils.stream(
|
|
[_pathToGitExecutable, ...arguments],
|
|
workingDirectory: workingDirectory,
|
|
allowReentrantFlutter: allowReentrantFlutter,
|
|
prefix: prefix,
|
|
trace: trace,
|
|
filter: filter,
|
|
stdoutErrorMatcher: stdoutErrorMatcher,
|
|
mapFunction: mapFunction,
|
|
environment: {if (_platform.isWindows) ..._useNoGlobCygwinGit, ...?environment},
|
|
);
|
|
}
|
|
|
|
static const _useNoGlobCygwinGit = {'MSYS': 'noglob', 'CYGWIN': 'noglob'};
|
|
}
|