]> git.ipfire.org Git - thirdparty/git.git/commitdiff
ci: check formatting of our Rust code
authorPatrick Steinhardt <ps@pks.im>
Wed, 15 Oct 2025 06:04:06 +0000 (08:04 +0200)
committerJunio C Hamano <gitster@pobox.com>
Wed, 15 Oct 2025 15:10:17 +0000 (08:10 -0700)
Introduce a CI check that verifies that our Rust code is well-formatted.
This check uses `cargo fmt`, which is a wrapper around rustfmt(1) that
executes formatting for all Rust source files. rustfmt(1) itself is the
de-facto standard for formatting code in the Rust ecosystem.

The rustfmt(1) tool allows to tweak the final format in theory. In
practice though, the Rust ecosystem has aligned on style "editions".
These editions only exist to ensure that any potential changes to the
style don't cause reformats to existing code bases. Other than that,
most Rust projects out there accept this default style of a specific
edition.

Let's do the same and use that default style. It may not be anyone's
favorite, but it is consistent and by making it part of our CI we also
enforce it right from the start.

Note that we don't have to pick a specific style edition here, as the
edition is automatically derived from the edition we have specified in
our "Cargo.toml" file.

The implemented script looks somewhat weird as we perfom manual error
handling instead of using something like `set -e`. The intent here is
that subsequent commits will add more checks, and we want to execute all
of these checks regardless of whether or not a previous check failed.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
.github/workflows/main.yml
.gitlab-ci.yml
ci/install-dependencies.sh
ci/run-rust-checks.sh [new file with mode: 0755]

index 393ea4d1ccf784ce6247c55abf8a75888b00fb46..9e36b5c5e3e360a103cae7bd1cbc9afe2df73feb 100644 (file)
@@ -458,6 +458,21 @@ jobs:
     - run: ci/install-dependencies.sh
     - run: ci/run-static-analysis.sh
     - run: ci/check-directional-formatting.bash
+  rust-analysis:
+    needs: ci-config
+    if: needs.ci-config.outputs.enabled == 'yes'
+    env:
+      jobname: RustAnalysis
+      CI_JOB_IMAGE: ubuntu:rolling
+    runs-on: ubuntu-latest
+    container: ubuntu:rolling
+    concurrency:
+      group: rust-analysis-${{ github.ref }}
+      cancel-in-progress: ${{ needs.ci-config.outputs.skip_concurrent == 'yes' }}
+    steps:
+    - uses: actions/checkout@v4
+    - run: ci/install-dependencies.sh
+    - run: ci/run-rust-checks.sh
   sparse:
     needs: ci-config
     if: needs.ci-config.outputs.enabled == 'yes'
index f7d57d1ee965281247f3841ff9382b22b7d7aa21..a47d839e39abca425fe8018a0beaf8ef68a23448 100644 (file)
@@ -212,6 +212,17 @@ static-analysis:
     - ./ci/run-static-analysis.sh
     - ./ci/check-directional-formatting.bash
 
+rust-analysis:
+  image: ubuntu:rolling
+  stage: analyze
+  needs: [ ]
+  variables:
+    jobname: RustAnalysis
+  before_script:
+    - ./ci/install-dependencies.sh
+  script:
+    - ./ci/run-rust-checks.sh
+
 check-whitespace:
   image: ubuntu:latest
   stage: analyze
index 645d03525044c9b1ed8af38d4907561324498fc3..a24b07edff83ac901c686d7c04ee6d228e90c74b 100755 (executable)
@@ -126,6 +126,11 @@ StaticAnalysis)
        sudo apt-get -q -y install coccinelle libcurl4-openssl-dev libssl-dev \
                libexpat-dev gettext make
        ;;
+RustAnalysis)
+       sudo apt-get -q -y install rustup
+       rustup default stable
+       rustup component add rustfmt
+       ;;
 sparse)
        sudo apt-get -q -y install libssl-dev libcurl4-openssl-dev \
                libexpat-dev gettext zlib1g-dev sparse
diff --git a/ci/run-rust-checks.sh b/ci/run-rust-checks.sh
new file mode 100755 (executable)
index 0000000..082eb52
--- /dev/null
@@ -0,0 +1,12 @@
+#!/bin/sh
+
+. ${0%/*}/lib.sh
+
+set +x
+
+if ! group "Check Rust formatting" cargo fmt --all --check
+then
+       RET=1
+fi
+
+exit $RET