Unverified Commit 316d4498 authored by Jonah Williams's avatar Jonah Williams Committed by GitHub

warn on uncomitted changes (#30235)

parent f66ee3e4
......@@ -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.
......
......@@ -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 {}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment