Skip to main content
 首页 » 编程设计

python sh git.diff ('HEAD' ) block

2025年05月04日14zlslch

以下脚本 block :

from sh import git  
git.diff('HEAD') 

如果我中断我得到以下回溯:

^CTraceback (most recent call last): 
  File "main.py", line 2, in <module> 
    git.diff('HEAD') 
  File "/usr/local/lib/python2.7/dist-packages/sh.py", line 726, in __call__ 
    return RunningCommand(cmd, call_args, stdin, stdout, stderr) 
  File "/usr/local/lib/python2.7/dist-packages/sh.py", line 291, in __init__ 
    self.wait() 
  File "/usr/local/lib/python2.7/dist-packages/sh.py", line 295, in wait 
    self._handle_exit_code(self.process.wait()) 
  File "/usr/local/lib/python2.7/dist-packages/sh.py", line 1096, in wait 
    pid, exit_code = os.wait 

但是,git.branch() 运行良好。那么我需要做什么才能使用 python sh 获得 git diff 输出?

Python v.2.7 Git v.1.7.9.5

请您参考如下方法:

如果你的 git diff 输出超过一个屏幕(记不太清楚了,大约 48 行),git diff 会等待你按 q 退出或使用其他键继续 : 提示。

正如@phobic 在评论中指出的那样,可以使用 --no-pager git 选项来防止输出分页。

对于命令等待用户输入的一般情况,可以实现交互处理,类似于tutorial中的一个。 .