๐Ÿฅž BE
home

Git Hooks

ESLint, Prettier, Testing โ€ฆ

์œ„์— ์„œ์ˆ ํ•œ ๋ชจ๋“  ๋„๊ตฌ ๋˜๋Š” ๊ทœ์น™์€ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๊ฑฐ๋‚˜ ํ˜‘์—…์„ ํ•˜๋Š”๋ฐ ์žˆ์–ด ํ†ต์ผ์„ฑ, ๊ฐ€๋…์„ฑ, ์•ˆ์ •์„ฑ ๋“ฑ์„ ๋†’์ผ ์ˆ˜ ์žˆ๋Š” ์•„์ฃผ ์œ ์šฉํ•œ ๊ทœ์น™๋“ค์ด๋‹ค.
์ด๋Ÿฌํ•œ ๊ทœ์น™๋“ค์„ ๊ฐ€์ง€๊ณ  ๊ฐœ๋ฐœ์„ ์ง„ํ–‰ํ•˜๊ฒŒ ๋˜๋ฉด ์ถ”ํ›„์— ๋ฐœ์ƒํ•˜๊ฒŒ ๋  ์œ ์ง€, ๋ณด์ˆ˜์˜ ๋น„์šฉ์„ ์ค„์—ฌ์ค„ ์ˆ˜ ์žˆ๋‹ค.
ํ•˜์ง€๋งŒ ๋ง๋งŒ ๋ฒˆ์ง€๋ฅด๋ฅดํ•˜๊ฒŒ ํ•˜๊ณ  ์ •์ž‘ ์ •์˜ํ•œ ๊ทœ์น™๋“ค์„ ์ง€ํ‚ค์ง€ ์•Š์œผ๋ฉด ๋ง์งฑ ๋„๋ฃจ๋ฌต์ด๋‹ค. ์ด๋Ÿฌํ•œ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ๊ฒƒ์ด ๋ฐ”๋กœ Git Hooks๋‹ค.
์•„๋ฌด๋ฆฌ ์ง€ํ‚ค๋ ค๊ณ  ๋…ธ๋ ฅํ•ด๋„ ์ธ๊ฐ„์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š” Human Error๊นŒ์ง€ ๋ชจ๋‘ ์žก์•„๋‚ด๊ธฐ์—๋Š” ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค. ๊ทธ๋ž˜์„œ Git hooks๋ฅผ ํ†ตํ•ด ์ด๋Ÿฌํ•œ ๊ทœ์น™๋“ค์„ ๊ฐ•์ œํ•˜๋ฉด์„œย ๋ฌด์กฐ๊ฑด ์ง€์ผœ์ง€๋Š” ๊ทœ์น™์„ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด๋‹ค.

Git Hooks

