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?
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With