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} />