Skip to content

Commit 45c510e

Browse files
authored
Merge pull request #672 from peter-evans/reset-perf
perf: git reset instead of fetch
2 parents 6c2b44c + 249b80d commit 45c510e

File tree

3 files changed

+33
-8
lines changed

3 files changed

+33
-8
lines changed

dist/index.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,19 @@ function createOrUpdateBranch(git, commitMessage, base, branch, branchRemoteName
134134
// Perform fetch and reset the working base
135135
// Commits made during the workflow will be removed
136136
if (workingBaseType == WorkingBaseType.Branch) {
137-
core.info(`Resetting working base branch '${workingBase}' to its remote`);
138-
yield git.fetch([`${workingBase}:${workingBase}`], baseRemote, ['--force']);
137+
core.info(`Resetting working base branch '${workingBase}'`);
138+
if (branchRemoteName == 'fork') {
139+
// If pushing to a fork we must fetch with 'unshallow' to avoid the following error on git push
140+
// ! [remote rejected] HEAD -> tests/push-branch-to-fork (shallow update not allowed)
141+
yield git.fetch([`${workingBase}:${workingBase}`], baseRemote, [
142+
'--force'
143+
]);
144+
}
145+
else {
146+
// If the remote is 'origin' we can git reset
147+
yield git.checkout(workingBase);
148+
yield git.exec(['reset', '--hard', `${baseRemote}/${workingBase}`]);
149+
}
139150
}
140151
// If the working base is not the base, rebase the temp branch commits
141152
// This will also be true if the working base type is a commit
@@ -162,7 +173,7 @@ function createOrUpdateBranch(git, commitMessage, base, branch, branchRemoteName
162173
// The pull request branch does not exist
163174
core.info(`Pull request branch '${branch}' does not exist yet.`);
164175
// Create the pull request branch
165-
yield git.checkout(branch, 'HEAD');
176+
yield git.checkout(branch, tempBranch);
166177
// Check if the pull request branch is ahead of the base
167178
result.hasDiffWithBase = yield isAhead(git, base, branch);
168179
if (result.hasDiffWithBase) {

package-lock.json

Lines changed: 6 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/create-or-update-branch.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,18 @@ export async function createOrUpdateBranch(
130130
// Perform fetch and reset the working base
131131
// Commits made during the workflow will be removed
132132
if (workingBaseType == WorkingBaseType.Branch) {
133-
core.info(`Resetting working base branch '${workingBase}' to its remote`)
134-
await git.fetch([`${workingBase}:${workingBase}`], baseRemote, ['--force'])
133+
core.info(`Resetting working base branch '${workingBase}'`)
134+
if (branchRemoteName == 'fork') {
135+
// If pushing to a fork we must fetch with 'unshallow' to avoid the following error on git push
136+
// ! [remote rejected] HEAD -> tests/push-branch-to-fork (shallow update not allowed)
137+
await git.fetch([`${workingBase}:${workingBase}`], baseRemote, [
138+
'--force'
139+
])
140+
} else {
141+
// If the remote is 'origin' we can git reset
142+
await git.checkout(workingBase)
143+
await git.exec(['reset', '--hard', `${baseRemote}/${workingBase}`])
144+
}
135145
}
136146

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

0 commit comments

Comments
 (0)