我如何以编程方式发现git使用的编辑器,跨平台? [英] How do I programmatically discover the editor git uses, cross-platform?
问题描述
我们如何确定git使用的编辑器?
如果我们确定git使用的编辑器, / p>
如果只是环境变量,我们可以这样做:
os.getenv('GIT_EDITOR')
但它也可以在配置中。
可以解析git配置文件,但我们不想重新实现整个搜索(repo,user,system?)。
<问题:
我们如何以编程方式发现git使用的编辑器?
运行 git var GIT_EDITOR
。生成的输出是要使用的编辑器名称,适合传递给shell:
导入子流程
def git_var(what):
return GIT_EDITOR or GIT_PAGER,例如
proc = subprocess.Popen(['git','var',what],shell = False,
stdout = subprocess.PIPE)
output = proc.stdout.read()
status = proc.wait()
if status!= 0:
... raise一些错误...
output = output.rstrip(b'\\\
')
output = output.decode('utf8',errors ='ignore')#py3k $或
返回输出
(当然,您是否想要将字节串化取决于您)
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).
这篇关于我如何以编程方式发现git使用的编辑器,跨平台?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!