python - "Fatal: too many params"从 python 调用 bash 命令时,直接在 git bash 中使用相同命令没有错误

标签 python bash git subprocess git-bash

扩展之前的工作函数,我遇到了 $(echo -e"...") 部分的问题,subprocess.call 返回“致命:参数太多”。

如果我复制打印的 bashCmd 并将其直接粘贴到 Git Bash 中,我最终会得到预期的结果(使用标题创建的新标签,以及标签“主体”的一些格式化表示;“新功能:.. .\n 错误修复:...\n"等。

打印的 bashCmd 字符串作为参数传递给 subprocess.call: git tag -a v1.4.9 -m "新标签描述"-m"$(echo -e "==新功能==\n没有新功能\n但功能1\n和功能2\n= =错误修复==\n修复了空格\n希望它能工作\n==已知问题==\n没有报告已知问题。\n")"

bashCmd = 'git tag -a v' + str(major) + '.' + str(minor) + '.' + str(bugfix) +' -m'+ ''' "''' + heading + '''" '''+'-m'+ '''"$(echo -e'''+ ''' "'''  +body+'''"''' ''')"'''

subprocess.call(bashCmd, shell=True)
print(bashCmd)

最佳答案

这里没有理由使用 shell。使用列表形式作为 call 的第一个参数。请注意,这将要求您修改 body,但这将 让它更简单

body = """\
==New Features==
still not working

==Bugfixes==
0 bugs fixed

==Known Issues==
infinite amounts of bugs left"""

commit_msg = "heading\n\n" + body
version_str = '.'.join(['v', str(major), str(minor), str(bugfix)]),

git_cmd = [
    'git', 
    'tag',
    '-a',
    version_str,
    '-m',
    commit_msg
]


subprocess.call(git_cmd)

关于python - "Fatal: too many params"从 python 调用 bash 命令时,直接在 git bash 中使用相同命令没有错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54723271/

相关文章:

python : Assigning image to a variable using loop statement

python - Django 表单小部件渲染

linux - bash 函数中的命令引用

mysql - mysql 查询下的 Bash 变量

git - `git rebase --skip` 在 rebase 过程中究竟跳过了什么?

git - 从 Git 历史记录中删除二进制文件后,为什么我的存储库仍然很大?

python - 如何永远运行脚本?

Python - 运行时间和内存使用方面的可扩展性很重要

bash - 如何用sed替换第一次出现?

git - 如何解决 Git 错误 : "fatal: BUG: get_tempfile_fd() called for inactive object"