Last active
May 13, 2024 21:19
-
-
Save Philippe-Cholet/15b63f1a25b593fa803c6bce42340429 to your computer and use it in GitHub Desktop.
rust-itertools: my git hooks to avoid most CI failures
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# Automatically sync the base branch of the remote fork and fetch it. | |
fork=Philippe-Cholet/itertools | |
base_branch=master | |
branch="$(git rev-parse --abbrev-ref HEAD)" | |
if [ "$branch" = "$base_branch" ]; then | |
gh repo sync $fork -b $base_branch | |
git fetch origin $base_branch | |
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# To run without committing (alias recommanded): git hook run pre-commit | |
base_branch=master | |
assert () { | |
local failure_message="$1" | |
shift # The remaining arguments make the command to execute | |
"$@" # Run the command | |
local return_code=$? | |
[ $return_code -eq 0 ] && return 0 | |
echo "$failure_message" >&2 | |
exit $return_code | |
} | |
assert_file_contains () { | |
assert "Missing in $1: $2" grep --quiet "$2" "$1" | |
} | |
branch="$(git rev-parse --abbrev-ref HEAD)" | |
assert "Do NOT commit directly to $base_branch!" [ "$branch" != "$base_branch" ] | |
# With "short", it only prints filepaths of unformatted files. | |
assert "rustfmt check failed" cargo fmt --check --message-format=short | |
# "quiet" remove cargo logs but the output remains large. | |
assert "check failed (default)" cargo check --quiet | |
assert "check failed (no feature)" cargo check --quiet --no-default-features | |
assert "check failed (only use_alloc)" cargo check --quiet --no-default-features --features=use_alloc | |
assert "clippy failed (all features/targets)" cargo clippy --quiet --all-features --all-targets # -- -A unknown_lints | |
cargo_json="$(cargo metadata --no-deps --format-version=1)" | |
pkg_version="$(echo "$cargo_json" | jq --raw-output '.packages[0].version')" | |
pkg_rust_version="$(echo "$cargo_json" | jq --raw-output '.packages[0].rust_version')" | |
assert_file_contains 'README.md' "itertools = \"$pkg_version\"" | |
assert_file_contains '.github/workflows/ci.yml' "toolchain: $pkg_rust_version" | |
assert_file_contains 'src/lib.rs' "//! This version of itertools requires Rust $pkg_rust_version or later." | |
# rustup install $pkg_rust_version --profile minimal | |
assert "MSRV check failed" cargo +$pkg_rust_version check --quiet |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# Same as pre-commit but warnings are not allowed anymore. | |
if ! RUSTFLAGS=-Dwarnings git hook run pre-commit | |
then | |
echo "Warnings are not allowed in CI" | |
exit 1 | |
fi | |
# or maybe directly in pre-commit export RUSTFLAGS=-Dwarnings |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment