diff mbox series

[4/4] Add script to set up running rustfmt on commit

Message ID 20180712094945.8368-4-ruscur@russell.cc
State Accepted
Headers show
Series None | expand

Commit Message

Russell Currey July 12, 2018, 9:49 a.m. UTC
Signed-off-by: Russell Currey <ruscur@russell.cc>
---
 setup_rustfmt_hook.sh | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)
 create mode 100755 setup_rustfmt_hook.sh
diff mbox series

Patch

diff --git a/setup_rustfmt_hook.sh b/setup_rustfmt_hook.sh
new file mode 100755
index 0000000..76a8932
--- /dev/null
+++ b/setup_rustfmt_hook.sh
@@ -0,0 +1,31 @@ 
+#!/bin/sh
+rustup component add rustfmt-preview
+
+rustfmt_path=`which rustfmt`
+echo "#!/bin/bash
+declare -a rust_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
+done
+if [ \${#rust_files[@]} -ne 0  ]; then
+     $rustfmt_path \${rust_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"