Skip to content

Commit 04695b1

Browse files
Merge pull request #10545 from gitbutlerapp/rewrite-hooks-service-to-use-rtkq
rewrite-hooks-service-to-use-rtkq
2 parents 6000e4f + 6e020f1 commit 04695b1

File tree

4 files changed

+49
-25
lines changed

4 files changed

+49
-25
lines changed

apps/desktop/src/components/NewCommitView.svelte

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
const laneState = $derived(uiState.lane(stackId || 'new-commit-view--new-stack'));
3333
3434
const [createCommitInStack, commitCreation] = stackService.createCommit();
35+
const [runMessageHook] = hooksService.message;
3536
3637
let isCooking = $state(false);
3738
@@ -87,7 +88,7 @@
8788
// Run commit-msg hook if hooks are enabled
8889
let finalMessage = message;
8990
if ($runCommitHooks) {
90-
const messageHookResult = await hooksService.message(projectId, message);
91+
const messageHookResult = await runMessageHook({ projectId, message });
9192
if (messageHookResult?.status === 'failure') {
9293
showError('Commit message hook failed', messageHookResult.error);
9394
return;

apps/desktop/src/lib/bootstrap/deps.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ export function initDependencies(args: {
187187
const branchService = new BranchService(clientState['backendApi']);
188188
const cherryApplyService = new CherryApplyService(clientState.backendApi);
189189
const remotesService = new RemotesService(backend);
190-
const hooksService = new HooksService(backend);
190+
const hooksService = new HooksService(clientState.backendApi);
191191

192192
// ============================================================================
193193
// STACKS & WORKSPACE MANAGEMENT

apps/desktop/src/lib/commits/dropHandler.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,13 @@ export class AmendCommitWithHunkDzHandler implements DropzoneHandler {
339339
commitId: commit.id,
340340
worktreeChanges
341341
});
342-
await this.args.hooksService.runPostCommitHooks(projectId);
342+
if (runHooks) {
343+
try {
344+
await this.args.hooksService.runPostCommitHooks(projectId);
345+
} catch {
346+
return;
347+
}
348+
}
343349
}
344350
}
345351
}

apps/desktop/src/lib/hooks/hooksService.ts

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { InjectionToken } from '@gitbutler/core/context';
22
import { chipToasts } from '@gitbutler/ui';
3-
import type { IBackend } from '$lib/backend';
43
import type { DiffSpec } from '$lib/hunks/hunk';
4+
import type { BackendApi } from '$lib/state/clientState.svelte';
55

66
export type HookStatus =
77
| {
@@ -34,37 +34,29 @@ export type MessageHookStatus =
3434
export const HOOKS_SERVICE = new InjectionToken<HooksService>('HooksService');
3535

3636
export class HooksService {
37-
constructor(private backend: IBackend) {}
37+
private api: ReturnType<typeof injectEndpoints>;
3838

39-
async preCommitDiffspecs(projectId: string, changes: DiffSpec[]) {
40-
return await this.backend.invoke<HookStatus>('pre_commit_hook_diffspecs', {
41-
projectId,
42-
changes
43-
});
39+
constructor(backendApi: BackendApi) {
40+
this.api = injectEndpoints(backendApi);
4441
}
4542

46-
async postCommit(projectId: string) {
47-
return await this.backend.invoke<HookStatus>('post_commit_hook', {
48-
projectId
49-
});
50-
}
51-
52-
async message(projectId: string, message: string) {
53-
return await this.backend.invoke<MessageHookStatus>('message_hook', {
54-
projectId,
55-
message
56-
});
43+
get message() {
44+
return this.api.endpoints.message.useMutation();
5745
}
5846

47+
// Promise-based wrapper methods with toast handling
5948
async runPreCommitHooks(projectId: string, changes: DiffSpec[]): Promise<void> {
6049
const loadingToastId = chipToasts.loading('Started pre-commit hooks');
6150

6251
try {
63-
const result = await this.preCommitDiffspecs(projectId, changes);
52+
const result = await this.api.endpoints.preCommitDiffspecs.mutate({
53+
projectId,
54+
changes
55+
});
6456

6557
if (result?.status === 'failure') {
6658
chipToasts.removeChipToast(loadingToastId);
67-
throw new Error(result.error);
59+
throw new Error(formatError(result.error));
6860
}
6961

7062
chipToasts.removeChipToast(loadingToastId);
@@ -79,11 +71,13 @@ export class HooksService {
7971
const loadingToastId = chipToasts.loading('Started post-commit hooks');
8072

8173
try {
82-
const result = await this.postCommit(projectId);
74+
const result = await this.api.endpoints.postCommit.mutate({
75+
projectId
76+
});
8377

8478
if (result?.status === 'failure') {
8579
chipToasts.removeChipToast(loadingToastId);
86-
throw new Error(result.error);
80+
throw new Error(formatError(result.error));
8781
}
8882

8983
chipToasts.removeChipToast(loadingToastId);
@@ -94,3 +88,26 @@ export class HooksService {
9488
}
9589
}
9690
}
91+
92+
function formatError(error: string): string {
93+
return `${error}\n\nIf you don't want git hooks to be run, you can disable them in the project settings.`;
94+
}
95+
96+
function injectEndpoints(backendApi: BackendApi) {
97+
return backendApi.injectEndpoints({
98+
endpoints: (build) => ({
99+
preCommitDiffspecs: build.mutation<HookStatus, { projectId: string; changes: DiffSpec[] }>({
100+
extraOptions: { command: 'pre_commit_hook_diffspecs' },
101+
query: (args) => args
102+
}),
103+
postCommit: build.mutation<HookStatus, { projectId: string }>({
104+
extraOptions: { command: 'post_commit_hook' },
105+
query: (args) => args
106+
}),
107+
message: build.mutation<MessageHookStatus, { projectId: string; message: string }>({
108+
extraOptions: { command: 'message_hook' },
109+
query: (args) => args
110+
})
111+
})
112+
});
113+
}

0 commit comments

Comments
 (0)