File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -150,8 +150,8 @@ src/config
150150# codespell dictionary
151151scripts /dictionary /
152152
153- # time marker for pre-commit hook to check if staged files have been formatted
154- scripts /.last_format_time
153+ # marker files for pre-commit and pre-push hooks to check if staged files have been formatted
154+ scripts /.format_markers /
155155
156156# clangd for nvim lsp
157157src /.clangd
Original file line number Diff line number Diff line change @@ -5,12 +5,15 @@ filter_formattable_files() {
55}
66
77toplevel=" $( git rev-parse --show-toplevel) "
8- mtime=$( stat -c %Y " $toplevel /scripts/.last_format_time" 2> /dev/null || echo 0)
8+ marker=" $toplevel /scripts/.format_markers/$( git rev-parse --abbrev-ref HEAD) "
9+ mtime=$( stat -c %Y " $marker " 2> /dev/null || echo 0)
910
1011git diff --staged --name-only | filter_formattable_files | while read -r file; do
1112 if [ $mtime -lt " $( stat -c %Y " $toplevel /$file " ) " ]; then
12- # last format invocation was before staged changes
13- echo " Please run $toplevel /scripts/lint_and_format.sh before committing!" 1>&2
14- exit 1
13+ # The file was modified AFTER formatting. Remove the marker to tell the pre-push hook
14+ # that this commit introduces unformatted code.
15+ rm -f " $marker " 2> /dev/null
16+ echo " Warning: Code is unformatted."
17+ break
1518 fi
1619done
Original file line number Diff line number Diff line change 1+ #! /usr/bin/env bash
2+
3+ confirm () {
4+ read -r -p " $1 [y/N] " response < /dev/tty
5+ case " $response " in
6+ [yY][eE][sS]|[yY])
7+ return 0
8+ ;;
9+ * )
10+ return 1
11+ ;;
12+ esac
13+ }
14+
15+ scriptsdir=" $( git rev-parse --show-toplevel) /scripts"
16+
17+ if ! [ -f " $scriptsdir /.format_markers/$( git rev-parse --abbrev-ref HEAD) " ]; then
18+ echo " Warning: Code is unformatted. Please run $scriptsdir /lint_and_format.sh."
19+ if confirm ' Continue without formatting?' ; then
20+ echo " Continuing push anyway."
21+ exit 0
22+ else
23+ echo " Aborting push."
24+ exit 1
25+ fi
26+ fi
27+
28+ # vim:ts=4 et
Original file line number Diff line number Diff line change @@ -149,7 +149,10 @@ run_eof_new_line
149149run_git_diff_check
150150run_ansible_lint
151151
152- # Update time marker so pre-commit git hook knows that code has been formatted
153- touch " $CURR_DIR /.last_format_time"
152+ # Update markers, telling Git hooks that formatting has been done
153+ # (Per-branch, so switching branches doesn't confuse the hooks)
154+ branch=" $( git rev-parse --abbrev-ref HEAD) "
155+ mkdir -p " $CURR_DIR /.format_markers/$( dirname " $branch " ) "
156+ touch " $CURR_DIR /.format_markers/${branch} "
154157
155158exit 0
You can’t perform that action at this time.
0 commit comments