| ... | ... | @@ -36,7 +36,6 @@ import { |
| 36 | 36 | parseRemoteRefWithRemoteNames, |
| 37 | 37 | } from "../git/remoteRefs.ts"; |
| 38 | 38 | import { ServerConfig } from "../config.ts"; |
| 39 | | const isGitCommandError = Schema.is(GitCommandError); |
| 40 | 39 | |
| 41 | 40 | const DEFAULT_TIMEOUT_MS = 30_000; |
| 42 | 41 | const DEFAULT_MAX_OUTPUT_BYTES = 1_000_000; |
| ... | ... | @@ -100,7 +99,7 @@ interface ExecuteGitOptions { |
| 100 | 99 | stdin?: string | undefined; |
| 101 | 100 | timeoutMs?: number | undefined; |
| 102 | 101 | allowNonZeroExit?: boolean | undefined; |
| 103 | | fallbackErrorMessage?: string | undefined; |
| 102 | fallbackErrorDetail?: string | undefined; |
| 104 | 103 | env?: NodeJS.ProcessEnv | undefined; |
| 105 | 104 | maxOutputBytes?: number | undefined; |
| 106 | 105 | appendTruncationMarker?: boolean | undefined; |
| ... | ... | @@ -326,8 +325,15 @@ function deriveLocalBranchNameFromRemoteRef(branchName: string): string | null { |
| 326 | 325 | return localBranch.length > 0 ? localBranch : null; |
| 327 | 326 | } |
| 328 | 327 | |
| 329 | | function commandLabel(args: readonly string[]): string { |
| 330 | | return `git ${args.join(" ")}`; |
| 328 | function gitCommandContext( |
| 329 | input: Pick<GitVcsDriver.ExecuteGitInput, "operation" | "cwd" | "args">, |
| 330 | ) { |
| 331 | return { |
| 332 | operation: input.operation, |
| 333 | command: "git", |
| 334 | cwd: input.cwd, |
| 335 | argumentCount: input.args.length, |
| 336 | } as const; |
| 331 | 337 | } |
| 332 | 338 | |
| 333 | 339 | function parseDefaultBranchFromRemoteHeadRef(value: string, remoteName: string): string | null { |
| ... | ... | @@ -340,50 +346,28 @@ function parseDefaultBranchFromRemoteHeadRef(value: string, remoteName: string): |
| 340 | 346 | return refName.length > 0 ? refName : null; |
| 341 | 347 | } |
| 342 | 348 | |
| 343 | | function createGitCommandError( |
| 344 | | operation: string, |
| 345 | | cwd: string, |
| 346 | | args: readonly string[], |
| 347 | | detail: string, |
| 348 | | cause?: unknown, |
| 349 | | ): GitCommandError { |
| 350 | | return new GitCommandError({ |
| 351 | | operation, |
| 352 | | command: commandLabel(args), |
| 353 | | cwd, |
| 354 | | detail, |
| 355 | | ...(cause !== undefined ? { cause } : {}), |
| 356 | | }); |
| 357 | | } |
| 349 | function isMissingGitCwdError(error: GitCommandError): boolean { |
| 350 | if (!(error.cause instanceof PlatformError.PlatformError)) { |
| 351 | return false; |
| 352 | } |
| 358 | 353 | |
| 359 | | function quoteGitCommand(args: ReadonlyArray<string>): string { |
| 360 | | return `git ${args.join(" ")}`; |
| 361 | | } |
| 354 | const reason = error.cause.reason; |
| 355 | if (reason._tag === "NotFound") { |
| 356 | return reason.pathOrDescriptor === error.cwd; |
| 357 | } |
| 362 | 358 | |
| 363 | | function isMissingGitCwdError(error: GitCommandError): boolean { |
| 364 | | const normalized = `${error.detail}\n${error.message}`.toLowerCase(); |
| 365 | 359 | return ( |
| 366 | | normalized.includes("no such file or directory") || |
| 367 | | normalized.includes("notfound: filesystem.access") || |
| 368 | | normalized.includes("enoent") || |
| 369 | | normalized.includes("not a directory") |
| 360 | reason._tag === "BadResource" && |
| 361 | reason.pathOrDescriptor === error.cwd && |
| 362 | typeof reason.cause === "object" && |
| 363 | reason.cause !== null && |
| 364 | "code" in reason.cause && |
| 365 | reason.cause.code === "ENOTDIR" |
| 370 | 366 | ); |
| 371 | 367 | } |
| 372 | 368 | |
| 373 | | function toGitCommandError( |
| 374 | | input: Pick<GitVcsDriver.ExecuteGitInput, "operation" | "cwd" | "args">, |
| 375 | | detail: string, |
| 376 | | ) { |
| 377 | | return (cause: unknown) => |
| 378 | | isGitCommandError(cause) |
| 379 | | ? cause |
| 380 | | : new GitCommandError({ |
| 381 | | operation: input.operation, |
| 382 | | command: quoteGitCommand(input.args), |
| 383 | | cwd: input.cwd, |
| 384 | | detail: `${cause instanceof Error && cause.message.length > 0 ? cause.message : "Unknown error"} - ${detail}`, |
| 385 | | ...(cause !== undefined ? { cause } : {}), |
| 386 | | }); |
| 369 | function isNonRepositoryGitStderr(stderr: string): boolean { |
| 370 | return stderr.toLowerCase().includes("not a git repository"); |
| 387 | 371 | } |
| 388 | 372 | |
| 389 | 373 | interface Trace2Monitor { |
| ... | ... | @@ -402,7 +386,11 @@ const addCurrentSpanEvent = (name: string, attributes: Record<string, unknown>) |
| 402 | 386 | yield* Effect.sync(() => { |
| 403 | 387 | span.event(name, timestamp, compactTraceAttributes(attributes)); |
| 404 | 388 | }); |
| 405 | | }).pipe(Effect.catch(() => Effect.void)); |
| 389 | }).pipe( |
| 390 | Effect.catchTags({ |
| 391 | NoSuchElementError: () => Effect.void, |
| 392 | }), |
| 393 | ); |
| 406 | 394 | |
| 407 | 395 | function trace2ChildKey(record: Record<string, unknown>): string | null { |
| 408 | 396 | const childId = record.child_id; |
| ... | ... | @@ -451,7 +439,7 @@ const createTrace2Monitor = Effect.fn("createTrace2Monitor")(function* ( |
| 451 | 439 | const traceRecord = decodeJsonResult(Trace2Record)(trimmedLine); |
| 452 | 440 | if (Result.isFailure(traceRecord)) { |
| 453 | 441 | yield* Effect.logDebug( |
| 454 | | `GitVcsDriver.trace2: failed to parse trace line for ${quoteGitCommand(input.args)} in ${input.cwd}`, |
| 442 | `GitVcsDriver.trace2: failed to parse trace line for ${input.operation} in ${input.cwd} (${input.args.length} arguments)`, |
| 455 | 443 | traceRecord.failure, |
| 456 | 444 | ); |
| 457 | 445 | return; |
| ... | ... | @@ -574,9 +562,9 @@ const createTrace2Monitor = Effect.fn("createTrace2Monitor")(function* ( |
| 574 | 562 | }; |
| 575 | 563 | }); |
| 576 | 564 | |
| 577 | | const collectOutput = Effect.fnUntraced(function* <E>( |
| 565 | const collectOutput = Effect.fnUntraced(function* ( |
| 578 | 566 | input: Pick<GitVcsDriver.ExecuteGitInput, "operation" | "cwd" | "args">, |
| 579 | | stream: Stream.Stream<Uint8Array, E>, |
| 567 | stream: Stream.Stream<Uint8Array, PlatformError.PlatformError>, |
| 580 | 568 | maxOutputBytes: number, |
| 581 | 569 | appendTruncationMarker: boolean, |
| 582 | 570 | onLine: ((line: string) => Effect.Effect<void, never>) | undefined, |
| ... | ... | @@ -614,10 +602,9 @@ const collectOutput = Effect.fnUntraced(function* <E>( |
| 614 | 602 | const nextBytes = bytes + chunk.byteLength; |
| 615 | 603 | if (!appendTruncationMarker && nextBytes > maxOutputBytes) { |
| 616 | 604 | return yield* new GitCommandError({ |
| 617 | | operation: input.operation, |
| 618 | | command: quoteGitCommand(input.args), |
| 619 | | cwd: input.cwd, |
| 620 | | detail: `${quoteGitCommand(input.args)} output exceeded ${maxOutputBytes} bytes and was truncated.`, |
| 605 | ...gitCommandContext(input), |
| 606 | detail: `Git output exceeded ${maxOutputBytes} bytes and was truncated.`, |
| 607 | outputLength: nextBytes, |
| 621 | 608 | }); |
| 622 | 609 | } |
| 623 | 610 | |
| ... | ... | @@ -635,7 +622,14 @@ const collectOutput = Effect.fnUntraced(function* <E>( |
| 635 | 622 | }); |
| 636 | 623 | |
| 637 | 624 | yield* Stream.runForEach(stream, processChunk).pipe( |
| 638 | | Effect.mapError(toGitCommandError(input, "output stream failed.")), |
| 625 | Effect.catchTags({ |
| 626 | PlatformError: (cause) => |
| 627 | new GitCommandError({ |
| 628 | ...gitCommandContext(input), |
| 629 | detail: "Failed to read Git process output.", |
| 630 | cause, |
| 631 | }), |
| 632 | }), |
| 639 | 633 | ); |
| 640 | 634 | |
| 641 | 635 | const remainder = truncated ? "" : decoder.decode(); |
| ... | ... | @@ -669,7 +663,14 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* |
| 669 | 663 | const trace2Monitor = yield* createTrace2Monitor(commandInput, input.progress).pipe( |
| 670 | 664 | Effect.provideService(Path.Path, path), |
| 671 | 665 | Effect.provideService(FileSystem.FileSystem, fileSystem), |
| 672 | | Effect.mapError(toGitCommandError(commandInput, "failed to create trace2 monitor.")), |
| 666 | Effect.mapError( |
| 667 | (cause) => |
| 668 | new GitCommandError({ |
| 669 | ...gitCommandContext(commandInput), |
| 670 | detail: "Failed to create Git trace monitor.", |
| 671 | cause, |
| 672 | }), |
| 673 | ), |
| 673 | 674 | ); |
| 674 | 675 | const child = yield* commandSpawner |
| 675 | 676 | .spawn( |
| ... | ... | @@ -682,7 +683,16 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* |
| 682 | 683 | }, |
| 683 | 684 | }), |
| 684 | 685 | ) |
| 685 | | .pipe(Effect.mapError(toGitCommandError(commandInput, "failed to spawn."))); |
| 686 | .pipe( |
| 687 | Effect.mapError( |
| 688 | (cause) => |
| 689 | new GitCommandError({ |
| 690 | ...gitCommandContext(commandInput), |
| 691 | detail: "Failed to spawn Git process.", |
| 692 | cause, |
| 693 | }), |
| 694 | ), |
| 695 | ); |
| 686 | 696 | |
| 687 | 697 | const [stdout, stderr, exitCode] = yield* Effect.all( |
| 688 | 698 | [ |
| ... | ... | @@ -701,12 +711,26 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* |
| 701 | 711 | input.progress?.onStderrLine, |
| 702 | 712 | ), |
| 703 | 713 | child.exitCode.pipe( |
| 704 | | Effect.mapError(toGitCommandError(commandInput, "failed to report exit code.")), |
| 714 | Effect.mapError( |
| 715 | (cause) => |
| 716 | new GitCommandError({ |
| 717 | ...gitCommandContext(commandInput), |
| 718 | detail: "Failed to read Git process exit code.", |
| 719 | cause, |
| 720 | }), |
| 721 | ), |
| 705 | 722 | ), |
| 706 | 723 | input.stdin === undefined |
| 707 | 724 | ? Effect.void |
| 708 | 725 | : Stream.run(Stream.encodeText(Stream.make(input.stdin)), child.stdin).pipe( |
| 709 | | Effect.mapError(toGitCommandError(commandInput, "failed to write stdin.")), |
| 726 | Effect.mapError( |
| 727 | (cause) => |
| 728 | new GitCommandError({ |
| 729 | ...gitCommandContext(commandInput), |
| 730 | detail: "Failed to write Git process input.", |
| 731 | cause, |
| 732 | }), |
| 733 | ), |
| 710 | 734 | ), |
| 711 | 735 | ], |
| 712 | 736 | { concurrency: "unbounded" }, |
| ... | ... | @@ -714,15 +738,12 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* |
| 714 | 738 | yield* trace2Monitor.flush; |
| 715 | 739 | |
| 716 | 740 | if (!input.allowNonZeroExit && exitCode !== 0) { |
| 717 | | const trimmedStderr = stderr.text.trim(); |
| 718 | 741 | return yield* new GitCommandError({ |
| 719 | | operation: commandInput.operation, |
| 720 | | command: quoteGitCommand(commandInput.args), |
| 721 | | cwd: commandInput.cwd, |
| 722 | | detail: |
| 723 | | trimmedStderr.length > 0 |
| 724 | | ? `${quoteGitCommand(commandInput.args)} failed: ${trimmedStderr}` |
| 725 | | : `${quoteGitCommand(commandInput.args)} failed with code ${exitCode}.`, |
| 742 | ...gitCommandContext(commandInput), |
| 743 | detail: "Git command exited with a non-zero status.", |
| 744 | exitCode, |
| 745 | stdoutLength: stdout.text.length, |
| 746 | stderrLength: stderr.text.length, |
| 726 | 747 | }); |
| 727 | 748 | } |
| 728 | 749 | |
| ... | ... | @@ -743,10 +764,8 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* |
| 743 | 764 | onNone: () => |
| 744 | 765 | Effect.fail( |
| 745 | 766 | new GitCommandError({ |
| 746 | | operation: commandInput.operation, |
| 747 | | command: quoteGitCommand(commandInput.args), |
| 748 | | cwd: commandInput.cwd, |
| 749 | | detail: `${quoteGitCommand(commandInput.args)} timed out.`, |
| 767 | ...gitCommandContext(commandInput), |
| 768 | detail: "Git command timed out.", |
| 750 | 769 | }), |
| 751 | 770 | ), |
| 752 | 771 | onSome: Effect.succeed, |
| ... | ... | @@ -799,22 +818,14 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* |
| 799 | 818 | if (options.allowNonZeroExit || result.exitCode === 0) { |
| 800 | 819 | return Effect.succeed(result); |
| 801 | 820 | } |
| 802 | | const stderr = result.stderr.trim(); |
| 803 | | if (stderr.length > 0) { |
| 804 | | return Effect.fail(createGitCommandError(operation, cwd, args, stderr)); |
| 805 | | } |
| 806 | | if (options.fallbackErrorMessage) { |
| 807 | | return Effect.fail( |
| 808 | | createGitCommandError(operation, cwd, args, options.fallbackErrorMessage), |
| 809 | | ); |
| 810 | | } |
| 811 | 821 | return Effect.fail( |
| 812 | | createGitCommandError( |
| 813 | | operation, |
| 814 | | cwd, |
| 815 | | args, |
| 816 | | `${commandLabel(args)} failed: code=${result.exitCode ?? "null"}`, |
| 817 | | ), |
| 822 | new GitCommandError({ |
| 823 | ...gitCommandContext({ operation, cwd, args }), |
| 824 | detail: options.fallbackErrorDetail ?? "Git command exited with a non-zero status.", |
| 825 | ...(result.exitCode === null ? {} : { exitCode: result.exitCode }), |
| 826 | stdoutLength: result.stdout.length, |
| 827 | stderrLength: result.stderr.length, |
| 828 | }), |
| 818 | 829 | ); |
| 819 | 830 | }), |
| 820 | 831 | ); |
| ... | ... | @@ -877,12 +888,14 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* |
| 877 | 888 | } |
| 878 | 889 | } |
| 879 | 890 | |
| 880 | | return yield* createGitCommandError( |
| 881 | | "GitVcsDriver.renameBranch", |
| 882 | | cwd, |
| 883 | | ["branch", "-m", "--", desiredBranch], |
| 884 | | `Could not find an available branch name for '${desiredBranch}'.`, |
| 885 | | ); |
| 891 | return yield* new GitCommandError({ |
| 892 | ...gitCommandContext({ |
| 893 | operation: "GitVcsDriver.renameBranch", |
| 894 | cwd, |
| 895 | args: ["branch", "-m", "--", desiredBranch], |
| 896 | }), |
| 897 | detail: `Could not find an available branch name for '${desiredBranch}'.`, |
| 898 | }); |
| 886 | 899 | }); |
| 887 | 900 | |
| 888 | 901 | const resolveCurrentUpstream = Effect.fn("resolveCurrentUpstream")(function* (cwd: string) { |
| ... | ... | @@ -1024,12 +1037,14 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* |
| 1024 | 1037 | if (firstRemote) { |
| 1025 | 1038 | return firstRemote; |
| 1026 | 1039 | } |
| 1027 | | return yield* createGitCommandError( |
| 1028 | | "GitVcsDriver.resolvePrimaryRemoteName", |
| 1029 | | cwd, |
| 1030 | | ["remote"], |
| 1031 | | "No git remote is configured for this repository.", |
| 1032 | | ); |
| 1040 | return yield* new GitCommandError({ |
| 1041 | ...gitCommandContext({ |
| 1042 | operation: "GitVcsDriver.resolvePrimaryRemoteName", |
| 1043 | cwd, |
| 1044 | args: ["remote"], |
| 1045 | }), |
| 1046 | detail: "No git remote is configured for this repository.", |
| 1047 | }); |
| 1033 | 1048 | }); |
| 1034 | 1049 | |
| 1035 | 1050 | const resolvePushRemoteName = Effect.fn("resolvePushRemoteName")(function* ( |
| ... | ... | @@ -1174,19 +1189,31 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* |
| 1174 | 1189 | cwd, |
| 1175 | 1190 | ["rev-parse", "--abbrev-ref", "HEAD"], |
| 1176 | 1191 | { allowNonZeroExit: true }, |
| 1177 | | ).pipe(Effect.catchIf(isMissingGitCwdError, () => Effect.succeed(null))); |
| 1192 | ).pipe( |
| 1193 | Effect.catchTags({ |
| 1194 | GitCommandError: (error) => |
| 1195 | isMissingGitCwdError(error) ? Effect.succeed(null) : Effect.fail(error), |
| 1196 | }), |
| 1197 | ); |
| 1178 | 1198 | |
| 1179 | 1199 | if (branchResult === null) { |
| 1180 | 1200 | return NON_REPOSITORY_REMOTE_STATUS_DETAILS; |
| 1181 | 1201 | } |
| 1182 | 1202 | if (branchResult.exitCode !== 0) { |
| 1183 | | const stderr = branchResult.stderr.trim(); |
| 1184 | | return yield* createGitCommandError( |
| 1185 | | "GitVcsDriver.statusDetailsRemote.branch", |
| 1186 | | cwd, |
| 1187 | | ["rev-parse", "--abbrev-ref", "HEAD"], |
| 1188 | | stderr || "git branch lookup failed", |
| 1189 | | ); |
| 1203 | if (isNonRepositoryGitStderr(branchResult.stderr)) { |
| 1204 | return NON_REPOSITORY_REMOTE_STATUS_DETAILS; |
| 1205 | } |
| 1206 | return yield* new GitCommandError({ |
| 1207 | ...gitCommandContext({ |
| 1208 | operation: "GitVcsDriver.statusDetailsRemote.branch", |
| 1209 | cwd, |
| 1210 | args: ["rev-parse", "--abbrev-ref", "HEAD"], |
| 1211 | }), |
| 1212 | detail: "Git branch lookup failed.", |
| 1213 | exitCode: branchResult.exitCode, |
| 1214 | stdoutLength: branchResult.stdout.length, |
| 1215 | stderrLength: branchResult.stderr.length, |
| 1216 | }); |
| 1190 | 1217 | } |
| 1191 | 1218 | |
| 1192 | 1219 | const branchValue = branchResult.stdout.trim(); |
| ... | ... | @@ -1284,20 +1311,32 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* |
| 1284 | 1311 | { |
| 1285 | 1312 | allowNonZeroExit: true, |
| 1286 | 1313 | }, |
| 1287 | | ).pipe(Effect.catchIf(isMissingGitCwdError, () => Effect.succeed(null))); |
| 1314 | ).pipe( |
| 1315 | Effect.catchTags({ |
| 1316 | GitCommandError: (error) => |
| 1317 | isMissingGitCwdError(error) ? Effect.succeed(null) : Effect.fail(error), |
| 1318 | }), |
| 1319 | ); |
| 1288 | 1320 | |
| 1289 | 1321 | if (statusResult === null) { |
| 1290 | 1322 | return NON_REPOSITORY_STATUS_DETAILS; |
| 1291 | 1323 | } |
| 1292 | 1324 | |
| 1293 | 1325 | if (statusResult.exitCode !== 0) { |
| 1294 | | const stderr = statusResult.stderr.trim(); |
| 1295 | | return yield* createGitCommandError( |
| 1296 | | "GitVcsDriver.statusDetails.status", |
| 1297 | | cwd, |
| 1298 | | ["status", "--porcelain=2", "--branch"], |
| 1299 | | stderr || "git status failed", |
| 1300 | | ); |
| 1326 | if (isNonRepositoryGitStderr(statusResult.stderr)) { |
| 1327 | return NON_REPOSITORY_STATUS_DETAILS; |
| 1328 | } |
| 1329 | return yield* new GitCommandError({ |
| 1330 | ...gitCommandContext({ |
| 1331 | operation: "GitVcsDriver.statusDetails.status", |
| 1332 | cwd, |
| 1333 | args: ["status", "--porcelain=2", "--branch"], |
| 1334 | }), |
| 1335 | detail: "Git status failed.", |
| 1336 | exitCode: statusResult.exitCode, |
| 1337 | stdoutLength: statusResult.stdout.length, |
| 1338 | stderrLength: statusResult.stderr.length, |
| 1339 | }); |
| 1301 | 1340 | } |
| 1302 | 1341 | |
| 1303 | 1342 | const [unstagedNumstatStdout, stagedNumstatStdout, defaultRefResult, hasPrimaryRemote] = |
| ... | ... | @@ -1436,7 +1475,10 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* |
| 1436 | 1475 | "statusDetails", |
| 1437 | 1476 | )(function* (cwd) { |
| 1438 | 1477 | yield* refreshStatusUpstreamIfStale(cwd).pipe( |
| 1439 | | Effect.catchIf(isMissingGitCwdError, () => Effect.void), |
| 1478 | Effect.catchTags({ |
| 1479 | GitCommandError: (error) => |
| 1480 | isMissingGitCwdError(error) ? Effect.void : Effect.fail(error), |
| 1481 | }), |
| 1440 | 1482 | Effect.ignoreCause({ log: true }), |
| 1441 | 1483 | ); |
| 1442 | 1484 | return yield* readStatusDetailsLocal(cwd); |
| ... | ... | @@ -1446,7 +1488,10 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* |
| 1446 | 1488 | Effect.fn("statusDetailsRemote")(function* (cwd, options) { |
| 1447 | 1489 | if (options?.refreshUpstream !== false) { |
| 1448 | 1490 | yield* refreshStatusUpstreamIfStale(cwd).pipe( |
| 1449 | | Effect.catchIf(isMissingGitCwdError, () => Effect.void), |
| 1491 | Effect.catchTags({ |
| 1492 | GitCommandError: (error) => |
| 1493 | isMissingGitCwdError(error) ? Effect.void : Effect.fail(error), |
| 1494 | }), |
| 1450 | 1495 | Effect.ignoreCause({ log: true }), |
| 1451 | 1496 | ); |
| 1452 | 1497 | } |
| ... | ... | @@ -1474,7 +1519,9 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* |
| 1474 | 1519 | Effect.fn("prepareCommitContext")(function* (cwd, filePaths) { |
| 1475 | 1520 | if (filePaths && filePaths.length > 0) { |
| 1476 | 1521 | yield* runGit("GitVcsDriver.prepareCommitContext.reset", cwd, ["reset"]).pipe( |
| 1477 | | Effect.catch(() => Effect.void), |
| 1522 | Effect.catchTags({ |
| 1523 | GitCommandError: () => Effect.void, |
| 1524 | }), |
| 1478 | 1525 | ); |
| 1479 | 1526 | yield* runGit("GitVcsDriver.prepareCommitContext.addSelected", cwd, [ |
| 1480 | 1527 | "add", |
| ... | ... | @@ -1550,12 +1597,14 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* |
| 1550 | 1597 | const details = yield* statusDetails(cwd); |
| 1551 | 1598 | const branch = details.branch ?? fallbackBranch; |
| 1552 | 1599 | if (!branch) { |
| 1553 | | return yield* createGitCommandError( |
| 1554 | | "GitVcsDriver.pushCurrentBranch", |
| 1555 | | cwd, |
| 1556 | | ["push"], |
| 1557 | | "Cannot push from detached HEAD.", |
| 1558 | | ); |
| 1600 | return yield* new GitCommandError({ |
| 1601 | ...gitCommandContext({ |
| 1602 | operation: "GitVcsDriver.pushCurrentBranch", |
| 1603 | cwd, |
| 1604 | args: ["push"], |
| 1605 | }), |
| 1606 | detail: "Cannot push from detached HEAD.", |
| 1607 | }); |
| 1559 | 1608 | } |
| 1560 | 1609 | |
| 1561 | 1610 | const requestedRemoteName = options?.remoteName?.trim() || null; |
| ... | ... | @@ -1614,12 +1663,14 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* |
| 1614 | 1663 | if (!details.hasUpstream) { |
| 1615 | 1664 | const publishRemoteName = yield* resolvePushRemoteName(cwd, branch); |
| 1616 | 1665 | if (!publishRemoteName) { |
| 1617 | | return yield* createGitCommandError( |
| 1618 | | "GitVcsDriver.pushCurrentBranch", |
| 1619 | | cwd, |
| 1620 | | ["push"], |
| 1621 | | "Cannot push because no git remote is configured for this repository.", |
| 1622 | | ); |
| 1666 | return yield* new GitCommandError({ |
| 1667 | ...gitCommandContext({ |
| 1668 | operation: "GitVcsDriver.pushCurrentBranch", |
| 1669 | cwd, |
| 1670 | args: ["push"], |
| 1671 | }), |
| 1672 | detail: "Cannot push because no git remote is configured for this repository.", |
| 1673 | }); |
| 1623 | 1674 | } |
| 1624 | 1675 | const publishBranch = yield* resolvePublishBranchName(cwd, branch); |
| 1625 | 1676 | yield* runGit("GitVcsDriver.pushCurrentBranch.pushWithUpstream", cwd, [ |
| ... | ... | @@ -1668,20 +1719,24 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* |
| 1668 | 1719 | const details = yield* statusDetails(cwd); |
| 1669 | 1720 | const refName = details.branch; |
| 1670 | 1721 | if (!refName) { |
| 1671 | | return yield* createGitCommandError( |
| 1672 | | "GitVcsDriver.pullCurrentBranch", |
| 1673 | | cwd, |
| 1674 | | ["pull", "--ff-only"], |
| 1675 | | "Cannot pull from detached HEAD.", |
| 1676 | | ); |
| 1722 | return yield* new GitCommandError({ |
| 1723 | ...gitCommandContext({ |
| 1724 | operation: "GitVcsDriver.pullCurrentBranch", |
| 1725 | cwd, |
| 1726 | args: ["pull", "--ff-only"], |
| 1727 | }), |
| 1728 | detail: "Cannot pull from detached HEAD.", |
| 1729 | }); |
| 1677 | 1730 | } |
| 1678 | 1731 | if (!details.hasUpstream) { |
| 1679 | | return yield* createGitCommandError( |
| 1680 | | "GitVcsDriver.pullCurrentBranch", |
| 1681 | | cwd, |
| 1682 | | ["pull", "--ff-only"], |
| 1683 | | "Current branch has no upstream configured. Push with upstream first.", |
| 1684 | | ); |
| 1732 | return yield* new GitCommandError({ |
| 1733 | ...gitCommandContext({ |
| 1734 | operation: "GitVcsDriver.pullCurrentBranch", |
| 1735 | cwd, |
| 1736 | args: ["pull", "--ff-only"], |
| 1737 | }), |
| 1738 | detail: "Current branch has no upstream configured. Push with upstream first.", |
| 1739 | }); |
| 1685 | 1740 | } |
| 1686 | 1741 | const beforeSha = yield* runGitStdout( |
| 1687 | 1742 | "GitVcsDriver.pullCurrentBranch.beforeSha", |
| ... | ... | @@ -1691,7 +1746,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* |
| 1691 | 1746 | ).pipe(Effect.map((stdout) => stdout.trim())); |
| 1692 | 1747 | yield* executeGit("GitVcsDriver.pullCurrentBranch.pull", cwd, ["pull", "--ff-only"], { |
| 1693 | 1748 | timeoutMs: 30_000, |
| 1694 | | fallbackErrorMessage: "git pull failed", |
| 1749 | fallbackErrorDetail: "git pull failed", |
| 1695 | 1750 | }); |
| 1696 | 1751 | const afterSha = yield* runGitStdout( |
| 1697 | 1752 | "GitVcsDriver.pullCurrentBranch.afterSha", |
| ... | ... | @@ -1874,14 +1929,14 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* |
| 1874 | 1929 | crypto.digest("SHA-256", new TextEncoder().encode(diff)).pipe( |
| 1875 | 1930 | Effect.map(Encoding.encodeHex), |
| 1876 | 1931 | Effect.mapError( |
| 1877 | | toGitCommandError( |
| 1878 | | { |
| 1932 | (cause) => |
| 1933 | new GitCommandError({ |
| 1879 | 1934 | operation: "GitVcsDriver.getReviewDiffPreview.hash", |
| 1935 | command: "crypto.digest SHA-256", |
| 1880 | 1936 | cwd: input.cwd, |
| 1881 | | args: [], |
| 1882 | | }, |
| 1883 | | "failed to hash review diff.", |
| 1884 | | ), |
| 1937 | detail: "Failed to hash review diff.", |
| 1938 | cause, |
| 1939 | }), |
| 1885 | 1940 | ), |
| 1886 | 1941 | ); |
| 1887 | 1942 | const [dirtyDiffHash, baseDiffHash] = yield* Effect.all([ |
| ... | ... | @@ -1939,20 +1994,23 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* |
| 1939 | 1994 | allowNonZeroExit: true, |
| 1940 | 1995 | }, |
| 1941 | 1996 | ).pipe( |
| 1942 | | Effect.catchIf(isMissingGitCwdError, () => |
| 1943 | | Effect.succeed({ |
| 1944 | | exitCode: ChildProcessSpawner.ExitCode(128), |
| 1945 | | stdout: "", |
| 1946 | | stderr: "fatal: not a git repository", |
| 1947 | | stdoutTruncated: false, |
| 1948 | | stderrTruncated: false, |
| 1949 | | }), |
| 1950 | | ), |
| 1997 | Effect.catchTags({ |
| 1998 | GitCommandError: (error) => |
| 1999 | isMissingGitCwdError(error) |
| 2000 | ? Effect.succeed({ |
| 2001 | exitCode: ChildProcessSpawner.ExitCode(128), |
| 2002 | stdout: "", |
| 2003 | stderr: "fatal: not a git repository", |
| 2004 | stdoutTruncated: false, |
| 2005 | stderrTruncated: false, |
| 2006 | }) |
| 2007 | : Effect.fail(error), |
| 2008 | }), |
| 1951 | 2009 | ); |
| 1952 | 2010 | |
| 1953 | 2011 | if (localBranchResult.exitCode !== 0) { |
| 1954 | 2012 | const stderr = localBranchResult.stderr.trim(); |
| 1955 | | if (stderr.toLowerCase().includes("not a git repository")) { |
| 2013 | if (isNonRepositoryGitStderr(stderr)) { |
| 1956 | 2014 | return { |
| 1957 | 2015 | refs: [], |
| 1958 | 2016 | isRepo: false, |
| ... | ... | @@ -1961,12 +2019,17 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* |
| 1961 | 2019 | totalCount: 0, |
| 1962 | 2020 | }; |
| 1963 | 2021 | } |
| 1964 | | return yield* createGitCommandError( |
| 1965 | | "GitVcsDriver.listRefs", |
| 1966 | | input.cwd, |
| 1967 | | ["branch", "--no-color", "--no-column"], |
| 1968 | | stderr || "git branch failed", |
| 1969 | | ); |
| 2022 | return yield* new GitCommandError({ |
| 2023 | ...gitCommandContext({ |
| 2024 | operation: "GitVcsDriver.listRefs", |
| 2025 | cwd: input.cwd, |
| 2026 | args: ["branch", "--no-color", "--no-column"], |
| 2027 | }), |
| 2028 | detail: "Git branch listing failed.", |
| 2029 | exitCode: localBranchResult.exitCode, |
| 2030 | stdoutLength: localBranchResult.stdout.length, |
| 2031 | stderrLength: localBranchResult.stderr.length, |
| 2032 | }); |
| 1970 | 2033 | } |
| 1971 | 2034 | |
| 1972 | 2035 | const remoteBranchResultEffect = executeGit( |
| ... | ... | @@ -1978,19 +2041,27 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* |
| 1978 | 2041 | allowNonZeroExit: true, |
| 1979 | 2042 | }, |
| 1980 | 2043 | ).pipe( |
| 1981 | | Effect.catch((error) => |
| 1982 | | Effect.logWarning( |
| 1983 | | `GitVcsDriver.listRefs: remote refName lookup failed for ${input.cwd}: ${error.message}. Falling back to an empty remote refName list.`, |
| 1984 | | ).pipe( |
| 1985 | | Effect.as({ |
| 1986 | | exitCode: ChildProcessSpawner.ExitCode(1), |
| 1987 | | stdout: "", |
| 1988 | | stderr: "", |
| 1989 | | stdoutTruncated: false, |
| 1990 | | stderrTruncated: false, |
| 1991 | | } satisfies GitVcsDriver.ExecuteGitResult), |
| 1992 | | ), |
| 1993 | | ), |
| 2044 | Effect.catchTags({ |
| 2045 | GitCommandError: (error) => |
| 2046 | Effect.logWarning( |
| 2047 | "Git remote ref lookup failed; falling back to an empty remote ref list.", |
| 2048 | { |
| 2049 | operation: error.operation, |
| 2050 | command: error.command, |
| 2051 | cwd: error.cwd, |
| 2052 | detail: error.detail, |
| 2053 | cause: error, |
| 2054 | }, |
| 2055 | ).pipe( |
| 2056 | Effect.as({ |
| 2057 | exitCode: ChildProcessSpawner.ExitCode(1), |
| 2058 | stdout: "", |
| 2059 | stderr: "", |
| 2060 | stdoutTruncated: false, |
| 2061 | stderrTruncated: false, |
| 2062 | } satisfies GitVcsDriver.ExecuteGitResult), |
| 2063 | ), |
| 2064 | }), |
| 1994 | 2065 | ); |
| 1995 | 2066 | |
| 1996 | 2067 | const remoteNamesResultEffect = executeGit( |
| ... | ... | @@ -2002,19 +2073,27 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* |
| 2002 | 2073 | allowNonZeroExit: true, |
| 2003 | 2074 | }, |
| 2004 | 2075 | ).pipe( |
| 2005 | | Effect.catch((error) => |
| 2006 | | Effect.logWarning( |
| 2007 | | `GitVcsDriver.listRefs: remote name lookup failed for ${input.cwd}: ${error.message}. Falling back to an empty remote name list.`, |
| 2008 | | ).pipe( |
| 2009 | | Effect.as({ |
| 2010 | | exitCode: ChildProcessSpawner.ExitCode(1), |
| 2011 | | stdout: "", |
| 2012 | | stderr: "", |
| 2013 | | stdoutTruncated: false, |
| 2014 | | stderrTruncated: false, |
| 2015 | | } satisfies GitVcsDriver.ExecuteGitResult), |
| 2016 | | ), |
| 2017 | | ), |
| 2076 | Effect.catchTags({ |
| 2077 | GitCommandError: (error) => |
| 2078 | Effect.logWarning( |
| 2079 | "Git remote name lookup failed; falling back to an empty remote name list.", |
| 2080 | { |
| 2081 | operation: error.operation, |
| 2082 | command: error.command, |
| 2083 | cwd: error.cwd, |
| 2084 | detail: error.detail, |
| 2085 | cause: error, |
| 2086 | }, |
| 2087 | ).pipe( |
| 2088 | Effect.as({ |
| 2089 | exitCode: ChildProcessSpawner.ExitCode(1), |
| 2090 | stdout: "", |
| 2091 | stderr: "", |
| 2092 | stdoutTruncated: false, |
| 2093 | stderrTruncated: false, |
| 2094 | } satisfies GitVcsDriver.ExecuteGitResult), |
| 2095 | ), |
| 2096 | }), |
| 2018 | 2097 | ); |
| 2019 | 2098 | |
| 2020 | 2099 | const [defaultRef, worktreeList, remoteBranchResult, remoteNamesResult, branchLastCommit] = |
| ... | ... | @@ -2175,7 +2254,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* |
| 2175 | 2254 | : ["worktree", "add", worktreePath, input.refName]; |
| 2176 | 2255 | |
| 2177 | 2256 | yield* executeGit("GitVcsDriver.createWorktree", input.cwd, args, { |
| 2178 | | fallbackErrorMessage: "git worktree add failed", |
| 2257 | fallbackErrorDetail: "git worktree add failed", |
| 2179 | 2258 | }); |
| 2180 | 2259 | |
| 2181 | 2260 | if (input.newRefName && input.baseRefName) { |
| ... | ... | @@ -2214,7 +2293,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* |
| 2214 | 2293 | `+refs/pull/${input.prNumber}/head:refs/heads/${input.branch}`, |
| 2215 | 2294 | ], |
| 2216 | 2295 | { |
| 2217 | | fallbackErrorMessage: "git fetch pull request branch failed", |
| 2296 | fallbackErrorDetail: "git fetch pull request branch failed", |
| 2218 | 2297 | }, |
| 2219 | 2298 | ); |
| 2220 | 2299 | }); |
| ... | ... | @@ -2227,7 +2306,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* |
| 2227 | 2306 | ["fetch", "--quiet", input.remoteName], |
| 2228 | 2307 | { |
| 2229 | 2308 | env: STATUS_UPSTREAM_REFRESH_ENV, |
| 2230 | | fallbackErrorMessage: `git fetch ${input.remoteName} failed`, |
| 2309 | fallbackErrorDetail: `git fetch ${input.remoteName} failed`, |
| 2231 | 2310 | }, |
| 2232 | 2311 | ); |
| 2233 | 2312 | }, |
| ... | ... | @@ -2302,18 +2381,8 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* |
| 2302 | 2381 | args.push(input.path); |
| 2303 | 2382 | yield* executeGit("GitVcsDriver.removeWorktree", input.cwd, args, { |
| 2304 | 2383 | timeoutMs: 15_000, |
| 2305 | | fallbackErrorMessage: "git worktree remove failed", |
| 2306 | | }).pipe( |
| 2307 | | Effect.mapError((error) => |
| 2308 | | createGitCommandError( |
| 2309 | | "GitVcsDriver.removeWorktree", |
| 2310 | | input.cwd, |
| 2311 | | args, |
| 2312 | | `${commandLabel(args)} failed (cwd: ${input.cwd}): ${error.message}`, |
| 2313 | | error, |
| 2314 | | ), |
| 2315 | | ), |
| 2316 | | ); |
| 2384 | fallbackErrorDetail: "git worktree remove failed", |
| 2385 | }); |
| 2317 | 2386 | }); |
| 2318 | 2387 | |
| 2319 | 2388 | const renameBranch: GitVcsDriver.GitVcsDriver["Service"]["renameBranch"] = Effect.fn( |
| ... | ... | @@ -2330,7 +2399,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* |
| 2330 | 2399 | ["branch", "-m", "--", input.oldBranch, targetBranch], |
| 2331 | 2400 | { |
| 2332 | 2401 | timeoutMs: 10_000, |
| 2333 | | fallbackErrorMessage: "git branch rename failed", |
| 2402 | fallbackErrorDetail: "git branch rename failed", |
| 2334 | 2403 | }, |
| 2335 | 2404 | ); |
| 2336 | 2405 | |
| ... | ... | @@ -2407,7 +2476,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* |
| 2407 | 2476 | |
| 2408 | 2477 | yield* executeGit("GitVcsDriver.switchRef.checkout", input.cwd, checkoutArgs, { |
| 2409 | 2478 | timeoutMs: 10_000, |
| 2410 | | fallbackErrorMessage: "git checkout failed", |
| 2479 | fallbackErrorDetail: "git checkout failed", |
| 2411 | 2480 | }); |
| 2412 | 2481 | |
| 2413 | 2482 | const refName = yield* runGitStdout("GitVcsDriver.switchRef.currentBranch", input.cwd, [ |
| ... | ... | @@ -2423,7 +2492,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* |
| 2423 | 2492 | function* (input) { |
| 2424 | 2493 | yield* executeGit("GitVcsDriver.createRef", input.cwd, ["branch", input.refName], { |
| 2425 | 2494 | timeoutMs: 10_000, |
| 2426 | | fallbackErrorMessage: "git branch create failed", |
| 2495 | fallbackErrorDetail: "git branch create failed", |
| 2427 | 2496 | }); |
| 2428 | 2497 | if (input.switchRef) { |
| 2429 | 2498 | yield* switchRef({ cwd: input.cwd, refName: input.refName }); |
| ... | ... | @@ -2436,7 +2505,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* |
| 2436 | 2505 | const initRepo: GitVcsDriver.GitVcsDriver["Service"]["initRepo"] = (input) => |
| 2437 | 2506 | executeGit("GitVcsDriver.initRepo", input.cwd, ["init"], { |
| 2438 | 2507 | timeoutMs: 10_000, |
| 2439 | | fallbackErrorMessage: "git init failed", |
| 2508 | fallbackErrorDetail: "git init failed", |
| 2440 | 2509 | }).pipe(Effect.asVoid); |
| 2441 | 2510 | |
| 2442 | 2511 | const listLocalBranchNames: GitVcsDriver.GitVcsDriver["Service"]["listLocalBranchNames"] = ( |