#!/usr/bin/env python
import sys
from bzrlib.plugin import load_plugins
from bzrlib.branch import Branch
from bzrlib.errors import BzrError
# load plugins to resolve lp: and colo: shortcuts
load_plugins()
if len(sys.argv) == 1:
print("ERROR: Please pass a list of references to branches to be cleaned up"
" as command line arguments.")
sys.exit(1)
for branch_url in sys.argv[1:]:
try:
branch = Branch.open(branch_url)
except BzrError as e:
print e
continue
tags = branch.tags.get_tag_dict()
revids = branch.repository.all_revision_ids()
bad_tags = set()
for tag, revid in tags.iteritems():
if revid not in revids:
bad_tags.add(tag)
if not bad_tags:
print("{0}: clean".format(branch_url))
continue
try:
for tag in bad_tags:
branch.tags.delete_tag(tag)
print("Deleted {0}/{1}".format(branch_url, tag))
except BzrError as e:
print("{0}: would delete {1}".format(branch_url, ",".join(bad_tags)))
print e