git-gui: Add a --trace command line option
Often new Git users want to know what commands git-gui uses to make changes, so they can learn the command line interface by mimicking what git-gui does in response to GUI actions. Showing the direct commands being executed is easy enough to implement but this is of little value to end-users because git-gui frequently directly calls plumbing, not porcelain. Since the code is already written and tested, its fairly harmless to include. It may not help a new end-user, but it can help with debugging git-gui or reverse-engineering its logic to further make changes to it or implement another GUI for Git. Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
Shawn O. Pearce committed
May 18, 2008 at 13:08 UTC
16dd62ac4de10c9ae27d29bb1c906ad97ac8908f
1 file changed
+39
-8
git-gui.sh
+39
-8
@@ -122,6 +122,14 @@ set _reponame {}
122
set _iscygwin {}
123
set _search_path {}
124
125
+set _trace [lsearch -exact $argv --trace]
126
+if {$_trace >= 0} {
127
+ set argv [lreplace $argv $_trace $_trace]
128
+ set _trace 1
129
+} else {
130
+ set _trace 0
131
+}
132
+
133
proc appname {} {
134
global _appname
135
return $_appname
@@ -245,6 +253,21 @@ proc get_config {name} {
253
##
254
## handy utils
255
256
+proc _trace_exec {cmd} {
257
+ if {!$::_trace} return
258
+ set d {}
259
+ foreach v $cmd {
260
+ if {$d ne {}} {
261
+ append d { }
262
+ }
263
+ if {[regexp {[ \t\r\n'"$?*]} $v]} {
264
+ set v [sq $v]
265
+ }
266
+ append d $v
267
+ }
268
+ puts stderr $d
269
+}
270
+
271
proc _git_cmd {name} {
272
global _git_cmd_path
273
@@ -339,7 +362,7 @@ proc _lappend_nice {cmd_var} {
362
}
363
364
proc git {args} {
342
- set opt [list exec]
365
+ set opt [list]
366
367
while {1} {
368
switch -- [lindex $args 0] {
@@ -359,12 +382,18 @@ proc git {args} {
382
set cmdp [_git_cmd [lindex $args 0]]
383
set args [lrange $args 1 end]
384
362
- return [eval $opt $cmdp $args]
385
+ _trace_exec [concat $opt $cmdp $args]
386
+ set result [eval exec $opt $cmdp $args]
387
+ if {$::_trace} {
388
+ puts stderr "< $result"
389
+ }
390
+ return $result
391
}
392
393
proc _open_stdout_stderr {cmd} {
394
+ _trace_exec $cmd
395
if {[catch {
367
- set fd [open $cmd r]
396
+ set fd [open [concat [list | ] $cmd] r]
397
} err]} {
398
if { [lindex $cmd end] eq {2>@1}
399
&& $err eq {can not find channel named "1"}
@@ -375,6 +404,7 @@ proc _open_stdout_stderr {cmd} {
404
# to try to start it a second time.
405
#
406
set fd [open [concat \
407
+ [list | ] \
408
[lrange $cmd 0 end-1] \
409
[list |& cat] \
410
] r]
@@ -387,7 +417,7 @@ proc _open_stdout_stderr {cmd} {
417
}
418
419
proc git_read {args} {
390
- set opt [list |]
420
+ set opt [list]
421
422
while {1} {
423
switch -- [lindex $args 0] {
@@ -415,7 +445,7 @@ proc git_read {args} {
445
}
446
447
proc git_write {args} {
418
- set opt [list |]
448
+ set opt [list]
449
450
while {1} {
451
switch -- [lindex $args 0] {
@@ -435,7 +465,8 @@ proc git_write {args} {
465
set cmdp [_git_cmd [lindex $args 0]]
466
set args [lrange $args 1 end]
467
438
- return [open [concat $opt $cmdp $args] w]
468
+ _trace_exec [concat $opt $cmdp $args]
469
+ return [open [concat [list | ] $opt $cmdp $args] w]
470
}
471
472
proc githook_read {hook_name args} {
@@ -455,12 +486,12 @@ proc githook_read {hook_name args} {
486
}
487
488
set scr {if test -x "$1";then exec "$@";fi}
458
- set sh_c [list | $interp -c $scr $interp $pchook]
489
+ set sh_c [list $interp -c $scr $interp $pchook]
490
return [_open_stdout_stderr [concat $sh_c $args]]
491
}
492
493
if {[file executable $pchook]} {
463
- return [_open_stdout_stderr [concat [list | $pchook] $args]]
494
+ return [_open_stdout_stderr [concat [list $pchook] $args]]
495
}
496
497
return {}