Azure DevOps Server 2019 Cookbook(Second Edition)
上QQ阅读APP看书,第一时间看更新

Getting ready

Let's start by exploring client-side Git hooks. Navigate to the repo\.git\hooks directory  you'll find that there a bunch of samples, but they are disabled by default. For instance, if you open that folder, you'll find a file called pre-commit.sample. To enable it, just rename it to pre-commit by removing the .sample extension and make the script executable. When you attempt to commit using git commit, the script is found and executed. If your pre-commit script exits with a 0 (zero), you commit successfully; otherwise, the commit fails:

If you are using Windows, simply renaming the file won't work. Git will fail to find the shell in the designated path as specified in the script. The problem is lurking in the first line of the script, that is, in the shebang declaration:

#!/bin/sh

On Unix-like OSes, the #! tells the program loader that this is a script to be interpreted, and /bin/sh is the path to the interpreter you want to use, which is sh in this case. Windows is definitely not a Unix-like OS. Git for Windows supports Bash commands and shell scripts via Cygwin. By default, what does it find when it looks for sh.exe at /bin/sh? Yup, nothing-nothing at all. Fix it by providing the path to the sh executable on your system. I'm using the 64-bit version of Git for Windows, so my shebang line looks like this:

#!C:/Program\ Files/Git/usr/bin/sh.exe