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

后端 未结 1 1561
太阳男子
太阳男子 2021-01-22 22:14

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 v

相关标签:
1条回答
  • 2021-01-22 22:38

    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).

    0 讨论(0)
提交回复
热议问题