Skip to content

Commit b453224

Browse files
committed
Add pre-commit hook
1 parent eb68e01 commit b453224

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

.githooks/pre-commit

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/bin/sh
2+
LC_ALL=C
3+
4+
# Get the list of staged files
5+
staged_files=$(git diff --cached --name-only --diff-filter=ACM | grep '\.cs$')
6+
7+
# Exit if no staged C# files
8+
[ -z "$staged_files" ] && exit 0
9+
10+
# Stash unstaged changes
11+
stash_message="pre-commit-$(date +%s)"
12+
git stash push -k -u -m "$stash_message"
13+
14+
# Run dotnet format on the staged files
15+
dotnet format --include $(echo "$staged_files" | xargs)
16+
17+
# Add the formatted files back to staging
18+
git add $staged_files
19+
20+
# Restore unstaged changes
21+
git stash pop -q $(git stash list | grep "$stash_message" | head -n 1 | awk -F: '{print $1}')
22+
23+
# Exit with success
24+
exit 0

eng/build.ps1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,10 @@ $performDotnetBuild = $BuildJava -or $BuildManaged -or $BuildNodeJS -or `
322322
($All -and -not ($NoBuildJava -and $NoBuildManaged -and $NoBuildNodeJS)) -or `
323323
($Projects -and -not ($BuildInstallers -or $specifiedBuildNative))
324324

325+
# We need to change default git hooks directory as .git folder is not tracked. And by default hooks are stored in .git/hooks folder.
326+
# So we are setting git hooks default directory to .githooks, so that we can track and version the git hooks.
327+
& git config core.hooksPath .githooks
328+
325329
# Initialize global variables need to be set before the import of Arcade is imported
326330
$restore = $RunRestore
327331

eng/build.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,10 @@ if [ ! -z "$runtime_source_feed$runtime_source_feed_key" ]; then
321321
toolset_build_args[${#toolset_build_args[*]}]=$runtimeFeedKeyArg
322322
fi
323323

324+
# We need to change default git hooks directory as .git folder is not tracked. And by default hooks are stored in .git/hooks folder.
325+
# So we are setting git hooks default directory to .githooks, so that we can track and version the git hooks.
326+
git config core.hooksPath .githooks
327+
324328
# Initialize global variables need to be set before the import of Arcade is imported
325329
restore=$run_restore
326330

0 commit comments

Comments
 (0)