Merge branch 'master' of git://github.com/gitster/git
[git/mingw.git] / gitk-git / gitk
blob4a0a00ae565863946e73f8b988ce58e275d23129
1 #!/bin/sh
2 # Tcl ignores the next line -*- tcl -*- \
3 exec wish "$0" -- "$@"
5 # Copyright © 2005-2009 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 gitdir {} {
13 global env
14 if {[info exists env(GIT_DIR)]} {
15 return $env(GIT_DIR)
16 } else {
17 return [exec git rev-parse --git-dir]
21 # A simple scheduler for compute-intensive stuff.
22 # The aim is to make sure that event handlers for GUI actions can
23 # run at least every 50-100 ms. Unfortunately fileevent handlers are
24 # run before X event handlers, so reading from a fast source can
25 # make the GUI completely unresponsive.
26 proc run args {
27 global isonrunq runq currunq
29 set script $args
30 if {[info exists isonrunq($script)]} return
31 if {$runq eq {} && ![info exists currunq]} {
32 after idle dorunq
34 lappend runq [list {} $script]
35 set isonrunq($script) 1
38 proc filerun {fd script} {
39 fileevent $fd readable [list filereadable $fd $script]
42 proc filereadable {fd script} {
43 global runq currunq
45 fileevent $fd readable {}
46 if {$runq eq {} && ![info exists currunq]} {
47 after idle dorunq
49 lappend runq [list $fd $script]
52 proc nukefile {fd} {
53 global runq
55 for {set i 0} {$i < [llength $runq]} {} {
56 if {[lindex $runq $i 0] eq $fd} {
57 set runq [lreplace $runq $i $i]
58 } else {
59 incr i
64 proc dorunq {} {
65 global isonrunq runq currunq
67 set tstart [clock clicks -milliseconds]
68 set t0 $tstart
69 while {[llength $runq] > 0} {
70 set fd [lindex $runq 0 0]
71 set script [lindex $runq 0 1]
72 set currunq [lindex $runq 0]
73 set runq [lrange $runq 1 end]
74 set repeat [eval $script]
75 unset currunq
76 set t1 [clock clicks -milliseconds]
77 set t [expr {$t1 - $t0}]
78 if {$repeat ne {} && $repeat} {
79 if {$fd eq {} || $repeat == 2} {
80 # script returns 1 if it wants to be readded
81 # file readers return 2 if they could do more straight away
82 lappend runq [list $fd $script]
83 } else {
84 fileevent $fd readable [list filereadable $fd $script]
86 } elseif {$fd eq {}} {
87 unset isonrunq($script)
89 set t0 $t1
90 if {$t1 - $tstart >= 80} break
92 if {$runq ne {}} {
93 after idle dorunq
97 proc reg_instance {fd} {
98 global commfd leftover loginstance
100 set i [incr loginstance]
101 set commfd($i) $fd
102 set leftover($i) {}
103 return $i
106 proc unmerged_files {files} {
107 global nr_unmerged
109 # find the list of unmerged files
110 set mlist {}
111 set nr_unmerged 0
112 if {[catch {
113 set fd [open "| git ls-files -u" r]
114 } err]} {
115 show_error {} . "[mc "Couldn't get list of unmerged files:"] $err"
116 exit 1
118 while {[gets $fd line] >= 0} {
119 set i [string first "\t" $line]
120 if {$i < 0} continue
121 set fname [string range $line [expr {$i+1}] end]
122 if {[lsearch -exact $mlist $fname] >= 0} continue
123 incr nr_unmerged
124 if {$files eq {} || [path_filter $files $fname]} {
125 lappend mlist $fname
128 catch {close $fd}
129 return $mlist
132 proc parseviewargs {n arglist} {
133 global vdatemode vmergeonly vflags vdflags vrevs vfiltered vorigargs env
134 global worddiff git_version
136 set vdatemode($n) 0
137 set vmergeonly($n) 0
138 set glflags {}
139 set diffargs {}
140 set nextisval 0
141 set revargs {}
142 set origargs $arglist
143 set allknown 1
144 set filtered 0
145 set i -1
146 foreach arg $arglist {
147 incr i
148 if {$nextisval} {
149 lappend glflags $arg
150 set nextisval 0
151 continue
153 switch -glob -- $arg {
154 "-d" -
155 "--date-order" {
156 set vdatemode($n) 1
157 # remove from origargs in case we hit an unknown option
158 set origargs [lreplace $origargs $i $i]
159 incr i -1
161 "-[puabwcrRBMC]" -
162 "--no-renames" - "--full-index" - "--binary" - "--abbrev=*" -
163 "--find-copies-harder" - "-l*" - "--ext-diff" - "--no-ext-diff" -
164 "--src-prefix=*" - "--dst-prefix=*" - "--no-prefix" -
165 "-O*" - "--text" - "--full-diff" - "--ignore-space-at-eol" -
166 "--ignore-space-change" - "-U*" - "--unified=*" {
167 # These request or affect diff output, which we don't want.
168 # Some could be used to set our defaults for diff display.
169 lappend diffargs $arg
171 "--raw" - "--patch-with-raw" - "--patch-with-stat" -
172 "--name-only" - "--name-status" - "--color" -
173 "--log-size" - "--pretty=*" - "--decorate" - "--abbrev-commit" -
174 "--cc" - "-z" - "--header" - "--parents" - "--boundary" -
175 "--no-color" - "-g" - "--walk-reflogs" - "--no-walk" -
176 "--timestamp" - "relative-date" - "--date=*" - "--stdin" -
177 "--objects" - "--objects-edge" - "--reverse" {
178 # These cause our parsing of git log's output to fail, or else
179 # they're options we want to set ourselves, so ignore them.
181 "--color-words*" - "--word-diff=color" {
182 # These trigger a word diff in the console interface,
183 # so help the user by enabling our own support
184 if {[package vcompare $git_version "1.7.2"] >= 0} {
185 set worddiff [mc "Color words"]
188 "--word-diff*" {
189 if {[package vcompare $git_version "1.7.2"] >= 0} {
190 set worddiff [mc "Markup words"]
193 "--stat=*" - "--numstat" - "--shortstat" - "--summary" -
194 "--check" - "--exit-code" - "--quiet" - "--topo-order" -
195 "--full-history" - "--dense" - "--sparse" -
196 "--follow" - "--left-right" - "--encoding=*" {
197 # These are harmless, and some are even useful
198 lappend glflags $arg
200 "--diff-filter=*" - "--no-merges" - "--unpacked" -
201 "--max-count=*" - "--skip=*" - "--since=*" - "--after=*" -
202 "--until=*" - "--before=*" - "--max-age=*" - "--min-age=*" -
203 "--author=*" - "--committer=*" - "--grep=*" - "-[iE]" -
204 "--remove-empty" - "--first-parent" - "--cherry-pick" -
205 "-S*" - "--pickaxe-all" - "--pickaxe-regex" -
206 "--simplify-by-decoration" {
207 # These mean that we get a subset of the commits
208 set filtered 1
209 lappend glflags $arg
211 "-n" {
212 # This appears to be the only one that has a value as a
213 # separate word following it
214 set filtered 1
215 set nextisval 1
216 lappend glflags $arg
218 "--not" - "--all" {
219 lappend revargs $arg
221 "--merge" {
222 set vmergeonly($n) 1
223 # git rev-parse doesn't understand --merge
224 lappend revargs --gitk-symmetric-diff-marker MERGE_HEAD...HEAD
226 "--no-replace-objects" {
227 set env(GIT_NO_REPLACE_OBJECTS) "1"
229 "-*" {
230 # Other flag arguments including -<n>
231 if {[string is digit -strict [string range $arg 1 end]]} {
232 set filtered 1
233 } else {
234 # a flag argument that we don't recognize;
235 # that means we can't optimize
236 set allknown 0
238 lappend glflags $arg
240 default {
241 # Non-flag arguments specify commits or ranges of commits
242 if {[string match "*...*" $arg]} {
243 lappend revargs --gitk-symmetric-diff-marker
245 lappend revargs $arg
249 set vdflags($n) $diffargs
250 set vflags($n) $glflags
251 set vrevs($n) $revargs
252 set vfiltered($n) $filtered
253 set vorigargs($n) $origargs
254 return $allknown
257 proc parseviewrevs {view revs} {
258 global vposids vnegids
260 if {$revs eq {}} {
261 set revs HEAD
263 if {[catch {set ids [eval exec git rev-parse $revs]} err]} {
264 # we get stdout followed by stderr in $err
265 # for an unknown rev, git rev-parse echoes it and then errors out
266 set errlines [split $err "\n"]
267 set badrev {}
268 for {set l 0} {$l < [llength $errlines]} {incr l} {
269 set line [lindex $errlines $l]
270 if {!([string length $line] == 40 && [string is xdigit $line])} {
271 if {[string match "fatal:*" $line]} {
272 if {[string match "fatal: ambiguous argument*" $line]
273 && $badrev ne {}} {
274 if {[llength $badrev] == 1} {
275 set err "unknown revision $badrev"
276 } else {
277 set err "unknown revisions: [join $badrev ", "]"
279 } else {
280 set err [join [lrange $errlines $l end] "\n"]
282 break
284 lappend badrev $line
287 error_popup "[mc "Error parsing revisions:"] $err"
288 return {}
290 set ret {}
291 set pos {}
292 set neg {}
293 set sdm 0
294 foreach id [split $ids "\n"] {
295 if {$id eq "--gitk-symmetric-diff-marker"} {
296 set sdm 4
297 } elseif {[string match "^*" $id]} {
298 if {$sdm != 1} {
299 lappend ret $id
300 if {$sdm == 3} {
301 set sdm 0
304 lappend neg [string range $id 1 end]
305 } else {
306 if {$sdm != 2} {
307 lappend ret $id
308 } else {
309 lset ret end $id...[lindex $ret end]
311 lappend pos $id
313 incr sdm -1
315 set vposids($view) $pos
316 set vnegids($view) $neg
317 return $ret
320 # Start off a git log process and arrange to read its output
321 proc start_rev_list {view} {
322 global startmsecs commitidx viewcomplete curview
323 global tclencoding
324 global viewargs viewargscmd viewfiles vfilelimit
325 global showlocalchanges
326 global viewactive viewinstances vmergeonly
327 global mainheadid viewmainheadid viewmainheadid_orig
328 global vcanopt vflags vrevs vorigargs
329 global show_notes
331 set startmsecs [clock clicks -milliseconds]
332 set commitidx($view) 0
333 # these are set this way for the error exits
334 set viewcomplete($view) 1
335 set viewactive($view) 0
336 varcinit $view
338 set args $viewargs($view)
339 if {$viewargscmd($view) ne {}} {
340 if {[catch {
341 set str [exec sh -c $viewargscmd($view)]
342 } err]} {
343 error_popup "[mc "Error executing --argscmd command:"] $err"
344 return 0
346 set args [concat $args [split $str "\n"]]
348 set vcanopt($view) [parseviewargs $view $args]
350 set files $viewfiles($view)
351 if {$vmergeonly($view)} {
352 set files [unmerged_files $files]
353 if {$files eq {}} {
354 global nr_unmerged
355 if {$nr_unmerged == 0} {
356 error_popup [mc "No files selected: --merge specified but\
357 no files are unmerged."]
358 } else {
359 error_popup [mc "No files selected: --merge specified but\
360 no unmerged files are within file limit."]
362 return 0
365 set vfilelimit($view) $files
367 if {$vcanopt($view)} {
368 set revs [parseviewrevs $view $vrevs($view)]
369 if {$revs eq {}} {
370 return 0
372 set args [concat $vflags($view) $revs]
373 } else {
374 set args $vorigargs($view)
377 if {[catch {
378 set fd [open [concat | git log --no-color -z --pretty=raw $show_notes \
379 --parents --boundary $args "--" $files] r]
380 } err]} {
381 error_popup "[mc "Error executing git log:"] $err"
382 return 0
384 set i [reg_instance $fd]
385 set viewinstances($view) [list $i]
386 set viewmainheadid($view) $mainheadid
387 set viewmainheadid_orig($view) $mainheadid
388 if {$files ne {} && $mainheadid ne {}} {
389 get_viewmainhead $view
391 if {$showlocalchanges && $viewmainheadid($view) ne {}} {
392 interestedin $viewmainheadid($view) dodiffindex
394 fconfigure $fd -blocking 0 -translation lf -eofchar {}
395 if {$tclencoding != {}} {
396 fconfigure $fd -encoding $tclencoding
398 filerun $fd [list getcommitlines $fd $i $view 0]
399 nowbusy $view [mc "Reading"]
400 set viewcomplete($view) 0
401 set viewactive($view) 1
402 return 1
405 proc stop_instance {inst} {
406 global commfd leftover
408 set fd $commfd($inst)
409 catch {
410 set pid [pid $fd]
412 if {$::tcl_platform(platform) eq {windows}} {
413 exec kill -f $pid
414 } else {
415 exec kill $pid
418 catch {close $fd}
419 nukefile $fd
420 unset commfd($inst)
421 unset leftover($inst)
424 proc stop_backends {} {
425 global commfd
427 foreach inst [array names commfd] {
428 stop_instance $inst
432 proc stop_rev_list {view} {
433 global viewinstances
435 foreach inst $viewinstances($view) {
436 stop_instance $inst
438 set viewinstances($view) {}
441 proc reset_pending_select {selid} {
442 global pending_select mainheadid selectheadid
444 if {$selid ne {}} {
445 set pending_select $selid
446 } elseif {$selectheadid ne {}} {
447 set pending_select $selectheadid
448 } else {
449 set pending_select $mainheadid
453 proc getcommits {selid} {
454 global canv curview need_redisplay viewactive
456 initlayout
457 if {[start_rev_list $curview]} {
458 reset_pending_select $selid
459 show_status [mc "Reading commits..."]
460 set need_redisplay 1
461 } else {
462 show_status [mc "No commits selected"]
466 proc updatecommits {} {
467 global curview vcanopt vorigargs vfilelimit viewinstances
468 global viewactive viewcomplete tclencoding
469 global startmsecs showneartags showlocalchanges
470 global mainheadid viewmainheadid viewmainheadid_orig pending_select
471 global isworktree
472 global varcid vposids vnegids vflags vrevs
473 global show_notes
475 set isworktree [expr {[exec git rev-parse --is-inside-work-tree] == "true"}]
476 rereadrefs
477 set view $curview
478 if {$mainheadid ne $viewmainheadid_orig($view)} {
479 if {$showlocalchanges} {
480 dohidelocalchanges
482 set viewmainheadid($view) $mainheadid
483 set viewmainheadid_orig($view) $mainheadid
484 if {$vfilelimit($view) ne {}} {
485 get_viewmainhead $view
488 if {$showlocalchanges} {
489 doshowlocalchanges
491 if {$vcanopt($view)} {
492 set oldpos $vposids($view)
493 set oldneg $vnegids($view)
494 set revs [parseviewrevs $view $vrevs($view)]
495 if {$revs eq {}} {
496 return
498 # note: getting the delta when negative refs change is hard,
499 # and could require multiple git log invocations, so in that
500 # case we ask git log for all the commits (not just the delta)
501 if {$oldneg eq $vnegids($view)} {
502 set newrevs {}
503 set npos 0
504 # take out positive refs that we asked for before or
505 # that we have already seen
506 foreach rev $revs {
507 if {[string length $rev] == 40} {
508 if {[lsearch -exact $oldpos $rev] < 0
509 && ![info exists varcid($view,$rev)]} {
510 lappend newrevs $rev
511 incr npos
513 } else {
514 lappend $newrevs $rev
517 if {$npos == 0} return
518 set revs $newrevs
519 set vposids($view) [lsort -unique [concat $oldpos $vposids($view)]]
521 set args [concat $vflags($view) $revs --not $oldpos]
522 } else {
523 set args $vorigargs($view)
525 if {[catch {
526 set fd [open [concat | git log --no-color -z --pretty=raw $show_notes \
527 --parents --boundary $args "--" $vfilelimit($view)] r]
528 } err]} {
529 error_popup "[mc "Error executing git log:"] $err"
530 return
532 if {$viewactive($view) == 0} {
533 set startmsecs [clock clicks -milliseconds]
535 set i [reg_instance $fd]
536 lappend viewinstances($view) $i
537 fconfigure $fd -blocking 0 -translation lf -eofchar {}
538 if {$tclencoding != {}} {
539 fconfigure $fd -encoding $tclencoding
541 filerun $fd [list getcommitlines $fd $i $view 1]
542 incr viewactive($view)
543 set viewcomplete($view) 0
544 reset_pending_select {}
545 nowbusy $view [mc "Reading"]
546 if {$showneartags} {
547 getallcommits
551 proc reloadcommits {} {
552 global curview viewcomplete selectedline currentid thickerline
553 global showneartags treediffs commitinterest cached_commitrow
554 global targetid
556 set selid {}
557 if {$selectedline ne {}} {
558 set selid $currentid
561 if {!$viewcomplete($curview)} {
562 stop_rev_list $curview
564 resetvarcs $curview
565 set selectedline {}
566 catch {unset currentid}
567 catch {unset thickerline}
568 catch {unset treediffs}
569 readrefs
570 changedrefs
571 if {$showneartags} {
572 getallcommits
574 clear_display
575 catch {unset commitinterest}
576 catch {unset cached_commitrow}
577 catch {unset targetid}
578 setcanvscroll
579 getcommits $selid
580 return 0
583 # This makes a string representation of a positive integer which
584 # sorts as a string in numerical order
585 proc strrep {n} {
586 if {$n < 16} {
587 return [format "%x" $n]
588 } elseif {$n < 256} {
589 return [format "x%.2x" $n]
590 } elseif {$n < 65536} {
591 return [format "y%.4x" $n]
593 return [format "z%.8x" $n]
596 # Procedures used in reordering commits from git log (without
597 # --topo-order) into the order for display.
599 proc varcinit {view} {
600 global varcstart vupptr vdownptr vleftptr vbackptr varctok varcrow
601 global vtokmod varcmod vrowmod varcix vlastins
603 set varcstart($view) {{}}
604 set vupptr($view) {0}
605 set vdownptr($view) {0}
606 set vleftptr($view) {0}
607 set vbackptr($view) {0}
608 set varctok($view) {{}}
609 set varcrow($view) {{}}
610 set vtokmod($view) {}
611 set varcmod($view) 0
612 set vrowmod($view) 0
613 set varcix($view) {{}}
614 set vlastins($view) {0}
617 proc resetvarcs {view} {
618 global varcid varccommits parents children vseedcount ordertok
620 foreach vid [array names varcid $view,*] {
621 unset varcid($vid)
622 unset children($vid)
623 unset parents($vid)
625 # some commits might have children but haven't been seen yet
626 foreach vid [array names children $view,*] {
627 unset children($vid)
629 foreach va [array names varccommits $view,*] {
630 unset varccommits($va)
632 foreach vd [array names vseedcount $view,*] {
633 unset vseedcount($vd)
635 catch {unset ordertok}
638 # returns a list of the commits with no children
639 proc seeds {v} {
640 global vdownptr vleftptr varcstart
642 set ret {}
643 set a [lindex $vdownptr($v) 0]
644 while {$a != 0} {
645 lappend ret [lindex $varcstart($v) $a]
646 set a [lindex $vleftptr($v) $a]
648 return $ret
651 proc newvarc {view id} {
652 global varcid varctok parents children vdatemode
653 global vupptr vdownptr vleftptr vbackptr varcrow varcix varcstart
654 global commitdata commitinfo vseedcount varccommits vlastins
656 set a [llength $varctok($view)]
657 set vid $view,$id
658 if {[llength $children($vid)] == 0 || $vdatemode($view)} {
659 if {![info exists commitinfo($id)]} {
660 parsecommit $id $commitdata($id) 1
662 set cdate [lindex $commitinfo($id) 4]
663 if {![string is integer -strict $cdate]} {
664 set cdate 0
666 if {![info exists vseedcount($view,$cdate)]} {
667 set vseedcount($view,$cdate) -1
669 set c [incr vseedcount($view,$cdate)]
670 set cdate [expr {$cdate ^ 0xffffffff}]
671 set tok "s[strrep $cdate][strrep $c]"
672 } else {
673 set tok {}
675 set ka 0
676 if {[llength $children($vid)] > 0} {
677 set kid [lindex $children($vid) end]
678 set k $varcid($view,$kid)
679 if {[string compare [lindex $varctok($view) $k] $tok] > 0} {
680 set ki $kid
681 set ka $k
682 set tok [lindex $varctok($view) $k]
685 if {$ka != 0} {
686 set i [lsearch -exact $parents($view,$ki) $id]
687 set j [expr {[llength $parents($view,$ki)] - 1 - $i}]
688 append tok [strrep $j]
690 set c [lindex $vlastins($view) $ka]
691 if {$c == 0 || [string compare $tok [lindex $varctok($view) $c]] < 0} {
692 set c $ka
693 set b [lindex $vdownptr($view) $ka]
694 } else {
695 set b [lindex $vleftptr($view) $c]
697 while {$b != 0 && [string compare $tok [lindex $varctok($view) $b]] >= 0} {
698 set c $b
699 set b [lindex $vleftptr($view) $c]
701 if {$c == $ka} {
702 lset vdownptr($view) $ka $a
703 lappend vbackptr($view) 0
704 } else {
705 lset vleftptr($view) $c $a
706 lappend vbackptr($view) $c
708 lset vlastins($view) $ka $a
709 lappend vupptr($view) $ka
710 lappend vleftptr($view) $b
711 if {$b != 0} {
712 lset vbackptr($view) $b $a
714 lappend varctok($view) $tok
715 lappend varcstart($view) $id
716 lappend vdownptr($view) 0
717 lappend varcrow($view) {}
718 lappend varcix($view) {}
719 set varccommits($view,$a) {}
720 lappend vlastins($view) 0
721 return $a
724 proc splitvarc {p v} {
725 global varcid varcstart varccommits varctok vtokmod
726 global vupptr vdownptr vleftptr vbackptr varcix varcrow vlastins
728 set oa $varcid($v,$p)
729 set otok [lindex $varctok($v) $oa]
730 set ac $varccommits($v,$oa)
731 set i [lsearch -exact $varccommits($v,$oa) $p]
732 if {$i <= 0} return
733 set na [llength $varctok($v)]
734 # "%" sorts before "0"...
735 set tok "$otok%[strrep $i]"
736 lappend varctok($v) $tok
737 lappend varcrow($v) {}
738 lappend varcix($v) {}
739 set varccommits($v,$oa) [lrange $ac 0 [expr {$i - 1}]]
740 set varccommits($v,$na) [lrange $ac $i end]
741 lappend varcstart($v) $p
742 foreach id $varccommits($v,$na) {
743 set varcid($v,$id) $na
745 lappend vdownptr($v) [lindex $vdownptr($v) $oa]
746 lappend vlastins($v) [lindex $vlastins($v) $oa]
747 lset vdownptr($v) $oa $na
748 lset vlastins($v) $oa 0
749 lappend vupptr($v) $oa
750 lappend vleftptr($v) 0
751 lappend vbackptr($v) 0
752 for {set b [lindex $vdownptr($v) $na]} {$b != 0} {set b [lindex $vleftptr($v) $b]} {
753 lset vupptr($v) $b $na
755 if {[string compare $otok $vtokmod($v)] <= 0} {
756 modify_arc $v $oa
760 proc renumbervarc {a v} {
761 global parents children varctok varcstart varccommits
762 global vupptr vdownptr vleftptr vbackptr vlastins varcid vtokmod vdatemode
764 set t1 [clock clicks -milliseconds]
765 set todo {}
766 set isrelated($a) 1
767 set kidchanged($a) 1
768 set ntot 0
769 while {$a != 0} {
770 if {[info exists isrelated($a)]} {
771 lappend todo $a
772 set id [lindex $varccommits($v,$a) end]
773 foreach p $parents($v,$id) {
774 if {[info exists varcid($v,$p)]} {
775 set isrelated($varcid($v,$p)) 1
779 incr ntot
780 set b [lindex $vdownptr($v) $a]
781 if {$b == 0} {
782 while {$a != 0} {
783 set b [lindex $vleftptr($v) $a]
784 if {$b != 0} break
785 set a [lindex $vupptr($v) $a]
788 set a $b
790 foreach a $todo {
791 if {![info exists kidchanged($a)]} continue
792 set id [lindex $varcstart($v) $a]
793 if {[llength $children($v,$id)] > 1} {
794 set children($v,$id) [lsort -command [list vtokcmp $v] \
795 $children($v,$id)]
797 set oldtok [lindex $varctok($v) $a]
798 if {!$vdatemode($v)} {
799 set tok {}
800 } else {
801 set tok $oldtok
803 set ka 0
804 set kid [last_real_child $v,$id]
805 if {$kid ne {}} {
806 set k $varcid($v,$kid)
807 if {[string compare [lindex $varctok($v) $k] $tok] > 0} {
808 set ki $kid
809 set ka $k
810 set tok [lindex $varctok($v) $k]
813 if {$ka != 0} {
814 set i [lsearch -exact $parents($v,$ki) $id]
815 set j [expr {[llength $parents($v,$ki)] - 1 - $i}]
816 append tok [strrep $j]
818 if {$tok eq $oldtok} {
819 continue
821 set id [lindex $varccommits($v,$a) end]
822 foreach p $parents($v,$id) {
823 if {[info exists varcid($v,$p)]} {
824 set kidchanged($varcid($v,$p)) 1
825 } else {
826 set sortkids($p) 1
829 lset varctok($v) $a $tok
830 set b [lindex $vupptr($v) $a]
831 if {$b != $ka} {
832 if {[string compare [lindex $varctok($v) $ka] $vtokmod($v)] < 0} {
833 modify_arc $v $ka
835 if {[string compare [lindex $varctok($v) $b] $vtokmod($v)] < 0} {
836 modify_arc $v $b
838 set c [lindex $vbackptr($v) $a]
839 set d [lindex $vleftptr($v) $a]
840 if {$c == 0} {
841 lset vdownptr($v) $b $d
842 } else {
843 lset vleftptr($v) $c $d
845 if {$d != 0} {
846 lset vbackptr($v) $d $c
848 if {[lindex $vlastins($v) $b] == $a} {
849 lset vlastins($v) $b $c
851 lset vupptr($v) $a $ka
852 set c [lindex $vlastins($v) $ka]
853 if {$c == 0 || \
854 [string compare $tok [lindex $varctok($v) $c]] < 0} {
855 set c $ka
856 set b [lindex $vdownptr($v) $ka]
857 } else {
858 set b [lindex $vleftptr($v) $c]
860 while {$b != 0 && \
861 [string compare $tok [lindex $varctok($v) $b]] >= 0} {
862 set c $b
863 set b [lindex $vleftptr($v) $c]
865 if {$c == $ka} {
866 lset vdownptr($v) $ka $a
867 lset vbackptr($v) $a 0
868 } else {
869 lset vleftptr($v) $c $a
870 lset vbackptr($v) $a $c
872 lset vleftptr($v) $a $b
873 if {$b != 0} {
874 lset vbackptr($v) $b $a
876 lset vlastins($v) $ka $a
879 foreach id [array names sortkids] {
880 if {[llength $children($v,$id)] > 1} {
881 set children($v,$id) [lsort -command [list vtokcmp $v] \
882 $children($v,$id)]
885 set t2 [clock clicks -milliseconds]
886 #puts "renumbervarc did [llength $todo] of $ntot arcs in [expr {$t2-$t1}]ms"
889 # Fix up the graph after we have found out that in view $v,
890 # $p (a commit that we have already seen) is actually the parent
891 # of the last commit in arc $a.
892 proc fix_reversal {p a v} {
893 global varcid varcstart varctok vupptr
895 set pa $varcid($v,$p)
896 if {$p ne [lindex $varcstart($v) $pa]} {
897 splitvarc $p $v
898 set pa $varcid($v,$p)
900 # seeds always need to be renumbered
901 if {[lindex $vupptr($v) $pa] == 0 ||
902 [string compare [lindex $varctok($v) $a] \
903 [lindex $varctok($v) $pa]] > 0} {
904 renumbervarc $pa $v
908 proc insertrow {id p v} {
909 global cmitlisted children parents varcid varctok vtokmod
910 global varccommits ordertok commitidx numcommits curview
911 global targetid targetrow
913 readcommit $id
914 set vid $v,$id
915 set cmitlisted($vid) 1
916 set children($vid) {}
917 set parents($vid) [list $p]
918 set a [newvarc $v $id]
919 set varcid($vid) $a
920 if {[string compare [lindex $varctok($v) $a] $vtokmod($v)] < 0} {
921 modify_arc $v $a
923 lappend varccommits($v,$a) $id
924 set vp $v,$p
925 if {[llength [lappend children($vp) $id]] > 1} {
926 set children($vp) [lsort -command [list vtokcmp $v] $children($vp)]
927 catch {unset ordertok}
929 fix_reversal $p $a $v
930 incr commitidx($v)
931 if {$v == $curview} {
932 set numcommits $commitidx($v)
933 setcanvscroll
934 if {[info exists targetid]} {
935 if {![comes_before $targetid $p]} {
936 incr targetrow
942 proc insertfakerow {id p} {
943 global varcid varccommits parents children cmitlisted
944 global commitidx varctok vtokmod targetid targetrow curview numcommits
946 set v $curview
947 set a $varcid($v,$p)
948 set i [lsearch -exact $varccommits($v,$a) $p]
949 if {$i < 0} {
950 puts "oops: insertfakerow can't find [shortids $p] on arc $a"
951 return
953 set children($v,$id) {}
954 set parents($v,$id) [list $p]
955 set varcid($v,$id) $a
956 lappend children($v,$p) $id
957 set cmitlisted($v,$id) 1
958 set numcommits [incr commitidx($v)]
959 # note we deliberately don't update varcstart($v) even if $i == 0
960 set varccommits($v,$a) [linsert $varccommits($v,$a) $i $id]
961 modify_arc $v $a $i
962 if {[info exists targetid]} {
963 if {![comes_before $targetid $p]} {
964 incr targetrow
967 setcanvscroll
968 drawvisible
971 proc removefakerow {id} {
972 global varcid varccommits parents children commitidx
973 global varctok vtokmod cmitlisted currentid selectedline
974 global targetid curview numcommits
976 set v $curview
977 if {[llength $parents($v,$id)] != 1} {
978 puts "oops: removefakerow [shortids $id] has [llength $parents($v,$id)] parents"
979 return
981 set p [lindex $parents($v,$id) 0]
982 set a $varcid($v,$id)
983 set i [lsearch -exact $varccommits($v,$a) $id]
984 if {$i < 0} {
985 puts "oops: removefakerow can't find [shortids $id] on arc $a"
986 return
988 unset varcid($v,$id)
989 set varccommits($v,$a) [lreplace $varccommits($v,$a) $i $i]
990 unset parents($v,$id)
991 unset children($v,$id)
992 unset cmitlisted($v,$id)
993 set numcommits [incr commitidx($v) -1]
994 set j [lsearch -exact $children($v,$p) $id]
995 if {$j >= 0} {
996 set children($v,$p) [lreplace $children($v,$p) $j $j]
998 modify_arc $v $a $i
999 if {[info exist currentid] && $id eq $currentid} {
1000 unset currentid
1001 set selectedline {}
1003 if {[info exists targetid] && $targetid eq $id} {
1004 set targetid $p
1006 setcanvscroll
1007 drawvisible
1010 proc real_children {vp} {
1011 global children nullid nullid2
1013 set kids {}
1014 foreach id $children($vp) {
1015 if {$id ne $nullid && $id ne $nullid2} {
1016 lappend kids $id
1019 return $kids
1022 proc first_real_child {vp} {
1023 global children nullid nullid2
1025 foreach id $children($vp) {
1026 if {$id ne $nullid && $id ne $nullid2} {
1027 return $id
1030 return {}
1033 proc last_real_child {vp} {
1034 global children nullid nullid2
1036 set kids $children($vp)
1037 for {set i [llength $kids]} {[incr i -1] >= 0} {} {
1038 set id [lindex $kids $i]
1039 if {$id ne $nullid && $id ne $nullid2} {
1040 return $id
1043 return {}
1046 proc vtokcmp {v a b} {
1047 global varctok varcid
1049 return [string compare [lindex $varctok($v) $varcid($v,$a)] \
1050 [lindex $varctok($v) $varcid($v,$b)]]
1053 # This assumes that if lim is not given, the caller has checked that
1054 # arc a's token is less than $vtokmod($v)
1055 proc modify_arc {v a {lim {}}} {
1056 global varctok vtokmod varcmod varcrow vupptr curview vrowmod varccommits
1058 if {$lim ne {}} {
1059 set c [string compare [lindex $varctok($v) $a] $vtokmod($v)]
1060 if {$c > 0} return
1061 if {$c == 0} {
1062 set r [lindex $varcrow($v) $a]
1063 if {$r ne {} && $vrowmod($v) <= $r + $lim} return
1066 set vtokmod($v) [lindex $varctok($v) $a]
1067 set varcmod($v) $a
1068 if {$v == $curview} {
1069 while {$a != 0 && [lindex $varcrow($v) $a] eq {}} {
1070 set a [lindex $vupptr($v) $a]
1071 set lim {}
1073 set r 0
1074 if {$a != 0} {
1075 if {$lim eq {}} {
1076 set lim [llength $varccommits($v,$a)]
1078 set r [expr {[lindex $varcrow($v) $a] + $lim}]
1080 set vrowmod($v) $r
1081 undolayout $r
1085 proc update_arcrows {v} {
1086 global vtokmod varcmod vrowmod varcrow commitidx currentid selectedline
1087 global varcid vrownum varcorder varcix varccommits
1088 global vupptr vdownptr vleftptr varctok
1089 global displayorder parentlist curview cached_commitrow
1091 if {$vrowmod($v) == $commitidx($v)} return
1092 if {$v == $curview} {
1093 if {[llength $displayorder] > $vrowmod($v)} {
1094 set displayorder [lrange $displayorder 0 [expr {$vrowmod($v) - 1}]]
1095 set parentlist [lrange $parentlist 0 [expr {$vrowmod($v) - 1}]]
1097 catch {unset cached_commitrow}
1099 set narctot [expr {[llength $varctok($v)] - 1}]
1100 set a $varcmod($v)
1101 while {$a != 0 && [lindex $varcix($v) $a] eq {}} {
1102 # go up the tree until we find something that has a row number,
1103 # or we get to a seed
1104 set a [lindex $vupptr($v) $a]
1106 if {$a == 0} {
1107 set a [lindex $vdownptr($v) 0]
1108 if {$a == 0} return
1109 set vrownum($v) {0}
1110 set varcorder($v) [list $a]
1111 lset varcix($v) $a 0
1112 lset varcrow($v) $a 0
1113 set arcn 0
1114 set row 0
1115 } else {
1116 set arcn [lindex $varcix($v) $a]
1117 if {[llength $vrownum($v)] > $arcn + 1} {
1118 set vrownum($v) [lrange $vrownum($v) 0 $arcn]
1119 set varcorder($v) [lrange $varcorder($v) 0 $arcn]
1121 set row [lindex $varcrow($v) $a]
1123 while {1} {
1124 set p $a
1125 incr row [llength $varccommits($v,$a)]
1126 # go down if possible
1127 set b [lindex $vdownptr($v) $a]
1128 if {$b == 0} {
1129 # if not, go left, or go up until we can go left
1130 while {$a != 0} {
1131 set b [lindex $vleftptr($v) $a]
1132 if {$b != 0} break
1133 set a [lindex $vupptr($v) $a]
1135 if {$a == 0} break
1137 set a $b
1138 incr arcn
1139 lappend vrownum($v) $row
1140 lappend varcorder($v) $a
1141 lset varcix($v) $a $arcn
1142 lset varcrow($v) $a $row
1144 set vtokmod($v) [lindex $varctok($v) $p]
1145 set varcmod($v) $p
1146 set vrowmod($v) $row
1147 if {[info exists currentid]} {
1148 set selectedline [rowofcommit $currentid]
1152 # Test whether view $v contains commit $id
1153 proc commitinview {id v} {
1154 global varcid
1156 return [info exists varcid($v,$id)]
1159 # Return the row number for commit $id in the current view
1160 proc rowofcommit {id} {
1161 global varcid varccommits varcrow curview cached_commitrow
1162 global varctok vtokmod
1164 set v $curview
1165 if {![info exists varcid($v,$id)]} {
1166 puts "oops rowofcommit no arc for [shortids $id]"
1167 return {}
1169 set a $varcid($v,$id)
1170 if {[string compare [lindex $varctok($v) $a] $vtokmod($v)] >= 0} {
1171 update_arcrows $v
1173 if {[info exists cached_commitrow($id)]} {
1174 return $cached_commitrow($id)
1176 set i [lsearch -exact $varccommits($v,$a) $id]
1177 if {$i < 0} {
1178 puts "oops didn't find commit [shortids $id] in arc $a"
1179 return {}
1181 incr i [lindex $varcrow($v) $a]
1182 set cached_commitrow($id) $i
1183 return $i
1186 # Returns 1 if a is on an earlier row than b, otherwise 0
1187 proc comes_before {a b} {
1188 global varcid varctok curview
1190 set v $curview
1191 if {$a eq $b || ![info exists varcid($v,$a)] || \
1192 ![info exists varcid($v,$b)]} {
1193 return 0
1195 if {$varcid($v,$a) != $varcid($v,$b)} {
1196 return [expr {[string compare [lindex $varctok($v) $varcid($v,$a)] \
1197 [lindex $varctok($v) $varcid($v,$b)]] < 0}]
1199 return [expr {[rowofcommit $a] < [rowofcommit $b]}]
1202 proc bsearch {l elt} {
1203 if {[llength $l] == 0 || $elt <= [lindex $l 0]} {
1204 return 0
1206 set lo 0
1207 set hi [llength $l]
1208 while {$hi - $lo > 1} {
1209 set mid [expr {int(($lo + $hi) / 2)}]
1210 set t [lindex $l $mid]
1211 if {$elt < $t} {
1212 set hi $mid
1213 } elseif {$elt > $t} {
1214 set lo $mid
1215 } else {
1216 return $mid
1219 return $lo
1222 # Make sure rows $start..$end-1 are valid in displayorder and parentlist
1223 proc make_disporder {start end} {
1224 global vrownum curview commitidx displayorder parentlist
1225 global varccommits varcorder parents vrowmod varcrow
1226 global d_valid_start d_valid_end
1228 if {$end > $vrowmod($curview)} {
1229 update_arcrows $curview
1231 set ai [bsearch $vrownum($curview) $start]
1232 set start [lindex $vrownum($curview) $ai]
1233 set narc [llength $vrownum($curview)]
1234 for {set r $start} {$ai < $narc && $r < $end} {incr ai} {
1235 set a [lindex $varcorder($curview) $ai]
1236 set l [llength $displayorder]
1237 set al [llength $varccommits($curview,$a)]
1238 if {$l < $r + $al} {
1239 if {$l < $r} {
1240 set pad [ntimes [expr {$r - $l}] {}]
1241 set displayorder [concat $displayorder $pad]
1242 set parentlist [concat $parentlist $pad]
1243 } elseif {$l > $r} {
1244 set displayorder [lrange $displayorder 0 [expr {$r - 1}]]
1245 set parentlist [lrange $parentlist 0 [expr {$r - 1}]]
1247 foreach id $varccommits($curview,$a) {
1248 lappend displayorder $id
1249 lappend parentlist $parents($curview,$id)
1251 } elseif {[lindex $displayorder [expr {$r + $al - 1}]] eq {}} {
1252 set i $r
1253 foreach id $varccommits($curview,$a) {
1254 lset displayorder $i $id
1255 lset parentlist $i $parents($curview,$id)
1256 incr i
1259 incr r $al
1263 proc commitonrow {row} {
1264 global displayorder
1266 set id [lindex $displayorder $row]
1267 if {$id eq {}} {
1268 make_disporder $row [expr {$row + 1}]
1269 set id [lindex $displayorder $row]
1271 return $id
1274 proc closevarcs {v} {
1275 global varctok varccommits varcid parents children
1276 global cmitlisted commitidx vtokmod
1278 set missing_parents 0
1279 set scripts {}
1280 set narcs [llength $varctok($v)]
1281 for {set a 1} {$a < $narcs} {incr a} {
1282 set id [lindex $varccommits($v,$a) end]
1283 foreach p $parents($v,$id) {
1284 if {[info exists varcid($v,$p)]} continue
1285 # add p as a new commit
1286 incr missing_parents
1287 set cmitlisted($v,$p) 0
1288 set parents($v,$p) {}
1289 if {[llength $children($v,$p)] == 1 &&
1290 [llength $parents($v,$id)] == 1} {
1291 set b $a
1292 } else {
1293 set b [newvarc $v $p]
1295 set varcid($v,$p) $b
1296 if {[string compare [lindex $varctok($v) $b] $vtokmod($v)] < 0} {
1297 modify_arc $v $b
1299 lappend varccommits($v,$b) $p
1300 incr commitidx($v)
1301 set scripts [check_interest $p $scripts]
1304 if {$missing_parents > 0} {
1305 foreach s $scripts {
1306 eval $s
1311 # Use $rwid as a substitute for $id, i.e. reparent $id's children to $rwid
1312 # Assumes we already have an arc for $rwid.
1313 proc rewrite_commit {v id rwid} {
1314 global children parents varcid varctok vtokmod varccommits
1316 foreach ch $children($v,$id) {
1317 # make $rwid be $ch's parent in place of $id
1318 set i [lsearch -exact $parents($v,$ch) $id]
1319 if {$i < 0} {
1320 puts "oops rewrite_commit didn't find $id in parent list for $ch"
1322 set parents($v,$ch) [lreplace $parents($v,$ch) $i $i $rwid]
1323 # add $ch to $rwid's children and sort the list if necessary
1324 if {[llength [lappend children($v,$rwid) $ch]] > 1} {
1325 set children($v,$rwid) [lsort -command [list vtokcmp $v] \
1326 $children($v,$rwid)]
1328 # fix the graph after joining $id to $rwid
1329 set a $varcid($v,$ch)
1330 fix_reversal $rwid $a $v
1331 # parentlist is wrong for the last element of arc $a
1332 # even if displayorder is right, hence the 3rd arg here
1333 modify_arc $v $a [expr {[llength $varccommits($v,$a)] - 1}]
1337 # Mechanism for registering a command to be executed when we come
1338 # across a particular commit. To handle the case when only the
1339 # prefix of the commit is known, the commitinterest array is now
1340 # indexed by the first 4 characters of the ID. Each element is a
1341 # list of id, cmd pairs.
1342 proc interestedin {id cmd} {
1343 global commitinterest
1345 lappend commitinterest([string range $id 0 3]) $id $cmd
1348 proc check_interest {id scripts} {
1349 global commitinterest
1351 set prefix [string range $id 0 3]
1352 if {[info exists commitinterest($prefix)]} {
1353 set newlist {}
1354 foreach {i script} $commitinterest($prefix) {
1355 if {[string match "$i*" $id]} {
1356 lappend scripts [string map [list "%I" $id "%P" $i] $script]
1357 } else {
1358 lappend newlist $i $script
1361 if {$newlist ne {}} {
1362 set commitinterest($prefix) $newlist
1363 } else {
1364 unset commitinterest($prefix)
1367 return $scripts
1370 proc getcommitlines {fd inst view updating} {
1371 global cmitlisted leftover
1372 global commitidx commitdata vdatemode
1373 global parents children curview hlview
1374 global idpending ordertok
1375 global varccommits varcid varctok vtokmod vfilelimit
1377 set stuff [read $fd 500000]
1378 # git log doesn't terminate the last commit with a null...
1379 if {$stuff == {} && $leftover($inst) ne {} && [eof $fd]} {
1380 set stuff "\0"
1382 if {$stuff == {}} {
1383 if {![eof $fd]} {
1384 return 1
1386 global commfd viewcomplete viewactive viewname
1387 global viewinstances
1388 unset commfd($inst)
1389 set i [lsearch -exact $viewinstances($view) $inst]
1390 if {$i >= 0} {
1391 set viewinstances($view) [lreplace $viewinstances($view) $i $i]
1393 # set it blocking so we wait for the process to terminate
1394 fconfigure $fd -blocking 1
1395 if {[catch {close $fd} err]} {
1396 set fv {}
1397 if {$view != $curview} {
1398 set fv " for the \"$viewname($view)\" view"
1400 if {[string range $err 0 4] == "usage"} {
1401 set err "Gitk: error reading commits$fv:\
1402 bad arguments to git log."
1403 if {$viewname($view) eq "Command line"} {
1404 append err \
1405 " (Note: arguments to gitk are passed to git log\
1406 to allow selection of commits to be displayed.)"
1408 } else {
1409 set err "Error reading commits$fv: $err"
1411 error_popup $err
1413 if {[incr viewactive($view) -1] <= 0} {
1414 set viewcomplete($view) 1
1415 # Check if we have seen any ids listed as parents that haven't
1416 # appeared in the list
1417 closevarcs $view
1418 notbusy $view
1420 if {$view == $curview} {
1421 run chewcommits
1423 return 0
1425 set start 0
1426 set gotsome 0
1427 set scripts {}
1428 while 1 {
1429 set i [string first "\0" $stuff $start]
1430 if {$i < 0} {
1431 append leftover($inst) [string range $stuff $start end]
1432 break
1434 if {$start == 0} {
1435 set cmit $leftover($inst)
1436 append cmit [string range $stuff 0 [expr {$i - 1}]]
1437 set leftover($inst) {}
1438 } else {
1439 set cmit [string range $stuff $start [expr {$i - 1}]]
1441 set start [expr {$i + 1}]
1442 set j [string first "\n" $cmit]
1443 set ok 0
1444 set listed 1
1445 if {$j >= 0 && [string match "commit *" $cmit]} {
1446 set ids [string range $cmit 7 [expr {$j - 1}]]
1447 if {[string match {[-^<>]*} $ids]} {
1448 switch -- [string index $ids 0] {
1449 "-" {set listed 0}
1450 "^" {set listed 2}
1451 "<" {set listed 3}
1452 ">" {set listed 4}
1454 set ids [string range $ids 1 end]
1456 set ok 1
1457 foreach id $ids {
1458 if {[string length $id] != 40} {
1459 set ok 0
1460 break
1464 if {!$ok} {
1465 set shortcmit $cmit
1466 if {[string length $shortcmit] > 80} {
1467 set shortcmit "[string range $shortcmit 0 80]..."
1469 error_popup "[mc "Can't parse git log output:"] {$shortcmit}"
1470 exit 1
1472 set id [lindex $ids 0]
1473 set vid $view,$id
1475 if {!$listed && $updating && ![info exists varcid($vid)] &&
1476 $vfilelimit($view) ne {}} {
1477 # git log doesn't rewrite parents for unlisted commits
1478 # when doing path limiting, so work around that here
1479 # by working out the rewritten parent with git rev-list
1480 # and if we already know about it, using the rewritten
1481 # parent as a substitute parent for $id's children.
1482 if {![catch {
1483 set rwid [exec git rev-list --first-parent --max-count=1 \
1484 $id -- $vfilelimit($view)]
1485 }]} {
1486 if {$rwid ne {} && [info exists varcid($view,$rwid)]} {
1487 # use $rwid in place of $id
1488 rewrite_commit $view $id $rwid
1489 continue
1494 set a 0
1495 if {[info exists varcid($vid)]} {
1496 if {$cmitlisted($vid) || !$listed} continue
1497 set a $varcid($vid)
1499 if {$listed} {
1500 set olds [lrange $ids 1 end]
1501 } else {
1502 set olds {}
1504 set commitdata($id) [string range $cmit [expr {$j + 1}] end]
1505 set cmitlisted($vid) $listed
1506 set parents($vid) $olds
1507 if {![info exists children($vid)]} {
1508 set children($vid) {}
1509 } elseif {$a == 0 && [llength $children($vid)] == 1} {
1510 set k [lindex $children($vid) 0]
1511 if {[llength $parents($view,$k)] == 1 &&
1512 (!$vdatemode($view) ||
1513 $varcid($view,$k) == [llength $varctok($view)] - 1)} {
1514 set a $varcid($view,$k)
1517 if {$a == 0} {
1518 # new arc
1519 set a [newvarc $view $id]
1521 if {[string compare [lindex $varctok($view) $a] $vtokmod($view)] < 0} {
1522 modify_arc $view $a
1524 if {![info exists varcid($vid)]} {
1525 set varcid($vid) $a
1526 lappend varccommits($view,$a) $id
1527 incr commitidx($view)
1530 set i 0
1531 foreach p $olds {
1532 if {$i == 0 || [lsearch -exact $olds $p] >= $i} {
1533 set vp $view,$p
1534 if {[llength [lappend children($vp) $id]] > 1 &&
1535 [vtokcmp $view [lindex $children($vp) end-1] $id] > 0} {
1536 set children($vp) [lsort -command [list vtokcmp $view] \
1537 $children($vp)]
1538 catch {unset ordertok}
1540 if {[info exists varcid($view,$p)]} {
1541 fix_reversal $p $a $view
1544 incr i
1547 set scripts [check_interest $id $scripts]
1548 set gotsome 1
1550 if {$gotsome} {
1551 global numcommits hlview
1553 if {$view == $curview} {
1554 set numcommits $commitidx($view)
1555 run chewcommits
1557 if {[info exists hlview] && $view == $hlview} {
1558 # we never actually get here...
1559 run vhighlightmore
1561 foreach s $scripts {
1562 eval $s
1565 return 2
1568 proc chewcommits {} {
1569 global curview hlview viewcomplete
1570 global pending_select
1572 layoutmore
1573 if {$viewcomplete($curview)} {
1574 global commitidx varctok
1575 global numcommits startmsecs
1577 if {[info exists pending_select]} {
1578 update
1579 reset_pending_select {}
1581 if {[commitinview $pending_select $curview]} {
1582 selectline [rowofcommit $pending_select] 1
1583 } else {
1584 set row [first_real_row]
1585 selectline $row 1
1588 if {$commitidx($curview) > 0} {
1589 #set ms [expr {[clock clicks -milliseconds] - $startmsecs}]
1590 #puts "overall $ms ms for $numcommits commits"
1591 #puts "[llength $varctok($view)] arcs, $commitidx($view) commits"
1592 } else {
1593 show_status [mc "No commits selected"]
1595 notbusy layout
1597 return 0
1600 proc do_readcommit {id} {
1601 global tclencoding
1603 # Invoke git-log to handle automatic encoding conversion
1604 set fd [open [concat | git log --no-color --pretty=raw -1 $id] r]
1605 # Read the results using i18n.logoutputencoding
1606 fconfigure $fd -translation lf -eofchar {}
1607 if {$tclencoding != {}} {
1608 fconfigure $fd -encoding $tclencoding
1610 set contents [read $fd]
1611 close $fd
1612 # Remove the heading line
1613 regsub {^commit [0-9a-f]+\n} $contents {} contents
1615 return $contents
1618 proc readcommit {id} {
1619 if {[catch {set contents [do_readcommit $id]}]} return
1620 parsecommit $id $contents 1
1623 proc parsecommit {id contents listed} {
1624 global commitinfo cdate
1626 set inhdr 1
1627 set comment {}
1628 set headline {}
1629 set auname {}
1630 set audate {}
1631 set comname {}
1632 set comdate {}
1633 set hdrend [string first "\n\n" $contents]
1634 if {$hdrend < 0} {
1635 # should never happen...
1636 set hdrend [string length $contents]
1638 set header [string range $contents 0 [expr {$hdrend - 1}]]
1639 set comment [string range $contents [expr {$hdrend + 2}] end]
1640 foreach line [split $header "\n"] {
1641 set line [split $line " "]
1642 set tag [lindex $line 0]
1643 if {$tag == "author"} {
1644 set audate [lindex $line end-1]
1645 set auname [join [lrange $line 1 end-2] " "]
1646 } elseif {$tag == "committer"} {
1647 set comdate [lindex $line end-1]
1648 set comname [join [lrange $line 1 end-2] " "]
1651 set headline {}
1652 # take the first non-blank line of the comment as the headline
1653 set headline [string trimleft $comment]
1654 set i [string first "\n" $headline]
1655 if {$i >= 0} {
1656 set headline [string range $headline 0 $i]
1658 set headline [string trimright $headline]
1659 set i [string first "\r" $headline]
1660 if {$i >= 0} {
1661 set headline [string trimright [string range $headline 0 $i]]
1663 if {!$listed} {
1664 # git log indents the comment by 4 spaces;
1665 # if we got this via git cat-file, add the indentation
1666 set newcomment {}
1667 foreach line [split $comment "\n"] {
1668 append newcomment " "
1669 append newcomment $line
1670 append newcomment "\n"
1672 set comment $newcomment
1674 if {$comdate != {}} {
1675 set cdate($id) $comdate
1677 set commitinfo($id) [list $headline $auname $audate \
1678 $comname $comdate $comment]
1681 proc getcommit {id} {
1682 global commitdata commitinfo
1684 if {[info exists commitdata($id)]} {
1685 parsecommit $id $commitdata($id) 1
1686 } else {
1687 readcommit $id
1688 if {![info exists commitinfo($id)]} {
1689 set commitinfo($id) [list [mc "No commit information available"]]
1692 return 1
1695 # Expand an abbreviated commit ID to a list of full 40-char IDs that match
1696 # and are present in the current view.
1697 # This is fairly slow...
1698 proc longid {prefix} {
1699 global varcid curview
1701 set ids {}
1702 foreach match [array names varcid "$curview,$prefix*"] {
1703 lappend ids [lindex [split $match ","] 1]
1705 return $ids
1708 proc readrefs {} {
1709 global tagids idtags headids idheads tagobjid
1710 global otherrefids idotherrefs mainhead mainheadid
1711 global selecthead selectheadid
1712 global hideremotes
1714 foreach v {tagids idtags headids idheads otherrefids idotherrefs} {
1715 catch {unset $v}
1717 set refd [open [list | git show-ref -d] r]
1718 while {[gets $refd line] >= 0} {
1719 if {[string index $line 40] ne " "} continue
1720 set id [string range $line 0 39]
1721 set ref [string range $line 41 end]
1722 if {![string match "refs/*" $ref]} continue
1723 set name [string range $ref 5 end]
1724 if {[string match "remotes/*" $name]} {
1725 if {![string match "*/HEAD" $name] && !$hideremotes} {
1726 set headids($name) $id
1727 lappend idheads($id) $name
1729 } elseif {[string match "heads/*" $name]} {
1730 set name [string range $name 6 end]
1731 set headids($name) $id
1732 lappend idheads($id) $name
1733 } elseif {[string match "tags/*" $name]} {
1734 # this lets refs/tags/foo^{} overwrite refs/tags/foo,
1735 # which is what we want since the former is the commit ID
1736 set name [string range $name 5 end]
1737 if {[string match "*^{}" $name]} {
1738 set name [string range $name 0 end-3]
1739 } else {
1740 set tagobjid($name) $id
1742 set tagids($name) $id
1743 lappend idtags($id) $name
1744 } else {
1745 set otherrefids($name) $id
1746 lappend idotherrefs($id) $name
1749 catch {close $refd}
1750 set mainhead {}
1751 set mainheadid {}
1752 catch {
1753 set mainheadid [exec git rev-parse HEAD]
1754 set thehead [exec git symbolic-ref HEAD]
1755 if {[string match "refs/heads/*" $thehead]} {
1756 set mainhead [string range $thehead 11 end]
1759 set selectheadid {}
1760 if {$selecthead ne {}} {
1761 catch {
1762 set selectheadid [exec git rev-parse --verify $selecthead]
1767 # skip over fake commits
1768 proc first_real_row {} {
1769 global nullid nullid2 numcommits
1771 for {set row 0} {$row < $numcommits} {incr row} {
1772 set id [commitonrow $row]
1773 if {$id ne $nullid && $id ne $nullid2} {
1774 break
1777 return $row
1780 # update things for a head moved to a child of its previous location
1781 proc movehead {id name} {
1782 global headids idheads
1784 removehead $headids($name) $name
1785 set headids($name) $id
1786 lappend idheads($id) $name
1789 # update things when a head has been removed
1790 proc removehead {id name} {
1791 global headids idheads
1793 if {$idheads($id) eq $name} {
1794 unset idheads($id)
1795 } else {
1796 set i [lsearch -exact $idheads($id) $name]
1797 if {$i >= 0} {
1798 set idheads($id) [lreplace $idheads($id) $i $i]
1801 unset headids($name)
1804 proc ttk_toplevel {w args} {
1805 global use_ttk
1806 eval [linsert $args 0 ::toplevel $w]
1807 if {$use_ttk} {
1808 place [ttk::frame $w._toplevel_background] -x 0 -y 0 -relwidth 1 -relheight 1
1810 return $w
1813 proc make_transient {window origin} {
1814 global have_tk85
1816 # In MacOS Tk 8.4 transient appears to work by setting
1817 # overrideredirect, which is utterly useless, since the
1818 # windows get no border, and are not even kept above
1819 # the parent.
1820 if {!$have_tk85 && [tk windowingsystem] eq {aqua}} return
1822 wm transient $window $origin
1824 # Windows fails to place transient windows normally, so
1825 # schedule a callback to center them on the parent.
1826 if {[tk windowingsystem] eq {win32}} {
1827 after idle [list tk::PlaceWindow $window widget $origin]
1831 proc show_error {w top msg {mc mc}} {
1832 global NS
1833 if {![info exists NS]} {set NS ""}
1834 if {[wm state $top] eq "withdrawn"} { wm deiconify $top }
1835 message $w.m -text $msg -justify center -aspect 400
1836 pack $w.m -side top -fill x -padx 20 -pady 20
1837 ${NS}::button $w.ok -default active -text [$mc OK] -command "destroy $top"
1838 pack $w.ok -side bottom -fill x
1839 bind $top <Visibility> "grab $top; focus $top"
1840 bind $top <Key-Return> "destroy $top"
1841 bind $top <Key-space> "destroy $top"
1842 bind $top <Key-Escape> "destroy $top"
1843 tkwait window $top
1846 proc error_popup {msg {owner .}} {
1847 if {[tk windowingsystem] eq "win32"} {
1848 tk_messageBox -icon error -type ok -title [wm title .] \
1849 -parent $owner -message $msg
1850 } else {
1851 set w .error
1852 ttk_toplevel $w
1853 make_transient $w $owner
1854 show_error $w $w $msg
1858 proc confirm_popup {msg {owner .}} {
1859 global confirm_ok NS
1860 set confirm_ok 0
1861 set w .confirm
1862 ttk_toplevel $w
1863 make_transient $w $owner
1864 message $w.m -text $msg -justify center -aspect 400
1865 pack $w.m -side top -fill x -padx 20 -pady 20
1866 ${NS}::button $w.ok -text [mc OK] -command "set confirm_ok 1; destroy $w"
1867 pack $w.ok -side left -fill x
1868 ${NS}::button $w.cancel -text [mc Cancel] -command "destroy $w"
1869 pack $w.cancel -side right -fill x
1870 bind $w <Visibility> "grab $w; focus $w"
1871 bind $w <Key-Return> "set confirm_ok 1; destroy $w"
1872 bind $w <Key-space> "set confirm_ok 1; destroy $w"
1873 bind $w <Key-Escape> "destroy $w"
1874 tk::PlaceWindow $w widget $owner
1875 tkwait window $w
1876 return $confirm_ok
1879 proc setoptions {} {
1880 if {[tk windowingsystem] ne "win32"} {
1881 option add *Panedwindow.showHandle 1 startupFile
1882 option add *Panedwindow.sashRelief raised startupFile
1883 if {[tk windowingsystem] ne "aqua"} {
1884 option add *Menu.font uifont startupFile
1886 } else {
1887 option add *Menu.TearOff 0 startupFile
1889 option add *Button.font uifont startupFile
1890 option add *Checkbutton.font uifont startupFile
1891 option add *Radiobutton.font uifont startupFile
1892 option add *Menubutton.font uifont startupFile
1893 option add *Label.font uifont startupFile
1894 option add *Message.font uifont startupFile
1895 option add *Entry.font textfont startupFile
1896 option add *Text.font textfont startupFile
1897 option add *Labelframe.font uifont startupFile
1898 option add *Spinbox.font textfont startupFile
1899 option add *Listbox.font mainfont startupFile
1902 # Make a menu and submenus.
1903 # m is the window name for the menu, items is the list of menu items to add.
1904 # Each item is a list {mc label type description options...}
1905 # mc is ignored; it's so we can put mc there to alert xgettext
1906 # label is the string that appears in the menu
1907 # type is cascade, command or radiobutton (should add checkbutton)
1908 # description depends on type; it's the sublist for cascade, the
1909 # command to invoke for command, or {variable value} for radiobutton
1910 proc makemenu {m items} {
1911 menu $m
1912 if {[tk windowingsystem] eq {aqua}} {
1913 set Meta1 Cmd
1914 } else {
1915 set Meta1 Ctrl
1917 foreach i $items {
1918 set name [mc [lindex $i 1]]
1919 set type [lindex $i 2]
1920 set thing [lindex $i 3]
1921 set params [list $type]
1922 if {$name ne {}} {
1923 set u [string first "&" [string map {&& x} $name]]
1924 lappend params -label [string map {&& & & {}} $name]
1925 if {$u >= 0} {
1926 lappend params -underline $u
1929 switch -- $type {
1930 "cascade" {
1931 set submenu [string tolower [string map {& ""} [lindex $i 1]]]
1932 lappend params -menu $m.$submenu
1934 "command" {
1935 lappend params -command $thing
1937 "radiobutton" {
1938 lappend params -variable [lindex $thing 0] \
1939 -value [lindex $thing 1]
1942 set tail [lrange $i 4 end]
1943 regsub -all {\yMeta1\y} $tail $Meta1 tail
1944 eval $m add $params $tail
1945 if {$type eq "cascade"} {
1946 makemenu $m.$submenu $thing
1951 # translate string and remove ampersands
1952 proc mca {str} {
1953 return [string map {&& & & {}} [mc $str]]
1956 proc makedroplist {w varname args} {
1957 global use_ttk
1958 if {$use_ttk} {
1959 set width 0
1960 foreach label $args {
1961 set cx [string length $label]
1962 if {$cx > $width} {set width $cx}
1964 set gm [ttk::combobox $w -width $width -state readonly\
1965 -textvariable $varname -values $args]
1966 } else {
1967 set gm [eval [linsert $args 0 tk_optionMenu $w $varname]]
1969 return $gm
1972 proc makewindow {} {
1973 global canv canv2 canv3 linespc charspc ctext cflist cscroll
1974 global tabstop
1975 global findtype findtypemenu findloc findstring fstring geometry
1976 global entries sha1entry sha1string sha1but
1977 global diffcontextstring diffcontext
1978 global ignorespace
1979 global maincursor textcursor curtextcursor
1980 global rowctxmenu fakerowmenu mergemax wrapcomment
1981 global highlight_files gdttype
1982 global searchstring sstring
1983 global bgcolor fgcolor bglist fglist diffcolors selectbgcolor
1984 global headctxmenu progresscanv progressitem progresscoords statusw
1985 global fprogitem fprogcoord lastprogupdate progupdatepending
1986 global rprogitem rprogcoord rownumsel numcommits
1987 global have_tk85 use_ttk NS
1988 global git_version
1989 global worddiff
1991 # The "mc" arguments here are purely so that xgettext
1992 # sees the following string as needing to be translated
1993 set file {
1994 mc "File" cascade {
1995 {mc "Update" command updatecommits -accelerator F5}
1996 {mc "Reload" command reloadcommits -accelerator Meta1-F5}
1997 {mc "Reread references" command rereadrefs}
1998 {mc "List references" command showrefs -accelerator F2}
1999 {xx "" separator}
2000 {mc "Start git gui" command {exec git gui &}}
2001 {xx "" separator}
2002 {mc "Quit" command doquit -accelerator Meta1-Q}
2004 set edit {
2005 mc "Edit" cascade {
2006 {mc "Preferences" command doprefs}
2008 set view {
2009 mc "View" cascade {
2010 {mc "New view..." command {newview 0} -accelerator Shift-F4}
2011 {mc "Edit view..." command editview -state disabled -accelerator F4}
2012 {mc "Delete view" command delview -state disabled}
2013 {xx "" separator}
2014 {mc "All files" radiobutton {selectedview 0} -command {showview 0}}
2016 if {[tk windowingsystem] ne "aqua"} {
2017 set help {
2018 mc "Help" cascade {
2019 {mc "About gitk" command about}
2020 {mc "Key bindings" command keys}
2022 set bar [list $file $edit $view $help]
2023 } else {
2024 proc ::tk::mac::ShowPreferences {} {doprefs}
2025 proc ::tk::mac::Quit {} {doquit}
2026 lset file end [lreplace [lindex $file end] end-1 end]
2027 set apple {
2028 xx "Apple" cascade {
2029 {mc "About gitk" command about}
2030 {xx "" separator}
2032 set help {
2033 mc "Help" cascade {
2034 {mc "Key bindings" command keys}
2036 set bar [list $apple $file $view $help]
2038 makemenu .bar $bar
2039 . configure -menu .bar
2041 if {$use_ttk} {
2042 # cover the non-themed toplevel with a themed frame.
2043 place [ttk::frame ._main_background] -x 0 -y 0 -relwidth 1 -relheight 1
2046 # the gui has upper and lower half, parts of a paned window.
2047 ${NS}::panedwindow .ctop -orient vertical
2049 # possibly use assumed geometry
2050 if {![info exists geometry(pwsash0)]} {
2051 set geometry(topheight) [expr {15 * $linespc}]
2052 set geometry(topwidth) [expr {80 * $charspc}]
2053 set geometry(botheight) [expr {15 * $linespc}]
2054 set geometry(botwidth) [expr {50 * $charspc}]
2055 set geometry(pwsash0) [list [expr {40 * $charspc}] 2]
2056 set geometry(pwsash1) [list [expr {60 * $charspc}] 2]
2059 # the upper half will have a paned window, a scroll bar to the right, and some stuff below
2060 ${NS}::frame .tf -height $geometry(topheight) -width $geometry(topwidth)
2061 ${NS}::frame .tf.histframe
2062 ${NS}::panedwindow .tf.histframe.pwclist -orient horizontal
2063 if {!$use_ttk} {
2064 .tf.histframe.pwclist configure -sashpad 0 -handlesize 4
2067 # create three canvases
2068 set cscroll .tf.histframe.csb
2069 set canv .tf.histframe.pwclist.canv
2070 canvas $canv \
2071 -selectbackground $selectbgcolor \
2072 -background $bgcolor -bd 0 \
2073 -yscrollincr $linespc -yscrollcommand "scrollcanv $cscroll"
2074 .tf.histframe.pwclist add $canv
2075 set canv2 .tf.histframe.pwclist.canv2
2076 canvas $canv2 \
2077 -selectbackground $selectbgcolor \
2078 -background $bgcolor -bd 0 -yscrollincr $linespc
2079 .tf.histframe.pwclist add $canv2
2080 set canv3 .tf.histframe.pwclist.canv3
2081 canvas $canv3 \
2082 -selectbackground $selectbgcolor \
2083 -background $bgcolor -bd 0 -yscrollincr $linespc
2084 .tf.histframe.pwclist add $canv3
2085 if {$use_ttk} {
2086 bind .tf.histframe.pwclist <Map> {
2087 bind %W <Map> {}
2088 .tf.histframe.pwclist sashpos 1 [lindex $::geometry(pwsash1) 0]
2089 .tf.histframe.pwclist sashpos 0 [lindex $::geometry(pwsash0) 0]
2091 } else {
2092 eval .tf.histframe.pwclist sash place 0 $geometry(pwsash0)
2093 eval .tf.histframe.pwclist sash place 1 $geometry(pwsash1)
2096 # a scroll bar to rule them
2097 ${NS}::scrollbar $cscroll -command {allcanvs yview}
2098 if {!$use_ttk} {$cscroll configure -highlightthickness 0}
2099 pack $cscroll -side right -fill y
2100 bind .tf.histframe.pwclist <Configure> {resizeclistpanes %W %w}
2101 lappend bglist $canv $canv2 $canv3
2102 pack .tf.histframe.pwclist -fill both -expand 1 -side left
2104 # we have two button bars at bottom of top frame. Bar 1
2105 ${NS}::frame .tf.bar
2106 ${NS}::frame .tf.lbar -height 15
2108 set sha1entry .tf.bar.sha1
2109 set entries $sha1entry
2110 set sha1but .tf.bar.sha1label
2111 button $sha1but -text "[mc "SHA1 ID:"] " -state disabled -relief flat \
2112 -command gotocommit -width 8
2113 $sha1but conf -disabledforeground [$sha1but cget -foreground]
2114 pack .tf.bar.sha1label -side left
2115 ${NS}::entry $sha1entry -width 40 -font textfont -textvariable sha1string
2116 trace add variable sha1string write sha1change
2117 pack $sha1entry -side left -pady 2
2119 image create bitmap bm-left -data {
2120 #define left_width 16
2121 #define left_height 16
2122 static unsigned char left_bits[] = {
2123 0x00, 0x00, 0xc0, 0x01, 0xe0, 0x00, 0x70, 0x00, 0x38, 0x00, 0x1c, 0x00,
2124 0x0e, 0x00, 0xff, 0x7f, 0xff, 0x7f, 0xff, 0x7f, 0x0e, 0x00, 0x1c, 0x00,
2125 0x38, 0x00, 0x70, 0x00, 0xe0, 0x00, 0xc0, 0x01};
2127 image create bitmap bm-right -data {
2128 #define right_width 16
2129 #define right_height 16
2130 static unsigned char right_bits[] = {
2131 0x00, 0x00, 0xc0, 0x01, 0x80, 0x03, 0x00, 0x07, 0x00, 0x0e, 0x00, 0x1c,
2132 0x00, 0x38, 0xff, 0x7f, 0xff, 0x7f, 0xff, 0x7f, 0x00, 0x38, 0x00, 0x1c,
2133 0x00, 0x0e, 0x00, 0x07, 0x80, 0x03, 0xc0, 0x01};
2135 ${NS}::button .tf.bar.leftbut -image bm-left -command goback \
2136 -state disabled -width 26
2137 pack .tf.bar.leftbut -side left -fill y
2138 ${NS}::button .tf.bar.rightbut -image bm-right -command goforw \
2139 -state disabled -width 26
2140 pack .tf.bar.rightbut -side left -fill y
2142 ${NS}::label .tf.bar.rowlabel -text [mc "Row"]
2143 set rownumsel {}
2144 ${NS}::label .tf.bar.rownum -width 7 -textvariable rownumsel \
2145 -relief sunken -anchor e
2146 ${NS}::label .tf.bar.rowlabel2 -text "/"
2147 ${NS}::label .tf.bar.numcommits -width 7 -textvariable numcommits \
2148 -relief sunken -anchor e
2149 pack .tf.bar.rowlabel .tf.bar.rownum .tf.bar.rowlabel2 .tf.bar.numcommits \
2150 -side left
2151 if {!$use_ttk} {
2152 foreach w {rownum numcommits} {.tf.bar.$w configure -font textfont}
2154 global selectedline
2155 trace add variable selectedline write selectedline_change
2157 # Status label and progress bar
2158 set statusw .tf.bar.status
2159 ${NS}::label $statusw -width 15 -relief sunken
2160 pack $statusw -side left -padx 5
2161 if {$use_ttk} {
2162 set progresscanv [ttk::progressbar .tf.bar.progress]
2163 } else {
2164 set h [expr {[font metrics uifont -linespace] + 2}]
2165 set progresscanv .tf.bar.progress
2166 canvas $progresscanv -relief sunken -height $h -borderwidth 2
2167 set progressitem [$progresscanv create rect -1 0 0 $h -fill green]
2168 set fprogitem [$progresscanv create rect -1 0 0 $h -fill yellow]
2169 set rprogitem [$progresscanv create rect -1 0 0 $h -fill red]
2171 pack $progresscanv -side right -expand 1 -fill x -padx {0 2}
2172 set progresscoords {0 0}
2173 set fprogcoord 0
2174 set rprogcoord 0
2175 bind $progresscanv <Configure> adjustprogress
2176 set lastprogupdate [clock clicks -milliseconds]
2177 set progupdatepending 0
2179 # build up the bottom bar of upper window
2180 ${NS}::label .tf.lbar.flabel -text "[mc "Find"] "
2181 ${NS}::button .tf.lbar.fnext -text [mc "next"] -command {dofind 1 1}
2182 ${NS}::button .tf.lbar.fprev -text [mc "prev"] -command {dofind -1 1}
2183 ${NS}::label .tf.lbar.flab2 -text " [mc "commit"] "
2184 pack .tf.lbar.flabel .tf.lbar.fnext .tf.lbar.fprev .tf.lbar.flab2 \
2185 -side left -fill y
2186 set gdttype [mc "containing:"]
2187 set gm [makedroplist .tf.lbar.gdttype gdttype \
2188 [mc "containing:"] \
2189 [mc "touching paths:"] \
2190 [mc "adding/removing string:"]]
2191 trace add variable gdttype write gdttype_change
2192 pack .tf.lbar.gdttype -side left -fill y
2194 set findstring {}
2195 set fstring .tf.lbar.findstring
2196 lappend entries $fstring
2197 ${NS}::entry $fstring -width 30 -textvariable findstring
2198 trace add variable findstring write find_change
2199 set findtype [mc "Exact"]
2200 set findtypemenu [makedroplist .tf.lbar.findtype \
2201 findtype [mc "Exact"] [mc "IgnCase"] [mc "Regexp"]]
2202 trace add variable findtype write findcom_change
2203 set findloc [mc "All fields"]
2204 makedroplist .tf.lbar.findloc findloc [mc "All fields"] [mc "Headline"] \
2205 [mc "Comments"] [mc "Author"] [mc "Committer"]
2206 trace add variable findloc write find_change
2207 pack .tf.lbar.findloc -side right
2208 pack .tf.lbar.findtype -side right
2209 pack $fstring -side left -expand 1 -fill x
2211 # Finish putting the upper half of the viewer together
2212 pack .tf.lbar -in .tf -side bottom -fill x
2213 pack .tf.bar -in .tf -side bottom -fill x
2214 pack .tf.histframe -fill both -side top -expand 1
2215 .ctop add .tf
2216 if {!$use_ttk} {
2217 .ctop paneconfigure .tf -height $geometry(topheight)
2218 .ctop paneconfigure .tf -width $geometry(topwidth)
2221 # now build up the bottom
2222 ${NS}::panedwindow .pwbottom -orient horizontal
2224 # lower left, a text box over search bar, scroll bar to the right
2225 # if we know window height, then that will set the lower text height, otherwise
2226 # we set lower text height which will drive window height
2227 if {[info exists geometry(main)]} {
2228 ${NS}::frame .bleft -width $geometry(botwidth)
2229 } else {
2230 ${NS}::frame .bleft -width $geometry(botwidth) -height $geometry(botheight)
2232 ${NS}::frame .bleft.top
2233 ${NS}::frame .bleft.mid
2234 ${NS}::frame .bleft.bottom
2236 ${NS}::button .bleft.top.search -text [mc "Search"] -command dosearch
2237 pack .bleft.top.search -side left -padx 5
2238 set sstring .bleft.top.sstring
2239 set searchstring ""
2240 ${NS}::entry $sstring -width 20 -textvariable searchstring
2241 lappend entries $sstring
2242 trace add variable searchstring write incrsearch
2243 pack $sstring -side left -expand 1 -fill x
2244 ${NS}::radiobutton .bleft.mid.diff -text [mc "Diff"] \
2245 -command changediffdisp -variable diffelide -value {0 0}
2246 ${NS}::radiobutton .bleft.mid.old -text [mc "Old version"] \
2247 -command changediffdisp -variable diffelide -value {0 1}
2248 ${NS}::radiobutton .bleft.mid.new -text [mc "New version"] \
2249 -command changediffdisp -variable diffelide -value {1 0}
2250 ${NS}::label .bleft.mid.labeldiffcontext -text " [mc "Lines of context"]: "
2251 pack .bleft.mid.diff .bleft.mid.old .bleft.mid.new -side left
2252 spinbox .bleft.mid.diffcontext -width 5 \
2253 -from 0 -increment 1 -to 10000000 \
2254 -validate all -validatecommand "diffcontextvalidate %P" \
2255 -textvariable diffcontextstring
2256 .bleft.mid.diffcontext set $diffcontext
2257 trace add variable diffcontextstring write diffcontextchange
2258 lappend entries .bleft.mid.diffcontext
2259 pack .bleft.mid.labeldiffcontext .bleft.mid.diffcontext -side left
2260 ${NS}::checkbutton .bleft.mid.ignspace -text [mc "Ignore space change"] \
2261 -command changeignorespace -variable ignorespace
2262 pack .bleft.mid.ignspace -side left -padx 5
2264 set worddiff [mc "Line diff"]
2265 if {[package vcompare $git_version "1.7.2"] >= 0} {
2266 makedroplist .bleft.mid.worddiff worddiff [mc "Line diff"] \
2267 [mc "Markup words"] [mc "Color words"]
2268 trace add variable worddiff write changeworddiff
2269 pack .bleft.mid.worddiff -side left -padx 5
2272 set ctext .bleft.bottom.ctext
2273 text $ctext -background $bgcolor -foreground $fgcolor \
2274 -state disabled -font textfont \
2275 -yscrollcommand scrolltext -wrap none \
2276 -xscrollcommand ".bleft.bottom.sbhorizontal set"
2277 if {$have_tk85} {
2278 $ctext conf -tabstyle wordprocessor
2280 ${NS}::scrollbar .bleft.bottom.sb -command "$ctext yview"
2281 ${NS}::scrollbar .bleft.bottom.sbhorizontal -command "$ctext xview" -orient h
2282 pack .bleft.top -side top -fill x
2283 pack .bleft.mid -side top -fill x
2284 grid $ctext .bleft.bottom.sb -sticky nsew
2285 grid .bleft.bottom.sbhorizontal -sticky ew
2286 grid columnconfigure .bleft.bottom 0 -weight 1
2287 grid rowconfigure .bleft.bottom 0 -weight 1
2288 grid rowconfigure .bleft.bottom 1 -weight 0
2289 pack .bleft.bottom -side top -fill both -expand 1
2290 lappend bglist $ctext
2291 lappend fglist $ctext
2293 $ctext tag conf comment -wrap $wrapcomment
2294 $ctext tag conf filesep -font textfontbold -back "#aaaaaa"
2295 $ctext tag conf hunksep -fore [lindex $diffcolors 2]
2296 $ctext tag conf d0 -fore [lindex $diffcolors 0]
2297 $ctext tag conf dresult -fore [lindex $diffcolors 1]
2298 $ctext tag conf m0 -fore red
2299 $ctext tag conf m1 -fore blue
2300 $ctext tag conf m2 -fore green
2301 $ctext tag conf m3 -fore purple
2302 $ctext tag conf m4 -fore brown
2303 $ctext tag conf m5 -fore "#009090"
2304 $ctext tag conf m6 -fore magenta
2305 $ctext tag conf m7 -fore "#808000"
2306 $ctext tag conf m8 -fore "#009000"
2307 $ctext tag conf m9 -fore "#ff0080"
2308 $ctext tag conf m10 -fore cyan
2309 $ctext tag conf m11 -fore "#b07070"
2310 $ctext tag conf m12 -fore "#70b0f0"
2311 $ctext tag conf m13 -fore "#70f0b0"
2312 $ctext tag conf m14 -fore "#f0b070"
2313 $ctext tag conf m15 -fore "#ff70b0"
2314 $ctext tag conf mmax -fore darkgrey
2315 set mergemax 16
2316 $ctext tag conf mresult -font textfontbold
2317 $ctext tag conf msep -font textfontbold
2318 $ctext tag conf found -back yellow
2320 .pwbottom add .bleft
2321 if {!$use_ttk} {
2322 .pwbottom paneconfigure .bleft -width $geometry(botwidth)
2325 # lower right
2326 ${NS}::frame .bright
2327 ${NS}::frame .bright.mode
2328 ${NS}::radiobutton .bright.mode.patch -text [mc "Patch"] \
2329 -command reselectline -variable cmitmode -value "patch"
2330 ${NS}::radiobutton .bright.mode.tree -text [mc "Tree"] \
2331 -command reselectline -variable cmitmode -value "tree"
2332 grid .bright.mode.patch .bright.mode.tree -sticky ew
2333 pack .bright.mode -side top -fill x
2334 set cflist .bright.cfiles
2335 set indent [font measure mainfont "nn"]
2336 text $cflist \
2337 -selectbackground $selectbgcolor \
2338 -background $bgcolor -foreground $fgcolor \
2339 -font mainfont \
2340 -tabs [list $indent [expr {2 * $indent}]] \
2341 -yscrollcommand ".bright.sb set" \
2342 -cursor [. cget -cursor] \
2343 -spacing1 1 -spacing3 1
2344 lappend bglist $cflist
2345 lappend fglist $cflist
2346 ${NS}::scrollbar .bright.sb -command "$cflist yview"
2347 pack .bright.sb -side right -fill y
2348 pack $cflist -side left -fill both -expand 1
2349 $cflist tag configure highlight \
2350 -background [$cflist cget -selectbackground]
2351 $cflist tag configure bold -font mainfontbold
2353 .pwbottom add .bright
2354 .ctop add .pwbottom
2356 # restore window width & height if known
2357 if {[info exists geometry(main)]} {
2358 if {[scan $geometry(main) "%dx%d" w h] >= 2} {
2359 if {$w > [winfo screenwidth .]} {
2360 set w [winfo screenwidth .]
2362 if {$h > [winfo screenheight .]} {
2363 set h [winfo screenheight .]
2365 wm geometry . "${w}x$h"
2369 if {[info exists geometry(state)] && $geometry(state) eq "zoomed"} {
2370 wm state . $geometry(state)
2373 if {[tk windowingsystem] eq {aqua}} {
2374 set M1B M1
2375 set ::BM "3"
2376 } else {
2377 set M1B Control
2378 set ::BM "2"
2381 if {$use_ttk} {
2382 bind .ctop <Map> {
2383 bind %W <Map> {}
2384 %W sashpos 0 $::geometry(topheight)
2386 bind .pwbottom <Map> {
2387 bind %W <Map> {}
2388 %W sashpos 0 $::geometry(botwidth)
2392 bind .pwbottom <Configure> {resizecdetpanes %W %w}
2393 pack .ctop -fill both -expand 1
2394 bindall <1> {selcanvline %W %x %y}
2395 #bindall <B1-Motion> {selcanvline %W %x %y}
2396 if {[tk windowingsystem] == "win32"} {
2397 bind . <MouseWheel> { windows_mousewheel_redirector %W %X %Y %D }
2398 bind $ctext <MouseWheel> { windows_mousewheel_redirector %W %X %Y %D ; break }
2399 } else {
2400 bindall <ButtonRelease-4> "allcanvs yview scroll -5 units"
2401 bindall <ButtonRelease-5> "allcanvs yview scroll 5 units"
2402 if {[tk windowingsystem] eq "aqua"} {
2403 bindall <MouseWheel> {
2404 set delta [expr {- (%D)}]
2405 allcanvs yview scroll $delta units
2407 bindall <Shift-MouseWheel> {
2408 set delta [expr {- (%D)}]
2409 $canv xview scroll $delta units
2413 bindall <$::BM> "canvscan mark %W %x %y"
2414 bindall <B$::BM-Motion> "canvscan dragto %W %x %y"
2415 bind all <$M1B-Key-w> {destroy [winfo toplevel %W]}
2416 bind . <$M1B-Key-w> doquit
2417 bindkey <Home> selfirstline
2418 bindkey <End> sellastline
2419 bind . <Key-Up> "selnextline -1"
2420 bind . <Key-Down> "selnextline 1"
2421 bind . <Shift-Key-Up> "dofind -1 0"
2422 bind . <Shift-Key-Down> "dofind 1 0"
2423 bindkey <Key-Right> "goforw"
2424 bindkey <Key-Left> "goback"
2425 bind . <Key-Prior> "selnextpage -1"
2426 bind . <Key-Next> "selnextpage 1"
2427 bind . <$M1B-Home> "allcanvs yview moveto 0.0"
2428 bind . <$M1B-End> "allcanvs yview moveto 1.0"
2429 bind . <$M1B-Key-Up> "allcanvs yview scroll -1 units"
2430 bind . <$M1B-Key-Down> "allcanvs yview scroll 1 units"
2431 bind . <$M1B-Key-Prior> "allcanvs yview scroll -1 pages"
2432 bind . <$M1B-Key-Next> "allcanvs yview scroll 1 pages"
2433 bindkey <Key-Delete> "$ctext yview scroll -1 pages"
2434 bindkey <Key-BackSpace> "$ctext yview scroll -1 pages"
2435 bindkey <Key-space> "$ctext yview scroll 1 pages"
2436 bindkey p "selnextline -1"
2437 bindkey n "selnextline 1"
2438 bindkey z "goback"
2439 bindkey x "goforw"
2440 bindkey i "selnextline -1"
2441 bindkey k "selnextline 1"
2442 bindkey j "goback"
2443 bindkey l "goforw"
2444 bindkey b prevfile
2445 bindkey d "$ctext yview scroll 18 units"
2446 bindkey u "$ctext yview scroll -18 units"
2447 bindkey / {focus $fstring}
2448 bindkey <Key-KP_Divide> {focus $fstring}
2449 bindkey <Key-Return> {dofind 1 1}
2450 bindkey ? {dofind -1 1}
2451 bindkey f nextfile
2452 bind . <F5> updatecommits
2453 bind . <$M1B-F5> reloadcommits
2454 bind . <F2> showrefs
2455 bind . <Shift-F4> {newview 0}
2456 catch { bind . <Shift-Key-XF86_Switch_VT_4> {newview 0} }
2457 bind . <F4> edit_or_newview
2458 bind . <$M1B-q> doquit
2459 bind . <$M1B-f> {dofind 1 1}
2460 bind . <$M1B-g> {dofind 1 0}
2461 bind . <$M1B-r> dosearchback
2462 bind . <$M1B-s> dosearch
2463 bind . <$M1B-equal> {incrfont 1}
2464 bind . <$M1B-plus> {incrfont 1}
2465 bind . <$M1B-KP_Add> {incrfont 1}
2466 bind . <$M1B-minus> {incrfont -1}
2467 bind . <$M1B-KP_Subtract> {incrfont -1}
2468 wm protocol . WM_DELETE_WINDOW doquit
2469 bind . <Destroy> {stop_backends}
2470 bind . <Button-1> "click %W"
2471 bind $fstring <Key-Return> {dofind 1 1}
2472 bind $sha1entry <Key-Return> {gotocommit; break}
2473 bind $sha1entry <<PasteSelection>> clearsha1
2474 bind $cflist <1> {sel_flist %W %x %y; break}
2475 bind $cflist <B1-Motion> {sel_flist %W %x %y; break}
2476 bind $cflist <ButtonRelease-1> {treeclick %W %x %y}
2477 global ctxbut
2478 bind $cflist $ctxbut {pop_flist_menu %W %X %Y %x %y}
2479 bind $ctext $ctxbut {pop_diff_menu %W %X %Y %x %y}
2480 bind $ctext <Button-1> {focus %W}
2482 set maincursor [. cget -cursor]
2483 set textcursor [$ctext cget -cursor]
2484 set curtextcursor $textcursor
2486 set rowctxmenu .rowctxmenu
2487 makemenu $rowctxmenu {
2488 {mc "Diff this -> selected" command {diffvssel 0}}
2489 {mc "Diff selected -> this" command {diffvssel 1}}
2490 {mc "Make patch" command mkpatch}
2491 {mc "Create tag" command mktag}
2492 {mc "Write commit to file" command writecommit}
2493 {mc "Create new branch" command mkbranch}
2494 {mc "Cherry-pick this commit" command cherrypick}
2495 {mc "Reset HEAD branch to here" command resethead}
2496 {mc "Mark this commit" command markhere}
2497 {mc "Return to mark" command gotomark}
2498 {mc "Find descendant of this and mark" command find_common_desc}
2499 {mc "Compare with marked commit" command compare_commits}
2501 $rowctxmenu configure -tearoff 0
2503 set fakerowmenu .fakerowmenu
2504 makemenu $fakerowmenu {
2505 {mc "Diff this -> selected" command {diffvssel 0}}
2506 {mc "Diff selected -> this" command {diffvssel 1}}
2507 {mc "Make patch" command mkpatch}
2509 $fakerowmenu configure -tearoff 0
2511 set headctxmenu .headctxmenu
2512 makemenu $headctxmenu {
2513 {mc "Check out this branch" command cobranch}
2514 {mc "Remove this branch" command rmbranch}
2516 $headctxmenu configure -tearoff 0
2518 global flist_menu
2519 set flist_menu .flistctxmenu
2520 makemenu $flist_menu {
2521 {mc "Highlight this too" command {flist_hl 0}}
2522 {mc "Highlight this only" command {flist_hl 1}}
2523 {mc "External diff" command {external_diff}}
2524 {mc "Blame parent commit" command {external_blame 1}}
2526 $flist_menu configure -tearoff 0
2528 global diff_menu
2529 set diff_menu .diffctxmenu
2530 makemenu $diff_menu {
2531 {mc "Show origin of this line" command show_line_source}
2532 {mc "Run git gui blame on this line" command {external_blame_diff}}
2534 $diff_menu configure -tearoff 0
2537 # Windows sends all mouse wheel events to the current focused window, not
2538 # the one where the mouse hovers, so bind those events here and redirect
2539 # to the correct window
2540 proc windows_mousewheel_redirector {W X Y D} {
2541 global canv canv2 canv3
2542 set w [winfo containing -displayof $W $X $Y]
2543 if {$w ne ""} {
2544 set u [expr {$D < 0 ? 5 : -5}]
2545 if {$w == $canv || $w == $canv2 || $w == $canv3} {
2546 allcanvs yview scroll $u units
2547 } else {
2548 catch {
2549 $w yview scroll $u units
2555 # Update row number label when selectedline changes
2556 proc selectedline_change {n1 n2 op} {
2557 global selectedline rownumsel
2559 if {$selectedline eq {}} {
2560 set rownumsel {}
2561 } else {
2562 set rownumsel [expr {$selectedline + 1}]
2566 # mouse-2 makes all windows scan vertically, but only the one
2567 # the cursor is in scans horizontally
2568 proc canvscan {op w x y} {
2569 global canv canv2 canv3
2570 foreach c [list $canv $canv2 $canv3] {
2571 if {$c == $w} {
2572 $c scan $op $x $y
2573 } else {
2574 $c scan $op 0 $y
2579 proc scrollcanv {cscroll f0 f1} {
2580 $cscroll set $f0 $f1
2581 drawvisible
2582 flushhighlights
2585 # when we make a key binding for the toplevel, make sure
2586 # it doesn't get triggered when that key is pressed in the
2587 # find string entry widget.
2588 proc bindkey {ev script} {
2589 global entries
2590 bind . $ev $script
2591 set escript [bind Entry $ev]
2592 if {$escript == {}} {
2593 set escript [bind Entry <Key>]
2595 foreach e $entries {
2596 bind $e $ev "$escript; break"
2600 # set the focus back to the toplevel for any click outside
2601 # the entry widgets
2602 proc click {w} {
2603 global ctext entries
2604 foreach e [concat $entries $ctext] {
2605 if {$w == $e} return
2607 focus .
2610 # Adjust the progress bar for a change in requested extent or canvas size
2611 proc adjustprogress {} {
2612 global progresscanv progressitem progresscoords
2613 global fprogitem fprogcoord lastprogupdate progupdatepending
2614 global rprogitem rprogcoord use_ttk
2616 if {$use_ttk} {
2617 $progresscanv configure -value [expr {int($fprogcoord * 100)}]
2618 return
2621 set w [expr {[winfo width $progresscanv] - 4}]
2622 set x0 [expr {$w * [lindex $progresscoords 0]}]
2623 set x1 [expr {$w * [lindex $progresscoords 1]}]
2624 set h [winfo height $progresscanv]
2625 $progresscanv coords $progressitem $x0 0 $x1 $h
2626 $progresscanv coords $fprogitem 0 0 [expr {$w * $fprogcoord}] $h
2627 $progresscanv coords $rprogitem 0 0 [expr {$w * $rprogcoord}] $h
2628 set now [clock clicks -milliseconds]
2629 if {$now >= $lastprogupdate + 100} {
2630 set progupdatepending 0
2631 update
2632 } elseif {!$progupdatepending} {
2633 set progupdatepending 1
2634 after [expr {$lastprogupdate + 100 - $now}] doprogupdate
2638 proc doprogupdate {} {
2639 global lastprogupdate progupdatepending
2641 if {$progupdatepending} {
2642 set progupdatepending 0
2643 set lastprogupdate [clock clicks -milliseconds]
2644 update
2648 proc savestuff {w} {
2649 global canv canv2 canv3 mainfont textfont uifont tabstop
2650 global stuffsaved findmergefiles maxgraphpct
2651 global maxwidth showneartags showlocalchanges
2652 global viewname viewfiles viewargs viewargscmd viewperm nextviewnum
2653 global cmitmode wrapcomment datetimeformat limitdiffs
2654 global colors uicolor bgcolor fgcolor diffcolors diffcontext selectbgcolor
2655 global autoselect autosellen extdifftool perfile_attrs markbgcolor use_ttk
2656 global hideremotes want_ttk
2658 if {$stuffsaved} return
2659 if {![winfo viewable .]} return
2660 catch {
2661 if {[file exists ~/.gitk-new]} {file delete -force ~/.gitk-new}
2662 set f [open "~/.gitk-new" w]
2663 if {$::tcl_platform(platform) eq {windows}} {
2664 file attributes "~/.gitk-new" -hidden true
2666 puts $f [list set mainfont $mainfont]
2667 puts $f [list set textfont $textfont]
2668 puts $f [list set uifont $uifont]
2669 puts $f [list set tabstop $tabstop]
2670 puts $f [list set findmergefiles $findmergefiles]
2671 puts $f [list set maxgraphpct $maxgraphpct]
2672 puts $f [list set maxwidth $maxwidth]
2673 puts $f [list set cmitmode $cmitmode]
2674 puts $f [list set wrapcomment $wrapcomment]
2675 puts $f [list set autoselect $autoselect]
2676 puts $f [list set autosellen $autosellen]
2677 puts $f [list set showneartags $showneartags]
2678 puts $f [list set hideremotes $hideremotes]
2679 puts $f [list set showlocalchanges $showlocalchanges]
2680 puts $f [list set datetimeformat $datetimeformat]
2681 puts $f [list set limitdiffs $limitdiffs]
2682 puts $f [list set uicolor $uicolor]
2683 puts $f [list set want_ttk $want_ttk]
2684 puts $f [list set bgcolor $bgcolor]
2685 puts $f [list set fgcolor $fgcolor]
2686 puts $f [list set colors $colors]
2687 puts $f [list set diffcolors $diffcolors]
2688 puts $f [list set markbgcolor $markbgcolor]
2689 puts $f [list set diffcontext $diffcontext]
2690 puts $f [list set selectbgcolor $selectbgcolor]
2691 puts $f [list set extdifftool $extdifftool]
2692 puts $f [list set perfile_attrs $perfile_attrs]
2694 puts $f "set geometry(main) [wm geometry .]"
2695 puts $f "set geometry(state) [wm state .]"
2696 puts $f "set geometry(topwidth) [winfo width .tf]"
2697 puts $f "set geometry(topheight) [winfo height .tf]"
2698 if {$use_ttk} {
2699 puts $f "set geometry(pwsash0) \"[.tf.histframe.pwclist sashpos 0] 1\""
2700 puts $f "set geometry(pwsash1) \"[.tf.histframe.pwclist sashpos 1] 1\""
2701 } else {
2702 puts $f "set geometry(pwsash0) \"[.tf.histframe.pwclist sash coord 0]\""
2703 puts $f "set geometry(pwsash1) \"[.tf.histframe.pwclist sash coord 1]\""
2705 puts $f "set geometry(botwidth) [winfo width .bleft]"
2706 puts $f "set geometry(botheight) [winfo height .bleft]"
2708 puts -nonewline $f "set permviews {"
2709 for {set v 0} {$v < $nextviewnum} {incr v} {
2710 if {$viewperm($v)} {
2711 puts $f "{[list $viewname($v) $viewfiles($v) $viewargs($v) $viewargscmd($v)]}"
2714 puts $f "}"
2715 close $f
2716 catch {file delete "~/.gitk"}
2717 file rename -force "~/.gitk-new" "~/.gitk"
2719 set stuffsaved 1
2722 proc resizeclistpanes {win w} {
2723 global oldwidth use_ttk
2724 if {[info exists oldwidth($win)]} {
2725 if {$use_ttk} {
2726 set s0 [$win sashpos 0]
2727 set s1 [$win sashpos 1]
2728 } else {
2729 set s0 [$win sash coord 0]
2730 set s1 [$win sash coord 1]
2732 if {$w < 60} {
2733 set sash0 [expr {int($w/2 - 2)}]
2734 set sash1 [expr {int($w*5/6 - 2)}]
2735 } else {
2736 set factor [expr {1.0 * $w / $oldwidth($win)}]
2737 set sash0 [expr {int($factor * [lindex $s0 0])}]
2738 set sash1 [expr {int($factor * [lindex $s1 0])}]
2739 if {$sash0 < 30} {
2740 set sash0 30
2742 if {$sash1 < $sash0 + 20} {
2743 set sash1 [expr {$sash0 + 20}]
2745 if {$sash1 > $w - 10} {
2746 set sash1 [expr {$w - 10}]
2747 if {$sash0 > $sash1 - 20} {
2748 set sash0 [expr {$sash1 - 20}]
2752 if {$use_ttk} {
2753 $win sashpos 0 $sash0
2754 $win sashpos 1 $sash1
2755 } else {
2756 $win sash place 0 $sash0 [lindex $s0 1]
2757 $win sash place 1 $sash1 [lindex $s1 1]
2760 set oldwidth($win) $w
2763 proc resizecdetpanes {win w} {
2764 global oldwidth use_ttk
2765 if {[info exists oldwidth($win)]} {
2766 if {$use_ttk} {
2767 set s0 [$win sashpos 0]
2768 } else {
2769 set s0 [$win sash coord 0]
2771 if {$w < 60} {
2772 set sash0 [expr {int($w*3/4 - 2)}]
2773 } else {
2774 set factor [expr {1.0 * $w / $oldwidth($win)}]
2775 set sash0 [expr {int($factor * [lindex $s0 0])}]
2776 if {$sash0 < 45} {
2777 set sash0 45
2779 if {$sash0 > $w - 15} {
2780 set sash0 [expr {$w - 15}]
2783 if {$use_ttk} {
2784 $win sashpos 0 $sash0
2785 } else {
2786 $win sash place 0 $sash0 [lindex $s0 1]
2789 set oldwidth($win) $w
2792 proc allcanvs args {
2793 global canv canv2 canv3
2794 eval $canv $args
2795 eval $canv2 $args
2796 eval $canv3 $args
2799 proc bindall {event action} {
2800 global canv canv2 canv3
2801 bind $canv $event $action
2802 bind $canv2 $event $action
2803 bind $canv3 $event $action
2806 proc about {} {
2807 global uifont NS
2808 set w .about
2809 if {[winfo exists $w]} {
2810 raise $w
2811 return
2813 ttk_toplevel $w
2814 wm title $w [mc "About gitk"]
2815 make_transient $w .
2816 message $w.m -text [mc "
2817 Gitk - a commit viewer for git
2819 Copyright \u00a9 2005-2010 Paul Mackerras
2821 Use and redistribute under the terms of the GNU General Public License"] \
2822 -justify center -aspect 400 -border 2 -bg white -relief groove
2823 pack $w.m -side top -fill x -padx 2 -pady 2
2824 ${NS}::button $w.ok -text [mc "Close"] -command "destroy $w" -default active
2825 pack $w.ok -side bottom
2826 bind $w <Visibility> "focus $w.ok"
2827 bind $w <Key-Escape> "destroy $w"
2828 bind $w <Key-Return> "destroy $w"
2829 tk::PlaceWindow $w widget .
2832 proc keys {} {
2833 global NS
2834 set w .keys
2835 if {[winfo exists $w]} {
2836 raise $w
2837 return
2839 if {[tk windowingsystem] eq {aqua}} {
2840 set M1T Cmd
2841 } else {
2842 set M1T Ctrl
2844 ttk_toplevel $w
2845 wm title $w [mc "Gitk key bindings"]
2846 make_transient $w .
2847 message $w.m -text "
2848 [mc "Gitk key bindings:"]
2850 [mc "<%s-Q> Quit" $M1T]
2851 [mc "<%s-W> Close window" $M1T]
2852 [mc "<Home> Move to first commit"]
2853 [mc "<End> Move to last commit"]
2854 [mc "<Up>, p, i Move up one commit"]
2855 [mc "<Down>, n, k Move down one commit"]
2856 [mc "<Left>, z, j Go back in history list"]
2857 [mc "<Right>, x, l Go forward in history list"]
2858 [mc "<PageUp> Move up one page in commit list"]
2859 [mc "<PageDown> Move down one page in commit list"]
2860 [mc "<%s-Home> Scroll to top of commit list" $M1T]
2861 [mc "<%s-End> Scroll to bottom of commit list" $M1T]
2862 [mc "<%s-Up> Scroll commit list up one line" $M1T]
2863 [mc "<%s-Down> Scroll commit list down one line" $M1T]
2864 [mc "<%s-PageUp> Scroll commit list up one page" $M1T]
2865 [mc "<%s-PageDown> Scroll commit list down one page" $M1T]
2866 [mc "<Shift-Up> Find backwards (upwards, later commits)"]
2867 [mc "<Shift-Down> Find forwards (downwards, earlier commits)"]
2868 [mc "<Delete>, b Scroll diff view up one page"]
2869 [mc "<Backspace> Scroll diff view up one page"]
2870 [mc "<Space> Scroll diff view down one page"]
2871 [mc "u Scroll diff view up 18 lines"]
2872 [mc "d Scroll diff view down 18 lines"]
2873 [mc "<%s-F> Find" $M1T]
2874 [mc "<%s-G> Move to next find hit" $M1T]
2875 [mc "<Return> Move to next find hit"]
2876 [mc "/ Focus the search box"]
2877 [mc "? Move to previous find hit"]
2878 [mc "f Scroll diff view to next file"]
2879 [mc "<%s-S> Search for next hit in diff view" $M1T]
2880 [mc "<%s-R> Search for previous hit in diff view" $M1T]
2881 [mc "<%s-KP+> Increase font size" $M1T]
2882 [mc "<%s-plus> Increase font size" $M1T]
2883 [mc "<%s-KP-> Decrease font size" $M1T]
2884 [mc "<%s-minus> Decrease font size" $M1T]
2885 [mc "<F5> Update"]
2887 -justify left -bg white -border 2 -relief groove
2888 pack $w.m -side top -fill both -padx 2 -pady 2
2889 ${NS}::button $w.ok -text [mc "Close"] -command "destroy $w" -default active
2890 bind $w <Key-Escape> [list destroy $w]
2891 pack $w.ok -side bottom
2892 bind $w <Visibility> "focus $w.ok"
2893 bind $w <Key-Escape> "destroy $w"
2894 bind $w <Key-Return> "destroy $w"
2897 # Procedures for manipulating the file list window at the
2898 # bottom right of the overall window.
2900 proc treeview {w l openlevs} {
2901 global treecontents treediropen treeheight treeparent treeindex
2903 set ix 0
2904 set treeindex() 0
2905 set lev 0
2906 set prefix {}
2907 set prefixend -1
2908 set prefendstack {}
2909 set htstack {}
2910 set ht 0
2911 set treecontents() {}
2912 $w conf -state normal
2913 foreach f $l {
2914 while {[string range $f 0 $prefixend] ne $prefix} {
2915 if {$lev <= $openlevs} {
2916 $w mark set e:$treeindex($prefix) "end -1c"
2917 $w mark gravity e:$treeindex($prefix) left
2919 set treeheight($prefix) $ht
2920 incr ht [lindex $htstack end]
2921 set htstack [lreplace $htstack end end]
2922 set prefixend [lindex $prefendstack end]
2923 set prefendstack [lreplace $prefendstack end end]
2924 set prefix [string range $prefix 0 $prefixend]
2925 incr lev -1
2927 set tail [string range $f [expr {$prefixend+1}] end]
2928 while {[set slash [string first "/" $tail]] >= 0} {
2929 lappend htstack $ht
2930 set ht 0
2931 lappend prefendstack $prefixend
2932 incr prefixend [expr {$slash + 1}]
2933 set d [string range $tail 0 $slash]
2934 lappend treecontents($prefix) $d
2935 set oldprefix $prefix
2936 append prefix $d
2937 set treecontents($prefix) {}
2938 set treeindex($prefix) [incr ix]
2939 set treeparent($prefix) $oldprefix
2940 set tail [string range $tail [expr {$slash+1}] end]
2941 if {$lev <= $openlevs} {
2942 set ht 1
2943 set treediropen($prefix) [expr {$lev < $openlevs}]
2944 set bm [expr {$lev == $openlevs? "tri-rt": "tri-dn"}]
2945 $w mark set d:$ix "end -1c"
2946 $w mark gravity d:$ix left
2947 set str "\n"
2948 for {set i 0} {$i < $lev} {incr i} {append str "\t"}
2949 $w insert end $str
2950 $w image create end -align center -image $bm -padx 1 \
2951 -name a:$ix
2952 $w insert end $d [highlight_tag $prefix]
2953 $w mark set s:$ix "end -1c"
2954 $w mark gravity s:$ix left
2956 incr lev
2958 if {$tail ne {}} {
2959 if {$lev <= $openlevs} {
2960 incr ht
2961 set str "\n"
2962 for {set i 0} {$i < $lev} {incr i} {append str "\t"}
2963 $w insert end $str
2964 $w insert end $tail [highlight_tag $f]
2966 lappend treecontents($prefix) $tail
2969 while {$htstack ne {}} {
2970 set treeheight($prefix) $ht
2971 incr ht [lindex $htstack end]
2972 set htstack [lreplace $htstack end end]
2973 set prefixend [lindex $prefendstack end]
2974 set prefendstack [lreplace $prefendstack end end]
2975 set prefix [string range $prefix 0 $prefixend]
2977 $w conf -state disabled
2980 proc linetoelt {l} {
2981 global treeheight treecontents
2983 set y 2
2984 set prefix {}
2985 while {1} {
2986 foreach e $treecontents($prefix) {
2987 if {$y == $l} {
2988 return "$prefix$e"
2990 set n 1
2991 if {[string index $e end] eq "/"} {
2992 set n $treeheight($prefix$e)
2993 if {$y + $n > $l} {
2994 append prefix $e
2995 incr y
2996 break
2999 incr y $n
3004 proc highlight_tree {y prefix} {
3005 global treeheight treecontents cflist
3007 foreach e $treecontents($prefix) {
3008 set path $prefix$e
3009 if {[highlight_tag $path] ne {}} {
3010 $cflist tag add bold $y.0 "$y.0 lineend"
3012 incr y
3013 if {[string index $e end] eq "/" && $treeheight($path) > 1} {
3014 set y [highlight_tree $y $path]
3017 return $y
3020 proc treeclosedir {w dir} {
3021 global treediropen treeheight treeparent treeindex
3023 set ix $treeindex($dir)
3024 $w conf -state normal
3025 $w delete s:$ix e:$ix
3026 set treediropen($dir) 0
3027 $w image configure a:$ix -image tri-rt
3028 $w conf -state disabled
3029 set n [expr {1 - $treeheight($dir)}]
3030 while {$dir ne {}} {
3031 incr treeheight($dir) $n
3032 set dir $treeparent($dir)
3036 proc treeopendir {w dir} {
3037 global treediropen treeheight treeparent treecontents treeindex
3039 set ix $treeindex($dir)
3040 $w conf -state normal
3041 $w image configure a:$ix -image tri-dn
3042 $w mark set e:$ix s:$ix
3043 $w mark gravity e:$ix right
3044 set lev 0
3045 set str "\n"
3046 set n [llength $treecontents($dir)]
3047 for {set x $dir} {$x ne {}} {set x $treeparent($x)} {
3048 incr lev
3049 append str "\t"
3050 incr treeheight($x) $n
3052 foreach e $treecontents($dir) {
3053 set de $dir$e
3054 if {[string index $e end] eq "/"} {
3055 set iy $treeindex($de)
3056 $w mark set d:$iy e:$ix
3057 $w mark gravity d:$iy left
3058 $w insert e:$ix $str
3059 set treediropen($de) 0
3060 $w image create e:$ix -align center -image tri-rt -padx 1 \
3061 -name a:$iy
3062 $w insert e:$ix $e [highlight_tag $de]
3063 $w mark set s:$iy e:$ix
3064 $w mark gravity s:$iy left
3065 set treeheight($de) 1
3066 } else {
3067 $w insert e:$ix $str
3068 $w insert e:$ix $e [highlight_tag $de]
3071 $w mark gravity e:$ix right
3072 $w conf -state disabled
3073 set treediropen($dir) 1
3074 set top [lindex [split [$w index @0,0] .] 0]
3075 set ht [$w cget -height]
3076 set l [lindex [split [$w index s:$ix] .] 0]
3077 if {$l < $top} {
3078 $w yview $l.0
3079 } elseif {$l + $n + 1 > $top + $ht} {
3080 set top [expr {$l + $n + 2 - $ht}]
3081 if {$l < $top} {
3082 set top $l
3084 $w yview $top.0
3088 proc treeclick {w x y} {
3089 global treediropen cmitmode ctext cflist cflist_top
3091 if {$cmitmode ne "tree"} return
3092 if {![info exists cflist_top]} return
3093 set l [lindex [split [$w index "@$x,$y"] "."] 0]
3094 $cflist tag remove highlight $cflist_top.0 "$cflist_top.0 lineend"
3095 $cflist tag add highlight $l.0 "$l.0 lineend"
3096 set cflist_top $l
3097 if {$l == 1} {
3098 $ctext yview 1.0
3099 return
3101 set e [linetoelt $l]
3102 if {[string index $e end] ne "/"} {
3103 showfile $e
3104 } elseif {$treediropen($e)} {
3105 treeclosedir $w $e
3106 } else {
3107 treeopendir $w $e
3111 proc setfilelist {id} {
3112 global treefilelist cflist jump_to_here
3114 treeview $cflist $treefilelist($id) 0
3115 if {$jump_to_here ne {}} {
3116 set f [lindex $jump_to_here 0]
3117 if {[lsearch -exact $treefilelist($id) $f] >= 0} {
3118 showfile $f
3123 image create bitmap tri-rt -background black -foreground blue -data {
3124 #define tri-rt_width 13
3125 #define tri-rt_height 13
3126 static unsigned char tri-rt_bits[] = {
3127 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x30, 0x00, 0x70, 0x00, 0xf0, 0x00,
3128 0xf0, 0x01, 0xf0, 0x00, 0x70, 0x00, 0x30, 0x00, 0x10, 0x00, 0x00, 0x00,
3129 0x00, 0x00};
3130 } -maskdata {
3131 #define tri-rt-mask_width 13
3132 #define tri-rt-mask_height 13
3133 static unsigned char tri-rt-mask_bits[] = {
3134 0x08, 0x00, 0x18, 0x00, 0x38, 0x00, 0x78, 0x00, 0xf8, 0x00, 0xf8, 0x01,
3135 0xf8, 0x03, 0xf8, 0x01, 0xf8, 0x00, 0x78, 0x00, 0x38, 0x00, 0x18, 0x00,
3136 0x08, 0x00};
3138 image create bitmap tri-dn -background black -foreground blue -data {
3139 #define tri-dn_width 13
3140 #define tri-dn_height 13
3141 static unsigned char tri-dn_bits[] = {
3142 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0x07, 0xf8, 0x03,
3143 0xf0, 0x01, 0xe0, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3144 0x00, 0x00};
3145 } -maskdata {
3146 #define tri-dn-mask_width 13
3147 #define tri-dn-mask_height 13
3148 static unsigned char tri-dn-mask_bits[] = {
3149 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x1f, 0xfe, 0x0f, 0xfc, 0x07,
3150 0xf8, 0x03, 0xf0, 0x01, 0xe0, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00,
3151 0x00, 0x00};
3154 image create bitmap reficon-T -background black -foreground yellow -data {
3155 #define tagicon_width 13
3156 #define tagicon_height 9
3157 static unsigned char tagicon_bits[] = {
3158 0x00, 0x00, 0x00, 0x00, 0xf0, 0x07, 0xf8, 0x07,
3159 0xfc, 0x07, 0xf8, 0x07, 0xf0, 0x07, 0x00, 0x00, 0x00, 0x00};
3160 } -maskdata {
3161 #define tagicon-mask_width 13
3162 #define tagicon-mask_height 9
3163 static unsigned char tagicon-mask_bits[] = {
3164 0x00, 0x00, 0xf0, 0x0f, 0xf8, 0x0f, 0xfc, 0x0f,
3165 0xfe, 0x0f, 0xfc, 0x0f, 0xf8, 0x0f, 0xf0, 0x0f, 0x00, 0x00};
3167 set rectdata {
3168 #define headicon_width 13
3169 #define headicon_height 9
3170 static unsigned char headicon_bits[] = {
3171 0x00, 0x00, 0x00, 0x00, 0xf8, 0x07, 0xf8, 0x07,
3172 0xf8, 0x07, 0xf8, 0x07, 0xf8, 0x07, 0x00, 0x00, 0x00, 0x00};
3174 set rectmask {
3175 #define headicon-mask_width 13
3176 #define headicon-mask_height 9
3177 static unsigned char headicon-mask_bits[] = {
3178 0x00, 0x00, 0xfc, 0x0f, 0xfc, 0x0f, 0xfc, 0x0f,
3179 0xfc, 0x0f, 0xfc, 0x0f, 0xfc, 0x0f, 0xfc, 0x0f, 0x00, 0x00};
3181 image create bitmap reficon-H -background black -foreground green \
3182 -data $rectdata -maskdata $rectmask
3183 image create bitmap reficon-o -background black -foreground "#ddddff" \
3184 -data $rectdata -maskdata $rectmask
3186 proc init_flist {first} {
3187 global cflist cflist_top difffilestart
3189 $cflist conf -state normal
3190 $cflist delete 0.0 end
3191 if {$first ne {}} {
3192 $cflist insert end $first
3193 set cflist_top 1
3194 $cflist tag add highlight 1.0 "1.0 lineend"
3195 } else {
3196 catch {unset cflist_top}
3198 $cflist conf -state disabled
3199 set difffilestart {}
3202 proc highlight_tag {f} {
3203 global highlight_paths
3205 foreach p $highlight_paths {
3206 if {[string match $p $f]} {
3207 return "bold"
3210 return {}
3213 proc highlight_filelist {} {
3214 global cmitmode cflist
3216 $cflist conf -state normal
3217 if {$cmitmode ne "tree"} {
3218 set end [lindex [split [$cflist index end] .] 0]
3219 for {set l 2} {$l < $end} {incr l} {
3220 set line [$cflist get $l.0 "$l.0 lineend"]
3221 if {[highlight_tag $line] ne {}} {
3222 $cflist tag add bold $l.0 "$l.0 lineend"
3225 } else {
3226 highlight_tree 2 {}
3228 $cflist conf -state disabled
3231 proc unhighlight_filelist {} {
3232 global cflist
3234 $cflist conf -state normal
3235 $cflist tag remove bold 1.0 end
3236 $cflist conf -state disabled
3239 proc add_flist {fl} {
3240 global cflist
3242 $cflist conf -state normal
3243 foreach f $fl {
3244 $cflist insert end "\n"
3245 $cflist insert end $f [highlight_tag $f]
3247 $cflist conf -state disabled
3250 proc sel_flist {w x y} {
3251 global ctext difffilestart cflist cflist_top cmitmode
3253 if {$cmitmode eq "tree"} return
3254 if {![info exists cflist_top]} return
3255 set l [lindex [split [$w index "@$x,$y"] "."] 0]
3256 $cflist tag remove highlight $cflist_top.0 "$cflist_top.0 lineend"
3257 $cflist tag add highlight $l.0 "$l.0 lineend"
3258 set cflist_top $l
3259 if {$l == 1} {
3260 $ctext yview 1.0
3261 } else {
3262 catch {$ctext yview [lindex $difffilestart [expr {$l - 2}]]}
3266 proc pop_flist_menu {w X Y x y} {
3267 global ctext cflist cmitmode flist_menu flist_menu_file
3268 global treediffs diffids
3270 stopfinding
3271 set l [lindex [split [$w index "@$x,$y"] "."] 0]
3272 if {$l <= 1} return
3273 if {$cmitmode eq "tree"} {
3274 set e [linetoelt $l]
3275 if {[string index $e end] eq "/"} return
3276 } else {
3277 set e [lindex $treediffs($diffids) [expr {$l-2}]]
3279 set flist_menu_file $e
3280 set xdiffstate "normal"
3281 if {$cmitmode eq "tree"} {
3282 set xdiffstate "disabled"
3284 # Disable "External diff" item in tree mode
3285 $flist_menu entryconf 2 -state $xdiffstate
3286 tk_popup $flist_menu $X $Y
3289 proc find_ctext_fileinfo {line} {
3290 global ctext_file_names ctext_file_lines
3292 set ok [bsearch $ctext_file_lines $line]
3293 set tline [lindex $ctext_file_lines $ok]
3295 if {$ok >= [llength $ctext_file_lines] || $line < $tline} {
3296 return {}
3297 } else {
3298 return [list [lindex $ctext_file_names $ok] $tline]
3302 proc pop_diff_menu {w X Y x y} {
3303 global ctext diff_menu flist_menu_file
3304 global diff_menu_txtpos diff_menu_line
3305 global diff_menu_filebase
3307 set diff_menu_txtpos [split [$w index "@$x,$y"] "."]
3308 set diff_menu_line [lindex $diff_menu_txtpos 0]
3309 # don't pop up the menu on hunk-separator or file-separator lines
3310 if {[lsearch -glob [$ctext tag names $diff_menu_line.0] "*sep"] >= 0} {
3311 return
3313 stopfinding
3314 set f [find_ctext_fileinfo $diff_menu_line]
3315 if {$f eq {}} return
3316 set flist_menu_file [lindex $f 0]
3317 set diff_menu_filebase [lindex $f 1]
3318 tk_popup $diff_menu $X $Y
3321 proc flist_hl {only} {
3322 global flist_menu_file findstring gdttype
3324 set x [shellquote $flist_menu_file]
3325 if {$only || $findstring eq {} || $gdttype ne [mc "touching paths:"]} {
3326 set findstring $x
3327 } else {
3328 append findstring " " $x
3330 set gdttype [mc "touching paths:"]
3333 proc gitknewtmpdir {} {
3334 global diffnum gitktmpdir gitdir
3336 if {![info exists gitktmpdir]} {
3337 set gitktmpdir [file join [file dirname $gitdir] \
3338 [format ".gitk-tmp.%s" [pid]]]
3339 if {[catch {file mkdir $gitktmpdir} err]} {
3340 error_popup "[mc "Error creating temporary directory %s:" $gitktmpdir] $err"
3341 unset gitktmpdir
3342 return {}
3344 set diffnum 0
3346 incr diffnum
3347 set diffdir [file join $gitktmpdir $diffnum]
3348 if {[catch {file mkdir $diffdir} err]} {
3349 error_popup "[mc "Error creating temporary directory %s:" $diffdir] $err"
3350 return {}
3352 return $diffdir
3355 proc save_file_from_commit {filename output what} {
3356 global nullfile
3358 if {[catch {exec git show $filename -- > $output} err]} {
3359 if {[string match "fatal: bad revision *" $err]} {
3360 return $nullfile
3362 error_popup "[mc "Error getting \"%s\" from %s:" $filename $what] $err"
3363 return {}
3365 return $output
3368 proc external_diff_get_one_file {diffid filename diffdir} {
3369 global nullid nullid2 nullfile
3370 global gitdir
3372 if {$diffid == $nullid} {
3373 set difffile [file join [file dirname $gitdir] $filename]
3374 if {[file exists $difffile]} {
3375 return $difffile
3377 return $nullfile
3379 if {$diffid == $nullid2} {
3380 set difffile [file join $diffdir "\[index\] [file tail $filename]"]
3381 return [save_file_from_commit :$filename $difffile index]
3383 set difffile [file join $diffdir "\[$diffid\] [file tail $filename]"]
3384 return [save_file_from_commit $diffid:$filename $difffile \
3385 "revision $diffid"]
3388 proc external_diff {} {
3389 global nullid nullid2
3390 global flist_menu_file
3391 global diffids
3392 global extdifftool
3394 if {[llength $diffids] == 1} {
3395 # no reference commit given
3396 set diffidto [lindex $diffids 0]
3397 if {$diffidto eq $nullid} {
3398 # diffing working copy with index
3399 set diffidfrom $nullid2
3400 } elseif {$diffidto eq $nullid2} {
3401 # diffing index with HEAD
3402 set diffidfrom "HEAD"
3403 } else {
3404 # use first parent commit
3405 global parentlist selectedline
3406 set diffidfrom [lindex $parentlist $selectedline 0]
3408 } else {
3409 set diffidfrom [lindex $diffids 0]
3410 set diffidto [lindex $diffids 1]
3413 # make sure that several diffs wont collide
3414 set diffdir [gitknewtmpdir]
3415 if {$diffdir eq {}} return
3417 # gather files to diff
3418 set difffromfile [external_diff_get_one_file $diffidfrom $flist_menu_file $diffdir]
3419 set difftofile [external_diff_get_one_file $diffidto $flist_menu_file $diffdir]
3421 if {$difffromfile ne {} && $difftofile ne {}} {
3422 set cmd [list [shellsplit $extdifftool] $difffromfile $difftofile]
3423 if {[catch {set fl [open |$cmd r]} err]} {
3424 file delete -force $diffdir
3425 error_popup "$extdifftool: [mc "command failed:"] $err"
3426 } else {
3427 fconfigure $fl -blocking 0
3428 filerun $fl [list delete_at_eof $fl $diffdir]
3433 proc find_hunk_blamespec {base line} {
3434 global ctext
3436 # Find and parse the hunk header
3437 set s_lix [$ctext search -backwards -regexp ^@@ "$line.0 lineend" $base.0]
3438 if {$s_lix eq {}} return
3440 set s_line [$ctext get $s_lix "$s_lix + 1 lines"]
3441 if {![regexp {^@@@*(( -\d+(,\d+)?)+) \+(\d+)(,\d+)? @@} $s_line \
3442 s_line old_specs osz osz1 new_line nsz]} {
3443 return
3446 # base lines for the parents
3447 set base_lines [list $new_line]
3448 foreach old_spec [lrange [split $old_specs " "] 1 end] {
3449 if {![regexp -- {-(\d+)(,\d+)?} $old_spec \
3450 old_spec old_line osz]} {
3451 return
3453 lappend base_lines $old_line
3456 # Now scan the lines to determine offset within the hunk
3457 set max_parent [expr {[llength $base_lines]-2}]
3458 set dline 0
3459 set s_lno [lindex [split $s_lix "."] 0]
3461 # Determine if the line is removed
3462 set chunk [$ctext get $line.0 "$line.1 + $max_parent chars"]
3463 if {[string match {[-+ ]*} $chunk]} {
3464 set removed_idx [string first "-" $chunk]
3465 # Choose a parent index
3466 if {$removed_idx >= 0} {
3467 set parent $removed_idx
3468 } else {
3469 set unchanged_idx [string first " " $chunk]
3470 if {$unchanged_idx >= 0} {
3471 set parent $unchanged_idx
3472 } else {
3473 # blame the current commit
3474 set parent -1
3477 # then count other lines that belong to it
3478 for {set i $line} {[incr i -1] > $s_lno} {} {
3479 set chunk [$ctext get $i.0 "$i.1 + $max_parent chars"]
3480 # Determine if the line is removed
3481 set removed_idx [string first "-" $chunk]
3482 if {$parent >= 0} {
3483 set code [string index $chunk $parent]
3484 if {$code eq "-" || ($removed_idx < 0 && $code ne "+")} {
3485 incr dline
3487 } else {
3488 if {$removed_idx < 0} {
3489 incr dline
3493 incr parent
3494 } else {
3495 set parent 0
3498 incr dline [lindex $base_lines $parent]
3499 return [list $parent $dline]
3502 proc external_blame_diff {} {
3503 global currentid cmitmode
3504 global diff_menu_txtpos diff_menu_line
3505 global diff_menu_filebase flist_menu_file
3507 if {$cmitmode eq "tree"} {
3508 set parent_idx 0
3509 set line [expr {$diff_menu_line - $diff_menu_filebase}]
3510 } else {
3511 set hinfo [find_hunk_blamespec $diff_menu_filebase $diff_menu_line]
3512 if {$hinfo ne {}} {
3513 set parent_idx [lindex $hinfo 0]
3514 set line [lindex $hinfo 1]
3515 } else {
3516 set parent_idx 0
3517 set line 0
3521 external_blame $parent_idx $line
3524 # Find the SHA1 ID of the blob for file $fname in the index
3525 # at stage 0 or 2
3526 proc index_sha1 {fname} {
3527 set f [open [list | git ls-files -s $fname] r]
3528 while {[gets $f line] >= 0} {
3529 set info [lindex [split $line "\t"] 0]
3530 set stage [lindex $info 2]
3531 if {$stage eq "0" || $stage eq "2"} {
3532 close $f
3533 return [lindex $info 1]
3536 close $f
3537 return {}
3540 # Turn an absolute path into one relative to the current directory
3541 proc make_relative {f} {
3542 if {[file pathtype $f] eq "relative"} {
3543 return $f
3545 set elts [file split $f]
3546 set here [file split [pwd]]
3547 set ei 0
3548 set hi 0
3549 set res {}
3550 foreach d $here {
3551 if {$ei < $hi || $ei >= [llength $elts] || [lindex $elts $ei] ne $d} {
3552 lappend res ".."
3553 } else {
3554 incr ei
3556 incr hi
3558 set elts [concat $res [lrange $elts $ei end]]
3559 return [eval file join $elts]
3562 proc external_blame {parent_idx {line {}}} {
3563 global flist_menu_file gitdir
3564 global nullid nullid2
3565 global parentlist selectedline currentid
3567 if {$parent_idx > 0} {
3568 set base_commit [lindex $parentlist $selectedline [expr {$parent_idx-1}]]
3569 } else {
3570 set base_commit $currentid
3573 if {$base_commit eq {} || $base_commit eq $nullid || $base_commit eq $nullid2} {
3574 error_popup [mc "No such commit"]
3575 return
3578 set cmdline [list git gui blame]
3579 if {$line ne {} && $line > 1} {
3580 lappend cmdline "--line=$line"
3582 set f [file join [file dirname $gitdir] $flist_menu_file]
3583 # Unfortunately it seems git gui blame doesn't like
3584 # being given an absolute path...
3585 set f [make_relative $f]
3586 lappend cmdline $base_commit $f
3587 if {[catch {eval exec $cmdline &} err]} {
3588 error_popup "[mc "git gui blame: command failed:"] $err"
3592 proc show_line_source {} {
3593 global cmitmode currentid parents curview blamestuff blameinst
3594 global diff_menu_line diff_menu_filebase flist_menu_file
3595 global nullid nullid2 gitdir
3597 set from_index {}
3598 if {$cmitmode eq "tree"} {
3599 set id $currentid
3600 set line [expr {$diff_menu_line - $diff_menu_filebase}]
3601 } else {
3602 set h [find_hunk_blamespec $diff_menu_filebase $diff_menu_line]
3603 if {$h eq {}} return
3604 set pi [lindex $h 0]
3605 if {$pi == 0} {
3606 mark_ctext_line $diff_menu_line
3607 return
3609 incr pi -1
3610 if {$currentid eq $nullid} {
3611 if {$pi > 0} {
3612 # must be a merge in progress...
3613 if {[catch {
3614 # get the last line from .git/MERGE_HEAD
3615 set f [open [file join $gitdir MERGE_HEAD] r]
3616 set id [lindex [split [read $f] "\n"] end-1]
3617 close $f
3618 } err]} {
3619 error_popup [mc "Couldn't read merge head: %s" $err]
3620 return
3622 } elseif {$parents($curview,$currentid) eq $nullid2} {
3623 # need to do the blame from the index
3624 if {[catch {
3625 set from_index [index_sha1 $flist_menu_file]
3626 } err]} {
3627 error_popup [mc "Error reading index: %s" $err]
3628 return
3630 } else {
3631 set id $parents($curview,$currentid)
3633 } else {
3634 set id [lindex $parents($curview,$currentid) $pi]
3636 set line [lindex $h 1]
3638 set blameargs {}
3639 if {$from_index ne {}} {
3640 lappend blameargs | git cat-file blob $from_index
3642 lappend blameargs | git blame -p -L$line,+1
3643 if {$from_index ne {}} {
3644 lappend blameargs --contents -
3645 } else {
3646 lappend blameargs $id
3648 lappend blameargs -- [file join [file dirname $gitdir] $flist_menu_file]
3649 if {[catch {
3650 set f [open $blameargs r]
3651 } err]} {
3652 error_popup [mc "Couldn't start git blame: %s" $err]
3653 return
3655 nowbusy blaming [mc "Searching"]
3656 fconfigure $f -blocking 0
3657 set i [reg_instance $f]
3658 set blamestuff($i) {}
3659 set blameinst $i
3660 filerun $f [list read_line_source $f $i]
3663 proc stopblaming {} {
3664 global blameinst
3666 if {[info exists blameinst]} {
3667 stop_instance $blameinst
3668 unset blameinst
3669 notbusy blaming
3673 proc read_line_source {fd inst} {
3674 global blamestuff curview commfd blameinst nullid nullid2
3676 while {[gets $fd line] >= 0} {
3677 lappend blamestuff($inst) $line
3679 if {![eof $fd]} {
3680 return 1
3682 unset commfd($inst)
3683 unset blameinst
3684 notbusy blaming
3685 fconfigure $fd -blocking 1
3686 if {[catch {close $fd} err]} {
3687 error_popup [mc "Error running git blame: %s" $err]
3688 return 0
3691 set fname {}
3692 set line [split [lindex $blamestuff($inst) 0] " "]
3693 set id [lindex $line 0]
3694 set lnum [lindex $line 1]
3695 if {[string length $id] == 40 && [string is xdigit $id] &&
3696 [string is digit -strict $lnum]} {
3697 # look for "filename" line
3698 foreach l $blamestuff($inst) {
3699 if {[string match "filename *" $l]} {
3700 set fname [string range $l 9 end]
3701 break
3705 if {$fname ne {}} {
3706 # all looks good, select it
3707 if {$id eq $nullid} {
3708 # blame uses all-zeroes to mean not committed,
3709 # which would mean a change in the index
3710 set id $nullid2
3712 if {[commitinview $id $curview]} {
3713 selectline [rowofcommit $id] 1 [list $fname $lnum]
3714 } else {
3715 error_popup [mc "That line comes from commit %s, \
3716 which is not in this view" [shortids $id]]
3718 } else {
3719 puts "oops couldn't parse git blame output"
3721 return 0
3724 # delete $dir when we see eof on $f (presumably because the child has exited)
3725 proc delete_at_eof {f dir} {
3726 while {[gets $f line] >= 0} {}
3727 if {[eof $f]} {
3728 if {[catch {close $f} err]} {
3729 error_popup "[mc "External diff viewer failed:"] $err"
3731 file delete -force $dir
3732 return 0
3734 return 1
3737 # Functions for adding and removing shell-type quoting
3739 proc shellquote {str} {
3740 if {![string match "*\['\"\\ \t]*" $str]} {
3741 return $str
3743 if {![string match "*\['\"\\]*" $str]} {
3744 return "\"$str\""
3746 if {![string match "*'*" $str]} {
3747 return "'$str'"
3749 return "\"[string map {\" \\\" \\ \\\\} $str]\""
3752 proc shellarglist {l} {
3753 set str {}
3754 foreach a $l {
3755 if {$str ne {}} {
3756 append str " "
3758 append str [shellquote $a]
3760 return $str
3763 proc shelldequote {str} {
3764 set ret {}
3765 set used -1
3766 while {1} {
3767 incr used
3768 if {![regexp -start $used -indices "\['\"\\\\ \t]" $str first]} {
3769 append ret [string range $str $used end]
3770 set used [string length $str]
3771 break
3773 set first [lindex $first 0]
3774 set ch [string index $str $first]
3775 if {$first > $used} {
3776 append ret [string range $str $used [expr {$first - 1}]]
3777 set used $first
3779 if {$ch eq " " || $ch eq "\t"} break
3780 incr used
3781 if {$ch eq "'"} {
3782 set first [string first "'" $str $used]
3783 if {$first < 0} {
3784 error "unmatched single-quote"
3786 append ret [string range $str $used [expr {$first - 1}]]
3787 set used $first
3788 continue
3790 if {$ch eq "\\"} {
3791 if {$used >= [string length $str]} {
3792 error "trailing backslash"
3794 append ret [string index $str $used]
3795 continue
3797 # here ch == "\""
3798 while {1} {
3799 if {![regexp -start $used -indices "\[\"\\\\]" $str first]} {
3800 error "unmatched double-quote"
3802 set first [lindex $first 0]
3803 set ch [string index $str $first]
3804 if {$first > $used} {
3805 append ret [string range $str $used [expr {$first - 1}]]
3806 set used $first
3808 if {$ch eq "\""} break
3809 incr used
3810 append ret [string index $str $used]
3811 incr used
3814 return [list $used $ret]
3817 proc shellsplit {str} {
3818 set l {}
3819 while {1} {
3820 set str [string trimleft $str]
3821 if {$str eq {}} break
3822 set dq [shelldequote $str]
3823 set n [lindex $dq 0]
3824 set word [lindex $dq 1]
3825 set str [string range $str $n end]
3826 lappend l $word
3828 return $l
3831 # Code to implement multiple views
3833 proc newview {ishighlight} {
3834 global nextviewnum newviewname newishighlight
3835 global revtreeargs viewargscmd newviewopts curview
3837 set newishighlight $ishighlight
3838 set top .gitkview
3839 if {[winfo exists $top]} {
3840 raise $top
3841 return
3843 decode_view_opts $nextviewnum $revtreeargs
3844 set newviewname($nextviewnum) "[mc "View"] $nextviewnum"
3845 set newviewopts($nextviewnum,perm) 0
3846 set newviewopts($nextviewnum,cmd) $viewargscmd($curview)
3847 vieweditor $top $nextviewnum [mc "Gitk view definition"]
3850 set known_view_options {
3851 {perm b . {} {mc "Remember this view"}}
3852 {reflabel l + {} {mc "References (space separated list):"}}
3853 {refs t15 .. {} {mc "Branches & tags:"}}
3854 {allrefs b *. "--all" {mc "All refs"}}
3855 {branches b . "--branches" {mc "All (local) branches"}}
3856 {tags b . "--tags" {mc "All tags"}}
3857 {remotes b . "--remotes" {mc "All remote-tracking branches"}}
3858 {commitlbl l + {} {mc "Commit Info (regular expressions):"}}
3859 {author t15 .. "--author=*" {mc "Author:"}}
3860 {committer t15 . "--committer=*" {mc "Committer:"}}
3861 {loginfo t15 .. "--grep=*" {mc "Commit Message:"}}
3862 {allmatch b .. "--all-match" {mc "Matches all Commit Info criteria"}}
3863 {changes_l l + {} {mc "Changes to Files:"}}
3864 {pickaxe_s r0 . {} {mc "Fixed String"}}
3865 {pickaxe_t r1 . "--pickaxe-regex" {mc "Regular Expression"}}
3866 {pickaxe t15 .. "-S*" {mc "Search string:"}}
3867 {datelabel l + {} {mc "Commit Dates (\"2 weeks ago\", \"2009-03-17 15:27:38\", \"March 17, 2009 15:27:38\"):"}}
3868 {since t15 .. {"--since=*" "--after=*"} {mc "Since:"}}
3869 {until t15 . {"--until=*" "--before=*"} {mc "Until:"}}
3870 {limit_lbl l + {} {mc "Limit and/or skip a number of revisions (positive integer):"}}
3871 {limit t10 *. "--max-count=*" {mc "Number to show:"}}
3872 {skip t10 . "--skip=*" {mc "Number to skip:"}}
3873 {misc_lbl l + {} {mc "Miscellaneous options:"}}
3874 {dorder b *. {"--date-order" "-d"} {mc "Strictly sort by date"}}
3875 {lright b . "--left-right" {mc "Mark branch sides"}}
3876 {first b . "--first-parent" {mc "Limit to first parent"}}
3877 {smplhst b . "--simplify-by-decoration" {mc "Simple history"}}
3878 {args t50 *. {} {mc "Additional arguments to git log:"}}
3879 {allpaths path + {} {mc "Enter files and directories to include, one per line:"}}
3880 {cmd t50= + {} {mc "Command to generate more commits to include:"}}
3883 # Convert $newviewopts($n, ...) into args for git log.
3884 proc encode_view_opts {n} {
3885 global known_view_options newviewopts
3887 set rargs [list]
3888 foreach opt $known_view_options {
3889 set patterns [lindex $opt 3]
3890 if {$patterns eq {}} continue
3891 set pattern [lindex $patterns 0]
3893 if {[lindex $opt 1] eq "b"} {
3894 set val $newviewopts($n,[lindex $opt 0])
3895 if {$val} {
3896 lappend rargs $pattern
3898 } elseif {[regexp {^r(\d+)$} [lindex $opt 1] type value]} {
3899 regexp {^(.*_)} [lindex $opt 0] uselessvar button_id
3900 set val $newviewopts($n,$button_id)
3901 if {$val eq $value} {
3902 lappend rargs $pattern
3904 } else {
3905 set val $newviewopts($n,[lindex $opt 0])
3906 set val [string trim $val]
3907 if {$val ne {}} {
3908 set pfix [string range $pattern 0 end-1]
3909 lappend rargs $pfix$val
3913 set rargs [concat $rargs [shellsplit $newviewopts($n,refs)]]
3914 return [concat $rargs [shellsplit $newviewopts($n,args)]]
3917 # Fill $newviewopts($n, ...) based on args for git log.
3918 proc decode_view_opts {n view_args} {
3919 global known_view_options newviewopts
3921 foreach opt $known_view_options {
3922 set id [lindex $opt 0]
3923 if {[lindex $opt 1] eq "b"} {
3924 # Checkboxes
3925 set val 0
3926 } elseif {[regexp {^r(\d+)$} [lindex $opt 1]]} {
3927 # Radiobuttons
3928 regexp {^(.*_)} $id uselessvar id
3929 set val 0
3930 } else {
3931 # Text fields
3932 set val {}
3934 set newviewopts($n,$id) $val
3936 set oargs [list]
3937 set refargs [list]
3938 foreach arg $view_args {
3939 if {[regexp -- {^-([0-9]+)$} $arg arg cnt]
3940 && ![info exists found(limit)]} {
3941 set newviewopts($n,limit) $cnt
3942 set found(limit) 1
3943 continue
3945 catch { unset val }
3946 foreach opt $known_view_options {
3947 set id [lindex $opt 0]
3948 if {[info exists found($id)]} continue
3949 foreach pattern [lindex $opt 3] {
3950 if {![string match $pattern $arg]} continue
3951 if {[lindex $opt 1] eq "b"} {
3952 # Check buttons
3953 set val 1
3954 } elseif {[regexp {^r(\d+)$} [lindex $opt 1] match num]} {
3955 # Radio buttons
3956 regexp {^(.*_)} $id uselessvar id
3957 set val $num
3958 } else {
3959 # Text input fields
3960 set size [string length $pattern]
3961 set val [string range $arg [expr {$size-1}] end]
3963 set newviewopts($n,$id) $val
3964 set found($id) 1
3965 break
3967 if {[info exists val]} break
3969 if {[info exists val]} continue
3970 if {[regexp {^-} $arg]} {
3971 lappend oargs $arg
3972 } else {
3973 lappend refargs $arg
3976 set newviewopts($n,refs) [shellarglist $refargs]
3977 set newviewopts($n,args) [shellarglist $oargs]
3980 proc edit_or_newview {} {
3981 global curview
3983 if {$curview > 0} {
3984 editview
3985 } else {
3986 newview 0
3990 proc editview {} {
3991 global curview
3992 global viewname viewperm newviewname newviewopts
3993 global viewargs viewargscmd
3995 set top .gitkvedit-$curview
3996 if {[winfo exists $top]} {
3997 raise $top
3998 return
4000 decode_view_opts $curview $viewargs($curview)
4001 set newviewname($curview) $viewname($curview)
4002 set newviewopts($curview,perm) $viewperm($curview)
4003 set newviewopts($curview,cmd) $viewargscmd($curview)
4004 vieweditor $top $curview "[mc "Gitk: edit view"] $viewname($curview)"
4007 proc vieweditor {top n title} {
4008 global newviewname newviewopts viewfiles bgcolor
4009 global known_view_options NS
4011 ttk_toplevel $top
4012 wm title $top [concat $title [mc "-- criteria for selecting revisions"]]
4013 make_transient $top .
4015 # View name
4016 ${NS}::frame $top.nfr
4017 ${NS}::label $top.nl -text [mc "View Name"]
4018 ${NS}::entry $top.name -width 20 -textvariable newviewname($n)
4019 pack $top.nfr -in $top -fill x -pady 5 -padx 3
4020 pack $top.nl -in $top.nfr -side left -padx {0 5}
4021 pack $top.name -in $top.nfr -side left -padx {0 25}
4023 # View options
4024 set cframe $top.nfr
4025 set cexpand 0
4026 set cnt 0
4027 foreach opt $known_view_options {
4028 set id [lindex $opt 0]
4029 set type [lindex $opt 1]
4030 set flags [lindex $opt 2]
4031 set title [eval [lindex $opt 4]]
4032 set lxpad 0
4034 if {$flags eq "+" || $flags eq "*"} {
4035 set cframe $top.fr$cnt
4036 incr cnt
4037 ${NS}::frame $cframe
4038 pack $cframe -in $top -fill x -pady 3 -padx 3
4039 set cexpand [expr {$flags eq "*"}]
4040 } elseif {$flags eq ".." || $flags eq "*."} {
4041 set cframe $top.fr$cnt
4042 incr cnt
4043 ${NS}::frame $cframe
4044 pack $cframe -in $top -fill x -pady 3 -padx [list 15 3]
4045 set cexpand [expr {$flags eq "*."}]
4046 } else {
4047 set lxpad 5
4050 if {$type eq "l"} {
4051 ${NS}::label $cframe.l_$id -text $title
4052 pack $cframe.l_$id -in $cframe -side left -pady [list 3 0] -anchor w
4053 } elseif {$type eq "b"} {
4054 ${NS}::checkbutton $cframe.c_$id -text $title -variable newviewopts($n,$id)
4055 pack $cframe.c_$id -in $cframe -side left \
4056 -padx [list $lxpad 0] -expand $cexpand -anchor w
4057 } elseif {[regexp {^r(\d+)$} $type type sz]} {
4058 regexp {^(.*_)} $id uselessvar button_id
4059 ${NS}::radiobutton $cframe.c_$id -text $title -variable newviewopts($n,$button_id) -value $sz
4060 pack $cframe.c_$id -in $cframe -side left \
4061 -padx [list $lxpad 0] -expand $cexpand -anchor w
4062 } elseif {[regexp {^t(\d+)$} $type type sz]} {
4063 ${NS}::label $cframe.l_$id -text $title
4064 ${NS}::entry $cframe.e_$id -width $sz -background $bgcolor \
4065 -textvariable newviewopts($n,$id)
4066 pack $cframe.l_$id -in $cframe -side left -padx [list $lxpad 0]
4067 pack $cframe.e_$id -in $cframe -side left -expand 1 -fill x
4068 } elseif {[regexp {^t(\d+)=$} $type type sz]} {
4069 ${NS}::label $cframe.l_$id -text $title
4070 ${NS}::entry $cframe.e_$id -width $sz -background $bgcolor \
4071 -textvariable newviewopts($n,$id)
4072 pack $cframe.l_$id -in $cframe -side top -pady [list 3 0] -anchor w
4073 pack $cframe.e_$id -in $cframe -side top -fill x
4074 } elseif {$type eq "path"} {
4075 ${NS}::label $top.l -text $title
4076 pack $top.l -in $top -side top -pady [list 3 0] -anchor w -padx 3
4077 text $top.t -width 40 -height 5 -background $bgcolor
4078 if {[info exists viewfiles($n)]} {
4079 foreach f $viewfiles($n) {
4080 $top.t insert end $f
4081 $top.t insert end "\n"
4083 $top.t delete {end - 1c} end
4084 $top.t mark set insert 0.0
4086 pack $top.t -in $top -side top -pady [list 0 5] -fill both -expand 1 -padx 3
4090 ${NS}::frame $top.buts
4091 ${NS}::button $top.buts.ok -text [mc "OK"] -command [list newviewok $top $n]
4092 ${NS}::button $top.buts.apply -text [mc "Apply (F5)"] -command [list newviewok $top $n 1]
4093 ${NS}::button $top.buts.can -text [mc "Cancel"] -command [list destroy $top]
4094 bind $top <Control-Return> [list newviewok $top $n]
4095 bind $top <F5> [list newviewok $top $n 1]
4096 bind $top <Escape> [list destroy $top]
4097 grid $top.buts.ok $top.buts.apply $top.buts.can
4098 grid columnconfigure $top.buts 0 -weight 1 -uniform a
4099 grid columnconfigure $top.buts 1 -weight 1 -uniform a
4100 grid columnconfigure $top.buts 2 -weight 1 -uniform a
4101 pack $top.buts -in $top -side top -fill x
4102 focus $top.t
4105 proc doviewmenu {m first cmd op argv} {
4106 set nmenu [$m index end]
4107 for {set i $first} {$i <= $nmenu} {incr i} {
4108 if {[$m entrycget $i -command] eq $cmd} {
4109 eval $m $op $i $argv
4110 break
4115 proc allviewmenus {n op args} {
4116 # global viewhlmenu
4118 doviewmenu .bar.view 5 [list showview $n] $op $args
4119 # doviewmenu $viewhlmenu 1 [list addvhighlight $n] $op $args
4122 proc newviewok {top n {apply 0}} {
4123 global nextviewnum newviewperm newviewname newishighlight
4124 global viewname viewfiles viewperm selectedview curview
4125 global viewargs viewargscmd newviewopts viewhlmenu
4127 if {[catch {
4128 set newargs [encode_view_opts $n]
4129 } err]} {
4130 error_popup "[mc "Error in commit selection arguments:"] $err" $top
4131 return
4133 set files {}
4134 foreach f [split [$top.t get 0.0 end] "\n"] {
4135 set ft [string trim $f]
4136 if {$ft ne {}} {
4137 lappend files $ft
4140 if {![info exists viewfiles($n)]} {
4141 # creating a new view
4142 incr nextviewnum
4143 set viewname($n) $newviewname($n)
4144 set viewperm($n) $newviewopts($n,perm)
4145 set viewfiles($n) $files
4146 set viewargs($n) $newargs
4147 set viewargscmd($n) $newviewopts($n,cmd)
4148 addviewmenu $n
4149 if {!$newishighlight} {
4150 run showview $n
4151 } else {
4152 run addvhighlight $n
4154 } else {
4155 # editing an existing view
4156 set viewperm($n) $newviewopts($n,perm)
4157 if {$newviewname($n) ne $viewname($n)} {
4158 set viewname($n) $newviewname($n)
4159 doviewmenu .bar.view 5 [list showview $n] \
4160 entryconf [list -label $viewname($n)]
4161 # doviewmenu $viewhlmenu 1 [list addvhighlight $n] \
4162 # entryconf [list -label $viewname($n) -value $viewname($n)]
4164 if {$files ne $viewfiles($n) || $newargs ne $viewargs($n) || \
4165 $newviewopts($n,cmd) ne $viewargscmd($n)} {
4166 set viewfiles($n) $files
4167 set viewargs($n) $newargs
4168 set viewargscmd($n) $newviewopts($n,cmd)
4169 if {$curview == $n} {
4170 run reloadcommits
4174 if {$apply} return
4175 catch {destroy $top}
4178 proc delview {} {
4179 global curview viewperm hlview selectedhlview
4181 if {$curview == 0} return
4182 if {[info exists hlview] && $hlview == $curview} {
4183 set selectedhlview [mc "None"]
4184 unset hlview
4186 allviewmenus $curview delete
4187 set viewperm($curview) 0
4188 showview 0
4191 proc addviewmenu {n} {
4192 global viewname viewhlmenu
4194 .bar.view add radiobutton -label $viewname($n) \
4195 -command [list showview $n] -variable selectedview -value $n
4196 #$viewhlmenu add radiobutton -label $viewname($n) \
4197 # -command [list addvhighlight $n] -variable selectedhlview
4200 proc showview {n} {
4201 global curview cached_commitrow ordertok
4202 global displayorder parentlist rowidlist rowisopt rowfinal
4203 global colormap rowtextx nextcolor canvxmax
4204 global numcommits viewcomplete
4205 global selectedline currentid canv canvy0
4206 global treediffs
4207 global pending_select mainheadid
4208 global commitidx
4209 global selectedview
4210 global hlview selectedhlview commitinterest
4212 if {$n == $curview} return
4213 set selid {}
4214 set ymax [lindex [$canv cget -scrollregion] 3]
4215 set span [$canv yview]
4216 set ytop [expr {[lindex $span 0] * $ymax}]
4217 set ybot [expr {[lindex $span 1] * $ymax}]
4218 set yscreen [expr {($ybot - $ytop) / 2}]
4219 if {$selectedline ne {}} {
4220 set selid $currentid
4221 set y [yc $selectedline]
4222 if {$ytop < $y && $y < $ybot} {
4223 set yscreen [expr {$y - $ytop}]
4225 } elseif {[info exists pending_select]} {
4226 set selid $pending_select
4227 unset pending_select
4229 unselectline
4230 normalline
4231 catch {unset treediffs}
4232 clear_display
4233 if {[info exists hlview] && $hlview == $n} {
4234 unset hlview
4235 set selectedhlview [mc "None"]
4237 catch {unset commitinterest}
4238 catch {unset cached_commitrow}
4239 catch {unset ordertok}
4241 set curview $n
4242 set selectedview $n
4243 .bar.view entryconf [mca "Edit view..."] -state [expr {$n == 0? "disabled": "normal"}]
4244 .bar.view entryconf [mca "Delete view"] -state [expr {$n == 0? "disabled": "normal"}]
4246 run refill_reflist
4247 if {![info exists viewcomplete($n)]} {
4248 getcommits $selid
4249 return
4252 set displayorder {}
4253 set parentlist {}
4254 set rowidlist {}
4255 set rowisopt {}
4256 set rowfinal {}
4257 set numcommits $commitidx($n)
4259 catch {unset colormap}
4260 catch {unset rowtextx}
4261 set nextcolor 0
4262 set canvxmax [$canv cget -width]
4263 set curview $n
4264 set row 0
4265 setcanvscroll
4266 set yf 0
4267 set row {}
4268 if {$selid ne {} && [commitinview $selid $n]} {
4269 set row [rowofcommit $selid]
4270 # try to get the selected row in the same position on the screen
4271 set ymax [lindex [$canv cget -scrollregion] 3]
4272 set ytop [expr {[yc $row] - $yscreen}]
4273 if {$ytop < 0} {
4274 set ytop 0
4276 set yf [expr {$ytop * 1.0 / $ymax}]
4278 allcanvs yview moveto $yf
4279 drawvisible
4280 if {$row ne {}} {
4281 selectline $row 0
4282 } elseif {!$viewcomplete($n)} {
4283 reset_pending_select $selid
4284 } else {
4285 reset_pending_select {}
4287 if {[commitinview $pending_select $curview]} {
4288 selectline [rowofcommit $pending_select] 1
4289 } else {
4290 set row [first_real_row]
4291 if {$row < $numcommits} {
4292 selectline $row 0
4296 if {!$viewcomplete($n)} {
4297 if {$numcommits == 0} {
4298 show_status [mc "Reading commits..."]
4300 } elseif {$numcommits == 0} {
4301 show_status [mc "No commits selected"]
4305 # Stuff relating to the highlighting facility
4307 proc ishighlighted {id} {
4308 global vhighlights fhighlights nhighlights rhighlights
4310 if {[info exists nhighlights($id)] && $nhighlights($id) > 0} {
4311 return $nhighlights($id)
4313 if {[info exists vhighlights($id)] && $vhighlights($id) > 0} {
4314 return $vhighlights($id)
4316 if {[info exists fhighlights($id)] && $fhighlights($id) > 0} {
4317 return $fhighlights($id)
4319 if {[info exists rhighlights($id)] && $rhighlights($id) > 0} {
4320 return $rhighlights($id)
4322 return 0
4325 proc bolden {id font} {
4326 global canv linehtag currentid boldids need_redisplay markedid
4328 # need_redisplay = 1 means the display is stale and about to be redrawn
4329 if {$need_redisplay} return
4330 lappend boldids $id
4331 $canv itemconf $linehtag($id) -font $font
4332 if {[info exists currentid] && $id eq $currentid} {
4333 $canv delete secsel
4334 set t [eval $canv create rect [$canv bbox $linehtag($id)] \
4335 -outline {{}} -tags secsel \
4336 -fill [$canv cget -selectbackground]]
4337 $canv lower $t
4339 if {[info exists markedid] && $id eq $markedid} {
4340 make_idmark $id
4344 proc bolden_name {id font} {
4345 global canv2 linentag currentid boldnameids need_redisplay
4347 if {$need_redisplay} return
4348 lappend boldnameids $id
4349 $canv2 itemconf $linentag($id) -font $font
4350 if {[info exists currentid] && $id eq $currentid} {
4351 $canv2 delete secsel
4352 set t [eval $canv2 create rect [$canv2 bbox $linentag($id)] \
4353 -outline {{}} -tags secsel \
4354 -fill [$canv2 cget -selectbackground]]
4355 $canv2 lower $t
4359 proc unbolden {} {
4360 global boldids
4362 set stillbold {}
4363 foreach id $boldids {
4364 if {![ishighlighted $id]} {
4365 bolden $id mainfont
4366 } else {
4367 lappend stillbold $id
4370 set boldids $stillbold
4373 proc addvhighlight {n} {
4374 global hlview viewcomplete curview vhl_done commitidx
4376 if {[info exists hlview]} {
4377 delvhighlight
4379 set hlview $n
4380 if {$n != $curview && ![info exists viewcomplete($n)]} {
4381 start_rev_list $n
4383 set vhl_done $commitidx($hlview)
4384 if {$vhl_done > 0} {
4385 drawvisible
4389 proc delvhighlight {} {
4390 global hlview vhighlights
4392 if {![info exists hlview]} return
4393 unset hlview
4394 catch {unset vhighlights}
4395 unbolden
4398 proc vhighlightmore {} {
4399 global hlview vhl_done commitidx vhighlights curview
4401 set max $commitidx($hlview)
4402 set vr [visiblerows]
4403 set r0 [lindex $vr 0]
4404 set r1 [lindex $vr 1]
4405 for {set i $vhl_done} {$i < $max} {incr i} {
4406 set id [commitonrow $i $hlview]
4407 if {[commitinview $id $curview]} {
4408 set row [rowofcommit $id]
4409 if {$r0 <= $row && $row <= $r1} {
4410 if {![highlighted $row]} {
4411 bolden $id mainfontbold
4413 set vhighlights($id) 1
4417 set vhl_done $max
4418 return 0
4421 proc askvhighlight {row id} {
4422 global hlview vhighlights iddrawn
4424 if {[commitinview $id $hlview]} {
4425 if {[info exists iddrawn($id)] && ![ishighlighted $id]} {
4426 bolden $id mainfontbold
4428 set vhighlights($id) 1
4429 } else {
4430 set vhighlights($id) 0
4434 proc hfiles_change {} {
4435 global highlight_files filehighlight fhighlights fh_serial
4436 global highlight_paths
4438 if {[info exists filehighlight]} {
4439 # delete previous highlights
4440 catch {close $filehighlight}
4441 unset filehighlight
4442 catch {unset fhighlights}
4443 unbolden
4444 unhighlight_filelist
4446 set highlight_paths {}
4447 after cancel do_file_hl $fh_serial
4448 incr fh_serial
4449 if {$highlight_files ne {}} {
4450 after 300 do_file_hl $fh_serial
4454 proc gdttype_change {name ix op} {
4455 global gdttype highlight_files findstring findpattern
4457 stopfinding
4458 if {$findstring ne {}} {
4459 if {$gdttype eq [mc "containing:"]} {
4460 if {$highlight_files ne {}} {
4461 set highlight_files {}
4462 hfiles_change
4464 findcom_change
4465 } else {
4466 if {$findpattern ne {}} {
4467 set findpattern {}
4468 findcom_change
4470 set highlight_files $findstring
4471 hfiles_change
4473 drawvisible
4475 # enable/disable findtype/findloc menus too
4478 proc find_change {name ix op} {
4479 global gdttype findstring highlight_files
4481 stopfinding
4482 if {$gdttype eq [mc "containing:"]} {
4483 findcom_change
4484 } else {
4485 if {$highlight_files ne $findstring} {
4486 set highlight_files $findstring
4487 hfiles_change
4490 drawvisible
4493 proc findcom_change args {
4494 global nhighlights boldnameids
4495 global findpattern findtype findstring gdttype
4497 stopfinding
4498 # delete previous highlights, if any
4499 foreach id $boldnameids {
4500 bolden_name $id mainfont
4502 set boldnameids {}
4503 catch {unset nhighlights}
4504 unbolden
4505 unmarkmatches
4506 if {$gdttype ne [mc "containing:"] || $findstring eq {}} {
4507 set findpattern {}
4508 } elseif {$findtype eq [mc "Regexp"]} {
4509 set findpattern $findstring
4510 } else {
4511 set e [string map {"*" "\\*" "?" "\\?" "\[" "\\\[" "\\" "\\\\"} \
4512 $findstring]
4513 set findpattern "*$e*"
4517 proc makepatterns {l} {
4518 set ret {}
4519 foreach e $l {
4520 set ee [string map {"*" "\\*" "?" "\\?" "\[" "\\\[" "\\" "\\\\"} $e]
4521 if {[string index $ee end] eq "/"} {
4522 lappend ret "$ee*"
4523 } else {
4524 lappend ret $ee
4525 lappend ret "$ee/*"
4528 return $ret
4531 proc do_file_hl {serial} {
4532 global highlight_files filehighlight highlight_paths gdttype fhl_list
4534 if {$gdttype eq [mc "touching paths:"]} {
4535 if {[catch {set paths [shellsplit $highlight_files]}]} return
4536 set highlight_paths [makepatterns $paths]
4537 highlight_filelist
4538 set gdtargs [concat -- $paths]
4539 } elseif {$gdttype eq [mc "adding/removing string:"]} {
4540 set gdtargs [list "-S$highlight_files"]
4541 } else {
4542 # must be "containing:", i.e. we're searching commit info
4543 return
4545 set cmd [concat | git diff-tree -r -s --stdin $gdtargs]
4546 set filehighlight [open $cmd r+]
4547 fconfigure $filehighlight -blocking 0
4548 filerun $filehighlight readfhighlight
4549 set fhl_list {}
4550 drawvisible
4551 flushhighlights
4554 proc flushhighlights {} {
4555 global filehighlight fhl_list
4557 if {[info exists filehighlight]} {
4558 lappend fhl_list {}
4559 puts $filehighlight ""
4560 flush $filehighlight
4564 proc askfilehighlight {row id} {
4565 global filehighlight fhighlights fhl_list
4567 lappend fhl_list $id
4568 set fhighlights($id) -1
4569 puts $filehighlight $id
4572 proc readfhighlight {} {
4573 global filehighlight fhighlights curview iddrawn
4574 global fhl_list find_dirn
4576 if {![info exists filehighlight]} {
4577 return 0
4579 set nr 0
4580 while {[incr nr] <= 100 && [gets $filehighlight line] >= 0} {
4581 set line [string trim $line]
4582 set i [lsearch -exact $fhl_list $line]
4583 if {$i < 0} continue
4584 for {set j 0} {$j < $i} {incr j} {
4585 set id [lindex $fhl_list $j]
4586 set fhighlights($id) 0
4588 set fhl_list [lrange $fhl_list [expr {$i+1}] end]
4589 if {$line eq {}} continue
4590 if {![commitinview $line $curview]} continue
4591 if {[info exists iddrawn($line)] && ![ishighlighted $line]} {
4592 bolden $line mainfontbold
4594 set fhighlights($line) 1
4596 if {[eof $filehighlight]} {
4597 # strange...
4598 puts "oops, git diff-tree died"
4599 catch {close $filehighlight}
4600 unset filehighlight
4601 return 0
4603 if {[info exists find_dirn]} {
4604 run findmore
4606 return 1
4609 proc doesmatch {f} {
4610 global findtype findpattern
4612 if {$findtype eq [mc "Regexp"]} {
4613 return [regexp $findpattern $f]
4614 } elseif {$findtype eq [mc "IgnCase"]} {
4615 return [string match -nocase $findpattern $f]
4616 } else {
4617 return [string match $findpattern $f]
4621 proc askfindhighlight {row id} {
4622 global nhighlights commitinfo iddrawn
4623 global findloc
4624 global markingmatches
4626 if {![info exists commitinfo($id)]} {
4627 getcommit $id
4629 set info $commitinfo($id)
4630 set isbold 0
4631 set fldtypes [list [mc Headline] [mc Author] [mc Date] [mc Committer] [mc CDate] [mc Comments]]
4632 foreach f $info ty $fldtypes {
4633 if {($findloc eq [mc "All fields"] || $findloc eq $ty) &&
4634 [doesmatch $f]} {
4635 if {$ty eq [mc "Author"]} {
4636 set isbold 2
4637 break
4639 set isbold 1
4642 if {$isbold && [info exists iddrawn($id)]} {
4643 if {![ishighlighted $id]} {
4644 bolden $id mainfontbold
4645 if {$isbold > 1} {
4646 bolden_name $id mainfontbold
4649 if {$markingmatches} {
4650 markrowmatches $row $id
4653 set nhighlights($id) $isbold
4656 proc markrowmatches {row id} {
4657 global canv canv2 linehtag linentag commitinfo findloc
4659 set headline [lindex $commitinfo($id) 0]
4660 set author [lindex $commitinfo($id) 1]
4661 $canv delete match$row
4662 $canv2 delete match$row
4663 if {$findloc eq [mc "All fields"] || $findloc eq [mc "Headline"]} {
4664 set m [findmatches $headline]
4665 if {$m ne {}} {
4666 markmatches $canv $row $headline $linehtag($id) $m \
4667 [$canv itemcget $linehtag($id) -font] $row
4670 if {$findloc eq [mc "All fields"] || $findloc eq [mc "Author"]} {
4671 set m [findmatches $author]
4672 if {$m ne {}} {
4673 markmatches $canv2 $row $author $linentag($id) $m \
4674 [$canv2 itemcget $linentag($id) -font] $row
4679 proc vrel_change {name ix op} {
4680 global highlight_related
4682 rhighlight_none
4683 if {$highlight_related ne [mc "None"]} {
4684 run drawvisible
4688 # prepare for testing whether commits are descendents or ancestors of a
4689 proc rhighlight_sel {a} {
4690 global descendent desc_todo ancestor anc_todo
4691 global highlight_related
4693 catch {unset descendent}
4694 set desc_todo [list $a]
4695 catch {unset ancestor}
4696 set anc_todo [list $a]
4697 if {$highlight_related ne [mc "None"]} {
4698 rhighlight_none
4699 run drawvisible
4703 proc rhighlight_none {} {
4704 global rhighlights
4706 catch {unset rhighlights}
4707 unbolden
4710 proc is_descendent {a} {
4711 global curview children descendent desc_todo
4713 set v $curview
4714 set la [rowofcommit $a]
4715 set todo $desc_todo
4716 set leftover {}
4717 set done 0
4718 for {set i 0} {$i < [llength $todo]} {incr i} {
4719 set do [lindex $todo $i]
4720 if {[rowofcommit $do] < $la} {
4721 lappend leftover $do
4722 continue
4724 foreach nk $children($v,$do) {
4725 if {![info exists descendent($nk)]} {
4726 set descendent($nk) 1
4727 lappend todo $nk
4728 if {$nk eq $a} {
4729 set done 1
4733 if {$done} {
4734 set desc_todo [concat $leftover [lrange $todo [expr {$i+1}] end]]
4735 return
4738 set descendent($a) 0
4739 set desc_todo $leftover
4742 proc is_ancestor {a} {
4743 global curview parents ancestor anc_todo
4745 set v $curview
4746 set la [rowofcommit $a]
4747 set todo $anc_todo
4748 set leftover {}
4749 set done 0
4750 for {set i 0} {$i < [llength $todo]} {incr i} {
4751 set do [lindex $todo $i]
4752 if {![commitinview $do $v] || [rowofcommit $do] > $la} {
4753 lappend leftover $do
4754 continue
4756 foreach np $parents($v,$do) {
4757 if {![info exists ancestor($np)]} {
4758 set ancestor($np) 1
4759 lappend todo $np
4760 if {$np eq $a} {
4761 set done 1
4765 if {$done} {
4766 set anc_todo [concat $leftover [lrange $todo [expr {$i+1}] end]]
4767 return
4770 set ancestor($a) 0
4771 set anc_todo $leftover
4774 proc askrelhighlight {row id} {
4775 global descendent highlight_related iddrawn rhighlights
4776 global selectedline ancestor
4778 if {$selectedline eq {}} return
4779 set isbold 0
4780 if {$highlight_related eq [mc "Descendant"] ||
4781 $highlight_related eq [mc "Not descendant"]} {
4782 if {![info exists descendent($id)]} {
4783 is_descendent $id
4785 if {$descendent($id) == ($highlight_related eq [mc "Descendant"])} {
4786 set isbold 1
4788 } elseif {$highlight_related eq [mc "Ancestor"] ||
4789 $highlight_related eq [mc "Not ancestor"]} {
4790 if {![info exists ancestor($id)]} {
4791 is_ancestor $id
4793 if {$ancestor($id) == ($highlight_related eq [mc "Ancestor"])} {
4794 set isbold 1
4797 if {[info exists iddrawn($id)]} {
4798 if {$isbold && ![ishighlighted $id]} {
4799 bolden $id mainfontbold
4802 set rhighlights($id) $isbold
4805 # Graph layout functions
4807 proc shortids {ids} {
4808 set res {}
4809 foreach id $ids {
4810 if {[llength $id] > 1} {
4811 lappend res [shortids $id]
4812 } elseif {[regexp {^[0-9a-f]{40}$} $id]} {
4813 lappend res [string range $id 0 7]
4814 } else {
4815 lappend res $id
4818 return $res
4821 proc ntimes {n o} {
4822 set ret {}
4823 set o [list $o]
4824 for {set mask 1} {$mask <= $n} {incr mask $mask} {
4825 if {($n & $mask) != 0} {
4826 set ret [concat $ret $o]
4828 set o [concat $o $o]
4830 return $ret
4833 proc ordertoken {id} {
4834 global ordertok curview varcid varcstart varctok curview parents children
4835 global nullid nullid2
4837 if {[info exists ordertok($id)]} {
4838 return $ordertok($id)
4840 set origid $id
4841 set todo {}
4842 while {1} {
4843 if {[info exists varcid($curview,$id)]} {
4844 set a $varcid($curview,$id)
4845 set p [lindex $varcstart($curview) $a]
4846 } else {
4847 set p [lindex $children($curview,$id) 0]
4849 if {[info exists ordertok($p)]} {
4850 set tok $ordertok($p)
4851 break
4853 set id [first_real_child $curview,$p]
4854 if {$id eq {}} {
4855 # it's a root
4856 set tok [lindex $varctok($curview) $varcid($curview,$p)]
4857 break
4859 if {[llength $parents($curview,$id)] == 1} {
4860 lappend todo [list $p {}]
4861 } else {
4862 set j [lsearch -exact $parents($curview,$id) $p]
4863 if {$j < 0} {
4864 puts "oops didn't find [shortids $p] in parents of [shortids $id]"
4866 lappend todo [list $p [strrep $j]]
4869 for {set i [llength $todo]} {[incr i -1] >= 0} {} {
4870 set p [lindex $todo $i 0]
4871 append tok [lindex $todo $i 1]
4872 set ordertok($p) $tok
4874 set ordertok($origid) $tok
4875 return $tok
4878 # Work out where id should go in idlist so that order-token
4879 # values increase from left to right
4880 proc idcol {idlist id {i 0}} {
4881 set t [ordertoken $id]
4882 if {$i < 0} {
4883 set i 0
4885 if {$i >= [llength $idlist] || $t < [ordertoken [lindex $idlist $i]]} {
4886 if {$i > [llength $idlist]} {
4887 set i [llength $idlist]
4889 while {[incr i -1] >= 0 && $t < [ordertoken [lindex $idlist $i]]} {}
4890 incr i
4891 } else {
4892 if {$t > [ordertoken [lindex $idlist $i]]} {
4893 while {[incr i] < [llength $idlist] &&
4894 $t >= [ordertoken [lindex $idlist $i]]} {}
4897 return $i
4900 proc initlayout {} {
4901 global rowidlist rowisopt rowfinal displayorder parentlist
4902 global numcommits canvxmax canv
4903 global nextcolor
4904 global colormap rowtextx
4906 set numcommits 0
4907 set displayorder {}
4908 set parentlist {}
4909 set nextcolor 0
4910 set rowidlist {}
4911 set rowisopt {}
4912 set rowfinal {}
4913 set canvxmax [$canv cget -width]
4914 catch {unset colormap}
4915 catch {unset rowtextx}
4916 setcanvscroll
4919 proc setcanvscroll {} {
4920 global canv canv2 canv3 numcommits linespc canvxmax canvy0
4921 global lastscrollset lastscrollrows
4923 set ymax [expr {$canvy0 + ($numcommits - 0.5) * $linespc + 2}]
4924 $canv conf -scrollregion [list 0 0 $canvxmax $ymax]
4925 $canv2 conf -scrollregion [list 0 0 0 $ymax]
4926 $canv3 conf -scrollregion [list 0 0 0 $ymax]
4927 set lastscrollset [clock clicks -milliseconds]
4928 set lastscrollrows $numcommits
4931 proc visiblerows {} {
4932 global canv numcommits linespc
4934 set ymax [lindex [$canv cget -scrollregion] 3]
4935 if {$ymax eq {} || $ymax == 0} return
4936 set f [$canv yview]
4937 set y0 [expr {int([lindex $f 0] * $ymax)}]
4938 set r0 [expr {int(($y0 - 3) / $linespc) - 1}]
4939 if {$r0 < 0} {
4940 set r0 0
4942 set y1 [expr {int([lindex $f 1] * $ymax)}]
4943 set r1 [expr {int(($y1 - 3) / $linespc) + 1}]
4944 if {$r1 >= $numcommits} {
4945 set r1 [expr {$numcommits - 1}]
4947 return [list $r0 $r1]
4950 proc layoutmore {} {
4951 global commitidx viewcomplete curview
4952 global numcommits pending_select curview
4953 global lastscrollset lastscrollrows
4955 if {$lastscrollrows < 100 || $viewcomplete($curview) ||
4956 [clock clicks -milliseconds] - $lastscrollset > 500} {
4957 setcanvscroll
4959 if {[info exists pending_select] &&
4960 [commitinview $pending_select $curview]} {
4961 update
4962 selectline [rowofcommit $pending_select] 1
4964 drawvisible
4967 # With path limiting, we mightn't get the actual HEAD commit,
4968 # so ask git rev-list what is the first ancestor of HEAD that
4969 # touches a file in the path limit.
4970 proc get_viewmainhead {view} {
4971 global viewmainheadid vfilelimit viewinstances mainheadid
4973 catch {
4974 set rfd [open [concat | git rev-list -1 $mainheadid \
4975 -- $vfilelimit($view)] r]
4976 set j [reg_instance $rfd]
4977 lappend viewinstances($view) $j
4978 fconfigure $rfd -blocking 0
4979 filerun $rfd [list getviewhead $rfd $j $view]
4980 set viewmainheadid($curview) {}
4984 # git rev-list should give us just 1 line to use as viewmainheadid($view)
4985 proc getviewhead {fd inst view} {
4986 global viewmainheadid commfd curview viewinstances showlocalchanges
4988 set id {}
4989 if {[gets $fd line] < 0} {
4990 if {![eof $fd]} {
4991 return 1
4993 } elseif {[string length $line] == 40 && [string is xdigit $line]} {
4994 set id $line
4996 set viewmainheadid($view) $id
4997 close $fd
4998 unset commfd($inst)
4999 set i [lsearch -exact $viewinstances($view) $inst]
5000 if {$i >= 0} {
5001 set viewinstances($view) [lreplace $viewinstances($view) $i $i]
5003 if {$showlocalchanges && $id ne {} && $view == $curview} {
5004 doshowlocalchanges
5006 return 0
5009 proc doshowlocalchanges {} {
5010 global curview viewmainheadid
5012 if {$viewmainheadid($curview) eq {}} return
5013 if {[commitinview $viewmainheadid($curview) $curview]} {
5014 dodiffindex
5015 } else {
5016 interestedin $viewmainheadid($curview) dodiffindex
5020 proc dohidelocalchanges {} {
5021 global nullid nullid2 lserial curview
5023 if {[commitinview $nullid $curview]} {
5024 removefakerow $nullid
5026 if {[commitinview $nullid2 $curview]} {
5027 removefakerow $nullid2
5029 incr lserial
5032 # spawn off a process to do git diff-index --cached HEAD
5033 proc dodiffindex {} {
5034 global lserial showlocalchanges vfilelimit curview
5035 global isworktree
5037 if {!$showlocalchanges || !$isworktree} return
5038 incr lserial
5039 set cmd "|git diff-index --cached HEAD"
5040 if {$vfilelimit($curview) ne {}} {
5041 set cmd [concat $cmd -- $vfilelimit($curview)]
5043 set fd [open $cmd r]
5044 fconfigure $fd -blocking 0
5045 set i [reg_instance $fd]
5046 filerun $fd [list readdiffindex $fd $lserial $i]
5049 proc readdiffindex {fd serial inst} {
5050 global viewmainheadid nullid nullid2 curview commitinfo commitdata lserial
5051 global vfilelimit
5053 set isdiff 1
5054 if {[gets $fd line] < 0} {
5055 if {![eof $fd]} {
5056 return 1
5058 set isdiff 0
5060 # we only need to see one line and we don't really care what it says...
5061 stop_instance $inst
5063 if {$serial != $lserial} {
5064 return 0
5067 # now see if there are any local changes not checked in to the index
5068 set cmd "|git diff-files"
5069 if {$vfilelimit($curview) ne {}} {
5070 set cmd [concat $cmd -- $vfilelimit($curview)]
5072 set fd [open $cmd r]
5073 fconfigure $fd -blocking 0
5074 set i [reg_instance $fd]
5075 filerun $fd [list readdifffiles $fd $serial $i]
5077 if {$isdiff && ![commitinview $nullid2 $curview]} {
5078 # add the line for the changes in the index to the graph
5079 set hl [mc "Local changes checked in to index but not committed"]
5080 set commitinfo($nullid2) [list $hl {} {} {} {} " $hl\n"]
5081 set commitdata($nullid2) "\n $hl\n"
5082 if {[commitinview $nullid $curview]} {
5083 removefakerow $nullid
5085 insertfakerow $nullid2 $viewmainheadid($curview)
5086 } elseif {!$isdiff && [commitinview $nullid2 $curview]} {
5087 if {[commitinview $nullid $curview]} {
5088 removefakerow $nullid
5090 removefakerow $nullid2
5092 return 0
5095 proc readdifffiles {fd serial inst} {
5096 global viewmainheadid nullid nullid2 curview
5097 global commitinfo commitdata lserial
5099 set isdiff 1
5100 if {[gets $fd line] < 0} {
5101 if {![eof $fd]} {
5102 return 1
5104 set isdiff 0
5106 # we only need to see one line and we don't really care what it says...
5107 stop_instance $inst
5109 if {$serial != $lserial} {
5110 return 0
5113 if {$isdiff && ![commitinview $nullid $curview]} {
5114 # add the line for the local diff to the graph
5115 set hl [mc "Local uncommitted changes, not checked in to index"]
5116 set commitinfo($nullid) [list $hl {} {} {} {} " $hl\n"]
5117 set commitdata($nullid) "\n $hl\n"
5118 if {[commitinview $nullid2 $curview]} {
5119 set p $nullid2
5120 } else {
5121 set p $viewmainheadid($curview)
5123 insertfakerow $nullid $p
5124 } elseif {!$isdiff && [commitinview $nullid $curview]} {
5125 removefakerow $nullid
5127 return 0
5130 proc nextuse {id row} {
5131 global curview children
5133 if {[info exists children($curview,$id)]} {
5134 foreach kid $children($curview,$id) {
5135 if {![commitinview $kid $curview]} {
5136 return -1
5138 if {[rowofcommit $kid] > $row} {
5139 return [rowofcommit $kid]
5143 if {[commitinview $id $curview]} {
5144 return [rowofcommit $id]
5146 return -1
5149 proc prevuse {id row} {
5150 global curview children
5152 set ret -1
5153 if {[info exists children($curview,$id)]} {
5154 foreach kid $children($curview,$id) {
5155 if {![commitinview $kid $curview]} break
5156 if {[rowofcommit $kid] < $row} {
5157 set ret [rowofcommit $kid]
5161 return $ret
5164 proc make_idlist {row} {
5165 global displayorder parentlist uparrowlen downarrowlen mingaplen
5166 global commitidx curview children
5168 set r [expr {$row - $mingaplen - $downarrowlen - 1}]
5169 if {$r < 0} {
5170 set r 0
5172 set ra [expr {$row - $downarrowlen}]
5173 if {$ra < 0} {
5174 set ra 0
5176 set rb [expr {$row + $uparrowlen}]
5177 if {$rb > $commitidx($curview)} {
5178 set rb $commitidx($curview)
5180 make_disporder $r [expr {$rb + 1}]
5181 set ids {}
5182 for {} {$r < $ra} {incr r} {
5183 set nextid [lindex $displayorder [expr {$r + 1}]]
5184 foreach p [lindex $parentlist $r] {
5185 if {$p eq $nextid} continue
5186 set rn [nextuse $p $r]
5187 if {$rn >= $row &&
5188 $rn <= $r + $downarrowlen + $mingaplen + $uparrowlen} {
5189 lappend ids [list [ordertoken $p] $p]
5193 for {} {$r < $row} {incr r} {
5194 set nextid [lindex $displayorder [expr {$r + 1}]]
5195 foreach p [lindex $parentlist $r] {
5196 if {$p eq $nextid} continue
5197 set rn [nextuse $p $r]
5198 if {$rn < 0 || $rn >= $row} {
5199 lappend ids [list [ordertoken $p] $p]
5203 set id [lindex $displayorder $row]
5204 lappend ids [list [ordertoken $id] $id]
5205 while {$r < $rb} {
5206 foreach p [lindex $parentlist $r] {
5207 set firstkid [lindex $children($curview,$p) 0]
5208 if {[rowofcommit $firstkid] < $row} {
5209 lappend ids [list [ordertoken $p] $p]
5212 incr r
5213 set id [lindex $displayorder $r]
5214 if {$id ne {}} {
5215 set firstkid [lindex $children($curview,$id) 0]
5216 if {$firstkid ne {} && [rowofcommit $firstkid] < $row} {
5217 lappend ids [list [ordertoken $id] $id]
5221 set idlist {}
5222 foreach idx [lsort -unique $ids] {
5223 lappend idlist [lindex $idx 1]
5225 return $idlist
5228 proc rowsequal {a b} {
5229 while {[set i [lsearch -exact $a {}]] >= 0} {
5230 set a [lreplace $a $i $i]
5232 while {[set i [lsearch -exact $b {}]] >= 0} {
5233 set b [lreplace $b $i $i]
5235 return [expr {$a eq $b}]
5238 proc makeupline {id row rend col} {
5239 global rowidlist uparrowlen downarrowlen mingaplen
5241 for {set r $rend} {1} {set r $rstart} {
5242 set rstart [prevuse $id $r]
5243 if {$rstart < 0} return
5244 if {$rstart < $row} break
5246 if {$rstart + $uparrowlen + $mingaplen + $downarrowlen < $rend} {
5247 set rstart [expr {$rend - $uparrowlen - 1}]
5249 for {set r $rstart} {[incr r] <= $row} {} {
5250 set idlist [lindex $rowidlist $r]
5251 if {$idlist ne {} && [lsearch -exact $idlist $id] < 0} {
5252 set col [idcol $idlist $id $col]
5253 lset rowidlist $r [linsert $idlist $col $id]
5254 changedrow $r
5259 proc layoutrows {row endrow} {
5260 global rowidlist rowisopt rowfinal displayorder
5261 global uparrowlen downarrowlen maxwidth mingaplen
5262 global children parentlist
5263 global commitidx viewcomplete curview
5265 make_disporder [expr {$row - 1}] [expr {$endrow + $uparrowlen}]
5266 set idlist {}
5267 if {$row > 0} {
5268 set rm1 [expr {$row - 1}]
5269 foreach id [lindex $rowidlist $rm1] {
5270 if {$id ne {}} {
5271 lappend idlist $id
5274 set final [lindex $rowfinal $rm1]
5276 for {} {$row < $endrow} {incr row} {
5277 set rm1 [expr {$row - 1}]
5278 if {$rm1 < 0 || $idlist eq {}} {
5279 set idlist [make_idlist $row]
5280 set final 1
5281 } else {
5282 set id [lindex $displayorder $rm1]
5283 set col [lsearch -exact $idlist $id]
5284 set idlist [lreplace $idlist $col $col]
5285 foreach p [lindex $parentlist $rm1] {
5286 if {[lsearch -exact $idlist $p] < 0} {
5287 set col [idcol $idlist $p $col]
5288 set idlist [linsert $idlist $col $p]
5289 # if not the first child, we have to insert a line going up
5290 if {$id ne [lindex $children($curview,$p) 0]} {
5291 makeupline $p $rm1 $row $col
5295 set id [lindex $displayorder $row]
5296 if {$row > $downarrowlen} {
5297 set termrow [expr {$row - $downarrowlen - 1}]
5298 foreach p [lindex $parentlist $termrow] {
5299 set i [lsearch -exact $idlist $p]
5300 if {$i < 0} continue
5301 set nr [nextuse $p $termrow]
5302 if {$nr < 0 || $nr >= $row + $mingaplen + $uparrowlen} {
5303 set idlist [lreplace $idlist $i $i]
5307 set col [lsearch -exact $idlist $id]
5308 if {$col < 0} {
5309 set col [idcol $idlist $id]
5310 set idlist [linsert $idlist $col $id]
5311 if {$children($curview,$id) ne {}} {
5312 makeupline $id $rm1 $row $col
5315 set r [expr {$row + $uparrowlen - 1}]
5316 if {$r < $commitidx($curview)} {
5317 set x $col
5318 foreach p [lindex $parentlist $r] {
5319 if {[lsearch -exact $idlist $p] >= 0} continue
5320 set fk [lindex $children($curview,$p) 0]
5321 if {[rowofcommit $fk] < $row} {
5322 set x [idcol $idlist $p $x]
5323 set idlist [linsert $idlist $x $p]
5326 if {[incr r] < $commitidx($curview)} {
5327 set p [lindex $displayorder $r]
5328 if {[lsearch -exact $idlist $p] < 0} {
5329 set fk [lindex $children($curview,$p) 0]
5330 if {$fk ne {} && [rowofcommit $fk] < $row} {
5331 set x [idcol $idlist $p $x]
5332 set idlist [linsert $idlist $x $p]
5338 if {$final && !$viewcomplete($curview) &&
5339 $row + $uparrowlen + $mingaplen + $downarrowlen
5340 >= $commitidx($curview)} {
5341 set final 0
5343 set l [llength $rowidlist]
5344 if {$row == $l} {
5345 lappend rowidlist $idlist
5346 lappend rowisopt 0
5347 lappend rowfinal $final
5348 } elseif {$row < $l} {
5349 if {![rowsequal $idlist [lindex $rowidlist $row]]} {
5350 lset rowidlist $row $idlist
5351 changedrow $row
5353 lset rowfinal $row $final
5354 } else {
5355 set pad [ntimes [expr {$row - $l}] {}]
5356 set rowidlist [concat $rowidlist $pad]
5357 lappend rowidlist $idlist
5358 set rowfinal [concat $rowfinal $pad]
5359 lappend rowfinal $final
5360 set rowisopt [concat $rowisopt [ntimes [expr {$row - $l + 1}] 0]]
5363 return $row
5366 proc changedrow {row} {
5367 global displayorder iddrawn rowisopt need_redisplay
5369 set l [llength $rowisopt]
5370 if {$row < $l} {
5371 lset rowisopt $row 0
5372 if {$row + 1 < $l} {
5373 lset rowisopt [expr {$row + 1}] 0
5374 if {$row + 2 < $l} {
5375 lset rowisopt [expr {$row + 2}] 0
5379 set id [lindex $displayorder $row]
5380 if {[info exists iddrawn($id)]} {
5381 set need_redisplay 1
5385 proc insert_pad {row col npad} {
5386 global rowidlist
5388 set pad [ntimes $npad {}]
5389 set idlist [lindex $rowidlist $row]
5390 set bef [lrange $idlist 0 [expr {$col - 1}]]
5391 set aft [lrange $idlist $col end]
5392 set i [lsearch -exact $aft {}]
5393 if {$i > 0} {
5394 set aft [lreplace $aft $i $i]
5396 lset rowidlist $row [concat $bef $pad $aft]
5397 changedrow $row
5400 proc optimize_rows {row col endrow} {
5401 global rowidlist rowisopt displayorder curview children
5403 if {$row < 1} {
5404 set row 1
5406 for {} {$row < $endrow} {incr row; set col 0} {
5407 if {[lindex $rowisopt $row]} continue
5408 set haspad 0
5409 set y0 [expr {$row - 1}]
5410 set ym [expr {$row - 2}]
5411 set idlist [lindex $rowidlist $row]
5412 set previdlist [lindex $rowidlist $y0]
5413 if {$idlist eq {} || $previdlist eq {}} continue
5414 if {$ym >= 0} {
5415 set pprevidlist [lindex $rowidlist $ym]
5416 if {$pprevidlist eq {}} continue
5417 } else {
5418 set pprevidlist {}
5420 set x0 -1
5421 set xm -1
5422 for {} {$col < [llength $idlist]} {incr col} {
5423 set id [lindex $idlist $col]
5424 if {[lindex $previdlist $col] eq $id} continue
5425 if {$id eq {}} {
5426 set haspad 1
5427 continue
5429 set x0 [lsearch -exact $previdlist $id]
5430 if {$x0 < 0} continue
5431 set z [expr {$x0 - $col}]
5432 set isarrow 0
5433 set z0 {}
5434 if {$ym >= 0} {
5435 set xm [lsearch -exact $pprevidlist $id]
5436 if {$xm >= 0} {
5437 set z0 [expr {$xm - $x0}]
5440 if {$z0 eq {}} {
5441 # if row y0 is the first child of $id then it's not an arrow
5442 if {[lindex $children($curview,$id) 0] ne
5443 [lindex $displayorder $y0]} {
5444 set isarrow 1
5447 if {!$isarrow && $id ne [lindex $displayorder $row] &&
5448 [lsearch -exact [lindex $rowidlist [expr {$row+1}]] $id] < 0} {
5449 set isarrow 1
5451 # Looking at lines from this row to the previous row,
5452 # make them go straight up if they end in an arrow on
5453 # the previous row; otherwise make them go straight up
5454 # or at 45 degrees.
5455 if {$z < -1 || ($z < 0 && $isarrow)} {
5456 # Line currently goes left too much;
5457 # insert pads in the previous row, then optimize it
5458 set npad [expr {-1 - $z + $isarrow}]
5459 insert_pad $y0 $x0 $npad
5460 if {$y0 > 0} {
5461 optimize_rows $y0 $x0 $row
5463 set previdlist [lindex $rowidlist $y0]
5464 set x0 [lsearch -exact $previdlist $id]
5465 set z [expr {$x0 - $col}]
5466 if {$z0 ne {}} {
5467 set pprevidlist [lindex $rowidlist $ym]
5468 set xm [lsearch -exact $pprevidlist $id]
5469 set z0 [expr {$xm - $x0}]
5471 } elseif {$z > 1 || ($z > 0 && $isarrow)} {
5472 # Line currently goes right too much;
5473 # insert pads in this line
5474 set npad [expr {$z - 1 + $isarrow}]
5475 insert_pad $row $col $npad
5476 set idlist [lindex $rowidlist $row]
5477 incr col $npad
5478 set z [expr {$x0 - $col}]
5479 set haspad 1
5481 if {$z0 eq {} && !$isarrow && $ym >= 0} {
5482 # this line links to its first child on row $row-2
5483 set id [lindex $displayorder $ym]
5484 set xc [lsearch -exact $pprevidlist $id]
5485 if {$xc >= 0} {
5486 set z0 [expr {$xc - $x0}]
5489 # avoid lines jigging left then immediately right
5490 if {$z0 ne {} && $z < 0 && $z0 > 0} {
5491 insert_pad $y0 $x0 1
5492 incr x0
5493 optimize_rows $y0 $x0 $row
5494 set previdlist [lindex $rowidlist $y0]
5497 if {!$haspad} {
5498 # Find the first column that doesn't have a line going right
5499 for {set col [llength $idlist]} {[incr col -1] >= 0} {} {
5500 set id [lindex $idlist $col]
5501 if {$id eq {}} break
5502 set x0 [lsearch -exact $previdlist $id]
5503 if {$x0 < 0} {
5504 # check if this is the link to the first child
5505 set kid [lindex $displayorder $y0]
5506 if {[lindex $children($curview,$id) 0] eq $kid} {
5507 # it is, work out offset to child
5508 set x0 [lsearch -exact $previdlist $kid]
5511 if {$x0 <= $col} break
5513 # Insert a pad at that column as long as it has a line and
5514 # isn't the last column
5515 if {$x0 >= 0 && [incr col] < [llength $idlist]} {
5516 set idlist [linsert $idlist $col {}]
5517 lset rowidlist $row $idlist
5518 changedrow $row
5524 proc xc {row col} {
5525 global canvx0 linespc
5526 return [expr {$canvx0 + $col * $linespc}]
5529 proc yc {row} {
5530 global canvy0 linespc
5531 return [expr {$canvy0 + $row * $linespc}]
5534 proc linewidth {id} {
5535 global thickerline lthickness
5537 set wid $lthickness
5538 if {[info exists thickerline] && $id eq $thickerline} {
5539 set wid [expr {2 * $lthickness}]
5541 return $wid
5544 proc rowranges {id} {
5545 global curview children uparrowlen downarrowlen
5546 global rowidlist
5548 set kids $children($curview,$id)
5549 if {$kids eq {}} {
5550 return {}
5552 set ret {}
5553 lappend kids $id
5554 foreach child $kids {
5555 if {![commitinview $child $curview]} break
5556 set row [rowofcommit $child]
5557 if {![info exists prev]} {
5558 lappend ret [expr {$row + 1}]
5559 } else {
5560 if {$row <= $prevrow} {
5561 puts "oops children of [shortids $id] out of order [shortids $child] $row <= [shortids $prev] $prevrow"
5563 # see if the line extends the whole way from prevrow to row
5564 if {$row > $prevrow + $uparrowlen + $downarrowlen &&
5565 [lsearch -exact [lindex $rowidlist \
5566 [expr {int(($row + $prevrow) / 2)}]] $id] < 0} {
5567 # it doesn't, see where it ends
5568 set r [expr {$prevrow + $downarrowlen}]
5569 if {[lsearch -exact [lindex $rowidlist $r] $id] < 0} {
5570 while {[incr r -1] > $prevrow &&
5571 [lsearch -exact [lindex $rowidlist $r] $id] < 0} {}
5572 } else {
5573 while {[incr r] <= $row &&
5574 [lsearch -exact [lindex $rowidlist $r] $id] >= 0} {}
5575 incr r -1
5577 lappend ret $r
5578 # see where it starts up again
5579 set r [expr {$row - $uparrowlen}]
5580 if {[lsearch -exact [lindex $rowidlist $r] $id] < 0} {
5581 while {[incr r] < $row &&
5582 [lsearch -exact [lindex $rowidlist $r] $id] < 0} {}
5583 } else {
5584 while {[incr r -1] >= $prevrow &&
5585 [lsearch -exact [lindex $rowidlist $r] $id] >= 0} {}
5586 incr r
5588 lappend ret $r
5591 if {$child eq $id} {
5592 lappend ret $row
5594 set prev $child
5595 set prevrow $row
5597 return $ret
5600 proc drawlineseg {id row endrow arrowlow} {
5601 global rowidlist displayorder iddrawn linesegs
5602 global canv colormap linespc curview maxlinelen parentlist
5604 set cols [list [lsearch -exact [lindex $rowidlist $row] $id]]
5605 set le [expr {$row + 1}]
5606 set arrowhigh 1
5607 while {1} {
5608 set c [lsearch -exact [lindex $rowidlist $le] $id]
5609 if {$c < 0} {
5610 incr le -1
5611 break
5613 lappend cols $c
5614 set x [lindex $displayorder $le]
5615 if {$x eq $id} {
5616 set arrowhigh 0
5617 break
5619 if {[info exists iddrawn($x)] || $le == $endrow} {
5620 set c [lsearch -exact [lindex $rowidlist [expr {$le+1}]] $id]
5621 if {$c >= 0} {
5622 lappend cols $c
5623 set arrowhigh 0
5625 break
5627 incr le
5629 if {$le <= $row} {
5630 return $row
5633 set lines {}
5634 set i 0
5635 set joinhigh 0
5636 if {[info exists linesegs($id)]} {
5637 set lines $linesegs($id)
5638 foreach li $lines {
5639 set r0 [lindex $li 0]
5640 if {$r0 > $row} {
5641 if {$r0 == $le && [lindex $li 1] - $row <= $maxlinelen} {
5642 set joinhigh 1
5644 break
5646 incr i
5649 set joinlow 0
5650 if {$i > 0} {
5651 set li [lindex $lines [expr {$i-1}]]
5652 set r1 [lindex $li 1]
5653 if {$r1 == $row && $le - [lindex $li 0] <= $maxlinelen} {
5654 set joinlow 1
5658 set x [lindex $cols [expr {$le - $row}]]
5659 set xp [lindex $cols [expr {$le - 1 - $row}]]
5660 set dir [expr {$xp - $x}]
5661 if {$joinhigh} {
5662 set ith [lindex $lines $i 2]
5663 set coords [$canv coords $ith]
5664 set ah [$canv itemcget $ith -arrow]
5665 set arrowhigh [expr {$ah eq "first" || $ah eq "both"}]
5666 set x2 [lindex $cols [expr {$le + 1 - $row}]]
5667 if {$x2 ne {} && $x - $x2 == $dir} {
5668 set coords [lrange $coords 0 end-2]
5670 } else {
5671 set coords [list [xc $le $x] [yc $le]]
5673 if {$joinlow} {
5674 set itl [lindex $lines [expr {$i-1}] 2]
5675 set al [$canv itemcget $itl -arrow]
5676 set arrowlow [expr {$al eq "last" || $al eq "both"}]
5677 } elseif {$arrowlow} {
5678 if {[lsearch -exact [lindex $rowidlist [expr {$row-1}]] $id] >= 0 ||
5679 [lsearch -exact [lindex $parentlist [expr {$row-1}]] $id] >= 0} {
5680 set arrowlow 0
5683 set arrow [lindex {none first last both} [expr {$arrowhigh + 2*$arrowlow}]]
5684 for {set y $le} {[incr y -1] > $row} {} {
5685 set x $xp
5686 set xp [lindex $cols [expr {$y - 1 - $row}]]
5687 set ndir [expr {$xp - $x}]
5688 if {$dir != $ndir || $xp < 0} {
5689 lappend coords [xc $y $x] [yc $y]
5691 set dir $ndir
5693 if {!$joinlow} {
5694 if {$xp < 0} {
5695 # join parent line to first child
5696 set ch [lindex $displayorder $row]
5697 set xc [lsearch -exact [lindex $rowidlist $row] $ch]
5698 if {$xc < 0} {
5699 puts "oops: drawlineseg: child $ch not on row $row"
5700 } elseif {$xc != $x} {
5701 if {($arrowhigh && $le == $row + 1) || $dir == 0} {
5702 set d [expr {int(0.5 * $linespc)}]
5703 set x1 [xc $row $x]
5704 if {$xc < $x} {
5705 set x2 [expr {$x1 - $d}]
5706 } else {
5707 set x2 [expr {$x1 + $d}]
5709 set y2 [yc $row]
5710 set y1 [expr {$y2 + $d}]
5711 lappend coords $x1 $y1 $x2 $y2
5712 } elseif {$xc < $x - 1} {
5713 lappend coords [xc $row [expr {$x-1}]] [yc $row]
5714 } elseif {$xc > $x + 1} {
5715 lappend coords [xc $row [expr {$x+1}]] [yc $row]
5717 set x $xc
5719 lappend coords [xc $row $x] [yc $row]
5720 } else {
5721 set xn [xc $row $xp]
5722 set yn [yc $row]
5723 lappend coords $xn $yn
5725 if {!$joinhigh} {
5726 assigncolor $id
5727 set t [$canv create line $coords -width [linewidth $id] \
5728 -fill $colormap($id) -tags lines.$id -arrow $arrow]
5729 $canv lower $t
5730 bindline $t $id
5731 set lines [linsert $lines $i [list $row $le $t]]
5732 } else {
5733 $canv coords $ith $coords
5734 if {$arrow ne $ah} {
5735 $canv itemconf $ith -arrow $arrow
5737 lset lines $i 0 $row
5739 } else {
5740 set xo [lsearch -exact [lindex $rowidlist [expr {$row - 1}]] $id]
5741 set ndir [expr {$xo - $xp}]
5742 set clow [$canv coords $itl]
5743 if {$dir == $ndir} {
5744 set clow [lrange $clow 2 end]
5746 set coords [concat $coords $clow]
5747 if {!$joinhigh} {
5748 lset lines [expr {$i-1}] 1 $le
5749 } else {
5750 # coalesce two pieces
5751 $canv delete $ith
5752 set b [lindex $lines [expr {$i-1}] 0]
5753 set e [lindex $lines $i 1]
5754 set lines [lreplace $lines [expr {$i-1}] $i [list $b $e $itl]]
5756 $canv coords $itl $coords
5757 if {$arrow ne $al} {
5758 $canv itemconf $itl -arrow $arrow
5762 set linesegs($id) $lines
5763 return $le
5766 proc drawparentlinks {id row} {
5767 global rowidlist canv colormap curview parentlist
5768 global idpos linespc
5770 set rowids [lindex $rowidlist $row]
5771 set col [lsearch -exact $rowids $id]
5772 if {$col < 0} return
5773 set olds [lindex $parentlist $row]
5774 set row2 [expr {$row + 1}]
5775 set x [xc $row $col]
5776 set y [yc $row]
5777 set y2 [yc $row2]
5778 set d [expr {int(0.5 * $linespc)}]
5779 set ymid [expr {$y + $d}]
5780 set ids [lindex $rowidlist $row2]
5781 # rmx = right-most X coord used
5782 set rmx 0
5783 foreach p $olds {
5784 set i [lsearch -exact $ids $p]
5785 if {$i < 0} {
5786 puts "oops, parent $p of $id not in list"
5787 continue
5789 set x2 [xc $row2 $i]
5790 if {$x2 > $rmx} {
5791 set rmx $x2
5793 set j [lsearch -exact $rowids $p]
5794 if {$j < 0} {
5795 # drawlineseg will do this one for us
5796 continue
5798 assigncolor $p
5799 # should handle duplicated parents here...
5800 set coords [list $x $y]
5801 if {$i != $col} {
5802 # if attaching to a vertical segment, draw a smaller
5803 # slant for visual distinctness
5804 if {$i == $j} {
5805 if {$i < $col} {
5806 lappend coords [expr {$x2 + $d}] $y $x2 $ymid
5807 } else {
5808 lappend coords [expr {$x2 - $d}] $y $x2 $ymid
5810 } elseif {$i < $col && $i < $j} {
5811 # segment slants towards us already
5812 lappend coords [xc $row $j] $y
5813 } else {
5814 if {$i < $col - 1} {
5815 lappend coords [expr {$x2 + $linespc}] $y
5816 } elseif {$i > $col + 1} {
5817 lappend coords [expr {$x2 - $linespc}] $y
5819 lappend coords $x2 $y2
5821 } else {
5822 lappend coords $x2 $y2
5824 set t [$canv create line $coords -width [linewidth $p] \
5825 -fill $colormap($p) -tags lines.$p]
5826 $canv lower $t
5827 bindline $t $p
5829 if {$rmx > [lindex $idpos($id) 1]} {
5830 lset idpos($id) 1 $rmx
5831 redrawtags $id
5835 proc drawlines {id} {
5836 global canv
5838 $canv itemconf lines.$id -width [linewidth $id]
5841 proc drawcmittext {id row col} {
5842 global linespc canv canv2 canv3 fgcolor curview
5843 global cmitlisted commitinfo rowidlist parentlist
5844 global rowtextx idpos idtags idheads idotherrefs
5845 global linehtag linentag linedtag selectedline
5846 global canvxmax boldids boldnameids fgcolor markedid
5847 global mainheadid nullid nullid2 circleitem circlecolors ctxbut
5849 # listed is 0 for boundary, 1 for normal, 2 for negative, 3 for left, 4 for right
5850 set listed $cmitlisted($curview,$id)
5851 if {$id eq $nullid} {
5852 set ofill red
5853 } elseif {$id eq $nullid2} {
5854 set ofill green
5855 } elseif {$id eq $mainheadid} {
5856 set ofill yellow
5857 } else {
5858 set ofill [lindex $circlecolors $listed]
5860 set x [xc $row $col]
5861 set y [yc $row]
5862 set orad [expr {$linespc / 3}]
5863 if {$listed <= 2} {
5864 set t [$canv create oval [expr {$x - $orad}] [expr {$y - $orad}] \
5865 [expr {$x + $orad - 1}] [expr {$y + $orad - 1}] \
5866 -fill $ofill -outline $fgcolor -width 1 -tags circle]
5867 } elseif {$listed == 3} {
5868 # triangle pointing left for left-side commits
5869 set t [$canv create polygon \
5870 [expr {$x - $orad}] $y \
5871 [expr {$x + $orad - 1}] [expr {$y - $orad}] \
5872 [expr {$x + $orad - 1}] [expr {$y + $orad - 1}] \
5873 -fill $ofill -outline $fgcolor -width 1 -tags circle]
5874 } else {
5875 # triangle pointing right for right-side commits
5876 set t [$canv create polygon \
5877 [expr {$x + $orad - 1}] $y \
5878 [expr {$x - $orad}] [expr {$y - $orad}] \
5879 [expr {$x - $orad}] [expr {$y + $orad - 1}] \
5880 -fill $ofill -outline $fgcolor -width 1 -tags circle]
5882 set circleitem($row) $t
5883 $canv raise $t
5884 $canv bind $t <1> {selcanvline {} %x %y}
5885 set rmx [llength [lindex $rowidlist $row]]
5886 set olds [lindex $parentlist $row]
5887 if {$olds ne {}} {
5888 set nextids [lindex $rowidlist [expr {$row + 1}]]
5889 foreach p $olds {
5890 set i [lsearch -exact $nextids $p]
5891 if {$i > $rmx} {
5892 set rmx $i
5896 set xt [xc $row $rmx]
5897 set rowtextx($row) $xt
5898 set idpos($id) [list $x $xt $y]
5899 if {[info exists idtags($id)] || [info exists idheads($id)]
5900 || [info exists idotherrefs($id)]} {
5901 set xt [drawtags $id $x $xt $y]
5903 set headline [lindex $commitinfo($id) 0]
5904 set name [lindex $commitinfo($id) 1]
5905 set date [lindex $commitinfo($id) 2]
5906 set date [formatdate $date]
5907 set font mainfont
5908 set nfont mainfont
5909 set isbold [ishighlighted $id]
5910 if {$isbold > 0} {
5911 lappend boldids $id
5912 set font mainfontbold
5913 if {$isbold > 1} {
5914 lappend boldnameids $id
5915 set nfont mainfontbold
5918 set linehtag($id) [$canv create text $xt $y -anchor w -fill $fgcolor \
5919 -text $headline -font $font -tags text]
5920 $canv bind $linehtag($id) $ctxbut "rowmenu %X %Y $id"
5921 set linentag($id) [$canv2 create text 3 $y -anchor w -fill $fgcolor \
5922 -text $name -font $nfont -tags text]
5923 set linedtag($id) [$canv3 create text 3 $y -anchor w -fill $fgcolor \
5924 -text $date -font mainfont -tags text]
5925 if {$selectedline == $row} {
5926 make_secsel $id
5928 if {[info exists markedid] && $markedid eq $id} {
5929 make_idmark $id
5931 set xr [expr {$xt + [font measure $font $headline]}]
5932 if {$xr > $canvxmax} {
5933 set canvxmax $xr
5934 setcanvscroll
5938 proc drawcmitrow {row} {
5939 global displayorder rowidlist nrows_drawn
5940 global iddrawn markingmatches
5941 global commitinfo numcommits
5942 global filehighlight fhighlights findpattern nhighlights
5943 global hlview vhighlights
5944 global highlight_related rhighlights
5946 if {$row >= $numcommits} return
5948 set id [lindex $displayorder $row]
5949 if {[info exists hlview] && ![info exists vhighlights($id)]} {
5950 askvhighlight $row $id
5952 if {[info exists filehighlight] && ![info exists fhighlights($id)]} {
5953 askfilehighlight $row $id
5955 if {$findpattern ne {} && ![info exists nhighlights($id)]} {
5956 askfindhighlight $row $id
5958 if {$highlight_related ne [mc "None"] && ![info exists rhighlights($id)]} {
5959 askrelhighlight $row $id
5961 if {![info exists iddrawn($id)]} {
5962 set col [lsearch -exact [lindex $rowidlist $row] $id]
5963 if {$col < 0} {
5964 puts "oops, row $row id $id not in list"
5965 return
5967 if {![info exists commitinfo($id)]} {
5968 getcommit $id
5970 assigncolor $id
5971 drawcmittext $id $row $col
5972 set iddrawn($id) 1
5973 incr nrows_drawn
5975 if {$markingmatches} {
5976 markrowmatches $row $id
5980 proc drawcommits {row {endrow {}}} {
5981 global numcommits iddrawn displayorder curview need_redisplay
5982 global parentlist rowidlist rowfinal uparrowlen downarrowlen nrows_drawn
5984 if {$row < 0} {
5985 set row 0
5987 if {$endrow eq {}} {
5988 set endrow $row
5990 if {$endrow >= $numcommits} {
5991 set endrow [expr {$numcommits - 1}]
5994 set rl1 [expr {$row - $downarrowlen - 3}]
5995 if {$rl1 < 0} {
5996 set rl1 0
5998 set ro1 [expr {$row - 3}]
5999 if {$ro1 < 0} {
6000 set ro1 0
6002 set r2 [expr {$endrow + $uparrowlen + 3}]
6003 if {$r2 > $numcommits} {
6004 set r2 $numcommits
6006 for {set r $rl1} {$r < $r2} {incr r} {
6007 if {[lindex $rowidlist $r] ne {} && [lindex $rowfinal $r]} {
6008 if {$rl1 < $r} {
6009 layoutrows $rl1 $r
6011 set rl1 [expr {$r + 1}]
6014 if {$rl1 < $r} {
6015 layoutrows $rl1 $r
6017 optimize_rows $ro1 0 $r2
6018 if {$need_redisplay || $nrows_drawn > 2000} {
6019 clear_display
6022 # make the lines join to already-drawn rows either side
6023 set r [expr {$row - 1}]
6024 if {$r < 0 || ![info exists iddrawn([lindex $displayorder $r])]} {
6025 set r $row
6027 set er [expr {$endrow + 1}]
6028 if {$er >= $numcommits ||
6029 ![info exists iddrawn([lindex $displayorder $er])]} {
6030 set er $endrow
6032 for {} {$r <= $er} {incr r} {
6033 set id [lindex $displayorder $r]
6034 set wasdrawn [info exists iddrawn($id)]
6035 drawcmitrow $r
6036 if {$r == $er} break
6037 set nextid [lindex $displayorder [expr {$r + 1}]]
6038 if {$wasdrawn && [info exists iddrawn($nextid)]} continue
6039 drawparentlinks $id $r
6041 set rowids [lindex $rowidlist $r]
6042 foreach lid $rowids {
6043 if {$lid eq {}} continue
6044 if {[info exists lineend($lid)] && $lineend($lid) > $r} continue
6045 if {$lid eq $id} {
6046 # see if this is the first child of any of its parents
6047 foreach p [lindex $parentlist $r] {
6048 if {[lsearch -exact $rowids $p] < 0} {
6049 # make this line extend up to the child
6050 set lineend($p) [drawlineseg $p $r $er 0]
6053 } else {
6054 set lineend($lid) [drawlineseg $lid $r $er 1]
6060 proc undolayout {row} {
6061 global uparrowlen mingaplen downarrowlen
6062 global rowidlist rowisopt rowfinal need_redisplay
6064 set r [expr {$row - ($uparrowlen + $mingaplen + $downarrowlen)}]
6065 if {$r < 0} {
6066 set r 0
6068 if {[llength $rowidlist] > $r} {
6069 incr r -1
6070 set rowidlist [lrange $rowidlist 0 $r]
6071 set rowfinal [lrange $rowfinal 0 $r]
6072 set rowisopt [lrange $rowisopt 0 $r]
6073 set need_redisplay 1
6074 run drawvisible
6078 proc drawvisible {} {
6079 global canv linespc curview vrowmod selectedline targetrow targetid
6080 global need_redisplay cscroll numcommits
6082 set fs [$canv yview]
6083 set ymax [lindex [$canv cget -scrollregion] 3]
6084 if {$ymax eq {} || $ymax == 0 || $numcommits == 0} return
6085 set f0 [lindex $fs 0]
6086 set f1 [lindex $fs 1]
6087 set y0 [expr {int($f0 * $ymax)}]
6088 set y1 [expr {int($f1 * $ymax)}]
6090 if {[info exists targetid]} {
6091 if {[commitinview $targetid $curview]} {
6092 set r [rowofcommit $targetid]
6093 if {$r != $targetrow} {
6094 # Fix up the scrollregion and change the scrolling position
6095 # now that our target row has moved.
6096 set diff [expr {($r - $targetrow) * $linespc}]
6097 set targetrow $r
6098 setcanvscroll
6099 set ymax [lindex [$canv cget -scrollregion] 3]
6100 incr y0 $diff
6101 incr y1 $diff
6102 set f0 [expr {$y0 / $ymax}]
6103 set f1 [expr {$y1 / $ymax}]
6104 allcanvs yview moveto $f0
6105 $cscroll set $f0 $f1
6106 set need_redisplay 1
6108 } else {
6109 unset targetid
6113 set row [expr {int(($y0 - 3) / $linespc) - 1}]
6114 set endrow [expr {int(($y1 - 3) / $linespc) + 1}]
6115 if {$endrow >= $vrowmod($curview)} {
6116 update_arcrows $curview
6118 if {$selectedline ne {} &&
6119 $row <= $selectedline && $selectedline <= $endrow} {
6120 set targetrow $selectedline
6121 } elseif {[info exists targetid]} {
6122 set targetrow [expr {int(($row + $endrow) / 2)}]
6124 if {[info exists targetrow]} {
6125 if {$targetrow >= $numcommits} {
6126 set targetrow [expr {$numcommits - 1}]
6128 set targetid [commitonrow $targetrow]
6130 drawcommits $row $endrow
6133 proc clear_display {} {
6134 global iddrawn linesegs need_redisplay nrows_drawn
6135 global vhighlights fhighlights nhighlights rhighlights
6136 global linehtag linentag linedtag boldids boldnameids
6138 allcanvs delete all
6139 catch {unset iddrawn}
6140 catch {unset linesegs}
6141 catch {unset linehtag}
6142 catch {unset linentag}
6143 catch {unset linedtag}
6144 set boldids {}
6145 set boldnameids {}
6146 catch {unset vhighlights}
6147 catch {unset fhighlights}
6148 catch {unset nhighlights}
6149 catch {unset rhighlights}
6150 set need_redisplay 0
6151 set nrows_drawn 0
6154 proc findcrossings {id} {
6155 global rowidlist parentlist numcommits displayorder
6157 set cross {}
6158 set ccross {}
6159 foreach {s e} [rowranges $id] {
6160 if {$e >= $numcommits} {
6161 set e [expr {$numcommits - 1}]
6163 if {$e <= $s} continue
6164 for {set row $e} {[incr row -1] >= $s} {} {
6165 set x [lsearch -exact [lindex $rowidlist $row] $id]
6166 if {$x < 0} break
6167 set olds [lindex $parentlist $row]
6168 set kid [lindex $displayorder $row]
6169 set kidx [lsearch -exact [lindex $rowidlist $row] $kid]
6170 if {$kidx < 0} continue
6171 set nextrow [lindex $rowidlist [expr {$row + 1}]]
6172 foreach p $olds {
6173 set px [lsearch -exact $nextrow $p]
6174 if {$px < 0} continue
6175 if {($kidx < $x && $x < $px) || ($px < $x && $x < $kidx)} {
6176 if {[lsearch -exact $ccross $p] >= 0} continue
6177 if {$x == $px + ($kidx < $px? -1: 1)} {
6178 lappend ccross $p
6179 } elseif {[lsearch -exact $cross $p] < 0} {
6180 lappend cross $p
6186 return [concat $ccross {{}} $cross]
6189 proc assigncolor {id} {
6190 global colormap colors nextcolor
6191 global parents children children curview
6193 if {[info exists colormap($id)]} return
6194 set ncolors [llength $colors]
6195 if {[info exists children($curview,$id)]} {
6196 set kids $children($curview,$id)
6197 } else {
6198 set kids {}
6200 if {[llength $kids] == 1} {
6201 set child [lindex $kids 0]
6202 if {[info exists colormap($child)]
6203 && [llength $parents($curview,$child)] == 1} {
6204 set colormap($id) $colormap($child)
6205 return
6208 set badcolors {}
6209 set origbad {}
6210 foreach x [findcrossings $id] {
6211 if {$x eq {}} {
6212 # delimiter between corner crossings and other crossings
6213 if {[llength $badcolors] >= $ncolors - 1} break
6214 set origbad $badcolors
6216 if {[info exists colormap($x)]
6217 && [lsearch -exact $badcolors $colormap($x)] < 0} {
6218 lappend badcolors $colormap($x)
6221 if {[llength $badcolors] >= $ncolors} {
6222 set badcolors $origbad
6224 set origbad $badcolors
6225 if {[llength $badcolors] < $ncolors - 1} {
6226 foreach child $kids {
6227 if {[info exists colormap($child)]
6228 && [lsearch -exact $badcolors $colormap($child)] < 0} {
6229 lappend badcolors $colormap($child)
6231 foreach p $parents($curview,$child) {
6232 if {[info exists colormap($p)]
6233 && [lsearch -exact $badcolors $colormap($p)] < 0} {
6234 lappend badcolors $colormap($p)
6238 if {[llength $badcolors] >= $ncolors} {
6239 set badcolors $origbad
6242 for {set i 0} {$i <= $ncolors} {incr i} {
6243 set c [lindex $colors $nextcolor]
6244 if {[incr nextcolor] >= $ncolors} {
6245 set nextcolor 0
6247 if {[lsearch -exact $badcolors $c]} break
6249 set colormap($id) $c
6252 proc bindline {t id} {
6253 global canv
6255 $canv bind $t <Enter> "lineenter %x %y $id"
6256 $canv bind $t <Motion> "linemotion %x %y $id"
6257 $canv bind $t <Leave> "lineleave $id"
6258 $canv bind $t <Button-1> "lineclick %x %y $id 1"
6261 proc drawtags {id x xt y1} {
6262 global idtags idheads idotherrefs mainhead
6263 global linespc lthickness
6264 global canv rowtextx curview fgcolor bgcolor ctxbut
6266 set marks {}
6267 set ntags 0
6268 set nheads 0
6269 if {[info exists idtags($id)]} {
6270 set marks $idtags($id)
6271 set ntags [llength $marks]
6273 if {[info exists idheads($id)]} {
6274 set marks [concat $marks $idheads($id)]
6275 set nheads [llength $idheads($id)]
6277 if {[info exists idotherrefs($id)]} {
6278 set marks [concat $marks $idotherrefs($id)]
6280 if {$marks eq {}} {
6281 return $xt
6284 set delta [expr {int(0.5 * ($linespc - $lthickness))}]
6285 set yt [expr {$y1 - 0.5 * $linespc}]
6286 set yb [expr {$yt + $linespc - 1}]
6287 set xvals {}
6288 set wvals {}
6289 set i -1
6290 foreach tag $marks {
6291 incr i
6292 if {$i >= $ntags && $i < $ntags + $nheads && $tag eq $mainhead} {
6293 set wid [font measure mainfontbold $tag]
6294 } else {
6295 set wid [font measure mainfont $tag]
6297 lappend xvals $xt
6298 lappend wvals $wid
6299 set xt [expr {$xt + $delta + $wid + $lthickness + $linespc}]
6301 set t [$canv create line $x $y1 [lindex $xvals end] $y1 \
6302 -width $lthickness -fill black -tags tag.$id]
6303 $canv lower $t
6304 foreach tag $marks x $xvals wid $wvals {
6305 set tag_quoted [string map {% %%} $tag]
6306 set xl [expr {$x + $delta}]
6307 set xr [expr {$x + $delta + $wid + $lthickness}]
6308 set font mainfont
6309 if {[incr ntags -1] >= 0} {
6310 # draw a tag
6311 set t [$canv create polygon $x [expr {$yt + $delta}] $xl $yt \
6312 $xr $yt $xr $yb $xl $yb $x [expr {$yb - $delta}] \
6313 -width 1 -outline black -fill yellow -tags tag.$id]
6314 $canv bind $t <1> [list showtag $tag_quoted 1]
6315 set rowtextx([rowofcommit $id]) [expr {$xr + $linespc}]
6316 } else {
6317 # draw a head or other ref
6318 if {[incr nheads -1] >= 0} {
6319 set col green
6320 if {$tag eq $mainhead} {
6321 set font mainfontbold
6323 } else {
6324 set col "#ddddff"
6326 set xl [expr {$xl - $delta/2}]
6327 $canv create polygon $x $yt $xr $yt $xr $yb $x $yb \
6328 -width 1 -outline black -fill $col -tags tag.$id
6329 if {[regexp {^(remotes/.*/|remotes/)} $tag match remoteprefix]} {
6330 set rwid [font measure mainfont $remoteprefix]
6331 set xi [expr {$x + 1}]
6332 set yti [expr {$yt + 1}]
6333 set xri [expr {$x + $rwid}]
6334 $canv create polygon $xi $yti $xri $yti $xri $yb $xi $yb \
6335 -width 0 -fill "#ffddaa" -tags tag.$id
6338 set t [$canv create text $xl $y1 -anchor w -text $tag -fill $fgcolor \
6339 -font $font -tags [list tag.$id text]]
6340 if {$ntags >= 0} {
6341 $canv bind $t <1> [list showtag $tag_quoted 1]
6342 } elseif {$nheads >= 0} {
6343 $canv bind $t $ctxbut [list headmenu %X %Y $id $tag_quoted]
6346 return $xt
6349 proc xcoord {i level ln} {
6350 global canvx0 xspc1 xspc2
6352 set x [expr {$canvx0 + $i * $xspc1($ln)}]
6353 if {$i > 0 && $i == $level} {
6354 set x [expr {$x + 0.5 * ($xspc2 - $xspc1($ln))}]
6355 } elseif {$i > $level} {
6356 set x [expr {$x + $xspc2 - $xspc1($ln)}]
6358 return $x
6361 proc show_status {msg} {
6362 global canv fgcolor
6364 clear_display
6365 $canv create text 3 3 -anchor nw -text $msg -font mainfont \
6366 -tags text -fill $fgcolor
6369 # Don't change the text pane cursor if it is currently the hand cursor,
6370 # showing that we are over a sha1 ID link.
6371 proc settextcursor {c} {
6372 global ctext curtextcursor
6374 if {[$ctext cget -cursor] == $curtextcursor} {
6375 $ctext config -cursor $c
6377 set curtextcursor $c
6380 proc nowbusy {what {name {}}} {
6381 global isbusy busyname statusw
6383 if {[array names isbusy] eq {}} {
6384 . config -cursor watch
6385 settextcursor watch
6387 set isbusy($what) 1
6388 set busyname($what) $name
6389 if {$name ne {}} {
6390 $statusw conf -text $name
6394 proc notbusy {what} {
6395 global isbusy maincursor textcursor busyname statusw
6397 catch {
6398 unset isbusy($what)
6399 if {$busyname($what) ne {} &&
6400 [$statusw cget -text] eq $busyname($what)} {
6401 $statusw conf -text {}
6404 if {[array names isbusy] eq {}} {
6405 . config -cursor $maincursor
6406 settextcursor $textcursor
6410 proc findmatches {f} {
6411 global findtype findstring
6412 if {$findtype == [mc "Regexp"]} {
6413 set matches [regexp -indices -all -inline $findstring $f]
6414 } else {
6415 set fs $findstring
6416 if {$findtype == [mc "IgnCase"]} {
6417 set f [string tolower $f]
6418 set fs [string tolower $fs]
6420 set matches {}
6421 set i 0
6422 set l [string length $fs]
6423 while {[set j [string first $fs $f $i]] >= 0} {
6424 lappend matches [list $j [expr {$j+$l-1}]]
6425 set i [expr {$j + $l}]
6428 return $matches
6431 proc dofind {{dirn 1} {wrap 1}} {
6432 global findstring findstartline findcurline selectedline numcommits
6433 global gdttype filehighlight fh_serial find_dirn findallowwrap
6435 if {[info exists find_dirn]} {
6436 if {$find_dirn == $dirn} return
6437 stopfinding
6439 focus .
6440 if {$findstring eq {} || $numcommits == 0} return
6441 if {$selectedline eq {}} {
6442 set findstartline [lindex [visiblerows] [expr {$dirn < 0}]]
6443 } else {
6444 set findstartline $selectedline
6446 set findcurline $findstartline
6447 nowbusy finding [mc "Searching"]
6448 if {$gdttype ne [mc "containing:"] && ![info exists filehighlight]} {
6449 after cancel do_file_hl $fh_serial
6450 do_file_hl $fh_serial
6452 set find_dirn $dirn
6453 set findallowwrap $wrap
6454 run findmore
6457 proc stopfinding {} {
6458 global find_dirn findcurline fprogcoord
6460 if {[info exists find_dirn]} {
6461 unset find_dirn
6462 unset findcurline
6463 notbusy finding
6464 set fprogcoord 0
6465 adjustprogress
6467 stopblaming
6470 proc findmore {} {
6471 global commitdata commitinfo numcommits findpattern findloc
6472 global findstartline findcurline findallowwrap
6473 global find_dirn gdttype fhighlights fprogcoord
6474 global curview varcorder vrownum varccommits vrowmod
6476 if {![info exists find_dirn]} {
6477 return 0
6479 set fldtypes [list [mc "Headline"] [mc "Author"] [mc "Date"] [mc "Committer"] [mc "CDate"] [mc "Comments"]]
6480 set l $findcurline
6481 set moretodo 0
6482 if {$find_dirn > 0} {
6483 incr l
6484 if {$l >= $numcommits} {
6485 set l 0
6487 if {$l <= $findstartline} {
6488 set lim [expr {$findstartline + 1}]
6489 } else {
6490 set lim $numcommits
6491 set moretodo $findallowwrap
6493 } else {
6494 if {$l == 0} {
6495 set l $numcommits
6497 incr l -1
6498 if {$l >= $findstartline} {
6499 set lim [expr {$findstartline - 1}]
6500 } else {
6501 set lim -1
6502 set moretodo $findallowwrap
6505 set n [expr {($lim - $l) * $find_dirn}]
6506 if {$n > 500} {
6507 set n 500
6508 set moretodo 1
6510 if {$l + ($find_dirn > 0? $n: 1) > $vrowmod($curview)} {
6511 update_arcrows $curview
6513 set found 0
6514 set domore 1
6515 set ai [bsearch $vrownum($curview) $l]
6516 set a [lindex $varcorder($curview) $ai]
6517 set arow [lindex $vrownum($curview) $ai]
6518 set ids [lindex $varccommits($curview,$a)]
6519 set arowend [expr {$arow + [llength $ids]}]
6520 if {$gdttype eq [mc "containing:"]} {
6521 for {} {$n > 0} {incr n -1; incr l $find_dirn} {
6522 if {$l < $arow || $l >= $arowend} {
6523 incr ai $find_dirn
6524 set a [lindex $varcorder($curview) $ai]
6525 set arow [lindex $vrownum($curview) $ai]
6526 set ids [lindex $varccommits($curview,$a)]
6527 set arowend [expr {$arow + [llength $ids]}]
6529 set id [lindex $ids [expr {$l - $arow}]]
6530 # shouldn't happen unless git log doesn't give all the commits...
6531 if {![info exists commitdata($id)] ||
6532 ![doesmatch $commitdata($id)]} {
6533 continue
6535 if {![info exists commitinfo($id)]} {
6536 getcommit $id
6538 set info $commitinfo($id)
6539 foreach f $info ty $fldtypes {
6540 if {($findloc eq [mc "All fields"] || $findloc eq $ty) &&
6541 [doesmatch $f]} {
6542 set found 1
6543 break
6546 if {$found} break
6548 } else {
6549 for {} {$n > 0} {incr n -1; incr l $find_dirn} {
6550 if {$l < $arow || $l >= $arowend} {
6551 incr ai $find_dirn
6552 set a [lindex $varcorder($curview) $ai]
6553 set arow [lindex $vrownum($curview) $ai]
6554 set ids [lindex $varccommits($curview,$a)]
6555 set arowend [expr {$arow + [llength $ids]}]
6557 set id [lindex $ids [expr {$l - $arow}]]
6558 if {![info exists fhighlights($id)]} {
6559 # this sets fhighlights($id) to -1
6560 askfilehighlight $l $id
6562 if {$fhighlights($id) > 0} {
6563 set found $domore
6564 break
6566 if {$fhighlights($id) < 0} {
6567 if {$domore} {
6568 set domore 0
6569 set findcurline [expr {$l - $find_dirn}]
6574 if {$found || ($domore && !$moretodo)} {
6575 unset findcurline
6576 unset find_dirn
6577 notbusy finding
6578 set fprogcoord 0
6579 adjustprogress
6580 if {$found} {
6581 findselectline $l
6582 } else {
6583 bell
6585 return 0
6587 if {!$domore} {
6588 flushhighlights
6589 } else {
6590 set findcurline [expr {$l - $find_dirn}]
6592 set n [expr {($findcurline - $findstartline) * $find_dirn - 1}]
6593 if {$n < 0} {
6594 incr n $numcommits
6596 set fprogcoord [expr {$n * 1.0 / $numcommits}]
6597 adjustprogress
6598 return $domore
6601 proc findselectline {l} {
6602 global findloc commentend ctext findcurline markingmatches gdttype
6604 set markingmatches [expr {$gdttype eq [mc "containing:"]}]
6605 set findcurline $l
6606 selectline $l 1
6607 if {$markingmatches &&
6608 ($findloc eq [mc "All fields"] || $findloc eq [mc "Comments"])} {
6609 # highlight the matches in the comments
6610 set f [$ctext get 1.0 $commentend]
6611 set matches [findmatches $f]
6612 foreach match $matches {
6613 set start [lindex $match 0]
6614 set end [expr {[lindex $match 1] + 1}]
6615 $ctext tag add found "1.0 + $start c" "1.0 + $end c"
6618 drawvisible
6621 # mark the bits of a headline or author that match a find string
6622 proc markmatches {canv l str tag matches font row} {
6623 global selectedline
6625 set bbox [$canv bbox $tag]
6626 set x0 [lindex $bbox 0]
6627 set y0 [lindex $bbox 1]
6628 set y1 [lindex $bbox 3]
6629 foreach match $matches {
6630 set start [lindex $match 0]
6631 set end [lindex $match 1]
6632 if {$start > $end} continue
6633 set xoff [font measure $font [string range $str 0 [expr {$start-1}]]]
6634 set xlen [font measure $font [string range $str 0 [expr {$end}]]]
6635 set t [$canv create rect [expr {$x0+$xoff}] $y0 \
6636 [expr {$x0+$xlen+2}] $y1 \
6637 -outline {} -tags [list match$l matches] -fill yellow]
6638 $canv lower $t
6639 if {$row == $selectedline} {
6640 $canv raise $t secsel
6645 proc unmarkmatches {} {
6646 global markingmatches
6648 allcanvs delete matches
6649 set markingmatches 0
6650 stopfinding
6653 proc selcanvline {w x y} {
6654 global canv canvy0 ctext linespc
6655 global rowtextx
6656 set ymax [lindex [$canv cget -scrollregion] 3]
6657 if {$ymax == {}} return
6658 set yfrac [lindex [$canv yview] 0]
6659 set y [expr {$y + $yfrac * $ymax}]
6660 set l [expr {int(($y - $canvy0) / $linespc + 0.5)}]
6661 if {$l < 0} {
6662 set l 0
6664 if {$w eq $canv} {
6665 set xmax [lindex [$canv cget -scrollregion] 2]
6666 set xleft [expr {[lindex [$canv xview] 0] * $xmax}]
6667 if {![info exists rowtextx($l)] || $xleft + $x < $rowtextx($l)} return
6669 unmarkmatches
6670 selectline $l 1
6673 proc commit_descriptor {p} {
6674 global commitinfo
6675 if {![info exists commitinfo($p)]} {
6676 getcommit $p
6678 set l "..."
6679 if {[llength $commitinfo($p)] > 1} {
6680 set l [lindex $commitinfo($p) 0]
6682 return "$p ($l)\n"
6685 # append some text to the ctext widget, and make any SHA1 ID
6686 # that we know about be a clickable link.
6687 proc appendwithlinks {text tags} {
6688 global ctext linknum curview
6690 set start [$ctext index "end - 1c"]
6691 $ctext insert end $text $tags
6692 set links [regexp -indices -all -inline {\m[0-9a-f]{6,40}\M} $text]
6693 foreach l $links {
6694 set s [lindex $l 0]
6695 set e [lindex $l 1]
6696 set linkid [string range $text $s $e]
6697 incr e
6698 $ctext tag delete link$linknum
6699 $ctext tag add link$linknum "$start + $s c" "$start + $e c"
6700 setlink $linkid link$linknum
6701 incr linknum
6705 proc setlink {id lk} {
6706 global curview ctext pendinglinks
6708 set known 0
6709 if {[string length $id] < 40} {
6710 set matches [longid $id]
6711 if {[llength $matches] > 0} {
6712 if {[llength $matches] > 1} return
6713 set known 1
6714 set id [lindex $matches 0]
6716 } else {
6717 set known [commitinview $id $curview]
6719 if {$known} {
6720 $ctext tag conf $lk -foreground blue -underline 1
6721 $ctext tag bind $lk <1> [list selbyid $id]
6722 $ctext tag bind $lk <Enter> {linkcursor %W 1}
6723 $ctext tag bind $lk <Leave> {linkcursor %W -1}
6724 } else {
6725 lappend pendinglinks($id) $lk
6726 interestedin $id {makelink %P}
6730 proc appendshortlink {id {pre {}} {post {}}} {
6731 global ctext linknum
6733 $ctext insert end $pre
6734 $ctext tag delete link$linknum
6735 $ctext insert end [string range $id 0 7] link$linknum
6736 $ctext insert end $post
6737 setlink $id link$linknum
6738 incr linknum
6741 proc makelink {id} {
6742 global pendinglinks
6744 if {![info exists pendinglinks($id)]} return
6745 foreach lk $pendinglinks($id) {
6746 setlink $id $lk
6748 unset pendinglinks($id)
6751 proc linkcursor {w inc} {
6752 global linkentercount curtextcursor
6754 if {[incr linkentercount $inc] > 0} {
6755 $w configure -cursor hand2
6756 } else {
6757 $w configure -cursor $curtextcursor
6758 if {$linkentercount < 0} {
6759 set linkentercount 0
6764 proc viewnextline {dir} {
6765 global canv linespc
6767 $canv delete hover
6768 set ymax [lindex [$canv cget -scrollregion] 3]
6769 set wnow [$canv yview]
6770 set wtop [expr {[lindex $wnow 0] * $ymax}]
6771 set newtop [expr {$wtop + $dir * $linespc}]
6772 if {$newtop < 0} {
6773 set newtop 0
6774 } elseif {$newtop > $ymax} {
6775 set newtop $ymax
6777 allcanvs yview moveto [expr {$newtop * 1.0 / $ymax}]
6780 # add a list of tag or branch names at position pos
6781 # returns the number of names inserted
6782 proc appendrefs {pos ids var} {
6783 global ctext linknum curview $var maxrefs
6785 if {[catch {$ctext index $pos}]} {
6786 return 0
6788 $ctext conf -state normal
6789 $ctext delete $pos "$pos lineend"
6790 set tags {}
6791 foreach id $ids {
6792 foreach tag [set $var\($id\)] {
6793 lappend tags [list $tag $id]
6796 if {[llength $tags] > $maxrefs} {
6797 $ctext insert $pos "[mc "many"] ([llength $tags])"
6798 } else {
6799 set tags [lsort -index 0 -decreasing $tags]
6800 set sep {}
6801 foreach ti $tags {
6802 set id [lindex $ti 1]
6803 set lk link$linknum
6804 incr linknum
6805 $ctext tag delete $lk
6806 $ctext insert $pos $sep
6807 $ctext insert $pos [lindex $ti 0] $lk
6808 setlink $id $lk
6809 set sep ", "
6812 $ctext conf -state disabled
6813 return [llength $tags]
6816 # called when we have finished computing the nearby tags
6817 proc dispneartags {delay} {
6818 global selectedline currentid showneartags tagphase
6820 if {$selectedline eq {} || !$showneartags} return
6821 after cancel dispnexttag
6822 if {$delay} {
6823 after 200 dispnexttag
6824 set tagphase -1
6825 } else {
6826 after idle dispnexttag
6827 set tagphase 0
6831 proc dispnexttag {} {
6832 global selectedline currentid showneartags tagphase ctext
6834 if {$selectedline eq {} || !$showneartags} return
6835 switch -- $tagphase {
6837 set dtags [desctags $currentid]
6838 if {$dtags ne {}} {
6839 appendrefs precedes $dtags idtags
6843 set atags [anctags $currentid]
6844 if {$atags ne {}} {
6845 appendrefs follows $atags idtags
6849 set dheads [descheads $currentid]
6850 if {$dheads ne {}} {
6851 if {[appendrefs branch $dheads idheads] > 1
6852 && [$ctext get "branch -3c"] eq "h"} {
6853 # turn "Branch" into "Branches"
6854 $ctext conf -state normal
6855 $ctext insert "branch -2c" "es"
6856 $ctext conf -state disabled
6861 if {[incr tagphase] <= 2} {
6862 after idle dispnexttag
6866 proc make_secsel {id} {
6867 global linehtag linentag linedtag canv canv2 canv3
6869 if {![info exists linehtag($id)]} return
6870 $canv delete secsel
6871 set t [eval $canv create rect [$canv bbox $linehtag($id)] -outline {{}} \
6872 -tags secsel -fill [$canv cget -selectbackground]]
6873 $canv lower $t
6874 $canv2 delete secsel
6875 set t [eval $canv2 create rect [$canv2 bbox $linentag($id)] -outline {{}} \
6876 -tags secsel -fill [$canv2 cget -selectbackground]]
6877 $canv2 lower $t
6878 $canv3 delete secsel
6879 set t [eval $canv3 create rect [$canv3 bbox $linedtag($id)] -outline {{}} \
6880 -tags secsel -fill [$canv3 cget -selectbackground]]
6881 $canv3 lower $t
6884 proc make_idmark {id} {
6885 global linehtag canv fgcolor
6887 if {![info exists linehtag($id)]} return
6888 $canv delete markid
6889 set t [eval $canv create rect [$canv bbox $linehtag($id)] \
6890 -tags markid -outline $fgcolor]
6891 $canv raise $t
6894 proc selectline {l isnew {desired_loc {}}} {
6895 global canv ctext commitinfo selectedline
6896 global canvy0 linespc parents children curview
6897 global currentid sha1entry
6898 global commentend idtags linknum
6899 global mergemax numcommits pending_select
6900 global cmitmode showneartags allcommits
6901 global targetrow targetid lastscrollrows
6902 global autoselect autosellen jump_to_here
6904 catch {unset pending_select}
6905 $canv delete hover
6906 normalline
6907 unsel_reflist
6908 stopfinding
6909 if {$l < 0 || $l >= $numcommits} return
6910 set id [commitonrow $l]
6911 set targetid $id
6912 set targetrow $l
6913 set selectedline $l
6914 set currentid $id
6915 if {$lastscrollrows < $numcommits} {
6916 setcanvscroll
6919 set y [expr {$canvy0 + $l * $linespc}]
6920 set ymax [lindex [$canv cget -scrollregion] 3]
6921 set ytop [expr {$y - $linespc - 1}]
6922 set ybot [expr {$y + $linespc + 1}]
6923 set wnow [$canv yview]
6924 set wtop [expr {[lindex $wnow 0] * $ymax}]
6925 set wbot [expr {[lindex $wnow 1] * $ymax}]
6926 set wh [expr {$wbot - $wtop}]
6927 set newtop $wtop
6928 if {$ytop < $wtop} {
6929 if {$ybot < $wtop} {
6930 set newtop [expr {$y - $wh / 2.0}]
6931 } else {
6932 set newtop $ytop
6933 if {$newtop > $wtop - $linespc} {
6934 set newtop [expr {$wtop - $linespc}]
6937 } elseif {$ybot > $wbot} {
6938 if {$ytop > $wbot} {
6939 set newtop [expr {$y - $wh / 2.0}]
6940 } else {
6941 set newtop [expr {$ybot - $wh}]
6942 if {$newtop < $wtop + $linespc} {
6943 set newtop [expr {$wtop + $linespc}]
6947 if {$newtop != $wtop} {
6948 if {$newtop < 0} {
6949 set newtop 0
6951 allcanvs yview moveto [expr {$newtop * 1.0 / $ymax}]
6952 drawvisible
6955 make_secsel $id
6957 if {$isnew} {
6958 addtohistory [list selbyid $id 0] savecmitpos
6961 $sha1entry delete 0 end
6962 $sha1entry insert 0 $id
6963 if {$autoselect} {
6964 $sha1entry selection range 0 $autosellen
6966 rhighlight_sel $id
6968 $ctext conf -state normal
6969 clear_ctext
6970 set linknum 0
6971 if {![info exists commitinfo($id)]} {
6972 getcommit $id
6974 set info $commitinfo($id)
6975 set date [formatdate [lindex $info 2]]
6976 $ctext insert end "[mc "Author"]: [lindex $info 1] $date\n"
6977 set date [formatdate [lindex $info 4]]
6978 $ctext insert end "[mc "Committer"]: [lindex $info 3] $date\n"
6979 if {[info exists idtags($id)]} {
6980 $ctext insert end [mc "Tags:"]
6981 foreach tag $idtags($id) {
6982 $ctext insert end " $tag"
6984 $ctext insert end "\n"
6987 set headers {}
6988 set olds $parents($curview,$id)
6989 if {[llength $olds] > 1} {
6990 set np 0
6991 foreach p $olds {
6992 if {$np >= $mergemax} {
6993 set tag mmax
6994 } else {
6995 set tag m$np
6997 $ctext insert end "[mc "Parent"]: " $tag
6998 appendwithlinks [commit_descriptor $p] {}
6999 incr np
7001 } else {
7002 foreach p $olds {
7003 append headers "[mc "Parent"]: [commit_descriptor $p]"
7007 foreach c $children($curview,$id) {
7008 append headers "[mc "Child"]: [commit_descriptor $c]"
7011 # make anything that looks like a SHA1 ID be a clickable link
7012 appendwithlinks $headers {}
7013 if {$showneartags} {
7014 if {![info exists allcommits]} {
7015 getallcommits
7017 $ctext insert end "[mc "Branch"]: "
7018 $ctext mark set branch "end -1c"
7019 $ctext mark gravity branch left
7020 $ctext insert end "\n[mc "Follows"]: "
7021 $ctext mark set follows "end -1c"
7022 $ctext mark gravity follows left
7023 $ctext insert end "\n[mc "Precedes"]: "
7024 $ctext mark set precedes "end -1c"
7025 $ctext mark gravity precedes left
7026 $ctext insert end "\n"
7027 dispneartags 1
7029 $ctext insert end "\n"
7030 set comment [lindex $info 5]
7031 if {[string first "\r" $comment] >= 0} {
7032 set comment [string map {"\r" "\n "} $comment]
7034 appendwithlinks $comment {comment}
7036 $ctext tag remove found 1.0 end
7037 $ctext conf -state disabled
7038 set commentend [$ctext index "end - 1c"]
7040 set jump_to_here $desired_loc
7041 init_flist [mc "Comments"]
7042 if {$cmitmode eq "tree"} {
7043 gettree $id
7044 } elseif {[llength $olds] <= 1} {
7045 startdiff $id
7046 } else {
7047 mergediff $id
7051 proc selfirstline {} {
7052 unmarkmatches
7053 selectline 0 1
7056 proc sellastline {} {
7057 global numcommits
7058 unmarkmatches
7059 set l [expr {$numcommits - 1}]
7060 selectline $l 1
7063 proc selnextline {dir} {
7064 global selectedline
7065 focus .
7066 if {$selectedline eq {}} return
7067 set l [expr {$selectedline + $dir}]
7068 unmarkmatches
7069 selectline $l 1
7072 proc selnextpage {dir} {
7073 global canv linespc selectedline numcommits
7075 set lpp [expr {([winfo height $canv] - 2) / $linespc}]
7076 if {$lpp < 1} {
7077 set lpp 1
7079 allcanvs yview scroll [expr {$dir * $lpp}] units
7080 drawvisible
7081 if {$selectedline eq {}} return
7082 set l [expr {$selectedline + $dir * $lpp}]
7083 if {$l < 0} {
7084 set l 0
7085 } elseif {$l >= $numcommits} {
7086 set l [expr $numcommits - 1]
7088 unmarkmatches
7089 selectline $l 1
7092 proc unselectline {} {
7093 global selectedline currentid
7095 set selectedline {}
7096 catch {unset currentid}
7097 allcanvs delete secsel
7098 rhighlight_none
7101 proc reselectline {} {
7102 global selectedline
7104 if {$selectedline ne {}} {
7105 selectline $selectedline 0
7109 proc addtohistory {cmd {saveproc {}}} {
7110 global history historyindex curview
7112 unset_posvars
7113 save_position
7114 set elt [list $curview $cmd $saveproc {}]
7115 if {$historyindex > 0
7116 && [lindex $history [expr {$historyindex - 1}]] == $elt} {
7117 return
7120 if {$historyindex < [llength $history]} {
7121 set history [lreplace $history $historyindex end $elt]
7122 } else {
7123 lappend history $elt
7125 incr historyindex
7126 if {$historyindex > 1} {
7127 .tf.bar.leftbut conf -state normal
7128 } else {
7129 .tf.bar.leftbut conf -state disabled
7131 .tf.bar.rightbut conf -state disabled
7134 # save the scrolling position of the diff display pane
7135 proc save_position {} {
7136 global historyindex history
7138 if {$historyindex < 1} return
7139 set hi [expr {$historyindex - 1}]
7140 set fn [lindex $history $hi 2]
7141 if {$fn ne {}} {
7142 lset history $hi 3 [eval $fn]
7146 proc unset_posvars {} {
7147 global last_posvars
7149 if {[info exists last_posvars]} {
7150 foreach {var val} $last_posvars {
7151 global $var
7152 catch {unset $var}
7154 unset last_posvars
7158 proc godo {elt} {
7159 global curview last_posvars
7161 set view [lindex $elt 0]
7162 set cmd [lindex $elt 1]
7163 set pv [lindex $elt 3]
7164 if {$curview != $view} {
7165 showview $view
7167 unset_posvars
7168 foreach {var val} $pv {
7169 global $var
7170 set $var $val
7172 set last_posvars $pv
7173 eval $cmd
7176 proc goback {} {
7177 global history historyindex
7178 focus .
7180 if {$historyindex > 1} {
7181 save_position
7182 incr historyindex -1
7183 godo [lindex $history [expr {$historyindex - 1}]]
7184 .tf.bar.rightbut conf -state normal
7186 if {$historyindex <= 1} {
7187 .tf.bar.leftbut conf -state disabled
7191 proc goforw {} {
7192 global history historyindex
7193 focus .
7195 if {$historyindex < [llength $history]} {
7196 save_position
7197 set cmd [lindex $history $historyindex]
7198 incr historyindex
7199 godo $cmd
7200 .tf.bar.leftbut conf -state normal
7202 if {$historyindex >= [llength $history]} {
7203 .tf.bar.rightbut conf -state disabled
7207 proc gettree {id} {
7208 global treefilelist treeidlist diffids diffmergeid treepending
7209 global nullid nullid2
7211 set diffids $id
7212 catch {unset diffmergeid}
7213 if {![info exists treefilelist($id)]} {
7214 if {![info exists treepending]} {
7215 if {$id eq $nullid} {
7216 set cmd [list | git ls-files]
7217 } elseif {$id eq $nullid2} {
7218 set cmd [list | git ls-files --stage -t]
7219 } else {
7220 set cmd [list | git ls-tree -r $id]
7222 if {[catch {set gtf [open $cmd r]}]} {
7223 return
7225 set treepending $id
7226 set treefilelist($id) {}
7227 set treeidlist($id) {}
7228 fconfigure $gtf -blocking 0 -encoding binary
7229 filerun $gtf [list gettreeline $gtf $id]
7231 } else {
7232 setfilelist $id
7236 proc gettreeline {gtf id} {
7237 global treefilelist treeidlist treepending cmitmode diffids nullid nullid2
7239 set nl 0
7240 while {[incr nl] <= 1000 && [gets $gtf line] >= 0} {
7241 if {$diffids eq $nullid} {
7242 set fname $line
7243 } else {
7244 set i [string first "\t" $line]
7245 if {$i < 0} continue
7246 set fname [string range $line [expr {$i+1}] end]
7247 set line [string range $line 0 [expr {$i-1}]]
7248 if {$diffids ne $nullid2 && [lindex $line 1] ne "blob"} continue
7249 set sha1 [lindex $line 2]
7250 lappend treeidlist($id) $sha1
7252 if {[string index $fname 0] eq "\""} {
7253 set fname [lindex $fname 0]
7255 set fname [encoding convertfrom $fname]
7256 lappend treefilelist($id) $fname
7258 if {![eof $gtf]} {
7259 return [expr {$nl >= 1000? 2: 1}]
7261 close $gtf
7262 unset treepending
7263 if {$cmitmode ne "tree"} {
7264 if {![info exists diffmergeid]} {
7265 gettreediffs $diffids
7267 } elseif {$id ne $diffids} {
7268 gettree $diffids
7269 } else {
7270 setfilelist $id
7272 return 0
7275 proc showfile {f} {
7276 global treefilelist treeidlist diffids nullid nullid2
7277 global ctext_file_names ctext_file_lines
7278 global ctext commentend
7280 set i [lsearch -exact $treefilelist($diffids) $f]
7281 if {$i < 0} {
7282 puts "oops, $f not in list for id $diffids"
7283 return
7285 if {$diffids eq $nullid} {
7286 if {[catch {set bf [open $f r]} err]} {
7287 puts "oops, can't read $f: $err"
7288 return
7290 } else {
7291 set blob [lindex $treeidlist($diffids) $i]
7292 if {[catch {set bf [open [concat | git cat-file blob $blob] r]} err]} {
7293 puts "oops, error reading blob $blob: $err"
7294 return
7297 fconfigure $bf -blocking 0 -encoding [get_path_encoding $f]
7298 filerun $bf [list getblobline $bf $diffids]
7299 $ctext config -state normal
7300 clear_ctext $commentend
7301 lappend ctext_file_names $f
7302 lappend ctext_file_lines [lindex [split $commentend "."] 0]
7303 $ctext insert end "\n"
7304 $ctext insert end "$f\n" filesep
7305 $ctext config -state disabled
7306 $ctext yview $commentend
7307 settabs 0
7310 proc getblobline {bf id} {
7311 global diffids cmitmode ctext
7313 if {$id ne $diffids || $cmitmode ne "tree"} {
7314 catch {close $bf}
7315 return 0
7317 $ctext config -state normal
7318 set nl 0
7319 while {[incr nl] <= 1000 && [gets $bf line] >= 0} {
7320 $ctext insert end "$line\n"
7322 if {[eof $bf]} {
7323 global jump_to_here ctext_file_names commentend
7325 # delete last newline
7326 $ctext delete "end - 2c" "end - 1c"
7327 close $bf
7328 if {$jump_to_here ne {} &&
7329 [lindex $jump_to_here 0] eq [lindex $ctext_file_names 0]} {
7330 set lnum [expr {[lindex $jump_to_here 1] +
7331 [lindex [split $commentend .] 0]}]
7332 mark_ctext_line $lnum
7334 $ctext config -state disabled
7335 return 0
7337 $ctext config -state disabled
7338 return [expr {$nl >= 1000? 2: 1}]
7341 proc mark_ctext_line {lnum} {
7342 global ctext markbgcolor
7344 $ctext tag delete omark
7345 $ctext tag add omark $lnum.0 "$lnum.0 + 1 line"
7346 $ctext tag conf omark -background $markbgcolor
7347 $ctext see $lnum.0
7350 proc mergediff {id} {
7351 global diffmergeid
7352 global diffids treediffs
7353 global parents curview
7355 set diffmergeid $id
7356 set diffids $id
7357 set treediffs($id) {}
7358 set np [llength $parents($curview,$id)]
7359 settabs $np
7360 getblobdiffs $id
7363 proc startdiff {ids} {
7364 global treediffs diffids treepending diffmergeid nullid nullid2
7366 settabs 1
7367 set diffids $ids
7368 catch {unset diffmergeid}
7369 if {![info exists treediffs($ids)] ||
7370 [lsearch -exact $ids $nullid] >= 0 ||
7371 [lsearch -exact $ids $nullid2] >= 0} {
7372 if {![info exists treepending]} {
7373 gettreediffs $ids
7375 } else {
7376 addtocflist $ids
7380 proc path_filter {filter name} {
7381 foreach p $filter {
7382 set l [string length $p]
7383 if {[string index $p end] eq "/"} {
7384 if {[string compare -length $l $p $name] == 0} {
7385 return 1
7387 } else {
7388 if {[string compare -length $l $p $name] == 0 &&
7389 ([string length $name] == $l ||
7390 [string index $name $l] eq "/")} {
7391 return 1
7395 return 0
7398 proc addtocflist {ids} {
7399 global treediffs
7401 add_flist $treediffs($ids)
7402 getblobdiffs $ids
7405 proc diffcmd {ids flags} {
7406 global nullid nullid2
7408 set i [lsearch -exact $ids $nullid]
7409 set j [lsearch -exact $ids $nullid2]
7410 if {$i >= 0} {
7411 if {[llength $ids] > 1 && $j < 0} {
7412 # comparing working directory with some specific revision
7413 set cmd [concat | git diff-index $flags]
7414 if {$i == 0} {
7415 lappend cmd -R [lindex $ids 1]
7416 } else {
7417 lappend cmd [lindex $ids 0]
7419 } else {
7420 # comparing working directory with index
7421 set cmd [concat | git diff-files $flags]
7422 if {$j == 1} {
7423 lappend cmd -R
7426 } elseif {$j >= 0} {
7427 set cmd [concat | git diff-index --cached $flags]
7428 if {[llength $ids] > 1} {
7429 # comparing index with specific revision
7430 if {$j == 0} {
7431 lappend cmd -R [lindex $ids 1]
7432 } else {
7433 lappend cmd [lindex $ids 0]
7435 } else {
7436 # comparing index with HEAD
7437 lappend cmd HEAD
7439 } else {
7440 set cmd [concat | git diff-tree -r $flags $ids]
7442 return $cmd
7445 proc gettreediffs {ids} {
7446 global treediff treepending
7448 if {[catch {set gdtf [open [diffcmd $ids {--no-commit-id}] r]}]} return
7450 set treepending $ids
7451 set treediff {}
7452 fconfigure $gdtf -blocking 0 -encoding binary
7453 filerun $gdtf [list gettreediffline $gdtf $ids]
7456 proc gettreediffline {gdtf ids} {
7457 global treediff treediffs treepending diffids diffmergeid
7458 global cmitmode vfilelimit curview limitdiffs perfile_attrs
7460 set nr 0
7461 set sublist {}
7462 set max 1000
7463 if {$perfile_attrs} {
7464 # cache_gitattr is slow, and even slower on win32 where we
7465 # have to invoke it for only about 30 paths at a time
7466 set max 500
7467 if {[tk windowingsystem] == "win32"} {
7468 set max 120
7471 while {[incr nr] <= $max && [gets $gdtf line] >= 0} {
7472 set i [string first "\t" $line]
7473 if {$i >= 0} {
7474 set file [string range $line [expr {$i+1}] end]
7475 if {[string index $file 0] eq "\""} {
7476 set file [lindex $file 0]
7478 set file [encoding convertfrom $file]
7479 if {$file ne [lindex $treediff end]} {
7480 lappend treediff $file
7481 lappend sublist $file
7485 if {$perfile_attrs} {
7486 cache_gitattr encoding $sublist
7488 if {![eof $gdtf]} {
7489 return [expr {$nr >= $max? 2: 1}]
7491 close $gdtf
7492 if {$limitdiffs && $vfilelimit($curview) ne {}} {
7493 set flist {}
7494 foreach f $treediff {
7495 if {[path_filter $vfilelimit($curview) $f]} {
7496 lappend flist $f
7499 set treediffs($ids) $flist
7500 } else {
7501 set treediffs($ids) $treediff
7503 unset treepending
7504 if {$cmitmode eq "tree" && [llength $diffids] == 1} {
7505 gettree $diffids
7506 } elseif {$ids != $diffids} {
7507 if {![info exists diffmergeid]} {
7508 gettreediffs $diffids
7510 } else {
7511 addtocflist $ids
7513 return 0
7516 # empty string or positive integer
7517 proc diffcontextvalidate {v} {
7518 return [regexp {^(|[1-9][0-9]*)$} $v]
7521 proc diffcontextchange {n1 n2 op} {
7522 global diffcontextstring diffcontext
7524 if {[string is integer -strict $diffcontextstring]} {
7525 if {$diffcontextstring >= 0} {
7526 set diffcontext $diffcontextstring
7527 reselectline
7532 proc changeignorespace {} {
7533 reselectline
7536 proc changeworddiff {name ix op} {
7537 reselectline
7540 proc getblobdiffs {ids} {
7541 global blobdifffd diffids env
7542 global diffinhdr treediffs
7543 global diffcontext
7544 global ignorespace
7545 global worddiff
7546 global limitdiffs vfilelimit curview
7547 global diffencoding targetline diffnparents
7548 global git_version currdiffsubmod
7550 set textconv {}
7551 if {[package vcompare $git_version "1.6.1"] >= 0} {
7552 set textconv "--textconv"
7554 set submodule {}
7555 if {[package vcompare $git_version "1.6.6"] >= 0} {
7556 set submodule "--submodule"
7558 set cmd [diffcmd $ids "-p $textconv $submodule -C --cc --no-commit-id -U$diffcontext"]
7559 if {$ignorespace} {
7560 append cmd " -w"
7562 if {$worddiff ne [mc "Line diff"]} {
7563 append cmd " --word-diff=porcelain"
7565 if {$limitdiffs && $vfilelimit($curview) ne {}} {
7566 set cmd [concat $cmd -- $vfilelimit($curview)]
7568 if {[catch {set bdf [open $cmd r]} err]} {
7569 error_popup [mc "Error getting diffs: %s" $err]
7570 return
7572 set targetline {}
7573 set diffnparents 0
7574 set diffinhdr 0
7575 set diffencoding [get_path_encoding {}]
7576 fconfigure $bdf -blocking 0 -encoding binary -eofchar {}
7577 set blobdifffd($ids) $bdf
7578 set currdiffsubmod ""
7579 filerun $bdf [list getblobdiffline $bdf $diffids]
7582 proc savecmitpos {} {
7583 global ctext cmitmode
7585 if {$cmitmode eq "tree"} {
7586 return {}
7588 return [list target_scrollpos [$ctext index @0,0]]
7591 proc savectextpos {} {
7592 global ctext
7594 return [list target_scrollpos [$ctext index @0,0]]
7597 proc maybe_scroll_ctext {ateof} {
7598 global ctext target_scrollpos
7600 if {![info exists target_scrollpos]} return
7601 if {!$ateof} {
7602 set nlines [expr {[winfo height $ctext]
7603 / [font metrics textfont -linespace]}]
7604 if {[$ctext compare "$target_scrollpos + $nlines lines" <= end]} return
7606 $ctext yview $target_scrollpos
7607 unset target_scrollpos
7610 proc setinlist {var i val} {
7611 global $var
7613 while {[llength [set $var]] < $i} {
7614 lappend $var {}
7616 if {[llength [set $var]] == $i} {
7617 lappend $var $val
7618 } else {
7619 lset $var $i $val
7623 proc makediffhdr {fname ids} {
7624 global ctext curdiffstart treediffs diffencoding
7625 global ctext_file_names jump_to_here targetline diffline
7627 set fname [encoding convertfrom $fname]
7628 set diffencoding [get_path_encoding $fname]
7629 set i [lsearch -exact $treediffs($ids) $fname]
7630 if {$i >= 0} {
7631 setinlist difffilestart $i $curdiffstart
7633 lset ctext_file_names end $fname
7634 set l [expr {(78 - [string length $fname]) / 2}]
7635 set pad [string range "----------------------------------------" 1 $l]
7636 $ctext insert $curdiffstart "$pad $fname $pad" filesep
7637 set targetline {}
7638 if {$jump_to_here ne {} && [lindex $jump_to_here 0] eq $fname} {
7639 set targetline [lindex $jump_to_here 1]
7641 set diffline 0
7644 proc getblobdiffline {bdf ids} {
7645 global diffids blobdifffd ctext curdiffstart
7646 global diffnexthead diffnextnote difffilestart
7647 global ctext_file_names ctext_file_lines
7648 global diffinhdr treediffs mergemax diffnparents
7649 global diffencoding jump_to_here targetline diffline currdiffsubmod
7650 global worddiff
7652 set nr 0
7653 $ctext conf -state normal
7654 while {[incr nr] <= 1000 && [gets $bdf line] >= 0} {
7655 if {$ids != $diffids || $bdf != $blobdifffd($ids)} {
7656 catch {close $bdf}
7657 return 0
7659 if {![string compare -length 5 "diff " $line]} {
7660 if {![regexp {^diff (--cc|--git) } $line m type]} {
7661 set line [encoding convertfrom $line]
7662 $ctext insert end "$line\n" hunksep
7663 continue
7665 # start of a new file
7666 set diffinhdr 1
7667 $ctext insert end "\n"
7668 set curdiffstart [$ctext index "end - 1c"]
7669 lappend ctext_file_names ""
7670 lappend ctext_file_lines [lindex [split $curdiffstart "."] 0]
7671 $ctext insert end "\n" filesep
7673 if {$type eq "--cc"} {
7674 # start of a new file in a merge diff
7675 set fname [string range $line 10 end]
7676 if {[lsearch -exact $treediffs($ids) $fname] < 0} {
7677 lappend treediffs($ids) $fname
7678 add_flist [list $fname]
7681 } else {
7682 set line [string range $line 11 end]
7683 # If the name hasn't changed the length will be odd,
7684 # the middle char will be a space, and the two bits either
7685 # side will be a/name and b/name, or "a/name" and "b/name".
7686 # If the name has changed we'll get "rename from" and
7687 # "rename to" or "copy from" and "copy to" lines following
7688 # this, and we'll use them to get the filenames.
7689 # This complexity is necessary because spaces in the
7690 # filename(s) don't get escaped.
7691 set l [string length $line]
7692 set i [expr {$l / 2}]
7693 if {!(($l & 1) && [string index $line $i] eq " " &&
7694 [string range $line 2 [expr {$i - 1}]] eq \
7695 [string range $line [expr {$i + 3}] end])} {
7696 continue
7698 # unescape if quoted and chop off the a/ from the front
7699 if {[string index $line 0] eq "\""} {
7700 set fname [string range [lindex $line 0] 2 end]
7701 } else {
7702 set fname [string range $line 2 [expr {$i - 1}]]
7705 makediffhdr $fname $ids
7707 } elseif {![string compare -length 16 "* Unmerged path " $line]} {
7708 set fname [encoding convertfrom [string range $line 16 end]]
7709 $ctext insert end "\n"
7710 set curdiffstart [$ctext index "end - 1c"]
7711 lappend ctext_file_names $fname
7712 lappend ctext_file_lines [lindex [split $curdiffstart "."] 0]
7713 $ctext insert end "$line\n" filesep
7714 set i [lsearch -exact $treediffs($ids) $fname]
7715 if {$i >= 0} {
7716 setinlist difffilestart $i $curdiffstart
7719 } elseif {![string compare -length 2 "@@" $line]} {
7720 regexp {^@@+} $line ats
7721 set line [encoding convertfrom $diffencoding $line]
7722 $ctext insert end "$line\n" hunksep
7723 if {[regexp { \+(\d+),\d+ @@} $line m nl]} {
7724 set diffline $nl
7726 set diffnparents [expr {[string length $ats] - 1}]
7727 set diffinhdr 0
7729 } elseif {![string compare -length 10 "Submodule " $line]} {
7730 # start of a new submodule
7731 if {[regexp -indices "\[0-9a-f\]+\\.\\." $line nameend]} {
7732 set fname [string range $line 10 [expr [lindex $nameend 0] - 2]]
7733 } else {
7734 set fname [string range $line 10 [expr [string first "contains " $line] - 2]]
7736 if {$currdiffsubmod != $fname} {
7737 $ctext insert end "\n"; # Add newline after commit message
7739 set curdiffstart [$ctext index "end - 1c"]
7740 lappend ctext_file_names ""
7741 if {$currdiffsubmod != $fname} {
7742 lappend ctext_file_lines $fname
7743 makediffhdr $fname $ids
7744 set currdiffsubmod $fname
7745 $ctext insert end "\n$line\n" filesep
7746 } else {
7747 $ctext insert end "$line\n" filesep
7749 } elseif {![string compare -length 3 " >" $line]} {
7750 set $currdiffsubmod ""
7751 set line [encoding convertfrom $diffencoding $line]
7752 $ctext insert end "$line\n" dresult
7753 } elseif {![string compare -length 3 " <" $line]} {
7754 set $currdiffsubmod ""
7755 set line [encoding convertfrom $diffencoding $line]
7756 $ctext insert end "$line\n" d0
7757 } elseif {$diffinhdr} {
7758 if {![string compare -length 12 "rename from " $line]} {
7759 set fname [string range $line [expr 6 + [string first " from " $line] ] end]
7760 if {[string index $fname 0] eq "\""} {
7761 set fname [lindex $fname 0]
7763 set fname [encoding convertfrom $fname]
7764 set i [lsearch -exact $treediffs($ids) $fname]
7765 if {$i >= 0} {
7766 setinlist difffilestart $i $curdiffstart
7768 } elseif {![string compare -length 10 $line "rename to "] ||
7769 ![string compare -length 8 $line "copy to "]} {
7770 set fname [string range $line [expr 4 + [string first " to " $line] ] end]
7771 if {[string index $fname 0] eq "\""} {
7772 set fname [lindex $fname 0]
7774 makediffhdr $fname $ids
7775 } elseif {[string compare -length 3 $line "---"] == 0} {
7776 # do nothing
7777 continue
7778 } elseif {[string compare -length 3 $line "+++"] == 0} {
7779 set diffinhdr 0
7780 continue
7782 $ctext insert end "$line\n" filesep
7784 } else {
7785 set line [string map {\x1A ^Z} \
7786 [encoding convertfrom $diffencoding $line]]
7787 # parse the prefix - one ' ', '-' or '+' for each parent
7788 set prefix [string range $line 0 [expr {$diffnparents - 1}]]
7789 set tag [expr {$diffnparents > 1? "m": "d"}]
7790 set dowords [expr {$worddiff ne [mc "Line diff"] && $diffnparents == 1}]
7791 set words_pre_markup ""
7792 set words_post_markup ""
7793 if {[string trim $prefix " -+"] eq {}} {
7794 # prefix only has " ", "-" and "+" in it: normal diff line
7795 set num [string first "-" $prefix]
7796 if {$dowords} {
7797 set line [string range $line 1 end]
7799 if {$num >= 0} {
7800 # removed line, first parent with line is $num
7801 if {$num >= $mergemax} {
7802 set num "max"
7804 if {$dowords && $worddiff eq [mc "Markup words"]} {
7805 $ctext insert end "\[-$line-\]" $tag$num
7806 } else {
7807 $ctext insert end "$line" $tag$num
7809 if {!$dowords} {
7810 $ctext insert end "\n" $tag$num
7812 } else {
7813 set tags {}
7814 if {[string first "+" $prefix] >= 0} {
7815 # added line
7816 lappend tags ${tag}result
7817 if {$diffnparents > 1} {
7818 set num [string first " " $prefix]
7819 if {$num >= 0} {
7820 if {$num >= $mergemax} {
7821 set num "max"
7823 lappend tags m$num
7826 set words_pre_markup "{+"
7827 set words_post_markup "+}"
7829 if {$targetline ne {}} {
7830 if {$diffline == $targetline} {
7831 set seehere [$ctext index "end - 1 chars"]
7832 set targetline {}
7833 } else {
7834 incr diffline
7837 if {$dowords && $worddiff eq [mc "Markup words"]} {
7838 $ctext insert end "$words_pre_markup$line$words_post_markup" $tags
7839 } else {
7840 $ctext insert end "$line" $tags
7842 if {!$dowords} {
7843 $ctext insert end "\n" $tags
7846 } elseif {$dowords && $prefix eq "~"} {
7847 $ctext insert end "\n" {}
7848 } else {
7849 # "\ No newline at end of file",
7850 # or something else we don't recognize
7851 $ctext insert end "$line\n" hunksep
7855 if {[info exists seehere]} {
7856 mark_ctext_line [lindex [split $seehere .] 0]
7858 maybe_scroll_ctext [eof $bdf]
7859 $ctext conf -state disabled
7860 if {[eof $bdf]} {
7861 catch {close $bdf}
7862 return 0
7864 return [expr {$nr >= 1000? 2: 1}]
7867 proc changediffdisp {} {
7868 global ctext diffelide
7870 $ctext tag conf d0 -elide [lindex $diffelide 0]
7871 $ctext tag conf dresult -elide [lindex $diffelide 1]
7874 proc highlightfile {loc cline} {
7875 global ctext cflist cflist_top
7877 $ctext yview $loc
7878 $cflist tag remove highlight $cflist_top.0 "$cflist_top.0 lineend"
7879 $cflist tag add highlight $cline.0 "$cline.0 lineend"
7880 $cflist see $cline.0
7881 set cflist_top $cline
7884 proc prevfile {} {
7885 global difffilestart ctext cmitmode
7887 if {$cmitmode eq "tree"} return
7888 set prev 0.0
7889 set prevline 1
7890 set here [$ctext index @0,0]
7891 foreach loc $difffilestart {
7892 if {[$ctext compare $loc >= $here]} {
7893 highlightfile $prev $prevline
7894 return
7896 set prev $loc
7897 incr prevline
7899 highlightfile $prev $prevline
7902 proc nextfile {} {
7903 global difffilestart ctext cmitmode
7905 if {$cmitmode eq "tree"} return
7906 set here [$ctext index @0,0]
7907 set line 1
7908 foreach loc $difffilestart {
7909 incr line
7910 if {[$ctext compare $loc > $here]} {
7911 highlightfile $loc $line
7912 return
7917 proc clear_ctext {{first 1.0}} {
7918 global ctext smarktop smarkbot
7919 global ctext_file_names ctext_file_lines
7920 global pendinglinks
7922 set l [lindex [split $first .] 0]
7923 if {![info exists smarktop] || [$ctext compare $first < $smarktop.0]} {
7924 set smarktop $l
7926 if {![info exists smarkbot] || [$ctext compare $first < $smarkbot.0]} {
7927 set smarkbot $l
7929 $ctext delete $first end
7930 if {$first eq "1.0"} {
7931 catch {unset pendinglinks}
7933 set ctext_file_names {}
7934 set ctext_file_lines {}
7937 proc settabs {{firstab {}}} {
7938 global firsttabstop tabstop ctext have_tk85
7940 if {$firstab ne {} && $have_tk85} {
7941 set firsttabstop $firstab
7943 set w [font measure textfont "0"]
7944 if {$firsttabstop != 0} {
7945 $ctext conf -tabs [list [expr {($firsttabstop + $tabstop) * $w}] \
7946 [expr {($firsttabstop + 2 * $tabstop) * $w}]]
7947 } elseif {$have_tk85 || $tabstop != 8} {
7948 $ctext conf -tabs [expr {$tabstop * $w}]
7949 } else {
7950 $ctext conf -tabs {}
7954 proc incrsearch {name ix op} {
7955 global ctext searchstring searchdirn
7957 $ctext tag remove found 1.0 end
7958 if {[catch {$ctext index anchor}]} {
7959 # no anchor set, use start of selection, or of visible area
7960 set sel [$ctext tag ranges sel]
7961 if {$sel ne {}} {
7962 $ctext mark set anchor [lindex $sel 0]
7963 } elseif {$searchdirn eq "-forwards"} {
7964 $ctext mark set anchor @0,0
7965 } else {
7966 $ctext mark set anchor @0,[winfo height $ctext]
7969 if {$searchstring ne {}} {
7970 set here [$ctext search $searchdirn -- $searchstring anchor]
7971 if {$here ne {}} {
7972 $ctext see $here
7974 searchmarkvisible 1
7978 proc dosearch {} {
7979 global sstring ctext searchstring searchdirn
7981 focus $sstring
7982 $sstring icursor end
7983 set searchdirn -forwards
7984 if {$searchstring ne {}} {
7985 set sel [$ctext tag ranges sel]
7986 if {$sel ne {}} {
7987 set start "[lindex $sel 0] + 1c"
7988 } elseif {[catch {set start [$ctext index anchor]}]} {
7989 set start "@0,0"
7991 set match [$ctext search -count mlen -- $searchstring $start]
7992 $ctext tag remove sel 1.0 end
7993 if {$match eq {}} {
7994 bell
7995 return
7997 $ctext see $match
7998 set mend "$match + $mlen c"
7999 $ctext tag add sel $match $mend
8000 $ctext mark unset anchor
8004 proc dosearchback {} {
8005 global sstring ctext searchstring searchdirn
8007 focus $sstring
8008 $sstring icursor end
8009 set searchdirn -backwards
8010 if {$searchstring ne {}} {
8011 set sel [$ctext tag ranges sel]
8012 if {$sel ne {}} {
8013 set start [lindex $sel 0]
8014 } elseif {[catch {set start [$ctext index anchor]}]} {
8015 set start @0,[winfo height $ctext]
8017 set match [$ctext search -backwards -count ml -- $searchstring $start]
8018 $ctext tag remove sel 1.0 end
8019 if {$match eq {}} {
8020 bell
8021 return
8023 $ctext see $match
8024 set mend "$match + $ml c"
8025 $ctext tag add sel $match $mend
8026 $ctext mark unset anchor
8030 proc searchmark {first last} {
8031 global ctext searchstring
8033 set mend $first.0
8034 while {1} {
8035 set match [$ctext search -count mlen -- $searchstring $mend $last.end]
8036 if {$match eq {}} break
8037 set mend "$match + $mlen c"
8038 $ctext tag add found $match $mend
8042 proc searchmarkvisible {doall} {
8043 global ctext smarktop smarkbot
8045 set topline [lindex [split [$ctext index @0,0] .] 0]
8046 set botline [lindex [split [$ctext index @0,[winfo height $ctext]] .] 0]
8047 if {$doall || $botline < $smarktop || $topline > $smarkbot} {
8048 # no overlap with previous
8049 searchmark $topline $botline
8050 set smarktop $topline
8051 set smarkbot $botline
8052 } else {
8053 if {$topline < $smarktop} {
8054 searchmark $topline [expr {$smarktop-1}]
8055 set smarktop $topline
8057 if {$botline > $smarkbot} {
8058 searchmark [expr {$smarkbot+1}] $botline
8059 set smarkbot $botline
8064 proc scrolltext {f0 f1} {
8065 global searchstring
8067 .bleft.bottom.sb set $f0 $f1
8068 if {$searchstring ne {}} {
8069 searchmarkvisible 0
8073 proc setcoords {} {
8074 global linespc charspc canvx0 canvy0
8075 global xspc1 xspc2 lthickness
8077 set linespc [font metrics mainfont -linespace]
8078 set charspc [font measure mainfont "m"]
8079 set canvy0 [expr {int(3 + 0.5 * $linespc)}]
8080 set canvx0 [expr {int(3 + 0.5 * $linespc)}]
8081 set lthickness [expr {int($linespc / 9) + 1}]
8082 set xspc1(0) $linespc
8083 set xspc2 $linespc
8086 proc redisplay {} {
8087 global canv
8088 global selectedline
8090 set ymax [lindex [$canv cget -scrollregion] 3]
8091 if {$ymax eq {} || $ymax == 0} return
8092 set span [$canv yview]
8093 clear_display
8094 setcanvscroll
8095 allcanvs yview moveto [lindex $span 0]
8096 drawvisible
8097 if {$selectedline ne {}} {
8098 selectline $selectedline 0
8099 allcanvs yview moveto [lindex $span 0]
8103 proc parsefont {f n} {
8104 global fontattr
8106 set fontattr($f,family) [lindex $n 0]
8107 set s [lindex $n 1]
8108 if {$s eq {} || $s == 0} {
8109 set s 10
8110 } elseif {$s < 0} {
8111 set s [expr {int(-$s / [winfo fpixels . 1p] + 0.5)}]
8113 set fontattr($f,size) $s
8114 set fontattr($f,weight) normal
8115 set fontattr($f,slant) roman
8116 foreach style [lrange $n 2 end] {
8117 switch -- $style {
8118 "normal" -
8119 "bold" {set fontattr($f,weight) $style}
8120 "roman" -
8121 "italic" {set fontattr($f,slant) $style}
8126 proc fontflags {f {isbold 0}} {
8127 global fontattr
8129 return [list -family $fontattr($f,family) -size $fontattr($f,size) \
8130 -weight [expr {$isbold? "bold": $fontattr($f,weight)}] \
8131 -slant $fontattr($f,slant)]
8134 proc fontname {f} {
8135 global fontattr
8137 set n [list $fontattr($f,family) $fontattr($f,size)]
8138 if {$fontattr($f,weight) eq "bold"} {
8139 lappend n "bold"
8141 if {$fontattr($f,slant) eq "italic"} {
8142 lappend n "italic"
8144 return $n
8147 proc incrfont {inc} {
8148 global mainfont textfont ctext canv cflist showrefstop
8149 global stopped entries fontattr
8151 unmarkmatches
8152 set s $fontattr(mainfont,size)
8153 incr s $inc
8154 if {$s < 1} {
8155 set s 1
8157 set fontattr(mainfont,size) $s
8158 font config mainfont -size $s
8159 font config mainfontbold -size $s
8160 set mainfont [fontname mainfont]
8161 set s $fontattr(textfont,size)
8162 incr s $inc
8163 if {$s < 1} {
8164 set s 1
8166 set fontattr(textfont,size) $s
8167 font config textfont -size $s
8168 font config textfontbold -size $s
8169 set textfont [fontname textfont]
8170 setcoords
8171 settabs
8172 redisplay
8175 proc clearsha1 {} {
8176 global sha1entry sha1string
8177 if {[string length $sha1string] == 40} {
8178 $sha1entry delete 0 end
8182 proc sha1change {n1 n2 op} {
8183 global sha1string currentid sha1but
8184 if {$sha1string == {}
8185 || ([info exists currentid] && $sha1string == $currentid)} {
8186 set state disabled
8187 } else {
8188 set state normal
8190 if {[$sha1but cget -state] == $state} return
8191 if {$state == "normal"} {
8192 $sha1but conf -state normal -relief raised -text "[mc "Goto:"] "
8193 } else {
8194 $sha1but conf -state disabled -relief flat -text "[mc "SHA1 ID:"] "
8198 proc gotocommit {} {
8199 global sha1string tagids headids curview varcid
8201 if {$sha1string == {}
8202 || ([info exists currentid] && $sha1string == $currentid)} return
8203 if {[info exists tagids($sha1string)]} {
8204 set id $tagids($sha1string)
8205 } elseif {[info exists headids($sha1string)]} {
8206 set id $headids($sha1string)
8207 } else {
8208 set id [string tolower $sha1string]
8209 if {[regexp {^[0-9a-f]{4,39}$} $id]} {
8210 set matches [longid $id]
8211 if {$matches ne {}} {
8212 if {[llength $matches] > 1} {
8213 error_popup [mc "Short SHA1 id %s is ambiguous" $id]
8214 return
8216 set id [lindex $matches 0]
8218 } else {
8219 if {[catch {set id [exec git rev-parse --verify $sha1string]}]} {
8220 error_popup [mc "Revision %s is not known" $sha1string]
8221 return
8225 if {[commitinview $id $curview]} {
8226 selectline [rowofcommit $id] 1
8227 return
8229 if {[regexp {^[0-9a-fA-F]{4,}$} $sha1string]} {
8230 set msg [mc "SHA1 id %s is not known" $sha1string]
8231 } else {
8232 set msg [mc "Revision %s is not in the current view" $sha1string]
8234 error_popup $msg
8237 proc lineenter {x y id} {
8238 global hoverx hovery hoverid hovertimer
8239 global commitinfo canv
8241 if {![info exists commitinfo($id)] && ![getcommit $id]} return
8242 set hoverx $x
8243 set hovery $y
8244 set hoverid $id
8245 if {[info exists hovertimer]} {
8246 after cancel $hovertimer
8248 set hovertimer [after 500 linehover]
8249 $canv delete hover
8252 proc linemotion {x y id} {
8253 global hoverx hovery hoverid hovertimer
8255 if {[info exists hoverid] && $id == $hoverid} {
8256 set hoverx $x
8257 set hovery $y
8258 if {[info exists hovertimer]} {
8259 after cancel $hovertimer
8261 set hovertimer [after 500 linehover]
8265 proc lineleave {id} {
8266 global hoverid hovertimer canv
8268 if {[info exists hoverid] && $id == $hoverid} {
8269 $canv delete hover
8270 if {[info exists hovertimer]} {
8271 after cancel $hovertimer
8272 unset hovertimer
8274 unset hoverid
8278 proc linehover {} {
8279 global hoverx hovery hoverid hovertimer
8280 global canv linespc lthickness
8281 global commitinfo
8283 set text [lindex $commitinfo($hoverid) 0]
8284 set ymax [lindex [$canv cget -scrollregion] 3]
8285 if {$ymax == {}} return
8286 set yfrac [lindex [$canv yview] 0]
8287 set x [expr {$hoverx + 2 * $linespc}]
8288 set y [expr {$hovery + $yfrac * $ymax - $linespc / 2}]
8289 set x0 [expr {$x - 2 * $lthickness}]
8290 set y0 [expr {$y - 2 * $lthickness}]
8291 set x1 [expr {$x + [font measure mainfont $text] + 2 * $lthickness}]
8292 set y1 [expr {$y + $linespc + 2 * $lthickness}]
8293 set t [$canv create rectangle $x0 $y0 $x1 $y1 \
8294 -fill \#ffff80 -outline black -width 1 -tags hover]
8295 $canv raise $t
8296 set t [$canv create text $x $y -anchor nw -text $text -tags hover \
8297 -font mainfont]
8298 $canv raise $t
8301 proc clickisonarrow {id y} {
8302 global lthickness
8304 set ranges [rowranges $id]
8305 set thresh [expr {2 * $lthickness + 6}]
8306 set n [expr {[llength $ranges] - 1}]
8307 for {set i 1} {$i < $n} {incr i} {
8308 set row [lindex $ranges $i]
8309 if {abs([yc $row] - $y) < $thresh} {
8310 return $i
8313 return {}
8316 proc arrowjump {id n y} {
8317 global canv
8319 # 1 <-> 2, 3 <-> 4, etc...
8320 set n [expr {(($n - 1) ^ 1) + 1}]
8321 set row [lindex [rowranges $id] $n]
8322 set yt [yc $row]
8323 set ymax [lindex [$canv cget -scrollregion] 3]
8324 if {$ymax eq {} || $ymax <= 0} return
8325 set view [$canv yview]
8326 set yspan [expr {[lindex $view 1] - [lindex $view 0]}]
8327 set yfrac [expr {$yt / $ymax - $yspan / 2}]
8328 if {$yfrac < 0} {
8329 set yfrac 0
8331 allcanvs yview moveto $yfrac
8334 proc lineclick {x y id isnew} {
8335 global ctext commitinfo children canv thickerline curview
8337 if {![info exists commitinfo($id)] && ![getcommit $id]} return
8338 unmarkmatches
8339 unselectline
8340 normalline
8341 $canv delete hover
8342 # draw this line thicker than normal
8343 set thickerline $id
8344 drawlines $id
8345 if {$isnew} {
8346 set ymax [lindex [$canv cget -scrollregion] 3]
8347 if {$ymax eq {}} return
8348 set yfrac [lindex [$canv yview] 0]
8349 set y [expr {$y + $yfrac * $ymax}]
8351 set dirn [clickisonarrow $id $y]
8352 if {$dirn ne {}} {
8353 arrowjump $id $dirn $y
8354 return
8357 if {$isnew} {
8358 addtohistory [list lineclick $x $y $id 0] savectextpos
8360 # fill the details pane with info about this line
8361 $ctext conf -state normal
8362 clear_ctext
8363 settabs 0
8364 $ctext insert end "[mc "Parent"]:\t"
8365 $ctext insert end $id link0
8366 setlink $id link0
8367 set info $commitinfo($id)
8368 $ctext insert end "\n\t[lindex $info 0]\n"
8369 $ctext insert end "\t[mc "Author"]:\t[lindex $info 1]\n"
8370 set date [formatdate [lindex $info 2]]
8371 $ctext insert end "\t[mc "Date"]:\t$date\n"
8372 set kids $children($curview,$id)
8373 if {$kids ne {}} {
8374 $ctext insert end "\n[mc "Children"]:"
8375 set i 0
8376 foreach child $kids {
8377 incr i
8378 if {![info exists commitinfo($child)] && ![getcommit $child]} continue
8379 set info $commitinfo($child)
8380 $ctext insert end "\n\t"
8381 $ctext insert end $child link$i
8382 setlink $child link$i
8383 $ctext insert end "\n\t[lindex $info 0]"
8384 $ctext insert end "\n\t[mc "Author"]:\t[lindex $info 1]"
8385 set date [formatdate [lindex $info 2]]
8386 $ctext insert end "\n\t[mc "Date"]:\t$date\n"
8389 maybe_scroll_ctext 1
8390 $ctext conf -state disabled
8391 init_flist {}
8394 proc normalline {} {
8395 global thickerline
8396 if {[info exists thickerline]} {
8397 set id $thickerline
8398 unset thickerline
8399 drawlines $id
8403 proc selbyid {id {isnew 1}} {
8404 global curview
8405 if {[commitinview $id $curview]} {
8406 selectline [rowofcommit $id] $isnew
8410 proc mstime {} {
8411 global startmstime
8412 if {![info exists startmstime]} {
8413 set startmstime [clock clicks -milliseconds]
8415 return [format "%.3f" [expr {([clock click -milliseconds] - $startmstime) / 1000.0}]]
8418 proc rowmenu {x y id} {
8419 global rowctxmenu selectedline rowmenuid curview
8420 global nullid nullid2 fakerowmenu mainhead markedid
8422 stopfinding
8423 set rowmenuid $id
8424 if {$selectedline eq {} || [rowofcommit $id] eq $selectedline} {
8425 set state disabled
8426 } else {
8427 set state normal
8429 if {$id ne $nullid && $id ne $nullid2} {
8430 set menu $rowctxmenu
8431 if {$mainhead ne {}} {
8432 $menu entryconfigure 7 -label [mc "Reset %s branch to here" $mainhead] -state normal
8433 } else {
8434 $menu entryconfigure 7 -label [mc "Detached head: can't reset" $mainhead] -state disabled
8436 if {[info exists markedid] && $markedid ne $id} {
8437 $menu entryconfigure 9 -state normal
8438 $menu entryconfigure 10 -state normal
8439 $menu entryconfigure 11 -state normal
8440 } else {
8441 $menu entryconfigure 9 -state disabled
8442 $menu entryconfigure 10 -state disabled
8443 $menu entryconfigure 11 -state disabled
8445 } else {
8446 set menu $fakerowmenu
8448 $menu entryconfigure [mca "Diff this -> selected"] -state $state
8449 $menu entryconfigure [mca "Diff selected -> this"] -state $state
8450 $menu entryconfigure [mca "Make patch"] -state $state
8451 tk_popup $menu $x $y
8454 proc markhere {} {
8455 global rowmenuid markedid canv
8457 set markedid $rowmenuid
8458 make_idmark $markedid
8461 proc gotomark {} {
8462 global markedid
8464 if {[info exists markedid]} {
8465 selbyid $markedid
8469 proc replace_by_kids {l r} {
8470 global curview children
8472 set id [commitonrow $r]
8473 set l [lreplace $l 0 0]
8474 foreach kid $children($curview,$id) {
8475 lappend l [rowofcommit $kid]
8477 return [lsort -integer -decreasing -unique $l]
8480 proc find_common_desc {} {
8481 global markedid rowmenuid curview children
8483 if {![info exists markedid]} return
8484 if {![commitinview $markedid $curview] ||
8485 ![commitinview $rowmenuid $curview]} return
8486 #set t1 [clock clicks -milliseconds]
8487 set l1 [list [rowofcommit $markedid]]
8488 set l2 [list [rowofcommit $rowmenuid]]
8489 while 1 {
8490 set r1 [lindex $l1 0]
8491 set r2 [lindex $l2 0]
8492 if {$r1 eq {} || $r2 eq {}} break
8493 if {$r1 == $r2} {
8494 selectline $r1 1
8495 break
8497 if {$r1 > $r2} {
8498 set l1 [replace_by_kids $l1 $r1]
8499 } else {
8500 set l2 [replace_by_kids $l2 $r2]
8503 #set t2 [clock clicks -milliseconds]
8504 #puts "took [expr {$t2-$t1}]ms"
8507 proc compare_commits {} {
8508 global markedid rowmenuid curview children
8510 if {![info exists markedid]} return
8511 if {![commitinview $markedid $curview]} return
8512 addtohistory [list do_cmp_commits $markedid $rowmenuid]
8513 do_cmp_commits $markedid $rowmenuid
8516 proc getpatchid {id} {
8517 global patchids
8519 if {![info exists patchids($id)]} {
8520 set cmd [diffcmd [list $id] {-p --root}]
8521 # trim off the initial "|"
8522 set cmd [lrange $cmd 1 end]
8523 if {[catch {
8524 set x [eval exec $cmd | git patch-id]
8525 set patchids($id) [lindex $x 0]
8526 }]} {
8527 set patchids($id) "error"
8530 return $patchids($id)
8533 proc do_cmp_commits {a b} {
8534 global ctext curview parents children patchids commitinfo
8536 $ctext conf -state normal
8537 clear_ctext
8538 init_flist {}
8539 for {set i 0} {$i < 100} {incr i} {
8540 set skipa 0
8541 set skipb 0
8542 if {[llength $parents($curview,$a)] > 1} {
8543 appendshortlink $a [mc "Skipping merge commit "] "\n"
8544 set skipa 1
8545 } else {
8546 set patcha [getpatchid $a]
8548 if {[llength $parents($curview,$b)] > 1} {
8549 appendshortlink $b [mc "Skipping merge commit "] "\n"
8550 set skipb 1
8551 } else {
8552 set patchb [getpatchid $b]
8554 if {!$skipa && !$skipb} {
8555 set heada [lindex $commitinfo($a) 0]
8556 set headb [lindex $commitinfo($b) 0]
8557 if {$patcha eq "error"} {
8558 appendshortlink $a [mc "Error getting patch ID for "] \
8559 [mc " - stopping\n"]
8560 break
8562 if {$patchb eq "error"} {
8563 appendshortlink $b [mc "Error getting patch ID for "] \
8564 [mc " - stopping\n"]
8565 break
8567 if {$patcha eq $patchb} {
8568 if {$heada eq $headb} {
8569 appendshortlink $a [mc "Commit "]
8570 appendshortlink $b " == " " $heada\n"
8571 } else {
8572 appendshortlink $a [mc "Commit "] " $heada\n"
8573 appendshortlink $b [mc " is the same patch as\n "] \
8574 " $headb\n"
8576 set skipa 1
8577 set skipb 1
8578 } else {
8579 $ctext insert end "\n"
8580 appendshortlink $a [mc "Commit "] " $heada\n"
8581 appendshortlink $b [mc " differs from\n "] \
8582 " $headb\n"
8583 $ctext insert end [mc "Diff of commits:\n\n"]
8584 $ctext conf -state disabled
8585 update
8586 diffcommits $a $b
8587 return
8590 if {$skipa} {
8591 set kids [real_children $curview,$a]
8592 if {[llength $kids] != 1} {
8593 $ctext insert end "\n"
8594 appendshortlink $a [mc "Commit "] \
8595 [mc " has %s children - stopping\n" [llength $kids]]
8596 break
8598 set a [lindex $kids 0]
8600 if {$skipb} {
8601 set kids [real_children $curview,$b]
8602 if {[llength $kids] != 1} {
8603 appendshortlink $b [mc "Commit "] \
8604 [mc " has %s children - stopping\n" [llength $kids]]
8605 break
8607 set b [lindex $kids 0]
8610 $ctext conf -state disabled
8613 proc diffcommits {a b} {
8614 global diffcontext diffids blobdifffd diffinhdr currdiffsubmod
8616 set tmpdir [gitknewtmpdir]
8617 set fna [file join $tmpdir "commit-[string range $a 0 7]"]
8618 set fnb [file join $tmpdir "commit-[string range $b 0 7]"]
8619 if {[catch {
8620 exec git diff-tree -p --pretty $a >$fna
8621 exec git diff-tree -p --pretty $b >$fnb
8622 } err]} {
8623 error_popup [mc "Error writing commit to file: %s" $err]
8624 return
8626 if {[catch {
8627 set fd [open "| diff -U$diffcontext $fna $fnb" r]
8628 } err]} {
8629 error_popup [mc "Error diffing commits: %s" $err]
8630 return
8632 set diffids [list commits $a $b]
8633 set blobdifffd($diffids) $fd
8634 set diffinhdr 0
8635 set currdiffsubmod ""
8636 filerun $fd [list getblobdiffline $fd $diffids]
8639 proc diffvssel {dirn} {
8640 global rowmenuid selectedline
8642 if {$selectedline eq {}} return
8643 if {$dirn} {
8644 set oldid [commitonrow $selectedline]
8645 set newid $rowmenuid
8646 } else {
8647 set oldid $rowmenuid
8648 set newid [commitonrow $selectedline]
8650 addtohistory [list doseldiff $oldid $newid] savectextpos
8651 doseldiff $oldid $newid
8654 proc doseldiff {oldid newid} {
8655 global ctext
8656 global commitinfo
8658 $ctext conf -state normal
8659 clear_ctext
8660 init_flist [mc "Top"]
8661 $ctext insert end "[mc "From"] "
8662 $ctext insert end $oldid link0
8663 setlink $oldid link0
8664 $ctext insert end "\n "
8665 $ctext insert end [lindex $commitinfo($oldid) 0]
8666 $ctext insert end "\n\n[mc "To"] "
8667 $ctext insert end $newid link1
8668 setlink $newid link1
8669 $ctext insert end "\n "
8670 $ctext insert end [lindex $commitinfo($newid) 0]
8671 $ctext insert end "\n"
8672 $ctext conf -state disabled
8673 $ctext tag remove found 1.0 end
8674 startdiff [list $oldid $newid]
8677 proc mkpatch {} {
8678 global rowmenuid currentid commitinfo patchtop patchnum NS
8680 if {![info exists currentid]} return
8681 set oldid $currentid
8682 set oldhead [lindex $commitinfo($oldid) 0]
8683 set newid $rowmenuid
8684 set newhead [lindex $commitinfo($newid) 0]
8685 set top .patch
8686 set patchtop $top
8687 catch {destroy $top}
8688 ttk_toplevel $top
8689 make_transient $top .
8690 ${NS}::label $top.title -text [mc "Generate patch"]
8691 grid $top.title - -pady 10
8692 ${NS}::label $top.from -text [mc "From:"]
8693 ${NS}::entry $top.fromsha1 -width 40
8694 $top.fromsha1 insert 0 $oldid
8695 $top.fromsha1 conf -state readonly
8696 grid $top.from $top.fromsha1 -sticky w
8697 ${NS}::entry $top.fromhead -width 60
8698 $top.fromhead insert 0 $oldhead
8699 $top.fromhead conf -state readonly
8700 grid x $top.fromhead -sticky w
8701 ${NS}::label $top.to -text [mc "To:"]
8702 ${NS}::entry $top.tosha1 -width 40
8703 $top.tosha1 insert 0 $newid
8704 $top.tosha1 conf -state readonly
8705 grid $top.to $top.tosha1 -sticky w
8706 ${NS}::entry $top.tohead -width 60
8707 $top.tohead insert 0 $newhead
8708 $top.tohead conf -state readonly
8709 grid x $top.tohead -sticky w
8710 ${NS}::button $top.rev -text [mc "Reverse"] -command mkpatchrev
8711 grid $top.rev x -pady 10 -padx 5
8712 ${NS}::label $top.flab -text [mc "Output file:"]
8713 ${NS}::entry $top.fname -width 60
8714 $top.fname insert 0 [file normalize "patch$patchnum.patch"]
8715 incr patchnum
8716 grid $top.flab $top.fname -sticky w
8717 ${NS}::frame $top.buts
8718 ${NS}::button $top.buts.gen -text [mc "Generate"] -command mkpatchgo
8719 ${NS}::button $top.buts.can -text [mc "Cancel"] -command mkpatchcan
8720 bind $top <Key-Return> mkpatchgo
8721 bind $top <Key-Escape> mkpatchcan
8722 grid $top.buts.gen $top.buts.can
8723 grid columnconfigure $top.buts 0 -weight 1 -uniform a
8724 grid columnconfigure $top.buts 1 -weight 1 -uniform a
8725 grid $top.buts - -pady 10 -sticky ew
8726 focus $top.fname
8729 proc mkpatchrev {} {
8730 global patchtop
8732 set oldid [$patchtop.fromsha1 get]
8733 set oldhead [$patchtop.fromhead get]
8734 set newid [$patchtop.tosha1 get]
8735 set newhead [$patchtop.tohead get]
8736 foreach e [list fromsha1 fromhead tosha1 tohead] \
8737 v [list $newid $newhead $oldid $oldhead] {
8738 $patchtop.$e conf -state normal
8739 $patchtop.$e delete 0 end
8740 $patchtop.$e insert 0 $v
8741 $patchtop.$e conf -state readonly
8745 proc mkpatchgo {} {
8746 global patchtop nullid nullid2
8748 set oldid [$patchtop.fromsha1 get]
8749 set newid [$patchtop.tosha1 get]
8750 set fname [$patchtop.fname get]
8751 set cmd [diffcmd [list $oldid $newid] -p]
8752 # trim off the initial "|"
8753 set cmd [lrange $cmd 1 end]
8754 lappend cmd >$fname &
8755 if {[catch {eval exec $cmd} err]} {
8756 error_popup "[mc "Error creating patch:"] $err" $patchtop
8758 catch {destroy $patchtop}
8759 unset patchtop
8762 proc mkpatchcan {} {
8763 global patchtop
8765 catch {destroy $patchtop}
8766 unset patchtop
8769 proc mktag {} {
8770 global rowmenuid mktagtop commitinfo NS
8772 set top .maketag
8773 set mktagtop $top
8774 catch {destroy $top}
8775 ttk_toplevel $top
8776 make_transient $top .
8777 ${NS}::label $top.title -text [mc "Create tag"]
8778 grid $top.title - -pady 10
8779 ${NS}::label $top.id -text [mc "ID:"]
8780 ${NS}::entry $top.sha1 -width 40
8781 $top.sha1 insert 0 $rowmenuid
8782 $top.sha1 conf -state readonly
8783 grid $top.id $top.sha1 -sticky w
8784 ${NS}::entry $top.head -width 60
8785 $top.head insert 0 [lindex $commitinfo($rowmenuid) 0]
8786 $top.head conf -state readonly
8787 grid x $top.head -sticky w
8788 ${NS}::label $top.tlab -text [mc "Tag name:"]
8789 ${NS}::entry $top.tag -width 60
8790 grid $top.tlab $top.tag -sticky w
8791 ${NS}::label $top.op -text [mc "Tag message is optional"]
8792 grid $top.op -columnspan 2 -sticky we
8793 ${NS}::label $top.mlab -text [mc "Tag message:"]
8794 ${NS}::entry $top.msg -width 60
8795 grid $top.mlab $top.msg -sticky w
8796 ${NS}::frame $top.buts
8797 ${NS}::button $top.buts.gen -text [mc "Create"] -command mktaggo
8798 ${NS}::button $top.buts.can -text [mc "Cancel"] -command mktagcan
8799 bind $top <Key-Return> mktaggo
8800 bind $top <Key-Escape> mktagcan
8801 grid $top.buts.gen $top.buts.can
8802 grid columnconfigure $top.buts 0 -weight 1 -uniform a
8803 grid columnconfigure $top.buts 1 -weight 1 -uniform a
8804 grid $top.buts - -pady 10 -sticky ew
8805 focus $top.tag
8808 proc domktag {} {
8809 global mktagtop env tagids idtags
8811 set id [$mktagtop.sha1 get]
8812 set tag [$mktagtop.tag get]
8813 set msg [$mktagtop.msg get]
8814 if {$tag == {}} {
8815 error_popup [mc "No tag name specified"] $mktagtop
8816 return 0
8818 if {[info exists tagids($tag)]} {
8819 error_popup [mc "Tag \"%s\" already exists" $tag] $mktagtop
8820 return 0
8822 if {[catch {
8823 if {$msg != {}} {
8824 exec git tag -a -m $msg $tag $id
8825 } else {
8826 exec git tag $tag $id
8828 } err]} {
8829 error_popup "[mc "Error creating tag:"] $err" $mktagtop
8830 return 0
8833 set tagids($tag) $id
8834 lappend idtags($id) $tag
8835 redrawtags $id
8836 addedtag $id
8837 dispneartags 0
8838 run refill_reflist
8839 return 1
8842 proc redrawtags {id} {
8843 global canv linehtag idpos currentid curview cmitlisted markedid
8844 global canvxmax iddrawn circleitem mainheadid circlecolors
8846 if {![commitinview $id $curview]} return
8847 if {![info exists iddrawn($id)]} return
8848 set row [rowofcommit $id]
8849 if {$id eq $mainheadid} {
8850 set ofill yellow
8851 } else {
8852 set ofill [lindex $circlecolors $cmitlisted($curview,$id)]
8854 $canv itemconf $circleitem($row) -fill $ofill
8855 $canv delete tag.$id
8856 set xt [eval drawtags $id $idpos($id)]
8857 $canv coords $linehtag($id) $xt [lindex $idpos($id) 2]
8858 set text [$canv itemcget $linehtag($id) -text]
8859 set font [$canv itemcget $linehtag($id) -font]
8860 set xr [expr {$xt + [font measure $font $text]}]
8861 if {$xr > $canvxmax} {
8862 set canvxmax $xr
8863 setcanvscroll
8865 if {[info exists currentid] && $currentid == $id} {
8866 make_secsel $id
8868 if {[info exists markedid] && $markedid eq $id} {
8869 make_idmark $id
8873 proc mktagcan {} {
8874 global mktagtop
8876 catch {destroy $mktagtop}
8877 unset mktagtop
8880 proc mktaggo {} {
8881 if {![domktag]} return
8882 mktagcan
8885 proc writecommit {} {
8886 global rowmenuid wrcomtop commitinfo wrcomcmd NS
8888 set top .writecommit
8889 set wrcomtop $top
8890 catch {destroy $top}
8891 ttk_toplevel $top
8892 make_transient $top .
8893 ${NS}::label $top.title -text [mc "Write commit to file"]
8894 grid $top.title - -pady 10
8895 ${NS}::label $top.id -text [mc "ID:"]
8896 ${NS}::entry $top.sha1 -width 40
8897 $top.sha1 insert 0 $rowmenuid
8898 $top.sha1 conf -state readonly
8899 grid $top.id $top.sha1 -sticky w
8900 ${NS}::entry $top.head -width 60
8901 $top.head insert 0 [lindex $commitinfo($rowmenuid) 0]
8902 $top.head conf -state readonly
8903 grid x $top.head -sticky w
8904 ${NS}::label $top.clab -text [mc "Command:"]
8905 ${NS}::entry $top.cmd -width 60 -textvariable wrcomcmd
8906 grid $top.clab $top.cmd -sticky w -pady 10
8907 ${NS}::label $top.flab -text [mc "Output file:"]
8908 ${NS}::entry $top.fname -width 60
8909 $top.fname insert 0 [file normalize "commit-[string range $rowmenuid 0 6]"]
8910 grid $top.flab $top.fname -sticky w
8911 ${NS}::frame $top.buts
8912 ${NS}::button $top.buts.gen -text [mc "Write"] -command wrcomgo
8913 ${NS}::button $top.buts.can -text [mc "Cancel"] -command wrcomcan
8914 bind $top <Key-Return> wrcomgo
8915 bind $top <Key-Escape> wrcomcan
8916 grid $top.buts.gen $top.buts.can
8917 grid columnconfigure $top.buts 0 -weight 1 -uniform a
8918 grid columnconfigure $top.buts 1 -weight 1 -uniform a
8919 grid $top.buts - -pady 10 -sticky ew
8920 focus $top.fname
8923 proc wrcomgo {} {
8924 global wrcomtop
8926 set id [$wrcomtop.sha1 get]
8927 set cmd "echo $id | [$wrcomtop.cmd get]"
8928 set fname [$wrcomtop.fname get]
8929 if {[catch {exec sh -c $cmd >$fname &} err]} {
8930 error_popup "[mc "Error writing commit:"] $err" $wrcomtop
8932 catch {destroy $wrcomtop}
8933 unset wrcomtop
8936 proc wrcomcan {} {
8937 global wrcomtop
8939 catch {destroy $wrcomtop}
8940 unset wrcomtop
8943 proc mkbranch {} {
8944 global rowmenuid mkbrtop NS
8946 set top .makebranch
8947 catch {destroy $top}
8948 ttk_toplevel $top
8949 make_transient $top .
8950 ${NS}::label $top.title -text [mc "Create new branch"]
8951 grid $top.title - -pady 10
8952 ${NS}::label $top.id -text [mc "ID:"]
8953 ${NS}::entry $top.sha1 -width 40
8954 $top.sha1 insert 0 $rowmenuid
8955 $top.sha1 conf -state readonly
8956 grid $top.id $top.sha1 -sticky w
8957 ${NS}::label $top.nlab -text [mc "Name:"]
8958 ${NS}::entry $top.name -width 40
8959 grid $top.nlab $top.name -sticky w
8960 ${NS}::frame $top.buts
8961 ${NS}::button $top.buts.go -text [mc "Create"] -command [list mkbrgo $top]
8962 ${NS}::button $top.buts.can -text [mc "Cancel"] -command "catch {destroy $top}"
8963 bind $top <Key-Return> [list mkbrgo $top]
8964 bind $top <Key-Escape> "catch {destroy $top}"
8965 grid $top.buts.go $top.buts.can
8966 grid columnconfigure $top.buts 0 -weight 1 -uniform a
8967 grid columnconfigure $top.buts 1 -weight 1 -uniform a
8968 grid $top.buts - -pady 10 -sticky ew
8969 focus $top.name
8972 proc mkbrgo {top} {
8973 global headids idheads
8975 set name [$top.name get]
8976 set id [$top.sha1 get]
8977 set cmdargs {}
8978 set old_id {}
8979 if {$name eq {}} {
8980 error_popup [mc "Please specify a name for the new branch"] $top
8981 return
8983 if {[info exists headids($name)]} {
8984 if {![confirm_popup [mc \
8985 "Branch '%s' already exists. Overwrite?" $name] $top]} {
8986 return
8988 set old_id $headids($name)
8989 lappend cmdargs -f
8991 catch {destroy $top}
8992 lappend cmdargs $name $id
8993 nowbusy newbranch
8994 update
8995 if {[catch {
8996 eval exec git branch $cmdargs
8997 } err]} {
8998 notbusy newbranch
8999 error_popup $err
9000 } else {
9001 notbusy newbranch
9002 if {$old_id ne {}} {
9003 movehead $id $name
9004 movedhead $id $name
9005 redrawtags $old_id
9006 redrawtags $id
9007 } else {
9008 set headids($name) $id
9009 lappend idheads($id) $name
9010 addedhead $id $name
9011 redrawtags $id
9013 dispneartags 0
9014 run refill_reflist
9018 proc exec_citool {tool_args {baseid {}}} {
9019 global commitinfo env
9021 set save_env [array get env GIT_AUTHOR_*]
9023 if {$baseid ne {}} {
9024 if {![info exists commitinfo($baseid)]} {
9025 getcommit $baseid
9027 set author [lindex $commitinfo($baseid) 1]
9028 set date [lindex $commitinfo($baseid) 2]
9029 if {[regexp {^\s*(\S.*\S|\S)\s*<(.*)>\s*$} \
9030 $author author name email]
9031 && $date ne {}} {
9032 set env(GIT_AUTHOR_NAME) $name
9033 set env(GIT_AUTHOR_EMAIL) $email
9034 set env(GIT_AUTHOR_DATE) $date
9038 eval exec git citool $tool_args &
9040 array unset env GIT_AUTHOR_*
9041 array set env $save_env
9044 proc cherrypick {} {
9045 global rowmenuid curview
9046 global mainhead mainheadid
9048 set oldhead [exec git rev-parse HEAD]
9049 set dheads [descheads $rowmenuid]
9050 if {$dheads ne {} && [lsearch -exact $dheads $oldhead] >= 0} {
9051 set ok [confirm_popup [mc "Commit %s is already\
9052 included in branch %s -- really re-apply it?" \
9053 [string range $rowmenuid 0 7] $mainhead]]
9054 if {!$ok} return
9056 nowbusy cherrypick [mc "Cherry-picking"]
9057 update
9058 # Unfortunately git-cherry-pick writes stuff to stderr even when
9059 # no error occurs, and exec takes that as an indication of error...
9060 if {[catch {exec sh -c "git cherry-pick -r $rowmenuid 2>&1"} err]} {
9061 notbusy cherrypick
9062 if {[regexp -line \
9063 {Entry '(.*)' (would be overwritten by merge|not uptodate)} \
9064 $err msg fname]} {
9065 error_popup [mc "Cherry-pick failed because of local changes\
9066 to file '%s'.\nPlease commit, reset or stash\
9067 your changes and try again." $fname]
9068 } elseif {[regexp -line \
9069 {^(CONFLICT \(.*\):|Automatic cherry-pick failed|error: could not apply)} \
9070 $err]} {
9071 if {[confirm_popup [mc "Cherry-pick failed because of merge\
9072 conflict.\nDo you wish to run git citool to\
9073 resolve it?"]]} {
9074 # Force citool to read MERGE_MSG
9075 file delete [file join [gitdir] "GITGUI_MSG"]
9076 exec_citool {} $rowmenuid
9078 } else {
9079 error_popup $err
9081 run updatecommits
9082 return
9084 set newhead [exec git rev-parse HEAD]
9085 if {$newhead eq $oldhead} {
9086 notbusy cherrypick
9087 error_popup [mc "No changes committed"]
9088 return
9090 addnewchild $newhead $oldhead
9091 if {[commitinview $oldhead $curview]} {
9092 # XXX this isn't right if we have a path limit...
9093 insertrow $newhead $oldhead $curview
9094 if {$mainhead ne {}} {
9095 movehead $newhead $mainhead
9096 movedhead $newhead $mainhead
9098 set mainheadid $newhead
9099 redrawtags $oldhead
9100 redrawtags $newhead
9101 selbyid $newhead
9103 notbusy cherrypick
9106 proc resethead {} {
9107 global mainhead rowmenuid confirm_ok resettype NS
9109 set confirm_ok 0
9110 set w ".confirmreset"
9111 ttk_toplevel $w
9112 make_transient $w .
9113 wm title $w [mc "Confirm reset"]
9114 ${NS}::label $w.m -text \
9115 [mc "Reset branch %s to %s?" $mainhead [string range $rowmenuid 0 7]]
9116 pack $w.m -side top -fill x -padx 20 -pady 20
9117 ${NS}::labelframe $w.f -text [mc "Reset type:"]
9118 set resettype mixed
9119 ${NS}::radiobutton $w.f.soft -value soft -variable resettype \
9120 -text [mc "Soft: Leave working tree and index untouched"]
9121 grid $w.f.soft -sticky w
9122 ${NS}::radiobutton $w.f.mixed -value mixed -variable resettype \
9123 -text [mc "Mixed: Leave working tree untouched, reset index"]
9124 grid $w.f.mixed -sticky w
9125 ${NS}::radiobutton $w.f.hard -value hard -variable resettype \
9126 -text [mc "Hard: Reset working tree and index\n(discard ALL local changes)"]
9127 grid $w.f.hard -sticky w
9128 pack $w.f -side top -fill x -padx 4
9129 ${NS}::button $w.ok -text [mc OK] -command "set confirm_ok 1; destroy $w"
9130 pack $w.ok -side left -fill x -padx 20 -pady 20
9131 ${NS}::button $w.cancel -text [mc Cancel] -command "destroy $w"
9132 bind $w <Key-Escape> [list destroy $w]
9133 pack $w.cancel -side right -fill x -padx 20 -pady 20
9134 bind $w <Visibility> "grab $w; focus $w"
9135 tkwait window $w
9136 if {!$confirm_ok} return
9137 if {[catch {set fd [open \
9138 [list | git reset --$resettype $rowmenuid 2>@1] r]} err]} {
9139 error_popup $err
9140 } else {
9141 dohidelocalchanges
9142 filerun $fd [list readresetstat $fd]
9143 nowbusy reset [mc "Resetting"]
9144 selbyid $rowmenuid
9148 proc readresetstat {fd} {
9149 global mainhead mainheadid showlocalchanges rprogcoord
9151 if {[gets $fd line] >= 0} {
9152 if {[regexp {([0-9]+)% \(([0-9]+)/([0-9]+)\)} $line match p m n]} {
9153 set rprogcoord [expr {1.0 * $m / $n}]
9154 adjustprogress
9156 return 1
9158 set rprogcoord 0
9159 adjustprogress
9160 notbusy reset
9161 if {[catch {close $fd} err]} {
9162 error_popup $err
9164 set oldhead $mainheadid
9165 set newhead [exec git rev-parse HEAD]
9166 if {$newhead ne $oldhead} {
9167 movehead $newhead $mainhead
9168 movedhead $newhead $mainhead
9169 set mainheadid $newhead
9170 redrawtags $oldhead
9171 redrawtags $newhead
9173 if {$showlocalchanges} {
9174 doshowlocalchanges
9176 return 0
9179 # context menu for a head
9180 proc headmenu {x y id head} {
9181 global headmenuid headmenuhead headctxmenu mainhead
9183 stopfinding
9184 set headmenuid $id
9185 set headmenuhead $head
9186 set state normal
9187 if {[string match "remotes/*" $head]} {
9188 set state disabled
9190 if {$head eq $mainhead} {
9191 set state disabled
9193 $headctxmenu entryconfigure 0 -state $state
9194 $headctxmenu entryconfigure 1 -state $state
9195 tk_popup $headctxmenu $x $y
9198 proc cobranch {} {
9199 global headmenuid headmenuhead headids
9200 global showlocalchanges
9202 # check the tree is clean first??
9203 nowbusy checkout [mc "Checking out"]
9204 update
9205 dohidelocalchanges
9206 if {[catch {
9207 set fd [open [list | git checkout $headmenuhead 2>@1] r]
9208 } err]} {
9209 notbusy checkout
9210 error_popup $err
9211 if {$showlocalchanges} {
9212 dodiffindex
9214 } else {
9215 filerun $fd [list readcheckoutstat $fd $headmenuhead $headmenuid]
9219 proc readcheckoutstat {fd newhead newheadid} {
9220 global mainhead mainheadid headids showlocalchanges progresscoords
9221 global viewmainheadid curview
9223 if {[gets $fd line] >= 0} {
9224 if {[regexp {([0-9]+)% \(([0-9]+)/([0-9]+)\)} $line match p m n]} {
9225 set progresscoords [list 0 [expr {1.0 * $m / $n}]]
9226 adjustprogress
9228 return 1
9230 set progresscoords {0 0}
9231 adjustprogress
9232 notbusy checkout
9233 if {[catch {close $fd} err]} {
9234 error_popup $err
9236 set oldmainid $mainheadid
9237 set mainhead $newhead
9238 set mainheadid $newheadid
9239 set viewmainheadid($curview) $newheadid
9240 redrawtags $oldmainid
9241 redrawtags $newheadid
9242 selbyid $newheadid
9243 if {$showlocalchanges} {
9244 dodiffindex
9248 proc rmbranch {} {
9249 global headmenuid headmenuhead mainhead
9250 global idheads
9252 set head $headmenuhead
9253 set id $headmenuid
9254 # this check shouldn't be needed any more...
9255 if {$head eq $mainhead} {
9256 error_popup [mc "Cannot delete the currently checked-out branch"]
9257 return
9259 set dheads [descheads $id]
9260 if {[llength $dheads] == 1 && $idheads($dheads) eq $head} {
9261 # the stuff on this branch isn't on any other branch
9262 if {![confirm_popup [mc "The commits on branch %s aren't on any other\
9263 branch.\nReally delete branch %s?" $head $head]]} return
9265 nowbusy rmbranch
9266 update
9267 if {[catch {exec git branch -D $head} err]} {
9268 notbusy rmbranch
9269 error_popup $err
9270 return
9272 removehead $id $head
9273 removedhead $id $head
9274 redrawtags $id
9275 notbusy rmbranch
9276 dispneartags 0
9277 run refill_reflist
9280 # Display a list of tags and heads
9281 proc showrefs {} {
9282 global showrefstop bgcolor fgcolor selectbgcolor NS
9283 global bglist fglist reflistfilter reflist maincursor
9285 set top .showrefs
9286 set showrefstop $top
9287 if {[winfo exists $top]} {
9288 raise $top
9289 refill_reflist
9290 return
9292 ttk_toplevel $top
9293 wm title $top [mc "Tags and heads: %s" [file tail [pwd]]]
9294 make_transient $top .
9295 text $top.list -background $bgcolor -foreground $fgcolor \
9296 -selectbackground $selectbgcolor -font mainfont \
9297 -xscrollcommand "$top.xsb set" -yscrollcommand "$top.ysb set" \
9298 -width 30 -height 20 -cursor $maincursor \
9299 -spacing1 1 -spacing3 1 -state disabled
9300 $top.list tag configure highlight -background $selectbgcolor
9301 lappend bglist $top.list
9302 lappend fglist $top.list
9303 ${NS}::scrollbar $top.ysb -command "$top.list yview" -orient vertical
9304 ${NS}::scrollbar $top.xsb -command "$top.list xview" -orient horizontal
9305 grid $top.list $top.ysb -sticky nsew
9306 grid $top.xsb x -sticky ew
9307 ${NS}::frame $top.f
9308 ${NS}::label $top.f.l -text "[mc "Filter"]: "
9309 ${NS}::entry $top.f.e -width 20 -textvariable reflistfilter
9310 set reflistfilter "*"
9311 trace add variable reflistfilter write reflistfilter_change
9312 pack $top.f.e -side right -fill x -expand 1
9313 pack $top.f.l -side left
9314 grid $top.f - -sticky ew -pady 2
9315 ${NS}::button $top.close -command [list destroy $top] -text [mc "Close"]
9316 bind $top <Key-Escape> [list destroy $top]
9317 grid $top.close -
9318 grid columnconfigure $top 0 -weight 1
9319 grid rowconfigure $top 0 -weight 1
9320 bind $top.list <1> {break}
9321 bind $top.list <B1-Motion> {break}
9322 bind $top.list <ButtonRelease-1> {sel_reflist %W %x %y; break}
9323 set reflist {}
9324 refill_reflist
9327 proc sel_reflist {w x y} {
9328 global showrefstop reflist headids tagids otherrefids
9330 if {![winfo exists $showrefstop]} return
9331 set l [lindex [split [$w index "@$x,$y"] "."] 0]
9332 set ref [lindex $reflist [expr {$l-1}]]
9333 set n [lindex $ref 0]
9334 switch -- [lindex $ref 1] {
9335 "H" {selbyid $headids($n)}
9336 "T" {selbyid $tagids($n)}
9337 "o" {selbyid $otherrefids($n)}
9339 $showrefstop.list tag add highlight $l.0 "$l.0 lineend"
9342 proc unsel_reflist {} {
9343 global showrefstop
9345 if {![info exists showrefstop] || ![winfo exists $showrefstop]} return
9346 $showrefstop.list tag remove highlight 0.0 end
9349 proc reflistfilter_change {n1 n2 op} {
9350 global reflistfilter
9352 after cancel refill_reflist
9353 after 200 refill_reflist
9356 proc refill_reflist {} {
9357 global reflist reflistfilter showrefstop headids tagids otherrefids
9358 global curview
9360 if {![info exists showrefstop] || ![winfo exists $showrefstop]} return
9361 set refs {}
9362 foreach n [array names headids] {
9363 if {[string match $reflistfilter $n]} {
9364 if {[commitinview $headids($n) $curview]} {
9365 lappend refs [list $n H]
9366 } else {
9367 interestedin $headids($n) {run refill_reflist}
9371 foreach n [array names tagids] {
9372 if {[string match $reflistfilter $n]} {
9373 if {[commitinview $tagids($n) $curview]} {
9374 lappend refs [list $n T]
9375 } else {
9376 interestedin $tagids($n) {run refill_reflist}
9380 foreach n [array names otherrefids] {
9381 if {[string match $reflistfilter $n]} {
9382 if {[commitinview $otherrefids($n) $curview]} {
9383 lappend refs [list $n o]
9384 } else {
9385 interestedin $otherrefids($n) {run refill_reflist}
9389 set refs [lsort -index 0 $refs]
9390 if {$refs eq $reflist} return
9392 # Update the contents of $showrefstop.list according to the
9393 # differences between $reflist (old) and $refs (new)
9394 $showrefstop.list conf -state normal
9395 $showrefstop.list insert end "\n"
9396 set i 0
9397 set j 0
9398 while {$i < [llength $reflist] || $j < [llength $refs]} {
9399 if {$i < [llength $reflist]} {
9400 if {$j < [llength $refs]} {
9401 set cmp [string compare [lindex $reflist $i 0] \
9402 [lindex $refs $j 0]]
9403 if {$cmp == 0} {
9404 set cmp [string compare [lindex $reflist $i 1] \
9405 [lindex $refs $j 1]]
9407 } else {
9408 set cmp -1
9410 } else {
9411 set cmp 1
9413 switch -- $cmp {
9414 -1 {
9415 $showrefstop.list delete "[expr {$j+1}].0" "[expr {$j+2}].0"
9416 incr i
9419 incr i
9420 incr j
9423 set l [expr {$j + 1}]
9424 $showrefstop.list image create $l.0 -align baseline \
9425 -image reficon-[lindex $refs $j 1] -padx 2
9426 $showrefstop.list insert $l.1 "[lindex $refs $j 0]\n"
9427 incr j
9431 set reflist $refs
9432 # delete last newline
9433 $showrefstop.list delete end-2c end-1c
9434 $showrefstop.list conf -state disabled
9437 # Stuff for finding nearby tags
9438 proc getallcommits {} {
9439 global allcommits nextarc seeds allccache allcwait cachedarcs allcupdate
9440 global idheads idtags idotherrefs allparents tagobjid
9442 if {![info exists allcommits]} {
9443 set nextarc 0
9444 set allcommits 0
9445 set seeds {}
9446 set allcwait 0
9447 set cachedarcs 0
9448 set allccache [file join [gitdir] "gitk.cache"]
9449 if {![catch {
9450 set f [open $allccache r]
9451 set allcwait 1
9452 getcache $f
9453 }]} return
9456 if {$allcwait} {
9457 return
9459 set cmd [list | git rev-list --parents]
9460 set allcupdate [expr {$seeds ne {}}]
9461 if {!$allcupdate} {
9462 set ids "--all"
9463 } else {
9464 set refs [concat [array names idheads] [array names idtags] \
9465 [array names idotherrefs]]
9466 set ids {}
9467 set tagobjs {}
9468 foreach name [array names tagobjid] {
9469 lappend tagobjs $tagobjid($name)
9471 foreach id [lsort -unique $refs] {
9472 if {![info exists allparents($id)] &&
9473 [lsearch -exact $tagobjs $id] < 0} {
9474 lappend ids $id
9477 if {$ids ne {}} {
9478 foreach id $seeds {
9479 lappend ids "^$id"
9483 if {$ids ne {}} {
9484 set fd [open [concat $cmd $ids] r]
9485 fconfigure $fd -blocking 0
9486 incr allcommits
9487 nowbusy allcommits
9488 filerun $fd [list getallclines $fd]
9489 } else {
9490 dispneartags 0
9494 # Since most commits have 1 parent and 1 child, we group strings of
9495 # such commits into "arcs" joining branch/merge points (BMPs), which
9496 # are commits that either don't have 1 parent or don't have 1 child.
9498 # arcnos(id) - incoming arcs for BMP, arc we're on for other nodes
9499 # arcout(id) - outgoing arcs for BMP
9500 # arcids(a) - list of IDs on arc including end but not start
9501 # arcstart(a) - BMP ID at start of arc
9502 # arcend(a) - BMP ID at end of arc
9503 # growing(a) - arc a is still growing
9504 # arctags(a) - IDs out of arcids (excluding end) that have tags
9505 # archeads(a) - IDs out of arcids (excluding end) that have heads
9506 # The start of an arc is at the descendent end, so "incoming" means
9507 # coming from descendents, and "outgoing" means going towards ancestors.
9509 proc getallclines {fd} {
9510 global allparents allchildren idtags idheads nextarc
9511 global arcnos arcids arctags arcout arcend arcstart archeads growing
9512 global seeds allcommits cachedarcs allcupdate
9514 set nid 0
9515 while {[incr nid] <= 1000 && [gets $fd line] >= 0} {
9516 set id [lindex $line 0]
9517 if {[info exists allparents($id)]} {
9518 # seen it already
9519 continue
9521 set cachedarcs 0
9522 set olds [lrange $line 1 end]
9523 set allparents($id) $olds
9524 if {![info exists allchildren($id)]} {
9525 set allchildren($id) {}
9526 set arcnos($id) {}
9527 lappend seeds $id
9528 } else {
9529 set a $arcnos($id)
9530 if {[llength $olds] == 1 && [llength $a] == 1} {
9531 lappend arcids($a) $id
9532 if {[info exists idtags($id)]} {
9533 lappend arctags($a) $id
9535 if {[info exists idheads($id)]} {
9536 lappend archeads($a) $id
9538 if {[info exists allparents($olds)]} {
9539 # seen parent already
9540 if {![info exists arcout($olds)]} {
9541 splitarc $olds
9543 lappend arcids($a) $olds
9544 set arcend($a) $olds
9545 unset growing($a)
9547 lappend allchildren($olds) $id
9548 lappend arcnos($olds) $a
9549 continue
9552 foreach a $arcnos($id) {
9553 lappend arcids($a) $id
9554 set arcend($a) $id
9555 unset growing($a)
9558 set ao {}
9559 foreach p $olds {
9560 lappend allchildren($p) $id
9561 set a [incr nextarc]
9562 set arcstart($a) $id
9563 set archeads($a) {}
9564 set arctags($a) {}
9565 set archeads($a) {}
9566 set arcids($a) {}
9567 lappend ao $a
9568 set growing($a) 1
9569 if {[info exists allparents($p)]} {
9570 # seen it already, may need to make a new branch
9571 if {![info exists arcout($p)]} {
9572 splitarc $p
9574 lappend arcids($a) $p
9575 set arcend($a) $p
9576 unset growing($a)
9578 lappend arcnos($p) $a
9580 set arcout($id) $ao
9582 if {$nid > 0} {
9583 global cached_dheads cached_dtags cached_atags
9584 catch {unset cached_dheads}
9585 catch {unset cached_dtags}
9586 catch {unset cached_atags}
9588 if {![eof $fd]} {
9589 return [expr {$nid >= 1000? 2: 1}]
9591 set cacheok 1
9592 if {[catch {
9593 fconfigure $fd -blocking 1
9594 close $fd
9595 } err]} {
9596 # got an error reading the list of commits
9597 # if we were updating, try rereading the whole thing again
9598 if {$allcupdate} {
9599 incr allcommits -1
9600 dropcache $err
9601 return
9603 error_popup "[mc "Error reading commit topology information;\
9604 branch and preceding/following tag information\
9605 will be incomplete."]\n($err)"
9606 set cacheok 0
9608 if {[incr allcommits -1] == 0} {
9609 notbusy allcommits
9610 if {$cacheok} {
9611 run savecache
9614 dispneartags 0
9615 return 0
9618 proc recalcarc {a} {
9619 global arctags archeads arcids idtags idheads
9621 set at {}
9622 set ah {}
9623 foreach id [lrange $arcids($a) 0 end-1] {
9624 if {[info exists idtags($id)]} {
9625 lappend at $id
9627 if {[info exists idheads($id)]} {
9628 lappend ah $id
9631 set arctags($a) $at
9632 set archeads($a) $ah
9635 proc splitarc {p} {
9636 global arcnos arcids nextarc arctags archeads idtags idheads
9637 global arcstart arcend arcout allparents growing
9639 set a $arcnos($p)
9640 if {[llength $a] != 1} {
9641 puts "oops splitarc called but [llength $a] arcs already"
9642 return
9644 set a [lindex $a 0]
9645 set i [lsearch -exact $arcids($a) $p]
9646 if {$i < 0} {
9647 puts "oops splitarc $p not in arc $a"
9648 return
9650 set na [incr nextarc]
9651 if {[info exists arcend($a)]} {
9652 set arcend($na) $arcend($a)
9653 } else {
9654 set l [lindex $allparents([lindex $arcids($a) end]) 0]
9655 set j [lsearch -exact $arcnos($l) $a]
9656 set arcnos($l) [lreplace $arcnos($l) $j $j $na]
9658 set tail [lrange $arcids($a) [expr {$i+1}] end]
9659 set arcids($a) [lrange $arcids($a) 0 $i]
9660 set arcend($a) $p
9661 set arcstart($na) $p
9662 set arcout($p) $na
9663 set arcids($na) $tail
9664 if {[info exists growing($a)]} {
9665 set growing($na) 1
9666 unset growing($a)
9669 foreach id $tail {
9670 if {[llength $arcnos($id)] == 1} {
9671 set arcnos($id) $na
9672 } else {
9673 set j [lsearch -exact $arcnos($id) $a]
9674 set arcnos($id) [lreplace $arcnos($id) $j $j $na]
9678 # reconstruct tags and heads lists
9679 if {$arctags($a) ne {} || $archeads($a) ne {}} {
9680 recalcarc $a
9681 recalcarc $na
9682 } else {
9683 set arctags($na) {}
9684 set archeads($na) {}
9688 # Update things for a new commit added that is a child of one
9689 # existing commit. Used when cherry-picking.
9690 proc addnewchild {id p} {
9691 global allparents allchildren idtags nextarc
9692 global arcnos arcids arctags arcout arcend arcstart archeads growing
9693 global seeds allcommits
9695 if {![info exists allcommits] || ![info exists arcnos($p)]} return
9696 set allparents($id) [list $p]
9697 set allchildren($id) {}
9698 set arcnos($id) {}
9699 lappend seeds $id
9700 lappend allchildren($p) $id
9701 set a [incr nextarc]
9702 set arcstart($a) $id
9703 set archeads($a) {}
9704 set arctags($a) {}
9705 set arcids($a) [list $p]
9706 set arcend($a) $p
9707 if {![info exists arcout($p)]} {
9708 splitarc $p
9710 lappend arcnos($p) $a
9711 set arcout($id) [list $a]
9714 # This implements a cache for the topology information.
9715 # The cache saves, for each arc, the start and end of the arc,
9716 # the ids on the arc, and the outgoing arcs from the end.
9717 proc readcache {f} {
9718 global arcnos arcids arcout arcstart arcend arctags archeads nextarc
9719 global idtags idheads allparents cachedarcs possible_seeds seeds growing
9720 global allcwait
9722 set a $nextarc
9723 set lim $cachedarcs
9724 if {$lim - $a > 500} {
9725 set lim [expr {$a + 500}]
9727 if {[catch {
9728 if {$a == $lim} {
9729 # finish reading the cache and setting up arctags, etc.
9730 set line [gets $f]
9731 if {$line ne "1"} {error "bad final version"}
9732 close $f
9733 foreach id [array names idtags] {
9734 if {[info exists arcnos($id)] && [llength $arcnos($id)] == 1 &&
9735 [llength $allparents($id)] == 1} {
9736 set a [lindex $arcnos($id) 0]
9737 if {$arctags($a) eq {}} {
9738 recalcarc $a
9742 foreach id [array names idheads] {
9743 if {[info exists arcnos($id)] && [llength $arcnos($id)] == 1 &&
9744 [llength $allparents($id)] == 1} {
9745 set a [lindex $arcnos($id) 0]
9746 if {$archeads($a) eq {}} {
9747 recalcarc $a
9751 foreach id [lsort -unique $possible_seeds] {
9752 if {$arcnos($id) eq {}} {
9753 lappend seeds $id
9756 set allcwait 0
9757 } else {
9758 while {[incr a] <= $lim} {
9759 set line [gets $f]
9760 if {[llength $line] != 3} {error "bad line"}
9761 set s [lindex $line 0]
9762 set arcstart($a) $s
9763 lappend arcout($s) $a
9764 if {![info exists arcnos($s)]} {
9765 lappend possible_seeds $s
9766 set arcnos($s) {}
9768 set e [lindex $line 1]
9769 if {$e eq {}} {
9770 set growing($a) 1
9771 } else {
9772 set arcend($a) $e
9773 if {![info exists arcout($e)]} {
9774 set arcout($e) {}
9777 set arcids($a) [lindex $line 2]
9778 foreach id $arcids($a) {
9779 lappend allparents($s) $id
9780 set s $id
9781 lappend arcnos($id) $a
9783 if {![info exists allparents($s)]} {
9784 set allparents($s) {}
9786 set arctags($a) {}
9787 set archeads($a) {}
9789 set nextarc [expr {$a - 1}]
9791 } err]} {
9792 dropcache $err
9793 return 0
9795 if {!$allcwait} {
9796 getallcommits
9798 return $allcwait
9801 proc getcache {f} {
9802 global nextarc cachedarcs possible_seeds
9804 if {[catch {
9805 set line [gets $f]
9806 if {[llength $line] != 2 || [lindex $line 0] ne "1"} {error "bad version"}
9807 # make sure it's an integer
9808 set cachedarcs [expr {int([lindex $line 1])}]
9809 if {$cachedarcs < 0} {error "bad number of arcs"}
9810 set nextarc 0
9811 set possible_seeds {}
9812 run readcache $f
9813 } err]} {
9814 dropcache $err
9816 return 0
9819 proc dropcache {err} {
9820 global allcwait nextarc cachedarcs seeds
9822 #puts "dropping cache ($err)"
9823 foreach v {arcnos arcout arcids arcstart arcend growing \
9824 arctags archeads allparents allchildren} {
9825 global $v
9826 catch {unset $v}
9828 set allcwait 0
9829 set nextarc 0
9830 set cachedarcs 0
9831 set seeds {}
9832 getallcommits
9835 proc writecache {f} {
9836 global cachearc cachedarcs allccache
9837 global arcstart arcend arcnos arcids arcout
9839 set a $cachearc
9840 set lim $cachedarcs
9841 if {$lim - $a > 1000} {
9842 set lim [expr {$a + 1000}]
9844 if {[catch {
9845 while {[incr a] <= $lim} {
9846 if {[info exists arcend($a)]} {
9847 puts $f [list $arcstart($a) $arcend($a) $arcids($a)]
9848 } else {
9849 puts $f [list $arcstart($a) {} $arcids($a)]
9852 } err]} {
9853 catch {close $f}
9854 catch {file delete $allccache}
9855 #puts "writing cache failed ($err)"
9856 return 0
9858 set cachearc [expr {$a - 1}]
9859 if {$a > $cachedarcs} {
9860 puts $f "1"
9861 close $f
9862 return 0
9864 return 1
9867 proc savecache {} {
9868 global nextarc cachedarcs cachearc allccache
9870 if {$nextarc == $cachedarcs} return
9871 set cachearc 0
9872 set cachedarcs $nextarc
9873 catch {
9874 set f [open $allccache w]
9875 puts $f [list 1 $cachedarcs]
9876 run writecache $f
9880 # Returns 1 if a is an ancestor of b, -1 if b is an ancestor of a,
9881 # or 0 if neither is true.
9882 proc anc_or_desc {a b} {
9883 global arcout arcstart arcend arcnos cached_isanc
9885 if {$arcnos($a) eq $arcnos($b)} {
9886 # Both are on the same arc(s); either both are the same BMP,
9887 # or if one is not a BMP, the other is also not a BMP or is
9888 # the BMP at end of the arc (and it only has 1 incoming arc).
9889 # Or both can be BMPs with no incoming arcs.
9890 if {$a eq $b || $arcnos($a) eq {}} {
9891 return 0
9893 # assert {[llength $arcnos($a)] == 1}
9894 set arc [lindex $arcnos($a) 0]
9895 set i [lsearch -exact $arcids($arc) $a]
9896 set j [lsearch -exact $arcids($arc) $b]
9897 if {$i < 0 || $i > $j} {
9898 return 1
9899 } else {
9900 return -1
9904 if {![info exists arcout($a)]} {
9905 set arc [lindex $arcnos($a) 0]
9906 if {[info exists arcend($arc)]} {
9907 set aend $arcend($arc)
9908 } else {
9909 set aend {}
9911 set a $arcstart($arc)
9912 } else {
9913 set aend $a
9915 if {![info exists arcout($b)]} {
9916 set arc [lindex $arcnos($b) 0]
9917 if {[info exists arcend($arc)]} {
9918 set bend $arcend($arc)
9919 } else {
9920 set bend {}
9922 set b $arcstart($arc)
9923 } else {
9924 set bend $b
9926 if {$a eq $bend} {
9927 return 1
9929 if {$b eq $aend} {
9930 return -1
9932 if {[info exists cached_isanc($a,$bend)]} {
9933 if {$cached_isanc($a,$bend)} {
9934 return 1
9937 if {[info exists cached_isanc($b,$aend)]} {
9938 if {$cached_isanc($b,$aend)} {
9939 return -1
9941 if {[info exists cached_isanc($a,$bend)]} {
9942 return 0
9946 set todo [list $a $b]
9947 set anc($a) a
9948 set anc($b) b
9949 for {set i 0} {$i < [llength $todo]} {incr i} {
9950 set x [lindex $todo $i]
9951 if {$anc($x) eq {}} {
9952 continue
9954 foreach arc $arcnos($x) {
9955 set xd $arcstart($arc)
9956 if {$xd eq $bend} {
9957 set cached_isanc($a,$bend) 1
9958 set cached_isanc($b,$aend) 0
9959 return 1
9960 } elseif {$xd eq $aend} {
9961 set cached_isanc($b,$aend) 1
9962 set cached_isanc($a,$bend) 0
9963 return -1
9965 if {![info exists anc($xd)]} {
9966 set anc($xd) $anc($x)
9967 lappend todo $xd
9968 } elseif {$anc($xd) ne $anc($x)} {
9969 set anc($xd) {}
9973 set cached_isanc($a,$bend) 0
9974 set cached_isanc($b,$aend) 0
9975 return 0
9978 # This identifies whether $desc has an ancestor that is
9979 # a growing tip of the graph and which is not an ancestor of $anc
9980 # and returns 0 if so and 1 if not.
9981 # If we subsequently discover a tag on such a growing tip, and that
9982 # turns out to be a descendent of $anc (which it could, since we
9983 # don't necessarily see children before parents), then $desc
9984 # isn't a good choice to display as a descendent tag of
9985 # $anc (since it is the descendent of another tag which is
9986 # a descendent of $anc). Similarly, $anc isn't a good choice to
9987 # display as a ancestor tag of $desc.
9989 proc is_certain {desc anc} {
9990 global arcnos arcout arcstart arcend growing problems
9992 set certain {}
9993 if {[llength $arcnos($anc)] == 1} {
9994 # tags on the same arc are certain
9995 if {$arcnos($desc) eq $arcnos($anc)} {
9996 return 1
9998 if {![info exists arcout($anc)]} {
9999 # if $anc is partway along an arc, use the start of the arc instead
10000 set a [lindex $arcnos($anc) 0]
10001 set anc $arcstart($a)
10004 if {[llength $arcnos($desc)] > 1 || [info exists arcout($desc)]} {
10005 set x $desc
10006 } else {
10007 set a [lindex $arcnos($desc) 0]
10008 set x $arcend($a)
10010 if {$x == $anc} {
10011 return 1
10013 set anclist [list $x]
10014 set dl($x) 1
10015 set nnh 1
10016 set ngrowanc 0
10017 for {set i 0} {$i < [llength $anclist] && ($nnh > 0 || $ngrowanc > 0)} {incr i} {
10018 set x [lindex $anclist $i]
10019 if {$dl($x)} {
10020 incr nnh -1
10022 set done($x) 1
10023 foreach a $arcout($x) {
10024 if {[info exists growing($a)]} {
10025 if {![info exists growanc($x)] && $dl($x)} {
10026 set growanc($x) 1
10027 incr ngrowanc
10029 } else {
10030 set y $arcend($a)
10031 if {[info exists dl($y)]} {
10032 if {$dl($y)} {
10033 if {!$dl($x)} {
10034 set dl($y) 0
10035 if {![info exists done($y)]} {
10036 incr nnh -1
10038 if {[info exists growanc($x)]} {
10039 incr ngrowanc -1
10041 set xl [list $y]
10042 for {set k 0} {$k < [llength $xl]} {incr k} {
10043 set z [lindex $xl $k]
10044 foreach c $arcout($z) {
10045 if {[info exists arcend($c)]} {
10046 set v $arcend($c)
10047 if {[info exists dl($v)] && $dl($v)} {
10048 set dl($v) 0
10049 if {![info exists done($v)]} {
10050 incr nnh -1
10052 if {[info exists growanc($v)]} {
10053 incr ngrowanc -1
10055 lappend xl $v
10062 } elseif {$y eq $anc || !$dl($x)} {
10063 set dl($y) 0
10064 lappend anclist $y
10065 } else {
10066 set dl($y) 1
10067 lappend anclist $y
10068 incr nnh
10073 foreach x [array names growanc] {
10074 if {$dl($x)} {
10075 return 0
10077 return 0
10079 return 1
10082 proc validate_arctags {a} {
10083 global arctags idtags
10085 set i -1
10086 set na $arctags($a)
10087 foreach id $arctags($a) {
10088 incr i
10089 if {![info exists idtags($id)]} {
10090 set na [lreplace $na $i $i]
10091 incr i -1
10094 set arctags($a) $na
10097 proc validate_archeads {a} {
10098 global archeads idheads
10100 set i -1
10101 set na $archeads($a)
10102 foreach id $archeads($a) {
10103 incr i
10104 if {![info exists idheads($id)]} {
10105 set na [lreplace $na $i $i]
10106 incr i -1
10109 set archeads($a) $na
10112 # Return the list of IDs that have tags that are descendents of id,
10113 # ignoring IDs that are descendents of IDs already reported.
10114 proc desctags {id} {
10115 global arcnos arcstart arcids arctags idtags allparents
10116 global growing cached_dtags
10118 if {![info exists allparents($id)]} {
10119 return {}
10121 set t1 [clock clicks -milliseconds]
10122 set argid $id
10123 if {[llength $arcnos($id)] == 1 && [llength $allparents($id)] == 1} {
10124 # part-way along an arc; check that arc first
10125 set a [lindex $arcnos($id) 0]
10126 if {$arctags($a) ne {}} {
10127 validate_arctags $a
10128 set i [lsearch -exact $arcids($a) $id]
10129 set tid {}
10130 foreach t $arctags($a) {
10131 set j [lsearch -exact $arcids($a) $t]
10132 if {$j >= $i} break
10133 set tid $t
10135 if {$tid ne {}} {
10136 return $tid
10139 set id $arcstart($a)
10140 if {[info exists idtags($id)]} {
10141 return $id
10144 if {[info exists cached_dtags($id)]} {
10145 return $cached_dtags($id)
10148 set origid $id
10149 set todo [list $id]
10150 set queued($id) 1
10151 set nc 1
10152 for {set i 0} {$i < [llength $todo] && $nc > 0} {incr i} {
10153 set id [lindex $todo $i]
10154 set done($id) 1
10155 set ta [info exists hastaggedancestor($id)]
10156 if {!$ta} {
10157 incr nc -1
10159 # ignore tags on starting node
10160 if {!$ta && $i > 0} {
10161 if {[info exists idtags($id)]} {
10162 set tagloc($id) $id
10163 set ta 1
10164 } elseif {[info exists cached_dtags($id)]} {
10165 set tagloc($id) $cached_dtags($id)
10166 set ta 1
10169 foreach a $arcnos($id) {
10170 set d $arcstart($a)
10171 if {!$ta && $arctags($a) ne {}} {
10172 validate_arctags $a
10173 if {$arctags($a) ne {}} {
10174 lappend tagloc($id) [lindex $arctags($a) end]
10177 if {$ta || $arctags($a) ne {}} {
10178 set tomark [list $d]
10179 for {set j 0} {$j < [llength $tomark]} {incr j} {
10180 set dd [lindex $tomark $j]
10181 if {![info exists hastaggedancestor($dd)]} {
10182 if {[info exists done($dd)]} {
10183 foreach b $arcnos($dd) {
10184 lappend tomark $arcstart($b)
10186 if {[info exists tagloc($dd)]} {
10187 unset tagloc($dd)
10189 } elseif {[info exists queued($dd)]} {
10190 incr nc -1
10192 set hastaggedancestor($dd) 1
10196 if {![info exists queued($d)]} {
10197 lappend todo $d
10198 set queued($d) 1
10199 if {![info exists hastaggedancestor($d)]} {
10200 incr nc
10205 set tags {}
10206 foreach id [array names tagloc] {
10207 if {![info exists hastaggedancestor($id)]} {
10208 foreach t $tagloc($id) {
10209 if {[lsearch -exact $tags $t] < 0} {
10210 lappend tags $t
10215 set t2 [clock clicks -milliseconds]
10216 set loopix $i
10218 # remove tags that are descendents of other tags
10219 for {set i 0} {$i < [llength $tags]} {incr i} {
10220 set a [lindex $tags $i]
10221 for {set j 0} {$j < $i} {incr j} {
10222 set b [lindex $tags $j]
10223 set r [anc_or_desc $a $b]
10224 if {$r == 1} {
10225 set tags [lreplace $tags $j $j]
10226 incr j -1
10227 incr i -1
10228 } elseif {$r == -1} {
10229 set tags [lreplace $tags $i $i]
10230 incr i -1
10231 break
10236 if {[array names growing] ne {}} {
10237 # graph isn't finished, need to check if any tag could get
10238 # eclipsed by another tag coming later. Simply ignore any
10239 # tags that could later get eclipsed.
10240 set ctags {}
10241 foreach t $tags {
10242 if {[is_certain $t $origid]} {
10243 lappend ctags $t
10246 if {$tags eq $ctags} {
10247 set cached_dtags($origid) $tags
10248 } else {
10249 set tags $ctags
10251 } else {
10252 set cached_dtags($origid) $tags
10254 set t3 [clock clicks -milliseconds]
10255 if {0 && $t3 - $t1 >= 100} {
10256 puts "iterating descendents ($loopix/[llength $todo] nodes) took\
10257 [expr {$t2-$t1}]+[expr {$t3-$t2}]ms, $nc candidates left"
10259 return $tags
10262 proc anctags {id} {
10263 global arcnos arcids arcout arcend arctags idtags allparents
10264 global growing cached_atags
10266 if {![info exists allparents($id)]} {
10267 return {}
10269 set t1 [clock clicks -milliseconds]
10270 set argid $id
10271 if {[llength $arcnos($id)] == 1 && [llength $allparents($id)] == 1} {
10272 # part-way along an arc; check that arc first
10273 set a [lindex $arcnos($id) 0]
10274 if {$arctags($a) ne {}} {
10275 validate_arctags $a
10276 set i [lsearch -exact $arcids($a) $id]
10277 foreach t $arctags($a) {
10278 set j [lsearch -exact $arcids($a) $t]
10279 if {$j > $i} {
10280 return $t
10284 if {![info exists arcend($a)]} {
10285 return {}
10287 set id $arcend($a)
10288 if {[info exists idtags($id)]} {
10289 return $id
10292 if {[info exists cached_atags($id)]} {
10293 return $cached_atags($id)
10296 set origid $id
10297 set todo [list $id]
10298 set queued($id) 1
10299 set taglist {}
10300 set nc 1
10301 for {set i 0} {$i < [llength $todo] && $nc > 0} {incr i} {
10302 set id [lindex $todo $i]
10303 set done($id) 1
10304 set td [info exists hastaggeddescendent($id)]
10305 if {!$td} {
10306 incr nc -1
10308 # ignore tags on starting node
10309 if {!$td && $i > 0} {
10310 if {[info exists idtags($id)]} {
10311 set tagloc($id) $id
10312 set td 1
10313 } elseif {[info exists cached_atags($id)]} {
10314 set tagloc($id) $cached_atags($id)
10315 set td 1
10318 foreach a $arcout($id) {
10319 if {!$td && $arctags($a) ne {}} {
10320 validate_arctags $a
10321 if {$arctags($a) ne {}} {
10322 lappend tagloc($id) [lindex $arctags($a) 0]
10325 if {![info exists arcend($a)]} continue
10326 set d $arcend($a)
10327 if {$td || $arctags($a) ne {}} {
10328 set tomark [list $d]
10329 for {set j 0} {$j < [llength $tomark]} {incr j} {
10330 set dd [lindex $tomark $j]
10331 if {![info exists hastaggeddescendent($dd)]} {
10332 if {[info exists done($dd)]} {
10333 foreach b $arcout($dd) {
10334 if {[info exists arcend($b)]} {
10335 lappend tomark $arcend($b)
10338 if {[info exists tagloc($dd)]} {
10339 unset tagloc($dd)
10341 } elseif {[info exists queued($dd)]} {
10342 incr nc -1
10344 set hastaggeddescendent($dd) 1
10348 if {![info exists queued($d)]} {
10349 lappend todo $d
10350 set queued($d) 1
10351 if {![info exists hastaggeddescendent($d)]} {
10352 incr nc
10357 set t2 [clock clicks -milliseconds]
10358 set loopix $i
10359 set tags {}
10360 foreach id [array names tagloc] {
10361 if {![info exists hastaggeddescendent($id)]} {
10362 foreach t $tagloc($id) {
10363 if {[lsearch -exact $tags $t] < 0} {
10364 lappend tags $t
10370 # remove tags that are ancestors of other tags
10371 for {set i 0} {$i < [llength $tags]} {incr i} {
10372 set a [lindex $tags $i]
10373 for {set j 0} {$j < $i} {incr j} {
10374 set b [lindex $tags $j]
10375 set r [anc_or_desc $a $b]
10376 if {$r == -1} {
10377 set tags [lreplace $tags $j $j]
10378 incr j -1
10379 incr i -1
10380 } elseif {$r == 1} {
10381 set tags [lreplace $tags $i $i]
10382 incr i -1
10383 break
10388 if {[array names growing] ne {}} {
10389 # graph isn't finished, need to check if any tag could get
10390 # eclipsed by another tag coming later. Simply ignore any
10391 # tags that could later get eclipsed.
10392 set ctags {}
10393 foreach t $tags {
10394 if {[is_certain $origid $t]} {
10395 lappend ctags $t
10398 if {$tags eq $ctags} {
10399 set cached_atags($origid) $tags
10400 } else {
10401 set tags $ctags
10403 } else {
10404 set cached_atags($origid) $tags
10406 set t3 [clock clicks -milliseconds]
10407 if {0 && $t3 - $t1 >= 100} {
10408 puts "iterating ancestors ($loopix/[llength $todo] nodes) took\
10409 [expr {$t2-$t1}]+[expr {$t3-$t2}]ms, $nc candidates left"
10411 return $tags
10414 # Return the list of IDs that have heads that are descendents of id,
10415 # including id itself if it has a head.
10416 proc descheads {id} {
10417 global arcnos arcstart arcids archeads idheads cached_dheads
10418 global allparents
10420 if {![info exists allparents($id)]} {
10421 return {}
10423 set aret {}
10424 if {[llength $arcnos($id)] == 1 && [llength $allparents($id)] == 1} {
10425 # part-way along an arc; check it first
10426 set a [lindex $arcnos($id) 0]
10427 if {$archeads($a) ne {}} {
10428 validate_archeads $a
10429 set i [lsearch -exact $arcids($a) $id]
10430 foreach t $archeads($a) {
10431 set j [lsearch -exact $arcids($a) $t]
10432 if {$j > $i} break
10433 lappend aret $t
10436 set id $arcstart($a)
10438 set origid $id
10439 set todo [list $id]
10440 set seen($id) 1
10441 set ret {}
10442 for {set i 0} {$i < [llength $todo]} {incr i} {
10443 set id [lindex $todo $i]
10444 if {[info exists cached_dheads($id)]} {
10445 set ret [concat $ret $cached_dheads($id)]
10446 } else {
10447 if {[info exists idheads($id)]} {
10448 lappend ret $id
10450 foreach a $arcnos($id) {
10451 if {$archeads($a) ne {}} {
10452 validate_archeads $a
10453 if {$archeads($a) ne {}} {
10454 set ret [concat $ret $archeads($a)]
10457 set d $arcstart($a)
10458 if {![info exists seen($d)]} {
10459 lappend todo $d
10460 set seen($d) 1
10465 set ret [lsort -unique $ret]
10466 set cached_dheads($origid) $ret
10467 return [concat $ret $aret]
10470 proc addedtag {id} {
10471 global arcnos arcout cached_dtags cached_atags
10473 if {![info exists arcnos($id)]} return
10474 if {![info exists arcout($id)]} {
10475 recalcarc [lindex $arcnos($id) 0]
10477 catch {unset cached_dtags}
10478 catch {unset cached_atags}
10481 proc addedhead {hid head} {
10482 global arcnos arcout cached_dheads
10484 if {![info exists arcnos($hid)]} return
10485 if {![info exists arcout($hid)]} {
10486 recalcarc [lindex $arcnos($hid) 0]
10488 catch {unset cached_dheads}
10491 proc removedhead {hid head} {
10492 global cached_dheads
10494 catch {unset cached_dheads}
10497 proc movedhead {hid head} {
10498 global arcnos arcout cached_dheads
10500 if {![info exists arcnos($hid)]} return
10501 if {![info exists arcout($hid)]} {
10502 recalcarc [lindex $arcnos($hid) 0]
10504 catch {unset cached_dheads}
10507 proc changedrefs {} {
10508 global cached_dheads cached_dtags cached_atags
10509 global arctags archeads arcnos arcout idheads idtags
10511 foreach id [concat [array names idheads] [array names idtags]] {
10512 if {[info exists arcnos($id)] && ![info exists arcout($id)]} {
10513 set a [lindex $arcnos($id) 0]
10514 if {![info exists donearc($a)]} {
10515 recalcarc $a
10516 set donearc($a) 1
10520 catch {unset cached_dtags}
10521 catch {unset cached_atags}
10522 catch {unset cached_dheads}
10525 proc rereadrefs {} {
10526 global idtags idheads idotherrefs mainheadid
10528 set refids [concat [array names idtags] \
10529 [array names idheads] [array names idotherrefs]]
10530 foreach id $refids {
10531 if {![info exists ref($id)]} {
10532 set ref($id) [listrefs $id]
10535 set oldmainhead $mainheadid
10536 readrefs
10537 changedrefs
10538 set refids [lsort -unique [concat $refids [array names idtags] \
10539 [array names idheads] [array names idotherrefs]]]
10540 foreach id $refids {
10541 set v [listrefs $id]
10542 if {![info exists ref($id)] || $ref($id) != $v} {
10543 redrawtags $id
10546 if {$oldmainhead ne $mainheadid} {
10547 redrawtags $oldmainhead
10548 redrawtags $mainheadid
10550 run refill_reflist
10553 proc listrefs {id} {
10554 global idtags idheads idotherrefs
10556 set x {}
10557 if {[info exists idtags($id)]} {
10558 set x $idtags($id)
10560 set y {}
10561 if {[info exists idheads($id)]} {
10562 set y $idheads($id)
10564 set z {}
10565 if {[info exists idotherrefs($id)]} {
10566 set z $idotherrefs($id)
10568 return [list $x $y $z]
10571 proc showtag {tag isnew} {
10572 global ctext tagcontents tagids linknum tagobjid
10574 if {$isnew} {
10575 addtohistory [list showtag $tag 0] savectextpos
10577 $ctext conf -state normal
10578 clear_ctext
10579 settabs 0
10580 set linknum 0
10581 if {![info exists tagcontents($tag)]} {
10582 catch {
10583 set tagcontents($tag) [exec git cat-file tag $tag]
10586 if {[info exists tagcontents($tag)]} {
10587 set text $tagcontents($tag)
10588 } else {
10589 set text "[mc "Tag"]: $tag\n[mc "Id"]: $tagids($tag)"
10591 appendwithlinks $text {}
10592 maybe_scroll_ctext 1
10593 $ctext conf -state disabled
10594 init_flist {}
10597 proc doquit {} {
10598 global stopped
10599 global gitktmpdir
10601 set stopped 100
10602 savestuff .
10603 destroy .
10605 if {[info exists gitktmpdir]} {
10606 catch {file delete -force $gitktmpdir}
10610 proc mkfontdisp {font top which} {
10611 global fontattr fontpref $font NS use_ttk
10613 set fontpref($font) [set $font]
10614 ${NS}::button $top.${font}but -text $which \
10615 -command [list choosefont $font $which]
10616 ${NS}::label $top.$font -relief flat -font $font \
10617 -text $fontattr($font,family) -justify left
10618 grid x $top.${font}but $top.$font -sticky w
10621 proc choosefont {font which} {
10622 global fontparam fontlist fonttop fontattr
10623 global prefstop NS
10625 set fontparam(which) $which
10626 set fontparam(font) $font
10627 set fontparam(family) [font actual $font -family]
10628 set fontparam(size) $fontattr($font,size)
10629 set fontparam(weight) $fontattr($font,weight)
10630 set fontparam(slant) $fontattr($font,slant)
10631 set top .gitkfont
10632 set fonttop $top
10633 if {![winfo exists $top]} {
10634 font create sample
10635 eval font config sample [font actual $font]
10636 ttk_toplevel $top
10637 make_transient $top $prefstop
10638 wm title $top [mc "Gitk font chooser"]
10639 ${NS}::label $top.l -textvariable fontparam(which)
10640 pack $top.l -side top
10641 set fontlist [lsort [font families]]
10642 ${NS}::frame $top.f
10643 listbox $top.f.fam -listvariable fontlist \
10644 -yscrollcommand [list $top.f.sb set]
10645 bind $top.f.fam <<ListboxSelect>> selfontfam
10646 ${NS}::scrollbar $top.f.sb -command [list $top.f.fam yview]
10647 pack $top.f.sb -side right -fill y
10648 pack $top.f.fam -side left -fill both -expand 1
10649 pack $top.f -side top -fill both -expand 1
10650 ${NS}::frame $top.g
10651 spinbox $top.g.size -from 4 -to 40 -width 4 \
10652 -textvariable fontparam(size) \
10653 -validatecommand {string is integer -strict %s}
10654 checkbutton $top.g.bold -padx 5 \
10655 -font {{Times New Roman} 12 bold} -text [mc "B"] -indicatoron 0 \
10656 -variable fontparam(weight) -onvalue bold -offvalue normal
10657 checkbutton $top.g.ital -padx 5 \
10658 -font {{Times New Roman} 12 italic} -text [mc "I"] -indicatoron 0 \
10659 -variable fontparam(slant) -onvalue italic -offvalue roman
10660 pack $top.g.size $top.g.bold $top.g.ital -side left
10661 pack $top.g -side top
10662 canvas $top.c -width 150 -height 50 -border 2 -relief sunk \
10663 -background white
10664 $top.c create text 100 25 -anchor center -text $which -font sample \
10665 -fill black -tags text
10666 bind $top.c <Configure> [list centertext $top.c]
10667 pack $top.c -side top -fill x
10668 ${NS}::frame $top.buts
10669 ${NS}::button $top.buts.ok -text [mc "OK"] -command fontok -default active
10670 ${NS}::button $top.buts.can -text [mc "Cancel"] -command fontcan -default normal
10671 bind $top <Key-Return> fontok
10672 bind $top <Key-Escape> fontcan
10673 grid $top.buts.ok $top.buts.can
10674 grid columnconfigure $top.buts 0 -weight 1 -uniform a
10675 grid columnconfigure $top.buts 1 -weight 1 -uniform a
10676 pack $top.buts -side bottom -fill x
10677 trace add variable fontparam write chg_fontparam
10678 } else {
10679 raise $top
10680 $top.c itemconf text -text $which
10682 set i [lsearch -exact $fontlist $fontparam(family)]
10683 if {$i >= 0} {
10684 $top.f.fam selection set $i
10685 $top.f.fam see $i
10689 proc centertext {w} {
10690 $w coords text [expr {[winfo width $w] / 2}] [expr {[winfo height $w] / 2}]
10693 proc fontok {} {
10694 global fontparam fontpref prefstop
10696 set f $fontparam(font)
10697 set fontpref($f) [list $fontparam(family) $fontparam(size)]
10698 if {$fontparam(weight) eq "bold"} {
10699 lappend fontpref($f) "bold"
10701 if {$fontparam(slant) eq "italic"} {
10702 lappend fontpref($f) "italic"
10704 set w $prefstop.$f
10705 $w conf -text $fontparam(family) -font $fontpref($f)
10707 fontcan
10710 proc fontcan {} {
10711 global fonttop fontparam
10713 if {[info exists fonttop]} {
10714 catch {destroy $fonttop}
10715 catch {font delete sample}
10716 unset fonttop
10717 unset fontparam
10721 if {[package vsatisfies [package provide Tk] 8.6]} {
10722 # In Tk 8.6 we have a native font chooser dialog. Overwrite the above
10723 # function to make use of it.
10724 proc choosefont {font which} {
10725 tk fontchooser configure -title $which -font $font \
10726 -command [list on_choosefont $font $which]
10727 tk fontchooser show
10729 proc on_choosefont {font which newfont} {
10730 global fontparam
10731 puts stderr "$font $newfont"
10732 array set f [font actual $newfont]
10733 set fontparam(which) $which
10734 set fontparam(font) $font
10735 set fontparam(family) $f(-family)
10736 set fontparam(size) $f(-size)
10737 set fontparam(weight) $f(-weight)
10738 set fontparam(slant) $f(-slant)
10739 fontok
10743 proc selfontfam {} {
10744 global fonttop fontparam
10746 set i [$fonttop.f.fam curselection]
10747 if {$i ne {}} {
10748 set fontparam(family) [$fonttop.f.fam get $i]
10752 proc chg_fontparam {v sub op} {
10753 global fontparam
10755 font config sample -$sub $fontparam($sub)
10758 proc doprefs {} {
10759 global maxwidth maxgraphpct use_ttk NS
10760 global oldprefs prefstop showneartags showlocalchanges
10761 global uicolor bgcolor fgcolor ctext diffcolors selectbgcolor markbgcolor
10762 global tabstop limitdiffs autoselect autosellen extdifftool perfile_attrs
10763 global hideremotes want_ttk have_ttk
10765 set top .gitkprefs
10766 set prefstop $top
10767 if {[winfo exists $top]} {
10768 raise $top
10769 return
10771 foreach v {maxwidth maxgraphpct showneartags showlocalchanges \
10772 limitdiffs tabstop perfile_attrs hideremotes want_ttk} {
10773 set oldprefs($v) [set $v]
10775 ttk_toplevel $top
10776 wm title $top [mc "Gitk preferences"]
10777 make_transient $top .
10778 ${NS}::label $top.ldisp -text [mc "Commit list display options"]
10779 grid $top.ldisp - -sticky w -pady 10
10780 ${NS}::label $top.spacer -text " "
10781 ${NS}::label $top.maxwidthl -text [mc "Maximum graph width (lines)"]
10782 spinbox $top.maxwidth -from 0 -to 100 -width 4 -textvariable maxwidth
10783 grid $top.spacer $top.maxwidthl $top.maxwidth -sticky w
10784 ${NS}::label $top.maxpctl -text [mc "Maximum graph width (% of pane)"]
10785 spinbox $top.maxpct -from 1 -to 100 -width 4 -textvariable maxgraphpct
10786 grid x $top.maxpctl $top.maxpct -sticky w
10787 ${NS}::checkbutton $top.showlocal -text [mc "Show local changes"] \
10788 -variable showlocalchanges
10789 grid x $top.showlocal -sticky w
10790 ${NS}::checkbutton $top.autoselect -text [mc "Auto-select SHA1 (length)"] \
10791 -variable autoselect
10792 spinbox $top.autosellen -from 1 -to 40 -width 4 -textvariable autosellen
10793 grid x $top.autoselect $top.autosellen -sticky w
10794 ${NS}::checkbutton $top.hideremotes -text [mc "Hide remote refs"] \
10795 -variable hideremotes
10796 grid x $top.hideremotes -sticky w
10798 ${NS}::label $top.ddisp -text [mc "Diff display options"]
10799 grid $top.ddisp - -sticky w -pady 10
10800 ${NS}::label $top.tabstopl -text [mc "Tab spacing"]
10801 spinbox $top.tabstop -from 1 -to 20 -width 4 -textvariable tabstop
10802 grid x $top.tabstopl $top.tabstop -sticky w
10803 ${NS}::checkbutton $top.ntag -text [mc "Display nearby tags"] \
10804 -variable showneartags
10805 grid x $top.ntag -sticky w
10806 ${NS}::checkbutton $top.ldiff -text [mc "Limit diffs to listed paths"] \
10807 -variable limitdiffs
10808 grid x $top.ldiff -sticky w
10809 ${NS}::checkbutton $top.lattr -text [mc "Support per-file encodings"] \
10810 -variable perfile_attrs
10811 grid x $top.lattr -sticky w
10813 ${NS}::entry $top.extdifft -textvariable extdifftool
10814 ${NS}::frame $top.extdifff
10815 ${NS}::label $top.extdifff.l -text [mc "External diff tool" ]
10816 ${NS}::button $top.extdifff.b -text [mc "Choose..."] -command choose_extdiff
10817 pack $top.extdifff.l $top.extdifff.b -side left
10818 pack configure $top.extdifff.l -padx 10
10819 grid x $top.extdifff $top.extdifft -sticky ew
10821 ${NS}::label $top.lgen -text [mc "General options"]
10822 grid $top.lgen - -sticky w -pady 10
10823 ${NS}::checkbutton $top.want_ttk -variable want_ttk \
10824 -text [mc "Use themed widgets"]
10825 if {$have_ttk} {
10826 ${NS}::label $top.ttk_note -text [mc "(change requires restart)"]
10827 } else {
10828 ${NS}::label $top.ttk_note -text [mc "(currently unavailable)"]
10830 grid x $top.want_ttk $top.ttk_note -sticky w
10832 ${NS}::label $top.cdisp -text [mc "Colors: press to choose"]
10833 grid $top.cdisp - -sticky w -pady 10
10834 label $top.ui -padx 40 -relief sunk -background $uicolor
10835 ${NS}::button $top.uibut -text [mc "Interface"] \
10836 -command [list choosecolor uicolor {} $top.ui [mc "interface"] setui]
10837 grid x $top.uibut $top.ui -sticky w
10838 label $top.bg -padx 40 -relief sunk -background $bgcolor
10839 ${NS}::button $top.bgbut -text [mc "Background"] \
10840 -command [list choosecolor bgcolor {} $top.bg [mc "background"] setbg]
10841 grid x $top.bgbut $top.bg -sticky w
10842 label $top.fg -padx 40 -relief sunk -background $fgcolor
10843 ${NS}::button $top.fgbut -text [mc "Foreground"] \
10844 -command [list choosecolor fgcolor {} $top.fg [mc "foreground"] setfg]
10845 grid x $top.fgbut $top.fg -sticky w
10846 label $top.diffold -padx 40 -relief sunk -background [lindex $diffcolors 0]
10847 ${NS}::button $top.diffoldbut -text [mc "Diff: old lines"] \
10848 -command [list choosecolor diffcolors 0 $top.diffold [mc "diff old lines"] \
10849 [list $ctext tag conf d0 -foreground]]
10850 grid x $top.diffoldbut $top.diffold -sticky w
10851 label $top.diffnew -padx 40 -relief sunk -background [lindex $diffcolors 1]
10852 ${NS}::button $top.diffnewbut -text [mc "Diff: new lines"] \
10853 -command [list choosecolor diffcolors 1 $top.diffnew [mc "diff new lines"] \
10854 [list $ctext tag conf dresult -foreground]]
10855 grid x $top.diffnewbut $top.diffnew -sticky w
10856 label $top.hunksep -padx 40 -relief sunk -background [lindex $diffcolors 2]
10857 ${NS}::button $top.hunksepbut -text [mc "Diff: hunk header"] \
10858 -command [list choosecolor diffcolors 2 $top.hunksep \
10859 [mc "diff hunk header"] \
10860 [list $ctext tag conf hunksep -foreground]]
10861 grid x $top.hunksepbut $top.hunksep -sticky w
10862 label $top.markbgsep -padx 40 -relief sunk -background $markbgcolor
10863 ${NS}::button $top.markbgbut -text [mc "Marked line bg"] \
10864 -command [list choosecolor markbgcolor {} $top.markbgsep \
10865 [mc "marked line background"] \
10866 [list $ctext tag conf omark -background]]
10867 grid x $top.markbgbut $top.markbgsep -sticky w
10868 label $top.selbgsep -padx 40 -relief sunk -background $selectbgcolor
10869 ${NS}::button $top.selbgbut -text [mc "Select bg"] \
10870 -command [list choosecolor selectbgcolor {} $top.selbgsep [mc "background"] setselbg]
10871 grid x $top.selbgbut $top.selbgsep -sticky w
10873 ${NS}::label $top.cfont -text [mc "Fonts: press to choose"]
10874 grid $top.cfont - -sticky w -pady 10
10875 mkfontdisp mainfont $top [mc "Main font"]
10876 mkfontdisp textfont $top [mc "Diff display font"]
10877 mkfontdisp uifont $top [mc "User interface font"]
10879 ${NS}::frame $top.buts
10880 ${NS}::button $top.buts.ok -text [mc "OK"] -command prefsok -default active
10881 ${NS}::button $top.buts.can -text [mc "Cancel"] -command prefscan -default normal
10882 bind $top <Key-Return> prefsok
10883 bind $top <Key-Escape> prefscan
10884 grid $top.buts.ok $top.buts.can
10885 grid columnconfigure $top.buts 0 -weight 1 -uniform a
10886 grid columnconfigure $top.buts 1 -weight 1 -uniform a
10887 grid $top.buts - - -pady 10 -sticky ew
10888 grid columnconfigure $top 2 -weight 1
10889 bind $top <Visibility> "focus $top.buts.ok"
10892 proc choose_extdiff {} {
10893 global extdifftool
10895 set prog [tk_getOpenFile -title [mc "External diff tool"] -multiple false]
10896 if {$prog ne {}} {
10897 set extdifftool $prog
10901 proc choosecolor {v vi w x cmd} {
10902 global $v
10904 set c [tk_chooseColor -initialcolor [lindex [set $v] $vi] \
10905 -title [mc "Gitk: choose color for %s" $x]]
10906 if {$c eq {}} return
10907 $w conf -background $c
10908 lset $v $vi $c
10909 eval $cmd $c
10912 proc setselbg {c} {
10913 global bglist cflist
10914 foreach w $bglist {
10915 $w configure -selectbackground $c
10917 $cflist tag configure highlight \
10918 -background [$cflist cget -selectbackground]
10919 allcanvs itemconf secsel -fill $c
10922 # This sets the background color and the color scheme for the whole UI.
10923 # For some reason, tk_setPalette chooses a nasty dark red for selectColor
10924 # if we don't specify one ourselves, which makes the checkbuttons and
10925 # radiobuttons look bad. This chooses white for selectColor if the
10926 # background color is light, or black if it is dark.
10927 proc setui {c} {
10928 if {[tk windowingsystem] eq "win32"} { return }
10929 set bg [winfo rgb . $c]
10930 set selc black
10931 if {[lindex $bg 0] + 1.5 * [lindex $bg 1] + 0.5 * [lindex $bg 2] > 100000} {
10932 set selc white
10934 tk_setPalette background $c selectColor $selc
10937 proc setbg {c} {
10938 global bglist
10940 foreach w $bglist {
10941 $w conf -background $c
10945 proc setfg {c} {
10946 global fglist canv
10948 foreach w $fglist {
10949 $w conf -foreground $c
10951 allcanvs itemconf text -fill $c
10952 $canv itemconf circle -outline $c
10953 $canv itemconf markid -outline $c
10956 proc prefscan {} {
10957 global oldprefs prefstop
10959 foreach v {maxwidth maxgraphpct showneartags showlocalchanges \
10960 limitdiffs tabstop perfile_attrs hideremotes want_ttk} {
10961 global $v
10962 set $v $oldprefs($v)
10964 catch {destroy $prefstop}
10965 unset prefstop
10966 fontcan
10969 proc prefsok {} {
10970 global maxwidth maxgraphpct
10971 global oldprefs prefstop showneartags showlocalchanges
10972 global fontpref mainfont textfont uifont
10973 global limitdiffs treediffs perfile_attrs
10974 global hideremotes
10976 catch {destroy $prefstop}
10977 unset prefstop
10978 fontcan
10979 set fontchanged 0
10980 if {$mainfont ne $fontpref(mainfont)} {
10981 set mainfont $fontpref(mainfont)
10982 parsefont mainfont $mainfont
10983 eval font configure mainfont [fontflags mainfont]
10984 eval font configure mainfontbold [fontflags mainfont 1]
10985 setcoords
10986 set fontchanged 1
10988 if {$textfont ne $fontpref(textfont)} {
10989 set textfont $fontpref(textfont)
10990 parsefont textfont $textfont
10991 eval font configure textfont [fontflags textfont]
10992 eval font configure textfontbold [fontflags textfont 1]
10994 if {$uifont ne $fontpref(uifont)} {
10995 set uifont $fontpref(uifont)
10996 parsefont uifont $uifont
10997 eval font configure uifont [fontflags uifont]
10999 settabs
11000 if {$showlocalchanges != $oldprefs(showlocalchanges)} {
11001 if {$showlocalchanges} {
11002 doshowlocalchanges
11003 } else {
11004 dohidelocalchanges
11007 if {$limitdiffs != $oldprefs(limitdiffs) ||
11008 ($perfile_attrs && !$oldprefs(perfile_attrs))} {
11009 # treediffs elements are limited by path;
11010 # won't have encodings cached if perfile_attrs was just turned on
11011 catch {unset treediffs}
11013 if {$fontchanged || $maxwidth != $oldprefs(maxwidth)
11014 || $maxgraphpct != $oldprefs(maxgraphpct)} {
11015 redisplay
11016 } elseif {$showneartags != $oldprefs(showneartags) ||
11017 $limitdiffs != $oldprefs(limitdiffs)} {
11018 reselectline
11020 if {$hideremotes != $oldprefs(hideremotes)} {
11021 rereadrefs
11025 proc formatdate {d} {
11026 global datetimeformat
11027 if {$d ne {}} {
11028 set d [clock format $d -format $datetimeformat]
11030 return $d
11033 # This list of encoding names and aliases is distilled from
11034 # http://www.iana.org/assignments/character-sets.
11035 # Not all of them are supported by Tcl.
11036 set encoding_aliases {
11037 { ANSI_X3.4-1968 iso-ir-6 ANSI_X3.4-1986 ISO_646.irv:1991 ASCII
11038 ISO646-US US-ASCII us IBM367 cp367 csASCII }
11039 { ISO-10646-UTF-1 csISO10646UTF1 }
11040 { ISO_646.basic:1983 ref csISO646basic1983 }
11041 { INVARIANT csINVARIANT }
11042 { ISO_646.irv:1983 iso-ir-2 irv csISO2IntlRefVersion }
11043 { BS_4730 iso-ir-4 ISO646-GB gb uk csISO4UnitedKingdom }
11044 { NATS-SEFI iso-ir-8-1 csNATSSEFI }
11045 { NATS-SEFI-ADD iso-ir-8-2 csNATSSEFIADD }
11046 { NATS-DANO iso-ir-9-1 csNATSDANO }
11047 { NATS-DANO-ADD iso-ir-9-2 csNATSDANOADD }
11048 { SEN_850200_B iso-ir-10 FI ISO646-FI ISO646-SE se csISO10Swedish }
11049 { SEN_850200_C iso-ir-11 ISO646-SE2 se2 csISO11SwedishForNames }
11050 { KS_C_5601-1987 iso-ir-149 KS_C_5601-1989 KSC_5601 korean csKSC56011987 }
11051 { ISO-2022-KR csISO2022KR }
11052 { EUC-KR csEUCKR }
11053 { ISO-2022-JP csISO2022JP }
11054 { ISO-2022-JP-2 csISO2022JP2 }
11055 { JIS_C6220-1969-jp JIS_C6220-1969 iso-ir-13 katakana x0201-7
11056 csISO13JISC6220jp }
11057 { JIS_C6220-1969-ro iso-ir-14 jp ISO646-JP csISO14JISC6220ro }
11058 { IT iso-ir-15 ISO646-IT csISO15Italian }
11059 { PT iso-ir-16 ISO646-PT csISO16Portuguese }
11060 { ES iso-ir-17 ISO646-ES csISO17Spanish }
11061 { greek7-old iso-ir-18 csISO18Greek7Old }
11062 { latin-greek iso-ir-19 csISO19LatinGreek }
11063 { DIN_66003 iso-ir-21 de ISO646-DE csISO21German }
11064 { NF_Z_62-010_(1973) iso-ir-25 ISO646-FR1 csISO25French }
11065 { Latin-greek-1 iso-ir-27 csISO27LatinGreek1 }
11066 { ISO_5427 iso-ir-37 csISO5427Cyrillic }
11067 { JIS_C6226-1978 iso-ir-42 csISO42JISC62261978 }
11068 { BS_viewdata iso-ir-47 csISO47BSViewdata }
11069 { INIS iso-ir-49 csISO49INIS }
11070 { INIS-8 iso-ir-50 csISO50INIS8 }
11071 { INIS-cyrillic iso-ir-51 csISO51INISCyrillic }
11072 { ISO_5427:1981 iso-ir-54 ISO5427Cyrillic1981 }
11073 { ISO_5428:1980 iso-ir-55 csISO5428Greek }
11074 { GB_1988-80 iso-ir-57 cn ISO646-CN csISO57GB1988 }
11075 { GB_2312-80 iso-ir-58 chinese csISO58GB231280 }
11076 { NS_4551-1 iso-ir-60 ISO646-NO no csISO60DanishNorwegian
11077 csISO60Norwegian1 }
11078 { NS_4551-2 ISO646-NO2 iso-ir-61 no2 csISO61Norwegian2 }
11079 { NF_Z_62-010 iso-ir-69 ISO646-FR fr csISO69French }
11080 { videotex-suppl iso-ir-70 csISO70VideotexSupp1 }
11081 { PT2 iso-ir-84 ISO646-PT2 csISO84Portuguese2 }
11082 { ES2 iso-ir-85 ISO646-ES2 csISO85Spanish2 }
11083 { MSZ_7795.3 iso-ir-86 ISO646-HU hu csISO86Hungarian }
11084 { JIS_C6226-1983 iso-ir-87 x0208 JIS_X0208-1983 csISO87JISX0208 }
11085 { greek7 iso-ir-88 csISO88Greek7 }
11086 { ASMO_449 ISO_9036 arabic7 iso-ir-89 csISO89ASMO449 }
11087 { iso-ir-90 csISO90 }
11088 { JIS_C6229-1984-a iso-ir-91 jp-ocr-a csISO91JISC62291984a }
11089 { JIS_C6229-1984-b iso-ir-92 ISO646-JP-OCR-B jp-ocr-b
11090 csISO92JISC62991984b }
11091 { JIS_C6229-1984-b-add iso-ir-93 jp-ocr-b-add csISO93JIS62291984badd }
11092 { JIS_C6229-1984-hand iso-ir-94 jp-ocr-hand csISO94JIS62291984hand }
11093 { JIS_C6229-1984-hand-add iso-ir-95 jp-ocr-hand-add
11094 csISO95JIS62291984handadd }
11095 { JIS_C6229-1984-kana iso-ir-96 csISO96JISC62291984kana }
11096 { ISO_2033-1983 iso-ir-98 e13b csISO2033 }
11097 { ANSI_X3.110-1983 iso-ir-99 CSA_T500-1983 NAPLPS csISO99NAPLPS }
11098 { ISO_8859-1:1987 iso-ir-100 ISO_8859-1 ISO-8859-1 latin1 l1 IBM819
11099 CP819 csISOLatin1 }
11100 { ISO_8859-2:1987 iso-ir-101 ISO_8859-2 ISO-8859-2 latin2 l2 csISOLatin2 }
11101 { T.61-7bit iso-ir-102 csISO102T617bit }
11102 { T.61-8bit T.61 iso-ir-103 csISO103T618bit }
11103 { ISO_8859-3:1988 iso-ir-109 ISO_8859-3 ISO-8859-3 latin3 l3 csISOLatin3 }
11104 { ISO_8859-4:1988 iso-ir-110 ISO_8859-4 ISO-8859-4 latin4 l4 csISOLatin4 }
11105 { ECMA-cyrillic iso-ir-111 KOI8-E csISO111ECMACyrillic }
11106 { CSA_Z243.4-1985-1 iso-ir-121 ISO646-CA csa7-1 ca csISO121Canadian1 }
11107 { CSA_Z243.4-1985-2 iso-ir-122 ISO646-CA2 csa7-2 csISO122Canadian2 }
11108 { CSA_Z243.4-1985-gr iso-ir-123 csISO123CSAZ24341985gr }
11109 { ISO_8859-6:1987 iso-ir-127 ISO_8859-6 ISO-8859-6 ECMA-114 ASMO-708
11110 arabic csISOLatinArabic }
11111 { ISO_8859-6-E csISO88596E ISO-8859-6-E }
11112 { ISO_8859-6-I csISO88596I ISO-8859-6-I }
11113 { ISO_8859-7:1987 iso-ir-126 ISO_8859-7 ISO-8859-7 ELOT_928 ECMA-118
11114 greek greek8 csISOLatinGreek }
11115 { T.101-G2 iso-ir-128 csISO128T101G2 }
11116 { ISO_8859-8:1988 iso-ir-138 ISO_8859-8 ISO-8859-8 hebrew
11117 csISOLatinHebrew }
11118 { ISO_8859-8-E csISO88598E ISO-8859-8-E }
11119 { ISO_8859-8-I csISO88598I ISO-8859-8-I }
11120 { CSN_369103 iso-ir-139 csISO139CSN369103 }
11121 { JUS_I.B1.002 iso-ir-141 ISO646-YU js yu csISO141JUSIB1002 }
11122 { ISO_6937-2-add iso-ir-142 csISOTextComm }
11123 { IEC_P27-1 iso-ir-143 csISO143IECP271 }
11124 { ISO_8859-5:1988 iso-ir-144 ISO_8859-5 ISO-8859-5 cyrillic
11125 csISOLatinCyrillic }
11126 { JUS_I.B1.003-serb iso-ir-146 serbian csISO146Serbian }
11127 { JUS_I.B1.003-mac macedonian iso-ir-147 csISO147Macedonian }
11128 { ISO_8859-9:1989 iso-ir-148 ISO_8859-9 ISO-8859-9 latin5 l5 csISOLatin5 }
11129 { greek-ccitt iso-ir-150 csISO150 csISO150GreekCCITT }
11130 { NC_NC00-10:81 cuba iso-ir-151 ISO646-CU csISO151Cuba }
11131 { ISO_6937-2-25 iso-ir-152 csISO6937Add }
11132 { GOST_19768-74 ST_SEV_358-88 iso-ir-153 csISO153GOST1976874 }
11133 { ISO_8859-supp iso-ir-154 latin1-2-5 csISO8859Supp }
11134 { ISO_10367-box iso-ir-155 csISO10367Box }
11135 { ISO-8859-10 iso-ir-157 l6 ISO_8859-10:1992 csISOLatin6 latin6 }
11136 { latin-lap lap iso-ir-158 csISO158Lap }
11137 { JIS_X0212-1990 x0212 iso-ir-159 csISO159JISX02121990 }
11138 { DS_2089 DS2089 ISO646-DK dk csISO646Danish }
11139 { us-dk csUSDK }
11140 { dk-us csDKUS }
11141 { JIS_X0201 X0201 csHalfWidthKatakana }
11142 { KSC5636 ISO646-KR csKSC5636 }
11143 { ISO-10646-UCS-2 csUnicode }
11144 { ISO-10646-UCS-4 csUCS4 }
11145 { DEC-MCS dec csDECMCS }
11146 { hp-roman8 roman8 r8 csHPRoman8 }
11147 { macintosh mac csMacintosh }
11148 { IBM037 cp037 ebcdic-cp-us ebcdic-cp-ca ebcdic-cp-wt ebcdic-cp-nl
11149 csIBM037 }
11150 { IBM038 EBCDIC-INT cp038 csIBM038 }
11151 { IBM273 CP273 csIBM273 }
11152 { IBM274 EBCDIC-BE CP274 csIBM274 }
11153 { IBM275 EBCDIC-BR cp275 csIBM275 }
11154 { IBM277 EBCDIC-CP-DK EBCDIC-CP-NO csIBM277 }
11155 { IBM278 CP278 ebcdic-cp-fi ebcdic-cp-se csIBM278 }
11156 { IBM280 CP280 ebcdic-cp-it csIBM280 }
11157 { IBM281 EBCDIC-JP-E cp281 csIBM281 }
11158 { IBM284 CP284 ebcdic-cp-es csIBM284 }
11159 { IBM285 CP285 ebcdic-cp-gb csIBM285 }
11160 { IBM290 cp290 EBCDIC-JP-kana csIBM290 }
11161 { IBM297 cp297 ebcdic-cp-fr csIBM297 }
11162 { IBM420 cp420 ebcdic-cp-ar1 csIBM420 }
11163 { IBM423 cp423 ebcdic-cp-gr csIBM423 }
11164 { IBM424 cp424 ebcdic-cp-he csIBM424 }
11165 { IBM437 cp437 437 csPC8CodePage437 }
11166 { IBM500 CP500 ebcdic-cp-be ebcdic-cp-ch csIBM500 }
11167 { IBM775 cp775 csPC775Baltic }
11168 { IBM850 cp850 850 csPC850Multilingual }
11169 { IBM851 cp851 851 csIBM851 }
11170 { IBM852 cp852 852 csPCp852 }
11171 { IBM855 cp855 855 csIBM855 }
11172 { IBM857 cp857 857 csIBM857 }
11173 { IBM860 cp860 860 csIBM860 }
11174 { IBM861 cp861 861 cp-is csIBM861 }
11175 { IBM862 cp862 862 csPC862LatinHebrew }
11176 { IBM863 cp863 863 csIBM863 }
11177 { IBM864 cp864 csIBM864 }
11178 { IBM865 cp865 865 csIBM865 }
11179 { IBM866 cp866 866 csIBM866 }
11180 { IBM868 CP868 cp-ar csIBM868 }
11181 { IBM869 cp869 869 cp-gr csIBM869 }
11182 { IBM870 CP870 ebcdic-cp-roece ebcdic-cp-yu csIBM870 }
11183 { IBM871 CP871 ebcdic-cp-is csIBM871 }
11184 { IBM880 cp880 EBCDIC-Cyrillic csIBM880 }
11185 { IBM891 cp891 csIBM891 }
11186 { IBM903 cp903 csIBM903 }
11187 { IBM904 cp904 904 csIBBM904 }
11188 { IBM905 CP905 ebcdic-cp-tr csIBM905 }
11189 { IBM918 CP918 ebcdic-cp-ar2 csIBM918 }
11190 { IBM1026 CP1026 csIBM1026 }
11191 { EBCDIC-AT-DE csIBMEBCDICATDE }
11192 { EBCDIC-AT-DE-A csEBCDICATDEA }
11193 { EBCDIC-CA-FR csEBCDICCAFR }
11194 { EBCDIC-DK-NO csEBCDICDKNO }
11195 { EBCDIC-DK-NO-A csEBCDICDKNOA }
11196 { EBCDIC-FI-SE csEBCDICFISE }
11197 { EBCDIC-FI-SE-A csEBCDICFISEA }
11198 { EBCDIC-FR csEBCDICFR }
11199 { EBCDIC-IT csEBCDICIT }
11200 { EBCDIC-PT csEBCDICPT }
11201 { EBCDIC-ES csEBCDICES }
11202 { EBCDIC-ES-A csEBCDICESA }
11203 { EBCDIC-ES-S csEBCDICESS }
11204 { EBCDIC-UK csEBCDICUK }
11205 { EBCDIC-US csEBCDICUS }
11206 { UNKNOWN-8BIT csUnknown8BiT }
11207 { MNEMONIC csMnemonic }
11208 { MNEM csMnem }
11209 { VISCII csVISCII }
11210 { VIQR csVIQR }
11211 { KOI8-R csKOI8R }
11212 { IBM00858 CCSID00858 CP00858 PC-Multilingual-850+euro }
11213 { IBM00924 CCSID00924 CP00924 ebcdic-Latin9--euro }
11214 { IBM01140 CCSID01140 CP01140 ebcdic-us-37+euro }
11215 { IBM01141 CCSID01141 CP01141 ebcdic-de-273+euro }
11216 { IBM01142 CCSID01142 CP01142 ebcdic-dk-277+euro ebcdic-no-277+euro }
11217 { IBM01143 CCSID01143 CP01143 ebcdic-fi-278+euro ebcdic-se-278+euro }
11218 { IBM01144 CCSID01144 CP01144 ebcdic-it-280+euro }
11219 { IBM01145 CCSID01145 CP01145 ebcdic-es-284+euro }
11220 { IBM01146 CCSID01146 CP01146 ebcdic-gb-285+euro }
11221 { IBM01147 CCSID01147 CP01147 ebcdic-fr-297+euro }
11222 { IBM01148 CCSID01148 CP01148 ebcdic-international-500+euro }
11223 { IBM01149 CCSID01149 CP01149 ebcdic-is-871+euro }
11224 { IBM1047 IBM-1047 }
11225 { PTCP154 csPTCP154 PT154 CP154 Cyrillic-Asian }
11226 { Amiga-1251 Ami1251 Amiga1251 Ami-1251 }
11227 { UNICODE-1-1 csUnicode11 }
11228 { CESU-8 csCESU-8 }
11229 { BOCU-1 csBOCU-1 }
11230 { UNICODE-1-1-UTF-7 csUnicode11UTF7 }
11231 { ISO-8859-14 iso-ir-199 ISO_8859-14:1998 ISO_8859-14 latin8 iso-celtic
11232 l8 }
11233 { ISO-8859-15 ISO_8859-15 Latin-9 }
11234 { ISO-8859-16 iso-ir-226 ISO_8859-16:2001 ISO_8859-16 latin10 l10 }
11235 { GBK CP936 MS936 windows-936 }
11236 { JIS_Encoding csJISEncoding }
11237 { Shift_JIS MS_Kanji csShiftJIS ShiftJIS Shift-JIS }
11238 { Extended_UNIX_Code_Packed_Format_for_Japanese csEUCPkdFmtJapanese
11239 EUC-JP }
11240 { Extended_UNIX_Code_Fixed_Width_for_Japanese csEUCFixWidJapanese }
11241 { ISO-10646-UCS-Basic csUnicodeASCII }
11242 { ISO-10646-Unicode-Latin1 csUnicodeLatin1 ISO-10646 }
11243 { ISO-Unicode-IBM-1261 csUnicodeIBM1261 }
11244 { ISO-Unicode-IBM-1268 csUnicodeIBM1268 }
11245 { ISO-Unicode-IBM-1276 csUnicodeIBM1276 }
11246 { ISO-Unicode-IBM-1264 csUnicodeIBM1264 }
11247 { ISO-Unicode-IBM-1265 csUnicodeIBM1265 }
11248 { ISO-8859-1-Windows-3.0-Latin-1 csWindows30Latin1 }
11249 { ISO-8859-1-Windows-3.1-Latin-1 csWindows31Latin1 }
11250 { ISO-8859-2-Windows-Latin-2 csWindows31Latin2 }
11251 { ISO-8859-9-Windows-Latin-5 csWindows31Latin5 }
11252 { Adobe-Standard-Encoding csAdobeStandardEncoding }
11253 { Ventura-US csVenturaUS }
11254 { Ventura-International csVenturaInternational }
11255 { PC8-Danish-Norwegian csPC8DanishNorwegian }
11256 { PC8-Turkish csPC8Turkish }
11257 { IBM-Symbols csIBMSymbols }
11258 { IBM-Thai csIBMThai }
11259 { HP-Legal csHPLegal }
11260 { HP-Pi-font csHPPiFont }
11261 { HP-Math8 csHPMath8 }
11262 { Adobe-Symbol-Encoding csHPPSMath }
11263 { HP-DeskTop csHPDesktop }
11264 { Ventura-Math csVenturaMath }
11265 { Microsoft-Publishing csMicrosoftPublishing }
11266 { Windows-31J csWindows31J }
11267 { GB2312 csGB2312 }
11268 { Big5 csBig5 }
11271 proc tcl_encoding {enc} {
11272 global encoding_aliases tcl_encoding_cache
11273 if {[info exists tcl_encoding_cache($enc)]} {
11274 return $tcl_encoding_cache($enc)
11276 set names [encoding names]
11277 set lcnames [string tolower $names]
11278 set enc [string tolower $enc]
11279 set i [lsearch -exact $lcnames $enc]
11280 if {$i < 0} {
11281 # look for "isonnn" instead of "iso-nnn" or "iso_nnn"
11282 if {[regsub {^(iso|cp|ibm|jis)[-_]} $enc {\1} encx]} {
11283 set i [lsearch -exact $lcnames $encx]
11286 if {$i < 0} {
11287 foreach l $encoding_aliases {
11288 set ll [string tolower $l]
11289 if {[lsearch -exact $ll $enc] < 0} continue
11290 # look through the aliases for one that tcl knows about
11291 foreach e $ll {
11292 set i [lsearch -exact $lcnames $e]
11293 if {$i < 0} {
11294 if {[regsub {^(iso|cp|ibm|jis)[-_]} $e {\1} ex]} {
11295 set i [lsearch -exact $lcnames $ex]
11298 if {$i >= 0} break
11300 break
11303 set tclenc {}
11304 if {$i >= 0} {
11305 set tclenc [lindex $names $i]
11307 set tcl_encoding_cache($enc) $tclenc
11308 return $tclenc
11311 proc gitattr {path attr default} {
11312 global path_attr_cache
11313 if {[info exists path_attr_cache($attr,$path)]} {
11314 set r $path_attr_cache($attr,$path)
11315 } else {
11316 set r "unspecified"
11317 if {![catch {set line [exec git check-attr $attr -- $path]}]} {
11318 regexp "(.*): $attr: (.*)" $line m f r
11320 set path_attr_cache($attr,$path) $r
11322 if {$r eq "unspecified"} {
11323 return $default
11325 return $r
11328 proc cache_gitattr {attr pathlist} {
11329 global path_attr_cache
11330 set newlist {}
11331 foreach path $pathlist {
11332 if {![info exists path_attr_cache($attr,$path)]} {
11333 lappend newlist $path
11336 set lim 1000
11337 if {[tk windowingsystem] == "win32"} {
11338 # windows has a 32k limit on the arguments to a command...
11339 set lim 30
11341 while {$newlist ne {}} {
11342 set head [lrange $newlist 0 [expr {$lim - 1}]]
11343 set newlist [lrange $newlist $lim end]
11344 if {![catch {set rlist [eval exec git check-attr $attr -- $head]}]} {
11345 foreach row [split $rlist "\n"] {
11346 if {[regexp "(.*): $attr: (.*)" $row m path value]} {
11347 if {[string index $path 0] eq "\""} {
11348 set path [encoding convertfrom [lindex $path 0]]
11350 set path_attr_cache($attr,$path) $value
11357 proc get_path_encoding {path} {
11358 global gui_encoding perfile_attrs
11359 set tcl_enc $gui_encoding
11360 if {$path ne {} && $perfile_attrs} {
11361 set enc2 [tcl_encoding [gitattr $path encoding $tcl_enc]]
11362 if {$enc2 ne {}} {
11363 set tcl_enc $enc2
11366 return $tcl_enc
11369 # First check that Tcl/Tk is recent enough
11370 if {[catch {package require Tk 8.4} err]} {
11371 show_error {} . "Sorry, gitk cannot run with this version of Tcl/Tk.\n\
11372 Gitk requires at least Tcl/Tk 8.4." list
11373 exit 1
11376 # defaults...
11377 set wrcomcmd "git diff-tree --stdin -p --pretty"
11379 set gitencoding {}
11380 catch {
11381 set gitencoding [exec git config --get i18n.commitencoding]
11383 catch {
11384 set gitencoding [exec git config --get i18n.logoutputencoding]
11386 if {$gitencoding == ""} {
11387 set gitencoding "utf-8"
11389 set tclencoding [tcl_encoding $gitencoding]
11390 if {$tclencoding == {}} {
11391 puts stderr "Warning: encoding $gitencoding is not supported by Tcl/Tk"
11394 set gui_encoding [encoding system]
11395 catch {
11396 set enc [exec git config --get gui.encoding]
11397 if {$enc ne {}} {
11398 set tclenc [tcl_encoding $enc]
11399 if {$tclenc ne {}} {
11400 set gui_encoding $tclenc
11401 } else {
11402 puts stderr "Warning: encoding $enc is not supported by Tcl/Tk"
11407 if {[tk windowingsystem] eq "aqua"} {
11408 set mainfont {{Lucida Grande} 9}
11409 set textfont {Monaco 9}
11410 set uifont {{Lucida Grande} 9 bold}
11411 } else {
11412 set mainfont {Helvetica 9}
11413 set textfont {Courier 9}
11414 set uifont {Helvetica 9 bold}
11416 set tabstop 8
11417 set findmergefiles 0
11418 set maxgraphpct 50
11419 set maxwidth 16
11420 set revlistorder 0
11421 set fastdate 0
11422 set uparrowlen 5
11423 set downarrowlen 5
11424 set mingaplen 100
11425 set cmitmode "patch"
11426 set wrapcomment "none"
11427 set showneartags 1
11428 set hideremotes 0
11429 set maxrefs 20
11430 set maxlinelen 200
11431 set showlocalchanges 1
11432 set limitdiffs 1
11433 set datetimeformat "%Y-%m-%d %H:%M:%S"
11434 set autoselect 1
11435 set autosellen 40
11436 set perfile_attrs 0
11437 set want_ttk 1
11439 if {[tk windowingsystem] eq "aqua"} {
11440 set extdifftool "opendiff"
11441 } else {
11442 set extdifftool "meld"
11445 set colors {green red blue magenta darkgrey brown orange}
11446 if {[tk windowingsystem] eq "win32"} {
11447 set uicolor SystemButtonFace
11448 set bgcolor SystemWindow
11449 set fgcolor SystemButtonText
11450 set selectbgcolor SystemHighlight
11451 } else {
11452 set uicolor grey85
11453 set bgcolor white
11454 set fgcolor black
11455 set selectbgcolor gray85
11457 set diffcolors {red "#00a000" blue}
11458 set diffcontext 3
11459 set ignorespace 0
11460 set worddiff ""
11461 set markbgcolor "#e0e0ff"
11463 set circlecolors {white blue gray blue blue}
11465 # button for popping up context menus
11466 if {[tk windowingsystem] eq "aqua"} {
11467 set ctxbut <Button-2>
11468 } else {
11469 set ctxbut <Button-3>
11472 ## For msgcat loading, first locate the installation location.
11473 if { [info exists ::env(GITK_MSGSDIR)] } {
11474 ## Msgsdir was manually set in the environment.
11475 set gitk_msgsdir $::env(GITK_MSGSDIR)
11476 } else {
11477 ## Let's guess the prefix from argv0.
11478 set gitk_prefix [file dirname [file dirname [file normalize $argv0]]]
11479 set gitk_libdir [file join $gitk_prefix share gitk lib]
11480 set gitk_msgsdir [file join $gitk_libdir msgs]
11481 unset gitk_prefix
11484 ## Internationalization (i18n) through msgcat and gettext. See
11485 ## http://www.gnu.org/software/gettext/manual/html_node/Tcl.html
11486 package require msgcat
11487 namespace import ::msgcat::mc
11488 ## And eventually load the actual message catalog
11489 ::msgcat::mcload $gitk_msgsdir
11491 catch {source ~/.gitk}
11493 parsefont mainfont $mainfont
11494 eval font create mainfont [fontflags mainfont]
11495 eval font create mainfontbold [fontflags mainfont 1]
11497 parsefont textfont $textfont
11498 eval font create textfont [fontflags textfont]
11499 eval font create textfontbold [fontflags textfont 1]
11501 parsefont uifont $uifont
11502 eval font create uifont [fontflags uifont]
11504 setui $uicolor
11506 setoptions
11508 # check that we can find a .git directory somewhere...
11509 if {[catch {set gitdir [gitdir]}]} {
11510 show_error {} . [mc "Cannot find a git repository here."]
11511 exit 1
11513 if {![file isdirectory $gitdir]} {
11514 show_error {} . [mc "Cannot find the git directory \"%s\"." $gitdir]
11515 exit 1
11518 set selecthead {}
11519 set selectheadid {}
11521 set revtreeargs {}
11522 set cmdline_files {}
11523 set i 0
11524 set revtreeargscmd {}
11525 foreach arg $argv {
11526 switch -glob -- $arg {
11527 "" { }
11528 "--" {
11529 set cmdline_files [lrange $argv [expr {$i + 1}] end]
11530 break
11532 "--select-commit=*" {
11533 set selecthead [string range $arg 16 end]
11535 "--argscmd=*" {
11536 set revtreeargscmd [string range $arg 10 end]
11538 default {
11539 lappend revtreeargs $arg
11542 incr i
11545 if {$selecthead eq "HEAD"} {
11546 set selecthead {}
11549 if {$i >= [llength $argv] && $revtreeargs ne {}} {
11550 # no -- on command line, but some arguments (other than --argscmd)
11551 if {[catch {
11552 set f [eval exec git rev-parse --no-revs --no-flags $revtreeargs]
11553 set cmdline_files [split $f "\n"]
11554 set n [llength $cmdline_files]
11555 set revtreeargs [lrange $revtreeargs 0 end-$n]
11556 # Unfortunately git rev-parse doesn't produce an error when
11557 # something is both a revision and a filename. To be consistent
11558 # with git log and git rev-list, check revtreeargs for filenames.
11559 foreach arg $revtreeargs {
11560 if {[file exists $arg]} {
11561 show_error {} . [mc "Ambiguous argument '%s': both revision\
11562 and filename" $arg]
11563 exit 1
11566 } err]} {
11567 # unfortunately we get both stdout and stderr in $err,
11568 # so look for "fatal:".
11569 set i [string first "fatal:" $err]
11570 if {$i > 0} {
11571 set err [string range $err [expr {$i + 6}] end]
11573 show_error {} . "[mc "Bad arguments to gitk:"]\n$err"
11574 exit 1
11578 set nullid "0000000000000000000000000000000000000000"
11579 set nullid2 "0000000000000000000000000000000000000001"
11580 set nullfile "/dev/null"
11582 set have_tk85 [expr {[package vcompare $tk_version "8.5"] >= 0}]
11583 if {![info exists have_ttk]} {
11584 set have_ttk [llength [info commands ::ttk::style]]
11586 set use_ttk [expr {$have_ttk && $want_ttk}]
11587 set NS [expr {$use_ttk ? "ttk" : ""}]
11589 regexp {^git version ([\d.]*\d)} [exec git version] _ git_version
11591 set show_notes {}
11592 if {[package vcompare $git_version "1.6.6.2"] >= 0} {
11593 set show_notes "--show-notes"
11596 set runq {}
11597 set history {}
11598 set historyindex 0
11599 set fh_serial 0
11600 set nhl_names {}
11601 set highlight_paths {}
11602 set findpattern {}
11603 set searchdirn -forwards
11604 set boldids {}
11605 set boldnameids {}
11606 set diffelide {0 0}
11607 set markingmatches 0
11608 set linkentercount 0
11609 set need_redisplay 0
11610 set nrows_drawn 0
11611 set firsttabstop 0
11613 set nextviewnum 1
11614 set curview 0
11615 set selectedview 0
11616 set selectedhlview [mc "None"]
11617 set highlight_related [mc "None"]
11618 set highlight_files {}
11619 set viewfiles(0) {}
11620 set viewperm(0) 0
11621 set viewargs(0) {}
11622 set viewargscmd(0) {}
11624 set selectedline {}
11625 set numcommits 0
11626 set loginstance 0
11627 set cmdlineok 0
11628 set stopped 0
11629 set stuffsaved 0
11630 set patchnum 0
11631 set lserial 0
11632 set isworktree [expr {[exec git rev-parse --is-inside-work-tree] == "true"}]
11633 setcoords
11634 makewindow
11635 catch {
11636 image create photo gitlogo -width 16 -height 16
11638 image create photo gitlogominus -width 4 -height 2
11639 gitlogominus put #C00000 -to 0 0 4 2
11640 gitlogo copy gitlogominus -to 1 5
11641 gitlogo copy gitlogominus -to 6 5
11642 gitlogo copy gitlogominus -to 11 5
11643 image delete gitlogominus
11645 image create photo gitlogoplus -width 4 -height 4
11646 gitlogoplus put #008000 -to 1 0 3 4
11647 gitlogoplus put #008000 -to 0 1 4 3
11648 gitlogo copy gitlogoplus -to 1 9
11649 gitlogo copy gitlogoplus -to 6 9
11650 gitlogo copy gitlogoplus -to 11 9
11651 image delete gitlogoplus
11653 image create photo gitlogo32 -width 32 -height 32
11654 gitlogo32 copy gitlogo -zoom 2 2
11656 wm iconphoto . -default gitlogo gitlogo32
11658 # wait for the window to become visible
11659 tkwait visibility .
11660 wm title . "[file tail $argv0]: [file tail [pwd]]"
11661 update
11662 readrefs
11664 if {$cmdline_files ne {} || $revtreeargs ne {} || $revtreeargscmd ne {}} {
11665 # create a view for the files/dirs specified on the command line
11666 set curview 1
11667 set selectedview 1
11668 set nextviewnum 2
11669 set viewname(1) [mc "Command line"]
11670 set viewfiles(1) $cmdline_files
11671 set viewargs(1) $revtreeargs
11672 set viewargscmd(1) $revtreeargscmd
11673 set viewperm(1) 0
11674 set vdatemode(1) 0
11675 addviewmenu 1
11676 .bar.view entryconf [mca "Edit view..."] -state normal
11677 .bar.view entryconf [mca "Delete view"] -state normal
11680 if {[info exists permviews]} {
11681 foreach v $permviews {
11682 set n $nextviewnum
11683 incr nextviewnum
11684 set viewname($n) [lindex $v 0]
11685 set viewfiles($n) [lindex $v 1]
11686 set viewargs($n) [lindex $v 2]
11687 set viewargscmd($n) [lindex $v 3]
11688 set viewperm($n) 1
11689 addviewmenu $n
11693 if {[tk windowingsystem] eq "win32"} {
11694 focus -force .
11697 getcommits {}
11699 # Local variables:
11700 # mode: tcl
11701 # indent-tabs-mode: t
11702 # tab-width: 8
11703 # End: