setup_pre_commit.sh
is a helper script designed to set up a Git pre-commit
hook for your repository. The pre-commit
hook ensures that every commit is checked for code formatting and that the README.md
file is updated with the latest project structure.
The script creates or overwrites a pre-commit
hook in your Git repository. The hook performs the following actions before every commit:
- Formats the Code: Runs
make format
to ensure all source files are properly formatted. - Updates
README.md
: Runsmake update_readme
to regenerate the project structure section in theREADME.md
. - Stages the Updated
README.md
: Automatically stages changes to theREADME.md
file.
-
Save the Script
Save the script assetup_pre_commit.sh
in the directoryscripts/setup_precommit/
of your repository. -
Make the Script Executable
Run the following command to make the script executable:chmod +x scripts/setup_precommit/setup_pre_commit.sh
-
Run the Script
Execute the script to set up thepre-commit
hook:scripts/setup_precommit/setup_pre_commit.sh
-
Verify the Hook
Check that thepre-commit
hook exists in.git/hooks/
:ls .git/hooks/pre-commit
- A valid
Makefile
in the root of the repository with the following targets:format
: Ensures code formatting.update_readme
: Updates the project structure in theREADME.md
.
- Git must be initialized in the repository (
git init
).
-
Script Fails to Navigate to the Repository Root
Ensure the script is placed inscripts/setup_precommit/
and your repository root contains a.git
directory. -
The Hook Does Not Trigger
Make sure thepre-commit
hook is executable:chmod +x .git/hooks/pre-commit
-
Make Commands Not Found
Verify that yourMakefile
includes theformat
andupdate_readme
targets. -
Existing Pre-Commit Hook
This script overwrites any existingpre-commit
hook. If you have other functionality in your hook, consider appending instead of overwriting.
- The
pre-commit
hook is local to each developer's environment and is not automatically shared with others. For shared configurations, consider using tools like pre-commit. - Use this script to standardize pre-commit behavior across your team by sharing it in your repository.