File: _cli.py

package info (click to toggle)
git-delete-merged-branches 7.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 300 kB
  • sloc: python: 1,843; sh: 26; makefile: 2
file content (205 lines) | stat: -rw-r--r-- 6,464 bytes parent folder | download
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# Copyright (C) 2020 Sebastian Pipping <sebastian@pipping.org>
# Licensed under GPL v3 or later

import argparse
import os
import sys
import traceback
from argparse import RawDescriptionHelpFormatter
from functools import partial
from signal import SIGINT
from subprocess import CalledProcessError
from textwrap import dedent

import colorama

from ._argparse_color import add_color_to_formatter_class
from ._confirm import Confirmation
from ._engine import DeleteMergedBranches
from ._git import Git
from ._messenger import Messenger
from ._metadata import APP, DESCRIPTION, VERSION
from ._multiselect import multiselect


def _parse_command_line(colorize: bool, args=None):
    _EPILOG = dedent(f"""\
        Software libre licensed under GPL v3 or later.
        Brought to you by Sebastian Pipping <sebastian@pipping.org>.

        Please report bugs at https://github.com/hartwork/{APP} — thank you!
    """)

    if args is None:
        args = sys.argv[1:]

    formatter_class = RawDescriptionHelpFormatter
    if colorize:
        formatter_class = add_color_to_formatter_class(formatter_class)

    prog = os.path.basename(sys.argv[0])

    parser = argparse.ArgumentParser(
        prog=prog,
        add_help=False,
        description=DESCRIPTION,
        epilog=_EPILOG,
        formatter_class=formatter_class,
    )

    modes = parser.add_argument_group("modes").add_mutually_exclusive_group()
    modes.add_argument(
        "--configure",
        dest="force_reconfiguration",
        action="store_true",
        help=f"configure {APP} and exit (without processing any branches)",
    )
    modes.add_argument("--help", "-h", action="help", help="show this help message and exit")
    modes.add_argument("--version", action="version", version="%(prog)s " + VERSION)

    rules = parser.add_argument_group("rules")
    rules.add_argument(
        "--branch",
        "-b",
        metavar="BRANCH",
        dest="required_target_branches",
        default=[],
        action="append",
        help="require the given branch as a merge target (instead of what is"
        " configured for this repository); can be passed multiple times",
    )
    rules.add_argument(
        "--effort",
        metavar="LEVEL",
        dest="effort_level",
        type=int,
        default=2,
        choices=[1, 2, 3],
        help=(
            "level of effort to put into finding merged branches"
            '; level 1 uses nothing but "git branch --merged"'
            ', level 2 adds use of "git cherry"'
            ', level 3 adds use of "git cherry" on temporary squashed copies'
            " (default level: %(default)d)"
        ),
    )

    scope = parser.add_argument_group("scope")
    scope.add_argument(
        "--remote",
        "-r",
        metavar="REMOTE",
        dest="enabled_remotes",
        default=[],
        action="append",
        help=(
            "process the given remote (instead of the remotes that are"
            " configured for this repository); can be passed multiple times"
        ),
    )
    scope.add_argument(
        "--exclude",
        "-x",
        metavar="BRANCH",
        dest="excluded_branches",
        default=[],
        action="append",
        help=(
            "exclude the given branch from deletion (in addition to"
            " the exclusion list that is configured for this repository)"
            "; can be passed multiple times"
        ),
    )
    scope.add_argument(
        "--include-regex",
        metavar="PATTERN",
        dest="included_branches_patterns",
        default=[],
        action="append",
        help=(
            "only consider branches for deletion that match the given"
            ' regular expression (e.g. "^issue-")'
            '; syntax is that of Python module "re"'
            "; can be passed multiple times"
            ', then acts in logical conjunction ("and")'
        ),
    )

    switches = parser.add_argument_group("flags")
    switches.add_argument(
        "--debug", dest="debug", action="store_true", help="enable debugging output"
    )
    switches.add_argument(
        "--dry-run",
        "-n",
        dest="pretend",
        action="store_true",
        help="perform a trial run with no changes made",
    )
    switches.add_argument(
        "--verbose", "-v", dest="verbose", action="store_true", help="enable verbose output"
    )
    switches.add_argument(
        "--yes",
        "-y",
        dest="ask",
        default=True,
        action="store_false",
        help='do not ask for confirmation, assume reply "yes"',
    )

    return parser.parse_args(args)


def _innermost_main(config, messenger, colorize: bool):
    git = Git(messenger, pretend=config.pretend, verbose=config.verbose)
    confirmation = Confirmation(messenger, ask=config.ask)
    selector = partial(multiselect, colorize=colorize)

    dmb = DeleteMergedBranches(git, messenger, confirmation, selector, config.effort_level)

    git_config = dmb.ensure_configured(config.force_reconfiguration)
    if config.force_reconfiguration:
        return

    required_target_branches = dmb.determine_required_target_branches(
        git_config, config.required_target_branches
    )
    excluded_branches = dmb.determine_excluded_branches(
        git_config, config.excluded_branches, config.included_branches_patterns
    )
    enabled_remotes = dmb.determine_enabled_remotes(git_config, config.enabled_remotes)

    dmb.refresh_remotes(enabled_remotes)
    dmb.detect_stale_remotes(enabled_remotes, required_target_branches)
    dmb.refresh_target_branches(required_target_branches)
    dmb.delete_merged_branches(required_target_branches, excluded_branches, enabled_remotes)


def _inner_main():
    colorize = "NO_COLOR" not in os.environ
    if colorize:
        colorama.init()

    messenger = Messenger(colorize=colorize)

    config = _parse_command_line(colorize=colorize)
    try:
        _innermost_main(config, messenger, colorize)
    except CalledProcessError as e:
        # Produce more human-friendly output than str(e)
        message = f"Command '{' '.join(e.cmd)}' returned non-zero exit status {e.returncode}."
        messenger.tell_error(message)
        sys.exit(1)
    except Exception as e:
        if config.debug:
            traceback.print_exc()
        messenger.tell_error(str(e))
        sys.exit(1)


def main():
    try:
        _inner_main()
    except KeyboardInterrupt:
        sys.exit(128 + SIGINT)