Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ repos:
language: node
additional_dependencies: ["prettier@3.7.4"]
stages: [manual]
- id: check-makefile-indentation
name: check Makefiles are indented with tabs
description: ensures that Makefiles are indented with tabs
entry: ./scripts/check_makefiles_for_tabs.sh
language: system
files: "(?i)^makefile$"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The regex (?i)^makefile$ will only match a file named Makefile or makefile in the root of the repository. If you ever add Makefiles in subdirectories, they will not be checked. A more flexible pattern would be something like (?i)makefile$, which would match any file ending in makefile (e.g., src/Makefile). Consider if you want to support this for future-proofing.

pass_filenames: true # <-- Crucial change: pass filenames to the script
types: [file] # Ensure only regular files are passed, not directories
stages: [manual]
- id: check-zip-file-is-not-committed
name: disallow zip files
description: Zip files are not allowed in the repository
Expand Down
13 changes: 13 additions & 0 deletions scripts/check_makefiles_for_tabs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

# Iterate over all files passed as arguments by pre-commit
for makefile in "$@"; do
# Check if the file exists and is a regular file
if [[ -f "$makefile" ]]; then
if grep -P '^\s' "$makefile" | grep -vP '^\t' > /dev/null; then
echo "Error: File '$makefile' contains spaces at the beginning of lines instead of tabs."
exit 1
fi
fi
Comment on lines +6 to +11

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

This script has a couple of issues:

  1. Portability: The use of grep -P for Perl-compatible regular expressions is a GNU extension and is not available on all systems, notably default macOS installations. This will cause the hook to fail for Mac users.
  2. Redundancy & Efficiency: The if [[ -f "$makefile" ]] check is redundant because pre-commit is configured with types: [file], which ensures only existing files are passed. Additionally, using a pipe with two grep processes is less efficient than a single grep.

You can address these points by using a single, more portable grep command to check for lines starting with a space. The [ ] syntax for a space is used for clarity and portability.

Suggested change
if [[ -f "$makefile" ]]; then
if grep -P '^\s' "$makefile" | grep -vP '^\t' > /dev/null; then
echo "Error: File '$makefile' contains spaces at the beginning of lines instead of tabs."
exit 1
fi
fi
if grep -q '^[ ]' "$makefile"; then
echo "Error: File '$makefile' contains spaces at the beginning of lines instead of tabs."
exit 1
fi

done
exit 0
Loading