Skip to content

Commit 57a2859

Browse files
committed
getPullRequestEditedDiffRanges: work around fatal error
This commits adds a "git repack" step to getPullRequestEditedDiffRanges to work around a Git bug concerning tracking of grafted commits.
1 parent 3e10d34 commit 57a2859

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/actions-util.ts

+18
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,24 @@ export const gitFetch = async function (branch: string, extraFlags: string[]) {
205205
}
206206
};
207207

208+
/**
209+
* Repack the git repository, using with the given flags. Errors are logged.
210+
*
211+
* This function uses the `checkout_path` to determine the repository path and
212+
* works only when called from `analyze` or `upload-sarif`.
213+
*/
214+
export const gitRepack = async function (flags: string[]) {
215+
try {
216+
await runGitCommand(
217+
getOptionalInput("checkout_path"),
218+
["repack", ...flags],
219+
"Cannot repack the repository.",
220+
);
221+
} catch {
222+
// Errors are already logged by runGitCommand()
223+
}
224+
};
225+
208226
/**
209227
* Compute the all merge bases between the given refs. Returns an empty array
210228
* if no merge base is found, or if there is an error.

src/analyze.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ async function getPullRequestEditedDiffRanges(
298298

299299
// To compute the merge bases between the base branch and the PR topic branch,
300300
// we need to fetch the commit graph from the branch heads to those merge
301-
// babes. The following 4-step procedure does so while limiting the amount of
301+
// babes. The following 6-step procedure does so while limiting the amount of
302302
// history fetched.
303303

304304
// Step 1: Deepen from the PR merge commit to the base branch head and the PR
@@ -317,7 +317,12 @@ async function getPullRequestEditedDiffRanges(
317317
// Step 4: Fetch the base branch history, stopping when we reach commits that
318318
// are reachable from the PR topic branch head.
319319
await actionsUtil.gitFetch(baseRef, [`--shallow-exclude=${headRef}`]);
320-
// Step 5: Deepen the history so that we have the merge bases between the base
320+
// Step 5: Repack the history to remove the shallow grafts that were added by
321+
// the previous fetches. This step works around a bug that causes subsequent
322+
// deepening fetches to fail with "fatal: error in object: unshallow <SHA>".
323+
// See https://stackoverflow.com/q/63878612
324+
await actionsUtil.gitRepack(["-d"]);
325+
// Step 6: Deepen the history so that we have the merge bases between the base
321326
// branch and the PR topic branch.
322327
await actionsUtil.deepenGitHistory();
323328

0 commit comments

Comments
 (0)