[pbs-devel] [RFC proxmox-backup] git-hooks: pre-commit runs cargo fmt --check

Stefan Lendl s.lendl at proxmox.com
Fri Jan 12 15:30:26 CET 2024


* add a pre-commit hook that declines commiting if cargo fmt would make
  formatting changes.
* `make install-git-hook` installs the hook
* `make build` installs the hook
* `make uninstall-git-hook` removes the hook
* `make lint` runs cargo fmt --check in addition to cargo clippy

Signed-off-by: Stefan Lendl <s.lendl at proxmox.com>
---
 Makefile             |  9 ++++++++-
 git-hooks/pre-commit | 13 +++++++++++++
 2 files changed, 21 insertions(+), 1 deletion(-)
 create mode 100755 git-hooks/pre-commit

diff --git a/Makefile b/Makefile
index 0317dd5e..fc866872 100644
--- a/Makefile
+++ b/Makefile
@@ -86,7 +86,7 @@ doc:
 
 # always re-create this dir
 .PHONY: build
-build:
+build: install-git-hooks
 	rm -rf build
 	mkdir build
 	git rev-parse HEAD > build/.repoid
@@ -188,6 +188,7 @@ $(COMPILED_BINS) $(COMPILEDIR)/dump-catalog-shell-cli $(COMPILEDIR)/docgen: .do-
 
 .PHONY: lint
 lint:
+	cargo fmt --all -- --check
 	cargo clippy -- -A clippy::all -D clippy::correctness
 
 install: $(COMPILED_BINS)
@@ -221,3 +222,9 @@ upload: $(SERVER_DEB) $(CLIENT_DEB) $(RESTORE_DEB) $(DOC_DEB)
 	  | ssh -X repoman at repo.proxmox.com upload --product pbs --dist $(UPLOAD_DIST)
 	tar cf - $(CLIENT_DEB) $(CLIENT_DBG_DEB) | ssh -X repoman at repo.proxmox.com upload --product "pve,pmg,pbs-client" --dist $(UPLOAD_DIST)
 	tar cf - $(RESTORE_DEB) $(RESTORE_DBG_DEB) | ssh -X repoman at repo.proxmox.com upload --product "pve" --dist $(UPLOAD_DIST)
+
+install-git-hooks:
+	ln -sn ../../git-hooks/pre-commit .git/hooks/pre-commit > /dev/null 2>&1 || true
+
+uninstall-git-hooks:
+	rm -f .git/hooks/pre-commit
diff --git a/git-hooks/pre-commit b/git-hooks/pre-commit
new file mode 100755
index 00000000..729698ad
--- /dev/null
+++ b/git-hooks/pre-commit
@@ -0,0 +1,13 @@
+#!/usr/bin/env bash
+
+# Run cargo fmt before a commit to ensure the format style
+
+cargo fmt --all -- --check > /dev/null 2>&1
+res=$?
+
+if [[ $res != 0 ]]; then
+    echo "git pre-commit hook: There are some code style issues, run \`cargo fmt\` first."
+	exit 1
+fi
+
+exit 0
-- 
2.42.0





More information about the pbs-devel mailing list