@matheusbsbs / matheus-bagliano / commits / c136f2b8b9
Fork 0

git-gui: Perform our own magic shbang detection on Windows

If we cannot locate a .exe for a git tool that we want to run than it may just be a Bourne shell script as these are popular in Git. In such a case the first line of the file will say "#!/bin/sh" so a UNIX kernel knows what program to start to parse and run that. But Windows doesn't support shbang lines, and neither does the Tcl that comes with Cygwin. We can pass control off to the git wrapper as that is a real Cygwin program and can therefore start the Bourne shell script, but that is at least two fork+exec calls to get the program running. One to do the fork+exec of the git wrapper and another to start the Bourne shell script. If the program is run multiple times it is rather expensive as the magic shbang detection won't be cached across executions. On MinGW/MSYS we don't have the luxury of such magic detection. The MSYS team has taught some of this magic to the git wrapper, but again its slower than it needs to be as the git wrapper must still go and run the Bourne shell after it is called. We now attempt to guess the shbang line on Windows by reading the first line of the file and building our own command line path from it. Currently we support Bourne shell (sh), Perl and Python. That is the entire set of shbang lines that appear in git.git today. Signed-off-by: Shawn O. Pearce <spearce@spearce.org>

Shawn O. Pearce committed Jul 9, 2007 at 02:47 UTC c136f2b8b9eaac7a702799de25648b74ef12618a
1 file changed +24 -13
git-gui.sh
+24 -13
@@ -302,19 +302,31 @@ proc _git_cmd {name} {
302 set p [gitexec git-$name$::_search_exe]
303 if {[file exists $p]} {
304 set v [list $p]
305 - } elseif {[is_Cygwin]} {
306 - # On Cygwin git is a proper Cygwin program and knows
307 - # how to properly restart the Cygwin environment and
308 - # spawn its non-.exe support program.
305 + } elseif {[is_Windows] && [file exists [gitexec git-$name]]} {
306 + # Try to determine what sort of magic will make
307 + # git-$name go and do its thing, because native
308 + # Tcl on Windows doesn't know it.
309 #
310 - set v [list $::_git $name]
311 - } elseif {[is_Windows]
312 - && $::_sh ne {}
313 - && [file exists [gitexec git-$name]]} {
314 - # Assume this is a UNIX shell script. We can
315 - # probably execute it through a Bourne shell.
316 - #
317 - set v [list $::_sh [gitexec git-$name]]
310 + set p [gitexec git-$name]
311 + set f [open $p r]
312 + set s [gets $f]
313 + close $f
314 +
315 + switch -glob -- $s {
316 + #!*sh { set i sh }
317 + #!*perl { set i perl }
318 + #!*python { set i python }
319 + default { error "git-$name is not supported: $s" }
320 + }
321 +
322 + upvar #0 _$i interp
323 + if {![info exists interp]} {
324 + set interp [_which $i]
325 + }
326 + if {$interp eq {}} {
327 + error "git-$name requires $i (not in PATH)"
328 + }
329 + set v [list $interp $p]
330 } else {
331 # Assume it is builtin to git somehow and we
332 # aren't actually able to see a file for it.
@@ -506,7 +518,6 @@ if {$_git eq {}} {
518 exit 1
519 }
520 set _nice [_which nice]
509 -set _sh [_which sh]
521
522 ######################################################################
523 ##