如何在 PowerShell 中捕获到 git 命令中返回的全部信息?

2021-05-31 09:09:40 +08:00
 AndyAO

例如,git log 可能返回这样的信息

PS> git log

commit ed231a0a0923bd8b601e51e40b5249a56e78674a (HEAD -> master, tag: s2)
Author: fibo
Date:   Mon May 31 08:52:41 2021 +0800

    r2 mes

使用 PowerShell 捕获这个命令中产生的所有流,但中括号内的内容还是丢失掉了。

PS> Out-String -InputObject ((git log) *>&1)

commit 37aa5208e2d9b78d42bdc649c3c0fa0f2de6c1e0
Author: fibo
Date:   Mon May 31 08:56:53 2021 +0800

    r2 mes

在 PowerShell 中如何捕获这些信息?

1152 次点击
所在节点    问与答
15 条回复
oaix
2021-05-31 09:28:44 +08:00
git log --decorate=full
iovekkk
2021-05-31 09:33:16 +08:00
图形化界面不好吗?
用 sourcetree 好多年了
对 git flow 的支持很赞
oaix
2021-05-31 09:33:33 +08:00
https://git-scm.com/docs/git-log

--decorate[=short|full|auto|no]
Print out the ref names of any commits that are shown. If short is specified, the ref name prefixes refs/heads/, refs/tags/ and refs/remotes/ will not be printed. If full is specified, the full ref name (including prefix) will be printed. If auto is specified, then if the output is going to a terminal, the ref names are shown as if short were given, otherwise no ref names are shown. The default option is short.
AndyAO
2021-05-31 09:44:13 +08:00
@oaix #3
有个地方不太明白,默认的选项是 short,这个是没错的。

但是如果显式使用 git log --decorate=short 那么其中的信息是可以被 PowerShell 捕获的;反之,如果不显式使用,则信息可以被看到,但不能被捕获到变量当中去进行下一步的处理。

这是为什么呢?
oaix
2021-05-31 09:51:31 +08:00
@AndyAO #4 好问题,这里我也不知道原因,感觉是文档有问题,实际默认值是 auto 。
oaix
2021-05-31 09:57:52 +08:00
理解文档的意思了,默认值的意思是:--decorate 等效于--decorate=short
AndyAO
2021-05-31 10:12:08 +08:00
现在感觉之所以在 Shell 中获取不到信息,是因为文档上有写到

If auto is specified, then if the output is going to a terminal

那么,如果值为 auto 这些信息会直接输出到终端,而不是 Shell 当然这只是个猜测,我不确定。
slipper
2021-05-31 10:22:35 +08:00
git log --no-color -1 | awk '{print $0}' OK 嘛
slipper
2021-05-31 10:26:09 +08:00
还是你想要所有提交信息 git log --no-color | awk '{print $0}'
jeffreystoke
2021-05-31 12:55:36 +08:00
@AndyAO 设置为 auto 会让 git log 先判断 stdout 是不是 tty, 如果是 tty 才会输出 `(HEAD -> master, tag: s2)`
AndyAO
2021-06-07 10:52:49 +08:00
@oaix 你的最初判断是没有错的,的确是文档有问题,我向开发人员提交了 Pull Request,今天收到了邮件通知这个 Pull Request 已经被接受了。

https://cdn.jsdelivr.net/gh/Andy-AO/GitHubPictureBed/img/20210607105156.png

感觉我对这类著名开源项目的可靠性有了过高的估计,很多细节其实有不少的错误,从 Pull Request 列表中就可以看出来。
oaix
2021-06-07 11:19:32 +08:00
@AndyAO 👍
oaix
2021-06-07 11:35:26 +08:00
@AndyAO 看了下这个 PR 的讨论,如他们所说,这里面有 2 个默认值的概念,1:如果没有传 --decorate,那么默认值就是 auto ; 2:如果传了 --decorate,但没有指定 decorate 的值,那么默认值就是 short 。很明显,当前文档说的是情况 2,但其实情况 1 更符合直觉(如你的 PR 所修改的),如果能更进一步,在文档里面把这两个地方都说清楚可能更棒。
AndyAO
2021-06-07 14:13:34 +08:00
@oaix 原来是这样,其实后面的讨论没来得及看。

可能是因为第 1 次参与这种邮件列表讨论,需要注意的地方太多,花了很长时间才把信息给发出去,感觉很累,后来看到了相关的后续讨论,也就没有兴趣了。

如果不是你提醒的话,还不清楚「存在两个默认值」这个情况。谢谢。
AndyAO
2021-06-07 14:16:16 +08:00
感觉邮件列表上的讨论还是比较轻松的,对于这个不太起眼的 GitHub 的 Pull Request,维护者也发言了,后来我会用邮件列表了,在这里面发了个比较普通的问题,他也是第 1 个回复的,没想到他的时间还挺宽裕的,对于这些问题还来评论评论。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/780242

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX