Skip to content

Commit 411f3b9

Browse files
chore: add check for rpc url in pre-commit (#53)
<!-- Reviewable:start --> This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/starkware-libs/starknet-staking/53) <!-- Reviewable:end --> <!-- CURSOR_SUMMARY --> --- > [!NOTE] > Adds a local pre-commit hook that blocks changes to the `url` line in `Scarb.toml` by inspecting the staged diff. > > - **Tooling / Pre-commit**: > - Add local hook `scarb-toml-url-lock` in `.pre-commit-config.yaml` targeting `Scarb.toml` at commit stage. > - Introduce `scripts/check_scarb_url_lock.py` to inspect the staged diff of `Scarb.toml` and fail if `+url` or `-url` changes are detected. > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit c44bef8. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY -->
1 parent 83c5a0f commit 411f3b9

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

.pre-commit-config.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
repos:
2+
- repo: local
3+
hooks:
4+
- id: scarb-toml-url-lock
5+
name: forbid Scarb.toml rpc url change
6+
entry: scripts/check_scarb_url_lock.py
7+
language: system
8+
pass_filenames: false
9+
files: ^Scarb\.toml$
10+
stages: [commit]

scripts/check_scarb_url_lock.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#! /usr/bin/env python3
2+
"""Fail if the Scarb.toml rpc url line is staged for commit."""
3+
4+
from __future__ import annotations
5+
6+
import subprocess
7+
import sys
8+
9+
10+
def staged_diff(path: str) -> str:
11+
"""Return the staged diff for `path` or an empty string."""
12+
13+
result = subprocess.run(
14+
["git", "diff", "--cached", "--unified=0", "--", path],
15+
check=False,
16+
capture_output=True,
17+
text=True,
18+
)
19+
20+
if result.returncode != 0:
21+
# `git diff` returns 0 on success; any other code indicates an error.
22+
print("pre-commit: failed to inspect staged diff for Scarb.toml", file=sys.stderr)
23+
print(result.stderr.strip(), file=sys.stderr)
24+
sys.exit(result.returncode)
25+
26+
return result.stdout
27+
28+
29+
def main() -> int:
30+
diff = staged_diff("Scarb.toml")
31+
if not diff:
32+
return 0
33+
34+
for line in diff.splitlines():
35+
if not line or line[0] not in {"+", "-"}:
36+
continue
37+
if line.startswith(("+url", "-url")):
38+
print(
39+
"pre-commit: Scarb.toml rpc url must not change; revert or drop it before committing.",
40+
file=sys.stderr,
41+
)
42+
return 1
43+
44+
return 0
45+
46+
47+
if __name__ == "__main__":
48+
sys.exit(main())

0 commit comments

Comments
 (0)