|
| 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