-
Notifications
You must be signed in to change notification settings - Fork 124
Git hook to prevent unformatted code from being committed #3502
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
dfe3243
Git hook to prevent unformatted code from being committed
Apeiros-46B 8df5130
[pre-commit.ci lite] apply automatic fixes
pre-commit-ci-lite[bot] 6d64319
Re-add requirements_lock.txt
Apeiros-46B d59c4a0
Use pre-push hook instead of pre-commit hook
Apeiros-46B a7db072
Add explanatory comments in bash scripts
Apeiros-46B 9a654e5
Merge branch 'master' into fmt-githook
Apeiros-46B 9e998b2
Merge branch 'UBC-Thunderbots:master' into fmt-githook
Apeiros-46B 73a2773
Add mac support for pre-commit hook
Apeiros-46B File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,35 @@ | ||
| #!/bin/sh | ||
|
|
||
| # From the list of files on stdin (separated by newline), return formattable files to stdout | ||
| filter_formattable_files() { | ||
| grep -E '\.(h|cpp|c|cc|hpp|tpp|proto|py|md|yml)$|/BUILD$' | ||
| } | ||
|
|
||
| # Output the modification time of a file (or 0 if nonexistent) | ||
| get_mtime() { | ||
| case "$(uname -s)" in | ||
| Darwin*) | ||
| stat -f %m "$1" 2> /dev/null || echo 0 | ||
| ;; | ||
| *) | ||
| # No one will run this on BSD or other Unixes, so this should be fine | ||
| stat -c %Y "$1" 2> /dev/null || echo 0 | ||
| ;; | ||
| esac | ||
| } | ||
|
|
||
| # Find repo root, marker for the current branch, and marker mtime (0 if marker does not exist) | ||
| toplevel="$(git rev-parse --show-toplevel)" | ||
| marker="$toplevel/scripts/.format_markers/$(git rev-parse --abbrev-ref HEAD)" | ||
| mtime=$(get_mtime "$marker") | ||
|
|
||
| git diff --staged --name-only | filter_formattable_files | while read -r file; do | ||
| # Compare marker mtime with file mtime | ||
| if [ $mtime -lt $(get_mtime "$toplevel/$file") ]; then | ||
| # The file was modified AFTER formatting. Remove the marker to tell the pre-push hook | ||
| # that this commit introduces unformatted code. | ||
| rm -f "$marker" 2> /dev/null | ||
| echo "Warning: Code is unformatted." | ||
| break | ||
| fi | ||
| done | ||
This file contains hidden or 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,28 @@ | ||
| #!/usr/bin/env bash | ||
|
|
||
| # Prompt user for confirmation at a [y/N] prompt and return 0 when user responds "yes" or "y" | ||
| confirm() { | ||
Apeiros-46B marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| read -r -p "$1 [y/N] " response < /dev/tty | ||
| case "$response" in | ||
| [yY][eE][sS]|[yY]) | ||
| return 0 | ||
| ;; | ||
| *) | ||
| return 1 | ||
| ;; | ||
| esac | ||
| } | ||
|
|
||
| scriptsdir="$(git rev-parse --show-toplevel)/scripts" | ||
|
|
||
| # If marker for current branch does not exist | ||
| if ! [ -f "$scriptsdir/.format_markers/$(git rev-parse --abbrev-ref HEAD)" ]; then | ||
| echo "Warning: Code is unformatted. Please run $scriptsdir/lint_and_format.sh." | ||
| if confirm 'Continue without formatting?'; then | ||
| echo "Continuing push anyway." | ||
| exit 0 | ||
| else | ||
| echo "Aborting push." | ||
| exit 1 | ||
| fi | ||
| fi | ||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.