Skip to content

Commit 0b06e46

Browse files
authored
feat(cli): show branch name if locally clean (#971)
1 parent 555abeb commit 0b06e46

4 files changed

Lines changed: 29 additions & 20 deletions

File tree

src/utils/get-git-branch.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { execSync } from "node:child_process";
2+
import type { ExecSyncOptions } from "node:child_process";
3+
4+
export function getGitBranch(options?: ExecSyncOptions): string | null {
5+
try {
6+
const result = execSync(
7+
"git symbolic-ref --quiet --short HEAD || git rev-parse --short HEAD",
8+
{
9+
encoding: "utf8",
10+
stdio: ["ignore", "pipe", "ignore"],
11+
...options,
12+
},
13+
);
14+
15+
const branch = result.toString().trim();
16+
return branch === "" ? null : branch;
17+
} catch {
18+
return null;
19+
}
20+
}

src/utils/is-git-clean.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import type { ExecSyncOptions } from "node:child_process";
33

44
export function isGitClean(options?: ExecSyncOptions): boolean {
55
try {
6-
execSync("git diff-index --quiet HEAD --", { stdio: "ignore", ...options });
7-
return true;
6+
const diff = execSync("git status --porcelain", options);
7+
return diff.toString().trim() === "";
88
} catch {
99
return false;
1010
}

src/utils/is-in-git-repo.ts

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/utils/print-intro.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,25 @@ import * as p from "@clack/prompts";
22
import { readFileSync } from "node:fs";
33
import c from "picocolors";
44

5+
import { getGitBranch } from "./get-git-branch";
56
import { isGitClean } from "./is-git-clean";
6-
import { isInGitRepo } from "./is-in-git-repo";
77

88
export const printIntro = () => {
99
const packageJsonUrl = new URL("../../package.json", import.meta.url);
1010
const packageJson = JSON.parse(readFileSync(packageJsonUrl, "utf8")) as {
1111
version?: string;
1212
};
13-
const packageRoot = new URL("../", packageJsonUrl);
13+
const packageRoot = new URL("./", packageJsonUrl);
1414
const execOptions = { cwd: packageRoot };
15-
const clean = !isInGitRepo(execOptions) || isGitClean(execOptions);
1615
const version =
1716
packageJson.version == null || packageJson.version.trim() === ""
1817
? c.red("(unknown version)")
1918
: c.green(c.bold(`v${packageJson.version}`));
20-
const dirtyStatus = clean ? "" : c.white(" (dirty)");
19+
const branchName = getGitBranch(execOptions);
20+
const dirtyStatus =
21+
branchName == null
22+
? ""
23+
: c.white(` (${isGitClean(execOptions) ? branchName : "dirty"})`);
2124
p.intro(
2225
` ${c.blueBright(c.bold("@solvro/config"))} ${version}${dirtyStatus} `,
2326
);

0 commit comments

Comments
 (0)