MCPcopy Create free account
hub / github.com/cloudflare/computer / runRevParse

Function runRevParse

packages/computer/src/git/cli.ts:880–942  ·  view source on GitHub ↗
(
  client: GitClient,
  args: string[],
  input: GitCliInput,
)

Source from the content-addressed store, hash-verified

878// ---------------------------------------------------------------
879
880async 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 });

Callers 1

runGitCliFunction · 0.85

Calls 6

resolveDirFunction · 0.85
mapGitErrorFunction · 0.85
repoRootMethod · 0.80
revParseMethod · 0.80
parseFlagsFunction · 0.70
currentBranchMethod · 0.65

Tested by

no test coverage detected