From fa947ac6cc77536aaf7b97de493f2a6e3a5826d7 Mon Sep 17 00:00:00 2001 From: Elizabeth King Date: Wed, 5 Aug 2020 13:35:48 +0100 Subject: [PATCH 01/17] Add dotnet-format hook and sample files for testing --- .pre-commit-config.yaml | 17 +++++++++++++++++ .pre-commit-hooks.yaml | 15 ++++++++++++++- samples/Test.csproj | 8 ++++++++ samples/Testing.cs | 13 +++++++++++++ script/check-dotnet-format.sh | 11 +++++++++++ 5 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 samples/Test.csproj create mode 100644 samples/Testing.cs create mode 100755 script/check-dotnet-format.sh diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 121b3dc..8cbc023 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -59,3 +59,20 @@ repos: - -w # autofix in place - -d # show diff - -i=2 # indent 2 spaces + + # Example usage of dotnet-format: pre-commit run dotnet-format --all-files + - repo: local + hooks: + - id: dotnet-format + name: dotnet-format + entry: script/check-dotnet-format.sh + language: script + types: + - file + - text + pass_filenames: false + files: ^.*/.*.cs$ + args: + - ./samples/ + - --folder + diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml index daab259..66a943e 100644 --- a/.pre-commit-hooks.yaml +++ b/.pre-commit-hooks.yaml @@ -110,7 +110,7 @@ files: prometheus.ya?ml$ - id: prometheus_check_rules - name: Check prometheus rules + name: prometheus_check_rules description: Validates prometheus rules entry: script/check-prometheus-rules.sh language: script @@ -130,3 +130,16 @@ - text - yaml files: \.rules?\.test\.ya?ml$ + + # Specify the project as an arg (or a folder with multiple projects using the --folder flag following the directory name) + # ie. in .pre-commit-config.yaml use args: [./path/to/dir/] or args: [./path/to/dir/, --folder] + - id: dotnet-format + name: dotnet-format + description: Check C# source code + entry: script/check-dotnet-format.sh + language: script + types: + - file + - text + pass_filenames: false + files: \.cs$ diff --git a/samples/Test.csproj b/samples/Test.csproj new file mode 100644 index 0000000..c6e376e --- /dev/null +++ b/samples/Test.csproj @@ -0,0 +1,8 @@ + + + + netstandard2.0 + + + + diff --git a/samples/Testing.cs b/samples/Testing.cs new file mode 100644 index 0000000..ab8c2fa --- /dev/null +++ b/samples/Testing.cs @@ -0,0 +1,13 @@ +using System; + +namespace Test +{ + public class Testing + { + public void DoSomething() + { + Console.WriteLine("Do something"); + } } + } +} + diff --git a/script/check-dotnet-format.sh b/script/check-dotnet-format.sh new file mode 100755 index 0000000..64cbb43 --- /dev/null +++ b/script/check-dotnet-format.sh @@ -0,0 +1,11 @@ +#!/usr/bin/env bash +# shellcheck source=../lib/setup.sh +source "$(dirname "${BASH_SOURCE[0]}")/../lib/setup.sh" || exit "${EXIT_FAILED_TO_SOURCE}" + +# It's legitimate for a linter to exit non-zero; some indicate success-but-lint that way. +set +o errexit + +# Skip bash stack-trace, otherwise our bash stack-traces clutter up the output. +export SKIP_BASH_STACKTRACE=1 + +exec dotnet format --check "${@}" From 87bafa278ef875d3b8a03dcfeb31d6a64ffaba11 Mon Sep 17 00:00:00 2001 From: elizabethking2 Date: Wed, 5 Aug 2020 13:59:14 +0100 Subject: [PATCH 02/17] Change usage so by default dotnet runs on . but can limit to a directory --- .pre-commit-config.yaml | 2 -- samples/Testing.cs | 15 +++++++-------- script/check-dotnet-format.sh | 8 +++++++- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 8cbc023..7b27be0 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -74,5 +74,3 @@ repos: files: ^.*/.*.cs$ args: - ./samples/ - - --folder - diff --git a/samples/Testing.cs b/samples/Testing.cs index ab8c2fa..32d7746 100644 --- a/samples/Testing.cs +++ b/samples/Testing.cs @@ -2,12 +2,11 @@ namespace Test { - public class Testing - { - public void DoSomething() - { - Console.WriteLine("Do something"); - } } - } + public class Testing + { + public void DoSomething() + { + Console.WriteLine("Do something"); + } + } } - diff --git a/script/check-dotnet-format.sh b/script/check-dotnet-format.sh index 64cbb43..d9c6b41 100755 --- a/script/check-dotnet-format.sh +++ b/script/check-dotnet-format.sh @@ -8,4 +8,10 @@ set +o errexit # Skip bash stack-trace, otherwise our bash stack-traces clutter up the output. export SKIP_BASH_STACKTRACE=1 -exec dotnet format --check "${@}" +if [ $# -eq 0 ] + then + echo "No args received" + exec dotnet-format --check . --folder + else + exec dotnet-format --check "${@}" --folder +fi From 60bb7ba2a975ad74b5ad0f6e28a84c40be1d3f2b Mon Sep 17 00:00:00 2001 From: elizabethking2 Date: Thu, 6 Aug 2020 09:46:32 +0100 Subject: [PATCH 03/17] Remove args from dotnet-format hook --- .pre-commit-hooks.yaml | 2 -- script/check-dotnet-format.sh | 8 +------- 2 files changed, 1 insertion(+), 9 deletions(-) diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml index 66a943e..f848ceb 100644 --- a/.pre-commit-hooks.yaml +++ b/.pre-commit-hooks.yaml @@ -131,8 +131,6 @@ - yaml files: \.rules?\.test\.ya?ml$ - # Specify the project as an arg (or a folder with multiple projects using the --folder flag following the directory name) - # ie. in .pre-commit-config.yaml use args: [./path/to/dir/] or args: [./path/to/dir/, --folder] - id: dotnet-format name: dotnet-format description: Check C# source code diff --git a/script/check-dotnet-format.sh b/script/check-dotnet-format.sh index d9c6b41..848a574 100755 --- a/script/check-dotnet-format.sh +++ b/script/check-dotnet-format.sh @@ -8,10 +8,4 @@ set +o errexit # Skip bash stack-trace, otherwise our bash stack-traces clutter up the output. export SKIP_BASH_STACKTRACE=1 -if [ $# -eq 0 ] - then - echo "No args received" - exec dotnet-format --check . --folder - else - exec dotnet-format --check "${@}" --folder -fi +exec dotnet-format --check . --folder From 1d833560bfffc61b18ec0ca64d066c65adf94ebe Mon Sep 17 00:00:00 2001 From: elizabethking2 Date: Thu, 6 Aug 2020 10:40:24 +0100 Subject: [PATCH 04/17] Update version via pre-commit autoupdate --- .pre-commit-config.yaml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 7b27be0..0a5a9ae 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -9,7 +9,7 @@ exclude: vendor|\.pb\.go repos: - repo: https://github.com/adrienverge/yamllint - rev: v1.20.0 + rev: v1.24.2 hooks: - id: yamllint args: @@ -17,7 +17,7 @@ repos: - --strict - repo: https://github.com/jumanjihouse/pre-commit-hook-yamlfmt - rev: 0.0.10 + rev: 0.0.11 hooks: - id: yamlfmt args: @@ -27,7 +27,7 @@ repos: - --width=1200 # match .yamllint.yaml; we don't care about line-length. - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v2.4.0 + rev: v3.2.0 hooks: - id: check-added-large-files - id: check-byte-order-marker From 2601f438bdfa15d500b81cfb400aaefab9905d96 Mon Sep 17 00:00:00 2001 From: Elizabeth King Date: Thu, 6 Aug 2020 11:05:25 +0100 Subject: [PATCH 05/17] Move dotnet hook samples into subdirectory --- samples/{ => dotnet-hook-samples}/Test.csproj | 0 samples/{ => dotnet-hook-samples}/Testing.cs | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename samples/{ => dotnet-hook-samples}/Test.csproj (100%) rename samples/{ => dotnet-hook-samples}/Testing.cs (100%) diff --git a/samples/Test.csproj b/samples/dotnet-hook-samples/Test.csproj similarity index 100% rename from samples/Test.csproj rename to samples/dotnet-hook-samples/Test.csproj diff --git a/samples/Testing.cs b/samples/dotnet-hook-samples/Testing.cs similarity index 100% rename from samples/Testing.cs rename to samples/dotnet-hook-samples/Testing.cs From 6dc50405fc3f7d7699f8ca7c52060fe2cf8fc840 Mon Sep 17 00:00:00 2001 From: Elizabeth King Date: Thu, 6 Aug 2020 11:05:56 +0100 Subject: [PATCH 06/17] Change local repo reference to master reference - will break until PR 9 lands --- .pre-commit-config.yaml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 0a5a9ae..43a9316 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -61,7 +61,7 @@ repos: - -i=2 # indent 2 spaces # Example usage of dotnet-format: pre-commit run dotnet-format --all-files - - repo: local + - repo: https://github.com/improbable-eng/pre-commit-plugins/ hooks: - id: dotnet-format name: dotnet-format @@ -72,5 +72,3 @@ repos: - text pass_filenames: false files: ^.*/.*.cs$ - args: - - ./samples/ From 8b40ea50cc9174b06a9177ca98565df0b0f1492e Mon Sep 17 00:00:00 2001 From: Elizabeth King Date: Thu, 6 Aug 2020 11:07:38 +0100 Subject: [PATCH 07/17] Fix indentation mismatch - will break until PR 9 lands --- samples/dotnet-hook-samples/Test.csproj | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/samples/dotnet-hook-samples/Test.csproj b/samples/dotnet-hook-samples/Test.csproj index c6e376e..46102e8 100644 --- a/samples/dotnet-hook-samples/Test.csproj +++ b/samples/dotnet-hook-samples/Test.csproj @@ -1,8 +1,5 @@ - - - netstandard2.0 - - + + netstandard2.0 + - From 45ff27952872a9fb2414e5b30e4b152aa12b437e Mon Sep 17 00:00:00 2001 From: Elizabeth King Date: Thu, 6 Aug 2020 11:28:42 +0100 Subject: [PATCH 08/17] Add xml header to .csproj --- samples/dotnet-hook-samples/Test.csproj | 2 ++ 1 file changed, 2 insertions(+) diff --git a/samples/dotnet-hook-samples/Test.csproj b/samples/dotnet-hook-samples/Test.csproj index 46102e8..376d4d0 100644 --- a/samples/dotnet-hook-samples/Test.csproj +++ b/samples/dotnet-hook-samples/Test.csproj @@ -1,3 +1,5 @@ + + netstandard2.0 From 65966ce54bf2029a67bdd89e9b1c8733732a5610 Mon Sep 17 00:00:00 2001 From: Elizabeth King Date: Thu, 6 Aug 2020 11:31:47 +0100 Subject: [PATCH 09/17] Add check-xml hook --- .pre-commit-config.yaml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 43a9316..7349d4c 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -36,6 +36,7 @@ repos: - id: check-json - id: check-merge-conflict - id: check-symlinks + - id: check-xml - id: detect-private-key - id: end-of-file-fixer - id: forbid-new-submodules @@ -62,6 +63,7 @@ repos: # Example usage of dotnet-format: pre-commit run dotnet-format --all-files - repo: https://github.com/improbable-eng/pre-commit-plugins/ + rev: 0.1.3 hooks: - id: dotnet-format name: dotnet-format From 598cb81b8642e3125513841b595b8905437e9fdf Mon Sep 17 00:00:00 2001 From: elizabethking2 Date: Thu, 6 Aug 2020 12:29:21 +0100 Subject: [PATCH 10/17] Refactor to allow arguments specifying which C# projects should be linted --- .pre-commit-config.yaml | 9 +-------- .pre-commit-hooks.yaml | 2 +- script/check-dotnet-format.sh | 12 +++++++++++- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 7349d4c..588d567 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -62,15 +62,8 @@ repos: - -i=2 # indent 2 spaces # Example usage of dotnet-format: pre-commit run dotnet-format --all-files + # To restrict to only listed directory add: args: [ path/to/a.csproj, path/to/b.csproj ] - repo: https://github.com/improbable-eng/pre-commit-plugins/ rev: 0.1.3 hooks: - id: dotnet-format - name: dotnet-format - entry: script/check-dotnet-format.sh - language: script - types: - - file - - text - pass_filenames: false - files: ^.*/.*.cs$ diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml index f848ceb..441c39c 100644 --- a/.pre-commit-hooks.yaml +++ b/.pre-commit-hooks.yaml @@ -140,4 +140,4 @@ - file - text pass_filenames: false - files: \.cs$ + files: \.csproj$ diff --git a/script/check-dotnet-format.sh b/script/check-dotnet-format.sh index 848a574..c6f957e 100755 --- a/script/check-dotnet-format.sh +++ b/script/check-dotnet-format.sh @@ -8,4 +8,14 @@ set +o errexit # Skip bash stack-trace, otherwise our bash stack-traces clutter up the output. export SKIP_BASH_STACKTRACE=1 -exec dotnet-format --check . --folder +if [ $# -eq 0 ] + then + # Run dotnet-format on entire repository + exec dotnet-format --check . --folder + else + # Run dotnet-format on the directories of any C# project files found + for PROJ in ${@} + do + dotnet-format --check $(dirname $PROJ) + done +fi From cf28b04bd645cf402b9518d0aa8e336b2c3a6b72 Mon Sep 17 00:00:00 2001 From: elizabethking2 Date: Thu, 6 Aug 2020 13:51:51 +0100 Subject: [PATCH 11/17] Change type to c# --- .pre-commit-hooks.yaml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml index 441c39c..a771562 100644 --- a/.pre-commit-hooks.yaml +++ b/.pre-commit-hooks.yaml @@ -137,7 +137,5 @@ entry: script/check-dotnet-format.sh language: script types: - - file - - text + - c# pass_filenames: false - files: \.csproj$ From 485a444768c165ebe2823960f3c2faa27c0daf18 Mon Sep 17 00:00:00 2001 From: elizabethking2 Date: Thu, 6 Aug 2020 13:56:35 +0100 Subject: [PATCH 12/17] Include example args for dotnet format --- .pre-commit-config.yaml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 588d567..fe47e6e 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -67,3 +67,5 @@ repos: rev: 0.1.3 hooks: - id: dotnet-format + args: + - samples/dotnet-hook-samples/Test.csproj From 5524f951894bed8a42d1eacef6b15127e560ce06 Mon Sep 17 00:00:00 2001 From: Elizabeth King Date: Thu, 6 Aug 2020 17:02:23 +0100 Subject: [PATCH 13/17] Update script/check-dotnet-format.sh Co-authored-by: Peter Mounce --- script/check-dotnet-format.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/script/check-dotnet-format.sh b/script/check-dotnet-format.sh index c6f957e..d88112b 100755 --- a/script/check-dotnet-format.sh +++ b/script/check-dotnet-format.sh @@ -8,7 +8,7 @@ set +o errexit # Skip bash stack-trace, otherwise our bash stack-traces clutter up the output. export SKIP_BASH_STACKTRACE=1 -if [ $# -eq 0 ] +if [[ $# -eq 0 ]] then # Run dotnet-format on entire repository exec dotnet-format --check . --folder From 44bd9e6ddfde6a55f9a1bf74b20d8aaea63d7cc3 Mon Sep 17 00:00:00 2001 From: Elizabeth King Date: Thu, 6 Aug 2020 17:02:38 +0100 Subject: [PATCH 14/17] Update script/check-dotnet-format.sh Co-authored-by: Peter Mounce --- script/check-dotnet-format.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/script/check-dotnet-format.sh b/script/check-dotnet-format.sh index d88112b..ed648dd 100755 --- a/script/check-dotnet-format.sh +++ b/script/check-dotnet-format.sh @@ -16,6 +16,6 @@ if [[ $# -eq 0 ]] # Run dotnet-format on the directories of any C# project files found for PROJ in ${@} do - dotnet-format --check $(dirname $PROJ) + dotnet-format --check "$(dirname "${PROJ}")" done fi From 1e80fb20ad769c31d31a80ee10b5011a121ea49a Mon Sep 17 00:00:00 2001 From: Elizabeth King Date: Thu, 6 Aug 2020 17:02:52 +0100 Subject: [PATCH 15/17] Update script/check-dotnet-format.sh Co-authored-by: Peter Mounce --- script/check-dotnet-format.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/script/check-dotnet-format.sh b/script/check-dotnet-format.sh index ed648dd..f6b5c6c 100755 --- a/script/check-dotnet-format.sh +++ b/script/check-dotnet-format.sh @@ -14,7 +14,7 @@ if [[ $# -eq 0 ]] exec dotnet-format --check . --folder else # Run dotnet-format on the directories of any C# project files found - for PROJ in ${@} + for PROJ in "${@}" do dotnet-format --check "$(dirname "${PROJ}")" done From a700ca3fc27d95798dd331f56ea10e1abee35567 Mon Sep 17 00:00:00 2001 From: elizabethking2 Date: Thu, 6 Aug 2020 17:05:02 +0100 Subject: [PATCH 16/17] Snake case PROJ -> project --- script/check-dotnet-format.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/script/check-dotnet-format.sh b/script/check-dotnet-format.sh index f6b5c6c..36a45de 100755 --- a/script/check-dotnet-format.sh +++ b/script/check-dotnet-format.sh @@ -14,8 +14,8 @@ if [[ $# -eq 0 ]] exec dotnet-format --check . --folder else # Run dotnet-format on the directories of any C# project files found - for PROJ in "${@}" + for project in "${@}" do - dotnet-format --check "$(dirname "${PROJ}")" + dotnet-format --check "$(dirname "${project}")" done fi From 63542b98bf6187edbaeae2a58cef91800f204385 Mon Sep 17 00:00:00 2001 From: elizabethking2 Date: Thu, 6 Aug 2020 17:06:18 +0100 Subject: [PATCH 17/17] Fix whitespace in Test.csproj --- samples/dotnet-hook-samples/Test.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/dotnet-hook-samples/Test.csproj b/samples/dotnet-hook-samples/Test.csproj index 376d4d0..6ef8bf5 100644 --- a/samples/dotnet-hook-samples/Test.csproj +++ b/samples/dotnet-hook-samples/Test.csproj @@ -2,6 +2,6 @@ - netstandard2.0 + netstandard2.0