use compare API to deepen by exact divergence instead of fixed 1000
This commit is contained in:
committed by
pullfrog[bot]
parent
8bac460177
commit
ed91fbb18d
@@ -146647,15 +146647,33 @@ async function checkoutPrBranch(pullNumber, params) {
|
|||||||
const baseBranch = pr.data.base.ref;
|
const baseBranch = pr.data.base.ref;
|
||||||
const headBranch = pr.data.head.ref;
|
const headBranch = pr.data.head.ref;
|
||||||
const localBranch = `pr-${pullNumber}`;
|
const localBranch = `pr-${pullNumber}`;
|
||||||
|
const isShallow = $("git", ["rev-parse", "--is-shallow-repository"], { log: false }).trim() === "true";
|
||||||
|
let deepenArgs = [];
|
||||||
|
if (isShallow) {
|
||||||
|
let depth = 1e3;
|
||||||
|
try {
|
||||||
|
const comparison = await octokit.rest.repos.compareCommits({
|
||||||
|
owner,
|
||||||
|
repo: name,
|
||||||
|
base: baseBranch,
|
||||||
|
head: `pull/${pullNumber}/head`
|
||||||
|
});
|
||||||
|
depth = comparison.data.behind_by + 10;
|
||||||
|
log.debug(
|
||||||
|
`\xBB PR is ${comparison.data.behind_by} commits behind ${baseBranch}, deepening by ${depth}`
|
||||||
|
);
|
||||||
|
} catch {
|
||||||
|
log.debug(`\xBB compare API failed, falling back to --deepen=${depth}`);
|
||||||
|
}
|
||||||
|
deepenArgs = [`--deepen=${depth}`];
|
||||||
|
}
|
||||||
const currentSha = $("git", ["rev-parse", "HEAD"], { log: false }).trim();
|
const currentSha = $("git", ["rev-parse", "HEAD"], { log: false }).trim();
|
||||||
const alreadyOnBranch = currentSha === pr.data.head.sha;
|
const alreadyOnBranch = currentSha === pr.data.head.sha;
|
||||||
if (alreadyOnBranch) {
|
if (alreadyOnBranch) {
|
||||||
log.debug(`already on PR branch ${localBranch}, skipping checkout`);
|
log.debug(`already on PR branch ${localBranch}, skipping checkout`);
|
||||||
} else {
|
} else {
|
||||||
log.debug(`\xBB fetching base branch (${baseBranch})...`);
|
log.debug(`\xBB fetching base branch (${baseBranch})...`);
|
||||||
const isShallow = $("git", ["rev-parse", "--is-shallow-repository"], { log: false }).trim();
|
$git("fetch", [...deepenArgs, "--no-tags", "origin", baseBranch], {
|
||||||
const baseFetchArgs = isShallow === "true" ? ["--deepen=1000", "--no-tags", "origin", baseBranch] : ["--no-tags", "origin", baseBranch];
|
|
||||||
$git("fetch", baseFetchArgs, {
|
|
||||||
token: gitToken,
|
token: gitToken,
|
||||||
restricted: shell !== "enabled"
|
restricted: shell !== "enabled"
|
||||||
});
|
});
|
||||||
@@ -146670,9 +146688,7 @@ async function checkoutPrBranch(pullNumber, params) {
|
|||||||
}
|
}
|
||||||
if (alreadyOnBranch) {
|
if (alreadyOnBranch) {
|
||||||
log.debug(`\xBB fetching base branch (${baseBranch})...`);
|
log.debug(`\xBB fetching base branch (${baseBranch})...`);
|
||||||
const isShallow = $("git", ["rev-parse", "--is-shallow-repository"], { log: false }).trim();
|
$git("fetch", [...deepenArgs, "--no-tags", "origin", baseBranch], {
|
||||||
const baseFetchArgs = isShallow === "true" ? ["--deepen=1000", "--no-tags", "origin", baseBranch] : ["--no-tags", "origin", baseBranch];
|
|
||||||
$git("fetch", baseFetchArgs, {
|
|
||||||
token: gitToken,
|
token: gitToken,
|
||||||
restricted: shell !== "enabled"
|
restricted: shell !== "enabled"
|
||||||
});
|
});
|
||||||
|
|||||||
+28
-16
@@ -206,6 +206,31 @@ export async function checkoutPrBranch(
|
|||||||
// this avoids naming conflicts and makes push config simpler
|
// this avoids naming conflicts and makes push config simpler
|
||||||
const localBranch = `pr-${pullNumber}`;
|
const localBranch = `pr-${pullNumber}`;
|
||||||
|
|
||||||
|
// compute deepen depth for shallow clones. actions/checkout uses depth=1
|
||||||
|
// by default, which breaks rebase/log because git can't find the merge base.
|
||||||
|
// use the GitHub compare API to fetch exactly enough history.
|
||||||
|
const isShallow =
|
||||||
|
$("git", ["rev-parse", "--is-shallow-repository"], { log: false }).trim() === "true";
|
||||||
|
let deepenArgs: string[] = [];
|
||||||
|
if (isShallow) {
|
||||||
|
let depth = 1000; // fallback
|
||||||
|
try {
|
||||||
|
const comparison = await octokit.rest.repos.compareCommits({
|
||||||
|
owner,
|
||||||
|
repo: name,
|
||||||
|
base: baseBranch,
|
||||||
|
head: `pull/${pullNumber}/head`,
|
||||||
|
});
|
||||||
|
depth = comparison.data.behind_by + 10;
|
||||||
|
log.debug(
|
||||||
|
`» PR is ${comparison.data.behind_by} commits behind ${baseBranch}, deepening by ${depth}`
|
||||||
|
);
|
||||||
|
} catch {
|
||||||
|
log.debug(`» compare API failed, falling back to --deepen=${depth}`);
|
||||||
|
}
|
||||||
|
deepenArgs = [`--deepen=${depth}`];
|
||||||
|
}
|
||||||
|
|
||||||
// check if we're already on the correct commit (not just branch name)
|
// check if we're already on the correct commit (not just branch name)
|
||||||
// this handles fork PRs where head branch name might match base branch name
|
// this handles fork PRs where head branch name might match base branch name
|
||||||
const currentSha = $("git", ["rev-parse", "HEAD"], { log: false }).trim();
|
const currentSha = $("git", ["rev-parse", "HEAD"], { log: false }).trim();
|
||||||
@@ -214,17 +239,9 @@ export async function checkoutPrBranch(
|
|||||||
if (alreadyOnBranch) {
|
if (alreadyOnBranch) {
|
||||||
log.debug(`already on PR branch ${localBranch}, skipping checkout`);
|
log.debug(`already on PR branch ${localBranch}, skipping checkout`);
|
||||||
} else {
|
} else {
|
||||||
// fetch base branch so origin/<base> exists for diff operations.
|
// fetch base branch so origin/<base> exists for diff operations
|
||||||
// deepen if shallow — actions/checkout uses depth=1 by default, which
|
|
||||||
// breaks rebase/log operations because git can't find the merge base
|
|
||||||
// between the shallow base and the fully-fetched PR branch.
|
|
||||||
log.debug(`» fetching base branch (${baseBranch})...`);
|
log.debug(`» fetching base branch (${baseBranch})...`);
|
||||||
const isShallow = $("git", ["rev-parse", "--is-shallow-repository"], { log: false }).trim();
|
$git("fetch", [...deepenArgs, "--no-tags", "origin", baseBranch], {
|
||||||
const baseFetchArgs =
|
|
||||||
isShallow === "true"
|
|
||||||
? ["--deepen=1000", "--no-tags", "origin", baseBranch]
|
|
||||||
: ["--no-tags", "origin", baseBranch];
|
|
||||||
$git("fetch", baseFetchArgs, {
|
|
||||||
token: gitToken,
|
token: gitToken,
|
||||||
restricted: shell !== "enabled",
|
restricted: shell !== "enabled",
|
||||||
});
|
});
|
||||||
@@ -249,12 +266,7 @@ export async function checkoutPrBranch(
|
|||||||
// fetch if we skipped checkout (already on branch) - otherwise already fetched above
|
// fetch if we skipped checkout (already on branch) - otherwise already fetched above
|
||||||
if (alreadyOnBranch) {
|
if (alreadyOnBranch) {
|
||||||
log.debug(`» fetching base branch (${baseBranch})...`);
|
log.debug(`» fetching base branch (${baseBranch})...`);
|
||||||
const isShallow = $("git", ["rev-parse", "--is-shallow-repository"], { log: false }).trim();
|
$git("fetch", [...deepenArgs, "--no-tags", "origin", baseBranch], {
|
||||||
const baseFetchArgs =
|
|
||||||
isShallow === "true"
|
|
||||||
? ["--deepen=1000", "--no-tags", "origin", baseBranch]
|
|
||||||
: ["--no-tags", "origin", baseBranch];
|
|
||||||
$git("fetch", baseFetchArgs, {
|
|
||||||
token: gitToken,
|
token: gitToken,
|
||||||
restricted: shell !== "enabled",
|
restricted: shell !== "enabled",
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user