git 是否返回特定的返回错误代码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4917871/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 10:02:53  来源:igfitidea点击:

Does git return specific return error codes?

git

提问by poymode

Like merging errors, or rebase errors. Does it have a unique error code?

像合并错误,或变基错误。它有唯一的错误代码吗?

采纳答案by chrisaycock

I set-up a test to fail. This is what I got:

我设置了一个失败的测试。这是我得到的:

$ git merge newbranch
Auto-merging test.txt
CONFLICT (content): Merge conflict in test.txt
Automatic merge failed; fix conflicts and then commit the result.

$ echo $?
1

Git returns 0when it merges correctly, as expected.

0正如预期的那样,Git在正确合并时返回。

回答by Cascabel

In short, no. You're going to see exit code 1 for errors, and 0 for success.

简而言之,没有。您将看到错误退出代码 1,成功退出代码 0。

From a quick grepping of the source, there are some of the expected 127 and 128 for their specific purposes (command not found, errors already reported), and a few unusual codes in a few places, but for run of the mill errors, it's all exit(1).

从快速搜索源代码来看,有一些预期的 127 和 128 用于其特定目的(未找到命令,已报告错误),以及一些地方的一些不寻常代码,但对于运行错误,它是所有exit(1)

回答by Gus Shortz

Running git statuson a non-git repo returns 128, not 1, which is helpful in quickly determining whether a git repo exists or not.

git status在非 git repo 上运行返回 128,而不是 1,这有助于快速确定 git repo 是否存在。

回答by Darren Cook

Error 128, with no error message from git, could be a catch-all for "unexpected problem".

错误 128,没有来自 git 的错误消息,可能是“意外问题”的全部。

I was getting this on operations that needed to modify files under .git (e.g. "git checkout -- myfile" to revert a modified file) by a different user. (In my case "chmod -R og+w .git" fixed it; naturally, don't do that unless you understand the security implications for your case!)

我在git checkout -- myfile其他用户需要修改 .git 下的文件(例如“ ”以恢复修改后的文件)的操作上得到了这个。(就我而言,“ chmod -R og+w .git”修复了它;当然,除非您了解对您的案例的安全影响,否则不要这样做!)

回答by manningr

git push --delete origin a_remote_tag_name

git push --delete origin a_remote_tag_name

This returns 256 if the tag doesn't exist using git version 1.8.3.1

如果使用 git version 标签不存在,则返回 256 1.8.3.1

It would be nice to have a consolidated list of specific return codes returned by each command and what they indicate. This might also help prevent changing the return code meanings (which automation scripts might rely on).

最好有每个命令返回的特定返回代码及其指示的综合列表。这也可能有助于防止更改返回代码的含义(自动化脚本可能依赖)。

回答by VonC

Git 2.24 (Q4 2019) does illustrate how gitcommands return code.

Git 2.24(2019 年第四季度)确实说明了git命令如何返回代码。

See commit 50094ca, commit c1a6f21, commit 854b5cb, commit dd2b6b6, commit 6bd26f5, commit c6ec6da, commit f2e2fa8, commit 460609c, commit 92014b6, commit 0ab74e9, commit cb46c40, commit b562a54(27 Aug 2019), and commit fe49814(20 Aug 2019) by Denton Liu (Denton-L).
(Merged by Junio C Hamano -- gitster--in commit 1c6fc94, 30 Sep 2019)

提交50094ca提交c1a6f21提交854b5cb提交dd2b6b6提交6bd26f5提交c6ec6da提交f2e2fa8提交460609c提交92014b6提交0ab74e9提交cb46c40提交b562a54(2019年8月27日),以及提交fe49814(2019年8月20日)由丹顿刘(Denton-L
(由Junio C gitsterHamano合并-- --commit 1c6fc94,2019 年 9 月 30 日)

t4014: stop losing return codes of git commands

Currently, there are two ways where the return codes of Git commands are lost.

The first way is when a command is in the upstream of a pipe. In a pipe, only the return code of the last command is used. Thus, all other commands will have their return codes masked.
Rewrite pipes so that there are no Git commands upstream.

The other way is when a command is in a non-assignment subshell.
The return code will be lost in favour of the surrounding command's.
Rewrite instances of this such that Git commands output to a file and surrounding commands only call subshells with non-Git commands.

t4014:停止丢失 git 命令的返回码

目前,Git 命令的返回码丢失有两种方式。

第一种方式是当命令位于管道的上游时。在管道中,只使用最后一条命令的返回码。因此,所有其他命令的返回码都将被屏蔽。
重写管道,以便上游没有 Git 命令。

另一种方式是当命令位于非赋值子shell 中时
返回代码将丢失,以支持周围的命令。
重写此实例,以便 Git 命令输出到文件,并且周围的命令仅使用非 Git 命令调用子shell。

So instead of writing:

所以而不是写:

git cat-file commit rebuild-1 | grep "^Side .* with .* backslash-n"

Type:

类型:

git cat-file commit rebuild-1 >actual &&
    grep "^Side .* with .* backslash-n" actual