Skip to content

Commit 2b06003

Browse files
committed
Start a commit behaviours
1 parent 5d7f792 commit 2b06003

File tree

3 files changed

+67
-21
lines changed

3 files changed

+67
-21
lines changed

apps/desktop/src/components/v3/FileList.svelte

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@
5555
active,
5656
stackId,
5757
conflictEntries,
58-
draggableFiles
59-
conflictEntries,
58+
draggableFiles,
6059
group
6160
}: Props = $props();
6261

apps/desktop/src/components/v3/WorktreeChanges.svelte

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,16 @@
1414
import { focusable } from '$lib/focus/focusable.svelte';
1515
import { DiffService } from '$lib/hunks/diffService.svelte';
1616
import { AssignmentDropHandler } from '$lib/hunks/dropHandler';
17-
import { ChangeSelectionService } from '$lib/selection/changeSelection.svelte';
17+
import {
18+
ChangeSelectionService,
19+
selectForStartingCommit
20+
} from '$lib/selection/changeSelection.svelte';
1821
import { IdSelection } from '$lib/selection/idSelection.svelte';
1922
import { StackService } from '$lib/stacks/stackService.svelte';
2023
import { UiState } from '$lib/state/uiState.svelte';
2124
import { TestId } from '$lib/testing/testIds';
2225
import { WorktreeService } from '$lib/worktree/worktreeService.svelte';
23-
import { getContext, inject } from '@gitbutler/shared/context';
26+
import { inject } from '@gitbutler/shared/context';
2427
import Badge from '@gitbutler/ui/Badge.svelte';
2528
import Button from '@gitbutler/ui/Button.svelte';
2629
import { stickyHeader } from '@gitbutler/ui/utils/stickyHeader';
@@ -35,13 +38,15 @@
3538
3639
let { projectId, stackId, active }: Props = $props();
3740
38-
const [changeSelection, worktreeService, uiState, stackService, idSelection] = inject(
39-
ChangeSelectionService,
40-
WorktreeService,
41-
UiState,
42-
StackService,
43-
IdSelection
44-
);
41+
const [changeSelection, worktreeService, uiState, stackService, idSelection, diffService] =
42+
inject(
43+
ChangeSelectionService,
44+
WorktreeService,
45+
UiState,
46+
StackService,
47+
IdSelection,
48+
DiffService
49+
);
4550
4651
const uncommitDzHandler = $derived(new UncommitDzHandler(projectId, stackService, uiState));
4752
@@ -58,6 +63,13 @@
5863
const changesResult = $derived(worktreeService.getChanges(projectId));
5964
const affectedPaths = $derived(changesResult.current.data?.map((c) => c.path));
6065
66+
const changesKeyResult = $derived(worktreeService.getChangesKey(projectId));
67+
const hunkAssignments = $derived(
68+
changesKeyResult.current
69+
? diffService.hunkAssignments(projectId, changesKeyResult.current)
70+
: undefined
71+
);
72+
6173
// TODO: Make this go away.
6274
createCommitStore(undefined);
6375
@@ -76,7 +88,16 @@
7688
let listMode: 'list' | 'tree' = $state('list');
7789
7890
function startCommit() {
79-
// TODO: Implement "Select the right stuff"
91+
if (changesResult.current?.data && hunkAssignments?.current?.data) {
92+
selectForStartingCommit(
93+
stackId,
94+
changesResult.current.data,
95+
hunkAssignments.current.data,
96+
changeSelection.list().current,
97+
changeSelection
98+
);
99+
}
100+
80101
projectState.drawerPage.set('new-commit');
81102
if (defaultBranchName) {
82103
stackState?.selection.set({ branchName: defaultBranchName });
@@ -85,15 +106,6 @@
85106
86107
let listHeaderHeight = $state(0);
87108
let listFooterHeight = $state(0);
88-
89-
const diffService = getContext(DiffService);
90-
91-
const changesKeyResult = $derived(worktreeService.getChangesKey(projectId));
92-
const hunkAssignments = $derived(
93-
changesKeyResult.current
94-
? diffService.hunkAssignments(projectId, changesKeyResult.current)
95-
: undefined
96-
);
97109
const assignmentDZHandler = $derived(
98110
hunkAssignments?.current?.data
99111
? new AssignmentDropHandler(projectId, diffService, hunkAssignments.current.data, {

apps/desktop/src/lib/selection/changeSelection.svelte.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,41 @@ function assignmentsHaveHunkInformation(
187187
return assignments.some((assignment) => isDefined(assignment.hunkHeader));
188188
}
189189

190+
/**
191+
* Intended behaviour:
192+
* - If the user has selected a given stack when they start commiting
193+
* - If the stack has assigned changes
194+
* - Select those
195+
* - Otherwise
196+
* - Select the uncommited changes
197+
*/
198+
export function selectForStartingCommit(
199+
stackId: string | undefined,
200+
changes: TreeChange[],
201+
assignments: HunkAssignments,
202+
currentlySelectedFiles: SelectedFile[],
203+
changeSelection: ChangeSelectionService
204+
) {
205+
if (currentlySelectedFiles.length > 0) return;
206+
207+
let group: HunkGroup;
208+
if (
209+
stackId &&
210+
changes.some(
211+
(change) =>
212+
getRelevantAssignments(change, { type: 'grouped', stackId }, assignments).length > 0
213+
)
214+
) {
215+
group = { type: 'grouped', stackId };
216+
} else {
217+
group = { type: 'ungrouped' };
218+
}
219+
220+
for (const change of changes) {
221+
selectAllForChangeInGroup(change, group, assignments, undefined, changeSelection);
222+
}
223+
}
224+
190225
export function selectAllForChangeInGroup(
191226
change: TreeChange,
192227
group: HunkGroup,

0 commit comments

Comments
 (0)