Python sh模块和git尝试在命令中添加更多文件

时间:2014-11-14 15:49:10

标签: python git

我正在尝试使用sh python模块自动将文件添加到现有存储库。问题似乎是命令运行试图添加更多文件然后指定。

这是令人讨厌的命令:

sh.git("add", "-f" if args.add_force else '', '--', *to_add)

to_add是一个文件列表。下面你可以to_add确实有一个文件列表。

及其输出:

Adding modified files...Traceback (most recent call last):
  File "./update_member.py", line 182, in <module>
    sh.git("add", "-f" if args.add_force else '', '--', *to_add)
  File "/usr/lib/python2.7/site-packages/sh.py", line 769, in __call__
    return RunningCommand(cmd, call_args, stdin, stdout, stderr)
  File "/usr/lib/python2.7/site-packages/sh.py", line 330, in __init__
    self.wait()
  File "/usr/lib/python2.7/site-packages/sh.py", line 334, in wait
    self._handle_exit_code(self.process.wait())
  File "/usr/lib/python2.7/site-packages/sh.py", line 348, in _handle_exit_code
    self.process.stderr
sh.ErrorReturnCode_128:

  RAN: '/usr/bin/git add  -- CDD/Cdd_Diag.c CDD/Cdd_Diag.h CDD/Cdd_StatisticFunctions.h EcuSupervisor/Yes_Measurement.c EcuSupervisor/Yes_Os.c RTE/Dispatcher.c RTE/E2EM.c'

  STDOUT:


  STDERR:
fatal: LF would be replaced by CRLF in pylint_catch_to_ninja.html

我在python控制台中尝试了这个,我得到了相同的输出。

Python 2.7.8 (default, Jul 28 2014, 01:34:03)
[GCC 4.8.3] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sh
>>> sh.git('add', '', '--', 'CDD/Cdd_Diag.c')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/site-packages/sh.py", line 769, in __call__
    return RunningCommand(cmd, call_args, stdin, stdout, stderr)
  File "/usr/lib/python2.7/site-packages/sh.py", line 330, in __init__
    self.wait()
  File "/usr/lib/python2.7/site-packages/sh.py", line 334, in wait
    self._handle_exit_code(self.process.wait())
  File "/usr/lib/python2.7/site-packages/sh.py", line 348, in _handle_exit_code
    self.process.stderr
sh.ErrorReturnCode_128:

  RAN: '/usr/bin/git add  -- CDD/Cdd_Diag.c'

  STDOUT:


  STDERR:
fatal: LF would be replaced by CRLF in pylint_catch_to_ninja.html

当我将它粘贴到我的shell中时,RAN:下打印的命令运行正常。

任何人都知道会发生什么事情?

2 个答案:

答案 0 :(得分:1)

sh.git的每个参数都成为git本身的参数。当add_force为False时,"-f" if args.add_force else '',会添加一个混淆git的空参数。当您使用可选参数构建命令时,您可以强制它。

cmd = ["add"]
if args.add_force:
    cmd.append("-f")
cmd.extend(to_add)
sh.git(*cmd)

答案 1 :(得分:0)

实验表明&#34; git add&#39;&#34;&#34;&#34;与&#34; git add。&#34;具有相同的效果。这解释了效果&#34;关于CRLF的消息是针对我没有尝试添加的文件&#34; - Git认为你试图添加它。