Skip to content

Commit 48fc219

Browse files
committed
Use GitDiffGenerator in 'git-obs staging group' to speed it up
1 parent 2029b49 commit 48fc219

File tree

2 files changed

+22
-23
lines changed

2 files changed

+22
-23
lines changed

osc/commands_git/staging_group.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,7 @@ def run(self, args):
166166
ssh_private_key_path=self.gitea_conn.login.ssh_key,
167167
)
168168
clone_git = gitea_api.Git(clone_dir)
169-
clone_git.fetch()
170-
clone_git._run_git(["fetch", "origin", f"{target_branch}:{fork_branch}", "--force", "--update-head-ok"])
169+
clone_git._run_git(["fetch", "origin", f"{target_branch}:{fork_branch}", "--force", "--update-head-ok", "--depth=1"])
171170
clone_git.switch(fork_branch)
172171
clone_git.push(remote="origin", branch=fork_branch, set_upstream=True, force=args.force)
173172

@@ -202,12 +201,9 @@ def run(self, args):
202201
)
203202
target_number = pr_obj.number
204203

205-
# clone the git repos, cache submodule data
204+
# clone the target git repo, cache submodule data
206205
target = gitea_api.StagingPullRequestWrapper(self.gitea_conn, target_owner, target_repo, target_number, topdir=temp_dir, cache_directory=cache_dir)
207206
target.clone()
208-
for owner, repo, number in args.pr_list:
209-
pr = pr_map[(owner.lower(), repo.lower(), number)]
210-
pr.clone()
211207

212208
# locally merge package pull requests to the target project pull request (don't change anything on server yet)
213209
for owner, repo, number in args.pr_list:

osc/gitea_api/staging.py

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,10 @@ def clone(self):
3737
commit=self.pr_obj.head_commit,
3838
directory=path,
3939
cache_directory=self._cache_directory,
40+
depth=1,
4041
ssh_private_key_path=self.conn.login.ssh_key,
4142
)
4243
self.git = Git(path)
43-
# git fetch --all to have all commits from all remotes available
44-
self.git.fetch()
4544

4645
submodules = self.git.get_submodules()
4746
self.submodules_by_owner_repo = dict([((i["owner"].lower(), i["repo"].lower()), i) for i in submodules.values()])
@@ -65,31 +64,37 @@ def clone_base(self):
6564
commit=self.pr_obj.base_commit,
6665
directory=path,
6766
cache_directory=self._cache_directory,
67+
depth=1,
6868
ssh_private_key_path=self.conn.login.ssh_key,
6969
)
7070
self.base_git = Git(path)
71-
# git fetch --all to have all commits from all remotes available
72-
self.base_git.fetch()
7371

7472
submodules = self.base_git.get_submodules()
7573
self.base_submodules_by_owner_repo = dict([((i["owner"].lower(), i["repo"].lower()), i) for i in submodules.values()])
7674

7775
def merge(self, other):
7876
"""
7977
Merge ``other`` pull request into ``self`` by MOVING all ``PR: <pr_id>`` references.
80-
It is crucial to remove the references from the self.package_pr_maporiginal ``other`` request, otherwise the bots might get confused and close the pull request with reparented package pull request.
78+
It is crucial to remove the references from the original ``other`` request, otherwise the bots might get confused and close the pull request with reparented package pull request.
8179
"""
8280
from . import Git
81+
from . import GitDiffGenerator
8382
from . import PullRequest
8483

8584
self.pr_obj._data["body"] = PullRequest.add_pr_references(self.pr_obj.body, other.package_pr_map.keys())
8685
other.pr_obj._data["body"] = PullRequest.remove_pr_references(other.pr_obj.body, other.package_pr_map.keys())
8786

87+
self_diff = GitDiffGenerator()
88+
self_diff._gitmodules.read(os.path.join(self.git.abspath, ".gitmodules"))
89+
other_diff = GitDiffGenerator()
90+
other_diff._gitmodules.read(os.path.join(other.git.abspath, ".gitmodules"))
91+
8892
submodule_paths = []
8993

9094
for pkg_owner, pkg_repo, pkg_number in other.package_pr_map:
91-
other_submodule = other.submodules_by_owner_repo[(pkg_owner.lower(), pkg_repo.lower())]
9295
self_submodule = self.submodules_by_owner_repo.get((pkg_owner.lower(), pkg_repo.lower()), None)
96+
other_submodule = other.submodules_by_owner_repo.get((pkg_owner.lower(), pkg_repo.lower()), None)
97+
assert self_submodule or other_submodule
9398

9499
if self_submodule:
95100
# use an existing path if the submodule already exists
@@ -100,20 +105,19 @@ def merge(self, other):
100105
submodule_path = other_submodule["path"]
101106
submodule_branch = other_submodule["branch"]
102107

103-
# add a submodule if missing
104-
if not self_submodule:
105-
self.git._run_git(["submodule", "add", "-b", submodule_branch, f"../../{pkg_owner}/{pkg_repo}", submodule_path])
106-
107-
# init the submodule
108-
self.git._run_git(["submodule", "update", "--init", submodule_path])
108+
if self_submodule:
109+
self_diff.set_submodule_commit(submodule_path, self_submodule["commit"])
110+
if other_submodule:
111+
other_diff.set_submodule_commit(submodule_path, other_submodule["commit"])
109112

110-
submodule_git = Git(os.path.join(self.git.abspath, submodule_path))
111-
submodule_git.fetch()
112-
submodule_git._run_git(["fetch", "origin", f"pull/{pkg_number}/head:{submodule_branch}", "--force", "--update-head-ok"])
113-
submodule_git.switch(submodule_branch)
114113
submodule_paths.append(submodule_path)
115114

116115
if submodule_paths:
116+
import subprocess
117+
118+
with subprocess.Popen(["git", "apply", "--index", "-"], encoding="utf-8", stdin=subprocess.PIPE, cwd=self.git.abspath) as proc:
119+
proc.communicate("\n".join(self_diff.diff(other_diff)))
120+
117121
self.git.add(submodule_paths)
118122
self.git.commit(f"Merge package submodules from {other.pr_obj.base_owner}/{other.pr_obj.base_repo}!{other.pr_obj.number}")
119123

@@ -139,7 +143,6 @@ def remove(self, package_pr):
139143
# we're reverting the submodule to an older commit
140144
self.git._run_git(["submodule", "update", "--init", "--remote", submodule_path])
141145
submodule_git = Git(os.path.join(self.git.abspath, submodule_path))
142-
submodule_git.fetch()
143146
submodule_git.reset(commit=base_submodule["commit"], hard=True)
144147
self.git.add([submodule_path])
145148
else:

0 commit comments

Comments
 (0)