Skip to content
Browse files
Based on the discussion in msysgit issue #36, tweaking the fix a bit more to bring back the capability to peal tags on Windows without getting affected by git.cmd/git.exe mess.
  • Loading branch information
kohsuke committed Jun 26, 2012
1 parent 489fe80 commit 85afc7b0e64dd18c2807373880d1a8e2824fffbc
Showing 1 changed file with 27 additions and 7 deletions.
@@ -6,6 +6,7 @@
import hudson.Launcher;
import hudson.FilePath.FileCallable;
import hudson.Launcher.LocalLauncher;
import hudson.Util;
import hudson.model.TaskListener;
import hudson.remoting.VirtualChannel;
import hudson.util.ArgumentListBuilder;
@@ -263,16 +264,35 @@ public ObjectId revParse(String revName) throws GitException {
but if 'git' we are executing is git.cmd (which is the case of msysgit), then the arguments we pass in here ends up getting
processed by the command processor, and so 'xyz^{commit}' becomes 'xyz{commit}' and fails.
Since we can't really tell if we are calling into git.exe or git.cmd, the best we can do for Windows
is not to use '^{commit}'. This reverts 13f6038acc4fa5b5a62413155da6fc8cfcad3fe0
and it will not dereference tags, but it's far better than having this method completely broken.
We work around this problem by surrounding this with double-quote on Windows.
Unlike POSIX, where the arguments of a process is modeled as String[], Win32 API models the
arguments of a process as a single string (see CreateProcess). When we surround one argument with a quote,
java.lang.ProcessImpl on Windows preserve as-is and generate a single string like the following to pass to CreateProcess:
See JENKINS-13007 where this blew up on Windows users.
git rev-parse "tag^{commit}"
If we invoke git.exe, MSVCRT startup code in git.exe will handle escape and executes it as we expect.
If we invoke git.cmd, cmd.exe will not eats this ^ that's in double-quote. So it works on both cases.
Note that this is a borderline-buggy behaviour arguably. If I were implementing ProcessImpl for Windows
in JDK, My passing a string with double-quotes around it to be expanded to the following:
git rev-parse "\"tag^{commit}\""
I filed as a bug in msysgit.
So this work around that we are doing for Windows relies on the assumption that Java runtime will not
change this behaviour.
Also note that on Unix we cannot do this. Similarly, other ways of quoting (like using '^^' instead of '^'
that you do on interactive command prompt) do not work either, because MSVCRT startup won't handle
those in the same way cmd.exe does.
See JENKINS-13007 where this blew up on Windows users.
See where I filed this as a bug to msysgit.
String rpCommit = Functions.isWindows() ? "" : "^{commit}";
String result = launchCommand("rev-parse", revName + rpCommit);
String arg = revName + "^{commit}";
if (Functions.isWindows())
arg = '"'+arg+'"';
String result = launchCommand("rev-parse", arg);
return ObjectId.fromString(firstLine(result).trim());

0 comments on commit 85afc7b

Please sign in to comment.