( client: GitClient, args: string[], input: GitCliInput, )
| 1230 | // --------------------------------------------------------------- |
| 1231 | |
| 1232 | async function runCheckout( |
| 1233 | client: GitClient, |
| 1234 | args: string[], |
| 1235 | input: GitCliInput, |
| 1236 | ): Promise<GitCliResult> { |
| 1237 | // `git checkout [-b <new>] <ref> [-- <paths>...]`. `-b` creates |
| 1238 | // a branch (optionally at a start point) and switches to it. |
| 1239 | const parsed = parseFlags(args, { |
| 1240 | force: { kind: "bool", alias: ["f"] }, |
| 1241 | b: { kind: "bool" }, |
| 1242 | }); |
| 1243 | if ("error" in parsed) { |
| 1244 | return { stdout: "", stderr: `git checkout: ${parsed.error}\n`, exitCode: 129 }; |
| 1245 | } |
| 1246 | const sep = args.indexOf("--"); |
| 1247 | const refArgs = |
| 1248 | sep === -1 ? parsed.positional : args.slice(0, sep).filter((a) => !a.startsWith("-")); |
| 1249 | const pathArgs = sep === -1 ? [] : args.slice(sep + 1); |
| 1250 | const dir = resolveDir(undefined, input.cwd); |
| 1251 | |
| 1252 | if (parsed.flags.b === true) { |
| 1253 | // Create-and-switch. `refArgs` is `<new-branch> [<start>]`. |
| 1254 | if (refArgs.length === 0) { |
| 1255 | return { stdout: "", stderr: "git checkout: -b requires a branch name\n", exitCode: 129 }; |
| 1256 | } |
| 1257 | return createAndSwitch(client, dir, refArgs[0], refArgs[1], "checkout"); |
| 1258 | } |
| 1259 | |
| 1260 | if (refArgs.length === 0) { |
| 1261 | return { stdout: "", stderr: "git checkout: missing <ref>\n", exitCode: 129 }; |
| 1262 | } |
| 1263 | if (refArgs.length > 1) { |
| 1264 | return { |
| 1265 | stdout: "", |
| 1266 | stderr: `git checkout: unexpected argument '${refArgs[1]}'\n`, |
| 1267 | exitCode: 129, |
| 1268 | }; |
| 1269 | } |
| 1270 | try { |
| 1271 | await client.checkout({ |
| 1272 | dir, |
| 1273 | ref: refArgs[0], |
| 1274 | paths: pathArgs.length > 0 ? pathArgs : undefined, |
| 1275 | force: parsed.flags.force === true, |
| 1276 | }); |
| 1277 | return { stdout: "", stderr: "", exitCode: 0 }; |
| 1278 | } catch (cause) { |
| 1279 | return mapGitError("checkout", cause); |
| 1280 | } |
| 1281 | } |
| 1282 | |
| 1283 | // --------------------------------------------------------------- |
| 1284 | // switch |
no test coverage detected