Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions .githooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/bin/sh
# Shared pre-commit checks for the niche-value crate.
# Enable with: git config core.hooksPath .githooks
# Bypass the whole hook with `git commit --no-verify` if you must.

# 1. Forbid direct commits to protected branches.
protected="main master"
branch="$(git symbolic-ref --short HEAD 2>/dev/null)"
for b in $protected; do
if [ "$branch" = "$b" ]; then
echo "pre-commit: direct commits to '$branch' are forbidden." >&2
echo " Create a feature branch: git switch -c my-change" >&2
echo " (bypass with 'git commit --no-verify' only if you must)" >&2
exit 1
fi
done

# Run cargo checks from the repo root (hook may be invoked from a subdir).
root="$(git rev-parse --show-toplevel)" || exit 1
cd "$root" || exit 1

# 2. Formatting must be clean.
echo "pre-commit: cargo fmt --check"
if ! cargo fmt --all --check; then
echo "pre-commit: formatting issues found. Run 'cargo fmt --all', then re-stage." >&2
exit 1
fi

# 3. Clippy with warnings denied, across all features and targets.
echo "pre-commit: cargo clippy"
if ! cargo clippy --all-features --all-targets -- -D warnings; then
echo "pre-commit: clippy reported issues. Fix them, then commit again." >&2
exit 1
fi

exit 0
16 changes: 16 additions & 0 deletions .githooks/pre-push
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/sh
# Shared pre-push check for the niche-value crate: run the test suite before
# anything reaches the remote / CI.
# Enable with: git config core.hooksPath .githooks
# Bypass with `git push --no-verify`.

root="$(git rev-parse --show-toplevel)" || exit 1
cd "$root" || exit 1

echo "pre-push: cargo test --all-features"
if ! cargo test --all-features; then
echo "pre-push: tests failed. Push aborted (bypass with 'git push --no-verify')." >&2
exit 1
fi

exit 0
53 changes: 53 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Contributing

Thanks for helping improve `niche-value`!

## One-time setup: git hooks

This repo ships shared git hooks in [`.githooks/`](.githooks). Enable them once
per clone:

```sh
git config core.hooksPath .githooks
```

(`core.hooksPath` is a local setting and is not shared automatically, so each
clone must run this once.)

### What the hooks do

- **pre-commit**
- forbids direct commits to `main` / `master` (work on a feature branch);
- runs `cargo fmt --all --check`;
- runs `cargo clippy --all-features --all-targets -- -D warnings`.
- **pre-push**
- runs `cargo test --all-features`.

Bypass in a pinch with `git commit --no-verify` / `git push --no-verify`.

## Workflow

`main` is protected. Make changes on a feature branch and open a pull request:

```sh
git switch -c my-change
# ... edit ...
git commit -m "..." # pre-commit runs fmt + clippy
git push -u origin my-change # pre-push runs the tests
gh pr create
```

## Checks run in CI

CI (`.github/workflows/ci.yml`) runs, and PRs must pass: tests (stable),
`build` on the MSRV (1.83), `rustfmt`, `clippy` (warnings denied), Miri, and
docs. Running the hooks locally keeps you ahead of CI.

## Manual commands

```sh
cargo fmt --all
cargo clippy --all-features --all-targets -- -D warnings
cargo test --all-features
cargo +nightly miri test --all-features # requires: rustup +nightly component add miri
```
Loading