-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminimum_changes.py
More file actions
73 lines (59 loc) · 2.32 KB
/
minimum_changes.py
File metadata and controls
73 lines (59 loc) · 2.32 KB
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
"""minimum changes required pre-commit hook."""
import argparse
import re
import subprocess
def revert_file(filename):
"""automatically revert file"""
print(f"INFO: auto reverting `{filename}`")
# https://docs.gitlab.com/ee/topics/git/numerous_undo_possibilities_in_git/
# git reset HEAD filename
subprocess.check_output(["git", "reset", "HEAD", filename])
# git checkout -- filename
subprocess.check_output(["git", "checkout", "--", filename])
def build_argument_parser():
"""Build and return the argument parser."""
parser = argparse.ArgumentParser(
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
)
parser.add_argument("filenames", nargs="*", help="Filenames to check.")
parser.add_argument(
"--min-changes",
default="2",
help="Check for at least min-changes in git diff (defaults to 2).",
)
parser.add_argument(
"--auto-revert",
action="store_true",
help="DANGER! will revert the file automatically",
)
return parser
def main(argv=None):
"""Main process."""
# Parse command line arguments.
argparser = build_argument_parser()
args = argparser.parse_args(argv)
retval = 0
for filename in args.filenames:
output = (
subprocess.check_output(["git", "diff", "--numstat", "--cached", filename])
.decode()
.strip()
)
if output != "":
lines_add, lines_del = re.findall(r"(\d+)\s+(\d+)\s+", output)[0]
if max(int(lines_add), int(lines_del)) < int(args.min_changes):
retval = retval + 1
print(
f"INFO: file `{filename}` does not have at least `{args.min_changes}` changes"
)
if args.auto_revert:
revert_file(filename)
# print(f"INFO: auto reverting `{filename}`")
# # https://docs.gitlab.com/ee/topics/git/numerous_undo_possibilities_in_git/
# # git reset HEAD filename
# subprocess.check_output(["git", "reset", "HEAD", filename])
# # git checkout -- filename
# subprocess.check_output(["git", "checkout", "--", filename])
return retval
if __name__ == "__main__":
exit(main())