Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I programmatically discover the editor git uses, cross-platform?

Tags:

git

python

editor

Say we're within a Python environment, and we could be on Windows, OSX, or Linux.

How do we determine the editor that git uses?

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

os.getenv('GIT_EDITOR')

But it could also be in the config.

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

The question:

How can we programmatically discover the editor git uses?

like image 632
Russia Must Remove Putin Avatar asked Aug 19 '26 05:08

Russia Must Remove Putin


1 Answers

Run git var GIT_EDITOR. The resulting output is the name of the editor to use, suitable to pass to a 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

(whether and how you want to stringify bytes is up to you of course).

like image 151
torek Avatar answered Aug 20 '26 17:08

torek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!