diff --git a/backend/configs/hermes.py b/backend/configs/hermes.py index ef74b72..a445c98 100644 --- a/backend/configs/hermes.py +++ b/backend/configs/hermes.py @@ -5,7 +5,6 @@ from steps.git import GitClone, CheckoutMerged, GitUnmerge from steps.shared_state import SharedStateHolderInMemory clone = GitClone(remotes=[ - # {"name": "github", "url": "https://github.com/rudolf1/uber_backup.git", "prefix": "ansible"}, {"name": "gitea_hermes", "url": "https://gitea.rudolf.keenetic.link/hermes/ansible"}, {"name": "gitea_admin", "url": "https://gitea.rudolf.keenetic.link/admin/ansible"}, ]) diff --git a/backend/steps/git.py b/backend/steps/git.py index 8df31d2..77f1cfc 100644 --- a/backend/steps/git.py +++ b/backend/steps/git.py @@ -48,8 +48,13 @@ class GitClone(AbstractStep[str]): raise ValueError(f"Remote {name!r} requires a non-empty 'url' (string)") if name in seen: raise ValueError(f"Duplicate remote name: {name!r}") + prefix_value = entry.get("prefix") + if prefix_value is not None and not isinstance(prefix_value, str): + raise ValueError( + f"Remote {name!r} 'prefix' must be a string when set, got {type(prefix_value).__name__}" + ) seen.add(name) - normalized.append({"name": name, "url": url_value}) + normalized.append({"name": name, "url": url_value, "prefix": prefix_value or ""}) elif url is not None: if isinstance(url, str): raw = [url] @@ -103,10 +108,12 @@ class GitClone(AbstractStep[str]): remote.set_url(auth_url) else: remote = repo.create_remote(remote_name, auth_url) - if remote_name == "origin" and self.branchNamePrefix != "": + prefix = entry.get("prefix", "") + effective_prefix = prefix or (self.branchNamePrefix if remote_name == "origin" else "") + if effective_prefix: with repo.config_writer() as config: - config.set_value('remote "origin"', "fetch", - f"+refs/heads/{self.branchNamePrefix}/*:refs/remotes/origin/{self.branchNamePrefix}/*") + config.set_value(f'remote "{remote_name}"', "fetch", + f"+refs/heads/{effective_prefix}/*:refs/remotes/{remote_name}/{effective_prefix}/*") result = remote.fetch(prune=True) if not result or any(fetch_info.flags & fetch_info.ERROR for fetch_info in result): raise BaseException(f"Failed to fetch updates for {repository_url}") diff --git a/tests/test_git_integration.py b/tests/test_git_integration.py index 3a81c27..0efb41a 100644 --- a/tests/test_git_integration.py +++ b/tests/test_git_integration.py @@ -101,6 +101,104 @@ class TestGitIntegration: with pytest.raises(ValueError, match="requires either 'url' or 'remotes'"): GitClone() + def test_git_clone_remotes_per_remote_prefix(self, repo_helper: RemoteRepoHelper) -> None: + # Per-remote `prefix` filters fetch refspec so only matching branches are visible. + # Two repos, two branches each (one matching the prefix, one not). Only the + # matching branch should appear in get_branches() for each remote. + def fresh_repo(prefix: str) -> str: + path = tempfile.mkdtemp(prefix=prefix) + repo = git.Repo.init(path, bare=False) + with repo.config_writer() as cw: + cw.set_value("user", "email", "test@example.com") + cw.set_value("user", "name", "Test User") + # Branches literally named `ansible/...` to match the prefix filter. + # create_commit creates an initial master, then branches off it. + repo_helper.create_commit(repo, "master", "master", "README.md", "content", f"{prefix} init") + repo_helper.create_commit(repo, "master", "ansible/master", "AN.md", "ansible", f"{prefix} ansible") + repo_helper.create_commit(repo, "master", "ansible/fix", "FIX.md", "fix", f"{prefix} fix") + return path + + github_dir = fresh_repo("test_prefix_github_") + gitea_dir = fresh_repo("test_prefix_gitea_") + try: + clone = GitClone(remotes=[ + {"name": "github", "url": github_dir, "prefix": "ansible"}, + {"name": "gitea_admin", "url": gitea_dir, "prefix": "ansible"}, + ]) + clone.env = repo_helper.env + + repo_path = clone.progress() + repo = git.Repo(repo_path) + + branches = set(clone.get_branches()) + # Per-remote refspec was applied: only ansible/* branches fetched. + assert "github/ansible/master" in branches, f"missing: {branches}" + assert "github/ansible/fix" in branches, f"missing: {branches}" + assert "gitea_admin/ansible/master" in branches, f"missing: {branches}" + assert "gitea_admin/ansible/fix" in branches, f"missing: {branches}" + + # `master` exists upstream but is NOT visible (filtered by prefix). + assert "github/master" not in branches, f"unwanted: {branches}" + assert "gitea_admin/master" not in branches, f"unwanted: {branches}" + finally: + shutil.rmtree(github_dir, ignore_errors=True) + shutil.rmtree(gitea_dir, ignore_errors=True) + + def test_git_clone_remotes_mixed_prefix(self, repo_helper: RemoteRepoHelper) -> None: + # Some remotes with prefix, others without. Each behaves independently: + # prefixed remotes only see matching branches, unprefixed see all. + def fresh_repo(prefix: str) -> str: + path = tempfile.mkdtemp(prefix=prefix) + repo = git.Repo.init(path, bare=False) + with repo.config_writer() as cw: + cw.set_value("user", "email", "test@example.com") + cw.set_value("user", "name", "Test User") + repo_helper.create_commit(repo, "master", "master", "README.md", "content", f"{prefix} init") + repo_helper.create_commit(repo, "master", "ansible/master", "AN.md", "ansible", f"{prefix} ansible") + repo_helper.create_commit(repo, "master", "feature_y", "OTHER.md", "other", f"{prefix} feature") + return path + + github_dir = fresh_repo("test_mixed_github_") + gitea_dir = fresh_repo("test_mixed_gitea_") + try: + clone = GitClone(remotes=[ + {"name": "github", "url": github_dir, "prefix": "ansible"}, + {"name": "gitea_admin", "url": gitea_dir}, + ]) + clone.env = repo_helper.env + + repo_path = clone.progress() + repo = git.Repo(repo_path) + + branches = set(clone.get_branches()) + # Prefixed: only ansible/master. + assert "github/ansible/master" in branches, f"missing: {branches}" + assert "github/feature_y" not in branches, f"unwanted: {branches}" + assert "github/master" not in branches, f"unwanted: {branches}" + # Unprefixed: all branches visible. + assert "gitea_admin/ansible/master" in branches, f"missing: {branches}" + assert "gitea_admin/feature_y" in branches, f"missing: {branches}" + assert "gitea_admin/master" in branches, f"missing: {branches}" + finally: + shutil.rmtree(github_dir, ignore_errors=True) + shutil.rmtree(gitea_dir, ignore_errors=True) + + def test_git_clone_remotes_prefix_validation(self) -> None: + # `prefix` must be a string when set; non-string raises ValueError. + with pytest.raises(ValueError, match="'prefix' must be a string"): + GitClone(remotes=[ + {"name": "github", "url": "git@example.com:a/b.git", "prefix": 123}, # type: ignore[dict-item] + ]) + with pytest.raises(ValueError, match="'prefix' must be a string"): + GitClone(remotes=[ + {"name": "github", "url": "git@example.com:a/b.git", "prefix": ["ansible"]}, # type: ignore[dict-item] + ]) + # Empty string / None: accepted, treated as no prefix. + c1 = GitClone(remotes=[{"name": "github", "url": "git@example.com:a/b.git", "prefix": ""}]) + assert c1.remotes[0]["prefix"] == "" + c2 = GitClone(remotes=[{"name": "github", "url": "git@example.com:a/b.git"}]) + assert c2.remotes[0]["prefix"] == "" + def test_checkout_merged_one_branch(self, repo_helper: RemoteRepoHelper) -> None: # Create main branch with initial commit commit1 = repo_helper.create_commit(repo_helper.repo, "master", "master", "file1.txt", "content1",