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

Function runSwitch

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

Source from the content-addressed store, hash-verified

1285// ---------------------------------------------------------------
1286
1287async function runSwitch(
1288 client: GitClient,
1289 args: string[],
1290 input: GitCliInput,
1291): Promise<GitCliResult> {
1292 // `git switch [-c <new>] <branch> [<start>]`. The modern
1293 // spelling of `checkout` for branch movement; `-c` is the
1294 // `checkout -b` equivalent.
1295 const parsed = parseFlags(args, {
1296 c: { kind: "bool" },
1297 });
1298 if ("error" in parsed) {
1299 return { stdout: "", stderr: `git switch: ${parsed.error}\n`, exitCode: 129 };
1300 }
1301 const dir = resolveDir(undefined, input.cwd);
1302
1303 if (parsed.flags.c === true) {
1304 if (parsed.positional.length === 0) {
1305 return { stdout: "", stderr: "git switch: -c requires a branch name\n", exitCode: 129 };
1306 }
1307 return createAndSwitch(client, dir, parsed.positional[0], parsed.positional[1], "switch");
1308 }
1309
1310 if (parsed.positional.length === 0) {
1311 return { stdout: "", stderr: "git switch: missing <branch>\n", exitCode: 129 };
1312 }
1313 if (parsed.positional.length > 1) {
1314 return {
1315 stdout: "",
1316 stderr: `git switch: unexpected argument '${parsed.positional[1]}'\n`,
1317 exitCode: 129,
1318 };
1319 }
1320 try {
1321 await client.checkout({ dir, ref: parsed.positional[0] });
1322 return { stdout: "", stderr: "", exitCode: 0 };
1323 } catch (cause) {
1324 return mapGitError("switch", cause);
1325 }
1326}
1327
1328/**
1329 * Create a branch (optionally at a start point) and move HEAD to

Callers 1

runGitCliFunction · 0.85

Calls 5

resolveDirFunction · 0.85
createAndSwitchFunction · 0.85
mapGitErrorFunction · 0.85
parseFlagsFunction · 0.70
checkoutMethod · 0.65

Tested by

no test coverage detected