( client: GitClient, args: string[], input: GitCliInput, )
| 741 | // --------------------------------------------------------------- |
| 742 | |
| 743 | async function runLog( |
| 744 | client: GitClient, |
| 745 | args: string[], |
| 746 | input: GitCliInput, |
| 747 | ): Promise<GitCliResult> { |
| 748 | // `git log [-n <N>] [-<N>] [--oneline] [<ref>]`. Default output |
| 749 | // is the full commit form; --oneline collapses each entry to a |
| 750 | // single line. |
| 751 | // |
| 752 | // Rewrite the `-<N>` shorthand (e.g. `-1`, `-5`) to `-n <N>` |
| 753 | // before parsing — the generic parser would otherwise reject |
| 754 | // `-5` as an unknown short option. `-0` and non-numeric forms |
| 755 | // fall through to the `-n` validation below, which rejects |
| 756 | // them. |
| 757 | let shorthandDepth: string | undefined; |
| 758 | const rewritten: string[] = []; |
| 759 | for (const arg of args) { |
| 760 | const m = /^-(\d+)$/.exec(arg); |
| 761 | if (m) { |
| 762 | shorthandDepth = m[1]; |
| 763 | continue; |
| 764 | } |
| 765 | rewritten.push(arg); |
| 766 | } |
| 767 | const parsed = parseFlags(rewritten, { |
| 768 | n: { kind: "value" }, |
| 769 | oneline: { kind: "bool" }, |
| 770 | }); |
| 771 | if ("error" in parsed) { |
| 772 | return { stdout: "", stderr: `git log: ${parsed.error}\n`, exitCode: 129 }; |
| 773 | } |
| 774 | if (shorthandDepth !== undefined && parsed.flags.n === undefined) { |
| 775 | parsed.flags.n = shorthandDepth; |
| 776 | } |
| 777 | if (parsed.positional.length > 1) { |
| 778 | return { |
| 779 | stdout: "", |
| 780 | stderr: `git log: unexpected argument '${parsed.positional[1]}'\n`, |
| 781 | exitCode: 129, |
| 782 | }; |
| 783 | } |
| 784 | let depth: number | undefined; |
| 785 | if (parsed.flags.n !== undefined) { |
| 786 | const v = Number.parseInt(parsed.flags.n as string, 10); |
| 787 | if (!Number.isFinite(v) || v < 1) { |
| 788 | return { |
| 789 | stdout: "", |
| 790 | stderr: `git log: -n requires a positive integer (got ${JSON.stringify(parsed.flags.n)})\n`, |
| 791 | exitCode: 129, |
| 792 | }; |
| 793 | } |
| 794 | depth = v; |
| 795 | } |
| 796 | const dir = resolveDir(undefined, input.cwd); |
| 797 | try { |
| 798 | const ref = await resolveRevisionRef(client, dir, parsed.positional[0]); |
| 799 | const commits = await client.log({ dir, ref, depth }); |
| 800 | const stdout = parsed.flags.oneline ? formatLogOneline(commits) : formatLogFull(commits); |
no test coverage detected