Skip to content

Commit 75d690e

Browse files
chore: add check for rpc url in pre-commit
1 parent bc32940 commit 75d690e

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

.pre-commit-config.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,10 @@ repos:
1313
language: system
1414
pass_filenames: false
1515
stages: [commit]
16+
- id: scarb-toml-url-lock
17+
name: forbid Scarb.toml rpc url change
18+
entry: scripts/check_scarb_url_lock.py
19+
language: system
20+
pass_filenames: false
21+
files: ^Scarb\.toml$
22+
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 not in (0, 1):
21+
# `git diff` returns 1 if there were differences; treat other codes as fatal.
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)