Git commit message को ऑटोमेट करने के तरीके
(insight.infograb.net)*git commit message लिखते समय जब मुश्किल महसूस हो
- emoji का उपयोग करने और issue number जोड़ने वाले commit message को track करना आसान होता है और वे देखने में भी अच्छे लगते हैं
- लेकिन commit message जितना लंबा और जटिल होता है, हर बार उसे लिखना उतना ही असुविधाजनक होता है और गलतियाँ भी ज़्यादा होती हैं
- हर बार सही emoji ढूँढ़ना भी काफ़ी थकाऊ काम है
*Git Hooks से git commit message को अनिवार्य बनाना
- Git Hooks वह सुविधा है जो Git से जुड़े event होने पर हमारे द्वारा तय की गई script चलाती है
- यह सुविधा server hook और client hook में बँटी होती है
*Server hook: Git की push rule सुविधा का उपयोग करना
- अगर pre-receive hook में commit message की जाँच करने वाली script डाल दी जाए, तो गलत message वाले commit server पर push नहीं हो पाएँगे
- GitLab में push rule सुविधा का उपयोग करके regular expression से commit message की जाँच की जा सकती है
- अगर कोई commit message regular expression का उल्लंघन करता है, तो वह push reject कर दिया जाता है और error उत्पन्न होता है
*Client hook: push करने से पहले पहले से जाँच करना
- commit को अंतिम रूप से पूरा करने से पहले वाले commit-msg hook में script बनाकर commit message की जाँच की जा सकती है
- अगर commit message को बदलना हो, तो prepare-commit-msg hook में संबंधित script लिखी जा सकती है
*Infograb की commit message automation विधि
- Husky और Commitlint का उपयोग करके commit message automation system बनाया गया
- Husky, Git Hooks प्रबंधन में मदद करने वाला npm module है. यह
.huskydirectory के नीचे script वाले hook file बनाता है. उसके बादnpx husky installcommand चलाने पर वह script git में register हो जाती है - commitlint एक ऐसा tool है जो commit convention को आसानी से manage करता है और commit message की जाँच करता है. टीम द्वारा तय की गई commit convention को JSON format में लिखा जाता है. फिर
commitlintcommand का उपयोग करके message की जाँच की जाती है
- commit convention को commitlint format में परिभाषित करना
- commit message के बुनियादी ढाँचे के आधार पर
commitlint.config.tsfile में हर टीम के अनुसार properties परिभाषित की जाती हैं
- Husky में commitlint register करना
commit-msghook मेंcommitlintcommand register की जाती है..husky/commit-msgfile में script डाली जाती है
- automation जोड़ना
- emoji जोड़ने या issue number डालने जैसे कामों को automate करना
- Infograb product team में केवल type (
feat,fix) दर्ज करने पर emoji अपने आप जुड़ जाए, type के पहले अक्षर को अपने आप बड़ा कर दिया जाए, और अगर branch में issue number हो तो footer में issue number अपने आप जुड़ जाए—ऐसी setting की गई - automation script को
prepare-commit-msghook में डाला जा सकता है
2 टिप्पणियां
commitizen के साथ भी इस्तेमाल करना अच्छा रहेगा
अच्छे टूल के लिए धन्यवाद