Git Hook์ด๋ž€ Git๊ณผ ๊ด€๋ จ๋œ ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ–ˆ์„ ๋•Œ ํŠน์ •ํ•œ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‹คํ–‰ํ•˜๋Š” ๊ธฐ๋Šฅ์ด๋‹ค. ํฌ๊ฒŒ ํด๋ผ์ด์–ธํŠธ ํ›…๊ณผ ์„œ๋ฒ„ ํ›…์œผ๋กœ ๋‚˜๋‰˜๋Š”๋ฐ ํด๋ผ์ด์–ธํŠธ ํ›…์€ commit, push, merge ๋“ฑ์ด ๋ฐœ์ƒํ•˜๊ธฐ ์ „์— ์‹คํ–‰ํ•˜๋Š” ํ›…์ด๊ณ  ์„œ๋ฒ„ ํ›…์€ remote repository๋กœ push๊ฐ€ ๋ฐœ์ƒํ–ˆ์„ ๋•Œ ์‹คํ–‰ํ•˜๋Š” ํ›…์ด๋‹ค.
ํด๋ผ์ด์–ธํŠธ ํ›…ย ์€ย ์ปค๋ฐ‹ ์›Œํฌํ”Œ๋กœ ํ›…,ย ์ด๋ฉ”์ผ ์›Œํฌํ”Œ๋กœ ํ›…, ๊ทธ๋ฆฌ๊ณ ย ๊ธฐํƒ€ ํ›…ย ์œผ๋กœ ๋ถ„๋ฅ˜ํ•  ์ˆ˜ ์žˆ๋‹ค.
์ปค๋ฐ‹ ์›Œํฌํ”Œ๋กœ ํ›…ย ์€ย git commitย ๋ช…๋ น์œผ๋กœ ์ปค๋ฐ‹์„ ํ•  ๋•Œ ์‹คํ–‰ํ•˜๋Š” ํ›…์ด๊ณ ย ์ด๋ฉ”์ผ ์›Œํฌํ”Œ๋กœ ํ›…ย ์€ย git amย ๋ช…๋ น์œผ๋กœ ์ด๋ฉ”์ผ์„ ํ†ตํ•ด patch ํŒŒ์ผ์„ ์ ์šฉํ•  ๋•Œ ์‹คํ–‰ํ•˜๋Š” ํ›…์ด๋‹ค.ย ๊ธฐํƒ€ ํ›…ย ์€ rebase, merge, push ์™€ ๊ฐ™์€ ์ด๋ฒคํŠธ๋ฅผ ์‹คํ–‰ํ•  ๋•Œ ์‹คํ–‰ํ•˜๋Š” ํ›…์„ ํฌํ•จํ•œ๋‹ค.
๋ถ„๋ฅ˜
ํ›…
์„ค๋ช…
์ปค๋ฐ‹์›Œํฌํ”Œ๋กœ ํ›…
pre-commit
commit ์„ ์‹คํ–‰ํ•˜๊ธฐ ์ „์— ์‹คํ–‰
prepare-commit-msg
commit ๋ฉ”์‹œ์ง€๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ํŽธ์ง‘๊ธฐ๋ฅผ ์‹คํ–‰ํ•˜๊ธฐ ์ „์— ์‹คํ–‰
commit-msg
commit ๋ฉ”์‹œ์ง€๋ฅผ ์™„์„ฑํ•œ ํ›„ commit ์„ ์ตœ์ข… ์™„๋ฃŒํ•˜๊ธฐ ์ „์— ์‹คํ–‰
post-commit
commit ์„ ์™„๋ฃŒํ•œ ํ›„ ์‹คํ–‰
์ด๋ฉ”์ผ์›Œํฌํ”Œ๋กœ ํ›…
applypatch-msg
git am ๋ช…๋ น ์‹คํ–‰ ์‹œ ๊ฐ€์žฅ ๋จผ์ € ์‹คํ–‰
pre-applypatch
patch ์ ์šฉ ํ›„ ์‹คํ–‰ํ•˜๋ฉฐ, patch ๋ฅผ ์ค‘๋‹จ์‹œํ‚ฌ ์ˆ˜ ์žˆ์Œ
post-applypatch
git am ๋ช…๋ น์—์„œ ๋งˆ์ง€๋ง‰์œผ๋กœ ์‹คํ–‰ํ•˜๋ฉฐ, patch ๋ฅผ ์ค‘๋‹จ์‹œํ‚ฌ ์ˆ˜ ์—†์Œ
๊ธฐํƒ€ ํ›…
pre-rebase
Rebase ํ•˜๊ธฐ ์ „์— ์‹คํ–‰
post-rewrite
git commit โ€“amend, git rebase ์™€ ๊ฐ™์ด ์ปค๋ฐ‹์„ ๋ณ€๊ฒฝํ•˜๋Š” ๋ช…๋ น์„ ์‹คํ–‰ํ•œ ํ›„ ์‹คํ–‰
post-merge
Merge ๊ฐ€ ๋๋‚˜๊ณ  ๋‚˜์„œ ์‹คํ–‰
pre-push
git push ๋ช…๋ น ์‹คํ–‰ ์‹œ ๋™์ž‘ํ•˜๋ฉฐ ๋ฆฌ๋ชจํŠธ ์ •๋ณด๋ฅผ ์—…๋ฐ์ดํŠธ ํ•˜๊ณ  ๋‚œ ํ›„ ๋ฆฌ๋ชจํŠธ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ „์†กํ•˜๊ธฐ ์ „์— ์‹คํ–‰. push ๋ฅผ ์ค‘๋‹จ์‹œํ‚ฌ ์ˆ˜ ์žˆ์Œ

