无法从 git diff 获取文件的退出代码。

huangapple go评论106阅读模式
英文:

Cannot get exit code from git diff for files

问题

git diff is used to compare two files or changes within a Git repository. In your provided examples, you are using the --no-index flag to compare files outside of a Git repository. The exit code behavior you are observing is consistent with the documentation:

  • When there are no differences between the files, the exit code is 0, indicating success.
  • When there are differences between the files, the exit code is non-zero, typically 1, indicating differences exist.

This behavior is intended to align with the conventions of other Unix-like command-line utilities like diff.

In cmd.exe:

  • git diff between identical files (.\t1.txt and .\t1.txt) returns 0.
  • git diff between different files (.\t1.txt and .\t2.txt) returns a non-zero code (1).

In PowerShell:

  • $LASTEXITCODE is used to check the exit code after running a command.
  • $? is a special variable that returns $true if the last command was successful (exit code 0) and $false otherwise.

Your tests with testgit.bat and testgit.ps1 demonstrate that Git sets a non-zero exit code when there are differences, which is consistent with the documented behavior.

英文:

Is it possible to use git diff to compare two files outside of the repo? If so, why is the exit code not zero (0) when there is a difference between the files? What am I missing?

In cmd.exe:

 9:43:55.45 2023-08-04  C:\src\t\scripts>TYPE .\t1.txt
this is t1.txt

 9:43:57.92 2023-08-04  C:\src\t\scripts>TYPE .\t2.txt
this is t2.txt

 9:43:57.94 2023-08-04  C:\src\t\scripts>"C:\Program Files\Git\cmd\git.exe" diff --exit-code --no-index  --ignore-all-space  --shortstat  .\t1.txt .\t1.txt

 9:43:58.04 2023-08-04  C:\src\t\scripts>ECHO %ERRORLEVEL%
0

 9:43:58.04 2023-08-04  C:\src\t\scripts>"C:\Program Files\Git\cmd\git.exe" diff --exit-code --no-index  --ignore-all-space  --shortstat  .\t1.txt .\t2.txt
 1 file changed, 1 insertion(+), 1 deletion(-)

 9:43:58.14 2023-08-04  C:\src\t\scripts>ECHO %ERRORLEVEL%
0

In PowerShell 7.3.6:

PS C:\src\t\scripts> Get-Content -Path .\t1.txt
this is t1.txt
PS C:\src\t\scripts> Get-Content -Path  .\t2.txt
this is t2.txt
PS C:\src\t\scripts> & 'C:\Program Files\Git\cmd\git.exe' diff --exit-code --no-index  --ignore-all-space  --shortstat  .\t1.txt .\t1.txt
PS C:\src\t\scripts> $LASTEXITCODE
0
PS C:\src\t\scripts> & 'C:\Program Files\Git\cmd\git.exe' diff --exit-code --no-index  --ignore-all-space  --shortstat  .\t1.txt .\t2.txt
 1 file changed, 1 insertion(+), 1 deletion(-)
PS C:\src\t\scripts> $LASTEXITCODE
0
PS C:\src\t\scripts> & 'C:\Program Files\Git\cmd\git.exe' diff --exit-code --no-index  --ignore-all-space  --shortstat  .\t1.txt .\t1.txt
PS C:\src\t\scripts> $?
True
PS C:\src\t\scripts> & 'C:\Program Files\Git\cmd\git.exe' diff --exit-code --no-index  --ignore-all-space  --shortstat  .\t1.txt .\t2.txt
 1 file changed, 1 insertion(+), 1 deletion(-)
PS C:\src\t\scripts> $?
True

I am using:

PS C:\src\t\scripts> git --version
git version 2.39.2.windows.1

From the git-diff man page at https://git-scm.com/docs/git-diff

--exit-code
Make the program exit with codes similar to diff(1). That is,
it exits with 1 if there were differences and 0 means no differences.

Yes, git can set a non-zero exit code and returning in cmd.exe and pwsh.exe. Testing git setting the exit code to non-zero in cmd.exe.

 9:54:45.54 2023-08-04  C:\src\t\scripts>TYPE testgit.bat
@"C:\Program Files\Git\cmd\git.exe" >NUL
@ECHO %ERRORLEVEL%
 9:54:56.14 2023-08-04  C:\src\t\scripts>CALL testgit.bat
1

 9:54:58.82 2023-08-04  C:\src\t\scripts>testgit.bat
1

Testing git setting the exit code to non-zero in pwsh.exe.

PS C:\src\t\scripts> Get-Content -Path  .\testgit.ps1
& 'C:\Program Files\Git\cmd\git.exe' | Out-Null
$LASTEXITCODE
& 'C:\Program Files\Git\cmd\git.exe' | Out-Null
$?
PS C:\src\t\scripts> .\testgit.ps1
1
False

There is only one (1) git on the system.

PS C:\> Get-Command git -All

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Application     git.exe                                            2.39.2.1   C:\Program Files\Git\cmd\git.exe

答案1

得分: 4

这是一个只有在我使用 --ignore-all-space 与汇总统计信息 (--shortstat 以及类似选项) 时才出现的错误。为了解决此问题,请添加 --quiet,这样你将会得到返回码(但不会有其他输出)。如果你确实需要忽略所有空格的 shortstat 输出和返回码,那么你需要进行两次运行。

英文:

This is a bug that only shows up for me when combining --ignore-all-space and kin with summary stats (--shortstat and kin), as a workaround add --quiet and you'll get the return code (but no other output, if you really need the ignore-all-space shortstat output and the return code you'll need two runs).

huangapple
  • 本文由 发表于 2023年8月4日 22:04:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76836679.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定