contrib: make git-new-workdir work with windows symlinks.
[git/mingw/4msysgit.git] / gitk-git / gitk
blobe3925716d231c619209f11c8a698d6fd0511915a
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 [limit_arg_length [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 $cflist <1> {sel_flist %W %x %y; break}
2589 bind $cflist <B1-Motion> {sel_flist %W %x %y; break}
2590 bind $cflist <ButtonRelease-1> {treeclick %W %x %y}
2591 global ctxbut
2592 bind $cflist $ctxbut {pop_flist_menu %W %X %Y %x %y}
2593 bind $ctext $ctxbut {pop_diff_menu %W %X %Y %x %y}
2594 bind $ctext <Button-1> {focus %W}
2595 bind $ctext <<Selection>> rehighlight_search_results
2597 set maincursor [. cget -cursor]
2598 set textcursor [$ctext cget -cursor]
2599 set curtextcursor $textcursor
2601 set rowctxmenu .rowctxmenu
2602 makemenu $rowctxmenu {
2603 {mc "Diff this -> selected" command {diffvssel 0}}
2604 {mc "Diff selected -> this" command {diffvssel 1}}
2605 {mc "Make patch" command mkpatch}
2606 {mc "Create tag" command mktag}
2607 {mc "Write commit to file" command writecommit}
2608 {mc "Create new branch" command mkbranch}
2609 {mc "Cherry-pick this commit" command cherrypick}
2610 {mc "Reset HEAD branch to here" command resethead}
2611 {mc "Mark this commit" command markhere}
2612 {mc "Return to mark" command gotomark}
2613 {mc "Find descendant of this and mark" command find_common_desc}
2614 {mc "Compare with marked commit" command compare_commits}
2615 {mc "Diff this -> marked commit" command {diffvsmark 0}}
2616 {mc "Diff marked commit -> this" command {diffvsmark 1}}
2617 {mc "Revert this commit" command revert}
2619 $rowctxmenu configure -tearoff 0
2621 set fakerowmenu .fakerowmenu
2622 makemenu $fakerowmenu {
2623 {mc "Diff this -> selected" command {diffvssel 0}}
2624 {mc "Diff selected -> this" command {diffvssel 1}}
2625 {mc "Make patch" command mkpatch}
2626 {mc "Diff this -> marked commit" command {diffvsmark 0}}
2627 {mc "Diff marked commit -> this" command {diffvsmark 1}}
2629 $fakerowmenu configure -tearoff 0
2631 set headctxmenu .headctxmenu
2632 makemenu $headctxmenu {
2633 {mc "Check out this branch" command cobranch}
2634 {mc "Remove this branch" command rmbranch}
2636 $headctxmenu configure -tearoff 0
2638 global flist_menu
2639 set flist_menu .flistctxmenu
2640 makemenu $flist_menu {
2641 {mc "Highlight this too" command {flist_hl 0}}
2642 {mc "Highlight this only" command {flist_hl 1}}
2643 {mc "External diff" command {external_diff}}
2644 {mc "Blame parent commit" command {external_blame 1}}
2646 $flist_menu configure -tearoff 0
2648 global diff_menu
2649 set diff_menu .diffctxmenu
2650 makemenu $diff_menu {
2651 {mc "Show origin of this line" command show_line_source}
2652 {mc "Run git gui blame on this line" command {external_blame_diff}}
2654 $diff_menu configure -tearoff 0
2657 # Windows sends all mouse wheel events to the current focused window, not
2658 # the one where the mouse hovers, so bind those events here and redirect
2659 # to the correct window
2660 proc windows_mousewheel_redirector {W X Y D} {
2661 global canv canv2 canv3
2662 set w [winfo containing -displayof $W $X $Y]
2663 if {$w ne ""} {
2664 set u [expr {$D < 0 ? 5 : -5}]
2665 if {$w == $canv || $w == $canv2 || $w == $canv3} {
2666 allcanvs yview scroll $u units
2667 } else {
2668 catch {
2669 $w yview scroll $u units
2675 # Update row number label when selectedline changes
2676 proc selectedline_change {n1 n2 op} {
2677 global selectedline rownumsel
2679 if {$selectedline eq {}} {
2680 set rownumsel {}
2681 } else {
2682 set rownumsel [expr {$selectedline + 1}]
2686 # mouse-2 makes all windows scan vertically, but only the one
2687 # the cursor is in scans horizontally
2688 proc canvscan {op w x y} {
2689 global canv canv2 canv3
2690 foreach c [list $canv $canv2 $canv3] {
2691 if {$c == $w} {
2692 $c scan $op $x $y
2693 } else {
2694 $c scan $op 0 $y
2699 proc scrollcanv {cscroll f0 f1} {
2700 $cscroll set $f0 $f1
2701 drawvisible
2702 flushhighlights
2705 # when we make a key binding for the toplevel, make sure
2706 # it doesn't get triggered when that key is pressed in the
2707 # find string entry widget.
2708 proc bindkey {ev script} {
2709 global entries
2710 bind . $ev $script
2711 set escript [bind Entry $ev]
2712 if {$escript == {}} {
2713 set escript [bind Entry <Key>]
2715 foreach e $entries {
2716 bind $e $ev "$escript; break"
2720 proc bindmodfunctionkey {mod n script} {
2721 bind . <$mod-F$n> $script
2722 catch { bind . <$mod-XF86_Switch_VT_$n> $script }
2725 # set the focus back to the toplevel for any click outside
2726 # the entry widgets
2727 proc click {w} {
2728 global ctext entries
2729 foreach e [concat $entries $ctext] {
2730 if {$w == $e} return
2732 focus .
2735 # Adjust the progress bar for a change in requested extent or canvas size
2736 proc adjustprogress {} {
2737 global progresscanv progressitem progresscoords
2738 global fprogitem fprogcoord lastprogupdate progupdatepending
2739 global rprogitem rprogcoord use_ttk
2741 if {$use_ttk} {
2742 $progresscanv configure -value [expr {int($fprogcoord * 100)}]
2743 return
2746 set w [expr {[winfo width $progresscanv] - 4}]
2747 set x0 [expr {$w * [lindex $progresscoords 0]}]
2748 set x1 [expr {$w * [lindex $progresscoords 1]}]
2749 set h [winfo height $progresscanv]
2750 $progresscanv coords $progressitem $x0 0 $x1 $h
2751 $progresscanv coords $fprogitem 0 0 [expr {$w * $fprogcoord}] $h
2752 $progresscanv coords $rprogitem 0 0 [expr {$w * $rprogcoord}] $h
2753 set now [clock clicks -milliseconds]
2754 if {$now >= $lastprogupdate + 100} {
2755 set progupdatepending 0
2756 update
2757 } elseif {!$progupdatepending} {
2758 set progupdatepending 1
2759 after [expr {$lastprogupdate + 100 - $now}] doprogupdate
2763 proc doprogupdate {} {
2764 global lastprogupdate progupdatepending
2766 if {$progupdatepending} {
2767 set progupdatepending 0
2768 set lastprogupdate [clock clicks -milliseconds]
2769 update
2773 proc savestuff {w} {
2774 global canv canv2 canv3 mainfont textfont uifont tabstop
2775 global stuffsaved findmergefiles maxgraphpct
2776 global maxwidth showneartags showlocalchanges
2777 global viewname viewfiles viewargs viewargscmd viewperm nextviewnum
2778 global cmitmode wrapcomment datetimeformat limitdiffs
2779 global colors uicolor bgcolor fgcolor diffcolors diffcontext selectbgcolor
2780 global uifgcolor uifgdisabledcolor
2781 global headbgcolor headfgcolor headoutlinecolor remotebgcolor
2782 global tagbgcolor tagfgcolor tagoutlinecolor
2783 global reflinecolor filesepbgcolor filesepfgcolor
2784 global mergecolors foundbgcolor currentsearchhitbgcolor
2785 global linehoverbgcolor linehoverfgcolor linehoveroutlinecolor circlecolors
2786 global mainheadcirclecolor workingfilescirclecolor indexcirclecolor
2787 global linkfgcolor circleoutlinecolor
2788 global autoselect autosellen extdifftool perfile_attrs markbgcolor use_ttk
2789 global hideremotes want_ttk maxrefs
2790 global config_file config_file_tmp
2792 if {$stuffsaved} return
2793 if {![winfo viewable .]} return
2794 catch {
2795 if {[file exists $config_file_tmp]} {
2796 file delete -force $config_file_tmp
2798 set f [open $config_file_tmp w]
2799 if {$::tcl_platform(platform) eq {windows}} {
2800 file attributes $config_file_tmp -hidden true
2802 puts $f [list set mainfont $mainfont]
2803 puts $f [list set textfont $textfont]
2804 puts $f [list set uifont $uifont]
2805 puts $f [list set tabstop $tabstop]
2806 puts $f [list set findmergefiles $findmergefiles]
2807 puts $f [list set maxgraphpct $maxgraphpct]
2808 puts $f [list set maxwidth $maxwidth]
2809 puts $f [list set cmitmode $cmitmode]
2810 puts $f [list set wrapcomment $wrapcomment]
2811 puts $f [list set autoselect $autoselect]
2812 puts $f [list set autosellen $autosellen]
2813 puts $f [list set showneartags $showneartags]
2814 puts $f [list set maxrefs $maxrefs]
2815 puts $f [list set hideremotes $hideremotes]
2816 puts $f [list set showlocalchanges $showlocalchanges]
2817 puts $f [list set datetimeformat $datetimeformat]
2818 puts $f [list set limitdiffs $limitdiffs]
2819 puts $f [list set uicolor $uicolor]
2820 puts $f [list set want_ttk $want_ttk]
2821 puts $f [list set bgcolor $bgcolor]
2822 puts $f [list set fgcolor $fgcolor]
2823 puts $f [list set uifgcolor $uifgcolor]
2824 puts $f [list set uifgdisabledcolor $uifgdisabledcolor]
2825 puts $f [list set colors $colors]
2826 puts $f [list set diffcolors $diffcolors]
2827 puts $f [list set mergecolors $mergecolors]
2828 puts $f [list set markbgcolor $markbgcolor]
2829 puts $f [list set diffcontext $diffcontext]
2830 puts $f [list set selectbgcolor $selectbgcolor]
2831 puts $f [list set foundbgcolor $foundbgcolor]
2832 puts $f [list set currentsearchhitbgcolor $currentsearchhitbgcolor]
2833 puts $f [list set extdifftool $extdifftool]
2834 puts $f [list set perfile_attrs $perfile_attrs]
2835 puts $f [list set headbgcolor $headbgcolor]
2836 puts $f [list set headfgcolor $headfgcolor]
2837 puts $f [list set headoutlinecolor $headoutlinecolor]
2838 puts $f [list set remotebgcolor $remotebgcolor]
2839 puts $f [list set tagbgcolor $tagbgcolor]
2840 puts $f [list set tagfgcolor $tagfgcolor]
2841 puts $f [list set tagoutlinecolor $tagoutlinecolor]
2842 puts $f [list set reflinecolor $reflinecolor]
2843 puts $f [list set filesepbgcolor $filesepbgcolor]
2844 puts $f [list set filesepfgcolor $filesepfgcolor]
2845 puts $f [list set linehoverbgcolor $linehoverbgcolor]
2846 puts $f [list set linehoverfgcolor $linehoverfgcolor]
2847 puts $f [list set linehoveroutlinecolor $linehoveroutlinecolor]
2848 puts $f [list set mainheadcirclecolor $mainheadcirclecolor]
2849 puts $f [list set workingfilescirclecolor $workingfilescirclecolor]
2850 puts $f [list set indexcirclecolor $indexcirclecolor]
2851 puts $f [list set circlecolors $circlecolors]
2852 puts $f [list set linkfgcolor $linkfgcolor]
2853 puts $f [list set circleoutlinecolor $circleoutlinecolor]
2855 puts $f "set geometry(main) [wm geometry .]"
2856 puts $f "set geometry(state) [wm state .]"
2857 puts $f "set geometry(topwidth) [winfo width .tf]"
2858 puts $f "set geometry(topheight) [winfo height .tf]"
2859 if {$use_ttk} {
2860 puts $f "set geometry(pwsash0) \"[.tf.histframe.pwclist sashpos 0] 1\""
2861 puts $f "set geometry(pwsash1) \"[.tf.histframe.pwclist sashpos 1] 1\""
2862 } else {
2863 puts $f "set geometry(pwsash0) \"[.tf.histframe.pwclist sash coord 0]\""
2864 puts $f "set geometry(pwsash1) \"[.tf.histframe.pwclist sash coord 1]\""
2866 puts $f "set geometry(botwidth) [winfo width .bleft]"
2867 puts $f "set geometry(botheight) [winfo height .bleft]"
2869 puts -nonewline $f "set permviews {"
2870 for {set v 0} {$v < $nextviewnum} {incr v} {
2871 if {$viewperm($v)} {
2872 puts $f "{[list $viewname($v) $viewfiles($v) $viewargs($v) $viewargscmd($v)]}"
2875 puts $f "}"
2876 close $f
2877 file rename -force $config_file_tmp $config_file
2879 set stuffsaved 1
2882 proc resizeclistpanes {win w} {
2883 global oldwidth use_ttk
2884 if {[info exists oldwidth($win)]} {
2885 if {$use_ttk} {
2886 set s0 [$win sashpos 0]
2887 set s1 [$win sashpos 1]
2888 } else {
2889 set s0 [$win sash coord 0]
2890 set s1 [$win sash coord 1]
2892 if {$w < 60} {
2893 set sash0 [expr {int($w/2 - 2)}]
2894 set sash1 [expr {int($w*5/6 - 2)}]
2895 } else {
2896 set factor [expr {1.0 * $w / $oldwidth($win)}]
2897 set sash0 [expr {int($factor * [lindex $s0 0])}]
2898 set sash1 [expr {int($factor * [lindex $s1 0])}]
2899 if {$sash0 < 30} {
2900 set sash0 30
2902 if {$sash1 < $sash0 + 20} {
2903 set sash1 [expr {$sash0 + 20}]
2905 if {$sash1 > $w - 10} {
2906 set sash1 [expr {$w - 10}]
2907 if {$sash0 > $sash1 - 20} {
2908 set sash0 [expr {$sash1 - 20}]
2912 if {$use_ttk} {
2913 $win sashpos 0 $sash0
2914 $win sashpos 1 $sash1
2915 } else {
2916 $win sash place 0 $sash0 [lindex $s0 1]
2917 $win sash place 1 $sash1 [lindex $s1 1]
2920 set oldwidth($win) $w
2923 proc resizecdetpanes {win w} {
2924 global oldwidth use_ttk
2925 if {[info exists oldwidth($win)]} {
2926 if {$use_ttk} {
2927 set s0 [$win sashpos 0]
2928 } else {
2929 set s0 [$win sash coord 0]
2931 if {$w < 60} {
2932 set sash0 [expr {int($w*3/4 - 2)}]
2933 } else {
2934 set factor [expr {1.0 * $w / $oldwidth($win)}]
2935 set sash0 [expr {int($factor * [lindex $s0 0])}]
2936 if {$sash0 < 45} {
2937 set sash0 45
2939 if {$sash0 > $w - 15} {
2940 set sash0 [expr {$w - 15}]
2943 if {$use_ttk} {
2944 $win sashpos 0 $sash0
2945 } else {
2946 $win sash place 0 $sash0 [lindex $s0 1]
2949 set oldwidth($win) $w
2952 proc allcanvs args {
2953 global canv canv2 canv3
2954 eval $canv $args
2955 eval $canv2 $args
2956 eval $canv3 $args
2959 proc bindall {event action} {
2960 global canv canv2 canv3
2961 bind $canv $event $action
2962 bind $canv2 $event $action
2963 bind $canv3 $event $action
2966 proc about {} {
2967 global uifont NS
2968 set w .about
2969 if {[winfo exists $w]} {
2970 raise $w
2971 return
2973 ttk_toplevel $w
2974 wm title $w [mc "About gitk"]
2975 make_transient $w .
2976 message $w.m -text [mc "
2977 Gitk - a commit viewer for git
2979 Copyright \u00a9 2005-2014 Paul Mackerras
2981 Use and redistribute under the terms of the GNU General Public License"] \
2982 -justify center -aspect 400 -border 2 -bg white -relief groove
2983 pack $w.m -side top -fill x -padx 2 -pady 2
2984 ${NS}::button $w.ok -text [mc "Close"] -command "destroy $w" -default active
2985 pack $w.ok -side bottom
2986 bind $w <Visibility> "focus $w.ok"
2987 bind $w <Key-Escape> "destroy $w"
2988 bind $w <Key-Return> "destroy $w"
2989 tk::PlaceWindow $w widget .
2992 proc keys {} {
2993 global NS
2994 set w .keys
2995 if {[winfo exists $w]} {
2996 raise $w
2997 return
2999 if {[tk windowingsystem] eq {aqua}} {
3000 set M1T Cmd
3001 } else {
3002 set M1T Ctrl
3004 ttk_toplevel $w
3005 wm title $w [mc "Gitk key bindings"]
3006 make_transient $w .
3007 message $w.m -text "
3008 [mc "Gitk key bindings:"]
3010 [mc "<%s-Q> Quit" $M1T]
3011 [mc "<%s-W> Close window" $M1T]
3012 [mc "<Home> Move to first commit"]
3013 [mc "<End> Move to last commit"]
3014 [mc "<Up>, p, k Move up one commit"]
3015 [mc "<Down>, n, j Move down one commit"]
3016 [mc "<Left>, z, h Go back in history list"]
3017 [mc "<Right>, x, l Go forward in history list"]
3018 [mc "<PageUp> Move up one page in commit list"]
3019 [mc "<PageDown> Move down one page in commit list"]
3020 [mc "<%s-Home> Scroll to top of commit list" $M1T]
3021 [mc "<%s-End> Scroll to bottom of commit list" $M1T]
3022 [mc "<%s-Up> Scroll commit list up one line" $M1T]
3023 [mc "<%s-Down> Scroll commit list down one line" $M1T]
3024 [mc "<%s-PageUp> Scroll commit list up one page" $M1T]
3025 [mc "<%s-PageDown> Scroll commit list down one page" $M1T]
3026 [mc "<Shift-Up> Find backwards (upwards, later commits)"]
3027 [mc "<Shift-Down> Find forwards (downwards, earlier commits)"]
3028 [mc "<Delete>, b Scroll diff view up one page"]
3029 [mc "<Backspace> Scroll diff view up one page"]
3030 [mc "<Space> Scroll diff view down one page"]
3031 [mc "u Scroll diff view up 18 lines"]
3032 [mc "d Scroll diff view down 18 lines"]
3033 [mc "<%s-F> Find" $M1T]
3034 [mc "<%s-G> Move to next find hit" $M1T]
3035 [mc "<Return> Move to next find hit"]
3036 [mc "/ Focus the search box"]
3037 [mc "? Move to previous find hit"]
3038 [mc "f Scroll diff view to next file"]
3039 [mc "<%s-S> Search for next hit in diff view" $M1T]
3040 [mc "<%s-R> Search for previous hit in diff view" $M1T]
3041 [mc "<%s-KP+> Increase font size" $M1T]
3042 [mc "<%s-plus> Increase font size" $M1T]
3043 [mc "<%s-KP-> Decrease font size" $M1T]
3044 [mc "<%s-minus> Decrease font size" $M1T]
3045 [mc "<F5> Update"]
3047 -justify left -bg white -border 2 -relief groove
3048 pack $w.m -side top -fill both -padx 2 -pady 2
3049 ${NS}::button $w.ok -text [mc "Close"] -command "destroy $w" -default active
3050 bind $w <Key-Escape> [list destroy $w]
3051 pack $w.ok -side bottom
3052 bind $w <Visibility> "focus $w.ok"
3053 bind $w <Key-Escape> "destroy $w"
3054 bind $w <Key-Return> "destroy $w"
3057 # Procedures for manipulating the file list window at the
3058 # bottom right of the overall window.
3060 proc treeview {w l openlevs} {
3061 global treecontents treediropen treeheight treeparent treeindex
3063 set ix 0
3064 set treeindex() 0
3065 set lev 0
3066 set prefix {}
3067 set prefixend -1
3068 set prefendstack {}
3069 set htstack {}
3070 set ht 0
3071 set treecontents() {}
3072 $w conf -state normal
3073 foreach f $l {
3074 while {[string range $f 0 $prefixend] ne $prefix} {
3075 if {$lev <= $openlevs} {
3076 $w mark set e:$treeindex($prefix) "end -1c"
3077 $w mark gravity e:$treeindex($prefix) left
3079 set treeheight($prefix) $ht
3080 incr ht [lindex $htstack end]
3081 set htstack [lreplace $htstack end end]
3082 set prefixend [lindex $prefendstack end]
3083 set prefendstack [lreplace $prefendstack end end]
3084 set prefix [string range $prefix 0 $prefixend]
3085 incr lev -1
3087 set tail [string range $f [expr {$prefixend+1}] end]
3088 while {[set slash [string first "/" $tail]] >= 0} {
3089 lappend htstack $ht
3090 set ht 0
3091 lappend prefendstack $prefixend
3092 incr prefixend [expr {$slash + 1}]
3093 set d [string range $tail 0 $slash]
3094 lappend treecontents($prefix) $d
3095 set oldprefix $prefix
3096 append prefix $d
3097 set treecontents($prefix) {}
3098 set treeindex($prefix) [incr ix]
3099 set treeparent($prefix) $oldprefix
3100 set tail [string range $tail [expr {$slash+1}] end]
3101 if {$lev <= $openlevs} {
3102 set ht 1
3103 set treediropen($prefix) [expr {$lev < $openlevs}]
3104 set bm [expr {$lev == $openlevs? "tri-rt": "tri-dn"}]
3105 $w mark set d:$ix "end -1c"
3106 $w mark gravity d:$ix left
3107 set str "\n"
3108 for {set i 0} {$i < $lev} {incr i} {append str "\t"}
3109 $w insert end $str
3110 $w image create end -align center -image $bm -padx 1 \
3111 -name a:$ix
3112 $w insert end $d [highlight_tag $prefix]
3113 $w mark set s:$ix "end -1c"
3114 $w mark gravity s:$ix left
3116 incr lev
3118 if {$tail ne {}} {
3119 if {$lev <= $openlevs} {
3120 incr ht
3121 set str "\n"
3122 for {set i 0} {$i < $lev} {incr i} {append str "\t"}
3123 $w insert end $str
3124 $w insert end $tail [highlight_tag $f]
3126 lappend treecontents($prefix) $tail
3129 while {$htstack ne {}} {
3130 set treeheight($prefix) $ht
3131 incr ht [lindex $htstack end]
3132 set htstack [lreplace $htstack end end]
3133 set prefixend [lindex $prefendstack end]
3134 set prefendstack [lreplace $prefendstack end end]
3135 set prefix [string range $prefix 0 $prefixend]
3137 $w conf -state disabled
3140 proc linetoelt {l} {
3141 global treeheight treecontents
3143 set y 2
3144 set prefix {}
3145 while {1} {
3146 foreach e $treecontents($prefix) {
3147 if {$y == $l} {
3148 return "$prefix$e"
3150 set n 1
3151 if {[string index $e end] eq "/"} {
3152 set n $treeheight($prefix$e)
3153 if {$y + $n > $l} {
3154 append prefix $e
3155 incr y
3156 break
3159 incr y $n
3164 proc highlight_tree {y prefix} {
3165 global treeheight treecontents cflist
3167 foreach e $treecontents($prefix) {
3168 set path $prefix$e
3169 if {[highlight_tag $path] ne {}} {
3170 $cflist tag add bold $y.0 "$y.0 lineend"
3172 incr y
3173 if {[string index $e end] eq "/" && $treeheight($path) > 1} {
3174 set y [highlight_tree $y $path]
3177 return $y
3180 proc treeclosedir {w dir} {
3181 global treediropen treeheight treeparent treeindex
3183 set ix $treeindex($dir)
3184 $w conf -state normal
3185 $w delete s:$ix e:$ix
3186 set treediropen($dir) 0
3187 $w image configure a:$ix -image tri-rt
3188 $w conf -state disabled
3189 set n [expr {1 - $treeheight($dir)}]
3190 while {$dir ne {}} {
3191 incr treeheight($dir) $n
3192 set dir $treeparent($dir)
3196 proc treeopendir {w dir} {
3197 global treediropen treeheight treeparent treecontents treeindex
3199 set ix $treeindex($dir)
3200 $w conf -state normal
3201 $w image configure a:$ix -image tri-dn
3202 $w mark set e:$ix s:$ix
3203 $w mark gravity e:$ix right
3204 set lev 0
3205 set str "\n"
3206 set n [llength $treecontents($dir)]
3207 for {set x $dir} {$x ne {}} {set x $treeparent($x)} {
3208 incr lev
3209 append str "\t"
3210 incr treeheight($x) $n
3212 foreach e $treecontents($dir) {
3213 set de $dir$e
3214 if {[string index $e end] eq "/"} {
3215 set iy $treeindex($de)
3216 $w mark set d:$iy e:$ix
3217 $w mark gravity d:$iy left
3218 $w insert e:$ix $str
3219 set treediropen($de) 0
3220 $w image create e:$ix -align center -image tri-rt -padx 1 \
3221 -name a:$iy
3222 $w insert e:$ix $e [highlight_tag $de]
3223 $w mark set s:$iy e:$ix
3224 $w mark gravity s:$iy left
3225 set treeheight($de) 1
3226 } else {
3227 $w insert e:$ix $str
3228 $w insert e:$ix $e [highlight_tag $de]
3231 $w mark gravity e:$ix right
3232 $w conf -state disabled
3233 set treediropen($dir) 1
3234 set top [lindex [split [$w index @0,0] .] 0]
3235 set ht [$w cget -height]
3236 set l [lindex [split [$w index s:$ix] .] 0]
3237 if {$l < $top} {
3238 $w yview $l.0
3239 } elseif {$l + $n + 1 > $top + $ht} {
3240 set top [expr {$l + $n + 2 - $ht}]
3241 if {$l < $top} {
3242 set top $l
3244 $w yview $top.0
3248 proc treeclick {w x y} {
3249 global treediropen cmitmode ctext cflist cflist_top
3251 if {$cmitmode ne "tree"} return
3252 if {![info exists cflist_top]} return
3253 set l [lindex [split [$w index "@$x,$y"] "."] 0]
3254 $cflist tag remove highlight $cflist_top.0 "$cflist_top.0 lineend"
3255 $cflist tag add highlight $l.0 "$l.0 lineend"
3256 set cflist_top $l
3257 if {$l == 1} {
3258 $ctext yview 1.0
3259 return
3261 set e [linetoelt $l]
3262 if {[string index $e end] ne "/"} {
3263 showfile $e
3264 } elseif {$treediropen($e)} {
3265 treeclosedir $w $e
3266 } else {
3267 treeopendir $w $e
3271 proc setfilelist {id} {
3272 global treefilelist cflist jump_to_here
3274 treeview $cflist $treefilelist($id) 0
3275 if {$jump_to_here ne {}} {
3276 set f [lindex $jump_to_here 0]
3277 if {[lsearch -exact $treefilelist($id) $f] >= 0} {
3278 showfile $f
3283 image create bitmap tri-rt -background black -foreground blue -data {
3284 #define tri-rt_width 13
3285 #define tri-rt_height 13
3286 static unsigned char tri-rt_bits[] = {
3287 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x30, 0x00, 0x70, 0x00, 0xf0, 0x00,
3288 0xf0, 0x01, 0xf0, 0x00, 0x70, 0x00, 0x30, 0x00, 0x10, 0x00, 0x00, 0x00,
3289 0x00, 0x00};
3290 } -maskdata {
3291 #define tri-rt-mask_width 13
3292 #define tri-rt-mask_height 13
3293 static unsigned char tri-rt-mask_bits[] = {
3294 0x08, 0x00, 0x18, 0x00, 0x38, 0x00, 0x78, 0x00, 0xf8, 0x00, 0xf8, 0x01,
3295 0xf8, 0x03, 0xf8, 0x01, 0xf8, 0x00, 0x78, 0x00, 0x38, 0x00, 0x18, 0x00,
3296 0x08, 0x00};
3298 image create bitmap tri-dn -background black -foreground blue -data {
3299 #define tri-dn_width 13
3300 #define tri-dn_height 13
3301 static unsigned char tri-dn_bits[] = {
3302 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0x07, 0xf8, 0x03,
3303 0xf0, 0x01, 0xe0, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3304 0x00, 0x00};
3305 } -maskdata {
3306 #define tri-dn-mask_width 13
3307 #define tri-dn-mask_height 13
3308 static unsigned char tri-dn-mask_bits[] = {
3309 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x1f, 0xfe, 0x0f, 0xfc, 0x07,
3310 0xf8, 0x03, 0xf0, 0x01, 0xe0, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00,
3311 0x00, 0x00};
3314 image create bitmap reficon-T -background black -foreground yellow -data {
3315 #define tagicon_width 13
3316 #define tagicon_height 9
3317 static unsigned char tagicon_bits[] = {
3318 0x00, 0x00, 0x00, 0x00, 0xf0, 0x07, 0xf8, 0x07,
3319 0xfc, 0x07, 0xf8, 0x07, 0xf0, 0x07, 0x00, 0x00, 0x00, 0x00};
3320 } -maskdata {
3321 #define tagicon-mask_width 13
3322 #define tagicon-mask_height 9
3323 static unsigned char tagicon-mask_bits[] = {
3324 0x00, 0x00, 0xf0, 0x0f, 0xf8, 0x0f, 0xfc, 0x0f,
3325 0xfe, 0x0f, 0xfc, 0x0f, 0xf8, 0x0f, 0xf0, 0x0f, 0x00, 0x00};
3327 set rectdata {
3328 #define headicon_width 13
3329 #define headicon_height 9
3330 static unsigned char headicon_bits[] = {
3331 0x00, 0x00, 0x00, 0x00, 0xf8, 0x07, 0xf8, 0x07,
3332 0xf8, 0x07, 0xf8, 0x07, 0xf8, 0x07, 0x00, 0x00, 0x00, 0x00};
3334 set rectmask {
3335 #define headicon-mask_width 13
3336 #define headicon-mask_height 9
3337 static unsigned char headicon-mask_bits[] = {
3338 0x00, 0x00, 0xfc, 0x0f, 0xfc, 0x0f, 0xfc, 0x0f,
3339 0xfc, 0x0f, 0xfc, 0x0f, 0xfc, 0x0f, 0xfc, 0x0f, 0x00, 0x00};
3341 image create bitmap reficon-H -background black -foreground green \
3342 -data $rectdata -maskdata $rectmask
3343 image create bitmap reficon-o -background black -foreground "#ddddff" \
3344 -data $rectdata -maskdata $rectmask
3346 proc init_flist {first} {
3347 global cflist cflist_top difffilestart
3349 $cflist conf -state normal
3350 $cflist delete 0.0 end
3351 if {$first ne {}} {
3352 $cflist insert end $first
3353 set cflist_top 1
3354 $cflist tag add highlight 1.0 "1.0 lineend"
3355 } else {
3356 catch {unset cflist_top}
3358 $cflist conf -state disabled
3359 set difffilestart {}
3362 proc highlight_tag {f} {
3363 global highlight_paths
3365 foreach p $highlight_paths {
3366 if {[string match $p $f]} {
3367 return "bold"
3370 return {}
3373 proc highlight_filelist {} {
3374 global cmitmode cflist
3376 $cflist conf -state normal
3377 if {$cmitmode ne "tree"} {
3378 set end [lindex [split [$cflist index end] .] 0]
3379 for {set l 2} {$l < $end} {incr l} {
3380 set line [$cflist get $l.0 "$l.0 lineend"]
3381 if {[highlight_tag $line] ne {}} {
3382 $cflist tag add bold $l.0 "$l.0 lineend"
3385 } else {
3386 highlight_tree 2 {}
3388 $cflist conf -state disabled
3391 proc unhighlight_filelist {} {
3392 global cflist
3394 $cflist conf -state normal
3395 $cflist tag remove bold 1.0 end
3396 $cflist conf -state disabled
3399 proc add_flist {fl} {
3400 global cflist
3402 $cflist conf -state normal
3403 foreach f $fl {
3404 $cflist insert end "\n"
3405 $cflist insert end $f [highlight_tag $f]
3407 $cflist conf -state disabled
3410 proc sel_flist {w x y} {
3411 global ctext difffilestart cflist cflist_top cmitmode
3413 if {$cmitmode eq "tree"} return
3414 if {![info exists cflist_top]} return
3415 set l [lindex [split [$w index "@$x,$y"] "."] 0]
3416 $cflist tag remove highlight $cflist_top.0 "$cflist_top.0 lineend"
3417 $cflist tag add highlight $l.0 "$l.0 lineend"
3418 set cflist_top $l
3419 if {$l == 1} {
3420 $ctext yview 1.0
3421 } else {
3422 catch {$ctext yview [lindex $difffilestart [expr {$l - 2}]]}
3424 suppress_highlighting_file_for_current_scrollpos
3427 proc pop_flist_menu {w X Y x y} {
3428 global ctext cflist cmitmode flist_menu flist_menu_file
3429 global treediffs diffids
3431 stopfinding
3432 set l [lindex [split [$w index "@$x,$y"] "."] 0]
3433 if {$l <= 1} return
3434 if {$cmitmode eq "tree"} {
3435 set e [linetoelt $l]
3436 if {[string index $e end] eq "/"} return
3437 } else {
3438 set e [lindex $treediffs($diffids) [expr {$l-2}]]
3440 set flist_menu_file $e
3441 set xdiffstate "normal"
3442 if {$cmitmode eq "tree"} {
3443 set xdiffstate "disabled"
3445 # Disable "External diff" item in tree mode
3446 $flist_menu entryconf 2 -state $xdiffstate
3447 tk_popup $flist_menu $X $Y
3450 proc find_ctext_fileinfo {line} {
3451 global ctext_file_names ctext_file_lines
3453 set ok [bsearch $ctext_file_lines $line]
3454 set tline [lindex $ctext_file_lines $ok]
3456 if {$ok >= [llength $ctext_file_lines] || $line < $tline} {
3457 return {}
3458 } else {
3459 return [list [lindex $ctext_file_names $ok] $tline]
3463 proc pop_diff_menu {w X Y x y} {
3464 global ctext diff_menu flist_menu_file
3465 global diff_menu_txtpos diff_menu_line
3466 global diff_menu_filebase
3468 set diff_menu_txtpos [split [$w index "@$x,$y"] "."]
3469 set diff_menu_line [lindex $diff_menu_txtpos 0]
3470 # don't pop up the menu on hunk-separator or file-separator lines
3471 if {[lsearch -glob [$ctext tag names $diff_menu_line.0] "*sep"] >= 0} {
3472 return
3474 stopfinding
3475 set f [find_ctext_fileinfo $diff_menu_line]
3476 if {$f eq {}} return
3477 set flist_menu_file [lindex $f 0]
3478 set diff_menu_filebase [lindex $f 1]
3479 tk_popup $diff_menu $X $Y
3482 proc flist_hl {only} {
3483 global flist_menu_file findstring gdttype
3485 set x [shellquote $flist_menu_file]
3486 if {$only || $findstring eq {} || $gdttype ne [mc "touching paths:"]} {
3487 set findstring $x
3488 } else {
3489 append findstring " " $x
3491 set gdttype [mc "touching paths:"]
3494 proc gitknewtmpdir {} {
3495 global diffnum gitktmpdir gitdir
3497 if {![info exists gitktmpdir]} {
3498 set gitktmpdir [file join $gitdir [format ".gitk-tmp.%s" [pid]]]
3499 if {[catch {file mkdir $gitktmpdir} err]} {
3500 error_popup "[mc "Error creating temporary directory %s:" $gitktmpdir] $err"
3501 unset gitktmpdir
3502 return {}
3504 set diffnum 0
3506 incr diffnum
3507 set diffdir [file join $gitktmpdir $diffnum]
3508 if {[catch {file mkdir $diffdir} err]} {
3509 error_popup "[mc "Error creating temporary directory %s:" $diffdir] $err"
3510 return {}
3512 return $diffdir
3515 proc save_file_from_commit {filename output what} {
3516 global nullfile
3518 if {[catch {exec git show $filename -- > $output} err]} {
3519 if {[string match "fatal: bad revision *" $err]} {
3520 return $nullfile
3522 error_popup "[mc "Error getting \"%s\" from %s:" $filename $what] $err"
3523 return {}
3525 return $output
3528 proc external_diff_get_one_file {diffid filename diffdir} {
3529 global nullid nullid2 nullfile
3530 global worktree
3532 if {$diffid == $nullid} {
3533 set difffile [file join $worktree $filename]
3534 if {[file exists $difffile]} {
3535 return $difffile
3537 return $nullfile
3539 if {$diffid == $nullid2} {
3540 set difffile [file join $diffdir "\[index\] [file tail $filename]"]
3541 return [save_file_from_commit :$filename $difffile index]
3543 set difffile [file join $diffdir "\[$diffid\] [file tail $filename]"]
3544 return [save_file_from_commit $diffid:$filename $difffile \
3545 "revision $diffid"]
3548 proc external_diff {} {
3549 global nullid nullid2
3550 global flist_menu_file
3551 global diffids
3552 global extdifftool
3554 if {[llength $diffids] == 1} {
3555 # no reference commit given
3556 set diffidto [lindex $diffids 0]
3557 if {$diffidto eq $nullid} {
3558 # diffing working copy with index
3559 set diffidfrom $nullid2
3560 } elseif {$diffidto eq $nullid2} {
3561 # diffing index with HEAD
3562 set diffidfrom "HEAD"
3563 } else {
3564 # use first parent commit
3565 global parentlist selectedline
3566 set diffidfrom [lindex $parentlist $selectedline 0]
3568 } else {
3569 set diffidfrom [lindex $diffids 0]
3570 set diffidto [lindex $diffids 1]
3573 # make sure that several diffs wont collide
3574 set diffdir [gitknewtmpdir]
3575 if {$diffdir eq {}} return
3577 # gather files to diff
3578 set difffromfile [external_diff_get_one_file $diffidfrom $flist_menu_file $diffdir]
3579 set difftofile [external_diff_get_one_file $diffidto $flist_menu_file $diffdir]
3581 if {$difffromfile ne {} && $difftofile ne {}} {
3582 set cmd [list [shellsplit $extdifftool] $difffromfile $difftofile]
3583 if {[catch {set fl [open |$cmd r]} err]} {
3584 file delete -force $diffdir
3585 error_popup "$extdifftool: [mc "command failed:"] $err"
3586 } else {
3587 fconfigure $fl -blocking 0
3588 filerun $fl [list delete_at_eof $fl $diffdir]
3593 proc find_hunk_blamespec {base line} {
3594 global ctext
3596 # Find and parse the hunk header
3597 set s_lix [$ctext search -backwards -regexp ^@@ "$line.0 lineend" $base.0]
3598 if {$s_lix eq {}} return
3600 set s_line [$ctext get $s_lix "$s_lix + 1 lines"]
3601 if {![regexp {^@@@*(( -\d+(,\d+)?)+) \+(\d+)(,\d+)? @@} $s_line \
3602 s_line old_specs osz osz1 new_line nsz]} {
3603 return
3606 # base lines for the parents
3607 set base_lines [list $new_line]
3608 foreach old_spec [lrange [split $old_specs " "] 1 end] {
3609 if {![regexp -- {-(\d+)(,\d+)?} $old_spec \
3610 old_spec old_line osz]} {
3611 return
3613 lappend base_lines $old_line
3616 # Now scan the lines to determine offset within the hunk
3617 set max_parent [expr {[llength $base_lines]-2}]
3618 set dline 0
3619 set s_lno [lindex [split $s_lix "."] 0]
3621 # Determine if the line is removed
3622 set chunk [$ctext get $line.0 "$line.1 + $max_parent chars"]
3623 if {[string match {[-+ ]*} $chunk]} {
3624 set removed_idx [string first "-" $chunk]
3625 # Choose a parent index
3626 if {$removed_idx >= 0} {
3627 set parent $removed_idx
3628 } else {
3629 set unchanged_idx [string first " " $chunk]
3630 if {$unchanged_idx >= 0} {
3631 set parent $unchanged_idx
3632 } else {
3633 # blame the current commit
3634 set parent -1
3637 # then count other lines that belong to it
3638 for {set i $line} {[incr i -1] > $s_lno} {} {
3639 set chunk [$ctext get $i.0 "$i.1 + $max_parent chars"]
3640 # Determine if the line is removed
3641 set removed_idx [string first "-" $chunk]
3642 if {$parent >= 0} {
3643 set code [string index $chunk $parent]
3644 if {$code eq "-" || ($removed_idx < 0 && $code ne "+")} {
3645 incr dline
3647 } else {
3648 if {$removed_idx < 0} {
3649 incr dline
3653 incr parent
3654 } else {
3655 set parent 0
3658 incr dline [lindex $base_lines $parent]
3659 return [list $parent $dline]
3662 proc external_blame_diff {} {
3663 global currentid cmitmode
3664 global diff_menu_txtpos diff_menu_line
3665 global diff_menu_filebase flist_menu_file
3667 if {$cmitmode eq "tree"} {
3668 set parent_idx 0
3669 set line [expr {$diff_menu_line - $diff_menu_filebase}]
3670 } else {
3671 set hinfo [find_hunk_blamespec $diff_menu_filebase $diff_menu_line]
3672 if {$hinfo ne {}} {
3673 set parent_idx [lindex $hinfo 0]
3674 set line [lindex $hinfo 1]
3675 } else {
3676 set parent_idx 0
3677 set line 0
3681 external_blame $parent_idx $line
3684 # Find the SHA1 ID of the blob for file $fname in the index
3685 # at stage 0 or 2
3686 proc index_sha1 {fname} {
3687 set f [open [list | git ls-files -s $fname] r]
3688 while {[gets $f line] >= 0} {
3689 set info [lindex [split $line "\t"] 0]
3690 set stage [lindex $info 2]
3691 if {$stage eq "0" || $stage eq "2"} {
3692 close $f
3693 return [lindex $info 1]
3696 close $f
3697 return {}
3700 # Turn an absolute path into one relative to the current directory
3701 proc make_relative {f} {
3702 if {[file pathtype $f] eq "relative"} {
3703 return $f
3705 set elts [file split $f]
3706 set here [file split [pwd]]
3707 set ei 0
3708 set hi 0
3709 set res {}
3710 foreach d $here {
3711 if {$ei < $hi || $ei >= [llength $elts] || [lindex $elts $ei] ne $d} {
3712 lappend res ".."
3713 } else {
3714 incr ei
3716 incr hi
3718 set elts [concat $res [lrange $elts $ei end]]
3719 return [eval file join $elts]
3722 proc external_blame {parent_idx {line {}}} {
3723 global flist_menu_file cdup
3724 global nullid nullid2
3725 global parentlist selectedline currentid
3727 if {$parent_idx > 0} {
3728 set base_commit [lindex $parentlist $selectedline [expr {$parent_idx-1}]]
3729 } else {
3730 set base_commit $currentid
3733 if {$base_commit eq {} || $base_commit eq $nullid || $base_commit eq $nullid2} {
3734 error_popup [mc "No such commit"]
3735 return
3738 set cmdline [list git gui blame]
3739 if {$line ne {} && $line > 1} {
3740 lappend cmdline "--line=$line"
3742 set f [file join $cdup $flist_menu_file]
3743 # Unfortunately it seems git gui blame doesn't like
3744 # being given an absolute path...
3745 set f [make_relative $f]
3746 lappend cmdline $base_commit $f
3747 if {[catch {eval exec $cmdline &} err]} {
3748 error_popup "[mc "git gui blame: command failed:"] $err"
3752 proc show_line_source {} {
3753 global cmitmode currentid parents curview blamestuff blameinst
3754 global diff_menu_line diff_menu_filebase flist_menu_file
3755 global nullid nullid2 gitdir cdup
3757 set from_index {}
3758 if {$cmitmode eq "tree"} {
3759 set id $currentid
3760 set line [expr {$diff_menu_line - $diff_menu_filebase}]
3761 } else {
3762 set h [find_hunk_blamespec $diff_menu_filebase $diff_menu_line]
3763 if {$h eq {}} return
3764 set pi [lindex $h 0]
3765 if {$pi == 0} {
3766 mark_ctext_line $diff_menu_line
3767 return
3769 incr pi -1
3770 if {$currentid eq $nullid} {
3771 if {$pi > 0} {
3772 # must be a merge in progress...
3773 if {[catch {
3774 # get the last line from .git/MERGE_HEAD
3775 set f [open [file join $gitdir MERGE_HEAD] r]
3776 set id [lindex [split [read $f] "\n"] end-1]
3777 close $f
3778 } err]} {
3779 error_popup [mc "Couldn't read merge head: %s" $err]
3780 return
3782 } elseif {$parents($curview,$currentid) eq $nullid2} {
3783 # need to do the blame from the index
3784 if {[catch {
3785 set from_index [index_sha1 $flist_menu_file]
3786 } err]} {
3787 error_popup [mc "Error reading index: %s" $err]
3788 return
3790 } else {
3791 set id $parents($curview,$currentid)
3793 } else {
3794 set id [lindex $parents($curview,$currentid) $pi]
3796 set line [lindex $h 1]
3798 set blameargs {}
3799 if {$from_index ne {}} {
3800 lappend blameargs | git cat-file blob $from_index
3802 lappend blameargs | git blame -p -L$line,+1
3803 if {$from_index ne {}} {
3804 lappend blameargs --contents -
3805 } else {
3806 lappend blameargs $id
3808 lappend blameargs -- [file join $cdup $flist_menu_file]
3809 if {[catch {
3810 set f [open $blameargs r]
3811 } err]} {
3812 error_popup [mc "Couldn't start git blame: %s" $err]
3813 return
3815 nowbusy blaming [mc "Searching"]
3816 fconfigure $f -blocking 0
3817 set i [reg_instance $f]
3818 set blamestuff($i) {}
3819 set blameinst $i
3820 filerun $f [list read_line_source $f $i]
3823 proc stopblaming {} {
3824 global blameinst
3826 if {[info exists blameinst]} {
3827 stop_instance $blameinst
3828 unset blameinst
3829 notbusy blaming
3833 proc read_line_source {fd inst} {
3834 global blamestuff curview commfd blameinst nullid nullid2
3836 while {[gets $fd line] >= 0} {
3837 lappend blamestuff($inst) $line
3839 if {![eof $fd]} {
3840 return 1
3842 unset commfd($inst)
3843 unset blameinst
3844 notbusy blaming
3845 fconfigure $fd -blocking 1
3846 if {[catch {close $fd} err]} {
3847 error_popup [mc "Error running git blame: %s" $err]
3848 return 0
3851 set fname {}
3852 set line [split [lindex $blamestuff($inst) 0] " "]
3853 set id [lindex $line 0]
3854 set lnum [lindex $line 1]
3855 if {[string length $id] == 40 && [string is xdigit $id] &&
3856 [string is digit -strict $lnum]} {
3857 # look for "filename" line
3858 foreach l $blamestuff($inst) {
3859 if {[string match "filename *" $l]} {
3860 set fname [string range $l 9 end]
3861 break
3865 if {$fname ne {}} {
3866 # all looks good, select it
3867 if {$id eq $nullid} {
3868 # blame uses all-zeroes to mean not committed,
3869 # which would mean a change in the index
3870 set id $nullid2
3872 if {[commitinview $id $curview]} {
3873 selectline [rowofcommit $id] 1 [list $fname $lnum]
3874 } else {
3875 error_popup [mc "That line comes from commit %s, \
3876 which is not in this view" [shortids $id]]
3878 } else {
3879 puts "oops couldn't parse git blame output"
3881 return 0
3884 # delete $dir when we see eof on $f (presumably because the child has exited)
3885 proc delete_at_eof {f dir} {
3886 while {[gets $f line] >= 0} {}
3887 if {[eof $f]} {
3888 if {[catch {close $f} err]} {
3889 error_popup "[mc "External diff viewer failed:"] $err"
3891 file delete -force $dir
3892 return 0
3894 return 1
3897 # Functions for adding and removing shell-type quoting
3899 proc shellquote {str} {
3900 if {![string match "*\['\"\\ \t]*" $str]} {
3901 return $str
3903 if {![string match "*\['\"\\]*" $str]} {
3904 return "\"$str\""
3906 if {![string match "*'*" $str]} {
3907 return "'$str'"
3909 return "\"[string map {\" \\\" \\ \\\\} $str]\""
3912 proc shellarglist {l} {
3913 set str {}
3914 foreach a $l {
3915 if {$str ne {}} {
3916 append str " "
3918 append str [shellquote $a]
3920 return $str
3923 proc shelldequote {str} {
3924 set ret {}
3925 set used -1
3926 while {1} {
3927 incr used
3928 if {![regexp -start $used -indices "\['\"\\\\ \t]" $str first]} {
3929 append ret [string range $str $used end]
3930 set used [string length $str]
3931 break
3933 set first [lindex $first 0]
3934 set ch [string index $str $first]
3935 if {$first > $used} {
3936 append ret [string range $str $used [expr {$first - 1}]]
3937 set used $first
3939 if {$ch eq " " || $ch eq "\t"} break
3940 incr used
3941 if {$ch eq "'"} {
3942 set first [string first "'" $str $used]
3943 if {$first < 0} {
3944 error "unmatched single-quote"
3946 append ret [string range $str $used [expr {$first - 1}]]
3947 set used $first
3948 continue
3950 if {$ch eq "\\"} {
3951 if {$used >= [string length $str]} {
3952 error "trailing backslash"
3954 append ret [string index $str $used]
3955 continue
3957 # here ch == "\""
3958 while {1} {
3959 if {![regexp -start $used -indices "\[\"\\\\]" $str first]} {
3960 error "unmatched double-quote"
3962 set first [lindex $first 0]
3963 set ch [string index $str $first]
3964 if {$first > $used} {
3965 append ret [string range $str $used [expr {$first - 1}]]
3966 set used $first
3968 if {$ch eq "\""} break
3969 incr used
3970 append ret [string index $str $used]
3971 incr used
3974 return [list $used $ret]
3977 proc shellsplit {str} {
3978 set l {}
3979 while {1} {
3980 set str [string trimleft $str]
3981 if {$str eq {}} break
3982 set dq [shelldequote $str]
3983 set n [lindex $dq 0]
3984 set word [lindex $dq 1]
3985 set str [string range $str $n end]
3986 lappend l $word
3988 return $l
3991 # Code to implement multiple views
3993 proc newview {ishighlight} {
3994 global nextviewnum newviewname newishighlight
3995 global revtreeargs viewargscmd newviewopts curview
3997 set newishighlight $ishighlight
3998 set top .gitkview
3999 if {[winfo exists $top]} {
4000 raise $top
4001 return
4003 decode_view_opts $nextviewnum $revtreeargs
4004 set newviewname($nextviewnum) "[mc "View"] $nextviewnum"
4005 set newviewopts($nextviewnum,perm) 0
4006 set newviewopts($nextviewnum,cmd) $viewargscmd($curview)
4007 vieweditor $top $nextviewnum [mc "Gitk view definition"]
4010 set known_view_options {
4011 {perm b . {} {mc "Remember this view"}}
4012 {reflabel l + {} {mc "References (space separated list):"}}
4013 {refs t15 .. {} {mc "Branches & tags:"}}
4014 {allrefs b *. "--all" {mc "All refs"}}
4015 {branches b . "--branches" {mc "All (local) branches"}}
4016 {tags b . "--tags" {mc "All tags"}}
4017 {remotes b . "--remotes" {mc "All remote-tracking branches"}}
4018 {commitlbl l + {} {mc "Commit Info (regular expressions):"}}
4019 {author t15 .. "--author=*" {mc "Author:"}}
4020 {committer t15 . "--committer=*" {mc "Committer:"}}
4021 {loginfo t15 .. "--grep=*" {mc "Commit Message:"}}
4022 {allmatch b .. "--all-match" {mc "Matches all Commit Info criteria"}}
4023 {changes_l l + {} {mc "Changes to Files:"}}
4024 {pickaxe_s r0 . {} {mc "Fixed String"}}
4025 {pickaxe_t r1 . "--pickaxe-regex" {mc "Regular Expression"}}
4026 {pickaxe t15 .. "-S*" {mc "Search string:"}}
4027 {datelabel l + {} {mc "Commit Dates (\"2 weeks ago\", \"2009-03-17 15:27:38\", \"March 17, 2009 15:27:38\"):"}}
4028 {since t15 .. {"--since=*" "--after=*"} {mc "Since:"}}
4029 {until t15 . {"--until=*" "--before=*"} {mc "Until:"}}
4030 {limit_lbl l + {} {mc "Limit and/or skip a number of revisions (positive integer):"}}
4031 {limit t10 *. "--max-count=*" {mc "Number to show:"}}
4032 {skip t10 . "--skip=*" {mc "Number to skip:"}}
4033 {misc_lbl l + {} {mc "Miscellaneous options:"}}
4034 {dorder b *. {"--date-order" "-d"} {mc "Strictly sort by date"}}
4035 {lright b . "--left-right" {mc "Mark branch sides"}}
4036 {first b . "--first-parent" {mc "Limit to first parent"}}
4037 {smplhst b . "--simplify-by-decoration" {mc "Simple history"}}
4038 {args t50 *. {} {mc "Additional arguments to git log:"}}
4039 {allpaths path + {} {mc "Enter files and directories to include, one per line:"}}
4040 {cmd t50= + {} {mc "Command to generate more commits to include:"}}
4043 # Convert $newviewopts($n, ...) into args for git log.
4044 proc encode_view_opts {n} {
4045 global known_view_options newviewopts
4047 set rargs [list]
4048 foreach opt $known_view_options {
4049 set patterns [lindex $opt 3]
4050 if {$patterns eq {}} continue
4051 set pattern [lindex $patterns 0]
4053 if {[lindex $opt 1] eq "b"} {
4054 set val $newviewopts($n,[lindex $opt 0])
4055 if {$val} {
4056 lappend rargs $pattern
4058 } elseif {[regexp {^r(\d+)$} [lindex $opt 1] type value]} {
4059 regexp {^(.*_)} [lindex $opt 0] uselessvar button_id
4060 set val $newviewopts($n,$button_id)
4061 if {$val eq $value} {
4062 lappend rargs $pattern
4064 } else {
4065 set val $newviewopts($n,[lindex $opt 0])
4066 set val [string trim $val]
4067 if {$val ne {}} {
4068 set pfix [string range $pattern 0 end-1]
4069 lappend rargs $pfix$val
4073 set rargs [concat $rargs [shellsplit $newviewopts($n,refs)]]
4074 return [concat $rargs [shellsplit $newviewopts($n,args)]]
4077 # Fill $newviewopts($n, ...) based on args for git log.
4078 proc decode_view_opts {n view_args} {
4079 global known_view_options newviewopts
4081 foreach opt $known_view_options {
4082 set id [lindex $opt 0]
4083 if {[lindex $opt 1] eq "b"} {
4084 # Checkboxes
4085 set val 0
4086 } elseif {[regexp {^r(\d+)$} [lindex $opt 1]]} {
4087 # Radiobuttons
4088 regexp {^(.*_)} $id uselessvar id
4089 set val 0
4090 } else {
4091 # Text fields
4092 set val {}
4094 set newviewopts($n,$id) $val
4096 set oargs [list]
4097 set refargs [list]
4098 foreach arg $view_args {
4099 if {[regexp -- {^-([0-9]+)$} $arg arg cnt]
4100 && ![info exists found(limit)]} {
4101 set newviewopts($n,limit) $cnt
4102 set found(limit) 1
4103 continue
4105 catch { unset val }
4106 foreach opt $known_view_options {
4107 set id [lindex $opt 0]
4108 if {[info exists found($id)]} continue
4109 foreach pattern [lindex $opt 3] {
4110 if {![string match $pattern $arg]} continue
4111 if {[lindex $opt 1] eq "b"} {
4112 # Check buttons
4113 set val 1
4114 } elseif {[regexp {^r(\d+)$} [lindex $opt 1] match num]} {
4115 # Radio buttons
4116 regexp {^(.*_)} $id uselessvar id
4117 set val $num
4118 } else {
4119 # Text input fields
4120 set size [string length $pattern]
4121 set val [string range $arg [expr {$size-1}] end]
4123 set newviewopts($n,$id) $val
4124 set found($id) 1
4125 break
4127 if {[info exists val]} break
4129 if {[info exists val]} continue
4130 if {[regexp {^-} $arg]} {
4131 lappend oargs $arg
4132 } else {
4133 lappend refargs $arg
4136 set newviewopts($n,refs) [shellarglist $refargs]
4137 set newviewopts($n,args) [shellarglist $oargs]
4140 proc edit_or_newview {} {
4141 global curview
4143 if {$curview > 0} {
4144 editview
4145 } else {
4146 newview 0
4150 proc editview {} {
4151 global curview
4152 global viewname viewperm newviewname newviewopts
4153 global viewargs viewargscmd
4155 set top .gitkvedit-$curview
4156 if {[winfo exists $top]} {
4157 raise $top
4158 return
4160 decode_view_opts $curview $viewargs($curview)
4161 set newviewname($curview) $viewname($curview)
4162 set newviewopts($curview,perm) $viewperm($curview)
4163 set newviewopts($curview,cmd) $viewargscmd($curview)
4164 vieweditor $top $curview "[mc "Gitk: edit view"] $viewname($curview)"
4167 proc vieweditor {top n title} {
4168 global newviewname newviewopts viewfiles bgcolor
4169 global known_view_options NS
4171 ttk_toplevel $top
4172 wm title $top [concat $title [mc "-- criteria for selecting revisions"]]
4173 make_transient $top .
4175 # View name
4176 ${NS}::frame $top.nfr
4177 ${NS}::label $top.nl -text [mc "View Name"]
4178 ${NS}::entry $top.name -width 20 -textvariable newviewname($n)
4179 pack $top.nfr -in $top -fill x -pady 5 -padx 3
4180 pack $top.nl -in $top.nfr -side left -padx {0 5}
4181 pack $top.name -in $top.nfr -side left -padx {0 25}
4183 # View options
4184 set cframe $top.nfr
4185 set cexpand 0
4186 set cnt 0
4187 foreach opt $known_view_options {
4188 set id [lindex $opt 0]
4189 set type [lindex $opt 1]
4190 set flags [lindex $opt 2]
4191 set title [eval [lindex $opt 4]]
4192 set lxpad 0
4194 if {$flags eq "+" || $flags eq "*"} {
4195 set cframe $top.fr$cnt
4196 incr cnt
4197 ${NS}::frame $cframe
4198 pack $cframe -in $top -fill x -pady 3 -padx 3
4199 set cexpand [expr {$flags eq "*"}]
4200 } elseif {$flags eq ".." || $flags eq "*."} {
4201 set cframe $top.fr$cnt
4202 incr cnt
4203 ${NS}::frame $cframe
4204 pack $cframe -in $top -fill x -pady 3 -padx [list 15 3]
4205 set cexpand [expr {$flags eq "*."}]
4206 } else {
4207 set lxpad 5
4210 if {$type eq "l"} {
4211 ${NS}::label $cframe.l_$id -text $title
4212 pack $cframe.l_$id -in $cframe -side left -pady [list 3 0] -anchor w
4213 } elseif {$type eq "b"} {
4214 ${NS}::checkbutton $cframe.c_$id -text $title -variable newviewopts($n,$id)
4215 pack $cframe.c_$id -in $cframe -side left \
4216 -padx [list $lxpad 0] -expand $cexpand -anchor w
4217 } elseif {[regexp {^r(\d+)$} $type type sz]} {
4218 regexp {^(.*_)} $id uselessvar button_id
4219 ${NS}::radiobutton $cframe.c_$id -text $title -variable newviewopts($n,$button_id) -value $sz
4220 pack $cframe.c_$id -in $cframe -side left \
4221 -padx [list $lxpad 0] -expand $cexpand -anchor w
4222 } elseif {[regexp {^t(\d+)$} $type type sz]} {
4223 ${NS}::label $cframe.l_$id -text $title
4224 ${NS}::entry $cframe.e_$id -width $sz -background $bgcolor \
4225 -textvariable newviewopts($n,$id)
4226 pack $cframe.l_$id -in $cframe -side left -padx [list $lxpad 0]
4227 pack $cframe.e_$id -in $cframe -side left -expand 1 -fill x
4228 } elseif {[regexp {^t(\d+)=$} $type type sz]} {
4229 ${NS}::label $cframe.l_$id -text $title
4230 ${NS}::entry $cframe.e_$id -width $sz -background $bgcolor \
4231 -textvariable newviewopts($n,$id)
4232 pack $cframe.l_$id -in $cframe -side top -pady [list 3 0] -anchor w
4233 pack $cframe.e_$id -in $cframe -side top -fill x
4234 } elseif {$type eq "path"} {
4235 ${NS}::label $top.l -text $title
4236 pack $top.l -in $top -side top -pady [list 3 0] -anchor w -padx 3
4237 text $top.t -width 40 -height 5 -background $bgcolor
4238 if {[info exists viewfiles($n)]} {
4239 foreach f $viewfiles($n) {
4240 $top.t insert end $f
4241 $top.t insert end "\n"
4243 $top.t delete {end - 1c} end
4244 $top.t mark set insert 0.0
4246 pack $top.t -in $top -side top -pady [list 0 5] -fill both -expand 1 -padx 3
4250 ${NS}::frame $top.buts
4251 ${NS}::button $top.buts.ok -text [mc "OK"] -command [list newviewok $top $n]
4252 ${NS}::button $top.buts.apply -text [mc "Apply (F5)"] -command [list newviewok $top $n 1]
4253 ${NS}::button $top.buts.can -text [mc "Cancel"] -command [list destroy $top]
4254 bind $top <Control-Return> [list newviewok $top $n]
4255 bind $top <F5> [list newviewok $top $n 1]
4256 bind $top <Escape> [list destroy $top]
4257 grid $top.buts.ok $top.buts.apply $top.buts.can
4258 grid columnconfigure $top.buts 0 -weight 1 -uniform a
4259 grid columnconfigure $top.buts 1 -weight 1 -uniform a
4260 grid columnconfigure $top.buts 2 -weight 1 -uniform a
4261 pack $top.buts -in $top -side top -fill x
4262 focus $top.t
4265 proc doviewmenu {m first cmd op argv} {
4266 set nmenu [$m index end]
4267 for {set i $first} {$i <= $nmenu} {incr i} {
4268 if {[$m entrycget $i -command] eq $cmd} {
4269 eval $m $op $i $argv
4270 break
4275 proc allviewmenus {n op args} {
4276 # global viewhlmenu
4278 doviewmenu .bar.view 5 [list showview $n] $op $args
4279 # doviewmenu $viewhlmenu 1 [list addvhighlight $n] $op $args
4282 proc newviewok {top n {apply 0}} {
4283 global nextviewnum newviewperm newviewname newishighlight
4284 global viewname viewfiles viewperm selectedview curview
4285 global viewargs viewargscmd newviewopts viewhlmenu
4287 if {[catch {
4288 set newargs [encode_view_opts $n]
4289 } err]} {
4290 error_popup "[mc "Error in commit selection arguments:"] $err" $top
4291 return
4293 set files {}
4294 foreach f [split [$top.t get 0.0 end] "\n"] {
4295 set ft [string trim $f]
4296 if {$ft ne {}} {
4297 lappend files $ft
4300 if {![info exists viewfiles($n)]} {
4301 # creating a new view
4302 incr nextviewnum
4303 set viewname($n) $newviewname($n)
4304 set viewperm($n) $newviewopts($n,perm)
4305 set viewfiles($n) $files
4306 set viewargs($n) $newargs
4307 set viewargscmd($n) $newviewopts($n,cmd)
4308 addviewmenu $n
4309 if {!$newishighlight} {
4310 run showview $n
4311 } else {
4312 run addvhighlight $n
4314 } else {
4315 # editing an existing view
4316 set viewperm($n) $newviewopts($n,perm)
4317 if {$newviewname($n) ne $viewname($n)} {
4318 set viewname($n) $newviewname($n)
4319 doviewmenu .bar.view 5 [list showview $n] \
4320 entryconf [list -label $viewname($n)]
4321 # doviewmenu $viewhlmenu 1 [list addvhighlight $n] \
4322 # entryconf [list -label $viewname($n) -value $viewname($n)]
4324 if {$files ne $viewfiles($n) || $newargs ne $viewargs($n) || \
4325 $newviewopts($n,cmd) ne $viewargscmd($n)} {
4326 set viewfiles($n) $files
4327 set viewargs($n) $newargs
4328 set viewargscmd($n) $newviewopts($n,cmd)
4329 if {$curview == $n} {
4330 run reloadcommits
4334 if {$apply} return
4335 catch {destroy $top}
4338 proc delview {} {
4339 global curview viewperm hlview selectedhlview
4341 if {$curview == 0} return
4342 if {[info exists hlview] && $hlview == $curview} {
4343 set selectedhlview [mc "None"]
4344 unset hlview
4346 allviewmenus $curview delete
4347 set viewperm($curview) 0
4348 showview 0
4351 proc addviewmenu {n} {
4352 global viewname viewhlmenu
4354 .bar.view add radiobutton -label $viewname($n) \
4355 -command [list showview $n] -variable selectedview -value $n
4356 #$viewhlmenu add radiobutton -label $viewname($n) \
4357 # -command [list addvhighlight $n] -variable selectedhlview
4360 proc showview {n} {
4361 global curview cached_commitrow ordertok
4362 global displayorder parentlist rowidlist rowisopt rowfinal
4363 global colormap rowtextx nextcolor canvxmax
4364 global numcommits viewcomplete
4365 global selectedline currentid canv canvy0
4366 global treediffs
4367 global pending_select mainheadid
4368 global commitidx
4369 global selectedview
4370 global hlview selectedhlview commitinterest
4372 if {$n == $curview} return
4373 set selid {}
4374 set ymax [lindex [$canv cget -scrollregion] 3]
4375 set span [$canv yview]
4376 set ytop [expr {[lindex $span 0] * $ymax}]
4377 set ybot [expr {[lindex $span 1] * $ymax}]
4378 set yscreen [expr {($ybot - $ytop) / 2}]
4379 if {$selectedline ne {}} {
4380 set selid $currentid
4381 set y [yc $selectedline]
4382 if {$ytop < $y && $y < $ybot} {
4383 set yscreen [expr {$y - $ytop}]
4385 } elseif {[info exists pending_select]} {
4386 set selid $pending_select
4387 unset pending_select
4389 unselectline
4390 normalline
4391 catch {unset treediffs}
4392 clear_display
4393 if {[info exists hlview] && $hlview == $n} {
4394 unset hlview
4395 set selectedhlview [mc "None"]
4397 catch {unset commitinterest}
4398 catch {unset cached_commitrow}
4399 catch {unset ordertok}
4401 set curview $n
4402 set selectedview $n
4403 .bar.view entryconf [mca "Edit view..."] -state [expr {$n == 0? "disabled": "normal"}]
4404 .bar.view entryconf [mca "Delete view"] -state [expr {$n == 0? "disabled": "normal"}]
4406 run refill_reflist
4407 if {![info exists viewcomplete($n)]} {
4408 getcommits $selid
4409 return
4412 set displayorder {}
4413 set parentlist {}
4414 set rowidlist {}
4415 set rowisopt {}
4416 set rowfinal {}
4417 set numcommits $commitidx($n)
4419 catch {unset colormap}
4420 catch {unset rowtextx}
4421 set nextcolor 0
4422 set canvxmax [$canv cget -width]
4423 set curview $n
4424 set row 0
4425 setcanvscroll
4426 set yf 0
4427 set row {}
4428 if {$selid ne {} && [commitinview $selid $n]} {
4429 set row [rowofcommit $selid]
4430 # try to get the selected row in the same position on the screen
4431 set ymax [lindex [$canv cget -scrollregion] 3]
4432 set ytop [expr {[yc $row] - $yscreen}]
4433 if {$ytop < 0} {
4434 set ytop 0
4436 set yf [expr {$ytop * 1.0 / $ymax}]
4438 allcanvs yview moveto $yf
4439 drawvisible
4440 if {$row ne {}} {
4441 selectline $row 0
4442 } elseif {!$viewcomplete($n)} {
4443 reset_pending_select $selid
4444 } else {
4445 reset_pending_select {}
4447 if {[commitinview $pending_select $curview]} {
4448 selectline [rowofcommit $pending_select] 1
4449 } else {
4450 set row [first_real_row]
4451 if {$row < $numcommits} {
4452 selectline $row 0
4456 if {!$viewcomplete($n)} {
4457 if {$numcommits == 0} {
4458 show_status [mc "Reading commits..."]
4460 } elseif {$numcommits == 0} {
4461 show_status [mc "No commits selected"]
4465 # Stuff relating to the highlighting facility
4467 proc ishighlighted {id} {
4468 global vhighlights fhighlights nhighlights rhighlights
4470 if {[info exists nhighlights($id)] && $nhighlights($id) > 0} {
4471 return $nhighlights($id)
4473 if {[info exists vhighlights($id)] && $vhighlights($id) > 0} {
4474 return $vhighlights($id)
4476 if {[info exists fhighlights($id)] && $fhighlights($id) > 0} {
4477 return $fhighlights($id)
4479 if {[info exists rhighlights($id)] && $rhighlights($id) > 0} {
4480 return $rhighlights($id)
4482 return 0
4485 proc bolden {id font} {
4486 global canv linehtag currentid boldids need_redisplay markedid
4488 # need_redisplay = 1 means the display is stale and about to be redrawn
4489 if {$need_redisplay} return
4490 lappend boldids $id
4491 $canv itemconf $linehtag($id) -font $font
4492 if {[info exists currentid] && $id eq $currentid} {
4493 $canv delete secsel
4494 set t [eval $canv create rect [$canv bbox $linehtag($id)] \
4495 -outline {{}} -tags secsel \
4496 -fill [$canv cget -selectbackground]]
4497 $canv lower $t
4499 if {[info exists markedid] && $id eq $markedid} {
4500 make_idmark $id
4504 proc bolden_name {id font} {
4505 global canv2 linentag currentid boldnameids need_redisplay
4507 if {$need_redisplay} return
4508 lappend boldnameids $id
4509 $canv2 itemconf $linentag($id) -font $font
4510 if {[info exists currentid] && $id eq $currentid} {
4511 $canv2 delete secsel
4512 set t [eval $canv2 create rect [$canv2 bbox $linentag($id)] \
4513 -outline {{}} -tags secsel \
4514 -fill [$canv2 cget -selectbackground]]
4515 $canv2 lower $t
4519 proc unbolden {} {
4520 global boldids
4522 set stillbold {}
4523 foreach id $boldids {
4524 if {![ishighlighted $id]} {
4525 bolden $id mainfont
4526 } else {
4527 lappend stillbold $id
4530 set boldids $stillbold
4533 proc addvhighlight {n} {
4534 global hlview viewcomplete curview vhl_done commitidx
4536 if {[info exists hlview]} {
4537 delvhighlight
4539 set hlview $n
4540 if {$n != $curview && ![info exists viewcomplete($n)]} {
4541 start_rev_list $n
4543 set vhl_done $commitidx($hlview)
4544 if {$vhl_done > 0} {
4545 drawvisible
4549 proc delvhighlight {} {
4550 global hlview vhighlights
4552 if {![info exists hlview]} return
4553 unset hlview
4554 catch {unset vhighlights}
4555 unbolden
4558 proc vhighlightmore {} {
4559 global hlview vhl_done commitidx vhighlights curview
4561 set max $commitidx($hlview)
4562 set vr [visiblerows]
4563 set r0 [lindex $vr 0]
4564 set r1 [lindex $vr 1]
4565 for {set i $vhl_done} {$i < $max} {incr i} {
4566 set id [commitonrow $i $hlview]
4567 if {[commitinview $id $curview]} {
4568 set row [rowofcommit $id]
4569 if {$r0 <= $row && $row <= $r1} {
4570 if {![highlighted $row]} {
4571 bolden $id mainfontbold
4573 set vhighlights($id) 1
4577 set vhl_done $max
4578 return 0
4581 proc askvhighlight {row id} {
4582 global hlview vhighlights iddrawn
4584 if {[commitinview $id $hlview]} {
4585 if {[info exists iddrawn($id)] && ![ishighlighted $id]} {
4586 bolden $id mainfontbold
4588 set vhighlights($id) 1
4589 } else {
4590 set vhighlights($id) 0
4594 proc hfiles_change {} {
4595 global highlight_files filehighlight fhighlights fh_serial
4596 global highlight_paths
4598 if {[info exists filehighlight]} {
4599 # delete previous highlights
4600 catch {close $filehighlight}
4601 unset filehighlight
4602 catch {unset fhighlights}
4603 unbolden
4604 unhighlight_filelist
4606 set highlight_paths {}
4607 after cancel do_file_hl $fh_serial
4608 incr fh_serial
4609 if {$highlight_files ne {}} {
4610 after 300 do_file_hl $fh_serial
4614 proc gdttype_change {name ix op} {
4615 global gdttype highlight_files findstring findpattern
4617 stopfinding
4618 if {$findstring ne {}} {
4619 if {$gdttype eq [mc "containing:"]} {
4620 if {$highlight_files ne {}} {
4621 set highlight_files {}
4622 hfiles_change
4624 findcom_change
4625 } else {
4626 if {$findpattern ne {}} {
4627 set findpattern {}
4628 findcom_change
4630 set highlight_files $findstring
4631 hfiles_change
4633 drawvisible
4635 # enable/disable findtype/findloc menus too
4638 proc find_change {name ix op} {
4639 global gdttype findstring highlight_files
4641 stopfinding
4642 if {$gdttype eq [mc "containing:"]} {
4643 findcom_change
4644 } else {
4645 if {$highlight_files ne $findstring} {
4646 set highlight_files $findstring
4647 hfiles_change
4650 drawvisible
4653 proc findcom_change args {
4654 global nhighlights boldnameids
4655 global findpattern findtype findstring gdttype
4657 stopfinding
4658 # delete previous highlights, if any
4659 foreach id $boldnameids {
4660 bolden_name $id mainfont
4662 set boldnameids {}
4663 catch {unset nhighlights}
4664 unbolden
4665 unmarkmatches
4666 if {$gdttype ne [mc "containing:"] || $findstring eq {}} {
4667 set findpattern {}
4668 } elseif {$findtype eq [mc "Regexp"]} {
4669 set findpattern $findstring
4670 } else {
4671 set e [string map {"*" "\\*" "?" "\\?" "\[" "\\\[" "\\" "\\\\"} \
4672 $findstring]
4673 set findpattern "*$e*"
4677 proc makepatterns {l} {
4678 set ret {}
4679 foreach e $l {
4680 set ee [string map {"*" "\\*" "?" "\\?" "\[" "\\\[" "\\" "\\\\"} $e]
4681 if {[string index $ee end] eq "/"} {
4682 lappend ret "$ee*"
4683 } else {
4684 lappend ret $ee
4685 lappend ret "$ee/*"
4688 return $ret
4691 proc do_file_hl {serial} {
4692 global highlight_files filehighlight highlight_paths gdttype fhl_list
4693 global cdup findtype
4695 if {$gdttype eq [mc "touching paths:"]} {
4696 # If "exact" match then convert backslashes to forward slashes.
4697 # Most useful to support Windows-flavoured file paths.
4698 if {$findtype eq [mc "Exact"]} {
4699 set highlight_files [string map {"\\" "/"} $highlight_files]
4701 if {[catch {set paths [shellsplit $highlight_files]}]} return
4702 set highlight_paths [makepatterns $paths]
4703 highlight_filelist
4704 set relative_paths {}
4705 foreach path $paths {
4706 lappend relative_paths [file join $cdup $path]
4708 set gdtargs [concat -- $relative_paths]
4709 } elseif {$gdttype eq [mc "adding/removing string:"]} {
4710 set gdtargs [list "-S$highlight_files"]
4711 } elseif {$gdttype eq [mc "changing lines matching:"]} {
4712 set gdtargs [list "-G$highlight_files"]
4713 } else {
4714 # must be "containing:", i.e. we're searching commit info
4715 return
4717 set cmd [concat | git diff-tree -r -s --stdin $gdtargs]
4718 set filehighlight [open $cmd r+]
4719 fconfigure $filehighlight -blocking 0
4720 filerun $filehighlight readfhighlight
4721 set fhl_list {}
4722 drawvisible
4723 flushhighlights
4726 proc flushhighlights {} {
4727 global filehighlight fhl_list
4729 if {[info exists filehighlight]} {
4730 lappend fhl_list {}
4731 puts $filehighlight ""
4732 flush $filehighlight
4736 proc askfilehighlight {row id} {
4737 global filehighlight fhighlights fhl_list
4739 lappend fhl_list $id
4740 set fhighlights($id) -1
4741 puts $filehighlight $id
4744 proc readfhighlight {} {
4745 global filehighlight fhighlights curview iddrawn
4746 global fhl_list find_dirn
4748 if {![info exists filehighlight]} {
4749 return 0
4751 set nr 0
4752 while {[incr nr] <= 100 && [gets $filehighlight line] >= 0} {
4753 set line [string trim $line]
4754 set i [lsearch -exact $fhl_list $line]
4755 if {$i < 0} continue
4756 for {set j 0} {$j < $i} {incr j} {
4757 set id [lindex $fhl_list $j]
4758 set fhighlights($id) 0
4760 set fhl_list [lrange $fhl_list [expr {$i+1}] end]
4761 if {$line eq {}} continue
4762 if {![commitinview $line $curview]} continue
4763 if {[info exists iddrawn($line)] && ![ishighlighted $line]} {
4764 bolden $line mainfontbold
4766 set fhighlights($line) 1
4768 if {[eof $filehighlight]} {
4769 # strange...
4770 puts "oops, git diff-tree died"
4771 catch {close $filehighlight}
4772 unset filehighlight
4773 return 0
4775 if {[info exists find_dirn]} {
4776 run findmore
4778 return 1
4781 proc doesmatch {f} {
4782 global findtype findpattern
4784 if {$findtype eq [mc "Regexp"]} {
4785 return [regexp $findpattern $f]
4786 } elseif {$findtype eq [mc "IgnCase"]} {
4787 return [string match -nocase $findpattern $f]
4788 } else {
4789 return [string match $findpattern $f]
4793 proc askfindhighlight {row id} {
4794 global nhighlights commitinfo iddrawn
4795 global findloc
4796 global markingmatches
4798 if {![info exists commitinfo($id)]} {
4799 getcommit $id
4801 set info $commitinfo($id)
4802 set isbold 0
4803 set fldtypes [list [mc Headline] [mc Author] "" [mc Committer] "" [mc Comments]]
4804 foreach f $info ty $fldtypes {
4805 if {$ty eq ""} continue
4806 if {($findloc eq [mc "All fields"] || $findloc eq $ty) &&
4807 [doesmatch $f]} {
4808 if {$ty eq [mc "Author"]} {
4809 set isbold 2
4810 break
4812 set isbold 1
4815 if {$isbold && [info exists iddrawn($id)]} {
4816 if {![ishighlighted $id]} {
4817 bolden $id mainfontbold
4818 if {$isbold > 1} {
4819 bolden_name $id mainfontbold
4822 if {$markingmatches} {
4823 markrowmatches $row $id
4826 set nhighlights($id) $isbold
4829 proc markrowmatches {row id} {
4830 global canv canv2 linehtag linentag commitinfo findloc
4832 set headline [lindex $commitinfo($id) 0]
4833 set author [lindex $commitinfo($id) 1]
4834 $canv delete match$row
4835 $canv2 delete match$row
4836 if {$findloc eq [mc "All fields"] || $findloc eq [mc "Headline"]} {
4837 set m [findmatches $headline]
4838 if {$m ne {}} {
4839 markmatches $canv $row $headline $linehtag($id) $m \
4840 [$canv itemcget $linehtag($id) -font] $row
4843 if {$findloc eq [mc "All fields"] || $findloc eq [mc "Author"]} {
4844 set m [findmatches $author]
4845 if {$m ne {}} {
4846 markmatches $canv2 $row $author $linentag($id) $m \
4847 [$canv2 itemcget $linentag($id) -font] $row
4852 proc vrel_change {name ix op} {
4853 global highlight_related
4855 rhighlight_none
4856 if {$highlight_related ne [mc "None"]} {
4857 run drawvisible
4861 # prepare for testing whether commits are descendents or ancestors of a
4862 proc rhighlight_sel {a} {
4863 global descendent desc_todo ancestor anc_todo
4864 global highlight_related
4866 catch {unset descendent}
4867 set desc_todo [list $a]
4868 catch {unset ancestor}
4869 set anc_todo [list $a]
4870 if {$highlight_related ne [mc "None"]} {
4871 rhighlight_none
4872 run drawvisible
4876 proc rhighlight_none {} {
4877 global rhighlights
4879 catch {unset rhighlights}
4880 unbolden
4883 proc is_descendent {a} {
4884 global curview children descendent desc_todo
4886 set v $curview
4887 set la [rowofcommit $a]
4888 set todo $desc_todo
4889 set leftover {}
4890 set done 0
4891 for {set i 0} {$i < [llength $todo]} {incr i} {
4892 set do [lindex $todo $i]
4893 if {[rowofcommit $do] < $la} {
4894 lappend leftover $do
4895 continue
4897 foreach nk $children($v,$do) {
4898 if {![info exists descendent($nk)]} {
4899 set descendent($nk) 1
4900 lappend todo $nk
4901 if {$nk eq $a} {
4902 set done 1
4906 if {$done} {
4907 set desc_todo [concat $leftover [lrange $todo [expr {$i+1}] end]]
4908 return
4911 set descendent($a) 0
4912 set desc_todo $leftover
4915 proc is_ancestor {a} {
4916 global curview parents ancestor anc_todo
4918 set v $curview
4919 set la [rowofcommit $a]
4920 set todo $anc_todo
4921 set leftover {}
4922 set done 0
4923 for {set i 0} {$i < [llength $todo]} {incr i} {
4924 set do [lindex $todo $i]
4925 if {![commitinview $do $v] || [rowofcommit $do] > $la} {
4926 lappend leftover $do
4927 continue
4929 foreach np $parents($v,$do) {
4930 if {![info exists ancestor($np)]} {
4931 set ancestor($np) 1
4932 lappend todo $np
4933 if {$np eq $a} {
4934 set done 1
4938 if {$done} {
4939 set anc_todo [concat $leftover [lrange $todo [expr {$i+1}] end]]
4940 return
4943 set ancestor($a) 0
4944 set anc_todo $leftover
4947 proc askrelhighlight {row id} {
4948 global descendent highlight_related iddrawn rhighlights
4949 global selectedline ancestor
4951 if {$selectedline eq {}} return
4952 set isbold 0
4953 if {$highlight_related eq [mc "Descendant"] ||
4954 $highlight_related eq [mc "Not descendant"]} {
4955 if {![info exists descendent($id)]} {
4956 is_descendent $id
4958 if {$descendent($id) == ($highlight_related eq [mc "Descendant"])} {
4959 set isbold 1
4961 } elseif {$highlight_related eq [mc "Ancestor"] ||
4962 $highlight_related eq [mc "Not ancestor"]} {
4963 if {![info exists ancestor($id)]} {
4964 is_ancestor $id
4966 if {$ancestor($id) == ($highlight_related eq [mc "Ancestor"])} {
4967 set isbold 1
4970 if {[info exists iddrawn($id)]} {
4971 if {$isbold && ![ishighlighted $id]} {
4972 bolden $id mainfontbold
4975 set rhighlights($id) $isbold
4978 # Graph layout functions
4980 proc shortids {ids} {
4981 set res {}
4982 foreach id $ids {
4983 if {[llength $id] > 1} {
4984 lappend res [shortids $id]
4985 } elseif {[regexp {^[0-9a-f]{40}$} $id]} {
4986 lappend res [string range $id 0 7]
4987 } else {
4988 lappend res $id
4991 return $res
4994 proc ntimes {n o} {
4995 set ret {}
4996 set o [list $o]
4997 for {set mask 1} {$mask <= $n} {incr mask $mask} {
4998 if {($n & $mask) != 0} {
4999 set ret [concat $ret $o]
5001 set o [concat $o $o]
5003 return $ret
5006 proc ordertoken {id} {
5007 global ordertok curview varcid varcstart varctok curview parents children
5008 global nullid nullid2
5010 if {[info exists ordertok($id)]} {
5011 return $ordertok($id)
5013 set origid $id
5014 set todo {}
5015 while {1} {
5016 if {[info exists varcid($curview,$id)]} {
5017 set a $varcid($curview,$id)
5018 set p [lindex $varcstart($curview) $a]
5019 } else {
5020 set p [lindex $children($curview,$id) 0]
5022 if {[info exists ordertok($p)]} {
5023 set tok $ordertok($p)
5024 break
5026 set id [first_real_child $curview,$p]
5027 if {$id eq {}} {
5028 # it's a root
5029 set tok [lindex $varctok($curview) $varcid($curview,$p)]
5030 break
5032 if {[llength $parents($curview,$id)] == 1} {
5033 lappend todo [list $p {}]
5034 } else {
5035 set j [lsearch -exact $parents($curview,$id) $p]
5036 if {$j < 0} {
5037 puts "oops didn't find [shortids $p] in parents of [shortids $id]"
5039 lappend todo [list $p [strrep $j]]
5042 for {set i [llength $todo]} {[incr i -1] >= 0} {} {
5043 set p [lindex $todo $i 0]
5044 append tok [lindex $todo $i 1]
5045 set ordertok($p) $tok
5047 set ordertok($origid) $tok
5048 return $tok
5051 # Work out where id should go in idlist so that order-token
5052 # values increase from left to right
5053 proc idcol {idlist id {i 0}} {
5054 set t [ordertoken $id]
5055 if {$i < 0} {
5056 set i 0
5058 if {$i >= [llength $idlist] || $t < [ordertoken [lindex $idlist $i]]} {
5059 if {$i > [llength $idlist]} {
5060 set i [llength $idlist]
5062 while {[incr i -1] >= 0 && $t < [ordertoken [lindex $idlist $i]]} {}
5063 incr i
5064 } else {
5065 if {$t > [ordertoken [lindex $idlist $i]]} {
5066 while {[incr i] < [llength $idlist] &&
5067 $t >= [ordertoken [lindex $idlist $i]]} {}
5070 return $i
5073 proc initlayout {} {
5074 global rowidlist rowisopt rowfinal displayorder parentlist
5075 global numcommits canvxmax canv
5076 global nextcolor
5077 global colormap rowtextx
5079 set numcommits 0
5080 set displayorder {}
5081 set parentlist {}
5082 set nextcolor 0
5083 set rowidlist {}
5084 set rowisopt {}
5085 set rowfinal {}
5086 set canvxmax [$canv cget -width]
5087 catch {unset colormap}
5088 catch {unset rowtextx}
5089 setcanvscroll
5092 proc setcanvscroll {} {
5093 global canv canv2 canv3 numcommits linespc canvxmax canvy0
5094 global lastscrollset lastscrollrows
5096 set ymax [expr {$canvy0 + ($numcommits - 0.5) * $linespc + 2}]
5097 $canv conf -scrollregion [list 0 0 $canvxmax $ymax]
5098 $canv2 conf -scrollregion [list 0 0 0 $ymax]
5099 $canv3 conf -scrollregion [list 0 0 0 $ymax]
5100 set lastscrollset [clock clicks -milliseconds]
5101 set lastscrollrows $numcommits
5104 proc visiblerows {} {
5105 global canv numcommits linespc
5107 set ymax [lindex [$canv cget -scrollregion] 3]
5108 if {$ymax eq {} || $ymax == 0} return
5109 set f [$canv yview]
5110 set y0 [expr {int([lindex $f 0] * $ymax)}]
5111 set r0 [expr {int(($y0 - 3) / $linespc) - 1}]
5112 if {$r0 < 0} {
5113 set r0 0
5115 set y1 [expr {int([lindex $f 1] * $ymax)}]
5116 set r1 [expr {int(($y1 - 3) / $linespc) + 1}]
5117 if {$r1 >= $numcommits} {
5118 set r1 [expr {$numcommits - 1}]
5120 return [list $r0 $r1]
5123 proc layoutmore {} {
5124 global commitidx viewcomplete curview
5125 global numcommits pending_select curview
5126 global lastscrollset lastscrollrows
5128 if {$lastscrollrows < 100 || $viewcomplete($curview) ||
5129 [clock clicks -milliseconds] - $lastscrollset > 500} {
5130 setcanvscroll
5132 if {[info exists pending_select] &&
5133 [commitinview $pending_select $curview]} {
5134 update
5135 selectline [rowofcommit $pending_select] 1
5137 drawvisible
5140 # With path limiting, we mightn't get the actual HEAD commit,
5141 # so ask git rev-list what is the first ancestor of HEAD that
5142 # touches a file in the path limit.
5143 proc get_viewmainhead {view} {
5144 global viewmainheadid vfilelimit viewinstances mainheadid
5146 catch {
5147 set rfd [open [concat | git rev-list -1 $mainheadid \
5148 -- $vfilelimit($view)] r]
5149 set j [reg_instance $rfd]
5150 lappend viewinstances($view) $j
5151 fconfigure $rfd -blocking 0
5152 filerun $rfd [list getviewhead $rfd $j $view]
5153 set viewmainheadid($curview) {}
5157 # git rev-list should give us just 1 line to use as viewmainheadid($view)
5158 proc getviewhead {fd inst view} {
5159 global viewmainheadid commfd curview viewinstances showlocalchanges
5161 set id {}
5162 if {[gets $fd line] < 0} {
5163 if {![eof $fd]} {
5164 return 1
5166 } elseif {[string length $line] == 40 && [string is xdigit $line]} {
5167 set id $line
5169 set viewmainheadid($view) $id
5170 close $fd
5171 unset commfd($inst)
5172 set i [lsearch -exact $viewinstances($view) $inst]
5173 if {$i >= 0} {
5174 set viewinstances($view) [lreplace $viewinstances($view) $i $i]
5176 if {$showlocalchanges && $id ne {} && $view == $curview} {
5177 doshowlocalchanges
5179 return 0
5182 proc doshowlocalchanges {} {
5183 global curview viewmainheadid
5185 if {$viewmainheadid($curview) eq {}} return
5186 if {[commitinview $viewmainheadid($curview) $curview]} {
5187 dodiffindex
5188 } else {
5189 interestedin $viewmainheadid($curview) dodiffindex
5193 proc dohidelocalchanges {} {
5194 global nullid nullid2 lserial curview
5196 if {[commitinview $nullid $curview]} {
5197 removefakerow $nullid
5199 if {[commitinview $nullid2 $curview]} {
5200 removefakerow $nullid2
5202 incr lserial
5205 # spawn off a process to do git diff-index --cached HEAD
5206 proc dodiffindex {} {
5207 global lserial showlocalchanges vfilelimit curview
5208 global hasworktree
5210 if {!$showlocalchanges || !$hasworktree} return
5211 incr lserial
5212 set cmd "|git diff-index --cached HEAD"
5213 if {$vfilelimit($curview) ne {}} {
5214 set cmd [concat $cmd -- $vfilelimit($curview)]
5216 set fd [open $cmd r]
5217 fconfigure $fd -blocking 0
5218 set i [reg_instance $fd]
5219 filerun $fd [list readdiffindex $fd $lserial $i]
5222 proc readdiffindex {fd serial inst} {
5223 global viewmainheadid nullid nullid2 curview commitinfo commitdata lserial
5224 global vfilelimit
5226 set isdiff 1
5227 if {[gets $fd line] < 0} {
5228 if {![eof $fd]} {
5229 return 1
5231 set isdiff 0
5233 # we only need to see one line and we don't really care what it says...
5234 stop_instance $inst
5236 if {$serial != $lserial} {
5237 return 0
5240 # now see if there are any local changes not checked in to the index
5241 set cmd "|git diff-files"
5242 if {$vfilelimit($curview) ne {}} {
5243 set cmd [concat $cmd -- $vfilelimit($curview)]
5245 set fd [open $cmd r]
5246 fconfigure $fd -blocking 0
5247 set i [reg_instance $fd]
5248 filerun $fd [list readdifffiles $fd $serial $i]
5250 if {$isdiff && ![commitinview $nullid2 $curview]} {
5251 # add the line for the changes in the index to the graph
5252 set hl [mc "Local changes checked in to index but not committed"]
5253 set commitinfo($nullid2) [list $hl {} {} {} {} " $hl\n"]
5254 set commitdata($nullid2) "\n $hl\n"
5255 if {[commitinview $nullid $curview]} {
5256 removefakerow $nullid
5258 insertfakerow $nullid2 $viewmainheadid($curview)
5259 } elseif {!$isdiff && [commitinview $nullid2 $curview]} {
5260 if {[commitinview $nullid $curview]} {
5261 removefakerow $nullid
5263 removefakerow $nullid2
5265 return 0
5268 proc readdifffiles {fd serial inst} {
5269 global viewmainheadid nullid nullid2 curview
5270 global commitinfo commitdata lserial
5272 set isdiff 1
5273 if {[gets $fd line] < 0} {
5274 if {![eof $fd]} {
5275 return 1
5277 set isdiff 0
5279 # we only need to see one line and we don't really care what it says...
5280 stop_instance $inst
5282 if {$serial != $lserial} {
5283 return 0
5286 if {$isdiff && ![commitinview $nullid $curview]} {
5287 # add the line for the local diff to the graph
5288 set hl [mc "Local uncommitted changes, not checked in to index"]
5289 set commitinfo($nullid) [list $hl {} {} {} {} " $hl\n"]
5290 set commitdata($nullid) "\n $hl\n"
5291 if {[commitinview $nullid2 $curview]} {
5292 set p $nullid2
5293 } else {
5294 set p $viewmainheadid($curview)
5296 insertfakerow $nullid $p
5297 } elseif {!$isdiff && [commitinview $nullid $curview]} {
5298 removefakerow $nullid
5300 return 0
5303 proc nextuse {id row} {
5304 global curview children
5306 if {[info exists children($curview,$id)]} {
5307 foreach kid $children($curview,$id) {
5308 if {![commitinview $kid $curview]} {
5309 return -1
5311 if {[rowofcommit $kid] > $row} {
5312 return [rowofcommit $kid]
5316 if {[commitinview $id $curview]} {
5317 return [rowofcommit $id]
5319 return -1
5322 proc prevuse {id row} {
5323 global curview children
5325 set ret -1
5326 if {[info exists children($curview,$id)]} {
5327 foreach kid $children($curview,$id) {
5328 if {![commitinview $kid $curview]} break
5329 if {[rowofcommit $kid] < $row} {
5330 set ret [rowofcommit $kid]
5334 return $ret
5337 proc make_idlist {row} {
5338 global displayorder parentlist uparrowlen downarrowlen mingaplen
5339 global commitidx curview children
5341 set r [expr {$row - $mingaplen - $downarrowlen - 1}]
5342 if {$r < 0} {
5343 set r 0
5345 set ra [expr {$row - $downarrowlen}]
5346 if {$ra < 0} {
5347 set ra 0
5349 set rb [expr {$row + $uparrowlen}]
5350 if {$rb > $commitidx($curview)} {
5351 set rb $commitidx($curview)
5353 make_disporder $r [expr {$rb + 1}]
5354 set ids {}
5355 for {} {$r < $ra} {incr r} {
5356 set nextid [lindex $displayorder [expr {$r + 1}]]
5357 foreach p [lindex $parentlist $r] {
5358 if {$p eq $nextid} continue
5359 set rn [nextuse $p $r]
5360 if {$rn >= $row &&
5361 $rn <= $r + $downarrowlen + $mingaplen + $uparrowlen} {
5362 lappend ids [list [ordertoken $p] $p]
5366 for {} {$r < $row} {incr r} {
5367 set nextid [lindex $displayorder [expr {$r + 1}]]
5368 foreach p [lindex $parentlist $r] {
5369 if {$p eq $nextid} continue
5370 set rn [nextuse $p $r]
5371 if {$rn < 0 || $rn >= $row} {
5372 lappend ids [list [ordertoken $p] $p]
5376 set id [lindex $displayorder $row]
5377 lappend ids [list [ordertoken $id] $id]
5378 while {$r < $rb} {
5379 foreach p [lindex $parentlist $r] {
5380 set firstkid [lindex $children($curview,$p) 0]
5381 if {[rowofcommit $firstkid] < $row} {
5382 lappend ids [list [ordertoken $p] $p]
5385 incr r
5386 set id [lindex $displayorder $r]
5387 if {$id ne {}} {
5388 set firstkid [lindex $children($curview,$id) 0]
5389 if {$firstkid ne {} && [rowofcommit $firstkid] < $row} {
5390 lappend ids [list [ordertoken $id] $id]
5394 set idlist {}
5395 foreach idx [lsort -unique $ids] {
5396 lappend idlist [lindex $idx 1]
5398 return $idlist
5401 proc rowsequal {a b} {
5402 while {[set i [lsearch -exact $a {}]] >= 0} {
5403 set a [lreplace $a $i $i]
5405 while {[set i [lsearch -exact $b {}]] >= 0} {
5406 set b [lreplace $b $i $i]
5408 return [expr {$a eq $b}]
5411 proc makeupline {id row rend col} {
5412 global rowidlist uparrowlen downarrowlen mingaplen
5414 for {set r $rend} {1} {set r $rstart} {
5415 set rstart [prevuse $id $r]
5416 if {$rstart < 0} return
5417 if {$rstart < $row} break
5419 if {$rstart + $uparrowlen + $mingaplen + $downarrowlen < $rend} {
5420 set rstart [expr {$rend - $uparrowlen - 1}]
5422 for {set r $rstart} {[incr r] <= $row} {} {
5423 set idlist [lindex $rowidlist $r]
5424 if {$idlist ne {} && [lsearch -exact $idlist $id] < 0} {
5425 set col [idcol $idlist $id $col]
5426 lset rowidlist $r [linsert $idlist $col $id]
5427 changedrow $r
5432 proc layoutrows {row endrow} {
5433 global rowidlist rowisopt rowfinal displayorder
5434 global uparrowlen downarrowlen maxwidth mingaplen
5435 global children parentlist
5436 global commitidx viewcomplete curview
5438 make_disporder [expr {$row - 1}] [expr {$endrow + $uparrowlen}]
5439 set idlist {}
5440 if {$row > 0} {
5441 set rm1 [expr {$row - 1}]
5442 foreach id [lindex $rowidlist $rm1] {
5443 if {$id ne {}} {
5444 lappend idlist $id
5447 set final [lindex $rowfinal $rm1]
5449 for {} {$row < $endrow} {incr row} {
5450 set rm1 [expr {$row - 1}]
5451 if {$rm1 < 0 || $idlist eq {}} {
5452 set idlist [make_idlist $row]
5453 set final 1
5454 } else {
5455 set id [lindex $displayorder $rm1]
5456 set col [lsearch -exact $idlist $id]
5457 set idlist [lreplace $idlist $col $col]
5458 foreach p [lindex $parentlist $rm1] {
5459 if {[lsearch -exact $idlist $p] < 0} {
5460 set col [idcol $idlist $p $col]
5461 set idlist [linsert $idlist $col $p]
5462 # if not the first child, we have to insert a line going up
5463 if {$id ne [lindex $children($curview,$p) 0]} {
5464 makeupline $p $rm1 $row $col
5468 set id [lindex $displayorder $row]
5469 if {$row > $downarrowlen} {
5470 set termrow [expr {$row - $downarrowlen - 1}]
5471 foreach p [lindex $parentlist $termrow] {
5472 set i [lsearch -exact $idlist $p]
5473 if {$i < 0} continue
5474 set nr [nextuse $p $termrow]
5475 if {$nr < 0 || $nr >= $row + $mingaplen + $uparrowlen} {
5476 set idlist [lreplace $idlist $i $i]
5480 set col [lsearch -exact $idlist $id]
5481 if {$col < 0} {
5482 set col [idcol $idlist $id]
5483 set idlist [linsert $idlist $col $id]
5484 if {$children($curview,$id) ne {}} {
5485 makeupline $id $rm1 $row $col
5488 set r [expr {$row + $uparrowlen - 1}]
5489 if {$r < $commitidx($curview)} {
5490 set x $col
5491 foreach p [lindex $parentlist $r] {
5492 if {[lsearch -exact $idlist $p] >= 0} continue
5493 set fk [lindex $children($curview,$p) 0]
5494 if {[rowofcommit $fk] < $row} {
5495 set x [idcol $idlist $p $x]
5496 set idlist [linsert $idlist $x $p]
5499 if {[incr r] < $commitidx($curview)} {
5500 set p [lindex $displayorder $r]
5501 if {[lsearch -exact $idlist $p] < 0} {
5502 set fk [lindex $children($curview,$p) 0]
5503 if {$fk ne {} && [rowofcommit $fk] < $row} {
5504 set x [idcol $idlist $p $x]
5505 set idlist [linsert $idlist $x $p]
5511 if {$final && !$viewcomplete($curview) &&
5512 $row + $uparrowlen + $mingaplen + $downarrowlen
5513 >= $commitidx($curview)} {
5514 set final 0
5516 set l [llength $rowidlist]
5517 if {$row == $l} {
5518 lappend rowidlist $idlist
5519 lappend rowisopt 0
5520 lappend rowfinal $final
5521 } elseif {$row < $l} {
5522 if {![rowsequal $idlist [lindex $rowidlist $row]]} {
5523 lset rowidlist $row $idlist
5524 changedrow $row
5526 lset rowfinal $row $final
5527 } else {
5528 set pad [ntimes [expr {$row - $l}] {}]
5529 set rowidlist [concat $rowidlist $pad]
5530 lappend rowidlist $idlist
5531 set rowfinal [concat $rowfinal $pad]
5532 lappend rowfinal $final
5533 set rowisopt [concat $rowisopt [ntimes [expr {$row - $l + 1}] 0]]
5536 return $row
5539 proc changedrow {row} {
5540 global displayorder iddrawn rowisopt need_redisplay
5542 set l [llength $rowisopt]
5543 if {$row < $l} {
5544 lset rowisopt $row 0
5545 if {$row + 1 < $l} {
5546 lset rowisopt [expr {$row + 1}] 0
5547 if {$row + 2 < $l} {
5548 lset rowisopt [expr {$row + 2}] 0
5552 set id [lindex $displayorder $row]
5553 if {[info exists iddrawn($id)]} {
5554 set need_redisplay 1
5558 proc insert_pad {row col npad} {
5559 global rowidlist
5561 set pad [ntimes $npad {}]
5562 set idlist [lindex $rowidlist $row]
5563 set bef [lrange $idlist 0 [expr {$col - 1}]]
5564 set aft [lrange $idlist $col end]
5565 set i [lsearch -exact $aft {}]
5566 if {$i > 0} {
5567 set aft [lreplace $aft $i $i]
5569 lset rowidlist $row [concat $bef $pad $aft]
5570 changedrow $row
5573 proc optimize_rows {row col endrow} {
5574 global rowidlist rowisopt displayorder curview children
5576 if {$row < 1} {
5577 set row 1
5579 for {} {$row < $endrow} {incr row; set col 0} {
5580 if {[lindex $rowisopt $row]} continue
5581 set haspad 0
5582 set y0 [expr {$row - 1}]
5583 set ym [expr {$row - 2}]
5584 set idlist [lindex $rowidlist $row]
5585 set previdlist [lindex $rowidlist $y0]
5586 if {$idlist eq {} || $previdlist eq {}} continue
5587 if {$ym >= 0} {
5588 set pprevidlist [lindex $rowidlist $ym]
5589 if {$pprevidlist eq {}} continue
5590 } else {
5591 set pprevidlist {}
5593 set x0 -1
5594 set xm -1
5595 for {} {$col < [llength $idlist]} {incr col} {
5596 set id [lindex $idlist $col]
5597 if {[lindex $previdlist $col] eq $id} continue
5598 if {$id eq {}} {
5599 set haspad 1
5600 continue
5602 set x0 [lsearch -exact $previdlist $id]
5603 if {$x0 < 0} continue
5604 set z [expr {$x0 - $col}]
5605 set isarrow 0
5606 set z0 {}
5607 if {$ym >= 0} {
5608 set xm [lsearch -exact $pprevidlist $id]
5609 if {$xm >= 0} {
5610 set z0 [expr {$xm - $x0}]
5613 if {$z0 eq {}} {
5614 # if row y0 is the first child of $id then it's not an arrow
5615 if {[lindex $children($curview,$id) 0] ne
5616 [lindex $displayorder $y0]} {
5617 set isarrow 1
5620 if {!$isarrow && $id ne [lindex $displayorder $row] &&
5621 [lsearch -exact [lindex $rowidlist [expr {$row+1}]] $id] < 0} {
5622 set isarrow 1
5624 # Looking at lines from this row to the previous row,
5625 # make them go straight up if they end in an arrow on
5626 # the previous row; otherwise make them go straight up
5627 # or at 45 degrees.
5628 if {$z < -1 || ($z < 0 && $isarrow)} {
5629 # Line currently goes left too much;
5630 # insert pads in the previous row, then optimize it
5631 set npad [expr {-1 - $z + $isarrow}]
5632 insert_pad $y0 $x0 $npad
5633 if {$y0 > 0} {
5634 optimize_rows $y0 $x0 $row
5636 set previdlist [lindex $rowidlist $y0]
5637 set x0 [lsearch -exact $previdlist $id]
5638 set z [expr {$x0 - $col}]
5639 if {$z0 ne {}} {
5640 set pprevidlist [lindex $rowidlist $ym]
5641 set xm [lsearch -exact $pprevidlist $id]
5642 set z0 [expr {$xm - $x0}]
5644 } elseif {$z > 1 || ($z > 0 && $isarrow)} {
5645 # Line currently goes right too much;
5646 # insert pads in this line
5647 set npad [expr {$z - 1 + $isarrow}]
5648 insert_pad $row $col $npad
5649 set idlist [lindex $rowidlist $row]
5650 incr col $npad
5651 set z [expr {$x0 - $col}]
5652 set haspad 1
5654 if {$z0 eq {} && !$isarrow && $ym >= 0} {
5655 # this line links to its first child on row $row-2
5656 set id [lindex $displayorder $ym]
5657 set xc [lsearch -exact $pprevidlist $id]
5658 if {$xc >= 0} {
5659 set z0 [expr {$xc - $x0}]
5662 # avoid lines jigging left then immediately right
5663 if {$z0 ne {} && $z < 0 && $z0 > 0} {
5664 insert_pad $y0 $x0 1
5665 incr x0
5666 optimize_rows $y0 $x0 $row
5667 set previdlist [lindex $rowidlist $y0]
5670 if {!$haspad} {
5671 # Find the first column that doesn't have a line going right
5672 for {set col [llength $idlist]} {[incr col -1] >= 0} {} {
5673 set id [lindex $idlist $col]
5674 if {$id eq {}} break
5675 set x0 [lsearch -exact $previdlist $id]
5676 if {$x0 < 0} {
5677 # check if this is the link to the first child
5678 set kid [lindex $displayorder $y0]
5679 if {[lindex $children($curview,$id) 0] eq $kid} {
5680 # it is, work out offset to child
5681 set x0 [lsearch -exact $previdlist $kid]
5684 if {$x0 <= $col} break
5686 # Insert a pad at that column as long as it has a line and
5687 # isn't the last column
5688 if {$x0 >= 0 && [incr col] < [llength $idlist]} {
5689 set idlist [linsert $idlist $col {}]
5690 lset rowidlist $row $idlist
5691 changedrow $row
5697 proc xc {row col} {
5698 global canvx0 linespc
5699 return [expr {$canvx0 + $col * $linespc}]
5702 proc yc {row} {
5703 global canvy0 linespc
5704 return [expr {$canvy0 + $row * $linespc}]
5707 proc linewidth {id} {
5708 global thickerline lthickness
5710 set wid $lthickness
5711 if {[info exists thickerline] && $id eq $thickerline} {
5712 set wid [expr {2 * $lthickness}]
5714 return $wid
5717 proc rowranges {id} {
5718 global curview children uparrowlen downarrowlen
5719 global rowidlist
5721 set kids $children($curview,$id)
5722 if {$kids eq {}} {
5723 return {}
5725 set ret {}
5726 lappend kids $id
5727 foreach child $kids {
5728 if {![commitinview $child $curview]} break
5729 set row [rowofcommit $child]
5730 if {![info exists prev]} {
5731 lappend ret [expr {$row + 1}]
5732 } else {
5733 if {$row <= $prevrow} {
5734 puts "oops children of [shortids $id] out of order [shortids $child] $row <= [shortids $prev] $prevrow"
5736 # see if the line extends the whole way from prevrow to row
5737 if {$row > $prevrow + $uparrowlen + $downarrowlen &&
5738 [lsearch -exact [lindex $rowidlist \
5739 [expr {int(($row + $prevrow) / 2)}]] $id] < 0} {
5740 # it doesn't, see where it ends
5741 set r [expr {$prevrow + $downarrowlen}]
5742 if {[lsearch -exact [lindex $rowidlist $r] $id] < 0} {
5743 while {[incr r -1] > $prevrow &&
5744 [lsearch -exact [lindex $rowidlist $r] $id] < 0} {}
5745 } else {
5746 while {[incr r] <= $row &&
5747 [lsearch -exact [lindex $rowidlist $r] $id] >= 0} {}
5748 incr r -1
5750 lappend ret $r
5751 # see where it starts up again
5752 set r [expr {$row - $uparrowlen}]
5753 if {[lsearch -exact [lindex $rowidlist $r] $id] < 0} {
5754 while {[incr r] < $row &&
5755 [lsearch -exact [lindex $rowidlist $r] $id] < 0} {}
5756 } else {
5757 while {[incr r -1] >= $prevrow &&
5758 [lsearch -exact [lindex $rowidlist $r] $id] >= 0} {}
5759 incr r
5761 lappend ret $r
5764 if {$child eq $id} {
5765 lappend ret $row
5767 set prev $child
5768 set prevrow $row
5770 return $ret
5773 proc drawlineseg {id row endrow arrowlow} {
5774 global rowidlist displayorder iddrawn linesegs
5775 global canv colormap linespc curview maxlinelen parentlist
5777 set cols [list [lsearch -exact [lindex $rowidlist $row] $id]]
5778 set le [expr {$row + 1}]
5779 set arrowhigh 1
5780 while {1} {
5781 set c [lsearch -exact [lindex $rowidlist $le] $id]
5782 if {$c < 0} {
5783 incr le -1
5784 break
5786 lappend cols $c
5787 set x [lindex $displayorder $le]
5788 if {$x eq $id} {
5789 set arrowhigh 0
5790 break
5792 if {[info exists iddrawn($x)] || $le == $endrow} {
5793 set c [lsearch -exact [lindex $rowidlist [expr {$le+1}]] $id]
5794 if {$c >= 0} {
5795 lappend cols $c
5796 set arrowhigh 0
5798 break
5800 incr le
5802 if {$le <= $row} {
5803 return $row
5806 set lines {}
5807 set i 0
5808 set joinhigh 0
5809 if {[info exists linesegs($id)]} {
5810 set lines $linesegs($id)
5811 foreach li $lines {
5812 set r0 [lindex $li 0]
5813 if {$r0 > $row} {
5814 if {$r0 == $le && [lindex $li 1] - $row <= $maxlinelen} {
5815 set joinhigh 1
5817 break
5819 incr i
5822 set joinlow 0
5823 if {$i > 0} {
5824 set li [lindex $lines [expr {$i-1}]]
5825 set r1 [lindex $li 1]
5826 if {$r1 == $row && $le - [lindex $li 0] <= $maxlinelen} {
5827 set joinlow 1
5831 set x [lindex $cols [expr {$le - $row}]]
5832 set xp [lindex $cols [expr {$le - 1 - $row}]]
5833 set dir [expr {$xp - $x}]
5834 if {$joinhigh} {
5835 set ith [lindex $lines $i 2]
5836 set coords [$canv coords $ith]
5837 set ah [$canv itemcget $ith -arrow]
5838 set arrowhigh [expr {$ah eq "first" || $ah eq "both"}]
5839 set x2 [lindex $cols [expr {$le + 1 - $row}]]
5840 if {$x2 ne {} && $x - $x2 == $dir} {
5841 set coords [lrange $coords 0 end-2]
5843 } else {
5844 set coords [list [xc $le $x] [yc $le]]
5846 if {$joinlow} {
5847 set itl [lindex $lines [expr {$i-1}] 2]
5848 set al [$canv itemcget $itl -arrow]
5849 set arrowlow [expr {$al eq "last" || $al eq "both"}]
5850 } elseif {$arrowlow} {
5851 if {[lsearch -exact [lindex $rowidlist [expr {$row-1}]] $id] >= 0 ||
5852 [lsearch -exact [lindex $parentlist [expr {$row-1}]] $id] >= 0} {
5853 set arrowlow 0
5856 set arrow [lindex {none first last both} [expr {$arrowhigh + 2*$arrowlow}]]
5857 for {set y $le} {[incr y -1] > $row} {} {
5858 set x $xp
5859 set xp [lindex $cols [expr {$y - 1 - $row}]]
5860 set ndir [expr {$xp - $x}]
5861 if {$dir != $ndir || $xp < 0} {
5862 lappend coords [xc $y $x] [yc $y]
5864 set dir $ndir
5866 if {!$joinlow} {
5867 if {$xp < 0} {
5868 # join parent line to first child
5869 set ch [lindex $displayorder $row]
5870 set xc [lsearch -exact [lindex $rowidlist $row] $ch]
5871 if {$xc < 0} {
5872 puts "oops: drawlineseg: child $ch not on row $row"
5873 } elseif {$xc != $x} {
5874 if {($arrowhigh && $le == $row + 1) || $dir == 0} {
5875 set d [expr {int(0.5 * $linespc)}]
5876 set x1 [xc $row $x]
5877 if {$xc < $x} {
5878 set x2 [expr {$x1 - $d}]
5879 } else {
5880 set x2 [expr {$x1 + $d}]
5882 set y2 [yc $row]
5883 set y1 [expr {$y2 + $d}]
5884 lappend coords $x1 $y1 $x2 $y2
5885 } elseif {$xc < $x - 1} {
5886 lappend coords [xc $row [expr {$x-1}]] [yc $row]
5887 } elseif {$xc > $x + 1} {
5888 lappend coords [xc $row [expr {$x+1}]] [yc $row]
5890 set x $xc
5892 lappend coords [xc $row $x] [yc $row]
5893 } else {
5894 set xn [xc $row $xp]
5895 set yn [yc $row]
5896 lappend coords $xn $yn
5898 if {!$joinhigh} {
5899 assigncolor $id
5900 set t [$canv create line $coords -width [linewidth $id] \
5901 -fill $colormap($id) -tags lines.$id -arrow $arrow]
5902 $canv lower $t
5903 bindline $t $id
5904 set lines [linsert $lines $i [list $row $le $t]]
5905 } else {
5906 $canv coords $ith $coords
5907 if {$arrow ne $ah} {
5908 $canv itemconf $ith -arrow $arrow
5910 lset lines $i 0 $row
5912 } else {
5913 set xo [lsearch -exact [lindex $rowidlist [expr {$row - 1}]] $id]
5914 set ndir [expr {$xo - $xp}]
5915 set clow [$canv coords $itl]
5916 if {$dir == $ndir} {
5917 set clow [lrange $clow 2 end]
5919 set coords [concat $coords $clow]
5920 if {!$joinhigh} {
5921 lset lines [expr {$i-1}] 1 $le
5922 } else {
5923 # coalesce two pieces
5924 $canv delete $ith
5925 set b [lindex $lines [expr {$i-1}] 0]
5926 set e [lindex $lines $i 1]
5927 set lines [lreplace $lines [expr {$i-1}] $i [list $b $e $itl]]
5929 $canv coords $itl $coords
5930 if {$arrow ne $al} {
5931 $canv itemconf $itl -arrow $arrow
5935 set linesegs($id) $lines
5936 return $le
5939 proc drawparentlinks {id row} {
5940 global rowidlist canv colormap curview parentlist
5941 global idpos linespc
5943 set rowids [lindex $rowidlist $row]
5944 set col [lsearch -exact $rowids $id]
5945 if {$col < 0} return
5946 set olds [lindex $parentlist $row]
5947 set row2 [expr {$row + 1}]
5948 set x [xc $row $col]
5949 set y [yc $row]
5950 set y2 [yc $row2]
5951 set d [expr {int(0.5 * $linespc)}]
5952 set ymid [expr {$y + $d}]
5953 set ids [lindex $rowidlist $row2]
5954 # rmx = right-most X coord used
5955 set rmx 0
5956 foreach p $olds {
5957 set i [lsearch -exact $ids $p]
5958 if {$i < 0} {
5959 puts "oops, parent $p of $id not in list"
5960 continue
5962 set x2 [xc $row2 $i]
5963 if {$x2 > $rmx} {
5964 set rmx $x2
5966 set j [lsearch -exact $rowids $p]
5967 if {$j < 0} {
5968 # drawlineseg will do this one for us
5969 continue
5971 assigncolor $p
5972 # should handle duplicated parents here...
5973 set coords [list $x $y]
5974 if {$i != $col} {
5975 # if attaching to a vertical segment, draw a smaller
5976 # slant for visual distinctness
5977 if {$i == $j} {
5978 if {$i < $col} {
5979 lappend coords [expr {$x2 + $d}] $y $x2 $ymid
5980 } else {
5981 lappend coords [expr {$x2 - $d}] $y $x2 $ymid
5983 } elseif {$i < $col && $i < $j} {
5984 # segment slants towards us already
5985 lappend coords [xc $row $j] $y
5986 } else {
5987 if {$i < $col - 1} {
5988 lappend coords [expr {$x2 + $linespc}] $y
5989 } elseif {$i > $col + 1} {
5990 lappend coords [expr {$x2 - $linespc}] $y
5992 lappend coords $x2 $y2
5994 } else {
5995 lappend coords $x2 $y2
5997 set t [$canv create line $coords -width [linewidth $p] \
5998 -fill $colormap($p) -tags lines.$p]
5999 $canv lower $t
6000 bindline $t $p
6002 if {$rmx > [lindex $idpos($id) 1]} {
6003 lset idpos($id) 1 $rmx
6004 redrawtags $id
6008 proc drawlines {id} {
6009 global canv
6011 $canv itemconf lines.$id -width [linewidth $id]
6014 proc drawcmittext {id row col} {
6015 global linespc canv canv2 canv3 fgcolor curview
6016 global cmitlisted commitinfo rowidlist parentlist
6017 global rowtextx idpos idtags idheads idotherrefs
6018 global linehtag linentag linedtag selectedline
6019 global canvxmax boldids boldnameids fgcolor markedid
6020 global mainheadid nullid nullid2 circleitem circlecolors ctxbut
6021 global mainheadcirclecolor workingfilescirclecolor indexcirclecolor
6022 global circleoutlinecolor
6024 # listed is 0 for boundary, 1 for normal, 2 for negative, 3 for left, 4 for right
6025 set listed $cmitlisted($curview,$id)
6026 if {$id eq $nullid} {
6027 set ofill $workingfilescirclecolor
6028 } elseif {$id eq $nullid2} {
6029 set ofill $indexcirclecolor
6030 } elseif {$id eq $mainheadid} {
6031 set ofill $mainheadcirclecolor
6032 } else {
6033 set ofill [lindex $circlecolors $listed]
6035 set x [xc $row $col]
6036 set y [yc $row]
6037 set orad [expr {$linespc / 3}]
6038 if {$listed <= 2} {
6039 set t [$canv create oval [expr {$x - $orad}] [expr {$y - $orad}] \
6040 [expr {$x + $orad - 1}] [expr {$y + $orad - 1}] \
6041 -fill $ofill -outline $circleoutlinecolor -width 1 -tags circle]
6042 } elseif {$listed == 3} {
6043 # triangle pointing left for left-side commits
6044 set t [$canv create polygon \
6045 [expr {$x - $orad}] $y \
6046 [expr {$x + $orad - 1}] [expr {$y - $orad}] \
6047 [expr {$x + $orad - 1}] [expr {$y + $orad - 1}] \
6048 -fill $ofill -outline $circleoutlinecolor -width 1 -tags circle]
6049 } else {
6050 # triangle pointing right for right-side commits
6051 set t [$canv create polygon \
6052 [expr {$x + $orad - 1}] $y \
6053 [expr {$x - $orad}] [expr {$y - $orad}] \
6054 [expr {$x - $orad}] [expr {$y + $orad - 1}] \
6055 -fill $ofill -outline $circleoutlinecolor -width 1 -tags circle]
6057 set circleitem($row) $t
6058 $canv raise $t
6059 $canv bind $t <1> {selcanvline {} %x %y}
6060 set rmx [llength [lindex $rowidlist $row]]
6061 set olds [lindex $parentlist $row]
6062 if {$olds ne {}} {
6063 set nextids [lindex $rowidlist [expr {$row + 1}]]
6064 foreach p $olds {
6065 set i [lsearch -exact $nextids $p]
6066 if {$i > $rmx} {
6067 set rmx $i
6071 set xt [xc $row $rmx]
6072 set rowtextx($row) $xt
6073 set idpos($id) [list $x $xt $y]
6074 if {[info exists idtags($id)] || [info exists idheads($id)]
6075 || [info exists idotherrefs($id)]} {
6076 set xt [drawtags $id $x $xt $y]
6078 if {[lindex $commitinfo($id) 6] > 0} {
6079 set xt [drawnotesign $xt $y]
6081 set headline [lindex $commitinfo($id) 0]
6082 set name [lindex $commitinfo($id) 1]
6083 set date [lindex $commitinfo($id) 2]
6084 set date [formatdate $date]
6085 set font mainfont
6086 set nfont mainfont
6087 set isbold [ishighlighted $id]
6088 if {$isbold > 0} {
6089 lappend boldids $id
6090 set font mainfontbold
6091 if {$isbold > 1} {
6092 lappend boldnameids $id
6093 set nfont mainfontbold
6096 set linehtag($id) [$canv create text $xt $y -anchor w -fill $fgcolor \
6097 -text $headline -font $font -tags text]
6098 $canv bind $linehtag($id) $ctxbut "rowmenu %X %Y $id"
6099 set linentag($id) [$canv2 create text 3 $y -anchor w -fill $fgcolor \
6100 -text $name -font $nfont -tags text]
6101 set linedtag($id) [$canv3 create text 3 $y -anchor w -fill $fgcolor \
6102 -text $date -font mainfont -tags text]
6103 if {$selectedline == $row} {
6104 make_secsel $id
6106 if {[info exists markedid] && $markedid eq $id} {
6107 make_idmark $id
6109 set xr [expr {$xt + [font measure $font $headline]}]
6110 if {$xr > $canvxmax} {
6111 set canvxmax $xr
6112 setcanvscroll
6116 proc drawcmitrow {row} {
6117 global displayorder rowidlist nrows_drawn
6118 global iddrawn markingmatches
6119 global commitinfo numcommits
6120 global filehighlight fhighlights findpattern nhighlights
6121 global hlview vhighlights
6122 global highlight_related rhighlights
6124 if {$row >= $numcommits} return
6126 set id [lindex $displayorder $row]
6127 if {[info exists hlview] && ![info exists vhighlights($id)]} {
6128 askvhighlight $row $id
6130 if {[info exists filehighlight] && ![info exists fhighlights($id)]} {
6131 askfilehighlight $row $id
6133 if {$findpattern ne {} && ![info exists nhighlights($id)]} {
6134 askfindhighlight $row $id
6136 if {$highlight_related ne [mc "None"] && ![info exists rhighlights($id)]} {
6137 askrelhighlight $row $id
6139 if {![info exists iddrawn($id)]} {
6140 set col [lsearch -exact [lindex $rowidlist $row] $id]
6141 if {$col < 0} {
6142 puts "oops, row $row id $id not in list"
6143 return
6145 if {![info exists commitinfo($id)]} {
6146 getcommit $id
6148 assigncolor $id
6149 drawcmittext $id $row $col
6150 set iddrawn($id) 1
6151 incr nrows_drawn
6153 if {$markingmatches} {
6154 markrowmatches $row $id
6158 proc drawcommits {row {endrow {}}} {
6159 global numcommits iddrawn displayorder curview need_redisplay
6160 global parentlist rowidlist rowfinal uparrowlen downarrowlen nrows_drawn
6162 if {$row < 0} {
6163 set row 0
6165 if {$endrow eq {}} {
6166 set endrow $row
6168 if {$endrow >= $numcommits} {
6169 set endrow [expr {$numcommits - 1}]
6172 set rl1 [expr {$row - $downarrowlen - 3}]
6173 if {$rl1 < 0} {
6174 set rl1 0
6176 set ro1 [expr {$row - 3}]
6177 if {$ro1 < 0} {
6178 set ro1 0
6180 set r2 [expr {$endrow + $uparrowlen + 3}]
6181 if {$r2 > $numcommits} {
6182 set r2 $numcommits
6184 for {set r $rl1} {$r < $r2} {incr r} {
6185 if {[lindex $rowidlist $r] ne {} && [lindex $rowfinal $r]} {
6186 if {$rl1 < $r} {
6187 layoutrows $rl1 $r
6189 set rl1 [expr {$r + 1}]
6192 if {$rl1 < $r} {
6193 layoutrows $rl1 $r
6195 optimize_rows $ro1 0 $r2
6196 if {$need_redisplay || $nrows_drawn > 2000} {
6197 clear_display
6200 # make the lines join to already-drawn rows either side
6201 set r [expr {$row - 1}]
6202 if {$r < 0 || ![info exists iddrawn([lindex $displayorder $r])]} {
6203 set r $row
6205 set er [expr {$endrow + 1}]
6206 if {$er >= $numcommits ||
6207 ![info exists iddrawn([lindex $displayorder $er])]} {
6208 set er $endrow
6210 for {} {$r <= $er} {incr r} {
6211 set id [lindex $displayorder $r]
6212 set wasdrawn [info exists iddrawn($id)]
6213 drawcmitrow $r
6214 if {$r == $er} break
6215 set nextid [lindex $displayorder [expr {$r + 1}]]
6216 if {$wasdrawn && [info exists iddrawn($nextid)]} continue
6217 drawparentlinks $id $r
6219 set rowids [lindex $rowidlist $r]
6220 foreach lid $rowids {
6221 if {$lid eq {}} continue
6222 if {[info exists lineend($lid)] && $lineend($lid) > $r} continue
6223 if {$lid eq $id} {
6224 # see if this is the first child of any of its parents
6225 foreach p [lindex $parentlist $r] {
6226 if {[lsearch -exact $rowids $p] < 0} {
6227 # make this line extend up to the child
6228 set lineend($p) [drawlineseg $p $r $er 0]
6231 } else {
6232 set lineend($lid) [drawlineseg $lid $r $er 1]
6238 proc undolayout {row} {
6239 global uparrowlen mingaplen downarrowlen
6240 global rowidlist rowisopt rowfinal need_redisplay
6242 set r [expr {$row - ($uparrowlen + $mingaplen + $downarrowlen)}]
6243 if {$r < 0} {
6244 set r 0
6246 if {[llength $rowidlist] > $r} {
6247 incr r -1
6248 set rowidlist [lrange $rowidlist 0 $r]
6249 set rowfinal [lrange $rowfinal 0 $r]
6250 set rowisopt [lrange $rowisopt 0 $r]
6251 set need_redisplay 1
6252 run drawvisible
6256 proc drawvisible {} {
6257 global canv linespc curview vrowmod selectedline targetrow targetid
6258 global need_redisplay cscroll numcommits
6260 set fs [$canv yview]
6261 set ymax [lindex [$canv cget -scrollregion] 3]
6262 if {$ymax eq {} || $ymax == 0 || $numcommits == 0} return
6263 set f0 [lindex $fs 0]
6264 set f1 [lindex $fs 1]
6265 set y0 [expr {int($f0 * $ymax)}]
6266 set y1 [expr {int($f1 * $ymax)}]
6268 if {[info exists targetid]} {
6269 if {[commitinview $targetid $curview]} {
6270 set r [rowofcommit $targetid]
6271 if {$r != $targetrow} {
6272 # Fix up the scrollregion and change the scrolling position
6273 # now that our target row has moved.
6274 set diff [expr {($r - $targetrow) * $linespc}]
6275 set targetrow $r
6276 setcanvscroll
6277 set ymax [lindex [$canv cget -scrollregion] 3]
6278 incr y0 $diff
6279 incr y1 $diff
6280 set f0 [expr {$y0 / $ymax}]
6281 set f1 [expr {$y1 / $ymax}]
6282 allcanvs yview moveto $f0
6283 $cscroll set $f0 $f1
6284 set need_redisplay 1
6286 } else {
6287 unset targetid
6291 set row [expr {int(($y0 - 3) / $linespc) - 1}]
6292 set endrow [expr {int(($y1 - 3) / $linespc) + 1}]
6293 if {$endrow >= $vrowmod($curview)} {
6294 update_arcrows $curview
6296 if {$selectedline ne {} &&
6297 $row <= $selectedline && $selectedline <= $endrow} {
6298 set targetrow $selectedline
6299 } elseif {[info exists targetid]} {
6300 set targetrow [expr {int(($row + $endrow) / 2)}]
6302 if {[info exists targetrow]} {
6303 if {$targetrow >= $numcommits} {
6304 set targetrow [expr {$numcommits - 1}]
6306 set targetid [commitonrow $targetrow]
6308 drawcommits $row $endrow
6311 proc clear_display {} {
6312 global iddrawn linesegs need_redisplay nrows_drawn
6313 global vhighlights fhighlights nhighlights rhighlights
6314 global linehtag linentag linedtag boldids boldnameids
6316 allcanvs delete all
6317 catch {unset iddrawn}
6318 catch {unset linesegs}
6319 catch {unset linehtag}
6320 catch {unset linentag}
6321 catch {unset linedtag}
6322 set boldids {}
6323 set boldnameids {}
6324 catch {unset vhighlights}
6325 catch {unset fhighlights}
6326 catch {unset nhighlights}
6327 catch {unset rhighlights}
6328 set need_redisplay 0
6329 set nrows_drawn 0
6332 proc findcrossings {id} {
6333 global rowidlist parentlist numcommits displayorder
6335 set cross {}
6336 set ccross {}
6337 foreach {s e} [rowranges $id] {
6338 if {$e >= $numcommits} {
6339 set e [expr {$numcommits - 1}]
6341 if {$e <= $s} continue
6342 for {set row $e} {[incr row -1] >= $s} {} {
6343 set x [lsearch -exact [lindex $rowidlist $row] $id]
6344 if {$x < 0} break
6345 set olds [lindex $parentlist $row]
6346 set kid [lindex $displayorder $row]
6347 set kidx [lsearch -exact [lindex $rowidlist $row] $kid]
6348 if {$kidx < 0} continue
6349 set nextrow [lindex $rowidlist [expr {$row + 1}]]
6350 foreach p $olds {
6351 set px [lsearch -exact $nextrow $p]
6352 if {$px < 0} continue
6353 if {($kidx < $x && $x < $px) || ($px < $x && $x < $kidx)} {
6354 if {[lsearch -exact $ccross $p] >= 0} continue
6355 if {$x == $px + ($kidx < $px? -1: 1)} {
6356 lappend ccross $p
6357 } elseif {[lsearch -exact $cross $p] < 0} {
6358 lappend cross $p
6364 return [concat $ccross {{}} $cross]
6367 proc assigncolor {id} {
6368 global colormap colors nextcolor
6369 global parents children children curview
6371 if {[info exists colormap($id)]} return
6372 set ncolors [llength $colors]
6373 if {[info exists children($curview,$id)]} {
6374 set kids $children($curview,$id)
6375 } else {
6376 set kids {}
6378 if {[llength $kids] == 1} {
6379 set child [lindex $kids 0]
6380 if {[info exists colormap($child)]
6381 && [llength $parents($curview,$child)] == 1} {
6382 set colormap($id) $colormap($child)
6383 return
6386 set badcolors {}
6387 set origbad {}
6388 foreach x [findcrossings $id] {
6389 if {$x eq {}} {
6390 # delimiter between corner crossings and other crossings
6391 if {[llength $badcolors] >= $ncolors - 1} break
6392 set origbad $badcolors
6394 if {[info exists colormap($x)]
6395 && [lsearch -exact $badcolors $colormap($x)] < 0} {
6396 lappend badcolors $colormap($x)
6399 if {[llength $badcolors] >= $ncolors} {
6400 set badcolors $origbad
6402 set origbad $badcolors
6403 if {[llength $badcolors] < $ncolors - 1} {
6404 foreach child $kids {
6405 if {[info exists colormap($child)]
6406 && [lsearch -exact $badcolors $colormap($child)] < 0} {
6407 lappend badcolors $colormap($child)
6409 foreach p $parents($curview,$child) {
6410 if {[info exists colormap($p)]
6411 && [lsearch -exact $badcolors $colormap($p)] < 0} {
6412 lappend badcolors $colormap($p)
6416 if {[llength $badcolors] >= $ncolors} {
6417 set badcolors $origbad
6420 for {set i 0} {$i <= $ncolors} {incr i} {
6421 set c [lindex $colors $nextcolor]
6422 if {[incr nextcolor] >= $ncolors} {
6423 set nextcolor 0
6425 if {[lsearch -exact $badcolors $c]} break
6427 set colormap($id) $c
6430 proc bindline {t id} {
6431 global canv
6433 $canv bind $t <Enter> "lineenter %x %y $id"
6434 $canv bind $t <Motion> "linemotion %x %y $id"
6435 $canv bind $t <Leave> "lineleave $id"
6436 $canv bind $t <Button-1> "lineclick %x %y $id 1"
6439 proc graph_pane_width {} {
6440 global use_ttk
6442 if {$use_ttk} {
6443 set g [.tf.histframe.pwclist sashpos 0]
6444 } else {
6445 set g [.tf.histframe.pwclist sash coord 0]
6447 return [lindex $g 0]
6450 proc totalwidth {l font extra} {
6451 set tot 0
6452 foreach str $l {
6453 set tot [expr {$tot + [font measure $font $str] + $extra}]
6455 return $tot
6458 proc drawtags {id x xt y1} {
6459 global idtags idheads idotherrefs mainhead
6460 global linespc lthickness
6461 global canv rowtextx curview fgcolor bgcolor ctxbut
6462 global headbgcolor headfgcolor headoutlinecolor remotebgcolor
6463 global tagbgcolor tagfgcolor tagoutlinecolor
6464 global reflinecolor
6466 set marks {}
6467 set ntags 0
6468 set nheads 0
6469 set singletag 0
6470 set maxtags 3
6471 set maxtagpct 25
6472 set maxwidth [expr {[graph_pane_width] * $maxtagpct / 100}]
6473 set delta [expr {int(0.5 * ($linespc - $lthickness))}]
6474 set extra [expr {$delta + $lthickness + $linespc}]
6476 if {[info exists idtags($id)]} {
6477 set marks $idtags($id)
6478 set ntags [llength $marks]
6479 if {$ntags > $maxtags ||
6480 [totalwidth $marks mainfont $extra] > $maxwidth} {
6481 # show just a single "n tags..." tag
6482 set singletag 1
6483 if {$ntags == 1} {
6484 set marks [list "tag..."]
6485 } else {
6486 set marks [list [format "%d tags..." $ntags]]
6488 set ntags 1
6491 if {[info exists idheads($id)]} {
6492 set marks [concat $marks $idheads($id)]
6493 set nheads [llength $idheads($id)]
6495 if {[info exists idotherrefs($id)]} {
6496 set marks [concat $marks $idotherrefs($id)]
6498 if {$marks eq {}} {
6499 return $xt
6502 set yt [expr {$y1 - 0.5 * $linespc}]
6503 set yb [expr {$yt + $linespc - 1}]
6504 set xvals {}
6505 set wvals {}
6506 set i -1
6507 foreach tag $marks {
6508 incr i
6509 if {$i >= $ntags && $i < $ntags + $nheads && $tag eq $mainhead} {
6510 set wid [font measure mainfontbold $tag]
6511 } else {
6512 set wid [font measure mainfont $tag]
6514 lappend xvals $xt
6515 lappend wvals $wid
6516 set xt [expr {$xt + $wid + $extra}]
6518 set t [$canv create line $x $y1 [lindex $xvals end] $y1 \
6519 -width $lthickness -fill $reflinecolor -tags tag.$id]
6520 $canv lower $t
6521 foreach tag $marks x $xvals wid $wvals {
6522 set tag_quoted [string map {% %%} $tag]
6523 set xl [expr {$x + $delta}]
6524 set xr [expr {$x + $delta + $wid + $lthickness}]
6525 set font mainfont
6526 if {[incr ntags -1] >= 0} {
6527 # draw a tag
6528 set t [$canv create polygon $x [expr {$yt + $delta}] $xl $yt \
6529 $xr $yt $xr $yb $xl $yb $x [expr {$yb - $delta}] \
6530 -width 1 -outline $tagoutlinecolor -fill $tagbgcolor \
6531 -tags tag.$id]
6532 if {$singletag} {
6533 set tagclick [list showtags $id 1]
6534 } else {
6535 set tagclick [list showtag $tag_quoted 1]
6537 $canv bind $t <1> $tagclick
6538 set rowtextx([rowofcommit $id]) [expr {$xr + $linespc}]
6539 } else {
6540 # draw a head or other ref
6541 if {[incr nheads -1] >= 0} {
6542 set col $headbgcolor
6543 if {$tag eq $mainhead} {
6544 set font mainfontbold
6546 } else {
6547 set col "#ddddff"
6549 set xl [expr {$xl - $delta/2}]
6550 $canv create polygon $x $yt $xr $yt $xr $yb $x $yb \
6551 -width 1 -outline black -fill $col -tags tag.$id
6552 if {[regexp {^(remotes/.*/|remotes/)} $tag match remoteprefix]} {
6553 set rwid [font measure mainfont $remoteprefix]
6554 set xi [expr {$x + 1}]
6555 set yti [expr {$yt + 1}]
6556 set xri [expr {$x + $rwid}]
6557 $canv create polygon $xi $yti $xri $yti $xri $yb $xi $yb \
6558 -width 0 -fill $remotebgcolor -tags tag.$id
6561 set t [$canv create text $xl $y1 -anchor w -text $tag -fill $headfgcolor \
6562 -font $font -tags [list tag.$id text]]
6563 if {$ntags >= 0} {
6564 $canv bind $t <1> $tagclick
6565 } elseif {$nheads >= 0} {
6566 $canv bind $t $ctxbut [list headmenu %X %Y $id $tag_quoted]
6569 return $xt
6572 proc drawnotesign {xt y} {
6573 global linespc canv fgcolor
6575 set orad [expr {$linespc / 3}]
6576 set t [$canv create rectangle [expr {$xt - $orad}] [expr {$y - $orad}] \
6577 [expr {$xt + $orad - 1}] [expr {$y + $orad - 1}] \
6578 -fill yellow -outline $fgcolor -width 1 -tags circle]
6579 set xt [expr {$xt + $orad * 3}]
6580 return $xt
6583 proc xcoord {i level ln} {
6584 global canvx0 xspc1 xspc2
6586 set x [expr {$canvx0 + $i * $xspc1($ln)}]
6587 if {$i > 0 && $i == $level} {
6588 set x [expr {$x + 0.5 * ($xspc2 - $xspc1($ln))}]
6589 } elseif {$i > $level} {
6590 set x [expr {$x + $xspc2 - $xspc1($ln)}]
6592 return $x
6595 proc show_status {msg} {
6596 global canv fgcolor
6598 clear_display
6599 $canv create text 3 3 -anchor nw -text $msg -font mainfont \
6600 -tags text -fill $fgcolor
6603 # Don't change the text pane cursor if it is currently the hand cursor,
6604 # showing that we are over a sha1 ID link.
6605 proc settextcursor {c} {
6606 global ctext curtextcursor
6608 if {[$ctext cget -cursor] == $curtextcursor} {
6609 $ctext config -cursor $c
6611 set curtextcursor $c
6614 proc nowbusy {what {name {}}} {
6615 global isbusy busyname statusw
6617 if {[array names isbusy] eq {}} {
6618 . config -cursor watch
6619 settextcursor watch
6621 set isbusy($what) 1
6622 set busyname($what) $name
6623 if {$name ne {}} {
6624 $statusw conf -text $name
6628 proc notbusy {what} {
6629 global isbusy maincursor textcursor busyname statusw
6631 catch {
6632 unset isbusy($what)
6633 if {$busyname($what) ne {} &&
6634 [$statusw cget -text] eq $busyname($what)} {
6635 $statusw conf -text {}
6638 if {[array names isbusy] eq {}} {
6639 . config -cursor $maincursor
6640 settextcursor $textcursor
6644 proc findmatches {f} {
6645 global findtype findstring
6646 if {$findtype == [mc "Regexp"]} {
6647 set matches [regexp -indices -all -inline $findstring $f]
6648 } else {
6649 set fs $findstring
6650 if {$findtype == [mc "IgnCase"]} {
6651 set f [string tolower $f]
6652 set fs [string tolower $fs]
6654 set matches {}
6655 set i 0
6656 set l [string length $fs]
6657 while {[set j [string first $fs $f $i]] >= 0} {
6658 lappend matches [list $j [expr {$j+$l-1}]]
6659 set i [expr {$j + $l}]
6662 return $matches
6665 proc dofind {{dirn 1} {wrap 1}} {
6666 global findstring findstartline findcurline selectedline numcommits
6667 global gdttype filehighlight fh_serial find_dirn findallowwrap
6669 if {[info exists find_dirn]} {
6670 if {$find_dirn == $dirn} return
6671 stopfinding
6673 focus .
6674 if {$findstring eq {} || $numcommits == 0} return
6675 if {$selectedline eq {}} {
6676 set findstartline [lindex [visiblerows] [expr {$dirn < 0}]]
6677 } else {
6678 set findstartline $selectedline
6680 set findcurline $findstartline
6681 nowbusy finding [mc "Searching"]
6682 if {$gdttype ne [mc "containing:"] && ![info exists filehighlight]} {
6683 after cancel do_file_hl $fh_serial
6684 do_file_hl $fh_serial
6686 set find_dirn $dirn
6687 set findallowwrap $wrap
6688 run findmore
6691 proc stopfinding {} {
6692 global find_dirn findcurline fprogcoord
6694 if {[info exists find_dirn]} {
6695 unset find_dirn
6696 unset findcurline
6697 notbusy finding
6698 set fprogcoord 0
6699 adjustprogress
6701 stopblaming
6704 proc findmore {} {
6705 global commitdata commitinfo numcommits findpattern findloc
6706 global findstartline findcurline findallowwrap
6707 global find_dirn gdttype fhighlights fprogcoord
6708 global curview varcorder vrownum varccommits vrowmod
6710 if {![info exists find_dirn]} {
6711 return 0
6713 set fldtypes [list [mc "Headline"] [mc "Author"] "" [mc "Committer"] "" [mc "Comments"]]
6714 set l $findcurline
6715 set moretodo 0
6716 if {$find_dirn > 0} {
6717 incr l
6718 if {$l >= $numcommits} {
6719 set l 0
6721 if {$l <= $findstartline} {
6722 set lim [expr {$findstartline + 1}]
6723 } else {
6724 set lim $numcommits
6725 set moretodo $findallowwrap
6727 } else {
6728 if {$l == 0} {
6729 set l $numcommits
6731 incr l -1
6732 if {$l >= $findstartline} {
6733 set lim [expr {$findstartline - 1}]
6734 } else {
6735 set lim -1
6736 set moretodo $findallowwrap
6739 set n [expr {($lim - $l) * $find_dirn}]
6740 if {$n > 500} {
6741 set n 500
6742 set moretodo 1
6744 if {$l + ($find_dirn > 0? $n: 1) > $vrowmod($curview)} {
6745 update_arcrows $curview
6747 set found 0
6748 set domore 1
6749 set ai [bsearch $vrownum($curview) $l]
6750 set a [lindex $varcorder($curview) $ai]
6751 set arow [lindex $vrownum($curview) $ai]
6752 set ids [lindex $varccommits($curview,$a)]
6753 set arowend [expr {$arow + [llength $ids]}]
6754 if {$gdttype eq [mc "containing:"]} {
6755 for {} {$n > 0} {incr n -1; incr l $find_dirn} {
6756 if {$l < $arow || $l >= $arowend} {
6757 incr ai $find_dirn
6758 set a [lindex $varcorder($curview) $ai]
6759 set arow [lindex $vrownum($curview) $ai]
6760 set ids [lindex $varccommits($curview,$a)]
6761 set arowend [expr {$arow + [llength $ids]}]
6763 set id [lindex $ids [expr {$l - $arow}]]
6764 # shouldn't happen unless git log doesn't give all the commits...
6765 if {![info exists commitdata($id)] ||
6766 ![doesmatch $commitdata($id)]} {
6767 continue
6769 if {![info exists commitinfo($id)]} {
6770 getcommit $id
6772 set info $commitinfo($id)
6773 foreach f $info ty $fldtypes {
6774 if {$ty eq ""} continue
6775 if {($findloc eq [mc "All fields"] || $findloc eq $ty) &&
6776 [doesmatch $f]} {
6777 set found 1
6778 break
6781 if {$found} break
6783 } else {
6784 for {} {$n > 0} {incr n -1; incr l $find_dirn} {
6785 if {$l < $arow || $l >= $arowend} {
6786 incr ai $find_dirn
6787 set a [lindex $varcorder($curview) $ai]
6788 set arow [lindex $vrownum($curview) $ai]
6789 set ids [lindex $varccommits($curview,$a)]
6790 set arowend [expr {$arow + [llength $ids]}]
6792 set id [lindex $ids [expr {$l - $arow}]]
6793 if {![info exists fhighlights($id)]} {
6794 # this sets fhighlights($id) to -1
6795 askfilehighlight $l $id
6797 if {$fhighlights($id) > 0} {
6798 set found $domore
6799 break
6801 if {$fhighlights($id) < 0} {
6802 if {$domore} {
6803 set domore 0
6804 set findcurline [expr {$l - $find_dirn}]
6809 if {$found || ($domore && !$moretodo)} {
6810 unset findcurline
6811 unset find_dirn
6812 notbusy finding
6813 set fprogcoord 0
6814 adjustprogress
6815 if {$found} {
6816 findselectline $l
6817 } else {
6818 bell
6820 return 0
6822 if {!$domore} {
6823 flushhighlights
6824 } else {
6825 set findcurline [expr {$l - $find_dirn}]
6827 set n [expr {($findcurline - $findstartline) * $find_dirn - 1}]
6828 if {$n < 0} {
6829 incr n $numcommits
6831 set fprogcoord [expr {$n * 1.0 / $numcommits}]
6832 adjustprogress
6833 return $domore
6836 proc findselectline {l} {
6837 global findloc commentend ctext findcurline markingmatches gdttype
6839 set markingmatches [expr {$gdttype eq [mc "containing:"]}]
6840 set findcurline $l
6841 selectline $l 1
6842 if {$markingmatches &&
6843 ($findloc eq [mc "All fields"] || $findloc eq [mc "Comments"])} {
6844 # highlight the matches in the comments
6845 set f [$ctext get 1.0 $commentend]
6846 set matches [findmatches $f]
6847 foreach match $matches {
6848 set start [lindex $match 0]
6849 set end [expr {[lindex $match 1] + 1}]
6850 $ctext tag add found "1.0 + $start c" "1.0 + $end c"
6853 drawvisible
6856 # mark the bits of a headline or author that match a find string
6857 proc markmatches {canv l str tag matches font row} {
6858 global selectedline
6860 set bbox [$canv bbox $tag]
6861 set x0 [lindex $bbox 0]
6862 set y0 [lindex $bbox 1]
6863 set y1 [lindex $bbox 3]
6864 foreach match $matches {
6865 set start [lindex $match 0]
6866 set end [lindex $match 1]
6867 if {$start > $end} continue
6868 set xoff [font measure $font [string range $str 0 [expr {$start-1}]]]
6869 set xlen [font measure $font [string range $str 0 [expr {$end}]]]
6870 set t [$canv create rect [expr {$x0+$xoff}] $y0 \
6871 [expr {$x0+$xlen+2}] $y1 \
6872 -outline {} -tags [list match$l matches] -fill yellow]
6873 $canv lower $t
6874 if {$row == $selectedline} {
6875 $canv raise $t secsel
6880 proc unmarkmatches {} {
6881 global markingmatches
6883 allcanvs delete matches
6884 set markingmatches 0
6885 stopfinding
6888 proc selcanvline {w x y} {
6889 global canv canvy0 ctext linespc
6890 global rowtextx
6891 set ymax [lindex [$canv cget -scrollregion] 3]
6892 if {$ymax == {}} return
6893 set yfrac [lindex [$canv yview] 0]
6894 set y [expr {$y + $yfrac * $ymax}]
6895 set l [expr {int(($y - $canvy0) / $linespc + 0.5)}]
6896 if {$l < 0} {
6897 set l 0
6899 if {$w eq $canv} {
6900 set xmax [lindex [$canv cget -scrollregion] 2]
6901 set xleft [expr {[lindex [$canv xview] 0] * $xmax}]
6902 if {![info exists rowtextx($l)] || $xleft + $x < $rowtextx($l)} return
6904 unmarkmatches
6905 selectline $l 1
6908 proc commit_descriptor {p} {
6909 global commitinfo
6910 if {![info exists commitinfo($p)]} {
6911 getcommit $p
6913 set l "..."
6914 if {[llength $commitinfo($p)] > 1} {
6915 set l [lindex $commitinfo($p) 0]
6917 return "$p ($l)\n"
6920 # append some text to the ctext widget, and make any SHA1 ID
6921 # that we know about be a clickable link.
6922 proc appendwithlinks {text tags} {
6923 global ctext linknum curview
6925 set start [$ctext index "end - 1c"]
6926 $ctext insert end $text $tags
6927 set links [regexp -indices -all -inline {(?:\m|-g)[0-9a-f]{6,40}\M} $text]
6928 foreach l $links {
6929 set s [lindex $l 0]
6930 set e [lindex $l 1]
6931 set linkid [string range $text $s $e]
6932 incr e
6933 $ctext tag delete link$linknum
6934 $ctext tag add link$linknum "$start + $s c" "$start + $e c"
6935 setlink $linkid link$linknum
6936 incr linknum
6940 proc setlink {id lk} {
6941 global curview ctext pendinglinks
6942 global linkfgcolor
6944 if {[string range $id 0 1] eq "-g"} {
6945 set id [string range $id 2 end]
6948 set known 0
6949 if {[string length $id] < 40} {
6950 set matches [longid $id]
6951 if {[llength $matches] > 0} {
6952 if {[llength $matches] > 1} return
6953 set known 1
6954 set id [lindex $matches 0]
6956 } else {
6957 set known [commitinview $id $curview]
6959 if {$known} {
6960 $ctext tag conf $lk -foreground $linkfgcolor -underline 1
6961 $ctext tag bind $lk <1> [list selbyid $id]
6962 $ctext tag bind $lk <Enter> {linkcursor %W 1}
6963 $ctext tag bind $lk <Leave> {linkcursor %W -1}
6964 } else {
6965 lappend pendinglinks($id) $lk
6966 interestedin $id {makelink %P}
6970 proc appendshortlink {id {pre {}} {post {}}} {
6971 global ctext linknum
6973 $ctext insert end $pre
6974 $ctext tag delete link$linknum
6975 $ctext insert end [string range $id 0 7] link$linknum
6976 $ctext insert end $post
6977 setlink $id link$linknum
6978 incr linknum
6981 proc makelink {id} {
6982 global pendinglinks
6984 if {![info exists pendinglinks($id)]} return
6985 foreach lk $pendinglinks($id) {
6986 setlink $id $lk
6988 unset pendinglinks($id)
6991 proc linkcursor {w inc} {
6992 global linkentercount curtextcursor
6994 if {[incr linkentercount $inc] > 0} {
6995 $w configure -cursor hand2
6996 } else {
6997 $w configure -cursor $curtextcursor
6998 if {$linkentercount < 0} {
6999 set linkentercount 0
7004 proc viewnextline {dir} {
7005 global canv linespc
7007 $canv delete hover
7008 set ymax [lindex [$canv cget -scrollregion] 3]
7009 set wnow [$canv yview]
7010 set wtop [expr {[lindex $wnow 0] * $ymax}]
7011 set newtop [expr {$wtop + $dir * $linespc}]
7012 if {$newtop < 0} {
7013 set newtop 0
7014 } elseif {$newtop > $ymax} {
7015 set newtop $ymax
7017 allcanvs yview moveto [expr {$newtop * 1.0 / $ymax}]
7020 # add a list of tag or branch names at position pos
7021 # returns the number of names inserted
7022 proc appendrefs {pos ids var} {
7023 global ctext linknum curview $var maxrefs mainheadid
7025 if {[catch {$ctext index $pos}]} {
7026 return 0
7028 $ctext conf -state normal
7029 $ctext delete $pos "$pos lineend"
7030 set tags {}
7031 foreach id $ids {
7032 foreach tag [set $var\($id\)] {
7033 lappend tags [list $tag $id]
7037 set sep {}
7038 set tags [lsort -index 0 -decreasing $tags]
7039 set nutags 0
7041 if {[llength $tags] > $maxrefs} {
7042 # If we are displaying heads, and there are too many,
7043 # see if there are some important heads to display.
7044 # Currently this means "master" and the current head.
7045 set itags {}
7046 if {$var eq "idheads"} {
7047 set utags {}
7048 foreach ti $tags {
7049 set hname [lindex $ti 0]
7050 set id [lindex $ti 1]
7051 if {($hname eq "master" || $id eq $mainheadid) &&
7052 [llength $itags] < $maxrefs} {
7053 lappend itags $ti
7054 } else {
7055 lappend utags $ti
7058 set tags $utags
7060 if {$itags ne {}} {
7061 set str [mc "and many more"]
7062 set sep " "
7063 } else {
7064 set str [mc "many"]
7066 $ctext insert $pos "$str ([llength $tags])"
7067 set nutags [llength $tags]
7068 set tags $itags
7071 foreach ti $tags {
7072 set id [lindex $ti 1]
7073 set lk link$linknum
7074 incr linknum
7075 $ctext tag delete $lk
7076 $ctext insert $pos $sep
7077 $ctext insert $pos [lindex $ti 0] $lk
7078 setlink $id $lk
7079 set sep ", "
7081 $ctext tag add wwrap "$pos linestart" "$pos lineend"
7082 $ctext conf -state disabled
7083 return [expr {[llength $tags] + $nutags}]
7086 # called when we have finished computing the nearby tags
7087 proc dispneartags {delay} {
7088 global selectedline currentid showneartags tagphase
7090 if {$selectedline eq {} || !$showneartags} return
7091 after cancel dispnexttag
7092 if {$delay} {
7093 after 200 dispnexttag
7094 set tagphase -1
7095 } else {
7096 after idle dispnexttag
7097 set tagphase 0
7101 proc dispnexttag {} {
7102 global selectedline currentid showneartags tagphase ctext
7104 if {$selectedline eq {} || !$showneartags} return
7105 switch -- $tagphase {
7107 set dtags [desctags $currentid]
7108 if {$dtags ne {}} {
7109 appendrefs precedes $dtags idtags
7113 set atags [anctags $currentid]
7114 if {$atags ne {}} {
7115 appendrefs follows $atags idtags
7119 set dheads [descheads $currentid]
7120 if {$dheads ne {}} {
7121 if {[appendrefs branch $dheads idheads] > 1
7122 && [$ctext get "branch -3c"] eq "h"} {
7123 # turn "Branch" into "Branches"
7124 $ctext conf -state normal
7125 $ctext insert "branch -2c" "es"
7126 $ctext conf -state disabled
7131 if {[incr tagphase] <= 2} {
7132 after idle dispnexttag
7136 proc make_secsel {id} {
7137 global linehtag linentag linedtag canv canv2 canv3
7139 if {![info exists linehtag($id)]} return
7140 $canv delete secsel
7141 set t [eval $canv create rect [$canv bbox $linehtag($id)] -outline {{}} \
7142 -tags secsel -fill [$canv cget -selectbackground]]
7143 $canv lower $t
7144 $canv2 delete secsel
7145 set t [eval $canv2 create rect [$canv2 bbox $linentag($id)] -outline {{}} \
7146 -tags secsel -fill [$canv2 cget -selectbackground]]
7147 $canv2 lower $t
7148 $canv3 delete secsel
7149 set t [eval $canv3 create rect [$canv3 bbox $linedtag($id)] -outline {{}} \
7150 -tags secsel -fill [$canv3 cget -selectbackground]]
7151 $canv3 lower $t
7154 proc make_idmark {id} {
7155 global linehtag canv fgcolor
7157 if {![info exists linehtag($id)]} return
7158 $canv delete markid
7159 set t [eval $canv create rect [$canv bbox $linehtag($id)] \
7160 -tags markid -outline $fgcolor]
7161 $canv raise $t
7164 proc selectline {l isnew {desired_loc {}}} {
7165 global canv ctext commitinfo selectedline
7166 global canvy0 linespc parents children curview
7167 global currentid sha1entry
7168 global commentend idtags linknum
7169 global mergemax numcommits pending_select
7170 global cmitmode showneartags allcommits
7171 global targetrow targetid lastscrollrows
7172 global autoselect autosellen jump_to_here
7173 global vinlinediff
7175 catch {unset pending_select}
7176 $canv delete hover
7177 normalline
7178 unsel_reflist
7179 stopfinding
7180 if {$l < 0 || $l >= $numcommits} return
7181 set id [commitonrow $l]
7182 set targetid $id
7183 set targetrow $l
7184 set selectedline $l
7185 set currentid $id
7186 if {$lastscrollrows < $numcommits} {
7187 setcanvscroll
7190 set y [expr {$canvy0 + $l * $linespc}]
7191 set ymax [lindex [$canv cget -scrollregion] 3]
7192 set ytop [expr {$y - $linespc - 1}]
7193 set ybot [expr {$y + $linespc + 1}]
7194 set wnow [$canv yview]
7195 set wtop [expr {[lindex $wnow 0] * $ymax}]
7196 set wbot [expr {[lindex $wnow 1] * $ymax}]
7197 set wh [expr {$wbot - $wtop}]
7198 set newtop $wtop
7199 if {$ytop < $wtop} {
7200 if {$ybot < $wtop} {
7201 set newtop [expr {$y - $wh / 2.0}]
7202 } else {
7203 set newtop $ytop
7204 if {$newtop > $wtop - $linespc} {
7205 set newtop [expr {$wtop - $linespc}]
7208 } elseif {$ybot > $wbot} {
7209 if {$ytop > $wbot} {
7210 set newtop [expr {$y - $wh / 2.0}]
7211 } else {
7212 set newtop [expr {$ybot - $wh}]
7213 if {$newtop < $wtop + $linespc} {
7214 set newtop [expr {$wtop + $linespc}]
7218 if {$newtop != $wtop} {
7219 if {$newtop < 0} {
7220 set newtop 0
7222 allcanvs yview moveto [expr {$newtop * 1.0 / $ymax}]
7223 drawvisible
7226 make_secsel $id
7228 if {$isnew} {
7229 addtohistory [list selbyid $id 0] savecmitpos
7232 $sha1entry delete 0 end
7233 $sha1entry insert 0 $id
7234 if {$autoselect} {
7235 $sha1entry selection range 0 $autosellen
7237 rhighlight_sel $id
7239 $ctext conf -state normal
7240 clear_ctext
7241 set linknum 0
7242 if {![info exists commitinfo($id)]} {
7243 getcommit $id
7245 set info $commitinfo($id)
7246 set date [formatdate [lindex $info 2]]
7247 $ctext insert end "[mc "Author"]: [lindex $info 1] $date\n"
7248 set date [formatdate [lindex $info 4]]
7249 $ctext insert end "[mc "Committer"]: [lindex $info 3] $date\n"
7250 if {[info exists idtags($id)]} {
7251 $ctext insert end [mc "Tags:"]
7252 foreach tag $idtags($id) {
7253 $ctext insert end " $tag"
7255 $ctext insert end "\n"
7258 set headers {}
7259 set olds $parents($curview,$id)
7260 if {[llength $olds] > 1} {
7261 set np 0
7262 foreach p $olds {
7263 if {$np >= $mergemax} {
7264 set tag mmax
7265 } else {
7266 set tag m$np
7268 $ctext insert end "[mc "Parent"]: " $tag
7269 appendwithlinks [commit_descriptor $p] {}
7270 incr np
7272 } else {
7273 foreach p $olds {
7274 append headers "[mc "Parent"]: [commit_descriptor $p]"
7278 foreach c $children($curview,$id) {
7279 append headers "[mc "Child"]: [commit_descriptor $c]"
7282 # make anything that looks like a SHA1 ID be a clickable link
7283 appendwithlinks $headers {}
7284 if {$showneartags} {
7285 if {![info exists allcommits]} {
7286 getallcommits
7288 $ctext insert end "[mc "Branch"]: "
7289 $ctext mark set branch "end -1c"
7290 $ctext mark gravity branch left
7291 $ctext insert end "\n[mc "Follows"]: "
7292 $ctext mark set follows "end -1c"
7293 $ctext mark gravity follows left
7294 $ctext insert end "\n[mc "Precedes"]: "
7295 $ctext mark set precedes "end -1c"
7296 $ctext mark gravity precedes left
7297 $ctext insert end "\n"
7298 dispneartags 1
7300 $ctext insert end "\n"
7301 set comment [lindex $info 5]
7302 if {[string first "\r" $comment] >= 0} {
7303 set comment [string map {"\r" "\n "} $comment]
7305 appendwithlinks $comment {comment}
7307 $ctext tag remove found 1.0 end
7308 $ctext conf -state disabled
7309 set commentend [$ctext index "end - 1c"]
7311 set jump_to_here $desired_loc
7312 init_flist [mc "Comments"]
7313 if {$cmitmode eq "tree"} {
7314 gettree $id
7315 } elseif {$vinlinediff($curview) == 1} {
7316 showinlinediff $id
7317 } elseif {[llength $olds] <= 1} {
7318 startdiff $id
7319 } else {
7320 mergediff $id
7324 proc selfirstline {} {
7325 unmarkmatches
7326 selectline 0 1
7329 proc sellastline {} {
7330 global numcommits
7331 unmarkmatches
7332 set l [expr {$numcommits - 1}]
7333 selectline $l 1
7336 proc selnextline {dir} {
7337 global selectedline
7338 focus .
7339 if {$selectedline eq {}} return
7340 set l [expr {$selectedline + $dir}]
7341 unmarkmatches
7342 selectline $l 1
7345 proc selnextpage {dir} {
7346 global canv linespc selectedline numcommits
7348 set lpp [expr {([winfo height $canv] - 2) / $linespc}]
7349 if {$lpp < 1} {
7350 set lpp 1
7352 allcanvs yview scroll [expr {$dir * $lpp}] units
7353 drawvisible
7354 if {$selectedline eq {}} return
7355 set l [expr {$selectedline + $dir * $lpp}]
7356 if {$l < 0} {
7357 set l 0
7358 } elseif {$l >= $numcommits} {
7359 set l [expr $numcommits - 1]
7361 unmarkmatches
7362 selectline $l 1
7365 proc unselectline {} {
7366 global selectedline currentid
7368 set selectedline {}
7369 catch {unset currentid}
7370 allcanvs delete secsel
7371 rhighlight_none
7374 proc reselectline {} {
7375 global selectedline
7377 if {$selectedline ne {}} {
7378 selectline $selectedline 0
7382 proc addtohistory {cmd {saveproc {}}} {
7383 global history historyindex curview
7385 unset_posvars
7386 save_position
7387 set elt [list $curview $cmd $saveproc {}]
7388 if {$historyindex > 0
7389 && [lindex $history [expr {$historyindex - 1}]] == $elt} {
7390 return
7393 if {$historyindex < [llength $history]} {
7394 set history [lreplace $history $historyindex end $elt]
7395 } else {
7396 lappend history $elt
7398 incr historyindex
7399 if {$historyindex > 1} {
7400 .tf.bar.leftbut conf -state normal
7401 } else {
7402 .tf.bar.leftbut conf -state disabled
7404 .tf.bar.rightbut conf -state disabled
7407 # save the scrolling position of the diff display pane
7408 proc save_position {} {
7409 global historyindex history
7411 if {$historyindex < 1} return
7412 set hi [expr {$historyindex - 1}]
7413 set fn [lindex $history $hi 2]
7414 if {$fn ne {}} {
7415 lset history $hi 3 [eval $fn]
7419 proc unset_posvars {} {
7420 global last_posvars
7422 if {[info exists last_posvars]} {
7423 foreach {var val} $last_posvars {
7424 global $var
7425 catch {unset $var}
7427 unset last_posvars
7431 proc godo {elt} {
7432 global curview last_posvars
7434 set view [lindex $elt 0]
7435 set cmd [lindex $elt 1]
7436 set pv [lindex $elt 3]
7437 if {$curview != $view} {
7438 showview $view
7440 unset_posvars
7441 foreach {var val} $pv {
7442 global $var
7443 set $var $val
7445 set last_posvars $pv
7446 eval $cmd
7449 proc goback {} {
7450 global history historyindex
7451 focus .
7453 if {$historyindex > 1} {
7454 save_position
7455 incr historyindex -1
7456 godo [lindex $history [expr {$historyindex - 1}]]
7457 .tf.bar.rightbut conf -state normal
7459 if {$historyindex <= 1} {
7460 .tf.bar.leftbut conf -state disabled
7464 proc goforw {} {
7465 global history historyindex
7466 focus .
7468 if {$historyindex < [llength $history]} {
7469 save_position
7470 set cmd [lindex $history $historyindex]
7471 incr historyindex
7472 godo $cmd
7473 .tf.bar.leftbut conf -state normal
7475 if {$historyindex >= [llength $history]} {
7476 .tf.bar.rightbut conf -state disabled
7480 proc gettree {id} {
7481 global treefilelist treeidlist diffids diffmergeid treepending
7482 global nullid nullid2
7484 set diffids $id
7485 catch {unset diffmergeid}
7486 if {![info exists treefilelist($id)]} {
7487 if {![info exists treepending]} {
7488 if {$id eq $nullid} {
7489 set cmd [list | git ls-files]
7490 } elseif {$id eq $nullid2} {
7491 set cmd [list | git ls-files --stage -t]
7492 } else {
7493 set cmd [list | git ls-tree -r $id]
7495 if {[catch {set gtf [open $cmd r]}]} {
7496 return
7498 set treepending $id
7499 set treefilelist($id) {}
7500 set treeidlist($id) {}
7501 fconfigure $gtf -blocking 0 -encoding binary
7502 filerun $gtf [list gettreeline $gtf $id]
7504 } else {
7505 setfilelist $id
7509 proc gettreeline {gtf id} {
7510 global treefilelist treeidlist treepending cmitmode diffids nullid nullid2
7512 set nl 0
7513 while {[incr nl] <= 1000 && [gets $gtf line] >= 0} {
7514 if {$diffids eq $nullid} {
7515 set fname $line
7516 } else {
7517 set i [string first "\t" $line]
7518 if {$i < 0} continue
7519 set fname [string range $line [expr {$i+1}] end]
7520 set line [string range $line 0 [expr {$i-1}]]
7521 if {$diffids ne $nullid2 && [lindex $line 1] ne "blob"} continue
7522 set sha1 [lindex $line 2]
7523 lappend treeidlist($id) $sha1
7525 if {[string index $fname 0] eq "\""} {
7526 set fname [lindex $fname 0]
7528 set fname [encoding convertfrom utf-8 $fname]
7529 lappend treefilelist($id) $fname
7531 if {![eof $gtf]} {
7532 return [expr {$nl >= 1000? 2: 1}]
7534 close $gtf
7535 unset treepending
7536 if {$cmitmode ne "tree"} {
7537 if {![info exists diffmergeid]} {
7538 gettreediffs $diffids
7540 } elseif {$id ne $diffids} {
7541 gettree $diffids
7542 } else {
7543 setfilelist $id
7545 return 0
7548 proc showfile {f} {
7549 global treefilelist treeidlist diffids nullid nullid2
7550 global ctext_file_names ctext_file_lines
7551 global ctext commentend
7553 set i [lsearch -exact $treefilelist($diffids) $f]
7554 if {$i < 0} {
7555 puts "oops, $f not in list for id $diffids"
7556 return
7558 if {$diffids eq $nullid} {
7559 if {[catch {set bf [open $f r]} err]} {
7560 puts "oops, can't read $f: $err"
7561 return
7563 } else {
7564 set blob [lindex $treeidlist($diffids) $i]
7565 if {[catch {set bf [open [concat | git cat-file blob $blob] r]} err]} {
7566 puts "oops, error reading blob $blob: $err"
7567 return
7570 fconfigure $bf -blocking 0 -encoding [get_path_encoding $f]
7571 filerun $bf [list getblobline $bf $diffids]
7572 $ctext config -state normal
7573 clear_ctext $commentend
7574 lappend ctext_file_names $f
7575 lappend ctext_file_lines [lindex [split $commentend "."] 0]
7576 $ctext insert end "\n"
7577 $ctext insert end "$f\n" filesep
7578 $ctext config -state disabled
7579 $ctext yview $commentend
7580 settabs 0
7583 proc getblobline {bf id} {
7584 global diffids cmitmode ctext
7586 if {$id ne $diffids || $cmitmode ne "tree"} {
7587 catch {close $bf}
7588 return 0
7590 $ctext config -state normal
7591 set nl 0
7592 while {[incr nl] <= 1000 && [gets $bf line] >= 0} {
7593 $ctext insert end "$line\n"
7595 if {[eof $bf]} {
7596 global jump_to_here ctext_file_names commentend
7598 # delete last newline
7599 $ctext delete "end - 2c" "end - 1c"
7600 close $bf
7601 if {$jump_to_here ne {} &&
7602 [lindex $jump_to_here 0] eq [lindex $ctext_file_names 0]} {
7603 set lnum [expr {[lindex $jump_to_here 1] +
7604 [lindex [split $commentend .] 0]}]
7605 mark_ctext_line $lnum
7607 $ctext config -state disabled
7608 return 0
7610 $ctext config -state disabled
7611 return [expr {$nl >= 1000? 2: 1}]
7614 proc mark_ctext_line {lnum} {
7615 global ctext markbgcolor
7617 $ctext tag delete omark
7618 $ctext tag add omark $lnum.0 "$lnum.0 + 1 line"
7619 $ctext tag conf omark -background $markbgcolor
7620 $ctext see $lnum.0
7623 proc mergediff {id} {
7624 global diffmergeid
7625 global diffids treediffs
7626 global parents curview
7628 set diffmergeid $id
7629 set diffids $id
7630 set treediffs($id) {}
7631 set np [llength $parents($curview,$id)]
7632 settabs $np
7633 getblobdiffs $id
7636 proc startdiff {ids} {
7637 global treediffs diffids treepending diffmergeid nullid nullid2
7639 settabs 1
7640 set diffids $ids
7641 catch {unset diffmergeid}
7642 if {![info exists treediffs($ids)] ||
7643 [lsearch -exact $ids $nullid] >= 0 ||
7644 [lsearch -exact $ids $nullid2] >= 0} {
7645 if {![info exists treepending]} {
7646 gettreediffs $ids
7648 } else {
7649 addtocflist $ids
7653 proc showinlinediff {ids} {
7654 global commitinfo commitdata ctext
7655 global treediffs
7657 set info $commitinfo($ids)
7658 set diff [lindex $info 7]
7659 set difflines [split $diff "\n"]
7661 initblobdiffvars
7662 set treediff {}
7664 set inhdr 0
7665 foreach line $difflines {
7666 if {![string compare -length 5 "diff " $line]} {
7667 set inhdr 1
7668 } elseif {$inhdr && ![string compare -length 4 "+++ " $line]} {
7669 # offset also accounts for the b/ prefix
7670 lappend treediff [string range $line 6 end]
7671 set inhdr 0
7675 set treediffs($ids) $treediff
7676 add_flist $treediff
7678 $ctext conf -state normal
7679 foreach line $difflines {
7680 parseblobdiffline $ids $line
7682 maybe_scroll_ctext 1
7683 $ctext conf -state disabled
7686 # If the filename (name) is under any of the passed filter paths
7687 # then return true to include the file in the listing.
7688 proc path_filter {filter name} {
7689 set worktree [gitworktree]
7690 foreach p $filter {
7691 set fq_p [file normalize $p]
7692 set fq_n [file normalize [file join $worktree $name]]
7693 if {[string match [file normalize $fq_p]* $fq_n]} {
7694 return 1
7697 return 0
7700 proc addtocflist {ids} {
7701 global treediffs
7703 add_flist $treediffs($ids)
7704 getblobdiffs $ids
7707 proc diffcmd {ids flags} {
7708 global log_showroot nullid nullid2
7710 set i [lsearch -exact $ids $nullid]
7711 set j [lsearch -exact $ids $nullid2]
7712 if {$i >= 0} {
7713 if {[llength $ids] > 1 && $j < 0} {
7714 # comparing working directory with some specific revision
7715 set cmd [concat | git diff-index $flags]
7716 if {$i == 0} {
7717 lappend cmd -R [lindex $ids 1]
7718 } else {
7719 lappend cmd [lindex $ids 0]
7721 } else {
7722 # comparing working directory with index
7723 set cmd [concat | git diff-files $flags]
7724 if {$j == 1} {
7725 lappend cmd -R
7728 } elseif {$j >= 0} {
7729 set cmd [concat | git diff-index --cached $flags]
7730 if {[llength $ids] > 1} {
7731 # comparing index with specific revision
7732 if {$j == 0} {
7733 lappend cmd -R [lindex $ids 1]
7734 } else {
7735 lappend cmd [lindex $ids 0]
7737 } else {
7738 # comparing index with HEAD
7739 lappend cmd HEAD
7741 } else {
7742 if {$log_showroot} {
7743 lappend flags --root
7745 set cmd [concat | git diff-tree -r $flags $ids]
7747 return $cmd
7750 proc gettreediffs {ids} {
7751 global treediff treepending limitdiffs vfilelimit curview
7753 set cmd [diffcmd $ids {--no-commit-id}]
7754 if {$limitdiffs && $vfilelimit($curview) ne {}} {
7755 set cmd [concat $cmd -- $vfilelimit($curview)]
7757 if {[catch {set gdtf [open $cmd r]}]} return
7759 set treepending $ids
7760 set treediff {}
7761 fconfigure $gdtf -blocking 0 -encoding binary
7762 filerun $gdtf [list gettreediffline $gdtf $ids]
7765 proc gettreediffline {gdtf ids} {
7766 global treediff treediffs treepending diffids diffmergeid
7767 global cmitmode vfilelimit curview limitdiffs perfile_attrs
7769 set nr 0
7770 set sublist {}
7771 set max 1000
7772 if {$perfile_attrs} {
7773 # cache_gitattr is slow, and even slower on win32 where we
7774 # have to invoke it for only about 30 paths at a time
7775 set max 500
7776 if {[tk windowingsystem] == "win32"} {
7777 set max 120
7780 while {[incr nr] <= $max && [gets $gdtf line] >= 0} {
7781 set i [string first "\t" $line]
7782 if {$i >= 0} {
7783 set file [string range $line [expr {$i+1}] end]
7784 if {[string index $file 0] eq "\""} {
7785 set file [lindex $file 0]
7787 set file [encoding convertfrom utf-8 $file]
7788 if {$file ne [lindex $treediff end]} {
7789 lappend treediff $file
7790 lappend sublist $file
7794 if {$perfile_attrs} {
7795 cache_gitattr encoding $sublist
7797 if {![eof $gdtf]} {
7798 return [expr {$nr >= $max? 2: 1}]
7800 close $gdtf
7801 set treediffs($ids) $treediff
7802 unset treepending
7803 if {$cmitmode eq "tree" && [llength $diffids] == 1} {
7804 gettree $diffids
7805 } elseif {$ids != $diffids} {
7806 if {![info exists diffmergeid]} {
7807 gettreediffs $diffids
7809 } else {
7810 addtocflist $ids
7812 return 0
7815 # empty string or positive integer
7816 proc diffcontextvalidate {v} {
7817 return [regexp {^(|[1-9][0-9]*)$} $v]
7820 proc diffcontextchange {n1 n2 op} {
7821 global diffcontextstring diffcontext
7823 if {[string is integer -strict $diffcontextstring]} {
7824 if {$diffcontextstring >= 0} {
7825 set diffcontext $diffcontextstring
7826 reselectline
7831 proc changeignorespace {} {
7832 reselectline
7835 proc changeworddiff {name ix op} {
7836 reselectline
7839 proc initblobdiffvars {} {
7840 global diffencoding targetline diffnparents
7841 global diffinhdr currdiffsubmod diffseehere
7842 set targetline {}
7843 set diffnparents 0
7844 set diffinhdr 0
7845 set diffencoding [get_path_encoding {}]
7846 set currdiffsubmod ""
7847 set diffseehere -1
7850 proc getblobdiffs {ids} {
7851 global blobdifffd diffids env
7852 global treediffs
7853 global diffcontext
7854 global ignorespace
7855 global worddiff
7856 global limitdiffs vfilelimit curview
7857 global git_version
7859 set textconv {}
7860 if {[package vcompare $git_version "1.6.1"] >= 0} {
7861 set textconv "--textconv"
7863 set submodule {}
7864 if {[package vcompare $git_version "1.6.6"] >= 0} {
7865 set submodule "--submodule"
7867 set cmd [diffcmd $ids "-p $textconv $submodule -C --cc --no-commit-id -U$diffcontext"]
7868 if {$ignorespace} {
7869 append cmd " -w"
7871 if {$worddiff ne [mc "Line diff"]} {
7872 append cmd " --word-diff=porcelain"
7874 if {$limitdiffs && $vfilelimit($curview) ne {}} {
7875 set cmd [concat $cmd -- $vfilelimit($curview)]
7877 if {[catch {set bdf [open $cmd r]} err]} {
7878 error_popup [mc "Error getting diffs: %s" $err]
7879 return
7881 fconfigure $bdf -blocking 0 -encoding binary -eofchar {}
7882 set blobdifffd($ids) $bdf
7883 initblobdiffvars
7884 filerun $bdf [list getblobdiffline $bdf $diffids]
7887 proc savecmitpos {} {
7888 global ctext cmitmode
7890 if {$cmitmode eq "tree"} {
7891 return {}
7893 return [list target_scrollpos [$ctext index @0,0]]
7896 proc savectextpos {} {
7897 global ctext
7899 return [list target_scrollpos [$ctext index @0,0]]
7902 proc maybe_scroll_ctext {ateof} {
7903 global ctext target_scrollpos
7905 if {![info exists target_scrollpos]} return
7906 if {!$ateof} {
7907 set nlines [expr {[winfo height $ctext]
7908 / [font metrics textfont -linespace]}]
7909 if {[$ctext compare "$target_scrollpos + $nlines lines" <= end]} return
7911 $ctext yview $target_scrollpos
7912 unset target_scrollpos
7915 proc setinlist {var i val} {
7916 global $var
7918 while {[llength [set $var]] < $i} {
7919 lappend $var {}
7921 if {[llength [set $var]] == $i} {
7922 lappend $var $val
7923 } else {
7924 lset $var $i $val
7928 proc makediffhdr {fname ids} {
7929 global ctext curdiffstart treediffs diffencoding
7930 global ctext_file_names jump_to_here targetline diffline
7932 set fname [encoding convertfrom utf-8 $fname]
7933 set diffencoding [get_path_encoding $fname]
7934 set i [lsearch -exact $treediffs($ids) $fname]
7935 if {$i >= 0} {
7936 setinlist difffilestart $i $curdiffstart
7938 lset ctext_file_names end $fname
7939 set l [expr {(78 - [string length $fname]) / 2}]
7940 set pad [string range "----------------------------------------" 1 $l]
7941 $ctext insert $curdiffstart "$pad $fname $pad" filesep
7942 set targetline {}
7943 if {$jump_to_here ne {} && [lindex $jump_to_here 0] eq $fname} {
7944 set targetline [lindex $jump_to_here 1]
7946 set diffline 0
7949 proc blobdiffmaybeseehere {ateof} {
7950 global diffseehere
7951 if {$diffseehere >= 0} {
7952 mark_ctext_line [lindex [split $diffseehere .] 0]
7954 maybe_scroll_ctext $ateof
7957 proc getblobdiffline {bdf ids} {
7958 global diffids blobdifffd
7959 global ctext
7961 set nr 0
7962 $ctext conf -state normal
7963 while {[incr nr] <= 1000 && [gets $bdf line] >= 0} {
7964 if {$ids != $diffids || $bdf != $blobdifffd($ids)} {
7965 catch {close $bdf}
7966 return 0
7968 parseblobdiffline $ids $line
7970 $ctext conf -state disabled
7971 blobdiffmaybeseehere [eof $bdf]
7972 if {[eof $bdf]} {
7973 catch {close $bdf}
7974 return 0
7976 return [expr {$nr >= 1000? 2: 1}]
7979 proc parseblobdiffline {ids line} {
7980 global ctext curdiffstart
7981 global diffnexthead diffnextnote difffilestart
7982 global ctext_file_names ctext_file_lines
7983 global diffinhdr treediffs mergemax diffnparents
7984 global diffencoding jump_to_here targetline diffline currdiffsubmod
7985 global worddiff diffseehere
7987 if {![string compare -length 5 "diff " $line]} {
7988 if {![regexp {^diff (--cc|--git) } $line m type]} {
7989 set line [encoding convertfrom utf-8 $line]
7990 $ctext insert end "$line\n" hunksep
7991 continue
7993 # start of a new file
7994 set diffinhdr 1
7995 $ctext insert end "\n"
7996 set curdiffstart [$ctext index "end - 1c"]
7997 lappend ctext_file_names ""
7998 lappend ctext_file_lines [lindex [split $curdiffstart "."] 0]
7999 $ctext insert end "\n" filesep
8001 if {$type eq "--cc"} {
8002 # start of a new file in a merge diff
8003 set fname [string range $line 10 end]
8004 if {[lsearch -exact $treediffs($ids) $fname] < 0} {
8005 lappend treediffs($ids) $fname
8006 add_flist [list $fname]
8009 } else {
8010 set line [string range $line 11 end]
8011 # If the name hasn't changed the length will be odd,
8012 # the middle char will be a space, and the two bits either
8013 # side will be a/name and b/name, or "a/name" and "b/name".
8014 # If the name has changed we'll get "rename from" and
8015 # "rename to" or "copy from" and "copy to" lines following
8016 # this, and we'll use them to get the filenames.
8017 # This complexity is necessary because spaces in the
8018 # filename(s) don't get escaped.
8019 set l [string length $line]
8020 set i [expr {$l / 2}]
8021 if {!(($l & 1) && [string index $line $i] eq " " &&
8022 [string range $line 2 [expr {$i - 1}]] eq \
8023 [string range $line [expr {$i + 3}] end])} {
8024 return
8026 # unescape if quoted and chop off the a/ from the front
8027 if {[string index $line 0] eq "\""} {
8028 set fname [string range [lindex $line 0] 2 end]
8029 } else {
8030 set fname [string range $line 2 [expr {$i - 1}]]
8033 makediffhdr $fname $ids
8035 } elseif {![string compare -length 16 "* Unmerged path " $line]} {
8036 set fname [encoding convertfrom utf-8 [string range $line 16 end]]
8037 $ctext insert end "\n"
8038 set curdiffstart [$ctext index "end - 1c"]
8039 lappend ctext_file_names $fname
8040 lappend ctext_file_lines [lindex [split $curdiffstart "."] 0]
8041 $ctext insert end "$line\n" filesep
8042 set i [lsearch -exact $treediffs($ids) $fname]
8043 if {$i >= 0} {
8044 setinlist difffilestart $i $curdiffstart
8047 } elseif {![string compare -length 2 "@@" $line]} {
8048 regexp {^@@+} $line ats
8049 set line [encoding convertfrom $diffencoding $line]
8050 $ctext insert end "$line\n" hunksep
8051 if {[regexp { \+(\d+),\d+ @@} $line m nl]} {
8052 set diffline $nl
8054 set diffnparents [expr {[string length $ats] - 1}]
8055 set diffinhdr 0
8057 } elseif {![string compare -length 10 "Submodule " $line]} {
8058 # start of a new submodule
8059 if {[regexp -indices "\[0-9a-f\]+\\.\\." $line nameend]} {
8060 set fname [string range $line 10 [expr [lindex $nameend 0] - 2]]
8061 } else {
8062 set fname [string range $line 10 [expr [string first "contains " $line] - 2]]
8064 if {$currdiffsubmod != $fname} {
8065 $ctext insert end "\n"; # Add newline after commit message
8067 set curdiffstart [$ctext index "end - 1c"]
8068 lappend ctext_file_names ""
8069 if {$currdiffsubmod != $fname} {
8070 lappend ctext_file_lines $fname
8071 makediffhdr $fname $ids
8072 set currdiffsubmod $fname
8073 $ctext insert end "\n$line\n" filesep
8074 } else {
8075 $ctext insert end "$line\n" filesep
8077 } elseif {![string compare -length 3 " >" $line]} {
8078 set $currdiffsubmod ""
8079 set line [encoding convertfrom $diffencoding $line]
8080 $ctext insert end "$line\n" dresult
8081 } elseif {![string compare -length 3 " <" $line]} {
8082 set $currdiffsubmod ""
8083 set line [encoding convertfrom $diffencoding $line]
8084 $ctext insert end "$line\n" d0
8085 } elseif {$diffinhdr} {
8086 if {![string compare -length 12 "rename from " $line]} {
8087 set fname [string range $line [expr 6 + [string first " from " $line] ] end]
8088 if {[string index $fname 0] eq "\""} {
8089 set fname [lindex $fname 0]
8091 set fname [encoding convertfrom utf-8 $fname]
8092 set i [lsearch -exact $treediffs($ids) $fname]
8093 if {$i >= 0} {
8094 setinlist difffilestart $i $curdiffstart
8096 } elseif {![string compare -length 10 $line "rename to "] ||
8097 ![string compare -length 8 $line "copy to "]} {
8098 set fname [string range $line [expr 4 + [string first " to " $line] ] end]
8099 if {[string index $fname 0] eq "\""} {
8100 set fname [lindex $fname 0]
8102 makediffhdr $fname $ids
8103 } elseif {[string compare -length 3 $line "---"] == 0} {
8104 # do nothing
8105 return
8106 } elseif {[string compare -length 3 $line "+++"] == 0} {
8107 set diffinhdr 0
8108 return
8110 set line [encoding convertfrom utf-8 $line]
8111 $ctext insert end "$line\n" filesep
8113 } else {
8114 set line [string map {\x1A ^Z} \
8115 [encoding convertfrom $diffencoding $line]]
8116 # parse the prefix - one ' ', '-' or '+' for each parent
8117 set prefix [string range $line 0 [expr {$diffnparents - 1}]]
8118 set tag [expr {$diffnparents > 1? "m": "d"}]
8119 set dowords [expr {$worddiff ne [mc "Line diff"] && $diffnparents == 1}]
8120 set words_pre_markup ""
8121 set words_post_markup ""
8122 if {[string trim $prefix " -+"] eq {}} {
8123 # prefix only has " ", "-" and "+" in it: normal diff line
8124 set num [string first "-" $prefix]
8125 if {$dowords} {
8126 set line [string range $line 1 end]
8128 if {$num >= 0} {
8129 # removed line, first parent with line is $num
8130 if {$num >= $mergemax} {
8131 set num "max"
8133 if {$dowords && $worddiff eq [mc "Markup words"]} {
8134 $ctext insert end "\[-$line-\]" $tag$num
8135 } else {
8136 $ctext insert end "$line" $tag$num
8138 if {!$dowords} {
8139 $ctext insert end "\n" $tag$num
8141 } else {
8142 set tags {}
8143 if {[string first "+" $prefix] >= 0} {
8144 # added line
8145 lappend tags ${tag}result
8146 if {$diffnparents > 1} {
8147 set num [string first " " $prefix]
8148 if {$num >= 0} {
8149 if {$num >= $mergemax} {
8150 set num "max"
8152 lappend tags m$num
8155 set words_pre_markup "{+"
8156 set words_post_markup "+}"
8158 if {$targetline ne {}} {
8159 if {$diffline == $targetline} {
8160 set diffseehere [$ctext index "end - 1 chars"]
8161 set targetline {}
8162 } else {
8163 incr diffline
8166 if {$dowords && $worddiff eq [mc "Markup words"]} {
8167 $ctext insert end "$words_pre_markup$line$words_post_markup" $tags
8168 } else {
8169 $ctext insert end "$line" $tags
8171 if {!$dowords} {
8172 $ctext insert end "\n" $tags
8175 } elseif {$dowords && $prefix eq "~"} {
8176 $ctext insert end "\n" {}
8177 } else {
8178 # "\ No newline at end of file",
8179 # or something else we don't recognize
8180 $ctext insert end "$line\n" hunksep
8185 proc changediffdisp {} {
8186 global ctext diffelide
8188 $ctext tag conf d0 -elide [lindex $diffelide 0]
8189 $ctext tag conf dresult -elide [lindex $diffelide 1]
8192 proc highlightfile {cline} {
8193 global cflist cflist_top
8195 if {![info exists cflist_top]} return
8197 $cflist tag remove highlight $cflist_top.0 "$cflist_top.0 lineend"
8198 $cflist tag add highlight $cline.0 "$cline.0 lineend"
8199 $cflist see $cline.0
8200 set cflist_top $cline
8203 proc highlightfile_for_scrollpos {topidx} {
8204 global cmitmode difffilestart
8206 if {$cmitmode eq "tree"} return
8207 if {![info exists difffilestart]} return
8209 set top [lindex [split $topidx .] 0]
8210 if {$difffilestart eq {} || $top < [lindex $difffilestart 0]} {
8211 highlightfile 0
8212 } else {
8213 highlightfile [expr {[bsearch $difffilestart $top] + 2}]
8217 proc prevfile {} {
8218 global difffilestart ctext cmitmode
8220 if {$cmitmode eq "tree"} return
8221 set prev 0.0
8222 set here [$ctext index @0,0]
8223 foreach loc $difffilestart {
8224 if {[$ctext compare $loc >= $here]} {
8225 $ctext yview $prev
8226 return
8228 set prev $loc
8230 $ctext yview $prev
8233 proc nextfile {} {
8234 global difffilestart ctext cmitmode
8236 if {$cmitmode eq "tree"} return
8237 set here [$ctext index @0,0]
8238 foreach loc $difffilestart {
8239 if {[$ctext compare $loc > $here]} {
8240 $ctext yview $loc
8241 return
8246 proc clear_ctext {{first 1.0}} {
8247 global ctext smarktop smarkbot
8248 global ctext_file_names ctext_file_lines
8249 global pendinglinks
8251 set l [lindex [split $first .] 0]
8252 if {![info exists smarktop] || [$ctext compare $first < $smarktop.0]} {
8253 set smarktop $l
8255 if {![info exists smarkbot] || [$ctext compare $first < $smarkbot.0]} {
8256 set smarkbot $l
8258 $ctext delete $first end
8259 if {$first eq "1.0"} {
8260 catch {unset pendinglinks}
8262 set ctext_file_names {}
8263 set ctext_file_lines {}
8266 proc settabs {{firstab {}}} {
8267 global firsttabstop tabstop ctext have_tk85
8269 if {$firstab ne {} && $have_tk85} {
8270 set firsttabstop $firstab
8272 set w [font measure textfont "0"]
8273 if {$firsttabstop != 0} {
8274 $ctext conf -tabs [list [expr {($firsttabstop + $tabstop) * $w}] \
8275 [expr {($firsttabstop + 2 * $tabstop) * $w}]]
8276 } elseif {$have_tk85 || $tabstop != 8} {
8277 $ctext conf -tabs [expr {$tabstop * $w}]
8278 } else {
8279 $ctext conf -tabs {}
8283 proc incrsearch {name ix op} {
8284 global ctext searchstring searchdirn
8286 if {[catch {$ctext index anchor}]} {
8287 # no anchor set, use start of selection, or of visible area
8288 set sel [$ctext tag ranges sel]
8289 if {$sel ne {}} {
8290 $ctext mark set anchor [lindex $sel 0]
8291 } elseif {$searchdirn eq "-forwards"} {
8292 $ctext mark set anchor @0,0
8293 } else {
8294 $ctext mark set anchor @0,[winfo height $ctext]
8297 if {$searchstring ne {}} {
8298 set here [$ctext search -count mlen $searchdirn -- $searchstring anchor]
8299 if {$here ne {}} {
8300 $ctext see $here
8301 set mend "$here + $mlen c"
8302 $ctext tag remove sel 1.0 end
8303 $ctext tag add sel $here $mend
8304 suppress_highlighting_file_for_current_scrollpos
8305 highlightfile_for_scrollpos $here
8308 rehighlight_search_results
8311 proc dosearch {} {
8312 global sstring ctext searchstring searchdirn
8314 focus $sstring
8315 $sstring icursor end
8316 set searchdirn -forwards
8317 if {$searchstring ne {}} {
8318 set sel [$ctext tag ranges sel]
8319 if {$sel ne {}} {
8320 set start "[lindex $sel 0] + 1c"
8321 } elseif {[catch {set start [$ctext index anchor]}]} {
8322 set start "@0,0"
8324 set match [$ctext search -count mlen -- $searchstring $start]
8325 $ctext tag remove sel 1.0 end
8326 if {$match eq {}} {
8327 bell
8328 return
8330 $ctext see $match
8331 suppress_highlighting_file_for_current_scrollpos
8332 highlightfile_for_scrollpos $match
8333 set mend "$match + $mlen c"
8334 $ctext tag add sel $match $mend
8335 $ctext mark unset anchor
8336 rehighlight_search_results
8340 proc dosearchback {} {
8341 global sstring ctext searchstring searchdirn
8343 focus $sstring
8344 $sstring icursor end
8345 set searchdirn -backwards
8346 if {$searchstring ne {}} {
8347 set sel [$ctext tag ranges sel]
8348 if {$sel ne {}} {
8349 set start [lindex $sel 0]
8350 } elseif {[catch {set start [$ctext index anchor]}]} {
8351 set start @0,[winfo height $ctext]
8353 set match [$ctext search -backwards -count ml -- $searchstring $start]
8354 $ctext tag remove sel 1.0 end
8355 if {$match eq {}} {
8356 bell
8357 return
8359 $ctext see $match
8360 suppress_highlighting_file_for_current_scrollpos
8361 highlightfile_for_scrollpos $match
8362 set mend "$match + $ml c"
8363 $ctext tag add sel $match $mend
8364 $ctext mark unset anchor
8365 rehighlight_search_results
8369 proc rehighlight_search_results {} {
8370 global ctext searchstring
8372 $ctext tag remove found 1.0 end
8373 $ctext tag remove currentsearchhit 1.0 end
8375 if {$searchstring ne {}} {
8376 searchmarkvisible 1
8380 proc searchmark {first last} {
8381 global ctext searchstring
8383 set sel [$ctext tag ranges sel]
8385 set mend $first.0
8386 while {1} {
8387 set match [$ctext search -count mlen -- $searchstring $mend $last.end]
8388 if {$match eq {}} break
8389 set mend "$match + $mlen c"
8390 if {$sel ne {} && [$ctext compare $match == [lindex $sel 0]]} {
8391 $ctext tag add currentsearchhit $match $mend
8392 } else {
8393 $ctext tag add found $match $mend
8398 proc searchmarkvisible {doall} {
8399 global ctext smarktop smarkbot
8401 set topline [lindex [split [$ctext index @0,0] .] 0]
8402 set botline [lindex [split [$ctext index @0,[winfo height $ctext]] .] 0]
8403 if {$doall || $botline < $smarktop || $topline > $smarkbot} {
8404 # no overlap with previous
8405 searchmark $topline $botline
8406 set smarktop $topline
8407 set smarkbot $botline
8408 } else {
8409 if {$topline < $smarktop} {
8410 searchmark $topline [expr {$smarktop-1}]
8411 set smarktop $topline
8413 if {$botline > $smarkbot} {
8414 searchmark [expr {$smarkbot+1}] $botline
8415 set smarkbot $botline
8420 proc suppress_highlighting_file_for_current_scrollpos {} {
8421 global ctext suppress_highlighting_file_for_this_scrollpos
8423 set suppress_highlighting_file_for_this_scrollpos [$ctext index @0,0]
8426 proc scrolltext {f0 f1} {
8427 global searchstring cmitmode ctext
8428 global suppress_highlighting_file_for_this_scrollpos
8430 set topidx [$ctext index @0,0]
8431 if {![info exists suppress_highlighting_file_for_this_scrollpos]
8432 || $topidx ne $suppress_highlighting_file_for_this_scrollpos} {
8433 highlightfile_for_scrollpos $topidx
8436 catch {unset suppress_highlighting_file_for_this_scrollpos}
8438 .bleft.bottom.sb set $f0 $f1
8439 if {$searchstring ne {}} {
8440 searchmarkvisible 0
8444 proc setcoords {} {
8445 global linespc charspc canvx0 canvy0
8446 global xspc1 xspc2 lthickness
8448 set linespc [font metrics mainfont -linespace]
8449 set charspc [font measure mainfont "m"]
8450 set canvy0 [expr {int(3 + 0.5 * $linespc)}]
8451 set canvx0 [expr {int(3 + 0.5 * $linespc)}]
8452 set lthickness [expr {int($linespc / 9) + 1}]
8453 set xspc1(0) $linespc
8454 set xspc2 $linespc
8457 proc redisplay {} {
8458 global canv
8459 global selectedline
8461 set ymax [lindex [$canv cget -scrollregion] 3]
8462 if {$ymax eq {} || $ymax == 0} return
8463 set span [$canv yview]
8464 clear_display
8465 setcanvscroll
8466 allcanvs yview moveto [lindex $span 0]
8467 drawvisible
8468 if {$selectedline ne {}} {
8469 selectline $selectedline 0
8470 allcanvs yview moveto [lindex $span 0]
8474 proc parsefont {f n} {
8475 global fontattr
8477 set fontattr($f,family) [lindex $n 0]
8478 set s [lindex $n 1]
8479 if {$s eq {} || $s == 0} {
8480 set s 10
8481 } elseif {$s < 0} {
8482 set s [expr {int(-$s / [winfo fpixels . 1p] + 0.5)}]
8484 set fontattr($f,size) $s
8485 set fontattr($f,weight) normal
8486 set fontattr($f,slant) roman
8487 foreach style [lrange $n 2 end] {
8488 switch -- $style {
8489 "normal" -
8490 "bold" {set fontattr($f,weight) $style}
8491 "roman" -
8492 "italic" {set fontattr($f,slant) $style}
8497 proc fontflags {f {isbold 0}} {
8498 global fontattr
8500 return [list -family $fontattr($f,family) -size $fontattr($f,size) \
8501 -weight [expr {$isbold? "bold": $fontattr($f,weight)}] \
8502 -slant $fontattr($f,slant)]
8505 proc fontname {f} {
8506 global fontattr
8508 set n [list $fontattr($f,family) $fontattr($f,size)]
8509 if {$fontattr($f,weight) eq "bold"} {
8510 lappend n "bold"
8512 if {$fontattr($f,slant) eq "italic"} {
8513 lappend n "italic"
8515 return $n
8518 proc incrfont {inc} {
8519 global mainfont textfont ctext canv cflist showrefstop
8520 global stopped entries fontattr
8522 unmarkmatches
8523 set s $fontattr(mainfont,size)
8524 incr s $inc
8525 if {$s < 1} {
8526 set s 1
8528 set fontattr(mainfont,size) $s
8529 font config mainfont -size $s
8530 font config mainfontbold -size $s
8531 set mainfont [fontname mainfont]
8532 set s $fontattr(textfont,size)
8533 incr s $inc
8534 if {$s < 1} {
8535 set s 1
8537 set fontattr(textfont,size) $s
8538 font config textfont -size $s
8539 font config textfontbold -size $s
8540 set textfont [fontname textfont]
8541 setcoords
8542 settabs
8543 redisplay
8546 proc clearsha1 {} {
8547 global sha1entry sha1string
8548 if {[string length $sha1string] == 40} {
8549 $sha1entry delete 0 end
8553 proc sha1change {n1 n2 op} {
8554 global sha1string currentid sha1but
8555 if {$sha1string == {}
8556 || ([info exists currentid] && $sha1string == $currentid)} {
8557 set state disabled
8558 } else {
8559 set state normal
8561 if {[$sha1but cget -state] == $state} return
8562 if {$state == "normal"} {
8563 $sha1but conf -state normal -relief raised -text "[mc "Goto:"] "
8564 } else {
8565 $sha1but conf -state disabled -relief flat -text "[mc "SHA1 ID:"] "
8569 proc gotocommit {} {
8570 global sha1string tagids headids curview varcid
8572 if {$sha1string == {}
8573 || ([info exists currentid] && $sha1string == $currentid)} return
8574 if {[info exists tagids($sha1string)]} {
8575 set id $tagids($sha1string)
8576 } elseif {[info exists headids($sha1string)]} {
8577 set id $headids($sha1string)
8578 } else {
8579 set id [string tolower $sha1string]
8580 if {[regexp {^[0-9a-f]{4,39}$} $id]} {
8581 set matches [longid $id]
8582 if {$matches ne {}} {
8583 if {[llength $matches] > 1} {
8584 error_popup [mc "Short SHA1 id %s is ambiguous" $id]
8585 return
8587 set id [lindex $matches 0]
8589 } else {
8590 if {[catch {set id [exec git rev-parse --verify $sha1string]}]} {
8591 error_popup [mc "Revision %s is not known" $sha1string]
8592 return
8596 if {[commitinview $id $curview]} {
8597 selectline [rowofcommit $id] 1
8598 return
8600 if {[regexp {^[0-9a-fA-F]{4,}$} $sha1string]} {
8601 set msg [mc "SHA1 id %s is not known" $sha1string]
8602 } else {
8603 set msg [mc "Revision %s is not in the current view" $sha1string]
8605 error_popup $msg
8608 proc lineenter {x y id} {
8609 global hoverx hovery hoverid hovertimer
8610 global commitinfo canv
8612 if {![info exists commitinfo($id)] && ![getcommit $id]} return
8613 set hoverx $x
8614 set hovery $y
8615 set hoverid $id
8616 if {[info exists hovertimer]} {
8617 after cancel $hovertimer
8619 set hovertimer [after 500 linehover]
8620 $canv delete hover
8623 proc linemotion {x y id} {
8624 global hoverx hovery hoverid hovertimer
8626 if {[info exists hoverid] && $id == $hoverid} {
8627 set hoverx $x
8628 set hovery $y
8629 if {[info exists hovertimer]} {
8630 after cancel $hovertimer
8632 set hovertimer [after 500 linehover]
8636 proc lineleave {id} {
8637 global hoverid hovertimer canv
8639 if {[info exists hoverid] && $id == $hoverid} {
8640 $canv delete hover
8641 if {[info exists hovertimer]} {
8642 after cancel $hovertimer
8643 unset hovertimer
8645 unset hoverid
8649 proc linehover {} {
8650 global hoverx hovery hoverid hovertimer
8651 global canv linespc lthickness
8652 global linehoverbgcolor linehoverfgcolor linehoveroutlinecolor
8654 global commitinfo
8656 set text [lindex $commitinfo($hoverid) 0]
8657 set ymax [lindex [$canv cget -scrollregion] 3]
8658 if {$ymax == {}} return
8659 set yfrac [lindex [$canv yview] 0]
8660 set x [expr {$hoverx + 2 * $linespc}]
8661 set y [expr {$hovery + $yfrac * $ymax - $linespc / 2}]
8662 set x0 [expr {$x - 2 * $lthickness}]
8663 set y0 [expr {$y - 2 * $lthickness}]
8664 set x1 [expr {$x + [font measure mainfont $text] + 2 * $lthickness}]
8665 set y1 [expr {$y + $linespc + 2 * $lthickness}]
8666 set t [$canv create rectangle $x0 $y0 $x1 $y1 \
8667 -fill $linehoverbgcolor -outline $linehoveroutlinecolor \
8668 -width 1 -tags hover]
8669 $canv raise $t
8670 set t [$canv create text $x $y -anchor nw -text $text -tags hover \
8671 -font mainfont -fill $linehoverfgcolor]
8672 $canv raise $t
8675 proc clickisonarrow {id y} {
8676 global lthickness
8678 set ranges [rowranges $id]
8679 set thresh [expr {2 * $lthickness + 6}]
8680 set n [expr {[llength $ranges] - 1}]
8681 for {set i 1} {$i < $n} {incr i} {
8682 set row [lindex $ranges $i]
8683 if {abs([yc $row] - $y) < $thresh} {
8684 return $i
8687 return {}
8690 proc arrowjump {id n y} {
8691 global canv
8693 # 1 <-> 2, 3 <-> 4, etc...
8694 set n [expr {(($n - 1) ^ 1) + 1}]
8695 set row [lindex [rowranges $id] $n]
8696 set yt [yc $row]
8697 set ymax [lindex [$canv cget -scrollregion] 3]
8698 if {$ymax eq {} || $ymax <= 0} return
8699 set view [$canv yview]
8700 set yspan [expr {[lindex $view 1] - [lindex $view 0]}]
8701 set yfrac [expr {$yt / $ymax - $yspan / 2}]
8702 if {$yfrac < 0} {
8703 set yfrac 0
8705 allcanvs yview moveto $yfrac
8708 proc lineclick {x y id isnew} {
8709 global ctext commitinfo children canv thickerline curview
8711 if {![info exists commitinfo($id)] && ![getcommit $id]} return
8712 unmarkmatches
8713 unselectline
8714 normalline
8715 $canv delete hover
8716 # draw this line thicker than normal
8717 set thickerline $id
8718 drawlines $id
8719 if {$isnew} {
8720 set ymax [lindex [$canv cget -scrollregion] 3]
8721 if {$ymax eq {}} return
8722 set yfrac [lindex [$canv yview] 0]
8723 set y [expr {$y + $yfrac * $ymax}]
8725 set dirn [clickisonarrow $id $y]
8726 if {$dirn ne {}} {
8727 arrowjump $id $dirn $y
8728 return
8731 if {$isnew} {
8732 addtohistory [list lineclick $x $y $id 0] savectextpos
8734 # fill the details pane with info about this line
8735 $ctext conf -state normal
8736 clear_ctext
8737 settabs 0
8738 $ctext insert end "[mc "Parent"]:\t"
8739 $ctext insert end $id link0
8740 setlink $id link0
8741 set info $commitinfo($id)
8742 $ctext insert end "\n\t[lindex $info 0]\n"
8743 $ctext insert end "\t[mc "Author"]:\t[lindex $info 1]\n"
8744 set date [formatdate [lindex $info 2]]
8745 $ctext insert end "\t[mc "Date"]:\t$date\n"
8746 set kids $children($curview,$id)
8747 if {$kids ne {}} {
8748 $ctext insert end "\n[mc "Children"]:"
8749 set i 0
8750 foreach child $kids {
8751 incr i
8752 if {![info exists commitinfo($child)] && ![getcommit $child]} continue
8753 set info $commitinfo($child)
8754 $ctext insert end "\n\t"
8755 $ctext insert end $child link$i
8756 setlink $child link$i
8757 $ctext insert end "\n\t[lindex $info 0]"
8758 $ctext insert end "\n\t[mc "Author"]:\t[lindex $info 1]"
8759 set date [formatdate [lindex $info 2]]
8760 $ctext insert end "\n\t[mc "Date"]:\t$date\n"
8763 maybe_scroll_ctext 1
8764 $ctext conf -state disabled
8765 init_flist {}
8768 proc normalline {} {
8769 global thickerline
8770 if {[info exists thickerline]} {
8771 set id $thickerline
8772 unset thickerline
8773 drawlines $id
8777 proc selbyid {id {isnew 1}} {
8778 global curview
8779 if {[commitinview $id $curview]} {
8780 selectline [rowofcommit $id] $isnew
8784 proc mstime {} {
8785 global startmstime
8786 if {![info exists startmstime]} {
8787 set startmstime [clock clicks -milliseconds]
8789 return [format "%.3f" [expr {([clock click -milliseconds] - $startmstime) / 1000.0}]]
8792 proc rowmenu {x y id} {
8793 global rowctxmenu selectedline rowmenuid curview
8794 global nullid nullid2 fakerowmenu mainhead markedid
8796 stopfinding
8797 set rowmenuid $id
8798 if {$selectedline eq {} || [rowofcommit $id] eq $selectedline} {
8799 set state disabled
8800 } else {
8801 set state normal
8803 if {[info exists markedid] && $markedid ne $id} {
8804 set mstate normal
8805 } else {
8806 set mstate disabled
8808 if {$id ne $nullid && $id ne $nullid2} {
8809 set menu $rowctxmenu
8810 if {$mainhead ne {}} {
8811 $menu entryconfigure 7 -label [mc "Reset %s branch to here" $mainhead] -state normal
8812 } else {
8813 $menu entryconfigure 7 -label [mc "Detached head: can't reset" $mainhead] -state disabled
8815 $menu entryconfigure 9 -state $mstate
8816 $menu entryconfigure 10 -state $mstate
8817 $menu entryconfigure 11 -state $mstate
8818 } else {
8819 set menu $fakerowmenu
8821 $menu entryconfigure [mca "Diff this -> selected"] -state $state
8822 $menu entryconfigure [mca "Diff selected -> this"] -state $state
8823 $menu entryconfigure [mca "Make patch"] -state $state
8824 $menu entryconfigure [mca "Diff this -> marked commit"] -state $mstate
8825 $menu entryconfigure [mca "Diff marked commit -> this"] -state $mstate
8826 tk_popup $menu $x $y
8829 proc markhere {} {
8830 global rowmenuid markedid canv
8832 set markedid $rowmenuid
8833 make_idmark $markedid
8836 proc gotomark {} {
8837 global markedid
8839 if {[info exists markedid]} {
8840 selbyid $markedid
8844 proc replace_by_kids {l r} {
8845 global curview children
8847 set id [commitonrow $r]
8848 set l [lreplace $l 0 0]
8849 foreach kid $children($curview,$id) {
8850 lappend l [rowofcommit $kid]
8852 return [lsort -integer -decreasing -unique $l]
8855 proc find_common_desc {} {
8856 global markedid rowmenuid curview children
8858 if {![info exists markedid]} return
8859 if {![commitinview $markedid $curview] ||
8860 ![commitinview $rowmenuid $curview]} return
8861 #set t1 [clock clicks -milliseconds]
8862 set l1 [list [rowofcommit $markedid]]
8863 set l2 [list [rowofcommit $rowmenuid]]
8864 while 1 {
8865 set r1 [lindex $l1 0]
8866 set r2 [lindex $l2 0]
8867 if {$r1 eq {} || $r2 eq {}} break
8868 if {$r1 == $r2} {
8869 selectline $r1 1
8870 break
8872 if {$r1 > $r2} {
8873 set l1 [replace_by_kids $l1 $r1]
8874 } else {
8875 set l2 [replace_by_kids $l2 $r2]
8878 #set t2 [clock clicks -milliseconds]
8879 #puts "took [expr {$t2-$t1}]ms"
8882 proc compare_commits {} {
8883 global markedid rowmenuid curview children
8885 if {![info exists markedid]} return
8886 if {![commitinview $markedid $curview]} return
8887 addtohistory [list do_cmp_commits $markedid $rowmenuid]
8888 do_cmp_commits $markedid $rowmenuid
8891 proc getpatchid {id} {
8892 global patchids
8894 if {![info exists patchids($id)]} {
8895 set cmd [diffcmd [list $id] {-p --root}]
8896 # trim off the initial "|"
8897 set cmd [lrange $cmd 1 end]
8898 if {[catch {
8899 set x [eval exec $cmd | git patch-id]
8900 set patchids($id) [lindex $x 0]
8901 }]} {
8902 set patchids($id) "error"
8905 return $patchids($id)
8908 proc do_cmp_commits {a b} {
8909 global ctext curview parents children patchids commitinfo
8911 $ctext conf -state normal
8912 clear_ctext
8913 init_flist {}
8914 for {set i 0} {$i < 100} {incr i} {
8915 set skipa 0
8916 set skipb 0
8917 if {[llength $parents($curview,$a)] > 1} {
8918 appendshortlink $a [mc "Skipping merge commit "] "\n"
8919 set skipa 1
8920 } else {
8921 set patcha [getpatchid $a]
8923 if {[llength $parents($curview,$b)] > 1} {
8924 appendshortlink $b [mc "Skipping merge commit "] "\n"
8925 set skipb 1
8926 } else {
8927 set patchb [getpatchid $b]
8929 if {!$skipa && !$skipb} {
8930 set heada [lindex $commitinfo($a) 0]
8931 set headb [lindex $commitinfo($b) 0]
8932 if {$patcha eq "error"} {
8933 appendshortlink $a [mc "Error getting patch ID for "] \
8934 [mc " - stopping\n"]
8935 break
8937 if {$patchb eq "error"} {
8938 appendshortlink $b [mc "Error getting patch ID for "] \
8939 [mc " - stopping\n"]
8940 break
8942 if {$patcha eq $patchb} {
8943 if {$heada eq $headb} {
8944 appendshortlink $a [mc "Commit "]
8945 appendshortlink $b " == " " $heada\n"
8946 } else {
8947 appendshortlink $a [mc "Commit "] " $heada\n"
8948 appendshortlink $b [mc " is the same patch as\n "] \
8949 " $headb\n"
8951 set skipa 1
8952 set skipb 1
8953 } else {
8954 $ctext insert end "\n"
8955 appendshortlink $a [mc "Commit "] " $heada\n"
8956 appendshortlink $b [mc " differs from\n "] \
8957 " $headb\n"
8958 $ctext insert end [mc "Diff of commits:\n\n"]
8959 $ctext conf -state disabled
8960 update
8961 diffcommits $a $b
8962 return
8965 if {$skipa} {
8966 set kids [real_children $curview,$a]
8967 if {[llength $kids] != 1} {
8968 $ctext insert end "\n"
8969 appendshortlink $a [mc "Commit "] \
8970 [mc " has %s children - stopping\n" [llength $kids]]
8971 break
8973 set a [lindex $kids 0]
8975 if {$skipb} {
8976 set kids [real_children $curview,$b]
8977 if {[llength $kids] != 1} {
8978 appendshortlink $b [mc "Commit "] \
8979 [mc " has %s children - stopping\n" [llength $kids]]
8980 break
8982 set b [lindex $kids 0]
8985 $ctext conf -state disabled
8988 proc diffcommits {a b} {
8989 global diffcontext diffids blobdifffd diffinhdr currdiffsubmod
8991 set tmpdir [gitknewtmpdir]
8992 set fna [file join $tmpdir "commit-[string range $a 0 7]"]
8993 set fnb [file join $tmpdir "commit-[string range $b 0 7]"]
8994 if {[catch {
8995 exec git diff-tree -p --pretty $a >$fna
8996 exec git diff-tree -p --pretty $b >$fnb
8997 } err]} {
8998 error_popup [mc "Error writing commit to file: %s" $err]
8999 return
9001 if {[catch {
9002 set fd [open "| diff -U$diffcontext $fna $fnb" r]
9003 } err]} {
9004 error_popup [mc "Error diffing commits: %s" $err]
9005 return
9007 set diffids [list commits $a $b]
9008 set blobdifffd($diffids) $fd
9009 set diffinhdr 0
9010 set currdiffsubmod ""
9011 filerun $fd [list getblobdiffline $fd $diffids]
9014 proc diffvssel {dirn} {
9015 global rowmenuid selectedline
9017 if {$selectedline eq {}} return
9018 if {$dirn} {
9019 set oldid [commitonrow $selectedline]
9020 set newid $rowmenuid
9021 } else {
9022 set oldid $rowmenuid
9023 set newid [commitonrow $selectedline]
9025 addtohistory [list doseldiff $oldid $newid] savectextpos
9026 doseldiff $oldid $newid
9029 proc diffvsmark {dirn} {
9030 global rowmenuid markedid
9032 if {![info exists markedid]} return
9033 if {$dirn} {
9034 set oldid $markedid
9035 set newid $rowmenuid
9036 } else {
9037 set oldid $rowmenuid
9038 set newid $markedid
9040 addtohistory [list doseldiff $oldid $newid] savectextpos
9041 doseldiff $oldid $newid
9044 proc doseldiff {oldid newid} {
9045 global ctext
9046 global commitinfo
9048 $ctext conf -state normal
9049 clear_ctext
9050 init_flist [mc "Top"]
9051 $ctext insert end "[mc "From"] "
9052 $ctext insert end $oldid link0
9053 setlink $oldid link0
9054 $ctext insert end "\n "
9055 $ctext insert end [lindex $commitinfo($oldid) 0]
9056 $ctext insert end "\n\n[mc "To"] "
9057 $ctext insert end $newid link1
9058 setlink $newid link1
9059 $ctext insert end "\n "
9060 $ctext insert end [lindex $commitinfo($newid) 0]
9061 $ctext insert end "\n"
9062 $ctext conf -state disabled
9063 $ctext tag remove found 1.0 end
9064 startdiff [list $oldid $newid]
9067 proc mkpatch {} {
9068 global rowmenuid currentid commitinfo patchtop patchnum NS
9070 if {![info exists currentid]} return
9071 set oldid $currentid
9072 set oldhead [lindex $commitinfo($oldid) 0]
9073 set newid $rowmenuid
9074 set newhead [lindex $commitinfo($newid) 0]
9075 set top .patch
9076 set patchtop $top
9077 catch {destroy $top}
9078 ttk_toplevel $top
9079 make_transient $top .
9080 ${NS}::label $top.title -text [mc "Generate patch"]
9081 grid $top.title - -pady 10
9082 ${NS}::label $top.from -text [mc "From:"]
9083 ${NS}::entry $top.fromsha1 -width 40
9084 $top.fromsha1 insert 0 $oldid
9085 $top.fromsha1 conf -state readonly
9086 grid $top.from $top.fromsha1 -sticky w
9087 ${NS}::entry $top.fromhead -width 60
9088 $top.fromhead insert 0 $oldhead
9089 $top.fromhead conf -state readonly
9090 grid x $top.fromhead -sticky w
9091 ${NS}::label $top.to -text [mc "To:"]
9092 ${NS}::entry $top.tosha1 -width 40
9093 $top.tosha1 insert 0 $newid
9094 $top.tosha1 conf -state readonly
9095 grid $top.to $top.tosha1 -sticky w
9096 ${NS}::entry $top.tohead -width 60
9097 $top.tohead insert 0 $newhead
9098 $top.tohead conf -state readonly
9099 grid x $top.tohead -sticky w
9100 ${NS}::button $top.rev -text [mc "Reverse"] -command mkpatchrev
9101 grid $top.rev x -pady 10 -padx 5
9102 ${NS}::label $top.flab -text [mc "Output file:"]
9103 ${NS}::entry $top.fname -width 60
9104 $top.fname insert 0 [file normalize "patch$patchnum.patch"]
9105 incr patchnum
9106 grid $top.flab $top.fname -sticky w
9107 ${NS}::frame $top.buts
9108 ${NS}::button $top.buts.gen -text [mc "Generate"] -command mkpatchgo
9109 ${NS}::button $top.buts.can -text [mc "Cancel"] -command mkpatchcan
9110 bind $top <Key-Return> mkpatchgo
9111 bind $top <Key-Escape> mkpatchcan
9112 grid $top.buts.gen $top.buts.can
9113 grid columnconfigure $top.buts 0 -weight 1 -uniform a
9114 grid columnconfigure $top.buts 1 -weight 1 -uniform a
9115 grid $top.buts - -pady 10 -sticky ew
9116 focus $top.fname
9119 proc mkpatchrev {} {
9120 global patchtop
9122 set oldid [$patchtop.fromsha1 get]
9123 set oldhead [$patchtop.fromhead get]
9124 set newid [$patchtop.tosha1 get]
9125 set newhead [$patchtop.tohead get]
9126 foreach e [list fromsha1 fromhead tosha1 tohead] \
9127 v [list $newid $newhead $oldid $oldhead] {
9128 $patchtop.$e conf -state normal
9129 $patchtop.$e delete 0 end
9130 $patchtop.$e insert 0 $v
9131 $patchtop.$e conf -state readonly
9135 proc mkpatchgo {} {
9136 global patchtop nullid nullid2
9138 set oldid [$patchtop.fromsha1 get]
9139 set newid [$patchtop.tosha1 get]
9140 set fname [$patchtop.fname get]
9141 set cmd [diffcmd [list $oldid $newid] -p]
9142 # trim off the initial "|"
9143 set cmd [lrange $cmd 1 end]
9144 lappend cmd >$fname &
9145 if {[catch {eval exec $cmd} err]} {
9146 error_popup "[mc "Error creating patch:"] $err" $patchtop
9148 catch {destroy $patchtop}
9149 unset patchtop
9152 proc mkpatchcan {} {
9153 global patchtop
9155 catch {destroy $patchtop}
9156 unset patchtop
9159 proc mktag {} {
9160 global rowmenuid mktagtop commitinfo NS
9162 set top .maketag
9163 set mktagtop $top
9164 catch {destroy $top}
9165 ttk_toplevel $top
9166 make_transient $top .
9167 ${NS}::label $top.title -text [mc "Create tag"]
9168 grid $top.title - -pady 10
9169 ${NS}::label $top.id -text [mc "ID:"]
9170 ${NS}::entry $top.sha1 -width 40
9171 $top.sha1 insert 0 $rowmenuid
9172 $top.sha1 conf -state readonly
9173 grid $top.id $top.sha1 -sticky w
9174 ${NS}::entry $top.head -width 60
9175 $top.head insert 0 [lindex $commitinfo($rowmenuid) 0]
9176 $top.head conf -state readonly
9177 grid x $top.head -sticky w
9178 ${NS}::label $top.tlab -text [mc "Tag name:"]
9179 ${NS}::entry $top.tag -width 60
9180 grid $top.tlab $top.tag -sticky w
9181 ${NS}::label $top.op -text [mc "Tag message is optional"]
9182 grid $top.op -columnspan 2 -sticky we
9183 ${NS}::label $top.mlab -text [mc "Tag message:"]
9184 ${NS}::entry $top.msg -width 60
9185 grid $top.mlab $top.msg -sticky w
9186 ${NS}::frame $top.buts
9187 ${NS}::button $top.buts.gen -text [mc "Create"] -command mktaggo
9188 ${NS}::button $top.buts.can -text [mc "Cancel"] -command mktagcan
9189 bind $top <Key-Return> mktaggo
9190 bind $top <Key-Escape> mktagcan
9191 grid $top.buts.gen $top.buts.can
9192 grid columnconfigure $top.buts 0 -weight 1 -uniform a
9193 grid columnconfigure $top.buts 1 -weight 1 -uniform a
9194 grid $top.buts - -pady 10 -sticky ew
9195 focus $top.tag
9198 proc domktag {} {
9199 global mktagtop env tagids idtags
9201 set id [$mktagtop.sha1 get]
9202 set tag [$mktagtop.tag get]
9203 set msg [$mktagtop.msg get]
9204 if {$tag == {}} {
9205 error_popup [mc "No tag name specified"] $mktagtop
9206 return 0
9208 if {[info exists tagids($tag)]} {
9209 error_popup [mc "Tag \"%s\" already exists" $tag] $mktagtop
9210 return 0
9212 if {[catch {
9213 if {$msg != {}} {
9214 exec git tag -a -m $msg $tag $id
9215 } else {
9216 exec git tag $tag $id
9218 } err]} {
9219 error_popup "[mc "Error creating tag:"] $err" $mktagtop
9220 return 0
9223 set tagids($tag) $id
9224 lappend idtags($id) $tag
9225 redrawtags $id
9226 addedtag $id
9227 dispneartags 0
9228 run refill_reflist
9229 return 1
9232 proc redrawtags {id} {
9233 global canv linehtag idpos currentid curview cmitlisted markedid
9234 global canvxmax iddrawn circleitem mainheadid circlecolors
9235 global mainheadcirclecolor
9237 if {![commitinview $id $curview]} return
9238 if {![info exists iddrawn($id)]} return
9239 set row [rowofcommit $id]
9240 if {$id eq $mainheadid} {
9241 set ofill $mainheadcirclecolor
9242 } else {
9243 set ofill [lindex $circlecolors $cmitlisted($curview,$id)]
9245 $canv itemconf $circleitem($row) -fill $ofill
9246 $canv delete tag.$id
9247 set xt [eval drawtags $id $idpos($id)]
9248 $canv coords $linehtag($id) $xt [lindex $idpos($id) 2]
9249 set text [$canv itemcget $linehtag($id) -text]
9250 set font [$canv itemcget $linehtag($id) -font]
9251 set xr [expr {$xt + [font measure $font $text]}]
9252 if {$xr > $canvxmax} {
9253 set canvxmax $xr
9254 setcanvscroll
9256 if {[info exists currentid] && $currentid == $id} {
9257 make_secsel $id
9259 if {[info exists markedid] && $markedid eq $id} {
9260 make_idmark $id
9264 proc mktagcan {} {
9265 global mktagtop
9267 catch {destroy $mktagtop}
9268 unset mktagtop
9271 proc mktaggo {} {
9272 if {![domktag]} return
9273 mktagcan
9276 proc writecommit {} {
9277 global rowmenuid wrcomtop commitinfo wrcomcmd NS
9279 set top .writecommit
9280 set wrcomtop $top
9281 catch {destroy $top}
9282 ttk_toplevel $top
9283 make_transient $top .
9284 ${NS}::label $top.title -text [mc "Write commit to file"]
9285 grid $top.title - -pady 10
9286 ${NS}::label $top.id -text [mc "ID:"]
9287 ${NS}::entry $top.sha1 -width 40
9288 $top.sha1 insert 0 $rowmenuid
9289 $top.sha1 conf -state readonly
9290 grid $top.id $top.sha1 -sticky w
9291 ${NS}::entry $top.head -width 60
9292 $top.head insert 0 [lindex $commitinfo($rowmenuid) 0]
9293 $top.head conf -state readonly
9294 grid x $top.head -sticky w
9295 ${NS}::label $top.clab -text [mc "Command:"]
9296 ${NS}::entry $top.cmd -width 60 -textvariable wrcomcmd
9297 grid $top.clab $top.cmd -sticky w -pady 10
9298 ${NS}::label $top.flab -text [mc "Output file:"]
9299 ${NS}::entry $top.fname -width 60
9300 $top.fname insert 0 [file normalize "commit-[string range $rowmenuid 0 6]"]
9301 grid $top.flab $top.fname -sticky w
9302 ${NS}::frame $top.buts
9303 ${NS}::button $top.buts.gen -text [mc "Write"] -command wrcomgo
9304 ${NS}::button $top.buts.can -text [mc "Cancel"] -command wrcomcan
9305 bind $top <Key-Return> wrcomgo
9306 bind $top <Key-Escape> wrcomcan
9307 grid $top.buts.gen $top.buts.can
9308 grid columnconfigure $top.buts 0 -weight 1 -uniform a
9309 grid columnconfigure $top.buts 1 -weight 1 -uniform a
9310 grid $top.buts - -pady 10 -sticky ew
9311 focus $top.fname
9314 proc wrcomgo {} {
9315 global wrcomtop
9317 set id [$wrcomtop.sha1 get]
9318 set cmd "echo $id | [$wrcomtop.cmd get]"
9319 set fname [$wrcomtop.fname get]
9320 if {[catch {exec sh -c $cmd >$fname &} err]} {
9321 error_popup "[mc "Error writing commit:"] $err" $wrcomtop
9323 catch {destroy $wrcomtop}
9324 unset wrcomtop
9327 proc wrcomcan {} {
9328 global wrcomtop
9330 catch {destroy $wrcomtop}
9331 unset wrcomtop
9334 proc mkbranch {} {
9335 global rowmenuid mkbrtop NS
9337 set top .makebranch
9338 catch {destroy $top}
9339 ttk_toplevel $top
9340 make_transient $top .
9341 ${NS}::label $top.title -text [mc "Create new branch"]
9342 grid $top.title - -pady 10
9343 ${NS}::label $top.id -text [mc "ID:"]
9344 ${NS}::entry $top.sha1 -width 40
9345 $top.sha1 insert 0 $rowmenuid
9346 $top.sha1 conf -state readonly
9347 grid $top.id $top.sha1 -sticky w
9348 ${NS}::label $top.nlab -text [mc "Name:"]
9349 ${NS}::entry $top.name -width 40
9350 grid $top.nlab $top.name -sticky w
9351 ${NS}::frame $top.buts
9352 ${NS}::button $top.buts.go -text [mc "Create"] -command [list mkbrgo $top]
9353 ${NS}::button $top.buts.can -text [mc "Cancel"] -command "catch {destroy $top}"
9354 bind $top <Key-Return> [list mkbrgo $top]
9355 bind $top <Key-Escape> "catch {destroy $top}"
9356 grid $top.buts.go $top.buts.can
9357 grid columnconfigure $top.buts 0 -weight 1 -uniform a
9358 grid columnconfigure $top.buts 1 -weight 1 -uniform a
9359 grid $top.buts - -pady 10 -sticky ew
9360 focus $top.name
9363 proc mkbrgo {top} {
9364 global headids idheads
9366 set name [$top.name get]
9367 set id [$top.sha1 get]
9368 set cmdargs {}
9369 set old_id {}
9370 if {$name eq {}} {
9371 error_popup [mc "Please specify a name for the new branch"] $top
9372 return
9374 if {[info exists headids($name)]} {
9375 if {![confirm_popup [mc \
9376 "Branch '%s' already exists. Overwrite?" $name] $top]} {
9377 return
9379 set old_id $headids($name)
9380 lappend cmdargs -f
9382 catch {destroy $top}
9383 lappend cmdargs $name $id
9384 nowbusy newbranch
9385 update
9386 if {[catch {
9387 eval exec git branch $cmdargs
9388 } err]} {
9389 notbusy newbranch
9390 error_popup $err
9391 } else {
9392 notbusy newbranch
9393 if {$old_id ne {}} {
9394 movehead $id $name
9395 movedhead $id $name
9396 redrawtags $old_id
9397 redrawtags $id
9398 } else {
9399 set headids($name) $id
9400 lappend idheads($id) $name
9401 addedhead $id $name
9402 redrawtags $id
9404 dispneartags 0
9405 run refill_reflist
9409 proc exec_citool {tool_args {baseid {}}} {
9410 global commitinfo env
9412 set save_env [array get env GIT_AUTHOR_*]
9414 if {$baseid ne {}} {
9415 if {![info exists commitinfo($baseid)]} {
9416 getcommit $baseid
9418 set author [lindex $commitinfo($baseid) 1]
9419 set date [lindex $commitinfo($baseid) 2]
9420 if {[regexp {^\s*(\S.*\S|\S)\s*<(.*)>\s*$} \
9421 $author author name email]
9422 && $date ne {}} {
9423 set env(GIT_AUTHOR_NAME) $name
9424 set env(GIT_AUTHOR_EMAIL) $email
9425 set env(GIT_AUTHOR_DATE) $date
9429 eval exec git citool $tool_args &
9431 array unset env GIT_AUTHOR_*
9432 array set env $save_env
9435 proc cherrypick {} {
9436 global rowmenuid curview
9437 global mainhead mainheadid
9438 global gitdir
9440 set oldhead [exec git rev-parse HEAD]
9441 set dheads [descheads $rowmenuid]
9442 if {$dheads ne {} && [lsearch -exact $dheads $oldhead] >= 0} {
9443 set ok [confirm_popup [mc "Commit %s is already\
9444 included in branch %s -- really re-apply it?" \
9445 [string range $rowmenuid 0 7] $mainhead]]
9446 if {!$ok} return
9448 nowbusy cherrypick [mc "Cherry-picking"]
9449 update
9450 # Unfortunately git-cherry-pick writes stuff to stderr even when
9451 # no error occurs, and exec takes that as an indication of error...
9452 if {[catch {exec sh -c "git cherry-pick -r $rowmenuid 2>&1"} err]} {
9453 notbusy cherrypick
9454 if {[regexp -line \
9455 {Entry '(.*)' (would be overwritten by merge|not uptodate)} \
9456 $err msg fname]} {
9457 error_popup [mc "Cherry-pick failed because of local changes\
9458 to file '%s'.\nPlease commit, reset or stash\
9459 your changes and try again." $fname]
9460 } elseif {[regexp -line \
9461 {^(CONFLICT \(.*\):|Automatic cherry-pick failed|error: could not apply)} \
9462 $err]} {
9463 if {[confirm_popup [mc "Cherry-pick failed because of merge\
9464 conflict.\nDo you wish to run git citool to\
9465 resolve it?"]]} {
9466 # Force citool to read MERGE_MSG
9467 file delete [file join $gitdir "GITGUI_MSG"]
9468 exec_citool {} $rowmenuid
9470 } else {
9471 error_popup $err
9473 run updatecommits
9474 return
9476 set newhead [exec git rev-parse HEAD]
9477 if {$newhead eq $oldhead} {
9478 notbusy cherrypick
9479 error_popup [mc "No changes committed"]
9480 return
9482 addnewchild $newhead $oldhead
9483 if {[commitinview $oldhead $curview]} {
9484 # XXX this isn't right if we have a path limit...
9485 insertrow $newhead $oldhead $curview
9486 if {$mainhead ne {}} {
9487 movehead $newhead $mainhead
9488 movedhead $newhead $mainhead
9490 set mainheadid $newhead
9491 redrawtags $oldhead
9492 redrawtags $newhead
9493 selbyid $newhead
9495 notbusy cherrypick
9498 proc revert {} {
9499 global rowmenuid curview
9500 global mainhead mainheadid
9501 global gitdir
9503 set oldhead [exec git rev-parse HEAD]
9504 set dheads [descheads $rowmenuid]
9505 if { $dheads eq {} || [lsearch -exact $dheads $oldhead] == -1 } {
9506 set ok [confirm_popup [mc "Commit %s is not\
9507 included in branch %s -- really revert it?" \
9508 [string range $rowmenuid 0 7] $mainhead]]
9509 if {!$ok} return
9511 nowbusy revert [mc "Reverting"]
9512 update
9514 if [catch {exec git revert --no-edit $rowmenuid} err] {
9515 notbusy revert
9516 if [regexp {files would be overwritten by merge:(\n(( |\t)+[^\n]+\n)+)}\
9517 $err match files] {
9518 regsub {\n( |\t)+} $files "\n" files
9519 error_popup [mc "Revert failed because of local changes to\
9520 the following files:%s Please commit, reset or stash \
9521 your changes and try again." $files]
9522 } elseif [regexp {error: could not revert} $err] {
9523 if [confirm_popup [mc "Revert failed because of merge conflict.\n\
9524 Do you wish to run git citool to resolve it?"]] {
9525 # Force citool to read MERGE_MSG
9526 file delete [file join $gitdir "GITGUI_MSG"]
9527 exec_citool {} $rowmenuid
9529 } else { error_popup $err }
9530 run updatecommits
9531 return
9534 set newhead [exec git rev-parse HEAD]
9535 if { $newhead eq $oldhead } {
9536 notbusy revert
9537 error_popup [mc "No changes committed"]
9538 return
9541 addnewchild $newhead $oldhead
9543 if [commitinview $oldhead $curview] {
9544 # XXX this isn't right if we have a path limit...
9545 insertrow $newhead $oldhead $curview
9546 if {$mainhead ne {}} {
9547 movehead $newhead $mainhead
9548 movedhead $newhead $mainhead
9550 set mainheadid $newhead
9551 redrawtags $oldhead
9552 redrawtags $newhead
9553 selbyid $newhead
9556 notbusy revert
9559 proc resethead {} {
9560 global mainhead rowmenuid confirm_ok resettype NS
9562 set confirm_ok 0
9563 set w ".confirmreset"
9564 ttk_toplevel $w
9565 make_transient $w .
9566 wm title $w [mc "Confirm reset"]
9567 ${NS}::label $w.m -text \
9568 [mc "Reset branch %s to %s?" $mainhead [string range $rowmenuid 0 7]]
9569 pack $w.m -side top -fill x -padx 20 -pady 20
9570 ${NS}::labelframe $w.f -text [mc "Reset type:"]
9571 set resettype mixed
9572 ${NS}::radiobutton $w.f.soft -value soft -variable resettype \
9573 -text [mc "Soft: Leave working tree and index untouched"]
9574 grid $w.f.soft -sticky w
9575 ${NS}::radiobutton $w.f.mixed -value mixed -variable resettype \
9576 -text [mc "Mixed: Leave working tree untouched, reset index"]
9577 grid $w.f.mixed -sticky w
9578 ${NS}::radiobutton $w.f.hard -value hard -variable resettype \
9579 -text [mc "Hard: Reset working tree and index\n(discard ALL local changes)"]
9580 grid $w.f.hard -sticky w
9581 pack $w.f -side top -fill x -padx 4
9582 ${NS}::button $w.ok -text [mc OK] -command "set confirm_ok 1; destroy $w"
9583 pack $w.ok -side left -fill x -padx 20 -pady 20
9584 ${NS}::button $w.cancel -text [mc Cancel] -command "destroy $w"
9585 bind $w <Key-Escape> [list destroy $w]
9586 pack $w.cancel -side right -fill x -padx 20 -pady 20
9587 bind $w <Visibility> "grab $w; focus $w"
9588 tkwait window $w
9589 if {!$confirm_ok} return
9590 if {[catch {set fd [open \
9591 [list | git reset --$resettype $rowmenuid 2>@1] r]} err]} {
9592 error_popup $err
9593 } else {
9594 dohidelocalchanges
9595 filerun $fd [list readresetstat $fd]
9596 nowbusy reset [mc "Resetting"]
9597 selbyid $rowmenuid
9601 proc readresetstat {fd} {
9602 global mainhead mainheadid showlocalchanges rprogcoord
9604 if {[gets $fd line] >= 0} {
9605 if {[regexp {([0-9]+)% \(([0-9]+)/([0-9]+)\)} $line match p m n]} {
9606 set rprogcoord [expr {1.0 * $m / $n}]
9607 adjustprogress
9609 return 1
9611 set rprogcoord 0
9612 adjustprogress
9613 notbusy reset
9614 if {[catch {close $fd} err]} {
9615 error_popup $err
9617 set oldhead $mainheadid
9618 set newhead [exec git rev-parse HEAD]
9619 if {$newhead ne $oldhead} {
9620 movehead $newhead $mainhead
9621 movedhead $newhead $mainhead
9622 set mainheadid $newhead
9623 redrawtags $oldhead
9624 redrawtags $newhead
9626 if {$showlocalchanges} {
9627 doshowlocalchanges
9629 return 0
9632 # context menu for a head
9633 proc headmenu {x y id head} {
9634 global headmenuid headmenuhead headctxmenu mainhead
9636 stopfinding
9637 set headmenuid $id
9638 set headmenuhead $head
9639 set state normal
9640 if {[string match "remotes/*" $head]} {
9641 set state disabled
9643 if {$head eq $mainhead} {
9644 set state disabled
9646 $headctxmenu entryconfigure 0 -state $state
9647 $headctxmenu entryconfigure 1 -state $state
9648 tk_popup $headctxmenu $x $y
9651 proc cobranch {} {
9652 global headmenuid headmenuhead headids
9653 global showlocalchanges
9655 # check the tree is clean first??
9656 nowbusy checkout [mc "Checking out"]
9657 update
9658 dohidelocalchanges
9659 if {[catch {
9660 set fd [open [list | git checkout $headmenuhead 2>@1] r]
9661 } err]} {
9662 notbusy checkout
9663 error_popup $err
9664 if {$showlocalchanges} {
9665 dodiffindex
9667 } else {
9668 filerun $fd [list readcheckoutstat $fd $headmenuhead $headmenuid]
9672 proc readcheckoutstat {fd newhead newheadid} {
9673 global mainhead mainheadid headids showlocalchanges progresscoords
9674 global viewmainheadid curview
9676 if {[gets $fd line] >= 0} {
9677 if {[regexp {([0-9]+)% \(([0-9]+)/([0-9]+)\)} $line match p m n]} {
9678 set progresscoords [list 0 [expr {1.0 * $m / $n}]]
9679 adjustprogress
9681 return 1
9683 set progresscoords {0 0}
9684 adjustprogress
9685 notbusy checkout
9686 if {[catch {close $fd} err]} {
9687 error_popup $err
9689 set oldmainid $mainheadid
9690 set mainhead $newhead
9691 set mainheadid $newheadid
9692 set viewmainheadid($curview) $newheadid
9693 redrawtags $oldmainid
9694 redrawtags $newheadid
9695 selbyid $newheadid
9696 if {$showlocalchanges} {
9697 dodiffindex
9701 proc rmbranch {} {
9702 global headmenuid headmenuhead mainhead
9703 global idheads
9705 set head $headmenuhead
9706 set id $headmenuid
9707 # this check shouldn't be needed any more...
9708 if {$head eq $mainhead} {
9709 error_popup [mc "Cannot delete the currently checked-out branch"]
9710 return
9712 set dheads [descheads $id]
9713 if {[llength $dheads] == 1 && $idheads($dheads) eq $head} {
9714 # the stuff on this branch isn't on any other branch
9715 if {![confirm_popup [mc "The commits on branch %s aren't on any other\
9716 branch.\nReally delete branch %s?" $head $head]]} return
9718 nowbusy rmbranch
9719 update
9720 if {[catch {exec git branch -D $head} err]} {
9721 notbusy rmbranch
9722 error_popup $err
9723 return
9725 removehead $id $head
9726 removedhead $id $head
9727 redrawtags $id
9728 notbusy rmbranch
9729 dispneartags 0
9730 run refill_reflist
9733 # Display a list of tags and heads
9734 proc showrefs {} {
9735 global showrefstop bgcolor fgcolor selectbgcolor NS
9736 global bglist fglist reflistfilter reflist maincursor
9738 set top .showrefs
9739 set showrefstop $top
9740 if {[winfo exists $top]} {
9741 raise $top
9742 refill_reflist
9743 return
9745 ttk_toplevel $top
9746 wm title $top [mc "Tags and heads: %s" [file tail [pwd]]]
9747 make_transient $top .
9748 text $top.list -background $bgcolor -foreground $fgcolor \
9749 -selectbackground $selectbgcolor -font mainfont \
9750 -xscrollcommand "$top.xsb set" -yscrollcommand "$top.ysb set" \
9751 -width 30 -height 20 -cursor $maincursor \
9752 -spacing1 1 -spacing3 1 -state disabled
9753 $top.list tag configure highlight -background $selectbgcolor
9754 lappend bglist $top.list
9755 lappend fglist $top.list
9756 ${NS}::scrollbar $top.ysb -command "$top.list yview" -orient vertical
9757 ${NS}::scrollbar $top.xsb -command "$top.list xview" -orient horizontal
9758 grid $top.list $top.ysb -sticky nsew
9759 grid $top.xsb x -sticky ew
9760 ${NS}::frame $top.f
9761 ${NS}::label $top.f.l -text "[mc "Filter"]: "
9762 ${NS}::entry $top.f.e -width 20 -textvariable reflistfilter
9763 set reflistfilter "*"
9764 trace add variable reflistfilter write reflistfilter_change
9765 pack $top.f.e -side right -fill x -expand 1
9766 pack $top.f.l -side left
9767 grid $top.f - -sticky ew -pady 2
9768 ${NS}::button $top.close -command [list destroy $top] -text [mc "Close"]
9769 bind $top <Key-Escape> [list destroy $top]
9770 grid $top.close -
9771 grid columnconfigure $top 0 -weight 1
9772 grid rowconfigure $top 0 -weight 1
9773 bind $top.list <1> {break}
9774 bind $top.list <B1-Motion> {break}
9775 bind $top.list <ButtonRelease-1> {sel_reflist %W %x %y; break}
9776 set reflist {}
9777 refill_reflist
9780 proc sel_reflist {w x y} {
9781 global showrefstop reflist headids tagids otherrefids
9783 if {![winfo exists $showrefstop]} return
9784 set l [lindex [split [$w index "@$x,$y"] "."] 0]
9785 set ref [lindex $reflist [expr {$l-1}]]
9786 set n [lindex $ref 0]
9787 switch -- [lindex $ref 1] {
9788 "H" {selbyid $headids($n)}
9789 "T" {selbyid $tagids($n)}
9790 "o" {selbyid $otherrefids($n)}
9792 $showrefstop.list tag add highlight $l.0 "$l.0 lineend"
9795 proc unsel_reflist {} {
9796 global showrefstop
9798 if {![info exists showrefstop] || ![winfo exists $showrefstop]} return
9799 $showrefstop.list tag remove highlight 0.0 end
9802 proc reflistfilter_change {n1 n2 op} {
9803 global reflistfilter
9805 after cancel refill_reflist
9806 after 200 refill_reflist
9809 proc refill_reflist {} {
9810 global reflist reflistfilter showrefstop headids tagids otherrefids
9811 global curview
9813 if {![info exists showrefstop] || ![winfo exists $showrefstop]} return
9814 set refs {}
9815 foreach n [array names headids] {
9816 if {[string match $reflistfilter $n]} {
9817 if {[commitinview $headids($n) $curview]} {
9818 lappend refs [list $n H]
9819 } else {
9820 interestedin $headids($n) {run refill_reflist}
9824 foreach n [array names tagids] {
9825 if {[string match $reflistfilter $n]} {
9826 if {[commitinview $tagids($n) $curview]} {
9827 lappend refs [list $n T]
9828 } else {
9829 interestedin $tagids($n) {run refill_reflist}
9833 foreach n [array names otherrefids] {
9834 if {[string match $reflistfilter $n]} {
9835 if {[commitinview $otherrefids($n) $curview]} {
9836 lappend refs [list $n o]
9837 } else {
9838 interestedin $otherrefids($n) {run refill_reflist}
9842 set refs [lsort -index 0 $refs]
9843 if {$refs eq $reflist} return
9845 # Update the contents of $showrefstop.list according to the
9846 # differences between $reflist (old) and $refs (new)
9847 $showrefstop.list conf -state normal
9848 $showrefstop.list insert end "\n"
9849 set i 0
9850 set j 0
9851 while {$i < [llength $reflist] || $j < [llength $refs]} {
9852 if {$i < [llength $reflist]} {
9853 if {$j < [llength $refs]} {
9854 set cmp [string compare [lindex $reflist $i 0] \
9855 [lindex $refs $j 0]]
9856 if {$cmp == 0} {
9857 set cmp [string compare [lindex $reflist $i 1] \
9858 [lindex $refs $j 1]]
9860 } else {
9861 set cmp -1
9863 } else {
9864 set cmp 1
9866 switch -- $cmp {
9867 -1 {
9868 $showrefstop.list delete "[expr {$j+1}].0" "[expr {$j+2}].0"
9869 incr i
9872 incr i
9873 incr j
9876 set l [expr {$j + 1}]
9877 $showrefstop.list image create $l.0 -align baseline \
9878 -image reficon-[lindex $refs $j 1] -padx 2
9879 $showrefstop.list insert $l.1 "[lindex $refs $j 0]\n"
9880 incr j
9884 set reflist $refs
9885 # delete last newline
9886 $showrefstop.list delete end-2c end-1c
9887 $showrefstop.list conf -state disabled
9890 # Stuff for finding nearby tags
9891 proc getallcommits {} {
9892 global allcommits nextarc seeds allccache allcwait cachedarcs allcupdate
9893 global idheads idtags idotherrefs allparents tagobjid
9894 global gitdir
9896 if {![info exists allcommits]} {
9897 set nextarc 0
9898 set allcommits 0
9899 set seeds {}
9900 set allcwait 0
9901 set cachedarcs 0
9902 set allccache [file join $gitdir "gitk.cache"]
9903 if {![catch {
9904 set f [open $allccache r]
9905 set allcwait 1
9906 getcache $f
9907 }]} return
9910 if {$allcwait} {
9911 return
9913 set cmd [list | git rev-list --parents]
9914 set allcupdate [expr {$seeds ne {}}]
9915 if {!$allcupdate} {
9916 set ids "--all"
9917 } else {
9918 set refs [concat [array names idheads] [array names idtags] \
9919 [array names idotherrefs]]
9920 set ids {}
9921 set tagobjs {}
9922 foreach name [array names tagobjid] {
9923 lappend tagobjs $tagobjid($name)
9925 foreach id [lsort -unique $refs] {
9926 if {![info exists allparents($id)] &&
9927 [lsearch -exact $tagobjs $id] < 0} {
9928 lappend ids $id
9931 if {$ids ne {}} {
9932 foreach id $seeds {
9933 lappend ids "^$id"
9937 if {$ids ne {}} {
9938 set cmd [limit_arg_length [concat $cmd $ids]]
9939 set fd [open $cmd r]
9940 fconfigure $fd -blocking 0
9941 incr allcommits
9942 nowbusy allcommits
9943 filerun $fd [list getallclines $fd]
9944 } else {
9945 dispneartags 0
9949 # The maximum command line length for the CreateProcess function is 32767 characters, see
9950 # http://blogs.msdn.com/oldnewthing/archive/2003/12/10/56028.aspx
9951 # Be a little conservative in case Tcl adds some more stuff to the command line we do not
9952 # know about and truncate the command line at a SHA1-boundary below 32000 characters.
9953 proc limit_arg_length {cmd} {
9954 if {[tk windowingsystem] == "win32" &&
9955 [string length $cmd] > 32000} {
9956 set ndx [string last " " $cmd 32000]
9957 if {$ndx != -1} {
9958 return [string range $cmd 0 $ndx]
9961 return $cmd
9964 # Since most commits have 1 parent and 1 child, we group strings of
9965 # such commits into "arcs" joining branch/merge points (BMPs), which
9966 # are commits that either don't have 1 parent or don't have 1 child.
9968 # arcnos(id) - incoming arcs for BMP, arc we're on for other nodes
9969 # arcout(id) - outgoing arcs for BMP
9970 # arcids(a) - list of IDs on arc including end but not start
9971 # arcstart(a) - BMP ID at start of arc
9972 # arcend(a) - BMP ID at end of arc
9973 # growing(a) - arc a is still growing
9974 # arctags(a) - IDs out of arcids (excluding end) that have tags
9975 # archeads(a) - IDs out of arcids (excluding end) that have heads
9976 # The start of an arc is at the descendent end, so "incoming" means
9977 # coming from descendents, and "outgoing" means going towards ancestors.
9979 proc getallclines {fd} {
9980 global allparents allchildren idtags idheads nextarc
9981 global arcnos arcids arctags arcout arcend arcstart archeads growing
9982 global seeds allcommits cachedarcs allcupdate
9984 set nid 0
9985 while {[incr nid] <= 1000 && [gets $fd line] >= 0} {
9986 set id [lindex $line 0]
9987 if {[info exists allparents($id)]} {
9988 # seen it already
9989 continue
9991 set cachedarcs 0
9992 set olds [lrange $line 1 end]
9993 set allparents($id) $olds
9994 if {![info exists allchildren($id)]} {
9995 set allchildren($id) {}
9996 set arcnos($id) {}
9997 lappend seeds $id
9998 } else {
9999 set a $arcnos($id)
10000 if {[llength $olds] == 1 && [llength $a] == 1} {
10001 lappend arcids($a) $id
10002 if {[info exists idtags($id)]} {
10003 lappend arctags($a) $id
10005 if {[info exists idheads($id)]} {
10006 lappend archeads($a) $id
10008 if {[info exists allparents($olds)]} {
10009 # seen parent already
10010 if {![info exists arcout($olds)]} {
10011 splitarc $olds
10013 lappend arcids($a) $olds
10014 set arcend($a) $olds
10015 unset growing($a)
10017 lappend allchildren($olds) $id
10018 lappend arcnos($olds) $a
10019 continue
10022 foreach a $arcnos($id) {
10023 lappend arcids($a) $id
10024 set arcend($a) $id
10025 unset growing($a)
10028 set ao {}
10029 foreach p $olds {
10030 lappend allchildren($p) $id
10031 set a [incr nextarc]
10032 set arcstart($a) $id
10033 set archeads($a) {}
10034 set arctags($a) {}
10035 set archeads($a) {}
10036 set arcids($a) {}
10037 lappend ao $a
10038 set growing($a) 1
10039 if {[info exists allparents($p)]} {
10040 # seen it already, may need to make a new branch
10041 if {![info exists arcout($p)]} {
10042 splitarc $p
10044 lappend arcids($a) $p
10045 set arcend($a) $p
10046 unset growing($a)
10048 lappend arcnos($p) $a
10050 set arcout($id) $ao
10052 if {$nid > 0} {
10053 global cached_dheads cached_dtags cached_atags
10054 catch {unset cached_dheads}
10055 catch {unset cached_dtags}
10056 catch {unset cached_atags}
10058 if {![eof $fd]} {
10059 return [expr {$nid >= 1000? 2: 1}]
10061 set cacheok 1
10062 if {[catch {
10063 fconfigure $fd -blocking 1
10064 close $fd
10065 } err]} {
10066 # got an error reading the list of commits
10067 # if we were updating, try rereading the whole thing again
10068 if {$allcupdate} {
10069 incr allcommits -1
10070 dropcache $err
10071 return
10073 error_popup "[mc "Error reading commit topology information;\
10074 branch and preceding/following tag information\
10075 will be incomplete."]\n($err)"
10076 set cacheok 0
10078 if {[incr allcommits -1] == 0} {
10079 notbusy allcommits
10080 if {$cacheok} {
10081 run savecache
10084 dispneartags 0
10085 return 0
10088 proc recalcarc {a} {
10089 global arctags archeads arcids idtags idheads
10091 set at {}
10092 set ah {}
10093 foreach id [lrange $arcids($a) 0 end-1] {
10094 if {[info exists idtags($id)]} {
10095 lappend at $id
10097 if {[info exists idheads($id)]} {
10098 lappend ah $id
10101 set arctags($a) $at
10102 set archeads($a) $ah
10105 proc splitarc {p} {
10106 global arcnos arcids nextarc arctags archeads idtags idheads
10107 global arcstart arcend arcout allparents growing
10109 set a $arcnos($p)
10110 if {[llength $a] != 1} {
10111 puts "oops splitarc called but [llength $a] arcs already"
10112 return
10114 set a [lindex $a 0]
10115 set i [lsearch -exact $arcids($a) $p]
10116 if {$i < 0} {
10117 puts "oops splitarc $p not in arc $a"
10118 return
10120 set na [incr nextarc]
10121 if {[info exists arcend($a)]} {
10122 set arcend($na) $arcend($a)
10123 } else {
10124 set l [lindex $allparents([lindex $arcids($a) end]) 0]
10125 set j [lsearch -exact $arcnos($l) $a]
10126 set arcnos($l) [lreplace $arcnos($l) $j $j $na]
10128 set tail [lrange $arcids($a) [expr {$i+1}] end]
10129 set arcids($a) [lrange $arcids($a) 0 $i]
10130 set arcend($a) $p
10131 set arcstart($na) $p
10132 set arcout($p) $na
10133 set arcids($na) $tail
10134 if {[info exists growing($a)]} {
10135 set growing($na) 1
10136 unset growing($a)
10139 foreach id $tail {
10140 if {[llength $arcnos($id)] == 1} {
10141 set arcnos($id) $na
10142 } else {
10143 set j [lsearch -exact $arcnos($id) $a]
10144 set arcnos($id) [lreplace $arcnos($id) $j $j $na]
10148 # reconstruct tags and heads lists
10149 if {$arctags($a) ne {} || $archeads($a) ne {}} {
10150 recalcarc $a
10151 recalcarc $na
10152 } else {
10153 set arctags($na) {}
10154 set archeads($na) {}
10158 # Update things for a new commit added that is a child of one
10159 # existing commit. Used when cherry-picking.
10160 proc addnewchild {id p} {
10161 global allparents allchildren idtags nextarc
10162 global arcnos arcids arctags arcout arcend arcstart archeads growing
10163 global seeds allcommits
10165 if {![info exists allcommits] || ![info exists arcnos($p)]} return
10166 set allparents($id) [list $p]
10167 set allchildren($id) {}
10168 set arcnos($id) {}
10169 lappend seeds $id
10170 lappend allchildren($p) $id
10171 set a [incr nextarc]
10172 set arcstart($a) $id
10173 set archeads($a) {}
10174 set arctags($a) {}
10175 set arcids($a) [list $p]
10176 set arcend($a) $p
10177 if {![info exists arcout($p)]} {
10178 splitarc $p
10180 lappend arcnos($p) $a
10181 set arcout($id) [list $a]
10184 # This implements a cache for the topology information.
10185 # The cache saves, for each arc, the start and end of the arc,
10186 # the ids on the arc, and the outgoing arcs from the end.
10187 proc readcache {f} {
10188 global arcnos arcids arcout arcstart arcend arctags archeads nextarc
10189 global idtags idheads allparents cachedarcs possible_seeds seeds growing
10190 global allcwait
10192 set a $nextarc
10193 set lim $cachedarcs
10194 if {$lim - $a > 500} {
10195 set lim [expr {$a + 500}]
10197 if {[catch {
10198 if {$a == $lim} {
10199 # finish reading the cache and setting up arctags, etc.
10200 set line [gets $f]
10201 if {$line ne "1"} {error "bad final version"}
10202 close $f
10203 foreach id [array names idtags] {
10204 if {[info exists arcnos($id)] && [llength $arcnos($id)] == 1 &&
10205 [llength $allparents($id)] == 1} {
10206 set a [lindex $arcnos($id) 0]
10207 if {$arctags($a) eq {}} {
10208 recalcarc $a
10212 foreach id [array names idheads] {
10213 if {[info exists arcnos($id)] && [llength $arcnos($id)] == 1 &&
10214 [llength $allparents($id)] == 1} {
10215 set a [lindex $arcnos($id) 0]
10216 if {$archeads($a) eq {}} {
10217 recalcarc $a
10221 foreach id [lsort -unique $possible_seeds] {
10222 if {$arcnos($id) eq {}} {
10223 lappend seeds $id
10226 set allcwait 0
10227 } else {
10228 while {[incr a] <= $lim} {
10229 set line [gets $f]
10230 if {[llength $line] != 3} {error "bad line"}
10231 set s [lindex $line 0]
10232 set arcstart($a) $s
10233 lappend arcout($s) $a
10234 if {![info exists arcnos($s)]} {
10235 lappend possible_seeds $s
10236 set arcnos($s) {}
10238 set e [lindex $line 1]
10239 if {$e eq {}} {
10240 set growing($a) 1
10241 } else {
10242 set arcend($a) $e
10243 if {![info exists arcout($e)]} {
10244 set arcout($e) {}
10247 set arcids($a) [lindex $line 2]
10248 foreach id $arcids($a) {
10249 lappend allparents($s) $id
10250 set s $id
10251 lappend arcnos($id) $a
10253 if {![info exists allparents($s)]} {
10254 set allparents($s) {}
10256 set arctags($a) {}
10257 set archeads($a) {}
10259 set nextarc [expr {$a - 1}]
10261 } err]} {
10262 dropcache $err
10263 return 0
10265 if {!$allcwait} {
10266 getallcommits
10268 return $allcwait
10271 proc getcache {f} {
10272 global nextarc cachedarcs possible_seeds
10274 if {[catch {
10275 set line [gets $f]
10276 if {[llength $line] != 2 || [lindex $line 0] ne "1"} {error "bad version"}
10277 # make sure it's an integer
10278 set cachedarcs [expr {int([lindex $line 1])}]
10279 if {$cachedarcs < 0} {error "bad number of arcs"}
10280 set nextarc 0
10281 set possible_seeds {}
10282 run readcache $f
10283 } err]} {
10284 dropcache $err
10286 return 0
10289 proc dropcache {err} {
10290 global allcwait nextarc cachedarcs seeds
10292 #puts "dropping cache ($err)"
10293 foreach v {arcnos arcout arcids arcstart arcend growing \
10294 arctags archeads allparents allchildren} {
10295 global $v
10296 catch {unset $v}
10298 set allcwait 0
10299 set nextarc 0
10300 set cachedarcs 0
10301 set seeds {}
10302 getallcommits
10305 proc writecache {f} {
10306 global cachearc cachedarcs allccache
10307 global arcstart arcend arcnos arcids arcout
10309 set a $cachearc
10310 set lim $cachedarcs
10311 if {$lim - $a > 1000} {
10312 set lim [expr {$a + 1000}]
10314 if {[catch {
10315 while {[incr a] <= $lim} {
10316 if {[info exists arcend($a)]} {
10317 puts $f [list $arcstart($a) $arcend($a) $arcids($a)]
10318 } else {
10319 puts $f [list $arcstart($a) {} $arcids($a)]
10322 } err]} {
10323 catch {close $f}
10324 catch {file delete $allccache}
10325 #puts "writing cache failed ($err)"
10326 return 0
10328 set cachearc [expr {$a - 1}]
10329 if {$a > $cachedarcs} {
10330 puts $f "1"
10331 close $f
10332 return 0
10334 return 1
10337 proc savecache {} {
10338 global nextarc cachedarcs cachearc allccache
10340 if {$nextarc == $cachedarcs} return
10341 set cachearc 0
10342 set cachedarcs $nextarc
10343 catch {
10344 set f [open $allccache w]
10345 puts $f [list 1 $cachedarcs]
10346 run writecache $f
10350 # Returns 1 if a is an ancestor of b, -1 if b is an ancestor of a,
10351 # or 0 if neither is true.
10352 proc anc_or_desc {a b} {
10353 global arcout arcstart arcend arcnos cached_isanc
10355 if {$arcnos($a) eq $arcnos($b)} {
10356 # Both are on the same arc(s); either both are the same BMP,
10357 # or if one is not a BMP, the other is also not a BMP or is
10358 # the BMP at end of the arc (and it only has 1 incoming arc).
10359 # Or both can be BMPs with no incoming arcs.
10360 if {$a eq $b || $arcnos($a) eq {}} {
10361 return 0
10363 # assert {[llength $arcnos($a)] == 1}
10364 set arc [lindex $arcnos($a) 0]
10365 set i [lsearch -exact $arcids($arc) $a]
10366 set j [lsearch -exact $arcids($arc) $b]
10367 if {$i < 0 || $i > $j} {
10368 return 1
10369 } else {
10370 return -1
10374 if {![info exists arcout($a)]} {
10375 set arc [lindex $arcnos($a) 0]
10376 if {[info exists arcend($arc)]} {
10377 set aend $arcend($arc)
10378 } else {
10379 set aend {}
10381 set a $arcstart($arc)
10382 } else {
10383 set aend $a
10385 if {![info exists arcout($b)]} {
10386 set arc [lindex $arcnos($b) 0]
10387 if {[info exists arcend($arc)]} {
10388 set bend $arcend($arc)
10389 } else {
10390 set bend {}
10392 set b $arcstart($arc)
10393 } else {
10394 set bend $b
10396 if {$a eq $bend} {
10397 return 1
10399 if {$b eq $aend} {
10400 return -1
10402 if {[info exists cached_isanc($a,$bend)]} {
10403 if {$cached_isanc($a,$bend)} {
10404 return 1
10407 if {[info exists cached_isanc($b,$aend)]} {
10408 if {$cached_isanc($b,$aend)} {
10409 return -1
10411 if {[info exists cached_isanc($a,$bend)]} {
10412 return 0
10416 set todo [list $a $b]
10417 set anc($a) a
10418 set anc($b) b
10419 for {set i 0} {$i < [llength $todo]} {incr i} {
10420 set x [lindex $todo $i]
10421 if {$anc($x) eq {}} {
10422 continue
10424 foreach arc $arcnos($x) {
10425 set xd $arcstart($arc)
10426 if {$xd eq $bend} {
10427 set cached_isanc($a,$bend) 1
10428 set cached_isanc($b,$aend) 0
10429 return 1
10430 } elseif {$xd eq $aend} {
10431 set cached_isanc($b,$aend) 1
10432 set cached_isanc($a,$bend) 0
10433 return -1
10435 if {![info exists anc($xd)]} {
10436 set anc($xd) $anc($x)
10437 lappend todo $xd
10438 } elseif {$anc($xd) ne $anc($x)} {
10439 set anc($xd) {}
10443 set cached_isanc($a,$bend) 0
10444 set cached_isanc($b,$aend) 0
10445 return 0
10448 # This identifies whether $desc has an ancestor that is
10449 # a growing tip of the graph and which is not an ancestor of $anc
10450 # and returns 0 if so and 1 if not.
10451 # If we subsequently discover a tag on such a growing tip, and that
10452 # turns out to be a descendent of $anc (which it could, since we
10453 # don't necessarily see children before parents), then $desc
10454 # isn't a good choice to display as a descendent tag of
10455 # $anc (since it is the descendent of another tag which is
10456 # a descendent of $anc). Similarly, $anc isn't a good choice to
10457 # display as a ancestor tag of $desc.
10459 proc is_certain {desc anc} {
10460 global arcnos arcout arcstart arcend growing problems
10462 set certain {}
10463 if {[llength $arcnos($anc)] == 1} {
10464 # tags on the same arc are certain
10465 if {$arcnos($desc) eq $arcnos($anc)} {
10466 return 1
10468 if {![info exists arcout($anc)]} {
10469 # if $anc is partway along an arc, use the start of the arc instead
10470 set a [lindex $arcnos($anc) 0]
10471 set anc $arcstart($a)
10474 if {[llength $arcnos($desc)] > 1 || [info exists arcout($desc)]} {
10475 set x $desc
10476 } else {
10477 set a [lindex $arcnos($desc) 0]
10478 set x $arcend($a)
10480 if {$x == $anc} {
10481 return 1
10483 set anclist [list $x]
10484 set dl($x) 1
10485 set nnh 1
10486 set ngrowanc 0
10487 for {set i 0} {$i < [llength $anclist] && ($nnh > 0 || $ngrowanc > 0)} {incr i} {
10488 set x [lindex $anclist $i]
10489 if {$dl($x)} {
10490 incr nnh -1
10492 set done($x) 1
10493 foreach a $arcout($x) {
10494 if {[info exists growing($a)]} {
10495 if {![info exists growanc($x)] && $dl($x)} {
10496 set growanc($x) 1
10497 incr ngrowanc
10499 } else {
10500 set y $arcend($a)
10501 if {[info exists dl($y)]} {
10502 if {$dl($y)} {
10503 if {!$dl($x)} {
10504 set dl($y) 0
10505 if {![info exists done($y)]} {
10506 incr nnh -1
10508 if {[info exists growanc($x)]} {
10509 incr ngrowanc -1
10511 set xl [list $y]
10512 for {set k 0} {$k < [llength $xl]} {incr k} {
10513 set z [lindex $xl $k]
10514 foreach c $arcout($z) {
10515 if {[info exists arcend($c)]} {
10516 set v $arcend($c)
10517 if {[info exists dl($v)] && $dl($v)} {
10518 set dl($v) 0
10519 if {![info exists done($v)]} {
10520 incr nnh -1
10522 if {[info exists growanc($v)]} {
10523 incr ngrowanc -1
10525 lappend xl $v
10532 } elseif {$y eq $anc || !$dl($x)} {
10533 set dl($y) 0
10534 lappend anclist $y
10535 } else {
10536 set dl($y) 1
10537 lappend anclist $y
10538 incr nnh
10543 foreach x [array names growanc] {
10544 if {$dl($x)} {
10545 return 0
10547 return 0
10549 return 1
10552 proc validate_arctags {a} {
10553 global arctags idtags
10555 set i -1
10556 set na $arctags($a)
10557 foreach id $arctags($a) {
10558 incr i
10559 if {![info exists idtags($id)]} {
10560 set na [lreplace $na $i $i]
10561 incr i -1
10564 set arctags($a) $na
10567 proc validate_archeads {a} {
10568 global archeads idheads
10570 set i -1
10571 set na $archeads($a)
10572 foreach id $archeads($a) {
10573 incr i
10574 if {![info exists idheads($id)]} {
10575 set na [lreplace $na $i $i]
10576 incr i -1
10579 set archeads($a) $na
10582 # Return the list of IDs that have tags that are descendents of id,
10583 # ignoring IDs that are descendents of IDs already reported.
10584 proc desctags {id} {
10585 global arcnos arcstart arcids arctags idtags allparents
10586 global growing cached_dtags
10588 if {![info exists allparents($id)]} {
10589 return {}
10591 set t1 [clock clicks -milliseconds]
10592 set argid $id
10593 if {[llength $arcnos($id)] == 1 && [llength $allparents($id)] == 1} {
10594 # part-way along an arc; check that arc first
10595 set a [lindex $arcnos($id) 0]
10596 if {$arctags($a) ne {}} {
10597 validate_arctags $a
10598 set i [lsearch -exact $arcids($a) $id]
10599 set tid {}
10600 foreach t $arctags($a) {
10601 set j [lsearch -exact $arcids($a) $t]
10602 if {$j >= $i} break
10603 set tid $t
10605 if {$tid ne {}} {
10606 return $tid
10609 set id $arcstart($a)
10610 if {[info exists idtags($id)]} {
10611 return $id
10614 if {[info exists cached_dtags($id)]} {
10615 return $cached_dtags($id)
10618 set origid $id
10619 set todo [list $id]
10620 set queued($id) 1
10621 set nc 1
10622 for {set i 0} {$i < [llength $todo] && $nc > 0} {incr i} {
10623 set id [lindex $todo $i]
10624 set done($id) 1
10625 set ta [info exists hastaggedancestor($id)]
10626 if {!$ta} {
10627 incr nc -1
10629 # ignore tags on starting node
10630 if {!$ta && $i > 0} {
10631 if {[info exists idtags($id)]} {
10632 set tagloc($id) $id
10633 set ta 1
10634 } elseif {[info exists cached_dtags($id)]} {
10635 set tagloc($id) $cached_dtags($id)
10636 set ta 1
10639 foreach a $arcnos($id) {
10640 set d $arcstart($a)
10641 if {!$ta && $arctags($a) ne {}} {
10642 validate_arctags $a
10643 if {$arctags($a) ne {}} {
10644 lappend tagloc($id) [lindex $arctags($a) end]
10647 if {$ta || $arctags($a) ne {}} {
10648 set tomark [list $d]
10649 for {set j 0} {$j < [llength $tomark]} {incr j} {
10650 set dd [lindex $tomark $j]
10651 if {![info exists hastaggedancestor($dd)]} {
10652 if {[info exists done($dd)]} {
10653 foreach b $arcnos($dd) {
10654 lappend tomark $arcstart($b)
10656 if {[info exists tagloc($dd)]} {
10657 unset tagloc($dd)
10659 } elseif {[info exists queued($dd)]} {
10660 incr nc -1
10662 set hastaggedancestor($dd) 1
10666 if {![info exists queued($d)]} {
10667 lappend todo $d
10668 set queued($d) 1
10669 if {![info exists hastaggedancestor($d)]} {
10670 incr nc
10675 set tags {}
10676 foreach id [array names tagloc] {
10677 if {![info exists hastaggedancestor($id)]} {
10678 foreach t $tagloc($id) {
10679 if {[lsearch -exact $tags $t] < 0} {
10680 lappend tags $t
10685 set t2 [clock clicks -milliseconds]
10686 set loopix $i
10688 # remove tags that are descendents of other tags
10689 for {set i 0} {$i < [llength $tags]} {incr i} {
10690 set a [lindex $tags $i]
10691 for {set j 0} {$j < $i} {incr j} {
10692 set b [lindex $tags $j]
10693 set r [anc_or_desc $a $b]
10694 if {$r == 1} {
10695 set tags [lreplace $tags $j $j]
10696 incr j -1
10697 incr i -1
10698 } elseif {$r == -1} {
10699 set tags [lreplace $tags $i $i]
10700 incr i -1
10701 break
10706 if {[array names growing] ne {}} {
10707 # graph isn't finished, need to check if any tag could get
10708 # eclipsed by another tag coming later. Simply ignore any
10709 # tags that could later get eclipsed.
10710 set ctags {}
10711 foreach t $tags {
10712 if {[is_certain $t $origid]} {
10713 lappend ctags $t
10716 if {$tags eq $ctags} {
10717 set cached_dtags($origid) $tags
10718 } else {
10719 set tags $ctags
10721 } else {
10722 set cached_dtags($origid) $tags
10724 set t3 [clock clicks -milliseconds]
10725 if {0 && $t3 - $t1 >= 100} {
10726 puts "iterating descendents ($loopix/[llength $todo] nodes) took\
10727 [expr {$t2-$t1}]+[expr {$t3-$t2}]ms, $nc candidates left"
10729 return $tags
10732 proc anctags {id} {
10733 global arcnos arcids arcout arcend arctags idtags allparents
10734 global growing cached_atags
10736 if {![info exists allparents($id)]} {
10737 return {}
10739 set t1 [clock clicks -milliseconds]
10740 set argid $id
10741 if {[llength $arcnos($id)] == 1 && [llength $allparents($id)] == 1} {
10742 # part-way along an arc; check that arc first
10743 set a [lindex $arcnos($id) 0]
10744 if {$arctags($a) ne {}} {
10745 validate_arctags $a
10746 set i [lsearch -exact $arcids($a) $id]
10747 foreach t $arctags($a) {
10748 set j [lsearch -exact $arcids($a) $t]
10749 if {$j > $i} {
10750 return $t
10754 if {![info exists arcend($a)]} {
10755 return {}
10757 set id $arcend($a)
10758 if {[info exists idtags($id)]} {
10759 return $id
10762 if {[info exists cached_atags($id)]} {
10763 return $cached_atags($id)
10766 set origid $id
10767 set todo [list $id]
10768 set queued($id) 1
10769 set taglist {}
10770 set nc 1
10771 for {set i 0} {$i < [llength $todo] && $nc > 0} {incr i} {
10772 set id [lindex $todo $i]
10773 set done($id) 1
10774 set td [info exists hastaggeddescendent($id)]
10775 if {!$td} {
10776 incr nc -1
10778 # ignore tags on starting node
10779 if {!$td && $i > 0} {
10780 if {[info exists idtags($id)]} {
10781 set tagloc($id) $id
10782 set td 1
10783 } elseif {[info exists cached_atags($id)]} {
10784 set tagloc($id) $cached_atags($id)
10785 set td 1
10788 foreach a $arcout($id) {
10789 if {!$td && $arctags($a) ne {}} {
10790 validate_arctags $a
10791 if {$arctags($a) ne {}} {
10792 lappend tagloc($id) [lindex $arctags($a) 0]
10795 if {![info exists arcend($a)]} continue
10796 set d $arcend($a)
10797 if {$td || $arctags($a) ne {}} {
10798 set tomark [list $d]
10799 for {set j 0} {$j < [llength $tomark]} {incr j} {
10800 set dd [lindex $tomark $j]
10801 if {![info exists hastaggeddescendent($dd)]} {
10802 if {[info exists done($dd)]} {
10803 foreach b $arcout($dd) {
10804 if {[info exists arcend($b)]} {
10805 lappend tomark $arcend($b)
10808 if {[info exists tagloc($dd)]} {
10809 unset tagloc($dd)
10811 } elseif {[info exists queued($dd)]} {
10812 incr nc -1
10814 set hastaggeddescendent($dd) 1
10818 if {![info exists queued($d)]} {
10819 lappend todo $d
10820 set queued($d) 1
10821 if {![info exists hastaggeddescendent($d)]} {
10822 incr nc
10827 set t2 [clock clicks -milliseconds]
10828 set loopix $i
10829 set tags {}
10830 foreach id [array names tagloc] {
10831 if {![info exists hastaggeddescendent($id)]} {
10832 foreach t $tagloc($id) {
10833 if {[lsearch -exact $tags $t] < 0} {
10834 lappend tags $t
10840 # remove tags that are ancestors of other tags
10841 for {set i 0} {$i < [llength $tags]} {incr i} {
10842 set a [lindex $tags $i]
10843 for {set j 0} {$j < $i} {incr j} {
10844 set b [lindex $tags $j]
10845 set r [anc_or_desc $a $b]
10846 if {$r == -1} {
10847 set tags [lreplace $tags $j $j]
10848 incr j -1
10849 incr i -1
10850 } elseif {$r == 1} {
10851 set tags [lreplace $tags $i $i]
10852 incr i -1
10853 break
10858 if {[array names growing] ne {}} {
10859 # graph isn't finished, need to check if any tag could get
10860 # eclipsed by another tag coming later. Simply ignore any
10861 # tags that could later get eclipsed.
10862 set ctags {}
10863 foreach t $tags {
10864 if {[is_certain $origid $t]} {
10865 lappend ctags $t
10868 if {$tags eq $ctags} {
10869 set cached_atags($origid) $tags
10870 } else {
10871 set tags $ctags
10873 } else {
10874 set cached_atags($origid) $tags
10876 set t3 [clock clicks -milliseconds]
10877 if {0 && $t3 - $t1 >= 100} {
10878 puts "iterating ancestors ($loopix/[llength $todo] nodes) took\
10879 [expr {$t2-$t1}]+[expr {$t3-$t2}]ms, $nc candidates left"
10881 return $tags
10884 # Return the list of IDs that have heads that are descendents of id,
10885 # including id itself if it has a head.
10886 proc descheads {id} {
10887 global arcnos arcstart arcids archeads idheads cached_dheads
10888 global allparents arcout
10890 if {![info exists allparents($id)]} {
10891 return {}
10893 set aret {}
10894 if {![info exists arcout($id)]} {
10895 # part-way along an arc; check it first
10896 set a [lindex $arcnos($id) 0]
10897 if {$archeads($a) ne {}} {
10898 validate_archeads $a
10899 set i [lsearch -exact $arcids($a) $id]
10900 foreach t $archeads($a) {
10901 set j [lsearch -exact $arcids($a) $t]
10902 if {$j > $i} break
10903 lappend aret $t
10906 set id $arcstart($a)
10908 set origid $id
10909 set todo [list $id]
10910 set seen($id) 1
10911 set ret {}
10912 for {set i 0} {$i < [llength $todo]} {incr i} {
10913 set id [lindex $todo $i]
10914 if {[info exists cached_dheads($id)]} {
10915 set ret [concat $ret $cached_dheads($id)]
10916 } else {
10917 if {[info exists idheads($id)]} {
10918 lappend ret $id
10920 foreach a $arcnos($id) {
10921 if {$archeads($a) ne {}} {
10922 validate_archeads $a
10923 if {$archeads($a) ne {}} {
10924 set ret [concat $ret $archeads($a)]
10927 set d $arcstart($a)
10928 if {![info exists seen($d)]} {
10929 lappend todo $d
10930 set seen($d) 1
10935 set ret [lsort -unique $ret]
10936 set cached_dheads($origid) $ret
10937 return [concat $ret $aret]
10940 proc addedtag {id} {
10941 global arcnos arcout cached_dtags cached_atags
10943 if {![info exists arcnos($id)]} return
10944 if {![info exists arcout($id)]} {
10945 recalcarc [lindex $arcnos($id) 0]
10947 catch {unset cached_dtags}
10948 catch {unset cached_atags}
10951 proc addedhead {hid head} {
10952 global arcnos arcout cached_dheads
10954 if {![info exists arcnos($hid)]} return
10955 if {![info exists arcout($hid)]} {
10956 recalcarc [lindex $arcnos($hid) 0]
10958 catch {unset cached_dheads}
10961 proc removedhead {hid head} {
10962 global cached_dheads
10964 catch {unset cached_dheads}
10967 proc movedhead {hid head} {
10968 global arcnos arcout cached_dheads
10970 if {![info exists arcnos($hid)]} return
10971 if {![info exists arcout($hid)]} {
10972 recalcarc [lindex $arcnos($hid) 0]
10974 catch {unset cached_dheads}
10977 proc changedrefs {} {
10978 global cached_dheads cached_dtags cached_atags cached_tagcontent
10979 global arctags archeads arcnos arcout idheads idtags
10981 foreach id [concat [array names idheads] [array names idtags]] {
10982 if {[info exists arcnos($id)] && ![info exists arcout($id)]} {
10983 set a [lindex $arcnos($id) 0]
10984 if {![info exists donearc($a)]} {
10985 recalcarc $a
10986 set donearc($a) 1
10990 catch {unset cached_tagcontent}
10991 catch {unset cached_dtags}
10992 catch {unset cached_atags}
10993 catch {unset cached_dheads}
10996 proc rereadrefs {} {
10997 global idtags idheads idotherrefs mainheadid
10999 set refids [concat [array names idtags] \
11000 [array names idheads] [array names idotherrefs]]
11001 foreach id $refids {
11002 if {![info exists ref($id)]} {
11003 set ref($id) [listrefs $id]
11006 set oldmainhead $mainheadid
11007 readrefs
11008 changedrefs
11009 set refids [lsort -unique [concat $refids [array names idtags] \
11010 [array names idheads] [array names idotherrefs]]]
11011 foreach id $refids {
11012 set v [listrefs $id]
11013 if {![info exists ref($id)] || $ref($id) != $v} {
11014 redrawtags $id
11017 if {$oldmainhead ne $mainheadid} {
11018 redrawtags $oldmainhead
11019 redrawtags $mainheadid
11021 run refill_reflist
11024 proc listrefs {id} {
11025 global idtags idheads idotherrefs
11027 set x {}
11028 if {[info exists idtags($id)]} {
11029 set x $idtags($id)
11031 set y {}
11032 if {[info exists idheads($id)]} {
11033 set y $idheads($id)
11035 set z {}
11036 if {[info exists idotherrefs($id)]} {
11037 set z $idotherrefs($id)
11039 return [list $x $y $z]
11042 proc add_tag_ctext {tag} {
11043 global ctext cached_tagcontent tagids
11045 if {![info exists cached_tagcontent($tag)]} {
11046 catch {
11047 set cached_tagcontent($tag) [exec git cat-file -p $tag]
11050 $ctext insert end "[mc "Tag"]: $tag\n" bold
11051 if {[info exists cached_tagcontent($tag)]} {
11052 set text $cached_tagcontent($tag)
11053 } else {
11054 set text "[mc "Id"]: $tagids($tag)"
11056 appendwithlinks $text {}
11059 proc showtag {tag isnew} {
11060 global ctext cached_tagcontent tagids linknum tagobjid
11062 if {$isnew} {
11063 addtohistory [list showtag $tag 0] savectextpos
11065 $ctext conf -state normal
11066 clear_ctext
11067 settabs 0
11068 set linknum 0
11069 add_tag_ctext $tag
11070 maybe_scroll_ctext 1
11071 $ctext conf -state disabled
11072 init_flist {}
11075 proc showtags {id isnew} {
11076 global idtags ctext linknum
11078 if {$isnew} {
11079 addtohistory [list showtags $id 0] savectextpos
11081 $ctext conf -state normal
11082 clear_ctext
11083 settabs 0
11084 set linknum 0
11085 set sep {}
11086 foreach tag $idtags($id) {
11087 $ctext insert end $sep
11088 add_tag_ctext $tag
11089 set sep "\n\n"
11091 maybe_scroll_ctext 1
11092 $ctext conf -state disabled
11093 init_flist {}
11096 proc doquit {} {
11097 global stopped
11098 global gitktmpdir
11100 set stopped 100
11101 savestuff .
11102 destroy .
11104 if {[info exists gitktmpdir]} {
11105 catch {file delete -force $gitktmpdir}
11109 proc mkfontdisp {font top which} {
11110 global fontattr fontpref $font NS use_ttk
11112 set fontpref($font) [set $font]
11113 ${NS}::button $top.${font}but -text $which \
11114 -command [list choosefont $font $which]
11115 ${NS}::label $top.$font -relief flat -font $font \
11116 -text $fontattr($font,family) -justify left
11117 grid x $top.${font}but $top.$font -sticky w
11120 proc choosefont {font which} {
11121 global fontparam fontlist fonttop fontattr
11122 global prefstop NS
11124 set fontparam(which) $which
11125 set fontparam(font) $font
11126 set fontparam(family) [font actual $font -family]
11127 set fontparam(size) $fontattr($font,size)
11128 set fontparam(weight) $fontattr($font,weight)
11129 set fontparam(slant) $fontattr($font,slant)
11130 set top .gitkfont
11131 set fonttop $top
11132 if {![winfo exists $top]} {
11133 font create sample
11134 eval font config sample [font actual $font]
11135 ttk_toplevel $top
11136 make_transient $top $prefstop
11137 wm title $top [mc "Gitk font chooser"]
11138 ${NS}::label $top.l -textvariable fontparam(which)
11139 pack $top.l -side top
11140 set fontlist [lsort [font families]]
11141 ${NS}::frame $top.f
11142 listbox $top.f.fam -listvariable fontlist \
11143 -yscrollcommand [list $top.f.sb set]
11144 bind $top.f.fam <<ListboxSelect>> selfontfam
11145 ${NS}::scrollbar $top.f.sb -command [list $top.f.fam yview]
11146 pack $top.f.sb -side right -fill y
11147 pack $top.f.fam -side left -fill both -expand 1
11148 pack $top.f -side top -fill both -expand 1
11149 ${NS}::frame $top.g
11150 spinbox $top.g.size -from 4 -to 40 -width 4 \
11151 -textvariable fontparam(size) \
11152 -validatecommand {string is integer -strict %s}
11153 checkbutton $top.g.bold -padx 5 \
11154 -font {{Times New Roman} 12 bold} -text [mc "B"] -indicatoron 0 \
11155 -variable fontparam(weight) -onvalue bold -offvalue normal
11156 checkbutton $top.g.ital -padx 5 \
11157 -font {{Times New Roman} 12 italic} -text [mc "I"] -indicatoron 0 \
11158 -variable fontparam(slant) -onvalue italic -offvalue roman
11159 pack $top.g.size $top.g.bold $top.g.ital -side left
11160 pack $top.g -side top
11161 canvas $top.c -width 150 -height 50 -border 2 -relief sunk \
11162 -background white
11163 $top.c create text 100 25 -anchor center -text $which -font sample \
11164 -fill black -tags text
11165 bind $top.c <Configure> [list centertext $top.c]
11166 pack $top.c -side top -fill x
11167 ${NS}::frame $top.buts
11168 ${NS}::button $top.buts.ok -text [mc "OK"] -command fontok -default active
11169 ${NS}::button $top.buts.can -text [mc "Cancel"] -command fontcan -default normal
11170 bind $top <Key-Return> fontok
11171 bind $top <Key-Escape> fontcan
11172 grid $top.buts.ok $top.buts.can
11173 grid columnconfigure $top.buts 0 -weight 1 -uniform a
11174 grid columnconfigure $top.buts 1 -weight 1 -uniform a
11175 pack $top.buts -side bottom -fill x
11176 trace add variable fontparam write chg_fontparam
11177 } else {
11178 raise $top
11179 $top.c itemconf text -text $which
11181 set i [lsearch -exact $fontlist $fontparam(family)]
11182 if {$i >= 0} {
11183 $top.f.fam selection set $i
11184 $top.f.fam see $i
11188 proc centertext {w} {
11189 $w coords text [expr {[winfo width $w] / 2}] [expr {[winfo height $w] / 2}]
11192 proc fontok {} {
11193 global fontparam fontpref prefstop
11195 set f $fontparam(font)
11196 set fontpref($f) [list $fontparam(family) $fontparam(size)]
11197 if {$fontparam(weight) eq "bold"} {
11198 lappend fontpref($f) "bold"
11200 if {$fontparam(slant) eq "italic"} {
11201 lappend fontpref($f) "italic"
11203 set w $prefstop.notebook.fonts.$f
11204 $w conf -text $fontparam(family) -font $fontpref($f)
11206 fontcan
11209 proc fontcan {} {
11210 global fonttop fontparam
11212 if {[info exists fonttop]} {
11213 catch {destroy $fonttop}
11214 catch {font delete sample}
11215 unset fonttop
11216 unset fontparam
11220 if {[package vsatisfies [package provide Tk] 8.6]} {
11221 # In Tk 8.6 we have a native font chooser dialog. Overwrite the above
11222 # function to make use of it.
11223 proc choosefont {font which} {
11224 tk fontchooser configure -title $which -font $font \
11225 -command [list on_choosefont $font $which]
11226 tk fontchooser show
11228 proc on_choosefont {font which newfont} {
11229 global fontparam
11230 puts stderr "$font $newfont"
11231 array set f [font actual $newfont]
11232 set fontparam(which) $which
11233 set fontparam(font) $font
11234 set fontparam(family) $f(-family)
11235 set fontparam(size) $f(-size)
11236 set fontparam(weight) $f(-weight)
11237 set fontparam(slant) $f(-slant)
11238 fontok
11242 proc selfontfam {} {
11243 global fonttop fontparam
11245 set i [$fonttop.f.fam curselection]
11246 if {$i ne {}} {
11247 set fontparam(family) [$fonttop.f.fam get $i]
11251 proc chg_fontparam {v sub op} {
11252 global fontparam
11254 font config sample -$sub $fontparam($sub)
11257 # Create a property sheet tab page
11258 proc create_prefs_page {w} {
11259 global NS
11260 set parent [join [lrange [split $w .] 0 end-1] .]
11261 if {[winfo class $parent] eq "TNotebook"} {
11262 ${NS}::frame $w
11263 } else {
11264 ${NS}::labelframe $w
11268 proc prefspage_general {notebook} {
11269 global NS maxwidth maxgraphpct showneartags showlocalchanges
11270 global tabstop limitdiffs autoselect autosellen extdifftool perfile_attrs
11271 global hideremotes want_ttk have_ttk maxrefs
11273 set page [create_prefs_page $notebook.general]
11275 ${NS}::label $page.ldisp -text [mc "Commit list display options"]
11276 grid $page.ldisp - -sticky w -pady 10
11277 ${NS}::label $page.spacer -text " "
11278 ${NS}::label $page.maxwidthl -text [mc "Maximum graph width (lines)"]
11279 spinbox $page.maxwidth -from 0 -to 100 -width 4 -textvariable maxwidth
11280 grid $page.spacer $page.maxwidthl $page.maxwidth -sticky w
11281 ${NS}::label $page.maxpctl -text [mc "Maximum graph width (% of pane)"]
11282 spinbox $page.maxpct -from 1 -to 100 -width 4 -textvariable maxgraphpct
11283 grid x $page.maxpctl $page.maxpct -sticky w
11284 ${NS}::checkbutton $page.showlocal -text [mc "Show local changes"] \
11285 -variable showlocalchanges
11286 grid x $page.showlocal -sticky w
11287 ${NS}::checkbutton $page.autoselect -text [mc "Auto-select SHA1 (length)"] \
11288 -variable autoselect
11289 spinbox $page.autosellen -from 1 -to 40 -width 4 -textvariable autosellen
11290 grid x $page.autoselect $page.autosellen -sticky w
11291 ${NS}::checkbutton $page.hideremotes -text [mc "Hide remote refs"] \
11292 -variable hideremotes
11293 grid x $page.hideremotes -sticky w
11295 ${NS}::label $page.ddisp -text [mc "Diff display options"]
11296 grid $page.ddisp - -sticky w -pady 10
11297 ${NS}::label $page.tabstopl -text [mc "Tab spacing"]
11298 spinbox $page.tabstop -from 1 -to 20 -width 4 -textvariable tabstop
11299 grid x $page.tabstopl $page.tabstop -sticky w
11300 ${NS}::checkbutton $page.ntag -text [mc "Display nearby tags/heads"] \
11301 -variable showneartags
11302 grid x $page.ntag -sticky w
11303 ${NS}::label $page.maxrefsl -text [mc "Maximum # tags/heads to show"]
11304 spinbox $page.maxrefs -from 1 -to 1000 -width 4 -textvariable maxrefs
11305 grid x $page.maxrefsl $page.maxrefs -sticky w
11306 ${NS}::checkbutton $page.ldiff -text [mc "Limit diffs to listed paths"] \
11307 -variable limitdiffs
11308 grid x $page.ldiff -sticky w
11309 ${NS}::checkbutton $page.lattr -text [mc "Support per-file encodings"] \
11310 -variable perfile_attrs
11311 grid x $page.lattr -sticky w
11313 ${NS}::entry $page.extdifft -textvariable extdifftool
11314 ${NS}::frame $page.extdifff
11315 ${NS}::label $page.extdifff.l -text [mc "External diff tool" ]
11316 ${NS}::button $page.extdifff.b -text [mc "Choose..."] -command choose_extdiff
11317 pack $page.extdifff.l $page.extdifff.b -side left
11318 pack configure $page.extdifff.l -padx 10
11319 grid x $page.extdifff $page.extdifft -sticky ew
11321 ${NS}::label $page.lgen -text [mc "General options"]
11322 grid $page.lgen - -sticky w -pady 10
11323 ${NS}::checkbutton $page.want_ttk -variable want_ttk \
11324 -text [mc "Use themed widgets"]
11325 if {$have_ttk} {
11326 ${NS}::label $page.ttk_note -text [mc "(change requires restart)"]
11327 } else {
11328 ${NS}::label $page.ttk_note -text [mc "(currently unavailable)"]
11330 grid x $page.want_ttk $page.ttk_note -sticky w
11331 return $page
11334 proc prefspage_colors {notebook} {
11335 global NS uicolor bgcolor fgcolor ctext diffcolors selectbgcolor markbgcolor
11337 set page [create_prefs_page $notebook.colors]
11339 ${NS}::label $page.cdisp -text [mc "Colors: press to choose"]
11340 grid $page.cdisp - -sticky w -pady 10
11341 label $page.ui -padx 40 -relief sunk -background $uicolor
11342 ${NS}::button $page.uibut -text [mc "Interface"] \
11343 -command [list choosecolor uicolor {} $page.ui [mc "interface"] setui]
11344 grid x $page.uibut $page.ui -sticky w
11345 label $page.bg -padx 40 -relief sunk -background $bgcolor
11346 ${NS}::button $page.bgbut -text [mc "Background"] \
11347 -command [list choosecolor bgcolor {} $page.bg [mc "background"] setbg]
11348 grid x $page.bgbut $page.bg -sticky w
11349 label $page.fg -padx 40 -relief sunk -background $fgcolor
11350 ${NS}::button $page.fgbut -text [mc "Foreground"] \
11351 -command [list choosecolor fgcolor {} $page.fg [mc "foreground"] setfg]
11352 grid x $page.fgbut $page.fg -sticky w
11353 label $page.diffold -padx 40 -relief sunk -background [lindex $diffcolors 0]
11354 ${NS}::button $page.diffoldbut -text [mc "Diff: old lines"] \
11355 -command [list choosecolor diffcolors 0 $page.diffold [mc "diff old lines"] \
11356 [list $ctext tag conf d0 -foreground]]
11357 grid x $page.diffoldbut $page.diffold -sticky w
11358 label $page.diffnew -padx 40 -relief sunk -background [lindex $diffcolors 1]
11359 ${NS}::button $page.diffnewbut -text [mc "Diff: new lines"] \
11360 -command [list choosecolor diffcolors 1 $page.diffnew [mc "diff new lines"] \
11361 [list $ctext tag conf dresult -foreground]]
11362 grid x $page.diffnewbut $page.diffnew -sticky w
11363 label $page.hunksep -padx 40 -relief sunk -background [lindex $diffcolors 2]
11364 ${NS}::button $page.hunksepbut -text [mc "Diff: hunk header"] \
11365 -command [list choosecolor diffcolors 2 $page.hunksep \
11366 [mc "diff hunk header"] \
11367 [list $ctext tag conf hunksep -foreground]]
11368 grid x $page.hunksepbut $page.hunksep -sticky w
11369 label $page.markbgsep -padx 40 -relief sunk -background $markbgcolor
11370 ${NS}::button $page.markbgbut -text [mc "Marked line bg"] \
11371 -command [list choosecolor markbgcolor {} $page.markbgsep \
11372 [mc "marked line background"] \
11373 [list $ctext tag conf omark -background]]
11374 grid x $page.markbgbut $page.markbgsep -sticky w
11375 label $page.selbgsep -padx 40 -relief sunk -background $selectbgcolor
11376 ${NS}::button $page.selbgbut -text [mc "Select bg"] \
11377 -command [list choosecolor selectbgcolor {} $page.selbgsep [mc "background"] setselbg]
11378 grid x $page.selbgbut $page.selbgsep -sticky w
11379 return $page
11382 proc prefspage_fonts {notebook} {
11383 global NS
11384 set page [create_prefs_page $notebook.fonts]
11385 ${NS}::label $page.cfont -text [mc "Fonts: press to choose"]
11386 grid $page.cfont - -sticky w -pady 10
11387 mkfontdisp mainfont $page [mc "Main font"]
11388 mkfontdisp textfont $page [mc "Diff display font"]
11389 mkfontdisp uifont $page [mc "User interface font"]
11390 return $page
11393 proc doprefs {} {
11394 global maxwidth maxgraphpct use_ttk NS
11395 global oldprefs prefstop showneartags showlocalchanges
11396 global uicolor bgcolor fgcolor ctext diffcolors selectbgcolor markbgcolor
11397 global tabstop limitdiffs autoselect autosellen extdifftool perfile_attrs
11398 global hideremotes want_ttk have_ttk
11400 set top .gitkprefs
11401 set prefstop $top
11402 if {[winfo exists $top]} {
11403 raise $top
11404 return
11406 foreach v {maxwidth maxgraphpct showneartags showlocalchanges \
11407 limitdiffs tabstop perfile_attrs hideremotes want_ttk} {
11408 set oldprefs($v) [set $v]
11410 ttk_toplevel $top
11411 wm title $top [mc "Gitk preferences"]
11412 make_transient $top .
11414 if {[set use_notebook [expr {$use_ttk && [info command ::ttk::notebook] ne ""}]]} {
11415 set notebook [ttk::notebook $top.notebook]
11416 } else {
11417 set notebook [${NS}::frame $top.notebook -borderwidth 0 -relief flat]
11420 lappend pages [prefspage_general $notebook] [mc "General"]
11421 lappend pages [prefspage_colors $notebook] [mc "Colors"]
11422 lappend pages [prefspage_fonts $notebook] [mc "Fonts"]
11423 set col 0
11424 foreach {page title} $pages {
11425 if {$use_notebook} {
11426 $notebook add $page -text $title
11427 } else {
11428 set btn [${NS}::button $notebook.b_[string map {. X} $page] \
11429 -text $title -command [list raise $page]]
11430 $page configure -text $title
11431 grid $btn -row 0 -column [incr col] -sticky w
11432 grid $page -row 1 -column 0 -sticky news -columnspan 100
11436 if {!$use_notebook} {
11437 grid columnconfigure $notebook 0 -weight 1
11438 grid rowconfigure $notebook 1 -weight 1
11439 raise [lindex $pages 0]
11442 grid $notebook -sticky news -padx 2 -pady 2
11443 grid rowconfigure $top 0 -weight 1
11444 grid columnconfigure $top 0 -weight 1
11446 ${NS}::frame $top.buts
11447 ${NS}::button $top.buts.ok -text [mc "OK"] -command prefsok -default active
11448 ${NS}::button $top.buts.can -text [mc "Cancel"] -command prefscan -default normal
11449 bind $top <Key-Return> prefsok
11450 bind $top <Key-Escape> prefscan
11451 grid $top.buts.ok $top.buts.can
11452 grid columnconfigure $top.buts 0 -weight 1 -uniform a
11453 grid columnconfigure $top.buts 1 -weight 1 -uniform a
11454 grid $top.buts - - -pady 10 -sticky ew
11455 grid columnconfigure $top 2 -weight 1
11456 bind $top <Visibility> [list focus $top.buts.ok]
11459 proc choose_extdiff {} {
11460 global extdifftool
11462 set prog [tk_getOpenFile -title [mc "External diff tool"] -multiple false]
11463 if {$prog ne {}} {
11464 set extdifftool $prog
11468 proc choosecolor {v vi w x cmd} {
11469 global $v
11471 set c [tk_chooseColor -initialcolor [lindex [set $v] $vi] \
11472 -title [mc "Gitk: choose color for %s" $x]]
11473 if {$c eq {}} return
11474 $w conf -background $c
11475 lset $v $vi $c
11476 eval $cmd $c
11479 proc setselbg {c} {
11480 global bglist cflist
11481 foreach w $bglist {
11482 $w configure -selectbackground $c
11484 $cflist tag configure highlight \
11485 -background [$cflist cget -selectbackground]
11486 allcanvs itemconf secsel -fill $c
11489 # This sets the background color and the color scheme for the whole UI.
11490 # For some reason, tk_setPalette chooses a nasty dark red for selectColor
11491 # if we don't specify one ourselves, which makes the checkbuttons and
11492 # radiobuttons look bad. This chooses white for selectColor if the
11493 # background color is light, or black if it is dark.
11494 proc setui {c} {
11495 if {[tk windowingsystem] eq "win32"} { return }
11496 set bg [winfo rgb . $c]
11497 set selc black
11498 if {[lindex $bg 0] + 1.5 * [lindex $bg 1] + 0.5 * [lindex $bg 2] > 100000} {
11499 set selc white
11501 tk_setPalette background $c selectColor $selc
11504 proc setbg {c} {
11505 global bglist
11507 foreach w $bglist {
11508 $w conf -background $c
11512 proc setfg {c} {
11513 global fglist canv
11515 foreach w $fglist {
11516 $w conf -foreground $c
11518 allcanvs itemconf text -fill $c
11519 $canv itemconf circle -outline $c
11520 $canv itemconf markid -outline $c
11523 proc prefscan {} {
11524 global oldprefs prefstop
11526 foreach v {maxwidth maxgraphpct showneartags showlocalchanges \
11527 limitdiffs tabstop perfile_attrs hideremotes want_ttk} {
11528 global $v
11529 set $v $oldprefs($v)
11531 catch {destroy $prefstop}
11532 unset prefstop
11533 fontcan
11536 proc prefsok {} {
11537 global maxwidth maxgraphpct
11538 global oldprefs prefstop showneartags showlocalchanges
11539 global fontpref mainfont textfont uifont
11540 global limitdiffs treediffs perfile_attrs
11541 global hideremotes
11543 catch {destroy $prefstop}
11544 unset prefstop
11545 fontcan
11546 set fontchanged 0
11547 if {$mainfont ne $fontpref(mainfont)} {
11548 set mainfont $fontpref(mainfont)
11549 parsefont mainfont $mainfont
11550 eval font configure mainfont [fontflags mainfont]
11551 eval font configure mainfontbold [fontflags mainfont 1]
11552 setcoords
11553 set fontchanged 1
11555 if {$textfont ne $fontpref(textfont)} {
11556 set textfont $fontpref(textfont)
11557 parsefont textfont $textfont
11558 eval font configure textfont [fontflags textfont]
11559 eval font configure textfontbold [fontflags textfont 1]
11561 if {$uifont ne $fontpref(uifont)} {
11562 set uifont $fontpref(uifont)
11563 parsefont uifont $uifont
11564 eval font configure uifont [fontflags uifont]
11566 settabs
11567 if {$showlocalchanges != $oldprefs(showlocalchanges)} {
11568 if {$showlocalchanges} {
11569 doshowlocalchanges
11570 } else {
11571 dohidelocalchanges
11574 if {$limitdiffs != $oldprefs(limitdiffs) ||
11575 ($perfile_attrs && !$oldprefs(perfile_attrs))} {
11576 # treediffs elements are limited by path;
11577 # won't have encodings cached if perfile_attrs was just turned on
11578 catch {unset treediffs}
11580 if {$fontchanged || $maxwidth != $oldprefs(maxwidth)
11581 || $maxgraphpct != $oldprefs(maxgraphpct)} {
11582 redisplay
11583 } elseif {$showneartags != $oldprefs(showneartags) ||
11584 $limitdiffs != $oldprefs(limitdiffs)} {
11585 reselectline
11587 if {$hideremotes != $oldprefs(hideremotes)} {
11588 rereadrefs
11592 proc formatdate {d} {
11593 global datetimeformat
11594 if {$d ne {}} {
11595 set d [clock format [lindex $d 0] -format $datetimeformat]
11597 return $d
11600 # This list of encoding names and aliases is distilled from
11601 # http://www.iana.org/assignments/character-sets.
11602 # Not all of them are supported by Tcl.
11603 set encoding_aliases {
11604 { ANSI_X3.4-1968 iso-ir-6 ANSI_X3.4-1986 ISO_646.irv:1991 ASCII
11605 ISO646-US US-ASCII us IBM367 cp367 csASCII }
11606 { ISO-10646-UTF-1 csISO10646UTF1 }
11607 { ISO_646.basic:1983 ref csISO646basic1983 }
11608 { INVARIANT csINVARIANT }
11609 { ISO_646.irv:1983 iso-ir-2 irv csISO2IntlRefVersion }
11610 { BS_4730 iso-ir-4 ISO646-GB gb uk csISO4UnitedKingdom }
11611 { NATS-SEFI iso-ir-8-1 csNATSSEFI }
11612 { NATS-SEFI-ADD iso-ir-8-2 csNATSSEFIADD }
11613 { NATS-DANO iso-ir-9-1 csNATSDANO }
11614 { NATS-DANO-ADD iso-ir-9-2 csNATSDANOADD }
11615 { SEN_850200_B iso-ir-10 FI ISO646-FI ISO646-SE se csISO10Swedish }
11616 { SEN_850200_C iso-ir-11 ISO646-SE2 se2 csISO11SwedishForNames }
11617 { KS_C_5601-1987 iso-ir-149 KS_C_5601-1989 KSC_5601 korean csKSC56011987 }
11618 { ISO-2022-KR csISO2022KR }
11619 { EUC-KR csEUCKR }
11620 { ISO-2022-JP csISO2022JP }
11621 { ISO-2022-JP-2 csISO2022JP2 }
11622 { JIS_C6220-1969-jp JIS_C6220-1969 iso-ir-13 katakana x0201-7
11623 csISO13JISC6220jp }
11624 { JIS_C6220-1969-ro iso-ir-14 jp ISO646-JP csISO14JISC6220ro }
11625 { IT iso-ir-15 ISO646-IT csISO15Italian }
11626 { PT iso-ir-16 ISO646-PT csISO16Portuguese }
11627 { ES iso-ir-17 ISO646-ES csISO17Spanish }
11628 { greek7-old iso-ir-18 csISO18Greek7Old }
11629 { latin-greek iso-ir-19 csISO19LatinGreek }
11630 { DIN_66003 iso-ir-21 de ISO646-DE csISO21German }
11631 { NF_Z_62-010_(1973) iso-ir-25 ISO646-FR1 csISO25French }
11632 { Latin-greek-1 iso-ir-27 csISO27LatinGreek1 }
11633 { ISO_5427 iso-ir-37 csISO5427Cyrillic }
11634 { JIS_C6226-1978 iso-ir-42 csISO42JISC62261978 }
11635 { BS_viewdata iso-ir-47 csISO47BSViewdata }
11636 { INIS iso-ir-49 csISO49INIS }
11637 { INIS-8 iso-ir-50 csISO50INIS8 }
11638 { INIS-cyrillic iso-ir-51 csISO51INISCyrillic }
11639 { ISO_5427:1981 iso-ir-54 ISO5427Cyrillic1981 }
11640 { ISO_5428:1980 iso-ir-55 csISO5428Greek }
11641 { GB_1988-80 iso-ir-57 cn ISO646-CN csISO57GB1988 }
11642 { GB_2312-80 iso-ir-58 chinese csISO58GB231280 }
11643 { NS_4551-1 iso-ir-60 ISO646-NO no csISO60DanishNorwegian
11644 csISO60Norwegian1 }
11645 { NS_4551-2 ISO646-NO2 iso-ir-61 no2 csISO61Norwegian2 }
11646 { NF_Z_62-010 iso-ir-69 ISO646-FR fr csISO69French }
11647 { videotex-suppl iso-ir-70 csISO70VideotexSupp1 }
11648 { PT2 iso-ir-84 ISO646-PT2 csISO84Portuguese2 }
11649 { ES2 iso-ir-85 ISO646-ES2 csISO85Spanish2 }
11650 { MSZ_7795.3 iso-ir-86 ISO646-HU hu csISO86Hungarian }
11651 { JIS_C6226-1983 iso-ir-87 x0208 JIS_X0208-1983 csISO87JISX0208 }
11652 { greek7 iso-ir-88 csISO88Greek7 }
11653 { ASMO_449 ISO_9036 arabic7 iso-ir-89 csISO89ASMO449 }
11654 { iso-ir-90 csISO90 }
11655 { JIS_C6229-1984-a iso-ir-91 jp-ocr-a csISO91JISC62291984a }
11656 { JIS_C6229-1984-b iso-ir-92 ISO646-JP-OCR-B jp-ocr-b
11657 csISO92JISC62991984b }
11658 { JIS_C6229-1984-b-add iso-ir-93 jp-ocr-b-add csISO93JIS62291984badd }
11659 { JIS_C6229-1984-hand iso-ir-94 jp-ocr-hand csISO94JIS62291984hand }
11660 { JIS_C6229-1984-hand-add iso-ir-95 jp-ocr-hand-add
11661 csISO95JIS62291984handadd }
11662 { JIS_C6229-1984-kana iso-ir-96 csISO96JISC62291984kana }
11663 { ISO_2033-1983 iso-ir-98 e13b csISO2033 }
11664 { ANSI_X3.110-1983 iso-ir-99 CSA_T500-1983 NAPLPS csISO99NAPLPS }
11665 { ISO_8859-1:1987 iso-ir-100 ISO_8859-1 ISO-8859-1 latin1 l1 IBM819
11666 CP819 csISOLatin1 }
11667 { ISO_8859-2:1987 iso-ir-101 ISO_8859-2 ISO-8859-2 latin2 l2 csISOLatin2 }
11668 { T.61-7bit iso-ir-102 csISO102T617bit }
11669 { T.61-8bit T.61 iso-ir-103 csISO103T618bit }
11670 { ISO_8859-3:1988 iso-ir-109 ISO_8859-3 ISO-8859-3 latin3 l3 csISOLatin3 }
11671 { ISO_8859-4:1988 iso-ir-110 ISO_8859-4 ISO-8859-4 latin4 l4 csISOLatin4 }
11672 { ECMA-cyrillic iso-ir-111 KOI8-E csISO111ECMACyrillic }
11673 { CSA_Z243.4-1985-1 iso-ir-121 ISO646-CA csa7-1 ca csISO121Canadian1 }
11674 { CSA_Z243.4-1985-2 iso-ir-122 ISO646-CA2 csa7-2 csISO122Canadian2 }
11675 { CSA_Z243.4-1985-gr iso-ir-123 csISO123CSAZ24341985gr }
11676 { ISO_8859-6:1987 iso-ir-127 ISO_8859-6 ISO-8859-6 ECMA-114 ASMO-708
11677 arabic csISOLatinArabic }
11678 { ISO_8859-6-E csISO88596E ISO-8859-6-E }
11679 { ISO_8859-6-I csISO88596I ISO-8859-6-I }
11680 { ISO_8859-7:1987 iso-ir-126 ISO_8859-7 ISO-8859-7 ELOT_928 ECMA-118
11681 greek greek8 csISOLatinGreek }
11682 { T.101-G2 iso-ir-128 csISO128T101G2 }
11683 { ISO_8859-8:1988 iso-ir-138 ISO_8859-8 ISO-8859-8 hebrew
11684 csISOLatinHebrew }
11685 { ISO_8859-8-E csISO88598E ISO-8859-8-E }
11686 { ISO_8859-8-I csISO88598I ISO-8859-8-I }
11687 { CSN_369103 iso-ir-139 csISO139CSN369103 }
11688 { JUS_I.B1.002 iso-ir-141 ISO646-YU js yu csISO141JUSIB1002 }
11689 { ISO_6937-2-add iso-ir-142 csISOTextComm }
11690 { IEC_P27-1 iso-ir-143 csISO143IECP271 }
11691 { ISO_8859-5:1988 iso-ir-144 ISO_8859-5 ISO-8859-5 cyrillic
11692 csISOLatinCyrillic }
11693 { JUS_I.B1.003-serb iso-ir-146 serbian csISO146Serbian }
11694 { JUS_I.B1.003-mac macedonian iso-ir-147 csISO147Macedonian }
11695 { ISO_8859-9:1989 iso-ir-148 ISO_8859-9 ISO-8859-9 latin5 l5 csISOLatin5 }
11696 { greek-ccitt iso-ir-150 csISO150 csISO150GreekCCITT }
11697 { NC_NC00-10:81 cuba iso-ir-151 ISO646-CU csISO151Cuba }
11698 { ISO_6937-2-25 iso-ir-152 csISO6937Add }
11699 { GOST_19768-74 ST_SEV_358-88 iso-ir-153 csISO153GOST1976874 }
11700 { ISO_8859-supp iso-ir-154 latin1-2-5 csISO8859Supp }
11701 { ISO_10367-box iso-ir-155 csISO10367Box }
11702 { ISO-8859-10 iso-ir-157 l6 ISO_8859-10:1992 csISOLatin6 latin6 }
11703 { latin-lap lap iso-ir-158 csISO158Lap }
11704 { JIS_X0212-1990 x0212 iso-ir-159 csISO159JISX02121990 }
11705 { DS_2089 DS2089 ISO646-DK dk csISO646Danish }
11706 { us-dk csUSDK }
11707 { dk-us csDKUS }
11708 { JIS_X0201 X0201 csHalfWidthKatakana }
11709 { KSC5636 ISO646-KR csKSC5636 }
11710 { ISO-10646-UCS-2 csUnicode }
11711 { ISO-10646-UCS-4 csUCS4 }
11712 { DEC-MCS dec csDECMCS }
11713 { hp-roman8 roman8 r8 csHPRoman8 }
11714 { macintosh mac csMacintosh }
11715 { IBM037 cp037 ebcdic-cp-us ebcdic-cp-ca ebcdic-cp-wt ebcdic-cp-nl
11716 csIBM037 }
11717 { IBM038 EBCDIC-INT cp038 csIBM038 }
11718 { IBM273 CP273 csIBM273 }
11719 { IBM274 EBCDIC-BE CP274 csIBM274 }
11720 { IBM275 EBCDIC-BR cp275 csIBM275 }
11721 { IBM277 EBCDIC-CP-DK EBCDIC-CP-NO csIBM277 }
11722 { IBM278 CP278 ebcdic-cp-fi ebcdic-cp-se csIBM278 }
11723 { IBM280 CP280 ebcdic-cp-it csIBM280 }
11724 { IBM281 EBCDIC-JP-E cp281 csIBM281 }
11725 { IBM284 CP284 ebcdic-cp-es csIBM284 }
11726 { IBM285 CP285 ebcdic-cp-gb csIBM285 }
11727 { IBM290 cp290 EBCDIC-JP-kana csIBM290 }
11728 { IBM297 cp297 ebcdic-cp-fr csIBM297 }
11729 { IBM420 cp420 ebcdic-cp-ar1 csIBM420 }
11730 { IBM423 cp423 ebcdic-cp-gr csIBM423 }
11731 { IBM424 cp424 ebcdic-cp-he csIBM424 }
11732 { IBM437 cp437 437 csPC8CodePage437 }
11733 { IBM500 CP500 ebcdic-cp-be ebcdic-cp-ch csIBM500 }
11734 { IBM775 cp775 csPC775Baltic }
11735 { IBM850 cp850 850 csPC850Multilingual }
11736 { IBM851 cp851 851 csIBM851 }
11737 { IBM852 cp852 852 csPCp852 }
11738 { IBM855 cp855 855 csIBM855 }
11739 { IBM857 cp857 857 csIBM857 }
11740 { IBM860 cp860 860 csIBM860 }
11741 { IBM861 cp861 861 cp-is csIBM861 }
11742 { IBM862 cp862 862 csPC862LatinHebrew }
11743 { IBM863 cp863 863 csIBM863 }
11744 { IBM864 cp864 csIBM864 }
11745 { IBM865 cp865 865 csIBM865 }
11746 { IBM866 cp866 866 csIBM866 }
11747 { IBM868 CP868 cp-ar csIBM868 }
11748 { IBM869 cp869 869 cp-gr csIBM869 }
11749 { IBM870 CP870 ebcdic-cp-roece ebcdic-cp-yu csIBM870 }
11750 { IBM871 CP871 ebcdic-cp-is csIBM871 }
11751 { IBM880 cp880 EBCDIC-Cyrillic csIBM880 }
11752 { IBM891 cp891 csIBM891 }
11753 { IBM903 cp903 csIBM903 }
11754 { IBM904 cp904 904 csIBBM904 }
11755 { IBM905 CP905 ebcdic-cp-tr csIBM905 }
11756 { IBM918 CP918 ebcdic-cp-ar2 csIBM918 }
11757 { IBM1026 CP1026 csIBM1026 }
11758 { EBCDIC-AT-DE csIBMEBCDICATDE }
11759 { EBCDIC-AT-DE-A csEBCDICATDEA }
11760 { EBCDIC-CA-FR csEBCDICCAFR }
11761 { EBCDIC-DK-NO csEBCDICDKNO }
11762 { EBCDIC-DK-NO-A csEBCDICDKNOA }
11763 { EBCDIC-FI-SE csEBCDICFISE }
11764 { EBCDIC-FI-SE-A csEBCDICFISEA }
11765 { EBCDIC-FR csEBCDICFR }
11766 { EBCDIC-IT csEBCDICIT }
11767 { EBCDIC-PT csEBCDICPT }
11768 { EBCDIC-ES csEBCDICES }
11769 { EBCDIC-ES-A csEBCDICESA }
11770 { EBCDIC-ES-S csEBCDICESS }
11771 { EBCDIC-UK csEBCDICUK }
11772 { EBCDIC-US csEBCDICUS }
11773 { UNKNOWN-8BIT csUnknown8BiT }
11774 { MNEMONIC csMnemonic }
11775 { MNEM csMnem }
11776 { VISCII csVISCII }
11777 { VIQR csVIQR }
11778 { KOI8-R csKOI8R }
11779 { IBM00858 CCSID00858 CP00858 PC-Multilingual-850+euro }
11780 { IBM00924 CCSID00924 CP00924 ebcdic-Latin9--euro }
11781 { IBM01140 CCSID01140 CP01140 ebcdic-us-37+euro }
11782 { IBM01141 CCSID01141 CP01141 ebcdic-de-273+euro }
11783 { IBM01142 CCSID01142 CP01142 ebcdic-dk-277+euro ebcdic-no-277+euro }
11784 { IBM01143 CCSID01143 CP01143 ebcdic-fi-278+euro ebcdic-se-278+euro }
11785 { IBM01144 CCSID01144 CP01144 ebcdic-it-280+euro }
11786 { IBM01145 CCSID01145 CP01145 ebcdic-es-284+euro }
11787 { IBM01146 CCSID01146 CP01146 ebcdic-gb-285+euro }
11788 { IBM01147 CCSID01147 CP01147 ebcdic-fr-297+euro }
11789 { IBM01148 CCSID01148 CP01148 ebcdic-international-500+euro }
11790 { IBM01149 CCSID01149 CP01149 ebcdic-is-871+euro }
11791 { IBM1047 IBM-1047 }
11792 { PTCP154 csPTCP154 PT154 CP154 Cyrillic-Asian }
11793 { Amiga-1251 Ami1251 Amiga1251 Ami-1251 }
11794 { UNICODE-1-1 csUnicode11 }
11795 { CESU-8 csCESU-8 }
11796 { BOCU-1 csBOCU-1 }
11797 { UNICODE-1-1-UTF-7 csUnicode11UTF7 }
11798 { ISO-8859-14 iso-ir-199 ISO_8859-14:1998 ISO_8859-14 latin8 iso-celtic
11799 l8 }
11800 { ISO-8859-15 ISO_8859-15 Latin-9 }
11801 { ISO-8859-16 iso-ir-226 ISO_8859-16:2001 ISO_8859-16 latin10 l10 }
11802 { GBK CP936 MS936 windows-936 }
11803 { JIS_Encoding csJISEncoding }
11804 { Shift_JIS MS_Kanji csShiftJIS ShiftJIS Shift-JIS }
11805 { Extended_UNIX_Code_Packed_Format_for_Japanese csEUCPkdFmtJapanese
11806 EUC-JP }
11807 { Extended_UNIX_Code_Fixed_Width_for_Japanese csEUCFixWidJapanese }
11808 { ISO-10646-UCS-Basic csUnicodeASCII }
11809 { ISO-10646-Unicode-Latin1 csUnicodeLatin1 ISO-10646 }
11810 { ISO-Unicode-IBM-1261 csUnicodeIBM1261 }
11811 { ISO-Unicode-IBM-1268 csUnicodeIBM1268 }
11812 { ISO-Unicode-IBM-1276 csUnicodeIBM1276 }
11813 { ISO-Unicode-IBM-1264 csUnicodeIBM1264 }
11814 { ISO-Unicode-IBM-1265 csUnicodeIBM1265 }
11815 { ISO-8859-1-Windows-3.0-Latin-1 csWindows30Latin1 }
11816 { ISO-8859-1-Windows-3.1-Latin-1 csWindows31Latin1 }
11817 { ISO-8859-2-Windows-Latin-2 csWindows31Latin2 }
11818 { ISO-8859-9-Windows-Latin-5 csWindows31Latin5 }
11819 { Adobe-Standard-Encoding csAdobeStandardEncoding }
11820 { Ventura-US csVenturaUS }
11821 { Ventura-International csVenturaInternational }
11822 { PC8-Danish-Norwegian csPC8DanishNorwegian }
11823 { PC8-Turkish csPC8Turkish }
11824 { IBM-Symbols csIBMSymbols }
11825 { IBM-Thai csIBMThai }
11826 { HP-Legal csHPLegal }
11827 { HP-Pi-font csHPPiFont }
11828 { HP-Math8 csHPMath8 }
11829 { Adobe-Symbol-Encoding csHPPSMath }
11830 { HP-DeskTop csHPDesktop }
11831 { Ventura-Math csVenturaMath }
11832 { Microsoft-Publishing csMicrosoftPublishing }
11833 { Windows-31J csWindows31J }
11834 { GB2312 csGB2312 }
11835 { Big5 csBig5 }
11838 proc tcl_encoding {enc} {
11839 global encoding_aliases tcl_encoding_cache
11840 if {[info exists tcl_encoding_cache($enc)]} {
11841 return $tcl_encoding_cache($enc)
11843 set names [encoding names]
11844 set lcnames [string tolower $names]
11845 set enc [string tolower $enc]
11846 set i [lsearch -exact $lcnames $enc]
11847 if {$i < 0} {
11848 # look for "isonnn" instead of "iso-nnn" or "iso_nnn"
11849 if {[regsub {^(iso|cp|ibm|jis)[-_]} $enc {\1} encx]} {
11850 set i [lsearch -exact $lcnames $encx]
11853 if {$i < 0} {
11854 foreach l $encoding_aliases {
11855 set ll [string tolower $l]
11856 if {[lsearch -exact $ll $enc] < 0} continue
11857 # look through the aliases for one that tcl knows about
11858 foreach e $ll {
11859 set i [lsearch -exact $lcnames $e]
11860 if {$i < 0} {
11861 if {[regsub {^(iso|cp|ibm|jis)[-_]} $e {\1} ex]} {
11862 set i [lsearch -exact $lcnames $ex]
11865 if {$i >= 0} break
11867 break
11870 set tclenc {}
11871 if {$i >= 0} {
11872 set tclenc [lindex $names $i]
11874 set tcl_encoding_cache($enc) $tclenc
11875 return $tclenc
11878 proc gitattr {path attr default} {
11879 global path_attr_cache
11880 if {[info exists path_attr_cache($attr,$path)]} {
11881 set r $path_attr_cache($attr,$path)
11882 } else {
11883 set r "unspecified"
11884 if {![catch {set line [exec git check-attr $attr -- $path]}]} {
11885 regexp "(.*): $attr: (.*)" $line m f r
11887 set path_attr_cache($attr,$path) $r
11889 if {$r eq "unspecified"} {
11890 return $default
11892 return $r
11895 proc cache_gitattr {attr pathlist} {
11896 global path_attr_cache
11897 set newlist {}
11898 foreach path $pathlist {
11899 if {![info exists path_attr_cache($attr,$path)]} {
11900 lappend newlist $path
11903 set lim 1000
11904 if {[tk windowingsystem] == "win32"} {
11905 # windows has a 32k limit on the arguments to a command...
11906 set lim 30
11908 while {$newlist ne {}} {
11909 set head [lrange $newlist 0 [expr {$lim - 1}]]
11910 set newlist [lrange $newlist $lim end]
11911 if {![catch {set rlist [eval exec git check-attr $attr -- $head]}]} {
11912 foreach row [split $rlist "\n"] {
11913 if {[regexp "(.*): $attr: (.*)" $row m path value]} {
11914 if {[string index $path 0] eq "\""} {
11915 set path [encoding convertfrom utf-8 [lindex $path 0]]
11917 set path_attr_cache($attr,$path) $value
11924 proc get_path_encoding {path} {
11925 global gui_encoding perfile_attrs
11926 set tcl_enc $gui_encoding
11927 if {$path ne {} && $perfile_attrs} {
11928 set enc2 [tcl_encoding [gitattr $path encoding $tcl_enc]]
11929 if {$enc2 ne {}} {
11930 set tcl_enc $enc2
11933 return $tcl_enc
11936 # First check that Tcl/Tk is recent enough
11937 if {[catch {package require Tk 8.4} err]} {
11938 show_error {} . "Sorry, gitk cannot run with this version of Tcl/Tk.\n\
11939 Gitk requires at least Tcl/Tk 8.4." list
11940 exit 1
11943 # on OSX bring the current Wish process window to front
11944 if {[tk windowingsystem] eq "aqua"} {
11945 exec osascript -e [format {
11946 tell application "System Events"
11947 set frontmost of processes whose unix id is %d to true
11948 end tell
11949 } [pid] ]
11952 # Unset GIT_TRACE var if set
11953 if { [info exists ::env(GIT_TRACE)] } {
11954 unset ::env(GIT_TRACE)
11957 # defaults...
11958 set wrcomcmd "git diff-tree --stdin -p --pretty"
11960 set gitencoding {}
11961 catch {
11962 set gitencoding [exec git config --get i18n.commitencoding]
11964 catch {
11965 set gitencoding [exec git config --get i18n.logoutputencoding]
11967 if {$gitencoding == ""} {
11968 set gitencoding "utf-8"
11970 set tclencoding [tcl_encoding $gitencoding]
11971 if {$tclencoding == {}} {
11972 puts stderr "Warning: encoding $gitencoding is not supported by Tcl/Tk"
11975 set gui_encoding [encoding system]
11976 catch {
11977 set enc [exec git config --get gui.encoding]
11978 if {$enc ne {}} {
11979 set tclenc [tcl_encoding $enc]
11980 if {$tclenc ne {}} {
11981 set gui_encoding $tclenc
11982 } else {
11983 puts stderr "Warning: encoding $enc is not supported by Tcl/Tk"
11988 set log_showroot true
11989 catch {
11990 set log_showroot [exec git config --bool --get log.showroot]
11993 if {[tk windowingsystem] eq "aqua"} {
11994 set mainfont {{Lucida Grande} 9}
11995 set textfont {Monaco 9}
11996 set uifont {{Lucida Grande} 9 bold}
11997 } elseif {![catch {::tk::pkgconfig get fontsystem} xft] && $xft eq "xft"} {
11998 # fontconfig!
11999 set mainfont {sans 9}
12000 set textfont {monospace 9}
12001 set uifont {sans 9 bold}
12002 } else {
12003 set mainfont {Helvetica 9}
12004 set textfont {Courier 9}
12005 set uifont {Helvetica 9 bold}
12007 set tabstop 8
12008 set findmergefiles 0
12009 set maxgraphpct 50
12010 set maxwidth 16
12011 set revlistorder 0
12012 set fastdate 0
12013 set uparrowlen 5
12014 set downarrowlen 5
12015 set mingaplen 100
12016 set cmitmode "patch"
12017 set wrapcomment "none"
12018 set showneartags 1
12019 set hideremotes 0
12020 set maxrefs 20
12021 set maxlinelen 200
12022 set showlocalchanges 1
12023 set limitdiffs 1
12024 set datetimeformat "%Y-%m-%d %H:%M:%S"
12025 set autoselect 1
12026 set autosellen 40
12027 set perfile_attrs 0
12028 set want_ttk 1
12030 if {[tk windowingsystem] eq "aqua"} {
12031 set extdifftool "opendiff"
12032 } else {
12033 set extdifftool "meld"
12036 set colors {green red blue magenta darkgrey brown orange}
12037 if {[tk windowingsystem] eq "win32"} {
12038 set uicolor SystemButtonFace
12039 set uifgcolor SystemButtonText
12040 set uifgdisabledcolor SystemDisabledText
12041 set bgcolor SystemWindow
12042 set fgcolor SystemWindowText
12043 set selectbgcolor SystemHighlight
12044 } else {
12045 set uicolor grey85
12046 set uifgcolor black
12047 set uifgdisabledcolor "#999"
12048 set bgcolor white
12049 set fgcolor black
12050 set selectbgcolor gray85
12052 set diffcolors {red "#00a000" blue}
12053 set diffcontext 3
12054 set mergecolors {red blue green purple brown "#009090" magenta "#808000" "#009000" "#ff0080" cyan "#b07070" "#70b0f0" "#70f0b0" "#f0b070" "#ff70b0"}
12055 set ignorespace 0
12056 set worddiff ""
12057 set markbgcolor "#e0e0ff"
12059 set headbgcolor green
12060 set headfgcolor black
12061 set headoutlinecolor black
12062 set remotebgcolor #ffddaa
12063 set tagbgcolor yellow
12064 set tagfgcolor black
12065 set tagoutlinecolor black
12066 set reflinecolor black
12067 set filesepbgcolor #aaaaaa
12068 set filesepfgcolor black
12069 set linehoverbgcolor #ffff80
12070 set linehoverfgcolor black
12071 set linehoveroutlinecolor black
12072 set mainheadcirclecolor yellow
12073 set workingfilescirclecolor red
12074 set indexcirclecolor green
12075 set circlecolors {white blue gray blue blue}
12076 set linkfgcolor blue
12077 set circleoutlinecolor $fgcolor
12078 set foundbgcolor yellow
12079 set currentsearchhitbgcolor orange
12081 # button for popping up context menus
12082 if {[tk windowingsystem] eq "aqua"} {
12083 set ctxbut <Button-2>
12084 } else {
12085 set ctxbut <Button-3>
12088 ## For msgcat loading, first locate the installation location.
12089 if { [info exists ::env(GITK_MSGSDIR)] } {
12090 ## Msgsdir was manually set in the environment.
12091 set gitk_msgsdir $::env(GITK_MSGSDIR)
12092 } else {
12093 ## Let's guess the prefix from argv0.
12094 set gitk_prefix [file dirname [file dirname [file normalize $argv0]]]
12095 set gitk_libdir [file join $gitk_prefix share gitk lib]
12096 set gitk_msgsdir [file join $gitk_libdir msgs]
12099 ## Internationalization (i18n) through msgcat and gettext. See
12100 ## http://www.gnu.org/software/gettext/manual/html_node/Tcl.html
12101 package require msgcat
12102 namespace import ::msgcat::mc
12103 ## And eventually load the actual message catalog
12104 ::msgcat::mcload $gitk_msgsdir
12106 catch {
12107 # follow the XDG base directory specification by default. See
12108 # http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
12109 if {[info exists env(XDG_CONFIG_HOME)] && $env(XDG_CONFIG_HOME) ne ""} {
12110 # XDG_CONFIG_HOME environment variable is set
12111 set config_file [file join $env(XDG_CONFIG_HOME) git gitk]
12112 set config_file_tmp [file join $env(XDG_CONFIG_HOME) git gitk-tmp]
12113 } else {
12114 # default XDG_CONFIG_HOME
12115 set config_file "~/.config/git/gitk"
12116 set config_file_tmp "~/.config/git/gitk-tmp"
12118 if {![file exists $config_file]} {
12119 # for backward compatibility use the old config file if it exists
12120 if {[file exists "~/.gitk"]} {
12121 set config_file "~/.gitk"
12122 set config_file_tmp "~/.gitk-tmp"
12123 } elseif {![file exists [file dirname $config_file]]} {
12124 file mkdir [file dirname $config_file]
12127 source $config_file
12130 parsefont mainfont $mainfont
12131 eval font create mainfont [fontflags mainfont]
12132 eval font create mainfontbold [fontflags mainfont 1]
12134 parsefont textfont $textfont
12135 eval font create textfont [fontflags textfont]
12136 eval font create textfontbold [fontflags textfont 1]
12138 parsefont uifont $uifont
12139 eval font create uifont [fontflags uifont]
12141 setui $uicolor
12143 setoptions
12145 # check that we can find a .git directory somewhere...
12146 if {[catch {set gitdir [exec git rev-parse --git-dir]}]} {
12147 show_error {} . [mc "Cannot find a git repository here."]
12148 exit 1
12151 set selecthead {}
12152 set selectheadid {}
12154 set revtreeargs {}
12155 set cmdline_files {}
12156 set i 0
12157 set revtreeargscmd {}
12158 foreach arg $argv {
12159 switch -glob -- $arg {
12160 "" { }
12161 "--" {
12162 set cmdline_files [lrange $argv [expr {$i + 1}] end]
12163 break
12165 "--select-commit=*" {
12166 set selecthead [string range $arg 16 end]
12168 "--argscmd=*" {
12169 set revtreeargscmd [string range $arg 10 end]
12171 default {
12172 lappend revtreeargs $arg
12175 incr i
12178 if {$selecthead eq "HEAD"} {
12179 set selecthead {}
12182 if {$i >= [llength $argv] && $revtreeargs ne {}} {
12183 # no -- on command line, but some arguments (other than --argscmd)
12184 if {[catch {
12185 set f [eval exec git rev-parse --no-revs --no-flags $revtreeargs]
12186 set cmdline_files [split $f "\n"]
12187 set n [llength $cmdline_files]
12188 set revtreeargs [lrange $revtreeargs 0 end-$n]
12189 # Unfortunately git rev-parse doesn't produce an error when
12190 # something is both a revision and a filename. To be consistent
12191 # with git log and git rev-list, check revtreeargs for filenames.
12192 foreach arg $revtreeargs {
12193 if {[file exists $arg]} {
12194 show_error {} . [mc "Ambiguous argument '%s': both revision\
12195 and filename" $arg]
12196 exit 1
12199 } err]} {
12200 # unfortunately we get both stdout and stderr in $err,
12201 # so look for "fatal:".
12202 set i [string first "fatal:" $err]
12203 if {$i > 0} {
12204 set err [string range $err [expr {$i + 6}] end]
12206 show_error {} . "[mc "Bad arguments to gitk:"]\n$err"
12207 exit 1
12211 set nullid "0000000000000000000000000000000000000000"
12212 set nullid2 "0000000000000000000000000000000000000001"
12213 set nullfile "/dev/null"
12215 set have_tk85 [expr {[package vcompare $tk_version "8.5"] >= 0}]
12216 if {![info exists have_ttk]} {
12217 set have_ttk [llength [info commands ::ttk::style]]
12219 set use_ttk [expr {$have_ttk && $want_ttk}]
12220 set NS [expr {$use_ttk ? "ttk" : ""}]
12222 regexp {^git version ([\d.]*\d)} [exec git version] _ git_version
12224 set show_notes {}
12225 if {[package vcompare $git_version "1.6.6.2"] >= 0} {
12226 set show_notes "--show-notes"
12229 set appname "gitk"
12231 set runq {}
12232 set history {}
12233 set historyindex 0
12234 set fh_serial 0
12235 set nhl_names {}
12236 set highlight_paths {}
12237 set findpattern {}
12238 set searchdirn -forwards
12239 set boldids {}
12240 set boldnameids {}
12241 set diffelide {0 0}
12242 set markingmatches 0
12243 set linkentercount 0
12244 set need_redisplay 0
12245 set nrows_drawn 0
12246 set firsttabstop 0
12248 set nextviewnum 1
12249 set curview 0
12250 set selectedview 0
12251 set selectedhlview [mc "None"]
12252 set highlight_related [mc "None"]
12253 set highlight_files {}
12254 set viewfiles(0) {}
12255 set viewperm(0) 0
12256 set viewargs(0) {}
12257 set viewargscmd(0) {}
12259 set selectedline {}
12260 set numcommits 0
12261 set loginstance 0
12262 set cmdlineok 0
12263 set stopped 0
12264 set stuffsaved 0
12265 set patchnum 0
12266 set lserial 0
12267 set hasworktree [hasworktree]
12268 set cdup {}
12269 if {[expr {[exec git rev-parse --is-inside-work-tree] == "true"}]} {
12270 set cdup [exec git rev-parse --show-cdup]
12272 set worktree [exec git rev-parse --show-toplevel]
12273 setcoords
12274 makewindow
12275 if {$::tcl_platform(platform) eq {windows} && [file exists $gitk_prefix/etc/git.ico]} {
12276 wm iconbitmap . -default $gitk_prefix/etc/git.ico
12277 } else {
12278 catch {
12279 image create photo gitlogo -width 16 -height 16
12281 image create photo gitlogominus -width 4 -height 2
12282 gitlogominus put #C00000 -to 0 0 4 2
12283 gitlogo copy gitlogominus -to 1 5
12284 gitlogo copy gitlogominus -to 6 5
12285 gitlogo copy gitlogominus -to 11 5
12286 image delete gitlogominus
12288 image create photo gitlogoplus -width 4 -height 4
12289 gitlogoplus put #008000 -to 1 0 3 4
12290 gitlogoplus put #008000 -to 0 1 4 3
12291 gitlogo copy gitlogoplus -to 1 9
12292 gitlogo copy gitlogoplus -to 6 9
12293 gitlogo copy gitlogoplus -to 11 9
12294 image delete gitlogoplus
12296 image create photo gitlogo32 -width 32 -height 32
12297 gitlogo32 copy gitlogo -zoom 2 2
12299 wm iconphoto . -default gitlogo gitlogo32
12302 # wait for the window to become visible
12303 tkwait visibility .
12304 wm title . "$appname: [reponame]"
12305 update
12306 readrefs
12308 if {$cmdline_files ne {} || $revtreeargs ne {} || $revtreeargscmd ne {}} {
12309 # create a view for the files/dirs specified on the command line
12310 set curview 1
12311 set selectedview 1
12312 set nextviewnum 2
12313 set viewname(1) [mc "Command line"]
12314 set viewfiles(1) $cmdline_files
12315 set viewargs(1) $revtreeargs
12316 set viewargscmd(1) $revtreeargscmd
12317 set viewperm(1) 0
12318 set vdatemode(1) 0
12319 addviewmenu 1
12320 .bar.view entryconf [mca "Edit view..."] -state normal
12321 .bar.view entryconf [mca "Delete view"] -state normal
12324 if {[info exists permviews]} {
12325 foreach v $permviews {
12326 set n $nextviewnum
12327 incr nextviewnum
12328 set viewname($n) [lindex $v 0]
12329 set viewfiles($n) [lindex $v 1]
12330 set viewargs($n) [lindex $v 2]
12331 set viewargscmd($n) [lindex $v 3]
12332 set viewperm($n) 1
12333 addviewmenu $n
12337 if {[tk windowingsystem] eq "win32"} {
12338 focus -force .
12341 getcommits {}
12343 # Local variables:
12344 # mode: tcl
12345 # indent-tabs-mode: t
12346 # tab-width: 8
12347 # End: