Skip to content

Commit 8fccd49

Browse files
committed
Check for changes to the public API
We would like to get to a stage where we can commit to the public API. To help us achieve this add a script that generates the public API and checks it against three committed files, one for each feature set: no features, alloc, std. The idea is that with this applied any PR that changes the public API should include a final patch that is just the changes to the api/*.txt files, that way reviewers can discuss the changes without even needing to look at the code, quickly giving concept ACK/NACKs. We also run the script in CI to make sure we have not accidentally changed the public API so that we can be confident that don't break semver during releases. The script can also be used to diff between two release versions to get a complete list of API changes, useful for writing release notes and for users upgrading. There is a development burden involved if we apply this patch.
1 parent 1f773b9 commit 8fccd49

File tree

8 files changed

+92
-0
lines changed

8 files changed

+92
-0
lines changed

.github/workflows/rust.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,18 @@ jobs:
140140
run: cargo install cross --locked
141141
- name: run cross test
142142
run: cross test --target s390x-unknown-linux-gnu
143+
144+
API:
145+
name: Check for changes to the public API
146+
runs-on: ubuntu-latest
147+
strategy:
148+
fail-fast: false
149+
steps:
150+
- name: Checkout Crate
151+
uses: actions/checkout@v3
152+
- name: Checkout Toolchain
153+
uses: dtolnay/rust-toolchain@nightly
154+
- name: Install cargo-public-api
155+
run: cargo install --locked cargo-public-api
156+
- name: Running API checker script
157+
run: ./contrib/check-for-api-changes.sh

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,10 @@ Alternatively add symlinks in your `.git/hooks` directory to any of the githooks
3333

3434
We use a custom Rust compiler configuration conditional to guard the benchmark code. To run the
3535
benchmarks use: `RUSTFLAGS='--cfg=bench' cargo +nightly bench`.
36+
37+
38+
## API changes
39+
40+
All PRs that change the public API of `rust-bech32` must include a patch to the
41+
`api/` text files. For PRs that include API changes, add a separate patch to the PR
42+
that is the diff created by running `contrib/check-for-api-changes.sh`.

api/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
API text files
2+
==============
3+
4+
Each file here lists the public API when built with some set of features
5+
enabled. To create these files run `../contrib/check-for-api-changes.sh`:
6+
7+
Requires `cargo-public-api`, install with:
8+
9+
`cargo +stable install cargo-public-api --locked`.
10+
11+
ref: https://github.com/enselic/cargo-public-api

api/all-features.txt

Whitespace-only changes.

api/alloc-only.txt

Whitespace-only changes.

api/no-features.txt

Whitespace-only changes.

contrib/check-for-api-changes.sh

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!/usr/bin/env bash
2+
#
3+
# Checks the public API of crates, exits with non-zero if there are currently
4+
# changes to the public API not already committed to in the various api/*.txt
5+
# files.
6+
7+
set -e
8+
9+
REPO_DIR=$(git rev-parse --show-toplevel)
10+
API_DIR="$REPO_DIR/api"
11+
CARGO="cargo +nightly public-api --simplified"
12+
SORT="sort --numeric-sort --ignore-case --ignore-nonprinting --stable --unique"
13+
14+
main() {
15+
# cargo public-api uses nightly so the toolchain must be available.
16+
if ! cargo +nightly --version > /dev/null; then
17+
echo "script requires a nightly toolchain to be installed (possibly >= nightly-2023-05-24)" >&2
18+
exit 1
19+
fi
20+
21+
generate_api_files
22+
check_for_changes
23+
}
24+
25+
generate_api_files() {
26+
pushd "$REPO_DIR" > /dev/null
27+
28+
$CARGO --no-default-features | $SORT > "$API_DIR/no-features.txt"
29+
$CARGO --no-default-features --features=alloc | $SORT > "$API_DIR/alloc-only.txt"
30+
$CARGO --all-features | $SORT > "$API_DIR/all-features.txt"
31+
32+
popd > /dev/null
33+
}
34+
35+
# Check if there are changes (dirty git index) to the `api/` directory.
36+
check_for_changes() {
37+
pushd "$REPO_DIR" > /dev/null
38+
39+
if [[ $(git status --porcelain api) ]]; then
40+
git diff --color=always
41+
echo "You have introduced changes to the public API, commit the changes to api/ currently in your working directory" >&2
42+
exit 1
43+
44+
else
45+
echo "No changes to the current public API"
46+
fi
47+
48+
popd > /dev/null
49+
}
50+
51+
#
52+
# Main script
53+
#
54+
main "$@"
55+
exit 0

justfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,7 @@ clippy:
3030
# run `cargo clippy --fix` on everything
3131
clippy-fix:
3232
cargo clippy --locked --offline --workspace --all-targets --fix
33+
34+
# Check for API changes.
35+
check-api:
36+
contrib/check-for-api-changes.sh

0 commit comments

Comments
 (0)