GitPython icon indicating copy to clipboard operation
GitPython copied to clipboard

Use winreg to find GIT_PYTHON_GIT_EXECUTABLE

Open tardyp opened this issue 5 years ago • 2 comments

As per https://stackoverflow.com/questions/8507368/programmatically-not-manually-finding-the-path-where-git-is-installed-on-a-win

It looks like one could use the winreg in order to find git executable. Is this something you tried or are willing to take patch?

tardyp avatar Dec 14 '18 15:12 tardyp

FWIW, the following method do works for me:

import sys

if sys.platform == 'win32':
    import os
    import winreg
    def get_git_binary_path():
        proc_arch = os.getenv('PROCESSOR_ARCHITECTURE', '').lower()
        proc_arch64 = os.getenv('PROCESSOR_ARCHITEW6432','').lower()

        if proc_arch == 'x86' and not proc_arch64:
            arch_keys = {0}
        elif proc_arch == 'x86' or proc_arch == 'amd64':
            arch_keys = {winreg.KEY_WOW64_32KEY, winreg.KEY_WOW64_64KEY}
        else:
            raise Exception("Unhandled arch: %s" % proc_arch)

        for arch_key in arch_keys:
            for base_key in [winreg.HKEY_LOCAL_MACHINE, winreg.HKEY_CURRENT_USER]:
                try:
                    with winreg.OpenKey(
                            base_key, r"Software\GitForWindows",
                            0, winreg.KEY_READ | arch_key) as key:
                        path = winreg.QueryValueEx(key, 'InstallPath')[0]
                        git_path = os.path.join(path, 'bin', 'git.exe')
                        if os.path.exists(git_path):
                            return git_path
                except FileNotFoundError:
                    continue
        return None
else:
    # on normal platforms, git is just in the PATH
    def get_git_binary_path():
        return 'git'

tardyp avatar Dec 17 '18 10:12 tardyp

@tardyp thanks a lot for the suggestion and the research! If you could provide the code above in a PR I would happily merge it.

Byron avatar Dec 22 '18 13:12 Byron