diff --git a/src/actions-util.ts b/src/actions-util.ts index 4e4e772c3f..941e0ba01b 100644 --- a/src/actions-util.ts +++ b/src/actions-util.ts @@ -205,6 +205,24 @@ export const gitFetch = async function (branch: string, extraFlags: string[]) { } }; +/** + * Repack the git repository, using with the given flags. Errors are logged. + * + * This function uses the `checkout_path` to determine the repository path and + * works only when called from `analyze` or `upload-sarif`. + */ +export const gitRepack = async function (flags: string[]) { + try { + await runGitCommand( + getOptionalInput("checkout_path"), + ["repack", ...flags], + "Cannot repack the repository.", + ); + } catch { + // Errors are already logged by runGitCommand() + } +}; + /** * Compute the all merge bases between the given refs. Returns an empty array * if no merge base is found, or if there is an error. diff --git a/src/analyze.ts b/src/analyze.ts index fed0ae6c8e..a540527255 100644 --- a/src/analyze.ts +++ b/src/analyze.ts @@ -298,7 +298,7 @@ async function getPullRequestEditedDiffRanges( // To compute the merge bases between the base branch and the PR topic branch, // we need to fetch the commit graph from the branch heads to those merge - // babes. The following 4-step procedure does so while limiting the amount of + // babes. The following 6-step procedure does so while limiting the amount of // history fetched. // Step 1: Deepen from the PR merge commit to the base branch head and the PR @@ -317,7 +317,12 @@ async function getPullRequestEditedDiffRanges( // Step 4: Fetch the base branch history, stopping when we reach commits that // are reachable from the PR topic branch head. await actionsUtil.gitFetch(baseRef, [`--shallow-exclude=${headRef}`]); - // Step 5: Deepen the history so that we have the merge bases between the base + // Step 5: Repack the history to remove the shallow grafts that were added by + // the previous fetches. This step works around a bug that causes subsequent + // deepening fetches to fail with "fatal: error in object: unshallow ". + // See https://stackoverflow.com/q/63878612 + await actionsUtil.gitRepack(["-d"]); + // Step 6: Deepen the history so that we have the merge bases between the base // branch and the PR topic branch. await actionsUtil.deepenGitHistory();