gpt4 book ai didi

python - Git diff 提示, "external diff died, stopping at ... "与我的 python diff 程序

转载 作者:太空狗 更新时间:2023-10-29 14:14:47 31 4
gpt4 key购买 nike

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

#!/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 的外部差异。

但是一旦它到达二进制文件,我就会收到这个可怕的“外部差异已死,停止在 ”消息。

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

最佳答案

您的代码中没有退出函数。将 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 输出。

关于python - Git diff 提示, "external diff died, stopping at ... "与我的 python diff 程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17077983/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com