阻止git将非错误写入stderr [英] Stop git from writing non-errors to stderr

查看:84
本文介绍了阻止git将非错误写入stderr的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本,用于自动同步各种远程git存储库.我要对脚本执行的一件事是捕获每个命令的stderr输出,并将所有这些错误写入文本文件,脚本完成后通过电子邮件将其发送给我.这将使我警觉到任何需要修复的问题.我在以下两行中遇到了问题:

I have a script that I am using to automatically sync various remote git repositories. One thing I am trying to do with my scripts is to capture the output of stderr from every command and write all those errors into a text file that is then emailed to me after the script has finished. This will alert me to any problems that I need to fix. I am having a problem with the following two lines though:

{
    git fetch --prune-tags github-fetch master
    git push github master 
} 2> '/tmp/stderr-contents-sync_git_repositories.txt'

问题是git fetch行将以下内容写入stderr:

The problem is that the git fetch line is writing the following to stderr:

From https://github.com/XJDHDR/xjdhdr-random-code.wiki
 * branch            master     -> FETCH_HEAD
   13af304..333d602  master     -> github/master

git pull行正在编写:

To ssh://github.com/XJDHDR/xjdhdr-random-code.wiki.git
   333d602..da65970  master -> master

我的问题是这些都不是错误,每次我运行脚本时都会通过电子邮件发送这些错误.我想知道是否可以阻止git将这些非错误信息写入stderr或从标准错误输出中过滤掉此类消息,同时保留真正的错误.

My problem is that neither of these are errors and they are emailed every time I run the script. I would like to know if it is possible to either stop git from writing these non-errors to stderr or filter these sort of messages out of the stderr output while preserving genuine errors.

推荐答案

将所有这些错误写入文本文件

write all those errors into a text file

这些不是不是总是错误,考虑到大多数Git命令在stderr上输出信息消息,如我在这里提到的:

Those are not always error, considering most Git commands outputs information message on stderr, as I mentioned here:

stderr,因为它只是信息性消息,不会被机器使用.

stderr as it is just informative messages, not to be consumed by machines.

如果最好测试该命令的退出状态并通过电子邮件将 stdout和stderr 发送给该用户, 0

If it better to test the exit status of the command and email both stdout and stderr if said exit status differs from 0

另外,您正在执行两个重定向:>后跟>:第二个重定向将 recreate /tmp/stderr-contents-sync_git_repositories.txt:第二个重定向应该是>>,而不是>.

Plus, you are doing two redirections: > followed by >: the second one would recreate /tmp/stderr-contents-sync_git_repositories.txt: that second redirection should be >>, not >.

所以:

git fetch --prune-tags github-fetch master > tmp 2>&1 || cat tmp > '/tmp/stderr-contents-sync_git_repositories.txt'
git push github master > tmp 2>&1 || cat tmp >> '/tmp/stderr-contents-sync_git_repositories.txt'

在这里,我在每个命令(带有其stdout/stderr)上覆盖一个tmp文件,如果该命令失败,我将写入或附加到/tmp/stderr-contents-sync_git_repositories.txt.

Here I override a tmp file on each command (with their stdout/stderr), and if that command fails, I write to, or I append to /tmp/stderr-contents-sync_git_repositories.txt.

这比您的编辑要容易得多,在编辑时,您可以将两个命令都重定向到一个文件,即使其中一个命令可能失败了.

This is easier than your edit, where you redirect both commands to a file, even if one of them might have failed.

这就是为什么我要执行cmd1 || cat >> file:>>部分仅在cmd1失败时运行.

That is why I do cmd1 || cat >> file: the >> part only runs if cmd1 fails.

这篇关于阻止git将非错误写入stderr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