]> git.proxmox.com Git - fwupd-efi.git/commitdiff
Port pre-commit clang-format, codespell, markdownlint from fwupd
authorMario Limonciello <superm1@gmail.com>
Fri, 30 Jul 2021 13:11:06 +0000 (08:11 -0500)
committerMario Limonciello <superm1@gmail.com>
Fri, 30 Jul 2021 14:12:57 +0000 (09:12 -0500)
.clang-format [new file with mode: 0644]
.github/workflows/main.yml
.pre-commit-config.yaml
contrib/codespell.cfg [new file with mode: 0644]
contrib/reformat-code.py [new file with mode: 0755]

diff --git a/.clang-format b/.clang-format
new file mode 100644 (file)
index 0000000..6d5f176
--- /dev/null
@@ -0,0 +1,50 @@
+---
+AlignAfterOpenBracket: 'Align'
+AlignConsecutiveAssignments: 'false'
+AlignConsecutiveDeclarations: 'false'
+AlignConsecutiveMacros: 'true'
+AlignOperands: 'true'
+AlignTrailingComments: 'true'
+AllowAllArgumentsOnNextLine: 'false'
+AllowAllParametersOfDeclarationOnNextLine: 'false'
+AllowShortBlocksOnASingleLine: 'false'
+AllowShortCaseLabelsOnASingleLine: 'false'
+AllowShortFunctionsOnASingleLine: 'Inline'
+AllowShortIfStatementsOnASingleLine: 'false'
+AlwaysBreakAfterReturnType: 'All'
+BinPackParameters: 'false'
+BinPackArguments: 'false'
+BreakBeforeBraces: 'Linux'
+ColumnLimit: '100'
+DerivePointerAlignment: 'false'
+IndentCaseLabels: 'false'
+IndentWidth: '8'
+IncludeBlocks: 'Regroup'
+KeepEmptyLinesAtTheStartOfBlocks: 'false'
+Language: 'Cpp'
+MaxEmptyLinesToKeep: '1'
+PointerAlignment: 'Right'
+SortIncludes: 'true'
+SpaceAfterCStyleCast: 'false'
+SpaceBeforeAssignmentOperators : 'true'
+SpaceBeforeParens: 'ControlStatements'
+SpaceInEmptyParentheses: 'false'
+SpacesInSquareBrackets: 'false'
+TabWidth: '8'
+UseTab: 'Always'
+
+PenaltyBreakAssignment: '3'
+PenaltyBreakBeforeFirstCallParameter: '15'
+
+IncludeCategories:
+  - Regex:           '^"config.h"$'
+    Priority:        '0'
+  - Regex:           '<fwupdplugin.h>'
+    Priority:        '1'
+  - Regex:           '^<'
+    Priority:        '2'
+  - Regex:           'fwupd'
+    Priority:        '3'
+  - Regex:           '.*'
+    Priority:        '4'
+...
index 54ae5d5f5cde923141f87991637f55c66f2b881d..c23dd179d83b17bbbbbccfb48e53ff283a28e4bb 100644 (file)
@@ -18,7 +18,7 @@ jobs:
         run: |
           ./contrib/setup
           source venv/bin/activate
-          sed -i "/no-commit-to-branch/d; /args: [--branch, main]/d" .pre-commit-config.yaml
+          sed -i "/no-commit-to-branch/,+1d" .pre-commit-config.yaml
           pre-commit run --all-files
 
   build:
index 6842c983d31626a4b7fa8d70c8a516374aaff6e4..d5a7d7235636e287af730d14c5ca0bd40f48ecb3 100644 (file)
@@ -20,8 +20,13 @@ repos:
     -   id: check-merge-conflict
     -   id: mixed-line-ending
         args: [--fix=lf]
+-   repo: https://github.com/codespell-project/codespell
+    rev: v2.1.0
+    hooks:
+    -   id: codespell
+        args: ['--config', './contrib/codespell.cfg', --write-changes]
 -   repo: https://github.com/ambv/black
-    rev: 20.8b1
+    rev: 21.6b0
     hooks:
     - id: black
 -   repo: local
@@ -31,3 +36,13 @@ repos:
       language: system
       entry: shellcheck --severity=error -e SC2068
       types: [shell]
+    - id: clang-format
+      name: clang-format
+      language: script
+      entry: ./contrib/reformat-code.py
+      types: [c]
+-   repo: https://github.com/igorshubovych/markdownlint-cli
+    rev: v0.27.1
+    hooks:
+    - id: markdownlint
+      args: ['--fix', '--ignore', '.github']
diff --git a/contrib/codespell.cfg b/contrib/codespell.cfg
new file mode 100644 (file)
index 0000000..0de0d98
--- /dev/null
@@ -0,0 +1,4 @@
+[codespell]
+builtin = clear,informal,en-GB_to_en-US
+skip = *.po,*.csv,*.svg,*.p7c,subprojects,.git,pcrs,build*,.ossfuzz,*/tests/*
+ignore-words-list = conexant,Conexant,gir,GIR,hsi,HSI,cancelled,Cancelled,te
diff --git a/contrib/reformat-code.py b/contrib/reformat-code.py
new file mode 100755 (executable)
index 0000000..65a4ac4
--- /dev/null
@@ -0,0 +1,90 @@
+#!/usr/bin/python3
+#
+# Copyright (C) 2017 Dell Inc.
+#
+# SPDX-License-Identifier: LGPL-2.1+
+#
+
+import os
+import sys
+import subprocess
+import argparse
+
+CLANG_DIFF_FORMATTERS = [
+    "clang-format-diff-11",
+    "clang-format-diff-13",
+    "clang-format-diff",
+]
+
+
+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:
+            ret = subprocess.check_call(
+                [formatter, "--help"], stdout=subprocess.PIPE, stderr=subprocess.PIPE
+            )
+            if ret == 0:
+                return formatter
+        except FileNotFoundError:
+            continue
+    print("No clang formatter installed")
+    sys.exit(1)
+
+
+## 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)
+    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 %s\n%s" % (cmd, ret.stderr.strip()))
+        sys.exit(1)
+    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 %s\n%s" % (cmd, ret.stderr.strip()))
+        sys.exit(1)
+    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 %s\n%s" % (cmd, ret.stderr.strip()))
+        sys.exit(1)
+    sys.exit(0)