Skip to content

Commit 3684d06

Browse files
committed
GitAddon: skip cache usage when not running on the default branch
Fixes #335.
1 parent 760c9b0 commit 3684d06

1 file changed

Lines changed: 39 additions & 6 deletions

File tree

src/pkgcheck/addons/git.py

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ def gitignored(self, path):
461461
return False
462462

463463
@staticmethod
464-
def _get_commit_hash(path, commit='origin/HEAD'):
464+
def _get_commit_hash(path, commit):
465465
"""Retrieve a git repo's commit hash for a specific commit object."""
466466
try:
467467
p = subprocess.run(
@@ -472,6 +472,30 @@ def _get_commit_hash(path, commit='origin/HEAD'):
472472
raise GitError(f'failed retrieving commit hash for git repo: {path!r}')
473473
return p.stdout.strip()
474474

475+
@staticmethod
476+
def _get_current_branch(path, commit='HEAD'):
477+
"""Retrieve a git repo's current branch for a specific commit object."""
478+
try:
479+
p = subprocess.run(
480+
['git', 'rev-parse', '--abbrev-ref', commit],
481+
stdout=subprocess.PIPE, stderr=subprocess.DEVNULL,
482+
cwd=path, check=True, encoding='utf8')
483+
except subprocess.CalledProcessError:
484+
raise GitError(f'failed retrieving branch for git repo: {path!r}')
485+
return p.stdout.strip()
486+
487+
@staticmethod
488+
def _get_default_branch(path):
489+
"""Retrieve a git repo's default branch used with origin remote."""
490+
try:
491+
p = subprocess.run(
492+
['git', 'symbolic-ref', 'refs/remotes/origin/HEAD'],
493+
stdout=subprocess.PIPE, stderr=subprocess.DEVNULL,
494+
cwd=path, check=True, encoding='utf8')
495+
except subprocess.CalledProcessError:
496+
raise GitError(f'failed retrieving branch for git repo: {path!r}')
497+
return p.stdout.strip().split('/')[-1]
498+
475499
@staticmethod
476500
def pkg_history(repo, commit_range, data=None, local=False, verbosity=-1):
477501
"""Create or update historical package data for a given commit range."""
@@ -498,7 +522,15 @@ def update_cache(self, force=False):
498522
"""Update related cache and push updates to disk."""
499523
for repo in self.options.target_repo.trees:
500524
try:
501-
commit = self._get_commit_hash(repo.location)
525+
branch = self._get_current_branch(repo.location)
526+
default_branch = self._get_default_branch(repo.location)
527+
# skip cache usage when not running on the default branch
528+
if branch != default_branch:
529+
logger.debug(
530+
'skipping %s git repo cache update on '
531+
'non-default branch %r', repo, branch)
532+
continue
533+
commit = self._get_commit_hash(repo.location, 'origin/HEAD')
502534
except GitError:
503535
continue
504536

@@ -518,6 +550,7 @@ def update_cache(self, force=False):
518550
else:
519551
data = git_cache.data
520552
commit_range = f'{git_cache.commit}..origin/HEAD'
553+
521554
try:
522555
self.pkg_history(
523556
repo, commit_range, data=data,
@@ -549,8 +582,8 @@ def commits_repo(self, repo_cls):
549582
data = {}
550583

551584
try:
552-
origin = self._get_commit_hash(target_repo.location)
553-
head = self._get_commit_hash(target_repo.location, commit='HEAD')
585+
origin = self._get_commit_hash(target_repo.location, 'origin/HEAD')
586+
head = self._get_commit_hash(target_repo.location, 'HEAD')
554587
if origin != head:
555588
data = self.pkg_history(target_repo, 'origin/HEAD..HEAD', local=True)
556589
except GitError as e:
@@ -564,8 +597,8 @@ def commits(self):
564597
commits = ()
565598

566599
try:
567-
origin = self._get_commit_hash(target_repo.location)
568-
head = self._get_commit_hash(target_repo.location, commit='HEAD')
600+
origin = self._get_commit_hash(target_repo.location, 'origin/HEAD')
601+
head = self._get_commit_hash(target_repo.location, 'HEAD')
569602
if origin != head:
570603
commits = GitRepoCommits(target_repo.location, 'origin/HEAD..HEAD')
571604
except GitError as e:

0 commit comments

Comments
 (0)