checkupdates: remove appid branches that have been merged

To avoid having thousands of branches on checkupdatess-bot's remote, this
cleans up any remote branches that are pointing to commit that has been
fully merged.
This commit is contained in:
Hans-Christoph Steiner 2022-06-28 17:30:48 +02:00 committed by linsui
parent 78b368f88b
commit 206f07364b
2 changed files with 52 additions and 6 deletions

View file

@ -724,6 +724,27 @@ def push_commits(remote_name='origin'):
logging.debug(remote.url + ': ' + pushinfo.summary)
def prune_empty_appid_branches(git_repo=None):
"""Remove empty branches from checkupdates-bot git remote."""
if git_repo is None:
git_repo = git.Repo.init('.')
main_branch = 'main'
if main_branch not in git_repo.remotes.upstream.refs:
main_branch = 'master'
upstream_main = 'upstream/' + main_branch
remote = git_repo.remotes.origin
remote.update(prune=True)
merged_branches = git_repo.git().branch(remotes=True, merged=upstream_main).split()
for remote_branch in merged_branches:
if not remote_branch or '/' not in remote_branch:
continue
if remote_branch.split('/')[1] not in (main_branch, 'HEAD'):
for ref in git_repo.remotes.origin.refs:
if remote_branch == ref.name:
remote.push(':%s' % ref.remote_head, force=True) # rm remote branch
def status_update_json(processed: list, failed: dict) -> None:
"""Output a JSON file with metadata about this run."""
logging.debug(_('Outputting JSON'))

View file

@ -327,7 +327,7 @@ class CheckupdatesTest(unittest.TestCase):
self.assertEqual(vername, '2')
self.assertEqual(vercode, 2)
def test_push_commits(self):
def _get_test_git_repos(self):
testdir = self.testdir.name
os.chdir(testdir)
os.mkdir('metadata')
@ -338,22 +338,28 @@ class CheckupdatesTest(unittest.TestCase):
git_repo.index.commit("all metadata files")
git_remote_upstream = os.path.join(testdir, 'git_remote_upstream')
upstream = git.Repo.init(git_remote_upstream, bare=True)
upstream_repo = git.Repo.init(git_remote_upstream, bare=True)
git_repo.create_remote('upstream', 'file://' + git_remote_upstream)
git_remote_origin = os.path.join(testdir, 'git_remote_origin')
origin = git.Repo.init(git_remote_origin, bare=True)
origin_repo = git.Repo.init(git_remote_origin, bare=True)
git_repo.create_remote('origin', 'file://' + git_remote_origin)
return git_repo, origin_repo, upstream_repo
def test_push_commits(self):
git_repo, origin_repo, upstream_repo = self._get_test_git_repos()
for remote in git_repo.remotes:
remote.push(git_repo.active_branch)
self.assertEqual(git_repo.head, upstream.head)
self.assertEqual(origin.head, upstream.head)
self.assertEqual(git_repo.head, upstream_repo.head)
self.assertEqual(origin_repo.head, upstream_repo.head)
# pretend that checkupdates ran but didn't create any new commits
fdroidserver.checkupdates.push_commits()
appid = 'org.adaway'
self.assertNotIn(appid, git_repo.branches)
self.assertNotIn(appid, origin_repo.branches)
self.assertNotIn(appid, upstream_repo.branches)
self.assertNotIn('checkupdates', git_repo.branches)
self.assertNotIn(appid, git_repo.remotes.origin.repo.branches) # TODO fix
# now make commit
app = fdroidserver.metadata.read_metadata({appid: -1})[appid]
@ -373,6 +379,25 @@ class CheckupdatesTest(unittest.TestCase):
self.assertNotIn('checkupdates', git_repo.branches)
self.assertNotIn(appid, git_repo.remotes.upstream.refs)
def test_prune_empty_appid_branches(self):
git_repo, origin_repo, upstream_repo = self._get_test_git_repos()
for remote in git_repo.remotes:
remote.push(git_repo.active_branch)
self.assertEqual(git_repo.head, upstream_repo.head)
self.assertEqual(origin_repo.head, upstream_repo.head)
appid = 'org.adaway'
git_repo.create_head(appid, force=True)
git_repo.remotes.origin.push(appid, force=True)
self.assertIn(appid, git_repo.branches)
self.assertIn(appid, origin_repo.branches)
self.assertIn(appid, git_repo.remotes.origin.refs)
self.assertNotIn(appid, git_repo.remotes.upstream.refs)
fdroidserver.checkupdates.prune_empty_appid_branches()
self.assertNotIn(appid, origin_repo.branches)
self.assertNotIn(appid, git_repo.remotes.origin.refs)
self.assertNotIn(appid, git_repo.remotes.upstream.refs)
def test_make_merge_request(self):
testdir = self.testdir.name
os.chdir(testdir)