我的git的core.editor是崇高的,但是我在python中创建的自定义git命令中启动了sublime,所以如何在python中将git diff HEAD^ HEAD
作为参数传递给sublime
我已将core.editor的值存储在configdb ['core.editor']中,我可以使用subprocess.Popen
启动它,但是传递git diff HEAD^ HEAD
作为参数打开4个标签git,diff,HEAD ^,HEAD ......我应该如何使用git diff启动任何sublime,我可以添加自己的消息,我可以使用python存储在变量中。
# Read git config file
configFile, _ = execGitCommand('git config --list')
configDb = {}
for line in filter(None, configFile.split("\n")):
configDb[line.split("=")[0]] = line.split("=")[-1]
现在configDb['core.editor'] = /Applications/Sublime_Text.app/Contents/SharedSupport/bin/subl -n -w
然后
diff = subprocess.Popen(['git', 'diff', 'HEAD^', 'HEAD'], stdout=subprocess.PIPE)
msg, err, = subprocess.Popen(configDb['core.editor'].split(" ")[0], stdin=diff.stdout)
但是执行上面的最后一行确实会在sublime中打开diff但是会给出以下错误
Traceback (most recent call last):
File "/Users/san/Development/executables//git-ipush", line 186, in <module>
sys.exit(main())
File "/Users/san/Development/executables//git-ipush", line 43, in main
preCheck(args)
File "/Users/san/Development/executables//git-ipush", line 55, in preCheck
msg, err, = subprocess.Popen(configDb['core.editor'].split(" ")[0], stdin=diff.stdout)
TypeError: 'Popen' object is not utterable
并且终端现在没有等待sublime完成编辑,但它应该在我传递-w
标志时,如上所示。该代码是this git-command
答案 0 :(得分:0)
以下在Sublime Text 3的Python控制台中为我工作(在我已将当前工作目录设置为我的git工作副本之后):
>>> import subprocess
>>> diff = subprocess.Popen(['git', 'diff', 'HEAD^', 'HEAD'], stdout=subprocess.PIPE)
>>> subprocess.Popen(['/usr/local/bin/subl'], stdin=diff.stdout)
修改强> 好的,我想我明白你现在想要什么。你想在将它发送到Sublime之前编辑差异的文本,对吧?以下对我有用:
import subprocess, tempfile
diff = subprocess.Popen(['git', 'diff', 'HEAD^', 'HEAD'], stdout=subprocess.PIPE)
with tempfile.TemporaryFile() as f:
f.write('Hello, World\n')
f.write(diff.stdout.read())
f.seek(0)
subprocess.Popen(['/usr/local/bin/subl'], stdin=f)
这只是一个样本。如果您确实想要修改diff本身的内容,可以先将其读入字符串。请注意,您无法使用StringIO
(我认为会更好),因为在第二个Popen
某处有人需要fileno
。
编辑2: 以下是将Sublime中的文本转换为变量
的方法import sublime
window = sublime.active_window()
view = window.active_view()
region = sublime.Region(0, view.size())
text = view.substr(region)