public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
* [pbs-devel] [RFC v2 proxmox-backup] git-hooks: pre-commit runs cargo fmt --check
@ 2024-01-12 14:43 Stefan Lendl
  2024-01-15  7:27 ` Thomas Lamprecht
  0 siblings, 1 reply; 5+ messages in thread
From: Stefan Lendl @ 2024-01-12 14:43 UTC (permalink / raw)
  To: pbs-devel

* 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@proxmox.com>
Suggested-by: Lukas Wagner <l.wagner@proxmox.com>
---

Notes:
    Changes from v1:
    
    * Fix indentation style in hook *sigh*

 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@repo.proxmox.com upload --product pbs --dist $(UPLOAD_DIST)
 	tar cf - $(CLIENT_DEB) $(CLIENT_DBG_DEB) | ssh -X repoman@repo.proxmox.com upload --product "pve,pmg,pbs-client" --dist $(UPLOAD_DIST)
 	tar cf - $(RESTORE_DEB) $(RESTORE_DBG_DEB) | ssh -X repoman@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..6cd0b3bf
--- /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





^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2024-01-15 12:46 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-01-12 14:43 [pbs-devel] [RFC v2 proxmox-backup] git-hooks: pre-commit runs cargo fmt --check Stefan Lendl
2024-01-15  7:27 ` Thomas Lamprecht
2024-01-15  9:28   ` Stefan Lendl
2024-01-15  9:42     ` Thomas Lamprecht
2024-01-15 12:46       ` Lukas Wagner

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal