Skip to content

Commit

Permalink
[Fixes #27632][Infrastructure] Added dotnet-format pre commit hook to…
Browse files Browse the repository at this point in the history
… 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.
  • Loading branch information
ShreyasJejurkar authored Jan 19, 2021
1 parent 461c08b commit eadca9d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .githooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/sh
LC_ALL=C

# Select files to format
FILES=$(git show --name-only --pretty="")
[ -z "$FILES" ] && exit 0

# Format all selected files with dotnet-format
echo "dotnet-format: Formatting changed source files.."
echo "$FILES" | cat | xargs | sed -e 's/ /,/g' | xargs dotnet-format --folder . --include >/dev/null
echo "dotnet-format: $(git diff --cached --numstat | wc -l) file(s) formatted."

# Add files to staging
echo "$FILES" | xargs git add
exit 0
18 changes: 18 additions & 0 deletions build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,24 @@ if (-not $foundJdk -and $RunBuild -and ($All -or $BuildJava) -and -not $NoBuildJ
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)."
}

# Check dotnet-format is installed or not
$dotnetFormat = Get-Command dotnet-format -ErrorAction Ignore -CommandType Application

if($dotnetFormat)
{
Write-Host -f Magenta "dotnet format tool is already installed."
}
else
{
Write-Host -f Magenta "Installing dotnet-format tool.."
& dotnet tool install -g dotnet-format
}

# 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

Expand Down

0 comments on commit eadca9d

Please sign in to comment.