Win32: fix detection of empty directories in is_dir_empty
[git/dscho.git] / gitk-git / gitk
blob1f55c1840203cfddcc577b030fb85d65f30a323e
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 [limit_arg_length [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 makedroplist {w varname args} {
2002 global use_ttk
2003 if {$use_ttk} {
2004 set width 0
2005 foreach label $args {
2006 set cx [string length $label]
2007 if {$cx > $width} {set width $cx}
2009 set gm [ttk::combobox $w -width $width -state readonly\
2010 -textvariable $varname -values $args]
2011 } else {
2012 set gm [eval [linsert $args 0 tk_optionMenu $w $varname]]
2014 return $gm
2017 proc makewindow {} {
2018 global canv canv2 canv3 linespc charspc ctext cflist cscroll
2019 global tabstop
2020 global findtype findtypemenu findloc findstring fstring geometry
2021 global entries sha1entry sha1string sha1but
2022 global diffcontextstring diffcontext
2023 global ignorespace
2024 global maincursor textcursor curtextcursor
2025 global rowctxmenu fakerowmenu mergemax wrapcomment
2026 global highlight_files gdttype
2027 global searchstring sstring
2028 global bgcolor fgcolor bglist fglist diffcolors selectbgcolor
2029 global headctxmenu progresscanv progressitem progresscoords statusw
2030 global fprogitem fprogcoord lastprogupdate progupdatepending
2031 global rprogitem rprogcoord rownumsel numcommits
2032 global have_tk85 use_ttk NS
2033 global git_version
2034 global worddiff
2036 # The "mc" arguments here are purely so that xgettext
2037 # sees the following string as needing to be translated
2038 set file {
2039 mc "File" cascade {
2040 {mc "Update" command updatecommits -accelerator F5}
2041 {mc "Reload" command reloadcommits -accelerator Shift-F5}
2042 {mc "Reread references" command rereadrefs}
2043 {mc "List references" command showrefs -accelerator F2}
2044 {xx "" separator}
2045 {mc "Start git gui" command {exec git gui &}}
2046 {xx "" separator}
2047 {mc "Quit" command doquit -accelerator Meta1-Q}
2049 set edit {
2050 mc "Edit" cascade {
2051 {mc "Preferences" command doprefs}
2053 set view {
2054 mc "View" cascade {
2055 {mc "New view..." command {newview 0} -accelerator Shift-F4}
2056 {mc "Edit view..." command editview -state disabled -accelerator F4}
2057 {mc "Delete view" command delview -state disabled}
2058 {xx "" separator}
2059 {mc "All files" radiobutton {selectedview 0} -command {showview 0}}
2061 if {[tk windowingsystem] ne "aqua"} {
2062 set help {
2063 mc "Help" cascade {
2064 {mc "About gitk" command about}
2065 {mc "Key bindings" command keys}
2067 set bar [list $file $edit $view $help]
2068 } else {
2069 proc ::tk::mac::ShowPreferences {} {doprefs}
2070 proc ::tk::mac::Quit {} {doquit}
2071 lset file end [lreplace [lindex $file end] end-1 end]
2072 set apple {
2073 xx "Apple" cascade {
2074 {mc "About gitk" command about}
2075 {xx "" separator}
2077 set help {
2078 mc "Help" cascade {
2079 {mc "Key bindings" command keys}
2081 set bar [list $apple $file $view $help]
2083 makemenu .bar $bar
2084 . configure -menu .bar
2086 if {$use_ttk} {
2087 # cover the non-themed toplevel with a themed frame.
2088 place [ttk::frame ._main_background] -x 0 -y 0 -relwidth 1 -relheight 1
2091 # the gui has upper and lower half, parts of a paned window.
2092 ${NS}::panedwindow .ctop -orient vertical
2094 # possibly use assumed geometry
2095 if {![info exists geometry(pwsash0)]} {
2096 set geometry(topheight) [expr {15 * $linespc}]
2097 set geometry(topwidth) [expr {80 * $charspc}]
2098 set geometry(botheight) [expr {15 * $linespc}]
2099 set geometry(botwidth) [expr {50 * $charspc}]
2100 set geometry(pwsash0) [list [expr {40 * $charspc}] 2]
2101 set geometry(pwsash1) [list [expr {60 * $charspc}] 2]
2104 # the upper half will have a paned window, a scroll bar to the right, and some stuff below
2105 ${NS}::frame .tf -height $geometry(topheight) -width $geometry(topwidth)
2106 ${NS}::frame .tf.histframe
2107 ${NS}::panedwindow .tf.histframe.pwclist -orient horizontal
2108 if {!$use_ttk} {
2109 .tf.histframe.pwclist configure -sashpad 0 -handlesize 4
2112 # create three canvases
2113 set cscroll .tf.histframe.csb
2114 set canv .tf.histframe.pwclist.canv
2115 canvas $canv \
2116 -selectbackground $selectbgcolor \
2117 -background $bgcolor -bd 0 \
2118 -yscrollincr $linespc -yscrollcommand "scrollcanv $cscroll"
2119 .tf.histframe.pwclist add $canv
2120 set canv2 .tf.histframe.pwclist.canv2
2121 canvas $canv2 \
2122 -selectbackground $selectbgcolor \
2123 -background $bgcolor -bd 0 -yscrollincr $linespc
2124 .tf.histframe.pwclist add $canv2
2125 set canv3 .tf.histframe.pwclist.canv3
2126 canvas $canv3 \
2127 -selectbackground $selectbgcolor \
2128 -background $bgcolor -bd 0 -yscrollincr $linespc
2129 .tf.histframe.pwclist add $canv3
2130 if {$use_ttk} {
2131 bind .tf.histframe.pwclist <Map> {
2132 bind %W <Map> {}
2133 .tf.histframe.pwclist sashpos 1 [lindex $::geometry(pwsash1) 0]
2134 .tf.histframe.pwclist sashpos 0 [lindex $::geometry(pwsash0) 0]
2136 } else {
2137 eval .tf.histframe.pwclist sash place 0 $geometry(pwsash0)
2138 eval .tf.histframe.pwclist sash place 1 $geometry(pwsash1)
2141 # a scroll bar to rule them
2142 ${NS}::scrollbar $cscroll -command {allcanvs yview}
2143 if {!$use_ttk} {$cscroll configure -highlightthickness 0}
2144 pack $cscroll -side right -fill y
2145 bind .tf.histframe.pwclist <Configure> {resizeclistpanes %W %w}
2146 lappend bglist $canv $canv2 $canv3
2147 pack .tf.histframe.pwclist -fill both -expand 1 -side left
2149 # we have two button bars at bottom of top frame. Bar 1
2150 ${NS}::frame .tf.bar
2151 ${NS}::frame .tf.lbar -height 15
2153 set sha1entry .tf.bar.sha1
2154 set entries $sha1entry
2155 set sha1but .tf.bar.sha1label
2156 button $sha1but -text "[mc "SHA1 ID:"] " -state disabled -relief flat \
2157 -command gotocommit -width 8
2158 $sha1but conf -disabledforeground [$sha1but cget -foreground]
2159 pack .tf.bar.sha1label -side left
2160 ${NS}::entry $sha1entry -width 40 -font textfont -textvariable sha1string
2161 trace add variable sha1string write sha1change
2162 pack $sha1entry -side left -pady 2
2164 image create bitmap bm-left -data {
2165 #define left_width 16
2166 #define left_height 16
2167 static unsigned char left_bits[] = {
2168 0x00, 0x00, 0xc0, 0x01, 0xe0, 0x00, 0x70, 0x00, 0x38, 0x00, 0x1c, 0x00,
2169 0x0e, 0x00, 0xff, 0x7f, 0xff, 0x7f, 0xff, 0x7f, 0x0e, 0x00, 0x1c, 0x00,
2170 0x38, 0x00, 0x70, 0x00, 0xe0, 0x00, 0xc0, 0x01};
2172 image create bitmap bm-right -data {
2173 #define right_width 16
2174 #define right_height 16
2175 static unsigned char right_bits[] = {
2176 0x00, 0x00, 0xc0, 0x01, 0x80, 0x03, 0x00, 0x07, 0x00, 0x0e, 0x00, 0x1c,
2177 0x00, 0x38, 0xff, 0x7f, 0xff, 0x7f, 0xff, 0x7f, 0x00, 0x38, 0x00, 0x1c,
2178 0x00, 0x0e, 0x00, 0x07, 0x80, 0x03, 0xc0, 0x01};
2180 ${NS}::button .tf.bar.leftbut -image bm-left -command goback \
2181 -state disabled -width 26
2182 pack .tf.bar.leftbut -side left -fill y
2183 ${NS}::button .tf.bar.rightbut -image bm-right -command goforw \
2184 -state disabled -width 26
2185 pack .tf.bar.rightbut -side left -fill y
2187 ${NS}::label .tf.bar.rowlabel -text [mc "Row"]
2188 set rownumsel {}
2189 ${NS}::label .tf.bar.rownum -width 7 -textvariable rownumsel \
2190 -relief sunken -anchor e
2191 ${NS}::label .tf.bar.rowlabel2 -text "/"
2192 ${NS}::label .tf.bar.numcommits -width 7 -textvariable numcommits \
2193 -relief sunken -anchor e
2194 pack .tf.bar.rowlabel .tf.bar.rownum .tf.bar.rowlabel2 .tf.bar.numcommits \
2195 -side left
2196 if {!$use_ttk} {
2197 foreach w {rownum numcommits} {.tf.bar.$w configure -font textfont}
2199 global selectedline
2200 trace add variable selectedline write selectedline_change
2202 # Status label and progress bar
2203 set statusw .tf.bar.status
2204 ${NS}::label $statusw -width 15 -relief sunken
2205 pack $statusw -side left -padx 5
2206 if {$use_ttk} {
2207 set progresscanv [ttk::progressbar .tf.bar.progress]
2208 } else {
2209 set h [expr {[font metrics uifont -linespace] + 2}]
2210 set progresscanv .tf.bar.progress
2211 canvas $progresscanv -relief sunken -height $h -borderwidth 2
2212 set progressitem [$progresscanv create rect -1 0 0 $h -fill green]
2213 set fprogitem [$progresscanv create rect -1 0 0 $h -fill yellow]
2214 set rprogitem [$progresscanv create rect -1 0 0 $h -fill red]
2216 pack $progresscanv -side right -expand 1 -fill x -padx {0 2}
2217 set progresscoords {0 0}
2218 set fprogcoord 0
2219 set rprogcoord 0
2220 bind $progresscanv <Configure> adjustprogress
2221 set lastprogupdate [clock clicks -milliseconds]
2222 set progupdatepending 0
2224 # build up the bottom bar of upper window
2225 ${NS}::label .tf.lbar.flabel -text "[mc "Find"] "
2226 ${NS}::button .tf.lbar.fnext -text [mc "next"] -command {dofind 1 1}
2227 ${NS}::button .tf.lbar.fprev -text [mc "prev"] -command {dofind -1 1}
2228 ${NS}::label .tf.lbar.flab2 -text " [mc "commit"] "
2229 pack .tf.lbar.flabel .tf.lbar.fnext .tf.lbar.fprev .tf.lbar.flab2 \
2230 -side left -fill y
2231 set gdttype [mc "containing:"]
2232 set gm [makedroplist .tf.lbar.gdttype gdttype \
2233 [mc "containing:"] \
2234 [mc "touching paths:"] \
2235 [mc "adding/removing string:"]]
2236 trace add variable gdttype write gdttype_change
2237 pack .tf.lbar.gdttype -side left -fill y
2239 set findstring {}
2240 set fstring .tf.lbar.findstring
2241 lappend entries $fstring
2242 ${NS}::entry $fstring -width 30 -textvariable findstring
2243 trace add variable findstring write find_change
2244 set findtype [mc "Exact"]
2245 set findtypemenu [makedroplist .tf.lbar.findtype \
2246 findtype [mc "Exact"] [mc "IgnCase"] [mc "Regexp"]]
2247 trace add variable findtype write findcom_change
2248 set findloc [mc "All fields"]
2249 makedroplist .tf.lbar.findloc findloc [mc "All fields"] [mc "Headline"] \
2250 [mc "Comments"] [mc "Author"] [mc "Committer"]
2251 trace add variable findloc write find_change
2252 pack .tf.lbar.findloc -side right
2253 pack .tf.lbar.findtype -side right
2254 pack $fstring -side left -expand 1 -fill x
2256 # Finish putting the upper half of the viewer together
2257 pack .tf.lbar -in .tf -side bottom -fill x
2258 pack .tf.bar -in .tf -side bottom -fill x
2259 pack .tf.histframe -fill both -side top -expand 1
2260 .ctop add .tf
2261 if {!$use_ttk} {
2262 .ctop paneconfigure .tf -height $geometry(topheight)
2263 .ctop paneconfigure .tf -width $geometry(topwidth)
2266 # now build up the bottom
2267 ${NS}::panedwindow .pwbottom -orient horizontal
2269 # lower left, a text box over search bar, scroll bar to the right
2270 # if we know window height, then that will set the lower text height, otherwise
2271 # we set lower text height which will drive window height
2272 if {[info exists geometry(main)]} {
2273 ${NS}::frame .bleft -width $geometry(botwidth)
2274 } else {
2275 ${NS}::frame .bleft -width $geometry(botwidth) -height $geometry(botheight)
2277 ${NS}::frame .bleft.top
2278 ${NS}::frame .bleft.mid
2279 ${NS}::frame .bleft.bottom
2281 ${NS}::button .bleft.top.search -text [mc "Search"] -command dosearch
2282 pack .bleft.top.search -side left -padx 5
2283 set sstring .bleft.top.sstring
2284 set searchstring ""
2285 ${NS}::entry $sstring -width 20 -textvariable searchstring
2286 lappend entries $sstring
2287 trace add variable searchstring write incrsearch
2288 pack $sstring -side left -expand 1 -fill x
2289 ${NS}::radiobutton .bleft.mid.diff -text [mc "Diff"] \
2290 -command changediffdisp -variable diffelide -value {0 0}
2291 ${NS}::radiobutton .bleft.mid.old -text [mc "Old version"] \
2292 -command changediffdisp -variable diffelide -value {0 1}
2293 ${NS}::radiobutton .bleft.mid.new -text [mc "New version"] \
2294 -command changediffdisp -variable diffelide -value {1 0}
2295 ${NS}::label .bleft.mid.labeldiffcontext -text " [mc "Lines of context"]: "
2296 pack .bleft.mid.diff .bleft.mid.old .bleft.mid.new -side left
2297 spinbox .bleft.mid.diffcontext -width 5 \
2298 -from 0 -increment 1 -to 10000000 \
2299 -validate all -validatecommand "diffcontextvalidate %P" \
2300 -textvariable diffcontextstring
2301 .bleft.mid.diffcontext set $diffcontext
2302 trace add variable diffcontextstring write diffcontextchange
2303 lappend entries .bleft.mid.diffcontext
2304 pack .bleft.mid.labeldiffcontext .bleft.mid.diffcontext -side left
2305 ${NS}::checkbutton .bleft.mid.ignspace -text [mc "Ignore space change"] \
2306 -command changeignorespace -variable ignorespace
2307 pack .bleft.mid.ignspace -side left -padx 5
2309 set worddiff [mc "Line diff"]
2310 if {[package vcompare $git_version "1.7.2"] >= 0} {
2311 makedroplist .bleft.mid.worddiff worddiff [mc "Line diff"] \
2312 [mc "Markup words"] [mc "Color words"]
2313 trace add variable worddiff write changeworddiff
2314 pack .bleft.mid.worddiff -side left -padx 5
2317 set ctext .bleft.bottom.ctext
2318 text $ctext -background $bgcolor -foreground $fgcolor \
2319 -state disabled -font textfont \
2320 -yscrollcommand scrolltext -wrap none \
2321 -xscrollcommand ".bleft.bottom.sbhorizontal set"
2322 if {$have_tk85} {
2323 $ctext conf -tabstyle wordprocessor
2325 ${NS}::scrollbar .bleft.bottom.sb -command "$ctext yview"
2326 ${NS}::scrollbar .bleft.bottom.sbhorizontal -command "$ctext xview" -orient h
2327 pack .bleft.top -side top -fill x
2328 pack .bleft.mid -side top -fill x
2329 grid $ctext .bleft.bottom.sb -sticky nsew
2330 grid .bleft.bottom.sbhorizontal -sticky ew
2331 grid columnconfigure .bleft.bottom 0 -weight 1
2332 grid rowconfigure .bleft.bottom 0 -weight 1
2333 grid rowconfigure .bleft.bottom 1 -weight 0
2334 pack .bleft.bottom -side top -fill both -expand 1
2335 lappend bglist $ctext
2336 lappend fglist $ctext
2338 $ctext tag conf comment -wrap $wrapcomment
2339 $ctext tag conf filesep -font textfontbold -back "#aaaaaa"
2340 $ctext tag conf hunksep -fore [lindex $diffcolors 2]
2341 $ctext tag conf d0 -fore [lindex $diffcolors 0]
2342 $ctext tag conf dresult -fore [lindex $diffcolors 1]
2343 $ctext tag conf m0 -fore red
2344 $ctext tag conf m1 -fore blue
2345 $ctext tag conf m2 -fore green
2346 $ctext tag conf m3 -fore purple
2347 $ctext tag conf m4 -fore brown
2348 $ctext tag conf m5 -fore "#009090"
2349 $ctext tag conf m6 -fore magenta
2350 $ctext tag conf m7 -fore "#808000"
2351 $ctext tag conf m8 -fore "#009000"
2352 $ctext tag conf m9 -fore "#ff0080"
2353 $ctext tag conf m10 -fore cyan
2354 $ctext tag conf m11 -fore "#b07070"
2355 $ctext tag conf m12 -fore "#70b0f0"
2356 $ctext tag conf m13 -fore "#70f0b0"
2357 $ctext tag conf m14 -fore "#f0b070"
2358 $ctext tag conf m15 -fore "#ff70b0"
2359 $ctext tag conf mmax -fore darkgrey
2360 set mergemax 16
2361 $ctext tag conf mresult -font textfontbold
2362 $ctext tag conf msep -font textfontbold
2363 $ctext tag conf found -back yellow
2365 .pwbottom add .bleft
2366 if {!$use_ttk} {
2367 .pwbottom paneconfigure .bleft -width $geometry(botwidth)
2370 # lower right
2371 ${NS}::frame .bright
2372 ${NS}::frame .bright.mode
2373 ${NS}::radiobutton .bright.mode.patch -text [mc "Patch"] \
2374 -command reselectline -variable cmitmode -value "patch"
2375 ${NS}::radiobutton .bright.mode.tree -text [mc "Tree"] \
2376 -command reselectline -variable cmitmode -value "tree"
2377 grid .bright.mode.patch .bright.mode.tree -sticky ew
2378 pack .bright.mode -side top -fill x
2379 set cflist .bright.cfiles
2380 set indent [font measure mainfont "nn"]
2381 text $cflist \
2382 -selectbackground $selectbgcolor \
2383 -background $bgcolor -foreground $fgcolor \
2384 -font mainfont \
2385 -tabs [list $indent [expr {2 * $indent}]] \
2386 -yscrollcommand ".bright.sb set" \
2387 -cursor [. cget -cursor] \
2388 -spacing1 1 -spacing3 1
2389 lappend bglist $cflist
2390 lappend fglist $cflist
2391 ${NS}::scrollbar .bright.sb -command "$cflist yview"
2392 pack .bright.sb -side right -fill y
2393 pack $cflist -side left -fill both -expand 1
2394 $cflist tag configure highlight \
2395 -background [$cflist cget -selectbackground]
2396 $cflist tag configure bold -font mainfontbold
2398 .pwbottom add .bright
2399 .ctop add .pwbottom
2401 # restore window width & height if known
2402 if {[info exists geometry(main)]} {
2403 if {[scan $geometry(main) "%dx%d" w h] >= 2} {
2404 if {$w > [winfo screenwidth .]} {
2405 set w [winfo screenwidth .]
2407 if {$h > [winfo screenheight .]} {
2408 set h [winfo screenheight .]
2410 wm geometry . "${w}x$h"
2414 if {[info exists geometry(state)] && $geometry(state) eq "zoomed"} {
2415 wm state . $geometry(state)
2418 if {[tk windowingsystem] eq {aqua}} {
2419 set M1B M1
2420 set ::BM "3"
2421 } else {
2422 set M1B Control
2423 set ::BM "2"
2426 if {$use_ttk} {
2427 bind .ctop <Map> {
2428 bind %W <Map> {}
2429 %W sashpos 0 $::geometry(topheight)
2431 bind .pwbottom <Map> {
2432 bind %W <Map> {}
2433 %W sashpos 0 $::geometry(botwidth)
2437 bind .pwbottom <Configure> {resizecdetpanes %W %w}
2438 pack .ctop -fill both -expand 1
2439 bindall <1> {selcanvline %W %x %y}
2440 #bindall <B1-Motion> {selcanvline %W %x %y}
2441 if {[tk windowingsystem] == "win32"} {
2442 bind . <MouseWheel> { windows_mousewheel_redirector %W %X %Y %D }
2443 bind $ctext <MouseWheel> { windows_mousewheel_redirector %W %X %Y %D ; break }
2444 } else {
2445 bindall <ButtonRelease-4> "allcanvs yview scroll -5 units"
2446 bindall <ButtonRelease-5> "allcanvs yview scroll 5 units"
2447 if {[tk windowingsystem] eq "aqua"} {
2448 bindall <MouseWheel> {
2449 set delta [expr {- (%D)}]
2450 allcanvs yview scroll $delta units
2452 bindall <Shift-MouseWheel> {
2453 set delta [expr {- (%D)}]
2454 $canv xview scroll $delta units
2458 bindall <$::BM> "canvscan mark %W %x %y"
2459 bindall <B$::BM-Motion> "canvscan dragto %W %x %y"
2460 bind all <$M1B-Key-w> {destroy [winfo toplevel %W]}
2461 bind . <$M1B-Key-w> doquit
2462 bindkey <Home> selfirstline
2463 bindkey <End> sellastline
2464 bind . <Key-Up> "selnextline -1"
2465 bind . <Key-Down> "selnextline 1"
2466 bind . <Shift-Key-Up> "dofind -1 0"
2467 bind . <Shift-Key-Down> "dofind 1 0"
2468 bindkey <Key-Right> "goforw"
2469 bindkey <Key-Left> "goback"
2470 bind . <Key-Prior> "selnextpage -1"
2471 bind . <Key-Next> "selnextpage 1"
2472 bind . <$M1B-Home> "allcanvs yview moveto 0.0"
2473 bind . <$M1B-End> "allcanvs yview moveto 1.0"
2474 bind . <$M1B-Key-Up> "allcanvs yview scroll -1 units"
2475 bind . <$M1B-Key-Down> "allcanvs yview scroll 1 units"
2476 bind . <$M1B-Key-Prior> "allcanvs yview scroll -1 pages"
2477 bind . <$M1B-Key-Next> "allcanvs yview scroll 1 pages"
2478 bindkey <Key-Delete> "$ctext yview scroll -1 pages"
2479 bindkey <Key-BackSpace> "$ctext yview scroll -1 pages"
2480 bindkey <Key-space> "$ctext yview scroll 1 pages"
2481 bindkey p "selnextline -1"
2482 bindkey n "selnextline 1"
2483 bindkey z "goback"
2484 bindkey x "goforw"
2485 bindkey k "selnextline -1"
2486 bindkey j "selnextline 1"
2487 bindkey h "goback"
2488 bindkey l "goforw"
2489 bindkey b prevfile
2490 bindkey d "$ctext yview scroll 18 units"
2491 bindkey u "$ctext yview scroll -18 units"
2492 bindkey / {focus $fstring}
2493 bindkey <Key-KP_Divide> {focus $fstring}
2494 bindkey <Key-Return> {dofind 1 1}
2495 bindkey ? {dofind -1 1}
2496 bindkey f nextfile
2497 bind . <F5> updatecommits
2498 bind . <Shift-F5> reloadcommits
2499 bind . <F2> showrefs
2500 bind . <Shift-F4> {newview 0}
2501 catch { bind . <Shift-Key-XF86_Switch_VT_4> {newview 0} }
2502 bind . <F4> edit_or_newview
2503 bind . <$M1B-q> doquit
2504 bind . <$M1B-f> {dofind 1 1}
2505 bind . <$M1B-g> {dofind 1 0}
2506 bind . <$M1B-r> dosearchback
2507 bind . <$M1B-s> dosearch
2508 bind . <$M1B-equal> {incrfont 1}
2509 bind . <$M1B-plus> {incrfont 1}
2510 bind . <$M1B-KP_Add> {incrfont 1}
2511 bind . <$M1B-minus> {incrfont -1}
2512 bind . <$M1B-KP_Subtract> {incrfont -1}
2513 wm protocol . WM_DELETE_WINDOW doquit
2514 bind . <Destroy> {stop_backends}
2515 bind . <Button-1> "click %W"
2516 bind $fstring <Key-Return> {dofind 1 1}
2517 bind $sha1entry <Key-Return> {gotocommit; break}
2518 bind $sha1entry <<PasteSelection>> clearsha1
2519 bind $cflist <1> {sel_flist %W %x %y; break}
2520 bind $cflist <B1-Motion> {sel_flist %W %x %y; break}
2521 bind $cflist <ButtonRelease-1> {treeclick %W %x %y}
2522 global ctxbut
2523 bind $cflist $ctxbut {pop_flist_menu %W %X %Y %x %y}
2524 bind $ctext $ctxbut {pop_diff_menu %W %X %Y %x %y}
2525 bind $ctext <Button-1> {focus %W}
2527 set maincursor [. cget -cursor]
2528 set textcursor [$ctext cget -cursor]
2529 set curtextcursor $textcursor
2531 set rowctxmenu .rowctxmenu
2532 makemenu $rowctxmenu {
2533 {mc "Diff this -> selected" command {diffvssel 0}}
2534 {mc "Diff selected -> this" command {diffvssel 1}}
2535 {mc "Make patch" command mkpatch}
2536 {mc "Create tag" command mktag}
2537 {mc "Write commit to file" command writecommit}
2538 {mc "Create new branch" command mkbranch}
2539 {mc "Cherry-pick this commit" command cherrypick}
2540 {mc "Reset HEAD branch to here" command resethead}
2541 {mc "Mark this commit" command markhere}
2542 {mc "Return to mark" command gotomark}
2543 {mc "Find descendant of this and mark" command find_common_desc}
2544 {mc "Compare with marked commit" command compare_commits}
2545 {mc "Diff this -> marked commit" command {diffvsmark 0}}
2546 {mc "Diff marked commit -> this" command {diffvsmark 1}}
2548 $rowctxmenu configure -tearoff 0
2550 set fakerowmenu .fakerowmenu
2551 makemenu $fakerowmenu {
2552 {mc "Diff this -> selected" command {diffvssel 0}}
2553 {mc "Diff selected -> this" command {diffvssel 1}}
2554 {mc "Make patch" command mkpatch}
2555 {mc "Diff this -> marked commit" command {diffvsmark 0}}
2556 {mc "Diff marked commit -> this" command {diffvsmark 1}}
2558 $fakerowmenu configure -tearoff 0
2560 set headctxmenu .headctxmenu
2561 makemenu $headctxmenu {
2562 {mc "Check out this branch" command cobranch}
2563 {mc "Remove this branch" command rmbranch}
2565 $headctxmenu configure -tearoff 0
2567 global flist_menu
2568 set flist_menu .flistctxmenu
2569 makemenu $flist_menu {
2570 {mc "Highlight this too" command {flist_hl 0}}
2571 {mc "Highlight this only" command {flist_hl 1}}
2572 {mc "External diff" command {external_diff}}
2573 {mc "Blame parent commit" command {external_blame 1}}
2575 $flist_menu configure -tearoff 0
2577 global diff_menu
2578 set diff_menu .diffctxmenu
2579 makemenu $diff_menu {
2580 {mc "Show origin of this line" command show_line_source}
2581 {mc "Run git gui blame on this line" command {external_blame_diff}}
2583 $diff_menu configure -tearoff 0
2586 # Windows sends all mouse wheel events to the current focused window, not
2587 # the one where the mouse hovers, so bind those events here and redirect
2588 # to the correct window
2589 proc windows_mousewheel_redirector {W X Y D} {
2590 global canv canv2 canv3
2591 set w [winfo containing -displayof $W $X $Y]
2592 if {$w ne ""} {
2593 set u [expr {$D < 0 ? 5 : -5}]
2594 if {$w == $canv || $w == $canv2 || $w == $canv3} {
2595 allcanvs yview scroll $u units
2596 } else {
2597 catch {
2598 $w yview scroll $u units
2604 # Update row number label when selectedline changes
2605 proc selectedline_change {n1 n2 op} {
2606 global selectedline rownumsel
2608 if {$selectedline eq {}} {
2609 set rownumsel {}
2610 } else {
2611 set rownumsel [expr {$selectedline + 1}]
2615 # mouse-2 makes all windows scan vertically, but only the one
2616 # the cursor is in scans horizontally
2617 proc canvscan {op w x y} {
2618 global canv canv2 canv3
2619 foreach c [list $canv $canv2 $canv3] {
2620 if {$c == $w} {
2621 $c scan $op $x $y
2622 } else {
2623 $c scan $op 0 $y
2628 proc scrollcanv {cscroll f0 f1} {
2629 $cscroll set $f0 $f1
2630 drawvisible
2631 flushhighlights
2634 # when we make a key binding for the toplevel, make sure
2635 # it doesn't get triggered when that key is pressed in the
2636 # find string entry widget.
2637 proc bindkey {ev script} {
2638 global entries
2639 bind . $ev $script
2640 set escript [bind Entry $ev]
2641 if {$escript == {}} {
2642 set escript [bind Entry <Key>]
2644 foreach e $entries {
2645 bind $e $ev "$escript; break"
2649 # set the focus back to the toplevel for any click outside
2650 # the entry widgets
2651 proc click {w} {
2652 global ctext entries
2653 foreach e [concat $entries $ctext] {
2654 if {$w == $e} return
2656 focus .
2659 # Adjust the progress bar for a change in requested extent or canvas size
2660 proc adjustprogress {} {
2661 global progresscanv progressitem progresscoords
2662 global fprogitem fprogcoord lastprogupdate progupdatepending
2663 global rprogitem rprogcoord use_ttk
2665 if {$use_ttk} {
2666 $progresscanv configure -value [expr {int($fprogcoord * 100)}]
2667 return
2670 set w [expr {[winfo width $progresscanv] - 4}]
2671 set x0 [expr {$w * [lindex $progresscoords 0]}]
2672 set x1 [expr {$w * [lindex $progresscoords 1]}]
2673 set h [winfo height $progresscanv]
2674 $progresscanv coords $progressitem $x0 0 $x1 $h
2675 $progresscanv coords $fprogitem 0 0 [expr {$w * $fprogcoord}] $h
2676 $progresscanv coords $rprogitem 0 0 [expr {$w * $rprogcoord}] $h
2677 set now [clock clicks -milliseconds]
2678 if {$now >= $lastprogupdate + 100} {
2679 set progupdatepending 0
2680 update
2681 } elseif {!$progupdatepending} {
2682 set progupdatepending 1
2683 after [expr {$lastprogupdate + 100 - $now}] doprogupdate
2687 proc doprogupdate {} {
2688 global lastprogupdate progupdatepending
2690 if {$progupdatepending} {
2691 set progupdatepending 0
2692 set lastprogupdate [clock clicks -milliseconds]
2693 update
2697 proc savestuff {w} {
2698 global canv canv2 canv3 mainfont textfont uifont tabstop
2699 global stuffsaved findmergefiles maxgraphpct
2700 global maxwidth showneartags showlocalchanges
2701 global viewname viewfiles viewargs viewargscmd viewperm nextviewnum
2702 global cmitmode wrapcomment datetimeformat limitdiffs
2703 global colors uicolor bgcolor fgcolor diffcolors diffcontext selectbgcolor
2704 global autoselect autosellen extdifftool perfile_attrs markbgcolor use_ttk
2705 global hideremotes want_ttk
2707 if {$stuffsaved} return
2708 if {![winfo viewable .]} return
2709 catch {
2710 if {[file exists ~/.gitk-new]} {file delete -force ~/.gitk-new}
2711 set f [open "~/.gitk-new" w]
2712 if {$::tcl_platform(platform) eq {windows}} {
2713 file attributes "~/.gitk-new" -hidden true
2715 puts $f [list set mainfont $mainfont]
2716 puts $f [list set textfont $textfont]
2717 puts $f [list set uifont $uifont]
2718 puts $f [list set tabstop $tabstop]
2719 puts $f [list set findmergefiles $findmergefiles]
2720 puts $f [list set maxgraphpct $maxgraphpct]
2721 puts $f [list set maxwidth $maxwidth]
2722 puts $f [list set cmitmode $cmitmode]
2723 puts $f [list set wrapcomment $wrapcomment]
2724 puts $f [list set autoselect $autoselect]
2725 puts $f [list set autosellen $autosellen]
2726 puts $f [list set showneartags $showneartags]
2727 puts $f [list set hideremotes $hideremotes]
2728 puts $f [list set showlocalchanges $showlocalchanges]
2729 puts $f [list set datetimeformat $datetimeformat]
2730 puts $f [list set limitdiffs $limitdiffs]
2731 puts $f [list set uicolor $uicolor]
2732 puts $f [list set want_ttk $want_ttk]
2733 puts $f [list set bgcolor $bgcolor]
2734 puts $f [list set fgcolor $fgcolor]
2735 puts $f [list set colors $colors]
2736 puts $f [list set diffcolors $diffcolors]
2737 puts $f [list set markbgcolor $markbgcolor]
2738 puts $f [list set diffcontext $diffcontext]
2739 puts $f [list set selectbgcolor $selectbgcolor]
2740 puts $f [list set extdifftool $extdifftool]
2741 puts $f [list set perfile_attrs $perfile_attrs]
2743 puts $f "set geometry(main) [wm geometry .]"
2744 puts $f "set geometry(state) [wm state .]"
2745 puts $f "set geometry(topwidth) [winfo width .tf]"
2746 puts $f "set geometry(topheight) [winfo height .tf]"
2747 if {$use_ttk} {
2748 puts $f "set geometry(pwsash0) \"[.tf.histframe.pwclist sashpos 0] 1\""
2749 puts $f "set geometry(pwsash1) \"[.tf.histframe.pwclist sashpos 1] 1\""
2750 } else {
2751 puts $f "set geometry(pwsash0) \"[.tf.histframe.pwclist sash coord 0]\""
2752 puts $f "set geometry(pwsash1) \"[.tf.histframe.pwclist sash coord 1]\""
2754 puts $f "set geometry(botwidth) [winfo width .bleft]"
2755 puts $f "set geometry(botheight) [winfo height .bleft]"
2757 puts -nonewline $f "set permviews {"
2758 for {set v 0} {$v < $nextviewnum} {incr v} {
2759 if {$viewperm($v)} {
2760 puts $f "{[list $viewname($v) $viewfiles($v) $viewargs($v) $viewargscmd($v)]}"
2763 puts $f "}"
2764 close $f
2765 file rename -force "~/.gitk-new" "~/.gitk"
2767 set stuffsaved 1
2770 proc resizeclistpanes {win w} {
2771 global oldwidth use_ttk
2772 if {[info exists oldwidth($win)]} {
2773 if {$use_ttk} {
2774 set s0 [$win sashpos 0]
2775 set s1 [$win sashpos 1]
2776 } else {
2777 set s0 [$win sash coord 0]
2778 set s1 [$win sash coord 1]
2780 if {$w < 60} {
2781 set sash0 [expr {int($w/2 - 2)}]
2782 set sash1 [expr {int($w*5/6 - 2)}]
2783 } else {
2784 set factor [expr {1.0 * $w / $oldwidth($win)}]
2785 set sash0 [expr {int($factor * [lindex $s0 0])}]
2786 set sash1 [expr {int($factor * [lindex $s1 0])}]
2787 if {$sash0 < 30} {
2788 set sash0 30
2790 if {$sash1 < $sash0 + 20} {
2791 set sash1 [expr {$sash0 + 20}]
2793 if {$sash1 > $w - 10} {
2794 set sash1 [expr {$w - 10}]
2795 if {$sash0 > $sash1 - 20} {
2796 set sash0 [expr {$sash1 - 20}]
2800 if {$use_ttk} {
2801 $win sashpos 0 $sash0
2802 $win sashpos 1 $sash1
2803 } else {
2804 $win sash place 0 $sash0 [lindex $s0 1]
2805 $win sash place 1 $sash1 [lindex $s1 1]
2808 set oldwidth($win) $w
2811 proc resizecdetpanes {win w} {
2812 global oldwidth use_ttk
2813 if {[info exists oldwidth($win)]} {
2814 if {$use_ttk} {
2815 set s0 [$win sashpos 0]
2816 } else {
2817 set s0 [$win sash coord 0]
2819 if {$w < 60} {
2820 set sash0 [expr {int($w*3/4 - 2)}]
2821 } else {
2822 set factor [expr {1.0 * $w / $oldwidth($win)}]
2823 set sash0 [expr {int($factor * [lindex $s0 0])}]
2824 if {$sash0 < 45} {
2825 set sash0 45
2827 if {$sash0 > $w - 15} {
2828 set sash0 [expr {$w - 15}]
2831 if {$use_ttk} {
2832 $win sashpos 0 $sash0
2833 } else {
2834 $win sash place 0 $sash0 [lindex $s0 1]
2837 set oldwidth($win) $w
2840 proc allcanvs args {
2841 global canv canv2 canv3
2842 eval $canv $args
2843 eval $canv2 $args
2844 eval $canv3 $args
2847 proc bindall {event action} {
2848 global canv canv2 canv3
2849 bind $canv $event $action
2850 bind $canv2 $event $action
2851 bind $canv3 $event $action
2854 proc about {} {
2855 global uifont NS
2856 set w .about
2857 if {[winfo exists $w]} {
2858 raise $w
2859 return
2861 ttk_toplevel $w
2862 wm title $w [mc "About gitk"]
2863 make_transient $w .
2864 message $w.m -text [mc "
2865 Gitk - a commit viewer for git
2867 Copyright \u00a9 2005-2011 Paul Mackerras
2869 Use and redistribute under the terms of the GNU General Public License"] \
2870 -justify center -aspect 400 -border 2 -bg white -relief groove
2871 pack $w.m -side top -fill x -padx 2 -pady 2
2872 ${NS}::button $w.ok -text [mc "Close"] -command "destroy $w" -default active
2873 pack $w.ok -side bottom
2874 bind $w <Visibility> "focus $w.ok"
2875 bind $w <Key-Escape> "destroy $w"
2876 bind $w <Key-Return> "destroy $w"
2877 tk::PlaceWindow $w widget .
2880 proc keys {} {
2881 global NS
2882 set w .keys
2883 if {[winfo exists $w]} {
2884 raise $w
2885 return
2887 if {[tk windowingsystem] eq {aqua}} {
2888 set M1T Cmd
2889 } else {
2890 set M1T Ctrl
2892 ttk_toplevel $w
2893 wm title $w [mc "Gitk key bindings"]
2894 make_transient $w .
2895 message $w.m -text "
2896 [mc "Gitk key bindings:"]
2898 [mc "<%s-Q> Quit" $M1T]
2899 [mc "<%s-W> Close window" $M1T]
2900 [mc "<Home> Move to first commit"]
2901 [mc "<End> Move to last commit"]
2902 [mc "<Up>, p, k Move up one commit"]
2903 [mc "<Down>, n, j Move down one commit"]
2904 [mc "<Left>, z, h Go back in history list"]
2905 [mc "<Right>, x, l Go forward in history list"]
2906 [mc "<PageUp> Move up one page in commit list"]
2907 [mc "<PageDown> Move down one page in commit list"]
2908 [mc "<%s-Home> Scroll to top of commit list" $M1T]
2909 [mc "<%s-End> Scroll to bottom of commit list" $M1T]
2910 [mc "<%s-Up> Scroll commit list up one line" $M1T]
2911 [mc "<%s-Down> Scroll commit list down one line" $M1T]
2912 [mc "<%s-PageUp> Scroll commit list up one page" $M1T]
2913 [mc "<%s-PageDown> Scroll commit list down one page" $M1T]
2914 [mc "<Shift-Up> Find backwards (upwards, later commits)"]
2915 [mc "<Shift-Down> Find forwards (downwards, earlier commits)"]
2916 [mc "<Delete>, b Scroll diff view up one page"]
2917 [mc "<Backspace> Scroll diff view up one page"]
2918 [mc "<Space> Scroll diff view down one page"]
2919 [mc "u Scroll diff view up 18 lines"]
2920 [mc "d Scroll diff view down 18 lines"]
2921 [mc "<%s-F> Find" $M1T]
2922 [mc "<%s-G> Move to next find hit" $M1T]
2923 [mc "<Return> Move to next find hit"]
2924 [mc "/ Focus the search box"]
2925 [mc "? Move to previous find hit"]
2926 [mc "f Scroll diff view to next file"]
2927 [mc "<%s-S> Search for next hit in diff view" $M1T]
2928 [mc "<%s-R> Search for previous hit in diff view" $M1T]
2929 [mc "<%s-KP+> Increase font size" $M1T]
2930 [mc "<%s-plus> Increase font size" $M1T]
2931 [mc "<%s-KP-> Decrease font size" $M1T]
2932 [mc "<%s-minus> Decrease font size" $M1T]
2933 [mc "<F5> Update"]
2935 -justify left -bg white -border 2 -relief groove
2936 pack $w.m -side top -fill both -padx 2 -pady 2
2937 ${NS}::button $w.ok -text [mc "Close"] -command "destroy $w" -default active
2938 bind $w <Key-Escape> [list destroy $w]
2939 pack $w.ok -side bottom
2940 bind $w <Visibility> "focus $w.ok"
2941 bind $w <Key-Escape> "destroy $w"
2942 bind $w <Key-Return> "destroy $w"
2945 # Procedures for manipulating the file list window at the
2946 # bottom right of the overall window.
2948 proc treeview {w l openlevs} {
2949 global treecontents treediropen treeheight treeparent treeindex
2951 set ix 0
2952 set treeindex() 0
2953 set lev 0
2954 set prefix {}
2955 set prefixend -1
2956 set prefendstack {}
2957 set htstack {}
2958 set ht 0
2959 set treecontents() {}
2960 $w conf -state normal
2961 foreach f $l {
2962 while {[string range $f 0 $prefixend] ne $prefix} {
2963 if {$lev <= $openlevs} {
2964 $w mark set e:$treeindex($prefix) "end -1c"
2965 $w mark gravity e:$treeindex($prefix) left
2967 set treeheight($prefix) $ht
2968 incr ht [lindex $htstack end]
2969 set htstack [lreplace $htstack end end]
2970 set prefixend [lindex $prefendstack end]
2971 set prefendstack [lreplace $prefendstack end end]
2972 set prefix [string range $prefix 0 $prefixend]
2973 incr lev -1
2975 set tail [string range $f [expr {$prefixend+1}] end]
2976 while {[set slash [string first "/" $tail]] >= 0} {
2977 lappend htstack $ht
2978 set ht 0
2979 lappend prefendstack $prefixend
2980 incr prefixend [expr {$slash + 1}]
2981 set d [string range $tail 0 $slash]
2982 lappend treecontents($prefix) $d
2983 set oldprefix $prefix
2984 append prefix $d
2985 set treecontents($prefix) {}
2986 set treeindex($prefix) [incr ix]
2987 set treeparent($prefix) $oldprefix
2988 set tail [string range $tail [expr {$slash+1}] end]
2989 if {$lev <= $openlevs} {
2990 set ht 1
2991 set treediropen($prefix) [expr {$lev < $openlevs}]
2992 set bm [expr {$lev == $openlevs? "tri-rt": "tri-dn"}]
2993 $w mark set d:$ix "end -1c"
2994 $w mark gravity d:$ix left
2995 set str "\n"
2996 for {set i 0} {$i < $lev} {incr i} {append str "\t"}
2997 $w insert end $str
2998 $w image create end -align center -image $bm -padx 1 \
2999 -name a:$ix
3000 $w insert end $d [highlight_tag $prefix]
3001 $w mark set s:$ix "end -1c"
3002 $w mark gravity s:$ix left
3004 incr lev
3006 if {$tail ne {}} {
3007 if {$lev <= $openlevs} {
3008 incr ht
3009 set str "\n"
3010 for {set i 0} {$i < $lev} {incr i} {append str "\t"}
3011 $w insert end $str
3012 $w insert end $tail [highlight_tag $f]
3014 lappend treecontents($prefix) $tail
3017 while {$htstack ne {}} {
3018 set treeheight($prefix) $ht
3019 incr ht [lindex $htstack end]
3020 set htstack [lreplace $htstack end end]
3021 set prefixend [lindex $prefendstack end]
3022 set prefendstack [lreplace $prefendstack end end]
3023 set prefix [string range $prefix 0 $prefixend]
3025 $w conf -state disabled
3028 proc linetoelt {l} {
3029 global treeheight treecontents
3031 set y 2
3032 set prefix {}
3033 while {1} {
3034 foreach e $treecontents($prefix) {
3035 if {$y == $l} {
3036 return "$prefix$e"
3038 set n 1
3039 if {[string index $e end] eq "/"} {
3040 set n $treeheight($prefix$e)
3041 if {$y + $n > $l} {
3042 append prefix $e
3043 incr y
3044 break
3047 incr y $n
3052 proc highlight_tree {y prefix} {
3053 global treeheight treecontents cflist
3055 foreach e $treecontents($prefix) {
3056 set path $prefix$e
3057 if {[highlight_tag $path] ne {}} {
3058 $cflist tag add bold $y.0 "$y.0 lineend"
3060 incr y
3061 if {[string index $e end] eq "/" && $treeheight($path) > 1} {
3062 set y [highlight_tree $y $path]
3065 return $y
3068 proc treeclosedir {w dir} {
3069 global treediropen treeheight treeparent treeindex
3071 set ix $treeindex($dir)
3072 $w conf -state normal
3073 $w delete s:$ix e:$ix
3074 set treediropen($dir) 0
3075 $w image configure a:$ix -image tri-rt
3076 $w conf -state disabled
3077 set n [expr {1 - $treeheight($dir)}]
3078 while {$dir ne {}} {
3079 incr treeheight($dir) $n
3080 set dir $treeparent($dir)
3084 proc treeopendir {w dir} {
3085 global treediropen treeheight treeparent treecontents treeindex
3087 set ix $treeindex($dir)
3088 $w conf -state normal
3089 $w image configure a:$ix -image tri-dn
3090 $w mark set e:$ix s:$ix
3091 $w mark gravity e:$ix right
3092 set lev 0
3093 set str "\n"
3094 set n [llength $treecontents($dir)]
3095 for {set x $dir} {$x ne {}} {set x $treeparent($x)} {
3096 incr lev
3097 append str "\t"
3098 incr treeheight($x) $n
3100 foreach e $treecontents($dir) {
3101 set de $dir$e
3102 if {[string index $e end] eq "/"} {
3103 set iy $treeindex($de)
3104 $w mark set d:$iy e:$ix
3105 $w mark gravity d:$iy left
3106 $w insert e:$ix $str
3107 set treediropen($de) 0
3108 $w image create e:$ix -align center -image tri-rt -padx 1 \
3109 -name a:$iy
3110 $w insert e:$ix $e [highlight_tag $de]
3111 $w mark set s:$iy e:$ix
3112 $w mark gravity s:$iy left
3113 set treeheight($de) 1
3114 } else {
3115 $w insert e:$ix $str
3116 $w insert e:$ix $e [highlight_tag $de]
3119 $w mark gravity e:$ix right
3120 $w conf -state disabled
3121 set treediropen($dir) 1
3122 set top [lindex [split [$w index @0,0] .] 0]
3123 set ht [$w cget -height]
3124 set l [lindex [split [$w index s:$ix] .] 0]
3125 if {$l < $top} {
3126 $w yview $l.0
3127 } elseif {$l + $n + 1 > $top + $ht} {
3128 set top [expr {$l + $n + 2 - $ht}]
3129 if {$l < $top} {
3130 set top $l
3132 $w yview $top.0
3136 proc treeclick {w x y} {
3137 global treediropen cmitmode ctext cflist cflist_top
3139 if {$cmitmode ne "tree"} return
3140 if {![info exists cflist_top]} return
3141 set l [lindex [split [$w index "@$x,$y"] "."] 0]
3142 $cflist tag remove highlight $cflist_top.0 "$cflist_top.0 lineend"
3143 $cflist tag add highlight $l.0 "$l.0 lineend"
3144 set cflist_top $l
3145 if {$l == 1} {
3146 $ctext yview 1.0
3147 return
3149 set e [linetoelt $l]
3150 if {[string index $e end] ne "/"} {
3151 showfile $e
3152 } elseif {$treediropen($e)} {
3153 treeclosedir $w $e
3154 } else {
3155 treeopendir $w $e
3159 proc setfilelist {id} {
3160 global treefilelist cflist jump_to_here
3162 treeview $cflist $treefilelist($id) 0
3163 if {$jump_to_here ne {}} {
3164 set f [lindex $jump_to_here 0]
3165 if {[lsearch -exact $treefilelist($id) $f] >= 0} {
3166 showfile $f
3171 image create bitmap tri-rt -background black -foreground blue -data {
3172 #define tri-rt_width 13
3173 #define tri-rt_height 13
3174 static unsigned char tri-rt_bits[] = {
3175 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x30, 0x00, 0x70, 0x00, 0xf0, 0x00,
3176 0xf0, 0x01, 0xf0, 0x00, 0x70, 0x00, 0x30, 0x00, 0x10, 0x00, 0x00, 0x00,
3177 0x00, 0x00};
3178 } -maskdata {
3179 #define tri-rt-mask_width 13
3180 #define tri-rt-mask_height 13
3181 static unsigned char tri-rt-mask_bits[] = {
3182 0x08, 0x00, 0x18, 0x00, 0x38, 0x00, 0x78, 0x00, 0xf8, 0x00, 0xf8, 0x01,
3183 0xf8, 0x03, 0xf8, 0x01, 0xf8, 0x00, 0x78, 0x00, 0x38, 0x00, 0x18, 0x00,
3184 0x08, 0x00};
3186 image create bitmap tri-dn -background black -foreground blue -data {
3187 #define tri-dn_width 13
3188 #define tri-dn_height 13
3189 static unsigned char tri-dn_bits[] = {
3190 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0x07, 0xf8, 0x03,
3191 0xf0, 0x01, 0xe0, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3192 0x00, 0x00};
3193 } -maskdata {
3194 #define tri-dn-mask_width 13
3195 #define tri-dn-mask_height 13
3196 static unsigned char tri-dn-mask_bits[] = {
3197 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x1f, 0xfe, 0x0f, 0xfc, 0x07,
3198 0xf8, 0x03, 0xf0, 0x01, 0xe0, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00,
3199 0x00, 0x00};
3202 image create bitmap reficon-T -background black -foreground yellow -data {
3203 #define tagicon_width 13
3204 #define tagicon_height 9
3205 static unsigned char tagicon_bits[] = {
3206 0x00, 0x00, 0x00, 0x00, 0xf0, 0x07, 0xf8, 0x07,
3207 0xfc, 0x07, 0xf8, 0x07, 0xf0, 0x07, 0x00, 0x00, 0x00, 0x00};
3208 } -maskdata {
3209 #define tagicon-mask_width 13
3210 #define tagicon-mask_height 9
3211 static unsigned char tagicon-mask_bits[] = {
3212 0x00, 0x00, 0xf0, 0x0f, 0xf8, 0x0f, 0xfc, 0x0f,
3213 0xfe, 0x0f, 0xfc, 0x0f, 0xf8, 0x0f, 0xf0, 0x0f, 0x00, 0x00};
3215 set rectdata {
3216 #define headicon_width 13
3217 #define headicon_height 9
3218 static unsigned char headicon_bits[] = {
3219 0x00, 0x00, 0x00, 0x00, 0xf8, 0x07, 0xf8, 0x07,
3220 0xf8, 0x07, 0xf8, 0x07, 0xf8, 0x07, 0x00, 0x00, 0x00, 0x00};
3222 set rectmask {
3223 #define headicon-mask_width 13
3224 #define headicon-mask_height 9
3225 static unsigned char headicon-mask_bits[] = {
3226 0x00, 0x00, 0xfc, 0x0f, 0xfc, 0x0f, 0xfc, 0x0f,
3227 0xfc, 0x0f, 0xfc, 0x0f, 0xfc, 0x0f, 0xfc, 0x0f, 0x00, 0x00};
3229 image create bitmap reficon-H -background black -foreground green \
3230 -data $rectdata -maskdata $rectmask
3231 image create bitmap reficon-o -background black -foreground "#ddddff" \
3232 -data $rectdata -maskdata $rectmask
3234 proc init_flist {first} {
3235 global cflist cflist_top difffilestart
3237 $cflist conf -state normal
3238 $cflist delete 0.0 end
3239 if {$first ne {}} {
3240 $cflist insert end $first
3241 set cflist_top 1
3242 $cflist tag add highlight 1.0 "1.0 lineend"
3243 } else {
3244 catch {unset cflist_top}
3246 $cflist conf -state disabled
3247 set difffilestart {}
3250 proc highlight_tag {f} {
3251 global highlight_paths
3253 foreach p $highlight_paths {
3254 if {[string match $p $f]} {
3255 return "bold"
3258 return {}
3261 proc highlight_filelist {} {
3262 global cmitmode cflist
3264 $cflist conf -state normal
3265 if {$cmitmode ne "tree"} {
3266 set end [lindex [split [$cflist index end] .] 0]
3267 for {set l 2} {$l < $end} {incr l} {
3268 set line [$cflist get $l.0 "$l.0 lineend"]
3269 if {[highlight_tag $line] ne {}} {
3270 $cflist tag add bold $l.0 "$l.0 lineend"
3273 } else {
3274 highlight_tree 2 {}
3276 $cflist conf -state disabled
3279 proc unhighlight_filelist {} {
3280 global cflist
3282 $cflist conf -state normal
3283 $cflist tag remove bold 1.0 end
3284 $cflist conf -state disabled
3287 proc add_flist {fl} {
3288 global cflist
3290 $cflist conf -state normal
3291 foreach f $fl {
3292 $cflist insert end "\n"
3293 $cflist insert end $f [highlight_tag $f]
3295 $cflist conf -state disabled
3298 proc sel_flist {w x y} {
3299 global ctext difffilestart cflist cflist_top cmitmode
3301 if {$cmitmode eq "tree"} return
3302 if {![info exists cflist_top]} return
3303 set l [lindex [split [$w index "@$x,$y"] "."] 0]
3304 $cflist tag remove highlight $cflist_top.0 "$cflist_top.0 lineend"
3305 $cflist tag add highlight $l.0 "$l.0 lineend"
3306 set cflist_top $l
3307 if {$l == 1} {
3308 $ctext yview 1.0
3309 } else {
3310 catch {$ctext yview [lindex $difffilestart [expr {$l - 2}]]}
3314 proc pop_flist_menu {w X Y x y} {
3315 global ctext cflist cmitmode flist_menu flist_menu_file
3316 global treediffs diffids
3318 stopfinding
3319 set l [lindex [split [$w index "@$x,$y"] "."] 0]
3320 if {$l <= 1} return
3321 if {$cmitmode eq "tree"} {
3322 set e [linetoelt $l]
3323 if {[string index $e end] eq "/"} return
3324 } else {
3325 set e [lindex $treediffs($diffids) [expr {$l-2}]]
3327 set flist_menu_file $e
3328 set xdiffstate "normal"
3329 if {$cmitmode eq "tree"} {
3330 set xdiffstate "disabled"
3332 # Disable "External diff" item in tree mode
3333 $flist_menu entryconf 2 -state $xdiffstate
3334 tk_popup $flist_menu $X $Y
3337 proc find_ctext_fileinfo {line} {
3338 global ctext_file_names ctext_file_lines
3340 set ok [bsearch $ctext_file_lines $line]
3341 set tline [lindex $ctext_file_lines $ok]
3343 if {$ok >= [llength $ctext_file_lines] || $line < $tline} {
3344 return {}
3345 } else {
3346 return [list [lindex $ctext_file_names $ok] $tline]
3350 proc pop_diff_menu {w X Y x y} {
3351 global ctext diff_menu flist_menu_file
3352 global diff_menu_txtpos diff_menu_line
3353 global diff_menu_filebase
3355 set diff_menu_txtpos [split [$w index "@$x,$y"] "."]
3356 set diff_menu_line [lindex $diff_menu_txtpos 0]
3357 # don't pop up the menu on hunk-separator or file-separator lines
3358 if {[lsearch -glob [$ctext tag names $diff_menu_line.0] "*sep"] >= 0} {
3359 return
3361 stopfinding
3362 set f [find_ctext_fileinfo $diff_menu_line]
3363 if {$f eq {}} return
3364 set flist_menu_file [lindex $f 0]
3365 set diff_menu_filebase [lindex $f 1]
3366 tk_popup $diff_menu $X $Y
3369 proc flist_hl {only} {
3370 global flist_menu_file findstring gdttype
3372 set x [shellquote $flist_menu_file]
3373 if {$only || $findstring eq {} || $gdttype ne [mc "touching paths:"]} {
3374 set findstring $x
3375 } else {
3376 append findstring " " $x
3378 set gdttype [mc "touching paths:"]
3381 proc gitknewtmpdir {} {
3382 global diffnum gitktmpdir gitdir
3384 if {![info exists gitktmpdir]} {
3385 set gitktmpdir [file join $gitdir [format ".gitk-tmp.%s" [pid]]]
3386 if {[catch {file mkdir $gitktmpdir} err]} {
3387 error_popup "[mc "Error creating temporary directory %s:" $gitktmpdir] $err"
3388 unset gitktmpdir
3389 return {}
3391 set diffnum 0
3393 incr diffnum
3394 set diffdir [file join $gitktmpdir $diffnum]
3395 if {[catch {file mkdir $diffdir} err]} {
3396 error_popup "[mc "Error creating temporary directory %s:" $diffdir] $err"
3397 return {}
3399 return $diffdir
3402 proc save_file_from_commit {filename output what} {
3403 global nullfile
3405 if {[catch {exec git show $filename -- > $output} err]} {
3406 if {[string match "fatal: bad revision *" $err]} {
3407 return $nullfile
3409 error_popup "[mc "Error getting \"%s\" from %s:" $filename $what] $err"
3410 return {}
3412 return $output
3415 proc external_diff_get_one_file {diffid filename diffdir} {
3416 global nullid nullid2 nullfile
3417 global worktree
3419 if {$diffid == $nullid} {
3420 set difffile [file join $worktree $filename]
3421 if {[file exists $difffile]} {
3422 return $difffile
3424 return $nullfile
3426 if {$diffid == $nullid2} {
3427 set difffile [file join $diffdir "\[index\] [file tail $filename]"]
3428 return [save_file_from_commit :$filename $difffile index]
3430 set difffile [file join $diffdir "\[$diffid\] [file tail $filename]"]
3431 return [save_file_from_commit $diffid:$filename $difffile \
3432 "revision $diffid"]
3435 proc external_diff {} {
3436 global nullid nullid2
3437 global flist_menu_file
3438 global diffids
3439 global extdifftool
3441 if {[llength $diffids] == 1} {
3442 # no reference commit given
3443 set diffidto [lindex $diffids 0]
3444 if {$diffidto eq $nullid} {
3445 # diffing working copy with index
3446 set diffidfrom $nullid2
3447 } elseif {$diffidto eq $nullid2} {
3448 # diffing index with HEAD
3449 set diffidfrom "HEAD"
3450 } else {
3451 # use first parent commit
3452 global parentlist selectedline
3453 set diffidfrom [lindex $parentlist $selectedline 0]
3455 } else {
3456 set diffidfrom [lindex $diffids 0]
3457 set diffidto [lindex $diffids 1]
3460 # make sure that several diffs wont collide
3461 set diffdir [gitknewtmpdir]
3462 if {$diffdir eq {}} return
3464 # gather files to diff
3465 set difffromfile [external_diff_get_one_file $diffidfrom $flist_menu_file $diffdir]
3466 set difftofile [external_diff_get_one_file $diffidto $flist_menu_file $diffdir]
3468 if {$difffromfile ne {} && $difftofile ne {}} {
3469 set cmd [list [shellsplit $extdifftool] $difffromfile $difftofile]
3470 if {[catch {set fl [open |$cmd r]} err]} {
3471 file delete -force $diffdir
3472 error_popup "$extdifftool: [mc "command failed:"] $err"
3473 } else {
3474 fconfigure $fl -blocking 0
3475 filerun $fl [list delete_at_eof $fl $diffdir]
3480 proc find_hunk_blamespec {base line} {
3481 global ctext
3483 # Find and parse the hunk header
3484 set s_lix [$ctext search -backwards -regexp ^@@ "$line.0 lineend" $base.0]
3485 if {$s_lix eq {}} return
3487 set s_line [$ctext get $s_lix "$s_lix + 1 lines"]
3488 if {![regexp {^@@@*(( -\d+(,\d+)?)+) \+(\d+)(,\d+)? @@} $s_line \
3489 s_line old_specs osz osz1 new_line nsz]} {
3490 return
3493 # base lines for the parents
3494 set base_lines [list $new_line]
3495 foreach old_spec [lrange [split $old_specs " "] 1 end] {
3496 if {![regexp -- {-(\d+)(,\d+)?} $old_spec \
3497 old_spec old_line osz]} {
3498 return
3500 lappend base_lines $old_line
3503 # Now scan the lines to determine offset within the hunk
3504 set max_parent [expr {[llength $base_lines]-2}]
3505 set dline 0
3506 set s_lno [lindex [split $s_lix "."] 0]
3508 # Determine if the line is removed
3509 set chunk [$ctext get $line.0 "$line.1 + $max_parent chars"]
3510 if {[string match {[-+ ]*} $chunk]} {
3511 set removed_idx [string first "-" $chunk]
3512 # Choose a parent index
3513 if {$removed_idx >= 0} {
3514 set parent $removed_idx
3515 } else {
3516 set unchanged_idx [string first " " $chunk]
3517 if {$unchanged_idx >= 0} {
3518 set parent $unchanged_idx
3519 } else {
3520 # blame the current commit
3521 set parent -1
3524 # then count other lines that belong to it
3525 for {set i $line} {[incr i -1] > $s_lno} {} {
3526 set chunk [$ctext get $i.0 "$i.1 + $max_parent chars"]
3527 # Determine if the line is removed
3528 set removed_idx [string first "-" $chunk]
3529 if {$parent >= 0} {
3530 set code [string index $chunk $parent]
3531 if {$code eq "-" || ($removed_idx < 0 && $code ne "+")} {
3532 incr dline
3534 } else {
3535 if {$removed_idx < 0} {
3536 incr dline
3540 incr parent
3541 } else {
3542 set parent 0
3545 incr dline [lindex $base_lines $parent]
3546 return [list $parent $dline]
3549 proc external_blame_diff {} {
3550 global currentid cmitmode
3551 global diff_menu_txtpos diff_menu_line
3552 global diff_menu_filebase flist_menu_file
3554 if {$cmitmode eq "tree"} {
3555 set parent_idx 0
3556 set line [expr {$diff_menu_line - $diff_menu_filebase}]
3557 } else {
3558 set hinfo [find_hunk_blamespec $diff_menu_filebase $diff_menu_line]
3559 if {$hinfo ne {}} {
3560 set parent_idx [lindex $hinfo 0]
3561 set line [lindex $hinfo 1]
3562 } else {
3563 set parent_idx 0
3564 set line 0
3568 external_blame $parent_idx $line
3571 # Find the SHA1 ID of the blob for file $fname in the index
3572 # at stage 0 or 2
3573 proc index_sha1 {fname} {
3574 set f [open [list | git ls-files -s $fname] r]
3575 while {[gets $f line] >= 0} {
3576 set info [lindex [split $line "\t"] 0]
3577 set stage [lindex $info 2]
3578 if {$stage eq "0" || $stage eq "2"} {
3579 close $f
3580 return [lindex $info 1]
3583 close $f
3584 return {}
3587 # Turn an absolute path into one relative to the current directory
3588 proc make_relative {f} {
3589 if {[file pathtype $f] eq "relative"} {
3590 return $f
3592 set elts [file split $f]
3593 set here [file split [pwd]]
3594 set ei 0
3595 set hi 0
3596 set res {}
3597 foreach d $here {
3598 if {$ei < $hi || $ei >= [llength $elts] || [lindex $elts $ei] ne $d} {
3599 lappend res ".."
3600 } else {
3601 incr ei
3603 incr hi
3605 set elts [concat $res [lrange $elts $ei end]]
3606 return [eval file join $elts]
3609 proc external_blame {parent_idx {line {}}} {
3610 global flist_menu_file cdup
3611 global nullid nullid2
3612 global parentlist selectedline currentid
3614 if {$parent_idx > 0} {
3615 set base_commit [lindex $parentlist $selectedline [expr {$parent_idx-1}]]
3616 } else {
3617 set base_commit $currentid
3620 if {$base_commit eq {} || $base_commit eq $nullid || $base_commit eq $nullid2} {
3621 error_popup [mc "No such commit"]
3622 return
3625 set cmdline [list git gui blame]
3626 if {$line ne {} && $line > 1} {
3627 lappend cmdline "--line=$line"
3629 set f [file join $cdup $flist_menu_file]
3630 # Unfortunately it seems git gui blame doesn't like
3631 # being given an absolute path...
3632 set f [make_relative $f]
3633 lappend cmdline $base_commit $f
3634 if {[catch {eval exec $cmdline &} err]} {
3635 error_popup "[mc "git gui blame: command failed:"] $err"
3639 proc show_line_source {} {
3640 global cmitmode currentid parents curview blamestuff blameinst
3641 global diff_menu_line diff_menu_filebase flist_menu_file
3642 global nullid nullid2 gitdir cdup
3644 set from_index {}
3645 if {$cmitmode eq "tree"} {
3646 set id $currentid
3647 set line [expr {$diff_menu_line - $diff_menu_filebase}]
3648 } else {
3649 set h [find_hunk_blamespec $diff_menu_filebase $diff_menu_line]
3650 if {$h eq {}} return
3651 set pi [lindex $h 0]
3652 if {$pi == 0} {
3653 mark_ctext_line $diff_menu_line
3654 return
3656 incr pi -1
3657 if {$currentid eq $nullid} {
3658 if {$pi > 0} {
3659 # must be a merge in progress...
3660 if {[catch {
3661 # get the last line from .git/MERGE_HEAD
3662 set f [open [file join $gitdir MERGE_HEAD] r]
3663 set id [lindex [split [read $f] "\n"] end-1]
3664 close $f
3665 } err]} {
3666 error_popup [mc "Couldn't read merge head: %s" $err]
3667 return
3669 } elseif {$parents($curview,$currentid) eq $nullid2} {
3670 # need to do the blame from the index
3671 if {[catch {
3672 set from_index [index_sha1 $flist_menu_file]
3673 } err]} {
3674 error_popup [mc "Error reading index: %s" $err]
3675 return
3677 } else {
3678 set id $parents($curview,$currentid)
3680 } else {
3681 set id [lindex $parents($curview,$currentid) $pi]
3683 set line [lindex $h 1]
3685 set blameargs {}
3686 if {$from_index ne {}} {
3687 lappend blameargs | git cat-file blob $from_index
3689 lappend blameargs | git blame -p -L$line,+1
3690 if {$from_index ne {}} {
3691 lappend blameargs --contents -
3692 } else {
3693 lappend blameargs $id
3695 lappend blameargs -- [file join $cdup $flist_menu_file]
3696 if {[catch {
3697 set f [open $blameargs r]
3698 } err]} {
3699 error_popup [mc "Couldn't start git blame: %s" $err]
3700 return
3702 nowbusy blaming [mc "Searching"]
3703 fconfigure $f -blocking 0
3704 set i [reg_instance $f]
3705 set blamestuff($i) {}
3706 set blameinst $i
3707 filerun $f [list read_line_source $f $i]
3710 proc stopblaming {} {
3711 global blameinst
3713 if {[info exists blameinst]} {
3714 stop_instance $blameinst
3715 unset blameinst
3716 notbusy blaming
3720 proc read_line_source {fd inst} {
3721 global blamestuff curview commfd blameinst nullid nullid2
3723 while {[gets $fd line] >= 0} {
3724 lappend blamestuff($inst) $line
3726 if {![eof $fd]} {
3727 return 1
3729 unset commfd($inst)
3730 unset blameinst
3731 notbusy blaming
3732 fconfigure $fd -blocking 1
3733 if {[catch {close $fd} err]} {
3734 error_popup [mc "Error running git blame: %s" $err]
3735 return 0
3738 set fname {}
3739 set line [split [lindex $blamestuff($inst) 0] " "]
3740 set id [lindex $line 0]
3741 set lnum [lindex $line 1]
3742 if {[string length $id] == 40 && [string is xdigit $id] &&
3743 [string is digit -strict $lnum]} {
3744 # look for "filename" line
3745 foreach l $blamestuff($inst) {
3746 if {[string match "filename *" $l]} {
3747 set fname [string range $l 9 end]
3748 break
3752 if {$fname ne {}} {
3753 # all looks good, select it
3754 if {$id eq $nullid} {
3755 # blame uses all-zeroes to mean not committed,
3756 # which would mean a change in the index
3757 set id $nullid2
3759 if {[commitinview $id $curview]} {
3760 selectline [rowofcommit $id] 1 [list $fname $lnum]
3761 } else {
3762 error_popup [mc "That line comes from commit %s, \
3763 which is not in this view" [shortids $id]]
3765 } else {
3766 puts "oops couldn't parse git blame output"
3768 return 0
3771 # delete $dir when we see eof on $f (presumably because the child has exited)
3772 proc delete_at_eof {f dir} {
3773 while {[gets $f line] >= 0} {}
3774 if {[eof $f]} {
3775 if {[catch {close $f} err]} {
3776 error_popup "[mc "External diff viewer failed:"] $err"
3778 file delete -force $dir
3779 return 0
3781 return 1
3784 # Functions for adding and removing shell-type quoting
3786 proc shellquote {str} {
3787 if {![string match "*\['\"\\ \t]*" $str]} {
3788 return $str
3790 if {![string match "*\['\"\\]*" $str]} {
3791 return "\"$str\""
3793 if {![string match "*'*" $str]} {
3794 return "'$str'"
3796 return "\"[string map {\" \\\" \\ \\\\} $str]\""
3799 proc shellarglist {l} {
3800 set str {}
3801 foreach a $l {
3802 if {$str ne {}} {
3803 append str " "
3805 append str [shellquote $a]
3807 return $str
3810 proc shelldequote {str} {
3811 set ret {}
3812 set used -1
3813 while {1} {
3814 incr used
3815 if {![regexp -start $used -indices "\['\"\\\\ \t]" $str first]} {
3816 append ret [string range $str $used end]
3817 set used [string length $str]
3818 break
3820 set first [lindex $first 0]
3821 set ch [string index $str $first]
3822 if {$first > $used} {
3823 append ret [string range $str $used [expr {$first - 1}]]
3824 set used $first
3826 if {$ch eq " " || $ch eq "\t"} break
3827 incr used
3828 if {$ch eq "'"} {
3829 set first [string first "'" $str $used]
3830 if {$first < 0} {
3831 error "unmatched single-quote"
3833 append ret [string range $str $used [expr {$first - 1}]]
3834 set used $first
3835 continue
3837 if {$ch eq "\\"} {
3838 if {$used >= [string length $str]} {
3839 error "trailing backslash"
3841 append ret [string index $str $used]
3842 continue
3844 # here ch == "\""
3845 while {1} {
3846 if {![regexp -start $used -indices "\[\"\\\\]" $str first]} {
3847 error "unmatched double-quote"
3849 set first [lindex $first 0]
3850 set ch [string index $str $first]
3851 if {$first > $used} {
3852 append ret [string range $str $used [expr {$first - 1}]]
3853 set used $first
3855 if {$ch eq "\""} break
3856 incr used
3857 append ret [string index $str $used]
3858 incr used
3861 return [list $used $ret]
3864 proc shellsplit {str} {
3865 set l {}
3866 while {1} {
3867 set str [string trimleft $str]
3868 if {$str eq {}} break
3869 set dq [shelldequote $str]
3870 set n [lindex $dq 0]
3871 set word [lindex $dq 1]
3872 set str [string range $str $n end]
3873 lappend l $word
3875 return $l
3878 # Code to implement multiple views
3880 proc newview {ishighlight} {
3881 global nextviewnum newviewname newishighlight
3882 global revtreeargs viewargscmd newviewopts curview
3884 set newishighlight $ishighlight
3885 set top .gitkview
3886 if {[winfo exists $top]} {
3887 raise $top
3888 return
3890 decode_view_opts $nextviewnum $revtreeargs
3891 set newviewname($nextviewnum) "[mc "View"] $nextviewnum"
3892 set newviewopts($nextviewnum,perm) 0
3893 set newviewopts($nextviewnum,cmd) $viewargscmd($curview)
3894 vieweditor $top $nextviewnum [mc "Gitk view definition"]
3897 set known_view_options {
3898 {perm b . {} {mc "Remember this view"}}
3899 {reflabel l + {} {mc "References (space separated list):"}}
3900 {refs t15 .. {} {mc "Branches & tags:"}}
3901 {allrefs b *. "--all" {mc "All refs"}}
3902 {branches b . "--branches" {mc "All (local) branches"}}
3903 {tags b . "--tags" {mc "All tags"}}
3904 {remotes b . "--remotes" {mc "All remote-tracking branches"}}
3905 {commitlbl l + {} {mc "Commit Info (regular expressions):"}}
3906 {author t15 .. "--author=*" {mc "Author:"}}
3907 {committer t15 . "--committer=*" {mc "Committer:"}}
3908 {loginfo t15 .. "--grep=*" {mc "Commit Message:"}}
3909 {allmatch b .. "--all-match" {mc "Matches all Commit Info criteria"}}
3910 {changes_l l + {} {mc "Changes to Files:"}}
3911 {pickaxe_s r0 . {} {mc "Fixed String"}}
3912 {pickaxe_t r1 . "--pickaxe-regex" {mc "Regular Expression"}}
3913 {pickaxe t15 .. "-S*" {mc "Search string:"}}
3914 {datelabel l + {} {mc "Commit Dates (\"2 weeks ago\", \"2009-03-17 15:27:38\", \"March 17, 2009 15:27:38\"):"}}
3915 {since t15 .. {"--since=*" "--after=*"} {mc "Since:"}}