2020-09-10 20:58:30 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2020-09-10 02:06:05 +00:00
|
|
|
from collections import defaultdict
|
|
|
|
import re
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
|
2020-09-25 16:44:40 +00:00
|
|
|
def git_get_changed_files(margin, base):
|
|
|
|
return subprocess.check_output([
|
2020-09-10 02:06:05 +00:00
|
|
|
"git",
|
|
|
|
"diff",
|
|
|
|
base,
|
2020-09-25 16:44:40 +00:00
|
|
|
"--name-only",
|
|
|
|
]).decode().splitlines()
|
2020-09-10 02:06:05 +00:00
|
|
|
|
2020-09-25 16:44:40 +00:00
|
|
|
def filter_cpp(changed_files):
|
|
|
|
return [filename for filename in changed_files
|
|
|
|
if any(filename.endswith(ext) for ext in ".c .cpp .h".split())]
|
2020-09-10 02:06:05 +00:00
|
|
|
|
2020-09-25 16:44:40 +00:00
|
|
|
def reformat(changed_files):
|
|
|
|
for filename in changed_files:
|
|
|
|
subprocess.check_call(["clang-format", "-i", filename])
|
2020-09-10 02:06:05 +00:00
|
|
|
|
2020-09-25 16:44:40 +00:00
|
|
|
def warn(changed_files):
|
2020-09-10 02:06:05 +00:00
|
|
|
result = 0
|
2020-09-25 16:44:40 +00:00
|
|
|
for filename in changed_files:
|
|
|
|
result = subprocess.call(["clang-format", "-Werror", "--dry-run", filename]) or result
|
2020-09-10 02:06:05 +00:00
|
|
|
return result
|
|
|
|
|
2020-09-10 22:54:47 +00:00
|
|
|
def get_common_base(base):
|
|
|
|
return subprocess.check_output(["git", "merge-base", "HEAD", base]).decode().strip()
|
2020-09-10 02:06:05 +00:00
|
|
|
|
|
|
|
def main():
|
|
|
|
import argparse
|
2020-09-25 16:44:40 +00:00
|
|
|
p = argparse.ArgumentParser(description="Check and fix style of changed files")
|
2020-09-10 02:06:05 +00:00
|
|
|
p.add_argument("--base", default="HEAD", help="Revision to compare to")
|
|
|
|
p.add_argument("-n", "--dry-run", action="store_true", help="Don't fix, only warn")
|
|
|
|
args = p.parse_args()
|
|
|
|
if not args.dry_run:
|
2020-09-25 16:44:40 +00:00
|
|
|
reformat(filter_cpp(git_get_changed_files(1, base=args.base)))
|
2020-09-10 02:06:05 +00:00
|
|
|
else:
|
2020-09-25 16:44:40 +00:00
|
|
|
sys.exit(warn(filter_cpp(git_get_changed_files(1, base=args.base))))
|
2020-09-10 02:06:05 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|