Skip to main content
Documentation

Automation with Hooks

Hooks let you run custom scripts at key points in the Dits workflow. Automate validation, testing, notifications, and more.

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

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

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

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

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

Hook Parameters

HookParametersStdin
pre-commitNoneNone
commit-msg$1 = message file pathNone
pre-push$1 = remote name, $2 = URLref info
post-checkout$1 = prev ref, $2 = new ref, $3 = flagNone
pre-receiveNoneold-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.sh

Related Topics