Merge branch 'master' of git://repo.or.cz/alt-git
[git/mingw.git] / gitk-git / gitk
blob0b71039e20f7a84a4a790afa2e0e0889cb9e4e1e
1 #!/bin/sh
2 # Tcl ignores the next line -*- tcl -*- \
3 exec wish "$0" -- "$@"
5 # Copyright © 2005-2014 Paul Mackerras. All rights reserved.
6 # This program is free software; it may be used, copied, modified
7 # and distributed under the terms of the GNU General Public Licence,
8 # either version 2, or (at your option) any later version.
10 package require Tk
12 proc hasworktree {} {
13 return [expr {[exec git rev-parse --is-bare-repository] == "false" &&
14 [exec git rev-parse --is-inside-git-dir] == "false"}]
17 proc reponame {} {
18 global gitdir
19 set n [file normalize $gitdir]
20 if {[string match "*/.git" $n]} {
21 set n [string range $n 0 end-5]
23 return [file tail $n]
26 proc gitworktree {} {
27 variable _gitworktree
28 if {[info exists _gitworktree]} {
29 return $_gitworktree
31 # v1.7.0 introduced --show-toplevel to return the canonical work-tree
32 if {[catch {set _gitworktree [exec git rev-parse --show-toplevel]}]} {
33 # try to set work tree from environment, core.worktree or use
34 # cdup to obtain a relative path to the top of the worktree. If
35 # run from the top, the ./ prefix ensures normalize expands pwd.
36 if {[catch { set _gitworktree $env(GIT_WORK_TREE) }]} {
37 catch {set _gitworktree [exec git config --get core.worktree]}
38 if {$_gitworktree eq ""} {
39 set _gitworktree [file normalize ./[exec git rev-parse --show-cdup]]
43 return $_gitworktree
46 # A simple scheduler for compute-intensive stuff.
47 # The aim is to make sure that event handlers for GUI actions can
48 # run at least every 50-100 ms. Unfortunately fileevent handlers are
49 # run before X event handlers, so reading from a fast source can
50 # make the GUI completely unresponsive.
51 proc run args {
52 global isonrunq runq currunq
54 set script $args
55 if {[info exists isonrunq($script)]} return
56 if {$runq eq {} && ![info exists currunq]} {
57 after idle dorunq
59 lappend runq [list {} $script]
60 set isonrunq($script) 1
63 proc filerun {fd script} {
64 fileevent $fd readable [list filereadable $fd $script]
67 proc filereadable {fd script} {
68 global runq currunq
70 fileevent $fd readable {}
71 if {$runq eq {} && ![info exists currunq]} {
72 after idle dorunq
74 lappend runq [list $fd $script]
77 proc nukefile {fd} {
78 global runq
80 for {set i 0} {$i < [llength $runq]} {} {
81 if {[lindex $runq $i 0] eq $fd} {
82 set runq [lreplace $runq $i $i]
83 } else {
84 incr i
89 proc dorunq {} {
90 global isonrunq runq currunq
92 set tstart [clock clicks -milliseconds]
93 set t0 $tstart
94 while {[llength $runq] > 0} {
95 set fd [lindex $runq 0 0]
96 set script [lindex $runq 0 1]
97 set currunq [lindex $runq 0]
98 set runq [lrange $runq 1 end]
99 set repeat [eval $script]
100 unset currunq
101 set t1 [clock clicks -milliseconds]
102 set t [expr {$t1 - $t0}]
103 if {$repeat ne {} && $repeat} {
104 if {$fd eq {} || $repeat == 2} {
105 # script returns 1 if it wants to be readded
106 # file readers return 2 if they could do more straight away
107 lappend runq [list $fd $script]
108 } else {
109 fileevent $fd readable [list filereadable $fd $script]
111 } elseif {$fd eq {}} {
112 unset isonrunq($script)
114 set t0 $t1
115 if {$t1 - $tstart >= 80} break
117 if {$runq ne {}} {
118 after idle dorunq
122 proc reg_instance {fd} {
123 global commfd leftover loginstance
125 set i [incr loginstance]
126 set commfd($i) $fd
127 set leftover($i) {}
128 return $i
131 proc unmerged_files {files} {
132 global nr_unmerged
134 # find the list of unmerged files
135 set mlist {}
136 set nr_unmerged 0
137 if {[catch {
138 set fd [open "| git ls-files -u" r]
139 } err]} {
140 show_error {} . "[mc "Couldn't get list of unmerged files:"] $err"
141 exit 1
143 while {[gets $fd line] >= 0} {
144 set i [string first "\t" $line]
145 if {$i < 0} continue
146 set fname [string range $line [expr {$i+1}] end]
147 if {[lsearch -exact $mlist $fname] >= 0} continue
148 incr nr_unmerged
149 if {$files eq {} || [path_filter $files $fname]} {
150 lappend mlist $fname
153 catch {close $fd}
154 return $mlist
157 proc parseviewargs {n arglist} {
158 global vdatemode vmergeonly vflags vdflags vrevs vfiltered vorigargs env
159 global vinlinediff
160 global worddiff git_version
162 set vdatemode($n) 0
163 set vmergeonly($n) 0
164 set vinlinediff($n) 0
165 set glflags {}
166 set diffargs {}
167 set nextisval 0
168 set revargs {}
169 set origargs $arglist
170 set allknown 1
171 set filtered 0
172 set i -1
173 foreach arg $arglist {
174 incr i
175 if {$nextisval} {
176 lappend glflags $arg
177 set nextisval 0
178 continue
180 switch -glob -- $arg {
181 "-d" -
182 "--date-order" {
183 set vdatemode($n) 1
184 # remove from origargs in case we hit an unknown option
185 set origargs [lreplace $origargs $i $i]
186 incr i -1
188 "-[puabwcrRBMC]" -
189 "--no-renames" - "--full-index" - "--binary" - "--abbrev=*" -
190 "--find-copies-harder" - "-l*" - "--ext-diff" - "--no-ext-diff" -
191 "--src-prefix=*" - "--dst-prefix=*" - "--no-prefix" -
192 "-O*" - "--text" - "--full-diff" - "--ignore-space-at-eol" -
193 "--ignore-space-change" - "-U*" - "--unified=*" {
194 # These request or affect diff output, which we don't want.
195 # Some could be used to set our defaults for diff display.
196 lappend diffargs $arg
198 "--raw" - "--patch-with-raw" - "--patch-with-stat" -
199 "--name-only" - "--name-status" - "--color" -
200 "--log-size" - "--pretty=*" - "--decorate" - "--abbrev-commit" -
201 "--cc" - "-z" - "--header" - "--parents" - "--boundary" -
202 "--no-color" - "-g" - "--walk-reflogs" - "--no-walk" -
203 "--timestamp" - "relative-date" - "--date=*" - "--stdin" -
204 "--objects" - "--objects-edge" - "--reverse" {
205 # These cause our parsing of git log's output to fail, or else
206 # they're options we want to set ourselves, so ignore them.
208 "--color-words*" - "--word-diff=color" {
209 # These trigger a word diff in the console interface,
210 # so help the user by enabling our own support
211 if {[package vcompare $git_version "1.7.2"] >= 0} {
212 set worddiff [mc "Color words"]
215 "--word-diff*" {
216 if {[package vcompare $git_version "1.7.2"] >= 0} {
217 set worddiff [mc "Markup words"]
220 "--stat=*" - "--numstat" - "--shortstat" - "--summary" -
221 "--check" - "--exit-code" - "--quiet" - "--topo-order" -
222 "--full-history" - "--dense" - "--sparse" -
223 "--follow" - "--left-right" - "--encoding=*" {
224 # These are harmless, and some are even useful
225 lappend glflags $arg
227 "--diff-filter=*" - "--no-merges" - "--unpacked" -
228 "--max-count=*" - "--skip=*" - "--since=*" - "--after=*" -
229 "--until=*" - "--before=*" - "--max-age=*" - "--min-age=*" -
230 "--author=*" - "--committer=*" - "--grep=*" - "-[iE]" -
231 "--remove-empty" - "--first-parent" - "--cherry-pick" -
232 "-S*" - "-G*" - "--pickaxe-all" - "--pickaxe-regex" -
233 "--simplify-by-decoration" {
234 # These mean that we get a subset of the commits
235 set filtered 1
236 lappend glflags $arg
238 "-L*" {
239 # Line-log with 'stuck' argument (unstuck form is
240 # not supported)
241 set filtered 1
242 set vinlinediff($n) 1
243 set allknown 0
244 lappend glflags $arg
246 "-n" {
247 # This appears to be the only one that has a value as a
248 # separate word following it
249 set filtered 1
250 set nextisval 1
251 lappend glflags $arg
253 "--not" - "--all" {
254 lappend revargs $arg
256 "--merge" {
257 set vmergeonly($n) 1
258 # git rev-parse doesn't understand --merge
259 lappend revargs --gitk-symmetric-diff-marker MERGE_HEAD...HEAD
261 "--no-replace-objects" {
262 set env(GIT_NO_REPLACE_OBJECTS) "1"
264 "-*" {
265 # Other flag arguments including -<n>
266 if {[string is digit -strict [string range $arg 1 end]]} {
267 set filtered 1
268 } else {
269 # a flag argument that we don't recognize;
270 # that means we can't optimize
271 set allknown 0
273 lappend glflags $arg
275 default {
276 # Non-flag arguments specify commits or ranges of commits
277 if {[string match "*...*" $arg]} {
278 lappend revargs --gitk-symmetric-diff-marker
280 lappend revargs $arg
284 set vdflags($n) $diffargs
285 set vflags($n) $glflags
286 set vrevs($n) $revargs
287 set vfiltered($n) $filtered
288 set vorigargs($n) $origargs
289 return $allknown
292 proc parseviewrevs {view revs} {
293 global vposids vnegids
295 if {$revs eq {}} {
296 set revs HEAD
298 if {[catch {set ids [eval exec git rev-parse $revs]} err]} {
299 # we get stdout followed by stderr in $err
300 # for an unknown rev, git rev-parse echoes it and then errors out
301 set errlines [split $err "\n"]
302 set badrev {}
303 for {set l 0} {$l < [llength $errlines]} {incr l} {
304 set line [lindex $errlines $l]
305 if {!([string length $line] == 40 && [string is xdigit $line])} {
306 if {[string match "fatal:*" $line]} {
307 if {[string match "fatal: ambiguous argument*" $line]
308 && $badrev ne {}} {
309 if {[llength $badrev] == 1} {
310 set err "unknown revision $badrev"
311 } else {
312 set err "unknown revisions: [join $badrev ", "]"
314 } else {
315 set err [join [lrange $errlines $l end] "\n"]
317 break
319 lappend badrev $line
322 error_popup "[mc "Error parsing revisions:"] $err"
323 return {}
325 set ret {}
326 set pos {}
327 set neg {}
328 set sdm 0
329 foreach id [split $ids "\n"] {
330 if {$id eq "--gitk-symmetric-diff-marker"} {
331 set sdm 4
332 } elseif {[string match "^*" $id]} {
333 if {$sdm != 1} {
334 lappend ret $id
335 if {$sdm == 3} {
336 set sdm 0
339 lappend neg [string range $id 1 end]
340 } else {
341 if {$sdm != 2} {
342 lappend ret $id
343 } else {
344 lset ret end $id...[lindex $ret end]
346 lappend pos $id
348 incr sdm -1
350 set vposids($view) $pos
351 set vnegids($view) $neg
352 return $ret
355 # Start off a git log process and arrange to read its output
356 proc start_rev_list {view} {
357 global startmsecs commitidx viewcomplete curview
358 global tclencoding
359 global viewargs viewargscmd viewfiles vfilelimit
360 global showlocalchanges
361 global viewactive viewinstances vmergeonly
362 global mainheadid viewmainheadid viewmainheadid_orig
363 global vcanopt vflags vrevs vorigargs
364 global show_notes
366 set startmsecs [clock clicks -milliseconds]
367 set commitidx($view) 0
368 # these are set this way for the error exits
369 set viewcomplete($view) 1
370 set viewactive($view) 0
371 varcinit $view
373 set args $viewargs($view)
374 if {$viewargscmd($view) ne {}} {
375 if {[catch {
376 set str [exec sh -c $viewargscmd($view)]
377 } err]} {
378 error_popup "[mc "Error executing --argscmd command:"] $err"
379 return 0
381 set args [concat $args [split $str "\n"]]
383 set vcanopt($view) [parseviewargs $view $args]
385 set files $viewfiles($view)
386 if {$vmergeonly($view)} {
387 set files [unmerged_files $files]
388 if {$files eq {}} {
389 global nr_unmerged
390 if {$nr_unmerged == 0} {
391 error_popup [mc "No files selected: --merge specified but\
392 no files are unmerged."]
393 } else {
394 error_popup [mc "No files selected: --merge specified but\
395 no unmerged files are within file limit."]
397 return 0
400 set vfilelimit($view) $files
402 if {$vcanopt($view)} {
403 set revs [parseviewrevs $view $vrevs($view)]
404 if {$revs eq {}} {
405 return 0
407 set args [concat $vflags($view) $revs]
408 } else {
409 set args $vorigargs($view)
412 if {[catch {
413 set fd [open [concat | git log --no-color -z --pretty=raw $show_notes \
414 --parents --boundary $args "--" $files] r]
415 } err]} {
416 error_popup "[mc "Error executing git log:"] $err"
417 return 0
419 set i [reg_instance $fd]
420 set viewinstances($view) [list $i]
421 set viewmainheadid($view) $mainheadid
422 set viewmainheadid_orig($view) $mainheadid
423 if {$files ne {} && $mainheadid ne {}} {
424 get_viewmainhead $view
426 if {$showlocalchanges && $viewmainheadid($view) ne {}} {
427 interestedin $viewmainheadid($view) dodiffindex
429 fconfigure $fd -blocking 0 -translation lf -eofchar {}
430 if {$tclencoding != {}} {
431 fconfigure $fd -encoding $tclencoding
433 filerun $fd [list getcommitlines $fd $i $view 0]
434 nowbusy $view [mc "Reading"]
435 set viewcomplete($view) 0
436 set viewactive($view) 1
437 return 1
440 proc stop_instance {inst} {
441 global commfd leftover
443 set fd $commfd($inst)
444 catch {
445 set pid [pid $fd]
447 if {$::tcl_platform(platform) eq {windows}} {
448 exec kill -f $pid
449 } else {
450 exec kill $pid
453 catch {close $fd}
454 nukefile $fd
455 unset commfd($inst)
456 unset leftover($inst)
459 proc stop_backends {} {
460 global commfd
462 foreach inst [array names commfd] {
463 stop_instance $inst
467 proc stop_rev_list {view} {
468 global viewinstances
470 foreach inst $viewinstances($view) {
471 stop_instance $inst
473 set viewinstances($view) {}
476 proc reset_pending_select {selid} {
477 global pending_select mainheadid selectheadid
479 if {$selid ne {}} {
480 set pending_select $selid
481 } elseif {$selectheadid ne {}} {
482 set pending_select $selectheadid
483 } else {
484 set pending_select $mainheadid
488 proc getcommits {selid} {
489 global canv curview need_redisplay viewactive
491 initlayout
492 if {[start_rev_list $curview]} {
493 reset_pending_select $selid
494 show_status [mc "Reading commits..."]
495 set need_redisplay 1
496 } else {
497 show_status [mc "No commits selected"]
501 proc updatecommits {} {
502 global curview vcanopt vorigargs vfilelimit viewinstances
503 global viewactive viewcomplete tclencoding
504 global startmsecs showneartags showlocalchanges
505 global mainheadid viewmainheadid viewmainheadid_orig pending_select
506 global hasworktree
507 global varcid vposids vnegids vflags vrevs
508 global show_notes
510 set hasworktree [hasworktree]
511 rereadrefs
512 set view $curview
513 if {$mainheadid ne $viewmainheadid_orig($view)} {
514 if {$showlocalchanges} {
515 dohidelocalchanges
517 set viewmainheadid($view) $mainheadid
518 set viewmainheadid_orig($view) $mainheadid
519 if {$vfilelimit($view) ne {}} {
520 get_viewmainhead $view
523 if {$showlocalchanges} {
524 doshowlocalchanges
526 if {$vcanopt($view)} {
527 set oldpos $vposids($view)
528 set oldneg $vnegids($view)
529 set revs [parseviewrevs $view $vrevs($view)]
530 if {$revs eq {}} {
531 return
533 # note: getting the delta when negative refs change is hard,
534 # and could require multiple git log invocations, so in that
535 # case we ask git log for all the commits (not just the delta)
536 if {$oldneg eq $vnegids($view)} {
537 set newrevs {}
538 set npos 0
539 # take out positive refs that we asked for before or
540 # that we have already seen
541 foreach rev $revs {
542 if {[string length $rev] == 40} {
543 if {[lsearch -exact $oldpos $rev] < 0
544 && ![info exists varcid($view,$rev)]} {
545 lappend newrevs $rev
546 incr npos
548 } else {
549 lappend $newrevs $rev
552 if {$npos == 0} return
553 set revs $newrevs
554 set vposids($view) [lsort -unique [concat $oldpos $vposids($view)]]
556 set args [concat $vflags($view) $revs --not $oldpos]
557 } else {
558 set args $vorigargs($view)
560 if {[catch {
561 set fd [open [concat | git log --no-color -z --pretty=raw $show_notes \
562 --parents --boundary $args "--" $vfilelimit($view)] r]
563 } err]} {
564 error_popup "[mc "Error executing git log:"] $err"
565 return
567 if {$viewactive($view) == 0} {
568 set startmsecs [clock clicks -milliseconds]
570 set i [reg_instance $fd]
571 lappend viewinstances($view) $i
572 fconfigure $fd -blocking 0 -translation lf -eofchar {}
573 if {$tclencoding != {}} {
574 fconfigure $fd -encoding $tclencoding
576 filerun $fd [list getcommitlines $fd $i $view 1]
577 incr viewactive($view)
578 set viewcomplete($view) 0
579 reset_pending_select {}
580 nowbusy $view [mc "Reading"]
581 if {$showneartags} {
582 getallcommits
586 proc reloadcommits {} {
587 global curview viewcomplete selectedline currentid thickerline
588 global showneartags treediffs commitinterest cached_commitrow
589 global targetid
591 set selid {}
592 if {$selectedline ne {}} {
593 set selid $currentid
596 if {!$viewcomplete($curview)} {
597 stop_rev_list $curview
599 resetvarcs $curview
600 set selectedline {}
601 catch {unset currentid}
602 catch {unset thickerline}
603 catch {unset treediffs}
604 readrefs
605 changedrefs
606 if {$showneartags} {
607 getallcommits
609 clear_display
610 catch {unset commitinterest}
611 catch {unset cached_commitrow}
612 catch {unset targetid}
613 setcanvscroll
614 getcommits $selid
615 return 0
618 # This makes a string representation of a positive integer which
619 # sorts as a string in numerical order
620 proc strrep {n} {
621 if {$n < 16} {
622 return [format "%x" $n]
623 } elseif {$n < 256} {
624 return [format "x%.2x" $n]
625 } elseif {$n < 65536} {
626 return [format "y%.4x" $n]
628 return [format "z%.8x" $n]
631 # Procedures used in reordering commits from git log (without
632 # --topo-order) into the order for display.
634 proc varcinit {view} {
635 global varcstart vupptr vdownptr vleftptr vbackptr varctok varcrow
636 global vtokmod varcmod vrowmod varcix vlastins
638 set varcstart($view) {{}}
639 set vupptr($view) {0}
640 set vdownptr($view) {0}
641 set vleftptr($view) {0}
642 set vbackptr($view) {0}
643 set varctok($view) {{}}
644 set varcrow($view) {{}}
645 set vtokmod($view) {}
646 set varcmod($view) 0
647 set vrowmod($view) 0
648 set varcix($view) {{}}
649 set vlastins($view) {0}
652 proc resetvarcs {view} {
653 global varcid varccommits parents children vseedcount ordertok
654 global vshortids
656 foreach vid [array names varcid $view,*] {
657 unset varcid($vid)
658 unset children($vid)
659 unset parents($vid)
661 foreach vid [array names vshortids $view,*] {
662 unset vshortids($vid)
664 # some commits might have children but haven't been seen yet
665 foreach vid [array names children $view,*] {
666 unset children($vid)
668 foreach va [array names varccommits $view,*] {
669 unset varccommits($va)
671 foreach vd [array names vseedcount $view,*] {
672 unset vseedcount($vd)
674 catch {unset ordertok}
677 # returns a list of the commits with no children
678 proc seeds {v} {
679 global vdownptr vleftptr varcstart
681 set ret {}
682 set a [lindex $vdownptr($v) 0]
683 while {$a != 0} {
684 lappend ret [lindex $varcstart($v) $a]
685 set a [lindex $vleftptr($v) $a]
687 return $ret
690 proc newvarc {view id} {
691 global varcid varctok parents children vdatemode
692 global vupptr vdownptr vleftptr vbackptr varcrow varcix varcstart
693 global commitdata commitinfo vseedcount varccommits vlastins
695 set a [llength $varctok($view)]
696 set vid $view,$id
697 if {[llength $children($vid)] == 0 || $vdatemode($view)} {
698 if {![info exists commitinfo($id)]} {
699 parsecommit $id $commitdata($id) 1
701 set cdate [lindex [lindex $commitinfo($id) 4] 0]
702 if {![string is integer -strict $cdate]} {
703 set cdate 0
705 if {![info exists vseedcount($view,$cdate)]} {
706 set vseedcount($view,$cdate) -1
708 set c [incr vseedcount($view,$cdate)]
709 set cdate [expr {$cdate ^ 0xffffffff}]
710 set tok "s[strrep $cdate][strrep $c]"
711 } else {
712 set tok {}
714 set ka 0
715 if {[llength $children($vid)] > 0} {
716 set kid [lindex $children($vid) end]
717 set k $varcid($view,$kid)
718 if {[string compare [lindex $varctok($view) $k] $tok] > 0} {
719 set ki $kid
720 set ka $k
721 set tok [lindex $varctok($view) $k]
724 if {$ka != 0} {
725 set i [lsearch -exact $parents($view,$ki) $id]
726 set j [expr {[llength $parents($view,$ki)] - 1 - $i}]
727 append tok [strrep $j]
729 set c [lindex $vlastins($view) $ka]
730 if {$c == 0 || [string compare $tok [lindex $varctok($view) $c]] < 0} {
731 set c $ka
732 set b [lindex $vdownptr($view) $ka]
733 } else {
734 set b [lindex $vleftptr($view) $c]
736 while {$b != 0 && [string compare $tok [lindex $varctok($view) $b]] >= 0} {
737 set c $b
738 set b [lindex $vleftptr($view) $c]
740 if {$c == $ka} {
741 lset vdownptr($view) $ka $a
742 lappend vbackptr($view) 0
743 } else {
744 lset vleftptr($view) $c $a
745 lappend vbackptr($view) $c
747 lset vlastins($view) $ka $a
748 lappend vupptr($view) $ka
749 lappend vleftptr($view) $b
750 if {$b != 0} {
751 lset vbackptr($view) $b $a
753 lappend varctok($view) $tok
754 lappend varcstart($view) $id
755 lappend vdownptr($view) 0
756 lappend varcrow($view) {}
757 lappend varcix($view) {}
758 set varccommits($view,$a) {}
759 lappend vlastins($view) 0
760 return $a
763 proc splitvarc {p v} {
764 global varcid varcstart varccommits varctok vtokmod
765 global vupptr vdownptr vleftptr vbackptr varcix varcrow vlastins
767 set oa $varcid($v,$p)
768 set otok [lindex $varctok($v) $oa]
769 set ac $varccommits($v,$oa)
770 set i [lsearch -exact $varccommits($v,$oa) $p]
771 if {$i <= 0} return
772 set na [llength $varctok($v)]
773 # "%" sorts before "0"...
774 set tok "$otok%[strrep $i]"
775 lappend varctok($v) $tok
776 lappend varcrow($v) {}
777 lappend varcix($v) {}
778 set varccommits($v,$oa) [lrange $ac 0 [expr {$i - 1}]]
779 set varccommits($v,$na) [lrange $ac $i end]
780 lappend varcstart($v) $p
781 foreach id $varccommits($v,$na) {
782 set varcid($v,$id) $na
784 lappend vdownptr($v) [lindex $vdownptr($v) $oa]
785 lappend vlastins($v) [lindex $vlastins($v) $oa]
786 lset vdownptr($v) $oa $na
787 lset vlastins($v) $oa 0
788 lappend vupptr($v) $oa
789 lappend vleftptr($v) 0
790 lappend vbackptr($v) 0
791 for {set b [lindex $vdownptr($v) $na]} {$b != 0} {set b [lindex $vleftptr($v) $b]} {
792 lset vupptr($v) $b $na
794 if {[string compare $otok $vtokmod($v)] <= 0} {
795 modify_arc $v $oa
799 proc renumbervarc {a v} {
800 global parents children varctok varcstart varccommits
801 global vupptr vdownptr vleftptr vbackptr vlastins varcid vtokmod vdatemode
803 set t1 [clock clicks -milliseconds]
804 set todo {}
805 set isrelated($a) 1
806 set kidchanged($a) 1
807 set ntot 0
808 while {$a != 0} {
809 if {[info exists isrelated($a)]} {
810 lappend todo $a
811 set id [lindex $varccommits($v,$a) end]
812 foreach p $parents($v,$id) {
813 if {[info exists varcid($v,$p)]} {
814 set isrelated($varcid($v,$p)) 1
818 incr ntot
819 set b [lindex $vdownptr($v) $a]
820 if {$b == 0} {
821 while {$a != 0} {
822 set b [lindex $vleftptr($v) $a]
823 if {$b != 0} break
824 set a [lindex $vupptr($v) $a]
827 set a $b
829 foreach a $todo {
830 if {![info exists kidchanged($a)]} continue
831 set id [lindex $varcstart($v) $a]
832 if {[llength $children($v,$id)] > 1} {
833 set children($v,$id) [lsort -command [list vtokcmp $v] \
834 $children($v,$id)]
836 set oldtok [lindex $varctok($v) $a]
837 if {!$vdatemode($v)} {
838 set tok {}
839 } else {
840 set tok $oldtok
842 set ka 0
843 set kid [last_real_child $v,$id]
844 if {$kid ne {}} {
845 set k $varcid($v,$kid)
846 if {[string compare [lindex $varctok($v) $k] $tok] > 0} {
847 set ki $kid
848 set ka $k
849 set tok [lindex $varctok($v) $k]
852 if {$ka != 0} {
853 set i [lsearch -exact $parents($v,$ki) $id]
854 set j [expr {[llength $parents($v,$ki)] - 1 - $i}]
855 append tok [strrep $j]
857 if {$tok eq $oldtok} {
858 continue
860 set id [lindex $varccommits($v,$a) end]
861 foreach p $parents($v,$id) {
862 if {[info exists varcid($v,$p)]} {
863 set kidchanged($varcid($v,$p)) 1
864 } else {
865 set sortkids($p) 1
868 lset varctok($v) $a $tok
869 set b [lindex $vupptr($v) $a]
870 if {$b != $ka} {
871 if {[string compare [lindex $varctok($v) $ka] $vtokmod($v)] < 0} {
872 modify_arc $v $ka
874 if {[string compare [lindex $varctok($v) $b] $vtokmod($v)] < 0} {
875 modify_arc $v $b
877 set c [lindex $vbackptr($v) $a]
878 set d [lindex $vleftptr($v) $a]
879 if {$c == 0} {
880 lset vdownptr($v) $b $d
881 } else {
882 lset vleftptr($v) $c $d
884 if {$d != 0} {
885 lset vbackptr($v) $d $c
887 if {[lindex $vlastins($v) $b] == $a} {
888 lset vlastins($v) $b $c
890 lset vupptr($v) $a $ka
891 set c [lindex $vlastins($v) $ka]
892 if {$c == 0 || \
893 [string compare $tok [lindex $varctok($v) $c]] < 0} {
894 set c $ka
895 set b [lindex $vdownptr($v) $ka]
896 } else {
897 set b [lindex $vleftptr($v) $c]
899 while {$b != 0 && \
900 [string compare $tok [lindex $varctok($v) $b]] >= 0} {
901 set c $b
902 set b [lindex $vleftptr($v) $c]
904 if {$c == $ka} {
905 lset vdownptr($v) $ka $a
906 lset vbackptr($v) $a 0
907 } else {
908 lset vleftptr($v) $c $a
909 lset vbackptr($v) $a $c
911 lset vleftptr($v) $a $b
912 if {$b != 0} {
913 lset vbackptr($v) $b $a
915 lset vlastins($v) $ka $a
918 foreach id [array names sortkids] {
919 if {[llength $children($v,$id)] > 1} {
920 set children($v,$id) [lsort -command [list vtokcmp $v] \
921 $children($v,$id)]
924 set t2 [clock clicks -milliseconds]
925 #puts "renumbervarc did [llength $todo] of $ntot arcs in [expr {$t2-$t1}]ms"
928 # Fix up the graph after we have found out that in view $v,
929 # $p (a commit that we have already seen) is actually the parent
930 # of the last commit in arc $a.
931 proc fix_reversal {p a v} {
932 global varcid varcstart varctok vupptr
934 set pa $varcid($v,$p)
935 if {$p ne [lindex $varcstart($v) $pa]} {
936 splitvarc $p $v
937 set pa $varcid($v,$p)
939 # seeds always need to be renumbered
940 if {[lindex $vupptr($v) $pa] == 0 ||
941 [string compare [lindex $varctok($v) $a] \
942 [lindex $varctok($v) $pa]] > 0} {
943 renumbervarc $pa $v
947 proc insertrow {id p v} {
948 global cmitlisted children parents varcid varctok vtokmod
949 global varccommits ordertok commitidx numcommits curview
950 global targetid targetrow vshortids
952 readcommit $id
953 set vid $v,$id
954 set cmitlisted($vid) 1
955 set children($vid) {}
956 set parents($vid) [list $p]
957 set a [newvarc $v $id]
958 set varcid($vid) $a
959 lappend vshortids($v,[string range $id 0 3]) $id
960 if {[string compare [lindex $varctok($v) $a] $vtokmod($v)] < 0} {
961 modify_arc $v $a
963 lappend varccommits($v,$a) $id
964 set vp $v,$p
965 if {[llength [lappend children($vp) $id]] > 1} {
966 set children($vp) [lsort -command [list vtokcmp $v] $children($vp)]
967 catch {unset ordertok}
969 fix_reversal $p $a $v
970 incr commitidx($v)
971 if {$v == $curview} {
972 set numcommits $commitidx($v)
973 setcanvscroll
974 if {[info exists targetid]} {
975 if {![comes_before $targetid $p]} {
976 incr targetrow
982 proc insertfakerow {id p} {
983 global varcid varccommits parents children cmitlisted
984 global commitidx varctok vtokmod targetid targetrow curview numcommits
986 set v $curview
987 set a $varcid($v,$p)
988 set i [lsearch -exact $varccommits($v,$a) $p]
989 if {$i < 0} {
990 puts "oops: insertfakerow can't find [shortids $p] on arc $a"
991 return
993 set children($v,$id) {}
994 set parents($v,$id) [list $p]
995 set varcid($v,$id) $a
996 lappend children($v,$p) $id
997 set cmitlisted($v,$id) 1
998 set numcommits [incr commitidx($v)]
999 # note we deliberately don't update varcstart($v) even if $i == 0
1000 set varccommits($v,$a) [linsert $varccommits($v,$a) $i $id]
1001 modify_arc $v $a $i
1002 if {[info exists targetid]} {
1003 if {![comes_before $targetid $p]} {
1004 incr targetrow
1007 setcanvscroll
1008 drawvisible
1011 proc removefakerow {id} {
1012 global varcid varccommits parents children commitidx
1013 global varctok vtokmod cmitlisted currentid selectedline
1014 global targetid curview numcommits
1016 set v $curview
1017 if {[llength $parents($v,$id)] != 1} {
1018 puts "oops: removefakerow [shortids $id] has [llength $parents($v,$id)] parents"
1019 return
1021 set p [lindex $parents($v,$id) 0]
1022 set a $varcid($v,$id)
1023 set i [lsearch -exact $varccommits($v,$a) $id]
1024 if {$i < 0} {
1025 puts "oops: removefakerow can't find [shortids $id] on arc $a"
1026 return
1028 unset varcid($v,$id)
1029 set varccommits($v,$a) [lreplace $varccommits($v,$a) $i $i]
1030 unset parents($v,$id)
1031 unset children($v,$id)
1032 unset cmitlisted($v,$id)
1033 set numcommits [incr commitidx($v) -1]
1034 set j [lsearch -exact $children($v,$p) $id]
1035 if {$j >= 0} {
1036 set children($v,$p) [lreplace $children($v,$p) $j $j]
1038 modify_arc $v $a $i
1039 if {[info exist currentid] && $id eq $currentid} {
1040 unset currentid
1041 set selectedline {}
1043 if {[info exists targetid] && $targetid eq $id} {
1044 set targetid $p
1046 setcanvscroll
1047 drawvisible
1050 proc real_children {vp} {
1051 global children nullid nullid2
1053 set kids {}
1054 foreach id $children($vp) {
1055 if {$id ne $nullid && $id ne $nullid2} {
1056 lappend kids $id
1059 return $kids
1062 proc first_real_child {vp} {
1063 global children nullid nullid2
1065 foreach id $children($vp) {
1066 if {$id ne $nullid && $id ne $nullid2} {
1067 return $id
1070 return {}
1073 proc last_real_child {vp} {
1074 global children nullid nullid2
1076 set kids $children($vp)
1077 for {set i [llength $kids]} {[incr i -1] >= 0} {} {
1078 set id [lindex $kids $i]
1079 if {$id ne $nullid && $id ne $nullid2} {
1080 return $id
1083 return {}
1086 proc vtokcmp {v a b} {
1087 global varctok varcid
1089 return [string compare [lindex $varctok($v) $varcid($v,$a)] \
1090 [lindex $varctok($v) $varcid($v,$b)]]
1093 # This assumes that if lim is not given, the caller has checked that
1094 # arc a's token is less than $vtokmod($v)
1095 proc modify_arc {v a {lim {}}} {
1096 global varctok vtokmod varcmod varcrow vupptr curview vrowmod varccommits
1098 if {$lim ne {}} {
1099 set c [string compare [lindex $varctok($v) $a] $vtokmod($v)]
1100 if {$c > 0} return
1101 if {$c == 0} {
1102 set r [lindex $varcrow($v) $a]
1103 if {$r ne {} && $vrowmod($v) <= $r + $lim} return
1106 set vtokmod($v) [lindex $varctok($v) $a]
1107 set varcmod($v) $a
1108 if {$v == $curview} {
1109 while {$a != 0 && [lindex $varcrow($v) $a] eq {}} {
1110 set a [lindex $vupptr($v) $a]
1111 set lim {}
1113 set r 0
1114 if {$a != 0} {
1115 if {$lim eq {}} {
1116 set lim [llength $varccommits($v,$a)]
1118 set r [expr {[lindex $varcrow($v) $a] + $lim}]
1120 set vrowmod($v) $r
1121 undolayout $r
1125 proc update_arcrows {v} {
1126 global vtokmod varcmod vrowmod varcrow commitidx currentid selectedline
1127 global varcid vrownum varcorder varcix varccommits
1128 global vupptr vdownptr vleftptr varctok
1129 global displayorder parentlist curview cached_commitrow
1131 if {$vrowmod($v) == $commitidx($v)} return
1132 if {$v == $curview} {
1133 if {[llength $displayorder] > $vrowmod($v)} {
1134 set displayorder [lrange $displayorder 0 [expr {$vrowmod($v) - 1}]]
1135 set parentlist [lrange $parentlist 0 [expr {$vrowmod($v) - 1}]]
1137 catch {unset cached_commitrow}
1139 set narctot [expr {[llength $varctok($v)] - 1}]
1140 set a $varcmod($v)
1141 while {$a != 0 && [lindex $varcix($v) $a] eq {}} {
1142 # go up the tree until we find something that has a row number,
1143 # or we get to a seed
1144 set a [lindex $vupptr($v) $a]
1146 if {$a == 0} {
1147 set a [lindex $vdownptr($v) 0]
1148 if {$a == 0} return
1149 set vrownum($v) {0}
1150 set varcorder($v) [list $a]
1151 lset varcix($v) $a 0
1152 lset varcrow($v) $a 0
1153 set arcn 0
1154 set row 0
1155 } else {
1156 set arcn [lindex $varcix($v) $a]
1157 if {[llength $vrownum($v)] > $arcn + 1} {
1158 set vrownum($v) [lrange $vrownum($v) 0 $arcn]
1159 set varcorder($v) [lrange $varcorder($v) 0 $arcn]
1161 set row [lindex $varcrow($v) $a]
1163 while {1} {
1164 set p $a
1165 incr row [llength $varccommits($v,$a)]
1166 # go down if possible
1167 set b [lindex $vdownptr($v) $a]
1168 if {$b == 0} {
1169 # if not, go left, or go up until we can go left
1170 while {$a != 0} {
1171 set b [lindex $vleftptr($v) $a]
1172 if {$b != 0} break
1173 set a [lindex $vupptr($v) $a]
1175 if {$a == 0} break
1177 set a $b
1178 incr arcn
1179 lappend vrownum($v) $row
1180 lappend varcorder($v) $a
1181 lset varcix($v) $a $arcn
1182 lset varcrow($v) $a $row
1184 set vtokmod($v) [lindex $varctok($v) $p]
1185 set varcmod($v) $p
1186 set vrowmod($v) $row
1187 if {[info exists currentid]} {
1188 set selectedline [rowofcommit $currentid]
1192 # Test whether view $v contains commit $id
1193 proc commitinview {id v} {
1194 global varcid
1196 return [info exists varcid($v,$id)]
1199 # Return the row number for commit $id in the current view
1200 proc rowofcommit {id} {
1201 global varcid varccommits varcrow curview cached_commitrow
1202 global varctok vtokmod
1204 set v $curview
1205 if {![info exists varcid($v,$id)]} {
1206 puts "oops rowofcommit no arc for [shortids $id]"
1207 return {}
1209 set a $varcid($v,$id)
1210 if {[string compare [lindex $varctok($v) $a] $vtokmod($v)] >= 0} {
1211 update_arcrows $v
1213 if {[info exists cached_commitrow($id)]} {
1214 return $cached_commitrow($id)
1216 set i [lsearch -exact $varccommits($v,$a) $id]
1217 if {$i < 0} {
1218 puts "oops didn't find commit [shortids $id] in arc $a"
1219 return {}
1221 incr i [lindex $varcrow($v) $a]
1222 set cached_commitrow($id) $i
1223 return $i
1226 # Returns 1 if a is on an earlier row than b, otherwise 0
1227 proc comes_before {a b} {
1228 global varcid varctok curview
1230 set v $curview
1231 if {$a eq $b || ![info exists varcid($v,$a)] || \
1232 ![info exists varcid($v,$b)]} {
1233 return 0
1235 if {$varcid($v,$a) != $varcid($v,$b)} {
1236 return [expr {[string compare [lindex $varctok($v) $varcid($v,$a)] \
1237 [lindex $varctok($v) $varcid($v,$b)]] < 0}]
1239 return [expr {[rowofcommit $a] < [rowofcommit $b]}]
1242 proc bsearch {l elt} {
1243 if {[llength $l] == 0 || $elt <= [lindex $l 0]} {
1244 return 0
1246 set lo 0
1247 set hi [llength $l]
1248 while {$hi - $lo > 1} {
1249 set mid [expr {int(($lo + $hi) / 2)}]
1250 set t [lindex $l $mid]
1251 if {$elt < $t} {
1252 set hi $mid
1253 } elseif {$elt > $t} {
1254 set lo $mid
1255 } else {
1256 return $mid
1259 return $lo
1262 # Make sure rows $start..$end-1 are valid in displayorder and parentlist
1263 proc make_disporder {start end} {
1264 global vrownum curview commitidx displayorder parentlist
1265 global varccommits varcorder parents vrowmod varcrow
1266 global d_valid_start d_valid_end
1268 if {$end > $vrowmod($curview)} {
1269 update_arcrows $curview
1271 set ai [bsearch $vrownum($curview) $start]
1272 set start [lindex $vrownum($curview) $ai]
1273 set narc [llength $vrownum($curview)]
1274 for {set r $start} {$ai < $narc && $r < $end} {incr ai} {
1275 set a [lindex $varcorder($curview) $ai]
1276 set l [llength $displayorder]
1277 set al [llength $varccommits($curview,$a)]
1278 if {$l < $r + $al} {
1279 if {$l < $r} {
1280 set pad [ntimes [expr {$r - $l}] {}]
1281 set displayorder [concat $displayorder $pad]
1282 set parentlist [concat $parentlist $pad]
1283 } elseif {$l > $r} {
1284 set displayorder [lrange $displayorder 0 [expr {$r - 1}]]
1285 set parentlist [lrange $parentlist 0 [expr {$r - 1}]]
1287 foreach id $varccommits($curview,$a) {
1288 lappend displayorder $id
1289 lappend parentlist $parents($curview,$id)
1291 } elseif {[lindex $displayorder [expr {$r + $al - 1}]] eq {}} {
1292 set i $r
1293 foreach id $varccommits($curview,$a) {
1294 lset displayorder $i $id
1295 lset parentlist $i $parents($curview,$id)
1296 incr i
1299 incr r $al
1303 proc commitonrow {row} {
1304 global displayorder
1306 set id [lindex $displayorder $row]
1307 if {$id eq {}} {
1308 make_disporder $row [expr {$row + 1}]
1309 set id [lindex $displayorder $row]
1311 return $id
1314 proc closevarcs {v} {
1315 global varctok varccommits varcid parents children
1316 global cmitlisted commitidx vtokmod
1318 set missing_parents 0
1319 set scripts {}
1320 set narcs [llength $varctok($v)]
1321 for {set a 1} {$a < $narcs} {incr a} {
1322 set id [lindex $varccommits($v,$a) end]
1323 foreach p $parents($v,$id) {
1324 if {[info exists varcid($v,$p)]} continue
1325 # add p as a new commit
1326 incr missing_parents
1327 set cmitlisted($v,$p) 0
1328 set parents($v,$p) {}
1329 if {[llength $children($v,$p)] == 1 &&
1330 [llength $parents($v,$id)] == 1} {
1331 set b $a
1332 } else {
1333 set b [newvarc $v $p]
1335 set varcid($v,$p) $b
1336 if {[string compare [lindex $varctok($v) $b] $vtokmod($v)] < 0} {
1337 modify_arc $v $b
1339 lappend varccommits($v,$b) $p
1340 incr commitidx($v)
1341 set scripts [check_interest $p $scripts]
1344 if {$missing_parents > 0} {
1345 foreach s $scripts {
1346 eval $s
1351 # Use $rwid as a substitute for $id, i.e. reparent $id's children to $rwid
1352 # Assumes we already have an arc for $rwid.
1353 proc rewrite_commit {v id rwid} {
1354 global children parents varcid varctok vtokmod varccommits
1356 foreach ch $children($v,$id) {
1357 # make $rwid be $ch's parent in place of $id
1358 set i [lsearch -exact $parents($v,$ch) $id]
1359 if {$i < 0} {
1360 puts "oops rewrite_commit didn't find $id in parent list for $ch"
1362 set parents($v,$ch) [lreplace $parents($v,$ch) $i $i $rwid]
1363 # add $ch to $rwid's children and sort the list if necessary
1364 if {[llength [lappend children($v,$rwid) $ch]] > 1} {
1365 set children($v,$rwid) [lsort -command [list vtokcmp $v] \
1366 $children($v,$rwid)]
1368 # fix the graph after joining $id to $rwid
1369 set a $varcid($v,$ch)
1370 fix_reversal $rwid $a $v
1371 # parentlist is wrong for the last element of arc $a
1372 # even if displayorder is right, hence the 3rd arg here
1373 modify_arc $v $a [expr {[llength $varccommits($v,$a)] - 1}]
1377 # Mechanism for registering a command to be executed when we come
1378 # across a particular commit. To handle the case when only the
1379 # prefix of the commit is known, the commitinterest array is now
1380 # indexed by the first 4 characters of the ID. Each element is a
1381 # list of id, cmd pairs.
1382 proc interestedin {id cmd} {
1383 global commitinterest
1385 lappend commitinterest([string range $id 0 3]) $id $cmd
1388 proc check_interest {id scripts} {
1389 global commitinterest
1391 set prefix [string range $id 0 3]
1392 if {[info exists commitinterest($prefix)]} {
1393 set newlist {}
1394 foreach {i script} $commitinterest($prefix) {
1395 if {[string match "$i*" $id]} {
1396 lappend scripts [string map [list "%I" $id "%P" $i] $script]
1397 } else {
1398 lappend newlist $i $script
1401 if {$newlist ne {}} {
1402 set commitinterest($prefix) $newlist
1403 } else {
1404 unset commitinterest($prefix)
1407 return $scripts
1410 proc getcommitlines {fd inst view updating} {
1411 global cmitlisted leftover
1412 global commitidx commitdata vdatemode
1413 global parents children curview hlview
1414 global idpending ordertok
1415 global varccommits varcid varctok vtokmod vfilelimit vshortids
1417 set stuff [read $fd 500000]
1418 # git log doesn't terminate the last commit with a null...
1419 if {$stuff == {} && $leftover($inst) ne {} && [eof $fd]} {
1420 set stuff "\0"
1422 if {$stuff == {}} {
1423 if {![eof $fd]} {
1424 return 1
1426 global commfd viewcomplete viewactive viewname
1427 global viewinstances
1428 unset commfd($inst)
1429 set i [lsearch -exact $viewinstances($view) $inst]
1430 if {$i >= 0} {
1431 set viewinstances($view) [lreplace $viewinstances($view) $i $i]
1433 # set it blocking so we wait for the process to terminate
1434 fconfigure $fd -blocking 1
1435 if {[catch {close $fd} err]} {
1436 set fv {}
1437 if {$view != $curview} {
1438 set fv " for the \"$viewname($view)\" view"
1440 if {[string range $err 0 4] == "usage"} {
1441 set err "Gitk: error reading commits$fv:\
1442 bad arguments to git log."
1443 if {$viewname($view) eq "Command line"} {
1444 append err \
1445 " (Note: arguments to gitk are passed to git log\
1446 to allow selection of commits to be displayed.)"
1448 } else {
1449 set err "Error reading commits$fv: $err"
1451 error_popup $err
1453 if {[incr viewactive($view) -1] <= 0} {
1454 set viewcomplete($view) 1
1455 # Check if we have seen any ids listed as parents that haven't
1456 # appeared in the list
1457 closevarcs $view
1458 notbusy $view
1460 if {$view == $curview} {
1461 run chewcommits
1463 return 0
1465 set start 0
1466 set gotsome 0
1467 set scripts {}
1468 while 1 {
1469 set i [string first "\0" $stuff $start]
1470 if {$i < 0} {
1471 append leftover($inst) [string range $stuff $start end]
1472 break
1474 if {$start == 0} {
1475 set cmit $leftover($inst)
1476 append cmit [string range $stuff 0 [expr {$i - 1}]]
1477 set leftover($inst) {}
1478 } else {
1479 set cmit [string range $stuff $start [expr {$i - 1}]]
1481 set start [expr {$i + 1}]
1482 set j [string first "\n" $cmit]
1483 set ok 0
1484 set listed 1
1485 if {$j >= 0 && [string match "commit *" $cmit]} {
1486 set ids [string range $cmit 7 [expr {$j - 1}]]
1487 if {[string match {[-^<>]*} $ids]} {
1488 switch -- [string index $ids 0] {
1489 "-" {set listed 0}
1490 "^" {set listed 2}
1491 "<" {set listed 3}
1492 ">" {set listed 4}
1494 set ids [string range $ids 1 end]
1496 set ok 1
1497 foreach id $ids {
1498 if {[string length $id] != 40} {
1499 set ok 0
1500 break
1504 if {!$ok} {
1505 set shortcmit $cmit
1506 if {[string length $shortcmit] > 80} {
1507 set shortcmit "[string range $shortcmit 0 80]..."
1509 error_popup "[mc "Can't parse git log output:"] {$shortcmit}"
1510 exit 1
1512 set id [lindex $ids 0]
1513 set vid $view,$id
1515 lappend vshortids($view,[string range $id 0 3]) $id
1517 if {!$listed && $updating && ![info exists varcid($vid)] &&
1518 $vfilelimit($view) ne {}} {
1519 # git log doesn't rewrite parents for unlisted commits
1520 # when doing path limiting, so work around that here
1521 # by working out the rewritten parent with git rev-list
1522 # and if we already know about it, using the rewritten
1523 # parent as a substitute parent for $id's children.
1524 if {![catch {
1525 set rwid [exec git rev-list --first-parent --max-count=1 \
1526 $id -- $vfilelimit($view)]
1527 }]} {
1528 if {$rwid ne {} && [info exists varcid($view,$rwid)]} {
1529 # use $rwid in place of $id
1530 rewrite_commit $view $id $rwid
1531 continue
1536 set a 0
1537 if {[info exists varcid($vid)]} {
1538 if {$cmitlisted($vid) || !$listed} continue
1539 set a $varcid($vid)
1541 if {$listed} {
1542 set olds [lrange $ids 1 end]
1543 } else {
1544 set olds {}
1546 set commitdata($id) [string range $cmit [expr {$j + 1}] end]
1547 set cmitlisted($vid) $listed
1548 set parents($vid) $olds
1549 if {![info exists children($vid)]} {
1550 set children($vid) {}
1551 } elseif {$a == 0 && [llength $children($vid)] == 1} {
1552 set k [lindex $children($vid) 0]
1553 if {[llength $parents($view,$k)] == 1 &&
1554 (!$vdatemode($view) ||
1555 $varcid($view,$k) == [llength $varctok($view)] - 1)} {
1556 set a $varcid($view,$k)
1559 if {$a == 0} {
1560 # new arc
1561 set a [newvarc $view $id]
1563 if {[string compare [lindex $varctok($view) $a] $vtokmod($view)] < 0} {
1564 modify_arc $view $a
1566 if {![info exists varcid($vid)]} {
1567 set varcid($vid) $a
1568 lappend varccommits($view,$a) $id
1569 incr commitidx($view)
1572 set i 0
1573 foreach p $olds {
1574 if {$i == 0 || [lsearch -exact $olds $p] >= $i} {
1575 set vp $view,$p
1576 if {[llength [lappend children($vp) $id]] > 1 &&
1577 [vtokcmp $view [lindex $children($vp) end-1] $id] > 0} {
1578 set children($vp) [lsort -command [list vtokcmp $view] \
1579 $children($vp)]
1580 catch {unset ordertok}
1582 if {[info exists varcid($view,$p)]} {
1583 fix_reversal $p $a $view
1586 incr i
1589 set scripts [check_interest $id $scripts]
1590 set gotsome 1
1592 if {$gotsome} {
1593 global numcommits hlview
1595 if {$view == $curview} {
1596 set numcommits $commitidx($view)
1597 run chewcommits
1599 if {[info exists hlview] && $view == $hlview} {
1600 # we never actually get here...
1601 run vhighlightmore
1603 foreach s $scripts {
1604 eval $s
1607 return 2
1610 proc chewcommits {} {
1611 global curview hlview viewcomplete
1612 global pending_select
1614 layoutmore
1615 if {$viewcomplete($curview)} {
1616 global commitidx varctok
1617 global numcommits startmsecs
1619 if {[info exists pending_select]} {
1620 update
1621 reset_pending_select {}
1623 if {[commitinview $pending_select $curview]} {
1624 selectline [rowofcommit $pending_select] 1
1625 } else {
1626 set row [first_real_row]
1627 selectline $row 1
1630 if {$commitidx($curview) > 0} {
1631 #set ms [expr {[clock clicks -milliseconds] - $startmsecs}]
1632 #puts "overall $ms ms for $numcommits commits"
1633 #puts "[llength $varctok($view)] arcs, $commitidx($view) commits"
1634 } else {
1635 show_status [mc "No commits selected"]
1637 notbusy layout
1639 return 0
1642 proc do_readcommit {id} {
1643 global tclencoding
1645 # Invoke git-log to handle automatic encoding conversion
1646 set fd [open [concat | git log --no-color --pretty=raw -1 $id] r]
1647 # Read the results using i18n.logoutputencoding
1648 fconfigure $fd -translation lf -eofchar {}
1649 if {$tclencoding != {}} {
1650 fconfigure $fd -encoding $tclencoding
1652 set contents [read $fd]
1653 close $fd
1654 # Remove the heading line
1655 regsub {^commit [0-9a-f]+\n} $contents {} contents
1657 return $contents
1660 proc readcommit {id} {
1661 if {[catch {set contents [do_readcommit $id]}]} return
1662 parsecommit $id $contents 1
1665 proc parsecommit {id contents listed} {
1666 global commitinfo
1668 set inhdr 1
1669 set comment {}
1670 set headline {}
1671 set auname {}
1672 set audate {}
1673 set comname {}
1674 set comdate {}
1675 set hdrend [string first "\n\n" $contents]
1676 if {$hdrend < 0} {
1677 # should never happen...
1678 set hdrend [string length $contents]
1680 set header [string range $contents 0 [expr {$hdrend - 1}]]
1681 set comment [string range $contents [expr {$hdrend + 2}] end]
1682 foreach line [split $header "\n"] {
1683 set line [split $line " "]
1684 set tag [lindex $line 0]
1685 if {$tag == "author"} {
1686 set audate [lrange $line end-1 end]
1687 set auname [join [lrange $line 1 end-2] " "]
1688 } elseif {$tag == "committer"} {
1689 set comdate [lrange $line end-1 end]
1690 set comname [join [lrange $line 1 end-2] " "]
1693 set headline {}
1694 # take the first non-blank line of the comment as the headline
1695 set headline [string trimleft $comment]
1696 set i [string first "\n" $headline]
1697 if {$i >= 0} {
1698 set headline [string range $headline 0 $i]
1700 set headline [string trimright $headline]
1701 set i [string first "\r" $headline]
1702 if {$i >= 0} {
1703 set headline [string trimright [string range $headline 0 $i]]
1705 if {!$listed} {
1706 # git log indents the comment by 4 spaces;
1707 # if we got this via git cat-file, add the indentation
1708 set newcomment {}
1709 foreach line [split $comment "\n"] {
1710 append newcomment " "
1711 append newcomment $line
1712 append newcomment "\n"
1714 set comment $newcomment
1716 set hasnote [string first "\nNotes:\n" $contents]
1717 set diff ""
1718 # If there is diff output shown in the git-log stream, split it
1719 # out. But get rid of the empty line that always precedes the
1720 # diff.
1721 set i [string first "\n\ndiff" $comment]
1722 if {$i >= 0} {
1723 set diff [string range $comment $i+1 end]
1724 set comment [string range $comment 0 $i-1]
1726 set commitinfo($id) [list $headline $auname $audate \
1727 $comname $comdate $comment $hasnote $diff]
1730 proc getcommit {id} {
1731 global commitdata commitinfo
1733 if {[info exists commitdata($id)]} {
1734 parsecommit $id $commitdata($id) 1
1735 } else {
1736 readcommit $id
1737 if {![info exists commitinfo($id)]} {
1738 set commitinfo($id) [list [mc "No commit information available"]]
1741 return 1
1744 # Expand an abbreviated commit ID to a list of full 40-char IDs that match
1745 # and are present in the current view.
1746 # This is fairly slow...
1747 proc longid {prefix} {
1748 global varcid curview vshortids
1750 set ids {}
1751 if {[string length $prefix] >= 4} {
1752 set vshortid $curview,[string range $prefix 0 3]
1753 if {[info exists vshortids($vshortid)]} {
1754 foreach id $vshortids($vshortid) {
1755 if {[string match "$prefix*" $id]} {
1756 if {[lsearch -exact $ids $id] < 0} {
1757 lappend ids $id
1758 if {[llength $ids] >= 2} break
1763 } else {
1764 foreach match [array names varcid "$curview,$prefix*"] {
1765 lappend ids [lindex [split $match ","] 1]
1766 if {[llength $ids] >= 2} break
1769 return $ids
1772 proc readrefs {} {
1773 global tagids idtags headids idheads tagobjid
1774 global otherrefids idotherrefs mainhead mainheadid
1775 global selecthead selectheadid
1776 global hideremotes
1778 foreach v {tagids idtags headids idheads otherrefids idotherrefs} {
1779 catch {unset $v}
1781 set refd [open [list | git show-ref -d] r]
1782 while {[gets $refd line] >= 0} {
1783 if {[string index $line 40] ne " "} continue
1784 set id [string range $line 0 39]
1785 set ref [string range $line 41 end]
1786 if {![string match "refs/*" $ref]} continue
1787 set name [string range $ref 5 end]
1788 if {[string match "remotes/*" $name]} {
1789 if {![string match "*/HEAD" $name] && !$hideremotes} {
1790 set headids($name) $id
1791 lappend idheads($id) $name
1793 } elseif {[string match "heads/*" $name]} {
1794 set name [string range $name 6 end]
1795 set headids($name) $id
1796 lappend idheads($id) $name
1797 } elseif {[string match "tags/*" $name]} {
1798 # this lets refs/tags/foo^{} overwrite refs/tags/foo,
1799 # which is what we want since the former is the commit ID
1800 set name [string range $name 5 end]
1801 if {[string match "*^{}" $name]} {
1802 set name [string range $name 0 end-3]
1803 } else {
1804 set tagobjid($name) $id
1806 set tagids($name) $id
1807 lappend idtags($id) $name
1808 } else {
1809 set otherrefids($name) $id
1810 lappend idotherrefs($id) $name
1813 catch {close $refd}
1814 set mainhead {}
1815 set mainheadid {}
1816 catch {
1817 set mainheadid [exec git rev-parse HEAD]
1818 set thehead [exec git symbolic-ref HEAD]
1819 if {[string match "refs/heads/*" $thehead]} {
1820 set mainhead [string range $thehead 11 end]
1823 set selectheadid {}
1824 if {$selecthead ne {}} {
1825 catch {
1826 set selectheadid [exec git rev-parse --verify $selecthead]
1831 # skip over fake commits
1832 proc first_real_row {} {
1833 global nullid nullid2 numcommits
1835 for {set row 0} {$row < $numcommits} {incr row} {
1836 set id [commitonrow $row]
1837 if {$id ne $nullid && $id ne $nullid2} {
1838 break
1841 return $row
1844 # update things for a head moved to a child of its previous location
1845 proc movehead {id name} {
1846 global headids idheads
1848 removehead $headids($name) $name
1849 set headids($name) $id
1850 lappend idheads($id) $name
1853 # update things when a head has been removed
1854 proc removehead {id name} {
1855 global headids idheads
1857 if {$idheads($id) eq $name} {
1858 unset idheads($id)
1859 } else {
1860 set i [lsearch -exact $idheads($id) $name]
1861 if {$i >= 0} {
1862 set idheads($id) [lreplace $idheads($id) $i $i]
1865 unset headids($name)
1868 proc ttk_toplevel {w args} {
1869 global use_ttk
1870 eval [linsert $args 0 ::toplevel $w]
1871 if {$use_ttk} {
1872 place [ttk::frame $w._toplevel_background] -x 0 -y 0 -relwidth 1 -relheight 1
1874 return $w
1877 proc make_transient {window origin} {
1878 global have_tk85
1880 # In MacOS Tk 8.4 transient appears to work by setting
1881 # overrideredirect, which is utterly useless, since the
1882 # windows get no border, and are not even kept above
1883 # the parent.
1884 if {!$have_tk85 && [tk windowingsystem] eq {aqua}} return
1886 wm transient $window $origin
1888 # Windows fails to place transient windows normally, so
1889 # schedule a callback to center them on the parent.
1890 if {[tk windowingsystem] eq {win32}} {
1891 after idle [list tk::PlaceWindow $window widget $origin]
1895 proc show_error {w top msg {mc mc}} {
1896 global NS
1897 if {![info exists NS]} {set NS ""}
1898 if {[wm state $top] eq "withdrawn"} { wm deiconify $top }
1899 message $w.m -text $msg -justify center -aspect 400
1900 pack $w.m -side top -fill x -padx 20 -pady 20
1901 ${NS}::button $w.ok -default active -text [$mc OK] -command "destroy $top"
1902 pack $w.ok -side bottom -fill x
1903 bind $top <Visibility> "grab $top; focus $top"
1904 bind $top <Key-Return> "destroy $top"
1905 bind $top <Key-space> "destroy $top"
1906 bind $top <Key-Escape> "destroy $top"
1907 tkwait window $top
1910 proc error_popup {msg {owner .}} {
1911 if {[tk windowingsystem] eq "win32"} {
1912 tk_messageBox -icon error -type ok -title [wm title .] \
1913 -parent $owner -message $msg
1914 } else {
1915 set w .error
1916 ttk_toplevel $w
1917 make_transient $w $owner
1918 show_error $w $w $msg
1922 proc confirm_popup {msg {owner .}} {
1923 global confirm_ok NS
1924 set confirm_ok 0
1925 set w .confirm
1926 ttk_toplevel $w
1927 make_transient $w $owner
1928 message $w.m -text $msg -justify center -aspect 400
1929 pack $w.m -side top -fill x -padx 20 -pady 20
1930 ${NS}::button $w.ok -text [mc OK] -command "set confirm_ok 1; destroy $w"
1931 pack $w.ok -side left -fill x
1932 ${NS}::button $w.cancel -text [mc Cancel] -command "destroy $w"
1933 pack $w.cancel -side right -fill x
1934 bind $w <Visibility> "grab $w; focus $w"
1935 bind $w <Key-Return> "set confirm_ok 1; destroy $w"
1936 bind $w <Key-space> "set confirm_ok 1; destroy $w"
1937 bind $w <Key-Escape> "destroy $w"
1938 tk::PlaceWindow $w widget $owner
1939 tkwait window $w
1940 return $confirm_ok
1943 proc setoptions {} {
1944 if {[tk windowingsystem] ne "win32"} {
1945 option add *Panedwindow.showHandle 1 startupFile
1946 option add *Panedwindow.sashRelief raised startupFile
1947 if {[tk windowingsystem] ne "aqua"} {
1948 option add *Menu.font uifont startupFile
1950 } else {
1951 option add *Menu.TearOff 0 startupFile
1953 option add *Button.font uifont startupFile
1954 option add *Checkbutton.font uifont startupFile
1955 option add *Radiobutton.font uifont startupFile
1956 option add *Menubutton.font uifont startupFile
1957 option add *Label.font uifont startupFile
1958 option add *Message.font uifont startupFile
1959 option add *Entry.font textfont startupFile
1960 option add *Text.font textfont startupFile
1961 option add *Labelframe.font uifont startupFile
1962 option add *Spinbox.font textfont startupFile
1963 option add *Listbox.font mainfont startupFile
1966 # Make a menu and submenus.
1967 # m is the window name for the menu, items is the list of menu items to add.
1968 # Each item is a list {mc label type description options...}
1969 # mc is ignored; it's so we can put mc there to alert xgettext
1970 # label is the string that appears in the menu
1971 # type is cascade, command or radiobutton (should add checkbutton)
1972 # description depends on type; it's the sublist for cascade, the
1973 # command to invoke for command, or {variable value} for radiobutton
1974 proc makemenu {m items} {
1975 menu $m
1976 if {[tk windowingsystem] eq {aqua}} {
1977 set Meta1 Cmd
1978 } else {
1979 set Meta1 Ctrl
1981 foreach i $items {
1982 set name [mc [lindex $i 1]]
1983 set type [lindex $i 2]
1984 set thing [lindex $i 3]
1985 set params [list $type]
1986 if {$name ne {}} {
1987 set u [string first "&" [string map {&& x} $name]]
1988 lappend params -label [string map {&& & & {}} $name]
1989 if {$u >= 0} {
1990 lappend params -underline $u
1993 switch -- $type {
1994 "cascade" {
1995 set submenu [string tolower [string map {& ""} [lindex $i 1]]]
1996 lappend params -menu $m.$submenu
1998 "command" {
1999 lappend params -command $thing
2001 "radiobutton" {
2002 lappend params -variable [lindex $thing 0] \
2003 -value [lindex $thing 1]
2006 set tail [lrange $i 4 end]
2007 regsub -all {\yMeta1\y} $tail $Meta1 tail
2008 eval $m add $params $tail
2009 if {$type eq "cascade"} {
2010 makemenu $m.$submenu $thing
2015 # translate string and remove ampersands
2016 proc mca {str} {
2017 return [string map {&& & & {}} [mc $str]]
2020 proc cleardropsel {w} {
2021 $w selection clear
2023 proc makedroplist {w varname args} {
2024 global use_ttk
2025 if {$use_ttk} {
2026 set width 0
2027 foreach label $args {
2028 set cx [string length $label]
2029 if {$cx > $width} {set width $cx}
2031 set gm [ttk::combobox $w -width $width -state readonly\
2032 -textvariable $varname -values $args \
2033 -exportselection false]
2034 bind $gm <<ComboboxSelected>> [list $gm selection clear]
2035 } else {
2036 set gm [eval [linsert $args 0 tk_optionMenu $w $varname]]
2038 return $gm
2041 proc makewindow {} {
2042 global canv canv2 canv3 linespc charspc ctext cflist cscroll
2043 global tabstop
2044 global findtype findtypemenu findloc findstring fstring geometry
2045 global entries sha1entry sha1string sha1but
2046 global diffcontextstring diffcontext
2047 global ignorespace
2048 global maincursor textcursor curtextcursor
2049 global rowctxmenu fakerowmenu mergemax wrapcomment
2050 global highlight_files gdttype
2051 global searchstring sstring
2052 global bgcolor fgcolor bglist fglist diffcolors selectbgcolor
2053 global uifgcolor uifgdisabledcolor
2054 global filesepbgcolor filesepfgcolor
2055 global mergecolors foundbgcolor currentsearchhitbgcolor
2056 global headctxmenu progresscanv progressitem progresscoords statusw
2057 global fprogitem fprogcoord lastprogupdate progupdatepending
2058 global rprogitem rprogcoord rownumsel numcommits
2059 global have_tk85 use_ttk NS
2060 global git_version
2061 global worddiff
2063 # The "mc" arguments here are purely so that xgettext
2064 # sees the following string as needing to be translated
2065 set file {
2066 mc "File" cascade {
2067 {mc "Update" command updatecommits -accelerator F5}
2068 {mc "Reload" command reloadcommits -accelerator Shift-F5}
2069 {mc "Reread references" command rereadrefs}
2070 {mc "List references" command showrefs -accelerator F2}
2071 {xx "" separator}
2072 {mc "Start git gui" command {exec git gui &}}
2073 {xx "" separator}
2074 {mc "Quit" command doquit -accelerator Meta1-Q}
2076 set edit {
2077 mc "Edit" cascade {
2078 {mc "Preferences" command doprefs}
2080 set view {
2081 mc "View" cascade {
2082 {mc "New view..." command {newview 0} -accelerator Shift-F4}
2083 {mc "Edit view..." command editview -state disabled -accelerator F4}
2084 {mc "Delete view" command delview -state disabled}
2085 {xx "" separator}
2086 {mc "All files" radiobutton {selectedview 0} -command {showview 0}}
2088 if {[tk windowingsystem] ne "aqua"} {
2089 set help {
2090 mc "Help" cascade {
2091 {mc "About gitk" command about}
2092 {mc "Key bindings" command keys}
2094 set bar [list $file $edit $view $help]
2095 } else {
2096 proc ::tk::mac::ShowPreferences {} {doprefs}
2097 proc ::tk::mac::Quit {} {doquit}
2098 lset file end [lreplace [lindex $file end] end-1 end]
2099 set apple {
2100 xx "Apple" cascade {
2101 {mc "About gitk" command about}
2102 {xx "" separator}
2104 set help {
2105 mc "Help" cascade {
2106 {mc "Key bindings" command keys}
2108 set bar [list $apple $file $view $help]
2110 makemenu .bar $bar
2111 . configure -menu .bar
2113 if {$use_ttk} {
2114 # cover the non-themed toplevel with a themed frame.
2115 place [ttk::frame ._main_background] -x 0 -y 0 -relwidth 1 -relheight 1
2118 # the gui has upper and lower half, parts of a paned window.
2119 ${NS}::panedwindow .ctop -orient vertical
2121 # possibly use assumed geometry
2122 if {![info exists geometry(pwsash0)]} {
2123 set geometry(topheight) [expr {15 * $linespc}]
2124 set geometry(topwidth) [expr {80 * $charspc}]
2125 set geometry(botheight) [expr {15 * $linespc}]
2126 set geometry(botwidth) [expr {50 * $charspc}]
2127 set geometry(pwsash0) [list [expr {40 * $charspc}] 2]
2128 set geometry(pwsash1) [list [expr {60 * $charspc}] 2]
2131 # the upper half will have a paned window, a scroll bar to the right, and some stuff below
2132 ${NS}::frame .tf -height $geometry(topheight) -width $geometry(topwidth)
2133 ${NS}::frame .tf.histframe
2134 ${NS}::panedwindow .tf.histframe.pwclist -orient horizontal
2135 if {!$use_ttk} {
2136 .tf.histframe.pwclist configure -sashpad 0 -handlesize 4
2139 # create three canvases
2140 set cscroll .tf.histframe.csb
2141 set canv .tf.histframe.pwclist.canv
2142 canvas $canv \
2143 -selectbackground $selectbgcolor \
2144 -background $bgcolor -bd 0 \
2145 -yscrollincr $linespc -yscrollcommand "scrollcanv $cscroll"
2146 .tf.histframe.pwclist add $canv
2147 set canv2 .tf.histframe.pwclist.canv2
2148 canvas $canv2 \
2149 -selectbackground $selectbgcolor \
2150 -background $bgcolor -bd 0 -yscrollincr $linespc
2151 .tf.histframe.pwclist add $canv2
2152 set canv3 .tf.histframe.pwclist.canv3
2153 canvas $canv3 \
2154 -selectbackground $selectbgcolor \
2155 -background $bgcolor -bd 0 -yscrollincr $linespc
2156 .tf.histframe.pwclist add $canv3
2157 if {$use_ttk} {
2158 bind .tf.histframe.pwclist <Map> {
2159 bind %W <Map> {}
2160 .tf.histframe.pwclist sashpos 1 [lindex $::geometry(pwsash1) 0]
2161 .tf.histframe.pwclist sashpos 0 [lindex $::geometry(pwsash0) 0]
2163 } else {
2164 eval .tf.histframe.pwclist sash place 0 $geometry(pwsash0)
2165 eval .tf.histframe.pwclist sash place 1 $geometry(pwsash1)
2168 # a scroll bar to rule them
2169 ${NS}::scrollbar $cscroll -command {allcanvs yview}
2170 if {!$use_ttk} {$cscroll configure -highlightthickness 0}
2171 pack $cscroll -side right -fill y
2172 bind .tf.histframe.pwclist <Configure> {resizeclistpanes %W %w}
2173 lappend bglist $canv $canv2 $canv3
2174 pack .tf.histframe.pwclist -fill both -expand 1 -side left
2176 # we have two button bars at bottom of top frame. Bar 1
2177 ${NS}::frame .tf.bar
2178 ${NS}::frame .tf.lbar -height 15
2180 set sha1entry .tf.bar.sha1
2181 set entries $sha1entry
2182 set sha1but .tf.bar.sha1label
2183 button $sha1but -text "[mc "SHA1 ID:"] " -state disabled -relief flat \
2184 -command gotocommit -width 8
2185 $sha1but conf -disabledforeground [$sha1but cget -foreground]
2186 pack .tf.bar.sha1label -side left
2187 ${NS}::entry $sha1entry -width 40 -font textfont -textvariable sha1string
2188 trace add variable sha1string write sha1change
2189 pack $sha1entry -side left -pady 2
2191 set bm_left_data {
2192 #define left_width 16
2193 #define left_height 16
2194 static unsigned char left_bits[] = {
2195 0x00, 0x00, 0xc0, 0x01, 0xe0, 0x00, 0x70, 0x00, 0x38, 0x00, 0x1c, 0x00,
2196 0x0e, 0x00, 0xff, 0x7f, 0xff, 0x7f, 0xff, 0x7f, 0x0e, 0x00, 0x1c, 0x00,
2197 0x38, 0x00, 0x70, 0x00, 0xe0, 0x00, 0xc0, 0x01};
2199 set bm_right_data {
2200 #define right_width 16
2201 #define right_height 16
2202 static unsigned char right_bits[] = {
2203 0x00, 0x00, 0xc0, 0x01, 0x80, 0x03, 0x00, 0x07, 0x00, 0x0e, 0x00, 0x1c,
2204 0x00, 0x38, 0xff, 0x7f, 0xff, 0x7f, 0xff, 0x7f, 0x00, 0x38, 0x00, 0x1c,
2205 0x00, 0x0e, 0x00, 0x07, 0x80, 0x03, 0xc0, 0x01};
2207 image create bitmap bm-left -data $bm_left_data -foreground $uifgcolor
2208 image create bitmap bm-left-gray -data $bm_left_data -foreground $uifgdisabledcolor
2209 image create bitmap bm-right -data $bm_right_data -foreground $uifgcolor
2210 image create bitmap bm-right-gray -data $bm_right_data -foreground $uifgdisabledcolor
2212 ${NS}::button .tf.bar.leftbut -command goback -state disabled -width 26
2213 if {$use_ttk} {
2214 .tf.bar.leftbut configure -image [list bm-left disabled bm-left-gray]
2215 } else {
2216 .tf.bar.leftbut configure -image bm-left
2218 pack .tf.bar.leftbut -side left -fill y
2219 ${NS}::button .tf.bar.rightbut -command goforw -state disabled -width 26
2220 if {$use_ttk} {
2221 .tf.bar.rightbut configure -image [list bm-right disabled bm-right-gray]
2222 } else {
2223 .tf.bar.rightbut configure -image bm-right
2225 pack .tf.bar.rightbut -side left -fill y
2227 ${NS}::label .tf.bar.rowlabel -text [mc "Row"]
2228 set rownumsel {}
2229 ${NS}::label .tf.bar.rownum -width 7 -textvariable rownumsel \
2230 -relief sunken -anchor e
2231 ${NS}::label .tf.bar.rowlabel2 -text "/"
2232 ${NS}::label .tf.bar.numcommits -width 7 -textvariable numcommits \
2233 -relief sunken -anchor e
2234 pack .tf.bar.rowlabel .tf.bar.rownum .tf.bar.rowlabel2 .tf.bar.numcommits \
2235 -side left
2236 if {!$use_ttk} {
2237 foreach w {rownum numcommits} {.tf.bar.$w configure -font textfont}
2239 global selectedline
2240 trace add variable selectedline write selectedline_change
2242 # Status label and progress bar
2243 set statusw .tf.bar.status
2244 ${NS}::label $statusw -width 15 -relief sunken
2245 pack $statusw -side left -padx 5
2246 if {$use_ttk} {
2247 set progresscanv [ttk::progressbar .tf.bar.progress]
2248 } else {
2249 set h [expr {[font metrics uifont -linespace] + 2}]
2250 set progresscanv .tf.bar.progress
2251 canvas $progresscanv -relief sunken -height $h -borderwidth 2
2252 set progressitem [$progresscanv create rect -1 0 0 $h -fill green]
2253 set fprogitem [$progresscanv create rect -1 0 0 $h -fill yellow]
2254 set rprogitem [$progresscanv create rect -1 0 0 $h -fill red]
2256 pack $progresscanv -side right -expand 1 -fill x -padx {0 2}
2257 set progresscoords {0 0}
2258 set fprogcoord 0
2259 set rprogcoord 0
2260 bind $progresscanv <Configure> adjustprogress
2261 set lastprogupdate [clock clicks -milliseconds]
2262 set progupdatepending 0
2264 # build up the bottom bar of upper window
2265 ${NS}::label .tf.lbar.flabel -text "[mc "Find"] "
2267 set bm_down_data {
2268 #define down_width 16
2269 #define down_height 16
2270 static unsigned char down_bits[] = {
2271 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01,
2272 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01,
2273 0x87, 0xe1, 0x8e, 0x71, 0x9c, 0x39, 0xb8, 0x1d,
2274 0xf0, 0x0f, 0xe0, 0x07, 0xc0, 0x03, 0x80, 0x01};
2276 image create bitmap bm-down -data $bm_down_data -foreground $uifgcolor
2277 ${NS}::button .tf.lbar.fnext -width 26 -command {dofind 1 1}
2278 .tf.lbar.fnext configure -image bm-down
2280 set bm_up_data {
2281 #define up_width 16
2282 #define up_height 16
2283 static unsigned char up_bits[] = {
2284 0x80, 0x01, 0xc0, 0x03, 0xe0, 0x07, 0xf0, 0x0f,
2285 0xb8, 0x1d, 0x9c, 0x39, 0x8e, 0x71, 0x87, 0xe1,
2286 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01,
2287 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01};
2289 image create bitmap bm-up -data $bm_up_data -foreground $uifgcolor
2290 ${NS}::button .tf.lbar.fprev -width 26 -command {dofind -1 1}
2291 .tf.lbar.fprev configure -image bm-up
2293 ${NS}::label .tf.lbar.flab2 -text " [mc "commit"] "
2295 pack .tf.lbar.flabel .tf.lbar.fnext .tf.lbar.fprev .tf.lbar.flab2 \
2296 -side left -fill y
2297 set gdttype [mc "containing:"]
2298 set gm [makedroplist .tf.lbar.gdttype gdttype \
2299 [mc "containing:"] \
2300 [mc "touching paths:"] \
2301 [mc "adding/removing string:"] \
2302 [mc "changing lines matching:"]]
2303 trace add variable gdttype write gdttype_change
2304 pack .tf.lbar.gdttype -side left -fill y
2306 set findstring {}
2307 set fstring .tf.lbar.findstring
2308 lappend entries $fstring
2309 ${NS}::entry $fstring -width 30 -textvariable findstring
2310 trace add variable findstring write find_change
2311 set findtype [mc "Exact"]
2312 set findtypemenu [makedroplist .tf.lbar.findtype \
2313 findtype [mc "Exact"] [mc "IgnCase"] [mc "Regexp"]]
2314 trace add variable findtype write findcom_change
2315 set findloc [mc "All fields"]
2316 makedroplist .tf.lbar.findloc findloc [mc "All fields"] [mc "Headline"] \
2317 [mc "Comments"] [mc "Author"] [mc "Committer"]
2318 trace add variable findloc write find_change
2319 pack .tf.lbar.findloc -side right
2320 pack .tf.lbar.findtype -side right
2321 pack $fstring -side left -expand 1 -fill x
2323 # Finish putting the upper half of the viewer together
2324 pack .tf.lbar -in .tf -side bottom -fill x
2325 pack .tf.bar -in .tf -side bottom -fill x
2326 pack .tf.histframe -fill both -side top -expand 1
2327 .ctop add .tf
2328 if {!$use_ttk} {
2329 .ctop paneconfigure .tf -height $geometry(topheight)
2330 .ctop paneconfigure .tf -width $geometry(topwidth)
2333 # now build up the bottom
2334 ${NS}::panedwindow .pwbottom -orient horizontal
2336 # lower left, a text box over search bar, scroll bar to the right
2337 # if we know window height, then that will set the lower text height, otherwise
2338 # we set lower text height which will drive window height
2339 if {[info exists geometry(main)]} {
2340 ${NS}::frame .bleft -width $geometry(botwidth)
2341 } else {
2342 ${NS}::frame .bleft -width $geometry(botwidth) -height $geometry(botheight)
2344 ${NS}::frame .bleft.top
2345 ${NS}::frame .bleft.mid
2346 ${NS}::frame .bleft.bottom
2348 ${NS}::button .bleft.top.search -text [mc "Search"] -command dosearch
2349 pack .bleft.top.search -side left -padx 5
2350 set sstring .bleft.top.sstring
2351 set searchstring ""
2352 ${NS}::entry $sstring -width 20 -textvariable searchstring
2353 lappend entries $sstring
2354 trace add variable searchstring write incrsearch
2355 pack $sstring -side left -expand 1 -fill x
2356 ${NS}::radiobutton .bleft.mid.diff -text [mc "Diff"] \
2357 -command changediffdisp -variable diffelide -value {0 0}
2358 ${NS}::radiobutton .bleft.mid.old -text [mc "Old version"] \
2359 -command changediffdisp -variable diffelide -value {0 1}
2360 ${NS}::radiobutton .bleft.mid.new -text [mc "New version"] \
2361 -command changediffdisp -variable diffelide -value {1 0}
2362 ${NS}::label .bleft.mid.labeldiffcontext -text " [mc "Lines of context"]: "
2363 pack .bleft.mid.diff .bleft.mid.old .bleft.mid.new -side left
2364 spinbox .bleft.mid.diffcontext -width 5 \
2365 -from 0 -increment 1 -to 10000000 \
2366 -validate all -validatecommand "diffcontextvalidate %P" \
2367 -textvariable diffcontextstring
2368 .bleft.mid.diffcontext set $diffcontext
2369 trace add variable diffcontextstring write diffcontextchange
2370 lappend entries .bleft.mid.diffcontext
2371 pack .bleft.mid.labeldiffcontext .bleft.mid.diffcontext -side left
2372 ${NS}::checkbutton .bleft.mid.ignspace -text [mc "Ignore space change"] \
2373 -command changeignorespace -variable ignorespace
2374 pack .bleft.mid.ignspace -side left -padx 5
2376 set worddiff [mc "Line diff"]
2377 if {[package vcompare $git_version "1.7.2"] >= 0} {
2378 makedroplist .bleft.mid.worddiff worddiff [mc "Line diff"] \
2379 [mc "Markup words"] [mc "Color words"]
2380 trace add variable worddiff write changeworddiff
2381 pack .bleft.mid.worddiff -side left -padx 5
2384 set ctext .bleft.bottom.ctext
2385 text $ctext -background $bgcolor -foreground $fgcolor \
2386 -state disabled -font textfont \
2387 -yscrollcommand scrolltext -wrap none \
2388 -xscrollcommand ".bleft.bottom.sbhorizontal set"
2389 if {$have_tk85} {
2390 $ctext conf -tabstyle wordprocessor
2392 ${NS}::scrollbar .bleft.bottom.sb -command "$ctext yview"
2393 ${NS}::scrollbar .bleft.bottom.sbhorizontal -command "$ctext xview" -orient h
2394 pack .bleft.top -side top -fill x
2395 pack .bleft.mid -side top -fill x
2396 grid $ctext .bleft.bottom.sb -sticky nsew
2397 grid .bleft.bottom.sbhorizontal -sticky ew
2398 grid columnconfigure .bleft.bottom 0 -weight 1
2399 grid rowconfigure .bleft.bottom 0 -weight 1
2400 grid rowconfigure .bleft.bottom 1 -weight 0
2401 pack .bleft.bottom -side top -fill both -expand 1
2402 lappend bglist $ctext
2403 lappend fglist $ctext
2405 $ctext tag conf comment -wrap $wrapcomment
2406 $ctext tag conf filesep -font textfontbold -fore $filesepfgcolor -back $filesepbgcolor
2407 $ctext tag conf hunksep -fore [lindex $diffcolors 2]
2408 $ctext tag conf d0 -fore [lindex $diffcolors 0]
2409 $ctext tag conf dresult -fore [lindex $diffcolors 1]
2410 $ctext tag conf m0 -fore [lindex $mergecolors 0]
2411 $ctext tag conf m1 -fore [lindex $mergecolors 1]
2412 $ctext tag conf m2 -fore [lindex $mergecolors 2]
2413 $ctext tag conf m3 -fore [lindex $mergecolors 3]
2414 $ctext tag conf m4 -fore [lindex $mergecolors 4]
2415 $ctext tag conf m5 -fore [lindex $mergecolors 5]
2416 $ctext tag conf m6 -fore [lindex $mergecolors 6]
2417 $ctext tag conf m7 -fore [lindex $mergecolors 7]
2418 $ctext tag conf m8 -fore [lindex $mergecolors 8]
2419 $ctext tag conf m9 -fore [lindex $mergecolors 9]
2420 $ctext tag conf m10 -fore [lindex $mergecolors 10]
2421 $ctext tag conf m11 -fore [lindex $mergecolors 11]
2422 $ctext tag conf m12 -fore [lindex $mergecolors 12]
2423 $ctext tag conf m13 -fore [lindex $mergecolors 13]
2424 $ctext tag conf m14 -fore [lindex $mergecolors 14]
2425 $ctext tag conf m15 -fore [lindex $mergecolors 15]
2426 $ctext tag conf mmax -fore darkgrey
2427 set mergemax 16
2428 $ctext tag conf mresult -font textfontbold
2429 $ctext tag conf msep -font textfontbold
2430 $ctext tag conf found -back $foundbgcolor
2431 $ctext tag conf currentsearchhit -back $currentsearchhitbgcolor
2432 $ctext tag conf wwrap -wrap word -lmargin2 1c
2433 $ctext tag conf bold -font textfontbold
2435 .pwbottom add .bleft
2436 if {!$use_ttk} {
2437 .pwbottom paneconfigure .bleft -width $geometry(botwidth)
2440 # lower right
2441 ${NS}::frame .bright
2442 ${NS}::frame .bright.mode
2443 ${NS}::radiobutton .bright.mode.patch -text [mc "Patch"] \
2444 -command reselectline -variable cmitmode -value "patch"
2445 ${NS}::radiobutton .bright.mode.tree -text [mc "Tree"] \
2446 -command reselectline -variable cmitmode -value "tree"
2447 grid .bright.mode.patch .bright.mode.tree -sticky ew
2448 pack .bright.mode -side top -fill x
2449 set cflist .bright.cfiles
2450 set indent [font measure mainfont "nn"]
2451 text $cflist \
2452 -selectbackground $selectbgcolor \
2453 -background $bgcolor -foreground $fgcolor \
2454 -font mainfont \
2455 -tabs [list $indent [expr {2 * $indent}]] \
2456 -yscrollcommand ".bright.sb set" \
2457 -cursor [. cget -cursor] \
2458 -spacing1 1 -spacing3 1
2459 lappend bglist $cflist
2460 lappend fglist $cflist
2461 ${NS}::scrollbar .bright.sb -command "$cflist yview"
2462 pack .bright.sb -side right -fill y
2463 pack $cflist -side left -fill both -expand 1
2464 $cflist tag configure highlight \
2465 -background [$cflist cget -selectbackground]
2466 $cflist tag configure bold -font mainfontbold
2468 .pwbottom add .bright
2469 .ctop add .pwbottom
2471 # restore window width & height if known
2472 if {[info exists geometry(main)]} {
2473 if {[scan $geometry(main) "%dx%d" w h] >= 2} {
2474 if {$w > [winfo screenwidth .]} {
2475 set w [winfo screenwidth .]
2477 if {$h > [winfo screenheight .]} {
2478 set h [winfo screenheight .]
2480 wm geometry . "${w}x$h"
2484 if {[info exists geometry(state)] && $geometry(state) eq "zoomed"} {
2485 wm state . $geometry(state)
2488 if {[tk windowingsystem] eq {aqua}} {
2489 set M1B M1
2490 set ::BM "3"
2491 } else {
2492 set M1B Control
2493 set ::BM "2"
2496 if {$use_ttk} {
2497 bind .ctop <Map> {
2498 bind %W <Map> {}
2499 %W sashpos 0 $::geometry(topheight)
2501 bind .pwbottom <Map> {
2502 bind %W <Map> {}
2503 %W sashpos 0 $::geometry(botwidth)
2507 bind .pwbottom <Configure> {resizecdetpanes %W %w}
2508 pack .ctop -fill both -expand 1
2509 bindall <1> {selcanvline %W %x %y}
2510 #bindall <B1-Motion> {selcanvline %W %x %y}
2511 if {[tk windowingsystem] == "win32"} {
2512 bind . <MouseWheel> { windows_mousewheel_redirector %W %X %Y %D }
2513 bind $ctext <MouseWheel> { windows_mousewheel_redirector %W %X %Y %D ; break }
2514 } else {
2515 bindall <ButtonRelease-4> "allcanvs yview scroll -5 units"
2516 bindall <ButtonRelease-5> "allcanvs yview scroll 5 units"
2517 if {[tk windowingsystem] eq "aqua"} {
2518 bindall <MouseWheel> {
2519 set delta [expr {- (%D)}]
2520 allcanvs yview scroll $delta units
2522 bindall <Shift-MouseWheel> {
2523 set delta [expr {- (%D)}]
2524 $canv xview scroll $delta units
2528 bindall <$::BM> "canvscan mark %W %x %y"
2529 bindall <B$::BM-Motion> "canvscan dragto %W %x %y"
2530 bind all <$M1B-Key-w> {destroy [winfo toplevel %W]}
2531 bind . <$M1B-Key-w> doquit
2532 bindkey <Home> selfirstline
2533 bindkey <End> sellastline
2534 bind . <Key-Up> "selnextline -1"
2535 bind . <Key-Down> "selnextline 1"
2536 bind . <Shift-Key-Up> "dofind -1 0"
2537 bind . <Shift-Key-Down> "dofind 1 0"
2538 bindkey <Key-Right> "goforw"
2539 bindkey <Key-Left> "goback"
2540 bind . <Key-Prior> "selnextpage -1"
2541 bind . <Key-Next> "selnextpage 1"
2542 bind . <$M1B-Home> "allcanvs yview moveto 0.0"
2543 bind . <$M1B-End> "allcanvs yview moveto 1.0"
2544 bind . <$M1B-Key-Up> "allcanvs yview scroll -1 units"
2545 bind . <$M1B-Key-Down> "allcanvs yview scroll 1 units"
2546 bind . <$M1B-Key-Prior> "allcanvs yview scroll -1 pages"
2547 bind . <$M1B-Key-Next> "allcanvs yview scroll 1 pages"
2548 bindkey <Key-Delete> "$ctext yview scroll -1 pages"
2549 bindkey <Key-BackSpace> "$ctext yview scroll -1 pages"
2550 bindkey <Key-space> "$ctext yview scroll 1 pages"
2551 bindkey p "selnextline -1"
2552 bindkey n "selnextline 1"
2553 bindkey z "goback"
2554 bindkey x "goforw"
2555 bindkey k "selnextline -1"
2556 bindkey j "selnextline 1"
2557 bindkey h "goback"
2558 bindkey l "goforw"
2559 bindkey b prevfile
2560 bindkey d "$ctext yview scroll 18 units"
2561 bindkey u "$ctext yview scroll -18 units"
2562 bindkey / {focus $fstring}
2563 bindkey <Key-KP_Divide> {focus $fstring}
2564 bindkey <Key-Return> {dofind 1 1}
2565 bindkey ? {dofind -1 1}
2566 bindkey f nextfile
2567 bind . <F5> updatecommits
2568 bindmodfunctionkey Shift 5 reloadcommits
2569 bind . <F2> showrefs
2570 bindmodfunctionkey Shift 4 {newview 0}
2571 bind . <F4> edit_or_newview
2572 bind . <$M1B-q> doquit
2573 bind . <$M1B-f> {dofind 1 1}
2574 bind . <$M1B-g> {dofind 1 0}
2575 bind . <$M1B-r> dosearchback
2576 bind . <$M1B-s> dosearch
2577 bind . <$M1B-equal> {incrfont 1}
2578 bind . <$M1B-plus> {incrfont 1}
2579 bind . <$M1B-KP_Add> {incrfont 1}
2580 bind . <$M1B-minus> {incrfont -1}
2581 bind . <$M1B-KP_Subtract> {incrfont -1}
2582 wm protocol . WM_DELETE_WINDOW doquit
2583 bind . <Destroy> {stop_backends}
2584 bind . <Button-1> "click %W"
2585 bind $fstring <Key-Return> {dofind 1 1}
2586 bind $sha1entry <Key-Return> {gotocommit; break}
2587 bind $sha1entry <<PasteSelection>> clearsha1
2588 bind $sha1entry <<Paste>> clearsha1
2589 bind $cflist <1> {sel_flist %W %x %y; break}
2590 bind $cflist <B1-Motion> {sel_flist %W %x %y; break}
2591 bind $cflist <ButtonRelease-1> {treeclick %W %x %y}
2592 global ctxbut
2593 bind $cflist $ctxbut {pop_flist_menu %W %X %Y %x %y}
2594 bind $ctext $ctxbut {pop_diff_menu %W %X %Y %x %y}
2595 bind $ctext <Button-1> {focus %W}
2596 bind $ctext <<Selection>> rehighlight_search_results
2598 set maincursor [. cget -cursor]
2599 set textcursor [$ctext cget -cursor]
2600 set curtextcursor $textcursor
2602 set rowctxmenu .rowctxmenu
2603 makemenu $rowctxmenu {
2604 {mc "Diff this -> selected" command {diffvssel 0}}
2605 {mc "Diff selected -> this" command {diffvssel 1}}
2606 {mc "Make patch" command mkpatch}
2607 {mc "Create tag" command mktag}
2608 {mc "Write commit to file" command writecommit}
2609 {mc "Create new branch" command mkbranch}
2610 {mc "Cherry-pick this commit" command cherrypick}
2611 {mc "Reset HEAD branch to here" command resethead}
2612 {mc "Mark this commit" command markhere}
2613 {mc "Return to mark" command gotomark}
2614 {mc "Find descendant of this and mark" command find_common_desc}
2615 {mc "Compare with marked commit" command compare_commits}
2616 {mc "Diff this -> marked commit" command {diffvsmark 0}}
2617 {mc "Diff marked commit -> this" command {diffvsmark 1}}
2618 {mc "Revert this commit" command revert}
2620 $rowctxmenu configure -tearoff 0
2622 set fakerowmenu .fakerowmenu
2623 makemenu $fakerowmenu {
2624 {mc "Diff this -> selected" command {diffvssel 0}}
2625 {mc "Diff selected -> this" command {diffvssel 1}}
2626 {mc "Make patch" command mkpatch}
2627 {mc "Diff this -> marked commit" command {diffvsmark 0}}
2628 {mc "Diff marked commit -> this" command {diffvsmark 1}}
2630 $fakerowmenu configure -tearoff 0
2632 set headctxmenu .headctxmenu
2633 makemenu $headctxmenu {
2634 {mc "Check out this branch" command cobranch}
2635 {mc "Remove this branch" command rmbranch}
2637 $headctxmenu configure -tearoff 0
2639 global flist_menu
2640 set flist_menu .flistctxmenu
2641 makemenu $flist_menu {
2642 {mc "Highlight this too" command {flist_hl 0}}
2643 {mc "Highlight this only" command {flist_hl 1}}
2644 {mc "External diff" command {external_diff}}
2645 {mc "Blame parent commit" command {external_blame 1}}
2647 $flist_menu configure -tearoff 0
2649 global diff_menu
2650 set diff_menu .diffctxmenu
2651 makemenu $diff_menu {
2652 {mc "Show origin of this line" command show_line_source}
2653 {mc "Run git gui blame on this line" command {external_blame_diff}}
2655 $diff_menu configure -tearoff 0
2658 # Windows sends all mouse wheel events to the current focused window, not
2659 # the one where the mouse hovers, so bind those events here and redirect
2660 # to the correct window
2661 proc windows_mousewheel_redirector {W X Y D} {
2662 global canv canv2 canv3
2663 set w [winfo containing -displayof $W $X $Y]
2664 if {$w ne ""} {
2665 set u [expr {$D < 0 ? 5 : -5}]
2666 if {$w == $canv || $w == $canv2 || $w == $canv3} {
2667 allcanvs yview scroll $u units
2668 } else {
2669 catch {
2670 $w yview scroll $u units
2676 # Update row number label when selectedline changes
2677 proc selectedline_change {n1 n2 op} {
2678 global selectedline rownumsel
2680 if {$selectedline eq {}} {
2681 set rownumsel {}
2682 } else {
2683 set rownumsel [expr {$selectedline + 1}]
2687 # mouse-2 makes all windows scan vertically, but only the one
2688 # the cursor is in scans horizontally
2689 proc canvscan {op w x y} {
2690 global canv canv2 canv3
2691 foreach c [list $canv $canv2 $canv3] {
2692 if {$c == $w} {
2693 $c scan $op $x $y
2694 } else {
2695 $c scan $op 0 $y
2700 proc scrollcanv {cscroll f0 f1} {
2701 $cscroll set $f0 $f1
2702 drawvisible
2703 flushhighlights
2706 # when we make a key binding for the toplevel, make sure
2707 # it doesn't get triggered when that key is pressed in the
2708 # find string entry widget.
2709 proc bindkey {ev script} {
2710 global entries
2711 bind . $ev $script
2712 set escript [bind Entry $ev]
2713 if {$escript == {}} {
2714 set escript [bind Entry <Key>]
2716 foreach e $entries {
2717 bind $e $ev "$escript; break"
2721 proc bindmodfunctionkey {mod n script} {
2722 bind . <$mod-F$n> $script
2723 catch { bind . <$mod-XF86_Switch_VT_$n> $script }
2726 # set the focus back to the toplevel for any click outside
2727 # the entry widgets
2728 proc click {w} {
2729 global ctext entries
2730 foreach e [concat $entries $ctext] {
2731 if {$w == $e} return
2733 focus .
2736 # Adjust the progress bar for a change in requested extent or canvas size
2737 proc adjustprogress {} {
2738 global progresscanv progressitem progresscoords
2739 global fprogitem fprogcoord lastprogupdate progupdatepending
2740 global rprogitem rprogcoord use_ttk
2742 if {$use_ttk} {
2743 $progresscanv configure -value [expr {int($fprogcoord * 100)}]
2744 return
2747 set w [expr {[winfo width $progresscanv] - 4}]
2748 set x0 [expr {$w * [lindex $progresscoords 0]}]
2749 set x1 [expr {$w * [lindex $progresscoords 1]}]
2750 set h [winfo height $progresscanv]
2751 $progresscanv coords $progressitem $x0 0 $x1 $h
2752 $progresscanv coords $fprogitem 0 0 [expr {$w * $fprogcoord}] $h
2753 $progresscanv coords $rprogitem 0 0 [expr {$w * $rprogcoord}] $h
2754 set now [clock clicks -milliseconds]
2755 if {$now >= $lastprogupdate + 100} {
2756 set progupdatepending 0
2757 update
2758 } elseif {!$progupdatepending} {
2759 set progupdatepending 1
2760 after [expr {$lastprogupdate + 100 - $now}] doprogupdate
2764 proc doprogupdate {} {
2765 global lastprogupdate progupdatepending
2767 if {$progupdatepending} {
2768 set progupdatepending 0
2769 set lastprogupdate [clock clicks -milliseconds]
2770 update
2774 proc savestuff {w} {
2775 global canv canv2 canv3 mainfont textfont uifont tabstop
2776 global stuffsaved findmergefiles maxgraphpct
2777 global maxwidth showneartags showlocalchanges
2778 global viewname viewfiles viewargs viewargscmd viewperm nextviewnum
2779 global cmitmode wrapcomment datetimeformat limitdiffs
2780 global colors uicolor bgcolor fgcolor diffcolors diffcontext selectbgcolor
2781 global uifgcolor uifgdisabledcolor
2782 global headbgcolor headfgcolor headoutlinecolor remotebgcolor
2783 global tagbgcolor tagfgcolor tagoutlinecolor
2784 global reflinecolor filesepbgcolor filesepfgcolor
2785 global mergecolors foundbgcolor currentsearchhitbgcolor
2786 global linehoverbgcolor linehoverfgcolor linehoveroutlinecolor circlecolors
2787 global mainheadcirclecolor workingfilescirclecolor indexcirclecolor
2788 global linkfgcolor circleoutlinecolor
2789 global autoselect autosellen extdifftool perfile_attrs markbgcolor use_ttk
2790 global hideremotes want_ttk maxrefs visiblerefs
2791 global config_file config_file_tmp
2793 if {$stuffsaved} return
2794 if {![winfo viewable .]} return
2795 catch {
2796 if {[file exists $config_file_tmp]} {
2797 file delete -force $config_file_tmp
2799 set f [open $config_file_tmp w]
2800 if {$::tcl_platform(platform) eq {windows}} {
2801 file attributes $config_file_tmp -hidden true
2803 puts $f [list set mainfont $mainfont]
2804 puts $f [list set textfont $textfont]
2805 puts $f [list set uifont $uifont]
2806 puts $f [list set tabstop $tabstop]
2807 puts $f [list set findmergefiles $findmergefiles]
2808 puts $f [list set maxgraphpct $maxgraphpct]
2809 puts $f [list set maxwidth $maxwidth]
2810 puts $f [list set cmitmode $cmitmode]
2811 puts $f [list set wrapcomment $wrapcomment]
2812 puts $f [list set autoselect $autoselect]
2813 puts $f [list set autosellen $autosellen]
2814 puts $f [list set showneartags $showneartags]
2815 puts $f [list set maxrefs $maxrefs]
2816 puts $f [list set visiblerefs $visiblerefs]
2817 puts $f [list set hideremotes $hideremotes]
2818 puts $f [list set showlocalchanges $showlocalchanges]
2819 puts $f [list set datetimeformat $datetimeformat]
2820 puts $f [list set limitdiffs $limitdiffs]
2821 puts $f [list set uicolor $uicolor]
2822 puts $f [list set want_ttk $want_ttk]
2823 puts $f [list set bgcolor $bgcolor]
2824 puts $f [list set fgcolor $fgcolor]
2825 puts $f [list set uifgcolor $uifgcolor]
2826 puts $f [list set uifgdisabledcolor $uifgdisabledcolor]
2827 puts $f [list set colors $colors]
2828 puts $f [list set diffcolors $diffcolors]
2829 puts $f [list set mergecolors $mergecolors]
2830 puts $f [list set markbgcolor $markbgcolor]
2831 puts $f [list set diffcontext $diffcontext]
2832 puts $f [list set selectbgcolor $selectbgcolor]
2833 puts $f [list set foundbgcolor $foundbgcolor]
2834 puts $f [list set currentsearchhitbgcolor $currentsearchhitbgcolor]
2835 puts $f [list set extdifftool $extdifftool]
2836 puts $f [list set perfile_attrs $perfile_attrs]
2837 puts $f [list set headbgcolor $headbgcolor]
2838 puts $f [list set headfgcolor $headfgcolor]
2839 puts $f [list set headoutlinecolor $headoutlinecolor]
2840 puts $f [list set remotebgcolor $remotebgcolor]
2841 puts $f [list set tagbgcolor $tagbgcolor]
2842 puts $f [list set tagfgcolor $tagfgcolor]
2843 puts $f [list set tagoutlinecolor $tagoutlinecolor]
2844 puts $f [list set reflinecolor $reflinecolor]
2845 puts $f [list set filesepbgcolor $filesepbgcolor]
2846 puts $f [list set filesepfgcolor $filesepfgcolor]
2847 puts $f [list set linehoverbgcolor $linehoverbgcolor]
2848 puts $f [list set linehoverfgcolor $linehoverfgcolor]
2849 puts $f [list set linehoveroutlinecolor $linehoveroutlinecolor]
2850 puts $f [list set mainheadcirclecolor $mainheadcirclecolor]
2851 puts $f [list set workingfilescirclecolor $workingfilescirclecolor]
2852 puts $f [list set indexcirclecolor $indexcirclecolor]
2853 puts $f [list set circlecolors $circlecolors]
2854 puts $f [list set linkfgcolor $linkfgcolor]
2855 puts $f [list set circleoutlinecolor $circleoutlinecolor]
2857 puts $f "set geometry(main) [wm geometry .]"
2858 puts $f "set geometry(state) [wm state .]"
2859 puts $f "set geometry(topwidth) [winfo width .tf]"
2860 puts $f "set geometry(topheight) [winfo height .tf]"
2861 if {$use_ttk} {
2862 puts $f "set geometry(pwsash0) \"[.tf.histframe.pwclist sashpos 0] 1\""
2863 puts $f "set geometry(pwsash1) \"[.tf.histframe.pwclist sashpos 1] 1\""
2864 } else {
2865 puts $f "set geometry(pwsash0) \"[.tf.histframe.pwclist sash coord 0]\""
2866 puts $f "set geometry(pwsash1) \"[.tf.histframe.pwclist sash coord 1]\""
2868 puts $f "set geometry(botwidth) [winfo width .bleft]"
2869 puts $f "set geometry(botheight) [winfo height .bleft]"
2871 puts -nonewline $f "set permviews {"
2872 for {set v 0} {$v < $nextviewnum} {incr v} {
2873 if {$viewperm($v)} {
2874 puts $f "{[list $viewname($v) $viewfiles($v) $viewargs($v) $viewargscmd($v)]}"
2877 puts $f "}"
2878 close $f
2879 catch {file delete $config_file}
2880 file rename -force $config_file_tmp $config_file
2882 set stuffsaved 1
2885 proc resizeclistpanes {win w} {
2886 global oldwidth use_ttk
2887 if {[info exists oldwidth($win)]} {
2888 if {$use_ttk} {
2889 set s0 [$win sashpos 0]
2890 set s1 [$win sashpos 1]
2891 } else {
2892 set s0 [$win sash coord 0]
2893 set s1 [$win sash coord 1]
2895 if {$w < 60} {
2896 set sash0 [expr {int($w/2 - 2)}]
2897 set sash1 [expr {int($w*5/6 - 2)}]
2898 } else {
2899 set factor [expr {1.0 * $w / $oldwidth($win)}]
2900 set sash0 [expr {int($factor * [lindex $s0 0])}]
2901 set sash1 [expr {int($factor * [lindex $s1 0])}]
2902 if {$sash0 < 30} {
2903 set sash0 30
2905 if {$sash1 < $sash0 + 20} {
2906 set sash1 [expr {$sash0 + 20}]
2908 if {$sash1 > $w - 10} {
2909 set sash1 [expr {$w - 10}]
2910 if {$sash0 > $sash1 - 20} {
2911 set sash0 [expr {$sash1 - 20}]
2915 if {$use_ttk} {
2916 $win sashpos 0 $sash0
2917 $win sashpos 1 $sash1
2918 } else {
2919 $win sash place 0 $sash0 [lindex $s0 1]
2920 $win sash place 1 $sash1 [lindex $s1 1]
2923 set oldwidth($win) $w
2926 proc resizecdetpanes {win w} {
2927 global oldwidth use_ttk
2928 if {[info exists oldwidth($win)]} {
2929 if {$use_ttk} {
2930 set s0 [$win sashpos 0]
2931 } else {
2932 set s0 [$win sash coord 0]
2934 if {$w < 60} {
2935 set sash0 [expr {int($w*3/4 - 2)}]
2936 } else {
2937 set factor [expr {1.0 * $w / $oldwidth($win)}]
2938 set sash0 [expr {int($factor * [lindex $s0 0])}]
2939 if {$sash0 < 45} {
2940 set sash0 45
2942 if {$sash0 > $w - 15} {
2943 set sash0 [expr {$w - 15}]
2946 if {$use_ttk} {
2947 $win sashpos 0 $sash0
2948 } else {
2949 $win sash place 0 $sash0 [lindex $s0 1]
2952 set oldwidth($win) $w
2955 proc allcanvs args {
2956 global canv canv2 canv3
2957 eval $canv $args
2958 eval $canv2 $args
2959 eval $canv3 $args
2962 proc bindall {event action} {
2963 global canv canv2 canv3
2964 bind $canv $event $action
2965 bind $canv2 $event $action
2966 bind $canv3 $event $action
2969 proc about {} {
2970 global uifont NS
2971 set w .about
2972 if {[winfo exists $w]} {
2973 raise $w
2974 return
2976 ttk_toplevel $w
2977 wm title $w [mc "About gitk"]
2978 make_transient $w .
2979 message $w.m -text [mc "
2980 Gitk - a commit viewer for git
2982 Copyright \u00a9 2005-2014 Paul Mackerras
2984 Use and redistribute under the terms of the GNU General Public License"] \
2985 -justify center -aspect 400 -border 2 -bg white -relief groove
2986 pack $w.m -side top -fill x -padx 2 -pady 2
2987 ${NS}::button $w.ok -text [mc "Close"] -command "destroy $w" -default active
2988 pack $w.ok -side bottom
2989 bind $w <Visibility> "focus $w.ok"
2990 bind $w <Key-Escape> "destroy $w"
2991 bind $w <Key-Return> "destroy $w"
2992 tk::PlaceWindow $w widget .
2995 proc keys {} {
2996 global NS
2997 set w .keys
2998 if {[winfo exists $w]} {
2999 raise $w
3000 return
3002 if {[tk windowingsystem] eq {aqua}} {
3003 set M1T Cmd
3004 } else {
3005 set M1T Ctrl
3007 ttk_toplevel $w
3008 wm title $w [mc "Gitk key bindings"]
3009 make_transient $w .
3010 message $w.m -text "
3011 [mc "Gitk key bindings:"]
3013 [mc "<%s-Q> Quit" $M1T]
3014 [mc "<%s-W> Close window" $M1T]
3015 [mc "<Home> Move to first commit"]
3016 [mc "<End> Move to last commit"]
3017 [mc "<Up>, p, k Move up one commit"]
3018 [mc "<Down>, n, j Move down one commit"]
3019 [mc "<Left>, z, h Go back in history list"]
3020 [mc "<Right>, x, l Go forward in history list"]
3021 [mc "<PageUp> Move up one page in commit list"]
3022 [mc "<PageDown> Move down one page in commit list"]
3023 [mc "<%s-Home> Scroll to top of commit list" $M1T]
3024 [mc "<%s-End> Scroll to bottom of commit list" $M1T]
3025 [mc "<%s-Up> Scroll commit list up one line" $M1T]
3026 [mc "<%s-Down> Scroll commit list down one line" $M1T]
3027 [mc "<%s-PageUp> Scroll commit list up one page" $M1T]
3028 [mc "<%s-PageDown> Scroll commit list down one page" $M1T]
3029 [mc "<Shift-Up> Find backwards (upwards, later commits)"]
3030 [mc "<Shift-Down> Find forwards (downwards, earlier commits)"]
3031 [mc "<Delete>, b Scroll diff view up one page"]
3032 [mc "<Backspace> Scroll diff view up one page"]
3033 [mc "<Space> Scroll diff view down one page"]
3034 [mc "u Scroll diff view up 18 lines"]
3035 [mc "d Scroll diff view down 18 lines"]
3036 [mc "<%s-F> Find" $M1T]
3037 [mc "<%s-G> Move to next find hit" $M1T]
3038 [mc "<Return> Move to next find hit"]
3039 [mc "/ Focus the search box"]
3040 [mc "? Move to previous find hit"]
3041 [mc "f Scroll diff view to next file"]
3042 [mc "<%s-S> Search for next hit in diff view" $M1T]
3043 [mc "<%s-R> Search for previous hit in diff view" $M1T]
3044 [mc "<%s-KP+> Increase font size" $M1T]
3045 [mc "<%s-plus> Increase font size" $M1T]
3046 [mc "<%s-KP-> Decrease font size" $M1T]
3047 [mc "<%s-minus> Decrease font size" $M1T]
3048 [mc "<F5> Update"]
3050 -justify left -bg white -border 2 -relief groove
3051 pack $w.m -side top -fill both -padx 2 -pady 2
3052 ${NS}::button $w.ok -text [mc "Close"] -command "destroy $w" -default active
3053 bind $w <Key-Escape> [list destroy $w]
3054 pack $w.ok -side bottom
3055 bind $w <Visibility> "focus $w.ok"
3056 bind $w <Key-Escape> "destroy $w"
3057 bind $w <Key-Return> "destroy $w"
3060 # Procedures for manipulating the file list window at the
3061 # bottom right of the overall window.
3063 proc treeview {w l openlevs} {
3064 global treecontents treediropen treeheight treeparent treeindex
3066 set ix 0
3067 set treeindex() 0
3068 set lev 0
3069 set prefix {}
3070 set prefixend -1
3071 set prefendstack {}
3072 set htstack {}
3073 set ht 0
3074 set treecontents() {}
3075 $w conf -state normal
3076 foreach f $l {
3077 while {[string range $f 0 $prefixend] ne $prefix} {
3078 if {$lev <= $openlevs} {
3079 $w mark set e:$treeindex($prefix) "end -1c"
3080 $w mark gravity e:$treeindex($prefix) left
3082 set treeheight($prefix) $ht
3083 incr ht [lindex $htstack end]
3084 set htstack [lreplace $htstack end end]
3085 set prefixend [lindex $prefendstack end]
3086 set prefendstack [lreplace $prefendstack end end]
3087 set prefix [string range $prefix 0 $prefixend]
3088 incr lev -1
3090 set tail [string range $f [expr {$prefixend+1}] end]
3091 while {[set slash [string first "/" $tail]] >= 0} {
3092 lappend htstack $ht
3093 set ht 0
3094 lappend prefendstack $prefixend
3095 incr prefixend [expr {$slash + 1}]
3096 set d [string range $tail 0 $slash]
3097 lappend treecontents($prefix) $d
3098 set oldprefix $prefix
3099 append prefix $d
3100 set treecontents($prefix) {}
3101 set treeindex($prefix) [incr ix]
3102 set treeparent($prefix) $oldprefix
3103 set tail [string range $tail [expr {$slash+1}] end]
3104 if {$lev <= $openlevs} {
3105 set ht 1
3106 set treediropen($prefix) [expr {$lev < $openlevs}]
3107 set bm [expr {$lev == $openlevs? "tri-rt": "tri-dn"}]
3108 $w mark set d:$ix "end -1c"
3109 $w mark gravity d:$ix left
3110 set str "\n"
3111 for {set i 0} {$i < $lev} {incr i} {append str "\t"}
3112 $w insert end $str
3113 $w image create end -align center -image $bm -padx 1 \
3114 -name a:$ix
3115 $w insert end $d [highlight_tag $prefix]
3116 $w mark set s:$ix "end -1c"
3117 $w mark gravity s:$ix left
3119 incr lev
3121 if {$tail ne {}} {
3122 if {$lev <= $openlevs} {
3123 incr ht
3124 set str "\n"
3125 for {set i 0} {$i < $lev} {incr i} {append str "\t"}
3126 $w insert end $str
3127 $w insert end $tail [highlight_tag $f]
3129 lappend treecontents($prefix) $tail
3132 while {$htstack ne {}} {
3133 set treeheight($prefix) $ht
3134 incr ht [lindex $htstack end]
3135 set htstack [lreplace $htstack end end]
3136 set prefixend [lindex $prefendstack end]
3137 set prefendstack [lreplace $prefendstack end end]
3138 set prefix [string range $prefix 0 $prefixend]
3140 $w conf -state disabled
3143 proc linetoelt {l} {
3144 global treeheight treecontents
3146 set y 2
3147 set prefix {}
3148 while {1} {
3149 foreach e $treecontents($prefix) {
3150 if {$y == $l} {
3151 return "$prefix$e"
3153 set n 1
3154 if {[string index $e end] eq "/"} {
3155 set n $treeheight($prefix$e)
3156 if {$y + $n > $l} {
3157 append prefix $e
3158 incr y
3159 break
3162 incr y $n
3167 proc highlight_tree {y prefix} {
3168 global treeheight treecontents cflist
3170 foreach e $treecontents($prefix) {
3171 set path $prefix$e
3172 if {[highlight_tag $path] ne {}} {
3173 $cflist tag add bold $y.0 "$y.0 lineend"
3175 incr y
3176 if {[string index $e end] eq "/" && $treeheight($path) > 1} {
3177 set y [highlight_tree $y $path]
3180 return $y
3183 proc treeclosedir {w dir} {
3184 global treediropen treeheight treeparent treeindex
3186 set ix $treeindex($dir)
3187 $w conf -state normal
3188 $w delete s:$ix e:$ix
3189 set treediropen($dir) 0
3190 $w image configure a:$ix -image tri-rt
3191 $w conf -state disabled
3192 set n [expr {1 - $treeheight($dir)}]
3193 while {$dir ne {}} {
3194 incr treeheight($dir) $n
3195 set dir $treeparent($dir)
3199 proc treeopendir {w dir} {
3200 global treediropen treeheight treeparent treecontents treeindex
3202 set ix $treeindex($dir)
3203 $w conf -state normal
3204 $w image configure a:$ix -image tri-dn
3205 $w mark set e:$ix s:$ix
3206 $w mark gravity e:$ix right
3207 set lev 0
3208 set str "\n"
3209 set n [llength $treecontents($dir)]
3210 for {set x $dir} {$x ne {}} {set x $treeparent($x)} {
3211 incr lev
3212 append str "\t"
3213 incr treeheight($x) $n
3215 foreach e $treecontents($dir) {
3216 set de $dir$e
3217 if {[string index $e end] eq "/"} {
3218 set iy $treeindex($de)
3219 $w mark set d:$iy e:$ix
3220 $w mark gravity d:$iy left
3221 $w insert e:$ix $str
3222 set treediropen($de) 0
3223 $w image create e:$ix -align center -image tri-rt -padx 1 \
3224 -name a:$iy
3225 $w insert e:$ix $e [highlight_tag $de]
3226 $w mark set s:$iy e:$ix
3227 $w mark gravity s:$iy left
3228 set treeheight($de) 1
3229 } else {
3230 $w insert e:$ix $str
3231 $w insert e:$ix $e [highlight_tag $de]
3234 $w mark gravity e:$ix right
3235 $w conf -state disabled
3236 set treediropen($dir) 1
3237 set top [lindex [split [$w index @0,0] .] 0]
3238 set ht [$w cget -height]
3239 set l [lindex [split [$w index s:$ix] .] 0]
3240 if {$l < $top} {
3241 $w yview $l.0
3242 } elseif {$l + $n + 1 > $top + $ht} {
3243 set top [expr {$l + $n + 2 - $ht}]
3244 if {$l < $top} {
3245 set top $l
3247 $w yview $top.0
3251 proc treeclick {w x y} {
3252 global treediropen cmitmode ctext cflist cflist_top
3254 if {$cmitmode ne "tree"} return
3255 if {![info exists cflist_top]} return
3256 set l [lindex [split [$w index "@$x,$y"] "."] 0]
3257 $cflist tag remove highlight $cflist_top.0 "$cflist_top.0 lineend"
3258 $cflist tag add highlight $l.0 "$l.0 lineend"
3259 set cflist_top $l
3260 if {$l == 1} {
3261 $ctext yview 1.0
3262 return
3264 set e [linetoelt $l]
3265 if {[string index $e end] ne "/"} {
3266 showfile $e
3267 } elseif {$treediropen($e)} {
3268 treeclosedir $w $e
3269 } else {
3270 treeopendir $w $e
3274 proc setfilelist {id} {
3275 global treefilelist cflist jump_to_here
3277 treeview $cflist $treefilelist($id) 0
3278 if {$jump_to_here ne {}} {
3279 set f [lindex $jump_to_here 0]
3280 if {[lsearch -exact $treefilelist($id) $f] >= 0} {
3281 showfile $f
3286 image create bitmap tri-rt -background black -foreground blue -data {
3287 #define tri-rt_width 13
3288 #define tri-rt_height 13
3289 static unsigned char tri-rt_bits[] = {
3290 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x30, 0x00, 0x70, 0x00, 0xf0, 0x00,
3291 0xf0, 0x01, 0xf0, 0x00, 0x70, 0x00, 0x30, 0x00, 0x10, 0x00, 0x00, 0x00,
3292 0x00, 0x00};
3293 } -maskdata {
3294 #define tri-rt-mask_width 13
3295 #define tri-rt-mask_height 13
3296 static unsigned char tri-rt-mask_bits[] = {
3297 0x08, 0x00, 0x18, 0x00, 0x38, 0x00, 0x78, 0x00, 0xf8, 0x00, 0xf8, 0x01,
3298 0xf8, 0x03, 0xf8, 0x01, 0xf8, 0x00, 0x78, 0x00, 0x38, 0x00, 0x18, 0x00,
3299 0x08, 0x00};
3301 image create bitmap tri-dn -background black -foreground blue -data {
3302 #define tri-dn_width 13
3303 #define tri-dn_height 13
3304 static unsigned char tri-dn_bits[] = {
3305 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0x07, 0xf8, 0x03,
3306 0xf0, 0x01, 0xe0, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3307 0x00, 0x00};
3308 } -maskdata {
3309 #define tri-dn-mask_width 13
3310 #define tri-dn-mask_height 13
3311 static unsigned char tri-dn-mask_bits[] = {
3312 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x1f, 0xfe, 0x0f, 0xfc, 0x07,
3313 0xf8, 0x03, 0xf0, 0x01, 0xe0, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00,
3314 0x00, 0x00};
3317 image create bitmap reficon-T -background black -foreground yellow -data {
3318 #define tagicon_width 13
3319 #define tagicon_height 9
3320 static unsigned char tagicon_bits[] = {
3321 0x00, 0x00, 0x00, 0x00, 0xf0, 0x07, 0xf8, 0x07,
3322 0xfc, 0x07, 0xf8, 0x07, 0xf0, 0x07, 0x00, 0x00, 0x00, 0x00};
3323 } -maskdata {
3324 #define tagicon-mask_width 13
3325 #define tagicon-mask_height 9
3326 static unsigned char tagicon-mask_bits[] = {
3327 0x00, 0x00, 0xf0, 0x0f, 0xf8, 0x0f, 0xfc, 0x0f,
3328 0xfe, 0x0f, 0xfc, 0x0f, 0xf8, 0x0f, 0xf0, 0x0f, 0x00, 0x00};
3330 set rectdata {
3331 #define headicon_width 13
3332 #define headicon_height 9
3333 static unsigned char headicon_bits[] = {
3334 0x00, 0x00, 0x00, 0x00, 0xf8, 0x07, 0xf8, 0x07,
3335 0xf8, 0x07, 0xf8, 0x07, 0xf8, 0x07, 0x00, 0x00, 0x00, 0x00};
3337 set rectmask {
3338 #define headicon-mask_width 13
3339 #define headicon-mask_height 9
3340 static unsigned char headicon-mask_bits[] = {
3341 0x00, 0x00, 0xfc, 0x0f, 0xfc, 0x0f, 0xfc, 0x0f,
3342 0xfc, 0x0f, 0xfc, 0x0f, 0xfc, 0x0f, 0xfc, 0x0f, 0x00, 0x00};
3344 image create bitmap reficon-H -background black -foreground green \
3345 -data $rectdata -maskdata $rectmask
3346 image create bitmap reficon-o -background black -foreground "#ddddff" \
3347 -data $rectdata -maskdata $rectmask
3349 proc init_flist {first} {
3350 global cflist cflist_top difffilestart
3352 $cflist conf -state normal
3353 $cflist delete 0.0 end
3354 if {$first ne {}} {
3355 $cflist insert end $first
3356 set cflist_top 1
3357 $cflist tag add highlight 1.0 "1.0 lineend"
3358 } else {
3359 catch {unset cflist_top}
3361 $cflist conf -state disabled
3362 set difffilestart {}
3365 proc highlight_tag {f} {
3366 global highlight_paths
3368 foreach p $highlight_paths {
3369 if {[string match $p $f]} {
3370 return "bold"
3373 return {}
3376 proc highlight_filelist {} {
3377 global cmitmode cflist
3379 $cflist conf -state normal
3380 if {$cmitmode ne "tree"} {
3381 set end [lindex [split [$cflist index end] .] 0]
3382 for {set l 2} {$l < $end} {incr l} {
3383 set line [$cflist get $l.0 "$l.0 lineend"]
3384 if {[highlight_tag $line] ne {}} {
3385 $cflist tag add bold $l.0 "$l.0 lineend"
3388 } else {
3389 highlight_tree 2 {}
3391 $cflist conf -state disabled
3394 proc unhighlight_filelist {} {
3395 global cflist
3397 $cflist conf -state normal
3398 $cflist tag remove bold 1.0 end
3399 $cflist conf -state disabled
3402 proc add_flist {fl} {
3403 global cflist
3405 $cflist conf -state normal
3406 foreach f $fl {
3407 $cflist insert end "\n"
3408 $cflist insert end $f [highlight_tag $f]
3410 $cflist conf -state disabled
3413 proc sel_flist {w x y} {
3414 global ctext difffilestart cflist cflist_top cmitmode
3416 if {$cmitmode eq "tree"} return
3417 if {![info exists cflist_top]} return
3418 set l [lindex [split [$w index "@$x,$y"] "."] 0]
3419 $cflist tag remove highlight $cflist_top.0 "$cflist_top.0 lineend"
3420 $cflist tag add highlight $l.0 "$l.0 lineend"
3421 set cflist_top $l
3422 if {$l == 1} {
3423 $ctext yview 1.0
3424 } else {
3425 catch {$ctext yview [lindex $difffilestart [expr {$l - 2}]]}
3427 suppress_highlighting_file_for_current_scrollpos
3430 proc pop_flist_menu {w X Y x y} {
3431 global ctext cflist cmitmode flist_menu flist_menu_file
3432 global treediffs diffids
3434 stopfinding
3435 set l [lindex [split [$w index "@$x,$y"] "."] 0]
3436 if {$l <= 1} return
3437 if {$cmitmode eq "tree"} {
3438 set e [linetoelt $l]
3439 if {[string index $e end] eq "/"} return
3440 } else {
3441 set e [lindex $treediffs($diffids) [expr {$l-2}]]
3443 set flist_menu_file $e
3444 set xdiffstate "normal"
3445 if {$cmitmode eq "tree"} {
3446 set xdiffstate "disabled"
3448 # Disable "External diff" item in tree mode
3449 $flist_menu entryconf 2 -state $xdiffstate
3450 tk_popup $flist_menu $X $Y
3453 proc find_ctext_fileinfo {line} {
3454 global ctext_file_names ctext_file_lines
3456 set ok [bsearch $ctext_file_lines $line]
3457 set tline [lindex $ctext_file_lines $ok]
3459 if {$ok >= [llength $ctext_file_lines] || $line < $tline} {
3460 return {}
3461 } else {
3462 return [list [lindex $ctext_file_names $ok] $tline]
3466 proc pop_diff_menu {w X Y x y} {
3467 global ctext diff_menu flist_menu_file
3468 global diff_menu_txtpos diff_menu_line
3469 global diff_menu_filebase
3471 set diff_menu_txtpos [split [$w index "@$x,$y"] "."]
3472 set diff_menu_line [lindex $diff_menu_txtpos 0]
3473 # don't pop up the menu on hunk-separator or file-separator lines
3474 if {[lsearch -glob [$ctext tag names $diff_menu_line.0] "*sep"] >= 0} {
3475 return
3477 stopfinding
3478 set f [find_ctext_fileinfo $diff_menu_line]
3479 if {$f eq {}} return
3480 set flist_menu_file [lindex $f 0]
3481 set diff_menu_filebase [lindex $f 1]
3482 tk_popup $diff_menu $X $Y
3485 proc flist_hl {only} {
3486 global flist_menu_file findstring gdttype
3488 set x [shellquote $flist_menu_file]
3489 if {$only || $findstring eq {} || $gdttype ne [mc "touching paths:"]} {
3490 set findstring $x
3491 } else {
3492 append findstring " " $x
3494 set gdttype [mc "touching paths:"]
3497 proc gitknewtmpdir {} {
3498 global diffnum gitktmpdir gitdir env
3500 if {![info exists gitktmpdir]} {
3501 if {[info exists env(GITK_TMPDIR)]} {
3502 set tmpdir $env(GITK_TMPDIR)
3503 } elseif {[info exists env(TMPDIR)]} {
3504 set tmpdir $env(TMPDIR)
3505 } else {
3506 set tmpdir $gitdir
3508 set gitktmpformat [file join $tmpdir ".gitk-tmp.XXXXXX"]
3509 if {[catch {set gitktmpdir [exec mktemp -d $gitktmpformat]}]} {
3510 set gitktmpdir [file join $gitdir [format ".gitk-tmp.%s" [pid]]]
3512 if {[catch {file mkdir $gitktmpdir} err]} {
3513 error_popup "[mc "Error creating temporary directory %s:" $gitktmpdir] $err"
3514 unset gitktmpdir
3515 return {}
3517 set diffnum 0
3519 incr diffnum
3520 set diffdir [file join $gitktmpdir $diffnum]
3521 if {[catch {file mkdir $diffdir} err]} {
3522 error_popup "[mc "Error creating temporary directory %s:" $diffdir] $err"
3523 return {}
3525 return $diffdir
3528 proc save_file_from_commit {filename output what} {
3529 global nullfile
3531 if {[catch {exec git show $filename -- > $output} err]} {
3532 if {[string match "fatal: bad revision *" $err]} {
3533 return $nullfile
3535 error_popup "[mc "Error getting \"%s\" from %s:" $filename $what] $err"
3536 return {}
3538 return $output
3541 proc external_diff_get_one_file {diffid filename diffdir} {
3542 global nullid nullid2 nullfile
3543 global worktree
3545 if {$diffid == $nullid} {
3546 set difffile [file join $worktree $filename]
3547 if {[file exists $difffile]} {
3548 return $difffile
3550 return $nullfile
3552 if {$diffid == $nullid2} {
3553 set difffile [file join $diffdir "\[index\] [file tail $filename]"]
3554 return [save_file_from_commit :$filename $difffile index]
3556 set difffile [file join $diffdir "\[$diffid\] [file tail $filename]"]
3557 return [save_file_from_commit $diffid:$filename $difffile \
3558 "revision $diffid"]
3561 proc external_diff {} {
3562 global nullid nullid2
3563 global flist_menu_file
3564 global diffids
3565 global extdifftool
3567 if {[llength $diffids] == 1} {
3568 # no reference commit given
3569 set diffidto [lindex $diffids 0]
3570 if {$diffidto eq $nullid} {
3571 # diffing working copy with index
3572 set diffidfrom $nullid2
3573 } elseif {$diffidto eq $nullid2} {
3574 # diffing index with HEAD
3575 set diffidfrom "HEAD"
3576 } else {
3577 # use first parent commit
3578 global parentlist selectedline
3579 set diffidfrom [lindex $parentlist $selectedline 0]
3581 } else {
3582 set diffidfrom [lindex $diffids 0]
3583 set diffidto [lindex $diffids 1]
3586 # make sure that several diffs wont collide
3587 set diffdir [gitknewtmpdir]
3588 if {$diffdir eq {}} return
3590 # gather files to diff
3591 set difffromfile [external_diff_get_one_file $diffidfrom $flist_menu_file $diffdir]
3592 set difftofile [external_diff_get_one_file $diffidto $flist_menu_file $diffdir]
3594 if {$difffromfile ne {} && $difftofile ne {}} {
3595 set cmd [list [shellsplit $extdifftool] $difffromfile $difftofile]
3596 if {[catch {set fl [open |$cmd r]} err]} {
3597 file delete -force $diffdir
3598 error_popup "$extdifftool: [mc "command failed:"] $err"
3599 } else {
3600 fconfigure $fl -blocking 0
3601 filerun $fl [list delete_at_eof $fl $diffdir]
3606 proc find_hunk_blamespec {base line} {
3607 global ctext
3609 # Find and parse the hunk header
3610 set s_lix [$ctext search -backwards -regexp ^@@ "$line.0 lineend" $base.0]
3611 if {$s_lix eq {}} return
3613 set s_line [$ctext get $s_lix "$s_lix + 1 lines"]
3614 if {![regexp {^@@@*(( -\d+(,\d+)?)+) \+(\d+)(,\d+)? @@} $s_line \
3615 s_line old_specs osz osz1 new_line nsz]} {
3616 return
3619 # base lines for the parents
3620 set base_lines [list $new_line]
3621 foreach old_spec [lrange [split $old_specs " "] 1 end] {
3622 if {![regexp -- {-(\d+)(,\d+)?} $old_spec \
3623 old_spec old_line osz]} {
3624 return
3626 lappend base_lines $old_line
3629 # Now scan the lines to determine offset within the hunk
3630 set max_parent [expr {[llength $base_lines]-2}]
3631 set dline 0
3632 set s_lno [lindex [split $s_lix "."] 0]
3634 # Determine if the line is removed
3635 set chunk [$ctext get $line.0 "$line.1 + $max_parent chars"]
3636 if {[string match {[-+ ]*} $chunk]} {
3637 set removed_idx [string first "-" $chunk]
3638 # Choose a parent index
3639 if {$removed_idx >= 0} {
3640 set parent $removed_idx
3641 } else {
3642 set unchanged_idx [string first " " $chunk]
3643 if {$unchanged_idx >= 0} {
3644 set parent $unchanged_idx
3645 } else {
3646 # blame the current commit
3647 set parent -1
3650 # then count other lines that belong to it
3651 for {set i $line} {[incr i -1] > $s_lno} {} {
3652 set chunk [$ctext get $i.0 "$i.1 + $max_parent chars"]
3653 # Determine if the line is removed
3654 set removed_idx [string first "-" $chunk]
3655 if {$parent >= 0} {
3656 set code [string index $chunk $parent]
3657 if {$code eq "-" || ($removed_idx < 0 && $code ne "+")} {
3658 incr dline
3660 } else {
3661 if {$removed_idx < 0} {
3662 incr dline
3666 incr parent
3667 } else {
3668 set parent 0
3671 incr dline [lindex $base_lines $parent]
3672 return [list $parent $dline]
3675 proc external_blame_diff {} {
3676 global currentid cmitmode
3677 global diff_menu_txtpos diff_menu_line
3678 global diff_menu_filebase flist_menu_file
3680 if {$cmitmode eq "tree"} {
3681 set parent_idx 0
3682 set line [expr {$diff_menu_line - $diff_menu_filebase}]
3683 } else {
3684 set hinfo [find_hunk_blamespec $diff_menu_filebase $diff_menu_line]
3685 if {$hinfo ne {}} {
3686 set parent_idx [lindex $hinfo 0]
3687 set line [lindex $hinfo 1]
3688 } else {
3689 set parent_idx 0
3690 set line 0
3694 external_blame $parent_idx $line
3697 # Find the SHA1 ID of the blob for file $fname in the index
3698 # at stage 0 or 2
3699 proc index_sha1 {fname} {
3700 set f [open [list | git ls-files -s $fname] r]
3701 while {[gets $f line] >= 0} {
3702 set info [lindex [split $line "\t"] 0]
3703 set stage [lindex $info 2]
3704 if {$stage eq "0" || $stage eq "2"} {
3705 close $f
3706 return [lindex $info 1]
3709 close $f
3710 return {}
3713 # Turn an absolute path into one relative to the current directory
3714 proc make_relative {f} {
3715 if {[file pathtype $f] eq "relative"} {
3716 return $f
3718 set elts [file split $f]
3719 set here [file split [pwd]]
3720 set ei 0
3721 set hi 0
3722 set res {}
3723 foreach d $here {
3724 if {$ei < $hi || $ei >= [llength $elts] || [lindex $elts $ei] ne $d} {
3725 lappend res ".."
3726 } else {
3727 incr ei
3729 incr hi
3731 set elts [concat $res [lrange $elts $ei end]]
3732 return [eval file join $elts]
3735 proc external_blame {parent_idx {line {}}} {
3736 global flist_menu_file cdup
3737 global nullid nullid2
3738 global parentlist selectedline currentid
3740 if {$parent_idx > 0} {
3741 set base_commit [lindex $parentlist $selectedline [expr {$parent_idx-1}]]
3742 } else {
3743 set base_commit $currentid
3746 if {$base_commit eq {} || $base_commit eq $nullid || $base_commit eq $nullid2} {
3747 error_popup [mc "No such commit"]
3748 return
3751 set cmdline [list git gui blame]
3752 if {$line ne {} && $line > 1} {
3753 lappend cmdline "--line=$line"
3755 set f [file join $cdup $flist_menu_file]
3756 # Unfortunately it seems git gui blame doesn't like
3757 # being given an absolute path...
3758 set f [make_relative $f]
3759 lappend cmdline $base_commit $f
3760 if {[catch {eval exec $cmdline &} err]} {
3761 error_popup "[mc "git gui blame: command failed:"] $err"
3765 proc show_line_source {} {
3766 global cmitmode currentid parents curview blamestuff blameinst
3767 global diff_menu_line diff_menu_filebase flist_menu_file
3768 global nullid nullid2 gitdir cdup
3770 set from_index {}
3771 if {$cmitmode eq "tree"} {
3772 set id $currentid
3773 set line [expr {$diff_menu_line - $diff_menu_filebase}]
3774 } else {
3775 set h [find_hunk_blamespec $diff_menu_filebase $diff_menu_line]
3776 if {$h eq {}} return
3777 set pi [lindex $h 0]
3778 if {$pi == 0} {
3779 mark_ctext_line $diff_menu_line
3780 return
3782 incr pi -1
3783 if {$currentid eq $nullid} {
3784 if {$pi > 0} {
3785 # must be a merge in progress...
3786 if {[catch {
3787 # get the last line from .git/MERGE_HEAD
3788 set f [open [file join $gitdir MERGE_HEAD] r]
3789 set id [lindex [split [read $f] "\n"] end-1]
3790 close $f
3791 } err]} {
3792 error_popup [mc "Couldn't read merge head: %s" $err]
3793 return
3795 } elseif {$parents($curview,$currentid) eq $nullid2} {
3796 # need to do the blame from the index
3797 if {[catch {
3798 set from_index [index_sha1 $flist_menu_file]
3799 } err]} {
3800 error_popup [mc "Error reading index: %s" $err]
3801 return
3803 } else {
3804 set id $parents($curview,$currentid)
3806 } else {
3807 set id [lindex $parents($curview,$currentid) $pi]
3809 set line [lindex $h 1]
3811 set blameargs {}
3812 if {$from_index ne {}} {
3813 lappend blameargs | git cat-file blob $from_index
3815 lappend blameargs | git blame -p -L$line,+1
3816 if {$from_index ne {}} {
3817 lappend blameargs --contents -
3818 } else {
3819 lappend blameargs $id
3821 lappend blameargs -- [file join $cdup $flist_menu_file]
3822 if {[catch {
3823 set f [open $blameargs r]
3824 } err]} {
3825 error_popup [mc "Couldn't start git blame: %s" $err]
3826 return
3828 nowbusy blaming [mc "Searching"]
3829 fconfigure $f -blocking 0
3830 set i [reg_instance $f]
3831 set blamestuff($i) {}
3832 set blameinst $i
3833 filerun $f [list read_line_source $f $i]
3836 proc stopblaming {} {
3837 global blameinst
3839 if {[info exists blameinst]} {
3840 stop_instance $blameinst
3841 unset blameinst
3842 notbusy blaming
3846 proc read_line_source {fd inst} {
3847 global blamestuff curview commfd blameinst nullid nullid2
3849 while {[gets $fd line] >= 0} {
3850 lappend blamestuff($inst) $line
3852 if {![eof $fd]} {
3853 return 1
3855 unset commfd($inst)
3856 unset blameinst
3857 notbusy blaming
3858 fconfigure $fd -blocking 1
3859 if {[catch {close $fd} err]} {
3860 error_popup [mc "Error running git blame: %s" $err]
3861 return 0
3864 set fname {}
3865 set line [split [lindex $blamestuff($inst) 0] " "]
3866 set id [lindex $line 0]
3867 set lnum [lindex $line 1]
3868 if {[string length $id] == 40 && [string is xdigit $id] &&
3869 [string is digit -strict $lnum]} {
3870 # look for "filename" line
3871 foreach l $blamestuff($inst) {
3872 if {[string match "filename *" $l]} {
3873 set fname [string range $l 9 end]
3874 break
3878 if {$fname ne {}} {
3879 # all looks good, select it
3880 if {$id eq $nullid} {
3881 # blame uses all-zeroes to mean not committed,
3882 # which would mean a change in the index
3883 set id $nullid2
3885 if {[commitinview $id $curview]} {
3886 selectline [rowofcommit $id] 1 [list $fname $lnum] 1
3887 } else {
3888 error_popup [mc "That line comes from commit %s, \
3889 which is not in this view" [shortids $id]]
3891 } else {
3892 puts "oops couldn't parse git blame output"
3894 return 0
3897 # delete $dir when we see eof on $f (presumably because the child has exited)
3898 proc delete_at_eof {f dir} {
3899 while {[gets $f line] >= 0} {}
3900 if {[eof $f]} {
3901 if {[catch {close $f} err]} {
3902 error_popup "[mc "External diff viewer failed:"] $err"
3904 file delete -force $dir
3905 return 0
3907 return 1
3910 # Functions for adding and removing shell-type quoting
3912 proc shellquote {str} {
3913 if {![string match "*\['\"\\ \t]*" $str]} {
3914 return $str
3916 if {![string match "*\['\"\\]*" $str]} {
3917 return "\"$str\""
3919 if {![string match "*'*" $str]} {
3920 return "'$str'"
3922 return "\"[string map {\" \\\" \\ \\\\} $str]\""
3925 proc shellarglist {l} {
3926 set str {}
3927 foreach a $l {
3928 if {$str ne {}} {
3929 append str " "
3931 append str [shellquote $a]
3933 return $str
3936 proc shelldequote {str} {
3937 set ret {}
3938 set used -1
3939 while {1} {
3940 incr used
3941 if {![regexp -start $used -indices "\['\"\\\\ \t]" $str first]} {
3942 append ret [string range $str $used end]
3943 set used [string length $str]
3944 break
3946 set first [lindex $first 0]
3947 set ch [string index $str $first]
3948 if {$first > $used} {
3949 append ret [string range $str $used [expr {$first - 1}]]
3950 set used $first
3952 if {$ch eq " " || $ch eq "\t"} break
3953 incr used
3954 if {$ch eq "'"} {
3955 set first [string first "'" $str $used]
3956 if {$first < 0} {
3957 error "unmatched single-quote"
3959 append ret [string range $str $used [expr {$first - 1}]]
3960 set used $first
3961 continue
3963 if {$ch eq "\\"} {
3964 if {$used >= [string length $str]} {
3965 error "trailing backslash"
3967 append ret [string index $str $used]
3968 continue
3970 # here ch == "\""
3971 while {1} {
3972 if {![regexp -start $used -indices "\[\"\\\\]" $str first]} {
3973 error "unmatched double-quote"
3975 set first [lindex $first 0]
3976 set ch [string index $str $first]
3977 if {$first > $used} {
3978 append ret [string range $str $used [expr {$first - 1}]]
3979 set used $first
3981 if {$ch eq "\""} break
3982 incr used
3983 append ret [string index $str $used]
3984 incr used
3987 return [list $used $ret]
3990 proc shellsplit {str} {
3991 set l {}
3992 while {1} {
3993 set str [string trimleft $str]
3994 if {$str eq {}} break
3995 set dq [shelldequote $str]
3996 set n [lindex $dq 0]
3997 set word [lindex $dq 1]
3998 set str [string range $str $n end]
3999 lappend l $word
4001 return $l
4004 # Code to implement multiple views
4006 proc newview {ishighlight} {
4007 global nextviewnum newviewname newishighlight
4008 global revtreeargs viewargscmd newviewopts curview
4010 set newishighlight $ishighlight
4011 set top .gitkview
4012 if {[winfo exists $top]} {
4013 raise $top
4014 return
4016 decode_view_opts $nextviewnum $revtreeargs
4017 set newviewname($nextviewnum) "[mc "View"] $nextviewnum"
4018 set newviewopts($nextviewnum,perm) 0
4019 set newviewopts($nextviewnum,cmd) $viewargscmd($curview)
4020 vieweditor $top $nextviewnum [mc "Gitk view definition"]
4023 set known_view_options {
4024 {perm b . {} {mc "Remember this view"}}
4025 {reflabel l + {} {mc "References (space separated list):"}}
4026 {refs t15 .. {} {mc "Branches & tags:"}}
4027 {allrefs b *. "--all" {mc "All refs"}}
4028 {branches b . "--branches" {mc "All (local) branches"}}
4029 {tags b . "--tags" {mc "All tags"}}
4030 {remotes b . "--remotes" {mc "All remote-tracking branches"}}
4031 {commitlbl l + {} {mc "Commit Info (regular expressions):"}}
4032 {author t15 .. "--author=*" {mc "Author:"}}
4033 {committer t15 . "--committer=*" {mc "Committer:"}}
4034 {loginfo t15 .. "--grep=*" {mc "Commit Message:"}}
4035 {allmatch b .. "--all-match" {mc "Matches all Commit Info criteria"}}
4036 {changes_l l + {} {mc "Changes to Files:"}}
4037 {pickaxe_s r0 . {} {mc "Fixed String"}}
4038 {pickaxe_t r1 . "--pickaxe-regex" {mc "Regular Expression"}}
4039 {pickaxe t15 .. "-S*" {mc "Search string:"}}
4040 {datelabel l + {} {mc "Commit Dates (\"2 weeks ago\", \"2009-03-17 15:27:38\", \"March 17, 2009 15:27:38\"):"}}
4041 {since t15 .. {"--since=*" "--after=*"} {mc "Since:"}}
4042 {until t15 . {"--until=*" "--before=*"} {mc "Until:"}}
4043 {limit_lbl l + {} {mc "Limit and/or skip a number of revisions (positive integer):"}}
4044 {limit t10 *. "--max-count=*" {mc "Number to show:"}}
4045 {skip t10 . "--skip=*" {mc "Number to skip:"}}
4046 {misc_lbl l + {} {mc "Miscellaneous options:"}}
4047 {dorder b *. {"--date-order" "-d"} {mc "Strictly sort by date"}}
4048 {lright b . "--left-right" {mc "Mark branch sides"}}
4049 {first b . "--first-parent" {mc "Limit to first parent"}}
4050 {smplhst b . "--simplify-by-decoration" {mc "Simple history"}}
4051 {args t50 *. {} {mc "Additional arguments to git log:"}}
4052 {allpaths path + {} {mc "Enter files and directories to include, one per line:"}}
4053 {cmd t50= + {} {mc "Command to generate more commits to include:"}}
4056 # Convert $newviewopts($n, ...) into args for git log.
4057 proc encode_view_opts {n} {
4058 global known_view_options newviewopts
4060 set rargs [list]
4061 foreach opt $known_view_options {
4062 set patterns [lindex $opt 3]
4063 if {$patterns eq {}} continue
4064 set pattern [lindex $patterns 0]
4066 if {[lindex $opt 1] eq "b"} {
4067 set val $newviewopts($n,[lindex $opt 0])
4068 if {$val} {
4069 lappend rargs $pattern
4071 } elseif {[regexp {^r(\d+)$} [lindex $opt 1] type value]} {
4072 regexp {^(.*_)} [lindex $opt 0] uselessvar button_id
4073 set val $newviewopts($n,$button_id)
4074 if {$val eq $value} {
4075 lappend rargs $pattern
4077 } else {
4078 set val $newviewopts($n,[lindex $opt 0])
4079 set val [string trim $val]
4080 if {$val ne {}} {
4081 set pfix [string range $pattern 0 end-1]
4082 lappend rargs $pfix$val
4086 set rargs [concat $rargs [shellsplit $newviewopts($n,refs)]]
4087 return [concat $rargs [shellsplit $newviewopts($n,args)]]
4090 # Fill $newviewopts($n, ...) based on args for git log.
4091 proc decode_view_opts {n view_args} {
4092 global known_view_options newviewopts
4094 foreach opt $known_view_options {
4095 set id [lindex $opt 0]
4096 if {[lindex $opt 1] eq "b"} {
4097 # Checkboxes
4098 set val 0
4099 } elseif {[regexp {^r(\d+)$} [lindex $opt 1]]} {
4100 # Radiobuttons
4101 regexp {^(.*_)} $id uselessvar id
4102 set val 0
4103 } else {
4104 # Text fields
4105 set val {}
4107 set newviewopts($n,$id) $val
4109 set oargs [list]
4110 set refargs [list]
4111 foreach arg $view_args {
4112 if {[regexp -- {^-([0-9]+)$} $arg arg cnt]
4113 && ![info exists found(limit)]} {
4114 set newviewopts($n,limit) $cnt
4115 set found(limit) 1
4116 continue
4118 catch { unset val }
4119 foreach opt $known_view_options {
4120 set id [lindex $opt 0]
4121 if {[info exists found($id)]} continue
4122 foreach pattern [lindex $opt 3] {
4123 if {![string match $pattern $arg]} continue
4124 if {[lindex $opt 1] eq "b"} {
4125 # Check buttons
4126 set val 1
4127 } elseif {[regexp {^r(\d+)$} [lindex $opt 1] match num]} {
4128 # Radio buttons
4129 regexp {^(.*_)} $id uselessvar id
4130 set val $num
4131 } else {
4132 # Text input fields
4133 set size [string length $pattern]
4134 set val [string range $arg [expr {$size-1}] end]
4136 set newviewopts($n,$id) $val
4137 set found($id) 1
4138 break
4140 if {[info exists val]} break
4142 if {[info exists val]} continue
4143 if {[regexp {^-} $arg]} {
4144 lappend oargs $arg
4145 } else {
4146 lappend refargs $arg
4149 set newviewopts($n,refs) [shellarglist $refargs]
4150 set newviewopts($n,args) [shellarglist $oargs]
4153 proc edit_or_newview {} {
4154 global curview
4156 if {$curview > 0} {
4157 editview
4158 } else {
4159 newview 0
4163 proc editview {} {
4164 global curview
4165 global viewname viewperm newviewname newviewopts
4166 global viewargs viewargscmd
4168 set top .gitkvedit-$curview
4169 if {[winfo exists $top]} {
4170 raise $top
4171 return
4173 decode_view_opts $curview $viewargs($curview)
4174 set newviewname($curview) $viewname($curview)
4175 set newviewopts($curview,perm) $viewperm($curview)
4176 set newviewopts($curview,cmd) $viewargscmd($curview)
4177 vieweditor $top $curview "[mc "Gitk: edit view"] $viewname($curview)"
4180 proc vieweditor {top n title} {
4181 global newviewname newviewopts viewfiles bgcolor
4182 global known_view_options NS
4184 ttk_toplevel $top
4185 wm title $top [concat $title [mc "-- criteria for selecting revisions"]]
4186 make_transient $top .
4188 # View name
4189 ${NS}::frame $top.nfr
4190 ${NS}::label $top.nl -text [mc "View Name"]
4191 ${NS}::entry $top.name -width 20 -textvariable newviewname($n)
4192 pack $top.nfr -in $top -fill x -pady 5 -padx 3
4193 pack $top.nl -in $top.nfr -side left -padx {0 5}
4194 pack $top.name -in $top.nfr -side left -padx {0 25}
4196 # View options
4197 set cframe $top.nfr
4198 set cexpand 0
4199 set cnt 0
4200 foreach opt $known_view_options {
4201 set id [lindex $opt 0]
4202 set type [lindex $opt 1]
4203 set flags [lindex $opt 2]
4204 set title [eval [lindex $opt 4]]
4205 set lxpad 0
4207 if {$flags eq "+" || $flags eq "*"} {
4208 set cframe $top.fr$cnt
4209 incr cnt
4210 ${NS}::frame $cframe
4211 pack $cframe -in $top -fill x -pady 3 -padx 3
4212 set cexpand [expr {$flags eq "*"}]
4213 } elseif {$flags eq ".." || $flags eq "*."} {
4214 set cframe $top.fr$cnt
4215 incr cnt
4216 ${NS}::frame $cframe
4217 pack $cframe -in $top -fill x -pady 3 -padx [list 15 3]
4218 set cexpand [expr {$flags eq "*."}]
4219 } else {
4220 set lxpad 5
4223 if {$type eq "l"} {
4224 ${NS}::label $cframe.l_$id -text $title
4225 pack $cframe.l_$id -in $cframe -side left -pady [list 3 0] -anchor w
4226 } elseif {$type eq "b"} {
4227 ${NS}::checkbutton $cframe.c_$id -text $title -variable newviewopts($n,$id)
4228 pack $cframe.c_$id -in $cframe -side left \
4229 -padx [list $lxpad 0] -expand $cexpand -anchor w
4230 } elseif {[regexp {^r(\d+)$} $type type sz]} {
4231 regexp {^(.*_)} $id uselessvar button_id
4232 ${NS}::radiobutton $cframe.c_$id -text $title -variable newviewopts($n,$button_id) -value $sz
4233 pack $cframe.c_$id -in $cframe -side left \
4234 -padx [list $lxpad 0] -expand $cexpand -anchor w
4235 } elseif {[regexp {^t(\d+)$} $type type sz]} {
4236 ${NS}::label $cframe.l_$id -text $title
4237 ${NS}::entry $cframe.e_$id -width $sz -background $bgcolor \
4238 -textvariable newviewopts($n,$id)
4239 pack $cframe.l_$id -in $cframe -side left -padx [list $lxpad 0]
4240 pack $cframe.e_$id -in $cframe -side left -expand 1 -fill x
4241 } elseif {[regexp {^t(\d+)=$} $type type sz]} {
4242 ${NS}::label $cframe.l_$id -text $title
4243 ${NS}::entry $cframe.e_$id -width $sz -background $bgcolor \
4244 -textvariable newviewopts($n,$id)
4245 pack $cframe.l_$id -in $cframe -side top -pady [list 3 0] -anchor w
4246 pack $cframe.e_$id -in $cframe -side top -fill x
4247 } elseif {$type eq "path"} {
4248 ${NS}::label $top.l -text $title
4249 pack $top.l -in $top -side top -pady [list 3 0] -anchor w -padx 3
4250 text $top.t -width 40 -height 5 -background $bgcolor
4251 if {[info exists viewfiles($n)]} {
4252 foreach f $viewfiles($n) {
4253 $top.t insert end $f
4254 $top.t insert end "\n"
4256 $top.t delete {end - 1c} end
4257 $top.t mark set insert 0.0
4259 pack $top.t -in $top -side top -pady [list 0 5] -fill both -expand 1 -padx 3
4263 ${NS}::frame $top.buts
4264 ${NS}::button $top.buts.ok -text [mc "OK"] -command [list newviewok $top $n]
4265 ${NS}::button $top.buts.apply -text [mc "Apply (F5)"] -command [list newviewok $top $n 1]
4266 ${NS}::button $top.buts.can -text [mc "Cancel"] -command [list destroy $top]
4267 bind $top <Control-Return> [list newviewok $top $n]
4268 bind $top <F5> [list newviewok $top $n 1]
4269 bind $top <Escape> [list destroy $top]
4270 grid $top.buts.ok $top.buts.apply $top.buts.can
4271 grid columnconfigure $top.buts 0 -weight 1 -uniform a
4272 grid columnconfigure $top.buts 1 -weight 1 -uniform a
4273 grid columnconfigure $top.buts 2 -weight 1 -uniform a
4274 pack $top.buts -in $top -side top -fill x
4275 focus $top.t
4278 proc doviewmenu {m first cmd op argv} {
4279 set nmenu [$m index end]
4280 for {set i $first} {$i <= $nmenu} {incr i} {
4281 if {[$m entrycget $i -command] eq $cmd} {
4282 eval $m $op $i $argv
4283 break
4288 proc allviewmenus {n op args} {
4289 # global viewhlmenu
4291 doviewmenu .bar.view 5 [list showview $n] $op $args
4292 # doviewmenu $viewhlmenu 1 [list addvhighlight $n] $op $args
4295 proc newviewok {top n {apply 0}} {
4296 global nextviewnum newviewperm newviewname newishighlight
4297 global viewname viewfiles viewperm selectedview curview
4298 global viewargs viewargscmd newviewopts viewhlmenu
4300 if {[catch {
4301 set newargs [encode_view_opts $n]
4302 } err]} {
4303 error_popup "[mc "Error in commit selection arguments:"] $err" $top
4304 return
4306 set files {}
4307 foreach f [split [$top.t get 0.0 end] "\n"] {
4308 set ft [string trim $f]
4309 if {$ft ne {}} {
4310 lappend files $ft
4313 if {![info exists viewfiles($n)]} {
4314 # creating a new view
4315 incr nextviewnum
4316 set viewname($n) $newviewname($n)
4317 set viewperm($n) $newviewopts($n,perm)
4318 set viewfiles($n) $files
4319 set viewargs($n) $newargs
4320 set viewargscmd($n) $newviewopts($n,cmd)
4321 addviewmenu $n
4322 if {!$newishighlight} {
4323 run showview $n
4324 } else {
4325 run addvhighlight $n
4327 } else {
4328 # editing an existing view
4329 set viewperm($n) $newviewopts($n,perm)
4330 if {$newviewname($n) ne $viewname($n)} {
4331 set viewname($n) $newviewname($n)
4332 doviewmenu .bar.view 5 [list showview $n] \
4333 entryconf [list -label $viewname($n)]
4334 # doviewmenu $viewhlmenu 1 [list addvhighlight $n] \
4335 # entryconf [list -label $viewname($n) -value $viewname($n)]
4337 if {$files ne $viewfiles($n) || $newargs ne $viewargs($n) || \
4338 $newviewopts($n,cmd) ne $viewargscmd($n)} {
4339 set viewfiles($n) $files
4340 set viewargs($n) $newargs
4341 set viewargscmd($n) $newviewopts($n,cmd)
4342 if {$curview == $n} {
4343 run reloadcommits
4347 if {$apply} return
4348 catch {destroy $top}
4351 proc delview {} {
4352 global curview viewperm hlview selectedhlview
4354 if {$curview == 0} return
4355 if {[info exists hlview] && $hlview == $curview} {
4356 set selectedhlview [mc "None"]
4357 unset hlview
4359 allviewmenus $curview delete
4360 set viewperm($curview) 0
4361 showview 0
4364 proc addviewmenu {n} {
4365 global viewname viewhlmenu
4367 .bar.view add radiobutton -label $viewname($n) \
4368 -command [list showview $n] -variable selectedview -value $n
4369 #$viewhlmenu add radiobutton -label $viewname($n) \
4370 # -command [list addvhighlight $n] -variable selectedhlview
4373 proc showview {n} {
4374 global curview cached_commitrow ordertok
4375 global displayorder parentlist rowidlist rowisopt rowfinal
4376 global colormap rowtextx nextcolor canvxmax
4377 global numcommits viewcomplete
4378 global selectedline currentid canv canvy0
4379 global treediffs
4380 global pending_select mainheadid
4381 global commitidx
4382 global selectedview
4383 global hlview selectedhlview commitinterest
4385 if {$n == $curview} return
4386 set selid {}
4387 set ymax [lindex [$canv cget -scrollregion] 3]
4388 set span [$canv yview]
4389 set ytop [expr {[lindex $span 0] * $ymax}]
4390 set ybot [expr {[lindex $span 1] * $ymax}]
4391 set yscreen [expr {($ybot - $ytop) / 2}]
4392 if {$selectedline ne {}} {
4393 set selid $currentid
4394 set y [yc $selectedline]
4395 if {$ytop < $y && $y < $ybot} {
4396 set yscreen [expr {$y - $ytop}]
4398 } elseif {[info exists pending_select]} {
4399 set selid $pending_select
4400 unset pending_select
4402 unselectline
4403 normalline
4404 catch {unset treediffs}
4405 clear_display
4406 if {[info exists hlview] && $hlview == $n} {
4407 unset hlview
4408 set selectedhlview [mc "None"]
4410 catch {unset commitinterest}
4411 catch {unset cached_commitrow}
4412 catch {unset ordertok}
4414 set curview $n
4415 set selectedview $n
4416 .bar.view entryconf [mca "Edit view..."] -state [expr {$n == 0? "disabled": "normal"}]
4417 .bar.view entryconf [mca "Delete view"] -state [expr {$n == 0? "disabled": "normal"}]
4419 run refill_reflist
4420 if {![info exists viewcomplete($n)]} {
4421 getcommits $selid
4422 return
4425 set displayorder {}
4426 set parentlist {}
4427 set rowidlist {}
4428 set rowisopt {}
4429 set rowfinal {}
4430 set numcommits $commitidx($n)
4432 catch {unset colormap}
4433 catch {unset rowtextx}
4434 set nextcolor 0
4435 set canvxmax [$canv cget -width]
4436 set curview $n
4437 set row 0
4438 setcanvscroll
4439 set yf 0
4440 set row {}
4441 if {$selid ne {} && [commitinview $selid $n]} {
4442 set row [rowofcommit $selid]
4443 # try to get the selected row in the same position on the screen
4444 set ymax [lindex [$canv cget -scrollregion] 3]
4445 set ytop [expr {[yc $row] - $yscreen}]
4446 if {$ytop < 0} {
4447 set ytop 0
4449 set yf [expr {$ytop * 1.0 / $ymax}]
4451 allcanvs yview moveto $yf
4452 drawvisible
4453 if {$row ne {}} {
4454 selectline $row 0
4455 } elseif {!$viewcomplete($n)} {
4456 reset_pending_select $selid
4457 } else {
4458 reset_pending_select {}
4460 if {[commitinview $pending_select $curview]} {
4461 selectline [rowofcommit $pending_select] 1
4462 } else {
4463 set row [first_real_row]
4464 if {$row < $numcommits} {
4465 selectline $row 0
4469 if {!$viewcomplete($n)} {
4470 if {$numcommits == 0} {
4471 show_status [mc "Reading commits..."]
4473 } elseif {$numcommits == 0} {
4474 show_status [mc "No commits selected"]
4478 # Stuff relating to the highlighting facility
4480 proc ishighlighted {id} {
4481 global vhighlights fhighlights nhighlights rhighlights
4483 if {[info exists nhighlights($id)] && $nhighlights($id) > 0} {
4484 return $nhighlights($id)
4486 if {[info exists vhighlights($id)] && $vhighlights($id) > 0} {
4487 return $vhighlights($id)
4489 if {[info exists fhighlights($id)] && $fhighlights($id) > 0} {
4490 return $fhighlights($id)
4492 if {[info exists rhighlights($id)] && $rhighlights($id) > 0} {
4493 return $rhighlights($id)
4495 return 0
4498 proc bolden {id font} {
4499 global canv linehtag currentid boldids need_redisplay markedid
4501 # need_redisplay = 1 means the display is stale and about to be redrawn
4502 if {$need_redisplay} return
4503 lappend boldids $id
4504 $canv itemconf $linehtag($id) -font $font
4505 if {[info exists currentid] && $id eq $currentid} {
4506 $canv delete secsel
4507 set t [eval $canv create rect [$canv bbox $linehtag($id)] \
4508 -outline {{}} -tags secsel \
4509 -fill [$canv cget -selectbackground]]
4510 $canv lower $t
4512 if {[info exists markedid] && $id eq $markedid} {
4513 make_idmark $id
4517 proc bolden_name {id font} {
4518 global canv2 linentag currentid boldnameids need_redisplay
4520 if {$need_redisplay} return
4521 lappend boldnameids $id
4522 $canv2 itemconf $linentag($id) -font $font
4523 if {[info exists currentid] && $id eq $currentid} {
4524 $canv2 delete secsel
4525 set t [eval $canv2 create rect [$canv2 bbox $linentag($id)] \
4526 -outline {{}} -tags secsel \
4527 -fill [$canv2 cget -selectbackground]]
4528 $canv2 lower $t
4532 proc unbolden {} {
4533 global boldids
4535 set stillbold {}
4536 foreach id $boldids {
4537 if {![ishighlighted $id]} {
4538 bolden $id mainfont
4539 } else {
4540 lappend stillbold $id
4543 set boldids $stillbold
4546 proc addvhighlight {n} {
4547 global hlview viewcomplete curview vhl_done commitidx
4549 if {[info exists hlview]} {
4550 delvhighlight
4552 set hlview $n
4553 if {$n != $curview && ![info exists viewcomplete($n)]} {
4554 start_rev_list $n
4556 set vhl_done $commitidx($hlview)
4557 if {$vhl_done > 0} {
4558 drawvisible
4562 proc delvhighlight {} {
4563 global hlview vhighlights
4565 if {![info exists hlview]} return
4566 unset hlview
4567 catch {unset vhighlights}
4568 unbolden
4571 proc vhighlightmore {} {
4572 global hlview vhl_done commitidx vhighlights curview
4574 set max $commitidx($hlview)
4575 set vr [visiblerows]
4576 set r0 [lindex $vr 0]
4577 set r1 [lindex $vr 1]
4578 for {set i $vhl_done} {$i < $max} {incr i} {
4579 set id [commitonrow $i $hlview]
4580 if {[commitinview $id $curview]} {
4581 set row [rowofcommit $id]
4582 if {$r0 <= $row && $row <= $r1} {
4583 if {![highlighted $row]} {
4584 bolden $id mainfontbold
4586 set vhighlights($id) 1
4590 set vhl_done $max
4591 return 0
4594 proc askvhighlight {row id} {
4595 global hlview vhighlights iddrawn
4597 if {[commitinview $id $hlview]} {
4598 if {[info exists iddrawn($id)] && ![ishighlighted $id]} {
4599 bolden $id mainfontbold
4601 set vhighlights($id) 1
4602 } else {
4603 set vhighlights($id) 0
4607 proc hfiles_change {} {
4608 global highlight_files filehighlight fhighlights fh_serial
4609 global highlight_paths
4611 if {[info exists filehighlight]} {
4612 # delete previous highlights
4613 catch {close $filehighlight}
4614 unset filehighlight
4615 catch {unset fhighlights}
4616 unbolden
4617 unhighlight_filelist
4619 set highlight_paths {}
4620 after cancel do_file_hl $fh_serial
4621 incr fh_serial
4622 if {$highlight_files ne {}} {
4623 after 300 do_file_hl $fh_serial
4627 proc gdttype_change {name ix op} {
4628 global gdttype highlight_files findstring findpattern
4630 stopfinding
4631 if {$findstring ne {}} {
4632 if {$gdttype eq [mc "containing:"]} {
4633 if {$highlight_files ne {}} {
4634 set highlight_files {}
4635 hfiles_change
4637 findcom_change
4638 } else {
4639 if {$findpattern ne {}} {
4640 set findpattern {}
4641 findcom_change
4643 set highlight_files $findstring
4644 hfiles_change
4646 drawvisible
4648 # enable/disable findtype/findloc menus too
4651 proc find_change {name ix op} {
4652 global gdttype findstring highlight_files
4654 stopfinding
4655 if {$gdttype eq [mc "containing:"]} {
4656 findcom_change
4657 } else {
4658 if {$highlight_files ne $findstring} {
4659 set highlight_files $findstring
4660 hfiles_change
4663 drawvisible
4666 proc findcom_change args {
4667 global nhighlights boldnameids
4668 global findpattern findtype findstring gdttype
4670 stopfinding
4671 # delete previous highlights, if any
4672 foreach id $boldnameids {
4673 bolden_name $id mainfont
4675 set boldnameids {}
4676 catch {unset nhighlights}
4677 unbolden
4678 unmarkmatches
4679 if {$gdttype ne [mc "containing:"] || $findstring eq {}} {
4680 set findpattern {}
4681 } elseif {$findtype eq [mc "Regexp"]} {
4682 set findpattern $findstring
4683 } else {
4684 set e [string map {"*" "\\*" "?" "\\?" "\[" "\\\[" "\\" "\\\\"} \
4685 $findstring]
4686 set findpattern "*$e*"
4690 proc makepatterns {l} {
4691 set ret {}
4692 foreach e $l {
4693 set ee [string map {"*" "\\*" "?" "\\?" "\[" "\\\[" "\\" "\\\\"} $e]
4694 if {[string index $ee end] eq "/"} {
4695 lappend ret "$ee*"
4696 } else {
4697 lappend ret $ee
4698 lappend ret "$ee/*"
4701 return $ret
4704 proc do_file_hl {serial} {
4705 global highlight_files filehighlight highlight_paths gdttype fhl_list
4706 global cdup findtype
4708 if {$gdttype eq [mc "touching paths:"]} {
4709 # If "exact" match then convert backslashes to forward slashes.
4710 # Most useful to support Windows-flavoured file paths.
4711 if {$findtype eq [mc "Exact"]} {
4712 set highlight_files [string map {"\\" "/"} $highlight_files]
4714 if {[catch {set paths [shellsplit $highlight_files]}]} return
4715 set highlight_paths [makepatterns $paths]
4716 highlight_filelist
4717 set relative_paths {}
4718 foreach path $paths {
4719 lappend relative_paths [file join $cdup $path]
4721 set gdtargs [concat -- $relative_paths]
4722 } elseif {$gdttype eq [mc "adding/removing string:"]} {
4723 set gdtargs [list "-S$highlight_files"]
4724 } elseif {$gdttype eq [mc "changing lines matching:"]} {
4725 set gdtargs [list "-G$highlight_files"]
4726 } else {
4727 # must be "containing:", i.e. we're searching commit info
4728 return
4730 set cmd [concat | git diff-tree -r -s --stdin $gdtargs]
4731 set filehighlight [open $cmd r+]
4732 fconfigure $filehighlight -blocking 0
4733 filerun $filehighlight readfhighlight
4734 set fhl_list {}
4735 drawvisible
4736 flushhighlights
4739 proc flushhighlights {} {
4740 global filehighlight fhl_list
4742 if {[info exists filehighlight]} {
4743 lappend fhl_list {}
4744 puts $filehighlight ""
4745 flush $filehighlight
4749 proc askfilehighlight {row id} {
4750 global filehighlight fhighlights fhl_list
4752 lappend fhl_list $id
4753 set fhighlights($id) -1
4754 puts $filehighlight $id
4757 proc readfhighlight {} {
4758 global filehighlight fhighlights curview iddrawn
4759 global fhl_list find_dirn
4761 if {![info exists filehighlight]} {
4762 return 0
4764 set nr 0
4765 while {[incr nr] <= 100 && [gets $filehighlight line] >= 0} {
4766 set line [string trim $line]
4767 set i [lsearch -exact $fhl_list $line]
4768 if {$i < 0} continue
4769 for {set j 0} {$j < $i} {incr j} {
4770 set id [lindex $fhl_list $j]
4771 set fhighlights($id) 0
4773 set fhl_list [lrange $fhl_list [expr {$i+1}] end]
4774 if {$line eq {}} continue
4775 if {![commitinview $line $curview]} continue
4776 if {[info exists iddrawn($line)] && ![ishighlighted $line]} {
4777 bolden $line mainfontbold
4779 set fhighlights($line) 1
4781 if {[eof $filehighlight]} {
4782 # strange...
4783 puts "oops, git diff-tree died"
4784 catch {close $filehighlight}
4785 unset filehighlight
4786 return 0
4788 if {[info exists find_dirn]} {
4789 run findmore
4791 return 1
4794 proc doesmatch {f} {
4795 global findtype findpattern
4797 if {$findtype eq [mc "Regexp"]} {
4798 return [regexp $findpattern $f]
4799 } elseif {$findtype eq [mc "IgnCase"]} {
4800 return [string match -nocase $findpattern $f]
4801 } else {
4802 return [string match $findpattern $f]
4806 proc askfindhighlight {row id} {
4807 global nhighlights commitinfo iddrawn
4808 global findloc
4809 global markingmatches
4811 if {![info exists commitinfo($id)]} {
4812 getcommit $id
4814 set info $commitinfo($id)
4815 set isbold 0
4816 set fldtypes [list [mc Headline] [mc Author] "" [mc Committer] "" [mc Comments]]
4817 foreach f $info ty $fldtypes {
4818 if {$ty eq ""} continue
4819 if {($findloc eq [mc "All fields"] || $findloc eq $ty) &&
4820 [doesmatch $f]} {
4821 if {$ty eq [mc "Author"]} {
4822 set isbold 2
4823 break
4825 set isbold 1
4828 if {$isbold && [info exists iddrawn($id)]} {
4829 if {![ishighlighted $id]} {
4830 bolden $id mainfontbold
4831 if {$isbold > 1} {
4832 bolden_name $id mainfontbold
4835 if {$markingmatches} {
4836 markrowmatches $row $id
4839 set nhighlights($id) $isbold
4842 proc markrowmatches {row id} {
4843 global canv canv2 linehtag linentag commitinfo findloc
4845 set headline [lindex $commitinfo($id) 0]
4846 set author [lindex $commitinfo($id) 1]
4847 $canv delete match$row
4848 $canv2 delete match$row
4849 if {$findloc eq [mc "All fields"] || $findloc eq [mc "Headline"]} {
4850 set m [findmatches $headline]
4851 if {$m ne {}} {
4852 markmatches $canv $row $headline $linehtag($id) $m \
4853 [$canv itemcget $linehtag($id) -font] $row
4856 if {$findloc eq [mc "All fields"] || $findloc eq [mc "Author"]} {
4857 set m [findmatches $author]
4858 if {$m ne {}} {
4859 markmatches $canv2 $row $author $linentag($id) $m \
4860 [$canv2 itemcget $linentag($id) -font] $row
4865 proc vrel_change {name ix op} {
4866 global highlight_related
4868 rhighlight_none
4869 if {$highlight_related ne [mc "None"]} {
4870 run drawvisible
4874 # prepare for testing whether commits are descendents or ancestors of a
4875 proc rhighlight_sel {a} {
4876 global descendent desc_todo ancestor anc_todo
4877 global highlight_related
4879 catch {unset descendent}
4880 set desc_todo [list $a]
4881 catch {unset ancestor}
4882 set anc_todo [list $a]
4883 if {$highlight_related ne [mc "None"]} {
4884 rhighlight_none
4885 run drawvisible
4889 proc rhighlight_none {} {
4890 global rhighlights
4892 catch {unset rhighlights}
4893 unbolden
4896 proc is_descendent {a} {
4897 global curview children descendent desc_todo
4899 set v $curview
4900 set la [rowofcommit $a]
4901 set todo $desc_todo
4902 set leftover {}
4903 set done 0
4904 for {set i 0} {$i < [llength $todo]} {incr i} {
4905 set do [lindex $todo $i]
4906 if {[rowofcommit $do] < $la} {
4907 lappend leftover $do
4908 continue
4910 foreach nk $children($v,$do) {
4911 if {![info exists descendent($nk)]} {
4912 set descendent($nk) 1
4913 lappend todo $nk
4914 if {$nk eq $a} {
4915 set done 1
4919 if {$done} {
4920 set desc_todo [concat $leftover [lrange $todo [expr {$i+1}] end]]
4921 return
4924 set descendent($a) 0
4925 set desc_todo $leftover
4928 proc is_ancestor {a} {
4929 global curview parents ancestor anc_todo
4931 set v $curview
4932 set la [rowofcommit $a]
4933 set todo $anc_todo
4934 set leftover {}
4935 set done 0
4936 for {set i 0} {$i < [llength $todo]} {incr i} {
4937 set do [lindex $todo $i]
4938 if {![commitinview $do $v] || [rowofcommit $do] > $la} {
4939 lappend leftover $do
4940 continue
4942 foreach np $parents($v,$do) {
4943 if {![info exists ancestor($np)]} {
4944 set ancestor($np) 1
4945 lappend todo $np
4946 if {$np eq $a} {
4947 set done 1
4951 if {$done} {
4952 set anc_todo [concat $leftover [lrange $todo [expr {$i+1}] end]]
4953 return
4956 set ancestor($a) 0
4957 set anc_todo $leftover
4960 proc askrelhighlight {row id} {
4961 global descendent highlight_related iddrawn rhighlights
4962 global selectedline ancestor
4964 if {$selectedline eq {}} return
4965 set isbold 0
4966 if {$highlight_related eq [mc "Descendant"] ||
4967 $highlight_related eq [mc "Not descendant"]} {
4968 if {![info exists descendent($id)]} {
4969 is_descendent $id
4971 if {$descendent($id) == ($highlight_related eq [mc "Descendant"])} {
4972 set isbold 1
4974 } elseif {$highlight_related eq [mc "Ancestor"] ||
4975 $highlight_related eq [mc "Not ancestor"]} {
4976 if {![info exists ancestor($id)]} {
4977 is_ancestor $id
4979 if {$ancestor($id) == ($highlight_related eq [mc "Ancestor"])} {
4980 set isbold 1
4983 if {[info exists iddrawn($id)]} {
4984 if {$isbold && ![ishighlighted $id]} {
4985 bolden $id mainfontbold
4988 set rhighlights($id) $isbold
4991 # Graph layout functions
4993 proc shortids {ids} {
4994 set res {}
4995 foreach id $ids {
4996 if {[llength $id] > 1} {
4997 lappend res [shortids $id]
4998 } elseif {[regexp {^[0-9a-f]{40}$} $id]} {
4999 lappend res [string range $id 0 7]
5000 } else {
5001 lappend res $id
5004 return $res
5007 proc ntimes {n o} {
5008 set ret {}
5009 set o [list $o]
5010 for {set mask 1} {$mask <= $n} {incr mask $mask} {
5011 if {($n & $mask) != 0} {
5012 set ret [concat $ret $o]
5014 set o [concat $o $o]
5016 return $ret
5019 proc ordertoken {id} {
5020 global ordertok curview varcid varcstart varctok curview parents children
5021 global nullid nullid2
5023 if {[info exists ordertok($id)]} {
5024 return $ordertok($id)
5026 set origid $id
5027 set todo {}
5028 while {1} {
5029 if {[info exists varcid($curview,$id)]} {
5030 set a $varcid($curview,$id)
5031 set p [lindex $varcstart($curview) $a]
5032 } else {
5033 set p [lindex $children($curview,$id) 0]
5035 if {[info exists ordertok($p)]} {
5036 set tok $ordertok($p)
5037 break
5039 set id [first_real_child $curview,$p]
5040 if {$id eq {}} {
5041 # it's a root
5042 set tok [lindex $varctok($curview) $varcid($curview,$p)]
5043 break
5045 if {[llength $parents($curview,$id)] == 1} {
5046 lappend todo [list $p {}]
5047 } else {
5048 set j [lsearch -exact $parents($curview,$id) $p]
5049 if {$j < 0} {
5050 puts "oops didn't find [shortids $p] in parents of [shortids $id]"
5052 lappend todo [list $p [strrep $j]]
5055 for {set i [llength $todo]} {[incr i -1] >= 0} {} {
5056 set p [lindex $todo $i 0]
5057 append tok [lindex $todo $i 1]
5058 set ordertok($p) $tok
5060 set ordertok($origid) $tok
5061 return $tok
5064 # Work out where id should go in idlist so that order-token
5065 # values increase from left to right
5066 proc idcol {idlist id {i 0}} {
5067 set t [ordertoken $id]
5068 if {$i < 0} {
5069 set i 0
5071 if {$i >= [llength $idlist] || $t < [ordertoken [lindex $idlist $i]]} {
5072 if {$i > [llength $idlist]} {
5073 set i [llength $idlist]
5075 while {[incr i -1] >= 0 && $t < [ordertoken [lindex $idlist $i]]} {}
5076 incr i
5077 } else {
5078 if {$t > [ordertoken [lindex $idlist $i]]} {
5079 while {[incr i] < [llength $idlist] &&
5080 $t >= [ordertoken [lindex $idlist $i]]} {}
5083 return $i
5086 proc initlayout {} {
5087 global rowidlist rowisopt rowfinal displayorder parentlist
5088 global numcommits canvxmax canv
5089 global nextcolor
5090 global colormap rowtextx
5092 set numcommits 0
5093 set displayorder {}
5094 set parentlist {}
5095 set nextcolor 0
5096 set rowidlist {}
5097 set rowisopt {}
5098 set rowfinal {}
5099 set canvxmax [$canv cget -width]
5100 catch {unset colormap}
5101 catch {unset rowtextx}
5102 setcanvscroll
5105 proc setcanvscroll {} {
5106 global canv canv2 canv3 numcommits linespc canvxmax canvy0
5107 global lastscrollset lastscrollrows
5109 set ymax [expr {$canvy0 + ($numcommits - 0.5) * $linespc + 2}]
5110 $canv conf -scrollregion [list 0 0 $canvxmax $ymax]
5111 $canv2 conf -scrollregion [list 0 0 0 $ymax]
5112 $canv3 conf -scrollregion [list 0 0 0 $ymax]
5113 set lastscrollset [clock clicks -milliseconds]
5114 set lastscrollrows $numcommits
5117 proc visiblerows {} {
5118 global canv numcommits linespc
5120 set ymax [lindex [$canv cget -scrollregion] 3]
5121 if {$ymax eq {} || $ymax == 0} return
5122 set f [$canv yview]
5123 set y0 [expr {int([lindex $f 0] * $ymax)}]
5124 set r0 [expr {int(($y0 - 3) / $linespc) - 1}]
5125 if {$r0 < 0} {
5126 set r0 0
5128 set y1 [expr {int([lindex $f 1] * $ymax)}]
5129 set r1 [expr {int(($y1 - 3) / $linespc) + 1}]
5130 if {$r1 >= $numcommits} {
5131 set r1 [expr {$numcommits - 1}]
5133 return [list $r0 $r1]
5136 proc layoutmore {} {
5137 global commitidx viewcomplete curview
5138 global numcommits pending_select curview
5139 global lastscrollset lastscrollrows
5141 if {$lastscrollrows < 100 || $viewcomplete($curview) ||
5142 [clock clicks -milliseconds] - $lastscrollset > 500} {
5143 setcanvscroll
5145 if {[info exists pending_select] &&
5146 [commitinview $pending_select $curview]} {
5147 update
5148 selectline [rowofcommit $pending_select] 1
5150 drawvisible
5153 # With path limiting, we mightn't get the actual HEAD commit,
5154 # so ask git rev-list what is the first ancestor of HEAD that
5155 # touches a file in the path limit.
5156 proc get_viewmainhead {view} {
5157 global viewmainheadid vfilelimit viewinstances mainheadid
5159 catch {
5160 set rfd [open [concat | git rev-list -1 $mainheadid \
5161 -- $vfilelimit($view)] r]
5162 set j [reg_instance $rfd]
5163 lappend viewinstances($view) $j
5164 fconfigure $rfd -blocking 0
5165 filerun $rfd [list getviewhead $rfd $j $view]
5166 set viewmainheadid($curview) {}
5170 # git rev-list should give us just 1 line to use as viewmainheadid($view)
5171 proc getviewhead {fd inst view} {
5172 global viewmainheadid commfd curview viewinstances showlocalchanges
5174 set id {}
5175 if {[gets $fd line] < 0} {
5176 if {![eof $fd]} {
5177 return 1
5179 } elseif {[string length $line] == 40 && [string is xdigit $line]} {
5180 set id $line
5182 set viewmainheadid($view) $id
5183 close $fd
5184 unset commfd($inst)
5185 set i [lsearch -exact $viewinstances($view) $inst]
5186 if {$i >= 0} {
5187 set viewinstances($view) [lreplace $viewinstances($view) $i $i]
5189 if {$showlocalchanges && $id ne {} && $view == $curview} {
5190 doshowlocalchanges
5192 return 0
5195 proc doshowlocalchanges {} {
5196 global curview viewmainheadid
5198 if {$viewmainheadid($curview) eq {}} return
5199 if {[commitinview $viewmainheadid($curview) $curview]} {
5200 dodiffindex
5201 } else {
5202 interestedin $viewmainheadid($curview) dodiffindex
5206 proc dohidelocalchanges {} {
5207 global nullid nullid2 lserial curview
5209 if {[commitinview $nullid $curview]} {
5210 removefakerow $nullid
5212 if {[commitinview $nullid2 $curview]} {
5213 removefakerow $nullid2
5215 incr lserial
5218 # spawn off a process to do git diff-index --cached HEAD
5219 proc dodiffindex {} {
5220 global lserial showlocalchanges vfilelimit curview
5221 global hasworktree git_version
5223 if {!$showlocalchanges || !$hasworktree} return
5224 incr lserial
5225 if {[package vcompare $git_version "1.7.2"] >= 0} {
5226 set cmd "|git diff-index --cached --ignore-submodules=dirty HEAD"
5227 } else {
5228 set cmd "|git diff-index --cached HEAD"
5230 if {$vfilelimit($curview) ne {}} {
5231 set cmd [concat $cmd -- $vfilelimit($curview)]
5233 set fd [open $cmd r]
5234 fconfigure $fd -blocking 0
5235 set i [reg_instance $fd]
5236 filerun $fd [list readdiffindex $fd $lserial $i]
5239 proc readdiffindex {fd serial inst} {
5240 global viewmainheadid nullid nullid2 curview commitinfo commitdata lserial
5241 global vfilelimit
5243 set isdiff 1
5244 if {[gets $fd line] < 0} {
5245 if {![eof $fd]} {
5246 return 1
5248 set isdiff 0
5250 # we only need to see one line and we don't really care what it says...
5251 stop_instance $inst
5253 if {$serial != $lserial} {
5254 return 0
5257 # now see if there are any local changes not checked in to the index
5258 set cmd "|git diff-files"
5259 if {$vfilelimit($curview) ne {}} {
5260 set cmd [concat $cmd -- $vfilelimit($curview)]
5262 set fd [open $cmd r]
5263 fconfigure $fd -blocking 0
5264 set i [reg_instance $fd]
5265 filerun $fd [list readdifffiles $fd $serial $i]
5267 if {$isdiff && ![commitinview $nullid2 $curview]} {
5268 # add the line for the changes in the index to the graph
5269 set hl [mc "Local changes checked in to index but not committed"]
5270 set commitinfo($nullid2) [list $hl {} {} {} {} " $hl\n"]
5271 set commitdata($nullid2) "\n $hl\n"
5272 if {[commitinview $nullid $curview]} {
5273 removefakerow $nullid
5275 insertfakerow $nullid2 $viewmainheadid($curview)
5276 } elseif {!$isdiff && [commitinview $nullid2 $curview]} {
5277 if {[commitinview $nullid $curview]} {
5278 removefakerow $nullid
5280 removefakerow $nullid2
5282 return 0
5285 proc readdifffiles {fd serial inst} {
5286 global viewmainheadid nullid nullid2 curview
5287 global commitinfo commitdata lserial
5289 set isdiff 1
5290 if {[gets $fd line] < 0} {
5291 if {![eof $fd]} {
5292 return 1
5294 set isdiff 0
5296 # we only need to see one line and we don't really care what it says...
5297 stop_instance $inst
5299 if {$serial != $lserial} {
5300 return 0
5303 if {$isdiff && ![commitinview $nullid $curview]} {
5304 # add the line for the local diff to the graph
5305 set hl [mc "Local uncommitted changes, not checked in to index"]
5306 set commitinfo($nullid) [list $hl {} {} {} {} " $hl\n"]
5307 set commitdata($nullid) "\n $hl\n"
5308 if {[commitinview $nullid2 $curview]} {
5309 set p $nullid2
5310 } else {
5311 set p $viewmainheadid($curview)
5313 insertfakerow $nullid $p
5314 } elseif {!$isdiff && [commitinview $nullid $curview]} {
5315 removefakerow $nullid
5317 return 0
5320 proc nextuse {id row} {
5321 global curview children
5323 if {[info exists children($curview,$id)]} {
5324 foreach kid $children($curview,$id) {
5325 if {![commitinview $kid $curview]} {
5326 return -1
5328 if {[rowofcommit $kid] > $row} {
5329 return [rowofcommit $kid]
5333 if {[commitinview $id $curview]} {
5334 return [rowofcommit $id]
5336 return -1
5339 proc prevuse {id row} {
5340 global curview children
5342 set ret -1
5343 if {[info exists children($curview,$id)]} {
5344 foreach kid $children($curview,$id) {
5345 if {![commitinview $kid $curview]} break
5346 if {[rowofcommit $kid] < $row} {
5347 set ret [rowofcommit $kid]
5351 return $ret
5354 proc make_idlist {row} {
5355 global displayorder parentlist uparrowlen downarrowlen mingaplen
5356 global commitidx curview children
5358 set r [expr {$row - $mingaplen - $downarrowlen - 1}]
5359 if {$r < 0} {
5360 set r 0
5362 set ra [expr {$row - $downarrowlen}]
5363 if {$ra < 0} {
5364 set ra 0
5366 set rb [expr {$row + $uparrowlen}]
5367 if {$rb > $commitidx($curview)} {
5368 set rb $commitidx($curview)
5370 make_disporder $r [expr {$rb + 1}]
5371 set ids {}
5372 for {} {$r < $ra} {incr r} {
5373 set nextid [lindex $displayorder [expr {$r + 1}]]
5374 foreach p [lindex $parentlist $r] {
5375 if {$p eq $nextid} continue
5376 set rn [nextuse $p $r]
5377 if {$rn >= $row &&
5378 $rn <= $r + $downarrowlen + $mingaplen + $uparrowlen} {
5379 lappend ids [list [ordertoken $p] $p]
5383 for {} {$r < $row} {incr r} {
5384 set nextid [lindex $displayorder [expr {$r + 1}]]
5385 foreach p [lindex $parentlist $r] {
5386 if {$p eq $nextid} continue
5387 set rn [nextuse $p $r]
5388 if {$rn < 0 || $rn >= $row} {
5389 lappend ids [list [ordertoken $p] $p]
5393 set id [lindex $displayorder $row]
5394 lappend ids [list [ordertoken $id] $id]
5395 while {$r < $rb} {
5396 foreach p [lindex $parentlist $r] {
5397 set firstkid [lindex $children($curview,$p) 0]
5398 if {[rowofcommit $firstkid] < $row} {
5399 lappend ids [list [ordertoken $p] $p]
5402 incr r
5403 set id [lindex $displayorder $r]
5404 if {$id ne {}} {
5405 set firstkid [lindex $children($curview,$id) 0]
5406 if {$firstkid ne {} && [rowofcommit $firstkid] < $row} {
5407 lappend ids [list [ordertoken $id] $id]
5411 set idlist {}
5412 foreach idx [lsort -unique $ids] {
5413 lappend idlist [lindex $idx 1]
5415 return $idlist
5418 proc rowsequal {a b} {
5419 while {[set i [lsearch -exact $a {}]] >= 0} {
5420 set a [lreplace $a $i $i]
5422 while {[set i [lsearch -exact $b {}]] >= 0} {
5423 set b [lreplace $b $i $i]
5425 return [expr {$a eq $b}]
5428 proc makeupline {id row rend col} {
5429 global rowidlist uparrowlen downarrowlen mingaplen
5431 for {set r $rend} {1} {set r $rstart} {
5432 set rstart [prevuse $id $r]
5433 if {$rstart < 0} return
5434 if {$rstart < $row} break
5436 if {$rstart + $uparrowlen + $mingaplen + $downarrowlen < $rend} {
5437 set rstart [expr {$rend - $uparrowlen - 1}]
5439 for {set r $rstart} {[incr r] <= $row} {} {
5440 set idlist [lindex $rowidlist $r]
5441 if {$idlist ne {} && [lsearch -exact $idlist $id] < 0} {
5442 set col [idcol $idlist $id $col]
5443 lset rowidlist $r [linsert $idlist $col $id]
5444 changedrow $r
5449 proc layoutrows {row endrow} {
5450 global rowidlist rowisopt rowfinal displayorder
5451 global uparrowlen downarrowlen maxwidth mingaplen
5452 global children parentlist
5453 global commitidx viewcomplete curview
5455 make_disporder [expr {$row - 1}] [expr {$endrow + $uparrowlen}]
5456 set idlist {}
5457 if {$row > 0} {
5458 set rm1 [expr {$row - 1}]
5459 foreach id [lindex $rowidlist $rm1] {
5460 if {$id ne {}} {
5461 lappend idlist $id
5464 set final [lindex $rowfinal $rm1]
5466 for {} {$row < $endrow} {incr row} {
5467 set rm1 [expr {$row - 1}]
5468 if {$rm1 < 0 || $idlist eq {}} {
5469 set idlist [make_idlist $row]
5470 set final 1
5471 } else {
5472 set id [lindex $displayorder $rm1]
5473 set col [lsearch -exact $idlist $id]
5474 set idlist [lreplace $idlist $col $col]
5475 foreach p [lindex $parentlist $rm1] {
5476 if {[lsearch -exact $idlist $p] < 0} {
5477 set col [idcol $idlist $p $col]
5478 set idlist [linsert $idlist $col $p]
5479 # if not the first child, we have to insert a line going up
5480 if {$id ne [lindex $children($curview,$p) 0]} {
5481 makeupline $p $rm1 $row $col
5485 set id [lindex $displayorder $row]
5486 if {$row > $downarrowlen} {
5487 set termrow [expr {$row - $downarrowlen - 1}]
5488 foreach p [lindex $parentlist $termrow] {
5489 set i [lsearch -exact $idlist $p]
5490 if {$i < 0} continue
5491 set nr [nextuse $p $termrow]
5492 if {$nr < 0 || $nr >= $row + $mingaplen + $uparrowlen} {
5493 set idlist [lreplace $idlist $i $i]
5497 set col [lsearch -exact $idlist $id]
5498 if {$col < 0} {
5499 set col [idcol $idlist $id]
5500 set idlist [linsert $idlist $col $id]
5501 if {$children($curview,$id) ne {}} {
5502 makeupline $id $rm1 $row $col
5505 set r [expr {$row + $uparrowlen - 1}]
5506 if {$r < $commitidx($curview)} {
5507 set x $col
5508 foreach p [lindex $parentlist $r] {
5509 if {[lsearch -exact $idlist $p] >= 0} continue
5510 set fk [lindex $children($curview,$p) 0]
5511 if {[rowofcommit $fk] < $row} {
5512 set x [idcol $idlist $p $x]
5513 set idlist [linsert $idlist $x $p]
5516 if {[incr r] < $commitidx($curview)} {
5517 set p [lindex $displayorder $r]
5518 if {[lsearch -exact $idlist $p] < 0} {
5519 set fk [lindex $children($curview,$p) 0]
5520 if {$fk ne {} && [rowofcommit $fk] < $row} {
5521 set x [idcol $idlist $p $x]
5522 set idlist [linsert $idlist $x $p]
5528 if {$final && !$viewcomplete($curview) &&
5529 $row + $uparrowlen + $mingaplen + $downarrowlen
5530 >= $commitidx($curview)} {
5531 set final 0
5533 set l [llength $rowidlist]
5534 if {$row == $l} {
5535 lappend rowidlist $idlist
5536 lappend rowisopt 0
5537 lappend rowfinal $final
5538 } elseif {$row < $l} {
5539 if {![rowsequal $idlist [lindex $rowidlist $row]]} {
5540 lset rowidlist $row $idlist
5541 changedrow $row
5543 lset rowfinal $row $final
5544 } else {
5545 set pad [ntimes [expr {$row - $l}] {}]
5546 set rowidlist [concat $rowidlist $pad]
5547 lappend rowidlist $idlist
5548 set rowfinal [concat $rowfinal $pad]
5549 lappend rowfinal $final
5550 set rowisopt [concat $rowisopt [ntimes [expr {$row - $l + 1}] 0]]
5553 return $row
5556 proc changedrow {row} {
5557 global displayorder iddrawn rowisopt need_redisplay
5559 set l [llength $rowisopt]
5560 if {$row < $l} {
5561 lset rowisopt $row 0
5562 if {$row + 1 < $l} {
5563 lset rowisopt [expr {$row + 1}] 0
5564 if {$row + 2 < $l} {
5565 lset rowisopt [expr {$row + 2}] 0
5569 set id [lindex $displayorder $row]
5570 if {[info exists iddrawn($id)]} {
5571 set need_redisplay 1
5575 proc insert_pad {row col npad} {
5576 global rowidlist
5578 set pad [ntimes $npad {}]
5579 set idlist [lindex $rowidlist $row]
5580 set bef [lrange $idlist 0 [expr {$col - 1}]]
5581 set aft [lrange $idlist $col end]
5582 set i [lsearch -exact $aft {}]
5583 if {$i > 0} {
5584 set aft [lreplace $aft $i $i]
5586 lset rowidlist $row [concat $bef $pad $aft]
5587 changedrow $row
5590 proc optimize_rows {row col endrow} {
5591 global rowidlist rowisopt displayorder curview children
5593 if {$row < 1} {
5594 set row 1
5596 for {} {$row < $endrow} {incr row; set col 0} {
5597 if {[lindex $rowisopt $row]} continue
5598 set haspad 0
5599 set y0 [expr {$row - 1}]
5600 set ym [expr {$row - 2}]
5601 set idlist [lindex $rowidlist $row]
5602 set previdlist [lindex $rowidlist $y0]
5603 if {$idlist eq {} || $previdlist eq {}} continue
5604 if {$ym >= 0} {
5605 set pprevidlist [lindex $rowidlist $ym]
5606 if {$pprevidlist eq {}} continue
5607 } else {
5608 set pprevidlist {}
5610 set x0 -1
5611 set xm -1
5612 for {} {$col < [llength $idlist]} {incr col} {
5613 set id [lindex $idlist $col]
5614 if {[lindex $previdlist $col] eq $id} continue
5615 if {$id eq {}} {
5616 set haspad 1
5617 continue
5619 set x0 [lsearch -exact $previdlist $id]
5620 if {$x0 < 0} continue
5621 set z [expr {$x0 - $col}]
5622 set isarrow 0
5623 set z0 {}
5624 if {$ym >= 0} {
5625 set xm [lsearch -exact $pprevidlist $id]
5626 if {$xm >= 0} {
5627 set z0 [expr {$xm - $x0}]
5630 if {$z0 eq {}} {
5631 # if row y0 is the first child of $id then it's not an arrow
5632 if {[lindex $children($curview,$id) 0] ne
5633 [lindex $displayorder $y0]} {
5634 set isarrow 1
5637 if {!$isarrow && $id ne [lindex $displayorder $row] &&
5638 [lsearch -exact [lindex $rowidlist [expr {$row+1}]] $id] < 0} {
5639 set isarrow 1
5641 # Looking at lines from this row to the previous row,
5642 # make them go straight up if they end in an arrow on
5643 # the previous row; otherwise make them go straight up
5644 # or at 45 degrees.
5645 if {$z < -1 || ($z < 0 && $isarrow)} {
5646 # Line currently goes left too much;
5647 # insert pads in the previous row, then optimize it
5648 set npad [expr {-1 - $z + $isarrow}]
5649 insert_pad $y0 $x0 $npad
5650 if {$y0 > 0} {
5651 optimize_rows $y0 $x0 $row
5653 set previdlist [lindex $rowidlist $y0]
5654 set x0 [lsearch -exact $previdlist $id]
5655 set z [expr {$x0 - $col}]
5656 if {$z0 ne {}} {
5657 set pprevidlist [lindex $rowidlist $ym]
5658 set xm [lsearch -exact $pprevidlist $id]
5659 set z0 [expr {$xm - $x0}]
5661 } elseif {$z > 1 || ($z > 0 && $isarrow)} {
5662 # Line currently goes right too much;
5663 # insert pads in this line
5664 set npad [expr {$z - 1 + $isarrow}]
5665 insert_pad $row $col $npad
5666 set idlist [lindex $rowidlist $row]
5667 incr col $npad
5668 set z [expr {$x0 - $col}]
5669 set haspad 1
5671 if {$z0 eq {} && !$isarrow && $ym >= 0} {
5672 # this line links to its first child on row $row-2
5673 set id [lindex $displayorder $ym]
5674 set xc [lsearch -exact $pprevidlist $id]
5675 if {$xc >= 0} {
5676 set z0 [expr {$xc - $x0}]
5679 # avoid lines jigging left then immediately right
5680 if {$z0 ne {} && $z < 0 && $z0 > 0} {
5681 insert_pad $y0 $x0 1
5682 incr x0
5683 optimize_rows $y0 $x0 $row
5684 set previdlist [lindex $rowidlist $y0]
5687 if {!$haspad} {
5688 # Find the first column that doesn't have a line going right
5689 for {set col [llength $idlist]} {[incr col -1] >= 0} {} {
5690 set id [lindex $idlist $col]
5691 if {$id eq {}} break
5692 set x0 [lsearch -exact $previdlist $id]
5693 if {$x0 < 0} {
5694 # check if this is the link to the first child
5695 set kid [lindex $displayorder $y0]
5696 if {[lindex $children($curview,$id) 0] eq $kid} {
5697 # it is, work out offset to child
5698 set x0 [lsearch -exact $previdlist $kid]
5701 if {$x0 <= $col} break
5703 # Insert a pad at that column as long as it has a line and
5704 # isn't the last column
5705 if {$x0 >= 0 && [incr col] < [llength $idlist]} {
5706 set idlist [linsert $idlist $col {}]
5707 lset rowidlist $row $idlist
5708 changedrow $row
5714 proc xc {row col} {
5715 global canvx0 linespc
5716 return [expr {$canvx0 + $col * $linespc}]
5719 proc yc {row} {
5720 global canvy0 linespc
5721 return [expr {$canvy0 + $row * $linespc}]
5724 proc linewidth {id} {
5725 global thickerline lthickness
5727 set wid $lthickness
5728 if {[info exists thickerline] && $id eq $thickerline} {
5729 set wid [expr {2 * $lthickness}]
5731 return $wid
5734 proc rowranges {id} {
5735 global curview children uparrowlen downarrowlen
5736 global rowidlist
5738 set kids $children($curview,$id)
5739 if {$kids eq {}} {
5740 return {}
5742 set ret {}
5743 lappend kids $id
5744 foreach child $kids {
5745 if {![commitinview $child $curview]} break
5746 set row [rowofcommit $child]
5747 if {![info exists prev]} {
5748 lappend ret [expr {$row + 1}]
5749 } else {
5750 if {$row <= $prevrow} {
5751 puts "oops children of [shortids $id] out of order [shortids $child] $row <= [shortids $prev] $prevrow"
5753 # see if the line extends the whole way from prevrow to row
5754 if {$row > $prevrow + $uparrowlen + $downarrowlen &&
5755 [lsearch -exact [lindex $rowidlist \
5756 [expr {int(($row + $prevrow) / 2)}]] $id] < 0} {
5757 # it doesn't, see where it ends
5758 set r [expr {$prevrow + $downarrowlen}]
5759 if {[lsearch -exact [lindex $rowidlist $r] $id] < 0} {
5760 while {[incr r -1] > $prevrow &&
5761 [lsearch -exact [lindex $rowidlist $r] $id] < 0} {}
5762 } else {
5763 while {[incr r] <= $row &&
5764 [lsearch -exact [lindex $rowidlist $r] $id] >= 0} {}
5765 incr r -1
5767 lappend ret $r
5768 # see where it starts up again
5769 set r [expr {$row - $uparrowlen}]
5770 if {[lsearch -exact [lindex $rowidlist $r] $id] < 0} {
5771 while {[incr r] < $row &&
5772 [lsearch -exact [lindex $rowidlist $r] $id] < 0} {}
5773 } else {
5774 while {[incr r -1] >= $prevrow &&
5775 [lsearch -exact [lindex $rowidlist $r] $id] >= 0} {}
5776 incr r
5778 lappend ret $r
5781 if {$child eq $id} {
5782 lappend ret $row
5784 set prev $child
5785 set prevrow $row
5787 return $ret
5790 proc drawlineseg {id row endrow arrowlow} {
5791 global rowidlist displayorder iddrawn linesegs
5792 global canv colormap linespc curview maxlinelen parentlist
5794 set cols [list [lsearch -exact [lindex $rowidlist $row] $id]]
5795 set le [expr {$row + 1}]
5796 set arrowhigh 1
5797 while {1} {
5798 set c [lsearch -exact [lindex $rowidlist $le] $id]
5799 if {$c < 0} {
5800 incr le -1
5801 break
5803 lappend cols $c
5804 set x [lindex $displayorder $le]
5805 if {$x eq $id} {
5806 set arrowhigh 0
5807 break
5809 if {[info exists iddrawn($x)] || $le == $endrow} {
5810 set c [lsearch -exact [lindex $rowidlist [expr {$le+1}]] $id]
5811 if {$c >= 0} {
5812 lappend cols $c
5813 set arrowhigh 0
5815 break
5817 incr le
5819 if {$le <= $row} {
5820 return $row
5823 set lines {}
5824 set i 0
5825 set joinhigh 0
5826 if {[info exists linesegs($id)]} {
5827 set lines $linesegs($id)
5828 foreach li $lines {
5829 set r0 [lindex $li 0]
5830 if {$r0 > $row} {
5831 if {$r0 == $le && [lindex $li 1] - $row <= $maxlinelen} {
5832 set joinhigh 1
5834 break
5836 incr i
5839 set joinlow 0
5840 if {$i > 0} {
5841 set li [lindex $lines [expr {$i-1}]]
5842 set r1 [lindex $li 1]
5843 if {$r1 == $row && $le - [lindex $li 0] <= $maxlinelen} {
5844 set joinlow 1
5848 set x [lindex $cols [expr {$le - $row}]]
5849 set xp [lindex $cols [expr {$le - 1 - $row}]]
5850 set dir [expr {$xp - $x}]
5851 if {$joinhigh} {
5852 set ith [lindex $lines $i 2]
5853 set coords [$canv coords $ith]
5854 set ah [$canv itemcget $ith -arrow]
5855 set arrowhigh [expr {$ah eq "first" || $ah eq "both"}]
5856 set x2 [lindex $cols [expr {$le + 1 - $row}]]
5857 if {$x2 ne {} && $x - $x2 == $dir} {
5858 set coords [lrange $coords 0 end-2]
5860 } else {
5861 set coords [list [xc $le $x] [yc $le]]
5863 if {$joinlow} {
5864 set itl [lindex $lines [expr {$i-1}] 2]
5865 set al [$canv itemcget $itl -arrow]
5866 set arrowlow [expr {$al eq "last" || $al eq "both"}]
5867 } elseif {$arrowlow} {
5868 if {[lsearch -exact [lindex $rowidlist [expr {$row-1}]] $id] >= 0 ||
5869 [lsearch -exact [lindex $parentlist [expr {$row-1}]] $id] >= 0} {
5870 set arrowlow 0
5873 set arrow [lindex {none first last both} [expr {$arrowhigh + 2*$arrowlow}]]
5874 for {set y $le} {[incr y -1] > $row} {} {
5875 set x $xp
5876 set xp [lindex $cols [expr {$y - 1 - $row}]]
5877 set ndir [expr {$xp - $x}]
5878 if {$dir != $ndir || $xp < 0} {
5879 lappend coords [xc $y $x] [yc $y]
5881 set dir $ndir
5883 if {!$joinlow} {
5884 if {$xp < 0} {
5885 # join parent line to first child
5886 set ch [lindex $displayorder $row]
5887 set xc [lsearch -exact [lindex $rowidlist $row] $ch]
5888 if {$xc < 0} {
5889 puts "oops: drawlineseg: child $ch not on row $row"
5890 } elseif {$xc != $x} {
5891 if {($arrowhigh && $le == $row + 1) || $dir == 0} {
5892 set d [expr {int(0.5 * $linespc)}]
5893 set x1 [xc $row $x]
5894 if {$xc < $x} {
5895 set x2 [expr {$x1 - $d}]
5896 } else {
5897 set x2 [expr {$x1 + $d}]
5899 set y2 [yc $row]
5900 set y1 [expr {$y2 + $d}]
5901 lappend coords $x1 $y1 $x2 $y2
5902 } elseif {$xc < $x - 1} {
5903 lappend coords [xc $row [expr {$x-1}]] [yc $row]
5904 } elseif {$xc > $x + 1} {
5905 lappend coords [xc $row [expr {$x+1}]] [yc $row]
5907 set x $xc
5909 lappend coords [xc $row $x] [yc $row]
5910 } else {
5911 set xn [xc $row $xp]
5912 set yn [yc $row]
5913 lappend coords $xn $yn
5915 if {!$joinhigh} {
5916 assigncolor $id
5917 set t [$canv create line $coords -width [linewidth $id] \
5918 -fill $colormap($id) -tags lines.$id -arrow $arrow]
5919 $canv lower $t
5920 bindline $t $id
5921 set lines [linsert $lines $i [list $row $le $t]]
5922 } else {
5923 $canv coords $ith $coords
5924 if {$arrow ne $ah} {
5925 $canv itemconf $ith -arrow $arrow
5927 lset lines $i 0 $row
5929 } else {
5930 set xo [lsearch -exact [lindex $rowidlist [expr {$row - 1}]] $id]
5931 set ndir [expr {$xo - $xp}]
5932 set clow [$canv coords $itl]
5933 if {$dir == $ndir} {
5934 set clow [lrange $clow 2 end]
5936 set coords [concat $coords $clow]
5937 if {!$joinhigh} {
5938 lset lines [expr {$i-1}] 1 $le
5939 } else {
5940 # coalesce two pieces
5941 $canv delete $ith
5942 set b [lindex $lines [expr {$i-1}] 0]
5943 set e [lindex $lines $i 1]
5944 set lines [lreplace $lines [expr {$i-1}] $i [list $b $e $itl]]
5946 $canv coords $itl $coords
5947 if {$arrow ne $al} {
5948 $canv itemconf $itl -arrow $arrow
5952 set linesegs($id) $lines
5953 return $le
5956 proc drawparentlinks {id row} {
5957 global rowidlist canv colormap curview parentlist
5958 global idpos linespc
5960 set rowids [lindex $rowidlist $row]
5961 set col [lsearch -exact $rowids $id]
5962 if {$col < 0} return
5963 set olds [lindex $parentlist $row]
5964 set row2 [expr {$row + 1}]
5965 set x [xc $row $col]
5966 set y [yc $row]
5967 set y2 [yc $row2]
5968 set d [expr {int(0.5 * $linespc)}]
5969 set ymid [expr {$y + $d}]
5970 set ids [lindex $rowidlist $row2]
5971 # rmx = right-most X coord used
5972 set rmx 0
5973 foreach p $olds {
5974 set i [lsearch -exact $ids $p]
5975 if {$i < 0} {
5976 puts "oops, parent $p of $id not in list"
5977 continue
5979 set x2 [xc $row2 $i]
5980 if {$x2 > $rmx} {
5981 set rmx $x2
5983 set j [lsearch -exact $rowids $p]
5984 if {$j < 0} {
5985 # drawlineseg will do this one for us
5986 continue
5988 assigncolor $p
5989 # should handle duplicated parents here...
5990 set coords [list $x $y]
5991 if {$i != $col} {
5992 # if attaching to a vertical segment, draw a smaller
5993 # slant for visual distinctness
5994 if {$i == $j} {
5995 if {$i < $col} {
5996 lappend coords [expr {$x2 + $d}] $y $x2 $ymid
5997 } else {
5998 lappend coords [expr {$x2 - $d}] $y $x2 $ymid
6000 } elseif {$i < $col && $i < $j} {
6001 # segment slants towards us already
6002 lappend coords [xc $row $j] $y
6003 } else {
6004 if {$i < $col - 1} {
6005 lappend coords [expr {$x2 + $linespc}] $y
6006 } elseif {$i > $col + 1} {
6007 lappend coords [expr {$x2 - $linespc}] $y
6009 lappend coords $x2 $y2
6011 } else {
6012 lappend coords $x2 $y2
6014 set t [$canv create line $coords -width [linewidth $p] \
6015 -fill $colormap($p) -tags lines.$p]
6016 $canv lower $t
6017 bindline $t $p
6019 if {$rmx > [lindex $idpos($id) 1]} {
6020 lset idpos($id) 1 $rmx
6021 redrawtags $id
6025 proc drawlines {id} {
6026 global canv
6028 $canv itemconf lines.$id -width [linewidth $id]
6031 proc drawcmittext {id row col} {
6032 global linespc canv canv2 canv3 fgcolor curview
6033 global cmitlisted commitinfo rowidlist parentlist
6034 global rowtextx idpos idtags idheads idotherrefs
6035 global linehtag linentag linedtag selectedline
6036 global canvxmax boldids boldnameids fgcolor markedid
6037 global mainheadid nullid nullid2 circleitem circlecolors ctxbut
6038 global mainheadcirclecolor workingfilescirclecolor indexcirclecolor
6039 global circleoutlinecolor
6041 # listed is 0 for boundary, 1 for normal, 2 for negative, 3 for left, 4 for right
6042 set listed $cmitlisted($curview,$id)
6043 if {$id eq $nullid} {
6044 set ofill $workingfilescirclecolor
6045 } elseif {$id eq $nullid2} {
6046 set ofill $indexcirclecolor
6047 } elseif {$id eq $mainheadid} {
6048 set ofill $mainheadcirclecolor
6049 } else {
6050 set ofill [lindex $circlecolors $listed]
6052 set x [xc $row $col]
6053 set y [yc $row]
6054 set orad [expr {$linespc / 3}]
6055 if {$listed <= 2} {
6056 set t [$canv create oval [expr {$x - $orad}] [expr {$y - $orad}] \
6057 [expr {$x + $orad - 1}] [expr {$y + $orad - 1}] \
6058 -fill $ofill -outline $circleoutlinecolor -width 1 -tags circle]
6059 } elseif {$listed == 3} {
6060 # triangle pointing left for left-side commits
6061 set t [$canv create polygon \
6062 [expr {$x - $orad}] $y \
6063 [expr {$x + $orad - 1}] [expr {$y - $orad}] \
6064 [expr {$x + $orad - 1}] [expr {$y + $orad - 1}] \
6065 -fill $ofill -outline $circleoutlinecolor -width 1 -tags circle]
6066 } else {
6067 # triangle pointing right for right-side commits
6068 set t [$canv create polygon \
6069 [expr {$x + $orad - 1}] $y \
6070 [expr {$x - $orad}] [expr {$y - $orad}] \
6071 [expr {$x - $orad}] [expr {$y + $orad - 1}] \
6072 -fill $ofill -outline $circleoutlinecolor -width 1 -tags circle]
6074 set circleitem($row) $t
6075 $canv raise $t
6076 $canv bind $t <1> {selcanvline {} %x %y}
6077 set rmx [llength [lindex $rowidlist $row]]
6078 set olds [lindex $parentlist $row]
6079 if {$olds ne {}} {
6080 set nextids [lindex $rowidlist [expr {$row + 1}]]
6081 foreach p $olds {
6082 set i [lsearch -exact $nextids $p]
6083 if {$i > $rmx} {
6084 set rmx $i
6088 set xt [xc $row $rmx]
6089 set rowtextx($row) $xt
6090 set idpos($id) [list $x $xt $y]
6091 if {[info exists idtags($id)] || [info exists idheads($id)]
6092 || [info exists idotherrefs($id)]} {
6093 set xt [drawtags $id $x $xt $y]
6095 if {[lindex $commitinfo($id) 6] > 0} {
6096 set xt [drawnotesign $xt $y]
6098 set headline [lindex $commitinfo($id) 0]
6099 set name [lindex $commitinfo($id) 1]
6100 set date [lindex $commitinfo($id) 2]
6101 set date [formatdate $date]
6102 set font mainfont
6103 set nfont mainfont
6104 set isbold [ishighlighted $id]
6105 if {$isbold > 0} {
6106 lappend boldids $id
6107 set font mainfontbold
6108 if {$isbold > 1} {
6109 lappend boldnameids $id
6110 set nfont mainfontbold
6113 set linehtag($id) [$canv create text $xt $y -anchor w -fill $fgcolor \
6114 -text $headline -font $font -tags text]
6115 $canv bind $linehtag($id) $ctxbut "rowmenu %X %Y $id"
6116 set linentag($id) [$canv2 create text 3 $y -anchor w -fill $fgcolor \
6117 -text $name -font $nfont -tags text]
6118 set linedtag($id) [$canv3 create text 3 $y -anchor w -fill $fgcolor \
6119 -text $date -font mainfont -tags text]
6120 if {$selectedline == $row} {
6121 make_secsel $id
6123 if {[info exists markedid] && $markedid eq $id} {
6124 make_idmark $id
6126 set xr [expr {$xt + [font measure $font $headline]}]
6127 if {$xr > $canvxmax} {
6128 set canvxmax $xr
6129 setcanvscroll
6133 proc drawcmitrow {row} {
6134 global displayorder rowidlist nrows_drawn
6135 global iddrawn markingmatches
6136 global commitinfo numcommits
6137 global filehighlight fhighlights findpattern nhighlights
6138 global hlview vhighlights
6139 global highlight_related rhighlights
6141 if {$row >= $numcommits} return
6143 set id [lindex $displayorder $row]
6144 if {[info exists hlview] && ![info exists vhighlights($id)]} {
6145 askvhighlight $row $id
6147 if {[info exists filehighlight] && ![info exists fhighlights($id)]} {
6148 askfilehighlight $row $id
6150 if {$findpattern ne {} && ![info exists nhighlights($id)]} {
6151 askfindhighlight $row $id
6153 if {$highlight_related ne [mc "None"] && ![info exists rhighlights($id)]} {
6154 askrelhighlight $row $id
6156 if {![info exists iddrawn($id)]} {
6157 set col [lsearch -exact [lindex $rowidlist $row] $id]
6158 if {$col < 0} {
6159 puts "oops, row $row id $id not in list"
6160 return
6162 if {![info exists commitinfo($id)]} {
6163 getcommit $id
6165 assigncolor $id
6166 drawcmittext $id $row $col
6167 set iddrawn($id) 1
6168 incr nrows_drawn
6170 if {$markingmatches} {
6171 markrowmatches $row $id
6175 proc drawcommits {row {endrow {}}} {
6176 global numcommits iddrawn displayorder curview need_redisplay
6177 global parentlist rowidlist rowfinal uparrowlen downarrowlen nrows_drawn
6179 if {$row < 0} {
6180 set row 0
6182 if {$endrow eq {}} {
6183 set endrow $row
6185 if {$endrow >= $numcommits} {
6186 set endrow [expr {$numcommits - 1}]
6189 set rl1 [expr {$row - $downarrowlen - 3}]
6190 if {$rl1 < 0} {
6191 set rl1 0
6193 set ro1 [expr {$row - 3}]
6194 if {$ro1 < 0} {
6195 set ro1 0
6197 set r2 [expr {$endrow + $uparrowlen + 3}]
6198 if {$r2 > $numcommits} {
6199 set r2 $numcommits
6201 for {set r $rl1} {$r < $r2} {incr r} {
6202 if {[lindex $rowidlist $r] ne {} && [lindex $rowfinal $r]} {
6203 if {$rl1 < $r} {
6204 layoutrows $rl1 $r
6206 set rl1 [expr {$r + 1}]
6209 if {$rl1 < $r} {
6210 layoutrows $rl1 $r
6212 optimize_rows $ro1 0 $r2
6213 if {$need_redisplay || $nrows_drawn > 2000} {
6214 clear_display
6217 # make the lines join to already-drawn rows either side
6218 set r [expr {$row - 1}]
6219 if {$r < 0 || ![info exists iddrawn([lindex $displayorder $r])]} {
6220 set r $row
6222 set er [expr {$endrow + 1}]
6223 if {$er >= $numcommits ||
6224 ![info exists iddrawn([lindex $displayorder $er])]} {
6225 set er $endrow
6227 for {} {$r <= $er} {incr r} {
6228 set id [lindex $displayorder $r]
6229 set wasdrawn [info exists iddrawn($id)]
6230 drawcmitrow $r
6231 if {$r == $er} break
6232 set nextid [lindex $displayorder [expr {$r + 1}]]
6233 if {$wasdrawn && [info exists iddrawn($nextid)]} continue
6234 drawparentlinks $id $r
6236 set rowids [lindex $rowidlist $r]
6237 foreach lid $rowids {
6238 if {$lid eq {}} continue
6239 if {[info exists lineend($lid)] && $lineend($lid) > $r} continue
6240 if {$lid eq $id} {
6241 # see if this is the first child of any of its parents
6242 foreach p [lindex $parentlist $r] {
6243 if {[lsearch -exact $rowids $p] < 0} {
6244 # make this line extend up to the child
6245 set lineend($p) [drawlineseg $p $r $er 0]
6248 } else {
6249 set lineend($lid) [drawlineseg $lid $r $er 1]
6255 proc undolayout {row} {
6256 global uparrowlen mingaplen downarrowlen
6257 global rowidlist rowisopt rowfinal need_redisplay
6259 set r [expr {$row - ($uparrowlen + $mingaplen + $downarrowlen)}]
6260 if {$r < 0} {
6261 set r 0
6263 if {[llength $rowidlist] > $r} {
6264 incr r -1
6265 set rowidlist [lrange $rowidlist 0 $r]
6266 set rowfinal [lrange $rowfinal 0 $r]
6267 set rowisopt [lrange $rowisopt 0 $r]
6268 set need_redisplay 1
6269 run drawvisible
6273 proc drawvisible {} {
6274 global canv linespc curview vrowmod selectedline targetrow targetid
6275 global need_redisplay cscroll numcommits
6277 set fs [$canv yview]
6278 set ymax [lindex [$canv cget -scrollregion] 3]
6279 if {$ymax eq {} || $ymax == 0 || $numcommits == 0} return
6280 set f0 [lindex $fs 0]
6281 set f1 [lindex $fs 1]
6282 set y0 [expr {int($f0 * $ymax)}]
6283 set y1 [expr {int($f1 * $ymax)}]
6285 if {[info exists targetid]} {
6286 if {[commitinview $targetid $curview]} {
6287 set r [rowofcommit $targetid]
6288 if {$r != $targetrow} {
6289 # Fix up the scrollregion and change the scrolling position
6290 # now that our target row has moved.
6291 set diff [expr {($r - $targetrow) * $linespc}]
6292 set targetrow $r
6293 setcanvscroll
6294 set ymax [lindex [$canv cget -scrollregion] 3]
6295 incr y0 $diff
6296 incr y1 $diff
6297 set f0 [expr {$y0 / $ymax}]
6298 set f1 [expr {$y1 / $ymax}]
6299 allcanvs yview moveto $f0
6300 $cscroll set $f0 $f1
6301 set need_redisplay 1
6303 } else {
6304 unset targetid
6308 set row [expr {int(($y0 - 3) / $linespc) - 1}]
6309 set endrow [expr {int(($y1 - 3) / $linespc) + 1}]
6310 if {$endrow >= $vrowmod($curview)} {
6311 update_arcrows $curview
6313 if {$selectedline ne {} &&
6314 $row <= $selectedline && $selectedline <= $endrow} {
6315 set targetrow $selectedline
6316 } elseif {[info exists targetid]} {
6317 set targetrow [expr {int(($row + $endrow) / 2)}]
6319 if {[info exists targetrow]} {
6320 if {$targetrow >= $numcommits} {
6321 set targetrow [expr {$numcommits - 1}]
6323 set targetid [commitonrow $targetrow]
6325 drawcommits $row $endrow
6328 proc clear_display {} {
6329 global iddrawn linesegs need_redisplay nrows_drawn
6330 global vhighlights fhighlights nhighlights rhighlights
6331 global linehtag linentag linedtag boldids boldnameids
6333 allcanvs delete all
6334 catch {unset iddrawn}
6335 catch {unset linesegs}
6336 catch {unset linehtag}
6337 catch {unset linentag}
6338 catch {unset linedtag}
6339 set boldids {}
6340 set boldnameids {}
6341 catch {unset vhighlights}
6342 catch {unset fhighlights}
6343 catch {unset nhighlights}
6344 catch {unset rhighlights}
6345 set need_redisplay 0
6346 set nrows_drawn 0
6349 proc findcrossings {id} {
6350 global rowidlist parentlist numcommits displayorder
6352 set cross {}
6353 set ccross {}
6354 foreach {s e} [rowranges $id] {
6355 if {$e >= $numcommits} {
6356 set e [expr {$numcommits - 1}]
6358 if {$e <= $s} continue
6359 for {set row $e} {[incr row -1] >= $s} {} {
6360 set x [lsearch -exact [lindex $rowidlist $row] $id]
6361 if {$x < 0} break
6362 set olds [lindex $parentlist $row]
6363 set kid [lindex $displayorder $row]
6364 set kidx [lsearch -exact [lindex $rowidlist $row] $kid]
6365 if {$kidx < 0} continue
6366 set nextrow [lindex $rowidlist [expr {$row + 1}]]
6367 foreach p $olds {
6368 set px [lsearch -exact $nextrow $p]
6369 if {$px < 0} continue
6370 if {($kidx < $x && $x < $px) || ($px < $x && $x < $kidx)} {
6371 if {[lsearch -exact $ccross $p] >= 0} continue
6372 if {$x == $px + ($kidx < $px? -1: 1)} {
6373 lappend ccross $p
6374 } elseif {[lsearch -exact $cross $p] < 0} {
6375 lappend cross $p
6381 return [concat $ccross {{}} $cross]
6384 proc assigncolor {id} {
6385 global colormap colors nextcolor
6386 global parents children children curview
6388 if {[info exists colormap($id)]} return
6389 set ncolors [llength $colors]
6390 if {[info exists children($curview,$id)]} {
6391 set kids $children($curview,$id)
6392 } else {
6393 set kids {}
6395 if {[llength $kids] == 1} {
6396 set child [lindex $kids 0]
6397 if {[info exists colormap($child)]
6398 && [llength $parents($curview,$child)] == 1} {
6399 set colormap($id) $colormap($child)
6400 return
6403 set badcolors {}
6404 set origbad {}
6405 foreach x [findcrossings $id] {
6406 if {$x eq {}} {
6407 # delimiter between corner crossings and other crossings
6408 if {[llength $badcolors] >= $ncolors - 1} break
6409 set origbad $badcolors
6411 if {[info exists colormap($x)]
6412 && [lsearch -exact $badcolors $colormap($x)] < 0} {
6413 lappend badcolors $colormap($x)
6416 if {[llength $badcolors] >= $ncolors} {
6417 set badcolors $origbad
6419 set origbad $badcolors
6420 if {[llength $badcolors] < $ncolors - 1} {
6421 foreach child $kids {
6422 if {[info exists colormap($child)]
6423 && [lsearch -exact $badcolors $colormap($child)] < 0} {
6424 lappend badcolors $colormap($child)
6426 foreach p $parents($curview,$child) {
6427 if {[info exists colormap($p)]
6428 && [lsearch -exact $badcolors $colormap($p)] < 0} {
6429 lappend badcolors $colormap($p)
6433 if {[llength $badcolors] >= $ncolors} {
6434 set badcolors $origbad
6437 for {set i 0} {$i <= $ncolors} {incr i} {
6438 set c [lindex $colors $nextcolor]
6439 if {[incr nextcolor] >= $ncolors} {
6440 set nextcolor 0
6442 if {[lsearch -exact $badcolors $c]} break
6444 set colormap($id) $c
6447 proc bindline {t id} {
6448 global canv
6450 $canv bind $t <Enter> "lineenter %x %y $id"
6451 $canv bind $t <Motion> "linemotion %x %y $id"
6452 $canv bind $t <Leave> "lineleave $id"
6453 $canv bind $t <Button-1> "lineclick %x %y $id 1"
6456 proc graph_pane_width {} {
6457 global use_ttk
6459 if {$use_ttk} {
6460 set g [.tf.histframe.pwclist sashpos 0]
6461 } else {
6462 set g [.tf.histframe.pwclist sash coord 0]
6464 return [lindex $g 0]
6467 proc totalwidth {l font extra} {
6468 set tot 0
6469 foreach str $l {
6470 set tot [expr {$tot + [font measure $font $str] + $extra}]
6472 return $tot
6475 proc drawtags {id x xt y1} {
6476 global idtags idheads idotherrefs mainhead
6477 global linespc lthickness
6478 global canv rowtextx curview fgcolor bgcolor ctxbut
6479 global headbgcolor headfgcolor headoutlinecolor remotebgcolor
6480 global tagbgcolor tagfgcolor tagoutlinecolor
6481 global reflinecolor
6483 set marks {}
6484 set ntags 0
6485 set nheads 0
6486 set singletag 0
6487 set maxtags 3
6488 set maxtagpct 25
6489 set maxwidth [expr {[graph_pane_width] * $maxtagpct / 100}]
6490 set delta [expr {int(0.5 * ($linespc - $lthickness))}]
6491 set extra [expr {$delta + $lthickness + $linespc}]
6493 if {[info exists idtags($id)]} {
6494 set marks $idtags($id)
6495 set ntags [llength $marks]
6496 if {$ntags > $maxtags ||
6497 [totalwidth $marks mainfont $extra] > $maxwidth} {
6498 # show just a single "n tags..." tag
6499 set singletag 1
6500 if {$ntags == 1} {
6501 set marks [list "tag..."]
6502 } else {
6503 set marks [list [format "%d tags..." $ntags]]
6505 set ntags 1
6508 if {[info exists idheads($id)]} {
6509 set marks [concat $marks $idheads($id)]
6510 set nheads [llength $idheads($id)]
6512 if {[info exists idotherrefs($id)]} {
6513 set marks [concat $marks $idotherrefs($id)]
6515 if {$marks eq {}} {
6516 return $xt
6519 set yt [expr {$y1 - 0.5 * $linespc}]
6520 set yb [expr {$yt + $linespc - 1}]
6521 set xvals {}
6522 set wvals {}
6523 set i -1
6524 foreach tag $marks {
6525 incr i
6526 if {$i >= $ntags && $i < $ntags + $nheads && $tag eq $mainhead} {
6527 set wid [font measure mainfontbold $tag]
6528 } else {
6529 set wid [font measure mainfont $tag]
6531 lappend xvals $xt
6532 lappend wvals $wid
6533 set xt [expr {$xt + $wid + $extra}]
6535 set t [$canv create line $x $y1 [lindex $xvals end] $y1 \
6536 -width $lthickness -fill $reflinecolor -tags tag.$id]
6537 $canv lower $t
6538 foreach tag $marks x $xvals wid $wvals {
6539 set tag_quoted [string map {% %%} $tag]
6540 set xl [expr {$x + $delta}]
6541 set xr [expr {$x + $delta + $wid + $lthickness}]
6542 set font mainfont
6543 if {[incr ntags -1] >= 0} {
6544 # draw a tag
6545 set t [$canv create polygon $x [expr {$yt + $delta}] $xl $yt \
6546 $xr $yt $xr $yb $xl $yb $x [expr {$yb - $delta}] \
6547 -width 1 -outline $tagoutlinecolor -fill $tagbgcolor \
6548 -tags tag.$id]
6549 if {$singletag} {
6550 set tagclick [list showtags $id 1]
6551 } else {
6552 set tagclick [list showtag $tag_quoted 1]
6554 $canv bind $t <1> $tagclick
6555 set rowtextx([rowofcommit $id]) [expr {$xr + $linespc}]
6556 } else {
6557 # draw a head or other ref
6558 if {[incr nheads -1] >= 0} {
6559 set col $headbgcolor
6560 if {$tag eq $mainhead} {
6561 set font mainfontbold
6563 } else {
6564 set col "#ddddff"
6566 set xl [expr {$xl - $delta/2}]
6567 $canv create polygon $x $yt $xr $yt $xr $yb $x $yb \
6568 -width 1 -outline black -fill $col -tags tag.$id
6569 if {[regexp {^(remotes/.*/|remotes/)} $tag match remoteprefix]} {
6570 set rwid [font measure mainfont $remoteprefix]
6571 set xi [expr {$x + 1}]
6572 set yti [expr {$yt + 1}]
6573 set xri [expr {$x + $rwid}]
6574 $canv create polygon $xi $yti $xri $yti $xri $yb $xi $yb \
6575 -width 0 -fill $remotebgcolor -tags tag.$id
6578 set t [$canv create text $xl $y1 -anchor w -text $tag -fill $headfgcolor \
6579 -font $font -tags [list tag.$id text]]
6580 if {$ntags >= 0} {
6581 $canv bind $t <1> $tagclick
6582 } elseif {$nheads >= 0} {
6583 $canv bind $t $ctxbut [list headmenu %X %Y $id $tag_quoted]
6586 return $xt
6589 proc drawnotesign {xt y} {
6590 global linespc canv fgcolor
6592 set orad [expr {$linespc / 3}]
6593 set t [$canv create rectangle [expr {$xt - $orad}] [expr {$y - $orad}] \
6594 [expr {$xt + $orad - 1}] [expr {$y + $orad - 1}] \
6595 -fill yellow -outline $fgcolor -width 1 -tags circle]
6596 set xt [expr {$xt + $orad * 3}]
6597 return $xt
6600 proc xcoord {i level ln} {
6601 global canvx0 xspc1 xspc2
6603 set x [expr {$canvx0 + $i * $xspc1($ln)}]
6604 if {$i > 0 && $i == $level} {
6605 set x [expr {$x + 0.5 * ($xspc2 - $xspc1($ln))}]
6606 } elseif {$i > $level} {
6607 set x [expr {$x + $xspc2 - $xspc1($ln)}]
6609 return $x
6612 proc show_status {msg} {
6613 global canv fgcolor
6615 clear_display
6616 $canv create text 3 3 -anchor nw -text $msg -font mainfont \
6617 -tags text -fill $fgcolor
6620 # Don't change the text pane cursor if it is currently the hand cursor,
6621 # showing that we are over a sha1 ID link.
6622 proc settextcursor {c} {
6623 global ctext curtextcursor
6625 if {[$ctext cget -cursor] == $curtextcursor} {
6626 $ctext config -cursor $c
6628 set curtextcursor $c
6631 proc nowbusy {what {name {}}} {
6632 global isbusy busyname statusw
6634 if {[array names isbusy] eq {}} {
6635 . config -cursor watch
6636 settextcursor watch
6638 set isbusy($what) 1
6639 set busyname($what) $name
6640 if {$name ne {}} {
6641 $statusw conf -text $name
6645 proc notbusy {what} {
6646 global isbusy maincursor textcursor busyname statusw
6648 catch {
6649 unset isbusy($what)
6650 if {$busyname($what) ne {} &&
6651 [$statusw cget -text] eq $busyname($what)} {
6652 $statusw conf -text {}
6655 if {[array names isbusy] eq {}} {
6656 . config -cursor $maincursor
6657 settextcursor $textcursor
6661 proc findmatches {f} {
6662 global findtype findstring
6663 if {$findtype == [mc "Regexp"]} {
6664 set matches [regexp -indices -all -inline $findstring $f]
6665 } else {
6666 set fs $findstring
6667 if {$findtype == [mc "IgnCase"]} {
6668 set f [string tolower $f]
6669 set fs [string tolower $fs]
6671 set matches {}
6672 set i 0
6673 set l [string length $fs]
6674 while {[set j [string first $fs $f $i]] >= 0} {
6675 lappend matches [list $j [expr {$j+$l-1}]]
6676 set i [expr {$j + $l}]
6679 return $matches
6682 proc dofind {{dirn 1} {wrap 1}} {
6683 global findstring findstartline findcurline selectedline numcommits
6684 global gdttype filehighlight fh_serial find_dirn findallowwrap
6686 if {[info exists find_dirn]} {
6687 if {$find_dirn == $dirn} return
6688 stopfinding
6690 focus .
6691 if {$findstring eq {} || $numcommits == 0} return
6692 if {$selectedline eq {}} {
6693 set findstartline [lindex [visiblerows] [expr {$dirn < 0}]]
6694 } else {
6695 set findstartline $selectedline
6697 set findcurline $findstartline
6698 nowbusy finding [mc "Searching"]
6699 if {$gdttype ne [mc "containing:"] && ![info exists filehighlight]} {
6700 after cancel do_file_hl $fh_serial
6701 do_file_hl $fh_serial
6703 set find_dirn $dirn
6704 set findallowwrap $wrap
6705 run findmore
6708 proc stopfinding {} {
6709 global find_dirn findcurline fprogcoord
6711 if {[info exists find_dirn]} {
6712 unset find_dirn
6713 unset findcurline
6714 notbusy finding
6715 set fprogcoord 0
6716 adjustprogress
6718 stopblaming
6721 proc findmore {} {
6722 global commitdata commitinfo numcommits findpattern findloc
6723 global findstartline findcurline findallowwrap
6724 global find_dirn gdttype fhighlights fprogcoord
6725 global curview varcorder vrownum varccommits vrowmod
6727 if {![info exists find_dirn]} {
6728 return 0
6730 set fldtypes [list [mc "Headline"] [mc "Author"] "" [mc "Committer"] "" [mc "Comments"]]
6731 set l $findcurline
6732 set moretodo 0
6733 if {$find_dirn > 0} {
6734 incr l
6735 if {$l >= $numcommits} {
6736 set l 0
6738 if {$l <= $findstartline} {
6739 set lim [expr {$findstartline + 1}]
6740 } else {
6741 set lim $numcommits
6742 set moretodo $findallowwrap
6744 } else {
6745 if {$l == 0} {
6746 set l $numcommits
6748 incr l -1
6749 if {$l >= $findstartline} {
6750 set lim [expr {$findstartline - 1}]
6751 } else {
6752 set lim -1
6753 set moretodo $findallowwrap
6756 set n [expr {($lim - $l) * $find_dirn}]
6757 if {$n > 500} {
6758 set n 500
6759 set moretodo 1
6761 if {$l + ($find_dirn > 0? $n: 1) > $vrowmod($curview)} {
6762 update_arcrows $curview
6764 set found 0
6765 set domore 1
6766 set ai [bsearch $vrownum($curview) $l]
6767 set a [lindex $varcorder($curview) $ai]
6768 set arow [lindex $vrownum($curview) $ai]
6769 set ids [lindex $varccommits($curview,$a)]
6770 set arowend [expr {$arow + [llength $ids]}]
6771 if {$gdttype eq [mc "containing:"]} {
6772 for {} {$n > 0} {incr n -1; incr l $find_dirn} {
6773 if {$l < $arow || $l >= $arowend} {
6774 incr ai $find_dirn
6775 set a [lindex $varcorder($curview) $ai]
6776 set arow [lindex $vrownum($curview) $ai]
6777 set ids [lindex $varccommits($curview,$a)]
6778 set arowend [expr {$arow + [llength $ids]}]
6780 set id [lindex $ids [expr {$l - $arow}]]
6781 # shouldn't happen unless git log doesn't give all the commits...
6782 if {![info exists commitdata($id)] ||
6783 ![doesmatch $commitdata($id)]} {
6784 continue
6786 if {![info exists commitinfo($id)]} {
6787 getcommit $id
6789 set info $commitinfo($id)
6790 foreach f $info ty $fldtypes {
6791 if {$ty eq ""} continue
6792 if {($findloc eq [mc "All fields"] || $findloc eq $ty) &&
6793 [doesmatch $f]} {
6794 set found 1
6795 break
6798 if {$found} break
6800 } else {
6801 for {} {$n > 0} {incr n -1; incr l $find_dirn} {
6802 if {$l < $arow || $l >= $arowend} {
6803 incr ai $find_dirn
6804 set a [lindex $varcorder($curview) $ai]
6805 set arow [lindex $vrownum($curview) $ai]
6806 set ids [lindex $varccommits($curview,$a)]
6807 set arowend [expr {$arow + [llength $ids]}]
6809 set id [lindex $ids [expr {$l - $arow}]]
6810 if {![info exists fhighlights($id)]} {
6811 # this sets fhighlights($id) to -1
6812 askfilehighlight $l $id
6814 if {$fhighlights($id) > 0} {
6815 set found $domore
6816 break
6818 if {$fhighlights($id) < 0} {
6819 if {$domore} {
6820 set domore 0
6821 set findcurline [expr {$l - $find_dirn}]
6826 if {$found || ($domore && !$moretodo)} {
6827 unset findcurline
6828 unset find_dirn
6829 notbusy finding
6830 set fprogcoord 0
6831 adjustprogress
6832 if {$found} {
6833 findselectline $l
6834 } else {
6835 bell
6837 return 0
6839 if {!$domore} {
6840 flushhighlights
6841 } else {
6842 set findcurline [expr {$l - $find_dirn}]
6844 set n [expr {($findcurline - $findstartline) * $find_dirn - 1}]
6845 if {$n < 0} {
6846 incr n $numcommits
6848 set fprogcoord [expr {$n * 1.0 / $numcommits}]
6849 adjustprogress
6850 return $domore
6853 proc findselectline {l} {
6854 global findloc commentend ctext findcurline markingmatches gdttype
6856 set markingmatches [expr {$gdttype eq [mc "containing:"]}]
6857 set findcurline $l
6858 selectline $l 1
6859 if {$markingmatches &&
6860 ($findloc eq [mc "All fields"] || $findloc eq [mc "Comments"])} {
6861 # highlight the matches in the comments
6862 set f [$ctext get 1.0 $commentend]
6863 set matches [findmatches $f]
6864 foreach match $matches {
6865 set start [lindex $match 0]
6866 set end [expr {[lindex $match 1] + 1}]
6867 $ctext tag add found "1.0 + $start c" "1.0 + $end c"
6870 drawvisible
6873 # mark the bits of a headline or author that match a find string
6874 proc markmatches {canv l str tag matches font row} {
6875 global selectedline
6877 set bbox [$canv bbox $tag]
6878 set x0 [lindex $bbox 0]
6879 set y0 [lindex $bbox 1]
6880 set y1 [lindex $bbox 3]
6881 foreach match $matches {
6882 set start [lindex $match 0]
6883 set end [lindex $match 1]
6884 if {$start > $end} continue
6885 set xoff [font measure $font [string range $str 0 [expr {$start-1}]]]
6886 set xlen [font measure $font [string range $str 0 [expr {$end}]]]
6887 set t [$canv create rect [expr {$x0+$xoff}] $y0 \
6888 [expr {$x0+$xlen+2}] $y1 \
6889 -outline {} -tags [list match$l matches] -fill yellow]
6890 $canv lower $t
6891 if {$row == $selectedline} {
6892 $canv raise $t secsel
6897 proc unmarkmatches {} {
6898 global markingmatches
6900 allcanvs delete matches
6901 set markingmatches 0
6902 stopfinding
6905 proc selcanvline {w x y} {
6906 global canv canvy0 ctext linespc
6907 global rowtextx
6908 set ymax [lindex [$canv cget -scrollregion] 3]
6909 if {$ymax == {}} return
6910 set yfrac [lindex [$canv yview] 0]
6911 set y [expr {$y + $yfrac * $ymax}]
6912 set l [expr {int(($y - $canvy0) / $linespc + 0.5)}]
6913 if {$l < 0} {
6914 set l 0
6916 if {$w eq $canv} {
6917 set xmax [lindex [$canv cget -scrollregion] 2]
6918 set xleft [expr {[lindex [$canv xview] 0] * $xmax}]
6919 if {![info exists rowtextx($l)] || $xleft + $x < $rowtextx($l)} return
6921 unmarkmatches
6922 selectline $l 1
6925 proc commit_descriptor {p} {
6926 global commitinfo
6927 if {![info exists commitinfo($p)]} {
6928 getcommit $p
6930 set l "..."
6931 if {[llength $commitinfo($p)] > 1} {
6932 set l [lindex $commitinfo($p) 0]
6934 return "$p ($l)\n"
6937 # append some text to the ctext widget, and make any SHA1 ID
6938 # that we know about be a clickable link.
6939 proc appendwithlinks {text tags} {
6940 global ctext linknum curview
6942 set start [$ctext index "end - 1c"]
6943 $ctext insert end $text $tags
6944 set links [regexp -indices -all -inline {(?:\m|-g)[0-9a-f]{6,40}\M} $text]
6945 foreach l $links {
6946 set s [lindex $l 0]
6947 set e [lindex $l 1]
6948 set linkid [string range $text $s $e]
6949 incr e
6950 $ctext tag delete link$linknum
6951 $ctext tag add link$linknum "$start + $s c" "$start + $e c"
6952 setlink $linkid link$linknum
6953 incr linknum
6957 proc setlink {id lk} {
6958 global curview ctext pendinglinks
6959 global linkfgcolor
6961 if {[string range $id 0 1] eq "-g"} {
6962 set id [string range $id 2 end]
6965 set known 0
6966 if {[string length $id] < 40} {
6967 set matches [longid $id]
6968 if {[llength $matches] > 0} {
6969 if {[llength $matches] > 1} return
6970 set known 1
6971 set id [lindex $matches 0]
6973 } else {
6974 set known [commitinview $id $curview]
6976 if {$known} {
6977 $ctext tag conf $lk -foreground $linkfgcolor -underline 1
6978 $ctext tag bind $lk <1> [list selbyid $id]
6979 $ctext tag bind $lk <Enter> {linkcursor %W 1}
6980 $ctext tag bind $lk <Leave> {linkcursor %W -1}
6981 } else {
6982 lappend pendinglinks($id) $lk
6983 interestedin $id {makelink %P}
6987 proc appendshortlink {id {pre {}} {post {}}} {
6988 global ctext linknum
6990 $ctext insert end $pre
6991 $ctext tag delete link$linknum
6992 $ctext insert end [string range $id 0 7] link$linknum
6993 $ctext insert end $post
6994 setlink $id link$linknum
6995 incr linknum
6998 proc makelink {id} {
6999 global pendinglinks
7001 if {![info exists pendinglinks($id)]} return
7002 foreach lk $pendinglinks($id) {
7003 setlink $id $lk
7005 unset pendinglinks($id)
7008 proc linkcursor {w inc} {
7009 global linkentercount curtextcursor
7011 if {[incr linkentercount $inc] > 0} {
7012 $w configure -cursor hand2
7013 } else {
7014 $w configure -cursor $curtextcursor
7015 if {$linkentercount < 0} {
7016 set linkentercount 0
7021 proc viewnextline {dir} {
7022 global canv linespc
7024 $canv delete hover
7025 set ymax [lindex [$canv cget -scrollregion] 3]
7026 set wnow [$canv yview]
7027 set wtop [expr {[lindex $wnow 0] * $ymax}]
7028 set newtop [expr {$wtop + $dir * $linespc}]
7029 if {$newtop < 0} {
7030 set newtop 0
7031 } elseif {$newtop > $ymax} {
7032 set newtop $ymax
7034 allcanvs yview moveto [expr {$newtop * 1.0 / $ymax}]
7037 # add a list of tag or branch names at position pos
7038 # returns the number of names inserted
7039 proc appendrefs {pos ids var} {
7040 global ctext linknum curview $var maxrefs visiblerefs mainheadid
7042 if {[catch {$ctext index $pos}]} {
7043 return 0
7045 $ctext conf -state normal
7046 $ctext delete $pos "$pos lineend"
7047 set tags {}
7048 foreach id $ids {
7049 foreach tag [set $var\($id\)] {
7050 lappend tags [list $tag $id]
7054 set sep {}
7055 set tags [lsort -index 0 -decreasing $tags]
7056 set nutags 0
7058 if {[llength $tags] > $maxrefs} {
7059 # If we are displaying heads, and there are too many,
7060 # see if there are some important heads to display.
7061 # Currently that are the current head and heads listed in $visiblerefs option
7062 set itags {}
7063 if {$var eq "idheads"} {
7064 set utags {}
7065 foreach ti $tags {
7066 set hname [lindex $ti 0]
7067 set id [lindex $ti 1]
7068 if {([lsearch -exact $visiblerefs $hname] != -1 || $id eq $mainheadid) &&
7069 [llength $itags] < $maxrefs} {
7070 lappend itags $ti
7071 } else {
7072 lappend utags $ti
7075 set tags $utags
7077 if {$itags ne {}} {
7078 set str [mc "and many more"]
7079 set sep " "
7080 } else {
7081 set str [mc "many"]
7083 $ctext insert $pos "$str ([llength $tags])"
7084 set nutags [llength $tags]
7085 set tags $itags
7088 foreach ti $tags {
7089 set id [lindex $ti 1]
7090 set lk link$linknum
7091 incr linknum
7092 $ctext tag delete $lk
7093 $ctext insert $pos $sep
7094 $ctext insert $pos [lindex $ti 0] $lk
7095 setlink $id $lk
7096 set sep ", "
7098 $ctext tag add wwrap "$pos linestart" "$pos lineend"
7099 $ctext conf -state disabled
7100 return [expr {[llength $tags] + $nutags}]
7103 # called when we have finished computing the nearby tags
7104 proc dispneartags {delay} {
7105 global selectedline currentid showneartags tagphase
7107 if {$selectedline eq {} || !$showneartags} return
7108 after cancel dispnexttag
7109 if {$delay} {
7110 after 200 dispnexttag
7111 set tagphase -1
7112 } else {
7113 after idle dispnexttag
7114 set tagphase 0
7118 proc dispnexttag {} {
7119 global selectedline currentid showneartags tagphase ctext
7121 if {$selectedline eq {} || !$showneartags} return
7122 switch -- $tagphase {
7124 set dtags [desctags $currentid]
7125 if {$dtags ne {}} {
7126 appendrefs precedes $dtags idtags
7130 set atags [anctags $currentid]
7131 if {$atags ne {}} {
7132 appendrefs follows $atags idtags
7136 set dheads [descheads $currentid]
7137 if {$dheads ne {}} {
7138 if {[appendrefs branch $dheads idheads] > 1
7139 && [$ctext get "branch -3c"] eq "h"} {
7140 # turn "Branch" into "Branches"
7141 $ctext conf -state normal
7142 $ctext insert "branch -2c" "es"
7143 $ctext conf -state disabled
7148 if {[incr tagphase] <= 2} {
7149 after idle dispnexttag
7153 proc make_secsel {id} {
7154 global linehtag linentag linedtag canv canv2 canv3
7156 if {![info exists linehtag($id)]} return
7157 $canv delete secsel
7158 set t [eval $canv create rect [$canv bbox $linehtag($id)] -outline {{}} \
7159 -tags secsel -fill [$canv cget -selectbackground]]
7160 $canv lower $t
7161 $canv2 delete secsel
7162 set t [eval $canv2 create rect [$canv2 bbox $linentag($id)] -outline {{}} \
7163 -tags secsel -fill [$canv2 cget -selectbackground]]
7164 $canv2 lower $t
7165 $canv3 delete secsel
7166 set t [eval $canv3 create rect [$canv3 bbox $linedtag($id)] -outline {{}} \
7167 -tags secsel -fill [$canv3 cget -selectbackground]]
7168 $canv3 lower $t
7171 proc make_idmark {id} {
7172 global linehtag canv fgcolor
7174 if {![info exists linehtag($id)]} return
7175 $canv delete markid
7176 set t [eval $canv create rect [$canv bbox $linehtag($id)] \
7177 -tags markid -outline $fgcolor]
7178 $canv raise $t
7181 proc selectline {l isnew {desired_loc {}} {switch_to_patch 0}} {
7182 global canv ctext commitinfo selectedline
7183 global canvy0 linespc parents children curview
7184 global currentid sha1entry
7185 global commentend idtags linknum
7186 global mergemax numcommits pending_select
7187 global cmitmode showneartags allcommits
7188 global targetrow targetid lastscrollrows
7189 global autoselect autosellen jump_to_here
7190 global vinlinediff
7192 catch {unset pending_select}
7193 $canv delete hover
7194 normalline
7195 unsel_reflist
7196 stopfinding
7197 if {$l < 0 || $l >= $numcommits} return
7198 set id [commitonrow $l]
7199 set targetid $id
7200 set targetrow $l
7201 set selectedline $l
7202 set currentid $id
7203 if {$lastscrollrows < $numcommits} {
7204 setcanvscroll
7207 if {$cmitmode ne "patch" && $switch_to_patch} {
7208 set cmitmode "patch"
7211 set y [expr {$canvy0 + $l * $linespc}]
7212 set ymax [lindex [$canv cget -scrollregion] 3]
7213 set ytop [expr {$y - $linespc - 1}]
7214 set ybot [expr {$y + $linespc + 1}]
7215 set wnow [$canv yview]
7216 set wtop [expr {[lindex $wnow 0] * $ymax}]
7217 set wbot [expr {[lindex $wnow 1] * $ymax}]
7218 set wh [expr {$wbot - $wtop}]
7219 set newtop $wtop
7220 if {$ytop < $wtop} {
7221 if {$ybot < $wtop} {
7222 set newtop [expr {$y - $wh / 2.0}]
7223 } else {
7224 set newtop $ytop
7225 if {$newtop > $wtop - $linespc} {
7226 set newtop [expr {$wtop - $linespc}]
7229 } elseif {$ybot > $wbot} {
7230 if {$ytop > $wbot} {
7231 set newtop [expr {$y - $wh / 2.0}]
7232 } else {
7233 set newtop [expr {$ybot - $wh}]
7234 if {$newtop < $wtop + $linespc} {
7235 set newtop [expr {$wtop + $linespc}]
7239 if {$newtop != $wtop} {
7240 if {$newtop < 0} {
7241 set newtop 0
7243 allcanvs yview moveto [expr {$newtop * 1.0 / $ymax}]
7244 drawvisible
7247 make_secsel $id
7249 if {$isnew} {
7250 addtohistory [list selbyid $id 0] savecmitpos
7253 $sha1entry delete 0 end
7254 $sha1entry insert 0 $id
7255 if {$autoselect} {
7256 $sha1entry selection range 0 $autosellen
7258 rhighlight_sel $id
7260 $ctext conf -state normal
7261 clear_ctext
7262 set linknum 0
7263 if {![info exists commitinfo($id)]} {
7264 getcommit $id
7266 set info $commitinfo($id)
7267 set date [formatdate [lindex $info 2]]
7268 $ctext insert end "[mc "Author"]: [lindex $info 1] $date\n"
7269 set date [formatdate [lindex $info 4]]
7270 $ctext insert end "[mc "Committer"]: [lindex $info 3] $date\n"
7271 if {[info exists idtags($id)]} {
7272 $ctext insert end [mc "Tags:"]
7273 foreach tag $idtags($id) {
7274 $ctext insert end " $tag"
7276 $ctext insert end "\n"
7279 set headers {}
7280 set olds $parents($curview,$id)
7281 if {[llength $olds] > 1} {
7282 set np 0
7283 foreach p $olds {
7284 if {$np >= $mergemax} {
7285 set tag mmax
7286 } else {
7287 set tag m$np
7289 $ctext insert end "[mc "Parent"]: " $tag
7290 appendwithlinks [commit_descriptor $p] {}
7291 incr np
7293 } else {
7294 foreach p $olds {
7295 append headers "[mc "Parent"]: [commit_descriptor $p]"
7299 foreach c $children($curview,$id) {
7300 append headers "[mc "Child"]: [commit_descriptor $c]"
7303 # make anything that looks like a SHA1 ID be a clickable link
7304 appendwithlinks $headers {}
7305 if {$showneartags} {
7306 if {![info exists allcommits]} {
7307 getallcommits
7309 $ctext insert end "[mc "Branch"]: "
7310 $ctext mark set branch "end -1c"
7311 $ctext mark gravity branch left
7312 $ctext insert end "\n[mc "Follows"]: "
7313 $ctext mark set follows "end -1c"
7314 $ctext mark gravity follows left
7315 $ctext insert end "\n[mc "Precedes"]: "
7316 $ctext mark set precedes "end -1c"
7317 $ctext mark gravity precedes left
7318 $ctext insert end "\n"
7319 dispneartags 1
7321 $ctext insert end "\n"
7322 set comment [lindex $info 5]
7323 if {[string first "\r" $comment] >= 0} {
7324 set comment [string map {"\r" "\n "} $comment]
7326 appendwithlinks $comment {comment}
7328 $ctext tag remove found 1.0 end
7329 $ctext conf -state disabled
7330 set commentend [$ctext index "end - 1c"]
7332 set jump_to_here $desired_loc
7333 init_flist [mc "Comments"]
7334 if {$cmitmode eq "tree"} {
7335 gettree $id
7336 } elseif {$vinlinediff($curview) == 1} {
7337 showinlinediff $id
7338 } elseif {[llength $olds] <= 1} {
7339 startdiff $id
7340 } else {
7341 mergediff $id
7345 proc selfirstline {} {
7346 unmarkmatches
7347 selectline 0 1
7350 proc sellastline {} {
7351 global numcommits
7352 unmarkmatches
7353 set l [expr {$numcommits - 1}]
7354 selectline $l 1
7357 proc selnextline {dir} {
7358 global selectedline
7359 focus .
7360 if {$selectedline eq {}} return
7361 set l [expr {$selectedline + $dir}]
7362 unmarkmatches
7363 selectline $l 1
7366 proc selnextpage {dir} {
7367 global canv linespc selectedline numcommits
7369 set lpp [expr {([winfo height $canv] - 2) / $linespc}]
7370 if {$lpp < 1} {
7371 set lpp 1
7373 allcanvs yview scroll [expr {$dir * $lpp}] units
7374 drawvisible
7375 if {$selectedline eq {}} return
7376 set l [expr {$selectedline + $dir * $lpp}]
7377 if {$l < 0} {
7378 set l 0
7379 } elseif {$l >= $numcommits} {
7380 set l [expr $numcommits - 1]
7382 unmarkmatches
7383 selectline $l 1
7386 proc unselectline {} {
7387 global selectedline currentid
7389 set selectedline {}
7390 catch {unset currentid}
7391 allcanvs delete secsel
7392 rhighlight_none
7395 proc reselectline {} {
7396 global selectedline
7398 if {$selectedline ne {}} {
7399 selectline $selectedline 0
7403 proc addtohistory {cmd {saveproc {}}} {
7404 global history historyindex curview
7406 unset_posvars
7407 save_position
7408 set elt [list $curview $cmd $saveproc {}]
7409 if {$historyindex > 0
7410 && [lindex $history [expr {$historyindex - 1}]] == $elt} {
7411 return
7414 if {$historyindex < [llength $history]} {
7415 set history [lreplace $history $historyindex end $elt]
7416 } else {
7417 lappend history $elt
7419 incr historyindex
7420 if {$historyindex > 1} {
7421 .tf.bar.leftbut conf -state normal
7422 } else {
7423 .tf.bar.leftbut conf -state disabled
7425 .tf.bar.rightbut conf -state disabled
7428 # save the scrolling position of the diff display pane
7429 proc save_position {} {
7430 global historyindex history
7432 if {$historyindex < 1} return
7433 set hi [expr {$historyindex - 1}]
7434 set fn [lindex $history $hi 2]
7435 if {$fn ne {}} {
7436 lset history $hi 3 [eval $fn]
7440 proc unset_posvars {} {
7441 global last_posvars
7443 if {[info exists last_posvars]} {
7444 foreach {var val} $last_posvars {
7445 global $var
7446 catch {unset $var}
7448 unset last_posvars
7452 proc godo {elt} {
7453 global curview last_posvars
7455 set view [lindex $elt 0]
7456 set cmd [lindex $elt 1]
7457 set pv [lindex $elt 3]
7458 if {$curview != $view} {
7459 showview $view
7461 unset_posvars
7462 foreach {var val} $pv {
7463 global $var
7464 set $var $val
7466 set last_posvars $pv
7467 eval $cmd
7470 proc goback {} {
7471 global history historyindex
7472 focus .
7474 if {$historyindex > 1} {
7475 save_position
7476 incr historyindex -1
7477 godo [lindex $history [expr {$historyindex - 1}]]
7478 .tf.bar.rightbut conf -state normal
7480 if {$historyindex <= 1} {
7481 .tf.bar.leftbut conf -state disabled
7485 proc goforw {} {
7486 global history historyindex
7487 focus .
7489 if {$historyindex < [llength $history]} {
7490 save_position
7491 set cmd [lindex $history $historyindex]
7492 incr historyindex
7493 godo $cmd
7494 .tf.bar.leftbut conf -state normal
7496 if {$historyindex >= [llength $history]} {
7497 .tf.bar.rightbut conf -state disabled
7501 proc gettree {id} {
7502 global treefilelist treeidlist diffids diffmergeid treepending
7503 global nullid nullid2
7505 set diffids $id
7506 catch {unset diffmergeid}
7507 if {![info exists treefilelist($id)]} {
7508 if {![info exists treepending]} {
7509 if {$id eq $nullid} {
7510 set cmd [list | git ls-files]
7511 } elseif {$id eq $nullid2} {
7512 set cmd [list | git ls-files --stage -t]
7513 } else {
7514 set cmd [list | git ls-tree -r $id]
7516 if {[catch {set gtf [open $cmd r]}]} {
7517 return
7519 set treepending $id
7520 set treefilelist($id) {}
7521 set treeidlist($id) {}
7522 fconfigure $gtf -blocking 0 -encoding binary
7523 filerun $gtf [list gettreeline $gtf $id]
7525 } else {
7526 setfilelist $id
7530 proc gettreeline {gtf id} {
7531 global treefilelist treeidlist treepending cmitmode diffids nullid nullid2
7533 set nl 0
7534 while {[incr nl] <= 1000 && [gets $gtf line] >= 0} {
7535 if {$diffids eq $nullid} {
7536 set fname $line
7537 } else {
7538 set i [string first "\t" $line]
7539 if {$i < 0} continue
7540 set fname [string range $line [expr {$i+1}] end]
7541 set line [string range $line 0 [expr {$i-1}]]
7542 if {$diffids ne $nullid2 && [lindex $line 1] ne "blob"} continue
7543 set sha1 [lindex $line 2]
7544 lappend treeidlist($id) $sha1
7546 if {[string index $fname 0] eq "\""} {
7547 set fname [lindex $fname 0]
7549 set fname [encoding convertfrom $fname]
7550 lappend treefilelist($id) $fname
7552 if {![eof $gtf]} {
7553 return [expr {$nl >= 1000? 2: 1}]
7555 close $gtf
7556 unset treepending
7557 if {$cmitmode ne "tree"} {
7558 if {![info exists diffmergeid]} {
7559 gettreediffs $diffids
7561 } elseif {$id ne $diffids} {
7562 gettree $diffids
7563 } else {
7564 setfilelist $id
7566 return 0
7569 proc showfile {f} {
7570 global treefilelist treeidlist diffids nullid nullid2
7571 global ctext_file_names ctext_file_lines
7572 global ctext commentend
7574 set i [lsearch -exact $treefilelist($diffids) $f]
7575 if {$i < 0} {
7576 puts "oops, $f not in list for id $diffids"
7577 return
7579 if {$diffids eq $nullid} {
7580 if {[catch {set bf [open $f r]} err]} {
7581 puts "oops, can't read $f: $err"
7582 return
7584 } else {
7585 set blob [lindex $treeidlist($diffids) $i]
7586 if {[catch {set bf [open [concat | git cat-file blob $blob] r]} err]} {
7587 puts "oops, error reading blob $blob: $err"
7588 return
7591 fconfigure $bf -blocking 0 -encoding [get_path_encoding $f]
7592 filerun $bf [list getblobline $bf $diffids]
7593 $ctext config -state normal
7594 clear_ctext $commentend
7595 lappend ctext_file_names $f
7596 lappend ctext_file_lines [lindex [split $commentend "."] 0]
7597 $ctext insert end "\n"
7598 $ctext insert end "$f\n" filesep
7599 $ctext config -state disabled
7600 $ctext yview $commentend
7601 settabs 0
7604 proc getblobline {bf id} {
7605 global diffids cmitmode ctext
7607 if {$id ne $diffids || $cmitmode ne "tree"} {
7608 catch {close $bf}
7609 return 0
7611 $ctext config -state normal
7612 set nl 0
7613 while {[incr nl] <= 1000 && [gets $bf line] >= 0} {
7614 $ctext insert end "$line\n"
7616 if {[eof $bf]} {
7617 global jump_to_here ctext_file_names commentend
7619 # delete last newline
7620 $ctext delete "end - 2c" "end - 1c"
7621 close $bf
7622 if {$jump_to_here ne {} &&
7623 [lindex $jump_to_here 0] eq [lindex $ctext_file_names 0]} {
7624 set lnum [expr {[lindex $jump_to_here 1] +
7625 [lindex [split $commentend .] 0]}]
7626 mark_ctext_line $lnum
7628 $ctext config -state disabled
7629 return 0
7631 $ctext config -state disabled
7632 return [expr {$nl >= 1000? 2: 1}]
7635 proc mark_ctext_line {lnum} {
7636 global ctext markbgcolor
7638 $ctext tag delete omark
7639 $ctext tag add omark $lnum.0 "$lnum.0 + 1 line"
7640 $ctext tag conf omark -background $markbgcolor
7641 $ctext see $lnum.0
7644 proc mergediff {id} {
7645 global diffmergeid
7646 global diffids treediffs
7647 global parents curview
7649 set diffmergeid $id
7650 set diffids $id
7651 set treediffs($id) {}
7652 set np [llength $parents($curview,$id)]
7653 settabs $np
7654 getblobdiffs $id
7657 proc startdiff {ids} {
7658 global treediffs diffids treepending diffmergeid nullid nullid2
7660 settabs 1
7661 set diffids $ids
7662 catch {unset diffmergeid}
7663 if {![info exists treediffs($ids)] ||
7664 [lsearch -exact $ids $nullid] >= 0 ||
7665 [lsearch -exact $ids $nullid2] >= 0} {
7666 if {![info exists treepending]} {
7667 gettreediffs $ids
7669 } else {
7670 addtocflist $ids
7674 proc showinlinediff {ids} {
7675 global commitinfo commitdata ctext
7676 global treediffs
7678 set info $commitinfo($ids)
7679 set diff [lindex $info 7]
7680 set difflines [split $diff "\n"]
7682 initblobdiffvars
7683 set treediff {}
7685 set inhdr 0
7686 foreach line $difflines {
7687 if {![string compare -length 5 "diff " $line]} {
7688 set inhdr 1
7689 } elseif {$inhdr && ![string compare -length 4 "+++ " $line]} {
7690 # offset also accounts for the b/ prefix
7691 lappend treediff [string range $line 6 end]
7692 set inhdr 0
7696 set treediffs($ids) $treediff
7697 add_flist $treediff
7699 $ctext conf -state normal
7700 foreach line $difflines {
7701 parseblobdiffline $ids $line
7703 maybe_scroll_ctext 1
7704 $ctext conf -state disabled
7707 # If the filename (name) is under any of the passed filter paths
7708 # then return true to include the file in the listing.
7709 proc path_filter {filter name} {
7710 set worktree [gitworktree]
7711 foreach p $filter {
7712 set fq_p [file normalize $p]
7713 set fq_n [file normalize [file join $worktree $name]]
7714 if {[string match [file normalize $fq_p]* $fq_n]} {
7715 return 1
7718 return 0
7721 proc addtocflist {ids} {
7722 global treediffs
7724 add_flist $treediffs($ids)
7725 getblobdiffs $ids
7728 proc diffcmd {ids flags} {
7729 global log_showroot nullid nullid2 git_version
7731 set i [lsearch -exact $ids $nullid]
7732 set j [lsearch -exact $ids $nullid2]
7733 if {$i >= 0} {
7734 if {[llength $ids] > 1 && $j < 0} {
7735 # comparing working directory with some specific revision
7736 set cmd [concat | git diff-index $flags]
7737 if {$i == 0} {
7738 lappend cmd -R [lindex $ids 1]
7739 } else {
7740 lappend cmd [lindex $ids 0]
7742 } else {
7743 # comparing working directory with index
7744 set cmd [concat | git diff-files $flags]
7745 if {$j == 1} {
7746 lappend cmd -R
7749 } elseif {$j >= 0} {
7750 if {[package vcompare $git_version "1.7.2"] >= 0} {
7751 set flags "$flags --ignore-submodules=dirty"
7753 set cmd [concat | git diff-index --cached $flags]
7754 if {[llength $ids] > 1} {
7755 # comparing index with specific revision
7756 if {$j == 0} {
7757 lappend cmd -R [lindex $ids 1]
7758 } else {
7759 lappend cmd [lindex $ids 0]
7761 } else {
7762 # comparing index with HEAD
7763 lappend cmd HEAD
7765 } else {
7766 if {$log_showroot} {
7767 lappend flags --root
7769 set cmd [concat | git diff-tree -r $flags $ids]
7771 return $cmd
7774 proc gettreediffs {ids} {
7775 global treediff treepending limitdiffs vfilelimit curview
7777 set cmd [diffcmd $ids {--no-commit-id}]
7778 if {$limitdiffs && $vfilelimit($curview) ne {}} {
7779 set cmd [concat $cmd -- $vfilelimit($curview)]
7781 if {[catch {set gdtf [open $cmd r]}]} return
7783 set treepending $ids
7784 set treediff {}
7785 fconfigure $gdtf -blocking 0 -encoding binary
7786 filerun $gdtf [list gettreediffline $gdtf $ids]
7789 proc gettreediffline {gdtf ids} {
7790 global treediff treediffs treepending diffids diffmergeid
7791 global cmitmode vfilelimit curview limitdiffs perfile_attrs
7793 set nr 0
7794 set sublist {}
7795 set max 1000
7796 if {$perfile_attrs} {
7797 # cache_gitattr is slow, and even slower on win32 where we
7798 # have to invoke it for only about 30 paths at a time
7799 set max 500
7800 if {[tk windowingsystem] == "win32"} {
7801 set max 120
7804 while {[incr nr] <= $max && [gets $gdtf line] >= 0} {
7805 set i [string first "\t" $line]
7806 if {$i >= 0} {
7807 set file [string range $line [expr {$i+1}] end]
7808 if {[string index $file 0] eq "\""} {
7809 set file [lindex $file 0]
7811 set file [encoding convertfrom $file]
7812 if {$file ne [lindex $treediff end]} {
7813 lappend treediff $file
7814 lappend sublist $file
7818 if {$perfile_attrs} {
7819 cache_gitattr encoding $sublist
7821 if {![eof $gdtf]} {
7822 return [expr {$nr >= $max? 2: 1}]
7824 close $gdtf
7825 set treediffs($ids) $treediff
7826 unset treepending
7827 if {$cmitmode eq "tree" && [llength $diffids] == 1} {
7828 gettree $diffids
7829 } elseif {$ids != $diffids} {
7830 if {![info exists diffmergeid]} {
7831 gettreediffs $diffids
7833 } else {
7834 addtocflist $ids
7836 return 0
7839 # empty string or positive integer
7840 proc diffcontextvalidate {v} {
7841 return [regexp {^(|[1-9][0-9]*)$} $v]
7844 proc diffcontextchange {n1 n2 op} {
7845 global diffcontextstring diffcontext
7847 if {[string is integer -strict $diffcontextstring]} {
7848 if {$diffcontextstring >= 0} {
7849 set diffcontext $diffcontextstring
7850 reselectline
7855 proc changeignorespace {} {
7856 reselectline
7859 proc changeworddiff {name ix op} {
7860 reselectline
7863 proc initblobdiffvars {} {
7864 global diffencoding targetline diffnparents
7865 global diffinhdr currdiffsubmod diffseehere
7866 set targetline {}
7867 set diffnparents 0
7868 set diffinhdr 0
7869 set diffencoding [get_path_encoding {}]
7870 set currdiffsubmod ""
7871 set diffseehere -1
7874 proc getblobdiffs {ids} {
7875 global blobdifffd diffids env
7876 global treediffs
7877 global diffcontext
7878 global ignorespace
7879 global worddiff
7880 global limitdiffs vfilelimit curview
7881 global git_version
7883 set textconv {}
7884 if {[package vcompare $git_version "1.6.1"] >= 0} {
7885 set textconv "--textconv"
7887 set submodule {}
7888 if {[package vcompare $git_version "1.6.6"] >= 0} {
7889 set submodule "--submodule"
7891 set cmd [diffcmd $ids "-p $textconv $submodule -C --cc --no-commit-id -U$diffcontext"]
7892 if {$ignorespace} {
7893 append cmd " -w"
7895 if {$worddiff ne [mc "Line diff"]} {
7896 append cmd " --word-diff=porcelain"
7898 if {$limitdiffs && $vfilelimit($curview) ne {}} {
7899 set cmd [concat $cmd -- $vfilelimit($curview)]
7901 if {[catch {set bdf [open $cmd r]} err]} {
7902 error_popup [mc "Error getting diffs: %s" $err]
7903 return
7905 fconfigure $bdf -blocking 0 -encoding binary -eofchar {}
7906 set blobdifffd($ids) $bdf
7907 initblobdiffvars
7908 filerun $bdf [list getblobdiffline $bdf $diffids]
7911 proc savecmitpos {} {
7912 global ctext cmitmode
7914 if {$cmitmode eq "tree"} {
7915 return {}
7917 return [list target_scrollpos [$ctext index @0,0]]
7920 proc savectextpos {} {
7921 global ctext
7923 return [list target_scrollpos [$ctext index @0,0]]
7926 proc maybe_scroll_ctext {ateof} {
7927 global ctext target_scrollpos
7929 if {![info exists target_scrollpos]} return
7930 if {!$ateof} {
7931 set nlines [expr {[winfo height $ctext]
7932 / [font metrics textfont -linespace]}]
7933 if {[$ctext compare "$target_scrollpos + $nlines lines" <= end]} return
7935 $ctext yview $target_scrollpos
7936 unset target_scrollpos
7939 proc setinlist {var i val} {
7940 global $var
7942 while {[llength [set $var]] < $i} {
7943 lappend $var {}
7945 if {[llength [set $var]] == $i} {
7946 lappend $var $val
7947 } else {
7948 lset $var $i $val
7952 proc makediffhdr {fname ids} {
7953 global ctext curdiffstart treediffs diffencoding
7954 global ctext_file_names jump_to_here targetline diffline
7956 set fname [encoding convertfrom $fname]
7957 set diffencoding [get_path_encoding $fname]
7958 set i [lsearch -exact $treediffs($ids) $fname]
7959 if {$i >= 0} {
7960 setinlist difffilestart $i $curdiffstart
7962 lset ctext_file_names end $fname
7963 set l [expr {(78 - [string length $fname]) / 2}]
7964 set pad [string range "----------------------------------------" 1 $l]
7965 $ctext insert $curdiffstart "$pad $fname $pad" filesep
7966 set targetline {}
7967 if {$jump_to_here ne {} && [lindex $jump_to_here 0] eq $fname} {
7968 set targetline [lindex $jump_to_here 1]
7970 set diffline 0
7973 proc blobdiffmaybeseehere {ateof} {
7974 global diffseehere
7975 if {$diffseehere >= 0} {
7976 mark_ctext_line [lindex [split $diffseehere .] 0]
7978 maybe_scroll_ctext $ateof
7981 proc getblobdiffline {bdf ids} {
7982 global diffids blobdifffd
7983 global ctext
7985 set nr 0
7986 $ctext conf -state normal
7987 while {[incr nr] <= 1000 && [gets $bdf line] >= 0} {
7988 if {$ids != $diffids || $bdf != $blobdifffd($ids)} {
7989 catch {close $bdf}
7990 return 0
7992 parseblobdiffline $ids $line
7994 $ctext conf -state disabled
7995 blobdiffmaybeseehere [eof $bdf]
7996 if {[eof $bdf]} {
7997 catch {close $bdf}
7998 return 0
8000 return [expr {$nr >= 1000? 2: 1}]
8003 proc parseblobdiffline {ids line} {
8004 global ctext curdiffstart
8005 global diffnexthead diffnextnote difffilestart
8006 global ctext_file_names ctext_file_lines
8007 global diffinhdr treediffs mergemax diffnparents
8008 global diffencoding jump_to_here targetline diffline currdiffsubmod
8009 global worddiff diffseehere
8011 if {![string compare -length 5 "diff " $line]} {
8012 if {![regexp {^diff (--cc|--git) } $line m type]} {
8013 set line [encoding convertfrom $line]
8014 $ctext insert end "$line\n" hunksep
8015 continue
8017 # start of a new file
8018 set diffinhdr 1
8019 $ctext insert end "\n"
8020 set curdiffstart [$ctext index "end - 1c"]
8021 lappend ctext_file_names ""
8022 lappend ctext_file_lines [lindex [split $curdiffstart "."] 0]
8023 $ctext insert end "\n" filesep
8025 if {$type eq "--cc"} {
8026 # start of a new file in a merge diff
8027 set fname [string range $line 10 end]
8028 if {[lsearch -exact $treediffs($ids) $fname] < 0} {
8029 lappend treediffs($ids) $fname
8030 add_flist [list $fname]
8033 } else {
8034 set line [string range $line 11 end]
8035 # If the name hasn't changed the length will be odd,
8036 # the middle char will be a space, and the two bits either
8037 # side will be a/name and b/name, or "a/name" and "b/name".
8038 # If the name has changed we'll get "rename from" and
8039 # "rename to" or "copy from" and "copy to" lines following
8040 # this, and we'll use them to get the filenames.
8041 # This complexity is necessary because spaces in the
8042 # filename(s) don't get escaped.
8043 set l [string length $line]
8044 set i [expr {$l / 2}]
8045 if {!(($l & 1) && [string index $line $i] eq " " &&
8046 [string range $line 2 [expr {$i - 1}]] eq \
8047 [string range $line [expr {$i + 3}] end])} {
8048 return
8050 # unescape if quoted and chop off the a/ from the front
8051 if {[string index $line 0] eq "\""} {
8052 set fname [string range [lindex $line 0] 2 end]
8053 } else {
8054 set fname [string range $line 2 [expr {$i - 1}]]
8057 makediffhdr $fname $ids
8059 } elseif {![string compare -length 16 "* Unmerged path " $line]} {
8060 set fname [encoding convertfrom [string range $line 16 end]]
8061 $ctext insert end "\n"
8062 set curdiffstart [$ctext index "end - 1c"]
8063 lappend ctext_file_names $fname
8064 lappend ctext_file_lines [lindex [split $curdiffstart "."] 0]
8065 $ctext insert end "$line\n" filesep
8066 set i [lsearch -exact $treediffs($ids) $fname]
8067 if {$i >= 0} {
8068 setinlist difffilestart $i $curdiffstart
8071 } elseif {![string compare -length 2 "@@" $line]} {
8072 regexp {^@@+} $line ats
8073 set line [encoding convertfrom $diffencoding $line]
8074 $ctext insert end "$line\n" hunksep
8075 if {[regexp { \+(\d+),\d+ @@} $line m nl]} {
8076 set diffline $nl
8078 set diffnparents [expr {[string length $ats] - 1}]
8079 set diffinhdr 0
8081 } elseif {![string compare -length 10 "Submodule " $line]} {
8082 # start of a new submodule
8083 if {[regexp -indices "\[0-9a-f\]+\\.\\." $line nameend]} {
8084 set fname [string range $line 10 [expr [lindex $nameend 0] - 2]]
8085 } else {
8086 set fname [string range $line 10 [expr [string first "contains " $line] - 2]]
8088 if {$currdiffsubmod != $fname} {
8089 $ctext insert end "\n"; # Add newline after commit message
8091 set curdiffstart [$ctext index "end - 1c"]
8092 lappend ctext_file_names ""
8093 if {$currdiffsubmod != $fname} {
8094 lappend ctext_file_lines $fname
8095 makediffhdr $fname $ids
8096 set currdiffsubmod $fname
8097 $ctext insert end "\n$line\n" filesep
8098 } else {
8099 $ctext insert end "$line\n" filesep
8101 } elseif {![string compare -length 3 " >" $line]} {
8102 set $currdiffsubmod ""
8103 set line [encoding convertfrom $diffencoding $line]
8104 $ctext insert end "$line\n" dresult
8105 } elseif {![string compare -length 3 " <" $line]} {
8106 set $currdiffsubmod ""
8107 set line [encoding convertfrom $diffencoding $line]
8108 $ctext insert end "$line\n" d0
8109 } elseif {$diffinhdr} {
8110 if {![string compare -length 12 "rename from " $line]} {
8111 set fname [string range $line [expr 6 + [string first " from " $line] ] end]
8112 if {[string index $fname 0] eq "\""} {
8113 set fname [lindex $fname 0]
8115 set fname [encoding convertfrom $fname]
8116 set i [lsearch -exact $treediffs($ids) $fname]
8117 if {$i >= 0} {
8118 setinlist difffilestart $i $curdiffstart
8120 } elseif {![string compare -length 10 $line "rename to "] ||
8121 ![string compare -length 8 $line "copy to "]} {
8122 set fname [string range $line [expr 4 + [string first " to " $line] ] end]
8123 if {[string index $fname 0] eq "\""} {
8124 set fname [lindex $fname 0]
8126 makediffhdr $fname $ids
8127 } elseif {[string compare -length 3 $line "---"] == 0} {
8128 # do nothing
8129 return
8130 } elseif {[string compare -length 3 $line "+++"] == 0} {
8131 set diffinhdr 0
8132 return
8134 $ctext insert end "$line\n" filesep
8136 } else {
8137 set line [string map {\x1A ^Z} \
8138 [encoding convertfrom $diffencoding $line]]
8139 # parse the prefix - one ' ', '-' or '+' for each parent
8140 set prefix [string range $line 0 [expr {$diffnparents - 1}]]
8141 set tag [expr {$diffnparents > 1? "m": "d"}]
8142 set dowords [expr {$worddiff ne [mc "Line diff"] && $diffnparents == 1}]
8143 set words_pre_markup ""
8144 set words_post_markup ""
8145 if {[string trim $prefix " -+"] eq {}} {
8146 # prefix only has " ", "-" and "+" in it: normal diff line
8147 set num [string first "-" $prefix]
8148 if {$dowords} {
8149 set line [string range $line 1 end]
8151 if {$num >= 0} {
8152 # removed line, first parent with line is $num
8153 if {$num >= $mergemax} {
8154 set num "max"
8156 if {$dowords && $worddiff eq [mc "Markup words"]} {
8157 $ctext insert end "\[-$line-\]" $tag$num
8158 } else {
8159 $ctext insert end "$line" $tag$num
8161 if {!$dowords} {
8162 $ctext insert end "\n" $tag$num
8164 } else {
8165 set tags {}
8166 if {[string first "+" $prefix] >= 0} {
8167 # added line
8168 lappend tags ${tag}result
8169 if {$diffnparents > 1} {
8170 set num [string first " " $prefix]
8171 if {$num >= 0} {
8172 if {$num >= $mergemax} {
8173 set num "max"
8175 lappend tags m$num
8178 set words_pre_markup "{+"
8179 set words_post_markup "+}"
8181 if {$targetline ne {}} {
8182 if {$diffline == $targetline} {
8183 set diffseehere [$ctext index "end - 1 chars"]
8184 set targetline {}
8185 } else {
8186 incr diffline
8189 if {$dowords && $worddiff eq [mc "Markup words"]} {
8190 $ctext insert end "$words_pre_markup$line$words_post_markup" $tags
8191 } else {
8192 $ctext insert end "$line" $tags
8194 if {!$dowords} {
8195 $ctext insert end "\n" $tags
8198 } elseif {$dowords && $prefix eq "~"} {
8199 $ctext insert end "\n" {}
8200 } else {
8201 # "\ No newline at end of file",
8202 # or something else we don't recognize
8203 $ctext insert end "$line\n" hunksep
8208 proc changediffdisp {} {
8209 global ctext diffelide
8211 $ctext tag conf d0 -elide [lindex $diffelide 0]
8212 $ctext tag conf dresult -elide [lindex $diffelide 1]
8215 proc highlightfile {cline} {
8216 global cflist cflist_top
8218 if {![info exists cflist_top]} return
8220 $cflist tag remove highlight $cflist_top.0 "$cflist_top.0 lineend"
8221 $cflist tag add highlight $cline.0 "$cline.0 lineend"
8222 $cflist see $cline.0
8223 set cflist_top $cline
8226 proc highlightfile_for_scrollpos {topidx} {
8227 global cmitmode difffilestart
8229 if {$cmitmode eq "tree"} return
8230 if {![info exists difffilestart]} return
8232 set top [lindex [split $topidx .] 0]
8233 if {$difffilestart eq {} || $top < [lindex $difffilestart 0]} {
8234 highlightfile 0
8235 } else {
8236 highlightfile [expr {[bsearch $difffilestart $top] + 2}]
8240 proc prevfile {} {
8241 global difffilestart ctext cmitmode
8243 if {$cmitmode eq "tree"} return
8244 set prev 0.0
8245 set here [$ctext index @0,0]
8246 foreach loc $difffilestart {
8247 if {[$ctext compare $loc >= $here]} {
8248 $ctext yview $prev
8249 return
8251 set prev $loc
8253 $ctext yview $prev
8256 proc nextfile {} {
8257 global difffilestart ctext cmitmode
8259 if {$cmitmode eq "tree"} return
8260 set here [$ctext index @0,0]
8261 foreach loc $difffilestart {
8262 if {[$ctext compare $loc > $here]} {
8263 $ctext yview $loc
8264 return
8269 proc clear_ctext {{first 1.0}} {
8270 global ctext smarktop smarkbot
8271 global ctext_file_names ctext_file_lines
8272 global pendinglinks
8274 set l [lindex [split $first .] 0]
8275 if {![info exists smarktop] || [$ctext compare $first < $smarktop.0]} {
8276 set smarktop $l
8278 if {![info exists smarkbot] || [$ctext compare $first < $smarkbot.0]} {
8279 set smarkbot $l
8281 $ctext delete $first end
8282 if {$first eq "1.0"} {
8283 catch {unset pendinglinks}
8285 set ctext_file_names {}
8286 set ctext_file_lines {}
8289 proc settabs {{firstab {}}} {
8290 global firsttabstop tabstop ctext have_tk85
8292 if {$firstab ne {} && $have_tk85} {
8293 set firsttabstop $firstab
8295 set w [font measure textfont "0"]
8296 if {$firsttabstop != 0} {
8297 $ctext conf -tabs [list [expr {($firsttabstop + $tabstop) * $w}] \
8298 [expr {($firsttabstop + 2 * $tabstop) * $w}]]
8299 } elseif {$have_tk85 || $tabstop != 8} {
8300 $ctext conf -tabs [expr {$tabstop * $w}]
8301 } else {
8302 $ctext conf -tabs {}
8306 proc incrsearch {name ix op} {
8307 global ctext searchstring searchdirn
8309 if {[catch {$ctext index anchor}]} {
8310 # no anchor set, use start of selection, or of visible area
8311 set sel [$ctext tag ranges sel]
8312 if {$sel ne {}} {
8313 $ctext mark set anchor [lindex $sel 0]
8314 } elseif {$searchdirn eq "-forwards"} {
8315 $ctext mark set anchor @0,0
8316 } else {
8317 $ctext mark set anchor @0,[winfo height $ctext]
8320 if {$searchstring ne {}} {
8321 set here [$ctext search -count mlen $searchdirn -- $searchstring anchor]
8322 if {$here ne {}} {
8323 $ctext see $here
8324 set mend "$here + $mlen c"
8325 $ctext tag remove sel 1.0 end
8326 $ctext tag add sel $here $mend
8327 suppress_highlighting_file_for_current_scrollpos
8328 highlightfile_for_scrollpos $here
8331 rehighlight_search_results
8334 proc dosearch {} {
8335 global sstring ctext searchstring searchdirn
8337 focus $sstring
8338 $sstring icursor end
8339 set searchdirn -forwards
8340 if {$searchstring ne {}} {
8341 set sel [$ctext tag ranges sel]
8342 if {$sel ne {}} {
8343 set start "[lindex $sel 0] + 1c"
8344 } elseif {[catch {set start [$ctext index anchor]}]} {
8345 set start "@0,0"
8347 set match [$ctext search -count mlen -- $searchstring $start]
8348 $ctext tag remove sel 1.0 end
8349 if {$match eq {}} {
8350 bell
8351 return
8353 $ctext see $match
8354 suppress_highlighting_file_for_current_scrollpos
8355 highlightfile_for_scrollpos $match
8356 set mend "$match + $mlen c"
8357 $ctext tag add sel $match $mend
8358 $ctext mark unset anchor
8359 rehighlight_search_results
8363 proc dosearchback {} {
8364 global sstring ctext searchstring searchdirn
8366 focus $sstring
8367 $sstring icursor end
8368 set searchdirn -backwards
8369 if {$searchstring ne {}} {
8370 set sel [$ctext tag ranges sel]
8371 if {$sel ne {}} {
8372 set start [lindex $sel 0]
8373 } elseif {[catch {set start [$ctext index anchor]}]} {
8374 set start @0,[winfo height $ctext]
8376 set match [$ctext search -backwards -count ml -- $searchstring $start]
8377 $ctext tag remove sel 1.0 end
8378 if {$match eq {}} {
8379 bell
8380 return
8382 $ctext see $match
8383 suppress_highlighting_file_for_current_scrollpos
8384 highlightfile_for_scrollpos $match
8385 set mend "$match + $ml c"
8386 $ctext tag add sel $match $mend
8387 $ctext mark unset anchor
8388 rehighlight_search_results
8392 proc rehighlight_search_results {} {
8393 global ctext searchstring
8395 $ctext tag remove found 1.0 end
8396 $ctext tag remove currentsearchhit 1.0 end
8398 if {$searchstring ne {}} {
8399 searchmarkvisible 1
8403 proc searchmark {first last} {
8404 global ctext searchstring
8406 set sel [$ctext tag ranges sel]
8408 set mend $first.0
8409 while {1} {
8410 set match [$ctext search -count mlen -- $searchstring $mend $last.end]
8411 if {$match eq {}} break
8412 set mend "$match + $mlen c"
8413 if {$sel ne {} && [$ctext compare $match == [lindex $sel 0]]} {
8414 $ctext tag add currentsearchhit $match $mend
8415 } else {
8416 $ctext tag add found $match $mend
8421 proc searchmarkvisible {doall} {
8422 global ctext smarktop smarkbot
8424 set topline [lindex [split [$ctext index @0,0] .] 0]
8425 set botline [lindex [split [$ctext index @0,[winfo height $ctext]] .] 0]
8426 if {$doall || $botline < $smarktop || $topline > $smarkbot} {
8427 # no overlap with previous
8428 searchmark $topline $botline
8429 set smarktop $topline
8430 set smarkbot $botline
8431 } else {
8432 if {$topline < $smarktop} {
8433 searchmark $topline [expr {$smarktop-1}]
8434 set smarktop $topline
8436 if {$botline > $smarkbot} {
8437 searchmark [expr {$smarkbot+1}] $botline
8438 set smarkbot $botline
8443 proc suppress_highlighting_file_for_current_scrollpos {} {
8444 global ctext suppress_highlighting_file_for_this_scrollpos
8446 set suppress_highlighting_file_for_this_scrollpos [$ctext index @0,0]
8449 proc scrolltext {f0 f1} {
8450 global searchstring cmitmode ctext
8451 global suppress_highlighting_file_for_this_scrollpos
8453 set topidx [$ctext index @0,0]
8454 if {![info exists suppress_highlighting_file_for_this_scrollpos]
8455 || $topidx ne $suppress_highlighting_file_for_this_scrollpos} {
8456 highlightfile_for_scrollpos $topidx
8459 catch {unset suppress_highlighting_file_for_this_scrollpos}
8461 .bleft.bottom.sb set $f0 $f1
8462 if {$searchstring ne {}} {
8463 searchmarkvisible 0
8467 proc setcoords {} {
8468 global linespc charspc canvx0 canvy0
8469 global xspc1 xspc2 lthickness
8471 set linespc [font metrics mainfont -linespace]
8472 set charspc [font measure mainfont "m"]
8473 set canvy0 [expr {int(3 + 0.5 * $linespc)}]
8474 set canvx0 [expr {int(3 + 0.5 * $linespc)}]
8475 set lthickness [expr {int($linespc / 9) + 1}]
8476 set xspc1(0) $linespc
8477 set xspc2 $linespc
8480 proc redisplay {} {
8481 global canv
8482 global selectedline
8484 set ymax [lindex [$canv cget -scrollregion] 3]
8485 if {$ymax eq {} || $ymax == 0} return
8486 set span [$canv yview]
8487 clear_display
8488 setcanvscroll
8489 allcanvs yview moveto [lindex $span 0]
8490 drawvisible
8491 if {$selectedline ne {}} {
8492 selectline $selectedline 0
8493 allcanvs yview moveto [lindex $span 0]
8497 proc parsefont {f n} {
8498 global fontattr
8500 set fontattr($f,family) [lindex $n 0]
8501 set s [lindex $n 1]
8502 if {$s eq {} || $s == 0} {
8503 set s 10
8504 } elseif {$s < 0} {
8505 set s [expr {int(-$s / [winfo fpixels . 1p] + 0.5)}]
8507 set fontattr($f,size) $s
8508 set fontattr($f,weight) normal
8509 set fontattr($f,slant) roman
8510 foreach style [lrange $n 2 end] {
8511 switch -- $style {
8512 "normal" -
8513 "bold" {set fontattr($f,weight) $style}
8514 "roman" -
8515 "italic" {set fontattr($f,slant) $style}
8520 proc fontflags {f {isbold 0}} {
8521 global fontattr
8523 return [list -family $fontattr($f,family) -size $fontattr($f,size) \
8524 -weight [expr {$isbold? "bold": $fontattr($f,weight)}] \
8525 -slant $fontattr($f,slant)]
8528 proc fontname {f} {
8529 global fontattr
8531 set n [list $fontattr($f,family) $fontattr($f,size)]
8532 if {$fontattr($f,weight) eq "bold"} {
8533 lappend n "bold"
8535 if {$fontattr($f,slant) eq "italic"} {
8536 lappend n "italic"
8538 return $n
8541 proc incrfont {inc} {
8542 global mainfont textfont ctext canv cflist showrefstop
8543 global stopped entries fontattr
8545 unmarkmatches
8546 set s $fontattr(mainfont,size)
8547 incr s $inc
8548 if {$s < 1} {
8549 set s 1
8551 set fontattr(mainfont,size) $s
8552 font config mainfont -size $s
8553 font config mainfontbold -size $s
8554 set mainfont [fontname mainfont]
8555 set s $fontattr(textfont,size)
8556 incr s $inc
8557 if {$s < 1} {
8558 set s 1
8560 set fontattr(textfont,size) $s
8561 font config textfont -size $s
8562 font config textfontbold -size $s
8563 set textfont [fontname textfont]
8564 setcoords
8565 settabs
8566 redisplay
8569 proc clearsha1 {} {
8570 global sha1entry sha1string
8571 if {[string length $sha1string] == 40} {
8572 $sha1entry delete 0 end
8576 proc sha1change {n1 n2 op} {
8577 global sha1string currentid sha1but
8578 if {$sha1string == {}
8579 || ([info exists currentid] && $sha1string == $currentid)} {
8580 set state disabled
8581 } else {
8582 set state normal
8584 if {[$sha1but cget -state] == $state} return
8585 if {$state == "normal"} {
8586 $sha1but conf -state normal -relief raised -text "[mc "Goto:"] "
8587 } else {
8588 $sha1but conf -state disabled -relief flat -text "[mc "SHA1 ID:"] "
8592 proc gotocommit {} {
8593 global sha1string tagids headids curview varcid
8595 if {$sha1string == {}
8596 || ([info exists currentid] && $sha1string == $currentid)} return
8597 if {[info exists tagids($sha1string)]} {
8598 set id $tagids($sha1string)
8599 } elseif {[info exists headids($sha1string)]} {
8600 set id $headids($sha1string)
8601 } else {
8602 set id [string tolower $sha1string]
8603 if {[regexp {^[0-9a-f]{4,39}$} $id]} {
8604 set matches [longid $id]
8605 if {$matches ne {}} {
8606 if {[llength $matches] > 1} {
8607 error_popup [mc "Short SHA1 id %s is ambiguous" $id]
8608 return
8610 set id [lindex $matches 0]
8612 } else {
8613 if {[catch {set id [exec git rev-parse --verify $sha1string]}]} {
8614 error_popup [mc "Revision %s is not known" $sha1string]
8615 return
8619 if {[commitinview $id $curview]} {
8620 selectline [rowofcommit $id] 1
8621 return
8623 if {[regexp {^[0-9a-fA-F]{4,}$} $sha1string]} {
8624 set msg [mc "SHA1 id %s is not known" $sha1string]
8625 } else {
8626 set msg [mc "Revision %s is not in the current view" $sha1string]
8628 error_popup $msg
8631 proc lineenter {x y id} {
8632 global hoverx hovery hoverid hovertimer
8633 global commitinfo canv
8635 if {![info exists commitinfo($id)] && ![getcommit $id]} return
8636 set hoverx $x
8637 set hovery $y
8638 set hoverid $id
8639 if {[info exists hovertimer]} {
8640 after cancel $hovertimer
8642 set hovertimer [after 500 linehover]
8643 $canv delete hover
8646 proc linemotion {x y id} {
8647 global hoverx hovery hoverid hovertimer
8649 if {[info exists hoverid] && $id == $hoverid} {
8650 set hoverx $x
8651 set hovery $y
8652 if {[info exists hovertimer]} {
8653 after cancel $hovertimer
8655 set hovertimer [after 500 linehover]
8659 proc lineleave {id} {
8660 global hoverid hovertimer canv
8662 if {[info exists hoverid] && $id == $hoverid} {
8663 $canv delete hover
8664 if {[info exists hovertimer]} {
8665 after cancel $hovertimer
8666 unset hovertimer
8668 unset hoverid
8672 proc linehover {} {
8673 global hoverx hovery hoverid hovertimer
8674 global canv linespc lthickness
8675 global linehoverbgcolor linehoverfgcolor linehoveroutlinecolor
8677 global commitinfo
8679 set text [lindex $commitinfo($hoverid) 0]
8680 set ymax [lindex [$canv cget -scrollregion] 3]
8681 if {$ymax == {}} return
8682 set yfrac [lindex [$canv yview] 0]
8683 set x [expr {$hoverx + 2 * $linespc}]
8684 set y [expr {$hovery + $yfrac * $ymax - $linespc / 2}]
8685 set x0 [expr {$x - 2 * $lthickness}]
8686 set y0 [expr {$y - 2 * $lthickness}]
8687 set x1 [expr {$x + [font measure mainfont $text] + 2 * $lthickness}]
8688 set y1 [expr {$y + $linespc + 2 * $lthickness}]
8689 set t [$canv create rectangle $x0 $y0 $x1 $y1 \
8690 -fill $linehoverbgcolor -outline $linehoveroutlinecolor \
8691 -width 1 -tags hover]
8692 $canv raise $t
8693 set t [$canv create text $x $y -anchor nw -text $text -tags hover \
8694 -font mainfont -fill $linehoverfgcolor]
8695 $canv raise $t
8698 proc clickisonarrow {id y} {
8699 global lthickness
8701 set ranges [rowranges $id]
8702 set thresh [expr {2 * $lthickness + 6}]
8703 set n [expr {[llength $ranges] - 1}]
8704 for {set i 1} {$i < $n} {incr i} {
8705 set row [lindex $ranges $i]
8706 if {abs([yc $row] - $y) < $thresh} {
8707 return $i
8710 return {}
8713 proc arrowjump {id n y} {
8714 global canv
8716 # 1 <-> 2, 3 <-> 4, etc...
8717 set n [expr {(($n - 1) ^ 1) + 1}]
8718 set row [lindex [rowranges $id] $n]
8719 set yt [yc $row]
8720 set ymax [lindex [$canv cget -scrollregion] 3]
8721 if {$ymax eq {} || $ymax <= 0} return
8722 set view [$canv yview]
8723 set yspan [expr {[lindex $view 1] - [lindex $view 0]}]
8724 set yfrac [expr {$yt / $ymax - $yspan / 2}]
8725 if {$yfrac < 0} {
8726 set yfrac 0
8728 allcanvs yview moveto $yfrac
8731 proc lineclick {x y id isnew} {
8732 global ctext commitinfo children canv thickerline curview
8734 if {![info exists commitinfo($id)] && ![getcommit $id]} return
8735 unmarkmatches
8736 unselectline
8737 normalline
8738 $canv delete hover
8739 # draw this line thicker than normal
8740 set thickerline $id
8741 drawlines $id
8742 if {$isnew} {
8743 set ymax [lindex [$canv cget -scrollregion] 3]
8744 if {$ymax eq {}} return
8745 set yfrac [lindex [$canv yview] 0]
8746 set y [expr {$y + $yfrac * $ymax}]
8748 set dirn [clickisonarrow $id $y]
8749 if {$dirn ne {}} {
8750 arrowjump $id $dirn $y
8751 return
8754 if {$isnew} {
8755 addtohistory [list lineclick $x $y $id 0] savectextpos
8757 # fill the details pane with info about this line
8758 $ctext conf -state normal
8759 clear_ctext
8760 settabs 0
8761 $ctext insert end "[mc "Parent"]:\t"
8762 $ctext insert end $id link0
8763 setlink $id link0
8764 set info $commitinfo($id)
8765 $ctext insert end "\n\t[lindex $info 0]\n"
8766 $ctext insert end "\t[mc "Author"]:\t[lindex $info 1]\n"
8767 set date [formatdate [lindex $info 2]]
8768 $ctext insert end "\t[mc "Date"]:\t$date\n"
8769 set kids $children($curview,$id)
8770 if {$kids ne {}} {
8771 $ctext insert end "\n[mc "Children"]:"
8772 set i 0
8773 foreach child $kids {
8774 incr i
8775 if {![info exists commitinfo($child)] && ![getcommit $child]} continue
8776 set info $commitinfo($child)
8777 $ctext insert end "\n\t"
8778 $ctext insert end $child link$i
8779 setlink $child link$i
8780 $ctext insert end "\n\t[lindex $info 0]"
8781 $ctext insert end "\n\t[mc "Author"]:\t[lindex $info 1]"
8782 set date [formatdate [lindex $info 2]]
8783 $ctext insert end "\n\t[mc "Date"]:\t$date\n"
8786 maybe_scroll_ctext 1
8787 $ctext conf -state disabled
8788 init_flist {}
8791 proc normalline {} {
8792 global thickerline
8793 if {[info exists thickerline]} {
8794 set id $thickerline
8795 unset thickerline
8796 drawlines $id
8800 proc selbyid {id {isnew 1}} {
8801 global curview
8802 if {[commitinview $id $curview]} {
8803 selectline [rowofcommit $id] $isnew
8807 proc mstime {} {
8808 global startmstime
8809 if {![info exists startmstime]} {
8810 set startmstime [clock clicks -milliseconds]
8812 return [format "%.3f" [expr {([clock click -milliseconds] - $startmstime) / 1000.0}]]
8815 proc rowmenu {x y id} {
8816 global rowctxmenu selectedline rowmenuid curview
8817 global nullid nullid2 fakerowmenu mainhead markedid
8819 stopfinding
8820 set rowmenuid $id
8821 if {$selectedline eq {} || [rowofcommit $id] eq $selectedline} {
8822 set state disabled
8823 } else {
8824 set state normal
8826 if {[info exists markedid] && $markedid ne $id} {
8827 set mstate normal
8828 } else {
8829 set mstate disabled
8831 if {$id ne $nullid && $id ne $nullid2} {
8832 set menu $rowctxmenu
8833 if {$mainhead ne {}} {
8834 $menu entryconfigure 7 -label [mc "Reset %s branch to here" $mainhead] -state normal
8835 } else {
8836 $menu entryconfigure 7 -label [mc "Detached head: can't reset" $mainhead] -state disabled
8838 $menu entryconfigure 9 -state $mstate
8839 $menu entryconfigure 10 -state $mstate
8840 $menu entryconfigure 11 -state $mstate
8841 } else {
8842 set menu $fakerowmenu
8844 $menu entryconfigure [mca "Diff this -> selected"] -state $state
8845 $menu entryconfigure [mca "Diff selected -> this"] -state $state
8846 $menu entryconfigure [mca "Make patch"] -state $state
8847 $menu entryconfigure [mca "Diff this -> marked commit"] -state $mstate
8848 $menu entryconfigure [mca "Diff marked commit -> this"] -state $mstate
8849 tk_popup $menu $x $y
8852 proc markhere {} {
8853 global rowmenuid markedid canv
8855 set markedid $rowmenuid
8856 make_idmark $markedid
8859 proc gotomark {} {
8860 global markedid
8862 if {[info exists markedid]} {
8863 selbyid $markedid
8867 proc replace_by_kids {l r} {
8868 global curview children
8870 set id [commitonrow $r]
8871 set l [lreplace $l 0 0]
8872 foreach kid $children($curview,$id) {
8873 lappend l [rowofcommit $kid]
8875 return [lsort -integer -decreasing -unique $l]
8878 proc find_common_desc {} {
8879 global markedid rowmenuid curview children
8881 if {![info exists markedid]} return
8882 if {![commitinview $markedid $curview] ||
8883 ![commitinview $rowmenuid $curview]} return
8884 #set t1 [clock clicks -milliseconds]
8885 set l1 [list [rowofcommit $markedid]]
8886 set l2 [list [rowofcommit $rowmenuid]]
8887 while 1 {
8888 set r1 [lindex $l1 0]
8889 set r2 [lindex $l2 0]
8890 if {$r1 eq {} || $r2 eq {}} break
8891 if {$r1 == $r2} {
8892 selectline $r1 1
8893 break
8895 if {$r1 > $r2} {
8896 set l1 [replace_by_kids $l1 $r1]
8897 } else {
8898 set l2 [replace_by_kids $l2 $r2]
8901 #set t2 [clock clicks -milliseconds]
8902 #puts "took [expr {$t2-$t1}]ms"
8905 proc compare_commits {} {
8906 global markedid rowmenuid curview children
8908 if {![info exists markedid]} return
8909 if {![commitinview $markedid $curview]} return
8910 addtohistory [list do_cmp_commits $markedid $rowmenuid]
8911 do_cmp_commits $markedid $rowmenuid
8914 proc getpatchid {id} {
8915 global patchids
8917 if {![info exists patchids($id)]} {
8918 set cmd [diffcmd [list $id] {-p --root}]
8919 # trim off the initial "|"
8920 set cmd [lrange $cmd 1 end]
8921 if {[catch {
8922 set x [eval exec $cmd | git patch-id]
8923 set patchids($id) [lindex $x 0]
8924 }]} {
8925 set patchids($id) "error"
8928 return $patchids($id)
8931 proc do_cmp_commits {a b} {
8932 global ctext curview parents children patchids commitinfo
8934 $ctext conf -state normal
8935 clear_ctext
8936 init_flist {}
8937 for {set i 0} {$i < 100} {incr i} {
8938 set skipa 0
8939 set skipb 0
8940 if {[llength $parents($curview,$a)] > 1} {
8941 appendshortlink $a [mc "Skipping merge commit "] "\n"
8942 set skipa 1
8943 } else {
8944 set patcha [getpatchid $a]
8946 if {[llength $parents($curview,$b)] > 1} {
8947 appendshortlink $b [mc "Skipping merge commit "] "\n"
8948 set skipb 1
8949 } else {
8950 set patchb [getpatchid $b]
8952 if {!$skipa && !$skipb} {
8953 set heada [lindex $commitinfo($a) 0]
8954 set headb [lindex $commitinfo($b) 0]
8955 if {$patcha eq "error"} {
8956 appendshortlink $a [mc "Error getting patch ID for "] \
8957 [mc " - stopping\n"]
8958 break
8960 if {$patchb eq "error"} {
8961 appendshortlink $b [mc "Error getting patch ID for "] \
8962 [mc " - stopping\n"]
8963 break
8965 if {$patcha eq $patchb} {
8966 if {$heada eq $headb} {
8967 appendshortlink $a [mc "Commit "]
8968 appendshortlink $b " == " " $heada\n"
8969 } else {
8970 appendshortlink $a [mc "Commit "] " $heada\n"
8971 appendshortlink $b [mc " is the same patch as\n "] \
8972 " $headb\n"
8974 set skipa 1
8975 set skipb 1
8976 } else {
8977 $ctext insert end "\n"
8978 appendshortlink $a [mc "Commit "] " $heada\n"
8979 appendshortlink $b [mc " differs from\n "] \
8980 " $headb\n"
8981 $ctext insert end [mc "Diff of commits:\n\n"]
8982 $ctext conf -state disabled
8983 update
8984 diffcommits $a $b
8985 return
8988 if {$skipa} {
8989 set kids [real_children $curview,$a]
8990 if {[llength $kids] != 1} {
8991 $ctext insert end "\n"
8992 appendshortlink $a [mc "Commit "] \
8993 [mc " has %s children - stopping\n" [llength $kids]]
8994 break
8996 set a [lindex $kids 0]
8998 if {$skipb} {
8999 set kids [real_children $curview,$b]
9000 if {[llength $kids] != 1} {
9001 appendshortlink $b [mc "Commit "] \
9002 [mc " has %s children - stopping\n" [llength $kids]]
9003 break
9005 set b [lindex $kids 0]
9008 $ctext conf -state disabled
9011 proc diffcommits {a b} {
9012 global diffcontext diffids blobdifffd diffinhdr currdiffsubmod
9014 set tmpdir [gitknewtmpdir]
9015 set fna [file join $tmpdir "commit-[string range $a 0 7]"]
9016 set fnb [file join $tmpdir "commit-[string range $b 0 7]"]
9017 if {[catch {
9018 exec git diff-tree -p --pretty $a >$fna
9019 exec git diff-tree -p --pretty $b >$fnb
9020 } err]} {
9021 error_popup [mc "Error writing commit to file: %s" $err]
9022 return
9024 if {[catch {
9025 set fd [open "| diff -U$diffcontext $fna $fnb" r]
9026 } err]} {
9027 error_popup [mc "Error diffing commits: %s" $err]
9028 return
9030 set diffids [list commits $a $b]
9031 set blobdifffd($diffids) $fd
9032 set diffinhdr 0
9033 set currdiffsubmod ""
9034 filerun $fd [list getblobdiffline $fd $diffids]
9037 proc diffvssel {dirn} {
9038 global rowmenuid selectedline
9040 if {$selectedline eq {}} return
9041 if {$dirn} {
9042 set oldid [commitonrow $selectedline]
9043 set newid $rowmenuid
9044 } else {
9045 set oldid $rowmenuid
9046 set newid [commitonrow $selectedline]
9048 addtohistory [list doseldiff $oldid $newid] savectextpos
9049 doseldiff $oldid $newid
9052 proc diffvsmark {dirn} {
9053 global rowmenuid markedid
9055 if {![info exists markedid]} return
9056 if {$dirn} {
9057 set oldid $markedid
9058 set newid $rowmenuid
9059 } else {
9060 set oldid $rowmenuid
9061 set newid $markedid
9063 addtohistory [list doseldiff $oldid $newid] savectextpos
9064 doseldiff $oldid $newid
9067 proc doseldiff {oldid newid} {
9068 global ctext
9069 global commitinfo
9071 $ctext conf -state normal
9072 clear_ctext
9073 init_flist [mc "Top"]
9074 $ctext insert end "[mc "From"] "
9075 $ctext insert end $oldid link0
9076 setlink $oldid link0
9077 $ctext insert end "\n "
9078 $ctext insert end [lindex $commitinfo($oldid) 0]
9079 $ctext insert end "\n\n[mc "To"] "
9080 $ctext insert end $newid link1
9081 setlink $newid link1
9082 $ctext insert end "\n "
9083 $ctext insert end [lindex $commitinfo($newid) 0]
9084 $ctext insert end "\n"
9085 $ctext conf -state disabled
9086 $ctext tag remove found 1.0 end
9087 startdiff [list $oldid $newid]
9090 proc mkpatch {} {
9091 global rowmenuid currentid commitinfo patchtop patchnum NS
9093 if {![info exists currentid]} return
9094 set oldid $currentid
9095 set oldhead [lindex $commitinfo($oldid) 0]
9096 set newid $rowmenuid
9097 set newhead [lindex $commitinfo($newid) 0]
9098 set top .patch
9099 set patchtop $top
9100 catch {destroy $top}
9101 ttk_toplevel $top
9102 make_transient $top .
9103 ${NS}::label $top.title -text [mc "Generate patch"]
9104 grid $top.title - -pady 10
9105 ${NS}::label $top.from -text [mc "From:"]
9106 ${NS}::entry $top.fromsha1 -width 40
9107 $top.fromsha1 insert 0 $oldid
9108 $top.fromsha1 conf -state readonly
9109 grid $top.from $top.fromsha1 -sticky w
9110 ${NS}::entry $top.fromhead -width 60
9111 $top.fromhead insert 0 $oldhead
9112 $top.fromhead conf -state readonly
9113 grid x $top.fromhead -sticky w
9114 ${NS}::label $top.to -text [mc "To:"]
9115 ${NS}::entry $top.tosha1 -width 40
9116 $top.tosha1 insert 0 $newid
9117 $top.tosha1 conf -state readonly
9118 grid $top.to $top.tosha1 -sticky w
9119 ${NS}::entry $top.tohead -width 60
9120 $top.tohead insert 0 $newhead
9121 $top.tohead conf -state readonly
9122 grid x $top.tohead -sticky w
9123 ${NS}::button $top.rev -text [mc "Reverse"] -command mkpatchrev
9124 grid $top.rev x -pady 10 -padx 5
9125 ${NS}::label $top.flab -text [mc "Output file:"]
9126 ${NS}::entry $top.fname -width 60
9127 $top.fname insert 0 [file normalize "patch$patchnum.patch"]
9128 incr patchnum
9129 grid $top.flab $top.fname -sticky w
9130 ${NS}::frame $top.buts
9131 ${NS}::button $top.buts.gen -text [mc "Generate"] -command mkpatchgo
9132 ${NS}::button $top.buts.can -text [mc "Cancel"] -command mkpatchcan
9133 bind $top <Key-Return> mkpatchgo
9134 bind $top <Key-Escape> mkpatchcan
9135 grid $top.buts.gen $top.buts.can
9136 grid columnconfigure $top.buts 0 -weight 1 -uniform a
9137 grid columnconfigure $top.buts 1 -weight 1 -uniform a
9138 grid $top.buts - -pady 10 -sticky ew
9139 focus $top.fname
9142 proc mkpatchrev {} {
9143 global patchtop
9145 set oldid [$patchtop.fromsha1 get]
9146 set oldhead [$patchtop.fromhead get]
9147 set newid [$patchtop.tosha1 get]
9148 set newhead [$patchtop.tohead get]
9149 foreach e [list fromsha1 fromhead tosha1 tohead] \
9150 v [list $newid $newhead $oldid $oldhead] {
9151 $patchtop.$e conf -state normal
9152 $patchtop.$e delete 0 end
9153 $patchtop.$e insert 0 $v
9154 $patchtop.$e conf -state readonly
9158 proc mkpatchgo {} {
9159 global patchtop nullid nullid2
9161 set oldid [$patchtop.fromsha1 get]
9162 set newid [$patchtop.tosha1 get]
9163 set fname [$patchtop.fname get]
9164 set cmd [diffcmd [list $oldid $newid] -p]
9165 # trim off the initial "|"
9166 set cmd [lrange $cmd 1 end]
9167 lappend cmd >$fname &
9168 if {[catch {eval exec $cmd} err]} {
9169 error_popup "[mc "Error creating patch:"] $err" $patchtop
9171 catch {destroy $patchtop}
9172 unset patchtop
9175 proc mkpatchcan {} {
9176 global patchtop
9178 catch {destroy $patchtop}
9179 unset patchtop
9182 proc mktag {} {
9183 global rowmenuid mktagtop commitinfo NS
9185 set top .maketag
9186 set mktagtop $top
9187 catch {destroy $top}
9188 ttk_toplevel $top
9189 make_transient $top .
9190 ${NS}::label $top.title -text [mc "Create tag"]
9191 grid $top.title - -pady 10
9192 ${NS}::label $top.id -text [mc "ID:"]
9193 ${NS}::entry $top.sha1 -width 40
9194 $top.sha1 insert 0 $rowmenuid
9195 $top.sha1 conf -state readonly
9196 grid $top.id $top.sha1 -sticky w
9197 ${NS}::entry $top.head -width 60
9198 $top.head insert 0 [lindex $commitinfo($rowmenuid) 0]
9199 $top.head conf -state readonly
9200 grid x $top.head -sticky w
9201 ${NS}::label $top.tlab -text [mc "Tag name:"]
9202 ${NS}::entry $top.tag -width 60
9203 grid $top.tlab $top.tag -sticky w
9204 ${NS}::label $top.op -text [mc "Tag message is optional"]
9205 grid $top.op -columnspan 2 -sticky we
9206 ${NS}::label $top.mlab -text [mc "Tag message:"]
9207 ${NS}::entry $top.msg -width 60
9208 grid $top.mlab $top.msg -sticky w
9209 ${NS}::frame $top.buts
9210 ${NS}::button $top.buts.gen -text [mc "Create"] -command mktaggo
9211 ${NS}::button $top.buts.can -text [mc "Cancel"] -command mktagcan
9212 bind $top <Key-Return> mktaggo
9213 bind $top <Key-Escape> mktagcan
9214 grid $top.buts.gen $top.buts.can
9215 grid columnconfigure $top.buts 0 -weight 1 -uniform a
9216 grid columnconfigure $top.buts 1 -weight 1 -uniform a
9217 grid $top.buts - -pady 10 -sticky ew
9218 focus $top.tag
9221 proc domktag {} {
9222 global mktagtop env tagids idtags
9224 set id [$mktagtop.sha1 get]
9225 set tag [$mktagtop.tag get]
9226 set msg [$mktagtop.msg get]
9227 if {$tag == {}} {
9228 error_popup [mc "No tag name specified"] $mktagtop
9229 return 0
9231 if {[info exists tagids($tag)]} {
9232 error_popup [mc "Tag \"%s\" already exists" $tag] $mktagtop
9233 return 0
9235 if {[catch {
9236 if {$msg != {}} {
9237 exec git tag -a -m $msg $tag $id
9238 } else {
9239 exec git tag $tag $id
9241 } err]} {
9242 error_popup "[mc "Error creating tag:"] $err" $mktagtop
9243 return 0
9246 set tagids($tag) $id
9247 lappend idtags($id) $tag
9248 redrawtags $id
9249 addedtag $id
9250 dispneartags 0
9251 run refill_reflist
9252 return 1
9255 proc redrawtags {id} {
9256 global canv linehtag idpos currentid curview cmitlisted markedid
9257 global canvxmax iddrawn circleitem mainheadid circlecolors
9258 global mainheadcirclecolor
9260 if {![commitinview $id $curview]} return
9261 if {![info exists iddrawn($id)]} return
9262 set row [rowofcommit $id]
9263 if {$id eq $mainheadid} {
9264 set ofill $mainheadcirclecolor
9265 } else {
9266 set ofill [lindex $circlecolors $cmitlisted($curview,$id)]
9268 $canv itemconf $circleitem($row) -fill $ofill
9269 $canv delete tag.$id
9270 set xt [eval drawtags $id $idpos($id)]
9271 $canv coords $linehtag($id) $xt [lindex $idpos($id) 2]
9272 set text [$canv itemcget $linehtag($id) -text]
9273 set font [$canv itemcget $linehtag($id) -font]
9274 set xr [expr {$xt + [font measure $font $text]}]
9275 if {$xr > $canvxmax} {
9276 set canvxmax $xr
9277 setcanvscroll
9279 if {[info exists currentid] && $currentid == $id} {
9280 make_secsel $id
9282 if {[info exists markedid] && $markedid eq $id} {
9283 make_idmark $id
9287 proc mktagcan {} {
9288 global mktagtop
9290 catch {destroy $mktagtop}
9291 unset mktagtop
9294 proc mktaggo {} {
9295 if {![domktag]} return
9296 mktagcan
9299 proc writecommit {} {
9300 global rowmenuid wrcomtop commitinfo wrcomcmd NS
9302 set top .writecommit
9303 set wrcomtop $top
9304 catch {destroy $top}
9305 ttk_toplevel $top
9306 make_transient $top .
9307 ${NS}::label $top.title -text [mc "Write commit to file"]
9308 grid $top.title - -pady 10
9309 ${NS}::label $top.id -text [mc "ID:"]
9310 ${NS}::entry $top.sha1 -width 40
9311 $top.sha1 insert 0 $rowmenuid
9312 $top.sha1 conf -state readonly
9313 grid $top.id $top.sha1 -sticky w
9314 ${NS}::entry $top.head -width 60
9315 $top.head insert 0 [lindex $commitinfo($rowmenuid) 0]
9316 $top.head conf -state readonly
9317 grid x $top.head -sticky w
9318 ${NS}::label $top.clab -text [mc "Command:"]
9319 ${NS}::entry $top.cmd -width 60 -textvariable wrcomcmd
9320 grid $top.clab $top.cmd -sticky w -pady 10
9321 ${NS}::label $top.flab -text [mc "Output file:"]
9322 ${NS}::entry $top.fname -width 60
9323 $top.fname insert 0 [file normalize "commit-[string range $rowmenuid 0 6]"]
9324 grid $top.flab $top.fname -sticky w
9325 ${NS}::frame $top.buts
9326 ${NS}::button $top.buts.gen -text [mc "Write"] -command wrcomgo
9327 ${NS}::button $top.buts.can -text [mc "Cancel"] -command wrcomcan
9328 bind $top <Key-Return> wrcomgo
9329 bind $top <Key-Escape> wrcomcan
9330 grid $top.buts.gen $top.buts.can
9331 grid columnconfigure $top.buts 0 -weight 1 -uniform a
9332 grid columnconfigure $top.buts 1 -weight 1 -uniform a
9333 grid $top.buts - -pady 10 -sticky ew
9334 focus $top.fname
9337 proc wrcomgo {} {
9338 global wrcomtop
9340 set id [$wrcomtop.sha1 get]
9341 set cmd "echo $id | [$wrcomtop.cmd get]"
9342 set fname [$wrcomtop.fname get]
9343 if {[catch {exec sh -c $cmd >$fname &} err]} {
9344 error_popup "[mc "Error writing commit:"] $err" $wrcomtop
9346 catch {destroy $wrcomtop}
9347 unset wrcomtop
9350 proc wrcomcan {} {
9351 global wrcomtop
9353 catch {destroy $wrcomtop}
9354 unset wrcomtop
9357 proc mkbranch {} {
9358 global rowmenuid mkbrtop NS
9360 set top .makebranch
9361 catch {destroy $top}
9362 ttk_toplevel $top
9363 make_transient $top .
9364 ${NS}::label $top.title -text [mc "Create new branch"]
9365 grid $top.title - -pady 10
9366 ${NS}::label $top.id -text [mc "ID:"]
9367 ${NS}::entry $top.sha1 -width 40
9368 $top.sha1 insert 0 $rowmenuid
9369 $top.sha1 conf -state readonly
9370 grid $top.id $top.sha1 -sticky w
9371 ${NS}::label $top.nlab -text [mc "Name:"]
9372 ${NS}::entry $top.name -width 40
9373 grid $top.nlab $top.name -sticky w
9374 ${NS}::frame $top.buts
9375 ${NS}::button $top.buts.go -text [mc "Create"] -command [list mkbrgo $top]
9376 ${NS}::button $top.buts.can -text [mc "Cancel"] -command "catch {destroy $top}"
9377 bind $top <Key-Return> [list mkbrgo $top]
9378 bind $top <Key-Escape> "catch {destroy $top}"
9379 grid $top.buts.go $top.buts.can
9380 grid columnconfigure $top.buts 0 -weight 1 -uniform a
9381 grid columnconfigure $top.buts 1 -weight 1 -uniform a
9382 grid $top.buts - -pady 10 -sticky ew
9383 focus $top.name
9386 proc mkbrgo {top} {
9387 global headids idheads
9389 set name [$top.name get]
9390 set id [$top.sha1 get]
9391 set cmdargs {}
9392 set old_id {}
9393 if {$name eq {}} {
9394 error_popup [mc "Please specify a name for the new branch"] $top
9395 return
9397 if {[info exists headids($name)]} {
9398 if {![confirm_popup [mc \
9399 "Branch '%s' already exists. Overwrite?" $name] $top]} {
9400 return
9402 set old_id $headids($name)
9403 lappend cmdargs -f
9405 catch {destroy $top}
9406 lappend cmdargs $name $id
9407 nowbusy newbranch
9408 update
9409 if {[catch {
9410 eval exec git branch $cmdargs
9411 } err]} {
9412 notbusy newbranch
9413 error_popup $err
9414 } else {
9415 notbusy newbranch
9416 if {$old_id ne {}} {
9417 movehead $id $name
9418 movedhead $id $name
9419 redrawtags $old_id
9420 redrawtags $id
9421 } else {
9422 set headids($name) $id
9423 lappend idheads($id) $name
9424 addedhead $id $name
9425 redrawtags $id
9427 dispneartags 0
9428 run refill_reflist
9432 proc exec_citool {tool_args {baseid {}}} {
9433 global commitinfo env
9435 set save_env [array get env GIT_AUTHOR_*]
9437 if {$baseid ne {}} {
9438 if {![info exists commitinfo($baseid)]} {
9439 getcommit $baseid
9441 set author [lindex $commitinfo($baseid) 1]
9442 set date [lindex $commitinfo($baseid) 2]
9443 if {[regexp {^\s*(\S.*\S|\S)\s*<(.*)>\s*$} \
9444 $author author name email]
9445 && $date ne {}} {
9446 set env(GIT_AUTHOR_NAME) $name
9447 set env(GIT_AUTHOR_EMAIL) $email
9448 set env(GIT_AUTHOR_DATE) $date
9452 eval exec git citool $tool_args &
9454 array unset env GIT_AUTHOR_*
9455 array set env $save_env
9458 proc cherrypick {} {
9459 global rowmenuid curview
9460 global mainhead mainheadid
9461 global gitdir
9463 set oldhead [exec git rev-parse HEAD]
9464 set dheads [descheads $rowmenuid]
9465 if {$dheads ne {} && [lsearch -exact $dheads $oldhead] >= 0} {
9466 set ok [confirm_popup [mc "Commit %s is already\
9467 included in branch %s -- really re-apply it?" \
9468 [string range $rowmenuid 0 7] $mainhead]]
9469 if {!$ok} return
9471 nowbusy cherrypick [mc "Cherry-picking"]
9472 update
9473 # Unfortunately git-cherry-pick writes stuff to stderr even when
9474 # no error occurs, and exec takes that as an indication of error...
9475 if {[catch {exec sh -c "git cherry-pick -r $rowmenuid 2>&1"} err]} {
9476 notbusy cherrypick
9477 if {[regexp -line \
9478 {Entry '(.*)' (would be overwritten by merge|not uptodate)} \
9479 $err msg fname]} {
9480 error_popup [mc "Cherry-pick failed because of local changes\
9481 to file '%s'.\nPlease commit, reset or stash\
9482 your changes and try again." $fname]
9483 } elseif {[regexp -line \
9484 {^(CONFLICT \(.*\):|Automatic cherry-pick failed|error: could not apply)} \
9485 $err]} {
9486 if {[confirm_popup [mc "Cherry-pick failed because of merge\
9487 conflict.\nDo you wish to run git citool to\
9488 resolve it?"]]} {
9489 # Force citool to read MERGE_MSG
9490 file delete [file join $gitdir "GITGUI_MSG"]
9491 exec_citool {} $rowmenuid
9493 } else {
9494 error_popup $err
9496 run updatecommits
9497 return
9499 set newhead [exec git rev-parse HEAD]
9500 if {$newhead eq $oldhead} {
9501 notbusy cherrypick
9502 error_popup [mc "No changes committed"]
9503 return
9505 addnewchild $newhead $oldhead
9506 if {[commitinview $oldhead $curview]} {
9507 # XXX this isn't right if we have a path limit...
9508 insertrow $newhead $oldhead $curview
9509 if {$mainhead ne {}} {
9510 movehead $newhead $mainhead
9511 movedhead $newhead $mainhead
9513 set mainheadid $newhead
9514 redrawtags $oldhead
9515 redrawtags $newhead
9516 selbyid $newhead
9518 notbusy cherrypick
9521 proc revert {} {
9522 global rowmenuid curview
9523 global mainhead mainheadid
9524 global gitdir
9526 set oldhead [exec git rev-parse HEAD]
9527 set dheads [descheads $rowmenuid]
9528 if { $dheads eq {} || [lsearch -exact $dheads $oldhead] == -1 } {
9529 set ok [confirm_popup [mc "Commit %s is not\
9530 included in branch %s -- really revert it?" \
9531 [string range $rowmenuid 0 7] $mainhead]]
9532 if {!$ok} return
9534 nowbusy revert [mc "Reverting"]
9535 update
9537 if [catch {exec git revert --no-edit $rowmenuid} err] {
9538 notbusy revert
9539 if [regexp {files would be overwritten by merge:(\n(( |\t)+[^\n]+\n)+)}\
9540 $err match files] {
9541 regsub {\n( |\t)+} $files "\n" files
9542 error_popup [mc "Revert failed because of local changes to\
9543 the following files:%s Please commit, reset or stash \
9544 your changes and try again." $files]
9545 } elseif [regexp {error: could not revert} $err] {
9546 if [confirm_popup [mc "Revert failed because of merge conflict.\n\
9547 Do you wish to run git citool to resolve it?"]] {
9548 # Force citool to read MERGE_MSG
9549 file delete [file join $gitdir "GITGUI_MSG"]
9550 exec_citool {} $rowmenuid
9552 } else { error_popup $err }
9553 run updatecommits
9554 return
9557 set newhead [exec git rev-parse HEAD]
9558 if { $newhead eq $oldhead } {
9559 notbusy revert
9560 error_popup [mc "No changes committed"]
9561 return
9564 addnewchild $newhead $oldhead
9566 if [commitinview $oldhead $curview] {
9567 # XXX this isn't right if we have a path limit...
9568 insertrow $newhead $oldhead $curview
9569 if {$mainhead ne {}} {
9570 movehead $newhead $mainhead
9571 movedhead $newhead $mainhead
9573 set mainheadid $newhead
9574 redrawtags $oldhead
9575 redrawtags $newhead
9576 selbyid $newhead
9579 notbusy revert
9582 proc resethead {} {
9583 global mainhead rowmenuid confirm_ok resettype NS
9585 set confirm_ok 0
9586 set w ".confirmreset"
9587 ttk_toplevel $w
9588 make_transient $w .
9589 wm title $w [mc "Confirm reset"]
9590 ${NS}::label $w.m -text \
9591 [mc "Reset branch %s to %s?" $mainhead [string range $rowmenuid 0 7]]
9592 pack $w.m -side top -fill x -padx 20 -pady 20
9593 ${NS}::labelframe $w.f -text [mc "Reset type:"]
9594 set resettype mixed
9595 ${NS}::radiobutton $w.f.soft -value soft -variable resettype \
9596 -text [mc "Soft: Leave working tree and index untouched"]
9597 grid $w.f.soft -sticky w
9598 ${NS}::radiobutton $w.f.mixed -value mixed -variable resettype \
9599 -text [mc "Mixed: Leave working tree untouched, reset index"]
9600 grid $w.f.mixed -sticky w
9601 ${NS}::radiobutton $w.f.hard -value hard -variable resettype \
9602 -text [mc "Hard: Reset working tree and index\n(discard ALL local changes)"]
9603 grid $w.f.hard -sticky w
9604 pack $w.f -side top -fill x -padx 4
9605 ${NS}::button $w.ok -text [mc OK] -command "set confirm_ok 1; destroy $w"
9606 pack $w.ok -side left -fill x -padx 20 -pady 20
9607 ${NS}::button $w.cancel -text [mc Cancel] -command "destroy $w"
9608 bind $w <Key-Escape> [list destroy $w]
9609 pack $w.cancel -side right -fill x -padx 20 -pady 20
9610 bind $w <Visibility> "grab $w; focus $w"
9611 tkwait window $w
9612 if {!$confirm_ok} return
9613 if {[catch {set fd [open \
9614 [list | git reset --$resettype $rowmenuid 2>@1] r]} err]} {
9615 error_popup $err
9616 } else {
9617 dohidelocalchanges
9618 filerun $fd [list readresetstat $fd]
9619 nowbusy reset [mc "Resetting"]
9620 selbyid $rowmenuid
9624 proc readresetstat {fd} {
9625 global mainhead mainheadid showlocalchanges rprogcoord
9627 if {[gets $fd line] >= 0} {
9628 if {[regexp {([0-9]+)% \(([0-9]+)/([0-9]+)\)} $line match p m n]} {
9629 set rprogcoord [expr {1.0 * $m / $n}]
9630 adjustprogress
9632 return 1
9634 set rprogcoord 0
9635 adjustprogress
9636 notbusy reset
9637 if {[catch {close $fd} err]} {
9638 error_popup $err
9640 set oldhead $mainheadid
9641 set newhead [exec git rev-parse HEAD]
9642 if {$newhead ne $oldhead} {
9643 movehead $newhead $mainhead
9644 movedhead $newhead $mainhead
9645 set mainheadid $newhead
9646 redrawtags $oldhead
9647 redrawtags $newhead
9649 if {$showlocalchanges} {
9650 doshowlocalchanges
9652 return 0
9655 # context menu for a head
9656 proc headmenu {x y id head} {
9657 global headmenuid headmenuhead headctxmenu mainhead
9659 stopfinding
9660 set headmenuid $id
9661 set headmenuhead $head
9662 set state normal
9663 if {[string match "remotes/*" $head]} {
9664 set state disabled
9666 if {$head eq $mainhead} {
9667 set state disabled
9669 $headctxmenu entryconfigure 0 -state $state
9670 $headctxmenu entryconfigure 1 -state $state
9671 tk_popup $headctxmenu $x $y
9674 proc cobranch {} {
9675 global headmenuid headmenuhead headids
9676 global showlocalchanges
9678 # check the tree is clean first??
9679 nowbusy checkout [mc "Checking out"]
9680 update
9681 dohidelocalchanges
9682 if {[catch {
9683 set fd [open [list | git checkout $headmenuhead 2>@1] r]
9684 } err]} {
9685 notbusy checkout
9686 error_popup $err
9687 if {$showlocalchanges} {
9688 dodiffindex
9690 } else {
9691 filerun $fd [list readcheckoutstat $fd $headmenuhead $headmenuid]
9695 proc readcheckoutstat {fd newhead newheadid} {
9696 global mainhead mainheadid headids showlocalchanges progresscoords
9697 global viewmainheadid curview
9699 if {[gets $fd line] >= 0} {
9700 if {[regexp {([0-9]+)% \(([0-9]+)/([0-9]+)\)} $line match p m n]} {
9701 set progresscoords [list 0 [expr {1.0 * $m / $n}]]
9702 adjustprogress
9704 return 1
9706 set progresscoords {0 0}
9707 adjustprogress
9708 notbusy checkout
9709 if {[catch {close $fd} err]} {
9710 error_popup $err
9712 set oldmainid $mainheadid
9713 set mainhead $newhead
9714 set mainheadid $newheadid
9715 set viewmainheadid($curview) $newheadid
9716 redrawtags $oldmainid
9717 redrawtags $newheadid
9718 selbyid $newheadid
9719 if {$showlocalchanges} {
9720 dodiffindex
9724 proc rmbranch {} {
9725 global headmenuid headmenuhead mainhead
9726 global idheads
9728 set head $headmenuhead
9729 set id $headmenuid
9730 # this check shouldn't be needed any more...
9731 if {$head eq $mainhead} {
9732 error_popup [mc "Cannot delete the currently checked-out branch"]
9733 return
9735 set dheads [descheads $id]
9736 if {[llength $dheads] == 1 && $idheads($dheads) eq $head} {
9737 # the stuff on this branch isn't on any other branch
9738 if {![confirm_popup [mc "The commits on branch %s aren't on any other\
9739 branch.\nReally delete branch %s?" $head $head]]} return
9741 nowbusy rmbranch
9742 update
9743 if {[catch {exec git branch -D $head} err]} {
9744 notbusy rmbranch
9745 error_popup $err
9746 return
9748 removehead $id $head
9749 removedhead $id $head
9750 redrawtags $id
9751 notbusy rmbranch
9752 dispneartags 0
9753 run refill_reflist
9756 # Display a list of tags and heads
9757 proc showrefs {} {
9758 global showrefstop bgcolor fgcolor selectbgcolor NS
9759 global bglist fglist reflistfilter reflist maincursor
9761 set top .showrefs
9762 set showrefstop $top
9763 if {[winfo exists $top]} {
9764 raise $top
9765 refill_reflist
9766 return
9768 ttk_toplevel $top
9769 wm title $top [mc "Tags and heads: %s" [file tail [pwd]]]
9770 make_transient $top .
9771 text $top.list -background $bgcolor -foreground $fgcolor \
9772 -selectbackground $selectbgcolor -font mainfont \
9773 -xscrollcommand "$top.xsb set" -yscrollcommand "$top.ysb set" \
9774 -width 30 -height 20 -cursor $maincursor \
9775 -spacing1 1 -spacing3 1 -state disabled
9776 $top.list tag configure highlight -background $selectbgcolor
9777 lappend bglist $top.list
9778 lappend fglist $top.list
9779 ${NS}::scrollbar $top.ysb -command "$top.list yview" -orient vertical
9780 ${NS}::scrollbar $top.xsb -command "$top.list xview" -orient horizontal
9781 grid $top.list $top.ysb -sticky nsew
9782 grid $top.xsb x -sticky ew
9783 ${NS}::frame $top.f
9784 ${NS}::label $top.f.l -text "[mc "Filter"]: "
9785 ${NS}::entry $top.f.e -width 20 -textvariable reflistfilter
9786 set reflistfilter "*"
9787 trace add variable reflistfilter write reflistfilter_change
9788 pack $top.f.e -side right -fill x -expand 1
9789 pack $top.f.l -side left
9790 grid $top.f - -sticky ew -pady 2
9791 ${NS}::button $top.close -command [list destroy $top] -text [mc "Close"]
9792 bind $top <Key-Escape> [list destroy $top]
9793 grid $top.close -
9794 grid columnconfigure $top 0 -weight 1
9795 grid rowconfigure $top 0 -weight 1
9796 bind $top.list <1> {break}
9797 bind $top.list <B1-Motion> {break}
9798 bind $top.list <ButtonRelease-1> {sel_reflist %W %x %y; break}
9799 set reflist {}
9800 refill_reflist
9803 proc sel_reflist {w x y} {
9804 global showrefstop reflist headids tagids otherrefids
9806 if {![winfo exists $showrefstop]} return
9807 set l [lindex [split [$w index "@$x,$y"] "."] 0]
9808 set ref [lindex $reflist [expr {$l-1}]]
9809 set n [lindex $ref 0]
9810 switch -- [lindex $ref 1] {
9811 "H" {selbyid $headids($n)}
9812 "T" {selbyid $tagids($n)}
9813 "o" {selbyid $otherrefids($n)}
9815 $showrefstop.list tag add highlight $l.0 "$l.0 lineend"
9818 proc unsel_reflist {} {
9819 global showrefstop
9821 if {![info exists showrefstop] || ![winfo exists $showrefstop]} return
9822 $showrefstop.list tag remove highlight 0.0 end
9825 proc reflistfilter_change {n1 n2 op} {
9826 global reflistfilter
9828 after cancel refill_reflist
9829 after 200 refill_reflist
9832 proc refill_reflist {} {
9833 global reflist reflistfilter showrefstop headids tagids otherrefids
9834 global curview
9836 if {![info exists showrefstop] || ![winfo exists $showrefstop]} return
9837 set refs {}
9838 foreach n [array names headids] {
9839 if {[string match $reflistfilter $n]} {
9840 if {[commitinview $headids($n) $curview]} {
9841 lappend refs [list $n H]
9842 } else {
9843 interestedin $headids($n) {run refill_reflist}
9847 foreach n [array names tagids] {
9848 if {[string match $reflistfilter $n]} {
9849 if {[commitinview $tagids($n) $curview]} {
9850 lappend refs [list $n T]
9851 } else {
9852 interestedin $tagids($n) {run refill_reflist}
9856 foreach n [array names otherrefids] {
9857 if {[string match $reflistfilter $n]} {
9858 if {[commitinview $otherrefids($n) $curview]} {
9859 lappend refs [list $n o]
9860 } else {
9861 interestedin $otherrefids($n) {run refill_reflist}
9865 set refs [lsort -index 0 $refs]
9866 if {$refs eq $reflist} return
9868 # Update the contents of $showrefstop.list according to the
9869 # differences between $reflist (old) and $refs (new)
9870 $showrefstop.list conf -state normal
9871 $showrefstop.list insert end "\n"
9872 set i 0
9873 set j 0
9874 while {$i < [llength $reflist] || $j < [llength $refs]} {
9875 if {$i < [llength $reflist]} {
9876 if {$j < [llength $refs]} {
9877 set cmp [string compare [lindex $reflist $i 0] \
9878 [lindex $refs $j 0]]
9879 if {$cmp == 0} {
9880 set cmp [string compare [lindex $reflist $i 1] \
9881 [lindex $refs $j 1]]
9883 } else {
9884 set cmp -1
9886 } else {
9887 set cmp 1
9889 switch -- $cmp {
9890 -1 {
9891 $showrefstop.list delete "[expr {$j+1}].0" "[expr {$j+2}].0"
9892 incr i
9895 incr i
9896 incr j
9899 set l [expr {$j + 1}]
9900 $showrefstop.list image create $l.0 -align baseline \
9901 -image reficon-[lindex $refs $j 1] -padx 2
9902 $showrefstop.list insert $l.1 "[lindex $refs $j 0]\n"
9903 incr j
9907 set reflist $refs
9908 # delete last newline
9909 $showrefstop.list delete end-2c end-1c
9910 $showrefstop.list conf -state disabled
9913 # Stuff for finding nearby tags
9914 proc getallcommits {} {
9915 global allcommits nextarc seeds allccache allcwait cachedarcs allcupdate
9916 global idheads idtags idotherrefs allparents tagobjid
9917 global gitdir
9919 if {![info exists allcommits]} {
9920 set nextarc 0
9921 set allcommits 0
9922 set seeds {}
9923 set allcwait 0
9924 set cachedarcs 0
9925 set allccache [file join $gitdir "gitk.cache"]
9926 if {![catch {
9927 set f [open $allccache r]
9928 set allcwait 1
9929 getcache $f
9930 }]} return
9933 if {$allcwait} {
9934 return
9936 set cmd [list | git rev-list --parents]
9937 set allcupdate [expr {$seeds ne {}}]
9938 if {!$allcupdate} {
9939 set ids "--all"
9940 } else {
9941 set refs [concat [array names idheads] [array names idtags] \
9942 [array names idotherrefs]]
9943 set ids {}
9944 set tagobjs {}
9945 foreach name [array names tagobjid] {
9946 lappend tagobjs $tagobjid($name)
9948 foreach id [lsort -unique $refs] {
9949 if {![info exists allparents($id)] &&
9950 [lsearch -exact $tagobjs $id] < 0} {
9951 lappend ids $id
9954 if {$ids ne {}} {
9955 foreach id $seeds {
9956 lappend ids "^$id"
9960 if {$ids ne {}} {
9961 set fd [open [concat $cmd $ids] r]
9962 fconfigure $fd -blocking 0
9963 incr allcommits
9964 nowbusy allcommits
9965 filerun $fd [list getallclines $fd]
9966 } else {
9967 dispneartags 0
9971 # Since most commits have 1 parent and 1 child, we group strings of
9972 # such commits into "arcs" joining branch/merge points (BMPs), which
9973 # are commits that either don't have 1 parent or don't have 1 child.
9975 # arcnos(id) - incoming arcs for BMP, arc we're on for other nodes
9976 # arcout(id) - outgoing arcs for BMP
9977 # arcids(a) - list of IDs on arc including end but not start
9978 # arcstart(a) - BMP ID at start of arc
9979 # arcend(a) - BMP ID at end of arc
9980 # growing(a) - arc a is still growing
9981 # arctags(a) - IDs out of arcids (excluding end) that have tags
9982 # archeads(a) - IDs out of arcids (excluding end) that have heads
9983 # The start of an arc is at the descendent end, so "incoming" means
9984 # coming from descendents, and "outgoing" means going towards ancestors.
9986 proc getallclines {fd} {
9987 global allparents allchildren idtags idheads nextarc
9988 global arcnos arcids arctags arcout arcend arcstart archeads growing
9989 global seeds allcommits cachedarcs allcupdate
9991 set nid 0
9992 while {[incr nid] <= 1000 && [gets $fd line] >= 0} {
9993 set id [lindex $line 0]
9994 if {[info exists allparents($id)]} {
9995 # seen it already
9996 continue
9998 set cachedarcs 0
9999 set olds [lrange $line 1 end]
10000 set allparents($id) $olds
10001 if {![info exists allchildren($id)]} {
10002 set allchildren($id) {}
10003 set arcnos($id) {}
10004 lappend seeds $id
10005 } else {
10006 set a $arcnos($id)
10007 if {[llength $olds] == 1 && [llength $a] == 1} {
10008 lappend arcids($a) $id
10009 if {[info exists idtags($id)]} {
10010 lappend arctags($a) $id
10012 if {[info exists idheads($id)]} {
10013 lappend archeads($a) $id
10015 if {[info exists allparents($olds)]} {
10016 # seen parent already
10017 if {![info exists arcout($olds)]} {
10018 splitarc $olds
10020 lappend arcids($a) $olds
10021 set arcend($a) $olds
10022 unset growing($a)
10024 lappend allchildren($olds) $id
10025 lappend arcnos($olds) $a
10026 continue
10029 foreach a $arcnos($id) {
10030 lappend arcids($a) $id
10031 set arcend($a) $id
10032 unset growing($a)
10035 set ao {}
10036 foreach p $olds {
10037 lappend allchildren($p) $id
10038 set a [incr nextarc]
10039 set arcstart($a) $id
10040 set archeads($a) {}
10041 set arctags($a) {}
10042 set archeads($a) {}
10043 set arcids($a) {}
10044 lappend ao $a
10045 set growing($a) 1
10046 if {[info exists allparents($p)]} {
10047 # seen it already, may need to make a new branch
10048 if {![info exists arcout($p)]} {
10049 splitarc $p
10051 lappend arcids($a) $p
10052 set arcend($a) $p
10053 unset growing($a)
10055 lappend arcnos($p) $a
10057 set arcout($id) $ao
10059 if {$nid > 0} {
10060 global cached_dheads cached_dtags cached_atags
10061 catch {unset cached_dheads}
10062 catch {unset cached_dtags}
10063 catch {unset cached_atags}
10065 if {![eof $fd]} {
10066 return [expr {$nid >= 1000? 2: 1}]
10068 set cacheok 1
10069 if {[catch {
10070 fconfigure $fd -blocking 1
10071 close $fd
10072 } err]} {
10073 # got an error reading the list of commits
10074 # if we were updating, try rereading the whole thing again
10075 if {$allcupdate} {
10076 incr allcommits -1
10077 dropcache $err
10078 return
10080 error_popup "[mc "Error reading commit topology information;\
10081 branch and preceding/following tag information\
10082 will be incomplete."]\n($err)"
10083 set cacheok 0
10085 if {[incr allcommits -1] == 0} {
10086 notbusy allcommits
10087 if {$cacheok} {
10088 run savecache
10091 dispneartags 0
10092 return 0
10095 proc recalcarc {a} {
10096 global arctags archeads arcids idtags idheads
10098 set at {}
10099 set ah {}
10100 foreach id [lrange $arcids($a) 0 end-1] {
10101 if {[info exists idtags($id)]} {
10102 lappend at $id
10104 if {[info exists idheads($id)]} {
10105 lappend ah $id
10108 set arctags($a) $at
10109 set archeads($a) $ah
10112 proc splitarc {p} {
10113 global arcnos arcids nextarc arctags archeads idtags idheads
10114 global arcstart arcend arcout allparents growing
10116 set a $arcnos($p)
10117 if {[llength $a] != 1} {
10118 puts "oops splitarc called but [llength $a] arcs already"
10119 return
10121 set a [lindex $a 0]
10122 set i [lsearch -exact $arcids($a) $p]
10123 if {$i < 0} {
10124 puts "oops splitarc $p not in arc $a"
10125 return
10127 set na [incr nextarc]
10128 if {[info exists arcend($a)]} {
10129 set arcend($na) $arcend($a)
10130 } else {
10131 set l [lindex $allparents([lindex $arcids($a) end]) 0]
10132 set j [lsearch -exact $arcnos($l) $a]
10133 set arcnos($l) [lreplace $arcnos($l) $j $j $na]
10135 set tail [lrange $arcids($a) [expr {$i+1}] end]
10136 set arcids($a) [lrange $arcids($a) 0 $i]
10137 set arcend($a) $p
10138 set arcstart($na) $p
10139 set arcout($p) $na
10140 set arcids($na) $tail
10141 if {[info exists growing($a)]} {
10142 set growing($na) 1
10143 unset growing($a)
10146 foreach id $tail {
10147 if {[llength $arcnos($id)] == 1} {
10148 set arcnos($id) $na
10149 } else {
10150 set j [lsearch -exact $arcnos($id) $a]
10151 set arcnos($id) [lreplace $arcnos($id) $j $j $na]
10155 # reconstruct tags and heads lists
10156 if {$arctags($a) ne {} || $archeads($a) ne {}} {
10157 recalcarc $a
10158 recalcarc $na
10159 } else {
10160 set arctags($na) {}
10161 set archeads($na) {}
10165 # Update things for a new commit added that is a child of one
10166 # existing commit. Used when cherry-picking.
10167 proc addnewchild {id p} {
10168 global allparents allchildren idtags nextarc
10169 global arcnos arcids arctags arcout arcend arcstart archeads growing
10170 global seeds allcommits
10172 if {![info exists allcommits] || ![info exists arcnos($p)]} return
10173 set allparents($id) [list $p]
10174 set allchildren($id) {}
10175 set arcnos($id) {}
10176 lappend seeds $id
10177 lappend allchildren($p) $id
10178 set a [incr nextarc]
10179 set arcstart($a) $id
10180 set archeads($a) {}
10181 set arctags($a) {}
10182 set arcids($a) [list $p]
10183 set arcend($a) $p
10184 if {![info exists arcout($p)]} {
10185 splitarc $p
10187 lappend arcnos($p) $a
10188 set arcout($id) [list $a]
10191 # This implements a cache for the topology information.
10192 # The cache saves, for each arc, the start and end of the arc,
10193 # the ids on the arc, and the outgoing arcs from the end.
10194 proc readcache {f} {
10195 global arcnos arcids arcout arcstart arcend arctags archeads nextarc
10196 global idtags idheads allparents cachedarcs possible_seeds seeds growing
10197 global allcwait
10199 set a $nextarc
10200 set lim $cachedarcs
10201 if {$lim - $a > 500} {
10202 set lim [expr {$a + 500}]
10204 if {[catch {
10205 if {$a == $lim} {
10206 # finish reading the cache and setting up arctags, etc.
10207 set line [gets $f]
10208 if {$line ne "1"} {error "bad final version"}
10209 close $f
10210 foreach id [array names idtags] {
10211 if {[info exists arcnos($id)] && [llength $arcnos($id)] == 1 &&
10212 [llength $allparents($id)] == 1} {
10213 set a [lindex $arcnos($id) 0]
10214 if {$arctags($a) eq {}} {
10215 recalcarc $a
10219 foreach id [array names idheads] {
10220 if {[info exists arcnos($id)] && [llength $arcnos($id)] == 1 &&
10221 [llength $allparents($id)] == 1} {
10222 set a [lindex $arcnos($id) 0]
10223 if {$archeads($a) eq {}} {
10224 recalcarc $a
10228 foreach id [lsort -unique $possible_seeds] {
10229 if {$arcnos($id) eq {}} {
10230 lappend seeds $id
10233 set allcwait 0
10234 } else {
10235 while {[incr a] <= $lim} {
10236 set line [gets $f]
10237 if {[llength $line] != 3} {error "bad line"}
10238 set s [lindex $line 0]
10239 set arcstart($a) $s
10240 lappend arcout($s) $a
10241 if {![info exists arcnos($s)]} {
10242 lappend possible_seeds $s
10243 set arcnos($s) {}
10245 set e [lindex $line 1]
10246 if {$e eq {}} {
10247 set growing($a) 1
10248 } else {
10249 set arcend($a) $e
10250 if {![info exists arcout($e)]} {
10251 set arcout($e) {}
10254 set arcids($a) [lindex $line 2]
10255 foreach id $arcids($a) {
10256 lappend allparents($s) $id
10257 set s $id
10258 lappend arcnos($id) $a
10260 if {![info exists allparents($s)]} {
10261 set allparents($s) {}
10263 set arctags($a) {}
10264 set archeads($a) {}
10266 set nextarc [expr {$a - 1}]
10268 } err]} {
10269 dropcache $err
10270 return 0
10272 if {!$allcwait} {
10273 getallcommits
10275 return $allcwait
10278 proc getcache {f} {
10279 global nextarc cachedarcs possible_seeds
10281 if {[catch {
10282 set line [gets $f]
10283 if {[llength $line] != 2 || [lindex $line 0] ne "1"} {error "bad version"}
10284 # make sure it's an integer
10285 set cachedarcs [expr {int([lindex $line 1])}]
10286 if {$cachedarcs < 0} {error "bad number of arcs"}
10287 set nextarc 0
10288 set possible_seeds {}
10289 run readcache $f
10290 } err]} {
10291 dropcache $err
10293 return 0
10296 proc dropcache {err} {
10297 global allcwait nextarc cachedarcs seeds
10299 #puts "dropping cache ($err)"
10300 foreach v {arcnos arcout arcids arcstart arcend growing \
10301 arctags archeads allparents allchildren} {
10302 global $v
10303 catch {unset $v}
10305 set allcwait 0
10306 set nextarc 0
10307 set cachedarcs 0
10308 set seeds {}
10309 getallcommits
10312 proc writecache {f} {
10313 global cachearc cachedarcs allccache
10314 global arcstart arcend arcnos arcids arcout
10316 set a $cachearc
10317 set lim $cachedarcs
10318 if {$lim - $a > 1000} {
10319 set lim [expr {$a + 1000}]
10321 if {[catch {
10322 while {[incr a] <= $lim} {
10323 if {[info exists arcend($a)]} {
10324 puts $f [list $arcstart($a) $arcend($a) $arcids($a)]
10325 } else {
10326 puts $f [list $arcstart($a) {} $arcids($a)]
10329 } err]} {
10330 catch {close $f}
10331 catch {file delete $allccache}
10332 #puts "writing cache failed ($err)"
10333 return 0
10335 set cachearc [expr {$a - 1}]
10336 if {$a > $cachedarcs} {
10337 puts $f "1"
10338 close $f
10339 return 0
10341 return 1
10344 proc savecache {} {
10345 global nextarc cachedarcs cachearc allccache
10347 if {$nextarc == $cachedarcs} return
10348 set cachearc 0
10349 set cachedarcs $nextarc
10350 catch {
10351 set f [open $allccache w]
10352 puts $f [list 1 $cachedarcs]
10353 run writecache $f
10357 # Returns 1 if a is an ancestor of b, -1 if b is an ancestor of a,
10358 # or 0 if neither is true.
10359 proc anc_or_desc {a b} {
10360 global arcout arcstart arcend arcnos cached_isanc
10362 if {$arcnos($a) eq $arcnos($b)} {
10363 # Both are on the same arc(s); either both are the same BMP,
10364 # or if one is not a BMP, the other is also not a BMP or is
10365 # the BMP at end of the arc (and it only has 1 incoming arc).
10366 # Or both can be BMPs with no incoming arcs.
10367 if {$a eq $b || $arcnos($a) eq {}} {
10368 return 0
10370 # assert {[llength $arcnos($a)] == 1}
10371 set arc [lindex $arcnos($a) 0]
10372 set i [lsearch -exact $arcids($arc) $a]
10373 set j [lsearch -exact $arcids($arc) $b]
10374 if {$i < 0 || $i > $j} {
10375 return 1
10376 } else {
10377 return -1
10381 if {![info exists arcout($a)]} {
10382 set arc [lindex $arcnos($a) 0]
10383 if {[info exists arcend($arc)]} {
10384 set aend $arcend($arc)
10385 } else {
10386 set aend {}
10388 set a $arcstart($arc)
10389 } else {
10390 set aend $a
10392 if {![info exists arcout($b)]} {
10393 set arc [lindex $arcnos($b) 0]
10394 if {[info exists arcend($arc)]} {
10395 set bend $arcend($arc)
10396 } else {
10397 set bend {}
10399 set b $arcstart($arc)
10400 } else {
10401 set bend $b
10403 if {$a eq $bend} {
10404 return 1
10406 if {$b eq $aend} {
10407 return -1
10409 if {[info exists cached_isanc($a,$bend)]} {
10410 if {$cached_isanc($a,$bend)} {
10411 return 1
10414 if {[info exists cached_isanc($b,$aend)]} {
10415 if {$cached_isanc($b,$aend)} {
10416 return -1
10418 if {[info exists cached_isanc($a,$bend)]} {
10419 return 0
10423 set todo [list $a $b]
10424 set anc($a) a
10425 set anc($b) b
10426 for {set i 0} {$i < [llength $todo]} {incr i} {
10427 set x [lindex $todo $i]
10428 if {$anc($x) eq {}} {
10429 continue
10431 foreach arc $arcnos($x) {
10432 set xd $arcstart($arc)
10433 if {$xd eq $bend} {
10434 set cached_isanc($a,$bend) 1
10435 set cached_isanc($b,$aend) 0
10436 return 1
10437 } elseif {$xd eq $aend} {
10438 set cached_isanc($b,$aend) 1
10439 set cached_isanc($a,$bend) 0
10440 return -1
10442 if {![info exists anc($xd)]} {
10443 set anc($xd) $anc($x)
10444 lappend todo $xd
10445 } elseif {$anc($xd) ne $anc($x)} {
10446 set anc($xd) {}
10450 set cached_isanc($a,$bend) 0
10451 set cached_isanc($b,$aend) 0
10452 return 0
10455 # This identifies whether $desc has an ancestor that is
10456 # a growing tip of the graph and which is not an ancestor of $anc
10457 # and returns 0 if so and 1 if not.
10458 # If we subsequently discover a tag on such a growing tip, and that
10459 # turns out to be a descendent of $anc (which it could, since we
10460 # don't necessarily see children before parents), then $desc
10461 # isn't a good choice to display as a descendent tag of
10462 # $anc (since it is the descendent of another tag which is
10463 # a descendent of $anc). Similarly, $anc isn't a good choice to
10464 # display as a ancestor tag of $desc.
10466 proc is_certain {desc anc} {
10467 global arcnos arcout arcstart arcend growing problems
10469 set certain {}
10470 if {[llength $arcnos($anc)] == 1} {
10471 # tags on the same arc are certain
10472 if {$arcnos($desc) eq $arcnos($anc)} {
10473 return 1
10475 if {![info exists arcout($anc)]} {
10476 # if $anc is partway along an arc, use the start of the arc instead
10477 set a [lindex $arcnos($anc) 0]
10478 set anc $arcstart($a)
10481 if {[llength $arcnos($desc)] > 1 || [info exists arcout($desc)]} {
10482 set x $desc
10483 } else {
10484 set a [lindex $arcnos($desc) 0]
10485 set x $arcend($a)
10487 if {$x == $anc} {
10488 return 1
10490 set anclist [list $x]
10491 set dl($x) 1
10492 set nnh 1
10493 set ngrowanc 0
10494 for {set i 0} {$i < [llength $anclist] && ($nnh > 0 || $ngrowanc > 0)} {incr i} {
10495 set x [lindex $anclist $i]
10496 if {$dl($x)} {
10497 incr nnh -1
10499 set done($x) 1
10500 foreach a $arcout($x) {
10501 if {[info exists growing($a)]} {
10502 if {![info exists growanc($x)] && $dl($x)} {
10503 set growanc($x) 1
10504 incr ngrowanc
10506 } else {
10507 set y $arcend($a)
10508 if {[info exists dl($y)]} {
10509 if {$dl($y)} {
10510 if {!$dl($x)} {
10511 set dl($y) 0
10512 if {![info exists done($y)]} {
10513 incr nnh -1
10515 if {[info exists growanc($x)]} {
10516 incr ngrowanc -1
10518 set xl [list $y]
10519 for {set k 0} {$k < [llength $xl]} {incr k} {
10520 set z [lindex $xl $k]
10521 foreach c $arcout($z) {
10522 if {[info exists arcend($c)]} {
10523 set v $arcend($c)
10524 if {[info exists dl($v)] && $dl($v)} {
10525 set dl($v) 0
10526 if {![info exists done($v)]} {
10527 incr nnh -1
10529 if {[info exists growanc($v)]} {
10530 incr ngrowanc -1
10532 lappend xl $v
10539 } elseif {$y eq $anc || !$dl($x)} {
10540 set dl($y) 0
10541 lappend anclist $y
10542 } else {
10543 set dl($y) 1
10544 lappend anclist $y
10545 incr nnh
10550 foreach x [array names growanc] {
10551 if {$dl($x)} {
10552 return 0
10554 return 0
10556 return 1
10559 proc validate_arctags {a} {
10560 global arctags idtags
10562 set i -1
10563 set na $arctags($a)
10564 foreach id $arctags($a) {
10565 incr i
10566 if {![info exists idtags($id)]} {
10567 set na [lreplace $na $i $i]
10568 incr i -1
10571 set arctags($a) $na
10574 proc validate_archeads {a} {
10575 global archeads idheads
10577 set i -1
10578 set na $archeads($a)
10579 foreach id $archeads($a) {
10580 incr i
10581 if {![info exists idheads($id)]} {
10582 set na [lreplace $na $i $i]
10583 incr i -1
10586 set archeads($a) $na
10589 # Return the list of IDs that have tags that are descendents of id,
10590 # ignoring IDs that are descendents of IDs already reported.
10591 proc desctags {id} {
10592 global arcnos arcstart arcids arctags idtags allparents
10593 global growing cached_dtags
10595 if {![info exists allparents($id)]} {
10596 return {}
10598 set t1 [clock clicks -milliseconds]
10599 set argid $id
10600 if {[llength $arcnos($id)] == 1 && [llength $allparents($id)] == 1} {
10601 # part-way along an arc; check that arc first
10602 set a [lindex $arcnos($id) 0]
10603 if {$arctags($a) ne {}} {
10604 validate_arctags $a
10605 set i [lsearch -exact $arcids($a) $id]
10606 set tid {}
10607 foreach t $arctags($a) {
10608 set j [lsearch -exact $arcids($a) $t]
10609 if {$j >= $i} break
10610 set tid $t
10612 if {$tid ne {}} {
10613 return $tid
10616 set id $arcstart($a)
10617 if {[info exists idtags($id)]} {
10618 return $id
10621 if {[info exists cached_dtags($id)]} {
10622 return $cached_dtags($id)
10625 set origid $id
10626 set todo [list $id]
10627 set queued($id) 1
10628 set nc 1
10629 for {set i 0} {$i < [llength $todo] && $nc > 0} {incr i} {
10630 set id [lindex $todo $i]
10631 set done($id) 1
10632 set ta [info exists hastaggedancestor($id)]
10633 if {!$ta} {
10634 incr nc -1
10636 # ignore tags on starting node
10637 if {!$ta && $i > 0} {
10638 if {[info exists idtags($id)]} {
10639 set tagloc($id) $id
10640 set ta 1
10641 } elseif {[info exists cached_dtags($id)]} {
10642 set tagloc($id) $cached_dtags($id)
10643 set ta 1
10646 foreach a $arcnos($id) {
10647 set d $arcstart($a)
10648 if {!$ta && $arctags($a) ne {}} {
10649 validate_arctags $a
10650 if {$arctags($a) ne {}} {
10651 lappend tagloc($id) [lindex $arctags($a) end]
10654 if {$ta || $arctags($a) ne {}} {
10655 set tomark [list $d]
10656 for {set j 0} {$j < [llength $tomark]} {incr j} {
10657 set dd [lindex $tomark $j]
10658 if {![info exists hastaggedancestor($dd)]} {
10659 if {[info exists done($dd)]} {
10660 foreach b $arcnos($dd) {
10661 lappend tomark $arcstart($b)
10663 if {[info exists tagloc($dd)]} {
10664 unset tagloc($dd)
10666 } elseif {[info exists queued($dd)]} {
10667 incr nc -1
10669 set hastaggedancestor($dd) 1
10673 if {![info exists queued($d)]} {
10674 lappend todo $d
10675 set queued($d) 1
10676 if {![info exists hastaggedancestor($d)]} {
10677 incr nc
10682 set tags {}
10683 foreach id [array names tagloc] {
10684 if {![info exists hastaggedancestor($id)]} {
10685 foreach t $tagloc($id) {
10686 if {[lsearch -exact $tags $t] < 0} {
10687 lappend tags $t
10692 set t2 [clock clicks -milliseconds]
10693 set loopix $i
10695 # remove tags that are descendents of other tags
10696 for {set i 0} {$i < [llength $tags]} {incr i} {
10697 set a [lindex $tags $i]
10698 for {set j 0} {$j < $i} {incr j} {
10699 set b [lindex $tags $j]
10700 set r [anc_or_desc $a $b]
10701 if {$r == 1} {
10702 set tags [lreplace $tags $j $j]
10703 incr j -1
10704 incr i -1
10705 } elseif {$r == -1} {
10706 set tags [lreplace $tags $i $i]
10707 incr i -1
10708 break
10713 if {[array names growing] ne {}} {
10714 # graph isn't finished, need to check if any tag could get
10715 # eclipsed by another tag coming later. Simply ignore any
10716 # tags that could later get eclipsed.
10717 set ctags {}
10718 foreach t $tags {
10719 if {[is_certain $t $origid]} {
10720 lappend ctags $t
10723 if {$tags eq $ctags} {
10724 set cached_dtags($origid) $tags
10725 } else {
10726 set tags $ctags
10728 } else {
10729 set cached_dtags($origid) $tags
10731 set t3 [clock clicks -milliseconds]
10732 if {0 && $t3 - $t1 >= 100} {
10733 puts "iterating descendents ($loopix/[llength $todo] nodes) took\
10734 [expr {$t2-$t1}]+[expr {$t3-$t2}]ms, $nc candidates left"
10736 return $tags
10739 proc anctags {id} {
10740 global arcnos arcids arcout arcend arctags idtags allparents
10741 global growing cached_atags
10743 if {![info exists allparents($id)]} {
10744 return {}
10746 set t1 [clock clicks -milliseconds]
10747 set argid $id
10748 if {[llength $arcnos($id)] == 1 && [llength $allparents($id)] == 1} {
10749 # part-way along an arc; check that arc first
10750 set a [lindex $arcnos($id) 0]
10751 if {$arctags($a) ne {}} {
10752 validate_arctags $a
10753 set i [lsearch -exact $arcids($a) $id]
10754 foreach t $arctags($a) {
10755 set j [lsearch -exact $arcids($a) $t]
10756 if {$j > $i} {
10757 return $t
10761 if {![info exists arcend($a)]} {
10762 return {}
10764 set id $arcend($a)
10765 if {[info exists idtags($id)]} {
10766 return $id
10769 if {[info exists cached_atags($id)]} {
10770 return $cached_atags($id)
10773 set origid $id
10774 set todo [list $id]
10775 set queued($id) 1
10776 set taglist {}
10777 set nc 1
10778 for {set i 0} {$i < [llength $todo] && $nc > 0} {incr i} {
10779 set id [lindex $todo $i]
10780 set done($id) 1
10781 set td [info exists hastaggeddescendent($id)]
10782 if {!$td} {
10783 incr nc -1
10785 # ignore tags on starting node
10786 if {!$td && $i > 0} {
10787 if {[info exists idtags($id)]} {
10788 set tagloc($id) $id
10789 set td 1
10790 } elseif {[info exists cached_atags($id)]} {
10791 set tagloc($id) $cached_atags($id)
10792 set td 1
10795 foreach a $arcout($id) {
10796 if {!$td && $arctags($a) ne {}} {
10797 validate_arctags $a
10798 if {$arctags($a) ne {}} {
10799 lappend tagloc($id) [lindex $arctags($a) 0]
10802 if {![info exists arcend($a)]} continue
10803 set d $arcend($a)
10804 if {$td || $arctags($a) ne {}} {
10805 set tomark [list $d]
10806 for {set j 0} {$j < [llength $tomark]} {incr j} {
10807 set dd [lindex $tomark $j]
10808 if {![info exists hastaggeddescendent($dd)]} {
10809 if {[info exists done($dd)]} {
10810 foreach b $arcout($dd) {
10811 if {[info exists arcend($b)]} {
10812 lappend tomark $arcend($b)
10815 if {[info exists tagloc($dd)]} {
10816 unset tagloc($dd)
10818 } elseif {[info exists queued($dd)]} {
10819 incr nc -1
10821 set hastaggeddescendent($dd) 1
10825 if {![info exists queued($d)]} {
10826 lappend todo $d
10827 set queued($d) 1
10828 if {![info exists hastaggeddescendent($d)]} {
10829 incr nc
10834 set t2 [clock clicks -milliseconds]
10835 set loopix $i
10836 set tags {}
10837 foreach id [array names tagloc] {
10838 if {![info exists hastaggeddescendent($id)]} {
10839 foreach t $tagloc($id) {
10840 if {[lsearch -exact $tags $t] < 0} {
10841 lappend tags $t
10847 # remove tags that are ancestors of other tags
10848 for {set i 0} {$i < [llength $tags]} {incr i} {
10849 set a [lindex $tags $i]
10850 for {set j 0} {$j < $i} {incr j} {
10851 set b [lindex $tags $j]
10852 set r [anc_or_desc $a $b]
10853 if {$r == -1} {
10854 set tags [lreplace $tags $j $j]
10855 incr j -1
10856 incr i -1
10857 } elseif {$r == 1} {
10858 set tags [lreplace $tags $i $i]
10859 incr i -1
10860 break
10865 if {[array names growing] ne {}} {
10866 # graph isn't finished, need to check if any tag could get
10867 # eclipsed by another tag coming later. Simply ignore any
10868 # tags that could later get eclipsed.
10869 set ctags {}
10870 foreach t $tags {
10871 if {[is_certain $origid $t]} {
10872 lappend ctags $t
10875 if {$tags eq $ctags} {
10876 set cached_atags($origid) $tags
10877 } else {
10878 set tags $ctags
10880 } else {
10881 set cached_atags($origid) $tags
10883 set t3 [clock clicks -milliseconds]
10884 if {0 && $t3 - $t1 >= 100} {
10885 puts "iterating ancestors ($loopix/[llength $todo] nodes) took\
10886 [expr {$t2-$t1}]+[expr {$t3-$t2}]ms, $nc candidates left"
10888 return $tags
10891 # Return the list of IDs that have heads that are descendents of id,
10892 # including id itself if it has a head.
10893 proc descheads {id} {
10894 global arcnos arcstart arcids archeads idheads cached_dheads
10895 global allparents arcout
10897 if {![info exists allparents($id)]} {
10898 return {}
10900 set aret {}
10901 if {![info exists arcout($id)]} {
10902 # part-way along an arc; check it first
10903 set a [lindex $arcnos($id) 0]
10904 if {$archeads($a) ne {}} {
10905 validate_archeads $a
10906 set i [lsearch -exact $arcids($a) $id]
10907 foreach t $archeads($a) {
10908 set j [lsearch -exact $arcids($a) $t]
10909 if {$j > $i} break
10910 lappend aret $t
10913 set id $arcstart($a)
10915 set origid $id
10916 set todo [list $id]
10917 set seen($id) 1
10918 set ret {}
10919 for {set i 0} {$i < [llength $todo]} {incr i} {
10920 set id [lindex $todo $i]
10921 if {[info exists cached_dheads($id)]} {
10922 set ret [concat $ret $cached_dheads($id)]
10923 } else {
10924 if {[info exists idheads($id)]} {
10925 lappend ret $id
10927 foreach a $arcnos($id) {
10928 if {$archeads($a) ne {}} {
10929 validate_archeads $a
10930 if {$archeads($a) ne {}} {
10931 set ret [concat $ret $archeads($a)]
10934 set d $arcstart($a)
10935 if {![info exists seen($d)]} {
10936 lappend todo $d
10937 set seen($d) 1
10942 set ret [lsort -unique $ret]
10943 set cached_dheads($origid) $ret
10944 return [concat $ret $aret]
10947 proc addedtag {id} {
10948 global arcnos arcout cached_dtags cached_atags
10950 if {![info exists arcnos($id)]} return
10951 if {![info exists arcout($id)]} {
10952 recalcarc [lindex $arcnos($id) 0]
10954 catch {unset cached_dtags}
10955 catch {unset cached_atags}
10958 proc addedhead {hid head} {
10959 global arcnos arcout cached_dheads
10961 if {![info exists arcnos($hid)]} return
10962 if {![info exists arcout($hid)]} {
10963 recalcarc [lindex $arcnos($hid) 0]
10965 catch {unset cached_dheads}
10968 proc removedhead {hid head} {
10969 global cached_dheads
10971 catch {unset cached_dheads}
10974 proc movedhead {hid head} {
10975 global arcnos arcout cached_dheads
10977 if {![info exists arcnos($hid)]} return
10978 if {![info exists arcout($hid)]} {
10979 recalcarc [lindex $arcnos($hid) 0]
10981 catch {unset cached_dheads}
10984 proc changedrefs {} {
10985 global cached_dheads cached_dtags cached_atags cached_tagcontent
10986 global arctags archeads arcnos arcout idheads idtags
10988 foreach id [concat [array names idheads] [array names idtags]] {
10989 if {[info exists arcnos($id)] && ![info exists arcout($id)]} {
10990 set a [lindex $arcnos($id) 0]
10991 if {![info exists donearc($a)]} {
10992 recalcarc $a
10993 set donearc($a) 1
10997 catch {unset cached_tagcontent}
10998 catch {unset cached_dtags}
10999 catch {unset cached_atags}
11000 catch {unset cached_dheads}
11003 proc rereadrefs {} {
11004 global idtags idheads idotherrefs mainheadid
11006 set refids [concat [array names idtags] \
11007 [array names idheads] [array names idotherrefs]]
11008 foreach id $refids {
11009 if {![info exists ref($id)]} {
11010 set ref($id) [listrefs $id]
11013 set oldmainhead $mainheadid
11014 readrefs
11015 changedrefs
11016 set refids [lsort -unique [concat $refids [array names idtags] \
11017 [array names idheads] [array names idotherrefs]]]
11018 foreach id $refids {
11019 set v [listrefs $id]
11020 if {![info exists ref($id)] || $ref($id) != $v} {
11021 redrawtags $id
11024 if {$oldmainhead ne $mainheadid} {
11025 redrawtags $oldmainhead
11026 redrawtags $mainheadid
11028 run refill_reflist
11031 proc listrefs {id} {
11032 global idtags idheads idotherrefs
11034 set x {}
11035 if {[info exists idtags($id)]} {
11036 set x $idtags($id)
11038 set y {}
11039 if {[info exists idheads($id)]} {
11040 set y $idheads($id)
11042 set z {}
11043 if {[info exists idotherrefs($id)]} {
11044 set z $idotherrefs($id)
11046 return [list $x $y $z]
11049 proc add_tag_ctext {tag} {
11050 global ctext cached_tagcontent tagids
11052 if {![info exists cached_tagcontent($tag)]} {
11053 catch {
11054 set cached_tagcontent($tag) [exec git cat-file -p $tag]
11057 $ctext insert end "[mc "Tag"]: $tag\n" bold
11058 if {[info exists cached_tagcontent($tag)]} {
11059 set text $cached_tagcontent($tag)
11060 } else {
11061 set text "[mc "Id"]: $tagids($tag)"
11063 appendwithlinks $text {}
11066 proc showtag {tag isnew} {
11067 global ctext cached_tagcontent tagids linknum tagobjid
11069 if {$isnew} {
11070 addtohistory [list showtag $tag 0] savectextpos
11072 $ctext conf -state normal
11073 clear_ctext
11074 settabs 0
11075 set linknum 0
11076 add_tag_ctext $tag
11077 maybe_scroll_ctext 1
11078 $ctext conf -state disabled
11079 init_flist {}
11082 proc showtags {id isnew} {
11083 global idtags ctext linknum
11085 if {$isnew} {
11086 addtohistory [list showtags $id 0] savectextpos
11088 $ctext conf -state normal
11089 clear_ctext
11090 settabs 0
11091 set linknum 0
11092 set sep {}
11093 foreach tag $idtags($id) {
11094 $ctext insert end $sep
11095 add_tag_ctext $tag
11096 set sep "\n\n"
11098 maybe_scroll_ctext 1
11099 $ctext conf -state disabled
11100 init_flist {}
11103 proc doquit {} {
11104 global stopped
11105 global gitktmpdir
11107 set stopped 100
11108 savestuff .
11109 destroy .
11111 if {[info exists gitktmpdir]} {
11112 catch {file delete -force $gitktmpdir}
11116 proc mkfontdisp {font top which} {
11117 global fontattr fontpref $font NS use_ttk
11119 set fontpref($font) [set $font]
11120 ${NS}::button $top.${font}but -text $which \
11121 -command [list choosefont $font $which]
11122 ${NS}::label $top.$font -relief flat -font $font \
11123 -text $fontattr($font,family) -justify left
11124 grid x $top.${font}but $top.$font -sticky w
11127 proc choosefont {font which} {
11128 global fontparam fontlist fonttop fontattr
11129 global prefstop NS
11131 set fontparam(which) $which
11132 set fontparam(font) $font
11133 set fontparam(family) [font actual $font -family]
11134 set fontparam(size) $fontattr($font,size)
11135 set fontparam(weight) $fontattr($font,weight)
11136 set fontparam(slant) $fontattr($font,slant)
11137 set top .gitkfont
11138 set fonttop $top
11139 if {![winfo exists $top]} {
11140 font create sample
11141 eval font config sample [font actual $font]
11142 ttk_toplevel $top
11143 make_transient $top $prefstop
11144 wm title $top [mc "Gitk font chooser"]
11145 ${NS}::label $top.l -textvariable fontparam(which)
11146 pack $top.l -side top
11147 set fontlist [lsort [font families]]
11148 ${NS}::frame $top.f
11149 listbox $top.f.fam -listvariable fontlist \
11150 -yscrollcommand [list $top.f.sb set]
11151 bind $top.f.fam <<ListboxSelect>> selfontfam
11152 ${NS}::scrollbar $top.f.sb -command [list $top.f.fam yview]
11153 pack $top.f.sb -side right -fill y
11154 pack $top.f.fam -side left -fill both -expand 1
11155 pack $top.f -side top -fill both -expand 1
11156 ${NS}::frame $top.g
11157 spinbox $top.g.size -from 4 -to 40 -width 4 \
11158 -textvariable fontparam(size) \
11159 -validatecommand {string is integer -strict %s}
11160 checkbutton $top.g.bold -padx 5 \
11161 -font {{Times New Roman} 12 bold} -text [mc "B"] -indicatoron 0 \
11162 -variable fontparam(weight) -onvalue bold -offvalue normal
11163 checkbutton $top.g.ital -padx 5 \
11164 -font {{Times New Roman} 12 italic} -text [mc "I"] -indicatoron 0 \
11165 -variable fontparam(slant) -onvalue italic -offvalue roman
11166 pack $top.g.size $top.g.bold $top.g.ital -side left
11167 pack $top.g -side top
11168 canvas $top.c -width 150 -height 50 -border 2 -relief sunk \
11169 -background white
11170 $top.c create text 100 25 -anchor center -text $which -font sample \
11171 -fill black -tags text
11172 bind $top.c <Configure> [list centertext $top.c]
11173 pack $top.c -side top -fill x
11174 ${NS}::frame $top.buts
11175 ${NS}::button $top.buts.ok -text [mc "OK"] -command fontok -default active
11176 ${NS}::button $top.buts.can -text [mc "Cancel"] -command fontcan -default normal
11177 bind $top <Key-Return> fontok
11178 bind $top <Key-Escape> fontcan
11179 grid $top.buts.ok $top.buts.can
11180 grid columnconfigure $top.buts 0 -weight 1 -uniform a
11181 grid columnconfigure $top.buts 1 -weight 1 -uniform a
11182 pack $top.buts -side bottom -fill x
11183 trace add variable fontparam write chg_fontparam
11184 } else {
11185 raise $top
11186 $top.c itemconf text -text $which
11188 set i [lsearch -exact $fontlist $fontparam(family)]
11189 if {$i >= 0} {
11190 $top.f.fam selection set $i
11191 $top.f.fam see $i
11195 proc centertext {w} {
11196 $w coords text [expr {[winfo width $w] / 2}] [expr {[winfo height $w] / 2}]
11199 proc fontok {} {
11200 global fontparam fontpref prefstop
11202 set f $fontparam(font)
11203 set fontpref($f) [list $fontparam(family) $fontparam(size)]
11204 if {$fontparam(weight) eq "bold"} {
11205 lappend fontpref($f) "bold"
11207 if {$fontparam(slant) eq "italic"} {
11208 lappend fontpref($f) "italic"
11210 set w $prefstop.notebook.fonts.$f
11211 $w conf -text $fontparam(family) -font $fontpref($f)
11213 fontcan
11216 proc fontcan {} {
11217 global fonttop fontparam
11219 if {[info exists fonttop]} {
11220 catch {destroy $fonttop}
11221 catch {font delete sample}
11222 unset fonttop
11223 unset fontparam
11227 if {[package vsatisfies [package provide Tk] 8.6]} {
11228 # In Tk 8.6 we have a native font chooser dialog. Overwrite the above
11229 # function to make use of it.
11230 proc choosefont {font which} {
11231 tk fontchooser configure -title $which -font $font \
11232 -command [list on_choosefont $font $which]
11233 tk fontchooser show
11235 proc on_choosefont {font which newfont} {
11236 global fontparam
11237 puts stderr "$font $newfont"
11238 array set f [font actual $newfont]
11239 set fontparam(which) $which
11240 set fontparam(font) $font
11241 set fontparam(family) $f(-family)
11242 set fontparam(size) $f(-size)
11243 set fontparam(weight) $f(-weight)
11244 set fontparam(slant) $f(-slant)
11245 fontok
11249 proc selfontfam {} {
11250 global fonttop fontparam
11252 set i [$fonttop.f.fam curselection]
11253 if {$i ne {}} {
11254 set fontparam(family) [$fonttop.f.fam get $i]
11258 proc chg_fontparam {v sub op} {
11259 global fontparam
11261 font config sample -$sub $fontparam($sub)
11264 # Create a property sheet tab page
11265 proc create_prefs_page {w} {
11266 global NS
11267 set parent [join [lrange [split $w .] 0 end-1] .]
11268 if {[winfo class $parent] eq "TNotebook"} {
11269 ${NS}::frame $w
11270 } else {
11271 ${NS}::labelframe $w
11275 proc prefspage_general {notebook} {
11276 global NS maxwidth maxgraphpct showneartags showlocalchanges
11277 global tabstop limitdiffs autoselect autosellen extdifftool perfile_attrs
11278 global hideremotes want_ttk have_ttk maxrefs
11280 set page [create_prefs_page $notebook.general]
11282 ${NS}::label $page.ldisp -text [mc "Commit list display options"]
11283 grid $page.ldisp - -sticky w -pady 10
11284 ${NS}::label $page.spacer -text " "
11285 ${NS}::label $page.maxwidthl -text [mc "Maximum graph width (lines)"]
11286 spinbox $page.maxwidth -from 0 -to 100 -width 4 -textvariable maxwidth
11287 grid $page.spacer $page.maxwidthl $page.maxwidth -sticky w
11288 ${NS}::label $page.maxpctl -text [mc "Maximum graph width (% of pane)"]
11289 spinbox $page.maxpct -from 1 -to 100 -width 4 -textvariable maxgraphpct
11290 grid x $page.maxpctl $page.maxpct -sticky w
11291 ${NS}::checkbutton $page.showlocal -text [mc "Show local changes"] \
11292 -variable showlocalchanges
11293 grid x $page.showlocal -sticky w
11294 ${NS}::checkbutton $page.autoselect -text [mc "Auto-select SHA1 (length)"] \
11295 -variable autoselect
11296 spinbox $page.autosellen -from 1 -to 40 -width 4 -textvariable autosellen
11297 grid x $page.autoselect $page.autosellen -sticky w
11298 ${NS}::checkbutton $page.hideremotes -text [mc "Hide remote refs"] \
11299 -variable hideremotes
11300 grid x $page.hideremotes -sticky w
11302 ${NS}::label $page.ddisp -text [mc "Diff display options"]
11303 grid $page.ddisp - -sticky w -pady 10
11304 ${NS}::label $page.tabstopl -text [mc "Tab spacing"]
11305 spinbox $page.tabstop -from 1 -to 20 -width 4 -textvariable tabstop
11306 grid x $page.tabstopl $page.tabstop -sticky w
11307 ${NS}::checkbutton $page.ntag -text [mc "Display nearby tags/heads"] \
11308 -variable showneartags
11309 grid x $page.ntag -sticky w
11310 ${NS}::label $page.maxrefsl -text [mc "Maximum # tags/heads to show"]
11311 spinbox $page.maxrefs -from 1 -to 1000 -width 4 -textvariable maxrefs
11312 grid x $page.maxrefsl $page.maxrefs -sticky w
11313 ${NS}::checkbutton $page.ldiff -text [mc "Limit diffs to listed paths"] \
11314 -variable limitdiffs
11315 grid x $page.ldiff -sticky w
11316 ${NS}::checkbutton $page.lattr -text [mc "Support per-file encodings"] \
11317 -variable perfile_attrs
11318 grid x $page.lattr -sticky w
11320 ${NS}::entry $page.extdifft -textvariable extdifftool
11321 ${NS}::frame $page.extdifff
11322 ${NS}::label $page.extdifff.l -text [mc "External diff tool" ]
11323 ${NS}::button $page.extdifff.b -text [mc "Choose..."] -command choose_extdiff
11324 pack $page.extdifff.l $page.extdifff.b -side left
11325 pack configure $page.extdifff.l -padx 10
11326 grid x $page.extdifff $page.extdifft -sticky ew
11328 ${NS}::label $page.lgen -text [mc "General options"]
11329 grid $page.lgen - -sticky w -pady 10
11330 ${NS}::checkbutton $page.want_ttk -variable want_ttk \
11331 -text [mc "Use themed widgets"]
11332 if {$have_ttk} {
11333 ${NS}::label $page.ttk_note -text [mc "(change requires restart)"]
11334 } else {
11335 ${NS}::label $page.ttk_note -text [mc "(currently unavailable)"]
11337 grid x $page.want_ttk $page.ttk_note -sticky w
11338 return $page
11341 proc prefspage_colors {notebook} {
11342 global NS uicolor bgcolor fgcolor ctext diffcolors selectbgcolor markbgcolor
11344 set page [create_prefs_page $notebook.colors]
11346 ${NS}::label $page.cdisp -text [mc "Colors: press to choose"]
11347 grid $page.cdisp - -sticky w -pady 10
11348 label $page.ui -padx 40 -relief sunk -background $uicolor
11349 ${NS}::button $page.uibut -text [mc "Interface"] \
11350 -command [list choosecolor uicolor {} $page.ui [mc "interface"] setui]
11351 grid x $page.uibut $page.ui -sticky w
11352 label $page.bg -padx 40 -relief sunk -background $bgcolor
11353 ${NS}::button $page.bgbut -text [mc "Background"] \
11354 -command [list choosecolor bgcolor {} $page.bg [mc "background"] setbg]
11355 grid x $page.bgbut $page.bg -sticky w
11356 label $page.fg -padx 40 -relief sunk -background $fgcolor
11357 ${NS}::button $page.fgbut -text [mc "Foreground"] \
11358 -command [list choosecolor fgcolor {} $page.fg [mc "foreground"] setfg]
11359 grid x $page.fgbut $page.fg -sticky w
11360 label $page.diffold -padx 40 -relief sunk -background [lindex $diffcolors 0]
11361 ${NS}::button $page.diffoldbut -text [mc "Diff: old lines"] \
11362 -command [list choosecolor diffcolors 0 $page.diffold [mc "diff old lines"] \
11363 [list $ctext tag conf d0 -foreground]]
11364 grid x $page.diffoldbut $page.diffold -sticky w
11365 label $page.diffnew -padx 40 -relief sunk -background [lindex $diffcolors 1]
11366 ${NS}::button $page.diffnewbut -text [mc "Diff: new lines"] \
11367 -command [list choosecolor diffcolors 1 $page.diffnew [mc "diff new lines"] \
11368 [list $ctext tag conf dresult -foreground]]
11369 grid x $page.diffnewbut $page.diffnew -sticky w
11370 label $page.hunksep -padx 40 -relief sunk -background [lindex $diffcolors 2]
11371 ${NS}::button $page.hunksepbut -text [mc "Diff: hunk header"] \
11372 -command [list choosecolor diffcolors 2 $page.hunksep \
11373 [mc "diff hunk header"] \
11374 [list $ctext tag conf hunksep -foreground]]
11375 grid x $page.hunksepbut $page.hunksep -sticky w
11376 label $page.markbgsep -padx 40 -relief sunk -background $markbgcolor
11377 ${NS}::button $page.markbgbut -text [mc "Marked line bg"] \
11378 -command [list choosecolor markbgcolor {} $page.markbgsep \
11379 [mc "marked line background"] \
11380 [list $ctext tag conf omark -background]]
11381 grid x $page.markbgbut $page.markbgsep -sticky w
11382 label $page.selbgsep -padx 40 -relief sunk -background $selectbgcolor
11383 ${NS}::button $page.selbgbut -text [mc "Select bg"] \
11384 -command [list choosecolor selectbgcolor {} $page.selbgsep [mc "background"] setselbg]
11385 grid x $page.selbgbut $page.selbgsep -sticky w
11386 return $page
11389 proc prefspage_fonts {notebook} {
11390 global NS
11391 set page [create_prefs_page $notebook.fonts]
11392 ${NS}::label $page.cfont -text [mc "Fonts: press to choose"]
11393 grid $page.cfont - -sticky w -pady 10
11394 mkfontdisp mainfont $page [mc "Main font"]
11395 mkfontdisp textfont $page [mc "Diff display font"]
11396 mkfontdisp uifont $page [mc "User interface font"]
11397 return $page
11400 proc doprefs {} {
11401 global maxwidth maxgraphpct use_ttk NS
11402 global oldprefs prefstop showneartags showlocalchanges
11403 global uicolor bgcolor fgcolor ctext diffcolors selectbgcolor markbgcolor
11404 global tabstop limitdiffs autoselect autosellen extdifftool perfile_attrs
11405 global hideremotes want_ttk have_ttk
11407 set top .gitkprefs
11408 set prefstop $top
11409 if {[winfo exists $top]} {
11410 raise $top
11411 return
11413 foreach v {maxwidth maxgraphpct showneartags showlocalchanges \
11414 limitdiffs tabstop perfile_attrs hideremotes want_ttk} {
11415 set oldprefs($v) [set $v]
11417 ttk_toplevel $top
11418 wm title $top [mc "Gitk preferences"]
11419 make_transient $top .
11421 if {[set use_notebook [expr {$use_ttk && [info command ::ttk::notebook] ne ""}]]} {
11422 set notebook [ttk::notebook $top.notebook]
11423 } else {
11424 set notebook [${NS}::frame $top.notebook -borderwidth 0 -relief flat]
11427 lappend pages [prefspage_general $notebook] [mc "General"]
11428 lappend pages [prefspage_colors $notebook] [mc "Colors"]
11429 lappend pages [prefspage_fonts $notebook] [mc "Fonts"]
11430 set col 0
11431 foreach {page title} $pages {
11432 if {$use_notebook} {
11433 $notebook add $page -text $title
11434 } else {
11435 set btn [${NS}::button $notebook.b_[string map {. X} $page] \
11436 -text $title -command [list raise $page]]
11437 $page configure -text $title
11438 grid $btn -row 0 -column [incr col] -sticky w
11439 grid $page -row 1 -column 0 -sticky news -columnspan 100
11443 if {!$use_notebook} {
11444 grid columnconfigure $notebook 0 -weight 1
11445 grid rowconfigure $notebook 1 -weight 1
11446 raise [lindex $pages 0]
11449 grid $notebook -sticky news -padx 2 -pady 2
11450 grid rowconfigure $top 0 -weight 1
11451 grid columnconfigure $top 0 -weight 1
11453 ${NS}::frame $top.buts
11454 ${NS}::button $top.buts.ok -text [mc "OK"] -command prefsok -default active
11455 ${NS}::button $top.buts.can -text [mc "Cancel"] -command prefscan -default normal
11456 bind $top <Key-Return> prefsok
11457 bind $top <Key-Escape> prefscan
11458 grid $top.buts.ok $top.buts.can
11459 grid columnconfigure $top.buts 0 -weight 1 -uniform a
11460 grid columnconfigure $top.buts 1 -weight 1 -uniform a
11461 grid $top.buts - - -pady 10 -sticky ew
11462 grid columnconfigure $top 2 -weight 1
11463 bind $top <Visibility> [list focus $top.buts.ok]
11466 proc choose_extdiff {} {
11467 global extdifftool
11469 set prog [tk_getOpenFile -title [mc "External diff tool"] -multiple false]
11470 if {$prog ne {}} {
11471 set extdifftool $prog
11475 proc choosecolor {v vi w x cmd} {
11476 global $v
11478 set c [tk_chooseColor -initialcolor [lindex [set $v] $vi] \
11479 -title [mc "Gitk: choose color for %s" $x]]
11480 if {$c eq {}} return
11481 $w conf -background $c
11482 lset $v $vi $c
11483 eval $cmd $c
11486 proc setselbg {c} {
11487 global bglist cflist
11488 foreach w $bglist {
11489 $w configure -selectbackground $c
11491 $cflist tag configure highlight \
11492 -background [$cflist cget -selectbackground]
11493 allcanvs itemconf secsel -fill $c
11496 # This sets the background color and the color scheme for the whole UI.
11497 # For some reason, tk_setPalette chooses a nasty dark red for selectColor
11498 # if we don't specify one ourselves, which makes the checkbuttons and
11499 # radiobuttons look bad. This chooses white for selectColor if the
11500 # background color is light, or black if it is dark.
11501 proc setui {c} {
11502 if {[tk windowingsystem] eq "win32"} { return }
11503 set bg [winfo rgb . $c]
11504 set selc black
11505 if {[lindex $bg 0] + 1.5 * [lindex $bg 1] + 0.5 * [lindex $bg 2] > 100000} {
11506 set selc white
11508 tk_setPalette background $c selectColor $selc
11511 proc setbg {c} {
11512 global bglist
11514 foreach w $bglist {
11515 $w conf -background $c
11519 proc setfg {c} {
11520 global fglist canv
11522 foreach w $fglist {
11523 $w conf -foreground $c
11525 allcanvs itemconf text -fill $c
11526 $canv itemconf circle -outline $c
11527 $canv itemconf markid -outline $c
11530 proc prefscan {} {
11531 global oldprefs prefstop
11533 foreach v {maxwidth maxgraphpct showneartags showlocalchanges \
11534 limitdiffs tabstop perfile_attrs hideremotes want_ttk} {
11535 global $v
11536 set $v $oldprefs($v)
11538 catch {destroy $prefstop}
11539 unset prefstop
11540 fontcan
11543 proc prefsok {} {
11544 global maxwidth maxgraphpct
11545 global oldprefs prefstop showneartags showlocalchanges
11546 global fontpref mainfont textfont uifont
11547 global limitdiffs treediffs perfile_attrs
11548 global hideremotes
11550 catch {destroy $prefstop}
11551 unset prefstop
11552 fontcan
11553 set fontchanged 0
11554 if {$mainfont ne $fontpref(mainfont)} {
11555 set mainfont $fontpref(mainfont)
11556 parsefont mainfont $mainfont
11557 eval font configure mainfont [fontflags mainfont]
11558 eval font configure mainfontbold [fontflags mainfont 1]
11559 setcoords
11560 set fontchanged 1
11562 if {$textfont ne $fontpref(textfont)} {
11563 set textfont $fontpref(textfont)
11564 parsefont textfont $textfont
11565 eval font configure textfont [fontflags textfont]
11566 eval font configure textfontbold [fontflags textfont 1]
11568 if {$uifont ne $fontpref(uifont)} {
11569 set uifont $fontpref(uifont)
11570 parsefont uifont $uifont
11571 eval font configure uifont [fontflags uifont]
11573 settabs
11574 if {$showlocalchanges != $oldprefs(showlocalchanges)} {
11575 if {$showlocalchanges} {
11576 doshowlocalchanges
11577 } else {
11578 dohidelocalchanges
11581 if {$limitdiffs != $oldprefs(limitdiffs) ||
11582 ($perfile_attrs && !$oldprefs(perfile_attrs))} {
11583 # treediffs elements are limited by path;
11584 # won't have encodings cached if perfile_attrs was just turned on
11585 catch {unset treediffs}
11587 if {$fontchanged || $maxwidth != $oldprefs(maxwidth)
11588 || $maxgraphpct != $oldprefs(maxgraphpct)} {
11589 redisplay
11590 } elseif {$showneartags != $oldprefs(showneartags) ||
11591 $limitdiffs != $oldprefs(limitdiffs)} {
11592 reselectline
11594 if {$hideremotes != $oldprefs(hideremotes)} {
11595 rereadrefs
11599 proc formatdate {d} {
11600 global datetimeformat
11601 if {$d ne {}} {
11602 # If $datetimeformat includes a timezone, display in the
11603 # timezone of the argument. Otherwise, display in local time.
11604 if {[string match {*%[zZ]*} $datetimeformat]} {
11605 if {[catch {set d [clock format [lindex $d 0] -timezone [lindex $d 1] -format $datetimeformat]}]} {
11606 # Tcl < 8.5 does not support -timezone. Emulate it by
11607 # setting TZ (e.g. TZ=<-0430>+04:30).
11608 global env
11609 if {[info exists env(TZ)]} {
11610 set savedTZ $env(TZ)
11612 set zone [lindex $d 1]
11613 set sign [string map {+ - - +} [string index $zone 0]]
11614 set env(TZ) <$zone>$sign[string range $zone 1 2]:[string range $zone 3 4]
11615 set d [clock format [lindex $d 0] -format $datetimeformat]
11616 if {[info exists savedTZ]} {
11617 set env(TZ) $savedTZ
11618 } else {
11619 unset env(TZ)
11622 } else {
11623 set d [clock format [lindex $d 0] -format $datetimeformat]
11626 return $d
11629 # This list of encoding names and aliases is distilled from
11630 # http://www.iana.org/assignments/character-sets.
11631 # Not all of them are supported by Tcl.
11632 set encoding_aliases {
11633 { ANSI_X3.4-1968 iso-ir-6 ANSI_X3.4-1986 ISO_646.irv:1991 ASCII
11634 ISO646-US US-ASCII us IBM367 cp367 csASCII }
11635 { ISO-10646-UTF-1 csISO10646UTF1 }
11636 { ISO_646.basic:1983 ref csISO646basic1983 }
11637 { INVARIANT csINVARIANT }
11638 { ISO_646.irv:1983 iso-ir-2 irv csISO2IntlRefVersion }
11639 { BS_4730 iso-ir-4 ISO646-GB gb uk csISO4UnitedKingdom }
11640 { NATS-SEFI iso-ir-8-1 csNATSSEFI }
11641 { NATS-SEFI-ADD iso-ir-8-2 csNATSSEFIADD }
11642 { NATS-DANO iso-ir-9-1 csNATSDANO }
11643 { NATS-DANO-ADD iso-ir-9-2 csNATSDANOADD }
11644 { SEN_850200_B iso-ir-10 FI ISO646-FI ISO646-SE se csISO10Swedish }
11645 { SEN_850200_C iso-ir-11 ISO646-SE2 se2 csISO11SwedishForNames }
11646 { KS_C_5601-1987 iso-ir-149 KS_C_5601-1989 KSC_5601 korean csKSC56011987 }
11647 { ISO-2022-KR csISO2022KR }
11648 { EUC-KR csEUCKR }
11649 { ISO-2022-JP csISO2022JP }
11650 { ISO-2022-JP-2 csISO2022JP2 }
11651 { JIS_C6220-1969-jp JIS_C6220-1969 iso-ir-13 katakana x0201-7
11652 csISO13JISC6220jp }
11653 { JIS_C6220-1969-ro iso-ir-14 jp ISO646-JP csISO14JISC6220ro }
11654 { IT iso-ir-15 ISO646-IT csISO15Italian }
11655 { PT iso-ir-16 ISO646-PT csISO16Portuguese }
11656 { ES iso-ir-17 ISO646-ES csISO17Spanish }
11657 { greek7-old iso-ir-18 csISO18Greek7Old }
11658 { latin-greek iso-ir-19 csISO19LatinGreek }
11659 { DIN_66003 iso-ir-21 de ISO646-DE csISO21German }
11660 { NF_Z_62-010_(1973) iso-ir-25 ISO646-FR1 csISO25French }
11661 { Latin-greek-1 iso-ir-27 csISO27LatinGreek1 }
11662 { ISO_5427 iso-ir-37 csISO5427Cyrillic }
11663 { JIS_C6226-1978 iso-ir-42 csISO42JISC62261978 }
11664 { BS_viewdata iso-ir-47 csISO47BSViewdata }
11665 { INIS iso-ir-49 csISO49INIS }
11666 { INIS-8 iso-ir-50 csISO50INIS8 }
11667 { INIS-cyrillic iso-ir-51 csISO51INISCyrillic }
11668 { ISO_5427:1981 iso-ir-54 ISO5427Cyrillic1981 }
11669 { ISO_5428:1980 iso-ir-55 csISO5428Greek }
11670 { GB_1988-80 iso-ir-57 cn ISO646-CN csISO57GB1988 }
11671 { GB_2312-80 iso-ir-58 chinese csISO58GB231280 }
11672 { NS_4551-1 iso-ir-60 ISO646-NO no csISO60DanishNorwegian
11673 csISO60Norwegian1 }
11674 { NS_4551-2 ISO646-NO2 iso-ir-61 no2 csISO61Norwegian2 }
11675 { NF_Z_62-010 iso-ir-69 ISO646-FR fr csISO69French }
11676 { videotex-suppl iso-ir-70 csISO70VideotexSupp1 }
11677 { PT2 iso-ir-84 ISO646-PT2 csISO84Portuguese2 }
11678 { ES2 iso-ir-85 ISO646-ES2 csISO85Spanish2 }
11679 { MSZ_7795.3 iso-ir-86 ISO646-HU hu csISO86Hungarian }
11680 { JIS_C6226-1983 iso-ir-87 x0208 JIS_X0208-1983 csISO87JISX0208 }
11681 { greek7 iso-ir-88 csISO88Greek7 }
11682 { ASMO_449 ISO_9036 arabic7 iso-ir-89 csISO89ASMO449 }
11683 { iso-ir-90 csISO90 }
11684 { JIS_C6229-1984-a iso-ir-91 jp-ocr-a csISO91JISC62291984a }
11685 { JIS_C6229-1984-b iso-ir-92 ISO646-JP-OCR-B jp-ocr-b
11686 csISO92JISC62991984b }
11687 { JIS_C6229-1984-b-add iso-ir-93 jp-ocr-b-add csISO93JIS62291984badd }
11688 { JIS_C6229-1984-hand iso-ir-94 jp-ocr-hand csISO94JIS62291984hand }
11689 { JIS_C6229-1984-hand-add iso-ir-95 jp-ocr-hand-add
11690 csISO95JIS62291984handadd }
11691 { JIS_C6229-1984-kana iso-ir-96 csISO96JISC62291984kana }
11692 { ISO_2033-1983 iso-ir-98 e13b csISO2033 }
11693 { ANSI_X3.110-1983 iso-ir-99 CSA_T500-1983 NAPLPS csISO99NAPLPS }
11694 { ISO_8859-1:1987 iso-ir-100 ISO_8859-1 ISO-8859-1 latin1 l1 IBM819
11695 CP819 csISOLatin1 }
11696 { ISO_8859-2:1987 iso-ir-101 ISO_8859-2 ISO-8859-2 latin2 l2 csISOLatin2 }
11697 { T.61-7bit iso-ir-102 csISO102T617bit }
11698 { T.61-8bit T.61 iso-ir-103 csISO103T618bit }
11699 { ISO_8859-3:1988 iso-ir-109 ISO_8859-3 ISO-8859-3 latin3 l3 csISOLatin3 }
11700 { ISO_8859-4:1988 iso-ir-110 ISO_8859-4 ISO-8859-4 latin4 l4 csISOLatin4 }
11701 { ECMA-cyrillic iso-ir-111 KOI8-E csISO111ECMACyrillic }
11702 { CSA_Z243.4-1985-1 iso-ir-121 ISO646-CA csa7-1 ca csISO121Canadian1 }
11703 { CSA_Z243.4-1985-2 iso-ir-122 ISO646-CA2 csa7-2 csISO122Canadian2 }
11704 { CSA_Z243.4-1985-gr iso-ir-123 csISO123CSAZ24341985gr }
11705 { ISO_8859-6:1987 iso-ir-127 ISO_8859-6 ISO-8859-6 ECMA-114 ASMO-708
11706 arabic csISOLatinArabic }
11707 { ISO_8859-6-E csISO88596E ISO-8859-6-E }
11708 { ISO_8859-6-I csISO88596I ISO-8859-6-I }
11709 { ISO_8859-7:1987 iso-ir-126 ISO_8859-7 ISO-8859-7 ELOT_928 ECMA-118
11710 greek greek8 csISOLatinGreek }
11711 { T.101-G2 iso-ir-128 csISO128T101G2 }
11712 { ISO_8859-8:1988 iso-ir-138 ISO_8859-8 ISO-8859-8 hebrew
11713 csISOLatinHebrew }
11714 { ISO_8859-8-E csISO88598E ISO-8859-8-E }
11715 { ISO_8859-8-I csISO88598I ISO-8859-8-I }
11716 { CSN_369103 iso-ir-139 csISO139CSN369103 }
11717 { JUS_I.B1.002 iso-ir-141 ISO646-YU js yu csISO141JUSIB1002 }
11718 { ISO_6937-2-add iso-ir-142 csISOTextComm }
11719 { IEC_P27-1 iso-ir-143 csISO143IECP271 }
11720 { ISO_8859-5:1988 iso-ir-144 ISO_8859-5 ISO-8859-5 cyrillic
11721 csISOLatinCyrillic }
11722 { JUS_I.B1.003-serb iso-ir-146 serbian csISO146Serbian }
11723 { JUS_I.B1.003-mac macedonian iso-ir-147 csISO147Macedonian }
11724 { ISO_8859-9:1989 iso-ir-148 ISO_8859-9 ISO-8859-9 latin5 l5 csISOLatin5 }
11725 { greek-ccitt iso-ir-150 csISO150 csISO150GreekCCITT }
11726 { NC_NC00-10:81 cuba iso-ir-151 ISO646-CU csISO151Cuba }
11727 { ISO_6937-2-25 iso-ir-152 csISO6937Add }
11728 { GOST_19768-74 ST_SEV_358-88 iso-ir-153 csISO153GOST1976874 }
11729 { ISO_8859-supp iso-ir-154 latin1-2-5 csISO8859Supp }
11730 { ISO_10367-box iso-ir-155 csISO10367Box }
11731 { ISO-8859-10 iso-ir-157 l6 ISO_8859-10:1992 csISOLatin6 latin6 }
11732 { latin-lap lap iso-ir-158 csISO158Lap }
11733 { JIS_X0212-1990 x0212 iso-ir-159 csISO159JISX02121990 }
11734 { DS_2089 DS2089 ISO646-DK dk csISO646Danish }
11735 { us-dk csUSDK }
11736 { dk-us csDKUS }
11737 { JIS_X0201 X0201 csHalfWidthKatakana }
11738 { KSC5636 ISO646-KR csKSC5636 }
11739 { ISO-10646-UCS-2 csUnicode }
11740 { ISO-10646-UCS-4 csUCS4 }
11741 { DEC-MCS dec csDECMCS }
11742 { hp-roman8 roman8 r8 csHPRoman8 }
11743 { macintosh mac csMacintosh }
11744 { IBM037 cp037 ebcdic-cp-us ebcdic-cp-ca ebcdic-cp-wt ebcdic-cp-nl
11745 csIBM037 }
11746 { IBM038 EBCDIC-INT cp038 csIBM038 }
11747 { IBM273 CP273 csIBM273 }
11748 { IBM274 EBCDIC-BE CP274 csIBM274 }
11749 { IBM275 EBCDIC-BR cp275 csIBM275 }
11750 { IBM277 EBCDIC-CP-DK EBCDIC-CP-NO csIBM277 }
11751 { IBM278 CP278 ebcdic-cp-fi ebcdic-cp-se csIBM278 }
11752 { IBM280 CP280 ebcdic-cp-it csIBM280 }
11753 { IBM281 EBCDIC-JP-E cp281 csIBM281 }
11754 { IBM284 CP284 ebcdic-cp-es csIBM284 }
11755 { IBM285 CP285 ebcdic-cp-gb csIBM285 }
11756 { IBM290 cp290 EBCDIC-JP-kana csIBM290 }
11757 { IBM297 cp297 ebcdic-cp-fr csIBM297 }
11758 { IBM420 cp420 ebcdic-cp-ar1 csIBM420 }
11759 { IBM423 cp423 ebcdic-cp-gr csIBM423 }
11760 { IBM424 cp424 ebcdic-cp-he csIBM424 }
11761 { IBM437 cp437 437 csPC8CodePage437 }
11762 { IBM500 CP500 ebcdic-cp-be ebcdic-cp-ch csIBM500 }
11763 { IBM775 cp775 csPC775Baltic }
11764 { IBM850 cp850 850 csPC850Multilingual }
11765 { IBM851 cp851 851 csIBM851 }
11766 { IBM852 cp852 852 csPCp852 }
11767 { IBM855 cp855 855 csIBM855 }
11768 { IBM857 cp857 857 csIBM857 }
11769 { IBM860 cp860 860 csIBM860 }
11770 { IBM861 cp861 861 cp-is csIBM861 }
11771 { IBM862 cp862 862 csPC862LatinHebrew }
11772 { IBM863 cp863 863 csIBM863 }
11773 { IBM864 cp864 csIBM864 }
11774 { IBM865 cp865 865 csIBM865 }
11775 { IBM866 cp866 866 csIBM866 }
11776 { IBM868 CP868 cp-ar csIBM868 }
11777 { IBM869 cp869 869 cp-gr csIBM869 }
11778 { IBM870 CP870 ebcdic-cp-roece ebcdic-cp-yu csIBM870 }
11779 { IBM871 CP871 ebcdic-cp-is csIBM871 }
11780 { IBM880 cp880 EBCDIC-Cyrillic csIBM880 }
11781 { IBM891 cp891 csIBM891 }
11782 { IBM903 cp903 csIBM903 }
11783 { IBM904 cp904 904 csIBBM904 }
11784 { IBM905 CP905 ebcdic-cp-tr csIBM905 }
11785 { IBM918 CP918 ebcdic-cp-ar2 csIBM918 }
11786 { IBM1026 CP1026 csIBM1026 }
11787 { EBCDIC-AT-DE csIBMEBCDICATDE }
11788 { EBCDIC-AT-DE-A csEBCDICATDEA }
11789 { EBCDIC-CA-FR csEBCDICCAFR }
11790 { EBCDIC-DK-NO csEBCDICDKNO }
11791 { EBCDIC-DK-NO-A csEBCDICDKNOA }
11792 { EBCDIC-FI-SE csEBCDICFISE }
11793 { EBCDIC-FI-SE-A csEBCDICFISEA }
11794 { EBCDIC-FR csEBCDICFR }
11795 { EBCDIC-IT csEBCDICIT }
11796 { EBCDIC-PT csEBCDICPT }
11797 { EBCDIC-ES csEBCDICES }
11798 { EBCDIC-ES-A csEBCDICESA }
11799 { EBCDIC-ES-S csEBCDICESS }
11800 { EBCDIC-UK csEBCDICUK }
11801 { EBCDIC-US csEBCDICUS }
11802 { UNKNOWN-8BIT csUnknown8BiT }
11803 { MNEMONIC csMnemonic }
11804 { MNEM csMnem }
11805 { VISCII csVISCII }
11806 { VIQR csVIQR }
11807 { KOI8-R csKOI8R }
11808 { IBM00858 CCSID00858 CP00858 PC-Multilingual-850+euro }
11809 { IBM00924 CCSID00924 CP00924 ebcdic-Latin9--euro }
11810 { IBM01140 CCSID01140 CP01140 ebcdic-us-37+euro }
11811 { IBM01141 CCSID01141 CP01141 ebcdic-de-273+euro }
11812 { IBM01142 CCSID01142 CP01142 ebcdic-dk-277+euro ebcdic-no-277+euro }
11813 { IBM01143 CCSID01143 CP01143 ebcdic-fi-278+euro ebcdic-se-278+euro }
11814 { IBM01144 CCSID01144 CP01144 ebcdic-it-280+euro }
11815 { IBM01145 CCSID01145 CP01145 ebcdic-es-284+euro }
11816 { IBM01146 CCSID01146 CP01146 ebcdic-gb-285+euro }
11817 { IBM01147 CCSID01147 CP01147 ebcdic-fr-297+euro }
11818 { IBM01148 CCSID01148 CP01148 ebcdic-international-500+euro }
11819 { IBM01149 CCSID01149 CP01149 ebcdic-is-871+euro }
11820 { IBM1047 IBM-1047 }
11821 { PTCP154 csPTCP154 PT154 CP154 Cyrillic-Asian }
11822 { Amiga-1251 Ami1251 Amiga1251 Ami-1251 }
11823 { UNICODE-1-1 csUnicode11 }
11824 { CESU-8 csCESU-8 }
11825 { BOCU-1 csBOCU-1 }
11826 { UNICODE-1-1-UTF-7 csUnicode11UTF7 }
11827 { ISO-8859-14 iso-ir-199 ISO_8859-14:1998 ISO_8859-14 latin8 iso-celtic
11828 l8 }
11829 { ISO-8859-15 ISO_8859-15 Latin-9 }
11830 { ISO-8859-16 iso-ir-226 ISO_8859-16:2001 ISO_8859-16 latin10 l10 }
11831 { GBK CP936 MS936 windows-936 }
11832 { JIS_Encoding csJISEncoding }
11833 { Shift_JIS MS_Kanji csShiftJIS ShiftJIS Shift-JIS }
11834 { Extended_UNIX_Code_Packed_Format_for_Japanese csEUCPkdFmtJapanese
11835 EUC-JP }
11836 { Extended_UNIX_Code_Fixed_Width_for_Japanese csEUCFixWidJapanese }
11837 { ISO-10646-UCS-Basic csUnicodeASCII }
11838 { ISO-10646-Unicode-Latin1 csUnicodeLatin1 ISO-10646 }
11839 { ISO-Unicode-IBM-1261 csUnicodeIBM1261 }
11840 { ISO-Unicode-IBM-1268 csUnicodeIBM1268 }
11841 { ISO-Unicode-IBM-1276 csUnicodeIBM1276 }
11842 { ISO-Unicode-IBM-1264 csUnicodeIBM1264 }
11843 { ISO-Unicode-IBM-1265 csUnicodeIBM1265 }
11844 { ISO-8859-1-Windows-3.0-Latin-1 csWindows30Latin1 }
11845 { ISO-8859-1-Windows-3.1-Latin-1 csWindows31Latin1 }
11846 { ISO-8859-2-Windows-Latin-2 csWindows31Latin2 }
11847 { ISO-8859-9-Windows-Latin-5 csWindows31Latin5 }
11848 { Adobe-Standard-Encoding csAdobeStandardEncoding }
11849 { Ventura-US csVenturaUS }
11850 { Ventura-International csVenturaInternational }
11851 { PC8-Danish-Norwegian csPC8DanishNorwegian }
11852 { PC8-Turkish csPC8Turkish }
11853 { IBM-Symbols csIBMSymbols }
11854 { IBM-Thai csIBMThai }
11855 { HP-Legal csHPLegal }
11856 { HP-Pi-font csHPPiFont }
11857 { HP-Math8 csHPMath8 }
11858 { Adobe-Symbol-Encoding csHPPSMath }
11859 { HP-DeskTop csHPDesktop }
11860 { Ventura-Math csVenturaMath }
11861 { Microsoft-Publishing csMicrosoftPublishing }
11862 { Windows-31J csWindows31J }
11863 { GB2312 csGB2312 }
11864 { Big5 csBig5 }
11867 proc tcl_encoding {enc} {
11868 global encoding_aliases tcl_encoding_cache
11869 if {[info exists tcl_encoding_cache($enc)]} {
11870 return $tcl_encoding_cache($enc)
11872 set names [encoding names]
11873 set lcnames [string tolower $names]
11874 set enc [string tolower $enc]
11875 set i [lsearch -exact $lcnames $enc]
11876 if {$i < 0} {
11877 # look for "isonnn" instead of "iso-nnn" or "iso_nnn"
11878 if {[regsub {^(iso|cp|ibm|jis)[-_]} $enc {\1} encx]} {
11879 set i [lsearch -exact $lcnames $encx]
11882 if {$i < 0} {
11883 foreach l $encoding_aliases {
11884 set ll [string tolower $l]
11885 if {[lsearch -exact $ll $enc] < 0} continue
11886 # look through the aliases for one that tcl knows about
11887 foreach e $ll {
11888 set i [lsearch -exact $lcnames $e]
11889 if {$i < 0} {
11890 if {[regsub {^(iso|cp|ibm|jis)[-_]} $e {\1} ex]} {
11891 set i [lsearch -exact $lcnames $ex]
11894 if {$i >= 0} break
11896 break
11899 set tclenc {}
11900 if {$i >= 0} {
11901 set tclenc [lindex $names $i]
11903 set tcl_encoding_cache($enc) $tclenc
11904 return $tclenc
11907 proc gitattr {path attr default} {
11908 global path_attr_cache
11909 if {[info exists path_attr_cache($attr,$path)]} {
11910 set r $path_attr_cache($attr,$path)
11911 } else {
11912 set r "unspecified"
11913 if {![catch {set line [exec git check-attr $attr -- $path]}]} {
11914 regexp "(.*): $attr: (.*)" $line m f r
11916 set path_attr_cache($attr,$path) $r
11918 if {$r eq "unspecified"} {
11919 return $default
11921 return $r
11924 proc cache_gitattr {attr pathlist} {
11925 global path_attr_cache
11926 set newlist {}
11927 foreach path $pathlist {
11928 if {![info exists path_attr_cache($attr,$path)]} {
11929 lappend newlist $path
11932 set lim 1000
11933 if {[tk windowingsystem] == "win32"} {
11934 # windows has a 32k limit on the arguments to a command...
11935 set lim 30
11937 while {$newlist ne {}} {
11938 set head [lrange $newlist 0 [expr {$lim - 1}]]
11939 set newlist [lrange $newlist $lim end]
11940 if {![catch {set rlist [eval exec git check-attr $attr -- $head]}]} {
11941 foreach row [split $rlist "\n"] {
11942 if {[regexp "(.*): $attr: (.*)" $row m path value]} {
11943 if {[string index $path 0] eq "\""} {
11944 set path [encoding convertfrom [lindex $path 0]]
11946 set path_attr_cache($attr,$path) $value
11953 proc get_path_encoding {path} {
11954 global gui_encoding perfile_attrs
11955 set tcl_enc $gui_encoding
11956 if {$path ne {} && $perfile_attrs} {
11957 set enc2 [tcl_encoding [gitattr $path encoding $tcl_enc]]
11958 if {$enc2 ne {}} {
11959 set tcl_enc $enc2
11962 return $tcl_enc
11965 # First check that Tcl/Tk is recent enough
11966 if {[catch {package require Tk 8.4} err]} {
11967 show_error {} . "Sorry, gitk cannot run with this version of Tcl/Tk.\n\
11968 Gitk requires at least Tcl/Tk 8.4." list
11969 exit 1
11972 # on OSX bring the current Wish process window to front
11973 if {[tk windowingsystem] eq "aqua"} {
11974 exec osascript -e [format {
11975 tell application "System Events"
11976 set frontmost of processes whose unix id is %d to true
11977 end tell
11978 } [pid] ]
11981 # Unset GIT_TRACE var if set
11982 if { [info exists ::env(GIT_TRACE)] } {
11983 unset ::env(GIT_TRACE)
11986 # defaults...
11987 set wrcomcmd "git diff-tree --stdin -p --pretty"
11989 set gitencoding {}
11990 catch {
11991 set gitencoding [exec git config --get i18n.commitencoding]
11993 catch {
11994 set gitencoding [exec git config --get i18n.logoutputencoding]
11996 if {$gitencoding == ""} {
11997 set gitencoding "utf-8"
11999 set tclencoding [tcl_encoding $gitencoding]
12000 if {$tclencoding == {}} {
12001 puts stderr "Warning: encoding $gitencoding is not supported by Tcl/Tk"
12004 set gui_encoding [encoding system]
12005 catch {
12006 set enc [exec git config --get gui.encoding]
12007 if {$enc ne {}} {
12008 set tclenc [tcl_encoding $enc]
12009 if {$tclenc ne {}} {
12010 set gui_encoding $tclenc
12011 } else {
12012 puts stderr "Warning: encoding $enc is not supported by Tcl/Tk"
12017 set log_showroot true
12018 catch {
12019 set log_showroot [exec git config --bool --get log.showroot]
12022 if {[tk windowingsystem] eq "aqua"} {
12023 set mainfont {{Lucida Grande} 9}
12024 set textfont {Monaco 9}
12025 set uifont {{Lucida Grande} 9 bold}
12026 } elseif {![catch {::tk::pkgconfig get fontsystem} xft] && $xft eq "xft"} {
12027 # fontconfig!
12028 set mainfont {sans 9}
12029 set textfont {monospace 9}
12030 set uifont {sans 9 bold}
12031 } else {
12032 set mainfont {Helvetica 9}
12033 set textfont {Courier 9}
12034 set uifont {Helvetica 9 bold}
12036 set tabstop 8
12037 set findmergefiles 0
12038 set maxgraphpct 50
12039 set maxwidth 16
12040 set revlistorder 0
12041 set fastdate 0
12042 set uparrowlen 5
12043 set downarrowlen 5
12044 set mingaplen 100
12045 set cmitmode "patch"
12046 set wrapcomment "none"
12047 set showneartags 1
12048 set hideremotes 0
12049 set maxrefs 20
12050 set visiblerefs {"master"}
12051 set maxlinelen 200
12052 set showlocalchanges 1
12053 set limitdiffs 1
12054 set datetimeformat "%Y-%m-%d %H:%M:%S"
12055 set autoselect 1
12056 set autosellen 40
12057 set perfile_attrs 0
12058 set want_ttk 1
12060 if {[tk windowingsystem] eq "aqua"} {
12061 set extdifftool "opendiff"
12062 } else {
12063 set extdifftool "meld"
12066 set colors {green red blue magenta darkgrey brown orange}
12067 if {[tk windowingsystem] eq "win32"} {
12068 set uicolor SystemButtonFace
12069 set uifgcolor SystemButtonText
12070 set uifgdisabledcolor SystemDisabledText
12071 set bgcolor SystemWindow
12072 set fgcolor SystemWindowText
12073 set selectbgcolor SystemHighlight
12074 } else {
12075 set uicolor grey85
12076 set uifgcolor black
12077 set uifgdisabledcolor "#999"
12078 set bgcolor white
12079 set fgcolor black
12080 set selectbgcolor gray85
12082 set diffcolors {red "#00a000" blue}
12083 set diffcontext 3
12084 set mergecolors {red blue green purple brown "#009090" magenta "#808000" "#009000" "#ff0080" cyan "#b07070" "#70b0f0" "#70f0b0" "#f0b070" "#ff70b0"}
12085 set ignorespace 0
12086 set worddiff ""
12087 set markbgcolor "#e0e0ff"
12089 set headbgcolor green
12090 set headfgcolor black
12091 set headoutlinecolor black
12092 set remotebgcolor #ffddaa
12093 set tagbgcolor yellow
12094 set tagfgcolor black
12095 set tagoutlinecolor black
12096 set reflinecolor black
12097 set filesepbgcolor #aaaaaa
12098 set filesepfgcolor black
12099 set linehoverbgcolor #ffff80
12100 set linehoverfgcolor black
12101 set linehoveroutlinecolor black
12102 set mainheadcirclecolor yellow
12103 set workingfilescirclecolor red
12104 set indexcirclecolor green
12105 set circlecolors {white blue gray blue blue}
12106 set linkfgcolor blue
12107 set circleoutlinecolor $fgcolor
12108 set foundbgcolor yellow
12109 set currentsearchhitbgcolor orange
12111 # button for popping up context menus
12112 if {[tk windowingsystem] eq "aqua"} {
12113 set ctxbut <Button-2>
12114 } else {
12115 set ctxbut <Button-3>
12118 ## For msgcat loading, first locate the installation location.
12119 if { [info exists ::env(GITK_MSGSDIR)] } {
12120 ## Msgsdir was manually set in the environment.
12121 set gitk_msgsdir $::env(GITK_MSGSDIR)
12122 } else {
12123 ## Let's guess the prefix from argv0.
12124 set gitk_prefix [file dirname [file dirname [file normalize $argv0]]]
12125 set gitk_libdir [file join $gitk_prefix share gitk lib]
12126 set gitk_msgsdir [file join $gitk_libdir msgs]
12127 unset gitk_prefix
12130 ## Internationalization (i18n) through msgcat and gettext. See
12131 ## http://www.gnu.org/software/gettext/manual/html_node/Tcl.html
12132 package require msgcat
12133 namespace import ::msgcat::mc
12134 ## And eventually load the actual message catalog
12135 ::msgcat::mcload $gitk_msgsdir
12137 catch {
12138 # follow the XDG base directory specification by default. See
12139 # http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
12140 if {[info exists env(XDG_CONFIG_HOME)] && $env(XDG_CONFIG_HOME) ne ""} {
12141 # XDG_CONFIG_HOME environment variable is set
12142 set config_file [file join $env(XDG_CONFIG_HOME) git gitk]
12143 set config_file_tmp [file join $env(XDG_CONFIG_HOME) git gitk-tmp]
12144 } else {
12145 # default XDG_CONFIG_HOME
12146 set config_file "~/.config/git/gitk"
12147 set config_file_tmp "~/.config/git/gitk-tmp"
12149 if {![file exists $config_file]} {
12150 # for backward compatibility use the old config file if it exists
12151 if {[file exists "~/.gitk"]} {
12152 set config_file "~/.gitk"
12153 set config_file_tmp "~/.gitk-tmp"
12154 } elseif {![file exists [file dirname $config_file]]} {
12155 file mkdir [file dirname $config_file]
12158 source $config_file
12161 parsefont mainfont $mainfont
12162 eval font create mainfont [fontflags mainfont]
12163 eval font create mainfontbold [fontflags mainfont 1]
12165 parsefont textfont $textfont
12166 eval font create textfont [fontflags textfont]
12167 eval font create textfontbold [fontflags textfont 1]
12169 parsefont uifont $uifont
12170 eval font create uifont [fontflags uifont]
12172 setui $uicolor
12174 setoptions
12176 # check that we can find a .git directory somewhere...
12177 if {[catch {set gitdir [exec git rev-parse --git-dir]}]} {
12178 show_error {} . [mc "Cannot find a git repository here."]
12179 exit 1
12182 set selecthead {}
12183 set selectheadid {}
12185 set revtreeargs {}
12186 set cmdline_files {}
12187 set i 0
12188 set revtreeargscmd {}
12189 foreach arg $argv {
12190 switch -glob -- $arg {
12191 "" { }
12192 "--" {
12193 set cmdline_files [lrange $argv [expr {$i + 1}] end]
12194 break
12196 "--select-commit=*" {
12197 set selecthead [string range $arg 16 end]
12199 "--argscmd=*" {
12200 set revtreeargscmd [string range $arg 10 end]
12202 default {
12203 lappend revtreeargs $arg
12206 incr i
12209 if {$selecthead eq "HEAD"} {
12210 set selecthead {}
12213 if {$i >= [llength $argv] && $revtreeargs ne {}} {
12214 # no -- on command line, but some arguments (other than --argscmd)
12215 if {[catch {
12216 set f [eval exec git rev-parse --no-revs --no-flags $revtreeargs]
12217 set cmdline_files [split $f "\n"]
12218 set n [llength $cmdline_files]
12219 set revtreeargs [lrange $revtreeargs 0 end-$n]
12220 # Unfortunately git rev-parse doesn't produce an error when
12221 # something is both a revision and a filename. To be consistent
12222 # with git log and git rev-list, check revtreeargs for filenames.
12223 foreach arg $revtreeargs {
12224 if {[file exists $arg]} {
12225 show_error {} . [mc "Ambiguous argument '%s': both revision\
12226 and filename" $arg]
12227 exit 1
12230 } err]} {
12231 # unfortunately we get both stdout and stderr in $err,
12232 # so look for "fatal:".
12233 set i [string first "fatal:" $err]
12234 if {$i > 0} {
12235 set err [string range $err [expr {$i + 6}] end]
12237 show_error {} . "[mc "Bad arguments to gitk:"]\n$err"
12238 exit 1
12242 set nullid "0000000000000000000000000000000000000000"
12243 set nullid2 "0000000000000000000000000000000000000001"
12244 set nullfile "/dev/null"
12246 set have_tk85 [expr {[package vcompare $tk_version "8.5"] >= 0}]
12247 if {![info exists have_ttk]} {
12248 set have_ttk [llength [info commands ::ttk::style]]
12250 set use_ttk [expr {$have_ttk && $want_ttk}]
12251 set NS [expr {$use_ttk ? "ttk" : ""}]
12253 regexp {^git version ([\d.]*\d)} [exec git version] _ git_version
12255 set show_notes {}
12256 if {[package vcompare $git_version "1.6.6.2"] >= 0} {
12257 set show_notes "--show-notes"
12260 set appname "gitk"
12262 set runq {}
12263 set history {}
12264 set historyindex 0
12265 set fh_serial 0
12266 set nhl_names {}
12267 set highlight_paths {}
12268 set findpattern {}
12269 set searchdirn -forwards
12270 set boldids {}
12271 set boldnameids {}
12272 set diffelide {0 0}
12273 set markingmatches 0
12274 set linkentercount 0
12275 set need_redisplay 0
12276 set nrows_drawn 0
12277 set firsttabstop 0
12279 set nextviewnum 1
12280 set curview 0
12281 set selectedview 0
12282 set selectedhlview [mc "None"]
12283 set highlight_related [mc "None"]
12284 set highlight_files {}
12285 set viewfiles(0) {}
12286 set viewperm(0) 0
12287 set viewargs(0) {}
12288 set viewargscmd(0) {}
12290 set selectedline {}
12291 set numcommits 0
12292 set loginstance 0
12293 set cmdlineok 0
12294 set stopped 0
12295 set stuffsaved 0
12296 set patchnum 0
12297 set lserial 0
12298 set hasworktree [hasworktree]
12299 set cdup {}
12300 if {[expr {[exec git rev-parse --is-inside-work-tree] == "true"}]} {
12301 set cdup [exec git rev-parse --show-cdup]
12303 set worktree [exec git rev-parse --show-toplevel]
12304 setcoords
12305 makewindow
12306 catch {
12307 image create photo gitlogo -width 16 -height 16
12309 image create photo gitlogominus -width 4 -height 2
12310 gitlogominus put #C00000 -to 0 0 4 2
12311 gitlogo copy gitlogominus -to 1 5
12312 gitlogo copy gitlogominus -to 6 5
12313 gitlogo copy gitlogominus -to 11 5
12314 image delete gitlogominus
12316 image create photo gitlogoplus -width 4 -height 4
12317 gitlogoplus put #008000 -to 1 0 3 4
12318 gitlogoplus put #008000 -to 0 1 4 3
12319 gitlogo copy gitlogoplus -to 1 9
12320 gitlogo copy gitlogoplus -to 6 9
12321 gitlogo copy gitlogoplus -to 11 9
12322 image delete gitlogoplus
12324 image create photo gitlogo32 -width 32 -height 32
12325 gitlogo32 copy gitlogo -zoom 2 2
12327 wm iconphoto . -default gitlogo gitlogo32
12329 # wait for the window to become visible
12330 tkwait visibility .
12331 wm title . "$appname: [reponame]"
12332 update
12333 readrefs
12335 if {$cmdline_files ne {} || $revtreeargs ne {} || $revtreeargscmd ne {}} {
12336 # create a view for the files/dirs specified on the command line
12337 set curview 1
12338 set selectedview 1
12339 set nextviewnum 2
12340 set viewname(1) [mc "Command line"]
12341 set viewfiles(1) $cmdline_files
12342 set viewargs(1) $revtreeargs
12343 set viewargscmd(1) $revtreeargscmd
12344 set viewperm(1) 0
12345 set vdatemode(1) 0
12346 addviewmenu 1
12347 .bar.view entryconf [mca "Edit view..."] -state normal
12348 .bar.view entryconf [mca "Delete view"] -state normal
12351 if {[info exists permviews]} {
12352 foreach v $permviews {
12353 set n $nextviewnum
12354 incr nextviewnum
12355 set viewname($n) [lindex $v 0]
12356 set viewfiles($n) [lindex $v 1]
12357 set viewargs($n) [lindex $v 2]
12358 set viewargscmd($n) [lindex $v 3]
12359 set viewperm($n) 1
12360 addviewmenu $n
12364 if {[tk windowingsystem] eq "win32"} {
12365 focus -force .
12368 getcommits {}
12370 # Local variables:
12371 # mode: tcl
12372 # indent-tabs-mode: t
12373 # tab-width: 8
12374 # End: