mirror of
https://git.proxmox.com/git/fwupd
synced 2025-04-28 16:44:23 +00:00
trivial: make contrib/reformat-code.py easier to use
1. Let the user know what commit it's reformatting in stdout 2. Add a debug mode to show the command that was launched 3. Fix output of errors 4. Allow the user to specify starting commit in an argument
This commit is contained in:
parent
269c1ea1d3
commit
8b679f7320
@ -58,5 +58,5 @@ from the PackageKit Coding Style.
|
||||
|
||||
`./contrib/reformat-code.py` can be used in order to get automated
|
||||
formatting. Calling the script without arguments formats the current
|
||||
patch while passing filenames will do whole-file formatting on the
|
||||
specified files.
|
||||
patch while passing commits will do formatting on everything changed since that
|
||||
commit.
|
||||
|
@ -8,6 +8,7 @@
|
||||
import os
|
||||
import sys
|
||||
import subprocess
|
||||
import argparse
|
||||
|
||||
CLANG_DIFF_FORMATTERS = [
|
||||
"clang-format-diff-11",
|
||||
@ -16,6 +17,20 @@ CLANG_DIFF_FORMATTERS = [
|
||||
]
|
||||
|
||||
|
||||
def parse_args():
|
||||
parser = argparse.ArgumentParser(
|
||||
description="Reformat C code to match project style",
|
||||
epilog="Call with no argument to reformat uncommitted code.",
|
||||
)
|
||||
parser.add_argument(
|
||||
"commit", nargs="*", default="", help="Reformat all changes since this commit"
|
||||
)
|
||||
parser.add_argument(
|
||||
"--debug", action="store_true", help="Display all launched commands"
|
||||
)
|
||||
return parser.parse_args()
|
||||
|
||||
|
||||
def select_clang_version(formatters):
|
||||
for formatter in formatters:
|
||||
try:
|
||||
@ -32,28 +47,44 @@ def select_clang_version(formatters):
|
||||
|
||||
## Entry Point ##
|
||||
if __name__ == "__main__":
|
||||
args = parse_args()
|
||||
base = os.getenv("GITHUB_BASE_REF")
|
||||
if base:
|
||||
base = "origin/%s" % base
|
||||
else:
|
||||
if args.commit:
|
||||
base = args.commit[0]
|
||||
else:
|
||||
base = "HEAD"
|
||||
cmd = ["git", "describe", base]
|
||||
if args.debug:
|
||||
print(cmd)
|
||||
ret = subprocess.run(cmd, capture_output=True)
|
||||
if ret.returncode:
|
||||
if args.debug:
|
||||
print(ret.stderr)
|
||||
base = "HEAD"
|
||||
print("Reformatting code against %s" % base)
|
||||
formatter = select_clang_version(CLANG_DIFF_FORMATTERS)
|
||||
ret = subprocess.run(
|
||||
["git", "diff", "-U0", base], capture_output=True, check=True, text=True
|
||||
)
|
||||
cmd = ["git", "diff", "-U0", base]
|
||||
if args.debug:
|
||||
print(cmd)
|
||||
ret = subprocess.run(cmd, capture_output=True, text=True)
|
||||
if ret.returncode:
|
||||
print("Failed to run git diff: %s" % ret.stderr)
|
||||
print("Failed to run %s\n%s" % (cmd, ret.stderr.strip()))
|
||||
sys.exit(1)
|
||||
ret = subprocess.run(
|
||||
[formatter, "-p1"], input=ret.stdout, capture_output=True, text=True
|
||||
)
|
||||
cmd = [formatter, "-p1"]
|
||||
if args.debug:
|
||||
print(cmd)
|
||||
ret = subprocess.run(cmd, input=ret.stdout, capture_output=True, text=True)
|
||||
if ret.returncode:
|
||||
print("Failed to run formatter: %s % ret.stderr")
|
||||
print("Failed to run %s\n%s" % (cmd, ret.stderr.strip()))
|
||||
sys.exit(1)
|
||||
ret = subprocess.run(
|
||||
["patch", "-p0"], input=ret.stdout, capture_output=True, text=True
|
||||
)
|
||||
cmd = ["patch", "-p0"]
|
||||
if args.debug:
|
||||
print(cmd)
|
||||
ret = subprocess.run(cmd, input=ret.stdout, capture_output=True, text=True)
|
||||
if ret.returncode:
|
||||
print("Failed to run patch: %s" % ret.stderr)
|
||||
print("Failed to run %s\n%s" % (cmd, ret.stderr.strip()))
|
||||
sys.exit(1)
|
||||
sys.exit(0)
|
||||
|
Loading…
Reference in New Issue
Block a user