Skip to content

Commit

Permalink
perf: git reset instead of fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-evans committed Dec 23, 2020
1 parent 6c2b44c commit 249b80d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
17 changes: 14 additions & 3 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,19 @@ function createOrUpdateBranch(git, commitMessage, base, branch, branchRemoteName
// Perform fetch and reset the working base
// Commits made during the workflow will be removed
if (workingBaseType == WorkingBaseType.Branch) {
core.info(`Resetting working base branch '${workingBase}' to its remote`);
yield git.fetch([`${workingBase}:${workingBase}`], baseRemote, ['--force']);
core.info(`Resetting working base branch '${workingBase}'`);
if (branchRemoteName == 'fork') {
// If pushing to a fork we must fetch with 'unshallow' to avoid the following error on git push
// ! [remote rejected] HEAD -> tests/push-branch-to-fork (shallow update not allowed)
yield git.fetch([`${workingBase}:${workingBase}`], baseRemote, [
'--force'
]);
}
else {
// If the remote is 'origin' we can git reset
yield git.checkout(workingBase);
yield git.exec(['reset', '--hard', `${baseRemote}/${workingBase}`]);
}
}
// If the working base is not the base, rebase the temp branch commits
// This will also be true if the working base type is a commit
Expand All @@ -162,7 +173,7 @@ function createOrUpdateBranch(git, commitMessage, base, branch, branchRemoteName
// The pull request branch does not exist
core.info(`Pull request branch '${branch}' does not exist yet.`);
// Create the pull request branch
yield git.checkout(branch, 'HEAD');
yield git.checkout(branch, tempBranch);
// Check if the pull request branch is ahead of the base
result.hasDiffWithBase = yield isAhead(git, base, branch);
if (result.hasDiffWithBase) {
Expand Down
8 changes: 6 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 13 additions & 3 deletions src/create-or-update-branch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,18 @@ export async function createOrUpdateBranch(
// Perform fetch and reset the working base
// Commits made during the workflow will be removed
if (workingBaseType == WorkingBaseType.Branch) {
core.info(`Resetting working base branch '${workingBase}' to its remote`)
await git.fetch([`${workingBase}:${workingBase}`], baseRemote, ['--force'])
core.info(`Resetting working base branch '${workingBase}'`)
if (branchRemoteName == 'fork') {
// If pushing to a fork we must fetch with 'unshallow' to avoid the following error on git push
// ! [remote rejected] HEAD -> tests/push-branch-to-fork (shallow update not allowed)
await git.fetch([`${workingBase}:${workingBase}`], baseRemote, [
'--force'
])
} else {
// If the remote is 'origin' we can git reset
await git.checkout(workingBase)
await git.exec(['reset', '--hard', `${baseRemote}/${workingBase}`])
}
}

// If the working base is not the base, rebase the temp branch commits
Expand Down Expand Up @@ -168,7 +178,7 @@ export async function createOrUpdateBranch(
// The pull request branch does not exist
core.info(`Pull request branch '${branch}' does not exist yet.`)
// Create the pull request branch
await git.checkout(branch, 'HEAD')
await git.checkout(branch, tempBranch)
// Check if the pull request branch is ahead of the base
result.hasDiffWithBase = await isAhead(git, base, branch)
if (result.hasDiffWithBase) {
Expand Down

0 comments on commit 249b80d

Please sign in to comment.