From 53d7eb4f401bc5c8b06c8a68b952582fe56c1982 Mon Sep 17 00:00:00 2001 From: Eric Webb Date: Tue, 12 Mar 2024 06:54:01 -0700 Subject: [PATCH 1/3] Update git.fetch calls to use depth=1 (#2810) * When base is set, fetch depth=1 * PR Feedback - remove depth=1 from tryFetch function --- dist/index.js | 4 ++-- src/create-or-update-branch.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/dist/index.js b/dist/index.js index bca7be4f4..bc0bfac20 100644 --- a/dist/index.js +++ b/dist/index.js @@ -184,7 +184,7 @@ function createOrUpdateBranch(git, commitMessage, base, branch, branchRemoteName if (workingBase != base) { core.info(`Rebasing commits made to ${workingBaseType} '${workingBase}' on to base branch '${base}'`); // Checkout the actual base - yield git.fetch([`${base}:${base}`], baseRemote, ['--force']); + yield git.fetch([`${base}:${base}`], baseRemote, ['--force', '--depth=1']); yield git.checkout(base); // Cherrypick commits from the temporary branch starting from the working base const commits = yield git.revList([`${workingBase}..${tempBranch}`, '.'], ['--reverse']); @@ -197,7 +197,7 @@ function createOrUpdateBranch(git, commitMessage, base, branch, branchRemoteName // Reset the temp branch to the working index yield git.checkout(tempBranch, 'HEAD'); // Reset the base - yield git.fetch([`${base}:${base}`], baseRemote, ['--force']); + yield git.fetch([`${base}:${base}`], baseRemote, ['--force', '--depth=1']); } // Try to fetch the pull request branch if (!(yield tryFetch(git, branchRemoteName, branch))) { diff --git a/src/create-or-update-branch.ts b/src/create-or-update-branch.ts index 1c55ea5fc..b048d5db9 100644 --- a/src/create-or-update-branch.ts +++ b/src/create-or-update-branch.ts @@ -200,7 +200,7 @@ export async function createOrUpdateBranch( `Rebasing commits made to ${workingBaseType} '${workingBase}' on to base branch '${base}'` ) // Checkout the actual base - await git.fetch([`${base}:${base}`], baseRemote, ['--force']) + await git.fetch([`${base}:${base}`], baseRemote, ['--force', '--depth=1']) await git.checkout(base) // Cherrypick commits from the temporary branch starting from the working base const commits = await git.revList( @@ -219,7 +219,7 @@ export async function createOrUpdateBranch( // Reset the temp branch to the working index await git.checkout(tempBranch, 'HEAD') // Reset the base - await git.fetch([`${base}:${base}`], baseRemote, ['--force']) + await git.fetch([`${base}:${base}`], baseRemote, ['--force', '--depth=1']) } // Try to fetch the pull request branch From 810fe9fda774e946cfc77452d6bb00a4a118c309 Mon Sep 17 00:00:00 2001 From: Peter Evans <18365890+peter-evans@users.noreply.github.com> Date: Tue, 12 Mar 2024 13:55:03 +0000 Subject: [PATCH 2/3] push-to-fork fix --- dist/index.js | 9 +++++++-- src/create-or-update-branch.ts | 9 +++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/dist/index.js b/dist/index.js index bc0bfac20..57130a626 100644 --- a/dist/index.js +++ b/dist/index.js @@ -183,8 +183,13 @@ function createOrUpdateBranch(git, commitMessage, base, branch, branchRemoteName // This will also be true if the working base type is a commit if (workingBase != base) { core.info(`Rebasing commits made to ${workingBaseType} '${workingBase}' on to base branch '${base}'`); + const fetchArgs = ['--force']; + if (branchRemoteName != 'fork') { + // If pushing to a fork we cannot shallow fetch otherwise the 'shallow update not allowed' error occurs + fetchArgs.push('--depth=1'); + } // Checkout the actual base - yield git.fetch([`${base}:${base}`], baseRemote, ['--force', '--depth=1']); + yield git.fetch([`${base}:${base}`], baseRemote, fetchArgs); yield git.checkout(base); // Cherrypick commits from the temporary branch starting from the working base const commits = yield git.revList([`${workingBase}..${tempBranch}`, '.'], ['--reverse']); @@ -197,7 +202,7 @@ function createOrUpdateBranch(git, commitMessage, base, branch, branchRemoteName // Reset the temp branch to the working index yield git.checkout(tempBranch, 'HEAD'); // Reset the base - yield git.fetch([`${base}:${base}`], baseRemote, ['--force', '--depth=1']); + yield git.fetch([`${base}:${base}`], baseRemote, fetchArgs); } // Try to fetch the pull request branch if (!(yield tryFetch(git, branchRemoteName, branch))) { diff --git a/src/create-or-update-branch.ts b/src/create-or-update-branch.ts index b048d5db9..000dc513d 100644 --- a/src/create-or-update-branch.ts +++ b/src/create-or-update-branch.ts @@ -199,8 +199,13 @@ export async function createOrUpdateBranch( core.info( `Rebasing commits made to ${workingBaseType} '${workingBase}' on to base branch '${base}'` ) + const fetchArgs = ['--force'] + if (branchRemoteName != 'fork') { + // If pushing to a fork we cannot shallow fetch otherwise the 'shallow update not allowed' error occurs + fetchArgs.push('--depth=1') + } // Checkout the actual base - await git.fetch([`${base}:${base}`], baseRemote, ['--force', '--depth=1']) + await git.fetch([`${base}:${base}`], baseRemote, fetchArgs) await git.checkout(base) // Cherrypick commits from the temporary branch starting from the working base const commits = await git.revList( @@ -219,7 +224,7 @@ export async function createOrUpdateBranch( // Reset the temp branch to the working index await git.checkout(tempBranch, 'HEAD') // Reset the base - await git.fetch([`${base}:${base}`], baseRemote, ['--force', '--depth=1']) + await git.fetch([`${base}:${base}`], baseRemote, fetchArgs) } // Try to fetch the pull request branch From 03ef719859de9c0e8554230f6c1c92084745c7c2 Mon Sep 17 00:00:00 2001 From: Peter Evans <18365890+peter-evans@users.noreply.github.com> Date: Tue, 12 Mar 2024 13:55:31 +0000 Subject: [PATCH 3/3] test updates to handle shallow fetch of base --- __test__/create-or-update-branch.int.test.ts | 29 ++++++++++++++------ 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/__test__/create-or-update-branch.int.test.ts b/__test__/create-or-update-branch.int.test.ts index 0f122541d..e392ad439 100644 --- a/__test__/create-or-update-branch.int.test.ts +++ b/__test__/create-or-update-branch.int.test.ts @@ -140,10 +140,22 @@ describe('create-or-update-branch tests', () => { }) async function beforeTest(): Promise { + await git.fetch( + [`${DEFAULT_BRANCH}:${DEFAULT_BRANCH}`], + REMOTE_NAME, + ['--force', '--update-head-ok'], + true + ) await git.checkout(DEFAULT_BRANCH) } async function afterTest(deleteRemote = true): Promise { + await git.fetch( + [`${DEFAULT_BRANCH}:${DEFAULT_BRANCH}`], + REMOTE_NAME, + ['--force', '--update-head-ok'], + true + ) await git.checkout(DEFAULT_BRANCH) try { // Get the upstream branch if it exists @@ -1454,8 +1466,7 @@ describe('create-or-update-branch tests', () => { expect( await gitLogMatches([ _commitMessage, - ...commits.commitMsgs, - INIT_COMMIT_MESSAGE + commits.commitMsgs[0] // fetch depth of base is 1 ]) ).toBeTruthy() }) @@ -1590,7 +1601,9 @@ describe('create-or-update-branch tests', () => { expect(await getFileContent(TRACKED_FILE)).toEqual(_changes.tracked) expect(await getFileContent(UNTRACKED_FILE)).toEqual(_changes.untracked) expect( - await gitLogMatches([...commits.commitMsgs, INIT_COMMIT_MESSAGE]) + await gitLogMatches([ + commits.commitMsgs[0] // fetch depth of base is 1 + ]) ).toBeTruthy() }) @@ -1668,7 +1681,9 @@ describe('create-or-update-branch tests', () => { expect(await getFileContent(TRACKED_FILE)).toEqual(_changes.tracked) expect(await getFileContent(UNTRACKED_FILE)).toEqual(_changes.untracked) expect( - await gitLogMatches([...commits.commitMsgs, INIT_COMMIT_MESSAGE]) + await gitLogMatches([ + commits.commitMsgs[0] // fetch depth of base is 1 + ]) ).toBeTruthy() }) @@ -1951,8 +1966,7 @@ describe('create-or-update-branch tests', () => { await gitLogMatches([ _commitMessage, ..._commits.commitMsgs, - ...commitsOnBase.commitMsgs, - INIT_COMMIT_MESSAGE + commitsOnBase.commitMsgs[0] // fetch depth of base is 1 ]) ).toBeTruthy() }) @@ -2147,8 +2161,7 @@ describe('create-or-update-branch tests', () => { expect( await gitLogMatches([ _commitMessage, - ...commitsOnBase.commitMsgs, - INIT_COMMIT_MESSAGE + commitsOnBase.commitMsgs[0] // fetch depth of base is 1 ]) ).toBeTruthy() })