레이블이 git-hooks인 게시물을 표시합니다. 모든 게시물 표시
레이블이 git-hooks인 게시물을 표시합니다. 모든 게시물 표시

git-cliff and git commit template

# git-cliff 로 커스텀한 changelog 를 생성할 수 있다.
https://github.com/orhun/git-cliff
# 설치
brew install git-cliff

# 이제 git-cliff 또는 git cliff 명령을 사용할 수 있다.
# 설정파일(cliff.toml) 생성
git cliff --init

# cliff.toml 에서 사용하는 template 엔진 tera 사용방법
# https://www.conventionalcommits.org 포맷을 사용하지 않으면 다음 2개의 설정을 비활성화 해야 커밋 메시지 파싱을 할 수 있다.
vi cliff.toml
[git]
conventional_commits = false
filter_unconventional = false

# changelog 생성
# stdout 출력
git cliff

# CHANGELOG.md 파일로 출력
git cliff -o CHANGELOG.md

# 버전명시(v 로시작하는 것은 자동 cliff.toml에서 trim 처리)
# 버전명시 하지 않으면 [unreleased] 로 표시된다.
git cliff -o CHANGELOG.md -t v1.0.0

# 현재 태그에 대한 변경사항만 생성(새 태그 버전에서의 변경 사항만 파악할때 유용)
git cliff -o CHANGELOG.md --current

#####

# commit 메시지 템플릿 적용 및 체크
# 참고 ~/.git/hooks 에 각종 hook sample 파일이 있다.
# hook 파일들도 푸시해서 관리하기 위해서 별도 hookPath 를 설정
git config core.hooksPath .githooks

# commit_message_template.txt 내용으로 커밋 메시지 생성
# 템플릿 파일 설정
git config commit.template .githooks/commit_message_template.txt

# git commit 실행시 처리 순서
1. pre-commit 에서 커밋 파일 체크
설정 파일 참고 https://github.com/ysoftman/test_code/blob/master/.githooks/pre-commit

2. prepare-commit-msg 에서 커밋 메시지 처리할 내용
3. (vim 으로) 커밋 메시지 작성

4. commit-msg 에서 메시지 규칙 체크

git commit master 브랜치 방지

# 보통 별도의 브랜치를 생성해서 작업해 커밋하고
# master, develop 브랜치에는 github 페이지에서 pull request 하게 된다.
# 만약 로컬에서 master, develop 브랜치 상태에서 커밋을 방지하려면
# 다음과 같이 pre-commit 파일을 만들어 주면 된다.
# 참고로 아래처럼 cat 사용시 구분자 (zzz) 를 '' 또는 "" 로 묶어줘야 한다.
# 아니면 $는 이스케이프(\)처리해줘야 한다.
cat > .git/hooks/pre-commit << 'zzz'
#!/bin/sh
branch=$(git rev-parse --abbrev-ref HEAD)
if [ "$branch" = "master" ] || [ "$branch" = "develop" ]; then
  echo "You can't commit directly to $branch branch"
  exit 1
fi
zzz
chmod +x .git/hooks/pre-commit

# 이제 master, develop 등에 브랜치에서 commit 명령을 실행하면
# 위 에러메시지 출력 후 커밋이 진행되지 않는다.