|
3 | 3 | * AutoCommitMsg CLI script - to check Git changes and generate a commit message. |
4 | 4 | */ |
5 | 5 | import { execFileSync } from "child_process"; |
| 6 | +import { Repository } from "../api/git"; |
| 7 | +import { getChanges } from "../git/cli"; |
| 8 | +import { NO_LINES_MSG } from "../lib/constants"; |
6 | 9 | import { generateMsg } from "../prepareCommitMsg"; |
7 | 10 | import { shouldShowHelp } from "./utils"; |
8 | 11 |
|
9 | | -const HELP_TEXT: string = `Usage: acm [--cached] [--help|-h] |
| 12 | +const HELP_TEXT: string = `Usage: acm [--help|-h] |
10 | 13 |
|
11 | | -Check Git changes and generate a commit message. |
| 14 | +Check Git changes and output a generated commit message. |
12 | 15 |
|
13 | 16 | Options: |
14 | | - --cached Use only staged changes (equivalent to \`git --cached\`). |
15 | | - If the flag is omitted, then the standard \`git commit\` logic is followed: |
16 | | - look for staged changes and use them, otherwise use unstaged changes. |
17 | | - --help, -h Show this help and exit.`; |
| 17 | + --help, -h Show this help and exit. |
| 18 | + --verbose Show debug output for this run. |
| 19 | +`; |
18 | 20 |
|
19 | | -const DIFF_FLAGS = [ |
20 | | - "diff-index", |
21 | | - "--name-status", |
22 | | - "--find-renames", |
23 | | - "--find-copies", |
24 | | - "--no-color", |
25 | | -]; |
26 | | - |
27 | | -/** |
28 | | - * Run `git diff-index` and return its stdout as a string. |
29 | | - * |
30 | | - * TODO: Use _diffIndex instead after refactoring for flags. |
31 | | - * @param useCached When true, include only staged changes using `--cached`. |
32 | | - * |
33 | | - * @returns output Diff output from git. |
34 | | - */ |
35 | | -function runGitDiff(useCached: boolean): string { |
36 | | - const flags: string[] = [...DIFF_FLAGS]; |
37 | | - |
38 | | - if (useCached) { |
39 | | - flags.push("--cached"); |
40 | | - } |
41 | | - flags.push("HEAD"); |
42 | | - |
43 | | - const output: string = execFileSync("git", flags, { |
44 | | - encoding: "utf8", |
45 | | - stdio: ["ignore", "pipe", "pipe"], |
46 | | - }); |
47 | | - |
48 | | - return output.trim(); |
| 21 | +function _getCurrentRepository(): Repository { |
| 22 | + return { |
| 23 | + rootUri: { |
| 24 | + fsPath: execFileSync("git", ["rev-parse", "--show-toplevel"], { |
| 25 | + encoding: "utf8", |
| 26 | + }).trim(), |
| 27 | + }, |
| 28 | + } as Repository; |
49 | 29 | } |
50 | 30 |
|
51 | 31 | /** |
52 | 32 | * Generate a commit message from the current repository diff. |
53 | 33 | * |
54 | | - * @param useCached When true, include only staged changes using `--cached`. |
55 | 34 | * @returns Generated commit message text. |
56 | 35 | */ |
57 | | -export function generateCommitMessage(useCached: boolean): string { |
58 | | - const diffOutput: string = runGitDiff(useCached); |
59 | | - if (!diffOutput) { |
60 | | - throw new Error("No file changes found"); |
| 36 | +export async function generateCommitMessage(): Promise<string> { |
| 37 | + const repo = _getCurrentRepository(); |
| 38 | + const fileChanges: string[] = await getChanges(repo); |
| 39 | + if (!fileChanges.length) { |
| 40 | + // todo how to handle elegantly at next level here and other script |
| 41 | + throw new Error(NO_LINES_MSG); |
61 | 42 | } |
62 | 43 |
|
63 | | - const lines: string[] = diffOutput.split("\n"); |
64 | | - return generateMsg(lines); |
| 44 | + return generateMsg(fileChanges); |
65 | 45 | } |
66 | 46 |
|
67 | 47 | /** |
68 | 48 | * Command-line entry-point. |
69 | 49 | * |
70 | | - * Accepts an optional `--cached` flag to use staged changes only. |
71 | 50 | * Prints the generated commit message to stdout. |
72 | 51 | */ |
73 | | -function main(argv: string[]): void { |
| 52 | +async function main(argv: string[]): Promise<void> { |
74 | 53 | if (shouldShowHelp(argv)) { |
75 | 54 | console.log(HELP_TEXT); |
76 | 55 | return; |
77 | 56 | } |
78 | | - const useCached: boolean = argv.includes("--cached"); |
79 | | - const msg: string = generateCommitMessage(useCached); |
| 57 | + |
| 58 | + const verbose: boolean = argv.includes("--verbose"); |
| 59 | + if (verbose) { |
| 60 | + process.env.ACM_DEBUG = "1"; |
| 61 | + } |
| 62 | + |
| 63 | + const msg: string = await generateCommitMessage(); |
80 | 64 | console.log(msg); |
81 | 65 | } |
82 | 66 |
|
83 | 67 | if (require.main === module) { |
84 | | - try { |
85 | | - main(process.argv.slice(2)); |
86 | | - } catch (err) { |
| 68 | + main(process.argv.slice(2)).catch((err: unknown) => { |
87 | 69 | const message: string = err instanceof Error ? err.message : String(err); |
88 | 70 | console.error(`Error: ${message}`); |
89 | 71 | process.exit(1); |
90 | | - } |
| 72 | + }); |
91 | 73 | } |
0 commit comments