Skip to content

Commit d5e98c9

Browse files
Merge pull request #10526 from gitbutlerapp/hook-errors
Tests around commit hooks
2 parents 4d45354 + 60c901f commit d5e98c9

File tree

6 files changed

+467
-2
lines changed

6 files changed

+467
-2
lines changed

apps/desktop/src/components/NewCommitView.svelte

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,18 @@
8484
throw new Error('No branch selected!');
8585
}
8686
87+
// Run commit-msg hook if hooks are enabled
88+
let finalMessage = message;
89+
if ($runCommitHooks) {
90+
const messageHookResult = await hooksService.message(projectId, message);
91+
if (messageHookResult?.status === 'failure') {
92+
showError('Commit message hook failed', messageHookResult.error);
93+
return;
94+
} else if (messageHookResult?.status === 'message') {
95+
finalMessage = messageHookResult.message;
96+
}
97+
}
98+
8799
const worktreeChanges = await uncommittedService.worktreeChanges(projectId, stackId);
88100
89101
// Get current editor mode from the component instance
@@ -94,7 +106,7 @@
94106
projectId,
95107
stackId: finalStackId,
96108
selectedBranchName: finalBranchName,
97-
message,
109+
message: finalMessage,
98110
parentId,
99111
isRichTextMode
100112
});
@@ -108,7 +120,7 @@
108120
projectId,
109121
parentId,
110122
stackId: finalStackId,
111-
message: message,
123+
message: finalMessage,
112124
stackBranchName: finalBranchName,
113125
worktreeChanges
114126
},
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/bash
2+
# Create a file with allowed content
3+
4+
pushd local-with-hooks
5+
echo "This is allowed content" > allowed.txt
6+
popd
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/bash
2+
# Create a file with forbidden content
3+
4+
pushd local-with-hooks
5+
echo "This contains FORBIDDEN content" > forbidden.txt
6+
popd
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/bash
2+
# Create a marker file to trigger post-commit failure
3+
4+
pushd local-with-hooks
5+
touch FAIL_POST_COMMIT
6+
popd
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#!/bin/bash
2+
3+
echo "GIT CONFIG $GIT_CONFIG_GLOBAL"
4+
echo "DATA DIR $GITBUTLER_CLI_DATA_DIR"
5+
echo "BUT_TESTING $BUT_TESTING"
6+
7+
# Setup a remote project
8+
mkdir remote-with-hooks
9+
pushd remote-with-hooks
10+
git init -b master --object-format=sha1
11+
echo "Initial content" >> initial_file.txt
12+
git add initial_file.txt
13+
git commit -am "Initial commit"
14+
popd
15+
16+
# Clone the remote into a folder
17+
git clone remote-with-hooks local-with-hooks
18+
pushd local-with-hooks
19+
git checkout master
20+
21+
# Create a commit-msg hook that modifies the message
22+
mkdir -p .git/hooks
23+
cat > .git/hooks/commit-msg << 'HOOK_EOF'
24+
#!/bin/sh
25+
# This hook modifies commit messages by adding a prefix
26+
MESSAGE_FILE="$1"
27+
ORIGINAL_MESSAGE=$(cat "$MESSAGE_FILE")
28+
29+
# If message contains "REJECT", reject it
30+
if echo "$ORIGINAL_MESSAGE" | grep -q "REJECT"; then
31+
echo "Error: Commit message contains forbidden word REJECT"
32+
exit 1
33+
fi
34+
35+
# If message contains "MODIFY", add a prefix
36+
if echo "$ORIGINAL_MESSAGE" | grep -q "MODIFY"; then
37+
echo "[MODIFIED] $ORIGINAL_MESSAGE" > "$MESSAGE_FILE"
38+
fi
39+
HOOK_EOF
40+
41+
chmod +x .git/hooks/commit-msg
42+
43+
# Create a pre-commit hook that checks for forbidden content
44+
cat > .git/hooks/pre-commit << 'HOOK_EOF'
45+
#!/bin/sh
46+
# This hook checks staged files for forbidden content
47+
48+
# Get the list of staged files
49+
STAGED_FILES=$(git diff --cached --name-only)
50+
51+
# Check each staged file for FORBIDDEN content
52+
for file in $STAGED_FILES; do
53+
if [ -f "$file" ]; then
54+
if grep -q "FORBIDDEN" "$file"; then
55+
echo "Error: File $file contains FORBIDDEN content"
56+
exit 1
57+
fi
58+
fi
59+
done
60+
61+
exit 0
62+
HOOK_EOF
63+
64+
chmod +x .git/hooks/pre-commit
65+
66+
# Create a post-commit hook that can be triggered to fail
67+
cat > .git/hooks/post-commit << 'HOOK_EOF'
68+
#!/bin/sh
69+
# This hook checks if a marker file exists to trigger failure
70+
71+
if [ -f "FAIL_POST_COMMIT" ]; then
72+
echo "Error: Post-commit hook failed due to FAIL_POST_COMMIT marker"
73+
exit 1
74+
fi
75+
76+
exit 0
77+
HOOK_EOF
78+
79+
chmod +x .git/hooks/post-commit
80+
81+
# Add some uncommitted changes for testing
82+
echo "Uncommitted changes" >> uncommitted.txt
83+
84+
# Add the project to GitButler
85+
$BUT_TESTING add-project --switch-to-workspace "$(git rev-parse --symbolic-full-name @{u})"
86+
popd

0 commit comments

Comments
 (0)