forked from aztec-labs-eng/aztec-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprecommit.sh
More file actions
executable file
·114 lines (98 loc) · 5.03 KB
/
Copy pathprecommit.sh
File metadata and controls
executable file
·114 lines (98 loc) · 5.03 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env bash
# Precommit hook for formatting staged noir files in the noir-projects workspaces and keeping
# generated contracts in sync.
# 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
# we have to unset this env var set by git hooks so that this relative paths work correctly when used inside worktrees
unset GIT_DIR
cd $(dirname $0)
export FORCE_COLOR=true
# Path to nargo binary
NARGO_PATH="../labs-aztec-toolchain/bin/nargo"
# Absolute path for use inside the per-workspace loop, where a relative path would
# resolve differently depending on each workspace's depth.
NARGO_ABS="$PWD/$NARGO_PATH"
# Check if there are staged .nr files
staged_nr_files=$(git diff --cached --name-only --diff-filter=d | grep '^noir-projects/.*\.nr$' || true)
# Check for unstaged .nr files too
unstaged_nr_files=$(git diff --name-only --diff-filter=d | grep '^noir-projects/.*\.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..."
# The toolchain dir is gitignored and can be absent even when the binaries themselves exist
# (e.g. after a partial build that never ran the toolchain bootstrap). Setting it up only
# creates symlinks, so try that before giving up — but never let it block the commit.
if [[ ! -x "$NARGO_PATH" ]]; then
(cd ../labs-aztec-toolchain && ./bootstrap.sh &>/dev/null) || true
fi
# 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 aztec-nr; do
if [[ -d "$dir" ]]; then
echo "Formatting in $dir..."
(cd "$dir" && "$NARGO_ABS" 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.
# Keep the generated TestToken in sync with canonical Token (see
# noir-contracts/scripts/gen_test_token.sh). That script is pure cp+perl (no build), so it's cheap to
# run here, and it replaces the build-time --check for local commits. Runs after nargo fmt above so it
# copies the freshly-formatted canonical source. Like the rest of this hook, it must never block a commit.
staged_token_files=$(git diff --cached --name-only --diff-filter=d \
| grep '^noir-projects/noir-contracts/contracts/app/token_contract/' || true)
if [[ -n "$staged_token_files" ]]; then
# Skip if any canonical Token file is partially staged, so we don't bake unstaged Token changes into
# the committed TestToken (mirrors the partial-staging guard above).
unstaged_token_files=$(git diff --name-only --diff-filter=d \
| grep '^noir-projects/noir-contracts/contracts/app/token_contract/' || true)
token_partially_staged=false
for file in $staged_token_files; do
if echo "$unstaged_token_files" | grep -Fxq "$file"; then
token_partially_staged=true
fi
done
if $token_partially_staged; then
echo -e "\033[33mWarning:\033[0m token_contract is partially staged; skipping TestToken regen."
echo -e "\033[33mRun noir-contracts/scripts/gen_test_token.sh and stage the result manually.\033[0m"
else
echo "Detected staged canonical Token change. Regenerating TestToken..."
if ./noir-contracts/scripts/gen_test_token.sh; then
repo_root=$(git rev-parse --show-toplevel)
git add "$repo_root/noir-projects/noir-contracts/contracts/test/test_token_contract"
else
echo -e "\033[33mWarning:\033[0m TestToken regen failed; your commit will proceed."
echo -e "\033[33mRun noir-contracts/scripts/gen_test_token.sh manually.\033[0m"
fi
fi
fi