Skip to content

Commit 99d2633

Browse files
committed
Add '--cache-dir' option to 'git-obs staging group' command
1 parent e15c0c0 commit 99d2633

File tree

3 files changed

+27
-8
lines changed

3 files changed

+27
-8
lines changed

osc/commands_git/staging_group.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ def init_arguments(self):
4444
help="List of project pull request to be merged into the target project pull request",
4545
).completer = osc.commandline_git.complete_pr
4646

47+
self.add_argument(
48+
"--cache-dir",
49+
help="Path to a git cache.",
50+
)
51+
4752
self.add_argument(
4853
"--keep-temp-dir",
4954
action="store_true",
@@ -67,6 +72,8 @@ def run(self, args):
6772
if args.fork_branch and args.target:
6873
self.parser.error("--fork-branch conflicts with --target")
6974

75+
cache_dir = os.path.abspath(args.cache_dir) if args.cache_dir else None
76+
7077
self.print_gitea_settings()
7178

7279
with TemporaryDirectory(prefix="git-obs-staging_", dir=".", delete=not args.keep_temp_dir) as temp_dir:
@@ -82,10 +89,9 @@ def run(self, args):
8289
# get pull request data from gitea
8390
pr_map = {}
8491
for owner, repo, number in args.pr_list:
85-
pr = gitea_api.StagingPullRequestWrapper(self.gitea_conn, owner, repo, number, topdir=temp_dir)
92+
pr = gitea_api.StagingPullRequestWrapper(self.gitea_conn, owner, repo, number, topdir=temp_dir, cache_directory=cache_dir)
8693
pr_map[(owner, repo, number)] = pr
8794

88-
8995
# run checks
9096
target_owner = None
9197
target_repo = None
@@ -150,6 +156,7 @@ def run(self, args):
150156
fork_repo,
151157
directory=os.path.join(temp_dir, f"{fork_owner}_{fork_repo}"),
152158
add_remotes=True,
159+
cache_directory=cache_dir,
153160
ssh_private_key_path=self.gitea_conn.login.ssh_key,
154161
)
155162
clone_git = gitea_api.Git(clone_dir)
@@ -190,7 +197,7 @@ def run(self, args):
190197
target_number = pr_obj.number
191198

192199
# clone the git repos, cache submodule data
193-
target = gitea_api.StagingPullRequestWrapper(self.gitea_conn, target_owner, target_repo, target_number, topdir=temp_dir)
200+
target = gitea_api.StagingPullRequestWrapper(self.gitea_conn, target_owner, target_repo, target_number, topdir=temp_dir, cache_directory=cache_dir)
194201
target.clone()
195202
for owner, repo, number in args.pr_list:
196203
pr = pr_map[(owner, repo, number)]

osc/gitea_api/repo.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ def clone(
9393
cwd: Optional[str] = None,
9494
use_http: bool = False,
9595
add_remotes: bool = False,
96+
cache_directory: Optional[str] = None,
9697
reference: Optional[str] = None,
9798
reference_if_able: Optional[str] = None,
9899
ssh_private_key_path: Optional[str] = None,
@@ -108,6 +109,7 @@ def clone(
108109
:param cwd: Working directory. Defaults to the current working directory.
109110
:param use_http: Whether to use``clone_url`` for cloning over http(s) instead of ``ssh_url`` for cloning over SSH.
110111
:param add_remotes: Determine and add 'parent' or 'fork' remotes to the cloned repo.
112+
:param cache_directory: Manage repo cache under ``cache_directory`` / ``conn.login.name`` / ``owner`` / ``repo`` and pass it as ``reference_if_able``.
111113
:param reference: Reuse objects from the specified local repository, error out if the repository doesn't exist.
112114
:param reference_if_able: Reuse objects from the specified local repository, only print warning if the repository doesn't exist.
113115
"""
@@ -118,6 +120,11 @@ def clone(
118120
# it's perfectly fine to use os.path.join() here because git can take an absolute path
119121
directory_abspath = os.path.join(cwd, directory)
120122

123+
if cache_directory and not reference_if_able:
124+
cache_directory = os.path.join(cache_directory, conn.login.name, owner, repo)
125+
cls.clone_or_update(conn, owner, repo, directory=cache_directory)
126+
reference_if_able = cache_directory
127+
121128
repo_obj = cls.get(conn, owner, repo)
122129

123130
clone_url = repo_obj.clone_url if use_http else repo_obj.ssh_url
@@ -208,23 +215,24 @@ def clone_or_update(
208215
branch: Optional[str] = None,
209216
commit: Optional[str] = None,
210217
directory: str,
218+
cache_directory: Optional[str] = None,
211219
reference: Optional[str] = None,
220+
reference_if_able: Optional[str] = None,
212221
remote: Optional[str] = None,
213222
ssh_private_key_path: Optional[str] = None,
214223
):
215224
from osc import gitea_api
216225

217-
if not pr_number and not branch:
218-
raise ValueError("Either 'pr_number' or 'branch' must be specified")
219-
220226
if not os.path.exists(os.path.join(directory, ".git")):
221227
gitea_api.Repo.clone(
222228
conn,
223229
owner,
224230
repo,
225231
directory=directory,
226232
add_remotes=True,
233+
cache_directory=cache_directory,
227234
reference=reference,
235+
reference_if_able=reference_if_able,
228236
ssh_private_key_path=ssh_private_key_path,
229237
)
230238

@@ -259,7 +267,7 @@ def clone_or_update(
259267
else:
260268
git.fetch()
261269
else:
262-
raise ValueError("Either 'pr_number' or 'branch' must be specified")
270+
git.fetch()
263271

264272
@classmethod
265273
def list_org_repos(cls, conn: Connection, owner: str) -> List["Repo"]:

osc/gitea_api/staging.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
import os
2+
from typing import Optional
23

34

45
class StagingPullRequestWrapper:
56
BACKLOG_LABEL = "staging/Backlog"
67
INPROGRESS_LABEL = "staging/In Progress"
78

8-
def __init__(self, conn, owner: str, repo: str, number: int, *, topdir: str):
9+
def __init__(self, conn, owner: str, repo: str, number: int, *, topdir: str, cache_directory: Optional[str] = None):
910
from . import PullRequest
1011

1112
self.conn = conn
1213
self.owner = owner
1314
self.repo = repo
1415
self.number = number
1516
self._topdir = topdir
17+
self._cache_directory = cache_directory
1618

1719
self.pr_obj = PullRequest.get(conn, owner, repo, number)
1820
self.git = None
@@ -34,6 +36,7 @@ def clone(self):
3436
pr_number=self.number,
3537
commit=self.pr_obj.head_commit,
3638
directory=path,
39+
cache_directory=self._cache_directory,
3740
ssh_private_key_path=self.conn.login.ssh_key,
3841
)
3942
self.git = Git(path)
@@ -61,6 +64,7 @@ def clone_base(self):
6164
branch=self.pr_obj.base_branch,
6265
commit=self.pr_obj.base_commit,
6366
directory=path,
67+
cache_directory=self._cache_directory,
6468
ssh_private_key_path=self.conn.login.ssh_key,
6569
)
6670
self.base_git = Git(path)

0 commit comments

Comments
 (0)