Skip to content

Instantly share code, notes, and snippets.

@Pzixel
Last active February 7, 2018 14:12
Show Gist options
  • Save Pzixel/b10969a02c6c96cf5fc3d30573312b1e to your computer and use it in GitHub Desktop.
Save Pzixel/b10969a02c6c96cf5fc3d30573312b1e to your computer and use it in GitHub Desktop.
Setups git hooks for rustfmt and clang-format
#!/bin/sh
rustup component add rustfmt-preview
rustfmt_path=`which rustfmt`
echo "#!/bin/sh
declare -a rust_files=()
declare -a cpp_files=()
files=\$(git diff-index --name-only HEAD)
echo 'Formatting source files'
for file in \$files; do
if [ ! -f \"\${file}\" ]; then
continue
fi
if [[ \"\${file}\" == *.rs ]]; then
rust_files+=(\"\${file}\")
fi
if [[ \"\${file}\" =~ (\.h|\.cpp|\.cc) ]]; then
cpp_files+=(\"\${file}\")
fi
done
if [ \${#rust_files[@]} -ne 0 ]; then
$rustfmt_path \${rust_files[@]} &
fi
if [ \${#cpp_files[@]} -ne 0 ]; then
clang-format -i \${cpp_files[@]} &
fi
wait
changed_files=(\"\${rust_files[@]}\" \"\${cpp_files[@]}\")
if [ \${#changed_files[@]} -ne 0 ]; then
git add \${changed_files[@]}
echo \"Formatting done, changed files: \${changed_files[@]}\"
else
echo \"No changes, formatting skipped\"
fi" > .git/hooks/pre-commit
chmod +x .git/hooks/pre-commit
echo "Hooks updated"
@RedwanFox
Copy link

В слове hookers в последней строке опечатка

@bmusin
Copy link

bmusin commented Feb 6, 2018

Может лучше переписать на скриптовом языке?

@Pzixel
Copy link
Author

Pzixel commented Feb 7, 2018

@bmusin на каком? Намекаю, что это еще и на винде должно работать. Того же питона на ней из коробки нет.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment