Skip to content

Commit

Permalink
[TNT-69] chore: Lint 관련 Git hook 구현
Browse files Browse the repository at this point in the history
* command : sh script/setup_lint.sh
  • Loading branch information
hoyahozz committed Jan 3, 2025
1 parent 83ae398 commit 5a1eeeb
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
11 changes: 11 additions & 0 deletions script/detekt_hook_script
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/sh
echo "Running git pre-push hook"

./gradlew detekt --daemon

# $? = ".gradlew ktlintFormat --daemon"에 대한 return 값
STATUS=$?

# 문제없이 끝났다면 exit 0, 아니면 1
[ $STATUS -ne 0 ] && exit 1
exit 0
60 changes: 60 additions & 0 deletions script/setup_lint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/usr/bin/env bash

# 현재 디렉터리 설정
LINTER_CURRENT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
PROJECT_ROOT="$LINTER_CURRENT_DIR/.."
GRADLEW_SCRIPT_PATH="$LINTER_CURRENT_DIR/../gradlew"
GIT_HOOK_DIR="$LINTER_CURRENT_DIR/../.git/hooks"
DETEKT_TARGET_FILE="$GIT_HOOK_DIR/pre-push"
KTLINT_TARGET_FILE="$GIT_HOOK_DIR/pre-commit"
DETEKT_HOOK_SCRIPT="$LINTER_CURRENT_DIR/detekt_hook_script"

# Git 저장소 확인
if ! git rev-parse --is-inside-work-tree &> /dev/null
then
echo "Error: Current directory is not a Git repository!"
exit 1
fi

# Gradle Wrapper 확인
if [[ ! -f "$GRADLEW_SCRIPT_PATH" ]]
then
echo "Error: Gradle Wrapper not found at $GRADLEW_SCRIPT_PATH"
exit 1
fi

# Git Hook 디렉터리 생성
if [[ ! -d "$GIT_HOOK_DIR" ]]
then
mkdir -p "$GIT_HOOK_DIR"
fi

# Detekt pre-push Hook 설치 확인
if [[ -f "$DETEKT_TARGET_FILE" ]]
then
echo "*************************************************"
echo " Detekt integration failed "
echo "*************************************************"
echo "Reason: GIT pre-push hook is already installed!"
exit 1
fi

# KtLint Git Hooks pre-commit 추가
cd "$PROJECT_ROOT" || exit # 프로젝트 루트로 이동해야만 아래 명령어 작동
eval "$GRADLEW_SCRIPT_PATH addKtlintCheckGitPreCommitHook"
chmod +x "$KTLINT_TARGET_FILE"

# Detekt Hook 추가
if [[ ! -f "$DETEKT_HOOK_SCRIPT" ]]
then
echo "Error: Detekt hook script not found at $DETEKT_HOOK_SCRIPT"
exit 1
fi

cat "$DETEKT_HOOK_SCRIPT" > "$DETEKT_TARGET_FILE"
chmod +x "$DETEKT_TARGET_FILE"

echo "************************************************"
echo " Linter installed "
echo "************************************************"
echo "Install path: $(ls "$GIT_HOOK_DIR")"

0 comments on commit 5a1eeeb

Please sign in to comment.