Skip to content
Snippets Groups Projects
Verified Commit 041c360b authored by Newbyte's avatar Newbyte :snowflake:
Browse files

pmb.helpers.git: Automatically migrate fetch and push URL

This could be re-purposed in the future in case we migrate URLs again.
parent 03c4ca2a
No related branches found
No related tags found
No related merge requests found
Pipeline #208090 passed
# Copyright 2024 Oliver Smith
# SPDX-License-Identifier: GPL-3.0-or-later
import configparser
from enum import Enum
from pathlib import Path
from typing import Final
from pmb.core.context import get_context
from pmb.core.pkgrepo import pkgrepo_path
from pmb.helpers import logging
import os
import re
import sys
import pmb.build
import pmb.chroot.apk
......@@ -103,13 +106,85 @@ def get_upstream_remote(aports: Path):
urls = pmb.config.git_repos[name_repo]
command = ["git", "remote", "-v"]
output = pmb.helpers.run.user_output(command, aports, output="null")
for line in output.split("\n"):
lines = output.split("\n")
for line in lines:
if any(u in line for u in urls):
return line.split("\t", 1)[0]
raise NonBugError(
f"{name_repo}: could not find remote name for any URL '{urls}' in git"
f" repository: {aports}"
)
did_migrate = migrate_upstream_remote(lines)
if did_migrate:
logging.info(
"NOTE: Migration from old to new git URLs successful! Please re-run your last command. Sorry about the inconvenience."
)
sys.exit(1)
else:
raise NonBugError(
f"{name_repo}: could not find remote name for any URL '{urls}' in git"
f" repository: {aports}"
)
class RemoteType(Enum):
FETCH = "fetch"
PUSH = "push"
@staticmethod
def from_git_output(git_remote_type: str) -> "RemoteType":
match git_remote_type:
case "(fetch)":
return RemoteType.FETCH
case "(push)":
return RemoteType.PUSH
case _:
raise ValueError(f'Unknown remote type "{git_remote_type}"')
def set_remote_url(remote_name: str, remote_url: str, remote_type: RemoteType) -> None:
command = [
"git",
"remote",
"set-url",
remote_name,
remote_url,
"--push" if remote_type == RemoteType.PUSH else "--no-push",
]
pmb.helpers.run.user(command, output="null")
OUTDATED_GIT_REMOTES_HTTP: Final[list[str]] = ["https://gitlab.com/postmarketOS/pmaports.git"]
OUTDATED_GIT_REMOTES_SSH: Final[list[str]] = ["git@gitlab.com:postmarketOS/pmaports.git"]
CURRENT_GIT_REMOTE_HTTP: Final[str] = "https://gitlab.postmarketos.org/postmarketOS/pmaports.git"
CURRENT_GIT_REMOTE_SSH: Final[str] = "git@gitlab.postmarketos.org:postmarketOS/pmaports.git"
def migrate_upstream_remote(lines: list[str]) -> bool:
did_migrate = False
for line in lines:
if not line:
continue # Skip empty line at the end.
remote_name, remote_url, remote_type_raw = line.split()
remote_type = RemoteType.from_git_output(remote_type_raw)
if remote_url in OUTDATED_GIT_REMOTES_HTTP:
new_remote = CURRENT_GIT_REMOTE_HTTP
elif remote_url in OUTDATED_GIT_REMOTES_SSH:
new_remote = CURRENT_GIT_REMOTE_SSH
else:
new_remote = None
if new_remote:
logging.info(
f"Migrating to new {remote_type.value} URL (from {remote_url} to {new_remote})"
)
set_remote_url(remote_name, CURRENT_GIT_REMOTE_HTTP, remote_type)
did_migrate = True
return did_migrate
@Cache("aports")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment