Automation with Hooks
Hooks let you run custom scripts at key points in the Dits workflow. Automate validation, testing, notifications, and more.
Powerful Automation
Hooks run locally and can execute any script or program. Use them to enforce policies, run tests, or integrate with external tools.
Available Hooks
Client-Side Hooks
Run on your local machine
- pre-commit - Before creating a commit
- prepare-commit-msg - Before commit message editor
- commit-msg - Validate commit message
- post-commit - After commit is created
- pre-push - Before pushing to remote
- post-checkout - After checkout/switch
- post-merge - After merge completes
Server-Side Hooks
Run on the remote server
- pre-receive - Before accepting push
- update - Per-branch before update
- post-receive - After push is accepted
- post-update - After refs are updated
Creating Hooks
Hooks are executable scripts in the .dits/hooks/ directory. The filename must match the hook name exactly (no extension).
# Create hooks directory
mkdir -p .dits/hooks
# Create a pre-commit hook
cat > .dits/hooks/pre-commit << 'EOF'
#!/bin/bash
echo "Running pre-commit checks..."
# Run linter
npm run lint
if [ $? -ne 0 ]; then
echo "Lint failed! Fix errors before committing."
exit 1
fi
# Run tests
npm run test
if [ $? -ne 0 ]; then
echo "Tests failed! Fix tests before committing."
exit 1
fi
echo "All checks passed!"
exit 0
EOF
# Make it executable
chmod +x .dits/hooks/pre-commitHook Examples
Pre-commit: Validate Large Files
#!/bin/bash
# Prevent committing files larger than 100MB without proxy
MAX_SIZE=$((100 * 1024 * 1024)) # 100MB in bytes
for file in $(dits diff --cached --name-only); do
if [ -f "$file" ]; then
size=$(stat -f%z "$file" 2>/dev/null || stat -c%s "$file")
if [ "$size" -gt "$MAX_SIZE" ]; then
echo "ERROR: $file is larger than 100MB"
echo "Consider using: dits proxy create $file"
exit 1
fi
fi
done
exit 0Commit-msg: Enforce Format
#!/bin/bash
# Enforce conventional commit format
commit_msg_file=$1
commit_msg=$(cat "$commit_msg_file")
# Pattern: type(scope): description
pattern="^(feat|fix|docs|style|refactor|test|chore)((.+))?: .{1,72}$"
if ! echo "$commit_msg" | head -1 | grep -qE "$pattern"; then
echo "ERROR: Invalid commit message format"
echo ""
echo "Expected format: type(scope): description"
echo "Types: feat, fix, docs, style, refactor, test, chore"
echo ""
echo "Example: feat(video): add timeline scrubbing"
exit 1
fi
exit 0Pre-push: Run Full Test Suite
#!/bin/bash
# Run comprehensive tests before pushing
echo "Running full test suite before push..."
# Run unit tests
npm run test:unit || exit 1
# Run integration tests
npm run test:integration || exit 1
# Verify build succeeds
npm run build || exit 1
echo "All tests passed, proceeding with push."
exit 0Post-commit: Send Notification
#!/bin/bash
# Send Slack notification after commit
COMMIT_MSG=$(dits log -1 --format="%s")
AUTHOR=$(dits log -1 --format="%an")
BRANCH=$(dits branch --show-current)
curl -X POST -H 'Content-type: application/json' \
--data "{
\"text\": \"New commit on $BRANCH by $AUTHOR: $COMMIT_MSG\"
}" \
"$SLACK_WEBHOOK_URL"
exit 0Hook Parameters
| Hook | Parameters | Stdin |
|---|---|---|
| pre-commit | None | None |
| commit-msg | $1 = message file path | None |
| pre-push | $1 = remote name, $2 = URL | ref info |
| post-checkout | $1 = prev ref, $2 = new ref, $3 = flag | None |
| pre-receive | None | old-sha new-sha ref |
Sharing Hooks
Hooks in .dits/hooks/ are not tracked by default. To share hooks with your team:
# Store hooks in a tracked directory
mkdir -p scripts/hooks
cp .dits/hooks/* scripts/hooks/
# Create setup script
cat > scripts/setup-hooks.sh << 'EOF'
#!/bin/bash
cp scripts/hooks/* .dits/hooks/
chmod +x .dits/hooks/*
echo "Hooks installed!"
EOF
# Team members run after clone
./scripts/setup-hooks.shBypass Hooks
Use
--no-verify to skip hooks in emergencies:dits commit --no-verify -m "Emergency fix"Related Topics
- Server Webhooks - HTTP-based automation
- CI/CD Integration - Pipeline automation
- Repository Commands - Commit and push