Skip to content
This repository was archived by the owner on Aug 27, 2025. It is now read-only.

Commit f09925e

Browse files
committed
feat: last commit api
1 parent bdc7a74 commit f09925e

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

src/api/last-commit.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { readFile } from "node:fs/promises";
2+
import { join } from "node:path";
3+
4+
export async function lastCommit(repo: string, head: string): Promise<string> {
5+
const headRefPath = join(repo, ".git", "refs", "heads", head);
6+
7+
try {
8+
const hash = await readFile(headRefPath, "utf8");
9+
10+
return hash.trimEnd();
11+
} catch {
12+
/* empty */
13+
}
14+
15+
const packRefs = join(repo, ".git", "packed-refs");
16+
try {
17+
const refs = await readFile(packRefs, "utf8");
18+
19+
for (const line of refs.split("\n")) {
20+
const [hash, rf = undefined] = line.split(" ");
21+
22+
if (`refs/heads/${head}` === rf) {
23+
return hash;
24+
}
25+
}
26+
} catch {
27+
/* empty */
28+
}
29+
30+
throw new Error("cannot get branch info");
31+
}

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export { diffCommits } from "./api/diff-commits";
22
export { findParents } from "./api/find-parents";
33
export { getBranches } from "./api/get-branches";
44
export { headBranch } from "./api/head-branch";
5+
export { lastCommit } from "./api/last-commit";
56
export { mergeBases } from "./api/merge-bases";
67
export { readCommit } from "./api/read-object";
78
export { searchRepo } from "./api/search-repo";

0 commit comments

Comments
 (0)