From 0951ec69c60cadc7ea54f23f408d0c943f58c5c2 Mon Sep 17 00:00:00 2001 From: Toni Peter Date: Fri, 17 Jan 2025 13:55:40 +0100 Subject: [PATCH] Run clang-format only over files changed in the PR --- .github/check-c-formatting.sh | 16 ++++++++++++++++ .github/workflows/linting.yml | 4 +--- 2 files changed, 17 insertions(+), 3 deletions(-) create mode 100755 .github/check-c-formatting.sh diff --git a/.github/check-c-formatting.sh b/.github/check-c-formatting.sh new file mode 100755 index 000000000..1877ae9de --- /dev/null +++ b/.github/check-c-formatting.sh @@ -0,0 +1,16 @@ +#!/usr/bin/env sh +# I wanted to get the merge base using {{ github.base_ref }}, however this is only +# available for the event that opens the PR or edits it, not on pushes to the branch. +# Comparing to main should be an OK alternative, since it will - at worst - do more +# autoformatting than it otherwise would. + +[ -z "$1" ] && merge_base=main || merge_base="$1" + +git fetch origin $merge_base:refs/remotes/origin/$merge_base + +(git diff --name-only "origin/$merge_base") | while read filename; do + extension="${filename##*.}" + if [ "$extension" = "c" ] || [ "$extension" = "h" ]; then + clang-format -i -style=file "$filename" + fi +done diff --git a/.github/workflows/linting.yml b/.github/workflows/linting.yml index e0d357537..78d2cdd40 100644 --- a/.github/workflows/linting.yml +++ b/.github/workflows/linting.yml @@ -9,9 +9,7 @@ jobs: - name: Check out openvas-scanner uses: actions/checkout@v4 - name: Formatting - run: | - clang-format -i -style=file {src,misc,nasl}/*.{c,h} - git diff --exit-code + run: sh .github/check-c-formatting.sh ${{ github.base_ref }} Rust: runs-on: ubuntu-latest defaults: