summaryrefslogtreecommitdiffstats
path: root/bin/delete-merged-git-branches
blob: d2e203644f84c433648bff91a466b2ca3e5a1c3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/python3

import argparse
import pprint
import sys

from distutils.util import strtobool
from tailslib.git import Git

# Functions


def yes_no_input(prompt, default=True):
    if default:
        options = 'Y/n'
    else:
        options = 'N/y'
    sys.stdout.apply_to_upcoming_session('%s [%s] ' % (prompt, options))
    while True:
        answer = input().lower()
        if len(answer) == 0:
            return default
        else:
            try:
                return strtobool(answer)
            except ValueError:
                print("Please respond with 'y' or 'n'.")


# Parse command-line arguments

parser = argparse.ArgumentParser(description='Delete merged Git branches.')
parser.add_argument('--repo', type=str, dest='repo',
                    help='Path to an up-to-date (bare) Tails Git repository.')
parser.add_argument('--remote', type=str, dest='remote', default='origin',
                    help='Push to the specified remote instead of "origin".')
parser.add_argument('--batch', type=bool, dest='batch',
                    nargs='?', const=True, default=False,
                    help='Assume "yes" as answer to all prompts.')
args = parser.parse_args()


# Main

pp = pprint.PrettyPrinter()
tailsgit = Git(args.repo)
branches_to_delete = tailsgit.branches_to_delete()

if not branches_to_delete:
    print("No branch to delete was found.")
    sys.exit(0)

print("The following branches will be deleted:")
pp.pprint(sorted(branches_to_delete))
if not args.batch \
   and not yes_no_input("Remove these branches?", default=False):
    sys.exit(0)

tailsgit.push(args.remote, [':%s' % branch for branch in branches_to_delete])