Skip to content

Commit

Permalink
ci: enforce a format style with clang-format and black
Browse files Browse the repository at this point in the history
On PR, we could use git-clang-format instead.
  • Loading branch information
vincentbernat committed Nov 6, 2022
1 parent 39c3307 commit 3ad6dfb
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 207 deletions.
47 changes: 47 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
BasedOnStyle: WebKit
AlignAfterOpenBracket: DontAlign
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlines: Left
AlignOperands: false
AlignTrailingComments: true
AllowAllArgumentsOnNextLine: false
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortBlocksOnASingleLine: Never
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: InlineOnly
AllowShortIfStatementsOnASingleLine: WithoutElse
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterReturnType: TopLevelDefinitions
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: MultiLine
BinPackArguments: true
BinPackParameters: true
BreakBeforeBinaryOperators: None
BreakBeforeBraces: WebKit
BreakBeforeTernaryOperators: false
BreakStringLiterals: false
PenaltyBreakBeforeFirstCallParameter: 100
CompactNamespaces: true
DerivePointerAlignment: false
DisableFormat: false
ForEachMacros:
- TAILQ_FOREACH
IndentCaseLabels: false
IndentPPDirectives: AfterHash
PPIndentWidth: 2
Language: Cpp
NamespaceIndentation: None
PointerAlignment: Right
ContinuationIndentWidth: 4
IndentWidth: 8
TabWidth: 8
ColumnLimit: 88
UseTab: Always
SpaceAfterCStyleCast: false
SortIncludes: false
KeepEmptyLinesAtTheStartOfBlocks: true
TypenameMacros:
- TAILQ_ENTRY
- TAILQ_HEAD
4 changes: 3 additions & 1 deletion .dir-locals.el
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
((nix-mode . ((mode . apheleia))))
((nix-mode . ((mode . format-all)))
(c-mode . ((mode . format-all)))
(python-mode . ((mode . format-all))))
17 changes: 17 additions & 0 deletions .github/workflows/style.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
name: Code style
on:
push:
branches:
- master
pull_request:
jobs:
style-check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: cachix/install-nix-action@v17
- name: Check Python/C style
run: |
nix develop --command ci-helper format-c format-python
git diff --exit-code
52 changes: 49 additions & 3 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
pkgs = inputs.nixpkgs.legacyPackages."${system}";
in
{
defaultPackage = pkgs.stdenv.mkDerivation rec {
packages.default = pkgs.stdenv.mkDerivation rec {
name = "lldpd";
# We should be able to just use ./., but we have libevent as a submodule.
# Currently, an alternative would be to use:
# nix build "git+file://$(pwd)?submodules=1"
# Currently, we should use:
# nix build ".?submodules=1"
# See:
# - https://github.com/NixOS/nix/pull/5434
# - https://github.com/NixOS/nix/pull/5497
Expand All @@ -29,5 +29,51 @@
buildInputs = with pkgs; [ libevent readline net-snmp openssl ];
outputs = [ "out" "dev" "man" "doc" ];
};
apps = {
# Use:
# nix run ".?submodules=1#lldpcli" -- --help
lldpd = {
type = "app";
program = "${self.packages."${system}".default}/bin/lldpd";
};
lldpcli = {
type = "app";
program = "${self.packages."${system}".default}/bin/lldpcli";
};
};
devShells.default =
let
llvm = pkgs.llvmPackages_14;
clang-tools = pkgs.clang-tools.override { llvmPackages = llvm; };
in
pkgs.mkShell {
name = "lldpd-dev";
buildInputs =
self.packages."${system}".default.nativeBuildInputs ++
self.packages."${system}".default.buildInputs ++ [
clang-tools # clang-format (C)
llvm.libclang.python # git-clang-format (C)
pkgs.python3Packages.black # black (Python)

# CI helper
(pkgs.writeShellScriptBin "ci-helper" ''
set -eu
while [ $# -gt 0 ]; do
case $1 in
format-c)
echo "Run clang-format on C code..."
${pkgs.git}/bin/git ls-files '*.c' '*.h' \
| xargs ${clang-tools}/bin/clang-format -i
;;
format-python)
echo "Run black on Python code..."
${pkgs.python3Packages.black}/bin/black tests/integration
;;
esac
shift
done
'')
];
};
});
}
3 changes: 3 additions & 0 deletions include/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
DisableFormat: true
SortIncludes: false
203 changes: 0 additions & 203 deletions tests/ci/openbsd-knf-style.el

This file was deleted.

0 comments on commit 3ad6dfb

Please sign in to comment.