|
| 1 | +"""Swap command for swapping origin and upstream remotes.""" |
| 2 | + |
| 3 | +import subprocess |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +import typer |
| 7 | + |
| 8 | +from dbx_python_cli.utils.repo import ( |
| 9 | + find_repo_by_name, |
| 10 | + find_repo_by_path, |
| 11 | + get_base_dir, |
| 12 | + get_config, |
| 13 | +) |
| 14 | + |
| 15 | +app = typer.Typer( |
| 16 | + help="Swap origin and upstream git remotes", |
| 17 | + no_args_is_help=True, |
| 18 | + invoke_without_command=True, |
| 19 | + context_settings={ |
| 20 | + "allow_interspersed_args": True, |
| 21 | + "help_option_names": ["-h", "--help"], |
| 22 | + }, |
| 23 | +) |
| 24 | + |
| 25 | + |
| 26 | +def _get_remote_url(repo_path: Path, remote: str) -> str | None: |
| 27 | + """Return the URL for a remote, or None if the remote doesn't exist.""" |
| 28 | + try: |
| 29 | + result = subprocess.run( |
| 30 | + ["git", "remote", "get-url", remote], |
| 31 | + cwd=repo_path, |
| 32 | + capture_output=True, |
| 33 | + text=True, |
| 34 | + check=True, |
| 35 | + ) |
| 36 | + return result.stdout.strip() |
| 37 | + except subprocess.CalledProcessError: |
| 38 | + return None |
| 39 | + |
| 40 | + |
| 41 | +def _set_remote_url(repo_path: Path, remote: str, url: str) -> None: |
| 42 | + subprocess.run( |
| 43 | + ["git", "remote", "set-url", remote, url], |
| 44 | + cwd=repo_path, |
| 45 | + check=True, |
| 46 | + ) |
| 47 | + |
| 48 | + |
| 49 | +def _swap_remotes( |
| 50 | + repo_path: Path, repo_name: str, verbose: bool, dry_run: bool |
| 51 | +) -> bool: |
| 52 | + """Swap origin and upstream for a single repo. Returns True on success.""" |
| 53 | + origin_url = _get_remote_url(repo_path, "origin") |
| 54 | + upstream_url = _get_remote_url(repo_path, "upstream") |
| 55 | + |
| 56 | + if not origin_url: |
| 57 | + typer.echo(f"❌ {repo_name}: no 'origin' remote found", err=True) |
| 58 | + return False |
| 59 | + if not upstream_url: |
| 60 | + typer.echo(f"❌ {repo_name}: no 'upstream' remote found", err=True) |
| 61 | + return False |
| 62 | + |
| 63 | + if verbose or dry_run: |
| 64 | + typer.echo(f" origin: {origin_url}") |
| 65 | + typer.echo(f" upstream: {upstream_url}") |
| 66 | + |
| 67 | + if dry_run: |
| 68 | + typer.echo(f" → would set origin={upstream_url}, upstream={origin_url}") |
| 69 | + return True |
| 70 | + |
| 71 | + _set_remote_url(repo_path, "origin", upstream_url) |
| 72 | + _set_remote_url(repo_path, "upstream", origin_url) |
| 73 | + typer.echo( |
| 74 | + f"🔄 {repo_name}: swapped — origin={upstream_url}, upstream={origin_url}" |
| 75 | + ) |
| 76 | + return True |
| 77 | + |
| 78 | + |
| 79 | +@app.callback() |
| 80 | +def swap_callback( |
| 81 | + ctx: typer.Context, |
| 82 | + repo_name: str = typer.Argument( |
| 83 | + None, |
| 84 | + help="Repository name or '.' for the current directory", |
| 85 | + ), |
| 86 | + group: str = typer.Option( |
| 87 | + None, |
| 88 | + "--group", |
| 89 | + "-g", |
| 90 | + help="Swap remotes in all repositories in a group", |
| 91 | + ), |
| 92 | + dry_run: bool = typer.Option( |
| 93 | + False, |
| 94 | + "--dry-run", |
| 95 | + help="Show what would be swapped without making changes", |
| 96 | + ), |
| 97 | +): |
| 98 | + """Swap origin and upstream git remotes in a repository. |
| 99 | +
|
| 100 | + Usage:: |
| 101 | +
|
| 102 | + dbx swap <repo_name> # Swap by name |
| 103 | + dbx swap . # Swap in the current directory |
| 104 | + dbx swap -g <group> # Swap all repos in a group |
| 105 | +
|
| 106 | + Examples:: |
| 107 | +
|
| 108 | + dbx swap mongo-python-driver |
| 109 | + dbx swap . |
| 110 | + dbx swap -g pymongo --dry-run |
| 111 | + """ |
| 112 | + verbose = ctx.obj.get("verbose", False) if ctx.obj else False |
| 113 | + |
| 114 | + try: |
| 115 | + config = get_config() |
| 116 | + base_dir = get_base_dir(config) |
| 117 | + except Exception as e: |
| 118 | + typer.echo(f"❌ Error: {e}", err=True) |
| 119 | + raise typer.Exit(1) |
| 120 | + |
| 121 | + # Group mode |
| 122 | + if group: |
| 123 | + from dbx_python_cli.utils.repo import find_all_repos |
| 124 | + |
| 125 | + all_repos = find_all_repos(base_dir, config) |
| 126 | + group_repos = [r for r in all_repos if r["group"] == group] |
| 127 | + if not group_repos: |
| 128 | + typer.echo( |
| 129 | + f"❌ Error: No cloned repositories found in group '{group}'", err=True |
| 130 | + ) |
| 131 | + raise typer.Exit(1) |
| 132 | + typer.echo( |
| 133 | + f"{'[dry-run] ' if dry_run else ''}Swapping remotes in {len(group_repos)} repo(s) in group '{group}':\n" |
| 134 | + ) |
| 135 | + for r in sorted(group_repos, key=lambda x: x["name"]): |
| 136 | + if verbose or dry_run: |
| 137 | + typer.echo(f"📦 {r['name']}:") |
| 138 | + _swap_remotes(r["path"], r["name"], verbose, dry_run) |
| 139 | + return |
| 140 | + |
| 141 | + # Single repo mode |
| 142 | + if not repo_name: |
| 143 | + typer.echo("❌ Error: Repository name or -g <group> required", err=True) |
| 144 | + raise typer.Exit(1) |
| 145 | + |
| 146 | + _is_path_like = ( |
| 147 | + repo_name in (".", "..") |
| 148 | + or repo_name.startswith(("./", "../", "/", "~/")) |
| 149 | + or "/" in repo_name |
| 150 | + or Path(repo_name).is_dir() |
| 151 | + ) |
| 152 | + |
| 153 | + if _is_path_like: |
| 154 | + repo_info = find_repo_by_path(repo_name, base_dir, config) |
| 155 | + if not repo_info: |
| 156 | + typer.echo( |
| 157 | + f"❌ Error: No managed repository found at '{Path(repo_name).resolve()}'", |
| 158 | + err=True, |
| 159 | + ) |
| 160 | + raise typer.Exit(1) |
| 161 | + else: |
| 162 | + repo_info = find_repo_by_name(repo_name, base_dir, config) |
| 163 | + if not repo_info: |
| 164 | + typer.echo(f"❌ Error: Repository '{repo_name}' not found", err=True) |
| 165 | + typer.echo("\nUse 'dbx list' to see available repositories") |
| 166 | + raise typer.Exit(1) |
| 167 | + |
| 168 | + if dry_run or verbose: |
| 169 | + typer.echo(f"📦 {repo_info['name']}:") |
| 170 | + if not _swap_remotes(repo_info["path"], repo_info["name"], verbose, dry_run): |
| 171 | + raise typer.Exit(1) |
0 commit comments