Programmatically (not manually) finding the path where Git is installed on a Windows system

前端 未结 6 749
太阳男子
太阳男子 2020-12-13 06:12

I\'d like to write a small build helper tool that shall read some properties of the current Git working directory, like the last commit hash, whether there\'s modified files

6条回答
  •  不知归路
    2020-12-13 06:46

    I'm using the following batch file to find out where Git for Windows has been installed:

    @echo off
    
    setlocal enabledelayedexpansion
    
    rem Read the Git for Windows installation path from the Registry.
    for %%k in (HKCU HKLM) do (
        for %%w in (\ \Wow6432Node\) do (
            for /f "skip=2 delims=: tokens=1*" %%a in ('reg query "%%k\SOFTWARE%%wMicrosoft\Windows\CurrentVersion\Uninstall\Git_is1" /v InstallLocation 2^> nul') do (
                for /f "tokens=3" %%z in ("%%a") do (
                    set GIT=%%z:%%b
                    echo Found Git at "!GIT!".
                    goto FOUND
                )
            )
        )
    )
    
    goto NOT_FOUND
    
    :FOUND
    
    rem Make sure Bash is in PATH (for running scripts).
    set PATH=%GIT%bin;%PATH%
    
    rem Do something with Git ...
    
    :NOT_FOUND
    

    I should be straight forward to do something similar in .NET. Just remember that you have to explicitly check the 32-bit branch of the Registry if you're on a 64-bit Windows.

    Edit: Git for Windows 2.6.1 now additionally writes the CurrentVersion, InstallPath and LibexecPath values to the HKEY_LOCAL_MACHINE\Software\GitForWindows key.

提交回复
热议问题