mirror of
https://git.proxmox.com/git/fwupd
synced 2025-07-25 08:31:56 +00:00
Add contrib/reformat-code.py to pre-commit hooks
This commit is contained in:
parent
bb202244e3
commit
987e7d2929
@ -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:
|
||||
|
@ -5,29 +5,15 @@
|
||||
# SPDX-License-Identifier: LGPL-2.1+
|
||||
#
|
||||
|
||||
import argparse
|
||||
import os
|
||||
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 = {}
|
||||
|
||||
|
||||
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):
|
||||
@ -44,77 +30,25 @@ 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
|
||||
)
|
||||
if ret.returncode:
|
||||
print("Failed to run git diff")
|
||||
print("Failed to run git diff: %s" % ret.stderr)
|
||||
sys.exit(1)
|
||||
ret = subprocess.run(
|
||||
[formatter, "-p1"], input=ret.stdout, capture_output=True, check=True, text=True
|
||||
[formatter, "-p1"], input=ret.stdout, capture_output=True, text=True
|
||||
)
|
||||
if ret.returncode:
|
||||
print("Failed to run formatter")
|
||||
print("Failed to run formatter: %s % ret.stderr")
|
||||
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, text=True
|
||||
)
|
||||
if ret.returncode:
|
||||
print("Failed to run patch")
|
||||
print("Failed to run patch: %s" % ret.stderr)
|
||||
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)
|
||||
|
Loading…
Reference in New Issue
Block a user