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:
Paul Gschwendtner
2020-06-25 16:15:14 +02:00
committed by Andrew Kushnir
parent eee2fd22e0
commit dbc2364d16
5 changed files with 33 additions and 26 deletions

View File

@ -50,10 +50,10 @@ export async function rebasePr(
}
/**
* The branch originally checked out before this method performs any Git
* operations that may change the working branch.
* The branch or revision originally checked out before this method performed
* any Git operations that may change the working branch.
*/
const originalBranch = git.getCurrentBranch();
const previousBranchOrRevision = git.getCurrentBranchOrRevision();
/* Get the PR information from Github. */
const pr = await getPr(PR_SCHEMA, prNumber, config.github);
@ -121,7 +121,7 @@ export async function rebasePr(
info();
info(`To abort the rebase and return to the state of the repository before this command`);
info(`run the following command:`);
info(` $ git rebase --abort && git reset --hard && git checkout ${originalBranch}`);
info(` $ git rebase --abort && git reset --hard && git checkout ${previousBranchOrRevision}`);
process.exit(1);
} else {
info(`Cleaning up git state, and restoring previous state.`);
@ -137,7 +137,7 @@ export async function rebasePr(
// Ensure that any changes in the current repo state are cleared.
git.runGraceful(['reset', '--hard'], {stdio: 'ignore'});
// Checkout the original branch from before the run began.
git.runGraceful(['checkout', originalBranch], {stdio: 'ignore'});
git.runGraceful(['checkout', previousBranchOrRevision], {stdio: 'ignore'});
}
}