From 2d8a1ee2b73eeec50f8b4badf0a895ff7982b165 Mon Sep 17 00:00:00 2001 From: def Date: Fri, 25 Sep 2020 18:44:40 +0200 Subject: [PATCH] Make fix_style.py look at files, not lines --- scripts/fix_style.py | 57 +++++++++++++------------------------------- 1 file changed, 16 insertions(+), 41 deletions(-) diff --git a/scripts/fix_style.py b/scripts/fix_style.py index b1e292248..0bc1df8c3 100755 --- a/scripts/fix_style.py +++ b/scripts/fix_style.py @@ -5,42 +5,26 @@ import re import subprocess import sys -RE_POSITION=re.compile(rb"^@@ -[0-9]+,[0-9]+ \+(?P[0-9]+),(?P[0-9]+) @@") -def get_line_ranges(git_diff): - new_filename = None - result = defaultdict(list) - for line in git_diff.splitlines(): - if line.startswith(b"+++ "): - new_filename = line[6:].decode() - continue - match = RE_POSITION.match(line) - if match: - start = int(match.group("start").decode()) - end = start + int(match.group("len").decode()) - 1 - result[new_filename].append((start, end)) - continue - return dict(result) - -def git_get_changed_lines(margin, base): - return get_line_ranges(subprocess.check_output([ +def git_get_changed_files(margin, base): + return subprocess.check_output([ "git", "diff", base, - "--unified={}".format(margin), - ])) + "--name-only", + ]).decode().splitlines() -def filter_cpp(changed_lines): - return {filename: changed_lines[filename] for filename in changed_lines - if any(filename.endswith(ext) for ext in ".c .cpp .h".split())} +def filter_cpp(changed_files): + return [filename for filename in changed_files + if any(filename.endswith(ext) for ext in ".c .cpp .h".split())] -def reformat(changed_lines): - for filename in changed_lines: - subprocess.check_call(["clang-format", "-i"] + ["--lines={}:{}".format(start, end) for start, end in changed_lines[filename]] + [filename]) +def reformat(changed_files): + for filename in changed_files: + subprocess.check_call(["clang-format", "-i", filename]) -def warn(changed_lines): +def warn(changed_files): result = 0 - for filename in changed_lines: - result = subprocess.call(["clang-format", "-Werror", "--dry-run"] + ["--lines={}:{}".format(start, end) for start, end in changed_lines[filename]] + [filename]) or result + for filename in changed_files: + result = subprocess.call(["clang-format", "-Werror", "--dry-run", filename]) or result return result def get_common_base(base): @@ -48,23 +32,14 @@ def get_common_base(base): def main(): import argparse - p = argparse.ArgumentParser(description="Check and fix style of changed lines") + p = argparse.ArgumentParser(description="Check and fix style of changed files") 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: - last_ranges = None - ranges = filter_cpp(git_get_changed_lines(1, base=args.base)) - i = 0 - while last_ranges != ranges: - print("Iteration {}".format(i)) - reformat(ranges) - last_ranges = ranges - ranges = filter_cpp(git_get_changed_lines(1, base=args.base)) - i += 1 - print("Done after {} iterations".format(i)) + reformat(filter_cpp(git_get_changed_files(1, base=args.base))) else: - sys.exit(warn(filter_cpp(git_get_changed_lines(1, base=args.base)))) + sys.exit(warn(filter_cpp(git_get_changed_files(1, base=args.base)))) if __name__ == "__main__": main()