From 18847a4acb05d00d9069f35493b5d76a9662ba3d Mon Sep 17 00:00:00 2001 From: Steve Lau Date: Sat, 7 Oct 2023 14:32:31 +0800 Subject: [PATCH 1/3] chore: move cargo fmt to pre-commit script --- .cirrus.yml | 9 -------- CONTRIBUTING.md | 55 ++++++++++++++++++++++++++++++++++++++++++++++++- pre-commit.sh | 36 ++++++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+), 10 deletions(-) create mode 100755 pre-commit.sh diff --git a/.cirrus.yml b/.cirrus.yml index fc564e65aa..bbeb4ac2b4 100644 --- a/.cirrus.yml +++ b/.cirrus.yml @@ -359,12 +359,3 @@ task: check_script: - cargo check before_cache_script: rm -rf $CARGO_HOME/registry/index - -# Tasks that checks if the code is formatted right using `cargo fmt` tool -task: - name: Rust Formatter - container: - image: rust:latest - cpu: 1 - setup_script: rustup component add rustfmt - test_script: cargo fmt --all -- --check **/*.rs diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index a45fc9905c..97b0517301 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -60,10 +60,50 @@ pull' model described there. Please make pull requests against the `master` branch. +## Check your code format + +We use [git pre-commit hook](https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks) +to automate code formatting. However, you need to manually link our script to the correct +location first, assume you are in the root of the nix project: + +```sh +ln -s ../../pre-commit.sh .git/hooks/pre-commit +``` + +Let's add some a commit: + +```shell +$ git add {CHANGES} +$ git commit -m "Some commit message" +``` + +If your code is not formatted, then you will see a message like this: + +```shell +INFO: Checking code format... +WARN: Unformatted code detected +INFO: Formatting... +FAIL: git commit ABORTED, please have a look and run git add/commit again +``` + +Our `pre-commit` script will find code that is not formatted and format it for +you, your commit operation will be aborted, add the changes and commit again: + +```shell +$ git add {CHANGES_MADE_BY_THE_FORMATTER} +$ git commit -m "Some commit message" +INFO: Checking code format... +INFO: Check passed +``` +Since the code has been formatted by the formatter, the check will pass and you +are good to go! + +## CHANGELOG + If you change the API by way of adding, removing or changing something or if you fix a bug, please add an appropriate note, every note should be a new markdown file under the [changelog directory][cl] stating the change made by your pull request, -the filename should be in the following foramt: +the filename should be in the following format: ``` ..md @@ -109,6 +149,19 @@ possible! Other tests cannot be run under QEMU, which is used for some architectures. To skip them, add a `#[cfg_attr(qemu, ignore)]` attribute to the test. +### Skip the CI + +It is highly recommended to skip the CI if your PR doesn't change nix's code +as this will save our CI usage. + +To do this, add: + +``` +[skip ci] +``` +to your PR description, just like [#2158](https://github.com/nix-rust/nix/pull/2158) +does. + ## GitHub Merge Queues We use GitHub merge queues to ensure that subtle merge conflicts won't result diff --git a/pre-commit.sh b/pre-commit.sh new file mode 100755 index 0000000000..6731a9954f --- /dev/null +++ b/pre-commit.sh @@ -0,0 +1,36 @@ +#!/bin/sh + +function red() { + echo "\033[0;31m$@\033[0m" +} + +function green() { + echo "\033[0;32m$@\033[0m" +} + +function byellow() { + echo "\033[1;33m$@\033[0m" +} + +CHANGED_BY_CARGO_FMT=false + + +echo -e "$(green INFO): Checking code format..." + +cargo fmt --all -q -- --check 2>/dev/null +if [ $? -ne 0 ]; then + echo -e "$(byellow WARN): Unformatted code detected" + echo -e "$(green INFO): Formatting..." + cargo fmt --all + CHANGED_BY_CARGO_FMT=true +fi + +if ${CHANGED_BY_CARGO_FMT}; +then + echo -e "$(red FAIL): git commit $(red ABORTED), please have a look and run git add/commit again" + exit 1 +else + echo -e "$(green INFO): Check passed" +fi + +exit 0 From 71a89a6597f014831901b698a9bd3033ff8c2b6a Mon Sep 17 00:00:00 2001 From: Steve Lau Date: Sat, 7 Oct 2023 14:40:55 +0800 Subject: [PATCH 2/3] fix cirrus CI --- .cirrus.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.cirrus.yml b/.cirrus.yml index bbeb4ac2b4..46b3f6ede3 100644 --- a/.cirrus.yml +++ b/.cirrus.yml @@ -87,7 +87,6 @@ task: - FreeBSD 14 amd64 & i686 - Linux x86_64 - macOS aarch64 - - Rust Formatter - OpenBSD x86_64 - Minver - Rust Stable From 683d39bf38f1d4c2939bd4abe4d66c21006055ff Mon Sep 17 00:00:00 2001 From: Steve Lau Date: Sat, 7 Oct 2023 14:42:05 +0800 Subject: [PATCH 3/3] fix cirrus CI --- .cirrus.yml | 7 ------- 1 file changed, 7 deletions(-) diff --git a/.cirrus.yml b/.cirrus.yml index 46b3f6ede3..8d26de97f1 100644 --- a/.cirrus.yml +++ b/.cirrus.yml @@ -151,7 +151,6 @@ task: - FreeBSD 14 amd64 & i686 - Linux x86_64 - macOS aarch64 - - Rust Formatter - OpenBSD x86_64 - Minver - Rust Stable @@ -171,7 +170,6 @@ task: - FreeBSD 14 amd64 & i686 - Linux x86_64 - macOS aarch64 - - Rust Formatter - OpenBSD x86_64 - Minver - Rust Stable @@ -204,7 +202,6 @@ task: - FreeBSD 14 amd64 & i686 - Linux x86_64 - macOS aarch64 - - Rust Formatter - OpenBSD x86_64 - Minver - Rust Stable @@ -274,7 +271,6 @@ task: - FreeBSD 14 amd64 & i686 - Linux x86_64 - macOS aarch64 - - Rust Formatter - OpenBSD x86_64 - Minver - Rust Stable @@ -307,7 +303,6 @@ task: - FreeBSD 14 amd64 & i686 - Linux x86_64 - macOS aarch64 - - Rust Formatter - OpenBSD x86_64 - Minver - Rust Stable @@ -321,7 +316,6 @@ task: - FreeBSD 14 amd64 & i686 - Linux x86_64 - macOS aarch64 - - Rust Formatter - OpenBSD x86_64 - Minver - Rust Stable @@ -332,7 +326,6 @@ task: - FreeBSD 14 amd64 & i686 - Linux x86_64 - macOS aarch64 - - Rust Formatter - OpenBSD x86_64 - Minver - Rust Stable