@dapplion/benchmark
Version:
Ensures that new code does not introduce performance regressions with CI. Tracks:
55 lines (54 loc) • 2.15 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getCurrentCommitInfo = getCurrentCommitInfo;
exports.getBranchCommitList = getBranchCommitList;
exports.getBranchLatestCommit = getBranchLatestCommit;
const shell_js_1 = require("./shell.js");
async function getCurrentCommitInfo() {
const commitSha = await (0, shell_js_1.shell)("git show -s --format=%H");
const timestampStr = await (0, shell_js_1.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.
*/
async function getBranchCommitList(branch, n = 50) {
await ensureBranchExists(branch);
const commitsStr = await (0, shell_js_1.shell)(`git --no-pager log --format=format:%H -n ${n} ${branch}`);
return commitsStr.trim().split("\n");
}
/**
* Resolve a heads ref
*/
async function getBranchLatestCommit(branch) {
await ensureBranchExists(branch);
const res = await (0, shell_js_1.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 (0, shell_js_1.shell)(`git show-ref --verify --quiet refs/heads/${branch}`).then(() => true, () => false);
if (!refExists) {
await (0, shell_js_1.shell)(`git fetch origin ${branch}`);
}
}