-
Notifications
You must be signed in to change notification settings - Fork 593
Expand file tree
/
Copy pathprecommit.sh
More file actions
executable file
·67 lines (55 loc) · 2.56 KB
/
precommit.sh
File metadata and controls
executable file
·67 lines (55 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env 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)
# Check for unstaged .nr files too
unstaged_nr_files=$(git diff --name-only --diff-filter=d | grep '\.nr$' || true)
# Detect partially staged .nr files (staged + unstaged changes).
# We don't want to auto-format anything if someone has staged a hunk (partial file)
# as we might corrupt their hunks.
# We'll identify partially staged files by looking
# at the intersection of staged and unstaged:
partially_staged_nr_files=()
for file in $staged_nr_files; do
if echo "$unstaged_nr_files" | grep -Fxq "$file"; then
partially_staged_nr_files+=("$file")
fi
done
if (( ${#partially_staged_nr_files[@]} > 0 )); then
echo -e "\033[33mWarning:\033[0m The following .nr files are partially staged:"
for f in "${partially_staged_nr_files[@]}"; do
echo " - $f"
done
echo -e "\033[33mSkipping nargo fmt because of the partial staging. Your files have been committed (as you wanted), but you'll have to format them manually with '$NARGO_PATH fmt'.\033[0m"
exit 0
fi
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."
# Re-stage formatted .nr files
echo "Re-staging formatted .nr files..."
repo_root=$(git rev-parse --show-toplevel)
echo "$staged_nr_files" | xargs -I {} git add "$repo_root/{}"
fi
# We just don't say anything if there are no staged nr files, because no one cares.