Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

feat(core): add a forges package for parsing git remotes (#3350)

Raghav Chamadiya453397e7e96c
diff --git a/packages/core/src/repowise/core/forges/__init__.py b/packages/core/src/repowise/core/forges/__init__.py
new file mode 100644
index 000000000..a1b0ac391
--- /dev/null
+++ b/packages/core/src/repowise/core/forges/__init__.py
@@ -0,0 +1,34 @@
+"""Git hosts (forges): remote parsing, URL builders, change refs and identity.
+
+Every forge-specific fact (hosts, URL shapes, noreply domains, bot names,
+merge-message conventions) lives in that forge's module here; callers ask
+this package instead of matching hosts themselves. Adding a forge is one
+module that subclasses ``BaseForge`` and calls ``register``.
+"""
+
+from __future__ import annotations
+
+# Forge modules register themselves on import. Their host claims are
+# disjoint, so the order does not matter; generic claims none and is the fallback.
+from . import azure, bitbucket, generic, github, gitlab # noqa: F401
+from .base import BaseForge, Forge, ForgeKind, RemoteRef
+from .detect import detect_forge, read_remote_url
+from .registry import HOSTS_ENV_VAR, all_forges, forge_hosts, get_forge, register
+from .remote import canonical_key, parse_remote, strip_credentials
+
+__all__ = [
+ "HOSTS_ENV_VAR",
+ "BaseForge",
+ "Forge",
+ "ForgeKind",
+ "RemoteRef",
+ "all_forges",
+ "canonical_key",
+ "detect_forge",
+ "forge_hosts",
+ "get_forge",
+ "parse_remote",
+ "read_remote_url",
+ "register",
+ "strip_credentials",
+]
diff --git a/packages/core/src/repowise/core/forges/azure.py b/packages/core/src/repowise/core/forges/azure.py
new file mode 100644
index 000000000..60ed546d0
--- /dev/null
+++ b/packages/core/src/repowise/core/forges/azure.py
@@ -0,0 +1,118 @@
+"""Azure DevOps (Services and Server).
+
+Services answers on three host shapes for one repo: ``dev.azure.com/org``,
+``ssh.dev.azure.com:v3/org`` and the legacy ``org.visualstudio.com``. All three
+fold to ``dev.azure.com`` so a repo has one host, one web base and one key.
+Server (on-prem, formerly TFS) keeps its host and whatever collection path
+sits before ``_git``.
+"""
+
+from __future__ import annotations
+
+from dataclasses import dataclass
+from urllib.parse import quote
+
+from .base import (
+ BaseForge,
+ ForgeKind,
+ RemoteParts,
+ RemoteRef,
+ join_url,
+ looks_like_sha,
+ quote_path,
+ quote_segment,
+)
+from .registry import register
+
+PUBLIC_HOST = "dev.azure.com"
+_PUBLIC_ORIGIN = f"https://{PUBLIC_HOST}"
+_SSH_HOSTS = frozenset({"ssh.dev.azure.com", "vs-ssh.visualstudio.com"})
+_LEGACY_SUFFIX = ".visualstudio.com"
+_GIT = "_git"
+
+
+def _project_repo(rest: tuple[str, ...]) -> tuple[str, str] | None:
+ """``(project, repo)`` from ``project/_git/repo``, or ``_git/repo`` when they share a name."""
+ if len(rest) >= 3 and rest[1].lower() == _GIT:
+ return rest[0], rest[2]
+ if len(rest) >= 2 and rest[0].lower() == _GIT:
+ return rest[1], rest[1]
+ return None
+
+
+def _version(rev: str) -> str:
+ """Azure's version selector: ``GC`` for a commit, ``GB`` for a branch."""
+ return ("GC" if looks_like_sha(rev) else "GB") + quote(rev, safe="/")
+
+
+@dataclass(frozen=True, slots=True)
+class Azure(BaseForge):
+ def claims_host(self, host: str) -> bool:
+ return host == PUBLIC_HOST or host in _SSH_HOSTS or host.endswith(_LEGACY_SUFFIX)
+
+ def _cloud(self, org: str, project: str, repo: str) -> RemoteRef:
+ web_base = join_url(_PUBLIC_ORIGIN, org, project, _GIT, repo)
+ return RemoteRef(self.kind, PUBLIC_HOST, (org, project), repo, web_base, False)
+
+ def parse(self, parts: RemoteParts) -> RemoteRef | None:
+ host, segs = parts.host, parts.segments
+ if host in _SSH_HOSTS:
+ if len(segs) >= 4 and segs[0].lower() == "v3":
+ return self._cloud(segs[1], segs[2], segs[3])
+ return None
+ if host == PUBLIC_HOST:
+ pr = _project_repo(segs[1:]) if segs else None
+ return self._cloud(segs[0], *pr) if pr else None
+ if host.endswith(_LEGACY_SUFFIX):
+ rest = segs[1:] if segs and segs[0].lower() == "defaultcollection" else segs
+ pr = _project_repo(rest)
+ return self._cloud(host[: -len(_LEGACY_SUFFIX)], *pr) if pr else None
+ # Server: everything before `_git` is the collection and project.
+ lowered = [s.lower() for s in segs]
+ if _GIT not in lowered:
+ return None
+ i = lowered.index(_GIT)
+ if i == 0 or i + 1 >= len(segs):
+ return None
+ web_base = join_url(parts.web_origin, *segs[:i], _GIT, segs[i + 1])
+ return RemoteRef(self.kind, host, segs[:i], segs[i + 1], web_base, True)
+
+ def canonical_path(self, parts: RemoteParts) -> str | None:
+ ref = self.parse(parts)
+ if ref is None or ref.is_self_hosted:
+ return None
+ return ref.web_base.removeprefix("https://")
+
+ def commit_url(self, ref: RemoteRef, sha: str) -> str:
+ return f"{ref.web_base}/commit/{quote_segment(sha)}"
+
+ def change_url(self, ref: RemoteRef, number: int) -> str:
+ return f"{ref.web_base}/pullrequest/{number}"
+
+ def blob_url(self, ref: RemoteRef, rev: str, path: str, line: int | None = None) -> str:
+ # The shape Azure's own "copy link to line" produces.
+ url = f"{ref.web_base}?path=/{quote_path(path)}&version={_version(rev)}"
+ if line:
+ url += (
+ f"&line={line}&lineEnd={line + 1}"
+ "&lineStartColumn=1&lineEndColumn=1&lineStyle=plain"
+ )
+ return url + "&_a=contents"
+
+ def compare_url(self, ref: RemoteRef, base: str, head: str) -> str:
+ return (
+ f"{ref.web_base}/branchCompare?baseVersion={_version(base)}"
+ f"&targetVersion={_version(head)}"
+ )
+
+ def format_change_ref(self, number: int) -> str:
+ return f"PR {number}"
+
+
+register(
+ Azure(
+ kind=ForgeKind.AZURE,
+ label="Azure DevOps",
+ ci_env_markers=("TF_BUILD", "SYSTEM_TEAMFOUNDATIONCOLLECTIONURI"),
+ )
+)
diff --git a/packages/core/src/repowise/core/forges/base.py b/packages/core/src/repowise/core/forges/base.py
new file mode 100644
index 000000000..79baf6182
--- /dev/null
+++ b/packages/core/src/repowise/core/forges/base.py
@@ -0,0 +1,162 @@
+"""The forge contract: which host a repository lives on and how to talk about it.
+
+``Forge`` is what callers code against. ``BaseForge`` is what a forge module
+subclasses: the contract plus the hooks the registry and the remote parser use
+(host claims, path shapes, canonical keys), so every fact about one forge sits
+in that forge's module.
+"""
+
+from __future__ import annotations
+
+import re
+from dataclasses import dataclass
+from enum import StrEnum
+from typing import Protocol
+from urllib.parse import quote
+
+
+class ForgeKind(StrEnum):
+ GITHUB = "github"
+ GITLAB = "gitlab"
+ AZURE = "azure"
+ BITBUCKET = "bitbucket"
+ GENERIC = "generic"
+
+
+@dataclass(frozen=True, slots=True)
+class RemoteRef:
+ """One remote, parsed. Fields hold unescaped names; ``web_base`` is escaped."""
+
+ forge: ForgeKind
+ host: str # lowercased, no userinfo, no port
+ namespace: tuple[str, ...] # ("group", "sub") / ("org", "project") / ("owner",)
+ repo: str
+ web_base: str # https://host/group/sub/repo or https://dev.azure.com/org/project/_git/repo
+ is_self_hosted: bool
+
+
+@dataclass(frozen=True, slots=True)
+class RemoteParts:
+ """A remote split into host and path, before any forge reads it.
+
+ ``segments`` are unescaped, with ``.git`` dropped from the last one and any
+ web route (GitLab's ``/-/...``) cut off. ``web_origin`` is where the web UI
+ most likely lives: the remote's own origin for http(s), else https on the
+ bare host, since an ssh port says nothing about the web port.
+ """
+
+ scheme: str # "http", "https", "ssh", "git" or "scp"
+ host: str
+ segments: tuple[str, ...]
+ web_origin: str
+
+
+class Forge(Protocol):
+ kind: ForgeKind
+ label: str # "GitHub", "GitLab", "Azure DevOps", "Bitbucket", "Git"
+ change_term: str # "pull request" | "merge request"
+
+ def commit_url(self, ref: RemoteRef, sha: str) -> str: ...
+ def change_url(self, ref: RemoteRef, number: int) -> str: ...
+ def blob_url(self, ref: RemoteRef, rev: str, path: str, line: int | None = None) -> str: ...
+ def compare_url(self, ref: RemoteRef, base: str, head: str) -> str: ...
+ def format_change_ref(self, number: int) -> str: ...
+ def parse_change_refs(self, subject: str, body: str = "") -> list[int]: ...
+ def normalize_identity(self, email: str, name: str) -> tuple[str, bool]: ...
+
+
+_HOST_RE = re.compile(r"[a-z0-9_](?:[a-z0-9_.-]*[a-z0-9_])?|[0-9a-f:.]*:[0-9a-f:.]*")
+
+
+def split_host_port(authority: str) -> tuple[str, str] | None:
+ """``(host, port)`` from ``host[:port]`` or ``[v6]:port``, host lowercased.
+
+ ``None`` when malformed. Parsed remotes and host overrides both go through
+ here, so an override key always compares equal to a parsed host.
+ """
+ if authority.startswith("["):
+ host, sep, tail = authority[1:].partition("]")
+ if not sep or (tail and not tail.startswith(":")):
+ return None
+ port = tail[1:]
+ else:
+ host, _, port = authority.partition(":")
+ host = host.lower()
+ if not _HOST_RE.fullmatch(host) or (port and not port.isdigit()):
+ return None
+ return host, port
+
+
+#: A full or abbreviated commit id (sha1 or sha256), as opposed to a branch.
+_SHA_RE = re.compile(r"[0-9a-fA-F]{7,64}")
+
+
+def looks_like_sha(rev: str) -> bool:
+ return bool(_SHA_RE.fullmatch(rev))
+
+
+def quote_segment(segment: str) -> str:
+ """Escape one path segment for a URL; spaces in Azure names are the usual case."""
+ return quote(segment, safe="!$&'()*+,;=:@~")
+
+
+def quote_path(path: str) -> str:
+ """Escape a repo-relative file path, keeping its slashes."""
+ return quote(path.lstrip("/"), safe="/!$&'()*+,;=:@~")
+
+
+def join_url(origin: str, *segments: str) -> str:
+ return "/".join([origin, *(quote_segment(s) for s in segments)])
+
+
+@dataclass(frozen=True, slots=True)
+class BaseForge:
+ """Shared defaults; a forge module subclasses this and calls ``register``."""
+
+ kind: ForgeKind
+ label: str
+ change_term: str = "pull request"
+ #: Env vars whose presence means a CI run on this forge.
+ ci_env_markers: tuple[str, ...] = ()
+ #: Path segments that start a web route on this forge and never name a
+ #: group or repo, so they are cut from a URL on any host.
+ route_markers: frozenset[str] = frozenset()
+
+ # -- registry hooks -------------------------------------------------
+
+ def claims_host(self, host: str) -> bool:
+ """Whether *host* is this forge's by name alone (no override needed)."""
+ return False
+
+ def parse(self, parts: RemoteParts) -> RemoteRef | None:
+ """The remote in this forge's path shape, or ``None`` when it does not fit."""
+ raise NotImplementedError
+
+ def canonical_path(self, parts: RemoteParts) -> str | None:
+ """A forge-specific lookup key; ``None`` keeps the plain host/path key."""
+ return None
+
+ # -- contract ---------------------------------------------------------
+
+ def commit_url(self, ref: RemoteRef, sha: str) -> str:
+ raise NotImplementedError
+
+ def change_url(self, ref: RemoteRef, number: int) -> str:
+ raise NotImplementedError
+
+ def blob_url(self, ref: RemoteRef, rev: str, path: str, line: int | None = None) -> str:
+ raise NotImplementedError
+
+ def compare_url(self, ref: RemoteRef, base: str, head: str) -> str:
+ raise NotImplementedError
+
+ def format_change_ref(self, number: int) -> str:
+ return f"#{number}"
+
+ def parse_change_refs(self, subject: str, body: str = "") -> list[int]:
+ """No convention known: no refs."""
+ return []
+
+ def normalize_identity(self, email: str, name: str) -> tuple[str, bool]:
+ """No noreply form or bot account known: the email lowercased, a person."""
+ return (email or "").strip().lower(), False
diff --git a/packages/core/src/repowise/core/forges/bitbucket.py b/packages/core/src/repowise/core/forges/bitbucket.py
new file mode 100644
index 000000000..7109ae38b
--- /dev/null
+++ b/packages/core/src/repowise/core/forges/bitbucket.py
@@ -0,0 +1,109 @@
+"""Bitbucket (Cloud at bitbucket.org, and Data Center / Server).
+
+The two share a name and little else: Cloud is ``workspace/repo`` with
+``/src`` and ``/commits`` pages; Data Center clones from ``/scm/PROJ/repo``
+(ssh on its own port, usually 7999) and browses at ``/projects/PROJ/repos/repo``.
+"""
+
+from __future__ import annotations
+
+from dataclasses import dataclass
+from urllib.parse import quote
+
+from .base import (
+ BaseForge,
+ ForgeKind,
+ RemoteParts,
+ RemoteRef,
+ join_url,
+ quote_path,
+ quote_segment,
+)
+from .registry import register
+
+PUBLIC_HOST = "bitbucket.org"
+_PUBLIC_ORIGIN = f"https://{PUBLIC_HOST}"
+_HOST_ALIASES = frozenset({PUBLIC_HOST, "www.bitbucket.org", "altssh.bitbucket.org"})
+
+
+def _data_center(parts: RemoteParts) -> tuple[tuple[str, ...], str, str] | None:
+ """``(context path, project, repo)`` from a Data Center clone or browse URL."""
+ segs = parts.segments
+ if parts.scheme not in ("http", "https"):
+ return ((), segs[0], segs[1]) if len(segs) >= 2 else None
+ lowered = [s.lower() for s in segs]
+ if "scm" in lowered:
+ i = lowered.index("scm")
+ return (segs[:i], segs[i + 1], segs[i + 2]) if i + 2 < len(segs) else None
+ if "projects" in lowered:
+ i = lowered.index("projects")
+ if i + 3 < len(segs) and lowered[i + 2] == "repos":
+ return segs[:i], segs[i + 1], segs[i + 3]
+ return None
+
+
+@dataclass(frozen=True, slots=True)
+class Bitbucket(BaseForge):
+ def claims_host(self, host: str) -> bool:
+ return host in _HOST_ALIASES
+
+ def parse(self, parts: RemoteParts) -> RemoteRef | None:
+ segs = parts.segments
+ if parts.host in _HOST_ALIASES:
+ if len(segs) < 2:
+ return None
+ web_base = join_url(_PUBLIC_ORIGIN, segs[0], segs[1])
+ return RemoteRef(self.kind, PUBLIC_HOST, (segs[0],), segs[1], web_base, False)
+ found = _data_center(parts)
+ if found is None:
+ return None
+ context, project, repo = found
+ # Data Center project keys are uppercase; ssh remotes often spell them
+ # lowercase, and both must name one project.
+ project = project if project.startswith("~") else project.upper()
+ # A personal repo's project is ``~user``; its pages live under /users.
+ owner = ("users", project[1:]) if project.startswith("~") else ("projects", project)
+ web_base = join_url(parts.web_origin, *context, *owner, "repos", repo)
+ return RemoteRef(self.kind, parts.host, (project,), repo, web_base, True)
+
+ def canonical_path(self, parts: RemoteParts) -> str | None:
+ """Data Center keys as ``host/PROJ/repo``: its http clone (``/scm/``,
+ maybe under a context path), ssh clone and browse URLs all match."""
+ if parts.host in _HOST_ALIASES:
+ return None
+ ref = self.parse(parts)
+ if ref is None:
+ return None
+ return "/".join([ref.host, *(quote_segment(s) for s in (*ref.namespace, ref.repo))])
+
+ def commit_url(self, ref: RemoteRef, sha: str) -> str:
+ return f"{ref.web_base}/commits/{quote_segment(sha)}"
+
+ def change_url(self, ref: RemoteRef, number: int) -> str:
+ suffix = "/overview" if ref.is_self_hosted else ""
+ return f"{ref.web_base}/pull-requests/{number}{suffix}"
+
+ def blob_url(self, ref: RemoteRef, rev: str, path: str, line: int | None = None) -> str:
+ if ref.is_self_hosted:
+ anchor = f"#{line}" if line else ""
+ return f"{ref.web_base}/browse/{quote_path(path)}?at={quote(rev, safe='')}{anchor}"
+ anchor = f"#lines-{line}" if line else ""
+ return f"{ref.web_base}/src/{quote_path(rev)}/{quote_path(path)}{anchor}"
+
+ def compare_url(self, ref: RemoteRef, base: str, head: str) -> str:
+ if ref.is_self_hosted:
+ return (
+ f"{ref.web_base}/compare/commits?sourceBranch={quote(head, safe='')}"
+ f"&targetBranch={quote(base, safe='')}"
+ )
+ # Cloud joins the two revs with a CR, head first.
+ return f"{ref.web_base}/branches/compare/{quote_path(head)}%0D{quote_path(base)}"
+
+
+register(
+ Bitbucket(
+ kind=ForgeKind.BITBUCKET,
+ label="Bitbucket",
+ ci_env_markers=("BITBUCKET_BUILD_NUMBER",),
+ )
+)
diff --git a/packages/core/src/repowise/core/forges/detect.py b/packages/core/src/repowise/core/forges/detect.py
new file mode 100644
index 000000000..533529d16
--- /dev/null
+++ b/packages/core/src/repowise/core/forges/detect.py
@@ -0,0 +1,82 @@
+"""Which forge hosts a checkout: its remote first, then the CI it runs in.
+
+Reads ``.git/config`` directly rather than shelling out to git: this runs on
+CLI start-up and server page loads, where a process spawn per call costs more
+than reading one small file.
+"""
+
+from __future__ import annotations
+
+import configparser
+import os
+from collections.abc import Mapping
+from pathlib import Path
+
+from .base import ForgeKind
+from .registry import all_forges, forge_hosts
+from .remote import parse_remote
+
+_REMOTES = ('remote "origin"', 'remote "upstream"')
+
+
+def _common_git_dir(repo_root: Path) -> Path | None:
+ """The directory holding the shared ``config``, following a worktree's ``.git`` file."""
+ git_path = repo_root / ".git"
+ if not git_path.is_file():
+ return git_path if git_path.is_dir() else None
+ try:
+ pointer = git_path.read_text(encoding="utf-8").strip()
+ except OSError:
+ return None
+ if not pointer.startswith("gitdir:"):
+ return None
+ gitdir = Path(pointer.split(":", 1)[1].strip())
+ if not gitdir.is_absolute():
+ gitdir = (repo_root / gitdir).resolve()
+ # A linked worktree names its main git dir in `commondir`; a submodule's
+ # gitdir holds its own config.
+ try:
+ common = (gitdir / "commondir").read_text(encoding="utf-8").strip()
+ except OSError:
+ return gitdir
+ common_dir = Path(common)
+ return common_dir if common_dir.is_absolute() else (gitdir / common_dir).resolve()
+
+
+def read_remote_url(repo_root: Path | str) -> str | None:
+ """``origin``'s URL, else ``upstream``'s, else ``None``. Never raises."""
+ git_dir = _common_git_dir(Path(repo_root))
+ if git_dir is None:
+ return None
+ # strict=False: git writes duplicate keys (two fetch refspecs) routinely.
+ # interpolation=None: a `%40` in a remote's userinfo is not a template.
+ parser = configparser.ConfigParser(strict=False, interpolation=None)
+ try:
+ parser.read(git_dir / "config", encoding="utf-8")
+ for section in _REMOTES:
+ if parser.has_option(section, "url"):
+ return parser.get(section, "url").strip() or None
+ except (OSError, configparser.Error, UnicodeDecodeError):
+ return None
+ return None
+
+
+def detect_forge(
+ repo_root: Path | str | None = None, env: Mapping[str, str] | None = None
+) -> ForgeKind:
+ """The forge for a checkout: remote, then CI env vars, else ``GENERIC``.
+
+ A remote on an unknown host does not settle it: a self-managed GitLab on
+ ``git.corp.com`` with no override is still GitLab when ``GITLAB_CI`` says so.
+ ``repo_root=None`` skips the remote; ``env=None`` reads ``os.environ``.
+ """
+ env = os.environ if env is None else env
+ if repo_root is not None:
+ url = read_remote_url(repo_root)
+ ref = parse_remote(url, hosts=forge_hosts(repo_root, env)) if url else None
+ if ref is not None and ref.forge is not ForgeKind.GENERIC:
+ return ref.forge
+ for forge in all_forges():
+ if any(env.get(var) for var in forge.ci_env_markers):
+ return forge.kind
+ return ForgeKind.GENERIC
diff --git a/packages/core/src/repowise/core/forges/generic.py b/packages/core/src/repowise/core/forges/generic.py
new file mode 100644
index 000000000..8a4828cc7
--- /dev/null
+++ b/packages/core/src/repowise/core/forges/generic.py
@@ -0,0 +1,37 @@
+"""Any other git host: Gitea, cgit, a bare ssh server, or a forge with no override.
+
+Nothing is known about its web pages, so every URL builder returns ``""`` and
+callers render plain text.
+"""
+
+from __future__ import annotations
+
+from dataclasses import dataclass
+
+from .base import BaseForge, ForgeKind, RemoteParts, RemoteRef, join_url
+from .registry import register
+
+
+@dataclass(frozen=True, slots=True)
+class Generic(BaseForge):
+ def parse(self, parts: RemoteParts) -> RemoteRef | None:
+ segs = parts.segments
+ if not segs:
+ return None
+ web_base = join_url(parts.web_origin, *segs)
+ return RemoteRef(self.kind, parts.host, segs[:-1], segs[-1], web_base, True)
+
+ def commit_url(self, ref: RemoteRef, sha: str) -> str:
+ return ""
+
+ def change_url(self, ref: RemoteRef, number: int) -> str:
+ return ""
+
+ def blob_url(self, ref: RemoteRef, rev: str, path: str, line: int | None = None) -> str:
+ return ""
+
+ def compare_url(self, ref: RemoteRef, base: str, head: str) -> str:
+ return ""
+
+
+register(Generic(kind=ForgeKind.GENERIC, label="Git"))
diff --git a/packages/core/src/repowise/core/forges/github.py b/packages/core/src/repowise/core/forges/github.py
new file mode 100644
index 000000000..e78738674
--- /dev/null
+++ b/packages/core/src/repowise/core/forges/github.py
@@ -0,0 +1,60 @@
+"""GitHub (github.com and Enterprise Server)."""
+
+from __future__ import annotations
+
+from dataclasses import dataclass
+
+from .base import (
+ BaseForge,
+ ForgeKind,
+ RemoteParts,
+ RemoteRef,
+ join_url,
+ quote_path,
+ quote_segment,
+)
+from .registry import register
+
+PUBLIC_HOST = "github.com"
+_PUBLIC_ORIGIN = f"https://{PUBLIC_HOST}"
+# ssh.github.com serves ssh over port 443; both aliases are github.com.
+_HOST_ALIASES = frozenset({PUBLIC_HOST, "www.github.com", "ssh.github.com"})
+
+
+@dataclass(frozen=True, slots=True)
+class GitHub(BaseForge):
+ def claims_host(self, host: str) -> bool:
+ return host in _HOST_ALIASES
+
+ def parse(self, parts: RemoteParts) -> RemoteRef | None:
+ if len(parts.segments) < 2:
+ return None
+ owner, repo = parts.segments[0], parts.segments[1]
+ public = parts.host in _HOST_ALIASES
+ host = PUBLIC_HOST if public else parts.host
+ origin = _PUBLIC_ORIGIN if public else parts.web_origin
+ return RemoteRef(
+ self.kind, host, (owner,), repo, join_url(origin, owner, repo), not public
+ )
+
+ def commit_url(self, ref: RemoteRef, sha: str) -> str:
+ return f"{ref.web_base}/commit/{quote_segment(sha)}"
+
+ def change_url(self, ref: RemoteRef, number: int) -> str:
+ return f"{ref.web_base}/pull/{number}"
+
+ def blob_url(self, ref: RemoteRef, rev: str, path: str, line: int | None = None) -> str:
+ anchor = f"#L{line}" if line else ""
+ return f"{ref.web_base}/blob/{quote_path(rev)}/{quote_path(path)}{anchor}"
+
+ def compare_url(self, ref: RemoteRef, base: str, head: str) -> str:
+ return f"{ref.web_base}/compare/{quote_path(base)}...{quote_path(head)}"
+
+
+register(
+ GitHub(
+ kind=ForgeKind.GITHUB,
+ label="GitHub",
+ ci_env_markers=("GITHUB_ACTIONS",),
+ )
+)
diff --git a/packages/core/src/repowise/core/forges/gitlab.py b/packages/core/src/repowise/core/forges/gitlab.py
new file mode 100644
index 000000000..126d83d01
--- /dev/null
+++ b/packages/core/src/repowise/core/forges/gitlab.py
@@ -0,0 +1,65 @@
+"""GitLab (gitlab.com and self-managed)."""
+
+from __future__ import annotations
+
+from dataclasses import dataclass
+
+from .base import (
+ BaseForge,
+ ForgeKind,
+ RemoteParts,
+ RemoteRef,
+ join_url,
+ quote_path,
+ quote_segment,
+)
+from .registry import register
+
+PUBLIC_HOST = "gitlab.com"
+
+
+@dataclass(frozen=True, slots=True)
+class GitLab(BaseForge):
+ def claims_host(self, host: str) -> bool:
+ # gitlab.com, and a self-managed host named for it (gitlab.corp.com).
+ return "gitlab" in host.split(".")
+
+ def parse(self, parts: RemoteParts) -> RemoteRef | None:
+ segs = parts.segments
+ if len(segs) < 2:
+ return None
+ return RemoteRef(
+ self.kind,
+ parts.host,
+ segs[:-1],
+ segs[-1],
+ join_url(parts.web_origin, *segs),
+ parts.host != PUBLIC_HOST,
+ )
+
+ def commit_url(self, ref: RemoteRef, sha: str) -> str:
+ return f"{ref.web_base}/-/commit/{quote_segment(sha)}"
+
+ def change_url(self, ref: RemoteRef, number: int) -> str:
+ return f"{ref.web_base}/-/merge_requests/{number}"
+
+ def blob_url(self, ref: RemoteRef, rev: str, path: str, line: int | None = None) -> str:
+ anchor = f"#L{line}" if line else ""
+ return f"{ref.web_base}/-/blob/{quote_path(rev)}/{quote_path(path)}{anchor}"
+
+ def compare_url(self, ref: RemoteRef, base: str, head: str) -> str:
+ return f"{ref.web_base}/-/compare/{quote_path(base)}...{quote_path(head)}"
+
+ def format_change_ref(self, number: int) -> str:
+ return f"!{number}"
+
+
+register(
+ GitLab(
+ kind=ForgeKind.GITLAB,
+ label="GitLab",
+ change_term="merge request",
+ ci_env_markers=("GITLAB_CI",),
+ route_markers=frozenset({"-"}),
+ )
+)
diff --git a/packages/core/src/repowise/core/forges/registry.py b/packages/core/src/repowise/core/forges/registry.py
new file mode 100644
index 000000000..3436d18af
--- /dev/null
+++ b/packages/core/src/repowise/core/forges/registry.py
@@ -0,0 +1,104 @@
+"""Registered forges, and which one a host belongs to.
+
+A host resolves in this order: an explicit override (``forges.hosts`` in the
+repo's ``.repowise/config.yaml``, then ``REPOWISE_FORGE_HOSTS``), then a
+forge claiming the host by name, else generic. Nothing is probed over the
+network, so a self-managed host with a neutral name needs an override.
+"""
+
+from __future__ import annotations
+
+import os
+from collections.abc import Iterator, Mapping
+from functools import lru_cache
+from pathlib import Path
+
+from .base import BaseForge, ForgeKind, split_host_port
+
+#: ``git.corp.com=gitlab,tfs.corp=azure``: CI sets this where no config is committed.
+HOSTS_ENV_VAR = "REPOWISE_FORGE_HOSTS"
+
+_FORGES: dict[ForgeKind, BaseForge] = {}
+
+
+def register(forge: BaseForge) -> None:
+ """Add or replace a forge. Registration order is host-claim order."""
+ _FORGES[forge.kind] = forge
+ _claimed.cache_clear()
+
+
+def all_forges() -> Iterator[BaseForge]:
+ return iter(_FORGES.values())
+
+
+def get_forge(kind: ForgeKind | str) -> BaseForge:
+ return _FORGES[ForgeKind(kind)]
+
+
+@lru_cache(maxsize=1024)
+def _claimed(host: str) -> BaseForge:
+ for forge in _FORGES.values():
+ if forge.claims_host(host):
+ return forge
+ return _FORGES[ForgeKind.GENERIC]
+
+
+def forge_for_host(host: str, hosts: Mapping[str, ForgeKind] | None = None) -> BaseForge:
+ """The forge serving *host*: an override in *hosts* first, then the name."""
+ if hosts:
+ kind = hosts.get(host)
+ if kind is None:
+ kind = next((v for k, v in hosts.items() if host_key(k) == host), None)
+ if kind is not None:
+ return get_forge(kind)
+ return _claimed(host)
+
+
+def host_key(raw: str) -> str:
+ """An override key as a parsed remote's host, ``""`` when it names none.
+
+ ``Git.Corp.com:8443`` and ``https://git.corp.com/`` become ``git.corp.com``;
+ ``[::1]:80`` and a bare ``::1`` become ``::1``.
+ """
+ authority = raw.strip().split("://", 1)[-1].split("/", 1)[0].rpartition("@")[2]
+ if authority.count(":") > 1 and not authority.startswith("["):
+ authority = f"[{authority}]" # a bare IPv6 address
+ hp = split_host_port(authority)
+ return hp[0] if hp else ""
+
+
+def _merge(out: dict[str, ForgeKind], items: Iterator[tuple[object, object]]) -> None:
+ for raw_host, raw_kind in items:
+ try:
+ kind = ForgeKind(str(raw_kind).strip().lower())
+ except ValueError:
+ continue # an unknown forge name is ignored, not fatal
+ host = host_key(str(raw_host))
+ if host:
+ out[host] = kind
+
+
+def forge_hosts(
+ repo_root: Path | str | None = None, env: Mapping[str, str] | None = None
+) -> dict[str, ForgeKind]:
+ """Self-managed host overrides for a repo: its config, then the env on top.
+
+ The env wins so a CI job can correct a committed mapping. A config that
+ cannot be read contributes nothing rather than failing detection.
+ """
+ out: dict[str, ForgeKind] = {}
+ if repo_root is not None:
+ from ..repo_config import RepoConfigError, load_repo_config
+
+ try:
+ config = load_repo_config(repo_root)
+ except (OSError, RepoConfigError):
+ config = {}
+ section = config.get("forges")
+ hosts = section.get("hosts") if isinstance(section, dict) else None
+ if isinstance(hosts, dict):
+ _merge(out, iter(hosts.items()))
+ raw = (os.environ if env is None else env).get(HOSTS_ENV_VAR, "")
+ pairs = (item.partition("=") for item in raw.split(",") if "=" in item)
+ _merge(out, ((h, k) for h, _, k in pairs))
+ return out
diff --git a/packages/core/src/repowise/core/forges/remote.py b/packages/core/src/repowise/core/forges/remote.py
new file mode 100644
index 000000000..19f9ef583
--- /dev/null
+++ b/packages/core/src/repowise/core/forges/remote.py
@@ -0,0 +1,175 @@
+"""Remote URLs: parse one into a ``RemoteRef``, key it for lookups, scrub secrets.
+
+Pure string work, no I/O. Accepts every shape git does for a network remote:
+``https://``, ``ssh://``, ``git://``, scp-style ``user@host:path`` and a bare
+``host/path`` as typed into a form. Local paths and ``file://`` are not remotes
+and yield ``None``.
+"""
+
+from __future__ import annotations
+
+import re
+from collections.abc import Mapping
+from urllib.parse import unquote
+
+from .base import ForgeKind, RemoteParts, RemoteRef, quote_segment, split_host_port
+from .registry import all_forges, forge_for_host
+
+_URL_SCHEME_RE = re.compile(r"([A-Za-z][A-Za-z0-9+.-]*)://")
+_SSH_SCHEMES = frozenset({"ssh", "git+ssh", "ssh+git"})
+#: Scheme -> the transport it names. Anything else is not a network remote.
+_TRANSPORTS = {
+ "https": "https",
+ "http": "http",
+ "git+https": "https",
+ "git+http": "http",
+ "ssh": "ssh",
+ "git+ssh": "ssh",
+ "ssh+git": "ssh",
+ "git": "git",
+}
+# git's own rule: a colon before any slash makes ``host:path`` scp-style.
+_SCP_RE = re.compile(r"(?:[^@/]+@)?(?P<host>\[[^\]/]+\]|[^:/@\[\]]+):(?P<path>.*)", re.DOTALL)
+_QUERY_RE = re.compile(r"[?#]")
+
+
+def strip_credentials(url: str) -> str:
+ """The remote with every secret removed, safe to log or send to a browser.
+
+ A token can sit in the userinfo (``https://user:glpat-x@host/...``,
+ ``git+https://ghp_x@host/...``) or the query (``?private_token=``). Every
+ scheme loses its query, fragment and userinfo, except that ssh keeps a bare
+ username: ``ssh://git@host/...`` and scp-style ``git@host:path`` name an
+ account, not a secret. The scheme keeps its case.
+ """
+ if not url:
+ return url
+ url = url.strip()
+ scheme_match = _URL_SCHEME_RE.match(url)
+ if not scheme_match:
+ # scp-style `user[:password]@host:path`. Git never sends a password here,
+ # but one typed in (even with a `/`) would still reach the browser. A
+ # local path (`/srv/a:b@c`, `.\x`, `C:\x`) is not a remote, and without a
+ # `host:` after the `@` the `@` is in the path (`host:org/[email protected]`).
+ userinfo, at, rest = url.partition("@")
+ local = "\\" in userinfo or userinfo[:1] in ("/", ".")
+ if at and ":" in userinfo and ":" in rest and not local:
+ return f"{userinfo.split(':', 1)[0]}@{rest}"
+ return url
+ scheme = scheme_match.group(1)
+ rest = url[scheme_match.end() :]
+ userinfo, hostpart = "", rest
+ # Userinfo is found before the query is cut, so a `?` or `#` typed into a
+ # password cannot split it and leave a prefix behind.
+ head = rest.split("/", 1)[0]
+ if "@" in head:
+ userinfo, _, host = head.rpartition("@")
+ hostpart = host + rest[len(head) :]
+ elif ":" in head:
+ # An unencoded `/` in a password moves the authority past the first
+ # `/`, and `user:123/x` even passes for host:port. Such a password
+ # always leaves a `:` before that `/`, so only then is a later `@` the
+ # end of the userinfo; `host/[email protected]` keeps its path. Ceiling:
+ # `host:8443/[email protected]` loses its host. A port plus a path `@` is
+ # rare, and losing a host beats a leak.
+ before_query = _QUERY_RE.split(rest, maxsplit=1)[0]
+ if "@" in before_query:
+ userinfo, _, hostpart = before_query.partition("@")
+ hostpart = _QUERY_RE.split(hostpart, maxsplit=1)[0]
+ user = userinfo.split(":", 1)[0] if scheme.lower() in _SSH_SCHEMES else ""
+ return f"{scheme}://{user + '@' if user else ''}{hostpart}"
+
+
+def _route_markers() -> frozenset[str]:
+ return frozenset().union(*(f.route_markers for f in all_forges()))
+
+
+def _locate(raw: str) -> tuple[str, str, str] | None:
+ """``(transport, authority, path)`` of a scrubbed remote; ``None`` for a non-remote."""
+ scheme_match = _URL_SCHEME_RE.match(raw)
+ scp = None if scheme_match else _SCP_RE.fullmatch(raw)
+ if scp and "@" not in raw.split(":", 1)[0] and scp.group("path").split("/", 1)[0].isdigit():
+ scp = None # `host:8443/g/p` as typed into a form: a port, not a path
+ if scheme_match:
+ transport = _TRANSPORTS.get(scheme_match.group(1).lower())
+ if transport is None:
+ return None
+ authority, _, path = raw[scheme_match.end() :].partition("/")
+ elif scp:
+ transport, authority, path = "scp", scp.group("host"), scp.group("path")
+ else:
+ transport = "https"
+ authority, _, path = _QUERY_RE.split(raw, maxsplit=1)[0].partition("/")
+ return transport, authority.rpartition("@")[2], path
+
+
+def _segments(path: str) -> tuple[str, ...]:
+ """Unescaped path segments, web routes cut, ``.git`` dropped from the repo.
+
+ A bare ``.git`` leaves an empty repo segment, which no forge parses.
+ """
+ segments = [unquote(s) for s in path.split("/") if s]
+ markers = _route_markers()
+ for i, seg in enumerate(segments):
+ if seg in markers:
+ del segments[i:]
+ break
+ if segments and segments[-1].endswith(".git"):
+ segments[-1] = segments[-1][:-4]
+ return tuple(segments)
+
+
+def split_remote(url: str) -> RemoteParts | None:
+ """Host, unescaped path segments and web origin; ``None`` for a non-remote."""
+ # Scrubbed first, so only an ssh or scp username can still precede the host.
+ raw = strip_credentials((url or "").strip())
+ if not raw or "\\" in raw:
+ return None # a Windows path, never a URL
+ located = _locate(raw)
+ hp = split_host_port(located[1]) if located else None
+ if located is None or hp is None:
+ return None
+ transport, _, path = located
+ host, port = hp
+ if transport == "scp" and len(host) == 1:
+ return None # `C:/repos/x` is a drive letter, as git itself reads it
+ shown = f"[{host}]" if ":" in host else host
+ if transport in ("http", "https"):
+ web_origin = f"{transport}://{shown}{':' + port if port else ''}"
+ else:
+ web_origin = f"https://{shown}"
+ return RemoteParts(transport, host, _segments(path), web_origin)
+
+
+def parse_remote(url: str, *, hosts: Mapping[str, ForgeKind] | None = None) -> RemoteRef | None:
+ """The forge, namespace and repo a remote names, or ``None``.
+
+ *hosts* maps self-managed hosts to their forge and wins over the host-name
+ heuristics; ``forge_hosts(repo_root)`` builds it from config and env.
+ ``None`` also when the path does not fit the forge's shape (a GitHub URL
+ naming only an owner, an Azure URL without ``_git``).
+ """
+ parts = split_remote(url)
+ if parts is None or not all(parts.segments):
+ return None
+ return forge_for_host(parts.host, hosts).parse(parts)
+
+
+def canonical_key(url: str, *, hosts: Mapping[str, ForgeKind] | None = None) -> str | None:
+ """A stable, lowercased ``host/path`` key: clone and web URLs of one repo match.
+
+ Drops scheme, userinfo, port, query, ``.git``, trailing slashes and web
+ routes after GitLab's ``/-/``. The whole key is lowercased because GitHub
+ and GitLab resolve repo paths case-insensitively. Azure's three URL shapes
+ (``dev.azure.com``, ``ssh.dev.azure.com:v3``, ``*.visualstudio.com``) fold
+ to ``dev.azure.com/org/project/_git/repo``. *hosts* names self-managed
+ forges as in ``parse_remote``; a host mapped to Bitbucket keys its Data
+ Center clone and browse URLs as ``host/PROJ/repo``.
+ """
+ parts = split_remote(url)
+ if parts is None:
+ return None
+ key = forge_for_host(parts.host, hosts).canonical_path(parts)
+ if key is None:
+ key = "/".join([parts.host, *(quote_segment(s) for s in parts.segments)])
+ return key.rstrip("/").lower()
diff --git a/tests/unit/core/test_forges_detect.py b/tests/unit/core/test_forges_detect.py
new file mode 100644
index 000000000..9a2d34b2f
--- /dev/null
+++ b/tests/unit/core/test_forges_detect.py
@@ -0,0 +1,146 @@
+"""Which forge a checkout is on: its remote, host overrides, then CI env."""
+
+from __future__ import annotations
+
+from pathlib import Path
+
+import pytest
+
+from repowise.core.forges import (
+ HOSTS_ENV_VAR,
+ ForgeKind,
+ detect_forge,
+ forge_hosts,
+ read_remote_url,
+)
+
+
+def _checkout(root: Path, config: str) -> Path:
+ (root / ".git").mkdir(parents=True)
+ (root / ".git" / "config").write_text(config, encoding="utf-8")
+ return root
+
+
+def _origin(url: str, name: str = "origin") -> str:
+ # Duplicate keys and a `%` are both legal in a git config.
+ return (
+ f'[remote "{name}"]\n\turl = {url}\n'
+ "\tfetch = +refs/heads/*:refs/remotes/origin/*\n"
+ "\tfetch = +refs/pull/*:refs/remotes/origin/pr/*\n"
+ )
+
+
+ ("url", "kind"),
+ [
+ ("[email protected]:o/r.git", ForgeKind.GITHUB),
+ ("https://gitlab.com/g/sub/p.git", ForgeKind.GITLAB),
+ ("https://user%[email protected]/org/proj/_git/repo", ForgeKind.AZURE),
+ ("[email protected]:ws/repo.git", ForgeKind.BITBUCKET),
+ ("https://gitea.example.org/t/r.git", ForgeKind.GENERIC),
+ ],
+)
+def test_the_remote_names_the_forge(tmp_path: Path, url: str, kind: ForgeKind) -> None:
+ root = _checkout(tmp_path, _origin(url))
+ assert detect_forge(root, env={}) is kind
+
+
+def test_upstream_is_read_when_there_is_no_origin(tmp_path: Path) -> None:
+ root = _checkout(tmp_path, _origin("[email protected]:g/p.git", "upstream"))
+ assert detect_forge(root, env={}) is ForgeKind.GITLAB
+
+
+def test_a_linked_worktree_reads_the_main_checkout_config(tmp_path: Path) -> None:
+ main = _checkout(tmp_path / "main", _origin("[email protected]:g/p.git"))
+ gitdir = main / ".git" / "worktrees" / "feature"
+ gitdir.mkdir(parents=True)
+ (gitdir / "commondir").write_text("../..\n", encoding="utf-8")
+ linked = tmp_path / "linked"
+ linked.mkdir()
+ (linked / ".git").write_text(f"gitdir: {gitdir}\n", encoding="utf-8")
+
+ assert read_remote_url(linked) == "[email protected]:g/p.git"
+ assert detect_forge(linked, env={}) is ForgeKind.GITLAB
+
+
+def test_a_broken_config_reads_as_no_remote(tmp_path: Path) -> None:
+ root = _checkout(tmp_path, "this is not ini\n=== nope ===\n")
+ assert read_remote_url(root) is None
+ assert detect_forge(root, env={}) is ForgeKind.GENERIC
+
+
+def test_no_git_dir_reads_as_no_remote(tmp_path: Path) -> None:
+ assert read_remote_url(tmp_path) is None
+
+
+ ("env", "kind"),
+ [
+ ({"GITHUB_ACTIONS": "true"}, ForgeKind.GITHUB),
+ ({"GITLAB_CI": "true"}, ForgeKind.GITLAB),
+ ({"TF_BUILD": "True"}, ForgeKind.AZURE),
+ ({"SYSTEM_TEAMFOUNDATIONCOLLECTIONURI": "https://dev.azure.com/org/"}, ForgeKind.AZURE),
+ ({"BITBUCKET_BUILD_NUMBER": "42"}, ForgeKind.BITBUCKET),
+ ({}, ForgeKind.GENERIC),
+ ],
+)
+def test_ci_env_decides_when_there_is_no_remote(env: dict[str, str], kind: ForgeKind) -> None:
+ assert detect_forge(None, env=env) is kind
+
+
+def test_an_unknown_remote_host_defers_to_ci(tmp_path: Path) -> None:
+ root = _checkout(tmp_path, _origin("[email protected]:g/p.git"))
+ assert detect_forge(root, env={"GITLAB_CI": "true"}) is ForgeKind.GITLAB
+
+
+def test_a_known_remote_beats_ci(tmp_path: Path) -> None:
+ root = _checkout(tmp_path, _origin("[email protected]:o/r.git"))
+ assert detect_forge(root, env={"GITLAB_CI": "true"}) is ForgeKind.GITHUB
+
+
+def test_hosts_from_config_name_a_self_managed_forge(tmp_path: Path) -> None:
+ root = _checkout(tmp_path, _origin("ssh://[email protected]:2222/g/p.git"))
+ (root / ".repowise").mkdir()
+ (root / ".repowise" / "config.yaml").write_text(
+ "forges:\n hosts:\n Git.Corp.com: gitlab\n tfs.corp: nonsense\n",
+ encoding="utf-8",
+ )
+ assert forge_hosts(root, env={}) == {"git.corp.com": ForgeKind.GITLAB}
+ assert detect_forge(root, env={}) is ForgeKind.GITLAB
+
+
+def test_the_env_override_wins_over_config(tmp_path: Path) -> None:
+ root = _checkout(tmp_path, _origin("ssh://[email protected]/g/p.git"))
+ (root / ".repowise").mkdir()
+ (root / ".repowise" / "config.yaml").write_text(
+ "forges:\n hosts:\n git.corp.com: gitlab\n", encoding="utf-8"
+ )
+ env = {HOSTS_ENV_VAR: "git.corp.com=azure, https://bb.corp:7990/ = bitbucket ,junk"}
+ assert forge_hosts(root, env=env) == {
+ "git.corp.com": ForgeKind.AZURE,
+ "bb.corp": ForgeKind.BITBUCKET,
+ }
+
+
+ ("key", "url"),
+ [
+ ("[::1]:80", "http://[::1]:80/g/p.git"),
+ ("::1", "ssh://git@[::1]:2222/g/p.git"),
+ ("https://[email protected]:8443/x", "[email protected]:g/p.git"),
+ ],
+)
+def test_override_keys_match_parsed_hosts(key: str, url: str) -> None:
+ from repowise.core.forges import parse_remote
+
+ hosts = forge_hosts(None, env={HOSTS_ENV_VAR: f"{key}=gitlab"})
+ ref = parse_remote(url, hosts=hosts)
+ assert ref is not None and ref.forge is ForgeKind.GITLAB
+ direct = parse_remote(url, hosts={key: ForgeKind.GITLAB})
+ assert direct is not None and direct.forge is ForgeKind.GITLAB
+
+
+def test_a_broken_repo_config_contributes_no_hosts(tmp_path: Path) -> None:
+ (tmp_path / ".repowise").mkdir()
+ (tmp_path / ".repowise" / "config.yaml").write_text("forges: [unclosed\n", encoding="utf-8")
+ assert forge_hosts(tmp_path, env={}) == {}
diff --git a/tests/unit/core/test_forges_remote.py b/tests/unit/core/test_forges_remote.py
new file mode 100644
index 000000000..ce68a017c
--- /dev/null
+++ b/tests/unit/core/test_forges_remote.py
@@ -0,0 +1,350 @@
+"""Remote parsing, canonical keys and credential scrubbing across forges."""
+
+from __future__ import annotations
+
+import pytest
+
+from repowise.core.forges import (
+ ForgeKind,
+ RemoteRef,
+ canonical_key,
+ parse_remote,
+ strip_credentials,
+)
+
+GH, GL, AZ, BB, GEN = (
+ ForgeKind.GITHUB,
+ ForgeKind.GITLAB,
+ ForgeKind.AZURE,
+ ForgeKind.BITBUCKET,
+ ForgeKind.GENERIC,
+)
+
+#: Self-managed hosts as a repo's config or REPOWISE_FORGE_HOSTS would map them.
+HOSTS = {
+ "ghe.corp.com": GH,
+ "git.corp.com": GL,
+ "tfs.corp": AZ,
+ "bitbucket.corp.com": BB,
+}
+
+# (url, forge, host, namespace, repo, web_base, canonical_key, is_self_hosted)
+MATRIX = [
+ # GitHub
+ ("https://github.com/Org/Repo.git", GH, "github.com", ("Org",), "Repo",
+ "https://github.com/Org/Repo", "github.com/org/repo", False),
+ ("[email protected]:Org/Repo.git", GH, "github.com", ("Org",), "Repo",
+ "https://github.com/Org/Repo", "github.com/org/repo", False),
+ ("ssh://[email protected]/org/repo", GH, "github.com", ("org",), "repo",
+ "https://github.com/org/repo", "github.com/org/repo", False),
+ ("ssh://[email protected]:443/org/repo.git", GH, "github.com", ("org",), "repo",
+ "https://github.com/org/repo", "ssh.github.com/org/repo", False),
+ ("https://x-access-token:[email protected]/org/repo.git", GH, "github.com", ("org",),
+ "repo", "https://github.com/org/repo", "github.com/org/repo", False),
+ ("https://github.com/org/repo/", GH, "github.com", ("org",), "repo",
+ "https://github.com/org/repo", "github.com/org/repo", False),
+ ("github.com/org/repo", GH, "github.com", ("org",), "repo",
+ "https://github.com/org/repo", "github.com/org/repo", False),
+ ("git://github.com/org/repo.git", GH, "github.com", ("org",), "repo",
+ "https://github.com/org/repo", "github.com/org/repo", False),
+ # GitHub Enterprise Server: only with a hosts override.
+ ("https://ghe.corp.com/team/svc.git", GH, "ghe.corp.com", ("team",), "svc",
+ "https://ghe.corp.com/team/svc", "ghe.corp.com/team/svc", True),
+ ("[email protected]:team/svc.git", GH, "ghe.corp.com", ("team",), "svc",
+ "https://ghe.corp.com/team/svc", "ghe.corp.com/team/svc", True),
+ # GitLab
+ ("https://gitlab.com/group/proj.git", GL, "gitlab.com", ("group",), "proj",
+ "https://gitlab.com/group/proj", "gitlab.com/group/proj", False),
+ ("[email protected]:g/sub/sub2/proj.git", GL, "gitlab.com", ("g", "sub", "sub2"), "proj",
+ "https://gitlab.com/g/sub/sub2/proj", "gitlab.com/g/sub/sub2/proj", False),
+ ("https://gitlab.com/g/sub/proj/-/tree/main/src", GL, "gitlab.com", ("g", "sub"), "proj",
+ "https://gitlab.com/g/sub/proj", "gitlab.com/g/sub/proj", False),
+ ("https://gitlab.com/g/proj/-/merge_requests/12", GL, "gitlab.com", ("g",), "proj",
+ "https://gitlab.com/g/proj", "gitlab.com/g/proj", False),
+ ("https://oauth2:[email protected]/g/proj.git", GL, "gitlab.com", ("g",), "proj",
+ "https://gitlab.com/g/proj", "gitlab.com/g/proj", False),
+ ("https://user%40corp.com:[email protected]/g/proj.git", GL, "gitlab.com", ("g",), "proj",
+ "https://gitlab.com/g/proj", "gitlab.com/g/proj", False),
+ # Self-managed GitLab: by host name, or by override, with an ssh port.
+ ("https://gitlab.example.com/group/sub/flask/", GL, "gitlab.example.com", ("group", "sub"),
+ "flask", "https://gitlab.example.com/group/sub/flask",
+ "gitlab.example.com/group/sub/flask", True),
+ ("ssh://[email protected]:2222/g/proj.git", GL, "gitlab.example.com", ("g",), "proj",
+ "https://gitlab.example.com/g/proj", "gitlab.example.com/g/proj", True),
+ ("ssh://[email protected]:2222/g/sub/proj.git", GL, "git.corp.com", ("g", "sub"), "proj",
+ "https://git.corp.com/g/sub/proj", "git.corp.com/g/sub/proj", True),
+ ("https://git.corp.com:8443/g/proj.git", GL, "git.corp.com", ("g",), "proj",
+ "https://git.corp.com:8443/g/proj", "git.corp.com/g/proj", True),
+ ("http://git.corp.com/g/proj", GL, "git.corp.com", ("g",), "proj",
+ "http://git.corp.com/g/proj", "git.corp.com/g/proj", True),
+ # Azure DevOps Services: three host shapes, one key.
+ ("https://[email protected]/org/project/_git/repo", AZ, "dev.azure.com", ("org", "project"),
+ "repo", "https://dev.azure.com/org/project/_git/repo",
+ "dev.azure.com/org/project/_git/repo", False),
+ ("https://dev.azure.com/Org/Project/_git/Repo", AZ, "dev.azure.com", ("Org", "Project"),
+ "Repo", "https://dev.azure.com/Org/Project/_git/Repo",
+ "dev.azure.com/org/project/_git/repo", False),
+ ("[email protected]:v3/org/project/repo", AZ, "dev.azure.com", ("org", "project"),
+ "repo", "https://dev.azure.com/org/project/_git/repo",
+ "dev.azure.com/org/project/_git/repo", False),
+ ("ssh://[email protected]/v3/org/project/repo", AZ, "dev.azure.com",
+ ("org", "project"), "repo", "https://dev.azure.com/org/project/_git/repo",
+ "dev.azure.com/org/project/_git/repo", False),
+ ("https://org.visualstudio.com/project/_git/repo", AZ, "dev.azure.com", ("org", "project"),
+ "repo", "https://dev.azure.com/org/project/_git/repo",
+ "dev.azure.com/org/project/_git/repo", False),
+ ("https://org.visualstudio.com/DefaultCollection/project/_git/repo", AZ, "dev.azure.com",
+ ("org", "project"), "repo", "https://dev.azure.com/org/project/_git/repo",
+ "dev.azure.com/org/project/_git/repo", False),
+ ("[email protected]:v3/org/project/repo", AZ, "dev.azure.com",
+ ("org", "project"), "repo", "https://dev.azure.com/org/project/_git/repo",
+ "dev.azure.com/org/project/_git/repo", False),
+ ("https://dev.azure.com/org/_git/repo", AZ, "dev.azure.com", ("org", "repo"), "repo",
+ "https://dev.azure.com/org/repo/_git/repo", "dev.azure.com/org/repo/_git/repo", False),
+ ("https://dev.azure.com/org/project/_git/repo/pullrequest/7", AZ, "dev.azure.com",
+ ("org", "project"), "repo", "https://dev.azure.com/org/project/_git/repo",
+ "dev.azure.com/org/project/_git/repo", False),
+ ("https://user%40corp.com:[email protected]/org/proj/_git/repo", AZ, "dev.azure.com",
+ ("org", "proj"), "repo", "https://dev.azure.com/org/proj/_git/repo",
+ "dev.azure.com/org/proj/_git/repo", False),
+ # Names with spaces, escaped and raw, all on one key.
+ ("https://[email protected]/org/My%20Project/_git/My%20Repo", AZ, "dev.azure.com",
+ ("org", "My Project"), "My Repo", "https://dev.azure.com/org/My%20Project/_git/My%20Repo",
+ "dev.azure.com/org/my%20project/_git/my%20repo", False),
+ ("https://dev.azure.com/org/My Project/_git/My Repo", AZ, "dev.azure.com",
+ ("org", "My Project"), "My Repo", "https://dev.azure.com/org/My%20Project/_git/My%20Repo",
+ "dev.azure.com/org/my%20project/_git/my%20repo", False),
+ ("[email protected]:v3/org/My%20Project/My%20Repo", AZ, "dev.azure.com",
+ ("org", "My Project"), "My Repo", "https://dev.azure.com/org/My%20Project/_git/My%20Repo",
+ "dev.azure.com/org/my%20project/_git/my%20repo", False),
+ # Azure DevOps Server: collection path kept, override needed.
+ ("https://tfs.corp/tfs/DefaultCollection/Proj/_git/Repo", AZ, "tfs.corp",
+ ("tfs", "DefaultCollection", "Proj"), "Repo",
+ "https://tfs.corp/tfs/DefaultCollection/Proj/_git/Repo",
+ "tfs.corp/tfs/defaultcollection/proj/_git/repo", True),
+ ("ssh://tfs.corp:22/DefaultCollection/Proj/_git/Repo", AZ, "tfs.corp",
+ ("DefaultCollection", "Proj"), "Repo", "https://tfs.corp/DefaultCollection/Proj/_git/Repo",
+ "tfs.corp/defaultcollection/proj/_git/repo", True),
+ # Bitbucket Cloud
+ ("https://bitbucket.org/ws/repo.git", BB, "bitbucket.org", ("ws",), "repo",
+ "https://bitbucket.org/ws/repo", "bitbucket.org/ws/repo", False),
+ ("https://[email protected]/ws/repo.git", BB, "bitbucket.org", ("ws",), "repo",
+ "https://bitbucket.org/ws/repo", "bitbucket.org/ws/repo", False),
+ ("[email protected]:ws/repo.git", BB, "bitbucket.org", ("ws",), "repo",
+ "https://bitbucket.org/ws/repo", "bitbucket.org/ws/repo", False),
+ # Bitbucket Data Center: http under /scm, ssh on its own port, browse URLs.
+ ("https://bitbucket.corp.com/scm/PROJ/repo.git", BB, "bitbucket.corp.com", ("PROJ",), "repo",
+ "https://bitbucket.corp.com/projects/PROJ/repos/repo",
+ "bitbucket.corp.com/proj/repo", True),
+ ("https://bitbucket.corp.com/bb/scm/PROJ/repo.git", BB, "bitbucket.corp.com", ("PROJ",),
+ "repo", "https://bitbucket.corp.com/bb/projects/PROJ/repos/repo",
+ "bitbucket.corp.com/proj/repo", True),
+ ("ssh://[email protected]:7999/proj/repo.git", BB, "bitbucket.corp.com", ("PROJ",),
+ "repo", "https://bitbucket.corp.com/projects/PROJ/repos/repo",
+ "bitbucket.corp.com/proj/repo", True),
+ ("https://bitbucket.corp.com/scm/~jdoe/repo.git", BB, "bitbucket.corp.com", ("~jdoe",),
+ "repo", "https://bitbucket.corp.com/users/jdoe/repos/repo",
+ "bitbucket.corp.com/~jdoe/repo", True),
+ ("ssh://[email protected]:7999/~jdoe/repo.git", BB, "bitbucket.corp.com", ("~jdoe",),
+ "repo", "https://bitbucket.corp.com/users/jdoe/repos/repo",
+ "bitbucket.corp.com/~jdoe/repo", True),
+ ("https://bitbucket.corp.com/projects/PROJ/repos/repo/browse", BB, "bitbucket.corp.com",
+ ("PROJ",), "repo", "https://bitbucket.corp.com/projects/PROJ/repos/repo",
+ "bitbucket.corp.com/proj/repo", True),
+ # Generic: unknown hosts, no override.
+ ("https://gitea.example.org/team/tool.git", GEN, "gitea.example.org", ("team",), "tool",
+ "https://gitea.example.org/team/tool", "gitea.example.org/team/tool", True),
+ ("[email protected]:~user/proj", GEN, "git.sr.ht", ("~user",), "proj",
+ "https://git.sr.ht/~user/proj", "git.sr.ht/~user/proj", True),
+ ("ssh://git@[::1]:2222/srv/repo.git", GEN, "::1", ("srv",), "repo",
+ "https://[::1]/srv/repo", "::1/srv/repo", True),
+ ("git.lan:8443/g/p", GEN, "git.lan", ("g",), "p", "https://git.lan:8443/g/p",
+ "git.lan/g/p", True),
+]
+
+
+ ("url", "forge", "host", "namespace", "repo", "web_base", "key", "self_hosted"), MATRIX
+)
+def test_parse_and_key(
+ url: str,
+ forge: ForgeKind,
+ host: str,
+ namespace: tuple[str, ...],
+ repo: str,
+ web_base: str,
+ key: str,
+ self_hosted: bool,
+) -> None:
+ assert parse_remote(url, hosts=HOSTS) == RemoteRef(
+ forge, host, namespace, repo, web_base, self_hosted
+ )
+ assert canonical_key(url, hosts=HOSTS) == key
+
+
+ "url",
+ [
+ "https://ghe.corp.com/team/svc.git",
+ "ssh://[email protected]:2222/g/proj.git",
+ "https://tfs.corp/tfs/DefaultCollection/Proj/_git/Repo",
+ "https://bitbucket.corp.com/scm/PROJ/repo.git",
+ ],
+)
+def test_a_self_managed_host_is_generic_without_an_override(url: str) -> None:
+ ref = parse_remote(url)
+ assert ref is not None and ref.forge is GEN
+
+
+ "urls",
+ [
+ (
+ "https://bitbucket.corp.com/scm/PROJ/repo.git",
+ "https://user:[email protected]/bb/scm/proj/repo.git",
+ "ssh://[email protected]:7999/proj/repo.git",
+ "[email protected]:PROJ/repo.git",
+ ),
+ (
+ "https://[email protected]/org/p/_git/r",
+ "[email protected]:v3/org/p/r",
+ "https://org.visualstudio.com/DefaultCollection/p/_git/r",
+ ),
+ ],
+)
+def test_every_clone_url_of_one_repo_shares_a_key_and_a_name(urls: tuple[str, ...]) -> None:
+ assert len({canonical_key(u, hosts=HOSTS) for u in urls}) == 1
+ refs = {parse_remote(u, hosts=HOSTS) for u in urls}
+ assert len({(r.namespace, r.repo) for r in refs if r}) == 1
+
+
+def test_an_empty_repo_keys_like_the_webhook_did() -> None:
+ assert canonical_key("https://github.com/o/.git") == "github.com/o"
+
+
+ ("url", "key"),
+ [
+ # Without an override a Data Center host is generic and keeps its path.
+ ("https://bitbucket.corp.com/scm/PROJ/repo.git", "bitbucket.corp.com/scm/proj/repo"),
+ ("https://git.corp.com/scm/x/repo.git", "git.corp.com/scm/x/repo"),
+ ("https://git.corp.com/g/scm/x/repo.git", "git.corp.com/g/scm/x/repo"),
+ ("https://gitlab.com/scm/x/repo", "gitlab.com/scm/x/repo"),
+ ("https://github.com/scm/x/repo.git", "github.com/scm/x/repo"),
+ ],
+)
+def test_a_path_named_scm_is_only_folded_on_bitbucket(url: str, key: str) -> None:
+ assert canonical_key(url) == key
+
+
+ ("url", "key"),
+ [
+ ("https://a/g/p.git", "a/g/p"),
+ ("ssh://git@b:2222/g/p.git", "b/g/p"),
+ ],
+)
+def test_a_single_character_host_parses_outside_scp_form(url: str, key: str) -> None:
+ """Only the scp form reads `c:path` as a drive letter, which is how git reads it."""
+ assert canonical_key(url) == key
+
+
+def test_an_override_accepts_a_plain_string_kind_and_any_key_case() -> None:
+ ref = parse_remote("[email protected]:g/p.git", hosts={"GIT.CORP.COM": "gitlab"}) # type: ignore[dict-item]
+ assert ref is not None and ref.forge is GL
+
+
+ "url",
+ [
+ "",
+ " ",
+ "/srv/git/repo.git",
+ "../sibling",
+ "C:\\repos\\flask",
+ "C:/repos/flask",
+ "file:///srv/git/p.git",
+ "ftp://host/x",
+ "https://",
+ "https://github.com/onlyowner",
+ "https://dev.azure.com/org/project/repo",
+ "[email protected]:org/project/repo",
+ "https://github.com/o/.git",
+ "[email protected]:g/.git",
+ ],
+)
+def test_non_remotes_and_misshapen_paths_parse_to_none(url: str) -> None:
+ assert parse_remote(url) is None
+
+
+ "url", ["", " ", "/srv/git/repo.git", "C:\\repos\\flask", "file:///x", "https://"]
+)
+def test_canonical_key_is_none_for_a_non_remote(url: str) -> None:
+ assert canonical_key(url) is None
+
+
+ ("url", "legacy_key"),
+ [
+ ("https://github.com/Org/Repo.git", "github.com/org/repo"),
+ ("https://github.com/Org/Repo/", "github.com/org/repo"),
+ ("[email protected]:Org/Repo.git", "github.com/org/repo"),
+ ("https://github.com/org/RepoWise", "github.com/org/repowise"),
+ ("https://user:[email protected]/o/r.git?x=1#f", "github.com/o/r"),
+ ("ssh://[email protected]:2222/g/p.git", "git.corp/g/p"),
+ ("https://github.com/o/r/tree/main", "github.com/o/r/tree/main"),
+ ("https://gitlab.example.com/a/b/c.git/", "gitlab.example.com/a/b/c"),
+ ],
+)
+def test_non_azure_keys_match_the_webhook_key_they_replace(url: str, legacy_key: str) -> None:
+ """Stored repos are matched by this key, so a changed shape stops a webhook syncing."""
+ assert canonical_key(url) == legacy_key
+
+
+# The same table as the server's own scrubber: the remote reaches the browser.
+ ("remote", "expected"),
+ [
+ ("https://user:[email protected]/g/sub/p.git", "https://gitlab.com/g/sub/p.git"),
+ ("https://[email protected]/o/r.git", "https://github.com/o/r.git"),
+ (
+ "https://user%40corp.com:[email protected]/org/proj/_git/repo",
+ "https://dev.azure.com/org/proj/_git/repo",
+ ),
+ ("http://u:[email protected]:8080/g/p", "http://git.corp:8080/g/p"),
+ ("ssh://git:[email protected]:2222/g/p.git", "ssh://[email protected]:2222/g/p.git"),
+ ("ssh://[email protected]/o/r.git", "ssh://[email protected]/o/r.git"),
+ ("[email protected]:o/r.git", "[email protected]:o/r.git"),
+ ("https://github.com/o/r", "https://github.com/o/r"),
+ ("HTTPS://[email protected]/o/r", "HTTPS://github.com/o/r"),
+ (" https://[email protected]/o/r ", "https://github.com/o/r"),
+ ("git+https://[email protected]/o/r", "git+https://github.com/o/r"),
+ ("ftp://tok@host/x", "ftp://host/x"),
+ ("https://user:pa/ss@host/p.git", "https://host/p.git"),
+ ("https://gitlab.com/g/p.git?private_token=abc#frag", "https://gitlab.com/g/p.git"),
+ ("https://user:123/secret@host/repo.git", "https://host/repo.git"),
+ ("file:///srv/git/[email protected]", "file:///srv/git/[email protected]"),
+ ("https://host/org/[email protected]", "https://host/org/[email protected]"),
+ ("https://git.example.com/org/[email protected]", "https://git.example.com/org/[email protected]"),
+ ("git@host:org/[email protected]", "git@host:org/[email protected]"),
+ ("host:org/[email protected]", "host:org/[email protected]"),
+ ("ssh://ghp_tok@host/x", "ssh://ghp_tok@host/x"),
+ ("user:pw@host:g/p.git", "user@host:g/p.git"),
+ ("user:pa/ss@host:g/p.git", "user@host:g/p.git"),
+ ("https://user:SE?CRET@host/r.git", "https://host/r.git"),
+ ("https://user:SE#CRET@host/r.git", "https://host/r.git"),
+ ("https://host:8080/p.git?x=a@b", "https://host:8080/p.git"),
+ ("/srv/a:b@c/p.git", "/srv/a:b@c/p.git"),
+ ("https://[::1]:8443/g/p", "https://[::1]:8443/g/p"),
+ ("file:///srv/git/p.git", "file:///srv/git/p.git"),
+ ("", ""),
+ ],
+)
+def test_strip_credentials(remote: str, expected: str) -> None:
+ assert strip_credentials(remote) == expected
+
+
+def test_a_password_with_a_slash_never_reaches_the_parsed_host() -> None:
+ ref = parse_remote("https://user:pa/[email protected]/o/r.git")
+ assert ref is not None and (ref.host, ref.repo) == ("github.com", "r")
diff --git a/tests/unit/core/test_forges_urls.py b/tests/unit/core/test_forges_urls.py
new file mode 100644
index 000000000..6113a8bd4
--- /dev/null
+++ b/tests/unit/core/test_forges_urls.py
@@ -0,0 +1,131 @@
+"""Forge web URLs, labels and change-ref display."""
+
+from __future__ import annotations
+
+import pytest
+
+from repowise.core.forges import ForgeKind, all_forges, get_forge, parse_remote
+
+GH_REF = "https://github.com/org/repo.git"
+GHE_REF = "https://ghe.corp.com/org/repo.git"
+GL_REF = "[email protected]:g/sub/proj.git"
+AZ_REF = "https://dev.azure.com/org/My%20Project/_git/repo"
+BB_REF = "[email protected]:ws/repo.git"
+BBDC_REF = "https://bitbucket.corp.com/scm/PROJ/repo.git"
+SHA = "0123456789abcdef0123456789abcdef01234567"
+
+_HOSTS = {"ghe.corp.com": ForgeKind.GITHUB, "bitbucket.corp.com": ForgeKind.BITBUCKET}
+
+
+def _ref(url: str):
+ ref = parse_remote(url, hosts=_HOSTS)
+ assert ref is not None
+ return ref
+
+
+ ("url", "commit", "change", "blob", "blob_line", "compare"),
+ [
+ (
+ GH_REF,
+ f"https://github.com/org/repo/commit/{SHA}",
+ "https://github.com/org/repo/pull/12",
+ "https://github.com/org/repo/blob/main/src/a%20b.py",
+ "https://github.com/org/repo/blob/feat/x/src/a%20b.py#L7",
+ "https://github.com/org/repo/compare/main...feat/x",
+ ),
+ (
+ GHE_REF,
+ f"https://ghe.corp.com/org/repo/commit/{SHA}",
+ "https://ghe.corp.com/org/repo/pull/12",
+ "https://ghe.corp.com/org/repo/blob/main/src/a%20b.py",
+ "https://ghe.corp.com/org/repo/blob/feat/x/src/a%20b.py#L7",
+ "https://ghe.corp.com/org/repo/compare/main...feat/x",
+ ),
+ (
+ GL_REF,
+ f"https://gitlab.com/g/sub/proj/-/commit/{SHA}",
+ "https://gitlab.com/g/sub/proj/-/merge_requests/12",
+ "https://gitlab.com/g/sub/proj/-/blob/main/src/a%20b.py",
+ "https://gitlab.com/g/sub/proj/-/blob/feat/x/src/a%20b.py#L7",
+ "https://gitlab.com/g/sub/proj/-/compare/main...feat/x",
+ ),
+ (
+ AZ_REF,
+ f"https://dev.azure.com/org/My%20Project/_git/repo/commit/{SHA}",
+ "https://dev.azure.com/org/My%20Project/_git/repo/pullrequest/12",
+ "https://dev.azure.com/org/My%20Project/_git/repo"
+ "?path=/src/a%20b.py&version=GBmain&_a=contents",
+ "https://dev.azure.com/org/My%20Project/_git/repo"
+ "?path=/src/a%20b.py&version=GBfeat/x&line=7&lineEnd=8"
+ "&lineStartColumn=1&lineEndColumn=1&lineStyle=plain&_a=contents",
+ "https://dev.azure.com/org/My%20Project/_git/repo"
+ "/branchCompare?baseVersion=GBmain&targetVersion=GBfeat/x",
+ ),
+ (
+ BB_REF,
+ f"https://bitbucket.org/ws/repo/commits/{SHA}",
+ "https://bitbucket.org/ws/repo/pull-requests/12",
+ "https://bitbucket.org/ws/repo/src/main/src/a%20b.py",
+ "https://bitbucket.org/ws/repo/src/feat/x/src/a%20b.py#lines-7",
+ "https://bitbucket.org/ws/repo/branches/compare/feat/x%0Dmain",
+ ),
+ (
+ BBDC_REF,
+ f"https://bitbucket.corp.com/projects/PROJ/repos/repo/commits/{SHA}",
+ "https://bitbucket.corp.com/projects/PROJ/repos/repo/pull-requests/12/overview",
+ "https://bitbucket.corp.com/projects/PROJ/repos/repo/browse/src/a%20b.py?at=main",
+ "https://bitbucket.corp.com/projects/PROJ/repos/repo/browse/src/a%20b.py"
+ "?at=feat%2Fx#7",
+ "https://bitbucket.corp.com/projects/PROJ/repos/repo/compare/commits"
+ "?sourceBranch=feat%2Fx&targetBranch=main",
+ ),
+ ],
+)
+def test_url_builders(
+ url: str, commit: str, change: str, blob: str, blob_line: str, compare: str
+) -> None:
+ ref = _ref(url)
+ forge = get_forge(ref.forge)
+ assert forge.commit_url(ref, SHA) == commit
+ assert forge.change_url(ref, 12) == change
+ assert forge.blob_url(ref, "main", "src/a b.py") == blob
+ assert forge.blob_url(ref, "feat/x", "src/a b.py", 7) == blob_line
+ assert forge.compare_url(ref, "main", "feat/x") == compare
+
+
+def test_azure_pins_a_commit_with_gc_and_a_branch_with_gb() -> None:
+ ref = _ref(AZ_REF)
+ url = get_forge(ForgeKind.AZURE).blob_url(ref, SHA[:12], "a.py")
+ assert f"version=GC{SHA[:12]}" in url
+
+
+def test_generic_builds_no_urls() -> None:
+ ref = _ref("https://gitea.example.org/team/tool.git")
+ forge = get_forge(ref.forge)
+ assert ref.forge is ForgeKind.GENERIC
+ assert forge.commit_url(ref, SHA) == ""
+ assert forge.change_url(ref, 1) == ""
+ assert forge.blob_url(ref, "main", "a.py", 3) == ""
+ assert forge.compare_url(ref, "a", "b") == ""
+
+
+ ("kind", "label", "term", "display"),
+ [
+ (ForgeKind.GITHUB, "GitHub", "pull request", "#12"),
+ (ForgeKind.GITLAB, "GitLab", "merge request", "!12"),
+ (ForgeKind.AZURE, "Azure DevOps", "pull request", "PR 12"),
+ (ForgeKind.BITBUCKET, "Bitbucket", "pull request", "#12"),
+ (ForgeKind.GENERIC, "Git", "pull request", "#12"),
+ ],
+)
+def test_labels_and_change_display(kind: ForgeKind, label: str, term: str, display: str) -> None:
+ forge = get_forge(kind)
+ assert (forge.kind, forge.label, forge.change_term) == (kind, label, term)
+ assert forge.format_change_ref(12) == display
+
+
+def test_every_kind_is_registered() -> None:
+ assert {f.kind for f in all_forges()} == set(ForgeKind)
+ assert get_forge("gitlab") is get_forge(ForgeKind.GITLAB)