@dapplion/benchmark
Version:
Ensures that new code does not introduce performance regressions with CI. Tracks:
50 lines (49 loc) • 1.84 kB
JavaScript
import { shell } from "./shell.js";
export async function getCurrentCommitInfo() {
const commitSha = await shell("git show -s --format=%H");
const timestampStr = await shell("git show -s --format=%ct");
const timestamp = parseInt(timestampStr, 10);
if (!timestamp || isNaN(timestamp)) {
throw Error(`Invalid timestampStr ${timestampStr}`);
}
return {
commitSha,
timestamp,
};
}
/**
* Returns a chornological list of commits from `$branch`.
*
* - `--format=format:%H`: Print the full commit hash only
* - `-n`: Display up to n commits
* - `--no-pager` suppress interactive mode
*
* (from git-log docs):
* List commits that are reachable by following the parent links from the given commit(s),
* but exclude commits that are reachable from the one(s) given with a ^ in front of them.
* The output is given in reverse chronological order by default.
*/
export async function getBranchCommitList(branch, n = 50) {
await ensureBranchExists(branch);
const commitsStr = await shell(`git --no-pager log --format=format:%H -n ${n} ${branch}`);
return commitsStr.trim().split("\n");
}
/**
* Resolve a heads ref
*/
export async function getBranchLatestCommit(branch) {
await ensureBranchExists(branch);
const res = await shell(`git rev-parse refs/heads/${branch}`);
return res.trim();
}
/**
* Ensure branch exists locally or try to fetch it from origin.
* When using actions/checkout users normally only clone a single commit.
* Getting the entire git history for all branches is a bit more tricky than this.
*/
async function ensureBranchExists(branch) {
const refExists = await shell(`git show-ref --verify --quiet refs/heads/${branch}`).then(() => true, () => false);
if (!refExists) {
await shell(`git fetch origin ${branch}`);
}
}