Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ src/config
# codespell dictionary
scripts/dictionary/

# time marker for pre-commit hook to check if staged files have been formatted
scripts/.last_format_time

# clangd for nvim lsp
src/.clangd

Expand Down
2 changes: 2 additions & 0 deletions environment_setup/setup_software.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
CURR_DIR=$(dirname -- "$(readlink -f -- "$BASH_SOURCE")")
cd "$CURR_DIR" || exit

git config core.hooksPath "$CURR_DIR/../scripts/githooks"

source util.sh

g_arch=$(uname -m) # Global variable. No function should use this name.
Expand Down
16 changes: 16 additions & 0 deletions scripts/githooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/sh

filter_formattable_files() {
Comment thread
Apeiros-46B marked this conversation as resolved.
grep -E '\.(h|cpp|c|cc|hpp|tpp|proto|py|md|yml)$|/BUILD$'
}

toplevel="$(git rev-parse --show-toplevel)"
mtime=$(stat -c %Y "$toplevel/scripts/.last_format_time" 2> /dev/null || echo 0)

git diff --staged --name-only | filter_formattable_files | while read -r file; do
if [ $mtime -lt "$(stat -c %Y "$toplevel/$file")" ]; then
# last format invocation was before staged changes
echo "Please run $toplevel/scripts/lint_and_format.sh before committing!" 1>&2
exit 1
fi
done
3 changes: 3 additions & 0 deletions scripts/lint_and_format.sh
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,7 @@ run_eof_new_line
run_git_diff_check
run_ansible_lint

# Update time marker so pre-commit git hook knows that code has been formatted
touch "$CURR_DIR/.last_format_time"

exit 0
Loading
Loading