From adb702f3c36ee19b43805b7cb27a5b97d9653ecc Mon Sep 17 00:00:00 2001 From: welpo Date: Wed, 16 Aug 2023 23:56:42 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix:=20`is=5Fdraft`=20returning?= =?UTF-8?q?=20opposite=20value?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ✨ feat: avoid commiting files with "TODO" ♻️ refactor: loop over all files, not just markdown and png. Useful to catch TODOs in Tera templates, for example. --- .githooks/pre-commit | 43 +++++++++++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/.githooks/pre-commit b/.githooks/pre-commit index 8e6646f..2468796 100755 --- a/.githooks/pre-commit +++ b/.githooks/pre-commit @@ -1,18 +1,18 @@ #!/usr/bin/env bash -################################################################################# -# This script is run by git before a commit is made. # -# To use it, copy it to .git/hooks/pre-commit and make it executable. # -# Alternatively, run the following command from the root of the repo: # -# git config core.hooksPath .githooks # -# # -# FEATURES # -# Updates the "updated" field in the front matter of .md files. # -# Compresses PNG files with either oxipng or optipng if available. # -# Runs subset_font if config.toml has been modified. # -# Aborts the commit if a draft .md file is being committed. # -# Updates the README if the line numbers for the language section have changed. # -################################################################################# +################################################################################### +# This script is run by git before a commit is made. # +# To use it, copy it to .git/hooks/pre-commit and make it executable. # +# Alternatively, run the following command from the root of the repo: # +# git config core.hooksPath .githooks # +# # +# FEATURES # +# Updates the "updated" field in the front matter of .md files. # +# Compresses PNG files with either oxipng or optipng if available. # +# Runs subset_font if config.toml has been modified. # +# Aborts the commit if a draft .md file or a file with "TODO" is being committed. # +# Updates the README if the line numbers for the language section have changed. # +################################################################################### # Function to exit the script with an error message. function error_exit() { @@ -30,7 +30,13 @@ function extract_date() { # Function to check if the .md file is a draft. function is_draft() { local file="$1" - awk '/^\+\+\+$/,/^\+\+\+$/{if(/draft = true/){exit 1}}' "$file" + awk '/^\+\+\+$/,/^\+\+\+$/ { if(/draft = true/) exit 0 } END { exit 1 }' "$file" +} + +# Check if the file contains "TODO". +function contains_todo() { + local file="$1" + grep -q "TODO" "$file" } # Check if the script is being run from the root of the repo. @@ -53,8 +59,8 @@ fi # Interrupt the commit if a draft .md file is being committed. # ################################################################## -# Get the newly added and modified .md and .png files, ignoring "_index.md" files. -all_changed_files=$(git diff --cached --name-only --diff-filter=AM | grep -E '\.(md|png)$' | grep -v '_index.md$') +# Get the newly added and modified files. +all_changed_files=$(git diff --cached --name-only --diff-filter=AM) # Loop through each newly added or modified .md or .png file for PNG optimization and draft checking. for file in $all_changed_files; do @@ -71,6 +77,11 @@ for file in $all_changed_files; do error_exit "Draft file $file is being committed!" fi fi + + # If the file contains "TODO", abort the commit. + if contains_todo "$file"; then + error_exit "File $file contains TODO! Remove or complete the TODO before committing." + fi done # Get the modified .md to update the "updated" field in the front matter.