Merge branch 'stable' into devel
[tails.git] / bin / delete-merged-git-branches
blob0512cec2e3f09ee93955b8db43184eb4a33d7055
1 #!/usr/bin/python3
3 import argparse
4 import pprint
5 import sys
7 from tailslib.git import Git
9 # Functions
12 def strtobool(val):
13     """Convert a string representation of truth to true (1) or false (0).
14     True values are 'y', 'yes', 't', 'true', 'on', and '1'; false values
15     are 'n', 'no', 'f', 'false', 'off', and '0'.  Raises ValueError if
16     'val' is anything else.
17     """
18     val = val.lower()
19     if val in ('y', 'yes', 't', 'true', 'on', '1'):
20         return 1
21     elif val in ('n', 'no', 'f', 'false', 'off', '0'):
22         return 0
23     else:
24         raise ValueError("invalid truth value %r" % (val,))
27 def yes_no_input(prompt, default=True):
28     if default:
29         options = 'Y/n'
30     else:
31         options = 'N/y'
32     sys.stdout.write('%s [%s] ' % (prompt, options))
33     while True:
34         answer = input().lower()
35         if len(answer) == 0:
36             return default
37         else:
38             try:
39                 return strtobool(answer)
40             except ValueError:
41                 print("Please respond with 'y' or 'n'.")
44 # Parse command-line arguments
46 parser = argparse.ArgumentParser(description='Delete merged Git branches.')
47 parser.add_argument('--repo', type=str, dest='repo',
48                     help='Path to an up-to-date (bare) Tails Git repository.')
49 parser.add_argument('--remote', type=str, dest='remote', default='origin',
50                     help='Push to the specified remote instead of "origin".')
51 parser.add_argument('--batch', type=bool, dest='batch',
52                     nargs='?', const=True, default=False,
53                     help='Assume "yes" as answer to all prompts.')
54 args = parser.parse_args()
57 # Main
59 pp = pprint.PrettyPrinter()
60 tailsgit = Git(args.repo)
61 branches_to_delete = tailsgit.branches_to_delete()
63 if not branches_to_delete:
64     print("No branch to delete was found.")
65     sys.exit(0)
67 print("The following branches will be deleted:")
68 pp.pprint(sorted(branches_to_delete))
69 if not args.batch \
70    and not yes_no_input("Remove these branches?", default=False):
71     sys.exit(0)
73 tailsgit.push(args.remote, [':%s' % branch for branch in branches_to_delete])