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

git-gui: Always use absolute path to all git executables

Rather than making the C library search for git every time we want to execute it we now search for the main git wrapper at startup, do symlink resolution, and then always use the absolute path that we found to execute the binary later on. This should save us some cycles, especially on stat challenged systems like Cygwin/Win32. While I was working on this change I also converted all of our existing pipes ([open "| git ..."]) to use two new pipe wrapper functions. These functions take additional options like --nice and --stderr which instructs Tcl to take special action, like running the underlying git program through `nice` (if available) or redirect stderr to stdout for capture in Tcl. Signed-off-by: Shawn O. Pearce <spearce@spearce.org>

Shawn O. Pearce committed Jul 9, 2007 at 01:17 UTC 0b81261622afad691501ee51d7811048cf4a5fce
13 files changed +258 -84
git-gui.sh
+201 -16
@@ -117,6 +117,7 @@ set _gitdir {}
117 set _gitexec {}
118 set _reponame {}
119 set _iscygwin {}
120 +set _search_path {}
121
122 proc appname {} {
123 global _appname
@@ -128,7 +129,7 @@ proc gitdir {args} {
129 if {$args eq {}} {
130 return $_gitdir
131 }
131 - return [eval [concat [list file join $_gitdir] $args]]
132 + return [eval [list file join $_gitdir] $args]
133 }
134
135 proc gitexec {args} {
@@ -137,11 +138,19 @@ proc gitexec {args} {
138 if {[catch {set _gitexec [git --exec-path]} err]} {
139 error "Git not installed?\n\n$err"
140 }
141 + if {[is_Cygwin]} {
142 + set _gitexec [exec cygpath \
143 + --windows \
144 + --absolute \
145 + $_gitexec]
146 + } else {
147 + set _gitexec [file normalize $_gitexec]
148 + }
149 }
150 if {$args eq {}} {
151 return $_gitexec
152 }
144 - return [eval [concat [list file join $_gitexec] $args]]
153 + return [eval [list file join $_gitexec] $args]
154 }
155
156 proc reponame {} {
@@ -237,7 +246,7 @@ proc load_config {include_global} {
246 array unset global_config
247 if {$include_global} {
248 catch {
240 - set fd_rc [open "| git config --global --list" r]
249 + set fd_rc [git_read config --global --list]
250 while {[gets $fd_rc line] >= 0} {
251 if {[regexp {^([^=]+)=(.*)$} $line line name value]} {
252 if {[is_many_config $name]} {
@@ -253,7 +262,7 @@ proc load_config {include_global} {
262
263 array unset repo_config
264 catch {
256 - set fd_rc [open "| git config --list" r]
265 + set fd_rc [git_read config --list]
266 while {[gets $fd_rc line] >= 0} {
267 if {[regexp {^([^=]+)=(.*)$} $line line name value]} {
268 if {[is_many_config $name]} {
@@ -280,8 +289,172 @@ proc load_config {include_global} {
289 ##
290 ## handy utils
291
292 +proc _git_cmd {name} {
293 + global _git_cmd_path
294 +
295 + if {[catch {set v $_git_cmd_path($name)}]} {
296 + switch -- $name {
297 + --version -
298 + --exec-path { return [list $::_git $name] }
299 + }
300 +
301 + set p [gitexec git-$name$::_search_exe]
302 + if {[file exists $p]} {
303 + set v [list $p]
304 + } elseif {[is_Cygwin]} {
305 + # On Cygwin git is a proper Cygwin program and knows
306 + # how to properly restart the Cygwin environment and
307 + # spawn its non-.exe support program.
308 + #
309 + set v [list $::_git $name]
310 + } elseif {[is_Windows]
311 + && $::_sh ne {}
312 + && [file exists [gitexec git-$name]]} {
313 + # Assume this is a UNIX shell script. We can
314 + # probably execute it through a Bourne shell.
315 + #
316 + set v [list $::_sh [gitexec git-$name]]
317 + } else {
318 + error "No [gitexec git-$name]"
319 + }
320 + set _git_cmd_path($name) $v
321 + }
322 + return $v
323 +}
324 +
325 +proc _which {what} {
326 + global env _search_exe _search_path
327 +
328 + if {$_search_path eq {}} {
329 + if {[is_Cygwin]} {
330 + set _search_path [split [exec cygpath \
331 + --windows \
332 + --path \
333 + --absolute \
334 + $env(PATH)] {;}]
335 + set _search_exe .exe
336 + } elseif {[is_Windows]} {
337 + set _search_path [split $env(PATH) {;}]
338 + set _search_exe .exe
339 + } else {
340 + set _search_path [split $env(PATH) :]
341 + set _search_exe {}
342 + }
343 + }
344 +
345 + foreach p $_search_path {
346 + set p [file join $p $what$_search_exe]
347 + if {[file exists $p]} {
348 + return [file normalize $p]
349 + }
350 + }
351 + return {}
352 +}
353 +
354 proc git {args} {
284 - return [eval exec git $args]
355 + set opt [list exec]
356 +
357 + while {1} {
358 + switch -- [lindex $args 0] {
359 + --nice {
360 + global _nice
361 + if {$_nice ne {}} {
362 + lappend opt $_nice
363 + }
364 + }
365 +
366 + default {
367 + break
368 + }
369 +
370 + }
371 +
372 + set args [lrange $args 1 end]
373 + }
374 +
375 + set cmdp [_git_cmd [lindex $args 0]]
376 + set args [lrange $args 1 end]
377 +
378 + return [eval $opt $cmdp $args]
379 +}
380 +
381 +proc git_read {args} {
382 + set opt [list |]
383 +
384 + while {1} {
385 + switch -- [lindex $args 0] {
386 + --nice {
387 + global _nice
388 + if {$_nice ne {}} {
389 + lappend opt $_nice
390 + }
391 + }
392 +
393 + --stderr {
394 + lappend args 2>@1
395 + }
396 +
397 + default {
398 + break
399 + }
400 +
401 + }
402 +
403 + set args [lrange $args 1 end]
404 + }
405 +
406 + set cmdp [_git_cmd [lindex $args 0]]
407 + set args [lrange $args 1 end]
408 +
409 + if {[catch {
410 + set fd [open [concat $opt $cmdp $args] r]
411 + } err]} {
412 + if { [lindex $args end] eq {2>@1}
413 + && $err eq {can not find channel named "1"}
414 + } {
415 + # Older versions of Tcl 8.4 don't have this 2>@1 IO
416 + # redirect operator. Fallback to |& cat for those.
417 + # The command was not actually started, so its safe
418 + # to try to start it a second time.
419 + #
420 + set fd [open [concat \
421 + $opt \
422 + $cmdp \
423 + [lrange $args 0 end-1] \
424 + [list |& cat] \
425 + ] r]
426 + } else {
427 + error $err
428 + }
429 + }
430 + return $fd
431 +}
432 +
433 +proc git_write {args} {
434 + set opt [list |]
435 +
436 + while {1} {
437 + switch -- [lindex $args 0] {
438 + --nice {
439 + global _nice
440 + if {$_nice ne {}} {
441 + lappend opt $_nice
442 + }
443 + }
444 +
445 + default {
446 + break
447 + }
448 +
449 + }
450 +
451 + set args [lrange $args 1 end]
452 + }
453 +
454 + set cmdp [_git_cmd [lindex $args 0]]
455 + set args [lrange $args 1 end]
456 +
457 + return [open [concat $opt $cmdp $args] w]
458 }
459
460 proc load_current_branch {} {
@@ -318,6 +491,19 @@ proc tk_optionMenu {w varName args} {
491 return $m
492 }
493
494 +######################################################################
495 +##
496 +## find git
497 +
498 +set _git [_which git]
499 +if {$_git eq {}} {
500 + catch {wm withdraw .}
501 + error_popup "Cannot find git in PATH."
502 + exit 1
503 +}
504 +set _nice [_which nice]
505 +set _sh [_which sh]
506 +
507 ######################################################################
508 ##
509 ## version check
@@ -566,12 +752,12 @@ proc rescan {after {honor_trustmtime 1}} {
752 } else {
753 set rescan_active 1
754 ui_status {Refreshing file status...}
569 - set cmd [list git update-index]
570 - lappend cmd -q
571 - lappend cmd --unmerged
572 - lappend cmd --ignore-missing
573 - lappend cmd --refresh
574 - set fd_rf [open "| $cmd" r]
755 + set fd_rf [git_read update-index \
756 + -q \
757 + --unmerged \
758 + --ignore-missing \
759 + --refresh \
760 + ]
761 fconfigure $fd_rf -blocking 0 -translation binary
762 fileevent $fd_rf readable \
763 [list rescan_stage2 $fd_rf $after]
@@ -587,8 +773,7 @@ proc rescan_stage2 {fd after} {
773 close $fd
774 }
775
590 - set ls_others [list | git ls-files --others -z \
591 - --exclude-per-directory=.gitignore]
776 + set ls_others [list --exclude-per-directory=.gitignore]
777 set info_exclude [gitdir info exclude]
778 if {[file readable $info_exclude]} {
779 lappend ls_others "--exclude-from=$info_exclude"
@@ -600,9 +785,9 @@ proc rescan_stage2 {fd after} {
785
786 set rescan_active 3
787 ui_status {Scanning for modified files ...}
603 - set fd_di [open "| git diff-index --cached -z [PARENT]" r]
604 - set fd_df [open "| git diff-files -z" r]
605 - set fd_lo [open $ls_others r]
788 + set fd_di [git_read diff-index --cached -z [PARENT]]
789 + set fd_df [git_read diff-files -z]
790 + set fd_lo [eval git_read ls-files --others -z $ls_others]
791
792 fconfigure $fd_di -blocking 0 -translation binary -encoding binary
793 fconfigure $fd_df -blocking 0 -translation binary -encoding binary
lib/blame.tcl
+7 -14
@@ -371,8 +371,7 @@ method _load {jump} {
371 if {$commit eq {}} {
372 set fd [open $path r]
373 } else {
374 - set cmd [list git cat-file blob "$commit:$path"]
375 - set fd [open "| $cmd" r]
374 + set fd [git_read cat-file blob "$commit:$path"]
375 }
376 fconfigure $fd -blocking 0 -translation lf -encoding binary
377 fileevent $fd readable [cb _read_file $fd $jump]
@@ -475,20 +474,14 @@ method _read_file {fd jump} {
474 } ifdeleted { catch {close $fd} }
475
476 method _exec_blame {cur_w cur_d options cur_s} {
478 - set cmd [list]
479 - if {![is_Windows] || [is_Cygwin]} {
480 - lappend cmd nice
481 - }
482 - lappend cmd git blame
483 - set cmd [concat $cmd $options]
484 - lappend cmd --incremental
477 + lappend options --incremental
478 if {$commit eq {}} {
486 - lappend cmd --contents $path
479 + lappend options --contents $path
480 } else {
488 - lappend cmd $commit
481 + lappend options $commit
482 }
490 - lappend cmd -- $path
491 - set fd [open "| $cmd" r]
483 + lappend options -- $path
484 + set fd [eval git_read --nice blame $options]
485 fconfigure $fd -blocking 0 -translation lf -encoding binary
486 fileevent $fd readable [cb _read_blame $fd $cur_w $cur_d]
487 set current_fd $fd
@@ -767,7 +760,7 @@ method _showcommit {cur_w lno} {
760 if {[catch {set msg $header($cmit,message)}]} {
761 set msg {}
762 catch {
770 - set fd [open "| git cat-file commit $cmit" r]
763 + set fd [git_read cat-file commit $cmit]
764 fconfigure $fd -encoding binary -translation lf
765 if {[catch {set enc $repo_config(i18n.commitencoding)}]} {
766 set enc utf-8
lib/branch.tcl
+5 -2
@@ -7,7 +7,7 @@ proc load_all_heads {} {
7 set rh refs/heads
8 set rh_len [expr {[string length $rh] + 1}]
9 set all_heads [list]
10 - set fd [open "| git for-each-ref --format=%(refname) $rh" r]
10 + set fd [git_read for-each-ref --format=%(refname) $rh]
11 while {[gets $fd line] > 0} {
12 if {!$some_heads_tracking || ![is_tracking_branch $line]} {
13 lappend all_heads [string range $line $rh_len end]
@@ -20,7 +20,10 @@ proc load_all_heads {} {
20
21 proc load_all_tags {} {
22 set all_tags [list]
23 - set fd [open "| git for-each-ref --sort=-taggerdate --format=%(refname) refs/tags" r]
23 + set fd [git_read for-each-ref \
24 + --sort=-taggerdate \
25 + --format=%(refname) \
26 + refs/tags]
27 while {[gets $fd line] > 0} {
28 if {![regsub ^refs/tags/ $line {} name]} continue
29 lappend all_tags $name
lib/browser.tcl
+1 -2
@@ -178,8 +178,7 @@ method _ls {tree_id {name {}}} {
178 lappend browser_stack [list $tree_id $name]
179 $w conf -state disabled
180
181 - set cmd [list git ls-tree -z $tree_id]
182 - set fd [open "| $cmd" r]
181 + set fd [git_read ls-tree -z $tree_id]
182 fconfigure $fd -blocking 0 -translation binary -encoding binary
183 fileevent $fd readable [cb _read $fd]
184 }
lib/checkout_op.tcl
+15 -24
@@ -274,12 +274,12 @@ The rescan will be automatically started now.
274 _readtree $this
275 } else {
276 ui_status {Refreshing file status...}
277 - set cmd [list git update-index]
278 - lappend cmd -q
279 - lappend cmd --unmerged
280 - lappend cmd --ignore-missing
281 - lappend cmd --refresh
282 - set fd [open "| $cmd" r]
277 + set fd [git_read update-index \
278 + -q \
279 + --unmerged \
280 + --ignore-missing \
281 + --refresh \
282 + ]
283 fconfigure $fd -blocking 0 -translation binary
284 fileevent $fd readable [cb _refresh_wait $fd]
285 }
@@ -315,23 +315,14 @@ method _readtree {} {
315 "Updating working directory to '[_name $this]'..." \
316 {files checked out}
317
318 - set cmd [list git read-tree]
319 - lappend cmd -m
320 - lappend cmd -u
321 - lappend cmd -v
322 - lappend cmd --exclude-per-directory=.gitignore
323 - lappend cmd $HEAD
324 - lappend cmd $new_hash
325 -
326 - if {[catch {
327 - set fd [open "| $cmd 2>@1" r]
328 - } err]} {
329 - # Older versions of Tcl 8.4 don't have this 2>@1 IO
330 - # redirect operator. Fallback to |& cat for those.
331 - #
332 - set fd [open "| $cmd |& cat" r]
333 - }
334 -
318 + set fd [git_read --stderr read-tree \
319 + -m \
320 + -u \
321 + -v \
322 + --exclude-per-directory=.gitignore \
323 + $HEAD \
324 + $new_hash \
325 + ]
326 fconfigure $fd -blocking 0 -translation binary
327 fileevent $fd readable [cb _readtree_wait $fd]
328 }
@@ -524,7 +515,7 @@ method _confirm_reset {cur} {
515 pack $w.buttons.cancel -side right -padx 5
516 pack $w.buttons -side bottom -fill x -pady 10 -padx 10
517
527 - set fd [open "| git rev-list --pretty=oneline $cur ^$new_hash" r]
518 + set fd [git_read rev-list --pretty=oneline $cur ^$new_hash]
519 while {[gets $fd line] > 0} {
520 set abbr [string range $line 0 7]
521 set subj [string range $line 41 end]
lib/commit.tcl
+2 -2
@@ -25,7 +25,7 @@ You are currently in the middle of a merge that has not been fully completed. Y
25 set msg {}
26 set parents [list]
27 if {[catch {
28 - set fd [open "| git cat-file commit $curHEAD" r]
28 + set fd [git_read cat-file commit $curHEAD]
29 fconfigure $fd -encoding binary -translation lf
30 if {[catch {set enc $repo_config(i18n.commitencoding)}]} {
31 set enc utf-8
@@ -235,7 +235,7 @@ proc commit_prehook_wait {fd_ph curHEAD msg} {
235
236 proc commit_writetree {curHEAD msg} {
237 ui_status {Committing changes...}
238 - set fd_wt [open "| git write-tree" r]
238 + set fd_wt [git_read write-tree]
239 fileevent $fd_wt readable \
240 [list commit_committree $fd_wt $curHEAD $msg]
241 }
lib/database.tcl
+1 -1
@@ -2,7 +2,7 @@
2 # Copyright (C) 2006, 2007 Shawn Pearce
3
4 proc do_stats {} {
5 - set fd [open "| git count-objects -v" r]
5 + set fd [git_read count-objects -v]
6 while {[gets $fd line] > 0} {
7 if {[regexp {^([^:]+): (\d+)$} $line _ name value]} {
8 set stats($name) $value
lib/diff.tcl
+4 -4
@@ -131,7 +131,7 @@ proc show_diff {path w {lno {}}} {
131 return
132 }
133
134 - set cmd [list | git]
134 + set cmd [list]
135 if {$w eq $ui_index} {
136 lappend cmd diff-index
137 lappend cmd --cached
@@ -154,7 +154,7 @@ proc show_diff {path w {lno {}}} {
154 lappend cmd --
155 lappend cmd $path
156
157 - if {[catch {set fd [open $cmd r]} err]} {
157 + if {[catch {set fd [eval git_read --nice $cmd]} err]} {
158 set diff_active 0
159 unlock_index
160 ui_status "Unable to display [escape_path $path]"
@@ -271,7 +271,7 @@ proc apply_hunk {x y} {
271 if {$current_diff_path eq {} || $current_diff_header eq {}} return
272 if {![lock_index apply_hunk]} return
273
274 - set apply_cmd {git apply --cached --whitespace=nowarn}
274 + set apply_cmd {apply --cached --whitespace=nowarn}
275 set mi [lindex $file_states($current_diff_path) 0]
276 if {$current_diff_side eq $ui_index} {
277 set mode unstage
@@ -301,7 +301,7 @@ proc apply_hunk {x y} {
301 }
302
303 if {[catch {
304 - set p [open "| $apply_cmd" w]
304 + set p [eval git_write $apply_cmd]
305 fconfigure $p -translation binary -encoding binary
306 puts -nonewline $p $current_diff_header
307 puts -nonewline $p [$ui_diff get $s_lno $e_lno]
lib/index.tcl
+9 -9
@@ -17,7 +17,7 @@ proc update_indexinfo {msg pathList after} {
17 $update_index_cp \
18 $totalCnt \
19 0.0]
20 - set fd [open "| git update-index -z --index-info" w]
20 + set fd [git_write update-index -z --index-info]
21 fconfigure $fd \
22 -blocking 0 \
23 -buffering full \
@@ -90,7 +90,7 @@ proc update_index {msg pathList after} {
90 $update_index_cp \
91 $totalCnt \
92 0.0]
93 - set fd [open "| git update-index --add --remove -z --stdin" w]
93 + set fd [git_write update-index --add --remove -z --stdin]
94 fconfigure $fd \
95 -blocking 0 \
96 -buffering full \
@@ -167,13 +167,13 @@ proc checkout_index {msg pathList after} {
167 $update_index_cp \
168 $totalCnt \
169 0.0]
170 - set cmd [list git checkout-index]
171 - lappend cmd --index
172 - lappend cmd --quiet
173 - lappend cmd --force
174 - lappend cmd -z
175 - lappend cmd --stdin
176 - set fd [open "| $cmd " w]
170 + set fd [git_write checkout-index \
171 + --index \
172 + --quiet \
173 + --force \
174 + -z \
175 + --stdin \
176 + ]
177 fconfigure $fd \
178 -blocking 0 \
179 -buffering full \
lib/merge.tcl
+10 -8
@@ -146,7 +146,7 @@ The working directory will now be reset.
146
147 You can attempt this merge again by merging only one branch at a time." $w
148
149 - set fd [open "| git read-tree --reset -u HEAD" r]
149 + set fd [git_read read-tree --reset -u HEAD]
150 fconfigure $fd -blocking 0 -translation binary
151 fileevent $fd readable \
152 [namespace code [list _reset_wait $fd]]
@@ -167,11 +167,13 @@ proc dialog {} {
167 if {![_can_merge]} return
168
169 set fmt {list %(objectname) %(*objectname) %(refname) %(subject)}
170 - set cmd [list git for-each-ref --tcl --format=$fmt]
171 - lappend cmd refs/heads
172 - lappend cmd refs/remotes
173 - lappend cmd refs/tags
174 - set fr_fd [open "| $cmd" r]
170 + set fr_fd [git_read for-each-ref \
171 + --tcl \
172 + --format=$fmt \
173 + refs/heads \
174 + refs/remotes \
175 + refs/tags \
176 + ]
177 fconfigure $fr_fd -translation binary
178 while {[gets $fr_fd line] > 0} {
179 set line [eval $line]
@@ -186,7 +188,7 @@ proc dialog {} {
188 close $fr_fd
189
190 set to_show {}
189 - set fr_fd [open "| git rev-list --all --not HEAD"]
191 + set fr_fd [git_read rev-list --all --not HEAD]
192 while {[gets $fr_fd line] > 0} {
193 if {[catch {set ref $sha1($line)}]} continue
194 foreach n $ref {
@@ -282,7 +284,7 @@ You must finish amending this commit.
284 Aborting the current $op will cause *ALL* uncommitted changes to be lost.
285
286 Continue with aborting the current $op?"] eq {yes}} {
285 - set fd [open "| git read-tree --reset -u HEAD" r]
287 + set fd [git_read read-tree --reset -u HEAD]
288 fconfigure $fd -blocking 0 -translation binary
289 fileevent $fd readable [namespace code [list _reset_wait $fd]]
290 ui_status {Aborting... please wait...}
lib/option.tcl
+1
@@ -95,6 +95,7 @@ $copyright" \
95 }
96
97 set d {}
98 + append d "git wrapper: $::_git\n"
99 append d "git exec dir: [gitexec]\n"
100 append d "git-gui lib: $oguilib"
101
lib/remote.tcl
+1 -1
@@ -32,7 +32,7 @@ proc all_tracking_branches {} {
32 }
33
34 if {$pat ne {}} {
35 - set fd [open "| git for-each-ref --format=%(refname) $cmd" r]
35 + set fd [eval git_read for-each-ref --format=%(refname) $cmd]
36 while {[gets $fd n] > 0} {
37 foreach spec $pat {
38 set dst [string range [lindex $spec 0] 0 end-2]
lib/remote_branch_delete.tcl
+1 -1
@@ -296,7 +296,7 @@ method _load {cache uri} {
296 set full_list [list]
297 set head_cache($cache) [list]
298 set full_cache($cache) [list]
299 - set active_ls [open "| [list git ls-remote $uri]" r]
299 + set active_ls [git_read ls-remote $uri]
300 fconfigure $active_ls \
301 -blocking 0 \
302 -translation lf \