-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgit_guard.py
More file actions
39 lines (33 loc) · 922 Bytes
/
Copy pathgit_guard.py
File metadata and controls
39 lines (33 loc) · 922 Bytes
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
"""Refuse to build with uncommitted tracked changes (unless --allow-dirty)."""
from __future__ import annotations
import subprocess
from pathlib import Path
class DirtyWorktreeError(RuntimeError):
"""Raised when source_dir has uncommitted tracked changes."""
def ensure_clean_worktree(
*,
repo_root: Path,
source_dir: str,
allow_dirty: bool = False,
) -> None:
if allow_dirty:
return
result = subprocess.run(
[
"git",
"status",
"--porcelain",
"--untracked-files=no",
"--",
source_dir,
],
cwd=repo_root,
check=True,
capture_output=True,
text=True,
)
if result.stdout.strip():
raise DirtyWorktreeError(
f"Uncommitted changes in {source_dir}:\n{result.stdout}"
"Commit or pass --allow-dirty for local iteration."
)