꺼내먹는지식 준

GITHUB 협업4 TIPS pre-commit 본문

CS/깃헙

GITHUB 협업4 TIPS pre-commit

알 수 없는 사용자 2022. 3. 18. 12:04

https://www.daleseo.com/pre-commit/

 

pre-commit 도구로 Git Hook 사용하기

Engineering Blog by Dale Seo

www.daleseo.com

해당 글의 내용을 요약 축약 한거라, 제 글보다는 해당 블로그에서 글을 읽는걸 추천드립니다. 

 

pre-commit 훅(hook)은 우리의 작성 코드를 커밋할 때 자동으로 특정 검수를 시행한다. formatter 를 실행하여 코드 스타일을 통일하고, linter 를 실행하여 코드의 잠재되어있는 문제를 찾아낸다. 

즉, 통일을 통해 readability 와 sustatinability 를 올리고 잠재 문제를 찾아내어 안정성을 보장한다. 


git add $\rightarrow$ staging area $\rightarrow$ commit 

staging area 에 있는 파일 대상으로 검수


설치 

pip install pre-commit

설치가 잘 되었을까? 

$ pre-commit -V
pre-commit 2.8.2

 

pre-commit 은 .prep-commit-config.yaml 이라는 설정파일을 필요로 한다. 

 

※ 얼마전까지 yaml 파일을 모르던 나같은 사람을 위해 간단히 언급하자면 여러 설정 내역들이 기입 되어 있는 파일이다. 

 

샘플 설정을 출력해주는 커맨드를 이용하여 설정 파일 생성 

pre-commit sample-config > .pre-commit-config.yaml

 

생성된 설정 파일을 열어보면 4개의 훅(hook)이 설정되어 있다. 

※hook? : callback 함수랑 비슷한 느낌 인자로 넘겨지지는 않지만, 불리는 순간 내부 구현 동작 

 

# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
  - repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v3.2.0
    hooks:
      - id: trailing-whitespace
      - id: end-of-file-fixer
      - id: check-yaml
      - id: check-added-large-files

pre-commit 도구는 인터넷에 공개되어 있는 Git 저장소로 부터 hook을 내려받아 실행 

즉, 우리가 원하는 실행 hook이 어디에 Git에 저장되어 있는지 알아야 한다. 

 

https://pre-commit.com/hooks.html

 

pre-commit

 

pre-commit.com

다음의 페이지 방문시 여러 pre-commit hook 참고 가능 

 

실행

초기 셋언 단계에서는 직접 pre-commit을 실행해보면서 설정이 잘 되었는지 확인 필요 

수동으로 pre-commit을 실행해보자. (즉, pre commit은 자동 설정)

 

$ pre-commit run
[INFO] Initializing environment for https://github.com/pre-commit/pre-commit-hooks.
[INFO] Installing environment for https://github.com/pre-commit/pre-commit-hooks.
[INFO] Once installed this environment will be reused.
[INFO] This may take a few minutes...
Trim Trailing Whitespace.............................(no files to check)Skipped
Fix End of Files.....................................(no files to check)Skipped
Check Yaml...........................................(no files to check)Skipped
Check for added large files..........................(no files to check)Skipped

설정 파일에 등록 된 모든 hook이 설치 된 후, 체크 파일이 없어 hook의 실행이 생략 되었다. 

 

$ git add .pre-commit-config.yaml
$ pre-commit run -a
Trim Trailing Whitespace.................................................Passed
Fix End of Files.........................................................Passed
Check Yaml...............................................................Passed
Check for added large files..............................................Passed

모든 hook 통과! 

 

본격적인 테스트를 위해 commit 하자! 

. file 이여서 ls 로는 안보이니 ls -a 로 파일이 있는지도 한번 먼저 확인해보자. 

$ git commit -m "creates .pre-commit-config.yaml"
[master (root-commit) d262bdb] creates .pre-commit-config.yaml
 1 file changed, 10 insertions(+)
 create mode 100644 .pre-commit-config.yaml

commit 안해도 동작하는데 정확히 왜 commit을 하는지는 모르겠지만, 추측하기로는 1) 자동화 하기전에 commit을 해야한다? (이건 아닌것 같다.) 2) pre-commit에서 지정한 hook 내역을 저장소에 저장한다. 

 

테스트

설정한 hook이 잘 동작하는지 확인해보자. whitespace 를 넣어서 test.txt파일 생성 

$ echo "test " > test.txt

 

생성된 파일을 스테이징 영역에 추가하고, pre-commit 도구를 실행해보면 trailing-whitespace hook이 실패했다. 

$ git add test.txt
$ pre-commit run
Trim Trailing Whitespace.................................................Failed
- hook id: trailing-whitespace
- exit code: 1
- files were modified by this hook

Fixing test.txt

Fix End of Files.........................................................Passed
Check Yaml...........................................(no files to check)Skipped
Check for added large files..............................................Passed

hook 이 실패한 파일은 자동으로 수정해주기 때문에, 수정이 잘 되었는지 확인을 위해

변경분을 staging 영역에 추가 한 후 다시 pre commit 실행하면 모든 hook이 통과된다. 

$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   test.txt

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   test.txt

$ git add test.txt
$ pre-commit run
Trim Trailing Whitespace.................................................Passed
Fix End of Files.........................................................Passed
Check Yaml...........................................(no files to check)Skipped
Check for added large files..............................................Passed

 

자동화 

pre-commit 사용 최종 목적은 Git으로 commit을 남길 때마다 특정 작업을 자동으로 실행 

Git clone 후 pre-commit install 을 통해 자동화 완료 

$ pre-commit install

pre-commit installed at .git/hooks/pre-commit

이후로는 pre-commit run 커맨드 없이도 자동 pre-commit이 실시된다. 

Comments