|
10 | 10 | from pathlib import Path |
11 | 11 | from typing import List, Tuple |
12 | 12 |
|
| 13 | + |
| 14 | +def has_local_changes(repodir: Path) -> bool: |
| 15 | + """Check if a git repository has uncommitted changes or is on a non-detached branch.""" |
| 16 | + # Check for uncommitted changes |
| 17 | + status_result = subprocess.run( |
| 18 | + ['git', '-C', repodir, 'status', '--porcelain'], |
| 19 | + capture_output=True, |
| 20 | + text=True |
| 21 | + ) |
| 22 | + if status_result.stdout.strip(): |
| 23 | + return True |
| 24 | + |
| 25 | + # Check if we're on a branch (not detached HEAD) |
| 26 | + branch_result = subprocess.run( |
| 27 | + ['git', '-C', repodir, 'symbolic-ref', '-q', 'HEAD'], |
| 28 | + capture_output=True, |
| 29 | + text=True |
| 30 | + ) |
| 31 | + if branch_result.returncode == 0: |
| 32 | + # We're on a branch, check if it has commits ahead of origin |
| 33 | + return True |
| 34 | + |
| 35 | + return False |
| 36 | + |
| 37 | + |
13 | 38 | def copy_docs(repos: List[Tuple[str, str]]) -> List[Path]: |
14 | 39 | """" |
15 | 40 | Pull in docs from other repos. |
@@ -43,11 +68,14 @@ def copy_docs(repos: List[Tuple[str, str]]) -> List[Path]: |
43 | 68 |
|
44 | 69 | # Check if repo already exists |
45 | 70 | if repodir.exists(): |
46 | | - print(f"{repo_path} already there, updating") |
47 | | - # Git fetch |
48 | | - subprocess.run(['git', '-C', repodir, 'fetch'], check=True) |
49 | | - # Checkout ref |
50 | | - subprocess.run(['git', '-C', repodir, 'checkout', '-f', '--detach', ref], check=True) |
| 71 | + if has_local_changes(repodir): |
| 72 | + print(f"{repo_path} has local changes, skipping update") |
| 73 | + else: |
| 74 | + print(f"{repo_path} already there, updating") |
| 75 | + # Git fetch |
| 76 | + subprocess.run(['git', '-C', repodir, 'fetch'], check=True) |
| 77 | + # Checkout ref |
| 78 | + subprocess.run(['git', '-C', repodir, 'checkout', '-f', '--detach', ref], check=True) |
51 | 79 | else: |
52 | 80 | print(f"Cloning {repo_path} {ref} as {repodir}") |
53 | 81 | # Clone the repository with gh |
|
0 commit comments