This commit is contained in:
Christoph Brandau
2026-06-27 14:11:51 +02:00
parent 6a78e768bc
commit ef1974f31f
7 changed files with 849 additions and 6 deletions
+451
View File
@@ -35,6 +35,8 @@
openRepository,
pull,
push,
readConflict,
resolveConflict,
restoreFileFromCommit,
restoreFiles,
restoreToCommit,
@@ -42,6 +44,7 @@
unstageFiles,
} from "./lib/git";
import type {
ConflictFile,
FileStatusKind,
GitBranch as GitBranchInfo,
GitCommit,
@@ -65,6 +68,12 @@
children: ExplorerNode[];
}
type ConflictChoice = "ours" | "theirs" | "both-ot" | "both-to";
type ConflictPart =
| { kind: "text"; lines: string[] }
| { kind: "conflict"; index: number; oursLines: string[]; theirsLines: string[] };
let repoPath = "";
let activeRepoPath = "";
let status: GitStatus | null = null;
@@ -83,6 +92,13 @@
let comparison: GitCommitComparison | null = null;
let compareDialogOpen = false;
let selectedDiffPath = "";
let resolveDialogOpen = false;
let conflictTarget = "";
let conflict: ConflictFile | null = null;
let resolveContent = "";
let conflictParts: ConflictPart[] = [];
let conflictChoices: (ConflictChoice | null)[] = [];
let manualMode = false;
$: isBusy = operation.length > 0;
$: hasRepository = activeRepoPath.length > 0 && status !== null;
@@ -96,6 +112,18 @@
compareTo.length > 0 &&
compareFrom !== compareTo &&
!isBusy;
$: conflictedFiles = changedFiles.filter(
(file) => file.staged === "conflicted" || file.unstaged === "conflicted",
);
$: hasConflicts = conflictedFiles.length > 0;
$: conflictRegionCount = conflictParts.filter((part) => part.kind === "conflict").length;
$: unresolvedCount = manualMode
? 0
: conflictChoices.filter((choice) => choice == null).length;
$: resolvedContent = manualMode
? resolveContent
: buildResolution(conflictParts, conflictChoices);
$: resolveHasMarkers = /^<{7}/m.test(resolvedContent) || /^>{7}/m.test(resolvedContent);
$: diffByPath = comparison ? buildDiffByPath(comparison.patch) : new Map<string, string>();
$: selectedDiffFile = comparison?.files.find((file) => file.path === selectedDiffPath) ?? null;
$: selectedDiffPatch = selectedDiffFile ? diffByPath.get(selectedDiffFile.path) ?? "" : "";
@@ -353,6 +381,13 @@
comparison = null;
compareDialogOpen = false;
selectedDiffPath = "";
resolveDialogOpen = false;
conflictTarget = "";
conflict = null;
resolveContent = "";
conflictParts = [];
conflictChoices = [];
manualMode = false;
await refreshBranchList(activeRepoPath);
await refreshCommitHistory(activeRepoPath);
await refreshExplorerFiles(activeRepoPath);
@@ -601,6 +636,215 @@
}
}
async function loadConflict(file: string) {
conflictTarget = file;
conflict = await readConflict(activeRepoPath, file);
conflictParts = parseConflicts(conflict.content);
conflictChoices = conflictParts.filter((part) => part.kind === "conflict").map(() => null);
manualMode = false;
resolveContent = conflict.content;
}
function parseConflicts(content: string): ConflictPart[] {
const lines = content.split("\n");
const parts: ConflictPart[] = [];
let textLines: string[] = [];
let regionIndex = 0;
let index = 0;
const flushText = () => {
if (textLines.length > 0) {
parts.push({ kind: "text", lines: textLines });
textLines = [];
}
};
while (index < lines.length) {
const line = lines[index];
if (line.startsWith("<<<<<<<")) {
flushText();
index += 1;
const oursLines: string[] = [];
while (
index < lines.length &&
!lines[index].startsWith("=======") &&
!lines[index].startsWith("|||||||")
) {
oursLines.push(lines[index]);
index += 1;
}
// Skip the diff3 base section (||||||| ... =======) if present.
if (index < lines.length && lines[index].startsWith("|||||||")) {
index += 1;
while (index < lines.length && !lines[index].startsWith("=======")) {
index += 1;
}
}
if (index < lines.length && lines[index].startsWith("=======")) {
index += 1;
}
const theirsLines: string[] = [];
while (index < lines.length && !lines[index].startsWith(">>>>>>>")) {
theirsLines.push(lines[index]);
index += 1;
}
if (index < lines.length && lines[index].startsWith(">>>>>>>")) {
index += 1;
}
parts.push({ kind: "conflict", index: regionIndex, oursLines, theirsLines });
regionIndex += 1;
} else {
textLines.push(line);
index += 1;
}
}
flushText();
return parts;
}
function buildResolution(parts: ConflictPart[], choices: (ConflictChoice | null)[]): string {
const out: string[] = [];
for (const part of parts) {
if (part.kind === "text") {
out.push(...part.lines);
continue;
}
const choice = choices[part.index];
if (choice === "ours") {
out.push(...part.oursLines);
} else if (choice === "theirs") {
out.push(...part.theirsLines);
} else if (choice === "both-ot") {
out.push(...part.oursLines, ...part.theirsLines);
} else if (choice === "both-to") {
out.push(...part.theirsLines, ...part.oursLines);
} else {
// Unresolved: keep the conflict markers so it stays clearly flagged.
out.push(
"<<<<<<< current",
...part.oursLines,
"=======",
...part.theirsLines,
">>>>>>> incoming",
);
}
}
return out.join("\n");
}
function setConflictChoice(index: number, choice: ConflictChoice) {
const next = [...conflictChoices];
next[index] = choice;
conflictChoices = next;
}
function setAllConflicts(choice: ConflictChoice) {
conflictChoices = conflictParts
.filter((part) => part.kind === "conflict")
.map(() => choice);
}
function enableManualEdit() {
resolveContent = buildResolution(conflictParts, conflictChoices);
manualMode = true;
}
function disableManualEdit() {
manualMode = false;
}
function displayLine(line: string): string {
// Strip a trailing CR for display only; the stored line keeps it so the
// rebuilt file preserves its original line endings.
return line.replace(/\r$/, "");
}
function oursActive(choice: ConflictChoice | null): boolean {
return choice === "ours" || choice === "both-ot" || choice === "both-to";
}
function theirsActive(choice: ConflictChoice | null): boolean {
return choice === "theirs" || choice === "both-ot" || choice === "both-to";
}
async function openResolveDialog() {
if (!hasConflicts || isBusy) {
return;
}
const first = conflictedFiles[0].path;
await runOperation("Loading conflicts", async () => {
resolveDialogOpen = true;
await loadConflict(first);
});
}
async function selectConflictFile(file: string) {
if (file === conflictTarget || isBusy) {
return;
}
await runOperation(`Loading ${file}`, async () => {
await loadConflict(file);
});
}
async function saveResolution() {
if (!activeRepoPath || !conflictTarget || isBusy) {
return;
}
if (
resolveHasMarkers &&
!window.confirm(
"Conflict markers (<<<<<<< / >>>>>>>) are still present. Mark this file as resolved anyway?",
)
) {
return;
}
const resolvedPath = conflictTarget;
const content = resolvedContent;
await runOperation(`Resolving ${resolvedPath}`, async () => {
const nextStatus = await resolveConflict(activeRepoPath, resolvedPath, content);
applyStatus(nextStatus);
await refreshExplorerFiles(activeRepoPath);
await refreshFileHistory(activeRepoPath);
const remaining = nextStatus.files.filter(
(file) => file.staged === "conflicted" || file.unstaged === "conflicted",
);
if (remaining.length === 0) {
resolveDialogOpen = false;
conflict = null;
conflictTarget = "";
resolveContent = "";
conflictParts = [];
conflictChoices = [];
manualMode = false;
} else {
await loadConflict(remaining[0].path);
}
});
}
function closeResolveDialog() {
resolveDialogOpen = false;
}
function closeCompareDialog() {
compareDialogOpen = false;
}
@@ -835,6 +1079,19 @@
</section>
{/if}
{#if hasConflicts}
<section class="notice conflict" role="alert">
<GitMerge size={17} aria-hidden="true" />
<span>
{conflictedFiles.length}
{conflictedFiles.length === 1 ? "file has" : "files have"} merge conflicts.
</span>
<button type="button" onclick={openResolveDialog} disabled={isBusy}>
Resolve conflicts
</button>
</section>
{/if}
<section class="workspace" aria-label="Git workspace">
<aside class="left-sidebar" aria-label="Repository navigation">
<section class="branches" aria-label="Branches">
@@ -1347,6 +1604,200 @@
</div>
</div>
{/if}
{#if resolveDialogOpen}
<div
class="dialog-backdrop"
role="presentation"
onclick={(event) => {
if (event.target === event.currentTarget) {
closeResolveDialog();
}
}}
>
<div
class="dialog"
role="dialog"
aria-modal="true"
aria-label="Resolve merge conflicts"
tabindex="-1"
>
<header class="dialog-header">
<div>
<span class="eyebrow">Resolve</span>
<h2 class="dialog-title">Merge conflicts</h2>
</div>
<button type="button" class="dialog-close" onclick={closeResolveDialog} title="Close">
<X size={18} aria-hidden="true" />
</button>
</header>
{#if conflictedFiles.length === 0}
<div class="blank-state">All conflicts resolved. You can commit the merge now.</div>
{:else}
<div class="dialog-body">
<aside class="dialog-files" aria-label="Conflicted files">
{#each conflictedFiles as file (file.path)}
<button
type="button"
class:active={conflictTarget === file.path}
class="dialog-file-row"
onclick={() => selectConflictFile(file.path)}
disabled={isBusy}
title={file.path}
>
<span class="status-badge conflicted">conflicted</span>
<strong>{file.path}</strong>
</button>
{/each}
</aside>
<div class="resolve-editor" aria-label="Conflict editor">
{#if !conflict}
<div class="blank-state">Select a file to resolve.</div>
{:else}
<div class="resolve-toolbar">
{#if manualMode}
<button type="button" onclick={disableManualEdit} disabled={isBusy}>
Back to guided
</button>
{:else}
<span class="resolve-toolbar-label">Apply to all:</span>
<button type="button" onclick={() => setAllConflicts("ours")} disabled={isBusy}>
Current
</button>
<button type="button" onclick={() => setAllConflicts("theirs")} disabled={isBusy}>
Incoming
</button>
<button type="button" onclick={() => setAllConflicts("both-ot")} disabled={isBusy}>
Both
</button>
<button type="button" onclick={enableManualEdit} disabled={isBusy}>
Edit manually
</button>
{/if}
<span class="resolve-status">
{#if unresolvedCount > 0}
<AlertCircle size={14} aria-hidden="true" />
{unresolvedCount} of {conflictRegionCount} unresolved
{:else if resolveHasMarkers}
<AlertCircle size={14} aria-hidden="true" />
Conflict markers still present
{:else}
<Check size={14} aria-hidden="true" />
{conflictRegionCount}
{conflictRegionCount === 1 ? "conflict" : "conflicts"} resolved
{/if}
</span>
</div>
{#if manualMode}
<textarea
class="resolve-textarea"
bind:value={resolveContent}
spellcheck="false"
disabled={isBusy}
></textarea>
{:else}
<div class="resolve-structured">
{#each conflictParts as part, partIndex (partIndex)}
{#if part.kind === "text"}
{#if part.lines.length > 0}
<pre class="resolve-context">{#each part.lines as line}<span
class="resolve-line context">{displayLine(line) || " "}</span>{/each}</pre>
{/if}
{:else}
<div
class="resolve-conflict"
class:unresolved={conflictChoices[part.index] == null}
>
<div class="resolve-conflict-bar">
<span class="resolve-conflict-label">Conflict {part.index + 1}</span>
<div class="resolve-choice-buttons">
<button
type="button"
class:active={conflictChoices[part.index] === "ours"}
onclick={() => setConflictChoice(part.index, "ours")}
disabled={isBusy}
>
Current
</button>
<button
type="button"
class:active={conflictChoices[part.index] === "theirs"}
onclick={() => setConflictChoice(part.index, "theirs")}
disabled={isBusy}
>
Incoming
</button>
<button
type="button"
class:active={conflictChoices[part.index] === "both-ot"}
onclick={() => setConflictChoice(part.index, "both-ot")}
disabled={isBusy}
title="Keep both current first"
>
Both C+I
</button>
<button
type="button"
class:active={conflictChoices[part.index] === "both-to"}
onclick={() => setConflictChoice(part.index, "both-to")}
disabled={isBusy}
title="Keep both incoming first"
>
Both I+C
</button>
</div>
</div>
<div
class="resolve-side ours"
class:dimmed={!oursActive(conflictChoices[part.index])}
>
<span class="resolve-side-label">Current (ours)</span>
<pre class="resolve-lines">{#each part.oursLines as line}<span
class="resolve-line ours">{displayLine(line) || " "}</span>{/each}</pre>
</div>
<div
class="resolve-side theirs"
class:dimmed={!theirsActive(conflictChoices[part.index])}
>
<span class="resolve-side-label">Incoming (theirs)</span>
<pre class="resolve-lines">{#each part.theirsLines as line}<span
class="resolve-line theirs">{displayLine(line) || " "}</span>{/each}</pre>
</div>
</div>
{/if}
{/each}
</div>
{/if}
<div class="resolve-actions">
<span class="resolve-path" title={conflictTarget}>{conflictTarget}</span>
<button
class="primary-button"
type="button"
onclick={saveResolution}
disabled={isBusy}
>
{#if operation.startsWith("Resolving")}
<LoaderCircle class="spin" size={16} aria-hidden="true" />
{:else}
<Check size={16} aria-hidden="true" />
{/if}
Save &amp; mark resolved
</button>
</div>
{/if}
</div>
</div>
{/if}
</div>
</div>
{/if}
</main>
<svelte:window on:keydown={handleWindowKeydown} />
+227
View File
@@ -161,6 +161,23 @@ textarea {
background: #edf8fd;
}
.notice.conflict {
border-color: #e3b778;
color: #8a4c0e;
background: #fff6e7;
}
.notice.conflict button {
margin-left: auto;
min-height: 30px;
padding: 0 12px;
border-color: #e3b778;
color: #8a4c0e;
background: #ffffff;
font-size: 13px;
font-weight: 700;
}
.workspace {
display: grid;
grid-template-columns: 340px minmax(0, 1fr);
@@ -937,6 +954,216 @@ textarea {
max-height: none;
}
.dialog-title {
margin: 2px 0 0;
color: #202326;
font-size: 16px;
}
.resolve-editor {
display: grid;
grid-template-rows: auto minmax(0, 1fr) auto;
min-height: 0;
padding: 10px;
gap: 8px;
}
.resolve-toolbar {
display: flex;
align-items: center;
flex-wrap: wrap;
gap: 8px;
}
.resolve-toolbar button {
min-height: 32px;
padding: 0 10px;
font-size: 13px;
}
.resolve-toolbar-label {
color: #596670;
font-size: 12px;
font-weight: 800;
}
.resolve-status {
display: inline-flex;
align-items: center;
gap: 5px;
margin-left: auto;
font-size: 12px;
font-weight: 700;
color: #8a4c0e;
}
.resolve-textarea {
width: 100%;
height: 100%;
min-height: 0;
padding: 10px;
border: 1px solid #c4ccd3;
border-radius: 6px;
background: #ffffff;
color: #202326;
font-family: "Cascadia Code", "SFMono-Regular", Consolas, monospace;
font-size: 12px;
line-height: 1.5;
tab-size: 2;
resize: none;
white-space: pre;
}
.resolve-structured {
display: grid;
align-content: start;
gap: 8px;
min-height: 0;
padding: 4px;
overflow: auto;
border: 1px solid #dce1e5;
border-radius: 6px;
background: #ffffff;
}
.resolve-context {
margin: 0;
padding: 2px 8px;
overflow-x: auto;
font-family: "Cascadia Code", "SFMono-Regular", Consolas, monospace;
font-size: 12px;
line-height: 1.5;
tab-size: 2;
color: #3a444c;
}
.resolve-conflict {
display: grid;
gap: 6px;
padding: 8px;
border: 1px solid #e3b778;
border-radius: 6px;
background: #fffaf1;
}
.resolve-conflict.unresolved {
border-color: #d98b8b;
background: #fff5f3;
}
.resolve-conflict-bar {
display: flex;
align-items: center;
justify-content: space-between;
flex-wrap: wrap;
gap: 8px;
}
.resolve-conflict-label {
color: #8a4c0e;
font-size: 12px;
font-weight: 800;
}
.resolve-choice-buttons {
display: flex;
flex-wrap: wrap;
gap: 4px;
}
.resolve-choice-buttons button {
min-height: 26px;
padding: 0 8px;
font-size: 12px;
}
.resolve-choice-buttons button.active {
border-color: #2f6fb0;
color: #ffffff;
background: #2f6fb0;
}
.resolve-side {
display: grid;
gap: 3px;
padding: 6px;
border: 1px solid #dce1e5;
border-radius: 6px;
border-left-width: 3px;
}
.resolve-side.ours {
border-left-color: #4aa777;
background: #f1faf4;
}
.resolve-side.theirs {
border-left-color: #5a8bd0;
background: #f1f5fc;
}
.resolve-side.dimmed {
opacity: 0.45;
filter: grayscale(0.4);
}
.resolve-side-label {
font-size: 11px;
font-weight: 800;
text-transform: uppercase;
}
.resolve-side.ours .resolve-side-label {
color: #1f7a4d;
}
.resolve-side.theirs .resolve-side-label {
color: #2f6fb0;
}
.resolve-lines {
margin: 0;
overflow-x: auto;
font-family: "Cascadia Code", "SFMono-Regular", Consolas, monospace;
font-size: 12px;
line-height: 1.5;
tab-size: 2;
}
.resolve-line {
display: block;
white-space: pre-wrap;
word-break: break-word;
}
.resolve-line.ours {
color: #176239;
}
.resolve-line.theirs {
color: #255b8b;
}
.resolve-line.context {
color: #3a444c;
}
.resolve-actions {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
}
.resolve-path {
overflow: hidden;
color: #6c7882;
font-family: "Cascadia Code", "SFMono-Regular", Consolas, monospace;
font-size: 12px;
text-overflow: ellipsis;
white-space: nowrap;
}
.diff-counts {
display: flex;
gap: 8px;
+13
View File
@@ -1,6 +1,7 @@
import { invoke } from "@tauri-apps/api/core";
import type {
ConflictFile,
GitBranch,
GitCommit,
GitCommitComparison,
@@ -95,3 +96,15 @@ export function diffFileAgainstWorkingTree(
): Promise<GitCommitComparison> {
return invoke<GitCommitComparison>("diff_file_against_working_tree", { path, commit, file });
}
export function readConflict(path: string, file: string): Promise<ConflictFile> {
return invoke<ConflictFile>("read_conflict", { path, file });
}
export function resolveConflict(
path: string,
file: string,
content: string,
): Promise<GitStatus> {
return invoke<GitStatus>("resolve_conflict", { path, file, content });
}
+8
View File
@@ -69,3 +69,11 @@ export interface GitCommitComparison {
files: GitDiffFile[];
patch: string;
}
export interface ConflictFile {
path: string;
content: string;
ours: string | null;
theirs: string | null;
base: string | null;
}