serene/scripts/pre-hook

45 lines
826 B
Plaintext
Raw Normal View History

#! /bin/bash
function get_changed_files() {
2021-09-16 18:00:24 +01:00
git diff --cached --name-status |egrep '*\.(cpp|h|hpp|cpp.inc|h.inc)'|awk '$1 != "D" { print $2 }'
}
2021-09-16 17:56:12 +01:00
function fix_includes(){
2021-09-16 18:00:24 +01:00
local files=$(get_changed_files)
2021-09-16 18:00:24 +01:00
if [[ "$files" ]];
then
2021-09-17 13:51:30 +01:00
echo "Fixing '#include' syntax...."
2021-09-17 13:49:55 +01:00
sed -i -E '/^#include "(serene|\.)/!s/^.include \"(.*)\"/#include <\1>/g' $files
2021-09-16 18:00:24 +01:00
fi
}
function clang_format_staged() {
2021-09-16 18:00:24 +01:00
local files=$(get_changed_files)
if [[ "$files" ]];
then
for file in $(get_changed_files)
do
2021-09-17 13:51:30 +01:00
echo "Reformatting $file..."
2021-09-16 18:00:24 +01:00
clang-format -i $file
done
fi
}
function git_add_changes() {
local files=$(get_changed_files)
if [[ "$files" ]];
then
2021-09-17 13:49:55 +01:00
git add $files
fi
}
fix_includes
clang_format_staged
git_add_changes