feat: restore working base branch and uncommitted changes

This commit is contained in:
Peter Evans 2022-11-29 15:56:08 +09:00
parent 1847e5d1d6
commit 27a3a9910c
5 changed files with 216 additions and 15 deletions

32
dist/index.js vendored
View file

@ -162,9 +162,8 @@ function createOrUpdateBranch(git, commitMessage, base, branch, branchRemoteName
throw new Error(`Unexpected error: ${commitResult.stderr}`);
}
}
// Remove uncommitted tracked and untracked changes
yield git.exec(['reset', '--hard']);
yield git.exec(['clean', '-f', '-d']);
// Stash any uncommitted tracked and untracked changes
const stashed = yield git.stashPush(['--include-untracked']);
// Perform fetch and reset the working base
// Commits made during the workflow will be removed
if (workingBaseType == WorkingBaseType.Branch) {
@ -262,6 +261,12 @@ function createOrUpdateBranch(git, commitMessage, base, branch, branchRemoteName
result.headSha = yield git.revParse('HEAD');
// Delete the temporary branch
yield git.exec(['branch', '--delete', '--force', tempBranch]);
// Checkout the working base to leave the local repository as it was found
yield git.checkout(workingBase);
// Restore any stashed changes
if (stashed) {
yield git.stashPop();
}
return result;
});
}
@ -434,7 +439,7 @@ function createPullRequest(inputs) {
yield git.push([
'--force-with-lease',
branchRemoteName,
`HEAD:refs/heads/${inputs.branch}`
`${inputs.branch}:refs/heads/${inputs.branch}`
]);
core.endGroup();
}
@ -858,6 +863,25 @@ class GitCommandManager {
return output.stdout.trim();
});
}
stashPush(options) {
return __awaiter(this, void 0, void 0, function* () {
const args = ['stash', 'push'];
if (options) {
args.push(...options);
}
const output = yield this.exec(args);
return output.stdout.trim() !== 'No local changes to save';
});
}
stashPop(options) {
return __awaiter(this, void 0, void 0, function* () {
const args = ['stash', 'pop'];
if (options) {
args.push(...options);
}
yield this.exec(args);
});
}
status(options) {
return __awaiter(this, void 0, void 0, function* () {
const args = ['status'];