Skip to content

Commit d59c4a0

Browse files
committed
Use pre-push hook instead of pre-commit hook
1 parent 6d64319 commit d59c4a0

4 files changed

Lines changed: 42 additions & 8 deletions

File tree

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@ src/config
150150
# codespell dictionary
151151
scripts/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
157157
src/.clangd

scripts/githooks/pre-commit

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@ filter_formattable_files() {
55
}
66

77
toplevel="$(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

1011
git 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
1619
done

scripts/githooks/pre-push

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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

scripts/lint_and_format.sh

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,10 @@ run_eof_new_line
149149
run_git_diff_check
150150
run_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

155158
exit 0

0 commit comments

Comments
 (0)