Skip to content

Commit 488168a

Browse files
committed
check_bugowner: Fix checkout for existing AGit branches.
1 parent 3bf616d commit 488168a

2 files changed

Lines changed: 18 additions & 13 deletions

File tree

check_bugowner.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ def _gitea_cache_dir(self):
127127
raise ValueError("Set the OSRT_BUGOWNER_CACHE_HOME variable to a directory where check_bugowner can write its cache")
128128

129129
def _gitea_checkout(
130-
self, owner: str, repo: str, revision: str, revision_name=None, remote="origin", remote_url=None
130+
self, owner: str, repo: str, revision: str, revision_name=None, remote="origin", remote_url=None, fetch=True
131131
):
132132
local_dir = Path(self._gitea_cache_dir(), owner, repo)
133133
self.logger.debug(f"Cache directory: {local_dir}")
@@ -142,7 +142,7 @@ def _gitea_checkout(
142142
)
143143
else:
144144
return self.scm.checkout_revision(
145-
local_dir, revision, revision_name=revision_name, remote=remote, remote_url=remote_url
145+
local_dir, revision, revision_name=revision_name, remote=remote, remote_url=remote_url, fetch=fetch
146146
)
147147

148148
def _git_remote_name(self, repo, project: str, url: str) -> str:
@@ -266,6 +266,8 @@ def _gitea_validate(
266266
revision_name=head_revision_name,
267267
remote=head_project,
268268
remote_url=self.scm.package_url(head_project, head_package),
269+
# No need to re-fetch, as we just did to produce the diff.
270+
fetch=False
269271
)
270272
# Read _maintainership.json and whitelist.json after checking out HEAD
271273
self._init_maintainership(repo)
@@ -314,6 +316,7 @@ def _ldap_active_user(self, email):
314316
for e in email:
315317
if e:
316318
if e not in self._cache(self.ldap_cache).keys():
319+
self.logger.debug(f"LDAP cache miss for {e}")
317320
result = instance.search_st(
318321
"OU=User accounts,DC=corp,DC=suse,DC=com",
319322
ldap.SCOPE_SUBTREE,
@@ -340,6 +343,8 @@ def _ldap_active_user(self, email):
340343
self._cache_set(self.ldap_cache, e, attrs)
341344
else:
342345
self._cache_set(self.ldap_cache, e, None)
346+
else:
347+
self.logger.debug(f"LDAP cache hit for {e}")
343348

344349
active_statuses.append(self._cache_get(self.ldap_cache, e))
345350

@@ -432,7 +437,7 @@ def _gitea_check_source_submission(
432437
if self.request.actions[0].src_branch:
433438
head_revision = self.request.actions[0].src_branch
434439

435-
head_revision_name = f"{head_project}_{head_package}_{head_revision.replace("/", "_")}"
440+
head_revision_name = f"{head_project}_{head_package}_{head_revision.replace('/', '_')}"
436441

437442
referenced_prs = [
438443
line
@@ -484,7 +489,8 @@ def _gitea_check_source_submission(
484489
# Cleanup branches
485490
self.scm.checkout_revision(repo, base_revision)
486491
repo.git.branch("-D", head_revision_name)
487-
repo.git.branch("-D", "-r", f"{head_remote_name}/{head_revision}")
492+
if f"{head_remote_name}/{head_revision}" in {ref for ref in repo.git.branch('-r').split('\n')}:
493+
repo.git.branch("-D", "-r", f"{head_remote_name}/{head_revision}")
488494

489495
return is_valid
490496

scm/git.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,23 +59,22 @@ def checkout_revision(repo, revision: str, revision_name=None, remote="origin",
5959
if not isinstance(repo, git.Repo):
6060
repo = git.Repo(repo)
6161

62-
branch_names = {b.name for b in repo.branches}
63-
64-
if fetch and revision in branch_names:
62+
if fetch:
6563
if remote_url and remote not in set(r.name for r in repo.remotes):
6664
repo.create_remote(remote, remote_url)
6765

6866
current_revision = repo.git.rev_parse("--abbrev-ref", "HEAD")
69-
if current_revision == revision:
67+
if current_revision == revision and revision_name is None:
7068
repo.remote(remote).pull(revision)
7169
else:
72-
repo.remote(remote).fetch([f"{revision}:{revision}"])
70+
if revision_name is not None:
71+
to_fetch = f"{revision}:{revision_name}"
72+
else:
73+
to_fetch = f"{revision}:{revision}"
74+
repo.remote(remote).fetch([to_fetch])
7375

7476
if revision_name is not None:
75-
if revision_name not in branch_names:
76-
repo.git.checkout("-b", revision_name, f"{remote}/{revision}")
77-
else:
78-
repo.git.switch(revision_name)
77+
repo.git.switch(revision_name)
7978
else:
8079
repo.git.checkout(revision, "--")
8180

0 commit comments

Comments
 (0)