Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions apps/desktop/src/components/commit/CommitView.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import { isLocalAndRemoteCommit } from "$components/lib";
import Drawer from "$components/shared/Drawer.svelte";
import { splitMessage } from "$lib/commits/commitMessage";
import { applyEditDraftUpdate, resolveEditDraft } from "$lib/commits/editCommitDraft";
import { rewrapCommitMessage } from "$lib/config/uiFeatureFlags";
import { commitUrl, FORGE_INFO_SERVICE } from "$lib/forge/forgeInfo.svelte";
import { MODE_SERVICE } from "$lib/mode/modeService";
Expand Down Expand Up @@ -81,6 +82,20 @@

const parsedMessage = $derived(splitMessage(commit.message));

const originalMessage = $derived({
title: parsedMessage?.title || "",
description: parsedMessage?.description || "",
});
const editorSeed = $derived(
resolveEditDraft(laneState.editCommitMessage.current, commit.id, originalMessage),
);

function persistDraft(update: { title?: string; description?: string }) {
laneState.editCommitMessage.set(
applyEditDraftUpdate(laneState.editCommitMessage.current, commit.id, originalMessage, update),
);
}

function combineParts(title?: string, description?: string): string {
if (!title) {
return "";
Expand Down Expand Up @@ -112,6 +127,7 @@
if (stackId) {
uiState.lane(stackId).selection.set({ branchName, commitId: newCommitId, previewOpen: true });
}
laneState.editCommitMessage.set(undefined);
setMode("view");
}

Expand Down Expand Up @@ -140,6 +156,11 @@
function cancelEdit() {
setMode("view");
}

function discardEdit() {
laneState.editCommitMessage.set(undefined);
setMode("view");
}
</script>

<Drawer
Expand Down Expand Up @@ -231,12 +252,13 @@
{stackId}
action={({ title, description }) => saveCommitMessage(title, description)}
actionLabel="Save changes"
onCancel={cancelEdit}
onChange={persistDraft}
onCancel={discardEdit}
floatingBoxHeader="Reword commit"
loading={messageUpdateQuery.current.isLoading}
existingCommitId={commit.id}
title={parsedMessage?.title || ""}
description={parsedMessage?.description || ""}
title={editorSeed.title}
description={editorSeed.description}
/>
</div>
{:else}
Expand Down
60 changes: 60 additions & 0 deletions apps/desktop/src/lib/commits/editCommitDraft.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { applyEditDraftUpdate, resolveEditDraft } from "$lib/commits/editCommitDraft";
import { describe, expect, test } from "vitest";

const original = { title: "Original title", description: "Original body" };

describe("resolveEditDraft", () => {
test("uses the original message when there is no draft", () => {
expect(resolveEditDraft(undefined, "c1", original)).toEqual(original);
});

test("uses the original message when the draft is for another commit", () => {
const draft = { commitId: "c2", title: "Other", description: "Other body" };
expect(resolveEditDraft(draft, "c1", original)).toEqual(original);
});

test("uses the draft when it belongs to the commit", () => {
const draft = { commitId: "c1", title: "Draft title", description: "Draft body" };
expect(resolveEditDraft(draft, "c1", original)).toEqual({
title: "Draft title",
description: "Draft body",
});
});
});

describe("applyEditDraftUpdate", () => {
test("seeds from the original message on the first change", () => {
expect(applyEditDraftUpdate(undefined, "c1", original, { title: "New title" })).toEqual({
commitId: "c1",
title: "New title",
description: "Original body",
});
});

test("merges a description-only change, keeping the existing title", () => {
const draft = { commitId: "c1", title: "A", description: "B" };
expect(applyEditDraftUpdate(draft, "c1", original, { description: "B2" })).toEqual({
commitId: "c1",
title: "A",
description: "B2",
});
});

test("restarts from the original when the draft is for another commit", () => {
const draft = { commitId: "c2", title: "A", description: "B" };
expect(applyEditDraftUpdate(draft, "c1", original, { title: "New" })).toEqual({
commitId: "c1",
title: "New",
description: "Original body",
});
});

test("keeps an explicitly cleared (empty) field", () => {
const draft = { commitId: "c1", title: "A", description: "B" };
expect(applyEditDraftUpdate(draft, "c1", original, { title: "" })).toEqual({
commitId: "c1",
title: "",
description: "B",
});
});
});
44 changes: 44 additions & 0 deletions apps/desktop/src/lib/commits/editCommitDraft.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* In-progress reword draft for a single commit. Persisted in lane UI state so it
* survives navigating away from the commit and back, instead of living only in
* the editor component's local state. See issue #13287.
*/
export type EditCommitMessage = {
commitId: string;
title: string;
description: string;
};

/**
* Picks the title/description to seed the reword editor with: the persisted
* draft when it belongs to `commitId`, otherwise the commit's own message.
*/
export function resolveEditDraft(
draft: EditCommitMessage | undefined,
commitId: string,
original: { title: string; description: string },
): { title: string; description: string } {
if (draft?.commitId === commitId) {
return { title: draft.title, description: draft.description };
}
return original;
}

/**
* Merges a partial editor change into the draft for `commitId`. The editor emits
* one field at a time, so the untouched field is carried over from the existing
* draft, or seeded from the original message when starting fresh.
*/
export function applyEditDraftUpdate(
draft: EditCommitMessage | undefined,
commitId: string,
original: { title: string; description: string },
update: { title?: string; description?: string },
): EditCommitMessage {
const base = draft?.commitId === commitId ? draft : { commitId, ...original };
return {
commitId,
title: update.title ?? base.title,
description: update.description ?? base.description,
};
}
3 changes: 3 additions & 0 deletions apps/desktop/src/lib/state/uiState.svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { InjectionToken } from "@gitbutler/core/context";
import { reactive } from "@gitbutler/shared/reactiveUtils.svelte";
import { type Reactive } from "@gitbutler/shared/storeUtils";
import { createEntityAdapter, createSlice, type EntityState } from "@reduxjs/toolkit";
import type { EditCommitMessage } from "$lib/commits/editCommitDraft";
import type { TerminalService } from "$lib/settings/terminalService";
import type { AppDispatch } from "$lib/state/clientState.svelte";
import type { ScrollbarVisilitySettings } from "@gitbutler/ui";
Expand Down Expand Up @@ -50,6 +51,7 @@ export type NewCommitMessage = {
export type StackState = {
selection: StackSelection | undefined;
newCommitMessage: NewCommitMessage;
editCommitMessage: EditCommitMessage | undefined;
};

export type ExclusiveAction =
Expand Down Expand Up @@ -217,6 +219,7 @@ export class UiState {
readonly lane = this.buildScopedProps<StackState>(this.scopesCache.lanes, {
selection: undefined,
newCommitMessage: { title: "", description: "" },
editCommitMessage: undefined,
});

/** Properties scoped to a specific project. */
Expand Down
Loading