-
Notifications
You must be signed in to change notification settings - Fork 446
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c8be2be
commit c7daa80
Showing
5 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# Git Hooks | ||
|
||
This document aims to guide you through the benefits and utilities of implementing Git Hooks in your | ||
development workflow | ||
|
||
## Introduction | ||
|
||
Git hooks are scripts that Git executes before or after events such as `commit` or `push`. This | ||
document discusses the benefits of using a `push` hook for Fleet Developers. | ||
|
||
## Benefits | ||
|
||
### Reduced Waiting Time | ||
|
||
Imagine pushing a commit and then realizing that there was a minor issue (ie. `make | ||
lint-go`), forcing you to restart the entire CI process, including tests that can take up to | ||
~30 minutes to | ||
complete | ||
|
||
### Streamlined Workflow | ||
|
||
Reduce the feedback loop and aid rapid development | ||
|
||
### Saving CI Resources | ||
|
||
By reducing the number of failed builds, you free up CI resources for other tasks | ||
|
||
## Getting Started | ||
|
||
1. Copy the `pre-push` to your fleet repo hooks directory | ||
|
||
```bash | ||
cp ./git-hooks/backend/setup/pre-push ./.git/hooks/ | ||
chmod +x ./.git/hooks/pre-push | ||
``` | ||
|
||
2. Edit the `pre-push` file and specify the scripts you want to run. Filenames must match scripts in the | ||
`./git-hooks/backend/hooks/` directory. This also specifies the order they run. | ||
|
||
```bash | ||
declare -a USED_HOOKS=( | ||
"compile-go" | ||
"db-schema" | ||
"lint-go" | ||
) | ||
``` | ||
|
||
## Contributing | ||
|
||
All related code is located in the `./git-hooks` directory | ||
|
||
Scripts in the `hooks/` directory need to exit a non-zero code in order to fail properly. These | ||
scripts do not need to be executable because we are calling it from the `pre-push` script as `bash | ||
$SCRIPT_NAME` | ||
|
||
Make sure you promote your changes in Slack! | ||
|
||
|
||
## Contributing Ideas | ||
|
||
- Update/Add a script to the `hooks` directory | ||
- Build out `./git-hooks/frontend/` | ||
- ?????? | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/bin/bash | ||
|
||
make |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#!/bin/bash | ||
|
||
## DB Schema Dump | ||
if [[ $DB_SCHEMA_DUMP ]]; then | ||
make dump-test-schema | ||
if [[ $(git diff server/datastore/mysql/schema.sql) ]]; then | ||
echo "❌ fail: uncommited changes in schema.sql" | ||
echo "please run `make dump-test-schema` and commit the changes" | ||
exit 1 | ||
fi | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/bin/bash | ||
|
||
make lint-go |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#!/bin/bash | ||
|
||
# Move this file to the .git/hooks directory of the project | ||
|
||
# Define the directory containing the hooks | ||
HOOK_DIRECTORY="./git-hooks/backend/hooks" | ||
|
||
# Define the list of hooks you want to execute | ||
declare -a USED_HOOKS=( | ||
"compile-go" | ||
"db-schema" | ||
"lint-go" | ||
) | ||
|
||
# Iterate over all files in the directory | ||
for SCRIPT in "$HOOK_DIRECTORY"/*; do | ||
# Extract just the filename from the path | ||
FILENAME=$(basename "$SCRIPT") | ||
|
||
# Check if the filename is in the list | ||
for TARGET in "${USED_HOOKS[@]}"; do | ||
if [[ "$FILENAME" == "$TARGET" ]]; then | ||
# Execute the script, even if it doesn't have execute permissions | ||
bash "$SCRIPT" | ||
if [[ $? -ne 0 ]]; then | ||
# Exit if script fails | ||
exit 1 | ||
fi | ||
break # Break inner loop since script was found and executed | ||
fi | ||
done | ||
done |