[PATCH] Fix problems with the exit code

130 views
Skip to first unread message

Alexey Borzenkov

unread,
Sep 9, 2009, 6:09:15 AM9/9/09
to msy...@googlegroups.com, msy...@googlegroups.com, Alexey Borzenkov
From: Alexey Borzenkov <sna...@gmail.com>

---
If you call git.cmd from a batch file, then you receive the exit code and
can check it. However, if you call git.cmd from a perl/ruby script, the
exit code will always be zero.

This is happening because cmd.exe has an obscure bug and exit /b does not
set exit code if git.cmd was not called from a batch file (i.e. cmd /c git
from a perl/ruby script). exit %ErrorLevel% would work, but it would make
it impossible to easily call git from batch files. So the solution is to
run cmd.exe /c exit /b %ErrorLevel% as a last line in the batch file, this
must be setting something internal to cmd.exe and causes it to work in all
situations.

I'm attaching a patch for this.

cmd/git.cmd | 4 +++-
1 files changed, 3 insertions(+), 1 deletions(-)

diff --git a/cmd/git.cmd b/cmd/git.cmd
index c0cea12..ba992f3 100644
--- a/cmd/git.cmd
+++ b/cmd/git.cmd
@@ -6,7 +6,9 @@
@if "%1"=="gui" @goto gui
:default
@git.exe %*
-@exit /b %ErrorLevel%
+@goto end
:gui
@if "%2"=="citool" @goto default
@start wish.exe "%git_install_root%\libexec\git-core\git-gui" -- %2 %3 %4 %5 %6 %7 %8 %9
+:end
+@%COMSPEC% /c exit /b %ErrorLevel%
--
1.6.4.2.df.1

Alexey Borzenkov

unread,
Sep 9, 2009, 5:40:17 AM9/9/09
to msysGit
---
If you call git.cmd from a batch file, then you receive the exit code
and
can check it. However, if you call git.cmd from a perl/ruby script,
the
exit code will always be zero.

This is happening because cmd.exe has an obscure bug and exit /b does
not
set exit code if git.cmd was not called from a batch file (i.e. cmd /c
git
from a perl/ruby script). exit %ErrorLevel% would work, but it would
make
it impossible to easily call git from batch files. So the solution is
to
run cmd.exe /c exit /b %ErrorLevel% as a last line in the batch file,
this
must be setting something internal to cmd.exe and causes it to work in
all
situations.

Pat Thoyts

unread,
Sep 9, 2009, 7:04:29 AM9/9/09
to Alexey Borzenkov, msy...@googlegroups.com
2009/9/9 Alexey Borzenkov <sna...@gmail.com>:

>  If you call git.cmd from a batch file, then you receive the exit code and
>  can check it. However, if you call git.cmd from a perl/ruby script, the
>  exit code will always be zero.
>
>  This is happening because cmd.exe has an obscure bug and exit /b does not
>  set exit code if git.cmd was not called from a batch file (i.e. cmd /c git
>  from a perl/ruby script). exit %ErrorLevel% would work, but it would make
>  it impossible to easily call git from batch files. So the solution is to
>  run cmd.exe /c exit /b %ErrorLevel% as a last line in the batch file, this
>  must be setting something internal to cmd.exe and causes it to work in all
>  situations.

Do you have some documentation link for this 'obscure bug'?

In testing here I find that this is true on Windows XP but on Windows
7 the current 'exit /b' code works as expected. Your suggested fix
appears to work fine on Windows 7. The following Tcl program checks
both version of the exit code in a tiny cmd file using 'find' which
has a return code of 1 when it cannot find a match in a file:

set A "@find \"%1\" %0\n@exit /b %errorlevel%"
set B "@find \"%1\" %0\n@%comspec% /c exit /b %errorlevel%"
puts [set f [open x.cmd w]] $A; close $f
puts [list [catch {exec cmd.exe /c x.cmd hello}] $errorCode]
puts [set f [open x.cmd w]] $B; close $f
puts [list [catch {exec cmd.exe /c x.cmd hello}] $errorCode]

on windows XP:
C:\temp>tclsh x.tcl
0 NONE
1 {CHILDSTATUS 2840 1}

on windows7:
c:\Users\pat>tclsh x.tcl
1 {CHILDSTATUS 4000 1}
1 {CHILDSTATUS 2732 1}

So seems like a useful patch at least for XP.

Johannes Schindelin

unread,
Oct 26, 2009, 5:48:41 PM10/26/09
to Pat Thoyts, Alexey Borzenkov, msy...@googlegroups.com
Hi,

I agree that we want to have some official documentation linked from the
commit message, and I highly doubt that an empty commit message does a
good job at explaining why this might be a desirable change.

Ciao,
Dscho

Reply all
Reply to author
Forward
0 new messages