This repository has been archived by the owner on Sep 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FLAKE-174] Resolve detached git HEADs to abbreviated ref names (#1)
This should support GitHub Actions `pull_request` events.
- Loading branch information
Showing
6 changed files
with
143 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import type { SimpleGit } from "simple-git"; | ||
import _debug = require("debug"); | ||
|
||
const debug = _debug("unflakable:git"); | ||
|
||
export const getCurrentGitBranch = async ( | ||
git: SimpleGit, | ||
commitSha: string | ||
): Promise<string | undefined> => { | ||
// In the common case (an attached HEAD), we can just use `git rev-parse`. | ||
const headRef = await git.revparse(["--abbrev-ref", "HEAD"]); | ||
|
||
// If `git rev-parse` returns `HEAD`, then we have a detached head, and we need to see if the | ||
// current commit SHA matches any known refs (i.e., local/remote branches or tags). This happens | ||
// when running GitHub Actions in response to a `pull_request` event. In that case, the commit | ||
// is a detached HEAD, but there's a `refs/remotes/pull/PR_NUMBER/merge` ref we can use as the | ||
// "branch" (abbreviated to pull/PR_NUMBER/merge). | ||
if (headRef !== "HEAD") { | ||
return headRef; | ||
} | ||
|
||
// The code below runs the equivalent of `git show-ref | grep $(git rev-parse HEAD)`. | ||
const gitOutput = await git.raw(["show-ref"]); | ||
const matchingRefs = gitOutput | ||
.split("\n") | ||
.map((line) => { | ||
const [sha, refName] = line.split(" ", 2); | ||
return { | ||
sha, | ||
refName, | ||
}; | ||
}) | ||
.filter(({ sha }) => sha === commitSha); | ||
debug( | ||
`git show-ref returned ${ | ||
matchingRefs.length | ||
} ref(s) SHA ${commitSha}: ${matchingRefs | ||
.map((ref) => ref.refName) | ||
.join(", ")}` | ||
); | ||
|
||
if (matchingRefs.length === 0) { | ||
return undefined; | ||
} | ||
|
||
// `git show-ref` returns refs sorted lexicographically: | ||
// refs/heads/* | ||
// refs/remotes/* | ||
// refs/stash | ||
// refs/tags/* | ||
// We just take the first matching ref and use its abbreviation (i.e., removing the refs/remotes | ||
// prefix) as the branch name. Users can override this behavior by setting the UNFLAKABLE_BRANCH | ||
// environment variable. | ||
return git.revparse(["--abbrev-ref", matchingRefs[0].refName]); | ||
}; | ||
|
||
export const getCurrentGitCommit = (git: SimpleGit): Promise<string> => | ||
git.revparse("HEAD"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters