Passed
Pull Request — main (#107)
by
unknown
01:10
created

pyclean.gitclean   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 23
dl 0
loc 37
rs 10
c 0
b 0
f 0

2 Functions

Rating   Name   Duplication   Size   Complexity  
A build_git_clean_command() 0 8 3
A execute_git_clean() 0 14 3
1
# SPDX-FileCopyrightText: 2020 Peter Bittner <[email protected]>
2
#
3
# SPDX-License-Identifier: GPL-3.0-or-later
4
5
import logging
6
import subprocess
7
8
GIT_FATAL_ERROR = 128
9
10
log = logging.getLogger(__name__)
11
12
13
def build_git_clean_command(
14
    ignore_patterns: list[str],
15
    dry_run=False,
16
    force=False,
17
) -> list[str]:
18
    exclude = (item for pattern in ignore_patterns for item in ['-e', pattern])
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable item does not seem to be defined.
Loading history...
19
    mode = '-n' if dry_run else '-f' if force else '-i'
20
    return ['git', 'clean', '-dx', *exclude, mode]
21
22
23
def execute_git_clean(directory, args):
24
    log.info('Executing git clean...')
25
    cmd = build_git_clean_command(args.ignore, dry_run=args.dry_run, force=args.yes)
26
27
    log.debug('Run: %s', ' '.join(cmd))
28
    result = subprocess.run(cmd, cwd=directory, check=False)  # noqa: S603
29
30
    if result.returncode == GIT_FATAL_ERROR:
31
        log.warning(
32
            'Directory %s is not under version control. Skipping git clean.',
33
            directory,
34
        )
35
    elif result.returncode:
36
        raise SystemExit(result.returncode)
37