Debian Codegraph @

contrib/hgk

raw
1 #!/usr/bin/env wish
2
3 # Copyright (C) 2005 Paul Mackerras. All rights reserved.
4 # This program is free software; it may be used, copied, modified
5 # and distributed under the terms of the GNU General Public Licence,
6 # either version 2, or (at your option) any later version.
7 #
8 # See hgk.py for extension usage and configuration.
9
10
11 # Modified version of Tip 171:
12 # http://www.tcl.tk/cgi-bin/tct/tip/171.html
13 #
14 # The in_mousewheel global was added to fix strange reentrancy issues.
15 # The whole snipped is activated only under windows, mouse wheel
16 # bindings working already under MacOSX and Linux.
17
18 if {[catch {package require Ttk}]} {
19 # use a shim
20 namespace eval ttk {
21 proc style args {}
22
23 proc entry args {
24 eval [linsert $args 0 ::entry] -relief flat
25 }
26 }
27
28 interp alias {} ttk::button {} button
29 interp alias {} ttk::frame {} frame
30 interp alias {} ttk::label {} label
31 interp alias {} ttk::scrollbar {} scrollbar
32 interp alias {} ttk::optionMenu {} tk_optionMenu
33
34 proc updatepalette {} {}
35 } else {
36 proc ::ttk::optionMenu {w varName firstValue args} {
37 upvar #0 $varName var
38
39 if {![info exists var]} {
40 set var $firstValue
41 }
42 ttk::menubutton $w -textvariable $varName -menu $w.menu \
43 -direction flush
44 menu $w.menu -tearoff 0
45 $w.menu add radiobutton -label $firstValue -variable $varName
46 foreach i $args {
47 $w.menu add radiobutton -label $i -variable $varName
48 }
49 return $w.menu
50 }
51 proc updatepalette {} {
52 catch {
53 tk_setPalette background [ttk::style lookup client -background]
54 }
55 }
56 }
57
58 if {[tk windowingsystem] eq "win32"} {
59
60 ttk::style theme use xpnative
61
62 set mw_classes [list Text Listbox Table TreeCtrl]
63 foreach class $mw_classes { bind $class <MouseWheel> {} }
64
65 set in_mousewheel 0
66
67 proc ::tk::MouseWheel {wFired X Y D {shifted 0}} {
68 global in_mousewheel
69 if { $in_mousewheel != 0 } { return }
70 # Set event to check based on call
71 set evt "<[expr {$shifted?{Shift-}:{}}]MouseWheel>"
72 # do not double-fire in case the class already has a binding
73 if {[bind [winfo class $wFired] $evt] ne ""} { return }
74 # obtain the window the mouse is over
75 set w [winfo containing $X $Y]
76 # if we are outside the app, try and scroll the focus widget
77 if {![winfo exists $w]} { catch {set w [focus]} }
78 if {[winfo exists $w]} {
79
80 if {[bind $w $evt] ne ""} {
81 # Awkward ... this widget has a MouseWheel binding, but to
82 # trigger successfully in it, we must give it focus.
83 catch {focus} old
84 if {$w ne $old} { focus $w }
85 set in_mousewheel 1
86 event generate $w $evt -rootx $X -rooty $Y -delta $D
87 set in_mousewheel 0
88 if {$w ne $old} { focus $old }
89 return
90 }
91
92 # aqua and x11/win32 have different delta handling
93 if {[tk windowingsystem] ne "aqua"} {
94 set delta [expr {- ($D / 30)}]
95 } else {
96 set delta [expr {- ($D)}]
97 }
98 # scrollbars have different call conventions
99 if {[string match "*Scrollbar" [winfo class $w]]} {
100 catch {tk::ScrollByUnits $w \
101 [string index [$w cget -orient] 0] $delta}
102 } else {
103 set cmd [list $w [expr {$shifted ? "xview" : "yview"}] \
104 scroll $delta units]
105 # Walking up to find the proper widget (handles cases like
106 # embedded widgets in a canvas)
107 while {[catch $cmd] && [winfo toplevel $w] ne $w} {
108 set w [winfo parent $w]
109 }
110 }
111 }
112 }
113
114 bind all <MouseWheel> [list ::tk::MouseWheel %W %X %Y %D 0]
115
116 # end of win32 section
117 } else {
118
119 if {[catch {
120 set theme [ttk::style theme use]
121 }]} {
122 set theme $::ttk::currentTheme
123 }
124 if {$theme eq "default"} {
125 ttk::style theme use clam
126 }
127
128 }
129
130 updatepalette
131
132 # Unify right mouse button handling.
133 # See "mouse buttons on macintosh" thread on comp.lang.tcl
134 if {[tk windowingsystem] eq "aqua"} {
135 event add <<B3>> <Control-ButtonPress-1>
136 event add <<B3>> <Button-2>
137 } else {
138 event add <<B3>> <Button-3>
139 }
140
141 proc gitdir {} {
142 global env
143 if {[info exists env(GIT_DIR)]} {
144 return $env(GIT_DIR)
145 } else {
146 return ".hg"
147 }
148 }
149
150 proc popupify {w} {
151 wm resizable $w 0 0
152 wm withdraw $w
153 update
154 set x [expr {([winfo screenwidth .]-[winfo reqwidth $w])/2}]
155 set y [expr {([winfo screenheight .]-[winfo reqheight $w])/2}]
156 wm geometry $w +$x+$y
157 wm transient $w .
158 wm deiconify $w
159 wm resizable $w 1 1
160 }
161
162 proc getcommits {rargs} {
163 global commits commfd phase canv mainfont env
164 global startmsecs nextupdate ncmupdate
165 global ctext maincursor textcursor leftover
166
167 # check that we can find a .git directory somewhere...
168 set gitdir [gitdir]
169 if {![file isdirectory $gitdir]} {
170 error_popup "Cannot find the git directory \"$gitdir\"."
171 exit 1
172 }
173 set commits {}
174 set phase getcommits
175 set startmsecs [clock clicks -milliseconds]
176 set nextupdate [expr $startmsecs + 100]
177 set ncmupdate 1
178 set limit 0
179 set revargs {}
180 set showhidden no
181 for {set i 0} {$i < [llength $rargs]} {incr i} {
182 set opt [lindex $rargs $i]
183 switch -- $opt --limit {
184 incr i
185 set limit [lindex $rargs $i]
186 } --hidden {
187 set showhidden yes
188 } default {
189 lappend revargs $opt
190 }
191 }
192 if [catch {
193 set parse_args [concat tip $revargs]
194 set parse_temp [eval exec {$env(HG)} --config ui.report_untrusted=false log --template '{node}\n' $parse_args]
195 regsub -all "\r\n" $parse_temp "\n" parse_temp
196 set parsed_args [split $parse_temp "\n"]
197 } err] {
198 # if git-rev-parse failed for some reason...
199 if {$rargs == {}} {
200 set revargs HEAD
201 }
202 set parsed_args $revargs
203 }
204 if {$limit > 0} {
205 set parsed_args [concat -n $limit $parsed_args]
206 }
207 if {$showhidden} {
208 append parsed_args --hidden
209 }
210 if [catch {
211 set commfd [open "|{$env(HG)} --config ui.report_untrusted=false debug-rev-list --header --topo-order --parents $parsed_args" r]
212 } err] {
213 puts stderr "Error executing hg debug-rev-list: $err"
214 exit 1
215 }
216 set leftover {}
217 fconfigure $commfd -blocking 0 -translation lf -eofchar {}
218 fileevent $commfd readable [list getcommitlines $commfd]
219 $canv delete all
220 $canv create text 3 3 -anchor nw -text "Reading commits..." \
221 -font $mainfont -tags textitems
222 . config -cursor watch
223 settextcursor watch
224 }
225
226 proc getcommitlines {commfd} {
227 global commits parents cdate children
228 global commitlisted phase commitinfo nextupdate
229 global stopped redisplaying leftover
230
231 set stuff [read $commfd]
232 if {$stuff == {}} {
233 if {![eof $commfd]} return
234 # set it blocking so we wait for the process to terminate
235 fconfigure $commfd -blocking 1
236 if {![catch {close $commfd} err]} {
237 after idle finishcommits
238 return
239 }
240 if {[string range $err 0 4] == "usage"} {
241 set err \
242 {Gitk: error reading commits: bad arguments to git-rev-list.
243 (Note: arguments to gitk are passed to git-rev-list
244 to allow selection of commits to be displayed.)}
245 } else {
246 set err "Error reading commits: $err"
247 }
248 error_popup $err
249 exit 1
250 }
251 set start 0
252 while 1 {
253 set i [string first "\0" $stuff $start]
254 if {$i < 0} {
255 append leftover [string range $stuff $start end]
256 return
257 }
258 set cmit [string range $stuff $start [expr {$i - 1}]]
259 if {$start == 0} {
260 set cmit "$leftover$cmit"
261 set leftover {}
262 }
263 set start [expr {$i + 1}]
264 regsub -all "\r\n" $cmit "\n" cmit
265 set j [string first "\n" $cmit]
266 set ok 0
267 if {$j >= 0} {
268 set ids [string range $cmit 0 [expr {$j - 1}]]
269 set ok 1
270 foreach id $ids {
271 if {![regexp {^[0-9a-f]{12}$} $id]} {
272 set ok 0
273 break
274 }
275 }
276 }
277 if {!$ok} {
278 set shortcmit $cmit
279 if {[string length $shortcmit] > 80} {
280 set shortcmit "[string range $shortcmit 0 80]..."
281 }
282 error_popup "Can't parse hg debug-rev-list output: {$shortcmit}"
283 exit 1
284 }
285 set id [lindex $ids 0]
286 set olds [lrange $ids 1 end]
287 set cmit [string range $cmit [expr {$j + 1}] end]
288 lappend commits $id
289 set commitlisted($id) 1
290 parsecommit $id $cmit 1 [lrange $ids 1 end]
291 drawcommit $id
292 if {[clock clicks -milliseconds] >= $nextupdate} {
293 doupdate 1
294 }
295 while {$redisplaying} {
296 set redisplaying 0
297 if {$stopped == 1} {
298 set stopped 0
299 set phase "getcommits"
300 foreach id $commits {
301 drawcommit $id
302 if {$stopped} break
303 if {[clock clicks -milliseconds] >= $nextupdate} {
304 doupdate 1
305 }
306 }
307 }
308 }
309 }
310 }
311
312 proc doupdate {reading} {
313 global commfd nextupdate numcommits ncmupdate
314
315 if {$reading} {
316 fileevent $commfd readable {}
317 }
318 update
319 set nextupdate [expr {[clock clicks -milliseconds] + 100}]
320 if {$numcommits < 100} {
321 set ncmupdate [expr {$numcommits + 1}]
322 } elseif {$numcommits < 10000} {
323 set ncmupdate [expr {$numcommits + 10}]
324 } else {
325 set ncmupdate [expr {$numcommits + 100}]
326 }
327 if {$reading} {
328 fileevent $commfd readable [list getcommitlines $commfd]
329 }
330 }
331
332 proc readcommit {id} {
333 global env
334 if [catch {set contents [exec $env(HG) --config ui.report_untrusted=false debug-cat-file commit $id]}] return
335 parsecommit $id $contents 0 {}
336 }
337
338 proc parsecommit {id contents listed olds} {
339 global commitinfo children nchildren parents nparents cdate ncleft
340 global firstparents obsolete
341
342 set inhdr 1
343 set comment {}
344 set headline {}
345 set auname {}
346 set audate {}
347 set comname {}
348 set comdate {}
349 set rev {}
350 set branch {}
351 set bookmark {}
352 if {![info exists nchildren($id)]} {
353 set children($id) {}
354 set nchildren($id) 0
355 set ncleft($id) 0
356 }
357 set parents($id) $olds
358 set nparents($id) [llength $olds]
359 foreach p $olds {
360 if {![info exists nchildren($p)]} {
361 set children($p) [list $id]
362 set nchildren($p) 1
363 set ncleft($p) 1
364 } elseif {[lsearch -exact $children($p) $id] < 0} {
365 lappend children($p) $id
366 incr nchildren($p)
367 incr ncleft($p)
368 }
369 }
370 regsub -all "\r\n" $contents "\n" contents
371 foreach line [split $contents "\n"] {
372 if {$inhdr} {
373 set line [split $line]
374 if {$line == {}} {
375 set inhdr 0
376 } else {
377 set tag [lindex $line 0]
378 switch -- $tag "author" {
379 set x [expr {[llength $line] - 2}]
380 set audate [lindex $line $x]
381 set auname [join [lrange $line 1 [expr {$x - 1}]]]
382 } "committer" {
383 set x [expr {[llength $line] - 2}]
384 set comdate [lindex $line $x]
385 set comname [join [lrange $line 1 [expr {$x - 1}]]]
386 } "revision" {
387 set rev [lindex $line 1]
388 } "branch" {
389 set branch [join [lrange $line 1 end]]
390 } "bookmark" {
391 set bookmark [join [lrange $line 1 end]]
392 } "obsolete" {
393 set obsolete($id) ""
394 } "phase" {
395 set phase [lindex $line 1 end]
396 }
397 }
398 } else {
399 if {$comment == {}} {
400 set headline [string trim $line]
401 } else {
402 append comment "\n"
403 }
404 if {!$listed} {
405 # git-rev-list indents the comment by 4 spaces;
406 # if we got this via git-cat-file, add the indentation
407 append comment " "
408 }
409 append comment $line
410 }
411 }
412 if {$audate != {}} {
413 set audate [clock format $audate]
414 }
415 if {$comdate != {}} {
416 set cdate($id) $comdate
417 set comdate [clock format $comdate]
418 }
419 set commitinfo($id) [list $headline $auname $audate \
420 $comname $comdate $comment $rev $branch $bookmark $phase]
421
422 if {[info exists firstparents]} {
423 set i [lsearch $firstparents $id]
424 if {$i != -1} {
425 # remove the parent from firstparents, possible building
426 # an empty list
427 set firstparents [concat \
428 [lrange $firstparents 0 [expr $i - 1]] \
429 [lrange $firstparents [expr $i + 1] end]]
430 if {$firstparents eq {}} {
431 # we have found all parents of the first changeset
432 # which means that we can safely select the first line
433 after idle {
434 selectline 0 0
435 }
436 }
437 }
438 } else {
439 # this is the first changeset, save the parents
440 set firstparents $olds
441 if {$firstparents eq {}} {
442 # a repository with a single changeset
443 after idle {
444 selectline 0 0
445 }
446 }
447 }
448 }
449
450 proc readrefs {} {
451 global bookmarkcurrent bookmarkids tagids idtags idbookmarks headids idheads tagcontents env curid
452
453 set status [catch {exec $env(HG) --config ui.report_untrusted=false id} curid]
454 if { $status != 0 } {
455 puts $::errorInfo
456 if { ![string equal $::errorCode NONE] } {
457 exit 2
458 }
459 }
460 regexp -- {[[:xdigit:]]+} $curid curid
461
462 set status [catch {exec $env(HG) --config ui.report_untrusted=false tags} tags]
463 if { $status != 0 } {
464 puts $::errorInfo
465 if { ![string equal $::errorCode NONE] } {
466 exit 2
467 }
468 }
469
470 foreach {- tag rev id} [regexp -inline -all -line {^(.+\S)\s+(\d+):(\S+)} $tags] {
471 # we use foreach as Tcl8.4 doesn't support lassign
472 lappend tagids($tag) $id
473 lappend idtags($id) $tag
474 }
475
476 set status [catch {exec $env(HG) --config ui.report_untrusted=false heads} heads]
477 if { $status != 0 } {
478 puts $::errorInfo
479 if { ![string equal $::errorCode NONE] } {
480 exit 2
481 }
482 }
483
484 set lines [split $heads \r\n]
485 foreach f $lines {
486 set match ""
487 regexp {changeset:\s+(\S+):(\S+)$} $f match id sha
488 if {$match != ""} {
489 lappend idheads($sha) $id
490 }
491 }
492
493 set status [catch {exec $env(HG) --config ui.report_untrusted=false bookmarks} bookmarks]
494 if { $status != 0 } {
495 puts $::errorInfo
496 if { ![string equal $::errorCode NONE] } {
497 exit 2
498 }
499 }
500 set lines [split $bookmarks "\n"]
501 set bookmarkcurrent 0
502 foreach f $lines {
503 regexp {(\S+)$} $f full
504 regsub {\s+(\S+)$} $f "" direct
505 set sha [split $full ':']
506 set bookmark [lindex $sha 1]
507 set current [string first " * " $direct)]
508 regsub {^\s(\*|\s)\s} $direct "" direct
509 lappend bookmarkids($direct) $bookmark
510 lappend idbookmarks($bookmark) $direct
511 if {$current >= 0} {
512 set bookmarkcurrent $direct
513 }
514 }
515 }
516
517 proc readotherrefs {base dname excl} {
518 global otherrefids idotherrefs
519
520 set git [gitdir]
521 set files [glob -nocomplain -types f [file join $git $base *]]
522 foreach f $files {
523 catch {
524 set fd [open $f r]
525 set line [read $fd 40]
526 if {[regexp {^[0-9a-f]{12}} $line id]} {
527 set name "$dname[file tail $f]"
528 set otherrefids($name) $id
529 lappend idotherrefs($id) $name
530 }
531 close $fd
532 }
533 }
534 set dirs [glob -nocomplain -types d [file join $git $base *]]
535 foreach d $dirs {
536 set dir [file tail $d]
537 if {[lsearch -exact $excl $dir] >= 0} continue
538 readotherrefs [file join $base $dir] "$dname$dir/" {}
539 }
540 }
541
542 proc allcansmousewheel {delta} {
543 set delta [expr -5*(int($delta)/abs($delta))]
544 allcanvs yview scroll $delta units
545 }
546
547 proc error_popup msg {
548 set w .error
549 toplevel $w
550 wm transient $w .
551 message $w.m -text $msg -justify center -aspect 400
552 pack $w.m -side top -fill x -padx 20 -pady 20
553 ttk::button $w.ok -text OK -command "destroy $w"
554 pack $w.ok -side bottom -fill x
555 bind $w <Visibility> "grab $w; focus $w"
556 popupify $w
557 tkwait window $w
558 }
559
560 proc makewindow {} {
561 global canv canv2 canv3 linespc charspc ctext cflist textfont
562 global findtype findtypemenu findloc findstring fstring geometry
563 global entries sha1entry sha1string sha1but
564 global maincursor textcursor curtextcursor
565 global rowctxmenu gaudydiff mergemax
566 global hgvdiff bgcolor fgcolor diffremcolor diffaddcolor diffmerge1color
567 global diffmerge2color hunksepcolor
568 global posx posy
569
570 if {[info exists posx]} {
571 wm geometry . +$posx+$posy
572 }
573
574 menu .bar
575 .bar add cascade -label "File" -menu .bar.file
576 menu .bar.file
577 .bar.file add command -label "Reread references" -command rereadrefs
578 .bar.file add command -label "Quit" -command doquit
579 menu .bar.help
580 .bar add cascade -label "Help" -menu .bar.help
581 .bar.help add command -label "About hgk" -command about
582 . configure -menu .bar
583
584 if {![info exists geometry(canv1)]} {
585 set geometry(canv1) [expr 45 * $charspc]
586 set geometry(canv2) [expr 30 * $charspc]
587 set geometry(canv3) [expr 15 * $charspc]
588 set geometry(canvh) [expr 25 * $linespc + 4]
589 set geometry(ctextw) 80
590 set geometry(ctexth) 30
591 set geometry(cflistw) 30
592 }
593 panedwindow .ctop -orient vertical
594 if {[info exists geometry(width)]} {
595 .ctop conf -width $geometry(width) -height $geometry(height)
596 set texth [expr {$geometry(height) - $geometry(canvh) - 56}]
597 set geometry(ctexth) [expr {($texth - 8) /
598 [font metrics $textfont -linespace]}]
599 }
600 ttk::frame .ctop.top
601 ttk::frame .ctop.top.bar
602 pack .ctop.top.bar -side bottom -fill x
603 set cscroll .ctop.top.csb
604 ttk::scrollbar $cscroll -command {allcanvs yview}
605 pack $cscroll -side right -fill y
606 panedwindow .ctop.top.clist -orient horizontal -sashpad 0 -handlesize 4
607 pack .ctop.top.clist -side top -fill both -expand 1
608 .ctop add .ctop.top
609 set canv .ctop.top.clist.canv
610 canvas $canv -height $geometry(canvh) -width $geometry(canv1) \
611 -bg $bgcolor -bd 0 \
612 -yscrollincr $linespc -yscrollcommand "$cscroll set" -selectbackground "#c0c0c0"
613 .ctop.top.clist add $canv
614 set canv2 .ctop.top.clist.canv2
615 canvas $canv2 -height $geometry(canvh) -width $geometry(canv2) \
616 -bg $bgcolor -bd 0 -yscrollincr $linespc -selectbackground "#c0c0c0"
617 .ctop.top.clist add $canv2
618 set canv3 .ctop.top.clist.canv3
619 canvas $canv3 -height $geometry(canvh) -width $geometry(canv3) \
620 -bg $bgcolor -bd 0 -yscrollincr $linespc -selectbackground "#c0c0c0"
621 .ctop.top.clist add $canv3
622 bind .ctop.top.clist <Configure> {resizeclistpanes %W %w}
623
624 set sha1entry .ctop.top.bar.sha1
625 set entries $sha1entry
626 set sha1but .ctop.top.bar.sha1label
627 button $sha1but -text "SHA1 ID: " -state disabled -relief flat \
628 -command gotocommit -width 8
629 $sha1but conf -disabledforeground [$sha1but cget -foreground]
630 pack .ctop.top.bar.sha1label -side left
631 ttk::entry $sha1entry -width 40 -font $textfont -textvariable sha1string
632 trace add variable sha1string write sha1change
633 pack $sha1entry -side left -pady 2
634
635 image create bitmap bm-left -data {
636 #define left_width 16
637 #define left_height 16
638 static unsigned char left_bits[] = {
639 0x00, 0x00, 0xc0, 0x01, 0xe0, 0x00, 0x70, 0x00, 0x38, 0x00, 0x1c, 0x00,
640 0x0e, 0x00, 0xff, 0x7f, 0xff, 0x7f, 0xff, 0x7f, 0x0e, 0x00, 0x1c, 0x00,
641 0x38, 0x00, 0x70, 0x00, 0xe0, 0x00, 0xc0, 0x01};
642 }
643 image create bitmap bm-right -data {
644 #define right_width 16
645 #define right_height 16
646 static unsigned char right_bits[] = {
647 0x00, 0x00, 0xc0, 0x01, 0x80, 0x03, 0x00, 0x07, 0x00, 0x0e, 0x00, 0x1c,
648 0x00, 0x38, 0xff, 0x7f, 0xff, 0x7f, 0xff, 0x7f, 0x00, 0x38, 0x00, 0x1c,
649 0x00, 0x0e, 0x00, 0x07, 0x80, 0x03, 0xc0, 0x01};
650 }
651 ttk::button .ctop.top.bar.leftbut -image bm-left -command goback \
652 -state disabled -width 26
653 pack .ctop.top.bar.leftbut -side left -fill y
654 ttk::button .ctop.top.bar.rightbut -image bm-right -command goforw \
655 -state disabled -width 26
656 pack .ctop.top.bar.rightbut -side left -fill y
657
658 ttk::button .ctop.top.bar.findbut -text "Find" -command dofind
659 pack .ctop.top.bar.findbut -side left
660 set findstring {}
661 set fstring .ctop.top.bar.findstring
662 lappend entries $fstring
663 ttk::entry $fstring -width 30 -font $textfont -textvariable findstring
664 pack $fstring -side left -expand 1 -fill x
665 set findtype Exact
666 set findtypemenu [ttk::optionMenu .ctop.top.bar.findtype \
667 findtype Exact IgnCase Regexp]
668 set findloc "All fields"
669 ttk::optionMenu .ctop.top.bar.findloc findloc "All fields" Headline \
670 Comments Author Files Pickaxe
671 pack .ctop.top.bar.findloc -side right
672 pack .ctop.top.bar.findtype -side right
673 # for making sure type==Exact whenever loc==Pickaxe
674 trace add variable findloc write findlocchange
675
676 panedwindow .ctop.cdet -orient horizontal
677 .ctop add .ctop.cdet
678 ttk::frame .ctop.cdet.left
679 set ctext .ctop.cdet.left.ctext
680 text $ctext -fg $fgcolor -bg $bgcolor -state disabled -font $textfont \
681 -width $geometry(ctextw) -height $geometry(ctexth) \
682 -yscrollcommand ".ctop.cdet.left.sb set" \
683 -xscrollcommand ".ctop.cdet.left.hb set" -wrap none
684 ttk::scrollbar .ctop.cdet.left.sb -command "$ctext yview"
685 ttk::scrollbar .ctop.cdet.left.hb -orient horizontal -command "$ctext xview"
686 pack .ctop.cdet.left.sb -side right -fill y
687 pack .ctop.cdet.left.hb -side bottom -fill x
688 pack $ctext -side left -fill both -expand 1
689 .ctop.cdet add .ctop.cdet.left
690
691 $ctext tag conf filesep -font [concat $textfont bold] -back "#aaaaaa"
692 if {$gaudydiff} {
693 $ctext tag conf hunksep -back blue -fore white
694 $ctext tag conf d0 -back "#ff8080"
695 $ctext tag conf d1 -back green
696 } else {
697 $ctext tag conf hunksep -fore $hunksepcolor
698 $ctext tag conf d0 -fore $diffremcolor
699 $ctext tag conf d1 -fore $diffaddcolor
700
701 # The mX colours seem to be used in merge changesets, where m0
702 # is first parent, m1 is second parent and so on. Git can have
703 # several parents, Hg cannot, so I think the m2..mmax would be
704 # unused.
705 $ctext tag conf m0 -fore $diffmerge1color
706 $ctext tag conf m1 -fore $diffmerge2color
707 $ctext tag conf m2 -fore green
708 $ctext tag conf m3 -fore purple
709 $ctext tag conf m4 -fore brown
710 $ctext tag conf mmax -fore darkgrey
711 set mergemax 5
712 $ctext tag conf mresult -font [concat $textfont bold]
713 $ctext tag conf msep -font [concat $textfont bold]
714 $ctext tag conf found -back yellow
715 }
716
717 ttk::frame .ctop.cdet.right
718 set cflist .ctop.cdet.right.cfiles
719 listbox $cflist -fg $fgcolor -bg $bgcolor \
720 -selectmode extended -width $geometry(cflistw) \
721 -yscrollcommand ".ctop.cdet.right.sb set"
722 ttk::scrollbar .ctop.cdet.right.sb -command "$cflist yview"
723 pack .ctop.cdet.right.sb -side right -fill y
724 pack $cflist -side left -fill both -expand 1
725 .ctop.cdet add .ctop.cdet.right
726 bind .ctop.cdet <Configure> {resizecdetpanes %W %w}
727
728 pack .ctop -side top -fill both -expand 1
729
730 bindall <1> {selcanvline %W %x %y}
731 #bindall <B1-Motion> {selcanvline %W %x %y}
732 bindall <MouseWheel> "allcansmousewheel %D"
733 bindall <ButtonRelease-4> "allcanvs yview scroll -5 units"
734 bindall <ButtonRelease-5> "allcanvs yview scroll 5 units"
735 bindall <2> "allcanvs scan mark 0 %y"
736 bindall <B2-Motion> "allcanvs scan dragto 0 %y"
737 bind . <Key-Up> "selnextline -1"
738 bind . <Key-Down> "selnextline 1"
739 bind . <Key-Prior> "allcanvs yview scroll -1 pages"
740 bind . <Key-Next> "allcanvs yview scroll 1 pages"
741 bindkey <Key-Delete> "$ctext yview scroll -1 pages"
742 bindkey <Key-BackSpace> "$ctext yview scroll -1 pages"
743 bindkey <Key-space> "$ctext yview scroll 1 pages"
744 bindkey p "selnextline -1"
745 bindkey n "selnextline 1"
746 bindkey b "$ctext yview scroll -1 pages"
747 bindkey d "$ctext yview scroll 18 units"
748 bindkey u "$ctext yview scroll -18 units"
749 bindkey / {findnext 1}
750 bindkey <Key-Return> {findnext 0}
751 bindkey ? findprev
752 bindkey f nextfile
753 bind . <Control-q> doquit
754 bind . <Control-w> doquit
755 bind . <Control-f> dofind
756 bind . <Control-g> {findnext 0}
757 bind . <Control-r> findprev
758 bind . <Control-equal> {incrfont 1}
759 bind . <Control-KP_Add> {incrfont 1}
760 bind . <Control-minus> {incrfont -1}
761 bind . <Control-KP_Subtract> {incrfont -1}
762 bind $cflist <<ListboxSelect>> listboxsel
763 bind . <Destroy> {savestuff %W}
764 bind . <Button-1> "click %W"
765 bind $fstring <Key-Return> dofind
766 bind $sha1entry <Key-Return> gotocommit
767 bind $sha1entry <<PasteSelection>> clearsha1
768
769 set maincursor [. cget -cursor]
770 set textcursor [$ctext cget -cursor]
771 set curtextcursor $textcursor
772
773 set rowctxmenu .rowctxmenu
774 menu $rowctxmenu -tearoff 0
775 $rowctxmenu add command -label "Diff this -> selected" \
776 -command {diffvssel 0}
777 $rowctxmenu add command -label "Diff selected -> this" \
778 -command {diffvssel 1}
779 $rowctxmenu add command -label "Make patch" -command mkpatch
780 $rowctxmenu add command -label "Create tag" -command mktag
781 $rowctxmenu add command -label "Write commit to file" -command writecommit
782 if { $hgvdiff ne "" } {
783 $rowctxmenu add command -label "Visual diff with parent" \
784 -command {vdiff 1}
785 $rowctxmenu add command -label "Visual diff with selected" \
786 -command {vdiff 0}
787 }
788 }
789
790 # when we make a key binding for the toplevel, make sure
791 # it doesn't get triggered when that key is pressed in the
792 # find string entry widget.
793 proc bindkey {ev script} {
794 global entries
795 bind . $ev $script
796 set escript [bind Entry $ev]
797 if {$escript == {}} {
798 set escript [bind Entry <Key>]
799 }
800 foreach e $entries {
801 bind $e $ev "$escript; break"
802 }
803 }
804
805 # set the focus back to the toplevel for any click outside
806 # the entry widgets
807 proc click {w} {
808 global ctext entries
809 foreach e [concat $entries $ctext] {
810 if {$w == $e} return
811 }
812 focus .
813 }
814
815 proc savestuff {w} {
816 global canv canv2 canv3 ctext cflist mainfont textfont
817 global stuffsaved findmergefiles gaudydiff maxgraphpct
818 global maxwidth authorcolors curidfont bgcolor fgcolor
819 global diffremcolor diffaddcolor hunksepcolor
820 global diffmerge1color diffmerge2color
821
822 if {$stuffsaved} return
823 if {![winfo viewable .]} return
824 catch {
825 set f [open "~/.hgk-new" w]
826 puts $f [list set mainfont $mainfont]
827 puts $f [list set curidfont $curidfont]
828 puts $f [list set textfont $textfont]
829 puts $f [list set findmergefiles $findmergefiles]
830 puts $f [list set gaudydiff $gaudydiff]
831 puts $f [list set maxgraphpct $maxgraphpct]
832 puts $f [list set maxwidth $maxwidth]
833 puts $f "set geometry(width) [winfo width .ctop]"
834 puts $f "set geometry(height) [winfo height .ctop]"
835 puts $f "set geometry(canv1) [expr [winfo width $canv]-2]"
836 puts $f "set geometry(canv2) [expr [winfo width $canv2]-2]"
837 puts $f "set geometry(canv3) [expr [winfo width $canv3]-2]"
838 puts $f "set geometry(canvh) [expr [winfo height $canv]-2]"
839 set wid [expr {([winfo width $ctext] - 8) \
840 / [font measure $textfont "0"]}]
841 puts $f "set geometry(ctextw) $wid"
842 set wid [expr {([winfo width $cflist] - 11) \
843 / [font measure [$cflist cget -font] "0"]}]
844 puts $f "set geometry(cflistw) $wid"
845 puts $f "#"
846 puts $f "# main window position:"
847 puts $f "set posx [winfo x .]"
848 puts $f "set posy [winfo y .]"
849 puts $f "#"
850 puts $f "# authorcolors format:"
851 puts $f "#"
852 puts $f "# zero or more sublists of"
853 puts $f "#"
854 puts $f "# { regex color }"
855 puts $f "#"
856 puts $f "# followed by a list of colors"
857 puts $f "#"
858 puts $f "# If the commit author matches a regex in a sublist,"
859 puts $f "# the commit will be colored by that color"
860 puts $f "# otherwise the next unused entry from the list of colors"
861 puts $f "# will be assigned to this commit and also all other commits"
862 puts $f "# of the same author. When the list of colors is exhausted,"
863 puts $f "# the last entry will be reused."
864 puts $f "#"
865 puts $f "set authorcolors {$authorcolors}"
866 puts $f "#"
867 puts $f "# The background color in the text windows"
868 puts $f "set bgcolor $bgcolor"
869 puts $f "#"
870 puts $f "# The text color used in the diff and file list view"
871 puts $f "set fgcolor $fgcolor"
872 puts $f "#"
873 puts $f "# Color to display + lines in diffs"
874 puts $f "set diffaddcolor $diffaddcolor"
875 puts $f "#"
876 puts $f "# Color to display - lines in diffs"
877 puts $f "set diffremcolor $diffremcolor"
878 puts $f "#"
879 puts $f "# Merge diffs: Color to signal lines from first parent"
880 puts $f "set diffmerge1color $diffmerge1color"
881 puts $f "#"
882 puts $f "# Merge diffs: Color to signal lines from second parent"
883 puts $f "set diffmerge2color $diffmerge2color"
884 puts $f "#"
885 puts $f "# Hunkseparator (@@ -lineno,lines +lineno,lines @@) color"
886 puts $f "set hunksepcolor $hunksepcolor"
887 close $f
888 file rename -force "~/.hgk-new" "~/.hgk"
889 }
890 set stuffsaved 1
891 }
892
893 proc resizeclistpanes {win w} {
894 global oldwidth
895 if [info exists oldwidth($win)] {
896 set s0 [$win sash coord 0]
897 set s1 [$win sash coord 1]
898 if {$w < 60} {
899 set sash0 [expr {int($w/2 - 2)}]
900 set sash1 [expr {int($w*5/6 - 2)}]
901 } else {
902 set factor [expr {1.0 * $w / $oldwidth($win)}]
903 set sash0 [expr {int($factor * [lindex $s0 0])}]
904 set sash1 [expr {int($factor * [lindex $s1 0])}]
905 if {$sash0 < 30} {
906 set sash0 30
907 }
908 if {$sash1 < $sash0 + 20} {
909 set sash1 [expr $sash0 + 20]
910 }
911 if {$sash1 > $w - 10} {
912 set sash1 [expr $w - 10]
913 if {$sash0 > $sash1 - 20} {
914 set sash0 [expr $sash1 - 20]
915 }
916 }
917 }
918 $win sash place 0 $sash0 [lindex $s0 1]
919 $win sash place 1 $sash1 [lindex $s1 1]
920 }
921 set oldwidth($win) $w
922 }
923
924 proc resizecdetpanes {win w} {
925 global oldwidth
926 if [info exists oldwidth($win)] {
927 set s0 [$win sash coord 0]
928 if {$w < 60} {
929 set sash0 [expr {int($w*3/4 - 2)}]
930 } else {
931 set factor [expr {1.0 * $w / $oldwidth($win)}]
932 set sash0 [expr {int($factor * [lindex $s0 0])}]
933 if {$sash0 < 45} {
934 set sash0 45
935 }
936 if {$sash0 > $w - 15} {
937 set sash0 [expr $w - 15]
938 }
939 }
940 $win sash place 0 $sash0 [lindex $s0 1]
941 }
942 set oldwidth($win) $w
943 }
944
945 proc allcanvs args {
946 global canv canv2 canv3
947 eval $canv $args
948 eval $canv2 $args
949 eval $canv3 $args
950 }
951
952 proc bindall {event action} {
953 global canv canv2 canv3
954 bind $canv $event $action
955 bind $canv2 $event $action
956 bind $canv3 $event $action
957 }
958
959 proc about {} {
960 set w .about
961 if {[winfo exists $w]} {
962 raise $w
963 return
964 }
965 toplevel $w
966 wm title $w "About hgk"
967 message $w.m -text {
968 Hgk version 1.2
969
970 Copyright (C) 2005 Paul Mackerras
971
972 Use and redistribute under the terms of the GNU General Public License} \
973 -justify center -aspect 400
974 pack $w.m -side top -fill x -padx 20 -pady 20
975 ttk::button $w.ok -text Close -command "destroy $w"
976 pack $w.ok -side bottom
977 popupify $w
978 }
979
980 set aunextcolor 0
981 proc assignauthorcolor {name} {
982 global authorcolors aucolormap aunextcolor
983 if [info exists aucolormap($name)] return
984
985 set randomcolors {black}
986 for {set i 0} {$i < [llength $authorcolors]} {incr i} {
987 set col [lindex $authorcolors $i]
988 if {[llength $col] > 1} {
989 set re [lindex $col 0]
990 set c [lindex $col 1]
991 if {[regexp -- $re $name]} {
992 set aucolormap($name) $c
993 return
994 }
995 } else {
996 set randomcolors [lrange $authorcolors $i end]
997 break
998 }
999 }
1000
1001 set ncolors [llength $randomcolors]
1002 set c [lindex $randomcolors $aunextcolor]
1003 if {[incr aunextcolor] >= $ncolors} {
1004 incr aunextcolor -1
1005 }
1006 set aucolormap($name) $c
1007 }
1008
1009 proc assigncolor {id} {
1010 global commitinfo colormap commcolors colors nextcolor
1011 global parents nparents children nchildren
1012 global cornercrossings crossings
1013
1014 if [info exists colormap($id)] return
1015 set ncolors [llength $colors]
1016 if {$nparents($id) <= 1 && $nchildren($id) == 1} {
1017 set child [lindex $children($id) 0]
1018 if {[info exists colormap($child)]
1019 && $nparents($child) == 1} {
1020 set colormap($id) $colormap($child)
1021 return
1022 }
1023 }
1024 set badcolors {}
1025 if {[info exists cornercrossings($id)]} {
1026 foreach x $cornercrossings($id) {
1027 if {[info exists colormap($x)]
1028 && [lsearch -exact $badcolors $colormap($x)] < 0} {
1029 lappend badcolors $colormap($x)
1030 }
1031 }
1032 if {[llength $badcolors] >= $ncolors} {
1033 set badcolors {}
1034 }
1035 }
1036 set origbad $badcolors
1037 if {[llength $badcolors] < $ncolors - 1} {
1038 if {[info exists crossings($id)]} {
1039 foreach x $crossings($id) {
1040 if {[info exists colormap($x)]
1041 && [lsearch -exact $badcolors $colormap($x)] < 0} {
1042 lappend badcolors $colormap($x)
1043 }
1044 }
1045 if {[llength $badcolors] >= $ncolors} {
1046 set badcolors $origbad
1047 }
1048 }
1049 set origbad $badcolors
1050 }
1051 if {[llength $badcolors] < $ncolors - 1} {
1052 foreach child $children($id) {
1053 if {[info exists colormap($child)]
1054 && [lsearch -exact $badcolors $colormap($child)] < 0} {
1055 lappend badcolors $colormap($child)
1056 }
1057 if {[info exists parents($child)]} {
1058 foreach p $parents($child) {
1059 if {[info exists colormap($p)]
1060 && [lsearch -exact $badcolors $colormap($p)] < 0} {
1061 lappend badcolors $colormap($p)
1062 }
1063 }
1064 }
1065 }
1066 if {[llength $badcolors] >= $ncolors} {
1067 set badcolors $origbad
1068 }
1069 }
1070 for {set i 0} {$i <= $ncolors} {incr i} {
1071 set c [lindex $colors $nextcolor]
1072 if {[incr nextcolor] >= $ncolors} {
1073 set nextcolor 0
1074 }
1075 if {[lsearch -exact $badcolors $c]} break
1076 }
1077 set colormap($id) $c
1078 }
1079
1080 proc initgraph {} {
1081 global canvy canvy0 lineno numcommits nextcolor linespc
1082 global mainline mainlinearrow sidelines
1083 global nchildren ncleft
1084 global displist nhyperspace
1085
1086 allcanvs delete all
1087 set nextcolor 0
1088 set canvy $canvy0
1089 set lineno -1
1090 set numcommits 0
1091 catch {unset mainline}
1092 catch {unset mainlinearrow}
1093 catch {unset sidelines}
1094 foreach id [array names nchildren] {
1095 set ncleft($id) $nchildren($id)
1096 }
1097 set displist {}
1098 set nhyperspace 0
1099 }
1100
1101 proc bindline {t id} {
1102 global canv
1103
1104 $canv bind $t <Enter> "lineenter %x %y $id"
1105 $canv bind $t <Motion> "linemotion %x %y $id"
1106 $canv bind $t <Leave> "lineleave $id"
1107 $canv bind $t <Button-1> "lineclick %x %y $id 1"
1108 }
1109
1110 proc drawlines {id xtra} {
1111 global mainline mainlinearrow sidelines lthickness colormap canv
1112
1113 $canv delete lines.$id
1114 if {[info exists mainline($id)]} {
1115 set t [$canv create line $mainline($id) \
1116 -width [expr {($xtra + 1) * $lthickness}] \
1117 -fill $colormap($id) -tags lines.$id \
1118 -arrow $mainlinearrow($id)]
1119 $canv lower $t
1120 bindline $t $id
1121 }
1122 if {[info exists sidelines($id)]} {
1123 foreach ls $sidelines($id) {
1124 set coords [lindex $ls 0]
1125 set thick [lindex $ls 1]
1126 set arrow [lindex $ls 2]
1127 set t [$canv create line $coords -fill $colormap($id) \
1128 -width [expr {($thick + $xtra) * $lthickness}] \
1129 -arrow $arrow -tags lines.$id]
1130 $canv lower $t
1131 bindline $t $id
1132 }
1133 }
1134 }
1135
1136 # level here is an index in displist
1137 proc drawcommitline {level} {
1138 global parents children nparents displist
1139 global canv canv2 canv3 mainfont namefont canvy linespc
1140 global lineid linehtag linentag linedtag commitinfo
1141 global colormap numcommits currentparents dupparents
1142 global idtags idline idheads idotherrefs idbookmarks
1143 global lineno lthickness mainline mainlinearrow sidelines
1144 global commitlisted rowtextx idpos lastuse displist
1145 global oldnlines olddlevel olddisplist
1146 global aucolormap curid curidfont obsolete
1147
1148 incr numcommits
1149 incr lineno
1150 set id [lindex $displist $level]
1151 set lastuse($id) $lineno
1152 set lineid($lineno) $id
1153 set idline($id) $lineno
1154 set shape oval
1155 set outline #000080
1156 set ofill [expr {[info exists commitlisted($id)]? "#7f7fff": "white"}]
1157 if {![info exists commitinfo($id)]} {
1158 readcommit $id
1159 if {![info exists commitinfo($id)]} {
1160 set commitinfo($id) {"No commit information available"}
1161 set nparents($id) 0
1162 }
1163 } else {
1164 switch [lindex $commitinfo($id) 9] secret {
1165 set shape rect
1166 } public {
1167 set outline black
1168 set ofill blue
1169 }
1170 }
1171 if {[info exists obsolete($id)]} {
1172 set outline darkgrey
1173 set ofill lightgrey
1174 }
1175 assigncolor $id
1176 set currentparents {}
1177 set dupparents {}
1178 if {[info exists commitlisted($id)] && [info exists parents($id)]} {
1179 foreach p $parents($id) {
1180 if {[lsearch -exact $currentparents $p] < 0} {
1181 lappend currentparents $p
1182 } else {
1183 # remember that this parent was listed twice
1184 lappend dupparents $p
1185 }
1186 }
1187 }
1188 set x [xcoord $level $level $lineno]
1189 set y1 $canvy
1190 set canvy [expr $canvy + $linespc]
1191 allcanvs conf -scrollregion \
1192 [list 0 0 0 [expr $y1 + 0.5 * $linespc + 2]]
1193 if {[info exists mainline($id)]} {
1194 lappend mainline($id) $x $y1
1195 if {$mainlinearrow($id) ne "none"} {
1196 set mainline($id) [trimdiagstart $mainline($id)]
1197 }
1198 }
1199 drawlines $id 0
1200 set orad [expr {$linespc / 3}]
1201 set t [$canv create $shape [expr $x - $orad] [expr $y1 - $orad] \
1202 [expr $x + $orad - 1] [expr $y1 + $orad - 1] \
1203 -fill $ofill -outline $outline -width 1]
1204 $canv raise $t
1205 $canv bind $t <1> {selcanvline {} %x %y}
1206 set xt [xcoord [llength $displist] $level $lineno]
1207 if {[llength $currentparents] > 2} {
1208 set xt [expr {$xt + ([llength $currentparents] - 2) * $linespc}]
1209 }
1210 set rowtextx($lineno) $xt
1211 set idpos($id) [list $x $xt $y1]
1212 if {[info exists idtags($id)] || [info exists idheads($id)]
1213 || [info exists idotherrefs($id)] || [info exists idbookmarks($id)]} {
1214 set xt [drawtags $id $x $xt $y1]
1215 }
1216 set headline [lindex $commitinfo($id) 0]
1217 set name [lindex $commitinfo($id) 1]
1218 assignauthorcolor $name
1219 set fg $aucolormap($name)
1220 if {$id == $curid} {
1221 set fn $curidfont
1222 } else {
1223 set fn $mainfont
1224 }
1225
1226 set date [lindex $commitinfo($id) 2]
1227 set linehtag($lineno) [$canv create text $xt $y1 -anchor w \
1228 -text $headline -font $fn \
1229 -fill $fg]
1230 $canv bind $linehtag($lineno) <<B3>> "rowmenu %X %Y $id"
1231 set linentag($lineno) [$canv2 create text 3 $y1 -anchor w \
1232 -text $name -font $namefont \
1233 -fill $fg]
1234 set linedtag($lineno) [$canv3 create text 3 $y1 -anchor w \
1235 -text $date -font $mainfont \
1236 -fill $fg]
1237
1238 set olddlevel $level
1239 set olddisplist $displist
1240 set oldnlines [llength $displist]
1241 }
1242
1243 proc drawtags {id x xt y1} {
1244 global bookmarkcurrent idtags idbookmarks idheads idotherrefs commitinfo
1245 global linespc lthickness
1246 global canv mainfont idline rowtextx
1247
1248 set marks {}
1249 set nbookmarks 0
1250 set ntags 0
1251 set nheads 0
1252 if {[info exists idtags($id)]} {
1253 set marks $idtags($id)
1254 set ntags [llength $marks]
1255 }
1256 if {[info exists idbookmarks($id)]} {
1257 set marks [concat $marks $idbookmarks($id)]
1258 set nbookmarks [llength $idbookmarks($id)]
1259 }
1260 if {[info exists idheads($id)]} {
1261 set headmark [lindex $commitinfo($id) 7]
1262 if {$headmark ne "default"} {
1263 lappend marks $headmark
1264 set nheads 1
1265 }
1266 }
1267 if {$marks eq {}} {
1268 return $xt
1269 }
1270
1271 set delta [expr {int(0.5 * ($linespc - $lthickness))}]
1272 set yt [expr $y1 - 0.5 * $linespc]
1273 set yb [expr $yt + $linespc - 1]
1274 set xvals {}
1275 set wvals {}
1276 foreach tag $marks {
1277 set wid [font measure $mainfont $tag]
1278 lappend xvals $xt
1279 lappend wvals $wid
1280 set xt [expr {$xt + $delta + $wid + $lthickness + $linespc}]
1281 }
1282 set t [$canv create line $x $y1 [lindex $xvals end] $y1 \
1283 -width $lthickness -fill black -tags tag.$id]
1284 $canv lower $t
1285 foreach tag $marks x $xvals wid $wvals {
1286 set xl [expr $x + $delta]
1287 set xr [expr $x + $delta + $wid + $lthickness]
1288 if {[incr ntags -1] >= 0} {
1289 # draw a tag
1290 set t [$canv create polygon $x [expr $yt + $delta] $xl $yt \
1291 $xr $yt $xr $yb $xl $yb $x [expr $yb - $delta] \
1292 -width 1 -outline black -fill yellow -tags tag.$id]
1293 $canv bind $t <1> [list showtag $tag 1]
1294 set rowtextx($idline($id)) [expr {$xr + $linespc}]
1295 } elseif {[incr nbookmarks -1] >= 0} {
1296 # draw a tag
1297 set col "#7f7f7f"
1298 if {[string compare $bookmarkcurrent $tag] == 0} {
1299 set col "#bebebe"
1300 }
1301 set xl [expr $xl - $delta/2]
1302 $canv create polygon $x $yt $xr $yt $xr $yb $x $yb \
1303 -width 1 -outline black -fill $col -tags tag.$id
1304 } else {
1305 # draw a head or other ref
1306 if {[incr nheads -1] >= 0} {
1307 set col "#00ff00"
1308 } else {
1309 set col "#ddddff"
1310 }
1311 set xl [expr $xl - $delta/2]
1312 $canv create polygon $x $yt $xr $yt $xr $yb $x $yb \
1313 -width 1 -outline black -fill $col -tags tag.$id
1314 }
1315 set t [$canv create text $xl $y1 -anchor w -text $tag \
1316 -font $mainfont -tags tag.$id]
1317 if {$ntags >= 0} {
1318 $canv bind $t <1> [list showtag $tag 1]
1319 }
1320 }
1321 return $xt
1322 }
1323
1324 proc notecrossings {id lo hi corner} {
1325 global olddisplist crossings cornercrossings
1326
1327 for {set i $lo} {[incr i] < $hi} {} {
1328 set p [lindex $olddisplist $i]
1329 if {$p == {}} continue
1330 if {$i == $corner} {
1331 if {![info exists cornercrossings($id)]
1332 || [lsearch -exact $cornercrossings($id) $p] < 0} {
1333 lappend cornercrossings($id) $p
1334 }
1335 if {![info exists cornercrossings($p)]
1336 || [lsearch -exact $cornercrossings($p) $id] < 0} {
1337 lappend cornercrossings($p) $id
1338 }
1339 } else {
1340 if {![info exists crossings($id)]
1341 || [lsearch -exact $crossings($id) $p] < 0} {
1342 lappend crossings($id) $p
1343 }
1344 if {![info exists crossings($p)]
1345 || [lsearch -exact $crossings($p) $id] < 0} {
1346 lappend crossings($p) $id
1347 }
1348 }
1349 }
1350 }
1351
1352 proc xcoord {i level ln} {
1353 global canvx0 xspc1 xspc2
1354
1355 set x [expr {$canvx0 + $i * $xspc1($ln)}]
1356 if {$i > 0 && $i == $level} {
1357 set x [expr {$x + 0.5 * ($xspc2 - $xspc1($ln))}]
1358 } elseif {$i > $level} {
1359 set x [expr {$x + $xspc2 - $xspc1($ln)}]
1360 }
1361 return $x
1362 }
1363
1364 # it seems Tk can't draw arrows on the end of diagonal line segments...
1365 proc trimdiagend {line} {
1366 while {[llength $line] > 4} {
1367 set x1 [lindex $line end-3]
1368 set y1 [lindex $line end-2]
1369 set x2 [lindex $line end-1]
1370 set y2 [lindex $line end]
1371 if {($x1 == $x2) != ($y1 == $y2)} break
1372 set line [lreplace $line end-1 end]
1373 }
1374 return $line
1375 }
1376
1377 proc trimdiagstart {line} {
1378 while {[llength $line] > 4} {
1379 set x1 [lindex $line 0]
1380 set y1 [lindex $line 1]
1381 set x2 [lindex $line 2]
1382 set y2 [lindex $line 3]
1383 if {($x1 == $x2) != ($y1 == $y2)} break
1384 set line [lreplace $line 0 1]
1385 }
1386 return $line
1387 }
1388
1389 proc drawslants {id needonscreen nohs} {
1390 global canv mainline mainlinearrow sidelines
1391 global canvx0 canvy xspc1 xspc2 lthickness
1392 global currentparents dupparents
1393 global lthickness linespc canvy colormap lineno geometry
1394 global maxgraphpct maxwidth
1395 global displist onscreen lastuse
1396 global parents commitlisted
1397 global oldnlines olddlevel olddisplist
1398 global nhyperspace numcommits nnewparents
1399
1400 if {$lineno < 0} {
1401 lappend displist $id
1402 set onscreen($id) 1
1403 return 0
1404 }
1405
1406 set y1 [expr {$canvy - $linespc}]
1407 set y2 $canvy
1408
1409 # work out what we need to get back on screen
1410 set reins {}
1411 if {$onscreen($id) < 0} {
1412 # next to do isn't displayed, better get it on screen...
1413 lappend reins [list $id 0]
1414 }
1415 # make sure all the previous commits's parents are on the screen
1416 foreach p $currentparents {
1417 if {$onscreen($p) < 0} {
1418 lappend reins [list $p 0]
1419 }
1420 }
1421 # bring back anything requested by caller
1422 if {$needonscreen ne {}} {
1423 lappend reins $needonscreen
1424 }
1425
1426 # try the shortcut
1427 if {$currentparents == $id && $onscreen($id) == 0 && $reins eq {}} {
1428 set dlevel $olddlevel
1429 set x [xcoord $dlevel $dlevel $lineno]
1430 set mainline($id) [list $x $y1]
1431 set mainlinearrow($id) none
1432 set lastuse($id) $lineno
1433 set displist [lreplace $displist $dlevel $dlevel $id]
1434 set onscreen($id) 1
1435 set xspc1([expr {$lineno + 1}]) $xspc1($lineno)
1436 return $dlevel
1437 }
1438
1439 # update displist
1440 set displist [lreplace $displist $olddlevel $olddlevel]
1441 set j $olddlevel
1442 foreach p $currentparents {
1443 set lastuse($p) $lineno
1444 if {$onscreen($p) == 0} {
1445 set displist [linsert $displist $j $p]
1446 set onscreen($p) 1
1447 incr j
1448 }
1449 }
1450 if {$onscreen($id) == 0} {
1451 lappend displist $id
1452 set onscreen($id) 1
1453 }
1454
1455 # remove the null entry if present
1456 set nullentry [lsearch -exact $displist {}]
1457 if {$nullentry >= 0} {
1458 set displist [lreplace $displist $nullentry $nullentry]
1459 }
1460
1461 # bring back the ones we need now (if we did it earlier
1462 # it would change displist and invalidate olddlevel)
1463 foreach pi $reins {
1464 # test again in case of duplicates in reins
1465 set p [lindex $pi 0]
1466 if {$onscreen($p) < 0} {
1467 set onscreen($p) 1
1468 set lastuse($p) $lineno
1469 set displist [linsert $displist [lindex $pi 1] $p]
1470 incr nhyperspace -1
1471 }
1472 }
1473
1474 set lastuse($id) $lineno
1475
1476 # see if we need to make any lines jump off into hyperspace
1477 set displ [llength $displist]
1478 if {$displ > $maxwidth} {
1479 set ages {}
1480 foreach x $displist {
1481 lappend ages [list $lastuse($x) $x]
1482 }
1483 set ages [lsort -integer -index 0 $ages]
1484 set k 0
1485 while {$displ > $maxwidth} {
1486 set use [lindex $ages $k 0]
1487 set victim [lindex $ages $k 1]
1488 if {$use >= $lineno - 5} break
1489 incr k
1490 if {[lsearch -exact $nohs $victim] >= 0} continue
1491 set i [lsearch -exact $displist $victim]
1492 set displist [lreplace $displist $i $i]
1493 set onscreen($victim) -1
1494 incr nhyperspace
1495 incr displ -1
1496 if {$i < $nullentry} {
1497 incr nullentry -1
1498 }
1499 set x [lindex $mainline($victim) end-1]
1500 lappend mainline($victim) $x $y1
1501 set line [trimdiagend $mainline($victim)]
1502 set arrow "last"
1503 if {$mainlinearrow($victim) ne "none"} {
1504 set line [trimdiagstart $line]
1505 set arrow "both"
1506 }
1507 lappend sidelines($victim) [list $line 1 $arrow]
1508 unset mainline($victim)
1509 }
1510 }
1511
1512 set dlevel [lsearch -exact $displist $id]
1513
1514 # If we are reducing, put in a null entry
1515 if {$displ < $oldnlines} {
1516 # does the next line look like a merge?
1517 # i.e. does it have > 1 new parent?
1518 if {$nnewparents($id) > 1} {
1519 set i [expr {$dlevel + 1}]
1520 } elseif {$nnewparents([lindex $olddisplist $olddlevel]) == 0} {
1521 set i $olddlevel
1522 if {$nullentry >= 0 && $nullentry < $i} {
1523 incr i -1
1524 }
1525 } elseif {$nullentry >= 0} {
1526 set i $nullentry
1527 while {$i < $displ
1528 && [lindex $olddisplist $i] == [lindex $displist $i]} {
1529 incr i
1530 }
1531 } else {
1532 set i $olddlevel
1533 if {$dlevel >= $i} {
1534 incr i
1535 }
1536 }
1537 if {$i < $displ} {
1538 set displist [linsert $displist $i {}]
1539 incr displ
1540 if {$dlevel >= $i} {
1541 incr dlevel
1542 }
1543 }
1544 }
1545
1546 # decide on the line spacing for the next line
1547 set lj [expr {$lineno + 1}]
1548 set maxw [expr {$maxgraphpct * $geometry(canv1) / 100}]
1549 if {$displ <= 1 || $canvx0 + $displ * $xspc2 <= $maxw} {
1550 set xspc1($lj) $xspc2
1551 } else {
1552 set xspc1($lj) [expr {($maxw - $canvx0 - $xspc2) / ($displ - 1)}]
1553 if {$xspc1($lj) < $lthickness} {
1554 set xspc1($lj) $lthickness
1555 }
1556 }
1557
1558 foreach idi $reins {
1559 set id [lindex $idi 0]
1560 set j [lsearch -exact $displist $id]
1561 set xj [xcoord $j $dlevel $lj]
1562 set mainline($id) [list $xj $y2]
1563 set mainlinearrow($id) first
1564 }
1565
1566 set i -1
1567 foreach id $olddisplist {
1568 incr i
1569 if {$id == {}} continue
1570 if {$onscreen($id) <= 0} continue
1571 set xi [xcoord $i $olddlevel $lineno]
1572 if {$i == $olddlevel} {
1573 foreach p $currentparents {
1574 set j [lsearch -exact $displist $p]
1575 set coords [list $xi $y1]
1576 set xj [xcoord $j $dlevel $lj]
1577 if {$xj < $xi - $linespc} {
1578 lappend coords [expr {$xj + $linespc}] $y1
1579 notecrossings $p $j $i [expr {$j + 1}]
1580 } elseif {$xj > $xi + $linespc} {
1581 lappend coords [expr {$xj - $linespc}] $y1
1582 notecrossings $p $i $j [expr {$j - 1}]
1583 }
1584 if {[lsearch -exact $dupparents $p] >= 0} {
1585 # draw a double-width line to indicate the doubled parent
1586 lappend coords $xj $y2
1587 lappend sidelines($p) [list $coords 2 none]
1588 if {![info exists mainline($p)]} {
1589 set mainline($p) [list $xj $y2]
1590 set mainlinearrow($p) none
1591 }
1592 } else {
1593 # normal case, no parent duplicated
1594 set yb $y2
1595 set dx [expr {abs($xi - $xj)}]
1596 if {0 && $dx < $linespc} {
1597 set yb [expr {$y1 + $dx}]
1598 }
1599 if {![info exists mainline($p)]} {
1600 if {$xi != $xj} {
1601 lappend coords $xj $yb
1602 }
1603 set mainline($p) $coords
1604 set mainlinearrow($p) none
1605 } else {
1606 lappend coords $xj $yb
1607 if {$yb < $y2} {
1608 lappend coords $xj $y2
1609 }
1610 lappend sidelines($p) [list $coords 1 none]
1611 }
1612 }
1613 }
1614 } else {
1615 set j $i
1616 if {[lindex $displist $i] != $id} {
1617 set j [lsearch -exact $displist $id]
1618 }
1619 if {$j != $i || $xspc1($lineno) != $xspc1($lj)
1620 || ($olddlevel < $i && $i < $dlevel)
1621 || ($dlevel < $i && $i < $olddlevel)} {
1622 set xj [xcoord $j $dlevel $lj]
1623 lappend mainline($id) $xi $y1 $xj $y2
1624 }
1625 }
1626 }
1627 return $dlevel
1628 }
1629
1630 # search for x in a list of lists
1631 proc llsearch {llist x} {
1632 set i 0
1633 foreach l $llist {
1634 if {$l == $x || [lsearch -exact $l $x] >= 0} {
1635 return $i
1636 }
1637 incr i
1638 }
1639 return -1
1640 }
1641
1642 proc drawmore {reading} {
1643 global displayorder numcommits ncmupdate nextupdate
1644 global stopped nhyperspace parents commitlisted
1645 global maxwidth onscreen displist currentparents olddlevel
1646
1647 set n [llength $displayorder]
1648 while {$numcommits < $n} {
1649 set id [lindex $displayorder $numcommits]
1650 set ctxend [expr {$numcommits + 10}]
1651 if {!$reading && $ctxend > $n} {
1652 set ctxend $n
1653 }
1654 set dlist {}
1655 if {$numcommits > 0} {
1656 set dlist [lreplace $displist $olddlevel $olddlevel]
1657 set i $olddlevel
1658 foreach p $currentparents {
1659 if {$onscreen($p) == 0} {
1660 set dlist [linsert $dlist $i $p]
1661 incr i
1662 }
1663 }
1664 }
1665 set nohs {}
1666 set reins {}
1667 set isfat [expr {[llength $dlist] > $maxwidth}]
1668 if {$nhyperspace > 0 || $isfat} {
1669 if {$ctxend > $n} break
1670 # work out what to bring back and
1671 # what we want to don't want to send into hyperspace
1672 set room 1
1673 for {set k $numcommits} {$k < $ctxend} {incr k} {
1674 set x [lindex $displayorder $k]
1675 set i [llsearch $dlist $x]
1676 if {$i < 0} {
1677 set i [llength $dlist]
1678 lappend dlist $x
1679 }
1680 if {[lsearch -exact $nohs $x] < 0} {
1681 lappend nohs $x
1682 }
1683 if {$reins eq {} && $onscreen($x) < 0 && $room} {
1684 set reins [list $x $i]
1685 }
1686 set newp {}
1687 if {[info exists commitlisted($x)]} {
1688 set right 0
1689 foreach p $parents($x) {
1690 if {[llsearch $dlist $p] < 0} {
1691 lappend newp $p
1692 if {[lsearch -exact $nohs $p] < 0} {
1693 lappend nohs $p
1694 }
1695 if {$reins eq {} && $onscreen($p) < 0 && $room} {
1696 set reins [list $p [expr {$i + $right}]]
1697 }
1698 }
1699 set right 1
1700 }
1701 }
1702 set l [lindex $dlist $i]
1703 if {[llength $l] == 1} {
1704 set l $newp
1705 } else {
1706 set j [lsearch -exact $l $x]
1707 set l [concat [lreplace $l $j $j] $newp]
1708 }
1709 set dlist [lreplace $dlist $i $i $l]
1710 if {$room && $isfat && [llength $newp] <= 1} {
1711 set room 0
1712 }
1713 }
1714 }
1715
1716 set dlevel [drawslants $id $reins $nohs]
1717 drawcommitline $dlevel
1718 if {[clock clicks -milliseconds] >= $nextupdate
1719 && $numcommits >= $ncmupdate} {
1720 doupdate $reading
1721 if {$stopped} break
1722 }
1723 }
1724 }
1725
1726 # level here is an index in todo
1727 proc updatetodo {level noshortcut} {
1728 global ncleft todo nnewparents
1729 global commitlisted parents onscreen
1730
1731 set id [lindex $todo $level]
1732 set olds {}
1733 if {[info exists commitlisted($id)]} {
1734 foreach p $parents($id) {
1735 if {[lsearch -exact $olds $p] < 0} {
1736 lappend olds $p
1737 }
1738 }
1739 }
1740 if {!$noshortcut && [llength $olds] == 1} {
1741 set p [lindex $olds 0]
1742 if {$ncleft($p) == 1 && [lsearch -exact $todo $p] < 0} {
1743 set ncleft($p) 0
1744 set todo [lreplace $todo $level $level $p]
1745 set onscreen($p) 0
1746 set nnewparents($id) 1
1747 return 0
1748 }
1749 }
1750
1751 set todo [lreplace $todo $level $level]
1752 set i $level
1753 set n 0
1754 foreach p $olds {
1755 incr ncleft($p) -1
1756 set k [lsearch -exact $todo $p]
1757 if {$k < 0} {
1758 set todo [linsert $todo $i $p]
1759 set onscreen($p) 0
1760 incr i
1761 incr n
1762 }
1763 }
1764 set nnewparents($id) $n
1765
1766 return 1
1767 }
1768
1769 proc decidenext {{noread 0}} {
1770 global ncleft todo
1771 global datemode cdate
1772 global commitinfo
1773
1774 # choose which one to do next time around
1775 set todol [llength $todo]
1776 set level -1
1777 set latest {}
1778 for {set k $todol} {[incr k -1] >= 0} {} {
1779 set p [lindex $todo $k]
1780 if {$ncleft($p) == 0} {
1781 if {$datemode} {
1782 if {![info exists commitinfo($p)]} {
1783 if {$noread} {
1784 return {}
1785 }
1786 readcommit $p
1787 }
1788 if {$latest == {} || $cdate($p) > $latest} {
1789 set level $k
1790 set latest $cdate($p)
1791 }
1792 } else {
1793 set level $k
1794 break
1795 }
1796 }
1797 }
1798 if {$level < 0} {
1799 if {$todo != {}} {
1800 puts "ERROR: none of the pending commits can be done yet:"
1801 foreach p $todo {
1802 puts " $p ($ncleft($p))"
1803 }
1804 }
1805 return -1
1806 }
1807
1808 return $level
1809 }
1810
1811 proc drawcommit {id} {
1812 global phase todo nchildren datemode nextupdate
1813 global numcommits ncmupdate displayorder todo onscreen
1814
1815 if {$phase != "incrdraw"} {
1816 set phase incrdraw
1817 set displayorder {}
1818 set todo {}
1819 initgraph
1820 }
1821 if {$nchildren($id) == 0} {
1822 lappend todo $id
1823 set onscreen($id) 0
1824 }
1825 set level [decidenext 1]
1826 if {$level == {} || $id != [lindex $todo $level]} {
1827 return
1828 }
1829 while 1 {
1830 lappend displayorder [lindex $todo $level]
1831 if {[updatetodo $level $datemode]} {
1832 set level [decidenext 1]
1833 if {$level == {}} break
1834 }
1835 set id [lindex $todo $level]
1836 if {![info exists commitlisted($id)]} {
1837 break
1838 }
1839 }
1840 drawmore 1
1841 }
1842
1843 proc finishcommits {} {
1844 global phase
1845 global canv mainfont ctext maincursor textcursor
1846
1847 if {$phase != "incrdraw"} {
1848 $canv delete all
1849 $canv create text 3 3 -anchor nw -text "No commits selected" \
1850 -font $mainfont -tags textitems
1851 set phase {}
1852 } else {
1853 drawrest
1854 }
1855 . config -cursor $maincursor
1856 settextcursor $textcursor
1857 }
1858
1859 # Don't change the text pane cursor if it is currently the hand cursor,
1860 # showing that we are over a sha1 ID link.
1861 proc settextcursor {c} {
1862 global ctext curtextcursor
1863
1864 if {[$ctext cget -cursor] == $curtextcursor} {
1865 $ctext config -cursor $c
1866 }
1867 set curtextcursor $c
1868 }
1869
1870 proc drawgraph {} {
1871 global nextupdate startmsecs ncmupdate
1872 global displayorder onscreen
1873
1874 if {$displayorder == {}} return
1875 set startmsecs [clock clicks -milliseconds]
1876 set nextupdate [expr $startmsecs + 100]
1877 set ncmupdate 1
1878 initgraph
1879 foreach id $displayorder {
1880 set onscreen($id) 0
1881 }
1882 drawmore 0
1883 }
1884
1885 proc drawrest {} {
1886 global phase stopped redisplaying selectedline
1887 global datemode todo displayorder
1888 global numcommits ncmupdate
1889 global nextupdate startmsecs
1890
1891 set level [decidenext]
1892 if {$level >= 0} {
1893 set phase drawgraph
1894 while 1 {
1895 lappend displayorder [lindex $todo $level]
1896 set hard [updatetodo $level $datemode]
1897 if {$hard} {
1898 set level [decidenext]
1899 if {$level < 0} break
1900 }
1901 }
1902 drawmore 0
1903 }
1904 set phase {}
1905 set drawmsecs [expr [clock clicks -milliseconds] - $startmsecs]
1906 #puts "overall $drawmsecs ms for $numcommits commits"
1907 if {$redisplaying} {
1908 if {$stopped == 0 && [info exists selectedline]} {
1909 selectline $selectedline 0
1910 }
1911 if {$stopped == 1} {
1912 set stopped 0
1913 after idle drawgraph
1914 } else {
1915 set redisplaying 0
1916 }
1917 }
1918 }
1919
1920 proc findmatches {f} {
1921 global findtype foundstring foundstrlen
1922 if {$findtype == "Regexp"} {
1923 set matches [regexp -indices -all -inline $foundstring $f]
1924 } else {
1925 if {$findtype == "IgnCase"} {
1926 set str [string tolower $f]
1927 } else {
1928 set str $f
1929 }
1930 set matches {}
1931 set i 0
1932 while {[set j [string first $foundstring $str $i]] >= 0} {
1933 lappend matches [list $j [expr $j+$foundstrlen-1]]
1934 set i [expr $j + $foundstrlen]
1935 }
1936 }
1937 return $matches
1938 }
1939
1940 proc dofind {} {
1941 global findtype findloc findstring markedmatches commitinfo
1942 global numcommits lineid linehtag linentag linedtag
1943 global mainfont namefont canv canv2 canv3 selectedline
1944 global matchinglines foundstring foundstrlen
1945
1946 stopfindproc
1947 unmarkmatches
1948 focus .
1949 set matchinglines {}
1950 if {$findloc == "Pickaxe"} {
1951 findpatches
1952 return
1953 }
1954 if {$findtype == "IgnCase"} {
1955 set foundstring [string tolower $findstring]
1956 } else {
1957 set foundstring $findstring
1958 }
1959 set foundstrlen [string length $findstring]
1960 if {$foundstrlen == 0} return
1961 if {$findloc == "Files"} {
1962 findfiles
1963 return
1964 }
1965 if {![info exists selectedline]} {
1966 set oldsel -1
1967 } else {
1968 set oldsel $selectedline
1969 }
1970 set didsel 0
1971 set fldtypes {Headline Author Date CDate Comment}
1972 for {set l 0} {$l < $numcommits} {incr l} {
1973 set id $lineid($l)
1974 set info $commitinfo($id)
1975 set doesmatch 0
1976 foreach f $info ty $fldtypes {
1977 if {$findloc != "All fields" && $findloc != $ty} {
1978 continue
1979 }
1980 set matches [findmatches $f]
1981 if {$matches == {}} continue
1982 set doesmatch 1
1983 if {$ty == "Headline"} {
1984 markmatches $canv $l $f $linehtag($l) $matches $mainfont
1985 } elseif {$ty == "Author"} {
1986 markmatches $canv2 $l $f $linentag($l) $matches $namefont
1987 } elseif {$ty == "Date"} {
1988 markmatches $canv3 $l $f $linedtag($l) $matches $mainfont
1989 }
1990 }
1991 if {$doesmatch} {
1992 lappend matchinglines $l
1993 if {!$didsel && $l > $oldsel} {
1994 findselectline $l
1995 set didsel 1
1996 }
1997 }
1998 }
1999 if {$matchinglines == {}} {
2000 bell
2001 } elseif {!$didsel} {
2002 findselectline [lindex $matchinglines 0]
2003 }
2004 }
2005
2006 proc findselectline {l} {
2007 global findloc commentend ctext
2008 selectline $l 1
2009 if {$findloc == "All fields" || $findloc == "Comments"} {
2010 # highlight the matches in the comments
2011 set f [$ctext get 1.0 $commentend]
2012 set matches [findmatches $f]
2013 foreach match $matches {
2014 set start [lindex $match 0]
2015 set end [expr [lindex $match 1] + 1]
2016 $ctext tag add found "1.0 + $start c" "1.0 + $end c"
2017 }
2018 }
2019 }
2020
2021 proc findnext {restart} {
2022 global matchinglines selectedline
2023 if {![info exists matchinglines]} {
2024 if {$restart} {
2025 dofind
2026 }
2027 return
2028 }
2029 if {![info exists selectedline]} return
2030 foreach l $matchinglines {
2031 if {$l > $selectedline} {
2032 findselectline $l
2033 return
2034 }
2035 }
2036 bell
2037 }
2038
2039 proc findprev {} {
2040 global matchinglines selectedline
2041 if {![info exists matchinglines]} {
2042 dofind
2043 return
2044 }
2045 if {![info exists selectedline]} return
2046 set prev {}
2047 foreach l $matchinglines {
2048 if {$l >= $selectedline} break
2049 set prev $l
2050 }
2051 if {$prev != {}} {
2052 findselectline $prev
2053 } else {
2054 bell
2055 }
2056 }
2057
2058 proc findlocchange {name ix op} {
2059 global findloc findtype findtypemenu
2060 if {$findloc == "Pickaxe"} {
2061 set findtype Exact
2062 set state disabled
2063 } else {
2064 set state normal
2065 }
2066 $findtypemenu entryconf 1 -state $state
2067 $findtypemenu entryconf 2 -state $state
2068 }
2069
2070 proc stopfindproc {{done 0}} {
2071 global findprocpid findprocfile findids
2072 global ctext findoldcursor phase maincursor textcursor
2073 global findinprogress
2074
2075 catch {unset findids}
2076 if {[info exists findprocpid]} {
2077 if {!$done} {
2078 catch {exec kill $findprocpid}
2079 }
2080 catch {close $findprocfile}
2081 unset findprocpid
2082 }
2083 if {[info exists findinprogress]} {
2084 unset findinprogress
2085 if {$phase != "incrdraw"} {
2086 . config -cursor $maincursor
2087 settextcursor $textcursor
2088 }
2089 }
2090 }
2091
2092 proc findpatches {} {
2093 global findstring selectedline numcommits
2094 global findprocpid findprocfile
2095 global finddidsel ctext lineid findinprogress
2096 global findinsertpos
2097 global env
2098
2099 if {$numcommits == 0} return
2100
2101 # make a list of all the ids to search, starting at the one
2102 # after the selected line (if any)
2103 if {[info exists selectedline]} {
2104 set l $selectedline
2105 } else {
2106 set l -1
2107 }
2108 set inputids {}
2109 for {set i 0} {$i < $numcommits} {incr i} {
2110 if {[incr l] >= $numcommits} {
2111 set l 0
2112 }
2113 append inputids $lineid($l) "\n"
2114 }
2115
2116 if {[catch {
2117 set f [open [list | $env(HG) --config ui.report_untrusted=false debug-diff-tree --stdin -s -r -S$findstring << $inputids] r]
2118 } err]} {
2119 error_popup "Error starting search process: $err"
2120 return
2121 }
2122
2123 set findinsertpos end
2124 set findprocfile $f
2125 set findprocpid [pid $f]
2126 fconfigure $f -blocking 0
2127 fileevent $f readable readfindproc
2128 set finddidsel 0
2129 . config -cursor watch
2130 settextcursor watch
2131 set findinprogress 1
2132 }
2133
2134 proc readfindproc {} {
2135 global findprocfile finddidsel
2136 global idline matchinglines findinsertpos
2137
2138 set n [gets $findprocfile line]
2139 if {$n < 0} {
2140 if {[eof $findprocfile]} {
2141 stopfindproc 1
2142 if {!$finddidsel} {
2143 bell
2144 }
2145 }
2146 return
2147 }
2148 if {![regexp {^[0-9a-f]{12}} $line id]} {
2149 error_popup "Can't parse git-diff-tree output: $line"
2150 stopfindproc
2151 return
2152 }
2153 if {![info exists idline($id)]} {
2154 puts stderr "spurious id: $id"
2155 return
2156 }
2157 set l $idline($id)
2158 insertmatch $l $id
2159 }
2160
2161 proc insertmatch {l id} {
2162 global matchinglines findinsertpos finddidsel
2163
2164 if {$findinsertpos == "end"} {
2165 if {$matchinglines != {} && $l < [lindex $matchinglines 0]} {
2166 set matchinglines [linsert $matchinglines 0 $l]
2167 set findinsertpos 1
2168 } else {
2169 lappend matchinglines $l
2170 }
2171 } else {
2172 set matchinglines [linsert $matchinglines $findinsertpos $l]
2173 incr findinsertpos
2174 }
2175 markheadline $l $id
2176 if {!$finddidsel} {
2177 findselectline $l
2178 set finddidsel 1
2179 }
2180 }
2181
2182 proc findfiles {} {
2183 global selectedline numcommits lineid ctext
2184 global ffileline finddidsel parents nparents
2185 global findinprogress findstartline findinsertpos
2186 global treediffs fdiffids fdiffsneeded fdiffpos
2187 global findmergefiles
2188 global env
2189
2190 if {$numcommits == 0} return
2191
2192 if {[info exists selectedline]} {
2193 set l [expr {$selectedline + 1}]
2194 } else {
2195 set l 0
2196 }
2197 set ffileline $l
2198 set findstartline $l
2199 set diffsneeded {}
2200 set fdiffsneeded {}
2201 while 1 {
2202 set id $lineid($l)
2203 if {$findmergefiles || $nparents($id) == 1} {
2204 foreach p $parents($id) {
2205 if {![info exists treediffs([list $id $p])]} {
2206 append diffsneeded "$id $p\n"
2207 lappend fdiffsneeded [list $id $p]
2208 }
2209 }
2210 }
2211 if {[incr l] >= $numcommits} {
2212 set l 0
2213 }
2214 if {$l == $findstartline} break
2215 }
2216
2217 # start off a git-diff-tree process if needed
2218 if {$diffsneeded ne {}} {
2219 if {[catch {
2220 set df [open [list | $env(HG) --config ui.report_untrusted=false debug-diff-tree -r --stdin << $diffsneeded] r]
2221 } err ]} {
2222 error_popup "Error starting search process: $err"
2223 return
2224 }
2225 catch {unset fdiffids}
2226 set fdiffpos 0
2227 fconfigure $df -blocking 0
2228 fileevent $df readable [list readfilediffs $df]
2229 }
2230
2231 set finddidsel 0
2232 set findinsertpos end
2233 set id $lineid($l)
2234 set p [lindex $parents($id) 0]
2235 . config -cursor watch
2236 settextcursor watch
2237 set findinprogress 1
2238 findcont [list $id $p]
2239 update
2240 }
2241
2242 proc readfilediffs {df} {
2243 global findids fdiffids fdiffs
2244
2245 set n [gets $df line]
2246 if {$n < 0} {
2247 if {[eof $df]} {
2248 donefilediff
2249 if {[catch {close $df} err]} {
2250 stopfindproc
2251 bell
2252 error_popup "Error in hg debug-diff-tree: $err"
2253 } elseif {[info exists findids]} {
2254 set ids $findids
2255 stopfindproc
2256 bell
2257 error_popup "Couldn't find diffs for {$ids}"
2258 }
2259 }
2260 return
2261 }
2262 if {[regexp {^([0-9a-f]{12}) \(from ([0-9a-f]{12})\)} $line match id p]} {
2263 # start of a new string of diffs
2264 donefilediff
2265 set fdiffids [list $id $p]
2266 set fdiffs {}
2267 } elseif {[string match ":*" $line]} {
2268 lappend fdiffs [lindex $line 5]
2269 }
2270 }
2271
2272 proc donefilediff {} {
2273 global fdiffids fdiffs treediffs findids
2274 global fdiffsneeded fdiffpos
2275
2276 if {[info exists fdiffids]} {
2277 while {[lindex $fdiffsneeded $fdiffpos] ne $fdiffids
2278 && $fdiffpos < [llength $fdiffsneeded]} {
2279 # git-diff-tree doesn't output anything for a commit
2280 # which doesn't change anything
2281 set nullids [lindex $fdiffsneeded $fdiffpos]
2282 set treediffs($nullids) {}
2283 if {[info exists findids] && $nullids eq $findids} {
2284 unset findids
2285 findcont $nullids
2286 }
2287 incr fdiffpos
2288 }
2289 incr fdiffpos
2290
2291 if {![info exists treediffs($fdiffids)]} {
2292 set treediffs($fdiffids) $fdiffs
2293 }
2294 if {[info exists findids] && $fdiffids eq $findids} {
2295 unset findids
2296 findcont $fdiffids
2297 }
2298 }
2299 }
2300
2301 proc findcont {ids} {
2302 global findids treediffs parents nparents
2303 global ffileline findstartline finddidsel
2304 global lineid numcommits matchinglines findinprogress
2305 global findmergefiles
2306
2307 set id [lindex $ids 0]
2308 set p [lindex $ids 1]
2309 set pi [lsearch -exact $parents($id) $p]
2310 set l $ffileline
2311 while 1 {
2312 if {$findmergefiles || $nparents($id) == 1} {
2313 if {![info exists treediffs($ids)]} {
2314 set findids $ids
2315 set ffileline $l
2316 return
2317 }
2318 set doesmatch 0
2319 foreach f $treediffs($ids) {
2320 set x [findmatches $f]
2321 if {$x != {}} {
2322 set doesmatch 1
2323 break
2324 }
2325 }
2326 if {$doesmatch} {
2327 insertmatch $l $id
2328 set pi $nparents($id)
2329 }
2330 } else {
2331 set pi $nparents($id)
2332 }
2333 if {[incr pi] >= $nparents($id)} {
2334 set pi 0
2335 if {[incr l] >= $numcommits} {
2336 set l 0
2337 }
2338 if {$l == $findstartline} break
2339 set id $lineid($l)
2340 }
2341 set p [lindex $parents($id) $pi]
2342 set ids [list $id $p]
2343 }
2344 stopfindproc
2345 if {!$finddidsel} {
2346 bell
2347 }
2348 }
2349
2350 # mark a commit as matching by putting a yellow background
2351 # behind the headline
2352 proc markheadline {l id} {
2353 global canv mainfont linehtag commitinfo
2354
2355 set bbox [$canv bbox $linehtag($l)]
2356 set t [$canv create rect $bbox -outline {} -tags matches -fill yellow]
2357 $canv lower $t
2358 }
2359
2360 # mark the bits of a headline, author or date that match a find string
2361 proc markmatches {canv l str tag matches font} {
2362 set bbox [$canv bbox $tag]
2363 set x0 [lindex $bbox 0]
2364 set y0 [lindex $bbox 1]
2365 set y1 [lindex $bbox 3]
2366 foreach match $matches {
2367 set start [lindex $match 0]
2368 set end [lindex $match 1]
2369 if {$start > $end} continue
2370 set xoff [font measure $font [string range $str 0 [expr $start-1]]]
2371 set xlen [font measure $font [string range $str 0 [expr $end]]]
2372 set t [$canv create rect [expr $x0+$xoff] $y0 [expr $x0+$xlen+2] $y1 \
2373 -outline {} -tags matches -fill yellow]
2374 $canv lower $t
2375 }
2376 }
2377
2378 proc unmarkmatches {} {
2379 global matchinglines findids
2380 allcanvs delete matches
2381 catch {unset matchinglines}
2382 catch {unset findids}
2383 }
2384
2385 proc selcanvline {w x y} {
2386 global canv canvy0 ctext linespc
2387 global lineid linehtag linentag linedtag rowtextx
2388 set ymax [lindex [$canv cget -scrollregion] 3]
2389 if {$ymax == {}} return
2390 set yfrac [lindex [$canv yview] 0]
2391 set y [expr {$y + $yfrac * $ymax}]
2392 set l [expr {int(($y - $canvy0) / $linespc + 0.5)}]
2393 if {$l < 0} {
2394 set l 0
2395 }
2396 if {$w eq $canv} {
2397 if {![info exists rowtextx($l)] || $x < $rowtextx($l)} return
2398 }
2399 unmarkmatches
2400 selectline $l 1
2401 }
2402
2403 proc commit_descriptor {p} {
2404 global commitinfo
2405 set l "..."
2406 if {[info exists commitinfo($p)]} {
2407 set l [lindex $commitinfo($p) 0]
2408 set r [lindex $commitinfo($p) 6]
2409 }
2410 return "$r:$p ($l)"
2411 }
2412
2413 # append some text to the ctext widget, and make any SHA1 ID
2414 # that we know about be a clickable link.
2415 proc appendwithlinks {text} {
2416 global ctext idline linknum
2417
2418 set start [$ctext index "end - 1c"]
2419 $ctext insert end $text
2420 $ctext insert end "\n"
2421 set links [regexp -indices -all -inline {[0-9a-f]{12}} $text]
2422 foreach l $links {
2423 set s [lindex $l 0]
2424 set e [lindex $l 1]
2425 set linkid [string range $text $s $e]
2426 if {![info exists idline($linkid)]} continue
2427 incr e
2428 $ctext tag add link "$start + $s c" "$start + $e c"
2429 $ctext tag add link$linknum "$start + $s c" "$start + $e c"
2430 $ctext tag bind link$linknum <1> [list selectline $idline($linkid) 1]
2431 incr linknum
2432 }
2433 $ctext tag conf link -foreground blue -underline 1
2434 $ctext tag bind link <Enter> { %W configure -cursor hand2 }
2435 $ctext tag bind link <Leave> { %W configure -cursor $curtextcursor }
2436 }
2437
2438 proc selectline {l isnew} {
2439 global canv canv2 canv3 ctext commitinfo selectedline
2440 global lineid linehtag linentag linedtag
2441 global canvy0 linespc parents nparents children
2442 global cflist currentid sha1entry
2443 global commentend idtags idbookmarks idline linknum
2444
2445 $canv delete hover
2446 normalline
2447 if {![info exists lineid($l)] || ![info exists linehtag($l)]} return
2448 $canv delete secsel
2449 set t [eval $canv create rect [$canv bbox $linehtag($l)] -outline {{}} \
2450 -tags secsel -fill [$canv cget -selectbackground]]
2451 $canv lower $t
2452 $canv2 delete secsel
2453 set t [eval $canv2 create rect [$canv2 bbox $linentag($l)] -outline {{}} \
2454 -tags secsel -fill [$canv2 cget -selectbackground]]
2455 $canv2 lower $t
2456 $canv3 delete secsel
2457 set t [eval $canv3 create rect [$canv3 bbox $linedtag($l)] -outline {{}} \
2458 -tags secsel -fill [$canv3 cget -selectbackground]]
2459 $canv3 lower $t
2460 set y [expr {$canvy0 + $l * $linespc}]
2461 set ymax [lindex [$canv cget -scrollregion] 3]
2462 set ytop [expr {$y - $linespc - 1}]
2463 set ybot [expr {$y + $linespc + 1}]
2464 set wnow [$canv yview]
2465 set wtop [expr [lindex $wnow 0] * $ymax]
2466 set wbot [expr [lindex $wnow 1] * $ymax]
2467 set wh [expr {$wbot - $wtop}]
2468 set newtop $wtop
2469 if {$ytop < $wtop} {
2470 if {$ybot < $wtop} {
2471 set newtop [expr {$y - $wh / 2.0}]
2472 } else {
2473 set newtop $ytop
2474 if {$newtop > $wtop - $linespc} {
2475 set newtop [expr {$wtop - $linespc}]
2476 }
2477 }
2478 } elseif {$ybot > $wbot} {
2479 if {$ytop > $wbot} {
2480 set newtop [expr {$y - $wh / 2.0}]
2481 } else {
2482 set newtop [expr {$ybot - $wh}]
2483 if {$newtop < $wtop + $linespc} {
2484 set newtop [expr {$wtop + $linespc}]
2485 }
2486 }
2487 }
2488 if {$newtop != $wtop} {
2489 if {$newtop < 0} {
2490 set newtop 0
2491 }
2492 allcanvs yview moveto [expr $newtop * 1.0 / $ymax]
2493 }
2494
2495 if {$isnew} {
2496 addtohistory [list selectline $l 0]
2497 }
2498
2499 set selectedline $l
2500
2501 set id $lineid($l)
2502 set currentid $id
2503 $sha1entry delete 0 end
2504 $sha1entry insert 0 $id
2505 $sha1entry selection range 0 end
2506
2507 $ctext conf -state normal
2508 $ctext delete 0.0 end
2509 set linknum 0
2510 $ctext mark set fmark.0 0.0
2511 $ctext mark gravity fmark.0 left
2512 set info $commitinfo($id)
2513 $ctext insert end "Changeset: [lindex $info 6]\n"
2514 if {[llength [lindex $info 7]] > 0} {
2515 $ctext insert end "Branch: [lindex $info 7]\n"
2516 }
2517 $ctext insert end "User: [lindex $info 1]\n"
2518 $ctext insert end "Date: [lindex $info 2]\n"
2519 if {[lindex $info 3] ne ""} {
2520 $ctext insert end "Committer: [lindex $info 3]\n"
2521 }
2522 if {[info exists idbookmarks($id)]} {
2523 $ctext insert end "Bookmarks:"
2524 foreach bookmark $idbookmarks($id) {
2525 $ctext insert end " $bookmark"
2526 }
2527 $ctext insert end "\n"
2528 }
2529
2530 if {[info exists idtags($id)]} {
2531 $ctext insert end "Tags:"
2532 foreach tag $idtags($id) {
2533 $ctext insert end " $tag"
2534 }
2535 $ctext insert end "\n"
2536 }
2537
2538 set comment {}
2539 if {[info exists parents($id)]} {
2540 foreach p $parents($id) {
2541 append comment "Parent: [commit_descriptor $p]\n"
2542 }
2543 }
2544 if {[info exists children($id)]} {
2545 foreach c $children($id) {
2546 append comment "Child: [commit_descriptor $c]\n"
2547 }
2548 }
2549
2550 if {[lindex $info 9] eq "secret"} {
2551 # for now, display phase for secret changesets only
2552 append comment "Phase: [lindex $info 9]\n"
2553 }
2554
2555 append comment "\n"
2556 append comment [lindex $info 5]
2557
2558 # make anything that looks like a SHA1 ID be a clickable link
2559 appendwithlinks $comment
2560
2561 $ctext tag delete Comments
2562 $ctext tag remove found 1.0 end
2563 $ctext conf -state disabled
2564 set commentend [$ctext index "end - 1c"]
2565
2566 $cflist delete 0 end
2567 $cflist insert end "Comments"
2568 if {$nparents($id) <= 1} {
2569 set parent "null"
2570 if {$nparents($id) == 1} {
2571 set parent $parents($id)
2572 }
2573 startdiff [concat $id $parent]
2574 } elseif {$nparents($id) > 1} {
2575 mergediff $id
2576 }
2577 }
2578
2579 proc selnextline {dir} {
2580 global selectedline
2581 focus .
2582 if {![info exists selectedline]} return
2583 set l [expr $selectedline + $dir]
2584 unmarkmatches
2585 selectline $l 1
2586 }
2587
2588 proc unselectline {} {
2589 global selectedline
2590
2591 catch {unset selectedline}
2592 allcanvs delete secsel
2593 }
2594
2595 proc addtohistory {cmd} {
2596 global history historyindex
2597
2598 if {$historyindex > 0
2599 && [lindex $history [expr {$historyindex - 1}]] == $cmd} {
2600 return
2601 }
2602
2603 if {$historyindex < [llength $history]} {
2604 set history [lreplace $history $historyindex end $cmd]
2605 } else {
2606 lappend history $cmd
2607 }
2608 incr historyindex
2609 if {$historyindex > 1} {
2610 .ctop.top.bar.leftbut conf -state normal
2611 } else {
2612 .ctop.top.bar.leftbut conf -state disabled
2613 }
2614 .ctop.top.bar.rightbut conf -state disabled
2615 }
2616
2617 proc goback {} {
2618 global history historyindex
2619 focus .
2620
2621 if {$historyindex > 1} {
2622 incr historyindex -1
2623 set cmd [lindex $history [expr {$historyindex - 1}]]
2624 eval $cmd
2625 .ctop.top.bar.rightbut conf -state normal
2626 }
2627 if {$historyindex <= 1} {
2628 .ctop.top.bar.leftbut conf -state disabled
2629 }
2630 }
2631
2632 proc goforw {} {
2633 global history historyindex
2634 focus .
2635
2636 if {$historyindex < [llength $history]} {
2637 set cmd [lindex $history $historyindex]
2638 incr historyindex
2639 eval $cmd
2640 .ctop.top.bar.leftbut conf -state normal
2641 }
2642 if {$historyindex >= [llength $history]} {
2643 .ctop.top.bar.rightbut conf -state disabled
2644 }
2645 }
2646
2647 proc mergediff {id} {
2648 global parents diffmergeid diffmergegca mergefilelist diffpindex
2649
2650 set diffmergeid $id
2651 set diffpindex -1
2652 set diffmergegca [findgca $parents($id)]
2653 if {[info exists mergefilelist($id)]} {
2654 if {$mergefilelist($id) ne {}} {
2655 showmergediff
2656 }
2657 } else {
2658 contmergediff {}
2659 }
2660 }
2661
2662 proc findgca {ids} {
2663 global env
2664 set gca {}
2665 foreach id $ids {
2666 if {$gca eq {}} {
2667 set gca $id
2668 } else {
2669 if {[catch {
2670 set gca [exec $env(HG) --config ui.report_untrusted=false debug-merge-base $gca $id]
2671 } err]} {
2672 return {}
2673 }
2674 }
2675 }
2676 return $gca
2677 }
2678
2679 proc contmergediff {ids} {
2680 global diffmergeid diffpindex parents nparents diffmergegca
2681 global treediffs mergefilelist diffids treepending
2682
2683 # diff the child against each of the parents, and diff
2684 # each of the parents against the GCA.
2685 while 1 {
2686 if {[lindex $ids 0] == $diffmergeid && $diffmergegca ne {}} {
2687 set ids [list [lindex $ids 1] $diffmergegca]
2688 } else {
2689 if {[incr diffpindex] >= $nparents($diffmergeid)} break
2690 set p [lindex $parents($diffmergeid) $diffpindex]
2691 set ids [list $diffmergeid $p]
2692 }
2693 if {![info exists treediffs($ids)]} {
2694 set diffids $ids
2695 if {![info exists treepending]} {
2696 gettreediffs $ids
2697 }
2698 return
2699 }
2700 }
2701
2702 # If a file in some parent is different from the child and also
2703 # different from the GCA, then it's interesting.
2704 # If we don't have a GCA, then a file is interesting if it is
2705 # different from the child in all the parents.
2706 if {$diffmergegca ne {}} {
2707 set files {}
2708 foreach p $parents($diffmergeid) {
2709 set gcadiffs $treediffs([list $p $diffmergegca])
2710 foreach f $treediffs([list $diffmergeid $p]) {
2711 if {[lsearch -exact $files $f] < 0
2712 && [lsearch -exact $gcadiffs $f] >= 0} {
2713 lappend files $f
2714 }
2715 }
2716 }
2717 set files [lsort $files]
2718 } else {
2719 set p [lindex $parents($diffmergeid) 0]
2720 set files $treediffs([list $diffmergeid $p])
2721 for {set i 1} {$i < $nparents($diffmergeid) && $files ne {}} {incr i} {
2722 set p [lindex $parents($diffmergeid) $i]
2723 set df $treediffs([list $diffmergeid $p])
2724 set nf {}
2725 foreach f $files {
2726 if {[lsearch -exact $df $f] >= 0} {
2727 lappend nf $f
2728 }
2729 }
2730 set files $nf
2731 }
2732 }
2733
2734 set mergefilelist($diffmergeid) $files
2735 if {$files ne {}} {
2736 showmergediff
2737 }
2738 }
2739
2740 proc showmergediff {} {
2741 global cflist diffmergeid mergefilelist parents
2742 global diffopts diffinhunk currentfile currenthunk filelines
2743 global diffblocked groupfilelast mergefds groupfilenum grouphunks
2744 global env
2745
2746 set files $mergefilelist($diffmergeid)
2747 foreach f $files {
2748 $cflist insert end $f
2749 }
2750 set env(GIT_DIFF_OPTS) $diffopts
2751 set flist {}
2752 catch {unset currentfile}
2753 catch {unset currenthunk}
2754 catch {unset filelines}
2755 catch {unset groupfilenum}
2756 catch {unset grouphunks}
2757 set groupfilelast -1
2758 foreach p $parents($diffmergeid) {
2759 set cmd [list | $env(HG) --config ui.report_untrusted=false debug-diff-tree -p $p $diffmergeid]
2760 set cmd [concat $cmd $mergefilelist($diffmergeid)]
2761 if {[catch {set f [open $cmd r]} err]} {
2762 error_popup "Error getting diffs: $err"
2763 foreach f $flist {
2764 catch {close $f}
2765 }
2766 return
2767 }
2768 lappend flist $f
2769 set ids [list $diffmergeid $p]
2770 set mergefds($ids) $f
2771 set diffinhunk($ids) 0
2772 set diffblocked($ids) 0
2773 fconfigure $f -blocking 0
2774 fileevent $f readable [list getmergediffline $f $ids $diffmergeid]
2775 }
2776 }
2777
2778 proc getmergediffline {f ids id} {
2779 global diffmergeid diffinhunk diffoldlines diffnewlines
2780 global currentfile currenthunk
2781 global diffoldstart diffnewstart diffoldlno diffnewlno
2782 global diffblocked mergefilelist
2783 global noldlines nnewlines difflcounts filelines
2784
2785 set n [gets $f line]
2786 if {$n < 0} {
2787 if {![eof $f]} return
2788 }
2789
2790 if {!([info exists diffmergeid] && $diffmergeid == $id)} {
2791 if {$n < 0} {
2792 close $f
2793 }
2794 return
2795 }
2796
2797 if {$diffinhunk($ids) != 0} {
2798 set fi $currentfile($ids)
2799 if {$n > 0 && [regexp {^[-+ \\]} $line match]} {
2800 # continuing an existing hunk
2801 set line [string range $line 1 end]
2802 set p [lindex $ids 1]
2803 if {$match eq "-" || $match eq " "} {
2804 set filelines($p,$fi,$diffoldlno($ids)) $line
2805 incr diffoldlno($ids)
2806 }
2807 if {$match eq "+" || $match eq " "} {
2808 set filelines($id,$fi,$diffnewlno($ids)) $line
2809 incr diffnewlno($ids)
2810 }
2811 if {$match eq " "} {
2812 if {$diffinhunk($ids) == 2} {
2813 lappend difflcounts($ids) \
2814 [list $noldlines($ids) $nnewlines($ids)]
2815 set noldlines($ids) 0
2816 set diffinhunk($ids) 1
2817 }
2818 incr noldlines($ids)
2819 } elseif {$match eq "-" || $match eq "+"} {
2820 if {$diffinhunk($ids) == 1} {
2821 lappend difflcounts($ids) [list $noldlines($ids)]
2822 set noldlines($ids) 0
2823 set nnewlines($ids) 0
2824 set diffinhunk($ids) 2
2825 }
2826 if {$match eq "-"} {
2827 incr noldlines($ids)
2828 } else {
2829 incr nnewlines($ids)
2830 }
2831 }
2832 # and if it's \ No newline at end of line, then what?
2833 return
2834 }
2835 # end of a hunk
2836 if {$diffinhunk($ids) == 1 && $noldlines($ids) != 0} {
2837 lappend difflcounts($ids) [list $noldlines($ids)]
2838 } elseif {$diffinhunk($ids) == 2
2839 && ($noldlines($ids) != 0 || $nnewlines($ids) != 0)} {
2840 lappend difflcounts($ids) [list $noldlines($ids) $nnewlines($ids)]
2841 }
2842 set currenthunk($ids) [list $currentfile($ids) \
2843 $diffoldstart($ids) $diffnewstart($ids) \
2844 $diffoldlno($ids) $diffnewlno($ids) \
2845 $difflcounts($ids)]
2846 set diffinhunk($ids) 0
2847 # -1 = need to block, 0 = unblocked, 1 = is blocked
2848 set diffblocked($ids) -1
2849 processhunks
2850 if {$diffblocked($ids) == -1} {
2851 fileevent $f readable {}
2852 set diffblocked($ids) 1
2853 }
2854 }
2855
2856 if {$n < 0} {
2857 # eof
2858 if {!$diffblocked($ids)} {
2859 close $f
2860 set currentfile($ids) [llength $mergefilelist($diffmergeid)]
2861 set currenthunk($ids) [list $currentfile($ids) 0 0 0 0 {}]
2862 processhunks
2863 }
2864 } elseif {[regexp {^diff --git a/(.*) b/} $line match fname]} {
2865 # start of a new file
2866 set currentfile($ids) \
2867 [lsearch -exact $mergefilelist($diffmergeid) $fname]
2868 } elseif {[regexp {^@@ -([0-9]+),([0-9]+) \+([0-9]+),([0-9]+) @@(.*)} \
2869 $line match f1l f1c f2l f2c rest]} {
2870 if {[info exists currentfile($ids)] && $currentfile($ids) >= 0} {
2871 # start of a new hunk
2872 if {$f1l == 0 && $f1c == 0} {
2873 set f1l 1
2874 }
2875 if {$f2l == 0 && $f2c == 0} {
2876 set f2l 1
2877 }
2878 set diffinhunk($ids) 1
2879 set diffoldstart($ids) $f1l
2880 set diffnewstart($ids) $f2l
2881 set diffoldlno($ids) $f1l
2882 set diffnewlno($ids) $f2l
2883 set difflcounts($ids) {}
2884 set noldlines($ids) 0
2885 set nnewlines($ids) 0
2886 }
2887 }
2888 }
2889
2890 proc processhunks {} {
2891 global diffmergeid parents nparents currenthunk
2892 global mergefilelist diffblocked mergefds
2893 global grouphunks grouplinestart grouplineend groupfilenum
2894
2895 set nfiles [llength $mergefilelist($diffmergeid)]
2896 while 1 {
2897 set fi $nfiles
2898 set lno 0
2899 # look for the earliest hunk
2900 foreach p $parents($diffmergeid) {
2901 set ids [list $diffmergeid $p]
2902 if {![info exists currenthunk($ids)]} return
2903 set i [lindex $currenthunk($ids) 0]
2904 set l [lindex $currenthunk($ids) 2]
2905 if {$i < $fi || ($i == $fi && $l < $lno)} {
2906 set fi $i
2907 set lno $l
2908 set pi $p
2909 }
2910 }
2911
2912 if {$fi < $nfiles} {
2913 set ids [list $diffmergeid $pi]
2914 set hunk $currenthunk($ids)
2915 unset currenthunk($ids)
2916 if {$diffblocked($ids) > 0} {
2917 fileevent $mergefds($ids) readable \
2918 [list getmergediffline $mergefds($ids) $ids $diffmergeid]
2919 }
2920 set diffblocked($ids) 0
2921
2922 if {[info exists groupfilenum] && $groupfilenum == $fi
2923 && $lno <= $grouplineend} {
2924 # add this hunk to the pending group
2925 lappend grouphunks($pi) $hunk
2926 set endln [lindex $hunk 4]
2927 if {$endln > $grouplineend} {
2928 set grouplineend $endln
2929 }
2930 continue
2931 }
2932 }
2933
2934 # succeeding stuff doesn't belong in this group, so
2935 # process the group now
2936 if {[info exists groupfilenum]} {
2937 processgroup
2938 unset groupfilenum
2939 unset grouphunks
2940 }
2941
2942 if {$fi >= $nfiles} break
2943
2944 # start a new group
2945 set groupfilenum $fi
2946 set grouphunks($pi) [list $hunk]
2947 set grouplinestart $lno
2948 set grouplineend [lindex $hunk 4]
2949 }
2950 }
2951
2952 proc processgroup {} {
2953 global groupfilelast groupfilenum difffilestart
2954 global mergefilelist diffmergeid ctext filelines
2955 global parents diffmergeid diffoffset
2956 global grouphunks grouplinestart grouplineend nparents
2957 global mergemax
2958
2959 $ctext conf -state normal
2960 set id $diffmergeid
2961 set f $groupfilenum
2962 if {$groupfilelast != $f} {
2963 $ctext insert end "\n"
2964 set here [$ctext index "end - 1c"]
2965 set difffilestart($f) $here
2966 set mark fmark.[expr {$f + 1}]
2967 $ctext mark set $mark $here
2968 $ctext mark gravity $mark left
2969 set header [lindex $mergefilelist($id) $f]
2970 set l [expr {(78 - [string length $header]) / 2}]
2971 set pad [string range "----------------------------------------" 1 $l]
2972 $ctext insert end "$pad $header $pad\n" filesep
2973 set groupfilelast $f
2974 foreach p $parents($id) {
2975 set diffoffset($p) 0
2976 }
2977 }
2978
2979 $ctext insert end "@@" msep
2980 set nlines [expr {$grouplineend - $grouplinestart}]
2981 set events {}
2982 set pnum 0
2983 foreach p $parents($id) {
2984 set startline [expr {$grouplinestart + $diffoffset($p)}]
2985 set ol $startline
2986 set nl $grouplinestart
2987 if {[info exists grouphunks($p)]} {
2988 foreach h $grouphunks($p) {
2989 set l [lindex $h 2]
2990 if {$nl < $l} {
2991 for {} {$nl < $l} {incr nl} {
2992 set filelines($p,$f,$ol) $filelines($id,$f,$nl)
2993 incr ol
2994 }
2995 }
2996 foreach chunk [lindex $h 5] {
2997 if {[llength $chunk] == 2} {
2998 set olc [lindex $chunk 0]
2999 set nlc [lindex $chunk 1]
3000 set nnl [expr {$nl + $nlc}]
3001 lappend events [list $nl $nnl $pnum $olc $nlc]
3002 incr ol $olc
3003 set nl $nnl
3004 } else {
3005 incr ol [lindex $chunk 0]
3006 incr nl [lindex $chunk 0]
3007 }
3008 }
3009 }
3010 }
3011 if {$nl < $grouplineend} {
3012 for {} {$nl < $grouplineend} {incr nl} {
3013 set filelines($p,$f,$ol) $filelines($id,$f,$nl)
3014 incr ol
3015 }
3016 }
3017 set nlines [expr {$ol - $startline}]
3018 $ctext insert end " -$startline,$nlines" msep
3019 incr pnum
3020 }
3021
3022 set nlines [expr {$grouplineend - $grouplinestart}]
3023 $ctext insert end " +$grouplinestart,$nlines @@\n" msep
3024
3025 set events [lsort -integer -index 0 $events]
3026 set nevents [llength $events]
3027 set nmerge $nparents($diffmergeid)
3028 set l $grouplinestart
3029 for {set i 0} {$i < $nevents} {set i $j} {
3030 set nl [lindex $events $i 0]
3031 while {$l < $nl} {
3032 $ctext insert end " $filelines($id,$f,$l)\n"
3033 incr l
3034 }
3035 set e [lindex $events $i]
3036 set enl [lindex $e 1]
3037 set j $i
3038 set active {}
3039 while 1 {
3040 set pnum [lindex $e 2]
3041 set olc [lindex $e 3]
3042 set nlc [lindex $e 4]
3043 if {![info exists delta($pnum)]} {
3044 set delta($pnum) [expr {$olc - $nlc}]
3045 lappend active $pnum
3046 } else {
3047 incr delta($pnum) [expr {$olc - $nlc}]
3048 }
3049 if {[incr j] >= $nevents} break
3050 set e [lindex $events $j]
3051 if {[lindex $e 0] >= $enl} break
3052 if {[lindex $e 1] > $enl} {
3053 set enl [lindex $e 1]
3054 }
3055 }
3056 set nlc [expr {$enl - $l}]
3057 set ncol mresult
3058 set bestpn -1
3059 if {[llength $active] == $nmerge - 1} {
3060 # no diff for one of the parents, i.e. it's identical
3061 for {set pnum 0} {$pnum < $nmerge} {incr pnum} {
3062 if {![info exists delta($pnum)]} {
3063 if {$pnum < $mergemax} {
3064 lappend ncol m$pnum
3065 } else {
3066 lappend ncol mmax
3067 }
3068 break
3069 }
3070 }
3071 } elseif {[llength $active] == $nmerge} {
3072 # all parents are different, see if one is very similar
3073 set bestsim 30
3074 for {set pnum 0} {$pnum < $nmerge} {incr pnum} {
3075 set sim [similarity $pnum $l $nlc $f \
3076 [lrange $events $i [expr {$j-1}]]]
3077 if {$sim > $bestsim} {
3078 set bestsim $sim
3079 set bestpn $pnum
3080 }
3081 }
3082 if {$bestpn >= 0} {
3083 lappend ncol m$bestpn
3084 }
3085 }
3086 set pnum -1
3087 foreach p $parents($id) {
3088 incr pnum
3089 if {![info exists delta($pnum)] || $pnum == $bestpn} continue
3090 set olc [expr {$nlc + $delta($pnum)}]
3091 set ol [expr {$l + $diffoffset($p)}]
3092 incr diffoffset($p) $delta($pnum)
3093 unset delta($pnum)
3094 for {} {$olc > 0} {incr olc -1} {
3095 $ctext insert end "-$filelines($p,$f,$ol)\n" m$pnum
3096 incr ol
3097 }
3098 }
3099 set endl [expr {$l + $nlc}]
3100 if {$bestpn >= 0} {
3101 # show this pretty much as a normal diff
3102 set p [lindex $parents($id) $bestpn]
3103 set ol [expr {$l + $diffoffset($p)}]
3104 incr diffoffset($p) $delta($bestpn)
3105 unset delta($bestpn)
3106 for {set k $i} {$k < $j} {incr k} {
3107 set e [lindex $events $k]
3108 if {[lindex $e 2] != $bestpn} continue
3109 set nl [lindex $e 0]
3110 set ol [expr {$ol + $nl - $l}]
3111 for {} {$l < $nl} {incr l} {
3112 $ctext insert end "+$filelines($id,$f,$l)\n" $ncol
3113 }
3114 set c [lindex $e 3]
3115 for {} {$c > 0} {incr c -1} {
3116 $ctext insert end "-$filelines($p,$f,$ol)\n" m$bestpn
3117 incr ol
3118 }
3119 set nl [lindex $e 1]
3120 for {} {$l < $nl} {incr l} {
3121 $ctext insert end "+$filelines($id,$f,$l)\n" mresult
3122 }
3123 }
3124 }
3125 for {} {$l < $endl} {incr l} {
3126 $ctext insert end "+$filelines($id,$f,$l)\n" $ncol
3127 }
3128 }
3129 while {$l < $grouplineend} {
3130 $ctext insert end " $filelines($id,$f,$l)\n"
3131 incr l
3132 }
3133 $ctext conf -state disabled
3134 }
3135
3136 proc similarity {pnum l nlc f events} {
3137 global diffmergeid parents diffoffset filelines
3138
3139 set id $diffmergeid
3140 set p [lindex $parents($id) $pnum]
3141 set ol [expr {$l + $diffoffset($p)}]
3142 set endl [expr {$l + $nlc}]
3143 set same 0
3144 set diff 0
3145 foreach e $events {
3146 if {[lindex $e 2] != $pnum} continue
3147 set nl [lindex $e 0]
3148 set ol [expr {$ol + $nl - $l}]
3149 for {} {$l < $nl} {incr l} {
3150 incr same [string length $filelines($id,$f,$l)]
3151 incr same
3152 }
3153 set oc [lindex $e 3]
3154 for {} {$oc > 0} {incr oc -1} {
3155 incr diff [string length $filelines($p,$f,$ol)]
3156 incr diff
3157 incr ol
3158 }
3159 set nl [lindex $e 1]
3160 for {} {$l < $nl} {incr l} {
3161 incr diff [string length $filelines($id,$f,$l)]
3162 incr diff
3163 }
3164 }
3165 for {} {$l < $endl} {incr l} {
3166 incr same [string length $filelines($id,$f,$l)]
3167 incr same
3168 }
3169 if {$same == 0} {
3170 return 0
3171 }
3172 return [expr {200 * $same / (2 * $same + $diff)}]
3173 }
3174
3175 proc startdiff {ids} {
3176 global treediffs diffids treepending diffmergeid
3177
3178 set diffids $ids
3179 catch {unset diffmergeid}
3180 if {![info exists treediffs($ids)]} {
3181 if {![info exists treepending]} {
3182 gettreediffs $ids
3183 }
3184 } else {
3185 addtocflist $ids
3186 }
3187 }
3188
3189 proc addtocflist {ids} {
3190 global treediffs cflist
3191 foreach f $treediffs($ids) {
3192 $cflist insert end $f
3193 }
3194 getblobdiffs $ids
3195 }
3196
3197 proc gettreediffs {ids} {
3198 global treediff parents treepending env
3199 set treepending $ids
3200 set treediff {}
3201 set id [lindex $ids 0]
3202 set p [lindex $ids 1]
3203 if [catch {set gdtf [open "|{$env(HG)} --config ui.report_untrusted=false debug-diff-tree -r $p $id" r]}] return
3204 fconfigure $gdtf -blocking 0
3205 fileevent $gdtf readable [list gettreediffline $gdtf $ids]
3206 }
3207
3208 proc gettreediffline {gdtf ids} {
3209 global treediff treediffs treepending diffids diffmergeid
3210
3211 set n [gets $gdtf line]
3212 if {$n < 0} {
3213 if {![eof $gdtf]} return
3214 close $gdtf
3215 set treediffs($ids) $treediff
3216 unset treepending
3217 if {$ids != $diffids} {
3218 gettreediffs $diffids
3219 } else {
3220 if {[info exists diffmergeid]} {
3221 contmergediff $ids
3222 } else {
3223 addtocflist $ids
3224 }
3225 }
3226 return
3227 }
3228 set tab1 [expr [string first "\t" $line] + 1]
3229 set tab2 [expr [string first "\t" $line $tab1] - 1]
3230 set file [string range $line $tab1 $tab2]
3231 lappend treediff $file
3232 }
3233
3234 proc getblobdiffs {ids} {
3235 global diffopts blobdifffd diffids env curdifftag curtagstart
3236 global difffilestart nextupdate diffinhdr treediffs
3237
3238 set id [lindex $ids 0]
3239 set p [lindex $ids 1]
3240 set env(GIT_DIFF_OPTS) $diffopts
3241 set cmd [list | $env(HG) --config ui.report_untrusted=false debug-diff-tree -r -p -C $p $id]
3242 if {[catch {set bdf [open $cmd r]} err]} {
3243 puts "error getting diffs: $err"
3244 return
3245 }
3246 set diffinhdr 0
3247 fconfigure $bdf -blocking 0
3248 set blobdifffd($ids) $bdf
3249 set curdifftag Comments
3250 set curtagstart 0.0
3251 catch {unset difffilestart}
3252 fileevent $bdf readable [list getblobdiffline $bdf $diffids]
3253 set nextupdate [expr {[clock clicks -milliseconds] + 100}]
3254 }
3255
3256 proc getblobdiffline {bdf ids} {
3257 global diffids blobdifffd ctext curdifftag curtagstart
3258 global diffnexthead diffnextnote difffilestart
3259 global nextupdate diffinhdr treediffs
3260 global gaudydiff
3261
3262 set n [gets $bdf line]
3263 if {$n < 0} {
3264 if {[eof $bdf]} {
3265 close $bdf
3266 if {$ids == $diffids && $bdf == $blobdifffd($ids)} {
3267 $ctext tag add $curdifftag $curtagstart end
3268 }
3269 }
3270 return
3271 }
3272 if {$ids != $diffids || $bdf != $blobdifffd($ids)} {
3273 return
3274 }
3275 regsub -all "\r" $line "" line
3276 $ctext conf -state normal
3277 if {[regexp {^diff --git a/(.*) b/(.*)} $line match fname newname]} {
3278 # start of a new file
3279 $ctext insert end "\n"
3280 $ctext tag add $curdifftag $curtagstart end
3281 set curtagstart [$ctext index "end - 1c"]
3282 set header $newname
3283 set here [$ctext index "end - 1c"]
3284 set i [lsearch -exact $treediffs($diffids) $fname]
3285 if {$i >= 0} {
3286 set difffilestart($i) $here
3287 incr i
3288 $ctext mark set fmark.$i $here
3289 $ctext mark gravity fmark.$i left
3290 }
3291 if {$newname != $fname} {
3292 set i [lsearch -exact $treediffs($diffids) $newname]
3293 if {$i >= 0} {
3294 set difffilestart($i) $here
3295 incr i
3296 $ctext mark set fmark.$i $here
3297 $ctext mark gravity fmark.$i left
3298 }
3299 }
3300 set curdifftag "f:$fname"
3301 $ctext tag delete $curdifftag
3302 set l [expr {(78 - [string length $header]) / 2}]
3303 set pad [string range "----------------------------------------" 1 $l]
3304 $ctext insert end "$pad $header $pad\n" filesep
3305 set diffinhdr 1
3306 } elseif {[regexp {^(---|\+\+\+) } $line] && $diffinhdr} {
3307 set diffinhdr 1
3308 } elseif {[regexp {^@@ -([0-9]+),([0-9]+) \+([0-9]+),([0-9]+) @@(.*)} \
3309 $line match f1l f1c f2l f2c rest]} {
3310 if {$gaudydiff} {
3311 $ctext insert end "\t" hunksep
3312 $ctext insert end " $f1l " d0 " $f2l " d1
3313 $ctext insert end " $rest \n" hunksep
3314 } else {
3315 $ctext insert end "$line\n" hunksep
3316 }
3317 set diffinhdr 0
3318 } else {
3319 set x [string range $line 0 0]
3320 if {$x == "-" || $x == "+"} {
3321 set tag [expr {$x == "+"}]
3322 if {$gaudydiff} {
3323 set line [string range $line 1 end]
3324 }
3325 $ctext insert end "$line\n" d$tag
3326 } elseif {$x == " "} {
3327 if {$gaudydiff} {
3328 set line [string range $line 1 end]
3329 }
3330 $ctext insert end "$line\n"
3331 } elseif {$diffinhdr || $x == "\\"} {
3332 # e.g. "\ No newline at end of file"
3333 $ctext insert end "$line\n" filesep
3334 } elseif {$line != ""} {
3335 # Something else we don't recognize
3336 if {$curdifftag != "Comments"} {
3337 $ctext insert end "\n"
3338 $ctext tag add $curdifftag $curtagstart end
3339 set curtagstart [$ctext index "end - 1c"]
3340 set curdifftag Comments
3341 }
3342 $ctext insert end "$line\n" filesep
3343 }
3344 }
3345 $ctext conf -state disabled
3346 if {[clock clicks -milliseconds] >= $nextupdate} {
3347 incr nextupdate 100
3348 fileevent $bdf readable {}
3349 update
3350 fileevent $bdf readable "getblobdiffline $bdf {$ids}"
3351 }
3352 }
3353
3354 proc nextfile {} {
3355 global difffilestart ctext
3356 set here [$ctext index @0,0]
3357 for {set i 0} {[info exists difffilestart($i)]} {incr i} {
3358 if {[$ctext compare $difffilestart($i) > $here]} {
3359 if {![info exists pos]
3360 || [$ctext compare $difffilestart($i) < $pos]} {
3361 set pos $difffilestart($i)
3362 }
3363 }
3364 }
3365 if {[info exists pos]} {
3366 $ctext yview $pos
3367 }
3368 }
3369
3370 proc listboxsel {} {
3371 global ctext cflist currentid
3372 if {![info exists currentid]} return
3373 set sel [lsort [$cflist curselection]]
3374 if {$sel eq {}} return
3375 set first [lindex $sel 0]
3376 catch {$ctext yview fmark.$first}
3377 }
3378
3379 proc setcoords {} {
3380 global linespc charspc canvx0 canvy0 mainfont
3381 global xspc1 xspc2 lthickness
3382
3383 set linespc [font metrics $mainfont -linespace]
3384 set charspc [font measure $mainfont "m"]
3385 set canvy0 [expr 3 + 0.5 * $linespc]
3386 set canvx0 [expr 3 + 0.5 * $linespc]
3387 set lthickness [expr {int($linespc / 9) + 1}]
3388 set xspc1(0) $linespc
3389 set xspc2 $linespc
3390 }
3391
3392 proc redisplay {} {
3393 global stopped redisplaying phase
3394 if {$stopped > 1} return
3395 if {$phase == "getcommits"} return
3396 set redisplaying 1
3397 if {$phase == "drawgraph" || $phase == "incrdraw"} {
3398 set stopped 1
3399 } else {
3400 drawgraph
3401 }
3402 }
3403
3404 proc incrfont {inc} {
3405 global mainfont namefont textfont ctext canv phase
3406 global stopped entries curidfont
3407 unmarkmatches
3408 set mainfont [lreplace $mainfont 1 1 [expr {[lindex $mainfont 1] + $inc}]]
3409 set curidfont [lreplace $curidfont 1 1 [expr {[lindex $curidfont 1] + $inc}]]
3410 set namefont [lreplace $namefont 1 1 [expr {[lindex $namefont 1] + $inc}]]
3411 set textfont [lreplace $textfont 1 1 [expr {[lindex $textfont 1] + $inc}]]
3412 setcoords
3413 $ctext conf -font $textfont
3414 $ctext tag conf filesep -font [concat $textfont bold]
3415 foreach e $entries {
3416 $e conf -font $mainfont
3417 }
3418 if {$phase == "getcommits"} {
3419 $canv itemconf textitems -font $mainfont
3420 }
3421 redisplay
3422 }
3423
3424 proc clearsha1 {} {
3425 global sha1entry sha1string
3426 if {[string length $sha1string] == 40} {
3427 $sha1entry delete 0 end
3428 }
3429 }
3430
3431 proc sha1change {n1 n2 op} {
3432 global sha1string currentid sha1but
3433 if {$sha1string == {}
3434 || ([info exists currentid] && $sha1string == $currentid)} {
3435 set state disabled
3436 } else {
3437 set state normal
3438 }
3439 if {[$sha1but cget -state] == $state} return
3440 if {$state == "normal"} {
3441 $sha1but conf -state normal -relief raised -text "Goto: "
3442 } else {
3443 $sha1but conf -state disabled -relief flat -text "SHA1 ID: "
3444 }
3445 }
3446
3447 proc gotocommit {} {
3448 global sha1string currentid idline tagids
3449 global lineid numcommits
3450
3451 if {$sha1string == {}
3452 || ([info exists currentid] && $sha1string == $currentid)} return
3453 if {[info exists tagids($sha1string)]} {
3454 set id $tagids($sha1string)
3455 } else {
3456 set id [string tolower $sha1string]
3457 if {[regexp {^[0-9a-f]{4,39}$} $id]} {
3458 set matches {}
3459 for {set l 0} {$l < $numcommits} {incr l} {
3460 if {[string match $id* $lineid($l)]} {
3461 lappend matches $lineid($l)
3462 }
3463 }
3464 if {$matches ne {}} {
3465 if {[llength $matches] > 1} {
3466 error_popup "Short SHA1 id $id is ambiguous"
3467 return
3468 }
3469 set id [lindex $matches 0]
3470 }
3471 }
3472 }
3473 if {[info exists idline($id)]} {
3474 selectline $idline($id) 1
3475 return
3476 }
3477 if {[regexp {^[0-9a-fA-F]{4,}$} $sha1string]} {
3478 set type "SHA1 id"
3479 } else {
3480 set type "Tag"
3481 }
3482 error_popup "$type $sha1string is not known"
3483 }
3484
3485 proc lineenter {x y id} {
3486 global hoverx hovery hoverid hovertimer
3487 global commitinfo canv
3488
3489 if {![info exists commitinfo($id)]} return
3490 set hoverx $x
3491 set hovery $y
3492 set hoverid $id
3493 if {[info exists hovertimer]} {
3494 after cancel $hovertimer
3495 }
3496 set hovertimer [after 500 linehover]
3497 $canv delete hover
3498 }
3499
3500 proc linemotion {x y id} {
3501 global hoverx hovery hoverid hovertimer
3502
3503 if {[info exists hoverid] && $id == $hoverid} {
3504 set hoverx $x
3505 set hovery $y
3506 if {[info exists hovertimer]} {
3507 after cancel $hovertimer
3508 }
3509 set hovertimer [after 500 linehover]
3510 }
3511 }
3512
3513 proc lineleave {id} {
3514 global hoverid hovertimer canv
3515
3516 if {[info exists hoverid] && $id == $hoverid} {
3517 $canv delete hover
3518 if {[info exists hovertimer]} {
3519 after cancel $hovertimer
3520 unset hovertimer
3521 }
3522 unset hoverid
3523 }
3524 }
3525
3526 proc linehover {} {
3527 global hoverx hovery hoverid hovertimer
3528 global canv linespc lthickness
3529 global commitinfo mainfont
3530
3531 set text [lindex $commitinfo($hoverid) 0]
3532 set ymax [lindex [$canv cget -scrollregion] 3]
3533 if {$ymax == {}} return
3534 set yfrac [lindex [$canv yview] 0]
3535 set x [expr {$hoverx + 2 * $linespc}]
3536 set y [expr {$hovery + $yfrac * $ymax - $linespc / 2}]
3537 set x0 [expr {$x - 2 * $lthickness}]
3538 set y0 [expr {$y - 2 * $lthickness}]
3539 set x1 [expr {$x + [font measure $mainfont $text] + 2 * $lthickness}]
3540 set y1 [expr {$y + $linespc + 2 * $lthickness}]
3541 set t [$canv create rectangle $x0 $y0 $x1 $y1 \
3542 -fill \#ffff80 -outline black -width 1 -tags hover]
3543 $canv raise $t
3544 set t [$canv create text $x $y -anchor nw -text $text -tags hover]
3545 $canv raise $t
3546 }
3547
3548 proc clickisonarrow {id y} {
3549 global mainline mainlinearrow sidelines lthickness
3550
3551 set thresh [expr {2 * $lthickness + 6}]
3552 if {[info exists mainline($id)]} {
3553 if {$mainlinearrow($id) ne "none"} {
3554 if {abs([lindex $mainline($id) 1] - $y) < $thresh} {
3555 return "up"
3556 }
3557 }
3558 }
3559 if {[info exists sidelines($id)]} {
3560 foreach ls $sidelines($id) {
3561 set coords [lindex $ls 0]
3562 set arrow [lindex $ls 2]
3563 if {$arrow eq "first" || $arrow eq "both"} {
3564 if {abs([lindex $coords 1] - $y) < $thresh} {
3565 return "up"
3566 }
3567 }
3568 if {$arrow eq "last" || $arrow eq "both"} {
3569 if {abs([lindex $coords end] - $y) < $thresh} {
3570 return "down"
3571 }
3572 }
3573 }
3574 }
3575 return {}
3576 }
3577
3578 proc arrowjump {id dirn y} {
3579 global mainline sidelines canv
3580
3581 set yt {}
3582 if {$dirn eq "down"} {
3583 if {[info exists mainline($id)]} {
3584 set y1 [lindex $mainline($id) 1]
3585 if {$y1 > $y} {
3586 set yt $y1
3587 }
3588 }
3589 if {[info exists sidelines($id)]} {
3590 foreach ls $sidelines($id) {
3591 set y1 [lindex $ls 0 1]
3592 if {$y1 > $y && ($yt eq {} || $y1 < $yt)} {
3593 set yt $y1
3594 }
3595 }
3596 }
3597 } else {
3598 if {[info exists sidelines($id)]} {
3599 foreach ls $sidelines($id) {
3600 set y1 [lindex $ls 0 end]
3601 if {$y1 < $y && ($yt eq {} || $y1 > $yt)} {
3602 set yt $y1
3603 }
3604 }
3605 }
3606 }
3607 if {$yt eq {}} return
3608 set ymax [lindex [$canv cget -scrollregion] 3]
3609 if {$ymax eq {} || $ymax <= 0} return
3610 set view [$canv yview]
3611 set yspan [expr {[lindex $view 1] - [lindex $view 0]}]
3612 set yfrac [expr {$yt / $ymax - $yspan / 2}]
3613 if {$yfrac < 0} {
3614 set yfrac 0
3615 }
3616 $canv yview moveto $yfrac
3617 }
3618
3619 proc lineclick {x y id isnew} {
3620 global ctext commitinfo children cflist canv thickerline
3621
3622 unmarkmatches
3623 unselectline
3624 normalline
3625 $canv delete hover
3626 # draw this line thicker than normal
3627 drawlines $id 1
3628 set thickerline $id
3629 if {$isnew} {
3630 set ymax [lindex [$canv cget -scrollregion] 3]
3631 if {$ymax eq {}} return
3632 set yfrac [lindex [$canv yview] 0]
3633 set y [expr {$y + $yfrac * $ymax}]
3634 }
3635 set dirn [clickisonarrow $id $y]
3636 if {$dirn ne {}} {
3637 arrowjump $id $dirn $y
3638 return
3639 }
3640
3641 if {$isnew} {
3642 addtohistory [list lineclick $x $y $id 0]
3643 }
3644 # fill the details pane with info about this line
3645 $ctext conf -state normal
3646 $ctext delete 0.0 end
3647 $ctext tag conf link -foreground blue -underline 1
3648 $ctext tag bind link <Enter> { %W configure -cursor hand2 }
3649 $ctext tag bind link <Leave> { %W configure -cursor $curtextcursor }
3650 $ctext insert end "Parent:\t"
3651 $ctext insert end $id [list link link0]
3652 $ctext tag bind link0 <1> [list selbyid $id]
3653 set info $commitinfo($id)
3654 $ctext insert end "\n\t[lindex $info 0]\n"
3655 $ctext insert end "\tUser:\t[lindex $info 1]\n"
3656 $ctext insert end "\tDate:\t[lindex $info 2]\n"
3657 if {[info exists children($id)]} {
3658 $ctext insert end "\nChildren:"
3659 set i 0
3660 foreach child $children($id) {
3661 incr i
3662 set info $commitinfo($child)
3663 $ctext insert end "\n\t"
3664 $ctext insert end $child [list link link$i]
3665 $ctext tag bind link$i <1> [list selbyid $child]
3666 $ctext insert end "\n\t[lindex $info 0]"
3667 $ctext insert end "\n\tUser:\t[lindex $info 1]"
3668 $ctext insert end "\n\tDate:\t[lindex $info 2]\n"
3669 }
3670 }
3671 $ctext conf -state disabled
3672
3673 $cflist delete 0 end
3674 }
3675
3676 proc normalline {} {
3677 global thickerline
3678 if {[info exists thickerline]} {
3679 drawlines $thickerline 0
3680 unset thickerline
3681 }
3682 }
3683
3684 proc selbyid {id} {
3685 global idline
3686 if {[info exists idline($id)]} {
3687 selectline $idline($id) 1
3688 }
3689 }
3690
3691 proc mstime {} {
3692 global startmstime
3693 if {![info exists startmstime]} {
3694 set startmstime [clock clicks -milliseconds]
3695 }
3696 return [format "%.3f" [expr {([clock click -milliseconds] - $startmstime) / 1000.0}]]
3697 }
3698
3699 proc rowmenu {x y id} {
3700 global rowctxmenu idline selectedline rowmenuid hgvdiff
3701
3702 if {![info exists selectedline] || $idline($id) eq $selectedline} {
3703 set state disabled
3704 } else {
3705 set state normal
3706 }
3707 $rowctxmenu entryconfigure 0 -state $state
3708 $rowctxmenu entryconfigure 1 -state $state
3709 $rowctxmenu entryconfigure 2 -state $state
3710 if { $hgvdiff ne "" } {
3711 $rowctxmenu entryconfigure 6 -state $state
3712 }
3713 set rowmenuid $id
3714 tk_popup $rowctxmenu $x $y
3715 }
3716
3717 proc diffvssel {dirn} {
3718 global rowmenuid selectedline lineid
3719
3720 if {![info exists selectedline]} return
3721 if {$dirn} {
3722 set oldid $lineid($selectedline)
3723 set newid $rowmenuid
3724 } else {
3725 set oldid $rowmenuid
3726 set newid $lineid($selectedline)
3727 }
3728 addtohistory [list doseldiff $oldid $newid]
3729 doseldiff $oldid $newid
3730 }
3731
3732 proc doseldiff {oldid newid} {
3733 global ctext cflist
3734 global commitinfo
3735
3736 $ctext conf -state normal
3737 $ctext delete 0.0 end
3738 $ctext mark set fmark.0 0.0
3739 $ctext mark gravity fmark.0 left
3740 $cflist delete 0 end
3741 $cflist insert end "Top"
3742 $ctext insert end "From "
3743 $ctext tag conf link -foreground blue -underline 1
3744 $ctext tag bind link <Enter> { %W configure -cursor hand2 }
3745 $ctext tag bind link <Leave> { %W configure -cursor $curtextcursor }
3746 $ctext tag bind link0 <1> [list selbyid $oldid]
3747 $ctext insert end $oldid [list link link0]
3748 $ctext insert end "\n "
3749 $ctext insert end [lindex $commitinfo($oldid) 0]
3750 $ctext insert end "\n\nTo "
3751 $ctext tag bind link1 <1> [list selbyid $newid]
3752 $ctext insert end $newid [list link link1]
3753 $ctext insert end "\n "
3754 $ctext insert end [lindex $commitinfo($newid) 0]
3755 $ctext insert end "\n"
3756 $ctext conf -state disabled
3757 $ctext tag delete Comments
3758 $ctext tag remove found 1.0 end
3759 startdiff [list $newid $oldid]
3760 }
3761
3762 proc mkpatch {} {
3763 global rowmenuid currentid commitinfo patchtop patchnum
3764
3765 if {![info exists currentid]} return
3766 set oldid $currentid
3767 set oldhead [lindex $commitinfo($oldid) 0]
3768 set newid $rowmenuid
3769 set newhead [lindex $commitinfo($newid) 0]
3770 set top .patch
3771 set patchtop $top
3772 catch {destroy $top}
3773 toplevel $top
3774 ttk::label $top.from -text "From:"
3775 ttk::entry $top.fromsha1 -width 40
3776 $top.fromsha1 insert 0 $oldid
3777 $top.fromsha1 conf -state readonly
3778 grid $top.from $top.fromsha1 -sticky w -pady {10 0}
3779 ttk::entry $top.fromhead -width 60
3780 $top.fromhead insert 0 $oldhead
3781 $top.fromhead conf -state readonly
3782 grid x $top.fromhead -sticky w
3783 ttk::label $top.to -text "To:"
3784 ttk::entry $top.tosha1 -width 40
3785 $top.tosha1 insert 0 $newid
3786 $top.tosha1 conf -state readonly
3787 grid $top.to $top.tosha1 -sticky w
3788 ttk::entry $top.tohead -width 60
3789 $top.tohead insert 0 $newhead
3790 $top.tohead conf -state readonly
3791 grid x $top.tohead -sticky w
3792 ttk::button $top.rev -text "Reverse" -command mkpatchrev
3793 grid $top.rev x -pady 10
3794 ttk::label $top.flab -text "Output file:"
3795 ttk::entry $top.fname -width 60
3796 $top.fname insert 0 [file normalize "patch$patchnum.patch"]
3797 incr patchnum
3798 grid $top.flab $top.fname -sticky w
3799 ttk::frame $top.buts
3800 ttk::button $top.buts.gen -text "Generate" -command mkpatchgo
3801 ttk::button $top.buts.can -text "Cancel" -command mkpatchcan
3802 grid $top.buts.gen $top.buts.can
3803 grid columnconfigure $top.buts 0 -weight 1 -uniform a
3804 grid columnconfigure $top.buts 1 -weight 1 -uniform a
3805 grid $top.buts - -pady 10 -sticky ew
3806 focus $top.fname
3807 popupify $top
3808 wm title $top "Generate a patch"
3809 }
3810
3811 proc mkpatchrev {} {
3812 global patchtop
3813
3814 set oldid [$patchtop.fromsha1 get]
3815 set oldhead [$patchtop.fromhead get]
3816 set newid [$patchtop.tosha1 get]
3817 set newhead [$patchtop.tohead get]
3818 foreach e [list fromsha1 fromhead tosha1 tohead] \
3819 v [list $newid $newhead $oldid $oldhead] {
3820 $patchtop.$e conf -state normal
3821 $patchtop.$e delete 0 end
3822 $patchtop.$e insert 0 $v
3823 $patchtop.$e conf -state readonly
3824 }
3825 }
3826
3827 proc mkpatchgo {} {
3828 global patchtop env
3829
3830 set oldid [$patchtop.fromsha1 get]
3831 set newid [$patchtop.tosha1 get]
3832 set fname [$patchtop.fname get]
3833 if {[catch {exec $env(HG) --config ui.report_untrusted=false debug-diff-tree -p $oldid $newid >$fname &} err]} {
3834 error_popup "Error creating patch: $err"
3835 }
3836 catch {destroy $patchtop}
3837 unset patchtop
3838 }
3839
3840 proc mkpatchcan {} {
3841 global patchtop
3842
3843 catch {destroy $patchtop}
3844 unset patchtop
3845 }
3846
3847 proc mktag {} {
3848 global rowmenuid mktagtop commitinfo
3849
3850 set top .maketag
3851 set mktagtop $top
3852 catch {destroy $top}
3853 toplevel $top
3854 ttk::label $top.id -text "ID:"
3855 ttk::entry $top.sha1 -width 40
3856 $top.sha1 insert 0 $rowmenuid
3857 $top.sha1 conf -state readonly
3858 grid $top.id $top.sha1 -sticky w -pady {10 0}
3859 ttk::entry $top.head -width 60
3860 $top.head insert 0 [lindex $commitinfo($rowmenuid) 0]
3861 $top.head conf -state readonly
3862 grid x $top.head -sticky w
3863 ttk::label $top.tlab -text "Tag name:"
3864 ttk::entry $top.tag -width 60
3865 grid $top.tlab $top.tag -sticky w
3866 ttk::frame $top.buts
3867 ttk::button $top.buts.gen -text "Create" -command mktaggo
3868 ttk::button $top.buts.can -text "Cancel" -command mktagcan
3869 grid $top.buts.gen $top.buts.can
3870 grid columnconfigure $top.buts 0 -weight 1 -uniform a
3871 grid columnconfigure $top.buts 1 -weight 1 -uniform a
3872 grid $top.buts - -pady 10 -sticky ew
3873 focus $top.tag
3874 popupify $top
3875 wm title $top "Create a tag"
3876 }
3877
3878 proc domktag {} {
3879 global mktagtop env tagids idtags
3880
3881 set id [$mktagtop.sha1 get]
3882 set tag [$mktagtop.tag get]
3883 if {$tag == {}} {
3884 error_popup "No tag name specified"
3885 return
3886 }
3887 if {[info exists tagids($tag)]} {
3888 error_popup "Tag \"$tag\" already exists"
3889 return
3890 }
3891 if {[catch {
3892 set out [exec $env(HG) --config ui.report_untrusted=false tag -r $id $tag]
3893 } err]} {
3894 error_popup "Error creating tag: $err"
3895 return
3896 }
3897
3898 set tagids($tag) $id
3899 lappend idtags($id) $tag
3900 redrawtags $id
3901 }
3902
3903 proc redrawtags {id} {
3904 global canv linehtag idline idpos selectedline
3905
3906 if {![info exists idline($id)]} return
3907 $canv delete tag.$id
3908 set xt [eval drawtags $id $idpos($id)]
3909 $canv coords $linehtag($idline($id)) $xt [lindex $idpos($id) 2]
3910 if {[info exists selectedline] && $selectedline == $idline($id)} {
3911 selectline $selectedline 0
3912 }
3913 }
3914
3915 proc mktagcan {} {
3916 global mktagtop
3917
3918 catch {destroy $mktagtop}
3919 unset mktagtop
3920 }
3921
3922 proc mktaggo {} {
3923 domktag
3924 mktagcan
3925 }
3926
3927 proc writecommit {} {
3928 global rowmenuid wrcomtop commitinfo
3929
3930 set top .writecommit
3931 set wrcomtop $top
3932 catch {destroy $top}
3933 toplevel $top
3934 ttk::label $top.id -text "ID:"
3935 ttk::entry $top.sha1 -width 40
3936 $top.sha1 insert 0 $rowmenuid
3937 $top.sha1 conf -state readonly
3938 grid $top.id $top.sha1 -sticky w -pady {10 0}
3939 ttk::entry $top.head -width 60
3940 $top.head insert 0 [lindex $commitinfo($rowmenuid) 0]
3941 $top.head conf -state readonly
3942 grid x $top.head -sticky w
3943 ttk::label $top.flab -text "Output file:"
3944 ttk::entry $top.fname -width 60
3945 $top.fname insert 0 [file normalize "commit-[string range $rowmenuid 0 6].diff"]
3946 grid $top.flab $top.fname -sticky w
3947 ttk::frame $top.buts
3948 ttk::button $top.buts.gen -text "Write" -command wrcomgo
3949 ttk::button $top.buts.can -text "Cancel" -command wrcomcan
3950 grid $top.buts.gen $top.buts.can
3951 grid columnconfigure $top.buts 0 -weight 1 -uniform a
3952 grid columnconfigure $top.buts 1 -weight 1 -uniform a
3953 grid $top.buts - -pady 10 -sticky ew
3954 focus $top.fname
3955 popupify $top
3956 wm title $top "Write commit to a file"
3957 }
3958
3959 proc wrcomgo {} {
3960 global wrcomtop
3961
3962 set id [$wrcomtop.sha1 get]
3963 set fname [$wrcomtop.fname get]
3964 if {[catch {exec $::env(HG) --config ui.report_untrusted=false export --git -o [string map {% %%} $fname] $id} err]} {
3965 error_popup "Error writing commit: $err"
3966 }
3967 catch {destroy $wrcomtop}
3968 unset wrcomtop
3969 }
3970
3971 proc wrcomcan {} {
3972 global wrcomtop
3973
3974 catch {destroy $wrcomtop}
3975 unset wrcomtop
3976 }
3977
3978 proc listrefs {id} {
3979 global idtags idheads idotherrefs idbookmarks
3980
3981 set w {}
3982 if {[info exists idbookmarks($id)]} {
3983 set w $idbookmarks($id)
3984 }
3985 set x {}
3986 if {[info exists idtags($id)]} {
3987 set x $idtags($id)
3988 }
3989 set y {}
3990 if {[info exists idheads($id)]} {
3991 set y $idheads($id)
3992 }
3993 set z {}
3994 if {[info exists idotherrefs($id)]} {
3995 set z $idotherrefs($id)
3996 }
3997 return [list $w $x $y $z]
3998 }
3999
4000 proc rereadrefs {} {
4001 global idbookmarks idtags idheads idotherrefs
4002 global bookmarkids tagids headids otherrefids
4003
4004 set refids [concat [array names idtags] \
4005 [array names idheads] [array names idotherrefs] \
4006 [array names idbookmarks]]
4007 foreach id $refids {
4008 if {![info exists ref($id)]} {
4009 set ref($id) [listrefs $id]
4010 }
4011 }
4012 foreach v {tagids idtags headids idheads otherrefids idotherrefs \
4013 bookmarkids idbookmarks} {
4014 catch {unset $v}
4015 }
4016 readrefs
4017 set refids [lsort -unique [concat $refids [array names idtags] \
4018 [array names idheads] [array names idotherrefs] \
4019 [array names idbookmarks]]]
4020 foreach id $refids {
4021 set v [listrefs $id]
4022 if {![info exists ref($id)] || $ref($id) != $v} {
4023 redrawtags $id
4024 }
4025 }
4026 }
4027
4028 proc vdiff {withparent} {
4029 global env rowmenuid selectedline lineid hgvdiff
4030
4031 if {![info exists rowmenuid]} return
4032 set curid $rowmenuid
4033
4034 if {$withparent} {
4035 set parents [exec $env(HG) --config ui.report_untrusted=false parents --rev $curid --template "{node}\n"]
4036 set firstparent [lindex [split $parents "\n"] 0]
4037 set otherid $firstparent
4038 } else {
4039 if {![info exists selectedline]} return
4040 set otherid $lineid($selectedline)
4041 }
4042 set range "$otherid:$curid"
4043 if {[catch {exec $env(HG) --config ui.report_untrusted=false $hgvdiff -r $range} err]} {
4044 # Ignore errors, this is just visualization
4045 }
4046 }
4047
4048 proc showtag {tag isnew} {
4049 global ctext cflist tagcontents tagids linknum
4050
4051 if {$isnew} {
4052 addtohistory [list showtag $tag 0]
4053 }
4054 $ctext conf -state normal
4055 $ctext delete 0.0 end
4056 set linknum 0
4057 if {[info exists tagcontents($tag)]} {
4058 set text $tagcontents($tag)
4059 } else {
4060 set text "Tag: $tag\nId: $tagids($tag)"
4061 }
4062 appendwithlinks $text
4063 $ctext conf -state disabled
4064 $cflist delete 0 end
4065 }
4066
4067 proc doquit {} {
4068 global stopped
4069 set stopped 100
4070 destroy .
4071 }
4072
4073 proc getconfig {} {
4074 global env
4075 set config {}
4076
4077 set lines [exec $env(HG) debugconfig]
4078 foreach line [split $lines \n] {
4079 set line [string trimright $line \r]
4080 if {[string match hgk.* $line]} {
4081 regexp {(.*)=(.*)} $line - k v
4082 lappend config $k $v
4083 }
4084 }
4085 return $config
4086 }
4087
4088 # defaults...
4089 set datemode 0
4090 set boldnames 0
4091 set diffopts "-U 5 -p"
4092
4093 set mainfont {Helvetica 9}
4094 set curidfont {}
4095 set textfont {Courier 9}
4096 set findmergefiles 0
4097 set gaudydiff 0
4098 set maxgraphpct 50
4099 set maxwidth 16
4100
4101 set colors {green red blue magenta darkgrey brown orange}
4102 set authorcolors {
4103 black blue deeppink mediumorchid blue burlywood4 goldenrod slateblue red2 navy dimgrey
4104 }
4105 set bgcolor white
4106
4107 # This color should probably be some system color (provided by tk),
4108 # but as the bgcolor has always been set to white, I choose to ignore
4109 set fgcolor black
4110 set diffaddcolor "#00a000"
4111 set diffremcolor red
4112 set diffmerge1color red
4113 set diffmerge2color blue
4114 set hunksepcolor blue
4115
4116 catch {source ~/.hgk}
4117
4118 if {$curidfont == ""} { # initialize late based on current mainfont
4119 set curidfont "$mainfont bold italic underline"
4120 }
4121
4122 set namefont $mainfont
4123 if {$boldnames} {
4124 lappend namefont bold
4125 }
4126
4127 set revtreeargs {}
4128 foreach arg $argv {
4129 switch -regexp -- $arg {
4130 "^$" { }
4131 "^-b" { set boldnames 1 }
4132 "^-d" { set datemode 1 }
4133 default {
4134 lappend revtreeargs $arg
4135 }
4136 }
4137 }
4138
4139 set history {}
4140 set historyindex 0
4141
4142 set stopped 0
4143 set redisplaying 0
4144 set stuffsaved 0
4145 set patchnum 0
4146
4147 set config(hgk.vdiff) ""
4148 array set config [getconfig]
4149 set hgvdiff $config(hgk.vdiff)
4150 setcoords
4151 makewindow
4152 readrefs
4153 set hgroot [exec $env(HG) root]
4154 wm title . "hgk $hgroot"
4155 getcommits $revtreeargs