【问题标题】:Git diff is complaining, "external diff died, stopping at ... " with my python diff programGit diff 用我的 python diff 程序抱怨,“外部 diff 死了,停在……”
【发布时间】:2013-06-13 01:41:25
【问题描述】:

这是我的差异的开始部分。

#!/usr/bin/env python
import fileinput
import difflib
import subprocess
import sys

# for debugging
def info(type, value, info):
    import traceback
    traceback.print_exception(type, value, info)
    print
    pdb.pm()

sys.excepthook = info
import pdb
#end debugging

if len(sys.argv) == 8:
    # assume this was passed to git; we can of course do
    # some parsing to check if we got valid git style args
    args = [sys.argv[2], sys.argv[5]]
elif len(sys.argv) == 3:
    args = sys.argv[1:]
else:
    exit("Not a valid number of args (2 or 7) to this diff program")
print "Files: " + ' '.join(args)
for filename in args:
    filetype = subprocess.check_output(['file', filename])
    if filetype.find('text') == -1:
        args.insert(0, 'diff')
        print "A binary file was found: " + filename + ", deferring to diff"
        exit(subprocess.call(args))

当遇到二进制(或其他非文本)文件时,它会尝试 fork diff 以获取二进制文件是否不同。目标是将此 python diff 程序用作 git 的外部差异。

但是一旦遇到二进制文件,我就会收到这个可怕的“external diff dead,stop at ”消息。

git 如何评估我的程序?它怎么知道自己死了?返回值不应该表示不同的条件吗?

【问题讨论】:

  • 我可能会放弃“外部差异”配置并将我的 python 差异设置为“差异工具”之一。希望这会更好。
  • 它有效,但我真的希望能够使用我的差异,例如git log -p

标签: python git subprocess return-value git-diff


【解决方案1】:

您的代码中没有退出函数。把exit换成sys.exit怎么样?

#!/usr/bin/env python

import subprocess
import sys

if len(sys.argv) == 8:
    # assume this was passed to git; we can of course do
    # some parsing to check if we got valid git style args
    args = [sys.argv[2], sys.argv[5]]
elif len(sys.argv) == 3:
    args = sys.argv[1:]
else:
    print "Not a valid number of args (2 or 7) to this diff program"
    sys.exit(1)
print "Files: ", args
for filename in args:
    filetype = subprocess.check_output(['file', filename])
    if filetype.find('text') == -1:
        args.insert(0, 'diff')
        print "A binary file was found: " + filename + ", deferring to diff"
        #sys.stdout.flush()
        subprocess.call(args)
        sys.exit(0)

编辑:git depend on external diff's exit status.diff 仅在没有差异的情况下以 0 退出。所以改代码不使用diff的退出状态。

PS:如果没有 sys.stdout.flush(),diff 输出在print 输出之前。

【讨论】:

  • exit(退出程序)和sys.exit(也退出程序)
  • 它们是一样的。但是您使用了未导入的出口。 (`from sys import exit')
  • 好吧。无论哪种方式,它都会做同样的事情。看来我得查一下git的源代码了。
  • 如果你不想用 sys.exit 替换 exit,你应该有 from sys import exit 语句。
  • 我应该使用哪一个?为什么有两个?
猜你喜欢
  • 1970-01-01
  • 2011-03-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-16
  • 1970-01-01
  • 2012-02-07
  • 2013-11-25
  • 2018-06-10
相关资源
最近更新 更多