How to programmatically detect an editor git is using cross platform?

Let's say we are in a Python environment and we can be in Windows, OSX, or Linux.

How do I determine the editor that git is using?

If it was just an environment variable, we could do:

os.getenv('GIT_EDITOR')

      

But it can also be in the config.

Can git parse config files, but we don't want to override the whole search (repo, user, system?).

Question:

How can we programmatically detect which editor git is using?

+3


source to share


1 answer


Run git var GIT_EDITOR

. The result is the name of the editor used, suitable for passing to the shell:

import subprocess

def git_var(what):
    "return GIT_EDITOR or GIT_PAGER, for instance"
    proc = subprocess.Popen(['git', 'var', what], shell=False,
        stdout=subprocess.PIPE)
    output = proc.stdout.read()
    status = proc.wait()
    if status != 0:
        ... raise some error ...
    output = output.rstrip(b'\n')
    output = output.decode('utf8', errors='ignore') # or similar for py3k
    return output

      



(no matter how or how you want to generate the bytes is of course up to you).

+5


source







All Articles