Programming/Git

[Git Hook] pre commit ์œผ๋กœ ์žฅ์•  ์˜ˆ๋ฐฉํ•˜๊ธฐ(ft. husky, osascript)

Space_Jin 2024. 8. 29. 17:33
728x90
๋ฐ˜์‘ํ˜•

d์ตœ์‹  ์šด์˜ ์ค‘์ธ ์•ฑ์—์„œ ํŠน์ • ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋‹ค ์žฅ์• ๊ฐ€ ๋‚ฌ์—ˆ๋‹ค.

 

์ผ๋ถ€ ์„œ๋น„์Šค์—์„œ ์‚ฌ์šฉ์ด ๋ถˆ๊ฐ€๋Šฅํ•œ ๋ฉ”์„œ๋“œ๊ฐ€ ์žˆ์—ˆ๋Š”๋ฐ, ํ™˜๊ฒฝ์ด ๋ถ„์‚ฐ๋˜๊ณ  ๋ณต์žกํ•˜๋‹ค๋ณด๋‹ˆ ๊ฐœ๋ฐœ์ž๊ฐ€ ํ…Œ์ŠคํŠธ๋ฅผ ๋†“์นœ ์ผ€์ด์Šค์˜€์—ˆ๋‹ค.

 

์ด ๋ถ€๋ถ„์„ ๊ฐœ์„ ํ•˜๊ณ  ๋น„์Šทํ•œ ์žฅ์• ๋ฅผ ์˜ˆ๋ฐฉํ•˜๊ธฐ ์œ„ํ•ด์„œ pre commit rule์„ ์ถ”๊ฐ€ํ•ด์„œ ํŠน์ • ํ‚ค์›Œ๋“œ๊ฐ€ ์†Œ์Šค์— ์ถ”๊ฐ€๋˜๋ฉด ์•ˆ๋‚ด๋ฅผ ์œ„ํ•œ ํŒ์—…์„ ๋„์šฐ๊ฒŒ ํ–ˆ๋‹ค. 

๐Ÿค– ํ™˜๊ฒฝ

๊ฐœ๋ฐœํŒ€์˜ ํ™˜๊ฒฝ์€ Mac์„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๊ณ  Husky plugin์ด ์„ค์น˜๋˜์–ด ์žˆ์—ˆ๋‹ค.

 

git์€ ๊ฐœ๋ฐœ์ž๊ฐ€ commit์„ ์ˆ˜ํ–‰ํ•˜๋ฉด, commit ์ด ์ˆ˜ํ–‰๋˜๊ธฐ ์ „์— ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‹คํ–‰๋˜๊ฒŒ๋”ํ•˜๋Š” pre-commit ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•œ๋‹ค.

๋ฐฉ๋ฒ•์€ ํ”„๋กœ์ ํŠธ์˜ .git/ ํ•˜์œ„์—  "pre-commit"์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ž‘์„ฑํ•˜๋ฉด ๋œ๋‹ค.

ํ•˜์ง€๋งŒ, git์—์„œ ์ œ๊ณตํ•˜๋Š” pre-commit์˜ ๊ฒฝ์šฐ, ๊ฐœ์ธ ๋กœ์ปฌ์—๋งŒ ์ ์šฉ๋˜๊ณ  remote ํ™˜๊ฒฝ์„ ์ด์šฉํ•ด์„œ ๊ฐœ๋ฐœ์ž๋“ค์ด ๋ฐ”๋กœ ์ ์šฉํ•˜๊ฒŒํ•  ์ˆ˜๋Š” ์—†์—ˆ๋‹ค.

 

https://www.npmjs.com/package/husky

 

husky

Modern native Git hooks. Latest version: 9.1.5, last published: 9 days ago. Start using husky in your project by running `npm i husky`. There are no other projects in the npm registry using husky.

www.npmjs.com

