@@ -0,0 +1,149 @@
1+import { afterEach, beforeEach, describe, expect, test } from "bun:test";
2+import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
3+import { tmpdir } from "node:os";
4+import { join, resolve } from "node:path";
5+import { getGitState } from "./git.ts";
6+
7+// ---------------------------------------------------------------------------
8+// Helpers
9+// ---------------------------------------------------------------------------
10+
11+let tmp: string;
12+
13+beforeEach(() => {
14+ tmp = mkdtempSync(join(tmpdir(), "mc-git-"));
15+});
16+
17+afterEach(() => {
18+ rmSync(tmp, { recursive: true, force: true });
19+});
20+
21+/** Run a git command in the temp directory. */
22+async function git(args: string, cwd = tmp): Promise<string> {
23+ const proc = Bun.spawn(["git", ...args.split(" ")], {
24+ cwd,
25+ stdout: "pipe",
26+ stderr: "pipe",
27+ env: {
28+ ...process.env,
29+ GIT_AUTHOR_NAME: "Test",
30+ GIT_AUTHOR_EMAIL: "test@test.com",
31+ GIT_COMMITTER_NAME: "Test",
32+ GIT_COMMITTER_EMAIL: "test@test.com",
33+ },
34+ });
35+ const out = await new Response(proc.stdout as ReadableStream).text();
36+ await proc.exited;
37+ return out.trim();
38+}
39+
40+/** Initialize a git repo with one commit so HEAD exists. */
41+async function initRepo(cwd = tmp): Promise<void> {
42+ await git("init", cwd);
43+ writeFileSync(join(cwd, "README.md"), "# init\n");
44+ await git("add .", cwd);
45+ await git("commit -m init", cwd);
46+}
47+
48+// ---------------------------------------------------------------------------
49+// Tests
50+// ---------------------------------------------------------------------------
51+
52+describe("getGitState", () => {
53+ test("returns null outside a git repo", async () => {
54+ const state = await getGitState(tmp);
55+ expect(state).toBeNull();
56+ });
57+
58+ test("detects repo root", async () => {
59+ await initRepo();
60+ const state = await getGitState(tmp);
61+ expect(state).not.toBeNull();
62+ expect(state!.root).toBe(resolve(tmp));
63+ });
64+
65+ test("gets current branch name", async () => {
66+ await initRepo();
67+ const state = await getGitState(tmp);
68+ expect(state!.branch).toBeOneOf(["main", "master"]);
69+ });
70+
71+ test("counts untracked files", async () => {
72+ await initRepo();
73+ writeFileSync(join(tmp, "new.txt"), "untracked");
74+ const state = await getGitState(tmp);
75+ expect(state!.untracked).toBe(1);
76+ });
77+
78+ test("counts modified files", async () => {
79+ await initRepo();
80+ writeFileSync(join(tmp, "README.md"), "modified\n");
81+ const state = await getGitState(tmp);
82+ expect(state!.modified).toBe(1);
83+ });
84+
85+ test("counts staged files", async () => {
86+ await initRepo();
87+ writeFileSync(join(tmp, "README.md"), "staged\n");
88+ await git("add README.md");
89+ const state = await getGitState(tmp);
90+ expect(state!.staged).toBe(1);
91+ });
92+
93+ test("counts mixed states correctly", async () => {
94+ await initRepo();
95+ // Stage a change
96+ writeFileSync(join(tmp, "README.md"), "staged\n");
97+ await git("add README.md");
98+ // Add an untracked file
99+ writeFileSync(join(tmp, "new.txt"), "untracked");
100+ // Add a modified (but not staged) file — modify README again after staging
101+ writeFileSync(join(tmp, "README.md"), "staged then modified\n");
102+
103+ const state = await getGitState(tmp);
104+ expect(state!.staged).toBe(1);
105+ expect(state!.modified).toBe(1);
106+ expect(state!.untracked).toBe(1);
107+ });
108+
109+ test("returns zero ahead/behind when no upstream", async () => {
110+ await initRepo();
111+ const state = await getGitState(tmp);
112+ expect(state!.ahead).toBe(0);
113+ expect(state!.behind).toBe(0);
114+ });
115+
116+ test("detects ahead count relative to upstream", async () => {
117+ // Create a bare "remote" repo
118+ const bare = join(tmp, "remote.git");
119+ await git(`init --bare ${bare}`);
120+
121+ // Init a working repo and push
122+ const work = join(tmp, "work");
123+ await git(`clone ${bare} ${work}`);
124+ writeFileSync(join(work, "file.txt"), "initial\n");
125+ await git("add .", work);
126+ await git("commit -m first", work);
127+ await git("push origin HEAD", work);
128+
129+ // Make a local commit (ahead by 1)
130+ writeFileSync(join(work, "file.txt"), "updated\n");
131+ await git("add .", work);
132+ await git("commit -m second", work);
133+
134+ const state = await getGitState(work);
135+ expect(state!.ahead).toBe(1);
136+ expect(state!.behind).toBe(0);
137+ });
138+
139+ test("works from a subdirectory", async () => {
140+ await initRepo();
141+ const sub = join(tmp, "src", "deep");
142+ const { mkdirSync } = await import("node:fs");
143+ mkdirSync(sub, { recursive: true });
144+
145+ const state = await getGitState(sub);
146+ expect(state).not.toBeNull();
147+ expect(state!.root).toBe(resolve(tmp));
148+ });
149+});