Patchwork [2,of,7,STABLE] hg.bat: return exit code explicitly for indirect invocation

login
register
mail settings
Submitter Katsunori FUJIWARA
Date Jan. 21, 2015, 3:10 p.m.
Message ID <5b976dff532a83b97b4b.1421853049@juju>
Download mbox | patch
Permalink /patch/7526/
State Accepted
Commit 975c4fc4a51295247fa46323b74cedb7dc989b24
Headers show

Comments

Katsunori FUJIWARA - Jan. 21, 2015, 3:10 p.m.
# HG changeset patch
# User FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
# Date 1421852826 -32400
#      Thu Jan 22 00:07:06 2015 +0900
# Branch stable
# Node ID 5b976dff532a83b97b4b76a3b95898b3638d040f
# Parent  0e281842c40a177bfbf7d062dadc04e2febc5aad
hg.bat: return exit code explicitly for indirect invocation

When "hg.bat" is invoked via interactive shell "cmd.exe" on Windows,
it can store own exit code into ERRORLEVEL correctly, regardless of
explicit "exit" statement in it: "cmd.exe" seems to hold ERRORLEVEL
updated by the last command in the batch file (= "python hg", in
"hg.bat" case).

On the other hand, "hg.bat" is invoked indirectly via
"subprocess.Popen" (e.g. shell alias, hooks, hgclient and so on), the
parent process always receives exit code 0 from spawned "hg.bat":
batch files on Windows seem not to be really spawned like as shell
scripts on UNIX, but to be executed in the "cmd.exe" process.

This patch returns exit code explicitly for indirect invocation.

"/b" should be specified for "exit" to prevent "cmd.exe" from being
terminated when "hg.bat" is invoked interactively from it.

Patch

diff --git a/contrib/win32/hg.bat b/contrib/win32/hg.bat
--- a/contrib/win32/hg.bat
+++ b/contrib/win32/hg.bat
@@ -15,3 +15,5 @@  if exist "%~dp0..\python.exe" (
     python "%~dp0hg" %*
 )
 endlocal
+
+exit /b %ERRORLEVEL%
diff --git a/tests/test-alias.t b/tests/test-alias.t
--- a/tests/test-alias.t
+++ b/tests/test-alias.t
@@ -525,3 +525,12 @@  return code of command and shell aliases
   [1]
   $ hg exit1
   [1]
+
+#if no-outer-repo
+  $ hg root
+  abort: no repository found in '$TESTTMP' (.hg not found)!
+  [255]
+  $ hg --config alias.hgroot='!hg root' hgroot
+  abort: no repository found in '$TESTTMP' (.hg not found)!
+  [255]
+#endif