-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathcheck_git_config_email.py
More file actions
55 lines (44 loc) · 1.58 KB
/
Copy pathcheck_git_config_email.py
File metadata and controls
55 lines (44 loc) · 1.58 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
#!/usr/bin/python
"""This hook checks to ensure the Git config email matches one of the specified domains."""
import argparse
import subprocess
def build_argument_parser() -> argparse.ArgumentParser:
"""Build and return the argument parser."""
parser = argparse.ArgumentParser(
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
)
parser.add_argument(
"--domains",
nargs="+",
help="One or more domains that the Git config email address must match.",
)
return parser
def main(argv: list[str] | None = None) -> int:
"""Main process."""
# Parse command line arguments.
argparser = build_argument_parser()
args = argparser.parse_args(argv)
retval = 0
if args.domains:
proc = subprocess.run(
["git", "config", "--get", "user.email"],
check=False,
capture_output=True,
text=True,
)
user_email = proc.stdout.strip()
if not user_email:
print("Git config email is not set.")
retval = 1
elif "@" not in user_email:
print("Git config email does not look like an email address.")
print("Git config email: " + user_email)
retval = 1
elif not any(user_email.endswith("@" + x) for x in args.domains):
print("Git config email is from an unexpected domain.")
print("Git config email: " + user_email)
print("Expected domains: " + str(args.domains))
retval = 1
return retval
if __name__ == "__main__":
exit(main())