Skip to content

Commit 7818602

Browse files
committed
Enh(aws): Guard against unpushed error
1 parent e10fb10 commit 7818602

2 files changed

Lines changed: 30 additions & 11 deletions

File tree

aws/run_job.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,24 @@
1414
import argparse
1515
import json
1616
import shlex
17-
import subprocess
1817
import sys
1918
import time
2019
from typing import Any
2120

2221
import boto3
2322

23+
from codeclash.utils.git_utils import get_current_git_branch, has_unpushed_commits, is_git_repo_dirty
2424
from codeclash.utils.log import get_logger
2525

2626
logger = get_logger("launch", emoji="🚀")
2727

2828

29-
def get_current_git_branch() -> str:
30-
"""Get the current git branch name."""
31-
try:
32-
result = subprocess.run(
33-
["git", "rev-parse", "--abbrev-ref", "HEAD"], capture_output=True, text=True, check=True
34-
)
35-
return result.stdout.strip()
36-
except subprocess.CalledProcessError as e:
37-
logger.error(f"Failed to get git branch: {e}")
38-
raise
29+
def check_git_status_and_confirm() -> None:
30+
if not is_git_repo_dirty() and not has_unpushed_commits():
31+
return
32+
33+
if input("Git repository dirty/unpushed, continue anyway? (y/N): ").strip().lower() not in ("y", "yes"):
34+
sys.exit(1)
3935

4036

4137
class AWSBatchJobLauncher:
@@ -169,6 +165,8 @@ def main():
169165

170166
args = parser.parse_args(aws_args)
171167

168+
check_git_status_and_confirm()
169+
172170
launcher = AWSBatchJobLauncher(job_definition_name=args.job_definition, job_queue=args.job_queue)
173171

174172
# Submit the job

codeclash/utils/git_utils.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import subprocess
2+
3+
4+
def get_current_git_branch() -> str:
5+
result = subprocess.run(["git", "rev-parse", "--abbrev-ref", "HEAD"], capture_output=True, text=True, check=True)
6+
return result.stdout.strip()
7+
8+
9+
def is_git_repo_dirty() -> bool:
10+
result = subprocess.run(["git", "status", "--porcelain"], capture_output=True, text=True, check=True)
11+
return bool(result.stdout.strip())
12+
13+
14+
def has_unpushed_commits() -> bool:
15+
try:
16+
result = subprocess.run(
17+
["git", "rev-list", "--count", "@{u}..HEAD"], capture_output=True, text=True, check=True
18+
)
19+
return int(result.stdout.strip()) > 0
20+
except subprocess.CalledProcessError:
21+
return False

0 commit comments

Comments
 (0)