From e0f03c99f8cd9db8ab5a97fd5656042a160f8955 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Sun, 9 May 2021 12:34:32 +0300 Subject: initial commit --- .clang-format | 6 ++++ .gitattributes | 1 + .github/workflows/test.yml | 56 +++++++++++++++++++++++++++++++++ LICENSE.txt | 21 +++++++++++++ README.md | 36 ++++++++++++++++++++++ action.yml | 77 ++++++++++++++++++++++++++++++++++++++++++++++ test/chromium.cpp | 3 ++ test/file.cpp | 3 ++ 8 files changed, 203 insertions(+) create mode 100644 .clang-format create mode 100644 .gitattributes create mode 100644 .github/workflows/test.yml create mode 100644 LICENSE.txt create mode 100644 README.md create mode 100644 action.yml create mode 100644 test/chromium.cpp create mode 100644 test/file.cpp diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..be90c3c --- /dev/null +++ b/.clang-format @@ -0,0 +1,6 @@ +--- +Language: Cpp +BasedOnStyle: Chromium + +IndentWidth: 4 +... diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..176a458 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +* text=auto diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..37492f2 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,56 @@ +name: Test + +on: + push: + pull_request: + schedule: + # Weekly, at 5:45 AM on Friday (somewhat randomly chosen). + - cron: '45 5 * * 5' + workflow_dispatch: + +jobs: + old_and_new: + strategy: + matrix: + os: [ubuntu-latest] + version: [10, 11] + style: [file, Chromium] + include: + - {style: file, exclude: test/chromium.cpp} + - {style: Chromium, exclude: test/file.cpp} + + runs-on: '${{ matrix.os }}' + + name: '${{ matrix.os }} / ${{ matrix.version }} / ${{ matrix.style }}' + + defaults: + run: + shell: pwsh + + steps: + - name: Checkout + uses: actions/checkout@v2 + + - name: clang-format (success) + uses: ./ + with: + version: '${{ matrix.version }}' + style: '${{ matrix.style }}' + exclude: '${{ matrix.exclude }}' + + - id: diff + name: clang-format (fail) + uses: ./ + with: + version: '${{ matrix.version }}' + style: '${{ matrix.style }}' + continue-on-error: true + + - name: Verify diff + run: | + $output = '${{ steps.diff.outputs.diff }}' + $basename = Split-Path '${{ matrix.exclude }}' -Leaf + $original = "$basename (original)" + $formatted = "$basename (formatted)" + $output.Contains($original) -or $(throw "Diff should contain: $original") + $output.Contains($formatted) -or $(throw "Diff should contain: $formatted") diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..e758552 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2021 Egor Tensin + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..eff4f5f --- /dev/null +++ b/README.md @@ -0,0 +1,36 @@ +clang-format +============ + +[![Test](https://github.com/egor-tensin/clang-format/actions/workflows/test.yml/badge.svg)](https://github.com/egor-tensin/clang-format/actions/workflows/test.yml) + +This GitHub action runs `clang-format` on your C/C++ source files and shows the +diff. + +Use it in your workflow like this: + + - name: clang-format + uses: egor-tensin/clang-format@v1 + +API +--- + +| Input | Value | Default | Description +| ------- | ------- | ------- | ----------- +| version | *empty* | ✓ | Use `clang-format`. +| | *any* | | Use `clang-format-VERSION`. +| style | file | ✓ | Use .clang-format. +| | *any* | | `-style=STYLE` (Microsoft, LLVM, etc.). +| exclude | *empty* | ✓ | Don't exclude any files. +| | *any* | | List of files and directories to exclude, separated by a colon `:`. + +| Output | Description +| ------ | ----------- +| diff | Diff between the original and the properly formatted files. + +License +------- + +Distributed under the MIT License. +See [LICENSE.txt] for details. + +[LICENSE.txt]: LICENSE.txt diff --git a/action.yml b/action.yml new file mode 100644 index 0000000..790f2a9 --- /dev/null +++ b/action.yml @@ -0,0 +1,77 @@ +name: clang-format (w/ diff) +description: clang-format all C/C++ files in the project, and show the diff + +inputs: + version: + description: clang-format version + required: false + style: + description: clang-format -style parameter value + required: false + default: file + exclude: + description: ':-separated list of excluded files and directories' + required: false +outputs: + diff: + description: diff between the actual and the formatted code + value: '${{ steps.run.outputs.diff }}' + +runs: + using: composite + steps: + - id: run + run: | + New-Variable version -Value '${{ inputs.version }}' -Option Constant + New-Variable style -Value '${{ inputs.style }}' -Option Constant + New-Variable exclude -Value '${{ inputs.exclude }}' -Option Constant + + if (Get-Command python3 -ErrorAction SilentlyContinue) { + $python = @('python3') + } elseif (Get-Command py -ErrorAction SilentlyContinue) { + $python = @('py', '-3') + } elseif (Get-Command python -ErrorAction SilentlyContinue) { + $python = @('python') + } else { + echo @' + ::error :: + egor-tensin/clang-format uses a Python script, which requires Python 3.6 or later. + We couldn't find a Python installation in this environment. + '@ + exit 1 + } + + $cmake_common_version = 'v3.2' + $script_name = 'project-clang-format.py' + $script_path = Join-Path '${{ github.workspace }}' .. $script_name + $download_url = "https://raw.githubusercontent.com/egor-tensin/cmake-common/$cmake_common_version/tools/$script_name" + + $ProgressPreference = 'SilentlyContinue' + Invoke-WebRequest $download_url -OutFile $script_path + + $clang_format = 'clang-format' + if ($version) { + $clang_format += "-$version" + } + + $exclude_list = $exclude.Split(':', [System.StringSplitOptions]::RemoveEmptyEntries) + if ($exclude_list) { + $exclude_list = ,'--exclude' + $exclude_list + } + + $exe, $args = $python + $output = & $exe $args $script_path --clang-format $clang_format --style $style $exclude_list | Out-String + + $output = $output.TrimEnd("`n") + $output = $output.TrimEnd("`r") + Write-Output $output + + # If anyone thinks this workaround is anything but complete batshit + # insanity, they should just fucking kill themselves. + # https://github.community/t/set-output-truncates-multiline-strings/16852/3 + $output = $output.replace('%', '%25') + $output = $output.replace("`n", '%0A') + $output = $output.replace("`r", '%0D') + + Write-Output "::set-output name=diff::$output" + shell: pwsh diff --git a/test/chromium.cpp b/test/chromium.cpp new file mode 100644 index 0000000..c2b0e40 --- /dev/null +++ b/test/chromium.cpp @@ -0,0 +1,3 @@ +int a() { + return 42; +} diff --git a/test/file.cpp b/test/file.cpp new file mode 100644 index 0000000..548ff67 --- /dev/null +++ b/test/file.cpp @@ -0,0 +1,3 @@ +int b() { + return 42; +} -- cgit v1.2.3