Skip to content

Commit

Permalink
chore: nargo fmt pre-commit hook (#11416)
Browse files Browse the repository at this point in the history
I was getting annoyed at forgetting to format noir files, and then
pushing, then waiting X mins, and then CI reminding me with a failure.

Tagging Charlie and Adam, because this would annoy people if I've made a
mistake.
  • Loading branch information
iAmMichaelConnor authored Jan 22, 2025
1 parent cf3c911 commit 6f2e2e0
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
1 change: 1 addition & 0 deletions bootstrap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ esac
hooks_dir=$(git rev-parse --git-path hooks)
echo "(cd barretenberg/cpp && ./format.sh staged)" >$hooks_dir/pre-commit
echo "./yarn-project/precommit.sh" >>$hooks_dir/pre-commit
echo "./noir-projects/precommit.sh" >>$hooks_dir/pre-commit
chmod +x $hooks_dir/pre-commit

github_group "pull submodules"
Expand Down
39 changes: 39 additions & 0 deletions noir-projects/precommit.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/bin/bash
# Precommit hook for formatting staged noir files.
# We only run the formatter if there are staged *.nr files.
# Nothing should cause a failure, because that would annoy everyone if all they're trying to do is commit.
set -euo pipefail

cd $(dirname $0)

export FORCE_COLOR=true

# Path to nargo binary
NARGO_PATH="../noir/noir-repo/target/release/nargo"

# Check if there are staged .nr files
staged_nr_files=$(git diff --cached --name-only --diff-filter=d | grep '\.nr$' || true)

if [[ -n "$staged_nr_files" ]]; then
echo "Detected staged .nr files. Running nargo fmt..."

# Check if nargo exists (the user might be making a quick change, without wanting to have to bootstrap the entire repo, so we don't want an inconvenient catastrophic failure if this hook can't complete execution; we want to fail gracefully).
if [[ ! -x "$NARGO_PATH" ]]; then
echo "Warning: nargo not found at $NARGO_PATH"
echo " Skipping the nargo fmt commit hook."
exit 0
fi

for dir in noir-contracts noir-protocol-circuits mock-protocol-circuits aztec-nr; do
if [[ -d "$dir" ]]; then
echo "Formatting in $dir..."
(cd "$dir" && "../$NARGO_PATH" fmt) || echo "Warning: Formatting failed in $dir, but continuing..."
else
echo "Warning: Directory $dir not found, skipping..."
fi
done

echo "Formatting completed."
fi

# We just don't say anything if there are no staged nr files, because no one cares.

0 comments on commit 6f2e2e0

Please sign in to comment.