Skip to content

Commit eadca9d

Browse files
[Fixes #27632][Infrastructure] Added dotnet-format pre commit hook to run dotnet-format on changed files
* Added pre-commit hook to format changed source files. * Added code to build script to check dotnet-format tool installation. * Configure default git hooks directory to .githooks to version and track hooks.
1 parent 461c08b commit eadca9d

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

.githooks/pre-commit

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/bin/sh
2+
LC_ALL=C
3+
4+
# Select files to format
5+
FILES=$(git show --name-only --pretty="")
6+
[ -z "$FILES" ] && exit 0
7+
8+
# Format all selected files with dotnet-format
9+
echo "dotnet-format: Formatting changed source files.."
10+
echo "$FILES" | cat | xargs | sed -e 's/ /,/g' | xargs dotnet-format --folder . --include >/dev/null
11+
echo "dotnet-format: $(git diff --cached --numstat | wc -l) file(s) formatted."
12+
13+
# Add files to staging
14+
echo "$FILES" | xargs git add
15+
exit 0

build.ps1

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,24 @@ if (-not $foundJdk -and $RunBuild -and ($All -or $BuildJava) -and -not $NoBuildJ
356356
Write-Error "Could not find the JDK. Either run $PSScriptRoot\eng\scripts\InstallJdk.ps1 to install for this repo, or install the JDK globally on your machine (see $PSScriptRoot\docs\BuildFromSource.md for details)."
357357
}
358358

359+
# Check dotnet-format is installed or not
360+
$dotnetFormat = Get-Command dotnet-format -ErrorAction Ignore -CommandType Application
361+
362+
if($dotnetFormat)
363+
{
364+
Write-Host -f Magenta "dotnet format tool is already installed."
365+
}
366+
else
367+
{
368+
Write-Host -f Magenta "Installing dotnet-format tool.."
369+
& dotnet tool install -g dotnet-format
370+
}
371+
372+
# We need to change default git hooks directory as .git folder is not tracked. And by default hooks are stored in .git/hooks folder.
373+
# So we are setting git hooks default directory to .githooks, so that we can track and version the git hooks.
374+
& git config core.hooksPath .githooks
375+
376+
359377
# Initialize global variables need to be set before the import of Arcade is imported
360378
$restore = $RunRestore
361379

0 commit comments

Comments
 (0)