fix(dev-infra): support running scripts from within a detached head (#37737)
Scripts provided in the `ng-dev` command might use local `git` commands. For such scripts, we keep track of the branch that has been checked out before the command has been invoked. We do this so that we can later (upon command completion) restore back to the original branch. We do not want to leave the Git repository in a dirty state. It looks like this logic currently only deals with branches but does not work properly when a command is invoked from a detached head. We can make it work by just checking out the previous revision (if no branch is checked out). PR Close #37737
This commit is contained in:

committed by
Andrew Kushnir

parent
eee2fd22e0
commit
dbc2364d16
@ -59,7 +59,7 @@ export class AutosquashMergeStrategy extends MergeStrategy {
|
||||
// is desired, we set the `GIT_SEQUENCE_EDITOR` environment variable to `true` so that
|
||||
// the rebase seems interactive to Git, while it's not interactive to the user.
|
||||
// See: https://github.com/git/git/commit/891d4a0313edc03f7e2ecb96edec5d30dc182294.
|
||||
const branchBeforeRebase = this.git.getCurrentBranch();
|
||||
const branchOrRevisionBeforeRebase = this.git.getCurrentBranchOrRevision();
|
||||
const rebaseEnv =
|
||||
needsCommitMessageFixup ? undefined : {...process.env, GIT_SEQUENCE_EDITOR: 'true'};
|
||||
this.git.run(
|
||||
@ -69,9 +69,9 @@ export class AutosquashMergeStrategy extends MergeStrategy {
|
||||
// Update pull requests commits to reference the pull request. This matches what
|
||||
// Github does when pull requests are merged through the Web UI. The motivation is
|
||||
// that it should be easy to determine which pull request contained a given commit.
|
||||
// **Note**: The filter-branch command relies on the working tree, so we want to make
|
||||
// sure that we are on the initial branch where the merge script has been run.
|
||||
this.git.run(['checkout', '-f', branchBeforeRebase]);
|
||||
// Note: The filter-branch command relies on the working tree, so we want to make sure
|
||||
// that we are on the initial branch or revision where the merge script has been invoked.
|
||||
this.git.run(['checkout', '-f', branchOrRevisionBeforeRebase]);
|
||||
this.git.run(
|
||||
['filter-branch', '-f', '--msg-filter', `${MSG_FILTER_SCRIPT} ${prNumber}`, revisionRange]);
|
||||
|
||||
|
@ -87,14 +87,14 @@ export class PullRequestMergeTask {
|
||||
new GithubApiMergeStrategy(this.git, this.config.githubApiMerge) :
|
||||
new AutosquashMergeStrategy(this.git);
|
||||
|
||||
// Branch that is currently checked out so that we can switch back to it once
|
||||
// the pull request has been merged.
|
||||
let previousBranch: null|string = null;
|
||||
// Branch or revision that is currently checked out so that we can switch back to
|
||||
// it once the pull request has been merged.
|
||||
let previousBranchOrRevision: null|string = null;
|
||||
|
||||
// The following block runs Git commands as child processes. These Git commands can fail.
|
||||
// We want to capture these command errors and return an appropriate merge request status.
|
||||
try {
|
||||
previousBranch = this.git.getCurrentBranch();
|
||||
previousBranchOrRevision = this.git.getCurrentBranchOrRevision();
|
||||
|
||||
// Run preparations for the merge (e.g. fetching branches).
|
||||
await strategy.prepare(pullRequest);
|
||||
@ -107,7 +107,7 @@ export class PullRequestMergeTask {
|
||||
|
||||
// Switch back to the previous branch. We need to do this before deleting the temporary
|
||||
// branches because we cannot delete branches which are currently checked out.
|
||||
this.git.run(['checkout', '-f', previousBranch]);
|
||||
this.git.run(['checkout', '-f', previousBranchOrRevision]);
|
||||
|
||||
await strategy.cleanup(pullRequest);
|
||||
|
||||
@ -123,8 +123,8 @@ export class PullRequestMergeTask {
|
||||
} finally {
|
||||
// Always try to restore the branch if possible. We don't want to leave
|
||||
// the repository in a different state than before.
|
||||
if (previousBranch !== null) {
|
||||
this.git.runGraceful(['checkout', '-f', previousBranch]);
|
||||
if (previousBranchOrRevision !== null) {
|
||||
this.git.runGraceful(['checkout', '-f', previousBranchOrRevision]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user