husky๋ฅผ ์ด์šฉํ•˜๋ฉด, pre-commit์„ ์ž‘์„ฑํ•˜๊ณ  ๋ฐฐํฌํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์žฅ์ ์ด ์žˆ์–ด์„œ ์ด์šฉํ•˜๊ธฐ๋กœ ์ด๋ฅผ ํ™œ์šฉํ•˜๊ธฐ๋กœ ํ–ˆ๋‹ค.

 

๐ŸŽ Mac ํŒ์—… ๋„์œ„๊ธฐ(osascript)

mac os์˜ ๊ฒฝ์šฐ, ์• ํ”Œ์—์„œ ์ œ๊ณตํ•˜๋Š” osascript๋ฅผ ์ด์šฉํ•ด์„œ ์•Œ๋žŒ์ด๋‚˜ ํŒ์—…์„ ๋„์šธ ์ˆ˜ ์žˆ๋‹ค.

$ osascript -e 'display dialog  "ํŒ์—… ์ฐฝ ๋ฉ”์„ธ์ œ"'

ํŒ์—… ์ฐฝ ๋„์šฐ๊ธฐ

๊ฒฐ๊ณผ

$ osascript -e 'display dialog  "ํŒ์—… ์ฐฝ ๋ฉ”์„ธ์ œ" buttons{"์ปค์Šคํ…€ ๋ฒ„ํŠผ"}'

์ด๋ ‡๊ฒŒ ๋ฒ„ํŠผ์„ ์ปค์Šคํ…€ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

๋ฒ„ํŠผ ์ปค์Šคํ…€ ๊ฒฐ๊ณผ

$ osascript -e 'set keyword to "ํ‚ค์›Œ๋“œ"' -e 'display dialog keyword & "๋ฅผ ์‚ฌ์šฉ" buttons{"์ปค์Šคํ…€ ๋ฒ„ํŠผ"}'

set ์˜ˆ์•ฝ์–ด๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋ณ€์ˆ˜๋ฅผ ์ง€์ •ํ•  ์ˆ˜๋„ ์žˆ๋‹ค

์œ„ ์†Œ์Šค๋Š” keyword ๋ผ๋Š” ๋ณ€์ˆ˜์— "ํ‚ค์›Œ๋“œ"๋ผ๋Š” ๋ฌธ์ž์—ด์„ ํ• ๋‹นํ•œ ํ›„, display dialog ์˜ˆ์•ฝ์–ด๋กœ ํŒ์—… ์ฐฝ์„ ๋„์› ๋‹ค.

๋ณ€์ˆ˜ ์‚ฌ์šฉ ๊ฒฐ๊ณผ

๐Ÿ” Git diff๋กœ ์ˆ˜์ •ํ•œ ์†Œ์Šค์˜ ํ‚ค์›Œ๋“œ ์ถ”๊ฐ€ ํ™•์ธํ•˜๊ธฐ

git ํ”„๋กœ์ ํŠธ์— git diff๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์†Œ์Šค์˜ ๋ณ€ํ™”๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

์ด๋•Œ, ์ถ”๊ฐ€๋œ line์€ +๊ฐ€ ์‚ญ์ œ๋œ line์€ -๊ฐ€ ๋งจ ์•ž์— ๋ถ™์–ด์žˆ๋‹ค.

์ด๋ฅผ ํ™œ์šฉํ•˜๋ฉด, ํŠน์ • ํ‚ค์›Œ๋“œ๊ฐ€ ์†Œ์Šค์— ์ถ”๊ฐ€๋˜์—ˆ๋Š”์ง€ ํ™•์ธ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

$ git diff --cached | grep -q "^\+.*${KEYWORD}"

--cached ์˜ต์…˜์€ ์Šคํƒœ์ด์ง• ์ƒํƒœ(add ํ›„)๋ฅผ ํ™•์ธํ•˜๊ณ  grep์˜ -q ์˜ต์…˜์€ ์กด์žฌํ•˜๋Š”์ง€ true / false ๊ฒฐ๊ณผ๋งŒ returnํ•˜๊ฒŒ ํ•ด์ค€๋‹ค.

