@matheusbsbs / matheus-bagliano / commits / 098dd8a34b
Fork 0

gitk: Allow view to specify arbitrary arguments to git-rev-list

The list of arguments to git-rev-list, including arguments that select the range of commits, is now a part of the view specification. If any arguments are given to gitk, they become part of the "Command line" view, and the non-file arguments become the default for any new views created. Getting an error from git-rev-list is no longer fatal; instead the error window pops up, and when you press OK, the main window just shows "No commits selected". The git-rev-list arguments are entered in an entry widget in the view editor window using shell quoting conventions, not Tcl quoting conventions. Signed-off-by: Paul Mackerras <paulus@samba.org>

Paul Mackerras committed May 3, 2006 at 09:32 UTC 098dd8a34b43ea1317527ca1a3354aae9f3704b5
1 file changed +196 -51
gitk
+196 -51
@@ -19,13 +19,13 @@ proc gitdir {} {
19 proc start_rev_list {view} {
20 global startmsecs nextupdate ncmupdate
21 global commfd leftover tclencoding datemode
22 - global revtreeargs viewfiles commitidx
22 + global viewargs viewfiles commitidx
23
24 set startmsecs [clock clicks -milliseconds]
25 set nextupdate [expr {$startmsecs + 100}]
26 set ncmupdate 1
27 set commitidx($view) 0
28 - set args $revtreeargs
28 + set args $viewargs($view)
29 if {$viewfiles($view) ne {}} {
30 set args [concat $args "--" $viewfiles($view)]
31 }
@@ -69,9 +69,7 @@ proc getcommits {} {
69 set phase getcommits
70 initlayout
71 start_rev_list $curview
72 - $canv delete all
73 - $canv create text 3 3 -anchor nw -text "Reading commits..." \
74 - -font $mainfont -tags textitems
72 + show_status "Reading commits..."
73 }
74
75 proc getcommitlines {fd view} {
@@ -84,26 +82,33 @@ proc getcommitlines {fd view} {
82 set stuff [read $fd]
83 if {$stuff == {}} {
84 if {![eof $fd]} return
85 + global viewname
86 unset commfd($view)
87 + notbusy $view
88 # set it blocking so we wait for the process to terminate
89 fconfigure $fd -blocking 1
90 - if {![catch {close $fd} err]} {
91 - notbusy $view
92 - if {$view == $curview} {
93 - after idle finishcommits
90 + if {[catch {close $fd} err]} {
91 + set fv {}
92 + if {$view != $curview} {
93 + set fv " for the \"$viewname($view)\" view"
94 }
95 - return
95 + if {[string range $err 0 4] == "usage"} {
96 + set err "Gitk: error reading commits$fv:\
97 + bad arguments to git-rev-list."
98 + if {$viewname($view) eq "Command line"} {
99 + append err \
100 + " (Note: arguments to gitk are passed to git-rev-list\
101 + to allow selection of commits to be displayed.)"
102 + }
103 + } else {
104 + set err "Error reading commits$fv: $err"
105 + }
106 + error_popup $err
107 }
97 - if {[string range $err 0 4] == "usage"} {
98 - set err \
99 - "Gitk: error reading commits: bad arguments to git-rev-list.\
100 - (Note: arguments to gitk are passed to git-rev-list\
101 - to allow selection of commits to be displayed.)"
102 - } else {
103 - set err "Error reading commits: $err"
108 + if {$view == $curview} {
109 + after idle finishcommits
110 }
105 - error_popup $err
106 - exit 1
111 + return
112 }
113 set start 0
114 set gotsome 0
@@ -217,7 +222,7 @@ proc readcommit {id} {
222 }
223
224 proc updatecommits {} {
220 - global viewdata curview revtreeargs phase displayorder
225 + global viewdata curview phase displayorder
226 global children commitrow
227
228 if {$phase ne {}} {
@@ -352,10 +357,7 @@ proc readrefs {} {
357 close $refd
358 }
359
355 -proc error_popup msg {
356 - set w .error
357 - toplevel $w
358 - wm transient $w .
360 +proc show_error {w msg} {
361 message $w.m -text $msg -justify center -aspect 400
362 pack $w.m -side top -fill x -padx 20 -pady 20
363 button $w.ok -text OK -command "destroy $w"
@@ -365,6 +367,13 @@ proc error_popup msg {
367 tkwait window $w
368 }
369
370 +proc error_popup msg {
371 + set w .error
372 + toplevel $w
373 + wm transient $w .
374 + show_error $w $msg
375 +}
376 +
377 proc makewindow {} {
378 global canv canv2 canv3 linespc charspc ctext cflist
379 global textfont mainfont uifont
@@ -686,7 +695,7 @@ proc savestuff {w} {
695 global canv canv2 canv3 ctext cflist mainfont textfont uifont
696 global stuffsaved findmergefiles maxgraphpct
697 global maxwidth
689 - global viewname viewfiles viewperm nextviewnum
698 + global viewname viewfiles viewargs viewperm nextviewnum
699 global cmitmode
700
701 if {$stuffsaved} return
@@ -715,7 +724,7 @@ proc savestuff {w} {
724 puts -nonewline $f "set permviews {"
725 for {set v 0} {$v < $nextviewnum} {incr v} {
726 if {$viewperm($v)} {
718 - puts $f "{[list $viewname($v) $viewfiles($v)]}"
727 + puts $f "{[list $viewname($v) $viewfiles($v) $viewargs($v)]}"
728 }
729 }
730 puts $f "}"
@@ -1136,10 +1145,105 @@ proc sel_flist {w x y} {
1145 }
1146 }
1147
1148 +# Functions for adding and removing shell-type quoting
1149 +
1150 +proc shellquote {str} {
1151 + if {![string match "*\['\"\\ \t]*" $str]} {
1152 + return $str
1153 + }
1154 + if {![string match "*\['\"\\]*" $str]} {
1155 + return "\"$str\""
1156 + }
1157 + if {![string match "*'*" $str]} {
1158 + return "'$str'"
1159 + }
1160 + return "\"[string map {\" \\\" \\ \\\\} $str]\""
1161 +}
1162 +
1163 +proc shellarglist {l} {
1164 + set str {}
1165 + foreach a $l {
1166 + if {$str ne {}} {
1167 + append str " "
1168 + }
1169 + append str [shellquote $a]
1170 + }
1171 + return $str
1172 +}
1173 +
1174 +proc shelldequote {str} {
1175 + set ret {}
1176 + set used -1
1177 + while {1} {
1178 + incr used
1179 + if {![regexp -start $used -indices "\['\"\\\\ \t]" $str first]} {
1180 + append ret [string range $str $used end]
1181 + set used [string length $str]
1182 + break
1183 + }
1184 + set first [lindex $first 0]
1185 + set ch [string index $str $first]
1186 + if {$first > $used} {
1187 + append ret [string range $str $used [expr {$first - 1}]]
1188 + set used $first
1189 + }
1190 + if {$ch eq " " || $ch eq "\t"} break
1191 + incr used
1192 + if {$ch eq "'"} {
1193 + set first [string first "'" $str $used]
1194 + if {$first < 0} {
1195 + error "unmatched single-quote"
1196 + }
1197 + append ret [string range $str $used [expr {$first - 1}]]
1198 + set used $first
1199 + continue
1200 + }
1201 + if {$ch eq "\\"} {
1202 + if {$used >= [string length $str]} {
1203 + error "trailing backslash"
1204 + }
1205 + append ret [string index $str $used]
1206 + continue
1207 + }
1208 + # here ch == "\""
1209 + while {1} {
1210 + if {![regexp -start $used -indices "\[\"\\\\]" $str first]} {
1211 + error "unmatched double-quote"
1212 + }
1213 + set first [lindex $first 0]
1214 + set ch [string index $str $first]
1215 + if {$first > $used} {
1216 + append ret [string range $str $used [expr {$first - 1}]]
1217 + set used $first
1218 + }
1219 + if {$ch eq "\""} break
1220 + incr used
1221 + append ret [string index $str $used]
1222 + incr used
1223 + }
1224 + }
1225 + return [list $used $ret]
1226 +}
1227 +
1228 +proc shellsplit {str} {
1229 + set l {}
1230 + while {1} {
1231 + set str [string trimleft $str]
1232 + if {$str eq {}} break
1233 + set dq [shelldequote $str]
1234 + set n [lindex $dq 0]
1235 + set word [lindex $dq 1]
1236 + set str [string range $str $n end]
1237 + lappend l $word
1238 + }
1239 + return $l
1240 +}
1241 +
1242 # Code to implement multiple views
1243
1244 proc newview {ishighlight} {
1245 global nextviewnum newviewname newviewperm uifont newishighlight
1246 + global newviewargs revtreeargs
1247
1248 set newishighlight $ishighlight
1249 set top .gitkview
@@ -1149,12 +1253,14 @@ proc newview {ishighlight} {
1253 }
1254 set newviewname($nextviewnum) "View $nextviewnum"
1255 set newviewperm($nextviewnum) 0
1256 + set newviewargs($nextviewnum) [shellarglist $revtreeargs]
1257 vieweditor $top $nextviewnum "Gitk view definition"
1258 }
1259
1260 proc editview {} {
1261 global curview
1262 global viewname viewperm newviewname newviewperm
1263 + global viewargs newviewargs
1264
1265 set top .gitkvedit-$curview
1266 if {[winfo exists $top]} {
@@ -1163,6 +1269,7 @@ proc editview {} {
1269 }
1270 set newviewname($curview) $viewname($curview)
1271 set newviewperm($curview) $viewperm($curview)
1272 + set newviewargs($curview) [shellarglist $viewargs($curview)]
1273 vieweditor $top $curview "Gitk: edit view $viewname($curview)"
1274 }
1275
@@ -1177,7 +1284,13 @@ proc vieweditor {top n title} {
1284 grid $top.nl $top.name -sticky w -pady 5
1285 checkbutton $top.perm -text "Remember this view" -variable newviewperm($n)
1286 grid $top.perm - -pady 5 -sticky w
1180 - message $top.l -aspect 500 -font $uifont \
1287 + message $top.al -aspect 1000 -font $uifont \
1288 + -text "Commits to include (arguments to git-rev-list):"
1289 + grid $top.al - -sticky w -pady 5
1290 + entry $top.args -width 50 -textvariable newviewargs($n) \
1291 + -background white
1292 + grid $top.args - -sticky ew -padx 5
1293 + message $top.l -aspect 1000 -font $uifont \
1294 -text "Enter files and directories to include, one per line:"
1295 grid $top.l - -sticky w
1296 text $top.t -width 40 -height 10 -background white
@@ -1189,7 +1302,7 @@ proc vieweditor {top n title} {
1302 $top.t delete {end - 1c} end
1303 $top.t mark set insert 0.0
1304 }
1192 - grid $top.t - -sticky w -padx 5
1305 + grid $top.t - -sticky ew -padx 5
1306 frame $top.buts
1307 button $top.buts.ok -text "OK" -command [list newviewok $top $n]
1308 button $top.buts.can -text "Cancel" -command [list destroy $top]
@@ -1211,14 +1324,23 @@ proc doviewmenu {m first cmd op args} {
1324 }
1325
1326 proc allviewmenus {n op args} {
1214 - doviewmenu .bar.view 6 [list showview $n] $op $args
1327 + doviewmenu .bar.view 7 [list showview $n] $op $args
1328 doviewmenu .bar.view.hl 3 [list addhighlight $n] $op $args
1329 }
1330
1331 proc newviewok {top n} {
1332 global nextviewnum newviewperm newviewname newishighlight
1333 global viewname viewfiles viewperm selectedview curview
1334 + global viewargs newviewargs
1335
1336 + if {[catch {
1337 + set newargs [shellsplit $newviewargs($n)]
1338 + } err]} {
1339 + error_popup "Error in commit selection arguments: $err"
1340 + wm raise $top
1341 + focus $top
1342 + return
1343 + }
1344 set files {}
1345 foreach f [split [$top.t get 0.0 end] "\n"] {
1346 set ft [string trim $f]
@@ -1232,6 +1354,7 @@ proc newviewok {top n} {
1354 set viewname($n) $newviewname($n)
1355 set viewperm($n) $newviewperm($n)
1356 set viewfiles($n) $files
1357 + set viewargs($n) $newargs
1358 addviewmenu $n
1359 if {!$newishighlight} {
1360 after idle showview $n
@@ -1245,8 +1368,9 @@ proc newviewok {top n} {
1368 set viewname($n) $newviewname($n)
1369 allviewmenus $n entryconf -label $viewname($n)
1370 }
1248 - if {$files ne $viewfiles($n)} {
1371 + if {$files ne $viewfiles($n) || $newargs ne $viewargs($n)} {
1372 set viewfiles($n) $files
1373 + set viewargs($n) $newargs
1374 if {$curview == $n} {
1375 after idle updatecommits
1376 }
@@ -1347,8 +1471,8 @@ proc showview {n} {
1471 set curview $n
1472 set selectedview $n
1473 set selectedhlview -1
1350 - .bar.view entryconf 1 -state [expr {$n == 0? "disabled": "normal"}]
1474 .bar.view entryconf 2 -state [expr {$n == 0? "disabled": "normal"}]
1475 + .bar.view entryconf 3 -state [expr {$n == 0? "disabled": "normal"}]
1476 catch {unset hlview}
1477 .bar.view.hl entryconf 1 -state disabled
1478
@@ -1403,15 +1527,15 @@ proc showview {n} {
1527 selectline $row 0
1528 if {$phase ne {}} {
1529 if {$phase eq "getcommits"} {
1406 - global mainfont
1407 - $canv create text 3 3 -anchor nw -text "Reading commits..." \
1408 - -font $mainfont -tags textitems
1530 + show_status "Reading commits..."
1531 }
1532 if {[info exists commfd($n)]} {
1533 layoutmore
1534 } else {
1535 finishcommits
1536 }
1537 + } elseif {$numcommits == 0} {
1538 + show_status "No commits selected"
1539 }
1540 }
1541
@@ -2533,6 +2657,13 @@ proc xcoord {i level ln} {
2657 return $x
2658 }
2659
2660 +proc show_status {msg} {
2661 + global canv mainfont
2662 +
2663 + clear_display
2664 + $canv create text 3 3 -anchor nw -text $msg -font $mainfont -tags textitems
2665 +}
2666 +
2667 proc finishcommits {} {
2668 global commitidx phase curview
2669 global canv mainfont ctext maincursor textcursor
@@ -2541,9 +2672,7 @@ proc finishcommits {} {
2672 if {$commitidx($curview) > 0} {
2673 drawrest
2674 } else {
2544 - $canv delete all
2545 - $canv create text 3 3 -anchor nw -text "No commits selected" \
2546 - -font $mainfont -tags textitems
2675 + show_status "No commits selected"
2676 }
2677 set phase {}
2678 catch {unset pending_select}
@@ -4782,10 +4911,33 @@ foreach arg $argv {
4911 # check that we can find a .git directory somewhere...
4912 set gitdir [gitdir]
4913 if {![file isdirectory $gitdir]} {
4785 - error_popup "Cannot find the git directory \"$gitdir\"."
4914 + show_error . "Cannot find the git directory \"$gitdir\"."
4915 exit 1
4916 }
4917
4918 +set cmdline_files {}
4919 +set i [lsearch -exact $revtreeargs "--"]
4920 +if {$i >= 0} {
4921 + set cmdline_files [lrange $revtreeargs [expr {$i + 1}] end]
4922 + set revtreeargs [lrange $revtreeargs 0 [expr {$i - 1}]]
4923 +} elseif {$revtreeargs ne {}} {
4924 + if {[catch {
4925 + set f [eval exec git-rev-parse --no-revs --no-flags $revtreeargs]
4926 + set cmdline_files [split $f "\n"]
4927 + set n [llength $cmdline_files]
4928 + set revtreeargs [lrange $revtreeargs 0 end-$n]
4929 + } err]} {
4930 + # unfortunately we get both stdout and stderr in $err,
4931 + # so look for "fatal:".
4932 + set i [string first "fatal:" $err]
4933 + if {$i > 0} {
4934 + set err [string range [expr {$i + 6}] end]
4935 + }
4936 + show_error . "Bad arguments to gitk:\n$err"
4937 + exit 1
4938 + }
4939 +}
4940 +
4941 set history {}
4942 set historyindex 0
4943
@@ -4797,7 +4949,9 @@ set selectedview 0
4949 set selectedhlview {}
4950 set viewfiles(0) {}
4951 set viewperm(0) 0
4952 +set viewargs(0) {}
4953
4954 +set cmdlineok 0
4955 set stopped 0
4956 set stuffsaved 0
4957 set patchnum 0
@@ -4805,28 +4959,18 @@ setcoords
4959 makewindow
4960 readrefs
4961
4808 -set cmdline_files {}
4809 -catch {
4810 - set fileargs [eval exec git-rev-parse --no-revs --no-flags $revtreeargs]
4811 - set cmdline_files [split $fileargs "\n"]
4812 - set n [llength $cmdline_files]
4813 - set revtreeargs [lrange $revtreeargs 0 end-$n]
4814 -}
4815 -if {[lindex $revtreeargs end] eq "--"} {
4816 - set revtreeargs [lrange $revtreeargs 0 end-1]
4817 -}
4818 -
4819 -if {$cmdline_files ne {}} {
4962 +if {$cmdline_files ne {} || $revtreeargs ne {}} {
4963 # create a view for the files/dirs specified on the command line
4964 set curview 1
4965 set selectedview 1
4966 set nextviewnum 2
4967 set viewname(1) "Command line"
4968 set viewfiles(1) $cmdline_files
4969 + set viewargs(1) $revtreeargs
4970 set viewperm(1) 0
4971 addviewmenu 1
4828 - .bar.view entryconf 1 -state normal
4972 .bar.view entryconf 2 -state normal
4973 + .bar.view entryconf 3 -state normal
4974 }
4975
4976 if {[info exists permviews]} {
@@ -4835,6 +4979,7 @@ if {[info exists permviews]} {
4979 incr nextviewnum
4980 set viewname($n) [lindex $v 0]
4981 set viewfiles($n) [lindex $v 1]
4982 + set viewargs($n) [lindex $v 2]
4983 set viewperm($n) 1
4984 addviewmenu $n
4985 }