warn on uncomitted changes (#30235)

This commit is contained in:
Jonah Williams 2019-04-10 12:24:53 -07:00 committed by GitHub
parent f66ee3e470
commit 316d44989a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 0 deletions

View File

@ -72,6 +72,17 @@ class UpgradeCommandRunner {
);
}
}
// If there are uncomitted changes we might be on the right commit but
// we should still warn.
if (!force && await hasUncomittedChanges()) {
throwToolExit(
'Your flutter checkout has local changes that would be erased by '
'upgrading. If you want to keep these changes, it is recommended that '
'you stash them via "git stash" or else commit the changes to a local '
'branch. If it is okay to remove local changes, then re-run this '
'command with --force.'
);
}
await resetChanges(gitTagVersion);
await upgradeChannel(flutterVersion);
await attemptFastForward();
@ -81,6 +92,18 @@ class UpgradeCommandRunner {
return null;
}
Future<bool> hasUncomittedChanges() async {
try {
final RunResult result = await runCheckedAsync(<String>[
'git', 'status', '-s'
], workingDirectory: Cache.flutterRoot);
return result.stdout.trim().isNotEmpty;
} catch (e) {
throwToolExit('git status failed: $e');
}
return false;
}
/// Check if there is an upstream repository configured.
///
/// Exits tool if there is no upstream.

View File

@ -29,6 +29,7 @@ void main() {
fakeCommandRunner = FakeUpgradeCommandRunner();
realCommandRunner = UpgradeCommandRunner();
processManager = MockProcessManager();
fakeCommandRunner.willHaveUncomittedChanges = false;
});
test('throws on unknown tag, official branch, noforce', () async {
@ -49,6 +50,26 @@ void main() {
expect(await result, null);
});
test('throws tool exit with uncommited changes', () async {
fakeCommandRunner.willHaveUncomittedChanges = true;
final Future<FlutterCommandResult> result = fakeCommandRunner.runCommand(
false,
gitTagVersion,
flutterVersion,
);
expect(result, throwsA(isA<ToolExit>()));
});
test('does not throw tool exit with uncommited changes and force', () async {
fakeCommandRunner.willHaveUncomittedChanges = true;
final Future<FlutterCommandResult> result = fakeCommandRunner.runCommand(
true,
gitTagVersion,
flutterVersion,
);
expect(await result, null);
});
test('Doesn\'t throw on known tag, dev branch, no force', () async {
final Future<FlutterCommandResult> result = fakeCommandRunner.runCommand(
false,
@ -127,9 +148,14 @@ void main() {
}
class FakeUpgradeCommandRunner extends UpgradeCommandRunner {
bool willHaveUncomittedChanges = false;
@override
Future<void> verifyUpstreamConfigured() async {}
@override
Future<bool> hasUncomittedChanges() async => willHaveUncomittedChanges;
@override
Future<void> resetChanges(GitTagVersion gitTagVersion) async {}