dir.c::test_one_path(): work around directory_exists_in_index_icase() breakage
[git.git] / gitk-git / gitk
blob5cd00d80fe2fb80a07a59b7e5327f8ff6c29d8f6
1 #!/bin/sh
2 # Tcl ignores the next line -*- tcl -*- \
3 exec wish "$0" -- "$@"
5 # Copyright © 2005-2011 Paul Mackerras. All rights reserved.
6 # This program is free software; it may be used, copied, modified
7 # and distributed under the terms of the GNU General Public Licence,
8 # either version 2, or (at your option) any later version.
10 package require Tk
12 proc hasworktree {} {
13 return [expr {[exec git rev-parse --is-bare-repository] == "false" &&
14 [exec git rev-parse --is-inside-git-dir] == "false"}]
17 proc reponame {} {
18 global gitdir
19 set n [file normalize $gitdir]
20 if {[string match "*/.git" $n]} {
21 set n [string range $n 0 end-5]
23 return [file tail $n]
26 proc gitworktree {} {
27 variable _gitworktree
28 if {[info exists _gitworktree]} {
29 return $_gitworktree
31 # v1.7.0 introduced --show-toplevel to return the canonical work-tree
32 if {[catch {set _gitworktree [exec git rev-parse --show-toplevel]}]} {
33 # try to set work tree from environment, core.worktree or use
34 # cdup to obtain a relative path to the top of the worktree. If
35 # run from the top, the ./ prefix ensures normalize expands pwd.
36 if {[catch { set _gitworktree $env(GIT_WORK_TREE) }]} {
37 catch {set _gitworktree [exec git config --get core.worktree]}
38 if {$_gitworktree eq ""} {
39 set _gitworktree [file normalize ./[exec git rev-parse --show-cdup]]
43 return $_gitworktree
46 # A simple scheduler for compute-intensive stuff.
47 # The aim is to make sure that event handlers for GUI actions can
48 # run at least every 50-100 ms. Unfortunately fileevent handlers are
49 # run before X event handlers, so reading from a fast source can
50 # make the GUI completely unresponsive.
51 proc run args {
52 global isonrunq runq currunq
54 set script $args
55 if {[info exists isonrunq($script)]} return
56 if {$runq eq {} && ![info exists currunq]} {
57 after idle dorunq
59 lappend runq [list {} $script]
60 set isonrunq($script) 1
63 proc filerun {fd script} {
64 fileevent $fd readable [list filereadable $fd $script]
67 proc filereadable {fd script} {
68 global runq currunq
70 fileevent $fd readable {}
71 if {$runq eq {} && ![info exists currunq]} {
72 after idle dorunq
74 lappend runq [list $fd $script]
77 proc nukefile {fd} {
78 global runq
80 for {set i 0} {$i < [llength $runq]} {} {
81 if {[lindex $runq $i 0] eq $fd} {
82 set runq [lreplace $runq $i $i]
83 } else {
84 incr i
89 proc dorunq {} {
90 global isonrunq runq currunq
92 set tstart [clock clicks -milliseconds]
93 set t0 $tstart
94 while {[llength $runq] > 0} {
95 set fd [lindex $runq 0 0]
96 set script [lindex $runq 0 1]
97 set currunq [lindex $runq 0]
98 set runq [lrange $runq 1 end]
99 set repeat [eval $script]
100 unset currunq
101 set t1 [clock clicks -milliseconds]
102 set t [expr {$t1 - $t0}]
103 if {$repeat ne {} && $repeat} {
104 if {$fd eq {} || $repeat == 2} {
105 # script returns 1 if it wants to be readded
106 # file readers return 2 if they could do more straight away
107 lappend runq [list $fd $script]
108 } else {
109 fileevent $fd readable [list filereadable $fd $script]
111 } elseif {$fd eq {}} {
112 unset isonrunq($script)
114 set t0 $t1
115 if {$t1 - $tstart >= 80} break
117 if {$runq ne {}} {
118 after idle dorunq
122 proc reg_instance {fd} {
123 global commfd leftover loginstance
125 set i [incr loginstance]
126 set commfd($i) $fd
127 set leftover($i) {}
128 return $i
131 proc unmerged_files {files} {
132 global nr_unmerged
134 # find the list of unmerged files
135 set mlist {}
136 set nr_unmerged 0
137 if {[catch {
138 set fd [open "| git ls-files -u" r]
139 } err]} {
140 show_error {} . "[mc "Couldn't get list of unmerged files:"] $err"
141 exit 1
143 while {[gets $fd line] >= 0} {
144 set i [string first "\t" $line]
145 if {$i < 0} continue
146 set fname [string range $line [expr {$i+1}] end]
147 if {[lsearch -exact $mlist $fname] >= 0} continue
148 incr nr_unmerged
149 if {$files eq {} || [path_filter $files $fname]} {
150 lappend mlist $fname
153 catch {close $fd}
154 return $mlist
157 proc parseviewargs {n arglist} {
158 global vdatemode vmergeonly vflags vdflags vrevs vfiltered vorigargs env
159 global worddiff git_version
161 set vdatemode($n) 0
162 set vmergeonly($n) 0
163 set glflags {}
164 set diffargs {}
165 set nextisval 0
166 set revargs {}
167 set origargs $arglist
168 set allknown 1
169 set filtered 0
170 set i -1
171 foreach arg $arglist {
172 incr i
173 if {$nextisval} {
174 lappend glflags $arg
175 set nextisval 0
176 continue
178 switch -glob -- $arg {
179 "-d" -
180 "--date-order" {
181 set vdatemode($n) 1
182 # remove from origargs in case we hit an unknown option
183 set origargs [lreplace $origargs $i $i]
184 incr i -1
186 "-[puabwcrRBMC]" -
187 "--no-renames" - "--full-index" - "--binary" - "--abbrev=*" -
188 "--find-copies-harder" - "-l*" - "--ext-diff" - "--no-ext-diff" -
189 "--src-prefix=*" - "--dst-prefix=*" - "--no-prefix" -
190 "-O*" - "--text" - "--full-diff" - "--ignore-space-at-eol" -
191 "--ignore-space-change" - "-U*" - "--unified=*" {
192 # These request or affect diff output, which we don't want.
193 # Some could be used to set our defaults for diff display.
194 lappend diffargs $arg
196 "--raw" - "--patch-with-raw" - "--patch-with-stat" -
197 "--name-only" - "--name-status" - "--color" -
198 "--log-size" - "--pretty=*" - "--decorate" - "--abbrev-commit" -
199 "--cc" - "-z" - "--header" - "--parents" - "--boundary" -
200 "--no-color" - "-g" - "--walk-reflogs" - "--no-walk" -
201 "--timestamp" - "relative-date" - "--date=*" - "--stdin" -
202 "--objects" - "--objects-edge" - "--reverse" {
203 # These cause our parsing of git log's output to fail, or else
204 # they're options we want to set ourselves, so ignore them.
206 "--color-words*" - "--word-diff=color" {
207 # These trigger a word diff in the console interface,
208 # so help the user by enabling our own support
209 if {[package vcompare $git_version "1.7.2"] >= 0} {
210 set worddiff [mc "Color words"]
213 "--word-diff*" {
214 if {[package vcompare $git_version "1.7.2"] >= 0} {
215 set worddiff [mc "Markup words"]
218 "--stat=*" - "--numstat" - "--shortstat" - "--summary" -
219 "--check" - "--exit-code" - "--quiet" - "--topo-order" -
220 "--full-history" - "--dense" - "--sparse" -
221 "--follow" - "--left-right" - "--encoding=*" {
222 # These are harmless, and some are even useful
223 lappend glflags $arg
225 "--diff-filter=*" - "--no-merges" - "--unpacked" -
226 "--max-count=*" - "--skip=*" - "--since=*" - "--after=*" -
227 "--until=*" - "--before=*" - "--max-age=*" - "--min-age=*" -
228 "--author=*" - "--committer=*" - "--grep=*" - "-[iE]" -
229 "--remove-empty" - "--first-parent" - "--cherry-pick" -
230 "-S*" - "--pickaxe-all" - "--pickaxe-regex" -
231 "--simplify-by-decoration" {
232 # These mean that we get a subset of the commits
233 set filtered 1
234 lappend glflags $arg
236 "-n" {
237 # This appears to be the only one that has a value as a
238 # separate word following it
239 set filtered 1
240 set nextisval 1
241 lappend glflags $arg
243 "--not" - "--all" {
244 lappend revargs $arg
246 "--merge" {
247 set vmergeonly($n) 1
248 # git rev-parse doesn't understand --merge
249 lappend revargs --gitk-symmetric-diff-marker MERGE_HEAD...HEAD
251 "--no-replace-objects" {
252 set env(GIT_NO_REPLACE_OBJECTS) "1"
254 "-*" {
255 # Other flag arguments including -<n>
256 if {[string is digit -strict [string range $arg 1 end]]} {
257 set filtered 1
258 } else {
259 # a flag argument that we don't recognize;
260 # that means we can't optimize
261 set allknown 0
263 lappend glflags $arg
265 default {
266 # Non-flag arguments specify commits or ranges of commits
267 if {[string match "*...*" $arg]} {
268 lappend revargs --gitk-symmetric-diff-marker
270 lappend revargs $arg
274 set vdflags($n) $diffargs
275 set vflags($n) $glflags
276 set vrevs($n) $revargs
277 set vfiltered($n) $filtered
278 set vorigargs($n) $origargs
279 return $allknown
282 proc parseviewrevs {view revs} {
283 global vposids vnegids
285 if {$revs eq {}} {
286 set revs HEAD
288 if {[catch {set ids [eval exec git rev-parse $revs]} err]} {
289 # we get stdout followed by stderr in $err
290 # for an unknown rev, git rev-parse echoes it and then errors out
291 set errlines [split $err "\n"]
292 set badrev {}
293 for {set l 0} {$l < [llength $errlines]} {incr l} {
294 set line [lindex $errlines $l]
295 if {!([string length $line] == 40 && [string is xdigit $line])} {
296 if {[string match "fatal:*" $line]} {
297 if {[string match "fatal: ambiguous argument*" $line]
298 && $badrev ne {}} {
299 if {[llength $badrev] == 1} {
300 set err "unknown revision $badrev"
301 } else {
302 set err "unknown revisions: [join $badrev ", "]"
304 } else {
305 set err [join [lrange $errlines $l end] "\n"]
307 break
309 lappend badrev $line
312 error_popup "[mc "Error parsing revisions:"] $err"
313 return {}
315 set ret {}
316 set pos {}
317 set neg {}
318 set sdm 0
319 foreach id [split $ids "\n"] {
320 if {$id eq "--gitk-symmetric-diff-marker"} {
321 set sdm 4
322 } elseif {[string match "^*" $id]} {
323 if {$sdm != 1} {
324 lappend ret $id
325 if {$sdm == 3} {
326 set sdm 0
329 lappend neg [string range $id 1 end]
330 } else {
331 if {$sdm != 2} {
332 lappend ret $id
333 } else {
334 lset ret end $id...[lindex $ret end]
336 lappend pos $id
338 incr sdm -1
340 set vposids($view) $pos
341 set vnegids($view) $neg
342 return $ret
345 # Start off a git log process and arrange to read its output
346 proc start_rev_list {view} {
347 global startmsecs commitidx viewcomplete curview
348 global tclencoding
349 global viewargs viewargscmd viewfiles vfilelimit
350 global showlocalchanges
351 global viewactive viewinstances vmergeonly
352 global mainheadid viewmainheadid viewmainheadid_orig
353 global vcanopt vflags vrevs vorigargs
354 global show_notes
356 set startmsecs [clock clicks -milliseconds]
357 set commitidx($view) 0
358 # these are set this way for the error exits
359 set viewcomplete($view) 1
360 set viewactive($view) 0
361 varcinit $view
363 set args $viewargs($view)
364 if {$viewargscmd($view) ne {}} {
365 if {[catch {
366 set str [exec sh -c $viewargscmd($view)]
367 } err]} {
368 error_popup "[mc "Error executing --argscmd command:"] $err"
369 return 0
371 set args [concat $args [split $str "\n"]]
373 set vcanopt($view) [parseviewargs $view $args]
375 set files $viewfiles($view)
376 if {$vmergeonly($view)} {
377 set files [unmerged_files $files]
378 if {$files eq {}} {
379 global nr_unmerged
380 if {$nr_unmerged == 0} {
381 error_popup [mc "No files selected: --merge specified but\
382 no files are unmerged."]
383 } else {
384 error_popup [mc "No files selected: --merge specified but\
385 no unmerged files are within file limit."]
387 return 0
390 set vfilelimit($view) $files
392 if {$vcanopt($view)} {
393 set revs [parseviewrevs $view $vrevs($view)]
394 if {$revs eq {}} {
395 return 0
397 set args [concat $vflags($view) $revs]
398 } else {
399 set args $vorigargs($view)
402 if {[catch {
403 set fd [open [concat | git log --no-color -z --pretty=raw $show_notes \
404 --parents --boundary $args "--" $files] r]
405 } err]} {
406 error_popup "[mc "Error executing git log:"] $err"
407 return 0
409 set i [reg_instance $fd]
410 set viewinstances($view) [list $i]
411 set viewmainheadid($view) $mainheadid
412 set viewmainheadid_orig($view) $mainheadid
413 if {$files ne {} && $mainheadid ne {}} {
414 get_viewmainhead $view
416 if {$showlocalchanges && $viewmainheadid($view) ne {}} {
417 interestedin $viewmainheadid($view) dodiffindex
419 fconfigure $fd -blocking 0 -translation lf -eofchar {}
420 if {$tclencoding != {}} {
421 fconfigure $fd -encoding $tclencoding
423 filerun $fd [list getcommitlines $fd $i $view 0]
424 nowbusy $view [mc "Reading"]
425 set viewcomplete($view) 0
426 set viewactive($view) 1
427 return 1
430 proc stop_instance {inst} {
431 global commfd leftover
433 set fd $commfd($inst)
434 catch {
435 set pid [pid $fd]
437 if {$::tcl_platform(platform) eq {windows}} {
438 exec kill -f $pid
439 } else {
440 exec kill $pid
443 catch {close $fd}
444 nukefile $fd
445 unset commfd($inst)
446 unset leftover($inst)
449 proc stop_backends {} {
450 global commfd
452 foreach inst [array names commfd] {
453 stop_instance $inst
457 proc stop_rev_list {view} {
458 global viewinstances
460 foreach inst $viewinstances($view) {
461 stop_instance $inst
463 set viewinstances($view) {}
466 proc reset_pending_select {selid} {
467 global pending_select mainheadid selectheadid
469 if {$selid ne {}} {
470 set pending_select $selid
471 } elseif {$selectheadid ne {}} {
472 set pending_select $selectheadid
473 } else {
474 set pending_select $mainheadid
478 proc getcommits {selid} {
479 global canv curview need_redisplay viewactive
481 initlayout
482 if {[start_rev_list $curview]} {
483 reset_pending_select $selid
484 show_status [mc "Reading commits..."]
485 set need_redisplay 1
486 } else {
487 show_status [mc "No commits selected"]
491 proc updatecommits {} {
492 global curview vcanopt vorigargs vfilelimit viewinstances
493 global viewactive viewcomplete tclencoding
494 global startmsecs showneartags showlocalchanges
495 global mainheadid viewmainheadid viewmainheadid_orig pending_select
496 global hasworktree
497 global varcid vposids vnegids vflags vrevs
498 global show_notes
500 set hasworktree [hasworktree]
501 rereadrefs
502 set view $curview
503 if {$mainheadid ne $viewmainheadid_orig($view)} {
504 if {$showlocalchanges} {
505 dohidelocalchanges
507 set viewmainheadid($view) $mainheadid
508 set viewmainheadid_orig($view) $mainheadid
509 if {$vfilelimit($view) ne {}} {
510 get_viewmainhead $view
513 if {$showlocalchanges} {
514 doshowlocalchanges
516 if {$vcanopt($view)} {
517 set oldpos $vposids($view)
518 set oldneg $vnegids($view)
519 set revs [parseviewrevs $view $vrevs($view)]
520 if {$revs eq {}} {
521 return
523 # note: getting the delta when negative refs change is hard,
524 # and could require multiple git log invocations, so in that
525 # case we ask git log for all the commits (not just the delta)
526 if {$oldneg eq $vnegids($view)} {
527 set newrevs {}
528 set npos 0
529 # take out positive refs that we asked for before or
530 # that we have already seen
531 foreach rev $revs {
532 if {[string length $rev] == 40} {
533 if {[lsearch -exact $oldpos $rev] < 0
534 && ![info exists varcid($view,$rev)]} {
535 lappend newrevs $rev
536 incr npos
538 } else {
539 lappend $newrevs $rev
542 if {$npos == 0} return
543 set revs $newrevs
544 set vposids($view) [lsort -unique [concat $oldpos $vposids($view)]]
546 set args [concat $vflags($view) $revs --not $oldpos]
547 } else {
548 set args $vorigargs($view)
550 if {[catch {
551 set fd [open [concat | git log --no-color -z --pretty=raw $show_notes \
552 --parents --boundary $args "--" $vfilelimit($view)] r]
553 } err]} {
554 error_popup "[mc "Error executing git log:"] $err"
555 return
557 if {$viewactive($view) == 0} {
558 set startmsecs [clock clicks -milliseconds]
560 set i [reg_instance $fd]
561 lappend viewinstances($view) $i
562 fconfigure $fd -blocking 0 -translation lf -eofchar {}
563 if {$tclencoding != {}} {
564 fconfigure $fd -encoding $tclencoding
566 filerun $fd [list getcommitlines $fd $i $view 1]
567 incr viewactive($view)
568 set viewcomplete($view) 0
569 reset_pending_select {}
570 nowbusy $view [mc "Reading"]
571 if {$showneartags} {
572 getallcommits
576 proc reloadcommits {} {
577 global curview viewcomplete selectedline currentid thickerline
578 global showneartags treediffs commitinterest cached_commitrow
579 global targetid
581 set selid {}
582 if {$selectedline ne {}} {
583 set selid $currentid
586 if {!$viewcomplete($curview)} {
587 stop_rev_list $curview
589 resetvarcs $curview
590 set selectedline {}
591 catch {unset currentid}
592 catch {unset thickerline}
593 catch {unset treediffs}
594 readrefs
595 changedrefs
596 if {$showneartags} {
597 getallcommits
599 clear_display
600 catch {unset commitinterest}
601 catch {unset cached_commitrow}
602 catch {unset targetid}
603 setcanvscroll
604 getcommits $selid
605 return 0
608 # This makes a string representation of a positive integer which
609 # sorts as a string in numerical order
610 proc strrep {n} {
611 if {$n < 16} {
612 return [format "%x" $n]
613 } elseif {$n < 256} {
614 return [format "x%.2x" $n]
615 } elseif {$n < 65536} {
616 return [format "y%.4x" $n]
618 return [format "z%.8x" $n]
621 # Procedures used in reordering commits from git log (without
622 # --topo-order) into the order for display.
624 proc varcinit {view} {
625 global varcstart vupptr vdownptr vleftptr vbackptr varctok varcrow
626 global vtokmod varcmod vrowmod varcix vlastins
628 set varcstart($view) {{}}
629 set vupptr($view) {0}
630 set vdownptr($view) {0}
631 set vleftptr($view) {0}
632 set vbackptr($view) {0}
633 set varctok($view) {{}}
634 set varcrow($view) {{}}
635 set vtokmod($view) {}
636 set varcmod($view) 0
637 set vrowmod($view) 0
638 set varcix($view) {{}}
639 set vlastins($view) {0}
642 proc resetvarcs {view} {
643 global varcid varccommits parents children vseedcount ordertok
644 global vshortids
646 foreach vid [array names varcid $view,*] {
647 unset varcid($vid)
648 unset children($vid)
649 unset parents($vid)
651 foreach vid [array names vshortids $view,*] {
652 unset vshortids($vid)
654 # some commits might have children but haven't been seen yet
655 foreach vid [array names children $view,*] {
656 unset children($vid)
658 foreach va [array names varccommits $view,*] {
659 unset varccommits($va)
661 foreach vd [array names vseedcount $view,*] {
662 unset vseedcount($vd)
664 catch {unset ordertok}
667 # returns a list of the commits with no children
668 proc seeds {v} {
669 global vdownptr vleftptr varcstart
671 set ret {}
672 set a [lindex $vdownptr($v) 0]
673 while {$a != 0} {
674 lappend ret [lindex $varcstart($v) $a]
675 set a [lindex $vleftptr($v) $a]
677 return $ret
680 proc newvarc {view id} {
681 global varcid varctok parents children vdatemode
682 global vupptr vdownptr vleftptr vbackptr varcrow varcix varcstart
683 global commitdata commitinfo vseedcount varccommits vlastins
685 set a [llength $varctok($view)]
686 set vid $view,$id
687 if {[llength $children($vid)] == 0 || $vdatemode($view)} {
688 if {![info exists commitinfo($id)]} {
689 parsecommit $id $commitdata($id) 1
691 set cdate [lindex [lindex $commitinfo($id) 4] 0]
692 if {![string is integer -strict $cdate]} {
693 set cdate 0
695 if {![info exists vseedcount($view,$cdate)]} {
696 set vseedcount($view,$cdate) -1
698 set c [incr vseedcount($view,$cdate)]
699 set cdate [expr {$cdate ^ 0xffffffff}]
700 set tok "s[strrep $cdate][strrep $c]"
701 } else {
702 set tok {}
704 set ka 0
705 if {[llength $children($vid)] > 0} {
706 set kid [lindex $children($vid) end]
707 set k $varcid($view,$kid)
708 if {[string compare [lindex $varctok($view) $k] $tok] > 0} {
709 set ki $kid
710 set ka $k
711 set tok [lindex $varctok($view) $k]
714 if {$ka != 0} {
715 set i [lsearch -exact $parents($view,$ki) $id]
716 set j [expr {[llength $parents($view,$ki)] - 1 - $i}]
717 append tok [strrep $j]
719 set c [lindex $vlastins($view) $ka]
720 if {$c == 0 || [string compare $tok [lindex $varctok($view) $c]] < 0} {
721 set c $ka
722 set b [lindex $vdownptr($view) $ka]
723 } else {
724 set b [lindex $vleftptr($view) $c]
726 while {$b != 0 && [string compare $tok [lindex $varctok($view) $b]] >= 0} {
727 set c $b
728 set b [lindex $vleftptr($view) $c]
730 if {$c == $ka} {
731 lset vdownptr($view) $ka $a
732 lappend vbackptr($view) 0
733 } else {
734 lset vleftptr($view) $c $a
735 lappend vbackptr($view) $c
737 lset vlastins($view) $ka $a
738 lappend vupptr($view) $ka
739 lappend vleftptr($view) $b
740 if {$b != 0} {
741 lset vbackptr($view) $b $a
743 lappend varctok($view) $tok
744 lappend varcstart($view) $id
745 lappend vdownptr($view) 0
746 lappend varcrow($view) {}
747 lappend varcix($view) {}
748 set varccommits($view,$a) {}
749 lappend vlastins($view) 0
750 return $a
753 proc splitvarc {p v} {
754 global varcid varcstart varccommits varctok vtokmod
755 global vupptr vdownptr vleftptr vbackptr varcix varcrow vlastins
757 set oa $varcid($v,$p)
758 set otok [lindex $varctok($v) $oa]
759 set ac $varccommits($v,$oa)
760 set i [lsearch -exact $varccommits($v,$oa) $p]
761 if {$i <= 0} return
762 set na [llength $varctok($v)]
763 # "%" sorts before "0"...
764 set tok "$otok%[strrep $i]"
765 lappend varctok($v) $tok
766 lappend varcrow($v) {}
767 lappend varcix($v) {}
768 set varccommits($v,$oa) [lrange $ac 0 [expr {$i - 1}]]
769 set varccommits($v,$na) [lrange $ac $i end]
770 lappend varcstart($v) $p
771 foreach id $varccommits($v,$na) {
772 set varcid($v,$id) $na
774 lappend vdownptr($v) [lindex $vdownptr($v) $oa]
775 lappend vlastins($v) [lindex $vlastins($v) $oa]
776 lset vdownptr($v) $oa $na
777 lset vlastins($v) $oa 0
778 lappend vupptr($v) $oa
779 lappend vleftptr($v) 0
780 lappend vbackptr($v) 0
781 for {set b [lindex $vdownptr($v) $na]} {$b != 0} {set b [lindex $vleftptr($v) $b]} {
782 lset vupptr($v) $b $na
784 if {[string compare $otok $vtokmod($v)] <= 0} {
785 modify_arc $v $oa
789 proc renumbervarc {a v} {
790 global parents children varctok varcstart varccommits
791 global vupptr vdownptr vleftptr vbackptr vlastins varcid vtokmod vdatemode
793 set t1 [clock clicks -milliseconds]
794 set todo {}
795 set isrelated($a) 1
796 set kidchanged($a) 1
797 set ntot 0
798 while {$a != 0} {
799 if {[info exists isrelated($a)]} {
800 lappend todo $a
801 set id [lindex $varccommits($v,$a) end]
802 foreach p $parents($v,$id) {
803 if {[info exists varcid($v,$p)]} {
804 set isrelated($varcid($v,$p)) 1
808 incr ntot
809 set b [lindex $vdownptr($v) $a]
810 if {$b == 0} {
811 while {$a != 0} {
812 set b [lindex $vleftptr($v) $a]
813 if {$b != 0} break
814 set a [lindex $vupptr($v) $a]
817 set a $b
819 foreach a $todo {
820 if {![info exists kidchanged($a)]} continue
821 set id [lindex $varcstart($v) $a]
822 if {[llength $children($v,$id)] > 1} {
823 set children($v,$id) [lsort -command [list vtokcmp $v] \
824 $children($v,$id)]
826 set oldtok [lindex $varctok($v) $a]
827 if {!$vdatemode($v)} {
828 set tok {}
829 } else {
830 set tok $oldtok
832 set ka 0
833 set kid [last_real_child $v,$id]
834 if {$kid ne {}} {
835 set k $varcid($v,$kid)
836 if {[string compare [lindex $varctok($v) $k] $tok] > 0} {
837 set ki $kid
838 set ka $k
839 set tok [lindex $varctok($v) $k]
842 if {$ka != 0} {
843 set i [lsearch -exact $parents($v,$ki) $id]
844 set j [expr {[llength $parents($v,$ki)] - 1 - $i}]
845 append tok [strrep $j]
847 if {$tok eq $oldtok} {
848 continue
850 set id [lindex $varccommits($v,$a) end]
851 foreach p $parents($v,$id) {
852 if {[info exists varcid($v,$p)]} {
853 set kidchanged($varcid($v,$p)) 1
854 } else {
855 set sortkids($p) 1
858 lset varctok($v) $a $tok
859 set b [lindex $vupptr($v) $a]
860 if {$b != $ka} {
861 if {[string compare [lindex $varctok($v) $ka] $vtokmod($v)] < 0} {
862 modify_arc $v $ka
864 if {[string compare [lindex $varctok($v) $b] $vtokmod($v)] < 0} {
865 modify_arc $v $b
867 set c [lindex $vbackptr($v) $a]
868 set d [lindex $vleftptr($v) $a]
869 if {$c == 0} {
870 lset vdownptr($v) $b $d
871 } else {
872 lset vleftptr($v) $c $d
874 if {$d != 0} {
875 lset vbackptr($v) $d $c
877 if {[lindex $vlastins($v) $b] == $a} {
878 lset vlastins($v) $b $c
880 lset vupptr($v) $a $ka
881 set c [lindex $vlastins($v) $ka]
882 if {$c == 0 || \
883 [string compare $tok [lindex $varctok($v) $c]] < 0} {
884 set c $ka
885 set b [lindex $vdownptr($v) $ka]
886 } else {
887 set b [lindex $vleftptr($v) $c]
889 while {$b != 0 && \
890 [string compare $tok [lindex $varctok($v) $b]] >= 0} {
891 set c $b
892 set b [lindex $vleftptr($v) $c]
894 if {$c == $ka} {
895 lset vdownptr($v) $ka $a
896 lset vbackptr($v) $a 0
897 } else {
898 lset vleftptr($v) $c $a
899 lset vbackptr($v) $a $c
901 lset vleftptr($v) $a $b
902 if {$b != 0} {
903 lset vbackptr($v) $b $a
905 lset vlastins($v) $ka $a
908 foreach id [array names sortkids] {
909 if {[llength $children($v,$id)] > 1} {
910 set children($v,$id) [lsort -command [list vtokcmp $v] \
911 $children($v,$id)]
914 set t2 [clock clicks -milliseconds]
915 #puts "renumbervarc did [llength $todo] of $ntot arcs in [expr {$t2-$t1}]ms"
918 # Fix up the graph after we have found out that in view $v,
919 # $p (a commit that we have already seen) is actually the parent
920 # of the last commit in arc $a.
921 proc fix_reversal {p a v} {
922 global varcid varcstart varctok vupptr
924 set pa $varcid($v,$p)
925 if {$p ne [lindex $varcstart($v) $pa]} {
926 splitvarc $p $v
927 set pa $varcid($v,$p)
929 # seeds always need to be renumbered
930 if {[lindex $vupptr($v) $pa] == 0 ||
931 [string compare [lindex $varctok($v) $a] \
932 [lindex $varctok($v) $pa]] > 0} {
933 renumbervarc $pa $v
937 proc insertrow {id p v} {
938 global cmitlisted children parents varcid varctok vtokmod
939 global varccommits ordertok commitidx numcommits curview
940 global targetid targetrow vshortids
942 readcommit $id
943 set vid $v,$id
944 set cmitlisted($vid) 1
945 set children($vid) {}
946 set parents($vid) [list $p]
947 set a [newvarc $v $id]
948 set varcid($vid) $a
949 lappend vshortids($v,[string range $id 0 3]) $id
950 if {[string compare [lindex $varctok($v) $a] $vtokmod($v)] < 0} {
951 modify_arc $v $a
953 lappend varccommits($v,$a) $id
954 set vp $v,$p
955 if {[llength [lappend children($vp) $id]] > 1} {
956 set children($vp) [lsort -command [list vtokcmp $v] $children($vp)]
957 catch {unset ordertok}
959 fix_reversal $p $a $v
960 incr commitidx($v)
961 if {$v == $curview} {
962 set numcommits $commitidx($v)
963 setcanvscroll
964 if {[info exists targetid]} {
965 if {![comes_before $targetid $p]} {
966 incr targetrow
972 proc insertfakerow {id p} {
973 global varcid varccommits parents children cmitlisted
974 global commitidx varctok vtokmod targetid targetrow curview numcommits
976 set v $curview
977 set a $varcid($v,$p)
978 set i [lsearch -exact $varccommits($v,$a) $p]
979 if {$i < 0} {
980 puts "oops: insertfakerow can't find [shortids $p] on arc $a"
981 return
983 set children($v,$id) {}
984 set parents($v,$id) [list $p]
985 set varcid($v,$id) $a
986 lappend children($v,$p) $id
987 set cmitlisted($v,$id) 1
988 set numcommits [incr commitidx($v)]
989 # note we deliberately don't update varcstart($v) even if $i == 0
990 set varccommits($v,$a) [linsert $varccommits($v,$a) $i $id]
991 modify_arc $v $a $i
992 if {[info exists targetid]} {
993 if {![comes_before $targetid $p]} {
994 incr targetrow
997 setcanvscroll
998 drawvisible
1001 proc removefakerow {id} {
1002 global varcid varccommits parents children commitidx
1003 global varctok vtokmod cmitlisted currentid selectedline
1004 global targetid curview numcommits
1006 set v $curview
1007 if {[llength $parents($v,$id)] != 1} {
1008 puts "oops: removefakerow [shortids $id] has [llength $parents($v,$id)] parents"
1009 return
1011 set p [lindex $parents($v,$id) 0]
1012 set a $varcid($v,$id)
1013 set i [lsearch -exact $varccommits($v,$a) $id]
1014 if {$i < 0} {
1015 puts "oops: removefakerow can't find [shortids $id] on arc $a"
1016 return
1018 unset varcid($v,$id)
1019 set varccommits($v,$a) [lreplace $varccommits($v,$a) $i $i]
1020 unset parents($v,$id)
1021 unset children($v,$id)
1022 unset cmitlisted($v,$id)
1023 set numcommits [incr commitidx($v) -1]
1024 set j [lsearch -exact $children($v,$p) $id]
1025 if {$j >= 0} {
1026 set children($v,$p) [lreplace $children($v,$p) $j $j]
1028 modify_arc $v $a $i
1029 if {[info exist currentid] && $id eq $currentid} {
1030 unset currentid
1031 set selectedline {}
1033 if {[info exists targetid] && $targetid eq $id} {
1034 set targetid $p
1036 setcanvscroll
1037 drawvisible
1040 proc real_children {vp} {
1041 global children nullid nullid2
1043 set kids {}
1044 foreach id $children($vp) {
1045 if {$id ne $nullid && $id ne $nullid2} {
1046 lappend kids $id
1049 return $kids
1052 proc first_real_child {vp} {
1053 global children nullid nullid2
1055 foreach id $children($vp) {
1056 if {$id ne $nullid && $id ne $nullid2} {
1057 return $id
1060 return {}
1063 proc last_real_child {vp} {
1064 global children nullid nullid2
1066 set kids $children($vp)
1067 for {set i [llength $kids]} {[incr i -1] >= 0} {} {
1068 set id [lindex $kids $i]
1069 if {$id ne $nullid && $id ne $nullid2} {
1070 return $id
1073 return {}
1076 proc vtokcmp {v a b} {
1077 global varctok varcid
1079 return [string compare [lindex $varctok($v) $varcid($v,$a)] \
1080 [lindex $varctok($v) $varcid($v,$b)]]
1083 # This assumes that if lim is not given, the caller has checked that
1084 # arc a's token is less than $vtokmod($v)
1085 proc modify_arc {v a {lim {}}} {
1086 global varctok vtokmod varcmod varcrow vupptr curview vrowmod varccommits
1088 if {$lim ne {}} {
1089 set c [string compare [lindex $varctok($v) $a] $vtokmod($v)]
1090 if {$c > 0} return
1091 if {$c == 0} {
1092 set r [lindex $varcrow($v) $a]
1093 if {$r ne {} && $vrowmod($v) <= $r + $lim} return
1096 set vtokmod($v) [lindex $varctok($v) $a]
1097 set varcmod($v) $a
1098 if {$v == $curview} {
1099 while {$a != 0 && [lindex $varcrow($v) $a] eq {}} {
1100 set a [lindex $vupptr($v) $a]
1101 set lim {}
1103 set r 0
1104 if {$a != 0} {
1105 if {$lim eq {}} {
1106 set lim [llength $varccommits($v,$a)]
1108 set r [expr {[lindex $varcrow($v) $a] + $lim}]
1110 set vrowmod($v) $r
1111 undolayout $r
1115 proc update_arcrows {v} {
1116 global vtokmod varcmod vrowmod varcrow commitidx currentid selectedline
1117 global varcid vrownum varcorder varcix varccommits
1118 global vupptr vdownptr vleftptr varctok
1119 global displayorder parentlist curview cached_commitrow
1121 if {$vrowmod($v) == $commitidx($v)} return
1122 if {$v == $curview} {
1123 if {[llength $displayorder] > $vrowmod($v)} {
1124 set displayorder [lrange $displayorder 0 [expr {$vrowmod($v) - 1}]]
1125 set parentlist [lrange $parentlist 0 [expr {$vrowmod($v) - 1}]]
1127 catch {unset cached_commitrow}
1129 set narctot [expr {[llength $varctok($v)] - 1}]
1130 set a $varcmod($v)
1131 while {$a != 0 && [lindex $varcix($v) $a] eq {}} {
1132 # go up the tree until we find something that has a row number,
1133 # or we get to a seed
1134 set a [lindex $vupptr($v) $a]
1136 if {$a == 0} {
1137 set a [lindex $vdownptr($v) 0]
1138 if {$a == 0} return
1139 set vrownum($v) {0}
1140 set varcorder($v) [list $a]
1141 lset varcix($v) $a 0
1142 lset varcrow($v) $a 0
1143 set arcn 0
1144 set row 0
1145 } else {
1146 set arcn [lindex $varcix($v) $a]
1147 if {[llength $vrownum($v)] > $arcn + 1} {
1148 set vrownum($v) [lrange $vrownum($v) 0 $arcn]
1149 set varcorder($v) [lrange $varcorder($v) 0 $arcn]
1151 set row [lindex $varcrow($v) $a]
1153 while {1} {
1154 set p $a
1155 incr row [llength $varccommits($v,$a)]
1156 # go down if possible
1157 set b [lindex $vdownptr($v) $a]
1158 if {$b == 0} {
1159 # if not, go left, or go up until we can go left
1160 while {$a != 0} {
1161 set b [lindex $vleftptr($v) $a]
1162 if {$b != 0} break
1163 set a [lindex $vupptr($v) $a]
1165 if {$a == 0} break
1167 set a $b
1168 incr arcn
1169 lappend vrownum($v) $row
1170 lappend varcorder($v) $a
1171 lset varcix($v) $a $arcn
1172 lset varcrow($v) $a $row
1174 set vtokmod($v) [lindex $varctok($v) $p]
1175 set varcmod($v) $p
1176 set vrowmod($v) $row
1177 if {[info exists currentid]} {
1178 set selectedline [rowofcommit $currentid]
1182 # Test whether view $v contains commit $id
1183 proc commitinview {id v} {
1184 global varcid
1186 return [info exists varcid($v,$id)]
1189 # Return the row number for commit $id in the current view
1190 proc rowofcommit {id} {
1191 global varcid varccommits varcrow curview cached_commitrow
1192 global varctok vtokmod
1194 set v $curview
1195 if {![info exists varcid($v,$id)]} {
1196 puts "oops rowofcommit no arc for [shortids $id]"
1197 return {}
1199 set a $varcid($v,$id)
1200 if {[string compare [lindex $varctok($v) $a] $vtokmod($v)] >= 0} {
1201 update_arcrows $v
1203 if {[info exists cached_commitrow($id)]} {
1204 return $cached_commitrow($id)
1206 set i [lsearch -exact $varccommits($v,$a) $id]
1207 if {$i < 0} {
1208 puts "oops didn't find commit [shortids $id] in arc $a"
1209 return {}
1211 incr i [lindex $varcrow($v) $a]
1212 set cached_commitrow($id) $i
1213 return $i
1216 # Returns 1 if a is on an earlier row than b, otherwise 0
1217 proc comes_before {a b} {
1218 global varcid varctok curview
1220 set v $curview
1221 if {$a eq $b || ![info exists varcid($v,$a)] || \
1222 ![info exists varcid($v,$b)]} {
1223 return 0
1225 if {$varcid($v,$a) != $varcid($v,$b)} {
1226 return [expr {[string compare [lindex $varctok($v) $varcid($v,$a)] \
1227 [lindex $varctok($v) $varcid($v,$b)]] < 0}]
1229 return [expr {[rowofcommit $a] < [rowofcommit $b]}]
1232 proc bsearch {l elt} {
1233 if {[llength $l] == 0 || $elt <= [lindex $l 0]} {
1234 return 0
1236 set lo 0
1237 set hi [llength $l]
1238 while {$hi - $lo > 1} {
1239 set mid [expr {int(($lo + $hi) / 2)}]
1240 set t [lindex $l $mid]
1241 if {$elt < $t} {
1242 set hi $mid
1243 } elseif {$elt > $t} {
1244 set lo $mid
1245 } else {
1246 return $mid
1249 return $lo
1252 # Make sure rows $start..$end-1 are valid in displayorder and parentlist
1253 proc make_disporder {start end} {
1254 global vrownum curview commitidx displayorder parentlist
1255 global varccommits varcorder parents vrowmod varcrow
1256 global d_valid_start d_valid_end
1258 if {$end > $vrowmod($curview)} {
1259 update_arcrows $curview
1261 set ai [bsearch $vrownum($curview) $start]
1262 set start [lindex $vrownum($curview) $ai]
1263 set narc [llength $vrownum($curview)]
1264 for {set r $start} {$ai < $narc && $r < $end} {incr ai} {
1265 set a [lindex $varcorder($curview) $ai]
1266 set l [llength $displayorder]
1267 set al [llength $varccommits($curview,$a)]
1268 if {$l < $r + $al} {
1269 if {$l < $r} {
1270 set pad [ntimes [expr {$r - $l}] {}]
1271 set displayorder [concat $displayorder $pad]
1272 set parentlist [concat $parentlist $pad]
1273 } elseif {$l > $r} {
1274 set displayorder [lrange $displayorder 0 [expr {$r - 1}]]
1275 set parentlist [lrange $parentlist 0 [expr {$r - 1}]]
1277 foreach id $varccommits($curview,$a) {
1278 lappend displayorder $id
1279 lappend parentlist $parents($curview,$id)
1281 } elseif {[lindex $displayorder [expr {$r + $al - 1}]] eq {}} {
1282 set i $r
1283 foreach id $varccommits($curview,$a) {
1284 lset displayorder $i $id
1285 lset parentlist $i $parents($curview,$id)
1286 incr i
1289 incr r $al
1293 proc commitonrow {row} {
1294 global displayorder
1296 set id [lindex $displayorder $row]
1297 if {$id eq {}} {
1298 make_disporder $row [expr {$row + 1}]
1299 set id [lindex $displayorder $row]
1301 return $id
1304 proc closevarcs {v} {
1305 global varctok varccommits varcid parents children
1306 global cmitlisted commitidx vtokmod
1308 set missing_parents 0
1309 set scripts {}
1310 set narcs [llength $varctok($v)]
1311 for {set a 1} {$a < $narcs} {incr a} {
1312 set id [lindex $varccommits($v,$a) end]
1313 foreach p $parents($v,$id) {
1314 if {[info exists varcid($v,$p)]} continue
1315 # add p as a new commit
1316 incr missing_parents
1317 set cmitlisted($v,$p) 0
1318 set parents($v,$p) {}
1319 if {[llength $children($v,$p)] == 1 &&
1320 [llength $parents($v,$id)] == 1} {
1321 set b $a
1322 } else {
1323 set b [newvarc $v $p]
1325 set varcid($v,$p) $b
1326 if {[string compare [lindex $varctok($v) $b] $vtokmod($v)] < 0} {
1327 modify_arc $v $b
1329 lappend varccommits($v,$b) $p
1330 incr commitidx($v)
1331 set scripts [check_interest $p $scripts]
1334 if {$missing_parents > 0} {
1335 foreach s $scripts {
1336 eval $s
1341 # Use $rwid as a substitute for $id, i.e. reparent $id's children to $rwid
1342 # Assumes we already have an arc for $rwid.
1343 proc rewrite_commit {v id rwid} {
1344 global children parents varcid varctok vtokmod varccommits
1346 foreach ch $children($v,$id) {
1347 # make $rwid be $ch's parent in place of $id
1348 set i [lsearch -exact $parents($v,$ch) $id]
1349 if {$i < 0} {
1350 puts "oops rewrite_commit didn't find $id in parent list for $ch"
1352 set parents($v,$ch) [lreplace $parents($v,$ch) $i $i $rwid]
1353 # add $ch to $rwid's children and sort the list if necessary
1354 if {[llength [lappend children($v,$rwid) $ch]] > 1} {
1355 set children($v,$rwid) [lsort -command [list vtokcmp $v] \
1356 $children($v,$rwid)]
1358 # fix the graph after joining $id to $rwid
1359 set a $varcid($v,$ch)
1360 fix_reversal $rwid $a $v
1361 # parentlist is wrong for the last element of arc $a
1362 # even if displayorder is right, hence the 3rd arg here
1363 modify_arc $v $a [expr {[llength $varccommits($v,$a)] - 1}]
1367 # Mechanism for registering a command to be executed when we come
1368 # across a particular commit. To handle the case when only the
1369 # prefix of the commit is known, the commitinterest array is now
1370 # indexed by the first 4 characters of the ID. Each element is a
1371 # list of id, cmd pairs.
1372 proc interestedin {id cmd} {
1373 global commitinterest
1375 lappend commitinterest([string range $id 0 3]) $id $cmd
1378 proc check_interest {id scripts} {
1379 global commitinterest
1381 set prefix [string range $id 0 3]
1382 if {[info exists commitinterest($prefix)]} {
1383 set newlist {}
1384 foreach {i script} $commitinterest($prefix) {
1385 if {[string match "$i*" $id]} {
1386 lappend scripts [string map [list "%I" $id "%P" $i] $script]
1387 } else {
1388 lappend newlist $i $script
1391 if {$newlist ne {}} {
1392 set commitinterest($prefix) $newlist
1393 } else {
1394 unset commitinterest($prefix)
1397 return $scripts
1400 proc getcommitlines {fd inst view updating} {
1401 global cmitlisted leftover
1402 global commitidx commitdata vdatemode
1403 global parents children curview hlview
1404 global idpending ordertok
1405 global varccommits varcid varctok vtokmod vfilelimit vshortids
1407 set stuff [read $fd 500000]
1408 # git log doesn't terminate the last commit with a null...
1409 if {$stuff == {} && $leftover($inst) ne {} && [eof $fd]} {
1410 set stuff "\0"
1412 if {$stuff == {}} {
1413 if {![eof $fd]} {
1414 return 1
1416 global commfd viewcomplete viewactive viewname
1417 global viewinstances
1418 unset commfd($inst)
1419 set i [lsearch -exact $viewinstances($view) $inst]
1420 if {$i >= 0} {
1421 set viewinstances($view) [lreplace $viewinstances($view) $i $i]
1423 # set it blocking so we wait for the process to terminate
1424 fconfigure $fd -blocking 1
1425 if {[catch {close $fd} err]} {
1426 set fv {}
1427 if {$view != $curview} {
1428 set fv " for the \"$viewname($view)\" view"
1430 if {[string range $err 0 4] == "usage"} {
1431 set err "Gitk: error reading commits$fv:\
1432 bad arguments to git log."
1433 if {$viewname($view) eq "Command line"} {
1434 append err \
1435 " (Note: arguments to gitk are passed to git log\
1436 to allow selection of commits to be displayed.)"
1438 } else {
1439 set err "Error reading commits$fv: $err"
1441 error_popup $err
1443 if {[incr viewactive($view) -1] <= 0} {
1444 set viewcomplete($view) 1
1445 # Check if we have seen any ids listed as parents that haven't
1446 # appeared in the list
1447 closevarcs $view
1448 notbusy $view
1450 if {$view == $curview} {
1451 run chewcommits
1453 return 0
1455 set start 0
1456 set gotsome 0
1457 set scripts {}
1458 while 1 {
1459 set i [string first "\0" $stuff $start]
1460 if {$i < 0} {
1461 append leftover($inst) [string range $stuff $start end]
1462 break
1464 if {$start == 0} {
1465 set cmit $leftover($inst)
1466 append cmit [string range $stuff 0 [expr {$i - 1}]]
1467 set leftover($inst) {}
1468 } else {
1469 set cmit [string range $stuff $start [expr {$i - 1}]]
1471 set start [expr {$i + 1}]
1472 set j [string first "\n" $cmit]
1473 set ok 0
1474 set listed 1
1475 if {$j >= 0 && [string match "commit *" $cmit]} {
1476 set ids [string range $cmit 7 [expr {$j - 1}]]
1477 if {[string match {[-^<>]*} $ids]} {
1478 switch -- [string index $ids 0] {
1479 "-" {set listed 0}
1480 "^" {set listed 2}
1481 "<" {set listed 3}
1482 ">" {set listed 4}
1484 set ids [string range $ids 1 end]
1486 set ok 1
1487 foreach id $ids {
1488 if {[string length $id] != 40} {
1489 set ok 0
1490 break
1494 if {!$ok} {
1495 set shortcmit $cmit
1496 if {[string length $shortcmit] > 80} {
1497 set shortcmit "[string range $shortcmit 0 80]..."
1499 error_popup "[mc "Can't parse git log output:"] {$shortcmit}"
1500 exit 1
1502 set id [lindex $ids 0]
1503 set vid $view,$id
1505 lappend vshortids($view,[string range $id 0 3]) $id
1507 if {!$listed && $updating && ![info exists varcid($vid)] &&
1508 $vfilelimit($view) ne {}} {
1509 # git log doesn't rewrite parents for unlisted commits
1510 # when doing path limiting, so work around that here
1511 # by working out the rewritten parent with git rev-list
1512 # and if we already know about it, using the rewritten
1513 # parent as a substitute parent for $id's children.
1514 if {![catch {
1515 set rwid [exec git rev-list --first-parent --max-count=1 \
1516 $id -- $vfilelimit($view)]
1517 }]} {
1518 if {$rwid ne {} && [info exists varcid($view,$rwid)]} {
1519 # use $rwid in place of $id
1520 rewrite_commit $view $id $rwid
1521 continue
1526 set a 0
1527 if {[info exists varcid($vid)]} {
1528 if {$cmitlisted($vid) || !$listed} continue
1529 set a $varcid($vid)
1531 if {$listed} {
1532 set olds [lrange $ids 1 end]
1533 } else {
1534 set olds {}
1536 set commitdata($id) [string range $cmit [expr {$j + 1}] end]
1537 set cmitlisted($vid) $listed
1538 set parents($vid) $olds
1539 if {![info exists children($vid)]} {
1540 set children($vid) {}
1541 } elseif {$a == 0 && [llength $children($vid)] == 1} {
1542 set k [lindex $children($vid) 0]
1543 if {[llength $parents($view,$k)] == 1 &&
1544 (!$vdatemode($view) ||
1545 $varcid($view,$k) == [llength $varctok($view)] - 1)} {
1546 set a $varcid($view,$k)
1549 if {$a == 0} {
1550 # new arc
1551 set a [newvarc $view $id]
1553 if {[string compare [lindex $varctok($view) $a] $vtokmod($view)] < 0} {
1554 modify_arc $view $a
1556 if {![info exists varcid($vid)]} {
1557 set varcid($vid) $a
1558 lappend varccommits($view,$a) $id
1559 incr commitidx($view)
1562 set i 0
1563 foreach p $olds {
1564 if {$i == 0 || [lsearch -exact $olds $p] >= $i} {
1565 set vp $view,$p
1566 if {[llength [lappend children($vp) $id]] > 1 &&
1567 [vtokcmp $view [lindex $children($vp) end-1] $id] > 0} {
1568 set children($vp) [lsort -command [list vtokcmp $view] \
1569 $children($vp)]
1570 catch {unset ordertok}
1572 if {[info exists varcid($view,$p)]} {
1573 fix_reversal $p $a $view
1576 incr i
1579 set scripts [check_interest $id $scripts]
1580 set gotsome 1
1582 if {$gotsome} {
1583 global numcommits hlview
1585 if {$view == $curview} {
1586 set numcommits $commitidx($view)
1587 run chewcommits
1589 if {[info exists hlview] && $view == $hlview} {
1590 # we never actually get here...
1591 run vhighlightmore
1593 foreach s $scripts {
1594 eval $s
1597 return 2
1600 proc chewcommits {} {
1601 global curview hlview viewcomplete
1602 global pending_select
1604 layoutmore
1605 if {$viewcomplete($curview)} {
1606 global commitidx varctok
1607 global numcommits startmsecs
1609 if {[info exists pending_select]} {
1610 update
1611 reset_pending_select {}
1613 if {[commitinview $pending_select $curview]} {
1614 selectline [rowofcommit $pending_select] 1
1615 } else {
1616 set row [first_real_row]
1617 selectline $row 1
1620 if {$commitidx($curview) > 0} {
1621 #set ms [expr {[clock clicks -milliseconds] - $startmsecs}]
1622 #puts "overall $ms ms for $numcommits commits"
1623 #puts "[llength $varctok($view)] arcs, $commitidx($view) commits"
1624 } else {
1625 show_status [mc "No commits selected"]
1627 notbusy layout
1629 return 0
1632 proc do_readcommit {id} {
1633 global tclencoding
1635 # Invoke git-log to handle automatic encoding conversion
1636 set fd [open [concat | git log --no-color --pretty=raw -1 $id] r]
1637 # Read the results using i18n.logoutputencoding
1638 fconfigure $fd -translation lf -eofchar {}
1639 if {$tclencoding != {}} {
1640 fconfigure $fd -encoding $tclencoding
1642 set contents [read $fd]
1643 close $fd
1644 # Remove the heading line
1645 regsub {^commit [0-9a-f]+\n} $contents {} contents
1647 return $contents
1650 proc readcommit {id} {
1651 if {[catch {set contents [do_readcommit $id]}]} return
1652 parsecommit $id $contents 1
1655 proc parsecommit {id contents listed} {
1656 global commitinfo
1658 set inhdr 1
1659 set comment {}
1660 set headline {}
1661 set auname {}
1662 set audate {}
1663 set comname {}
1664 set comdate {}
1665 set hdrend [string first "\n\n" $contents]
1666 if {$hdrend < 0} {
1667 # should never happen...
1668 set hdrend [string length $contents]
1670 set header [string range $contents 0 [expr {$hdrend - 1}]]
1671 set comment [string range $contents [expr {$hdrend + 2}] end]
1672 foreach line [split $header "\n"] {
1673 set line [split $line " "]
1674 set tag [lindex $line 0]
1675 if {$tag == "author"} {
1676 set audate [lrange $line end-1 end]
1677 set auname [join [lrange $line 1 end-2] " "]
1678 } elseif {$tag == "committer"} {
1679 set comdate [lrange $line end-1 end]
1680 set comname [join [lrange $line 1 end-2] " "]
1683 set headline {}
1684 # take the first non-blank line of the comment as the headline
1685 set headline [string trimleft $comment]
1686 set i [string first "\n" $headline]
1687 if {$i >= 0} {
1688 set headline [string range $headline 0 $i]
1690 set headline [string trimright $headline]
1691 set i [string first "\r" $headline]
1692 if {$i >= 0} {
1693 set headline [string trimright [string range $headline 0 $i]]
1695 if {!$listed} {
1696 # git log indents the comment by 4 spaces;
1697 # if we got this via git cat-file, add the indentation
1698 set newcomment {}
1699 foreach line [split $comment "\n"] {
1700 append newcomment " "
1701 append newcomment $line
1702 append newcomment "\n"
1704 set comment $newcomment
1706 set hasnote [string first "\nNotes:\n" $contents]
1707 set commitinfo($id) [list $headline $auname $audate \
1708 $comname $comdate $comment $hasnote]
1711 proc getcommit {id} {
1712 global commitdata commitinfo
1714 if {[info exists commitdata($id)]} {
1715 parsecommit $id $commitdata($id) 1
1716 } else {
1717 readcommit $id
1718 if {![info exists commitinfo($id)]} {
1719 set commitinfo($id) [list [mc "No commit information available"]]
1722 return 1
1725 # Expand an abbreviated commit ID to a list of full 40-char IDs that match
1726 # and are present in the current view.
1727 # This is fairly slow...
1728 proc longid {prefix} {
1729 global varcid curview vshortids
1731 set ids {}
1732 if {[string length $prefix] >= 4} {
1733 set vshortid $curview,[string range $prefix 0 3]
1734 if {[info exists vshortids($vshortid)]} {
1735 foreach id $vshortids($vshortid) {
1736 if {[string match "$prefix*" $id]} {
1737 if {[lsearch -exact $ids $id] < 0} {
1738 lappend ids $id
1739 if {[llength $ids] >= 2} break
1744 } else {
1745 foreach match [array names varcid "$curview,$prefix*"] {
1746 lappend ids [lindex [split $match ","] 1]
1747 if {[llength $ids] >= 2} break
1750 return $ids
1753 proc readrefs {} {
1754 global tagids idtags headids idheads tagobjid
1755 global otherrefids idotherrefs mainhead mainheadid
1756 global selecthead selectheadid
1757 global hideremotes
1759 foreach v {tagids idtags headids idheads otherrefids idotherrefs} {
1760 catch {unset $v}
1762 set refd [open [list | git show-ref -d] r]
1763 while {[gets $refd line] >= 0} {
1764 if {[string index $line 40] ne " "} continue
1765 set id [string range $line 0 39]
1766 set ref [string range $line 41 end]
1767 if {![string match "refs/*" $ref]} continue
1768 set name [string range $ref 5 end]
1769 if {[string match "remotes/*" $name]} {
1770 if {![string match "*/HEAD" $name] && !$hideremotes} {
1771 set headids($name) $id
1772 lappend idheads($id) $name
1774 } elseif {[string match "heads/*" $name]} {
1775 set name [string range $name 6 end]
1776 set headids($name) $id
1777 lappend idheads($id) $name
1778 } elseif {[string match "tags/*" $name]} {
1779 # this lets refs/tags/foo^{} overwrite refs/tags/foo,
1780 # which is what we want since the former is the commit ID
1781 set name [string range $name 5 end]
1782 if {[string match "*^{}" $name]} {
1783 set name [string range $name 0 end-3]
1784 } else {
1785 set tagobjid($name) $id
1787 set tagids($name) $id
1788 lappend idtags($id) $name
1789 } else {
1790 set otherrefids($name) $id
1791 lappend idotherrefs($id) $name
1794 catch {close $refd}
1795 set mainhead {}
1796 set mainheadid {}
1797 catch {
1798 set mainheadid [exec git rev-parse HEAD]
1799 set thehead [exec git symbolic-ref HEAD]
1800 if {[string match "refs/heads/*" $thehead]} {
1801 set mainhead [string range $thehead 11 end]
1804 set selectheadid {}
1805 if {$selecthead ne {}} {
1806 catch {
1807 set selectheadid [exec git rev-parse --verify $selecthead]
1812 # skip over fake commits
1813 proc first_real_row {} {
1814 global nullid nullid2 numcommits
1816 for {set row 0} {$row < $numcommits} {incr row} {
1817 set id [commitonrow $row]
1818 if {$id ne $nullid && $id ne $nullid2} {
1819 break
1822 return $row
1825 # update things for a head moved to a child of its previous location
1826 proc movehead {id name} {
1827 global headids idheads
1829 removehead $headids($name) $name
1830 set headids($name) $id
1831 lappend idheads($id) $name
1834 # update things when a head has been removed
1835 proc removehead {id name} {
1836 global headids idheads
1838 if {$idheads($id) eq $name} {
1839 unset idheads($id)
1840 } else {
1841 set i [lsearch -exact $idheads($id) $name]
1842 if {$i >= 0} {
1843 set idheads($id) [lreplace $idheads($id) $i $i]
1846 unset headids($name)
1849 proc ttk_toplevel {w args} {
1850 global use_ttk
1851 eval [linsert $args 0 ::toplevel $w]
1852 if {$use_ttk} {
1853 place [ttk::frame $w._toplevel_background] -x 0 -y 0 -relwidth 1 -relheight 1
1855 return $w
1858 proc make_transient {window origin} {
1859 global have_tk85
1861 # In MacOS Tk 8.4 transient appears to work by setting
1862 # overrideredirect, which is utterly useless, since the
1863 # windows get no border, and are not even kept above
1864 # the parent.
1865 if {!$have_tk85 && [tk windowingsystem] eq {aqua}} return
1867 wm transient $window $origin
1869 # Windows fails to place transient windows normally, so
1870 # schedule a callback to center them on the parent.
1871 if {[tk windowingsystem] eq {win32}} {
1872 after idle [list tk::PlaceWindow $window widget $origin]
1876 proc show_error {w top msg {mc mc}} {
1877 global NS
1878 if {![info exists NS]} {set NS ""}
1879 if {[wm state $top] eq "withdrawn"} { wm deiconify $top }
1880 message $w.m -text $msg -justify center -aspect 400
1881 pack $w.m -side top -fill x -padx 20 -pady 20
1882 ${NS}::button $w.ok -default active -text [$mc OK] -command "destroy $top"
1883 pack $w.ok -side bottom -fill x
1884 bind $top <Visibility> "grab $top; focus $top"
1885 bind $top <Key-Return> "destroy $top"
1886 bind $top <Key-space> "destroy $top"
1887 bind $top <Key-Escape> "destroy $top"
1888 tkwait window $top
1891 proc error_popup {msg {owner .}} {
1892 if {[tk windowingsystem] eq "win32"} {
1893 tk_messageBox -icon error -type ok -title [wm title .] \
1894 -parent $owner -message $msg
1895 } else {
1896 set w .error
1897 ttk_toplevel $w
1898 make_transient $w $owner
1899 show_error $w $w $msg
1903 proc confirm_popup {msg {owner .}} {
1904 global confirm_ok NS
1905 set confirm_ok 0
1906 set w .confirm
1907 ttk_toplevel $w
1908 make_transient $w $owner
1909 message $w.m -text $msg -justify center -aspect 400
1910 pack $w.m -side top -fill x -padx 20 -pady 20
1911 ${NS}::button $w.ok -text [mc OK] -command "set confirm_ok 1; destroy $w"
1912 pack $w.ok -side left -fill x
1913 ${NS}::button $w.cancel -text [mc Cancel] -command "destroy $w"
1914 pack $w.cancel -side right -fill x
1915 bind $w <Visibility> "grab $w; focus $w"
1916 bind $w <Key-Return> "set confirm_ok 1; destroy $w"
1917 bind $w <Key-space> "set confirm_ok 1; destroy $w"
1918 bind $w <Key-Escape> "destroy $w"
1919 tk::PlaceWindow $w widget $owner
1920 tkwait window $w
1921 return $confirm_ok
1924 proc setoptions {} {
1925 if {[tk windowingsystem] ne "win32"} {
1926 option add *Panedwindow.showHandle 1 startupFile
1927 option add *Panedwindow.sashRelief raised startupFile
1928 if {[tk windowingsystem] ne "aqua"} {
1929 option add *Menu.font uifont startupFile
1931 } else {
1932 option add *Menu.TearOff 0 startupFile
1934 option add *Button.font uifont startupFile
1935 option add *Checkbutton.font uifont startupFile
1936 option add *Radiobutton.font uifont startupFile
1937 option add *Menubutton.font uifont startupFile
1938 option add *Label.font uifont startupFile
1939 option add *Message.font uifont startupFile
1940 option add *Entry.font textfont startupFile
1941 option add *Text.font textfont startupFile
1942 option add *Labelframe.font uifont startupFile
1943 option add *Spinbox.font textfont startupFile
1944 option add *Listbox.font mainfont startupFile
1947 # Make a menu and submenus.
1948 # m is the window name for the menu, items is the list of menu items to add.
1949 # Each item is a list {mc label type description options...}
1950 # mc is ignored; it's so we can put mc there to alert xgettext
1951 # label is the string that appears in the menu
1952 # type is cascade, command or radiobutton (should add checkbutton)
1953 # description depends on type; it's the sublist for cascade, the
1954 # command to invoke for command, or {variable value} for radiobutton
1955 proc makemenu {m items} {
1956 menu $m
1957 if {[tk windowingsystem] eq {aqua}} {
1958 set Meta1 Cmd
1959 } else {
1960 set Meta1 Ctrl
1962 foreach i $items {
1963 set name [mc [lindex $i 1]]
1964 set type [lindex $i 2]
1965 set thing [lindex $i 3]
1966 set params [list $type]
1967 if {$name ne {}} {
1968 set u [string first "&" [string map {&& x} $name]]
1969 lappend params -label [string map {&& & & {}} $name]
1970 if {$u >= 0} {
1971 lappend params -underline $u
1974 switch -- $type {
1975 "cascade" {
1976 set submenu [string tolower [string map {& ""} [lindex $i 1]]]
1977 lappend params -menu $m.$submenu
1979 "command" {
1980 lappend params -command $thing
1982 "radiobutton" {
1983 lappend params -variable [lindex $thing 0] \
1984 -value [lindex $thing 1]
1987 set tail [lrange $i 4 end]
1988 regsub -all {\yMeta1\y} $tail $Meta1 tail
1989 eval $m add $params $tail
1990 if {$type eq "cascade"} {
1991 makemenu $m.$submenu $thing
1996 # translate string and remove ampersands
1997 proc mca {str} {
1998 return [string map {&& & & {}} [mc $str]]
2001 proc cleardropsel {w} {
2002 $w selection clear
2004 proc makedroplist {w varname args} {
2005 global use_ttk
2006 if {$use_ttk} {
2007 set width 0
2008 foreach label $args {
2009 set cx [string length $label]
2010 if {$cx > $width} {set width $cx}
2012 set gm [ttk::combobox $w -width $width -state readonly\
2013 -textvariable $varname -values $args \
2014 -exportselection false]
2015 bind $gm <<ComboboxSelected>> [list $gm selection clear]
2016 } else {
2017 set gm [eval [linsert $args 0 tk_optionMenu $w $varname]]
2019 return $gm
2022 proc makewindow {} {
2023 global canv canv2 canv3 linespc charspc ctext cflist cscroll
2024 global tabstop
2025 global findtype findtypemenu findloc findstring fstring geometry
2026 global entries sha1entry sha1string sha1but
2027 global diffcontextstring diffcontext
2028 global ignorespace
2029 global maincursor textcursor curtextcursor
2030 global rowctxmenu fakerowmenu mergemax wrapcomment
2031 global highlight_files gdttype
2032 global searchstring sstring
2033 global bgcolor fgcolor bglist fglist diffcolors selectbgcolor
2034 global uifgcolor uifgdisabledcolor
2035 global filesepbgcolor filesepfgcolor
2036 global mergecolors foundbgcolor currentsearchhitbgcolor
2037 global headctxmenu progresscanv progressitem progresscoords statusw
2038 global fprogitem fprogcoord lastprogupdate progupdatepending
2039 global rprogitem rprogcoord rownumsel numcommits
2040 global have_tk85 use_ttk NS
2041 global git_version
2042 global worddiff
2044 # The "mc" arguments here are purely so that xgettext
2045 # sees the following string as needing to be translated
2046 set file {
2047 mc "File" cascade {
2048 {mc "Update" command updatecommits -accelerator F5}
2049 {mc "Reload" command reloadcommits -accelerator Shift-F5}
2050 {mc "Reread references" command rereadrefs}
2051 {mc "List references" command showrefs -accelerator F2}
2052 {xx "" separator}
2053 {mc "Start git gui" command {exec git gui &}}
2054 {xx "" separator}
2055 {mc "Quit" command doquit -accelerator Meta1-Q}
2057 set edit {
2058 mc "Edit" cascade {
2059 {mc "Preferences" command doprefs}
2061 set view {
2062 mc "View" cascade {
2063 {mc "New view..." command {newview 0} -accelerator Shift-F4}
2064 {mc "Edit view..." command editview -state disabled -accelerator F4}
2065 {mc "Delete view" command delview -state disabled}
2066 {xx "" separator}
2067 {mc "All files" radiobutton {selectedview 0} -command {showview 0}}
2069 if {[tk windowingsystem] ne "aqua"} {
2070 set help {
2071 mc "Help" cascade {
2072 {mc "About gitk" command about}
2073 {mc "Key bindings" command keys}
2075 set bar [list $file $edit $view $help]
2076 } else {
2077 proc ::tk::mac::ShowPreferences {} {doprefs}
2078 proc ::tk::mac::Quit {} {doquit}
2079 lset file end [lreplace [lindex $file end] end-1 end]
2080 set apple {
2081 xx "Apple" cascade {
2082 {mc "About gitk" command about}
2083 {xx "" separator}
2085 set help {
2086 mc "Help" cascade {
2087 {mc "Key bindings" command keys}
2089 set bar [list $apple $file $view $help]
2091 makemenu .bar $bar
2092 . configure -menu .bar
2094 if {$use_ttk} {
2095 # cover the non-themed toplevel with a themed frame.
2096 place [ttk::frame ._main_background] -x 0 -y 0 -relwidth 1 -relheight 1
2099 # the gui has upper and lower half, parts of a paned window.
2100 ${NS}::panedwindow .ctop -orient vertical
2102 # possibly use assumed geometry
2103 if {![info exists geometry(pwsash0)]} {
2104 set geometry(topheight) [expr {15 * $linespc}]
2105 set geometry(topwidth) [expr {80 * $charspc}]
2106 set geometry(botheight) [expr {15 * $linespc}]
2107 set geometry(botwidth) [expr {50 * $charspc}]
2108 set geometry(pwsash0) [list [expr {40 * $charspc}] 2]
2109 set geometry(pwsash1) [list [expr {60 * $charspc}] 2]
2112 # the upper half will have a paned window, a scroll bar to the right, and some stuff below
2113 ${NS}::frame .tf -height $geometry(topheight) -width $geometry(topwidth)
2114 ${NS}::frame .tf.histframe
2115 ${NS}::panedwindow .tf.histframe.pwclist -orient horizontal
2116 if {!$use_ttk} {
2117 .tf.histframe.pwclist configure -sashpad 0 -handlesize 4
2120 # create three canvases
2121 set cscroll .tf.histframe.csb
2122 set canv .tf.histframe.pwclist.canv
2123 canvas $canv \
2124 -selectbackground $selectbgcolor \
2125 -background $bgcolor -bd 0 \
2126 -yscrollincr $linespc -yscrollcommand "scrollcanv $cscroll"
2127 .tf.histframe.pwclist add $canv
2128 set canv2 .tf.histframe.pwclist.canv2
2129 canvas $canv2 \
2130 -selectbackground $selectbgcolor \
2131 -background $bgcolor -bd 0 -yscrollincr $linespc
2132 .tf.histframe.pwclist add $canv2
2133 set canv3 .tf.histframe.pwclist.canv3
2134 canvas $canv3 \
2135 -selectbackground $selectbgcolor \
2136 -background $bgcolor -bd 0 -yscrollincr $linespc
2137 .tf.histframe.pwclist add $canv3
2138 if {$use_ttk} {
2139 bind .tf.histframe.pwclist <Map> {
2140 bind %W <Map> {}
2141 .tf.histframe.pwclist sashpos 1 [lindex $::geometry(pwsash1) 0]
2142 .tf.histframe.pwclist sashpos 0 [lindex $::geometry(pwsash0) 0]
2144 } else {
2145 eval .tf.histframe.pwclist sash place 0 $geometry(pwsash0)
2146 eval .tf.histframe.pwclist sash place 1 $geometry(pwsash1)
2149 # a scroll bar to rule them
2150 ${NS}::scrollbar $cscroll -command {allcanvs yview}
2151 if {!$use_ttk} {$cscroll configure -highlightthickness 0}
2152 pack $cscroll -side right -fill y
2153 bind .tf.histframe.pwclist <Configure> {resizeclistpanes %W %w}
2154 lappend bglist $canv $canv2 $canv3
2155 pack .tf.histframe.pwclist -fill both -expand 1 -side left
2157 # we have two button bars at bottom of top frame. Bar 1
2158 ${NS}::frame .tf.bar
2159 ${NS}::frame .tf.lbar -height 15
2161 set sha1entry .tf.bar.sha1
2162 set entries $sha1entry
2163 set sha1but .tf.bar.sha1label
2164 button $sha1but -text "[mc "SHA1 ID:"] " -state disabled -relief flat \
2165 -command gotocommit -width 8
2166 $sha1but conf -disabledforeground [$sha1but cget -foreground]
2167 pack .tf.bar.sha1label -side left
2168 ${NS}::entry $sha1entry -width 40 -font textfont -textvariable sha1string
2169 trace add variable sha1string write sha1change
2170 pack $sha1entry -side left -pady 2
2172 set bm_left_data {
2173 #define left_width 16
2174 #define left_height 16
2175 static unsigned char left_bits[] = {
2176 0x00, 0x00, 0xc0, 0x01, 0xe0, 0x00, 0x70, 0x00, 0x38, 0x00, 0x1c, 0x00,
2177 0x0e, 0x00, 0xff, 0x7f, 0xff, 0x7f, 0xff, 0x7f, 0x0e, 0x00, 0x1c, 0x00,
2178 0x38, 0x00, 0x70, 0x00, 0xe0, 0x00, 0xc0, 0x01};
2180 set bm_right_data {
2181 #define right_width 16
2182 #define right_height 16
2183 static unsigned char right_bits[] = {
2184 0x00, 0x00, 0xc0, 0x01, 0x80, 0x03, 0x00, 0x07, 0x00, 0x0e, 0x00, 0x1c,
2185 0x00, 0x38, 0xff, 0x7f, 0xff, 0x7f, 0xff, 0x7f, 0x00, 0x38, 0x00, 0x1c,
2186 0x00, 0x0e, 0x00, 0x07, 0x80, 0x03, 0xc0, 0x01};
2188 image create bitmap bm-left -data $bm_left_data -foreground $uifgcolor
2189 image create bitmap bm-left-gray -data $bm_left_data -foreground $uifgdisabledcolor
2190 image create bitmap bm-right -data $bm_right_data -foreground $uifgcolor
2191 image create bitmap bm-right-gray -data $bm_right_data -foreground $uifgdisabledcolor
2193 ${NS}::button .tf.bar.leftbut -command goback -state disabled -width 26
2194 if {$use_ttk} {
2195 .tf.bar.leftbut configure -image [list bm-left disabled bm-left-gray]
2196 } else {
2197 .tf.bar.leftbut configure -image bm-left
2199 pack .tf.bar.leftbut -side left -fill y
2200 ${NS}::button .tf.bar.rightbut -command goforw -state disabled -width 26
2201 if {$use_ttk} {
2202 .tf.bar.rightbut configure -image [list bm-right disabled bm-right-gray]
2203 } else {
2204 .tf.bar.rightbut configure -image bm-right
2206 pack .tf.bar.rightbut -side left -fill y
2208 ${NS}::label .tf.bar.rowlabel -text [mc "Row"]
2209 set rownumsel {}
2210 ${NS}::label .tf.bar.rownum -width 7 -textvariable rownumsel \
2211 -relief sunken -anchor e
2212 ${NS}::label .tf.bar.rowlabel2 -text "/"
2213 ${NS}::label .tf.bar.numcommits -width 7 -textvariable numcommits \
2214 -relief sunken -anchor e
2215 pack .tf.bar.rowlabel .tf.bar.rownum .tf.bar.rowlabel2 .tf.bar.numcommits \
2216 -side left
2217 if {!$use_ttk} {
2218 foreach w {rownum numcommits} {.tf.bar.$w configure -font textfont}
2220 global selectedline
2221 trace add variable selectedline write selectedline_change
2223 # Status label and progress bar
2224 set statusw .tf.bar.status
2225 ${NS}::label $statusw -width 15 -relief sunken
2226 pack $statusw -side left -padx 5
2227 if {$use_ttk} {
2228 set progresscanv [ttk::progressbar .tf.bar.progress]
2229 } else {
2230 set h [expr {[font metrics uifont -linespace] + 2}]
2231 set progresscanv .tf.bar.progress
2232 canvas $progresscanv -relief sunken -height $h -borderwidth 2
2233 set progressitem [$progresscanv create rect -1 0 0 $h -fill green]
2234 set fprogitem [$progresscanv create rect -1 0 0 $h -fill yellow]
2235 set rprogitem [$progresscanv create rect -1 0 0 $h -fill red]
2237 pack $progresscanv -side right -expand 1 -fill x -padx {0 2}
2238 set progresscoords {0 0}
2239 set fprogcoord 0
2240 set rprogcoord 0
2241 bind $progresscanv <Configure> adjustprogress
2242 set lastprogupdate [clock clicks -milliseconds]
2243 set progupdatepending 0
2245 # build up the bottom bar of upper window
2246 ${NS}::label .tf.lbar.flabel -text "[mc "Find"] "
2247 ${NS}::button .tf.lbar.fnext -text [mc "next"] -command {dofind 1 1}
2248 ${NS}::button .tf.lbar.fprev -text [mc "prev"] -command {dofind -1 1}
2249 ${NS}::label .tf.lbar.flab2 -text " [mc "commit"] "
2250 pack .tf.lbar.flabel .tf.lbar.fnext .tf.lbar.fprev .tf.lbar.flab2 \
2251 -side left -fill y
2252 set gdttype [mc "containing:"]
2253 set gm [makedroplist .tf.lbar.gdttype gdttype \
2254 [mc "containing:"] \
2255 [mc "touching paths:"] \
2256 [mc "adding/removing string:"] \
2257 [mc "changing lines matching:"]]
2258 trace add variable gdttype write gdttype_change
2259 pack .tf.lbar.gdttype -side left -fill y
2261 set findstring {}
2262 set fstring .tf.lbar.findstring
2263 lappend entries $fstring
2264 ${NS}::entry $fstring -width 30 -textvariable findstring
2265 trace add variable findstring write find_change
2266 set findtype [mc "Exact"]
2267 set findtypemenu [makedroplist .tf.lbar.findtype \
2268 findtype [mc "Exact"] [mc "IgnCase"] [mc "Regexp"]]
2269 trace add variable findtype write findcom_change
2270 set findloc [mc "All fields"]
2271 makedroplist .tf.lbar.findloc findloc [mc "All fields"] [mc "Headline"] \
2272 [mc "Comments"] [mc "Author"] [mc "Committer"]
2273 trace add variable findloc write find_change
2274 pack .tf.lbar.findloc -side right
2275 pack .tf.lbar.findtype -side right
2276 pack $fstring -side left -expand 1 -fill x
2278 # Finish putting the upper half of the viewer together
2279 pack .tf.lbar -in .tf -side bottom -fill x
2280 pack .tf.bar -in .tf -side bottom -fill x
2281 pack .tf.histframe -fill both -side top -expand 1
2282 .ctop add .tf
2283 if {!$use_ttk} {
2284 .ctop paneconfigure .tf -height $geometry(topheight)
2285 .ctop paneconfigure .tf -width $geometry(topwidth)
2288 # now build up the bottom
2289 ${NS}::panedwindow .pwbottom -orient horizontal
2291 # lower left, a text box over search bar, scroll bar to the right
2292 # if we know window height, then that will set the lower text height, otherwise
2293 # we set lower text height which will drive window height
2294 if {[info exists geometry(main)]} {
2295 ${NS}::frame .bleft -width $geometry(botwidth)
2296 } else {
2297 ${NS}::frame .bleft -width $geometry(botwidth) -height $geometry(botheight)
2299 ${NS}::frame .bleft.top
2300 ${NS}::frame .bleft.mid
2301 ${NS}::frame .bleft.bottom
2303 ${NS}::button .bleft.top.search -text [mc "Search"] -command dosearch
2304 pack .bleft.top.search -side left -padx 5
2305 set sstring .bleft.top.sstring
2306 set searchstring ""
2307 ${NS}::entry $sstring -width 20 -textvariable searchstring
2308 lappend entries $sstring
2309 trace add variable searchstring write incrsearch
2310 pack $sstring -side left -expand 1 -fill x
2311 ${NS}::radiobutton .bleft.mid.diff -text [mc "Diff"] \
2312 -command changediffdisp -variable diffelide -value {0 0}
2313 ${NS}::radiobutton .bleft.mid.old -text [mc "Old version"] \
2314 -command changediffdisp -variable diffelide -value {0 1}
2315 ${NS}::radiobutton .bleft.mid.new -text [mc "New version"] \
2316 -command changediffdisp -variable diffelide -value {1 0}
2317 ${NS}::label .bleft.mid.labeldiffcontext -text " [mc "Lines of context"]: "
2318 pack .bleft.mid.diff .bleft.mid.old .bleft.mid.new -side left
2319 spinbox .bleft.mid.diffcontext -width 5 \
2320 -from 0 -increment 1 -to 10000000 \
2321 -validate all -validatecommand "diffcontextvalidate %P" \
2322 -textvariable diffcontextstring
2323 .bleft.mid.diffcontext set $diffcontext
2324 trace add variable diffcontextstring write diffcontextchange
2325 lappend entries .bleft.mid.diffcontext
2326 pack .bleft.mid.labeldiffcontext .bleft.mid.diffcontext -side left
2327 ${NS}::checkbutton .bleft.mid.ignspace -text [mc "Ignore space change"] \
2328 -command changeignorespace -variable ignorespace
2329 pack .bleft.mid.ignspace -side left -padx 5
2331 set worddiff [mc "Line diff"]
2332 if {[package vcompare $git_version "1.7.2"] >= 0} {
2333 makedroplist .bleft.mid.worddiff worddiff [mc "Line diff"] \
2334 [mc "Markup words"] [mc "Color words"]
2335 trace add variable worddiff write changeworddiff
2336 pack .bleft.mid.worddiff -side left -padx 5
2339 set ctext .bleft.bottom.ctext
2340 text $ctext -background $bgcolor -foreground $fgcolor \
2341 -state disabled -font textfont \
2342 -yscrollcommand scrolltext -wrap none \
2343 -xscrollcommand ".bleft.bottom.sbhorizontal set"
2344 if {$have_tk85} {
2345 $ctext conf -tabstyle wordprocessor
2347 ${NS}::scrollbar .bleft.bottom.sb -command "$ctext yview"
2348 ${NS}::scrollbar .bleft.bottom.sbhorizontal -command "$ctext xview" -orient h
2349 pack .bleft.top -side top -fill x
2350 pack .bleft.mid -side top -fill x
2351 grid $ctext .bleft.bottom.sb -sticky nsew
2352 grid .bleft.bottom.sbhorizontal -sticky ew
2353 grid columnconfigure .bleft.bottom 0 -weight 1
2354 grid rowconfigure .bleft.bottom 0 -weight 1
2355 grid rowconfigure .bleft.bottom 1 -weight 0
2356 pack .bleft.bottom -side top -fill both -expand 1
2357 lappend bglist $ctext
2358 lappend fglist $ctext
2360 $ctext tag conf comment -wrap $wrapcomment
2361 $ctext tag conf filesep -font textfontbold -fore $filesepfgcolor -back $filesepbgcolor
2362 $ctext tag conf hunksep -fore [lindex $diffcolors 2]
2363 $ctext tag conf d0 -fore [lindex $diffcolors 0]
2364 $ctext tag conf dresult -fore [lindex $diffcolors 1]
2365 $ctext tag conf m0 -fore [lindex $mergecolors 0]
2366 $ctext tag conf m1 -fore [lindex $mergecolors 1]
2367 $ctext tag conf m2 -fore [lindex $mergecolors 2]
2368 $ctext tag conf m3 -fore [lindex $mergecolors 3]
2369 $ctext tag conf m4 -fore [lindex $mergecolors 4]
2370 $ctext tag conf m5 -fore [lindex $mergecolors 5]
2371 $ctext tag conf m6 -fore [lindex $mergecolors 6]
2372 $ctext tag conf m7 -fore [lindex $mergecolors 7]
2373 $ctext tag conf m8 -fore [lindex $mergecolors 8]
2374 $ctext tag conf m9 -fore [lindex $mergecolors 9]
2375 $ctext tag conf m10 -fore [lindex $mergecolors 10]
2376 $ctext tag conf m11 -fore [lindex $mergecolors 11]
2377 $ctext tag conf m12 -fore [lindex $mergecolors 12]
2378 $ctext tag conf m13 -fore [lindex $mergecolors 13]
2379 $ctext tag conf m14 -fore [lindex $mergecolors 14]
2380 $ctext tag conf m15 -fore [lindex $mergecolors 15]
2381 $ctext tag conf mmax -fore darkgrey
2382 set mergemax 16
2383 $ctext tag conf mresult -font textfontbold
2384 $ctext tag conf msep -font textfontbold
2385 $ctext tag conf found -back $foundbgcolor
2386 $ctext tag conf currentsearchhit -back $currentsearchhitbgcolor
2387 $ctext tag conf wwrap -wrap word
2389 .pwbottom add .bleft
2390 if {!$use_ttk} {
2391 .pwbottom paneconfigure .bleft -width $geometry(botwidth)
2394 # lower right
2395 ${NS}::frame .bright
2396 ${NS}::frame .bright.mode
2397 ${NS}::radiobutton .bright.mode.patch -text [mc "Patch"] \
2398 -command reselectline -variable cmitmode -value "patch"
2399 ${NS}::radiobutton .bright.mode.tree -text [mc "Tree"] \
2400 -command reselectline -variable cmitmode -value "tree"
2401 grid .bright.mode.patch .bright.mode.tree -sticky ew
2402 pack .bright.mode -side top -fill x
2403 set cflist .bright.cfiles
2404 set indent [font measure mainfont "nn"]
2405 text $cflist \
2406 -selectbackground $selectbgcolor \
2407 -background $bgcolor -foreground $fgcolor \
2408 -font mainfont \
2409 -tabs [list $indent [expr {2 * $indent}]] \
2410 -yscrollcommand ".bright.sb set" \
2411 -cursor [. cget -cursor] \
2412 -spacing1 1 -spacing3 1
2413 lappend bglist $cflist
2414 lappend fglist $cflist
2415 ${NS}::scrollbar .bright.sb -command "$cflist yview"
2416 pack .bright.sb -side right -fill y
2417 pack $cflist -side left -fill both -expand 1
2418 $cflist tag configure highlight \
2419 -background [$cflist cget -selectbackground]
2420 $cflist tag configure bold -font mainfontbold
2422 .pwbottom add .bright
2423 .ctop add .pwbottom
2425 # restore window width & height if known
2426 if {[info exists geometry(main)]} {
2427 if {[scan $geometry(main) "%dx%d" w h] >= 2} {
2428 if {$w > [winfo screenwidth .]} {
2429 set w [winfo screenwidth .]
2431 if {$h > [winfo screenheight .]} {
2432 set h [winfo screenheight .]
2434 wm geometry . "${w}x$h"
2438 if {[info exists geometry(state)] && $geometry(state) eq "zoomed"} {
2439 wm state . $geometry(state)
2442 if {[tk windowingsystem] eq {aqua}} {
2443 set M1B M1
2444 set ::BM "3"
2445 } else {
2446 set M1B Control
2447 set ::BM "2"
2450 if {$use_ttk} {
2451 bind .ctop <Map> {
2452 bind %W <Map> {}
2453 %W sashpos 0 $::geometry(topheight)
2455 bind .pwbottom <Map> {
2456 bind %W <Map> {}
2457 %W sashpos 0 $::geometry(botwidth)
2461 bind .pwbottom <Configure> {resizecdetpanes %W %w}
2462 pack .ctop -fill both -expand 1
2463 bindall <1> {selcanvline %W %x %y}
2464 #bindall <B1-Motion> {selcanvline %W %x %y}
2465 if {[tk windowingsystem] == "win32"} {
2466 bind . <MouseWheel> { windows_mousewheel_redirector %W %X %Y %D }
2467 bind $ctext <MouseWheel> { windows_mousewheel_redirector %W %X %Y %D ; break }
2468 } else {
2469 bindall <ButtonRelease-4> "allcanvs yview scroll -5 units"
2470 bindall <ButtonRelease-5> "allcanvs yview scroll 5 units"
2471 if {[tk windowingsystem] eq "aqua"} {
2472 bindall <MouseWheel> {
2473 set delta [expr {- (%D)}]
2474 allcanvs yview scroll $delta units
2476 bindall <Shift-MouseWheel> {
2477 set delta [expr {- (%D)}]
2478 $canv xview scroll $delta units
2482 bindall <$::BM> "canvscan mark %W %x %y"
2483 bindall <B$::BM-Motion> "canvscan dragto %W %x %y"
2484 bind all <$M1B-Key-w> {destroy [winfo toplevel %W]}
2485 bind . <$M1B-Key-w> doquit
2486 bindkey <Home> selfirstline
2487 bindkey <End> sellastline
2488 bind . <Key-Up> "selnextline -1"
2489 bind . <Key-Down> "selnextline 1"
2490 bind . <Shift-Key-Up> "dofind -1 0"
2491 bind . <Shift-Key-Down> "dofind 1 0"
2492 bindkey <Key-Right> "goforw"
2493 bindkey <Key-Left> "goback"
2494 bind . <Key-Prior> "selnextpage -1"
2495 bind . <Key-Next> "selnextpage 1"
2496 bind . <$M1B-Home> "allcanvs yview moveto 0.0"
2497 bind . <$M1B-End> "allcanvs yview moveto 1.0"
2498 bind . <$M1B-Key-Up> "allcanvs yview scroll -1 units"
2499 bind . <$M1B-Key-Down> "allcanvs yview scroll 1 units"
2500 bind . <$M1B-Key-Prior> "allcanvs yview scroll -1 pages"
2501 bind . <$M1B-Key-Next> "allcanvs yview scroll 1 pages"
2502 bindkey <Key-Delete> "$ctext yview scroll -1 pages"
2503 bindkey <Key-BackSpace> "$ctext yview scroll -1 pages"
2504 bindkey <Key-space> "$ctext yview scroll 1 pages"
2505 bindkey p "selnextline -1"
2506 bindkey n "selnextline 1"
2507 bindkey z "goback"
2508 bindkey x "goforw"
2509 bindkey k "selnextline -1"
2510 bindkey j "selnextline 1"
2511 bindkey h "goback"
2512 bindkey l "goforw"
2513 bindkey b prevfile
2514 bindkey d "$ctext yview scroll 18 units"
2515 bindkey u "$ctext yview scroll -18 units"
2516 bindkey / {focus $fstring}
2517 bindkey <Key-KP_Divide> {focus $fstring}
2518 bindkey <Key-Return> {dofind 1 1}
2519 bindkey ? {dofind -1 1}
2520 bindkey f nextfile
2521 bind . <F5> updatecommits
2522 bindmodfunctionkey Shift 5 reloadcommits
2523 bind . <F2> showrefs
2524 bindmodfunctionkey Shift 4 {newview 0}
2525 bind . <F4> edit_or_newview
2526 bind . <$M1B-q> doquit
2527 bind . <$M1B-f> {dofind 1 1}
2528 bind . <$M1B-g> {dofind 1 0}
2529 bind . <$M1B-r> dosearchback
2530 bind . <$M1B-s> dosearch
2531 bind . <$M1B-equal> {incrfont 1}
2532 bind . <$M1B-plus> {incrfont 1}
2533 bind . <$M1B-KP_Add> {incrfont 1}
2534 bind . <$M1B-minus> {incrfont -1}
2535 bind . <$M1B-KP_Subtract> {incrfont -1}
2536 wm protocol . WM_DELETE_WINDOW doquit
2537 bind . <Destroy> {stop_backends}
2538 bind . <Button-1> "click %W"
2539 bind $fstring <Key-Return> {dofind 1 1}
2540 bind $sha1entry <Key-Return> {gotocommit; break}
2541 bind $sha1entry <<PasteSelection>> clearsha1
2542 bind $cflist <1> {sel_flist %W %x %y; break}
2543 bind $cflist <B1-Motion> {sel_flist %W %x %y; break}
2544 bind $cflist <ButtonRelease-1> {treeclick %W %x %y}
2545 global ctxbut
2546 bind $cflist $ctxbut {pop_flist_menu %W %X %Y %x %y}
2547 bind $ctext $ctxbut {pop_diff_menu %W %X %Y %x %y}
2548 bind $ctext <Button-1> {focus %W}
2549 bind $ctext <<Selection>> rehighlight_search_results
2551 set maincursor [. cget -cursor]
2552 set textcursor [$ctext cget -cursor]
2553 set curtextcursor $textcursor
2555 set rowctxmenu .rowctxmenu
2556 makemenu $rowctxmenu {
2557 {mc "Diff this -> selected" command {diffvssel 0}}
2558 {mc "Diff selected -> this" command {diffvssel 1}}
2559 {mc "Make patch" command mkpatch}
2560 {mc "Create tag" command mktag}
2561 {mc "Write commit to file" command writecommit}
2562 {mc "Create new branch" command mkbranch}
2563 {mc "Cherry-pick this commit" command cherrypick}
2564 {mc "Reset HEAD branch to here" command resethead}
2565 {mc "Mark this commit" command markhere}
2566 {mc "Return to mark" command gotomark}
2567 {mc "Find descendant of this and mark" command find_common_desc}
2568 {mc "Compare with marked commit" command compare_commits}
2569 {mc "Diff this -> marked commit" command {diffvsmark 0}}
2570 {mc "Diff marked commit -> this" command {diffvsmark 1}}
2571 {mc "Revert this commit" command revert}
2573 $rowctxmenu configure -tearoff 0
2575 set fakerowmenu .fakerowmenu
2576 makemenu $fakerowmenu {
2577 {mc "Diff this -> selected" command {diffvssel 0}}
2578 {mc "Diff selected -> this" command {diffvssel 1}}
2579 {mc "Make patch" command mkpatch}
2580 {mc "Diff this -> marked commit" command {diffvsmark 0}}
2581 {mc "Diff marked commit -> this" command {diffvsmark 1}}
2583 $fakerowmenu configure -tearoff 0
2585 set headctxmenu .headctxmenu
2586 makemenu $headctxmenu {
2587 {mc "Check out this branch" command cobranch}
2588 {mc "Remove this branch" command rmbranch}
2590 $headctxmenu configure -tearoff 0
2592 global flist_menu
2593 set flist_menu .flistctxmenu
2594 makemenu $flist_menu {
2595 {mc "Highlight this too" command {flist_hl 0}}
2596 {mc "Highlight this only" command {flist_hl 1}}
2597 {mc "External diff" command {external_diff}}
2598 {mc "Blame parent commit" command {external_blame 1}}
2600 $flist_menu configure -tearoff 0
2602 global diff_menu
2603 set diff_menu .diffctxmenu
2604 makemenu $diff_menu {
2605 {mc "Show origin of this line" command show_line_source}
2606 {mc "Run git gui blame on this line" command {external_blame_diff}}
2608 $diff_menu configure -tearoff 0
2611 # Windows sends all mouse wheel events to the current focused window, not
2612 # the one where the mouse hovers, so bind those events here and redirect
2613 # to the correct window
2614 proc windows_mousewheel_redirector {W X Y D} {
2615 global canv canv2 canv3
2616 set w [winfo containing -displayof $W $X $Y]
2617 if {$w ne ""} {
2618 set u [expr {$D < 0 ? 5 : -5}]
2619 if {$w == $canv || $w == $canv2 || $w == $canv3} {
2620 allcanvs yview scroll $u units
2621 } else {
2622 catch {
2623 $w yview scroll $u units
2629 # Update row number label when selectedline changes
2630 proc selectedline_change {n1 n2 op} {
2631 global selectedline rownumsel
2633 if {$selectedline eq {}} {
2634 set rownumsel {}
2635 } else {
2636 set rownumsel [expr {$selectedline + 1}]
2640 # mouse-2 makes all windows scan vertically, but only the one
2641 # the cursor is in scans horizontally
2642 proc canvscan {op w x y} {
2643 global canv canv2 canv3
2644 foreach c [list $canv $canv2 $canv3] {
2645 if {$c == $w} {
2646 $c scan $op $x $y
2647 } else {
2648 $c scan $op 0 $y
2653 proc scrollcanv {cscroll f0 f1} {
2654 $cscroll set $f0 $f1
2655 drawvisible
2656 flushhighlights
2659 # when we make a key binding for the toplevel, make sure
2660 # it doesn't get triggered when that key is pressed in the
2661 # find string entry widget.
2662 proc bindkey {ev script} {
2663 global entries
2664 bind . $ev $script
2665 set escript [bind Entry $ev]
2666 if {$escript == {}} {
2667 set escript [bind Entry <Key>]
2669 foreach e $entries {
2670 bind $e $ev "$escript; break"
2674 proc bindmodfunctionkey {mod n script} {
2675 bind . <$mod-F$n> $script
2676 catch { bind . <$mod-XF86_Switch_VT_$n> $script }
2679 # set the focus back to the toplevel for any click outside
2680 # the entry widgets
2681 proc click {w} {
2682 global ctext entries
2683 foreach e [concat $entries $ctext] {
2684 if {$w == $e} return
2686 focus .
2689 # Adjust the progress bar for a change in requested extent or canvas size
2690 proc adjustprogress {} {
2691 global progresscanv progressitem progresscoords
2692 global fprogitem fprogcoord lastprogupdate progupdatepending
2693 global rprogitem rprogcoord use_ttk
2695 if {$use_ttk} {
2696 $progresscanv configure -value [expr {int($fprogcoord * 100)}]
2697 return
2700 set w [expr {[winfo width $progresscanv] - 4}]
2701 set x0 [expr {$w * [lindex $progresscoords 0]}]
2702 set x1 [expr {$w * [lindex $progresscoords 1]}]
2703 set h [winfo height $progresscanv]
2704 $progresscanv coords $progressitem $x0 0 $x1 $h
2705 $progresscanv coords $fprogitem 0 0 [expr {$w * $fprogcoord}] $h
2706 $progresscanv coords $rprogitem 0 0 [expr {$w * $rprogcoord}] $h
2707 set now [clock clicks -milliseconds]
2708 if {$now >= $lastprogupdate + 100} {
2709 set progupdatepending 0
2710 update
2711 } elseif {!$progupdatepending} {
2712 set progupdatepending 1
2713 after [expr {$lastprogupdate + 100 - $now}] doprogupdate
2717 proc doprogupdate {} {
2718 global lastprogupdate progupdatepending
2720 if {$progupdatepending} {
2721 set progupdatepending 0
2722 set lastprogupdate [clock clicks -milliseconds]
2723 update
2727 proc savestuff {w} {
2728 global canv canv2 canv3 mainfont textfont uifont tabstop
2729 global stuffsaved findmergefiles maxgraphpct
2730 global maxwidth showneartags showlocalchanges
2731 global viewname viewfiles viewargs viewargscmd viewperm nextviewnum
2732 global cmitmode wrapcomment datetimeformat limitdiffs
2733 global colors uicolor bgcolor fgcolor diffcolors diffcontext selectbgcolor
2734 global uifgcolor uifgdisabledcolor
2735 global headbgcolor headfgcolor headoutlinecolor remotebgcolor
2736 global tagbgcolor tagfgcolor tagoutlinecolor
2737 global reflinecolor filesepbgcolor filesepfgcolor
2738 global mergecolors foundbgcolor currentsearchhitbgcolor
2739 global linehoverbgcolor linehoverfgcolor linehoveroutlinecolor circlecolors
2740 global mainheadcirclecolor workingfilescirclecolor indexcirclecolor
2741 global linkfgcolor circleoutlinecolor
2742 global autoselect autosellen extdifftool perfile_attrs markbgcolor use_ttk
2743 global hideremotes want_ttk maxrefs
2745 if {$stuffsaved} return
2746 if {![winfo viewable .]} return
2747 catch {
2748 if {[file exists ~/.gitk-new]} {file delete -force ~/.gitk-new}
2749 set f [open "~/.gitk-new" w]
2750 if {$::tcl_platform(platform) eq {windows}} {
2751 file attributes "~/.gitk-new" -hidden true
2753 puts $f [list set mainfont $mainfont]
2754 puts $f [list set textfont $textfont]
2755 puts $f [list set uifont $uifont]
2756 puts $f [list set tabstop $tabstop]
2757 puts $f [list set findmergefiles $findmergefiles]
2758 puts $f [list set maxgraphpct $maxgraphpct]
2759 puts $f [list set maxwidth $maxwidth]
2760 puts $f [list set cmitmode $cmitmode]
2761 puts $f [list set wrapcomment $wrapcomment]
2762 puts $f [list set autoselect $autoselect]
2763 puts $f [list set autosellen $autosellen]
2764 puts $f [list set showneartags $showneartags]
2765 puts $f [list set maxrefs $maxrefs]
2766 puts $f [list set hideremotes $hideremotes]
2767 puts $f [list set showlocalchanges $showlocalchanges]
2768 puts $f [list set datetimeformat $datetimeformat]
2769 puts $f [list set limitdiffs $limitdiffs]
2770 puts $f [list set uicolor $uicolor]
2771 puts $f [list set want_ttk $want_ttk]
2772 puts $f [list set bgcolor $bgcolor]
2773 puts $f [list set fgcolor $fgcolor]
2774 puts $f [list set uifgcolor $uifgcolor]
2775 puts $f [list set uifgdisabledcolor $uifgdisabledcolor]
2776 puts $f [list set colors $colors]
2777 puts $f [list set diffcolors $diffcolors]
2778 puts $f [list set mergecolors $mergecolors]
2779 puts $f [list set markbgcolor $markbgcolor]
2780 puts $f [list set diffcontext $diffcontext]
2781 puts $f [list set selectbgcolor $selectbgcolor]
2782 puts $f [list set foundbgcolor $foundbgcolor]
2783 puts $f [list set currentsearchhitbgcolor $currentsearchhitbgcolor]
2784 puts $f [list set extdifftool $extdifftool]
2785 puts $f [list set perfile_attrs $perfile_attrs]
2786 puts $f [list set headbgcolor $headbgcolor]
2787 puts $f [list set headfgcolor $headfgcolor]
2788 puts $f [list set headoutlinecolor $headoutlinecolor]
2789 puts $f [list set remotebgcolor $remotebgcolor]
2790 puts $f [list set tagbgcolor $tagbgcolor]
2791 puts $f [list set tagfgcolor $tagfgcolor]
2792 puts $f [list set tagoutlinecolor $tagoutlinecolor]
2793 puts $f [list set reflinecolor $reflinecolor]
2794 puts $f [list set filesepbgcolor $filesepbgcolor]
2795 puts $f [list set filesepfgcolor $filesepfgcolor]
2796 puts $f [list set linehoverbgcolor $linehoverbgcolor]
2797 puts $f [list set linehoverfgcolor $linehoverfgcolor]
2798 puts $f [list set linehoveroutlinecolor $linehoveroutlinecolor]
2799 puts $f [list set mainheadcirclecolor $mainheadcirclecolor]
2800 puts $f [list set workingfilescirclecolor $workingfilescirclecolor]
2801 puts $f [list set indexcirclecolor $indexcirclecolor]
2802 puts $f [list set circlecolors $circlecolors]
2803 puts $f [list set linkfgcolor $linkfgcolor]
2804 puts $f [list set circleoutlinecolor $circleoutlinecolor]
2806 puts $f "set geometry(main) [wm geometry .]"
2807 puts $f "set geometry(state) [wm state .]"
2808 puts $f "set geometry(topwidth) [winfo width .tf]"
2809 puts $f "set geometry(topheight) [winfo height .tf]"
2810 if {$use_ttk} {
2811 puts $f "set geometry(pwsash0) \"[.tf.histframe.pwclist sashpos 0] 1\""
2812 puts $f "set geometry(pwsash1) \"[.tf.histframe.pwclist sashpos 1] 1\""
2813 } else {
2814 puts $f "set geometry(pwsash0) \"[.tf.histframe.pwclist sash coord 0]\""
2815 puts $f "set geometry(pwsash1) \"[.tf.histframe.pwclist sash coord 1]\""
2817 puts $f "set geometry(botwidth) [winfo width .bleft]"
2818 puts $f "set geometry(botheight) [winfo height .bleft]"
2820 puts -nonewline $f "set permviews {"
2821 for {set v 0} {$v < $nextviewnum} {incr v} {
2822 if {$viewperm($v)} {
2823 puts $f "{[list $viewname($v) $viewfiles($v) $viewargs($v) $viewargscmd($v)]}"
2826 puts $f "}"
2827 close $f
2828 file rename -force "~/.gitk-new" "~/.gitk"
2830 set stuffsaved 1
2833 proc resizeclistpanes {win w} {
2834 global oldwidth use_ttk
2835 if {[info exists oldwidth($win)]} {
2836 if {$use_ttk} {
2837 set s0 [$win sashpos 0]
2838 set s1 [$win sashpos 1]
2839 } else {
2840 set s0 [$win sash coord 0]
2841 set s1 [$win sash coord 1]
2843 if {$w < 60} {
2844 set sash0 [expr {int($w/2 - 2)}]
2845 set sash1 [expr {int($w*5/6 - 2)}]
2846 } else {
2847 set factor [expr {1.0 * $w / $oldwidth($win)}]
2848 set sash0 [expr {int($factor * [lindex $s0 0])}]
2849 set sash1 [expr {int($factor * [lindex $s1 0])}]
2850 if {$sash0 < 30} {
2851 set sash0 30
2853 if {$sash1 < $sash0 + 20} {
2854 set sash1 [expr {$sash0 + 20}]
2856 if {$sash1 > $w - 10} {
2857 set sash1 [expr {$w - 10}]
2858 if {$sash0 > $sash1 - 20} {
2859 set sash0 [expr {$sash1 - 20}]
2863 if {$use_ttk} {
2864 $win sashpos 0 $sash0
2865 $win sashpos 1 $sash1
2866 } else {
2867 $win sash place 0 $sash0 [lindex $s0 1]
2868 $win sash place 1 $sash1 [lindex $s1 1]
2871 set oldwidth($win) $w
2874 proc resizecdetpanes {win w} {
2875 global oldwidth use_ttk
2876 if {[info exists oldwidth($win)]} {
2877 if {$use_ttk} {
2878 set s0 [$win sashpos 0]
2879 } else {
2880 set s0 [$win sash coord 0]
2882 if {$w < 60} {
2883 set sash0 [expr {int($w*3/4 - 2)}]
2884 } else {
2885 set factor [expr {1.0 * $w / $oldwidth($win)}]
2886 set sash0 [expr {int($factor * [lindex $s0 0])}]
2887 if {$sash0 < 45} {
2888 set sash0 45
2890 if {$sash0 > $w - 15} {
2891 set sash0 [expr {$w - 15}]
2894 if {$use_ttk} {
2895 $win sashpos 0 $sash0
2896 } else {
2897 $win sash place 0 $sash0 [lindex $s0 1]
2900 set oldwidth($win) $w
2903 proc allcanvs args {
2904 global canv canv2 canv3
2905 eval $canv $args
2906 eval $canv2 $args
2907 eval $canv3 $args
2910 proc bindall {event action} {
2911 global canv canv2 canv3
2912 bind $canv $event $action
2913 bind $canv2 $event $action
2914 bind $canv3 $event $action
2917 proc about {} {
2918 global uifont NS
2919 set w .about
2920 if {[winfo exists $w]} {
2921 raise $w
2922 return
2924 ttk_toplevel $w
2925 wm title $w [mc "About gitk"]
2926 make_transient $w .
2927 message $w.m -text [mc "
2928 Gitk - a commit viewer for git
2930 Copyright \u00a9 2005-2011 Paul Mackerras
2932 Use and redistribute under the terms of the GNU General Public License"] \
2933 -justify center -aspect 400 -border 2 -bg white -relief groove
2934 pack $w.m -side top -fill x -padx 2 -pady 2
2935 ${NS}::button $w.ok -text [mc "Close"] -command "destroy $w" -default active
2936 pack $w.ok -side bottom
2937 bind $w <Visibility> "focus $w.ok"
2938 bind $w <Key-Escape> "destroy $w"
2939 bind $w <Key-Return> "destroy $w"
2940 tk::PlaceWindow $w widget .
2943 proc keys {} {
2944 global NS
2945 set w .keys
2946 if {[winfo exists $w]} {
2947 raise $w
2948 return
2950 if {[tk windowingsystem] eq {aqua}} {
2951 set M1T Cmd
2952 } else {
2953 set M1T Ctrl
2955 ttk_toplevel $w
2956 wm title $w [mc "Gitk key bindings"]
2957 make_transient $w .
2958 message $w.m -text "
2959 [mc "Gitk key bindings:"]
2961 [mc "<%s-Q> Quit" $M1T]
2962 [mc "<%s-W> Close window" $M1T]
2963 [mc "<Home> Move to first commit"]
2964 [mc "<End> Move to last commit"]
2965 [mc "<Up>, p, k Move up one commit"]
2966 [mc "<Down>, n, j Move down one commit"]
2967 [mc "<Left>, z, h Go back in history list"]
2968 [mc "<Right>, x, l Go forward in history list"]
2969 [mc "<PageUp> Move up one page in commit list"]
2970 [mc "<PageDown> Move down one page in commit list"]
2971 [mc "<%s-Home> Scroll to top of commit list" $M1T]
2972 [mc "<%s-End> Scroll to bottom of commit list" $M1T]
2973 [mc "<%s-Up> Scroll commit list up one line" $M1T]
2974 [mc "<%s-Down> Scroll commit list down one line" $M1T]
2975 [mc "<%s-PageUp> Scroll commit list up one page" $M1T]
2976 [mc "<%s-PageDown> Scroll commit list down one page" $M1T]
2977 [mc "<Shift-Up> Find backwards (upwards, later commits)"]
2978 [mc "<Shift-Down> Find forwards (downwards, earlier commits)"]
2979 [mc "<Delete>, b Scroll diff view up one page"]
2980 [mc "<Backspace> Scroll diff view up one page"]
2981 [mc "<Space> Scroll diff view down one page"]
2982 [mc "u Scroll diff view up 18 lines"]
2983 [mc "d Scroll diff view down 18 lines"]
2984 [mc "<%s-F> Find" $M1T]
2985 [mc "<%s-G> Move to next find hit" $M1T]
2986 [mc "<Return> Move to next find hit"]
2987 [mc "/ Focus the search box"]
2988 [mc "? Move to previous find hit"]
2989 [mc "f Scroll diff view to next file"]
2990 [mc "<%s-S> Search for next hit in diff view" $M1T]
2991 [mc "<%s-R> Search for previous hit in diff view" $M1T]
2992 [mc "<%s-KP+> Increase font size" $M1T]
2993 [mc "<%s-plus> Increase font size" $M1T]
2994 [mc "<%s-KP-> Decrease font size" $M1T]
2995 [mc "<%s-minus> Decrease font size" $M1T]
2996 [mc "<F5> Update"]
2998 -justify left -bg white -border 2 -relief groove
2999 pack $w.m -side top -fill both -padx 2 -pady 2
3000 ${NS}::button $w.ok -text [mc "Close"] -command "destroy $w" -default active
3001 bind $w <Key-Escape> [list destroy $w]
3002 pack $w.ok -side bottom
3003 bind $w <Visibility> "focus $w.ok"
3004 bind $w <Key-Escape> "destroy $w"
3005 bind $w <Key-Return> "destroy $w"
3008 # Procedures for manipulating the file list window at the
3009 # bottom right of the overall window.
3011 proc treeview {w l openlevs} {
3012 global treecontents treediropen treeheight treeparent treeindex
3014 set ix 0
3015 set treeindex() 0
3016 set lev 0
3017 set prefix {}
3018 set prefixend -1
3019 set prefendstack {}
3020 set htstack {}
3021 set ht 0
3022 set treecontents() {}
3023 $w conf -state normal
3024 foreach f $l {
3025 while {[string range $f 0 $prefixend] ne $prefix} {
3026 if {$lev <= $openlevs} {
3027 $w mark set e:$treeindex($prefix) "end -1c"
3028 $w mark gravity e:$treeindex($prefix) left
3030 set treeheight($prefix) $ht
3031 incr ht [lindex $htstack end]
3032 set htstack [lreplace $htstack end end]
3033 set prefixend [lindex $prefendstack end]
3034 set prefendstack [lreplace $prefendstack end end]
3035 set prefix [string range $prefix 0 $prefixend]
3036 incr lev -1
3038 set tail [string range $f [expr {$prefixend+1}] end]
3039 while {[set slash [string first "/" $tail]] >= 0} {
3040 lappend htstack $ht
3041 set ht 0
3042 lappend prefendstack $prefixend
3043 incr prefixend [expr {$slash + 1}]
3044 set d [string range $tail 0 $slash]
3045 lappend treecontents($prefix) $d
3046 set oldprefix $prefix
3047 append prefix $d
3048 set treecontents($prefix) {}
3049 set treeindex($prefix) [incr ix]
3050 set treeparent($prefix) $oldprefix
3051 set tail [string range $tail [expr {$slash+1}] end]
3052 if {$lev <= $openlevs} {
3053 set ht 1
3054 set treediropen($prefix) [expr {$lev < $openlevs}]
3055 set bm [expr {$lev == $openlevs? "tri-rt": "tri-dn"}]
3056 $w mark set d:$ix "end -1c"
3057 $w mark gravity d:$ix left
3058 set str "\n"
3059 for {set i 0} {$i < $lev} {incr i} {append str "\t"}
3060 $w insert end $str
3061 $w image create end -align center -image $bm -padx 1 \
3062 -name a:$ix
3063 $w insert end $d [highlight_tag $prefix]
3064 $w mark set s:$ix "end -1c"
3065 $w mark gravity s:$ix left
3067 incr lev
3069 if {$tail ne {}} {
3070 if {$lev <= $openlevs} {
3071 incr ht
3072 set str "\n"
3073 for {set i 0} {$i < $lev} {incr i} {append str "\t"}
3074 $w insert end $str
3075 $w insert end $tail [highlight_tag $f]
3077 lappend treecontents($prefix) $tail
3080 while {$htstack ne {}} {
3081 set treeheight($prefix) $ht
3082 incr ht [lindex $htstack end]
3083 set htstack [lreplace $htstack end end]
3084 set prefixend [lindex $prefendstack end]
3085 set prefendstack [lreplace $prefendstack end end]
3086 set prefix [string range $prefix 0 $prefixend]
3088 $w conf -state disabled
3091 proc linetoelt {l} {
3092 global treeheight treecontents
3094 set y 2
3095 set prefix {}
3096 while {1} {
3097 foreach e $treecontents($prefix) {
3098 if {$y == $l} {
3099 return "$prefix$e"
3101 set n 1
3102 if {[string index $e end] eq "/"} {
3103 set n $treeheight($prefix$e)
3104 if {$y + $n > $l} {
3105 append prefix $e
3106 incr y
3107 break
3110 incr y $n
3115 proc highlight_tree {y prefix} {
3116 global treeheight treecontents cflist
3118 foreach e $treecontents($prefix) {
3119 set path $prefix$e
3120 if {[highlight_tag $path] ne {}} {
3121 $cflist tag add bold $y.0 "$y.0 lineend"
3123 incr y
3124 if {[string index $e end] eq "/" && $treeheight($path) > 1} {
3125 set y [highlight_tree $y $path]
3128 return $y
3131 proc treeclosedir {w dir} {
3132 global treediropen treeheight treeparent treeindex
3134 set ix $treeindex($dir)
3135 $w conf -state normal
3136 $w delete s:$ix e:$ix
3137 set treediropen($dir) 0
3138 $w image configure a:$ix -image tri-rt
3139 $w conf -state disabled
3140 set n [expr {1 - $treeheight($dir)}]
3141 while {$dir ne {}} {
3142 incr treeheight($dir) $n
3143 set dir $treeparent($dir)
3147 proc treeopendir {w dir} {
3148 global treediropen treeheight treeparent treecontents treeindex
3150 set ix $treeindex($dir)
3151 $w conf -state normal
3152 $w image configure a:$ix -image tri-dn
3153 $w mark set e:$ix s:$ix
3154 $w mark gravity e:$ix right
3155 set lev 0
3156 set str "\n"
3157 set n [llength $treecontents($dir)]
3158 for {set x $dir} {$x ne {}} {set x $treeparent($x)} {
3159 incr lev
3160 append str "\t"
3161 incr treeheight($x) $n
3163 foreach e $treecontents($dir) {
3164 set de $dir$e
3165 if {[string index $e end] eq "/"} {
3166 set iy $treeindex($de)
3167 $w mark set d:$iy e:$ix
3168 $w mark gravity d:$iy left
3169 $w insert e:$ix $str
3170 set treediropen($de) 0
3171 $w image create e:$ix -align center -image tri-rt -padx 1 \
3172 -name a:$iy
3173 $w insert e:$ix $e [highlight_tag $de]
3174 $w mark set s:$iy e:$ix
3175 $w mark gravity s:$iy left
3176 set treeheight($de) 1
3177 } else {
3178 $w insert e:$ix $str
3179 $w insert e:$ix $e [highlight_tag $de]
3182 $w mark gravity e:$ix right
3183 $w conf -state disabled
3184 set treediropen($dir) 1
3185 set top [lindex [split [$w index @0,0] .] 0]
3186 set ht [$w cget -height]
3187 set l [lindex [split [$w index s:$ix] .] 0]
3188 if {$l < $top} {
3189 $w yview $l.0
3190 } elseif {$l + $n + 1 > $top + $ht} {
3191 set top [expr {$l + $n + 2 - $ht}]
3192 if {$l < $top} {
3193 set top $l
3195 $w yview $top.0
3199 proc treeclick {w x y} {
3200 global treediropen cmitmode ctext cflist cflist_top
3202 if {$cmitmode ne "tree"} return
3203 if {![info exists cflist_top]} return
3204 set l [lindex [split [$w index "@$x,$y"] "."] 0]
3205 $cflist tag remove highlight $cflist_top.0 "$cflist_top.0 lineend"
3206 $cflist tag add highlight $l.0 "$l.0 lineend"
3207 set cflist_top $l
3208 if {$l == 1} {
3209 $ctext yview 1.0
3210 return
3212 set e [linetoelt $l]
3213 if {[string index $e end] ne "/"} {
3214 showfile $e
3215 } elseif {$treediropen($e)} {
3216 treeclosedir $w $e
3217 } else {
3218 treeopendir $w $e
3222 proc setfilelist {id} {
3223 global treefilelist cflist jump_to_here
3225 treeview $cflist $treefilelist($id) 0
3226 if {$jump_to_here ne {}} {
3227 set f [lindex $jump_to_here 0]
3228 if {[lsearch -exact $treefilelist($id) $f] >= 0} {
3229 showfile $f
3234 image create bitmap tri-rt -background black -foreground blue -data {
3235 #define tri-rt_width 13
3236 #define tri-rt_height 13
3237 static unsigned char tri-rt_bits[] = {
3238 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x30, 0x00, 0x70, 0x00, 0xf0, 0x00,
3239 0xf0, 0x01, 0xf0, 0x00, 0x70, 0x00, 0x30, 0x00, 0x10, 0x00, 0x00, 0x00,
3240 0x00, 0x00};
3241 } -maskdata {
3242 #define tri-rt-mask_width 13
3243 #define tri-rt-mask_height 13
3244 static unsigned char tri-rt-mask_bits[] = {
3245 0x08, 0x00, 0x18, 0x00, 0x38, 0x00, 0x78, 0x00, 0xf8, 0x00, 0xf8, 0x01,
3246 0xf8, 0x03, 0xf8, 0x01, 0xf8, 0x00, 0x78, 0x00, 0x38, 0x00, 0x18, 0x00,
3247 0x08, 0x00};
3249 image create bitmap tri-dn -background black -foreground blue -data {
3250 #define tri-dn_width 13
3251 #define tri-dn_height 13
3252 static unsigned char tri-dn_bits[] = {
3253 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0x07, 0xf8, 0x03,
3254 0xf0, 0x01, 0xe0, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3255 0x00, 0x00};
3256 } -maskdata {
3257 #define tri-dn-mask_width 13
3258 #define tri-dn-mask_height 13
3259 static unsigned char tri-dn-mask_bits[] = {
3260 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x1f, 0xfe, 0x0f, 0xfc, 0x07,
3261 0xf8, 0x03, 0xf0, 0x01, 0xe0, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00,
3262 0x00, 0x00};
3265 image create bitmap reficon-T -background black -foreground yellow -data {
3266 #define tagicon_width 13
3267 #define tagicon_height 9
3268 static unsigned char tagicon_bits[] = {
3269 0x00, 0x00, 0x00, 0x00, 0xf0, 0x07, 0xf8, 0x07,
3270 0xfc, 0x07, 0xf8, 0x07, 0xf0, 0x07, 0x00, 0x00, 0x00, 0x00};
3271 } -maskdata {
3272 #define tagicon-mask_width 13
3273 #define tagicon-mask_height 9
3274 static unsigned char tagicon-mask_bits[] = {
3275 0x00, 0x00, 0xf0, 0x0f, 0xf8, 0x0f, 0xfc, 0x0f,
3276 0xfe, 0x0f, 0xfc, 0x0f, 0xf8, 0x0f, 0xf0, 0x0f, 0x00, 0x00};
3278 set rectdata {
3279 #define headicon_width 13
3280 #define headicon_height 9
3281 static unsigned char headicon_bits[] = {
3282 0x00, 0x00, 0x00, 0x00, 0xf8, 0x07, 0xf8, 0x07,
3283 0xf8, 0x07, 0xf8, 0x07, 0xf8, 0x07, 0x00, 0x00, 0x00, 0x00};
3285 set rectmask {
3286 #define headicon-mask_width 13
3287 #define headicon-mask_height 9
3288 static unsigned char headicon-mask_bits[] = {
3289 0x00, 0x00, 0xfc, 0x0f, 0xfc, 0x0f, 0xfc, 0x0f,
3290 0xfc, 0x0f, 0xfc, 0x0f, 0xfc, 0x0f, 0xfc, 0x0f, 0x00, 0x00};
3292 image create bitmap reficon-H -background black -foreground green \
3293 -data $rectdata -maskdata $rectmask
3294 image create bitmap reficon-o -background black -foreground "#ddddff" \
3295 -data $rectdata -maskdata $rectmask
3297 proc init_flist {first} {
3298 global cflist cflist_top difffilestart
3300 $cflist conf -state normal
3301 $cflist delete 0.0 end
3302 if {$first ne {}} {
3303 $cflist insert end $first
3304 set cflist_top 1
3305 $cflist tag add highlight 1.0 "1.0 lineend"
3306 } else {
3307 catch {unset cflist_top}
3309 $cflist conf -state disabled
3310 set difffilestart {}
3313 proc highlight_tag {f} {
3314 global highlight_paths
3316 foreach p $highlight_paths {
3317 if {[string match $p $f]} {
3318 return "bold"
3321 return {}
3324 proc highlight_filelist {} {
3325 global cmitmode cflist
3327 $cflist conf -state normal
3328 if {$cmitmode ne "tree"} {
3329 set end [lindex [split [$cflist index end] .] 0]
3330 for {set l 2} {$l < $end} {incr l} {
3331 set line [$cflist get $l.0 "$l.0 lineend"]
3332 if {[highlight_tag $line] ne {}} {
3333 $cflist tag add bold $l.0 "$l.0 lineend"
3336 } else {
3337 highlight_tree 2 {}
3339 $cflist conf -state disabled
3342 proc unhighlight_filelist {} {
3343 global cflist
3345 $cflist conf -state normal
3346 $cflist tag remove bold 1.0 end
3347 $cflist conf -state disabled
3350 proc add_flist {fl} {
3351 global cflist
3353 $cflist conf -state normal
3354 foreach f $fl {
3355 $cflist insert end "\n"
3356 $cflist insert end $f [highlight_tag $f]
3358 $cflist conf -state disabled
3361 proc sel_flist {w x y} {
3362 global ctext difffilestart cflist cflist_top cmitmode
3364 if {$cmitmode eq "tree"} return
3365 if {![info exists cflist_top]} return
3366 set l [lindex [split [$w index "@$x,$y"] "."] 0]
3367 $cflist tag remove highlight $cflist_top.0 "$cflist_top.0 lineend"
3368 $cflist tag add highlight $l.0 "$l.0 lineend"
3369 set cflist_top $l
3370 if {$l == 1} {
3371 $ctext yview 1.0
3372 } else {
3373 catch {$ctext yview [lindex $difffilestart [expr {$l - 2}]]}
3375 suppress_highlighting_file_for_current_scrollpos
3378 proc pop_flist_menu {w X Y x y} {
3379 global ctext cflist cmitmode flist_menu flist_menu_file
3380 global treediffs diffids
3382 stopfinding
3383 set l [lindex [split [$w index "@$x,$y"] "."] 0]
3384 if {$l <= 1} return
3385 if {$cmitmode eq "tree"} {
3386 set e [linetoelt $l]
3387 if {[string index $e end] eq "/"} return
3388 } else {
3389 set e [lindex $treediffs($diffids) [expr {$l-2}]]
3391 set flist_menu_file $e
3392 set xdiffstate "normal"
3393 if {$cmitmode eq "tree"} {
3394 set xdiffstate "disabled"
3396 # Disable "External diff" item in tree mode
3397 $flist_menu entryconf 2 -state $xdiffstate
3398 tk_popup $flist_menu $X $Y
3401 proc find_ctext_fileinfo {line} {
3402 global ctext_file_names ctext_file_lines
3404 set ok [bsearch $ctext_file_lines $line]
3405 set tline [lindex $ctext_file_lines $ok]
3407 if {$ok >= [llength $ctext_file_lines] || $line < $tline} {
3408 return {}
3409 } else {
3410 return [list [lindex $ctext_file_names $ok] $tline]
3414 proc pop_diff_menu {w X Y x y} {
3415 global ctext diff_menu flist_menu_file
3416 global diff_menu_txtpos diff_menu_line
3417 global diff_menu_filebase
3419 set diff_menu_txtpos [split [$w index "@$x,$y"] "."]
3420 set diff_menu_line [lindex $diff_menu_txtpos 0]
3421 # don't pop up the menu on hunk-separator or file-separator lines
3422 if {[lsearch -glob [$ctext tag names $diff_menu_line.0] "*sep"] >= 0} {
3423 return
3425 stopfinding
3426 set f [find_ctext_fileinfo $diff_menu_line]
3427 if {$f eq {}} return
3428 set flist_menu_file [lindex $f 0]
3429 set diff_menu_filebase [lindex $f 1]
3430 tk_popup $diff_menu $X $Y
3433 proc flist_hl {only} {
3434 global flist_menu_file findstring gdttype
3436 set x [shellquote $flist_menu_file]
3437 if {$only || $findstring eq {} || $gdttype ne [mc "touching paths:"]} {
3438 set findstring $x
3439 } else {
3440 append findstring " " $x
3442 set gdttype [mc "touching paths:"]
3445 proc gitknewtmpdir {} {
3446 global diffnum gitktmpdir gitdir
3448 if {![info exists gitktmpdir]} {
3449 set gitktmpdir [file join $gitdir [format ".gitk-tmp.%s" [pid]]]
3450 if {[catch {file mkdir $gitktmpdir} err]} {
3451 error_popup "[mc "Error creating temporary directory %s:" $gitktmpdir] $err"
3452 unset gitktmpdir
3453 return {}
3455 set diffnum 0
3457 incr diffnum
3458 set diffdir [file join $gitktmpdir $diffnum]
3459 if {[catch {file mkdir $diffdir} err]} {
3460 error_popup "[mc "Error creating temporary directory %s:" $diffdir] $err"
3461 return {}
3463 return $diffdir
3466 proc save_file_from_commit {filename output what} {
3467 global nullfile
3469 if {[catch {exec git show $filename -- > $output} err]} {
3470 if {[string match "fatal: bad revision *" $err]} {
3471 return $nullfile
3473 error_popup "[mc "Error getting \"%s\" from %s:" $filename $what] $err"
3474 return {}
3476 return $output
3479 proc external_diff_get_one_file {diffid filename diffdir} {
3480 global nullid nullid2 nullfile
3481 global worktree
3483 if {$diffid == $nullid} {
3484 set difffile [file join $worktree $filename]
3485 if {[file exists $difffile]} {
3486 return $difffile
3488 return $nullfile
3490 if {$diffid == $nullid2} {
3491 set difffile [file join $diffdir "\[index\] [file tail $filename]"]
3492 return [save_file_from_commit :$filename $difffile index]
3494 set difffile [file join $diffdir "\[$diffid\] [file tail $filename]"]
3495 return [save_file_from_commit $diffid:$filename $difffile \
3496 "revision $diffid"]
3499 proc external_diff {} {
3500 global nullid nullid2
3501 global flist_menu_file
3502 global diffids
3503 global extdifftool
3505 if {[llength $diffids] == 1} {
3506 # no reference commit given
3507 set diffidto [lindex $diffids 0]
3508 if {$diffidto eq $nullid} {
3509 # diffing working copy with index
3510 set diffidfrom $nullid2
3511 } elseif {$diffidto eq $nullid2} {
3512 # diffing index with HEAD
3513 set diffidfrom "HEAD"
3514 } else {
3515 # use first parent commit
3516 global parentlist selectedline
3517 set diffidfrom [lindex $parentlist $selectedline 0]
3519 } else {
3520 set diffidfrom [lindex $diffids 0]
3521 set diffidto [lindex $diffids 1]
3524 # make sure that several diffs wont collide
3525 set diffdir [gitknewtmpdir]
3526 if {$diffdir eq {}} return
3528 # gather files to diff
3529 set difffromfile [external_diff_get_one_file $diffidfrom $flist_menu_file $diffdir]
3530 set difftofile [external_diff_get_one_file $diffidto $flist_menu_file $diffdir]
3532 if {$difffromfile ne {} && $difftofile ne {}} {
3533 set cmd [list [shellsplit $extdifftool] $difffromfile $difftofile]
3534 if {[catch {set fl [open |$cmd r]} err]} {
3535 file delete -force $diffdir
3536 error_popup "$extdifftool: [mc "command failed:"] $err"
3537 } else {
3538 fconfigure $fl -blocking 0
3539 filerun $fl [list delete_at_eof $fl $diffdir]
3544 proc find_hunk_blamespec {base line} {
3545 global ctext
3547 # Find and parse the hunk header
3548 set s_lix [$ctext search -backwards -regexp ^@@ "$line.0 lineend" $base.0]
3549 if {$s_lix eq {}} return
3551 set s_line [$ctext get $s_lix "$s_lix + 1 lines"]
3552 if {![regexp {^@@@*(( -\d+(,\d+)?)+) \+(\d+)(,\d+)? @@} $s_line \
3553 s_line old_specs osz osz1 new_line nsz]} {
3554 return
3557 # base lines for the parents
3558 set base_lines [list $new_line]
3559 foreach old_spec [lrange [split $old_specs " "] 1 end] {
3560 if {![regexp -- {-(\d+)(,\d+)?} $old_spec \
3561 old_spec old_line osz]} {
3562 return
3564 lappend base_lines $old_line
3567 # Now scan the lines to determine offset within the hunk
3568 set max_parent [expr {[llength $base_lines]-2}]
3569 set dline 0
3570 set s_lno [lindex [split $s_lix "."] 0]
3572 # Determine if the line is removed
3573 set chunk [$ctext get $line.0 "$line.1 + $max_parent chars"]
3574 if {[string match {[-+ ]*} $chunk]} {
3575 set removed_idx [string first "-" $chunk]
3576 # Choose a parent index
3577 if {$removed_idx >= 0} {
3578 set parent $removed_idx
3579 } else {
3580 set unchanged_idx [string first " " $chunk]
3581 if {$unchanged_idx >= 0} {
3582 set parent $unchanged_idx
3583 } else {
3584 # blame the current commit
3585 set parent -1
3588 # then count other lines that belong to it
3589 for {set i $line} {[incr i -1] > $s_lno} {} {
3590 set chunk [$ctext get $i.0 "$i.1 + $max_parent chars"]
3591 # Determine if the line is removed
3592 set removed_idx [string first "-" $chunk]
3593 if {$parent >= 0} {
3594 set code [string index $chunk $parent]
3595 if {$code eq "-" || ($removed_idx < 0 && $code ne "+")} {
3596 incr dline
3598 } else {
3599 if {$removed_idx < 0} {
3600 incr dline
3604 incr parent
3605 } else {
3606 set parent 0
3609 incr dline [lindex $base_lines $parent]
3610 return [list $parent $dline]
3613 proc external_blame_diff {} {
3614 global currentid cmitmode
3615 global diff_menu_txtpos diff_menu_line
3616 global diff_menu_filebase flist_menu_file
3618 if {$cmitmode eq "tree"} {
3619 set parent_idx 0
3620 set line [expr {$diff_menu_line - $diff_menu_filebase}]
3621 } else {
3622 set hinfo [find_hunk_blamespec $diff_menu_filebase $diff_menu_line]
3623 if {$hinfo ne {}} {
3624 set parent_idx [lindex $hinfo 0]
3625 set line [lindex $hinfo 1]
3626 } else {
3627 set parent_idx 0
3628 set line 0
3632 external_blame $parent_idx $line
3635 # Find the SHA1 ID of the blob for file $fname in the index
3636 # at stage 0 or 2
3637 proc index_sha1 {fname} {
3638 set f [open [list | git ls-files -s $fname] r]
3639 while {[gets $f line] >= 0} {
3640 set info [lindex [split $line "\t"] 0]
3641 set stage [lindex $info 2]
3642 if {$stage eq "0" || $stage eq "2"} {
3643 close $f
3644 return [lindex $info 1]
3647 close $f
3648 return {}
3651 # Turn an absolute path into one relative to the current directory
3652 proc make_relative {f} {
3653 if {[file pathtype $f] eq "relative"} {
3654 return $f
3656 set elts [file split $f]
3657 set here [file split [pwd]]
3658 set ei 0
3659 set hi 0
3660 set res {}
3661 foreach d $here {
3662 if {$ei < $hi || $ei >= [llength $elts] || [lindex $elts $ei] ne $d} {
3663 lappend res ".."
3664 } else {
3665 incr ei
3667 incr hi
3669 set elts [concat $res [lrange $elts $ei end]]
3670 return [eval file join $elts]
3673 proc external_blame {parent_idx {line {}}} {
3674 global flist_menu_file cdup
3675 global nullid nullid2
3676 global parentlist selectedline currentid
3678 if {$parent_idx > 0} {
3679 set base_commit [lindex $parentlist $selectedline [expr {$parent_idx-1}]]
3680 } else {
3681 set base_commit $currentid
3684 if {$base_commit eq {} || $base_commit eq $nullid || $base_commit eq $nullid2} {
3685 error_popup [mc "No such commit"]
3686 return
3689 set cmdline [list git gui blame]
3690 if {$line ne {} && $line > 1} {
3691 lappend cmdline "--line=$line"
3693 set f [file join $cdup $flist_menu_file]
3694 # Unfortunately it seems git gui blame doesn't like
3695 # being given an absolute path...
3696 set f [make_relative $f]
3697 lappend cmdline $base_commit $f
3698 if {[catch {eval exec $cmdline &} err]} {
3699 error_popup "[mc "git gui blame: command failed:"] $err"
3703 proc show_line_source {} {
3704 global cmitmode currentid parents curview blamestuff blameinst
3705 global diff_menu_line diff_menu_filebase flist_menu_file
3706 global nullid nullid2 gitdir cdup
3708 set from_index {}
3709 if {$cmitmode eq "tree"} {
3710 set id $currentid
3711 set line [expr {$diff_menu_line - $diff_menu_filebase}]
3712 } else {
3713 set h [find_hunk_blamespec $diff_menu_filebase $diff_menu_line]
3714 if {$h eq {}} return
3715 set pi [lindex $h 0]
3716 if {$pi == 0} {
3717 mark_ctext_line $diff_menu_line
3718 return
3720 incr pi -1
3721 if {$currentid eq $nullid} {
3722 if {$pi > 0} {
3723 # must be a merge in progress...
3724 if {[catch {
3725 # get the last line from .git/MERGE_HEAD
3726 set f [open [file join $gitdir MERGE_HEAD] r]
3727 set id [lindex [split [read $f] "\n"] end-1]
3728 close $f
3729 } err]} {
3730 error_popup [mc "Couldn't read merge head: %s" $err]
3731 return
3733 } elseif {$parents($curview,$currentid) eq $nullid2} {
3734 # need to do the blame from the index
3735 if {[catch {
3736 set from_index [index_sha1 $flist_menu_file]
3737 } err]} {
3738 error_popup [mc "Error reading index: %s" $err]
3739 return
3741 } else {
3742 set id $parents($curview,$currentid)
3744 } else {
3745 set id [lindex $parents($curview,$currentid) $pi]
3747 set line [lindex $h 1]
3749 set blameargs {}
3750 if {$from_index ne {}} {
3751 lappend blameargs | git cat-file blob $from_index
3753 lappend blameargs | git blame -p -L$line,+1
3754 if {$from_index ne {}} {
3755 lappend blameargs --contents -
3756 } else {
3757 lappend blameargs $id
3759 lappend blameargs -- [file join $cdup $flist_menu_file]
3760 if {[catch {
3761 set f [open $blameargs r]
3762 } err]} {
3763 error_popup [mc "Couldn't start git blame: %s" $err]
3764 return
3766 nowbusy blaming [mc "Searching"]
3767 fconfigure $f -blocking 0
3768 set i [reg_instance $f]
3769 set blamestuff($i) {}
3770 set blameinst $i
3771 filerun $f [list read_line_source $f $i]
3774 proc stopblaming {} {
3775 global blameinst
3777 if {[info exists blameinst]} {
3778 stop_instance $blameinst
3779 unset blameinst
3780 notbusy blaming
3784 proc read_line_source {fd inst} {
3785 global blamestuff curview commfd blameinst nullid nullid2
3787 while {[gets $fd line] >= 0} {
3788 lappend blamestuff($inst) $line
3790 if {![eof $fd]} {
3791 return 1
3793 unset commfd($inst)
3794 unset blameinst
3795 notbusy blaming
3796 fconfigure $fd -blocking 1
3797 if {[catch {close $fd} err]} {
3798 error_popup [mc "Error running git blame: %s" $err]
3799 return 0
3802 set fname {}
3803 set line [split [lindex $blamestuff($inst) 0] " "]
3804 set id [lindex $line 0]
3805 set lnum [lindex $line 1]
3806 if {[string length $id] == 40 && [string is xdigit $id] &&
3807 [string is digit -strict $lnum]} {
3808 # look for "filename" line
3809 foreach l $blamestuff($inst) {
3810 if {[string match "filename *" $l]} {
3811 set fname [string range $l 9 end]
3812 break
3816 if {$fname ne {}} {
3817 # all looks good, select it
3818 if {$id eq $nullid} {
3819 # blame uses all-zeroes to mean not committed,
3820 # which would mean a change in the index
3821 set id $nullid2
3823 if {[commitinview $id $curview]} {
3824 selectline [rowofcommit $id] 1 [list $fname $lnum]
3825 } else {
3826 error_popup [mc "That line comes from commit %s, \
3827 which is not in this view" [shortids $id]]
3829 } else {
3830 puts "oops couldn't parse git blame output"
3832 return 0
3835 # delete $dir when we see eof on $f (presumably because the child has exited)
3836 proc delete_at_eof {f dir} {
3837 while {[gets $f line] >= 0} {}
3838 if {[eof $f]} {
3839 if {[catch {close $f} err]} {
3840 error_popup "[mc "External diff viewer failed:"] $err"
3842 file delete -force $dir
3843 return 0
3845 return 1
3848 # Functions for adding and removing shell-type quoting
3850 proc shellquote {str} {
3851 if {![string match "*\['\"\\ \t]*" $str]} {
3852 return $str
3854 if {![string match "*\['\"\\]*" $str]} {
3855 return "\"$str\""
3857 if {![string match "*'*" $str]} {
3858 return "'$str'"
3860 return "\"[string map {\" \\\" \\ \\\\} $str]\""
3863 proc shellarglist {l} {
3864 set str {}
3865 foreach a $l {
3866 if {$str ne {}} {
3867 append str " "
3869 append str [shellquote $a]
3871 return $str
3874 proc shelldequote {str} {
3875 set ret {}
3876 set used -1
3877 while {1} {
3878 incr used
3879 if {![regexp -start $used -indices "\['\"\\\\ \t]" $str first]} {
3880 append ret [string range $str $used end]