From b453224d50c14dbf7aeeef003f2cea03e10a1e80 Mon Sep 17 00:00:00 2001 From: Nazar Oleksiv Date: Thu, 14 Nov 2024 22:32:37 +0200 Subject: [PATCH] Add pre-commit hook --- .githooks/pre-commit | 24 ++++++++++++++++++++++++ eng/build.ps1 | 4 ++++ eng/build.sh | 4 ++++ 3 files changed, 32 insertions(+) create mode 100644 .githooks/pre-commit diff --git a/.githooks/pre-commit b/.githooks/pre-commit new file mode 100644 index 000000000000..63583a624ea0 --- /dev/null +++ b/.githooks/pre-commit @@ -0,0 +1,24 @@ +#!/bin/sh +LC_ALL=C + +# Get the list of staged files +staged_files=$(git diff --cached --name-only --diff-filter=ACM | grep '\.cs$') + +# Exit if no staged C# files +[ -z "$staged_files" ] && exit 0 + +# Stash unstaged changes +stash_message="pre-commit-$(date +%s)" +git stash push -k -u -m "$stash_message" + +# Run dotnet format on the staged files +dotnet format --include $(echo "$staged_files" | xargs) + +# Add the formatted files back to staging +git add $staged_files + +# Restore unstaged changes +git stash pop -q $(git stash list | grep "$stash_message" | head -n 1 | awk -F: '{print $1}') + +# Exit with success +exit 0 diff --git a/eng/build.ps1 b/eng/build.ps1 index 3696fe11e0df..fb79046300a5 100644 --- a/eng/build.ps1 +++ b/eng/build.ps1 @@ -322,6 +322,10 @@ $performDotnetBuild = $BuildJava -or $BuildManaged -or $BuildNodeJS -or ` ($All -and -not ($NoBuildJava -and $NoBuildManaged -and $NoBuildNodeJS)) -or ` ($Projects -and -not ($BuildInstallers -or $specifiedBuildNative)) +# We need to change default git hooks directory as .git folder is not tracked. And by default hooks are stored in .git/hooks folder. +# So we are setting git hooks default directory to .githooks, so that we can track and version the git hooks. +& git config core.hooksPath .githooks + # Initialize global variables need to be set before the import of Arcade is imported $restore = $RunRestore diff --git a/eng/build.sh b/eng/build.sh index 1275859dfaf0..c95a766d0802 100755 --- a/eng/build.sh +++ b/eng/build.sh @@ -321,6 +321,10 @@ if [ ! -z "$runtime_source_feed$runtime_source_feed_key" ]; then toolset_build_args[${#toolset_build_args[*]}]=$runtimeFeedKeyArg fi +# We need to change default git hooks directory as .git folder is not tracked. And by default hooks are stored in .git/hooks folder. +# So we are setting git hooks default directory to .githooks, so that we can track and version the git hooks. +git config core.hooksPath .githooks + # Initialize global variables need to be set before the import of Arcade is imported restore=$run_restore