Skip to content

Commit 8bf2265

Browse files
authored
Let loss_compare.py check the repo cleaness (#2062)
Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.12.0) (oldest at bottom): * #2063 * __->__ #2062 This will prevent errors when later doing git checkout
1 parent f541d91 commit 8bf2265

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

scripts/loss_compare.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,35 @@ def print_configuration(
301301
# =============================================================================
302302

303303

304+
def check_git_clean_state() -> None:
305+
"""Check if git working directory is clean before switching commits.
306+
307+
Raises SystemExit if there are uncommitted changes or untracked files.
308+
"""
309+
result = subprocess.run(
310+
["git", "status", "--porcelain"],
311+
capture_output=True,
312+
text=True,
313+
check=True,
314+
)
315+
316+
if result.stdout.strip():
317+
log_print("Error: Git working directory is not clean")
318+
log_print(" Cannot switch commits with uncommitted changes")
319+
log_print("")
320+
log_print("Modified/untracked files:")
321+
for line in result.stdout.strip().split("\n"):
322+
log_print(f" {line}")
323+
log_print("")
324+
log_print(
325+
"Please commit, stash, or discard your changes before running this script"
326+
)
327+
log_print(" - To commit: git add -A && git commit -m 'message'")
328+
log_print(" - To stash: git stash")
329+
log_print(" - To discard: git checkout -- . && git clean -fd")
330+
sys.exit(1)
331+
332+
304333
def checkout_commit(commit: str, commit_name: str) -> None:
305334
"""Checkout git commit."""
306335
if commit != ".":
@@ -840,6 +869,12 @@ def main() -> None:
840869
args.job_dump_folder,
841870
)
842871

872+
# Check if git working directory is clean before switching commits
873+
# Skip check if both commits are "." (comparing configs on same commit)
874+
needs_git_checkout = args.baseline_commit != "." or args.test_commit != "."
875+
if needs_git_checkout:
876+
check_git_clean_state()
877+
843878
create_seed_checkpoint(
844879
enable_seed_checkpoint,
845880
args.baseline_config,

0 commit comments

Comments
 (0)