Fix crash on flutter update-packages (#28922)

When running git update-packages, if the goldens repo is dirty, the
flutter tool currently crashes and logs the failure from git pull.

This adds a check for a dirty git client and emits a friendlier error
message in the case where it's not clean, and avoid the crash by
catching the NonZeroExitCode exception and rethrowing as toolExit
instead.

Fixes: https://github.com/flutter/flutter/issues/28915
This commit is contained in:
Chris Bracken 2019-03-06 09:14:39 -08:00 committed by GitHub
parent 4c1f4d14e1
commit 52e4214dbb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 3 deletions

View file

@ -80,6 +80,7 @@ class GoldensClient {
if (currentCommit == null) {
await _initRepository();
}
await _checkCanSync();
await _syncTo(goldensCommit);
}
} finally {
@ -117,6 +118,23 @@ class GoldensClient {
);
}
Future<void> _checkCanSync() async {
final io.ProcessResult result = await process.run(
<String>['git', 'status', '--porcelain'],
workingDirectory: repositoryRoot.path,
);
if (result.stdout.trim().isNotEmpty) {
final StringBuffer buf = StringBuffer();
buf
..writeln('flutter_goldens git checkout at ${repositoryRoot.path} has local changes and cannot be synced.')
..writeln('To reset your client to a clean state, and lose any local golden test changes:')
..writeln('cd ${repositoryRoot.path}')
..writeln('git reset --hard HEAD')
..writeln('git clean -x -d -f -f');
throw NonZeroExitCode(1, buf.toString());
}
}
Future<void> _syncTo(String commit) async {
await _runCommands(
<String>[
@ -163,7 +181,7 @@ class NonZeroExitCode implements Exception {
/// The first argument must be non-zero.
const NonZeroExitCode(this.exitCode, this.stderr) : assert(exitCode != 0);
/// The code that the process will signal to th eoperating system.
/// The code that the process will signal to the operating system.
///
/// By definiton, this is not zero.
final int exitCode;

View file

@ -132,8 +132,12 @@ class UpdatePackagesCommand extends FlutterCommand {
// package that is in the goldens repository. We need to make sure that the goldens
// repository is cloned locally before we verify or update pubspecs.
printStatus('Cloning goldens repository...');
final GoldensClient goldensClient = GoldensClient();
await goldensClient.prepare();
try {
final GoldensClient goldensClient = GoldensClient();
await goldensClient.prepare();
} on NonZeroExitCode catch (e) {
throwToolExit(e.stderr, exitCode: e.exitCode);
}
if (isVerifyOnly) {
bool needsUpdate = false;