Skip to content

Commit

Permalink
setting: gradle을 통한 git hooks 세팅 프로세스 자동화 (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
ckkim817 committed Jan 12, 2025
1 parent cc0afee commit ef34057
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
13 changes: 13 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,16 @@ dependencies {
tasks.named('test') {
useJUnitPlatform()
}

tasks.register('updateGitHooks', Copy) {
from './scripts'
into './.git/hooks'

filesMatching(['prepare-commit-msg', 'pre-commit']) {
mode = 0775 // 실행 권한 부여
}
}

tasks.named('build') {
dependsOn 'updateGitHooks'
}
20 changes: 20 additions & 0 deletions scripts/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/sh

# 1. 변경된 파일 목록 가져오기
stagedFiles=$(git diff --staged --name-only)

# 2. 변경된 파일들에 대하여 Spotless를 실행해 코드 컨벤션을 적용
if [ -n "$stagedFiles" ]; then
echo "Running spotlessApply on staged files: $stagedFiles"
./gradlew spotlessApply --daemon -PspotlessFiles="$stagedFiles"
else
echo "No files to format."
exit 0
fi

# 3. Spotless를 통해 변경된 파일들을 다시 추가
for file in $stagedFiles; do
if test -f "$file"; then
git add "$file"
fi
done
16 changes: 16 additions & 0 deletions scripts/prepare-commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash

COMMIT_EDITMSG_FILE_PATH=$1
DEFAULT_COMMIT_MSG=$(cat "$COMMIT_EDITMSG_FILE_PATH")

CURRENT_BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD)

ISSUE_NUMBER=$(echo "$CURRENT_BRANCH_NAME" | grep -o '/#.*' | sed 's/\///')

if [[ "$DEFAULT_COMMIT_MSG" =~ ^[Mm]erge ]]; then
SUFFIX=""
else
SUFFIX="($ISSUE_NUMBER)"
fi

echo "$DEFAULT_COMMIT_MSG $SUFFIX" > "$COMMIT_EDITMSG_FILE_PATH"

0 comments on commit ef34057

Please sign in to comment.