Skip to content

Commit 73a2773

Browse files
committed
Add mac support for pre-commit hook
1 parent 9e998b2 commit 73a2773

1 file changed

Lines changed: 15 additions & 2 deletions

File tree

scripts/githooks/pre-commit

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,27 @@ filter_formattable_files() {
55
grep -E '\.(h|cpp|c|cc|hpp|tpp|proto|py|md|yml)$|/BUILD$'
66
}
77

8+
# Output the modification time of a file (or 0 if nonexistent)
9+
get_mtime() {
10+
case "$(uname -s)" in
11+
Darwin*)
12+
stat -f %m "$1" 2> /dev/null || echo 0
13+
;;
14+
*)
15+
# No one will run this on BSD or other Unixes, so this should be fine
16+
stat -c %Y "$1" 2> /dev/null || echo 0
17+
;;
18+
esac
19+
}
20+
821
# Find repo root, marker for the current branch, and marker mtime (0 if marker does not exist)
922
toplevel="$(git rev-parse --show-toplevel)"
1023
marker="$toplevel/scripts/.format_markers/$(git rev-parse --abbrev-ref HEAD)"
11-
mtime=$(stat -c %Y "$marker" 2> /dev/null || echo 0)
24+
mtime=$(get_mtime "$marker")
1225

1326
git diff --staged --name-only | filter_formattable_files | while read -r file; do
1427
# Compare marker mtime with file mtime
15-
if [ $mtime -lt "$(stat -c %Y "$toplevel/$file")" ]; then
28+
if [ $mtime -lt $(get_mtime "$toplevel/$file") ]; then
1629
# The file was modified AFTER formatting. Remove the marker to tell the pre-push hook
1730
# that this commit introduces unformatted code.
1831
rm -f "$marker" 2> /dev/null

0 commit comments

Comments
 (0)