# JavaScript/TypeScript 프로젝트 if [ -f "package.json" ]; then echo"Running ESLint..." npm run lint if [ $? -ne 0 ]; then echo"❌ ESLint check failed. Please fix the errors and try again." exit 1 fi
echo"Running tests..." npm test if [ $? -ne 0 ]; then echo"❌ Tests failed. Please fix the tests and try again." exit 1 fi fi
# Python 프로젝트 if [ -f "requirements.txt" ]; then echo"Running flake8..." flake8 . if [ $? -ne 0 ]; then echo"❌ Flake8 check failed." exit 1 fi fi
# Trailing whitespace 제거 git diff --check if [ $? -ne 0 ]; then echo"❌ Detected trailing whitespace. Please remove it." exit 1 fi
if ! echo"$commit_msg" | grep -qE "$pattern"; then echo"❌ Invalid commit message format!" echo"" echo"Commit message must follow Conventional Commits format:" echo" <type>(<scope>): <subject>" echo"" echo"Examples:" echo" feat: add user authentication" echo" fix(api): resolve CORS issue" echo" docs: update README" echo"" echo"Types: feat, fix, docs, style, refactor, test, chore, perf, ci, build, revert" exit 1 fi
echo"✅ Commit message format is valid"
3. Pre-push Hook - 푸시 전 검증
원격 저장소에 푸시하기 전 모든 테스트를 실행합니다.
#!/bin/sh # .git/hooks/pre-push
echo"Running pre-push checks..."
protected_branch='main' current_branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')
# main 브랜치 직접 푸시 방지 if [ "$current_branch" = "$protected_branch" ]; then echo"❌ Direct push to main branch is not allowed!" echo"Please create a feature branch and open a pull request." exit 1 fi
# 전체 테스트 실행 echo"Running full test suite..." npm run test:ci if [ $? -ne 0 ]; then echo"❌ Tests failed. Push aborted." exit 1 fi
# 빌드 테스트 echo"Testing build..." npm run build if [ $? -ne 0 ]; then echo"❌ Build failed. Push aborted." exit 1 fi
echo"✅ All pre-push checks passed!"
4. Post-merge Hook - 머지 후 의존성 업데이트
브랜치 머지 후 자동으로 의존성을 설치합니다.
#!/bin/sh # .git/hooks/post-merge
echo"Checking for dependency changes..."
# package.json 또는 package-lock.json 변경 확인 changed_files="$(git diff-tree -r --name-only --no-commit-id ORIG_HEAD HEAD)"
# Go 모듈 업데이트 check_run go.mod "echo '📦 go.mod changed. Running go mod download...' && go mod download"
echo"✅ Post-merge checks completed!"
5. Prepare-commit-msg Hook - 이슈 번호 자동 추가
브랜치 이름에서 이슈 번호를 추출하여 커밋 메시지에 자동으로 추가합니다.
#!/bin/sh # .git/hooks/prepare-commit-msg
commit_msg_file=$1 commit_source=$2
# 머지 커밋이면 스킵 if [ "$commit_source" = "merge" ]; then exit 0 fi
# 브랜치 이름에서 이슈 번호 추출 (예: feature/ISSUE-123-description) branch_name=$(git symbolic-ref --short HEAD) issue_number=$(echo"$branch_name" | grep -o -E '[A-Z]+-[0-9]+' | head -1)
if [ -n "$issue_number" ]; then # 기존 메시지 읽기 commit_msg=$(cat"$commit_msg_file")
# 이미 이슈 번호가 있으면 스킵 if ! echo"$commit_msg" | grep -q "$issue_number"; then # 이슈 번호 추가 echo"$commit_msg" | sed "1s/^/[$issue_number] /" > "$commit_msg_file" echo"✅ Added issue number: $issue_number" fi fi