( client: GitClient, args: string[], input: GitCliInput, )
| 878 | // --------------------------------------------------------------- |
| 879 | |
| 880 | async function runRevParse( |
| 881 | client: GitClient, |
| 882 | args: string[], |
| 883 | input: GitCliInput, |
| 884 | ): Promise<GitCliResult> { |
| 885 | const parsed = parseFlags(args, { |
| 886 | "abbrev-ref": { kind: "bool" }, |
| 887 | "show-toplevel": { kind: "bool" }, |
| 888 | }); |
| 889 | if ("error" in parsed) { |
| 890 | return { stdout: "", stderr: `git rev-parse: ${parsed.error}\n`, exitCode: 129 }; |
| 891 | } |
| 892 | |
| 893 | if (parsed.flags["show-toplevel"] === true) { |
| 894 | // Print the working-tree root, walking up from cwd. Takes no |
| 895 | // ref, so it short-circuits before the missing-ref check. |
| 896 | const dir = resolveDir(undefined, input.cwd); |
| 897 | try { |
| 898 | const root = await client.repoRoot({ dir }); |
| 899 | return { stdout: `${root}\n`, stderr: "", exitCode: 0 }; |
| 900 | } catch (cause) { |
| 901 | return mapGitError("rev-parse", cause); |
| 902 | } |
| 903 | } |
| 904 | |
| 905 | if (parsed.positional.length === 0) { |
| 906 | return { stdout: "", stderr: "git rev-parse: missing <ref>\n", exitCode: 129 }; |
| 907 | } |
| 908 | if (parsed.positional.length > 1) { |
| 909 | return { |
| 910 | stdout: "", |
| 911 | stderr: `git rev-parse: unexpected argument '${parsed.positional[1]}'\n`, |
| 912 | exitCode: 129, |
| 913 | }; |
| 914 | } |
| 915 | const dir = resolveDir(undefined, input.cwd); |
| 916 | const ref = parsed.positional[0]; |
| 917 | |
| 918 | if (parsed.flags["abbrev-ref"] === true) { |
| 919 | // `--abbrev-ref HEAD` prints the symbolic branch name. On |
| 920 | // detached HEAD real git falls back to printing the resolved |
| 921 | // oid, so mirror that rather than erroring. |
| 922 | try { |
| 923 | if (ref === "HEAD") { |
| 924 | const current = await client.currentBranch({ dir }); |
| 925 | if (current !== undefined) { |
| 926 | return { stdout: `${current}\n`, stderr: "", exitCode: 0 }; |
| 927 | } |
| 928 | } |
| 929 | const oid = await client.revParse({ dir, ref }); |
| 930 | return { stdout: `${oid}\n`, stderr: "", exitCode: 0 }; |
| 931 | } catch (cause) { |
| 932 | return mapGitError("rev-parse", cause); |
| 933 | } |
| 934 | } |
| 935 | |
| 936 | try { |
| 937 | const oid = await client.revParse({ dir, ref }); |
no test coverage detected