Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: limit the fetch depth of pr branch #2857

Merged
merged 1 commit into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions __test__/create-or-update-branch.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
14 changes: 10 additions & 4 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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;
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 ||
Expand Down
18 changes: 14 additions & 4 deletions src/create-or-update-branch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -31,11 +33,13 @@ export async function getWorkingBaseAndType(
export async function tryFetch(
git: GitCommandManager,
remote: string,
branch: string
branch: string,
depth: number
): Promise<boolean> {
try {
await git.fetch([`${branch}:refs/remotes/${remote}/${branch}`], remote, [
'--force'
'--force',
`--depth=${depth}`
])
return true
} catch {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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}`])) ||
Expand Down