DEV Community

Kenichiro Nakamura
Kenichiro Nakamura

Posted on

7

C# git tips: Use git hook to test your code

I use Visual Studio to develop C# app, but I usually use git cli to run git command instead of Visual Studio git tools.

I don't have much issue doing this, but I sometimes encounter issues. For example:

  • Visual Studio don't save csproj file automatically, and I forget to save it before git commit
  • I usually run dotnet build and dotnet test, but I sometimes forget to confirm it

In both cases, I see CI error happens in the cloud which is embarrassing.

git hook

In such case, I can use git hook to make sure I run build and test right before I do git commit.

1. Go to .git/hooks folder and copy pre-commit.sample file.

2. Rename it to pre-commit.

3. Replace the code.

#!/bin/sh

echo "dotnet build"

dotnet clean; dotnet build
rc=$?

if [[ $rc != 0 ]] ; then
    echo -e "build failed"
    exit $rc
fi

dotnet "dotnet test"

rc=$?

if [[ $rc != 0 ]] ; then
    echo -e "test failed"
    exit $rc
fi

exit 0
Enter fullscreen mode Exit fullscreen mode

4. Save the file.

Test the commit

Now we can add any change and do commit. Then the script runs to check if my code is fine or not.

Use case

There are many use-cases such as:

  • build
  • test
  • format
  • comment validation, etc.

Each project may have different requirements so we can simply modify the file to meet our requirements.

PowerShell?

If you prefer PowerShell, then you can simply use it. One easy way to do it is to create ps1 file which contains your PowerShell script, and call it from pre-commit.

#!/bin/sh
echo "# start some PS script"
exec powershell.exe -NoProfile -ExecutionPolicy Bypass -file "the.ps1"

exit
Enter fullscreen mode Exit fullscreen mode

There is great article about this. Automation of your GIT repository via GIT hooks and PowerShell scripts

Image of AssemblyAI tool

Challenge Submission: SpeechCraft - AI-Powered Speech Analysis for Better Communication

SpeechCraft is an advanced real-time speech analytics platform that transforms spoken words into actionable insights. Using cutting-edge AI technology from AssemblyAI, it provides instant transcription while analyzing multiple dimensions of speech performance.

Read full post

Top comments (2)

Collapse
 
kryptobi profile image
kryptobi

Nice, what happened when u got a big Solution with 200 or more projects. Every commit will be a loooong waiting commit.
I use the hook to format the files.

Collapse
 
kenakamu profile image
Kenichiro Nakamura

That's so true. In case of big working tree, we may use sparse check out and partial clone

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay