【问题标题】:How do I programmatically discover the editor git uses, cross-platform?如何以编程方式发现 git 使用的跨平台编辑器?
【发布时间】:2017-05-25 01:04:48
【问题描述】:

假设我们在 Python 环境中,我们可以在 Windows、OSX 或 Linux 上。

我们如何确定git使用的编辑器?

如果只是环境变量,我们可以这样做:

os.getenv('GIT_EDITOR')

但它也可能在配置中。

可以解析 git 配置文件,但我们不想重新实现整个搜索(repo、用户、系统?)。

问题:

我们如何以编程方式发现 git 使用的编辑器?

【问题讨论】:

    标签: python git editor


    【解决方案1】:

    运行git var GIT_EDITOR。结果输出是要使用的编辑器的名称,适合传递给 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
    

    (当然,是否以及如何对字节进行字符串化取决于您)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-10
      • 1970-01-01
      • 2013-11-18
      • 2019-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多