From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <s.lendl@proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by lists.proxmox.com (Postfix) with ESMTPS id EC464C0CCC
 for <pbs-devel@lists.proxmox.com>; Fri, 12 Jan 2024 15:31:40 +0100 (CET)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id C663D330C8
 for <pbs-devel@lists.proxmox.com>; Fri, 12 Jan 2024 15:31:10 +0100 (CET)
Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com
 [94.136.29.106])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256)
 (No client certificate requested)
 by firstgate.proxmox.com (Proxmox) with ESMTPS
 for <pbs-devel@lists.proxmox.com>; Fri, 12 Jan 2024 15:31:07 +0100 (CET)
Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1])
 by proxmox-new.maurer-it.com (Proxmox) with ESMTP id D107349115
 for <pbs-devel@lists.proxmox.com>; Fri, 12 Jan 2024 15:31:06 +0100 (CET)
From: Stefan Lendl <s.lendl@proxmox.com>
To: pbs-devel@lists.proxmox.com
Date: Fri, 12 Jan 2024 15:30:26 +0100
Message-ID: <20240112143026.341414-1-s.lendl@proxmox.com>
X-Mailer: git-send-email 2.42.0
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-SPAM-LEVEL: Spam detection results:  0
 AWL 0.035 Adjusted score from AWL reputation of From: address
 BAYES_00                 -1.9 Bayes spam probability is 0 to 1%
 DMARC_MISSING             0.1 Missing DMARC policy
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
 T_SCC_BODY_TEXT_LINE    -0.01 -
Subject: [pbs-devel] [RFC proxmox-backup] git-hooks: pre-commit runs cargo
 fmt --check
X-BeenThere: pbs-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox Backup Server development discussion
 <pbs-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pbs-devel>, 
 <mailto:pbs-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pbs-devel/>
List-Post: <mailto:pbs-devel@lists.proxmox.com>
List-Help: <mailto:pbs-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel>, 
 <mailto:pbs-devel-request@lists.proxmox.com?subject=subscribe>
X-List-Received-Date: Fri, 12 Jan 2024 14:31:41 -0000

* 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>
---
 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..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