如何使用 Git Hooks 遵守 Drupal 編碼標準
已發表: 2023-05-23好的代碼就像精心打造的樂高積木作品——它很強大,看起來不錯,而且如果需要的話很容易改變。 當您在團隊中編碼、從事可擴展項目或參與 Drupal 等開源社區時,良好編碼標準的重要性尤為重要。
與任何其他開源項目一樣,Drupal 有數千名開發人員參與該項目。 他們每個人都有自己的專業水平。 您如何確保團隊或社區中的每個人都遵循良好的編碼實踐? Git 鉤子!
Git Hooks 是確保您的代碼始終符合 Drupal 編碼標準的一種簡單且自動化的方法。 使用 Git hook 實施 Drupal 編碼標準將幫助開發人員使用 Drupal 社區聲明的適當編碼標準提交和推送代碼。 它還可以幫助提高您的項目管理技能,並允許開發人員使用適當的提交消息標準提交代碼。 詳細了解 Git 掛鉤以及如何將它們付諸實踐。

什麼是 Git 鉤子
Git Hooks 是每次調用 Git 命令時都會自動運行的腳本。 正如您將使用hook_form_alter來更改 Drupal 中的表單一樣,您可以為每個 Git 操作設置單獨的預定義掛鉤。

Git hook 的圖示
尋找 Git 鉤子
您可以在.git/hooks下的項目文件夾中找到 Git 掛鉤(前提是 Git 已初始化)。 在那裡,您會找到所有帶有.sample擴展名的鉤子,以防止它們默認執行。
要使用所需的掛鉤,您需要刪除.sample擴展名並編輯執行代碼。
有許多可用的 Git 掛鉤,但我們將使用預提交Git 掛鉤來啟動 Drupal 編碼標準。
預提交Git 掛鉤是將在代碼提交之前運行的掛鉤。 它檢查正在提交的代碼行。
實施 Git 鉤子
在開始之前,請確保您已準備好以下基本要求:
- 作曲家
- 混帳
- PHP代碼嗅探器
- drupal/編碼器:8.3.13
以下過程用於在 Mac 設備中安裝它。 您可以在此處找到參考鏈接,了解在其他設備上的安裝說明。
- brew 安裝 php-code-sniffer
- composer global 需要 drupal/coder:8.3.13
- phpcs --config-set installed_paths ~/.composer/vendor/drupal/coder/coder_sniffer
- phpcs -i → 會給你安裝編碼標準。
讓我們開始!
我正在創建一個名為demo的新 Drupal 項目。 您也可以在現有項目中使用它。

→ 使用 cd 命令我們進入了項目文件夾。
CD演示
→ 初始化 git 到項目中
初始化
→ 添加並進行我的第一次提交。
git commit -m “初始提交”

→ 使用以下命令為 Mac 安裝 php 代碼嗅探器。
brew 安裝 php-code-sniffer

→ 使用 composer 安裝 Drupal coder
composer global 需要 drupal/coder:8.3.13
→ 一旦定義了編碼器及其路徑,您將獲得如下輸出,如下圖所示。
phpcs --config-set installed_paths ~/.composer/vendor/drupal/coder/coder_sniffer
→ phpcs -i
上面的命令會給你 Drupal 和 DrupalPractice

→ 現在您可以提交代碼了。 如果您有任何語法或編碼標準錯誤,您將在顯示屏上收到通知,並且您的提交過程將被中止。

→ 下面是自動修復錯誤的代碼
phpcbf --standard=Drupal --extensions=php,module,inc,install,test,profile,theme,css,info,txt,md,yml web/modules/custom/demo
任何其他問題都需要手動修復。 完成後提交您的代碼。

一旦你的代碼是乾淨的,它將允許你提交代碼

只需將代碼複製並粘貼到 .git/hooks 中的 pre-commit.sample 中。 不要忘記刪除示例擴展。
預提交代碼示例:
#!/bin/bash # Redirect output to stderr. exec 1>&2 # Color codes for the error message. redclr=`tput setaf 1` greenclr=`tput setaf 2` blueclr=`tput setaf 4` reset=`tput sgr0` # Printing the notification in the display screen. echo "${blueclr}" echo "................................. Validating your codes ……..…………....." echo "-----------------------------------------------------------${reset}" # Mentioning the directories which should be excluded. dir_exclude='\/kint\/|\/contrib\/|\/devel\/|\/libraries\/|\/vendor\/|\.info$|\.png$|\.gif$|\.jpg$|\.ico$|\.patch$|\.htaccess$|\.sh$|\.ttf$|\.woff$|\.eot$|\.svg$' # Checking for the debugging keyword in the commiting code base. keywords=(ddebug_backtrace debug_backtrace dpm print_r var_dump dump console\.log) keywords_for_grep=$(printf "|%s" "${keywords[@]}") keywords_for_grep=${keywords_for_grep:1} # Flags for the counter. synatx_error_found=0 debugging_function_found=0 merge_conflict=0 coding_standard_error=0 # Checking for PHP syntax errors. changed_files=`git diff-index --diff-filter=ACMRT --cached --name-only HEAD -- | egrep '\.theme$|\.module$|\.inc|\.php$'` if [ -n "$changed_files" ] then for FILE in $changed_files; do php -l $FILE > /dev/null 2>&1 compiler_result=$? if [ $compiler_result -eq 255 ] then if [ $synatx_error_found -eq 0 ] then echo "${redclr}" echo "# Compilation error(s):" echo "=========================${reset}" fi synatx_error_found=1 `php -l $FILE > /dev/null` fi done fi # Checking for debugging functions. files_changed=`git diff-index --diff-filter=ACMRT --cached --name-only HEAD -- | egrep -v $dir_exclude` if [ -n "$files_changed" ] then for FILE in $files_changed ; do for keyword in "${keywords[@]}" ; do pattern="^\+(.*)?$keyword(.*)?" resulted_files=`git diff --cached $FILE | egrep -x "$pattern"` if [ ! -z "$resulted_files" ] then if [ $debugging_function_found -eq 0 ] then echo "${redclr}" echo "Validating keywords" echo "================================================${reset}" fi debugging_function_found=1 echo "Debugging function" $keyword git grep -n $keyword $FILE | awk '{split($0,a,":"); printf "\found in " a[1] " in line " a[2] "\n"; }' fi done done fi # Checking for Drupal coding standards changed_files=`git diff-index --diff-filter=ACMRT --cached --name-only HEAD -- | egrep -v $dir_exclude | egrep '\.php$|\.module$|\.inc$|\.install$|\.test$|\.profile$|\.theme$|\.js$|\.css$|\.info$|\.txt$|\.yml$'` if [ -n "$changed_files" ] then phpcs_result=`phpcs --standard=Drupal --extensions=php,module,inc,install,test,profile,theme,css,info,txt,md,yml --report=csv $changed_files` if [ "$phpcs_result" != "File,Line,Column,Type,Message,Source,Severity,Fixable" ] then echo "${redclr}" echo "# Hey Buddy, The hook found some issue(s)." echo "---------------------------------------------------------------------------------------------${reset}" phpcs --standard=Drupal --extensions=php,module,inc,install,test,profile,theme,css,info,txt,md,yml $changed_files echo "<=======> Run below command to fix the issue(s)" echo "# phpcbf --standard=Drupal --extensions=php,module,inc,install,test,profile,theme,css,info,txt,md,yml your_custom_module_or_file_path" echo “<====================================================>" echo "# To skip the Drupal Coding standard issue(s), Please use this commands << git commit -m your commit Message --no-verify >>" echo "-----------------------------------------------------------------------------------------------------------------------------------------${reset}" coding_standard_error=1 fi fi # Checking for merge conflict markers. files_changed=`git diff-index --diff-filter=ACMRT --cached --name-only HEAD --` if [ -n "$files_changed" ] then for FILE in $files_changed; do pattern="(<<<<|====|>>>>)+.*(\n)?" resulted_files=`egrep -in "$pattern" $FILE` if [ ! -z "$resulted_files" ] then if [ $merge_conflict -eq 0 ] then echo "${redclr}" echo "-----------------------Unable to commit the file(s):------------------------" echo "-----------------------------------${reset}" fi merge_conflict=1 echo $FILE fi done fi # Printing final result errors_found=$((synatx_error_found+debugging_function_found+merge_conflict+coding_standard_error)) if [ $errors_found -eq 0 ] then echo "${greenclr}" echo "Wow! It is clean code" echo "${reset}" else echo "${redclr}" echo "Please Correct the errors mentioned above. We are aborting your commit." echo "${reset}" exit 1 fi

最後的想法
我希望您發現這篇文章很有趣,並且它可以幫助您編寫更好的代碼,因為更好的代碼意味著更好的網絡! 喜歡您剛剛閱讀的內容? 考慮訂閱我們的每週時事通訊,並將像這樣的技術見解發送到您的收件箱!