Section 14/181 menit
14. Git Hooks & Pre-commit Automation
14. Git Hooks & Pre-commit Automation
pre-commit Framework
swift
# Install pre-commit (Python tool)
brew install pre-commit
# Setup di project
cat > .pre-commit-config.yaml << 'EOF'
repos:
- repo: local
hooks:
- id: swiftlint
name: SwiftLint
entry: swiftlint
language: system
types: [swift]
- id: swiftformat
name: SwiftFormat
entry: swiftformat
args: [--lint]
language: system
types: [swift]
- id: trailing-whitespace
name: Trailing Whitespace
entry: trailing-whitespace-fixer
language: python
types: [text]
- id: check-merge-conflict
name: Check Merge Conflicts
entry: check-merge-conflict
language: python
types: [text]
EOF
# Install hooks ke .git/hooks/
pre-commit install
# Run manual pada semua file
pre-commit run --all-files
Custom Git Hooks
swift
# .git/hooks/pre-push
#!/bin/bash
set -e
echo "Running tests before push..."
xcodebuild test \
-project MyApp.xcodeproj \
-scheme MyApp \
-destination "platform=iOS Simulator,name=iPhone 15 Pro" \
-quiet \
| grep -E "Test Suite|error:|warning:" \
| tail -20
echo "✓ All tests passed. Pushing..."
swift
# .git/hooks/commit-msg — enforce conventional commits
#!/bin/bash
COMMIT_MSG=$(cat "$1")
PATTERN="^(feat|fix|docs|style|refactor|perf|test|chore|ci|build)(\(.+\))?: .+"
if ! echo "$COMMIT_MSG" | grep -qE "$PATTERN"; then
echo "❌ Invalid commit message format!"
echo "Expected: type(scope): description"
echo "Example: feat(auth): add biometric login"
echo "Types: feat|fix|docs|style|refactor|perf|test|chore|ci|build"
exit 1
fi
echo "✓ Commit message format valid"
Makefile Workflow Lengkap
swift
# Makefile di root project
.PHONY: setup lint format test build clean ci
# Setup dev environment (jalankan setelah clone)
setup:
brew install swiftlint swiftformat periphery pre-commit
pre-commit install
@echo "✓ Dev environment ready"
# Code quality
lint:
swiftlint --strict
format:
swiftformat .
# Cek tanpa ubah file (untuk CI)
lint-ci:
swiftlint --strict
swiftformat . --lint
# Dead code
deadcode:
periphery scan
# Tests
test:
xcodebuild test \
-project MyApp.xcodeproj \
-scheme MyApp \
-destination "platform=iOS Simulator,name=iPhone 15 Pro" \
-quiet
# Build for release
build:
xcodebuild archive \
-project MyApp.xcodeproj \
-scheme MyApp \
-configuration Release \
-archivePath build/MyApp.xcarchive
# Clean semua build artifacts
clean:
rm -rf build/
rm -rf DerivedData/
xcodebuild clean
# Full CI pipeline (urutan yang sama dengan Xcode Cloud)
ci: lint-ci test build
@echo "✓ CI pipeline passed"
# Deploy beta ke TestFlight
beta:
bundle exec fastlane beta
# Generate docs
docs:
swift package generate-documentation \
--target MyApp \
--output-path docs