-q ์˜ต์…˜์„ ์ œ์™ธํ•˜๋ฉด, ํ•ด๋‹น line์„ ํ„ฐ๋ฏธ๋„์— ์ถœ๋ ฅ ํ•ด์ค€๋‹ค.

 

์ด๋ฅผ ํ™œ์šฉํ•˜๋ฉด commit ์‹œ ํŠน์ • ํ‚ค์›Œ๋“œ๋ฅผ ํ™•์ธํ•ด์„œ ๋™์ž‘ํ•˜๋Š” pre-commit์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

#!/usr/bin/env sh

KEYWORD="eval"

if git diff --cached | grep -q "^\+.*${KEYWORD}"; then
        response=$(osascript -e 'set KW to "$KEYWORD"' -e 'display dialog KW & "๋ฅผ ์ถ”๊ฐ€ํ•˜์…จ์Šต๋‹ˆ๋‹ค." buttons{"ํ™•์ธ"}')
fi

exit 0

 

์ด๋กœ์จ, ์ž์นซ ๊ฐœ๋ฐœ์ž๋“ค์ด ๋†“์น  ์ˆ˜ ์žˆ๋Š” ๋ถ€๋ถ„์„ ํ•œ๋ฒˆ ๋” ์ฑ™๊ฒจ์„œ ์žฅ์•  ์˜ˆ๋ฐฉ์— ๋„์›€์ด๋  ์ˆ˜ ์žˆ์ง€ ์•Š์„๊นŒ ์‹ถ๋‹ค.

 

์ฐธ๊ณ ๋กœ husky๋ฅผ ํ™œ์šฉํ•ด์„œ ์ˆ˜์ •ํ•œ ์†Œ์Šค๋ฅผ remote ํ™˜๊ฒฝ์— ์˜ฌ๋ ค๋‘๋ฉด, ๊ฐœ๋ฐœ์ž๋“ค์ด pullํ•ด๊ฐˆ ์ˆ˜ ์žˆ์ง€๋งŒ, .git/pre-commit์„ ์ž‘์„ฑํ•ด์•ผํ•œ๋‹ค๋ฉด, ์†Œ์Šค๋ฅผ ๊ฐœ๋ฐœ์ž๋“ค์—๊ฒŒ ์ „ํŒŒํ•ด์„œ ์ง์ ‘ ๋กœ์ปฌ ํ™˜๊ฒฝ์— ์ ์šฉ์‹œํ‚ค๊ฒŒ ํ•˜๊ฑฐ๋‚˜ ํ•œ ์‚ฌ๋žŒ์ด pre-commit ํŒŒ์ผ์„ remote์— ์˜ฌ๋ฆฌ๊ณ  ํ•ด๋‹น ํŒŒ์ผ์„ ์ง์ ‘ ์ ์šฉ์‹œ์ผœ์ฃผ๋Š” ์Šคํฌ๋ฆฝํŠธ ๋“ฑ์„ ๋งŒ๋“ค์–ด์„œ ๊ฐœ๋ฐœ์ž๋“ค์€ ์†Œ์Šค๋ฅผ pull ๋ฐ›๊ณ  ์Šคํฌ๋ฆฝํŠธ๋งŒ ์‹คํ–‰ํ•˜๋ฉด ์ ์šฉ๋  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค˜์•ผํ•  ๊ฒƒ ๊ฐ™๋‹ค.

 

๋‚ด ๊ฒฝ์šฐ๋Š” ์ผ๋ถ€ repo์˜ ๊ฒฝ์šฐ husky๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์•„์„œ ํ›„์ž์˜ ๋ฐฉ๋ฒ•์œผ๋กœ pre-commit์„ ์ ์šฉ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋Š” ์Šคํฌ๋ฆฝํŠธ๋ฅผ ๋งŒ๋“ค์–ด์„œ ์ œ๊ณตํ–ˆ๋‹ค.

728x90
๋ฐ˜์‘ํ˜•