Skip to content

Commit

Permalink
perf: limit the fetch depth of pr branch (#2857)
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-evans authored Apr 17, 2024
1 parent c55203c commit 9153d83
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
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;

This comment has been minimized.

Copy link
@mohamedmligy

mohamedmligy May 7, 2024

[submodule ".config/ranger/plugins/zoxide"]
path = .config/ranger/plugins/zoxide
url = git@github.com:jchook/ranger-zoxide.git
[submodule ".hammerspoon/stackline"]
path = .hammerspoon/stackline
url = git@github.com:AdamWagner/stackline.git

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

0 comments on commit 9153d83

Please sign in to comment.