pre-commit

๋ณดํ†ต pre-commit hook์„ ๋งŽ์ด ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ ๊ฐ™๋‹ค. ์ปค๋ฐ‹ํ•  ๋•Œ ๊ฐ€์žฅ ๋จผ์ € ํ˜ธ์ถœ๋˜๋Š” hook์œผ๋กœ, git์œผ๋กœ ์ปค๋ฐ‹์„ ๋‚จ๊ธธ ๋•Œ ๋งˆ๋‹ค ํŠน์ • ์ž‘์—…์„ ์ž๋™์œผ๋กœ ์‹คํ–‰ํ•˜๋Š” ๊ฒƒ์„ ๋ชฉ์ ์œผ๋กœ ํ•œ๋‹ค.
black, isort, flake8 ๋“ฑ ๊ฐ€๋…์„ฑ๊ณผ ์ฝ”๋“œ ํ€„๋ฆฌํ‹ฐ๋ฅผ ๋†’์—ฌ์ฃผ๋Š” tool์„ ์ž๋™ํ™”์‹œ์ผœ์ฃผ๋Š” tool์ด๋‹ค.
pre-push๋„ ์ž์ฃผ ์‚ฌ์šฉ๋œ๋‹ค.

Husky

๋งŒ์•ฝ ๋ชจ๋“ˆ ๊ด€๋ฆฌ๋ฅผ ์œ„ํ•ด npm์„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋‹ค๋ฉด, husky๋ฅผ ํ™œ์šฉํ•ด์„œ git hook๊ด€๋ฆฌ๋ฅผ ํ•  ์ˆ˜ ์žˆ๋‹ค.

huskyhooks๋ฅผ ํ†ตํ•œ pre-push ์‚ฌ์šฉ ์˜ˆ์‹œ

#!/bin/sh FORBIDDEN_HTTPS_URL="https://github.com/gabia/git-hooks-study.git" # insert your remote url (https) FORBIDDEN_SSH_URL="git@github.com:gabia/git-hooks-study.git" # insert your remote url (ssh) FORBIDDEN_REF="refs/heads/master" # insert branch ref ARR_GIT_PARAMS=($(echo $HUSKY_GIT_PARAMS)) ARR_GIT_STDIN=($(echo $HUSKY_GIT_STDIN)) remote=${ARR_GIT_PARAMS[0]} url=${ARR_GIT_PARAMS[1]} local_ref=${ARR_GIT_STDIN[0]} local_sha=${ARR_GIT_STDIN[1]} remote_ref=${ARR_GIT_STDIN[2]} remote_sha=${ARR_GIT_STDIN[3]} if [ "$url" != "$FORBIDDEN_HTTPS_URL" -a "$url" != "$FORBIDDEN_SSH_URL" ] then exit 0 # Forked Project ์—์„œ๋Š” ์ œํ•œํ•˜์ง€ ์•Š์Œ fi if [ "$remote_ref" == "$FORBIDDEN_REF" ] then echo "DO NOT PUSH it master" exit 1 # ๊ธˆ์ง€๋œ ref ๋กœ push ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ์—๋Ÿฌ fi exit 0
Bash
๋ณต์‚ฌ
๊ทธ๋ฆฌ๊ณ ย .huskyrcย ํŒŒ์ผ์—์„œย pre-pushย ํ›…์ด ์œ„ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‹คํ–‰ํ•˜๋„๋ก ์„ค์ •ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.
{ "hooks": { "pre-commit": "echo 'Hello World!", "pre-push": "./huskyhooks/pre-push" } }
Bash
๋ณต์‚ฌ

Reference