Auto-rename origin during token sync

This commit is contained in:
mpabi
2026-04-29 02:21:59 +02:00
parent af96388f6a
commit 46f2f44118
4 changed files with 79 additions and 36 deletions
+44 -1
View File
@@ -1939,6 +1939,35 @@ def remote_url_from_token_record(token_record: dict) -> str:
return f"{endpoint}/{quote(owner, safe='')}/{quote(repo_name, safe='')}"
def remote_urls_point_to_same_repo(left_url: str, right_url: str) -> bool:
try:
left_sanitized, _ = sanitize_remote_url(left_url)
right_sanitized, _ = sanitize_remote_url(right_url)
except SystemExit:
return False
return left_sanitized.rstrip("/") == right_sanitized.rstrip("/")
def find_origin_rename_candidate(
config: WorkspaceConfig,
repo_root: Path,
target_remote: str,
target_url: str,
) -> tuple[str, str] | None:
origin_remote = config.git.origin_remote
if not origin_remote or origin_remote == target_remote:
return None
if git_capture(repo_root, ["remote", "get-url", target_remote]) is not None:
return None
origin_url = git_capture(repo_root, ["remote", "get-url", origin_remote])
if not origin_url:
return None
if not remote_urls_point_to_same_repo(origin_url, target_url):
return None
return origin_remote, origin_url
def resolve_repo_argument(repo_arg: str | None) -> Path:
return Path(repo_arg).expanduser().resolve() if repo_arg else Path.cwd().resolve()
@@ -2171,6 +2200,7 @@ def run_tokens_sync(config: WorkspaceConfig, args: argparse.Namespace) -> None:
args.repo = str(repo_path)
args.remote = args.remote_id
args.token_name = args.remote_id
args.auto_rename_origin = True
run_tokens_write(config, args)
return
raise SystemExit(f"Unsupported tokens sync direction: {args.direction}")
@@ -2255,11 +2285,17 @@ def run_tokens_write(config: WorkspaceConfig, args: argparse.Namespace) -> None:
f"Missing target remote URL and tokens.json has no org/repo for: {server_endpoint}:{remote_id}"
)
rename_candidate = None
if getattr(args, "auto_rename_origin", False) and existing_remote_url is None:
rename_candidate = find_origin_rename_candidate(config, repo_root, args.remote, target_url)
if rename_candidate:
_, existing_remote_url = rename_candidate
final_url = credentialed_remote_url(target_url, user_name, token_value)
status = "added" if existing_remote_url is None else "updated"
existing_credentials = remote_credentials(existing_remote_url) if existing_remote_url else None
if existing_credentials == (user_name, token_value):
status = "unchanged"
status = "renamed" if rename_candidate else "unchanged"
elif existing_credentials is not None and not args.replace:
raise SystemExit(
f"Remote '{args.remote}' already has different credentials. "
@@ -2268,6 +2304,8 @@ def run_tokens_write(config: WorkspaceConfig, args: argparse.Namespace) -> None:
print(f"repo_root\t{repo_root}")
print(f"remote\t{args.remote}")
if rename_candidate:
print(f"renamed_from\t{rename_candidate[0]}")
print(f"server\t{server_endpoint}")
print(f"user\t{user_name}")
print(f"id\t{remote_id}")
@@ -2277,6 +2315,11 @@ def run_tokens_write(config: WorkspaceConfig, args: argparse.Namespace) -> None:
if args.dry_run or status == "unchanged":
return
if rename_candidate:
subprocess.run(
["git", "-C", str(repo_root), "remote", "rename", rename_candidate[0], args.remote],
check=True,
)
set_git_remote(repo_root, args.remote, final_url)