|
| 1 | +import { invoke as tauriInvoke } from "@/platform/tauri-core"; |
| 2 | +import { emitGitChanged } from "../events/git-events"; |
| 3 | +import { resolveRepositoryPathOrThrow } from "./git-repo-api"; |
| 4 | +import type { GitOperationState } from "../types/git.types"; |
| 5 | + |
| 6 | +type IntegrationOperation = "merge" | "rebase"; |
| 7 | + |
| 8 | +interface IntegrationPreflightResult { |
| 9 | + blockingPaths: string[]; |
| 10 | + blocksEntirely: boolean; |
| 11 | +} |
| 12 | + |
| 13 | +export type IntegrationOutcome = |
| 14 | + | { status: "clean" } |
| 15 | + | { status: "conflicts"; conflictedPaths: string[] } |
| 16 | + | { status: "stopped" } |
| 17 | + | { status: "blocked"; blockingPaths: string[]; blocksEntirely: boolean } |
| 18 | + | { status: "error"; message: string }; |
| 19 | + |
| 20 | +export interface OperationResolution { |
| 21 | + ok: boolean; |
| 22 | + message: string; |
| 23 | +} |
| 24 | + |
| 25 | +const errorMessage = (error: unknown): string => { |
| 26 | + const message = error instanceof Error ? error.message : String(error); |
| 27 | + return message.trim() || "Git operation failed"; |
| 28 | +}; |
| 29 | + |
| 30 | +const notifyOperationChanged = (repoPath: string, source: string) => { |
| 31 | + emitGitChanged({ |
| 32 | + repoPath, |
| 33 | + scopes: ["working-tree", "history", "refs"], |
| 34 | + source, |
| 35 | + }); |
| 36 | +}; |
| 37 | + |
| 38 | +export const getOperationState = async ( |
| 39 | + repoPath: string, |
| 40 | +): Promise<GitOperationState | null> => { |
| 41 | + const resolvedRepoPath = await resolveRepositoryPathOrThrow(repoPath); |
| 42 | + return tauriInvoke<GitOperationState | null>("git_operation_state", { |
| 43 | + repoPath: resolvedRepoPath, |
| 44 | + }); |
| 45 | +}; |
| 46 | + |
| 47 | +export const getConflictMarkerPaths = async (repoPath: string): Promise<string[]> => { |
| 48 | + const resolvedRepoPath = await resolveRepositoryPathOrThrow(repoPath); |
| 49 | + const result = await tauriInvoke<{ paths: string[] }>("git_conflict_markers", { |
| 50 | + repoPath: resolvedRepoPath, |
| 51 | + }); |
| 52 | + return result.paths; |
| 53 | +}; |
| 54 | + |
| 55 | +const runIntegration = async ( |
| 56 | + repoPath: string, |
| 57 | + branchName: string, |
| 58 | + operation: IntegrationOperation, |
| 59 | +): Promise<IntegrationOutcome> => { |
| 60 | + const command = operation === "merge" ? "git_merge" : "git_rebase"; |
| 61 | + try { |
| 62 | + await tauriInvoke(command, { repoPath, branchName }); |
| 63 | + notifyOperationChanged(repoPath, `${operation}-completed`); |
| 64 | + return { status: "clean" }; |
| 65 | + } catch (error) { |
| 66 | + // A conflict stop exits non-zero like a real failure; the authoritative |
| 67 | + // distinction is whether Git left an operation state behind. |
| 68 | + notifyOperationChanged(repoPath, `${operation}-rejected`); |
| 69 | + try { |
| 70 | + const state = await getOperationState(repoPath); |
| 71 | + if (state?.kind === operation) { |
| 72 | + if (state.conflictedPaths.length > 0) { |
| 73 | + return { status: "conflicts", conflictedPaths: state.conflictedPaths }; |
| 74 | + } |
| 75 | + return { status: "stopped" }; |
| 76 | + } |
| 77 | + } catch (stateError) { |
| 78 | + console.error(`Failed to read ${operation} state after Git rejected the operation:`, stateError); |
| 79 | + } |
| 80 | + return { status: "error", message: errorMessage(error) }; |
| 81 | + } |
| 82 | +}; |
| 83 | + |
| 84 | +const startIntegration = async ( |
| 85 | + repoPath: string, |
| 86 | + branchName: string, |
| 87 | + operation: IntegrationOperation, |
| 88 | +): Promise<IntegrationOutcome> => { |
| 89 | + const resolvedRepoPath = await resolveRepositoryPathOrThrow(repoPath); |
| 90 | + |
| 91 | + let preflight: IntegrationPreflightResult | null = null; |
| 92 | + try { |
| 93 | + preflight = await tauriInvoke<IntegrationPreflightResult>( |
| 94 | + "git_integration_preflight", |
| 95 | + { repoPath: resolvedRepoPath, branchName, operation }, |
| 96 | + ); |
| 97 | + } catch { |
| 98 | + // A failed preflight must not block the operation itself; Git will still |
| 99 | + // refuse safely when the tree is dirty. |
| 100 | + } |
| 101 | + |
| 102 | + if (preflight && preflight.blockingPaths.length > 0) { |
| 103 | + return { |
| 104 | + status: "blocked", |
| 105 | + blockingPaths: preflight.blockingPaths, |
| 106 | + blocksEntirely: preflight.blocksEntirely, |
| 107 | + }; |
| 108 | + } |
| 109 | + |
| 110 | + return runIntegration(resolvedRepoPath, branchName, operation); |
| 111 | +}; |
| 112 | + |
| 113 | +export const mergeBranch = (repoPath: string, branchName: string) => |
| 114 | + startIntegration(repoPath, branchName, "merge"); |
| 115 | + |
| 116 | +export const rebaseOntoBranch = (repoPath: string, branchName: string) => |
| 117 | + startIntegration(repoPath, branchName, "rebase"); |
| 118 | + |
| 119 | +const resolveOperation = async ( |
| 120 | + repoPath: string, |
| 121 | + action: "git_operation_continue" | "git_operation_abort" | "git_operation_skip", |
| 122 | +): Promise<OperationResolution> => { |
| 123 | + const resolvedRepoPath = await resolveRepositoryPathOrThrow(repoPath); |
| 124 | + try { |
| 125 | + await tauriInvoke(action, { repoPath: resolvedRepoPath }); |
| 126 | + notifyOperationChanged(resolvedRepoPath, "operation-resolved"); |
| 127 | + return { ok: true, message: "Git operation finished" }; |
| 128 | + } catch (error) { |
| 129 | + // Even a rejected continue can change repository state, so refresh anyway. |
| 130 | + notifyOperationChanged(resolvedRepoPath, "operation-resolution-rejected"); |
| 131 | + return { ok: false, message: errorMessage(error) }; |
| 132 | + } |
| 133 | +}; |
| 134 | + |
| 135 | +export const continueOperation = (repoPath: string) => |
| 136 | + resolveOperation(repoPath, "git_operation_continue"); |
| 137 | + |
| 138 | +export const abortOperation = (repoPath: string) => |
| 139 | + resolveOperation(repoPath, "git_operation_abort"); |
| 140 | + |
| 141 | +export const skipOperationStep = (repoPath: string) => |
| 142 | + resolveOperation(repoPath, "git_operation_skip"); |
0 commit comments