Skip to content

Commit 9153d83

Browse files
authored
perf: limit the fetch depth of pr branch (#2857)
1 parent c55203c commit 9153d83

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed

__test__/create-or-update-branch.int.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,8 @@ describe('create-or-update-branch tests', () => {
210210
}
211211

212212
it('tests if a branch exists and can be fetched', async () => {
213-
expect(await tryFetch(git, REMOTE_NAME, NOT_BASE_BRANCH)).toBeTruthy()
214-
expect(await tryFetch(git, REMOTE_NAME, NOT_EXIST_BRANCH)).toBeFalsy()
213+
expect(await tryFetch(git, REMOTE_NAME, NOT_BASE_BRANCH, 1)).toBeTruthy()
214+
expect(await tryFetch(git, REMOTE_NAME, NOT_EXIST_BRANCH, 1)).toBeFalsy()
215215
})
216216

217217
it('tests getWorkingBaseAndType on a checked out ref', async () => {

dist/index.js

+10-4
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ const core = __importStar(__nccwpck_require__(2186));
4444
const uuid_1 = __nccwpck_require__(5840);
4545
const CHERRYPICK_EMPTY = 'The previous cherry-pick is now empty, possibly due to conflict resolution.';
4646
const NOTHING_TO_COMMIT = 'nothing to commit, working tree clean';
47+
const FETCH_DEPTH_MARGIN = 10;
4748
var WorkingBaseType;
4849
(function (WorkingBaseType) {
4950
WorkingBaseType["Branch"] = "branch";
@@ -64,11 +65,12 @@ function getWorkingBaseAndType(git) {
6465
});
6566
}
6667
exports.getWorkingBaseAndType = getWorkingBaseAndType;
67-
function tryFetch(git, remote, branch) {
68+
function tryFetch(git, remote, branch, depth) {
6869
return __awaiter(this, void 0, void 0, function* () {
6970
try {
7071
yield git.fetch([`${branch}:refs/remotes/${remote}/${branch}`], remote, [
71-
'--force'
72+
'--force',
73+
`--depth=${depth}`
7274
]);
7375
return true;
7476
}
@@ -196,8 +198,13 @@ function createOrUpdateBranch(git, commitMessage, base, branch, branchRemoteName
196198
// Reset the base
197199
yield git.fetch([`${base}:${base}`], baseRemote, fetchArgs);
198200
}
201+
// Determine the fetch depth for the pull request branch (best effort)
202+
const tempBranchCommitsAhead = yield commitsAhead(git, base, tempBranch);
203+
const fetchDepth = tempBranchCommitsAhead > 0
204+
? tempBranchCommitsAhead + FETCH_DEPTH_MARGIN
205+
: FETCH_DEPTH_MARGIN;
199206
// Try to fetch the pull request branch
200-
if (!(yield tryFetch(git, branchRemoteName, branch))) {
207+
if (!(yield tryFetch(git, branchRemoteName, branch, fetchDepth))) {
201208
// The pull request branch does not exist
202209
core.info(`Pull request branch '${branch}' does not exist yet.`);
203210
// Create the pull request branch
@@ -228,7 +235,6 @@ function createOrUpdateBranch(git, commitMessage, base, branch, branchRemoteName
228235
// temp branch. This catches a case where the base branch has been force pushed to
229236
// a new commit.
230237
// For changes on base this reset is equivalent to a rebase of the pull request branch.
231-
const tempBranchCommitsAhead = yield commitsAhead(git, base, tempBranch);
232238
const branchCommitsAhead = yield commitsAhead(git, base, branch);
233239
if ((yield git.hasDiff([`${branch}..${tempBranch}`])) ||
234240
branchCommitsAhead != tempBranchCommitsAhead ||

src/create-or-update-branch.ts

+14-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ const CHERRYPICK_EMPTY =
66
'The previous cherry-pick is now empty, possibly due to conflict resolution.'
77
const NOTHING_TO_COMMIT = 'nothing to commit, working tree clean'
88

9+
const FETCH_DEPTH_MARGIN = 10
10+
911
export enum WorkingBaseType {
1012
Branch = 'branch',
1113
Commit = 'commit'
@@ -31,11 +33,13 @@ export async function getWorkingBaseAndType(
3133
export async function tryFetch(
3234
git: GitCommandManager,
3335
remote: string,
34-
branch: string
36+
branch: string,
37+
depth: number
3538
): Promise<boolean> {
3639
try {
3740
await git.fetch([`${branch}:refs/remotes/${remote}/${branch}`], remote, [
38-
'--force'
41+
'--force',
42+
`--depth=${depth}`
3943
])
4044
return true
4145
} catch {
@@ -215,8 +219,15 @@ export async function createOrUpdateBranch(
215219
await git.fetch([`${base}:${base}`], baseRemote, fetchArgs)
216220
}
217221

222+
// Determine the fetch depth for the pull request branch (best effort)
223+
const tempBranchCommitsAhead = await commitsAhead(git, base, tempBranch)
224+
const fetchDepth =
225+
tempBranchCommitsAhead > 0
226+
? tempBranchCommitsAhead + FETCH_DEPTH_MARGIN
227+
: FETCH_DEPTH_MARGIN
228+
218229
// Try to fetch the pull request branch
219-
if (!(await tryFetch(git, branchRemoteName, branch))) {
230+
if (!(await tryFetch(git, branchRemoteName, branch, fetchDepth))) {
220231
// The pull request branch does not exist
221232
core.info(`Pull request branch '${branch}' does not exist yet.`)
222233
// Create the pull request branch
@@ -250,7 +261,6 @@ export async function createOrUpdateBranch(
250261
// temp branch. This catches a case where the base branch has been force pushed to
251262
// a new commit.
252263
// For changes on base this reset is equivalent to a rebase of the pull request branch.
253-
const tempBranchCommitsAhead = await commitsAhead(git, base, tempBranch)
254264
const branchCommitsAhead = await commitsAhead(git, base, branch)
255265
if (
256266
(await git.hasDiff([`${branch}..${tempBranch}`])) ||

0 commit comments

Comments
 (0)