Skip to content

Commit

Permalink
🐛 fix: is_draft returning opposite value
Browse files Browse the repository at this point in the history
✨ 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.
  • Loading branch information
welpo committed Aug 16, 2023
1 parent fe872a7 commit adb702f
Showing 1 changed file with 27 additions and 16 deletions.
43 changes: 27 additions & 16 deletions .githooks/pre-commit
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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.
Expand All @@ -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
Expand All @@ -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.
Expand Down

0 comments on commit adb702f

Please sign in to comment.