From 9153d834b60caba6d51c9b9510b087acf9f33f83 Mon Sep 17 00:00:00 2001 From: Peter Evans <18365890+peter-evans@users.noreply.github.com> Date: Wed, 17 Apr 2024 20:00:12 +0900 Subject: [PATCH] perf: limit the fetch depth of pr branch (#2857) --- __test__/create-or-update-branch.int.test.ts | 4 ++-- dist/index.js | 14 ++++++++++---- src/create-or-update-branch.ts | 18 ++++++++++++++---- 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/__test__/create-or-update-branch.int.test.ts b/__test__/create-or-update-branch.int.test.ts index e392ad439c..3e8414b178 100644 --- a/__test__/create-or-update-branch.int.test.ts +++ b/__test__/create-or-update-branch.int.test.ts @@ -210,8 +210,8 @@ describe('create-or-update-branch tests', () => { } it('tests if a branch exists and can be fetched', async () => { - expect(await tryFetch(git, REMOTE_NAME, NOT_BASE_BRANCH)).toBeTruthy() - expect(await tryFetch(git, REMOTE_NAME, NOT_EXIST_BRANCH)).toBeFalsy() + expect(await tryFetch(git, REMOTE_NAME, NOT_BASE_BRANCH, 1)).toBeTruthy() + expect(await tryFetch(git, REMOTE_NAME, NOT_EXIST_BRANCH, 1)).toBeFalsy() }) it('tests getWorkingBaseAndType on a checked out ref', async () => { diff --git a/dist/index.js b/dist/index.js index b410e361d3..6f8335d598 100644 --- a/dist/index.js +++ b/dist/index.js @@ -44,6 +44,7 @@ const core = __importStar(__nccwpck_require__(2186)); const uuid_1 = __nccwpck_require__(5840); const CHERRYPICK_EMPTY = 'The previous cherry-pick is now empty, possibly due to conflict resolution.'; const NOTHING_TO_COMMIT = 'nothing to commit, working tree clean'; +const FETCH_DEPTH_MARGIN = 10; var WorkingBaseType; (function (WorkingBaseType) { WorkingBaseType["Branch"] = "branch"; @@ -64,11 +65,12 @@ function getWorkingBaseAndType(git) { }); } exports.getWorkingBaseAndType = getWorkingBaseAndType; -function tryFetch(git, remote, branch) { +function tryFetch(git, remote, branch, depth) { return __awaiter(this, void 0, void 0, function* () { try { yield git.fetch([`${branch}:refs/remotes/${remote}/${branch}`], remote, [ - '--force' + '--force', + `--depth=${depth}` ]); return true; } @@ -196,8 +198,13 @@ function createOrUpdateBranch(git, commitMessage, base, branch, branchRemoteName // Reset the base yield git.fetch([`${base}:${base}`], baseRemote, fetchArgs); } + // Determine the fetch depth for the pull request branch (best effort) + const tempBranchCommitsAhead = yield commitsAhead(git, base, tempBranch); + const fetchDepth = tempBranchCommitsAhead > 0 + ? tempBranchCommitsAhead + FETCH_DEPTH_MARGIN + : FETCH_DEPTH_MARGIN; // Try to fetch the pull request branch - if (!(yield tryFetch(git, branchRemoteName, branch))) { + if (!(yield tryFetch(git, branchRemoteName, branch, fetchDepth))) { // The pull request branch does not exist core.info(`Pull request branch '${branch}' does not exist yet.`); // Create the pull request branch @@ -228,7 +235,6 @@ function createOrUpdateBranch(git, commitMessage, base, branch, branchRemoteName // temp branch. This catches a case where the base branch has been force pushed to // a new commit. // For changes on base this reset is equivalent to a rebase of the pull request branch. - const tempBranchCommitsAhead = yield commitsAhead(git, base, tempBranch); const branchCommitsAhead = yield commitsAhead(git, base, branch); if ((yield git.hasDiff([`${branch}..${tempBranch}`])) || branchCommitsAhead != tempBranchCommitsAhead || diff --git a/src/create-or-update-branch.ts b/src/create-or-update-branch.ts index 4d489dd35d..7b584e8db8 100644 --- a/src/create-or-update-branch.ts +++ b/src/create-or-update-branch.ts @@ -6,6 +6,8 @@ const CHERRYPICK_EMPTY = 'The previous cherry-pick is now empty, possibly due to conflict resolution.' const NOTHING_TO_COMMIT = 'nothing to commit, working tree clean' +const FETCH_DEPTH_MARGIN = 10 + export enum WorkingBaseType { Branch = 'branch', Commit = 'commit' @@ -31,11 +33,13 @@ export async function getWorkingBaseAndType( export async function tryFetch( git: GitCommandManager, remote: string, - branch: string + branch: string, + depth: number ): Promise { try { await git.fetch([`${branch}:refs/remotes/${remote}/${branch}`], remote, [ - '--force' + '--force', + `--depth=${depth}` ]) return true } catch { @@ -215,8 +219,15 @@ export async function createOrUpdateBranch( await git.fetch([`${base}:${base}`], baseRemote, fetchArgs) } + // Determine the fetch depth for the pull request branch (best effort) + const tempBranchCommitsAhead = await commitsAhead(git, base, tempBranch) + const fetchDepth = + tempBranchCommitsAhead > 0 + ? tempBranchCommitsAhead + FETCH_DEPTH_MARGIN + : FETCH_DEPTH_MARGIN + // Try to fetch the pull request branch - if (!(await tryFetch(git, branchRemoteName, branch))) { + if (!(await tryFetch(git, branchRemoteName, branch, fetchDepth))) { // The pull request branch does not exist core.info(`Pull request branch '${branch}' does not exist yet.`) // Create the pull request branch @@ -250,7 +261,6 @@ export async function createOrUpdateBranch( // temp branch. This catches a case where the base branch has been force pushed to // a new commit. // For changes on base this reset is equivalent to a rebase of the pull request branch. - const tempBranchCommitsAhead = await commitsAhead(git, base, tempBranch) const branchCommitsAhead = await commitsAhead(git, base, branch) if ( (await git.hasDiff([`${branch}..${tempBranch}`])) ||