python - Git checkout to a commit id 使用 python 子进程时将输出重定向到 stderr
问题描述
我subprocess
在 python 中使用模块来签出特定的提交。下面是示例代码。
commit_id = '305efe41dc9e9bfa375781fa2b69dd979dcaa7f1'
process = subprocess.Popen(["git", "checkout", commit_id], stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
out, err = process.communicate()
当我打印out
时没有输出,而当我打印时err
我看到以下消息 -
b"Note: switching to '305efe41dc9e9bfa375781fa2b69dd979dcaa7f1'.\n\nYou are in 'detached HEAD' state. You can look around, make experimental\nchanges and commit them, and you can discard any commits you make in this\nstate without impacting any branches by switching back to a branch.\n\nIf you want to create a new branch to retain commits you create, you may\ndo so (now or later) by using -c with the switch command. Example:\n\n git switch -c <new-branch-name>\n\nOr undo this operation with:\n\n git switch -\n\nTurn off this advice by setting config variable advice.detachedHead to false\n\nHEAD is now at 305efe41 checking out back to current branch after operation complete\n"
而当我执行 a 时,git branch
我看到我的 HEAD 已更改为305efe41dc9e9bfa375781fa2b69dd979dcaa7f1
.
如果我从这个当前分离的头进一步签出其他提交 ID,它仍然可以工作,但消息是在err
而不是out
像以前一样。
为什么我在 stderr 而不是 stdout 中收到消息?难道我做错了什么?
我的目标是编写一些干净的代码并避免可能的黑客攻击——
if not err:
// do something
INSTEAD OF
if err:
// do something as the checkout is working but for some reason the out is in err
注意:我知道有一些 git 库可以直接使用,而不是使用 subprocess 手动操作,但由于某种原因我不能使用它们。
解决方案
推荐阅读
- asynchronous - F# AsyncSeq - mke asyncSeq 如何按返回值的顺序迭代?
- python - 将动态查询集传递给 Django 模板
- php - 用于 print_r 的 FOR 循环中的 jQuery 和 PHP
- javascript - 如何在已经运行的 Electron 应用程序中通过“打开方式”打开文件?
- sql - 是否存在连接类型影响输出符号(相反符号)的任何情况
- chisel - FIRRTL 中的定点数表示
- jestjs - 如何用 jest 在 Nest js 中测试异步函数
- vb.net - 单击用户控件中的对象时不出现 ContextMenuStrip
- ios - Swift:从 JSON 中创建可解码模型,其中包含字典中的字典
- python-3.x - 气流:S3FileTransformOperator:Python 脚本在本地运行良好,但在 S3FileTransformOperator 中运行良好:尝试了不同的方法