Skip to content

Commit

Permalink
Add contrib/reformat-code.py to pre-commit hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
djcampello committed Jul 24, 2021
1 parent a75266e commit 972fd77
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 68 deletions.
4 changes: 4 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ repos:
language: system
entry: shellcheck --severity=error -e SC2068
types: [shell]
- id: clang-format
name: clang-format
language: script
entry: ./contrib/reformat-code.py
- repo: https://github.com/igorshubovych/markdownlint-cli
rev: v0.27.1
hooks:
Expand Down
71 changes: 3 additions & 68 deletions contrib/reformat-code.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,11 @@
import sys
import subprocess

CLANG_FORMATTERS = [
"clang-format-11",
"clang-format-13",
"clang-format",
]
CLANG_DIFF_FORMATTERS = [
"clang-format-diff-11",
"clang-format-diff-13",
"clang-format-diff",
]
FIXUPS = {"g_autoptr (": "g_autoptr(", "sizeof (": "sizeof(", "g_auto (": "g_auto("}


def parse_args():
parser = argparse.ArgumentParser(description="Reformat code to match style")
parser.add_argument("files", nargs="*", help="files to reformat")
args = parser.parse_args()
return args


def select_clang_version(formatters):
Expand All @@ -44,43 +31,8 @@ def select_clang_version(formatters):
sys.exit(1)


def reformat_file(formatter, f):
print("Reformatting %s using %s" % (f, formatter))
lines = None
with open(f, "r") as rfd:
lines = rfd.readlines()
ret = subprocess.run(
[formatter, "-style=file", f], capture_output=True, check=True, text=True
)
if ret.returncode:
print("Failed to run formatter")
sys.exit(1)
formatted = ret.stdout.splitlines(True)
save = False
for idx, line in enumerate(formatted):
for fixup in FIXUPS:
if fixup in line:
formatted[idx] = line.replace(fixup, FIXUPS[fixup])
if not save and formatted[idx] != lines[idx]:
save = True
if save:
with open(f, "w") as wfd:
wfd.writelines(formatted)


def reformat_files(files):
formatter = select_clang_version(CLANG_FORMATTERS)
for f in files:
if not os.path.exists(f):
print("%s does not exist" % f)
sys.exit(1)
if not (f.endswith(".c") or f.endswith(".h")):
print("Skipping %s" % f)
continue
reformat_file(formatter, f)


def reformat_diff():
## Entry Point ##
if __name__ == "__main__":
formatter = select_clang_version(CLANG_DIFF_FORMATTERS)
ret = subprocess.run(
["git", "diff", "-U0", "HEAD"], capture_output=True, check=True, text=True
Expand All @@ -94,27 +46,10 @@ def reformat_diff():
if ret.returncode:
print("Failed to run formatter")
sys.exit(1)
formatted = ret.stdout.splitlines(True)
for idx, line in enumerate(formatted):
if not line.startswith("+"):
continue
for fixup in FIXUPS:
if fixup in line:
formatted[idx] = line.replace(fixup, FIXUPS[fixup])
fixedup = "".join(formatted)
ret = subprocess.run(
["patch", "-p0"], input=fixedup, capture_output=True, check=True, text=True
["patch", "-p0"], input=ret.stdout, capture_output=True, check=True, text=True
)
if ret.returncode:
print("Failed to run patch")
sys.exit(1)


## Entry Point ##
if __name__ == "__main__":
args = parse_args()
if len(args.files) == 0:
reformat_diff()
else:
reformat_files(args.files)
sys.exit(0)

0 comments on commit 972fd77

Please sign in to comment.