Skip to content

Commit

Permalink
getPullRequestEditedDiffRanges: work around fatal error
Browse files Browse the repository at this point in the history
This commits adds a "git repack" step to getPullRequestEditedDiffRanges
to work around a Git bug concerning tracking of grafted commits.
  • Loading branch information
cklin committed Dec 9, 2024
1 parent 3e10d34 commit 57a2859
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
18 changes: 18 additions & 0 deletions src/actions-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 7 additions & 2 deletions src/analyze.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 <SHA>".
// 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();

Expand Down

0 comments on commit 57a2859

Please sign in to comment.