mirror of
https://github.com/openSUSE/osc.git
synced 2025-11-26 14:39:50 +01:00
Add '--cache-dir' option to 'git-obs staging group' command
This commit is contained in:
@@ -44,6 +44,11 @@ class StagingGroupCommand(osc.commandline_git.GitObsCommand):
|
||||
help="List of project pull request to be merged into the target project pull request",
|
||||
).completer = osc.commandline_git.complete_pr
|
||||
|
||||
self.add_argument(
|
||||
"--cache-dir",
|
||||
help="Path to a git cache.",
|
||||
)
|
||||
|
||||
self.add_argument(
|
||||
"--keep-temp-dir",
|
||||
action="store_true",
|
||||
@@ -67,6 +72,8 @@ class StagingGroupCommand(osc.commandline_git.GitObsCommand):
|
||||
if args.fork_branch and args.target:
|
||||
self.parser.error("--fork-branch conflicts with --target")
|
||||
|
||||
cache_dir = os.path.abspath(args.cache_dir) if args.cache_dir else None
|
||||
|
||||
self.print_gitea_settings()
|
||||
|
||||
with TemporaryDirectory(prefix="git-obs-staging_", dir=".", delete=not args.keep_temp_dir) as temp_dir:
|
||||
@@ -82,10 +89,9 @@ class StagingGroupCommand(osc.commandline_git.GitObsCommand):
|
||||
# get pull request data from gitea
|
||||
pr_map = {}
|
||||
for owner, repo, number in args.pr_list:
|
||||
pr = gitea_api.StagingPullRequestWrapper(self.gitea_conn, owner, repo, number, topdir=temp_dir)
|
||||
pr = gitea_api.StagingPullRequestWrapper(self.gitea_conn, owner, repo, number, topdir=temp_dir, cache_directory=cache_dir)
|
||||
pr_map[(owner, repo, number)] = pr
|
||||
|
||||
|
||||
# run checks
|
||||
target_owner = None
|
||||
target_repo = None
|
||||
@@ -150,6 +156,7 @@ class StagingGroupCommand(osc.commandline_git.GitObsCommand):
|
||||
fork_repo,
|
||||
directory=os.path.join(temp_dir, f"{fork_owner}_{fork_repo}"),
|
||||
add_remotes=True,
|
||||
cache_directory=cache_dir,
|
||||
ssh_private_key_path=self.gitea_conn.login.ssh_key,
|
||||
)
|
||||
clone_git = gitea_api.Git(clone_dir)
|
||||
@@ -190,7 +197,7 @@ class StagingGroupCommand(osc.commandline_git.GitObsCommand):
|
||||
target_number = pr_obj.number
|
||||
|
||||
# clone the git repos, cache submodule data
|
||||
target = gitea_api.StagingPullRequestWrapper(self.gitea_conn, target_owner, target_repo, target_number, topdir=temp_dir)
|
||||
target = gitea_api.StagingPullRequestWrapper(self.gitea_conn, target_owner, target_repo, target_number, topdir=temp_dir, cache_directory=cache_dir)
|
||||
target.clone()
|
||||
for owner, repo, number in args.pr_list:
|
||||
pr = pr_map[(owner, repo, number)]
|
||||
|
||||
@@ -93,6 +93,7 @@ class Repo(GiteaModel):
|
||||
cwd: Optional[str] = None,
|
||||
use_http: bool = False,
|
||||
add_remotes: bool = False,
|
||||
cache_directory: Optional[str] = None,
|
||||
reference: Optional[str] = None,
|
||||
reference_if_able: Optional[str] = None,
|
||||
ssh_private_key_path: Optional[str] = None,
|
||||
@@ -108,6 +109,7 @@ class Repo(GiteaModel):
|
||||
:param cwd: Working directory. Defaults to the current working directory.
|
||||
:param use_http: Whether to use``clone_url`` for cloning over http(s) instead of ``ssh_url`` for cloning over SSH.
|
||||
:param add_remotes: Determine and add 'parent' or 'fork' remotes to the cloned repo.
|
||||
:param cache_directory: Manage repo cache under ``cache_directory`` / ``conn.login.name`` / ``owner`` / ``repo`` and pass it as ``reference_if_able``.
|
||||
:param reference: Reuse objects from the specified local repository, error out if the repository doesn't exist.
|
||||
:param reference_if_able: Reuse objects from the specified local repository, only print warning if the repository doesn't exist.
|
||||
"""
|
||||
@@ -118,6 +120,11 @@ class Repo(GiteaModel):
|
||||
# it's perfectly fine to use os.path.join() here because git can take an absolute path
|
||||
directory_abspath = os.path.join(cwd, directory)
|
||||
|
||||
if cache_directory and not reference_if_able:
|
||||
cache_directory = os.path.join(cache_directory, conn.login.name, owner, repo)
|
||||
cls.clone_or_update(conn, owner, repo, directory=cache_directory)
|
||||
reference_if_able = cache_directory
|
||||
|
||||
repo_obj = cls.get(conn, owner, repo)
|
||||
|
||||
clone_url = repo_obj.clone_url if use_http else repo_obj.ssh_url
|
||||
@@ -208,15 +215,14 @@ class Repo(GiteaModel):
|
||||
branch: Optional[str] = None,
|
||||
commit: Optional[str] = None,
|
||||
directory: str,
|
||||
cache_directory: Optional[str] = None,
|
||||
reference: Optional[str] = None,
|
||||
reference_if_able: Optional[str] = None,
|
||||
remote: Optional[str] = None,
|
||||
ssh_private_key_path: Optional[str] = None,
|
||||
):
|
||||
from osc import gitea_api
|
||||
|
||||
if not pr_number and not branch:
|
||||
raise ValueError("Either 'pr_number' or 'branch' must be specified")
|
||||
|
||||
if not os.path.exists(os.path.join(directory, ".git")):
|
||||
gitea_api.Repo.clone(
|
||||
conn,
|
||||
@@ -224,7 +230,9 @@ class Repo(GiteaModel):
|
||||
repo,
|
||||
directory=directory,
|
||||
add_remotes=True,
|
||||
cache_directory=cache_directory,
|
||||
reference=reference,
|
||||
reference_if_able=reference_if_able,
|
||||
ssh_private_key_path=ssh_private_key_path,
|
||||
)
|
||||
|
||||
@@ -259,7 +267,7 @@ class Repo(GiteaModel):
|
||||
else:
|
||||
git.fetch()
|
||||
else:
|
||||
raise ValueError("Either 'pr_number' or 'branch' must be specified")
|
||||
git.fetch()
|
||||
|
||||
@classmethod
|
||||
def list_org_repos(cls, conn: Connection, owner: str) -> List["Repo"]:
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
import os
|
||||
from typing import Optional
|
||||
|
||||
|
||||
class StagingPullRequestWrapper:
|
||||
BACKLOG_LABEL = "staging/Backlog"
|
||||
INPROGRESS_LABEL = "staging/In Progress"
|
||||
|
||||
def __init__(self, conn, owner: str, repo: str, number: int, *, topdir: str):
|
||||
def __init__(self, conn, owner: str, repo: str, number: int, *, topdir: str, cache_directory: Optional[str] = None):
|
||||
from . import PullRequest
|
||||
|
||||
self.conn = conn
|
||||
@@ -13,6 +14,7 @@ class StagingPullRequestWrapper:
|
||||
self.repo = repo
|
||||
self.number = number
|
||||
self._topdir = topdir
|
||||
self._cache_directory = cache_directory
|
||||
|
||||
self.pr_obj = PullRequest.get(conn, owner, repo, number)
|
||||
self.git = None
|
||||
@@ -34,6 +36,7 @@ class StagingPullRequestWrapper:
|
||||
pr_number=self.number,
|
||||
commit=self.pr_obj.head_commit,
|
||||
directory=path,
|
||||
cache_directory=self._cache_directory,
|
||||
ssh_private_key_path=self.conn.login.ssh_key,
|
||||
)
|
||||
self.git = Git(path)
|
||||
@@ -61,6 +64,7 @@ class StagingPullRequestWrapper:
|
||||
branch=self.pr_obj.base_branch,
|
||||
commit=self.pr_obj.base_commit,
|
||||
directory=path,
|
||||
cache_directory=self._cache_directory,
|
||||
ssh_private_key_path=self.conn.login.ssh_key,
|
||||
)
|
||||
self.base_git = Git(path)
|
||||
|
||||
Reference in New Issue
Block a user