Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pre-commit hook with code formatting #58965

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions .githooks/pre-commit
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions eng/build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions eng/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Loading