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
3 changes: 2 additions & 1 deletion apps/desktop/src/components/NewCommitView.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
const laneState = $derived(uiState.lane(stackId || 'new-commit-view--new-stack'));

const [createCommitInStack, commitCreation] = stackService.createCommit();
const [runMessageHook] = hooksService.message;

let isCooking = $state(false);

Expand Down Expand Up @@ -87,7 +88,7 @@
// Run commit-msg hook if hooks are enabled
let finalMessage = message;
if ($runCommitHooks) {
const messageHookResult = await hooksService.message(projectId, message);
const messageHookResult = await runMessageHook({ projectId, message });
if (messageHookResult?.status === 'failure') {
showError('Commit message hook failed', messageHookResult.error);
return;
Expand Down
2 changes: 1 addition & 1 deletion apps/desktop/src/lib/bootstrap/deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ export function initDependencies(args: {
const baseBranchService = new BaseBranchService(clientState.backendApi);
const branchService = new BranchService(clientState['backendApi']);
const remotesService = new RemotesService(backend);
const hooksService = new HooksService(backend);
const hooksService = new HooksService(clientState.backendApi);

// ============================================================================
// STACKS & WORKSPACE MANAGEMENT
Expand Down
8 changes: 7 additions & 1 deletion apps/desktop/src/lib/commits/dropHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,13 @@ export class AmendCommitWithHunkDzHandler implements DropzoneHandler {
commitId: commit.id,
worktreeChanges
});
await this.args.hooksService.runPostCommitHooks(projectId);
if (runHooks) {
try {
await this.args.hooksService.runPostCommitHooks(projectId);
} catch {
return;
}
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion apps/desktop/src/lib/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export function projectAiExperimentalFeaturesEnabled(projectId: string): Persist

export function projectRunCommitHooks(projectId: string): Persisted<boolean> {
const key = 'projectRunCommitHooks_';
return persisted(false, key + projectId);
return persisted(true, key + projectId);
}

export function persistedChatModelName<T extends string>(
Expand Down
61 changes: 39 additions & 22 deletions apps/desktop/src/lib/hooks/hooksService.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { InjectionToken } from '@gitbutler/core/context';
import { chipToasts } from '@gitbutler/ui';
import type { IBackend } from '$lib/backend';
import type { DiffSpec } from '$lib/hunks/hunk';
import type { BackendApi } from '$lib/state/clientState.svelte';

export type HookStatus =
| {
Expand Down Expand Up @@ -34,37 +34,29 @@ export type MessageHookStatus =
export const HOOKS_SERVICE = new InjectionToken<HooksService>('HooksService');

export class HooksService {
constructor(private backend: IBackend) {}
private api: ReturnType<typeof injectEndpoints>;

async preCommitDiffspecs(projectId: string, changes: DiffSpec[]) {
return await this.backend.invoke<HookStatus>('pre_commit_hook_diffspecs', {
projectId,
changes
});
constructor(backendApi: BackendApi) {
this.api = injectEndpoints(backendApi);
}

async postCommit(projectId: string) {
return await this.backend.invoke<HookStatus>('post_commit_hook', {
projectId
});
}

async message(projectId: string, message: string) {
return await this.backend.invoke<MessageHookStatus>('message_hook', {
projectId,
message
});
get message() {
return this.api.endpoints.message.useMutation();
}

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

try {
const result = await this.preCommitDiffspecs(projectId, changes);
const result = await this.api.endpoints.preCommitDiffspecs.mutate({
projectId,
changes
});

if (result?.status === 'failure') {
chipToasts.removeChipToast(loadingToastId);
throw new Error(result.error);
throw new Error(formatError(result.error));
}

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

try {
const result = await this.postCommit(projectId);
const result = await this.api.endpoints.postCommit.mutate({
projectId
});

if (result?.status === 'failure') {
chipToasts.removeChipToast(loadingToastId);
throw new Error(result.error);
throw new Error(formatError(result.error));
}

chipToasts.removeChipToast(loadingToastId);
Expand All @@ -94,3 +88,26 @@ export class HooksService {
}
}
}

function formatError(error: string): string {
return `${error}\n\nIf you don't want git hooks to be run, you can disable them in the project settings.`;
}

function injectEndpoints(backendApi: BackendApi) {
return backendApi.injectEndpoints({
endpoints: (build) => ({
preCommitDiffspecs: build.mutation<HookStatus, { projectId: string; changes: DiffSpec[] }>({
extraOptions: { command: 'pre_commit_hook_diffspecs' },
query: (args) => args
}),
postCommit: build.mutation<HookStatus, { projectId: string }>({
extraOptions: { command: 'post_commit_hook' },
query: (args) => args
}),
message: build.mutation<MessageHookStatus, { projectId: string; message: string }>({
extraOptions: { command: 'message_hook' },
query: (args) => args
})
})
});
}
Loading