[flutter_conductor] catch and warn when pushing working branch to mirror (#94506)

This commit is contained in:
Christopher Fujino
2021-12-06 16:59:05 -08:00
committed by GitHub
parent ad3fe21cbc
commit eea6c54dc2
4 changed files with 178 additions and 31 deletions

View File

@@ -74,15 +74,55 @@ class Git {
if ((result.stderr as String).isNotEmpty) {
message.writeln('stderr from git:\n${result.stderr}\n');
}
throw GitException(message.toString());
throw GitException(message.toString(), args);
}
}
enum GitExceptionType {
/// Git push failed because the remote branch contained commits the local did
/// not.
///
/// Either the local branch was wrong, and needs a rebase before pushing
/// again, or the remote branch needs to be overwritten with a force push.
///
/// Example output:
///
/// ```
/// To github.com:user/engine.git
///
/// ! [rejected] HEAD -> cherrypicks-flutter-2.8-candidate.3 (non-fast-forward)
/// error: failed to push some refs to 'github.com:user/engine.git'
/// hint: Updates were rejected because the tip of your current branch is behind
/// hint: its remote counterpart. Integrate the remote changes (e.g.
/// hint: 'git pull ...') before pushing again.
/// hint: See the 'Note about fast-forwards' in 'git push --help' for details.
/// ```
PushRejected,
}
/// An exception created because a git subprocess failed.
///
/// Known git failures will be assigned a [GitExceptionType] in the [type]
/// field. If this field is null it means and unknown git failure.
class GitException implements Exception {
GitException(this.message);
GitException(this.message, this.args) {
if (_pushRejectedPattern.hasMatch(message)) {
type = GitExceptionType.PushRejected;
} else {
// because type is late final, it must be explicitly set before it is
// accessed.
type = null;
}
}
static final RegExp _pushRejectedPattern = RegExp(
r'Updates were rejected because the tip of your current branch is behind',
);
final String message;
final List<String> args;
late final GitExceptionType? type;
@override
String toString() => 'Exception: $message';
String toString() => 'Exception on command "${args.join(' ')}": $message';
}

View File

@@ -4,8 +4,10 @@
import 'package:args/command_runner.dart';
import 'package:file/file.dart' show File;
import 'package:meta/meta.dart' show visibleForTesting;
import 'context.dart';
import 'git.dart';
import 'globals.dart';
import 'proto/conductor_state.pb.dart' as pb;
import 'proto/conductor_state.pbenum.dart';
@@ -152,13 +154,7 @@ class NextContext extends Context {
}
}
await engine.pushRef(
fromRef: 'HEAD',
// Explicitly create new branch
toRef: 'refs/heads/${state.engine.workingBranch}',
remote: state.engine.mirror.name,
);
await pushWorkingBranch(engine, state.engine);
break;
case pb.ReleasePhase.CODESIGN_ENGINE_BINARIES:
stdio.printStatus(<String>[
@@ -285,12 +281,7 @@ class NextContext extends Context {
}
}
await framework.pushRef(
fromRef: 'HEAD',
// Explicitly create new branch
toRef: 'refs/heads/${state.framework.workingBranch}',
remote: state.framework.mirror.name,
);
await pushWorkingBranch(framework, state.framework);
break;
case pb.ReleasePhase.PUBLISH_VERSION:
stdio.printStatus('Please ensure that you have merged your framework PR and that');
@@ -381,4 +372,37 @@ class NextContext extends Context {
updateState(state, stdio.logs);
}
/// Push the working branch to the user's mirror.
///
/// [repository] represents the actual Git repository on disk, and is used to
/// call `git push`, while [pbRepository] represents the user-specified
/// configuration for the repository, and is used to read the name of the
/// working branch and the mirror's remote name.
///
/// May throw either a [ConductorException] if the user already has a branch
/// of the same name on their mirror, or a [GitException] for any other
/// failures from the underlying git process call.
@visibleForTesting
Future<void> pushWorkingBranch(Repository repository, pb.Repository pbRepository) async {
try {
await repository.pushRef(
fromRef: 'HEAD',
// Explicitly create new branch
toRef: 'refs/heads/${pbRepository.workingBranch}',
remote: pbRepository.mirror.name,
force: force,
);
} on GitException catch (exception) {
if (exception.type == GitExceptionType.PushRejected && force == false) {
throw ConductorException(
'Push failed because the working branch named '
'${pbRepository.workingBranch} already exists on your mirror. '
'Re-run this command with --force to overwrite the remote branch.\n'
'${exception.message}',
);
}
rethrow;
}
}
}

View File

@@ -556,7 +556,7 @@ class FrameworkRepository extends Repository {
}
@override
Future<Repository> cloneRepository(String? cloneName) async {
Future<FrameworkRepository> cloneRepository(String? cloneName) async {
assert(localUpstream);
cloneName ??= 'clone-of-$name';
return FrameworkRepository(