👉🏼 Click here to Join I ❤️ .NET WhatsApp Channel to get 🔔 notified about new articles and other updates.
Automating Git Hook Setup in .NET Projects with MSBuild

Automating Git Hook Setup in .NET Projects with MSBuild

Authors - Abdul Rahman (Content Writer), Regina Sharon (Graphic Designer)

MSBuild

1 Articles

Improve

Table of Contents

  1. What we gonna do?
  2. Why we gonna do?
  3. How we gonna do?
  4. Summary

What we gonna do?

Git hooks are custom scripts that run at specific points in the Git workflow. Pre-commit hooks, for example, execute before a commit is made and can enforce coding standards or run tests. Automating the setup of these hooks ensures consistency across a team, removing the need for manual configuration.

In this article, we'll explore how to automate the setup of a pre-commit hook in a .NET project using MSBuild. The same principle can be applied to any git hook setup in dotnet apps.

Sequence Diagram

Why we gonna do?

Setting up Git hooks manually can lead to errors and inconsistencies, especially in large team environments where its not possible to follow up with every developer or new joiner to setup git hooks because we cannot add default git hooks to source control. Automating this process within a .NET project using MSBuild ensures that the required pre-commit hook is tracked in source control and is copied before project build to git hooks directory and consistently installed and made executable for all developers. This enhances productivity and ensures that coding standards and checks are always applied.

How we gonna do?

The first step is to add a pre-commit hook script to the project. This script can include any logic you want to run before a commit is made.

Pre-Commit in Source Control

By adding a custom target to the .csproj file, you can automate the setup of a pre-commit hook. Below is an example of a custom target named CopyGitHook and a detailed explanation of its components.

Code Sample - Copying Git Pre Commit Hook before Project Build

  1. Define the pre-commit hook setup: The CopyGitHook target runs BeforeBuild, ensuring the pre-commit hook is set up before the build process starts. It executes only in non-release builds, as specified by the condition '$(Configuration)'!='Release'.
  2. Create the .git/hooks directory: The MakeDir task ensures the .git/hooks directory exists. It runs only if the directory is missing, as determined by the condition !Exists('$(ProjectDir)/../.git/hooks').
  3. Copy the pre-commit hook: The Copy task copies the pre-commit script from the ../Scripts/git_hooks folder to the .git/hooks directory. The SkipUnchangedFiles="true" attribute ensures the file is copied only if it has changed, avoiding redundant operations.
  4. Make the hook executable (Linux/macOS): The Exec task runs a shell command chmod +x to make the copied pre-commit script executable. This task runs only if the file exists and the system is not Windows, as determined by the condition Exists('$(ProjectDir)/../.git/hooks/pre-commit') AND '$(OS)' != 'Windows_NT'.
  5. Create a Windows-compatible batch script: Since Windows does not execute shell scripts natively, the WriteLinesToFile task generates a pre-commit.bat file. This file contains: @echo off bash .git/hooks/pre-commit This batch script ensures that the pre-commit hook runs correctly on Windows machines by executing it through Bash. It is created only on Windows, as specified by the condition '$(OS)' == 'Windows_NT'.

Now you can add pre commit logics like dotnet format, run test, measure code coverage, secret scanning, etc to the ../Scripts/git_hooks/pre-commit file and add it to source control. This will ensure that all developers have the same pre commit hooks setup. This also helps to add new logics or changes to pre commit in single place and track the changes.

Here is an example of ilovedotnet pre-commit hook file.

Code Sample - I Love .NET Git Pre Commit Hook

Windows Pre-Commit Output Windows Pre-Commit Output

Summary

Automating Git hook setup with MSBuild simplifies project configuration and ensures consistency across all developer machines. By leveraging a custom target in the .csproj file, you can efficiently set up pre-commit hooks without requiring manual intervention, improving both productivity and team collaboration.

👉🏼 Click here to Join I ❤️ .NET WhatsApp Channel to get 🔔 notified about new articles and other updates.
  • Msbuild
  • Git Hook
  • Pre-Commit