|
17 | 17 | import cruft |
18 | 18 | import typer |
19 | 19 | from cookiecutter.utils import work_in |
20 | | -from pygments.lexers import q |
21 | 20 | from typer.models import OptionInfo |
22 | 21 |
|
23 | 22 |
|
@@ -65,6 +64,39 @@ def run_command(command: str, *args: str, ignore_error: bool = False) -> Optiona |
65 | 64 | uv: partial[subprocess.CompletedProcess] = partial(run_command, "uv") |
66 | 65 |
|
67 | 66 |
|
| 67 | +def require_clean_and_up_to_date_repo() -> None: |
| 68 | + """Checks if the repo is clean and up to date with any important branches.""" |
| 69 | + if not is_repo_clean_and_up_to_date(): |
| 70 | + typer.secho("The repo is either not clean or is not up to date.", fg="red") |
| 71 | + raise typer.Exit(code=1) |
| 72 | + |
| 73 | + |
| 74 | +def is_repo_clean_and_up_to_date() -> bool: |
| 75 | + """Checks if the repo is clean and up to date with any important branches.""" |
| 76 | + try: |
| 77 | + git("fetch") |
| 78 | + git("status", "--porcelain") |
| 79 | + if not is_branch_synced_with_remote("develop"): |
| 80 | + raise ValueError("develop is not synced with origin/develop") |
| 81 | + if not is_branch_synced_with_remote("main"): |
| 82 | + raise ValueError("main is not synced with origin/main") |
| 83 | + if not is_ancestor("main", "develop"): |
| 84 | + raise ValueError("main is not an ancestor of develop") |
| 85 | + return True |
| 86 | + except subprocess.CalledProcessError: |
| 87 | + return False |
| 88 | + |
| 89 | + |
| 90 | +def is_branch_synced_with_remote(branch: str) -> bool: |
| 91 | + """Checks if the branch is synced with its remote.""" |
| 92 | + return is_ancestor(branch, f"origin/{branch}") and is_ancestor(f"origin/{branch}", branch) |
| 93 | + |
| 94 | + |
| 95 | +def is_ancestor(ancestor: str, descendent: str) -> bool: |
| 96 | + """Checks if the branch is synced with its remote.""" |
| 97 | + return git("merge-base", "--is-ancestor", ancestor, descendent).returncode == 0 |
| 98 | + |
| 99 | + |
68 | 100 | @contextmanager |
69 | 101 | def in_new_demo( |
70 | 102 | demos_cache_folder: Path, |
|
0 commit comments