at commit d2af22cc

1 #!/bin/sh
2 # Tcl ignores the next line -*- tcl -*- \
3 exec wish "$0" -- "$@"
4
5 # Copyright © 2005-2016 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.
9
10 package require Tk
11
12 ######################################################################
13 ##
14 ## Enabling platform-specific code paths
15
16 proc is_Windows {} {
17 if {$::tcl_platform(platform) eq {windows}} {
18 return 1
19 }
20 return 0
21 }
22
23 ######################################################################
24 ##
25 ## PATH lookup
26
27 if {[is_Windows]} {
28 set _search_path {}
29 proc _which {what args} {
30 global env _search_path
31
32 if {$_search_path eq {}} {
33 set gitguidir [file dirname [info script]]
34 regsub -all ";" $gitguidir "\\;" gitguidir
35 set env(PATH) "$gitguidir;$env(PATH)"
36 set _search_path [split $env(PATH) {;}]
37 # Skip empty `PATH` elements
38 set _search_path [lsearch -all -inline -not -exact \
39 $_search_path ""]
40 }
41
42 if {[lsearch -exact $args -script] >= 0} {
43 set suffix {}
44 } else {
45 set suffix .exe
46 }
47
48 foreach p $_search_path {
49 set p [file join $p $what$suffix]
50 if {[file exists $p]} {
51 return [file normalize $p]
52 }
53 }
54 return {}
55 }
56
57 proc sanitize_command_line {command_line from_index} {
58 set i $from_index
59 while {$i < [llength $command_line]} {
60 set cmd [lindex $command_line $i]
61 if {[llength [file split $cmd]] < 2} {
62 set fullpath [_which $cmd]
63 if {$fullpath eq ""} {
64 throw {NOT-FOUND} "$cmd not found in PATH"
65 }
66 lset command_line $i $fullpath
67 }
68
69 # handle piped commands, e.g. `exec A | B`
70 for {incr i} {$i < [llength $command_line]} {incr i} {
71 if {[lindex $command_line $i] eq "|"} {
72 incr i
73 break
74 }
75 }
76 }
77 return $command_line
78 }
79
80 # Override `exec` to avoid unsafe PATH lookup
81
82 rename exec real_exec
83
84 proc exec {args} {
85 # skip options
86 for {set i 0} {$i < [llength $args]} {incr i} {
87 set arg [lindex $args $i]
88 if {$arg eq "--"} {
89 incr i
90 break
91 }
92 if {[string range $arg 0 0] ne "-"} {
93 break
94 }
95 }
96 set args [sanitize_command_line $args $i]
97 uplevel 1 real_exec $args
98 }
99
100 # Override `open` to avoid unsafe PATH lookup
101
102 rename open real_open
103
104 proc open {args} {
105 set arg0 [lindex $args 0]
106 if {[string range $arg0 0 0] eq "|"} {
107 set command_line [string trim [string range $arg0 1 end]]
108 lset args 0 "| [sanitize_command_line $command_line 0]"
109 }
110 uplevel 1 real_open $args
111 }
112 }
113
114 # End of safe PATH lookup stuff
115
116 proc hasworktree {} {
117 return [expr {[exec git rev-parse --is-bare-repository] == "false" &&
118 [exec git rev-parse --is-inside-git-dir] == "false"}]
119 }
120
121 proc reponame {} {
122 global gitdir
123 set n [file normalize $gitdir]
124 if {[string match "*/.git" $n]} {
125 set n [string range $n 0 end-5]
126 }
127 return [file tail $n]
128 }
129
130 proc gitworktree {} {
131 variable _gitworktree
132 if {[info exists _gitworktree]} {
133 return $_gitworktree
134 }
135 # v1.7.0 introduced --show-toplevel to return the canonical work-tree
136 if {[catch {set _gitworktree [exec git rev-parse --show-toplevel]}]} {
137 # try to set work tree from environment, core.worktree or use
138 # cdup to obtain a relative path to the top of the worktree. If
139 # run from the top, the ./ prefix ensures normalize expands pwd.
140 if {[catch { set _gitworktree $env(GIT_WORK_TREE) }]} {
141 if {[catch {set _gitworktree [exec git config --get core.worktree]}]} {
142 set _gitworktree [file normalize ./[exec git rev-parse --show-cdup]]
143 }
144 }
145 }
146 return $_gitworktree
147 }
148
149 # A simple scheduler for compute-intensive stuff.
150 # The aim is to make sure that event handlers for GUI actions can
151 # run at least every 50-100 ms. Unfortunately fileevent handlers are
152 # run before X event handlers, so reading from a fast source can
153 # make the GUI completely unresponsive.
154 proc run args {
155 global isonrunq runq currunq
156
157 set script $args
158 if {[info exists isonrunq($script)]} return
159 if {$runq eq {} && ![info exists currunq]} {
160 after idle dorunq
161 }
162 lappend runq [list {} $script]
163 set isonrunq($script) 1
164 }
165
166 proc filerun {fd script} {
167 fileevent $fd readable [list filereadable $fd $script]
168 }
169
170 proc filereadable {fd script} {
171 global runq currunq
172
173 fileevent $fd readable {}
174 if {$runq eq {} && ![info exists currunq]} {
175 after idle dorunq
176 }
177 lappend runq [list $fd $script]
178 }
179
180 proc nukefile {fd} {
181 global runq
182
183 for {set i 0} {$i < [llength $runq]} {} {
184 if {[lindex $runq $i 0] eq $fd} {
185 set runq [lreplace $runq $i $i]
186 } else {
187 incr i
188 }
189 }
190 }
191
192 proc dorunq {} {
193 global isonrunq runq currunq
194
195 set tstart [clock clicks -milliseconds]
196 set t0 $tstart
197 while {[llength $runq] > 0} {
198 set fd [lindex $runq 0 0]
199 set script [lindex $runq 0 1]
200 set currunq [lindex $runq 0]
201 set runq [lrange $runq 1 end]
202 set repeat [eval $script]
203 unset currunq
204 set t1 [clock clicks -milliseconds]
205 set t [expr {$t1 - $t0}]
206 if {$repeat ne {} && $repeat} {
207 if {$fd eq {} || $repeat == 2} {
208 # script returns 1 if it wants to be readded
209 # file readers return 2 if they could do more straight away
210 lappend runq [list $fd $script]
211 } else {
212 fileevent $fd readable [list filereadable $fd $script]
213 }
214 } elseif {$fd eq {}} {
215 unset isonrunq($script)
216 }
217 set t0 $t1
218 if {$t1 - $tstart >= 80} break
219 }
220 if {$runq ne {}} {
221 after idle dorunq
222 }
223 }
224
225 proc reg_instance {fd} {
226 global commfd leftover loginstance
227
228 set i [incr loginstance]
229 set commfd($i) $fd
230 set leftover($i) {}
231 return $i
232 }
233
234 proc unmerged_files {files} {
235 global nr_unmerged
236
237 # find the list of unmerged files
238 set mlist {}
239 set nr_unmerged 0
240 if {[catch {
241 set fd [open "| git ls-files -u" r]
242 } err]} {
243 show_error {} . "[mc "Couldn't get list of unmerged files:"] $err"
244 exit 1
245 }
246 while {[gets $fd line] >= 0} {
247 set i [string first "\t" $line]
248 if {$i < 0} continue
249 set fname [string range $line [expr {$i+1}] end]
250 if {[lsearch -exact $mlist $fname] >= 0} continue
251 incr nr_unmerged
252 if {$files eq {} || [path_filter $files $fname]} {
253 lappend mlist $fname
254 }
255 }
256 catch {close $fd}
257 return $mlist
258 }
259
260 proc parseviewargs {n arglist} {
261 global vdatemode vmergeonly vflags vdflags vrevs vfiltered vorigargs env
262 global vinlinediff
263 global worddiff git_version
264
265 set vdatemode($n) 0
266 set vmergeonly($n) 0
267 set vinlinediff($n) 0
268 set glflags {}
269 set diffargs {}
270 set nextisval 0
271 set revargs {}
272 set origargs $arglist
273 set allknown 1
274 set filtered 0
275 set i -1
276 foreach arg $arglist {
277 incr i
278 if {$nextisval} {
279 lappend glflags $arg
280 set nextisval 0
281 continue
282 }
283 switch -glob -- $arg {
284 "-d" -
285 "--date-order" {
286 set vdatemode($n) 1
287 # remove from origargs in case we hit an unknown option
288 set origargs [lreplace $origargs $i $i]
289 incr i -1
290 }
291 "-[puabwcrRBMC]" -
292 "--no-renames" - "--full-index" - "--binary" - "--abbrev=*" -
293 "--find-copies-harder" - "-l*" - "--ext-diff" - "--no-ext-diff" -
294 "--src-prefix=*" - "--dst-prefix=*" - "--no-prefix" -
295 "-O*" - "--text" - "--full-diff" - "--ignore-space-at-eol" -
296 "--ignore-space-change" - "-U*" - "--unified=*" {
297 # These request or affect diff output, which we don't want.
298 # Some could be used to set our defaults for diff display.
299 lappend diffargs $arg
300 }
301 "--raw" - "--patch-with-raw" - "--patch-with-stat" -
302 "--name-only" - "--name-status" - "--color" -
303 "--log-size" - "--pretty=*" - "--decorate" - "--abbrev-commit" -
304 "--cc" - "-z" - "--header" - "--parents" - "--boundary" -
305 "--no-color" - "-g" - "--walk-reflogs" - "--no-walk" -
306 "--timestamp" - "relative-date" - "--date=*" - "--stdin" -
307 "--objects" - "--objects-edge" - "--reverse" {
308 # These cause our parsing of git log's output to fail, or else
309 # they're options we want to set ourselves, so ignore them.
310 }
311 "--color-words*" - "--word-diff=color" {
312 # These trigger a word diff in the console interface,
313 # so help the user by enabling our own support
314 if {[package vcompare $git_version "1.7.2"] >= 0} {
315 set worddiff [mc "Color words"]
316 }
317 }
318 "--word-diff*" {
319 if {[package vcompare $git_version "1.7.2"] >= 0} {
320 set worddiff [mc "Markup words"]
321 }
322 }
323 "--stat=*" - "--numstat" - "--shortstat" - "--summary" -
324 "--check" - "--exit-code" - "--quiet" - "--topo-order" -
325 "--full-history" - "--dense" - "--sparse" -
326 "--follow" - "--left-right" - "--encoding=*" {
327 # These are harmless, and some are even useful
328 lappend glflags $arg
329 }
330 "--diff-filter=*" - "--no-merges" - "--unpacked" -
331 "--max-count=*" - "--skip=*" - "--since=*" - "--after=*" -
332 "--until=*" - "--before=*" - "--max-age=*" - "--min-age=*" -
333 "--author=*" - "--committer=*" - "--grep=*" - "-[iE]" -
334 "--remove-empty" - "--first-parent" - "--cherry-pick" -
335 "-S*" - "-G*" - "--pickaxe-all" - "--pickaxe-regex" -
336 "--simplify-by-decoration" {
337 # These mean that we get a subset of the commits
338 set filtered 1
339 lappend glflags $arg
340 }
341 "-L*" {
342 # Line-log with 'stuck' argument (unstuck form is
343 # not supported)
344 set filtered 1
345 set vinlinediff($n) 1
346 set allknown 0
347 lappend glflags $arg
348 }
349 "-n" {
350 # This appears to be the only one that has a value as a
351 # separate word following it
352 set filtered 1
353 set nextisval 1
354 lappend glflags $arg
355 }
356 "--not" - "--all" {
357 lappend revargs $arg
358 }
359 "--merge" {
360 set vmergeonly($n) 1
361 # git rev-parse doesn't understand --merge
362 lappend revargs --gitk-symmetric-diff-marker MERGE_HEAD...HEAD
363 }
364 "--no-replace-objects" {
365 set env(GIT_NO_REPLACE_OBJECTS) "1"
366 }
367 "-*" {
368 # Other flag arguments including -<n>
369 if {[string is digit -strict [string range $arg 1 end]]} {
370 set filtered 1
371 } else {
372 # a flag argument that we don't recognize;
373 # that means we can't optimize
374 set allknown 0
375 }
376 lappend glflags $arg
377 }
378 default {
379 # Non-flag arguments specify commits or ranges of commits
380 if {[string match "*...*" $arg]} {
381 lappend revargs --gitk-symmetric-diff-marker
382 }
383 lappend revargs $arg
384 }
385 }
386 }
387 set vdflags($n) $diffargs
388 set vflags($n) $glflags
389 set vrevs($n) $revargs
390 set vfiltered($n) $filtered
391 set vorigargs($n) $origargs
392 return $allknown
393 }
394
395 proc parseviewrevs {view revs} {
396 global vposids vnegids
397
398 if {$revs eq {}} {
399 set revs HEAD
400 } elseif {[lsearch -exact $revs --all] >= 0} {
401 lappend revs HEAD
402 }
403 if {[catch {set ids [eval exec git rev-parse $revs]} err]} {
404 # we get stdout followed by stderr in $err
405 # for an unknown rev, git rev-parse echoes it and then errors out
406 set errlines [split $err "\n"]
407 set badrev {}
408 for {set l 0} {$l < [llength $errlines]} {incr l} {
409 set line [lindex $errlines $l]
410 if {!([string length $line] == 40 && [string is xdigit $line])} {
411 if {[string match "fatal:*" $line]} {
412 if {[string match "fatal: ambiguous argument*" $line]
413 && $badrev ne {}} {
414 if {[llength $badrev] == 1} {
415 set err "unknown revision $badrev"
416 } else {
417 set err "unknown revisions: [join $badrev ", "]"
418 }
419 } else {
420 set err [join [lrange $errlines $l end] "\n"]
421 }
422 break
423 }
424 lappend badrev $line
425 }
426 }
427 error_popup "[mc "Error parsing revisions:"] $err"
428 return {}
429 }
430 set ret {}
431 set pos {}
432 set neg {}
433 set sdm 0
434 foreach id [split $ids "\n"] {
435 if {$id eq "--gitk-symmetric-diff-marker"} {
436 set sdm 4
437 } elseif {[string match "^*" $id]} {
438 if {$sdm != 1} {
439 lappend ret $id
440 if {$sdm == 3} {
441 set sdm 0
442 }
443 }
444 lappend neg [string range $id 1 end]
445 } else {
446 if {$sdm != 2} {
447 lappend ret $id
448 } else {
449 lset ret end $id...[lindex $ret end]
450 }
451 lappend pos $id
452 }
453 incr sdm -1
454 }
455 set vposids($view) $pos
456 set vnegids($view) $neg
457 return $ret
458 }
459
460 # Escapes a list of filter paths to be passed to git log via stdin. Note that
461 # paths must not be quoted.
462 proc escape_filter_paths {paths} {
463 set escaped [list]
464 foreach path $paths {
465 lappend escaped [string map {\\ \\\\ "\ " "\\\ "} $path]
466 }
467 return $escaped
468 }
469
470 # Start off a git log process and arrange to read its output
471 proc start_rev_list {view} {
472 global startmsecs commitidx viewcomplete curview
473 global tclencoding
474 global viewargs viewargscmd viewfiles vfilelimit
475 global showlocalchanges
476 global viewactive viewinstances vmergeonly
477 global mainheadid viewmainheadid viewmainheadid_orig
478 global vcanopt vflags vrevs vorigargs
479 global show_notes
480
481 set startmsecs [clock clicks -milliseconds]
482 set commitidx($view) 0
483 # these are set this way for the error exits
484 set viewcomplete($view) 1
485 set viewactive($view) 0
486 varcinit $view
487
488 set args $viewargs($view)
489 if {$viewargscmd($view) ne {}} {
490 if {[catch {
491 set str [exec sh -c $viewargscmd($view)]
492 } err]} {
493 error_popup "[mc "Error executing --argscmd command:"] $err"
494 return 0
495 }
496 set args [concat $args [split $str "\n"]]
497 }
498 set vcanopt($view) [parseviewargs $view $args]
499
500 set files $viewfiles($view)
501 if {$vmergeonly($view)} {
502 set files [unmerged_files $files]
503 if {$files eq {}} {
504 global nr_unmerged
505 if {$nr_unmerged == 0} {
506 error_popup [mc "No files selected: --merge specified but\
507 no files are unmerged."]
508 } else {
509 error_popup [mc "No files selected: --merge specified but\
510 no unmerged files are within file limit."]
511 }
512 return 0
513 }
514 }
515 set vfilelimit($view) $files
516
517 if {$vcanopt($view)} {
518 set revs [parseviewrevs $view $vrevs($view)]
519 if {$revs eq {}} {
520 return 0
521 }
522 set args $vflags($view)
523 } else {
524 set revs {}
525 set args $vorigargs($view)
526 }
527
528 if {[catch {
529 set fd [open [concat | git log --no-color -z --pretty=raw $show_notes \
530 --parents --boundary $args --stdin \
531 "<<[join [concat $revs "--" \
532 [escape_filter_paths $files]] "\\n"]"] r]
533 } err]} {
534 error_popup "[mc "Error executing git log:"] $err"
535 return 0
536 }
537 set i [reg_instance $fd]
538 set viewinstances($view) [list $i]
539 set viewmainheadid($view) $mainheadid
540 set viewmainheadid_orig($view) $mainheadid
541 if {$files ne {} && $mainheadid ne {}} {
542 get_viewmainhead $view
543 }
544 if {$showlocalchanges && $viewmainheadid($view) ne {}} {
545 interestedin $viewmainheadid($view) dodiffindex
546 }
547 fconfigure $fd -blocking 0 -translation lf -eofchar {}
548 if {$tclencoding != {}} {
549 fconfigure $fd -encoding $tclencoding
550 }
551 filerun $fd [list getcommitlines $fd $i $view 0]
552 nowbusy $view [mc "Reading"]
553 set viewcomplete($view) 0
554 set viewactive($view) 1
555 return 1
556 }
557
558 proc stop_instance {inst} {
559 global commfd leftover
560
561 set fd $commfd($inst)
562 catch {
563 set pid [pid $fd]
564
565 if {$::tcl_platform(platform) eq {windows}} {
566 exec taskkill /pid $pid
567 } else {
568 exec kill $pid
569 }
570 }
571 catch {close $fd}
572 nukefile $fd
573 unset commfd($inst)
574 unset leftover($inst)
575 }
576
577 proc stop_backends {} {
578 global commfd
579
580 foreach inst [array names commfd] {
581 stop_instance $inst
582 }
583 }
584
585 proc stop_rev_list {view} {
586 global viewinstances
587
588 foreach inst $viewinstances($view) {
589 stop_instance $inst
590 }
591 set viewinstances($view) {}
592 }
593
594 proc reset_pending_select {selid} {
595 global pending_select mainheadid selectheadid
596
597 if {$selid ne {}} {
598 set pending_select $selid
599 } elseif {$selectheadid ne {}} {
600 set pending_select $selectheadid
601 } else {
602 set pending_select $mainheadid
603 }
604 }
605
606 proc getcommits {selid} {
607 global canv curview need_redisplay viewactive
608
609 initlayout
610 if {[start_rev_list $curview]} {
611 reset_pending_select $selid
612 show_status [mc "Reading commits..."]
613 set need_redisplay 1
614 } else {
615 show_status [mc "No commits selected"]
616 }
617 }
618
619 proc updatecommits {} {
620 global curview vcanopt vorigargs vfilelimit viewinstances
621 global viewactive viewcomplete tclencoding
622 global startmsecs showneartags showlocalchanges
623 global mainheadid viewmainheadid viewmainheadid_orig pending_select
624 global hasworktree
625 global varcid vposids vnegids vflags vrevs
626 global show_notes
627
628 set hasworktree [hasworktree]
629 rereadrefs
630 set view $curview
631 if {$mainheadid ne $viewmainheadid_orig($view)} {
632 if {$showlocalchanges} {
633 dohidelocalchanges
634 }
635 set viewmainheadid($view) $mainheadid
636 set viewmainheadid_orig($view) $mainheadid
637 if {$vfilelimit($view) ne {}} {
638 get_viewmainhead $view
639 }
640 }
641 if {$showlocalchanges} {
642 doshowlocalchanges
643 }
644 if {$vcanopt($view)} {
645 set oldpos $vposids($view)
646 set oldneg $vnegids($view)
647 set revs [parseviewrevs $view $vrevs($view)]
648 if {$revs eq {}} {
649 return
650 }
651 # note: getting the delta when negative refs change is hard,
652 # and could require multiple git log invocations, so in that
653 # case we ask git log for all the commits (not just the delta)
654 if {$oldneg eq $vnegids($view)} {
655 set newrevs {}
656 set npos 0
657 # take out positive refs that we asked for before or
658 # that we have already seen
659 foreach rev $revs {
660 if {[string length $rev] == 40} {
661 if {[lsearch -exact $oldpos $rev] < 0
662 && ![info exists varcid($view,$rev)]} {
663 lappend newrevs $rev
664 incr npos
665 }
666 } else {
667 lappend $newrevs $rev
668 }
669 }
670 if {$npos == 0} return
671 set revs $newrevs
672 set vposids($view) [lsort -unique [concat $oldpos $vposids($view)]]
673 }
674 set args $vflags($view)
675 foreach r $oldpos {
676 lappend revs "^$r"
677 }
678 } else {
679 set revs {}
680 set args $vorigargs($view)
681 }
682 if {[catch {
683 set fd [open [concat | git log --no-color -z --pretty=raw $show_notes \
684 --parents --boundary $args --stdin \
685 "<<[join [concat $revs "--" \
686 [escape_filter_paths \
687 $vfilelimit($view)]] "\\n"]"] r]
688 } err]} {
689 error_popup "[mc "Error executing git log:"] $err"
690 return
691 }
692 if {$viewactive($view) == 0} {
693 set startmsecs [clock clicks -milliseconds]
694 }
695 set i [reg_instance $fd]
696 lappend viewinstances($view) $i
697 fconfigure $fd -blocking 0 -translation lf -eofchar {}
698 if {$tclencoding != {}} {
699 fconfigure $fd -encoding $tclencoding
700 }
701 filerun $fd [list getcommitlines $fd $i $view 1]
702 incr viewactive($view)
703 set viewcomplete($view) 0
704 reset_pending_select {}
705 nowbusy $view [mc "Reading"]
706 if {$showneartags} {
707 getallcommits
708 }
709 }
710
711 proc reloadcommits {} {
712 global curview viewcomplete selectedline currentid thickerline
713 global showneartags treediffs commitinterest cached_commitrow
714 global targetid commitinfo
715
716 set selid {}
717 if {$selectedline ne {}} {
718 set selid $currentid
719 }
720
721 if {!$viewcomplete($curview)} {
722 stop_rev_list $curview
723 }
724 resetvarcs $curview
725 set selectedline {}
726 unset -nocomplain currentid
727 unset -nocomplain thickerline
728 unset -nocomplain treediffs
729 readrefs
730 changedrefs
731 if {$showneartags} {
732 getallcommits
733 }
734 clear_display
735 unset -nocomplain commitinfo
736 unset -nocomplain commitinterest
737 unset -nocomplain cached_commitrow
738 unset -nocomplain targetid
739 setcanvscroll
740 getcommits $selid
741 return 0
742 }
743
744 # This makes a string representation of a positive integer which
745 # sorts as a string in numerical order
746 proc strrep {n} {
747 if {$n < 16} {
748 return [format "%x" $n]
749 } elseif {$n < 256} {
750 return [format "x%.2x" $n]
751 } elseif {$n < 65536} {
752 return [format "y%.4x" $n]
753 }
754 return [format "z%.8x" $n]
755 }
756
757 # Procedures used in reordering commits from git log (without
758 # --topo-order) into the order for display.
759
760 proc varcinit {view} {
761 global varcstart vupptr vdownptr vleftptr vbackptr varctok varcrow
762 global vtokmod varcmod vrowmod varcix vlastins
763
764 set varcstart($view) {{}}
765 set vupptr($view) {0}
766 set vdownptr($view) {0}
767 set vleftptr($view) {0}
768 set vbackptr($view) {0}
769 set varctok($view) {{}}
770 set varcrow($view) {{}}
771 set vtokmod($view) {}
772 set varcmod($view) 0
773 set vrowmod($view) 0
774 set varcix($view) {{}}
775 set vlastins($view) {0}
776 }
777
778 proc resetvarcs {view} {
779 global varcid varccommits parents children vseedcount ordertok
780 global vshortids
781
782 foreach vid [array names varcid $view,*] {
783 unset varcid($vid)
784 unset children($vid)
785 unset parents($vid)
786 }
787 foreach vid [array names vshortids $view,*] {
788 unset vshortids($vid)
789 }
790 # some commits might have children but haven't been seen yet
791 foreach vid [array names children $view,*] {
792 unset children($vid)
793 }
794 foreach va [array names varccommits $view,*] {
795 unset varccommits($va)
796 }
797 foreach vd [array names vseedcount $view,*] {
798 unset vseedcount($vd)
799 }
800 unset -nocomplain ordertok
801 }
802
803 # returns a list of the commits with no children
804 proc seeds {v} {
805 global vdownptr vleftptr varcstart
806
807 set ret {}
808 set a [lindex $vdownptr($v) 0]
809 while {$a != 0} {
810 lappend ret [lindex $varcstart($v) $a]
811 set a [lindex $vleftptr($v) $a]
812 }
813 return $ret
814 }
815
816 proc newvarc {view id} {
817 global varcid varctok parents children vdatemode
818 global vupptr vdownptr vleftptr vbackptr varcrow varcix varcstart
819 global commitdata commitinfo vseedcount varccommits vlastins
820
821 set a [llength $varctok($view)]
822 set vid $view,$id
823 if {[llength $children($vid)] == 0 || $vdatemode($view)} {
824 if {![info exists commitinfo($id)]} {
825 parsecommit $id $commitdata($id) 1
826 }
827 set cdate [lindex [lindex $commitinfo($id) 4] 0]
828 if {![string is integer -strict $cdate]} {
829 set cdate 0
830 }
831 if {![info exists vseedcount($view,$cdate)]} {
832 set vseedcount($view,$cdate) -1
833 }
834 set c [incr vseedcount($view,$cdate)]
835 set cdate [expr {$cdate ^ 0xffffffff}]
836 set tok "s[strrep $cdate][strrep $c]"
837 } else {
838 set tok {}
839 }
840 set ka 0
841 if {[llength $children($vid)] > 0} {
842 set kid [lindex $children($vid) end]
843 set k $varcid($view,$kid)
844 if {[string compare [lindex $varctok($view) $k] $tok] > 0} {
845 set ki $kid
846 set ka $k
847 set tok [lindex $varctok($view) $k]
848 }
849 }
850 if {$ka != 0} {
851 set i [lsearch -exact $parents($view,$ki) $id]
852 set j [expr {[llength $parents($view,$ki)] - 1 - $i}]
853 append tok [strrep $j]
854 }
855 set c [lindex $vlastins($view) $ka]
856 if {$c == 0 || [string compare $tok [lindex $varctok($view) $c]] < 0} {
857 set c $ka
858 set b [lindex $vdownptr($view) $ka]
859 } else {
860 set b [lindex $vleftptr($view) $c]
861 }
862 while {$b != 0 && [string compare $tok [lindex $varctok($view) $b]] >= 0} {
863 set c $b
864 set b [lindex $vleftptr($view) $c]
865 }
866 if {$c == $ka} {
867 lset vdownptr($view) $ka $a
868 lappend vbackptr($view) 0
869 } else {
870 lset vleftptr($view) $c $a
871 lappend vbackptr($view) $c
872 }
873 lset vlastins($view) $ka $a
874 lappend vupptr($view) $ka
875 lappend vleftptr($view) $b
876 if {$b != 0} {
877 lset vbackptr($view) $b $a
878 }
879 lappend varctok($view) $tok
880 lappend varcstart($view) $id
881 lappend vdownptr($view) 0
882 lappend varcrow($view) {}
883 lappend varcix($view) {}
884 set varccommits($view,$a) {}
885 lappend vlastins($view) 0
886 return $a
887 }
888
889 proc splitvarc {p v} {
890 global varcid varcstart varccommits varctok vtokmod
891 global vupptr vdownptr vleftptr vbackptr varcix varcrow vlastins
892
893 set oa $varcid($v,$p)
894 set otok [lindex $varctok($v) $oa]
895 set ac $varccommits($v,$oa)
896 set i [lsearch -exact $varccommits($v,$oa) $p]
897 if {$i <= 0} return
898 set na [llength $varctok($v)]
899 # "%" sorts before "0"...
900 set tok "$otok%[strrep $i]"
901 lappend varctok($v) $tok
902 lappend varcrow($v) {}
903 lappend varcix($v) {}
904 set varccommits($v,$oa) [lrange $ac 0 [expr {$i - 1}]]
905 set varccommits($v,$na) [lrange $ac $i end]
906 lappend varcstart($v) $p
907 foreach id $varccommits($v,$na) {
908 set varcid($v,$id) $na
909 }
910 lappend vdownptr($v) [lindex $vdownptr($v) $oa]
911 lappend vlastins($v) [lindex $vlastins($v) $oa]
912 lset vdownptr($v) $oa $na
913 lset vlastins($v) $oa 0
914 lappend vupptr($v) $oa
915 lappend vleftptr($v) 0
916 lappend vbackptr($v) 0
917 for {set b [lindex $vdownptr($v) $na]} {$b != 0} {set b [lindex $vleftptr($v) $b]} {
918 lset vupptr($v) $b $na
919 }
920 if {[string compare $otok $vtokmod($v)] <= 0} {
921 modify_arc $v $oa
922 }
923 }
924
925 proc renumbervarc {a v} {
926 global parents children varctok varcstart varccommits
927 global vupptr vdownptr vleftptr vbackptr vlastins varcid vtokmod vdatemode
928
929 set t1 [clock clicks -milliseconds]
930 set todo {}
931 set isrelated($a) 1
932 set kidchanged($a) 1
933 set ntot 0
934 while {$a != 0} {
935 if {[info exists isrelated($a)]} {
936 lappend todo $a
937 set id [lindex $varccommits($v,$a) end]
938 foreach p $parents($v,$id) {
939 if {[info exists varcid($v,$p)]} {
940 set isrelated($varcid($v,$p)) 1
941 }
942 }
943 }
944 incr ntot
945 set b [lindex $vdownptr($v) $a]
946 if {$b == 0} {
947 while {$a != 0} {
948 set b [lindex $vleftptr($v) $a]
949 if {$b != 0} break
950 set a [lindex $vupptr($v) $a]
951 }
952 }
953 set a $b
954 }
955 foreach a $todo {
956 if {![info exists kidchanged($a)]} continue
957 set id [lindex $varcstart($v) $a]
958 if {[llength $children($v,$id)] > 1} {
959 set children($v,$id) [lsort -command [list vtokcmp $v] \
960 $children($v,$id)]
961 }
962 set oldtok [lindex $varctok($v) $a]
963 if {!$vdatemode($v)} {
964 set tok {}
965 } else {
966 set tok $oldtok
967 }
968 set ka 0
969 set kid [last_real_child $v,$id]
970 if {$kid ne {}} {
971 set k $varcid($v,$kid)
972 if {[string compare [lindex $varctok($v) $k] $tok] > 0} {
973 set ki $kid
974 set ka $k
975 set tok [lindex $varctok($v) $k]
976 }
977 }
978 if {$ka != 0} {
979 set i [lsearch -exact $parents($v,$ki) $id]
980 set j [expr {[llength $parents($v,$ki)] - 1 - $i}]
981 append tok [strrep $j]
982 }
983 if {$tok eq $oldtok} {
984 continue
985 }
986 set id [lindex $varccommits($v,$a) end]
987 foreach p $parents($v,$id) {
988 if {[info exists varcid($v,$p)]} {
989 set kidchanged($varcid($v,$p)) 1
990 } else {
991 set sortkids($p) 1
992 }
993 }
994 lset varctok($v) $a $tok
995 set b [lindex $vupptr($v) $a]
996 if {$b != $ka} {
997 if {[string compare [lindex $varctok($v) $ka] $vtokmod($v)] < 0} {
998 modify_arc $v $ka
999 }
1000 if {[string compare [lindex $varctok($v) $b] $vtokmod($v)] < 0} {
1001 modify_arc $v $b
1002 }
1003 set c [lindex $vbackptr($v) $a]
1004 set d [lindex $vleftptr($v) $a]
1005 if {$c == 0} {
1006 lset vdownptr($v) $b $d
1007 } else {
1008 lset vleftptr($v) $c $d
1009 }
1010 if {$d != 0} {
1011 lset vbackptr($v) $d $c
1012 }
1013 if {[lindex $vlastins($v) $b] == $a} {
1014 lset vlastins($v) $b $c
1015 }
1016 lset vupptr($v) $a $ka
1017 set c [lindex $vlastins($v) $ka]
1018 if {$c == 0 || \
1019 [string compare $tok [lindex $varctok($v) $c]] < 0} {
1020 set c $ka
1021 set b [lindex $vdownptr($v) $ka]
1022 } else {
1023 set b [lindex $vleftptr($v) $c]
1024 }
1025 while {$b != 0 && \
1026 [string compare $tok [lindex $varctok($v) $b]] >= 0} {
1027 set c $b
1028 set b [lindex $vleftptr($v) $c]
1029 }
1030 if {$c == $ka} {
1031 lset vdownptr($v) $ka $a
1032 lset vbackptr($v) $a 0
1033 } else {
1034 lset vleftptr($v) $c $a
1035 lset vbackptr($v) $a $c
1036 }
1037 lset vleftptr($v) $a $b
1038 if {$b != 0} {
1039 lset vbackptr($v) $b $a
1040 }
1041 lset vlastins($v) $ka $a
1042 }
1043 }
1044 foreach id [array names sortkids] {
1045 if {[llength $children($v,$id)] > 1} {
1046 set children($v,$id) [lsort -command [list vtokcmp $v] \
1047 $children($v,$id)]
1048 }
1049 }
1050 set t2 [clock clicks -milliseconds]
1051 #puts "renumbervarc did [llength $todo] of $ntot arcs in [expr {$t2-$t1}]ms"
1052 }
1053
1054 # Fix up the graph after we have found out that in view $v,
1055 # $p (a commit that we have already seen) is actually the parent
1056 # of the last commit in arc $a.
1057 proc fix_reversal {p a v} {
1058 global varcid varcstart varctok vupptr
1059
1060 set pa $varcid($v,$p)
1061 if {$p ne [lindex $varcstart($v) $pa]} {
1062 splitvarc $p $v
1063 set pa $varcid($v,$p)
1064 }
1065 # seeds always need to be renumbered
1066 if {[lindex $vupptr($v) $pa] == 0 ||
1067 [string compare [lindex $varctok($v) $a] \
1068 [lindex $varctok($v) $pa]] > 0} {
1069 renumbervarc $pa $v
1070 }
1071 }
1072
1073 proc insertrow {id p v} {
1074 global cmitlisted children parents varcid varctok vtokmod
1075 global varccommits ordertok commitidx numcommits curview
1076 global targetid targetrow vshortids
1077
1078 readcommit $id
1079 set vid $v,$id
1080 set cmitlisted($vid) 1
1081 set children($vid) {}
1082 set parents($vid) [list $p]
1083 set a [newvarc $v $id]
1084 set varcid($vid) $a
1085 lappend vshortids($v,[string range $id 0 3]) $id
1086 if {[string compare [lindex $varctok($v) $a] $vtokmod($v)] < 0} {
1087 modify_arc $v $a
1088 }
1089 lappend varccommits($v,$a) $id
1090 set vp $v,$p
1091 if {[llength [lappend children($vp) $id]] > 1} {
1092 set children($vp) [lsort -command [list vtokcmp $v] $children($vp)]
1093 unset -nocomplain ordertok
1094 }
1095 fix_reversal $p $a $v
1096 incr commitidx($v)
1097 if {$v == $curview} {
1098 set numcommits $commitidx($v)
1099 setcanvscroll
1100 if {[info exists targetid]} {
1101 if {![comes_before $targetid $p]} {
1102 incr targetrow
1103 }
1104 }
1105 }
1106 }
1107
1108 proc insertfakerow {id p} {
1109 global varcid varccommits parents children cmitlisted
1110 global commitidx varctok vtokmod targetid targetrow curview numcommits
1111
1112 set v $curview
1113 set a $varcid($v,$p)
1114 set i [lsearch -exact $varccommits($v,$a) $p]
1115 if {$i < 0} {
1116 puts "oops: insertfakerow can't find [shortids $p] on arc $a"
1117 return
1118 }
1119 set children($v,$id) {}
1120 set parents($v,$id) [list $p]
1121 set varcid($v,$id) $a
1122 lappend children($v,$p) $id
1123 set cmitlisted($v,$id) 1
1124 set numcommits [incr commitidx($v)]
1125 # note we deliberately don't update varcstart($v) even if $i == 0
1126 set varccommits($v,$a) [linsert $varccommits($v,$a) $i $id]
1127 modify_arc $v $a $i
1128 if {[info exists targetid]} {
1129 if {![comes_before $targetid $p]} {
1130 incr targetrow
1131 }
1132 }
1133 setcanvscroll
1134 drawvisible
1135 }
1136
1137 proc removefakerow {id} {
1138 global varcid varccommits parents children commitidx
1139 global varctok vtokmod cmitlisted currentid selectedline
1140 global targetid curview numcommits
1141
1142 set v $curview
1143 if {[llength $parents($v,$id)] != 1} {
1144 puts "oops: removefakerow [shortids $id] has [llength $parents($v,$id)] parents"
1145 return
1146 }
1147 set p [lindex $parents($v,$id) 0]
1148 set a $varcid($v,$id)
1149 set i [lsearch -exact $varccommits($v,$a) $id]
1150 if {$i < 0} {
1151 puts "oops: removefakerow can't find [shortids $id] on arc $a"
1152 return
1153 }
1154 unset varcid($v,$id)
1155 set varccommits($v,$a) [lreplace $varccommits($v,$a) $i $i]
1156 unset parents($v,$id)
1157 unset children($v,$id)
1158 unset cmitlisted($v,$id)
1159 set numcommits [incr commitidx($v) -1]
1160 set j [lsearch -exact $children($v,$p) $id]
1161 if {$j >= 0} {
1162 set children($v,$p) [lreplace $children($v,$p) $j $j]
1163 }
1164 modify_arc $v $a $i
1165 if {[info exist currentid] && $id eq $currentid} {
1166 unset currentid
1167 set selectedline {}
1168 }
1169 if {[info exists targetid] && $targetid eq $id} {
1170 set targetid $p
1171 }
1172 setcanvscroll
1173 drawvisible
1174 }
1175
1176 proc real_children {vp} {
1177 global children nullid nullid2
1178
1179 set kids {}
1180 foreach id $children($vp) {
1181 if {$id ne $nullid && $id ne $nullid2} {
1182 lappend kids $id
1183 }
1184 }
1185 return $kids
1186 }
1187
1188 proc first_real_child {vp} {
1189 global children nullid nullid2
1190
1191 foreach id $children($vp) {
1192 if {$id ne $nullid && $id ne $nullid2} {
1193 return $id
1194 }
1195 }
1196 return {}
1197 }
1198
1199 proc last_real_child {vp} {
1200 global children nullid nullid2
1201
1202 set kids $children($vp)
1203 for {set i [llength $kids]} {[incr i -1] >= 0} {} {
1204 set id [lindex $kids $i]
1205 if {$id ne $nullid && $id ne $nullid2} {
1206 return $id
1207 }
1208 }
1209 return {}
1210 }
1211
1212 proc vtokcmp {v a b} {
1213 global varctok varcid
1214
1215 return [string compare [lindex $varctok($v) $varcid($v,$a)] \
1216 [lindex $varctok($v) $varcid($v,$b)]]
1217 }
1218
1219 # This assumes that if lim is not given, the caller has checked that
1220 # arc a's token is less than $vtokmod($v)
1221 proc modify_arc {v a {lim {}}} {
1222 global varctok vtokmod varcmod varcrow vupptr curview vrowmod varccommits
1223
1224 if {$lim ne {}} {
1225 set c [string compare [lindex $varctok($v) $a] $vtokmod($v)]
1226 if {$c > 0} return
1227 if {$c == 0} {
1228 set r [lindex $varcrow($v) $a]
1229 if {$r ne {} && $vrowmod($v) <= $r + $lim} return
1230 }
1231 }
1232 set vtokmod($v) [lindex $varctok($v) $a]
1233 set varcmod($v) $a
1234 if {$v == $curview} {
1235 while {$a != 0 && [lindex $varcrow($v) $a] eq {}} {
1236 set a [lindex $vupptr($v) $a]
1237 set lim {}
1238 }
1239 set r 0
1240 if {$a != 0} {
1241 if {$lim eq {}} {
1242 set lim [llength $varccommits($v,$a)]
1243 }
1244 set r [expr {[lindex $varcrow($v) $a] + $lim}]
1245 }
1246 set vrowmod($v) $r
1247 undolayout $r
1248 }
1249 }
1250
1251 proc update_arcrows {v} {
1252 global vtokmod varcmod vrowmod varcrow commitidx currentid selectedline
1253 global varcid vrownum varcorder varcix varccommits
1254 global vupptr vdownptr vleftptr varctok
1255 global displayorder parentlist curview cached_commitrow
1256
1257 if {$vrowmod($v) == $commitidx($v)} return
1258 if {$v == $curview} {
1259 if {[llength $displayorder] > $vrowmod($v)} {
1260 set displayorder [lrange $displayorder 0 [expr {$vrowmod($v) - 1}]]
1261 set parentlist [lrange $parentlist 0 [expr {$vrowmod($v) - 1}]]
1262 }
1263 unset -nocomplain cached_commitrow
1264 }
1265 set narctot [expr {[llength $varctok($v)] - 1}]
1266 set a $varcmod($v)
1267 while {$a != 0 && [lindex $varcix($v) $a] eq {}} {
1268 # go up the tree until we find something that has a row number,
1269 # or we get to a seed
1270 set a [lindex $vupptr($v) $a]
1271 }
1272 if {$a == 0} {
1273 set a [lindex $vdownptr($v) 0]
1274 if {$a == 0} return
1275 set vrownum($v) {0}
1276 set varcorder($v) [list $a]
1277 lset varcix($v) $a 0
1278 lset varcrow($v) $a 0
1279 set arcn 0
1280 set row 0
1281 } else {
1282 set arcn [lindex $varcix($v) $a]
1283 if {[llength $vrownum($v)] > $arcn + 1} {
1284 set vrownum($v) [lrange $vrownum($v) 0 $arcn]
1285 set varcorder($v) [lrange $varcorder($v) 0 $arcn]
1286 }
1287 set row [lindex $varcrow($v) $a]
1288 }
1289 while {1} {
1290 set p $a
1291 incr row [llength $varccommits($v,$a)]
1292 # go down if possible
1293 set b [lindex $vdownptr($v) $a]
1294 if {$b == 0} {
1295 # if not, go left, or go up until we can go left
1296 while {$a != 0} {
1297 set b [lindex $vleftptr($v) $a]
1298 if {$b != 0} break
1299 set a [lindex $vupptr($v) $a]
1300 }
1301 if {$a == 0} break
1302 }
1303 set a $b
1304 incr arcn
1305 lappend vrownum($v) $row
1306 lappend varcorder($v) $a
1307 lset varcix($v) $a $arcn
1308 lset varcrow($v) $a $row
1309 }
1310 set vtokmod($v) [lindex $varctok($v) $p]
1311 set varcmod($v) $p
1312 set vrowmod($v) $row
1313 if {[info exists currentid]} {
1314 set selectedline [rowofcommit $currentid]
1315 }
1316 }
1317
1318 # Test whether view $v contains commit $id
1319 proc commitinview {id v} {
1320 global varcid
1321
1322 return [info exists varcid($v,$id)]
1323 }
1324
1325 # Return the row number for commit $id in the current view
1326 proc rowofcommit {id} {
1327 global varcid varccommits varcrow curview cached_commitrow
1328 global varctok vtokmod
1329
1330 set v $curview
1331 if {![info exists varcid($v,$id)]} {
1332 puts "oops rowofcommit no arc for [shortids $id]"
1333 return {}
1334 }
1335 set a $varcid($v,$id)
1336 if {[string compare [lindex $varctok($v) $a] $vtokmod($v)] >= 0} {
1337 update_arcrows $v
1338 }
1339 if {[info exists cached_commitrow($id)]} {
1340 return $cached_commitrow($id)
1341 }
1342 set i [lsearch -exact $varccommits($v,$a) $id]
1343 if {$i < 0} {
1344 puts "oops didn't find commit [shortids $id] in arc $a"
1345 return {}
1346 }
1347 incr i [lindex $varcrow($v) $a]
1348 set cached_commitrow($id) $i
1349 return $i
1350 }
1351
1352 # Returns 1 if a is on an earlier row than b, otherwise 0
1353 proc comes_before {a b} {
1354 global varcid varctok curview
1355
1356 set v $curview
1357 if {$a eq $b || ![info exists varcid($v,$a)] || \
1358 ![info exists varcid($v,$b)]} {
1359 return 0
1360 }
1361 if {$varcid($v,$a) != $varcid($v,$b)} {
1362 return [expr {[string compare [lindex $varctok($v) $varcid($v,$a)] \
1363 [lindex $varctok($v) $varcid($v,$b)]] < 0}]
1364 }
1365 return [expr {[rowofcommit $a] < [rowofcommit $b]}]
1366 }
1367
1368 proc bsearch {l elt} {
1369 if {[llength $l] == 0 || $elt <= [lindex $l 0]} {
1370 return 0
1371 }
1372 set lo 0
1373 set hi [llength $l]
1374 while {$hi - $lo > 1} {
1375 set mid [expr {int(($lo + $hi) / 2)}]
1376 set t [lindex $l $mid]
1377 if {$elt < $t} {
1378 set hi $mid
1379 } elseif {$elt > $t} {
1380 set lo $mid
1381 } else {
1382 return $mid
1383 }
1384 }
1385 return $lo
1386 }
1387
1388 # Make sure rows $start..$end-1 are valid in displayorder and parentlist
1389 proc make_disporder {start end} {
1390 global vrownum curview commitidx displayorder parentlist
1391 global varccommits varcorder parents vrowmod varcrow
1392 global d_valid_start d_valid_end
1393
1394 if {$end > $vrowmod($curview)} {
1395 update_arcrows $curview
1396 }
1397 set ai [bsearch $vrownum($curview) $start]
1398 set start [lindex $vrownum($curview) $ai]
1399 set narc [llength $vrownum($curview)]
1400 for {set r $start} {$ai < $narc && $r < $end} {incr ai} {
1401 set a [lindex $varcorder($curview) $ai]
1402 set l [llength $displayorder]
1403 set al [llength $varccommits($curview,$a)]
1404 if {$l < $r + $al} {
1405 if {$l < $r} {
1406 set pad [ntimes [expr {$r - $l}] {}]
1407 set displayorder [concat $displayorder $pad]
1408 set parentlist [concat $parentlist $pad]
1409 } elseif {$l > $r} {
1410 set displayorder [lrange $displayorder 0 [expr {$r - 1}]]
1411 set parentlist [lrange $parentlist 0 [expr {$r - 1}]]
1412 }
1413 foreach id $varccommits($curview,$a) {
1414 lappend displayorder $id
1415 lappend parentlist $parents($curview,$id)
1416 }
1417 } elseif {[lindex $displayorder [expr {$r + $al - 1}]] eq {}} {
1418 set i $r
1419 foreach id $varccommits($curview,$a) {
1420 lset displayorder $i $id
1421 lset parentlist $i $parents($curview,$id)
1422 incr i
1423 }
1424 }
1425 incr r $al
1426 }
1427 }
1428
1429 proc commitonrow {row} {
1430 global displayorder
1431
1432 set id [lindex $displayorder $row]
1433 if {$id eq {}} {
1434 make_disporder $row [expr {$row + 1}]
1435 set id [lindex $displayorder $row]
1436 }
1437 return $id
1438 }
1439
1440 proc closevarcs {v} {
1441 global varctok varccommits varcid parents children
1442 global cmitlisted commitidx vtokmod curview numcommits
1443
1444 set missing_parents 0
1445 set scripts {}
1446 set narcs [llength $varctok($v)]
1447 for {set a 1} {$a < $narcs} {incr a} {
1448 set id [lindex $varccommits($v,$a) end]
1449 foreach p $parents($v,$id) {
1450 if {[info exists varcid($v,$p)]} continue
1451 # add p as a new commit
1452 incr missing_parents
1453 set cmitlisted($v,$p) 0
1454 set parents($v,$p) {}
1455 if {[llength $children($v,$p)] == 1 &&
1456 [llength $parents($v,$id)] == 1} {
1457 set b $a
1458 } else {
1459 set b [newvarc $v $p]
1460 }
1461 set varcid($v,$p) $b
1462 if {[string compare [lindex $varctok($v) $b] $vtokmod($v)] < 0} {
1463 modify_arc $v $b
1464 }
1465 lappend varccommits($v,$b) $p
1466 incr commitidx($v)
1467 if {$v == $curview} {
1468 set numcommits $commitidx($v)
1469 }
1470 set scripts [check_interest $p $scripts]
1471 }
1472 }
1473 if {$missing_parents > 0} {
1474 foreach s $scripts {
1475 eval $s
1476 }
1477 }
1478 }
1479
1480 # Use $rwid as a substitute for $id, i.e. reparent $id's children to $rwid
1481 # Assumes we already have an arc for $rwid.
1482 proc rewrite_commit {v id rwid} {
1483 global children parents varcid varctok vtokmod varccommits
1484
1485 foreach ch $children($v,$id) {
1486 # make $rwid be $ch's parent in place of $id
1487 set i [lsearch -exact $parents($v,$ch) $id]
1488 if {$i < 0} {
1489 puts "oops rewrite_commit didn't find $id in parent list for $ch"
1490 }
1491 set parents($v,$ch) [lreplace $parents($v,$ch) $i $i $rwid]
1492 # add $ch to $rwid's children and sort the list if necessary
1493 if {[llength [lappend children($v,$rwid) $ch]] > 1} {
1494 set children($v,$rwid) [lsort -command [list vtokcmp $v] \
1495 $children($v,$rwid)]
1496 }
1497 # fix the graph after joining $id to $rwid
1498 set a $varcid($v,$ch)
1499 fix_reversal $rwid $a $v
1500 # parentlist is wrong for the last element of arc $a
1501 # even if displayorder is right, hence the 3rd arg here
1502 modify_arc $v $a [expr {[llength $varccommits($v,$a)] - 1}]
1503 }
1504 }
1505
1506 # Mechanism for registering a command to be executed when we come
1507 # across a particular commit. To handle the case when only the
1508 # prefix of the commit is known, the commitinterest array is now
1509 # indexed by the first 4 characters of the ID. Each element is a
1510 # list of id, cmd pairs.
1511 proc interestedin {id cmd} {
1512 global commitinterest
1513
1514 lappend commitinterest([string range $id 0 3]) $id $cmd
1515 }
1516
1517 proc check_interest {id scripts} {
1518 global commitinterest
1519
1520 set prefix [string range $id 0 3]
1521 if {[info exists commitinterest($prefix)]} {
1522 set newlist {}
1523 foreach {i script} $commitinterest($prefix) {
1524 if {[string match "$i*" $id]} {
1525 lappend scripts [string map [list "%I" $id "%P" $i] $script]
1526 } else {
1527 lappend newlist $i $script
1528 }
1529 }
1530 if {$newlist ne {}} {
1531 set commitinterest($prefix) $newlist
1532 } else {
1533 unset commitinterest($prefix)
1534 }
1535 }
1536 return $scripts
1537 }
1538
1539 proc getcommitlines {fd inst view updating} {
1540 global cmitlisted leftover
1541 global commitidx commitdata vdatemode
1542 global parents children curview hlview
1543 global idpending ordertok
1544 global varccommits varcid varctok vtokmod vfilelimit vshortids
1545
1546 set stuff [read $fd 500000]
1547 # git log doesn't terminate the last commit with a null...
1548 if {$stuff == {} && $leftover($inst) ne {} && [eof $fd]} {
1549 set stuff "\0"
1550 }
1551 if {$stuff == {}} {
1552 if {![eof $fd]} {
1553 return 1
1554 }
1555 global commfd viewcomplete viewactive viewname
1556 global viewinstances
1557 unset commfd($inst)
1558 set i [lsearch -exact $viewinstances($view) $inst]
1559 if {$i >= 0} {
1560 set viewinstances($view) [lreplace $viewinstances($view) $i $i]
1561 }
1562 # set it blocking so we wait for the process to terminate
1563 fconfigure $fd -blocking 1
1564 if {[catch {close $fd} err]} {
1565 set fv {}
1566 if {$view != $curview} {
1567 set fv " for the \"$viewname($view)\" view"
1568 }
1569 if {[string range $err 0 4] == "usage"} {
1570 set err "Gitk: error reading commits$fv:\
1571 bad arguments to git log."
1572 if {$viewname($view) eq [mc "Command line"]} {
1573 append err \
1574 " (Note: arguments to gitk are passed to git log\
1575 to allow selection of commits to be displayed.)"
1576 }
1577 } else {
1578 set err "Error reading commits$fv: $err"
1579 }
1580 error_popup $err
1581 }
1582 if {[incr viewactive($view) -1] <= 0} {
1583 set viewcomplete($view) 1
1584 # Check if we have seen any ids listed as parents that haven't
1585 # appeared in the list
1586 closevarcs $view
1587 notbusy $view
1588 }
1589 if {$view == $curview} {
1590 run chewcommits
1591 }
1592 return 0
1593 }
1594 set start 0
1595 set gotsome 0
1596 set scripts {}
1597 while 1 {
1598 set i [string first "\0" $stuff $start]
1599 if {$i < 0} {
1600 append leftover($inst) [string range $stuff $start end]
1601 break
1602 }
1603 if {$start == 0} {
1604 set cmit $leftover($inst)
1605 append cmit [string range $stuff 0 [expr {$i - 1}]]
1606 set leftover($inst) {}
1607 } else {
1608 set cmit [string range $stuff $start [expr {$i - 1}]]
1609 }
1610 set start [expr {$i + 1}]
1611 set j [string first "\n" $cmit]
1612 set ok 0
1613 set listed 1
1614 if {$j >= 0 && [string match "commit *" $cmit]} {
1615 set ids [string range $cmit 7 [expr {$j - 1}]]
1616 if {[string match {[-^<>]*} $ids]} {
1617 switch -- [string index $ids 0] {
1618 "-" {set listed 0}
1619 "^" {set listed 2}
1620 "<" {set listed 3}
1621 ">" {set listed 4}
1622 }
1623 set ids [string range $ids 1 end]
1624 }
1625 set ok 1
1626 foreach id $ids {
1627 if {[string length $id] != 40} {
1628 set ok 0
1629 break
1630 }
1631 }
1632 }
1633 if {!$ok} {
1634 set shortcmit $cmit
1635 if {[string length $shortcmit] > 80} {
1636 set shortcmit "[string range $shortcmit 0 80]..."
1637 }
1638 error_popup "[mc "Can't parse git log output:"] {$shortcmit}"
1639 exit 1
1640 }
1641 set id [lindex $ids 0]
1642 set vid $view,$id
1643
1644 lappend vshortids($view,[string range $id 0 3]) $id
1645
1646 if {!$listed && $updating && ![info exists varcid($vid)] &&
1647 $vfilelimit($view) ne {}} {
1648 # git log doesn't rewrite parents for unlisted commits
1649 # when doing path limiting, so work around that here
1650 # by working out the rewritten parent with git rev-list
1651 # and if we already know about it, using the rewritten
1652 # parent as a substitute parent for $id's children.
1653 if {![catch {
1654 set rwid [exec git rev-list --first-parent --max-count=1 \
1655 $id -- $vfilelimit($view)]
1656 }]} {
1657 if {$rwid ne {} && [info exists varcid($view,$rwid)]} {
1658 # use $rwid in place of $id
1659 rewrite_commit $view $id $rwid
1660 continue
1661 }
1662 }
1663 }
1664
1665 set a 0
1666 if {[info exists varcid($vid)]} {
1667 if {$cmitlisted($vid) || !$listed} continue
1668 set a $varcid($vid)
1669 }
1670 if {$listed} {
1671 set olds [lrange $ids 1 end]
1672 } else {
1673 set olds {}
1674 }
1675 set commitdata($id) [string range $cmit [expr {$j + 1}] end]
1676 set cmitlisted($vid) $listed
1677 set parents($vid) $olds
1678 if {![info exists children($vid)]} {
1679 set children($vid) {}
1680 } elseif {$a == 0 && [llength $children($vid)] == 1} {
1681 set k [lindex $children($vid) 0]
1682 if {[llength $parents($view,$k)] == 1 &&
1683 (!$vdatemode($view) ||
1684 $varcid($view,$k) == [llength $varctok($view)] - 1)} {
1685 set a $varcid($view,$k)
1686 }
1687 }
1688 if {$a == 0} {
1689 # new arc
1690 set a [newvarc $view $id]
1691 }
1692 if {[string compare [lindex $varctok($view) $a] $vtokmod($view)] < 0} {
1693 modify_arc $view $a
1694 }
1695 if {![info exists varcid($vid)]} {
1696 set varcid($vid) $a
1697 lappend varccommits($view,$a) $id
1698 incr commitidx($view)
1699 }
1700
1701 set i 0
1702 foreach p $olds {
1703 if {$i == 0 || [lsearch -exact $olds $p] >= $i} {
1704 set vp $view,$p
1705 if {[llength [lappend children($vp) $id]] > 1 &&
1706 [vtokcmp $view [lindex $children($vp) end-1] $id] > 0} {
1707 set children($vp) [lsort -command [list vtokcmp $view] \
1708 $children($vp)]
1709 unset -nocomplain ordertok
1710 }
1711 if {[info exists varcid($view,$p)]} {
1712 fix_reversal $p $a $view
1713 }
1714 }
1715 incr i
1716 }
1717
1718 set scripts [check_interest $id $scripts]
1719 set gotsome 1
1720 }
1721 if {$gotsome} {
1722 global numcommits hlview
1723
1724 if {$view == $curview} {
1725 set numcommits $commitidx($view)
1726 run chewcommits
1727 }
1728 if {[info exists hlview] && $view == $hlview} {
1729 # we never actually get here...
1730 run vhighlightmore
1731 }
1732 foreach s $scripts {
1733 eval $s
1734 }
1735 }
1736 return 2
1737 }
1738
1739 proc chewcommits {} {
1740 global curview hlview viewcomplete
1741 global pending_select
1742
1743 layoutmore
1744 if {$viewcomplete($curview)} {
1745 global commitidx varctok
1746 global numcommits startmsecs
1747
1748 if {[info exists pending_select]} {
1749 update
1750 reset_pending_select {}
1751
1752 if {[commitinview $pending_select $curview]} {
1753 selectline [rowofcommit $pending_select] 1
1754 } else {
1755 set row [first_real_row]
1756 selectline $row 1
1757 }
1758 }
1759 if {$commitidx($curview) > 0} {
1760 #set ms [expr {[clock clicks -milliseconds] - $startmsecs}]
1761 #puts "overall $ms ms for $numcommits commits"
1762 #puts "[llength $varctok($view)] arcs, $commitidx($view) commits"
1763 } else {
1764 show_status [mc "No commits selected"]
1765 }
1766 notbusy layout
1767 }
1768 return 0
1769 }
1770
1771 proc do_readcommit {id} {
1772 global tclencoding
1773
1774 # Invoke git-log to handle automatic encoding conversion
1775 set fd [open [concat | git log --no-color --pretty=raw -1 $id] r]
1776 # Read the results using i18n.logoutputencoding
1777 fconfigure $fd -translation lf -eofchar {}
1778 if {$tclencoding != {}} {
1779 fconfigure $fd -encoding $tclencoding
1780 }
1781 set contents [read $fd]
1782 close $fd
1783 # Remove the heading line
1784 regsub {^commit [0-9a-f]+\n} $contents {} contents
1785
1786 return $contents
1787 }
1788
1789 proc readcommit {id} {
1790 if {[catch {set contents [do_readcommit $id]}]} return
1791 parsecommit $id $contents 1
1792 }
1793
1794 proc parsecommit {id contents listed} {
1795 global commitinfo
1796
1797 set inhdr 1
1798 set comment {}
1799 set headline {}
1800 set auname {}
1801 set audate {}
1802 set comname {}
1803 set comdate {}
1804 set hdrend [string first "\n\n" $contents]
1805 if {$hdrend < 0} {
1806 # should never happen...
1807 set hdrend [string length $contents]
1808 }
1809 set header [string range $contents 0 [expr {$hdrend - 1}]]
1810 set comment [string range $contents [expr {$hdrend + 2}] end]
1811 foreach line [split $header "\n"] {
1812 set line [split $line " "]
1813 set tag [lindex $line 0]
1814 if {$tag == "author"} {
1815 set audate [lrange $line end-1 end]
1816 set auname [join [lrange $line 1 end-2] " "]
1817 } elseif {$tag == "committer"} {
1818 set comdate [lrange $line end-1 end]
1819 set comname [join [lrange $line 1 end-2] " "]
1820 }
1821 }
1822 set headline {}
1823 # take the first non-blank line of the comment as the headline
1824 set headline [string trimleft $comment]
1825 set i [string first "\n" $headline]
1826 if {$i >= 0} {
1827 set headline [string range $headline 0 $i]
1828 }
1829 set headline [string trimright $headline]
1830 set i [string first "\r" $headline]
1831 if {$i >= 0} {
1832 set headline [string trimright [string range $headline 0 $i]]
1833 }
1834 if {!$listed} {
1835 # git log indents the comment by 4 spaces;
1836 # if we got this via git cat-file, add the indentation
1837 set newcomment {}
1838 foreach line [split $comment "\n"] {
1839 append newcomment " "
1840 append newcomment $line
1841 append newcomment "\n"
1842 }
1843 set comment $newcomment
1844 }
1845 set hasnote [string first "\nNotes:\n" $contents]
1846 set diff ""
1847 # If there is diff output shown in the git-log stream, split it
1848 # out. But get rid of the empty line that always precedes the
1849 # diff.
1850 set i [string first "\n\ndiff" $comment]
1851 if {$i >= 0} {
1852 set diff [string range $comment $i+1 end]
1853 set comment [string range $comment 0 $i-1]
1854 }
1855 set commitinfo($id) [list $headline $auname $audate \
1856 $comname $comdate $comment $hasnote $diff]
1857 }
1858
1859 proc getcommit {id} {
1860 global commitdata commitinfo
1861
1862 if {[info exists commitdata($id)]} {
1863 parsecommit $id $commitdata($id) 1
1864 } else {
1865 readcommit $id
1866 if {![info exists commitinfo($id)]} {
1867 set commitinfo($id) [list [mc "No commit information available"]]
1868 }
1869 }
1870 return 1
1871 }
1872
1873 # Expand an abbreviated commit ID to a list of full 40-char IDs that match
1874 # and are present in the current view.
1875 # This is fairly slow...
1876 proc longid {prefix} {
1877 global varcid curview vshortids
1878
1879 set ids {}
1880 if {[string length $prefix] >= 4} {
1881 set vshortid $curview,[string range $prefix 0 3]
1882 if {[info exists vshortids($vshortid)]} {
1883 foreach id $vshortids($vshortid) {
1884 if {[string match "$prefix*" $id]} {
1885 if {[lsearch -exact $ids $id] < 0} {
1886 lappend ids $id
1887 if {[llength $ids] >= 2} break
1888 }
1889 }
1890 }
1891 }
1892 } else {
1893 foreach match [array names varcid "$curview,$prefix*"] {
1894 lappend ids [lindex [split $match ","] 1]
1895 if {[llength $ids] >= 2} break
1896 }
1897 }
1898 return $ids
1899 }
1900
1901 proc readrefs {} {
1902 global tagids idtags headids idheads tagobjid
1903 global otherrefids idotherrefs mainhead mainheadid
1904 global selecthead selectheadid
1905 global hideremotes
1906 global tclencoding
1907
1908 foreach v {tagids idtags headids idheads otherrefids idotherrefs} {
1909 unset -nocomplain $v
1910 }
1911 set refd [open [list | git show-ref -d] r]
1912 if {$tclencoding != {}} {
1913 fconfigure $refd -encoding $tclencoding
1914 }
1915 while {[gets $refd line] >= 0} {
1916 if {[string index $line 40] ne " "} continue
1917 set id [string range $line 0 39]
1918 set ref [string range $line 41 end]
1919 if {![string match "refs/*" $ref]} continue
1920 set name [string range $ref 5 end]
1921 if {[string match "remotes/*" $name]} {
1922 if {![string match "*/HEAD" $name] && !$hideremotes} {
1923 set headids($name) $id
1924 lappend idheads($id) $name
1925 }
1926 } elseif {[string match "heads/*" $name]} {
1927 set name [string range $name 6 end]
1928 set headids($name) $id
1929 lappend idheads($id) $name
1930 } elseif {[string match "tags/*" $name]} {
1931 # this lets refs/tags/foo^{} overwrite refs/tags/foo,
1932 # which is what we want since the former is the commit ID
1933 set name [string range $name 5 end]
1934 if {[string match "*^{}" $name]} {
1935 set name [string range $name 0 end-3]
1936 } else {
1937 set tagobjid($name) $id
1938 }
1939 set tagids($name) $id
1940 lappend idtags($id) $name
1941 } else {
1942 set otherrefids($name) $id
1943 lappend idotherrefs($id) $name
1944 }
1945 }
1946 catch {close $refd}
1947 set mainhead {}
1948 set mainheadid {}
1949 catch {
1950 set mainheadid [exec git rev-parse HEAD]
1951 set thehead [exec git symbolic-ref HEAD]
1952 if {[string match "refs/heads/*" $thehead]} {
1953 set mainhead [string range $thehead 11 end]
1954 }
1955 }
1956 set selectheadid {}
1957 if {$selecthead ne {}} {
1958 catch {
1959 set selectheadid [exec git rev-parse --verify $selecthead]
1960 }
1961 }
1962 }
1963
1964 # skip over fake commits
1965 proc first_real_row {} {
1966 global nullid nullid2 numcommits
1967
1968 for {set row 0} {$row < $numcommits} {incr row} {
1969 set id [commitonrow $row]
1970 if {$id ne $nullid && $id ne $nullid2} {
1971 break
1972 }
1973 }
1974 return $row
1975 }
1976
1977 # update things for a head moved to a child of its previous location
1978 proc movehead {id name} {
1979 global headids idheads
1980
1981 removehead $headids($name) $name
1982 set headids($name) $id
1983 lappend idheads($id) $name
1984 }
1985
1986 # update things when a head has been removed
1987 proc removehead {id name} {
1988 global headids idheads
1989
1990 if {$idheads($id) eq $name} {
1991 unset idheads($id)
1992 } else {
1993 set i [lsearch -exact $idheads($id) $name]
1994 if {$i >= 0} {
1995 set idheads($id) [lreplace $idheads($id) $i $i]
1996 }
1997 }
1998 unset headids($name)
1999 }
2000
2001 proc ttk_toplevel {w args} {
2002 global use_ttk
2003 eval [linsert $args 0 ::toplevel $w]
2004 if {$use_ttk} {
2005 place [ttk::frame $w._toplevel_background] -x 0 -y 0 -relwidth 1 -relheight 1
2006 }
2007 return $w
2008 }
2009
2010 proc make_transient {window origin} {
2011 global have_tk85
2012
2013 # In MacOS Tk 8.4 transient appears to work by setting
2014 # overrideredirect, which is utterly useless, since the
2015 # windows get no border, and are not even kept above
2016 # the parent.
2017 if {!$have_tk85 && [tk windowingsystem] eq {aqua}} return
2018
2019 wm transient $window $origin
2020
2021 # Windows fails to place transient windows normally, so
2022 # schedule a callback to center them on the parent.
2023 if {[tk windowingsystem] eq {win32}} {
2024 after idle [list tk::PlaceWindow $window widget $origin]
2025 }
2026 }
2027
2028 proc show_error {w top msg} {
2029 global NS
2030 if {![info exists NS]} {set NS ""}
2031 if {[wm state $top] eq "withdrawn"} { wm deiconify $top }
2032 message $w.m -text $msg -justify center -aspect 400
2033 pack $w.m -side top -fill x -padx 20 -pady 20
2034 ${NS}::button $w.ok -default active -text [mc OK] -command "destroy $top"
2035 pack $w.ok -side bottom -fill x
2036 bind $top <Visibility> "grab $top; focus $top"
2037 bind $top <Key-Return> "destroy $top"
2038 bind $top <Key-space> "destroy $top"
2039 bind $top <Key-Escape> "destroy $top"
2040 tkwait window $top
2041 }
2042
2043 proc error_popup {msg {owner .}} {
2044 if {[tk windowingsystem] eq "win32"} {
2045 tk_messageBox -icon error -type ok -title [wm title .] \
2046 -parent $owner -message $msg
2047 } else {
2048 set w .error
2049 ttk_toplevel $w
2050 make_transient $w $owner
2051 show_error $w $w $msg
2052 }
2053 }
2054
2055 proc confirm_popup {msg {owner .}} {
2056 global confirm_ok NS
2057 set confirm_ok 0
2058 set w .confirm
2059 ttk_toplevel $w
2060 make_transient $w $owner
2061 message $w.m -text $msg -justify center -aspect 400
2062 pack $w.m -side top -fill x -padx 20 -pady 20
2063 ${NS}::button $w.ok -text [mc OK] -command "set confirm_ok 1; destroy $w"
2064 pack $w.ok -side left -fill x
2065 ${NS}::button $w.cancel -text [mc Cancel] -command "destroy $w"
2066 pack $w.cancel -side right -fill x
2067 bind $w <Visibility> "grab $w; focus $w"
2068 bind $w <Key-Return> "set confirm_ok 1; destroy $w"
2069 bind $w <Key-space> "set confirm_ok 1; destroy $w"
2070 bind $w <Key-Escape> "destroy $w"
2071 tk::PlaceWindow $w widget $owner
2072 tkwait window $w
2073 return $confirm_ok
2074 }
2075
2076 proc haveselectionclipboard {} {
2077 return [expr {[tk windowingsystem] eq "x11"}]
2078 }
2079
2080 proc setoptions {} {
2081 global use_ttk
2082
2083 if {[tk windowingsystem] ne "win32"} {
2084 option add *Panedwindow.showHandle 1 startupFile
2085 option add *Panedwindow.sashRelief raised startupFile
2086 if {[tk windowingsystem] ne "aqua"} {
2087 option add *Menu.font uifont startupFile
2088 }
2089 } else {
2090 option add *Menu.TearOff 0 startupFile
2091 }
2092 option add *Button.font uifont startupFile
2093 option add *Checkbutton.font uifont startupFile
2094 option add *Radiobutton.font uifont startupFile
2095 option add *Menubutton.font uifont startupFile
2096 option add *Label.font uifont startupFile
2097 option add *Message.font uifont startupFile
2098 option add *Entry.font textfont startupFile
2099 option add *Text.font textfont startupFile
2100 option add *Labelframe.font uifont startupFile
2101 option add *Spinbox.font textfont startupFile
2102 option add *Listbox.font mainfont startupFile
2103 }
2104
2105 proc setttkstyle {} {
2106 eval font configure TkDefaultFont [fontflags mainfont]
2107 eval font configure TkTextFont [fontflags textfont]
2108 eval font configure TkHeadingFont [fontflags mainfont]
2109 eval font configure TkCaptionFont [fontflags mainfont] -weight bold
2110 eval font configure TkTooltipFont [fontflags uifont]
2111 eval font configure TkFixedFont [fontflags textfont]
2112 eval font configure TkIconFont [fontflags uifont]
2113 eval font configure TkMenuFont [fontflags uifont]
2114 eval font configure TkSmallCaptionFont [fontflags uifont]
2115 }
2116
2117 # Make a menu and submenus.
2118 # m is the window name for the menu, items is the list of menu items to add.
2119 # Each item is a list {mc label type description options...}
2120 # mc is ignored; it's so we can put mc there to alert xgettext
2121 # label is the string that appears in the menu
2122 # type is cascade, command or radiobutton (should add checkbutton)
2123 # description depends on type; it's the sublist for cascade, the
2124 # command to invoke for command, or {variable value} for radiobutton
2125 proc makemenu {m items} {
2126 menu $m
2127 if {[tk windowingsystem] eq {aqua}} {
2128 set Meta1 Cmd
2129 } else {
2130 set Meta1 Ctrl
2131 }
2132 foreach i $items {
2133 set name [mc [lindex $i 1]]
2134 set type [lindex $i 2]
2135 set thing [lindex $i 3]
2136 set params [list $type]
2137 if {$name ne {}} {
2138 set u [string first "&" [string map {&& x} $name]]
2139 lappend params -label [string map {&& & & {}} $name]
2140 if {$u >= 0} {
2141 lappend params -underline $u
2142 }
2143 }
2144 switch -- $type {
2145 "cascade" {
2146 set submenu [string tolower [string map {& ""} [lindex $i 1]]]
2147 lappend params -menu $m.$submenu
2148 }
2149 "command" {
2150 lappend params -command $thing
2151 }
2152 "radiobutton" {
2153 lappend params -variable [lindex $thing 0] \
2154 -value [lindex $thing 1]
2155 }
2156 }
2157 set tail [lrange $i 4 end]
2158 regsub -all {\yMeta1\y} $tail $Meta1 tail
2159 eval $m add $params $tail
2160 if {$type eq "cascade"} {
2161 makemenu $m.$submenu $thing
2162 }
2163 }
2164 }
2165
2166 # translate string and remove ampersands
2167 proc mca {str} {
2168 return [string map {&& & & {}} [mc $str]]
2169 }
2170
2171 proc cleardropsel {w} {
2172 $w selection clear
2173 }
2174 proc makedroplist {w varname args} {
2175 global use_ttk
2176 if {$use_ttk} {
2177 set width 0
2178 foreach label $args {
2179 set cx [string length $label]
2180 if {$cx > $width} {set width $cx}
2181 }
2182 set gm [ttk::combobox $w -width $width -state readonly\
2183 -textvariable $varname -values $args \
2184 -exportselection false]
2185 bind $gm <<ComboboxSelected>> [list $gm selection clear]
2186 } else {
2187 set gm [eval [linsert $args 0 tk_optionMenu $w $varname]]
2188 }
2189 return $gm
2190 }
2191
2192 proc makewindow {} {
2193 global canv canv2 canv3 linespc charspc ctext cflist cscroll
2194 global tabstop
2195 global findtype findtypemenu findloc findstring fstring geometry
2196 global entries sha1entry sha1string sha1but
2197 global diffcontextstring diffcontext
2198 global ignorespace
2199 global maincursor textcursor curtextcursor
2200 global rowctxmenu fakerowmenu mergemax wrapcomment wrapdefault
2201 global highlight_files gdttype
2202 global searchstring sstring
2203 global bgcolor fgcolor bglist fglist diffcolors diffbgcolors selectbgcolor
2204 global uifgcolor uifgdisabledcolor
2205 global filesepbgcolor filesepfgcolor
2206 global mergecolors foundbgcolor currentsearchhitbgcolor
2207 global headctxmenu progresscanv progressitem progresscoords statusw
2208 global fprogitem fprogcoord lastprogupdate progupdatepending
2209 global rprogitem rprogcoord rownumsel numcommits
2210 global have_tk85 have_tk86 use_ttk NS
2211 global git_version
2212 global worddiff
2213
2214 # The "mc" arguments here are purely so that xgettext
2215 # sees the following string as needing to be translated
2216 set file {
2217 mc "&File" cascade {
2218 {mc "&Update" command updatecommits -accelerator F5}
2219 {mc "&Reload" command reloadcommits -accelerator Shift-F5}
2220 {mc "Reread re&ferences" command rereadrefs}
2221 {mc "&List references" command showrefs -accelerator F2}
2222 {xx "" separator}
2223 {mc "Start git &gui" command {exec git gui &}}
2224 {xx "" separator}
2225 {mc "&Quit" command doquit -accelerator Meta1-Q}
2226 }}
2227 set edit {
2228 mc "&Edit" cascade {
2229 {mc "&Preferences" command doprefs}
2230 }}
2231 set view {
2232 mc "&View" cascade {
2233 {mc "&New view..." command {newview 0} -accelerator Shift-F4}
2234 {mc "&Edit view..." command editview -state disabled -accelerator F4}
2235 {mc "&Delete view" command delview -state disabled}
2236 {xx "" separator}
2237 {mc "&All files" radiobutton {selectedview 0} -command {showview 0}}
2238 }}
2239 if {[tk windowingsystem] ne "aqua"} {
2240 set help {
2241 mc "&Help" cascade {
2242 {mc "&About gitk" command about}
2243 {mc "&Key bindings" command keys}
2244 }}
2245 set bar [list $file $edit $view $help]
2246 } else {
2247 proc ::tk::mac::ShowPreferences {} {doprefs}
2248 proc ::tk::mac::Quit {} {doquit}
2249 lset file end [lreplace [lindex $file end] end-1 end]
2250 set apple {
2251 xx "&Apple" cascade {
2252 {mc "&About gitk" command about}
2253 {xx "" separator}
2254 }}
2255 set help {
2256 mc "&Help" cascade {
2257 {mc "&Key bindings" command keys}
2258 }}
2259 set bar [list $apple $file $view $help]
2260 }
2261 makemenu .bar $bar
2262 . configure -menu .bar
2263
2264 if {$use_ttk} {
2265 # cover the non-themed toplevel with a themed frame.
2266 place [ttk::frame ._main_background] -x 0 -y 0 -relwidth 1 -relheight 1
2267 }
2268
2269 # the gui has upper and lower half, parts of a paned window.
2270 ${NS}::panedwindow .ctop -orient vertical
2271
2272 # possibly use assumed geometry
2273 if {![info exists geometry(pwsash0)]} {
2274 set geometry(topheight) [expr {15 * $linespc}]
2275 set geometry(topwidth) [expr {80 * $charspc}]
2276 set geometry(botheight) [expr {15 * $linespc}]
2277 set geometry(botwidth) [expr {50 * $charspc}]
2278 set geometry(pwsash0) [list [expr {40 * $charspc}] 2]
2279 set geometry(pwsash1) [list [expr {60 * $charspc}] 2]
2280 }
2281
2282 # the upper half will have a paned window, a scroll bar to the right, and some stuff below
2283 ${NS}::frame .tf -height $geometry(topheight) -width $geometry(topwidth)
2284 ${NS}::frame .tf.histframe
2285 ${NS}::panedwindow .tf.histframe.pwclist -orient horizontal
2286 if {!$use_ttk} {
2287 .tf.histframe.pwclist configure -sashpad 0 -handlesize 4
2288 }
2289
2290 # create three canvases
2291 set cscroll .tf.histframe.csb
2292 set canv .tf.histframe.pwclist.canv
2293 canvas $canv \
2294 -selectbackground $selectbgcolor \
2295 -background $bgcolor -bd 0 \
2296 -yscrollincr $linespc -yscrollcommand "scrollcanv $cscroll"
2297 .tf.histframe.pwclist add $canv
2298 set canv2 .tf.histframe.pwclist.canv2
2299 canvas $canv2 \
2300 -selectbackground $selectbgcolor \
2301 -background $bgcolor -bd 0 -yscrollincr $linespc
2302 .tf.histframe.pwclist add $canv2
2303 set canv3 .tf.histframe.pwclist.canv3
2304 canvas $canv3 \
2305 -selectbackground $selectbgcolor \
2306 -background $bgcolor -bd 0 -yscrollincr $linespc
2307 .tf.histframe.pwclist add $canv3
2308 if {$use_ttk} {
2309 bind .tf.histframe.pwclist <Map> {
2310 bind %W <Map> {}
2311 .tf.histframe.pwclist sashpos 1 [lindex $::geometry(pwsash1) 0]
2312 .tf.histframe.pwclist sashpos 0 [lindex $::geometry(pwsash0) 0]
2313 }
2314 } else {
2315 eval .tf.histframe.pwclist sash place 0 $geometry(pwsash0)
2316 eval .tf.histframe.pwclist sash place 1 $geometry(pwsash1)
2317 }
2318
2319 # a scroll bar to rule them
2320 ${NS}::scrollbar $cscroll -command {allcanvs yview}
2321 if {!$use_ttk} {$cscroll configure -highlightthickness 0}
2322 pack $cscroll -side right -fill y
2323 bind .tf.histframe.pwclist <Configure> {resizeclistpanes %W %w}
2324 lappend bglist $canv $canv2 $canv3
2325 pack .tf.histframe.pwclist -fill both -expand 1 -side left
2326
2327 # we have two button bars at bottom of top frame. Bar 1
2328 ${NS}::frame .tf.bar
2329 ${NS}::frame .tf.lbar -height 15
2330
2331 set sha1entry .tf.bar.sha1
2332 set entries $sha1entry
2333 set sha1but .tf.bar.sha1label
2334 button $sha1but -text "[mc "Commit ID:"] " -state disabled -relief flat \
2335 -command gotocommit -width 8
2336 $sha1but conf -disabledforeground [$sha1but cget -foreground]
2337 pack .tf.bar.sha1label -side left
2338 ${NS}::entry $sha1entry -width 40 -font textfont -textvariable sha1string
2339 trace add variable sha1string write sha1change
2340 pack $sha1entry -side left -pady 2
2341
2342 set bm_left_data {
2343 #define left_width 16
2344 #define left_height 16
2345 static unsigned char left_bits[] = {
2346 0x00, 0x00, 0xc0, 0x01, 0xe0, 0x00, 0x70, 0x00, 0x38, 0x00, 0x1c, 0x00,
2347 0x0e, 0x00, 0xff, 0x7f, 0xff, 0x7f, 0xff, 0x7f, 0x0e, 0x00, 0x1c, 0x00,
2348 0x38, 0x00, 0x70, 0x00, 0xe0, 0x00, 0xc0, 0x01};
2349 }
2350 set bm_right_data {
2351 #define right_width 16
2352 #define right_height 16
2353 static unsigned char right_bits[] = {
2354 0x00, 0x00, 0xc0, 0x01, 0x80, 0x03, 0x00, 0x07, 0x00, 0x0e, 0x00, 0x1c,
2355 0x00, 0x38, 0xff, 0x7f, 0xff, 0x7f, 0xff, 0x7f, 0x00, 0x38, 0x00, 0x1c,
2356 0x00, 0x0e, 0x00, 0x07, 0x80, 0x03, 0xc0, 0x01};
2357 }
2358 image create bitmap bm-left -data $bm_left_data -foreground $uifgcolor
2359 image create bitmap bm-left-gray -data $bm_left_data -foreground $uifgdisabledcolor
2360 image create bitmap bm-right -data $bm_right_data -foreground $uifgcolor
2361 image create bitmap bm-right-gray -data $bm_right_data -foreground $uifgdisabledcolor
2362
2363 ${NS}::button .tf.bar.leftbut -command goback -state disabled -width 26
2364 if {$use_ttk} {
2365 .tf.bar.leftbut configure -image [list bm-left disabled bm-left-gray]
2366 } else {
2367 .tf.bar.leftbut configure -image bm-left
2368 }
2369 pack .tf.bar.leftbut -side left -fill y
2370 ${NS}::button .tf.bar.rightbut -command goforw -state disabled -width 26
2371 if {$use_ttk} {
2372 .tf.bar.rightbut configure -image [list bm-right disabled bm-right-gray]
2373 } else {
2374 .tf.bar.rightbut configure -image bm-right
2375 }
2376 pack .tf.bar.rightbut -side left -fill y
2377
2378 ${NS}::label .tf.bar.rowlabel -text [mc "Row"]
2379 set rownumsel {}
2380 ${NS}::label .tf.bar.rownum -width 7 -textvariable rownumsel \
2381 -relief sunken -anchor e
2382 ${NS}::label .tf.bar.rowlabel2 -text "/"
2383 ${NS}::label .tf.bar.numcommits -width 7 -textvariable numcommits \
2384 -relief sunken -anchor e
2385 pack .tf.bar.rowlabel .tf.bar.rownum .tf.bar.rowlabel2 .tf.bar.numcommits \
2386 -side left
2387 if {!$use_ttk} {
2388 foreach w {rownum numcommits} {.tf.bar.$w configure -font textfont}
2389 }
2390 global selectedline
2391 trace add variable selectedline write selectedline_change
2392
2393 # Status label and progress bar
2394 set statusw .tf.bar.status
2395 ${NS}::label $statusw -width 15 -relief sunken
2396 pack $statusw -side left -padx 5
2397 if {$use_ttk} {
2398 set progresscanv [ttk::progressbar .tf.bar.progress]
2399 } else {
2400 set h [expr {[font metrics uifont -linespace] + 2}]
2401 set progresscanv .tf.bar.progress
2402 canvas $progresscanv -relief sunken -height $h -borderwidth 2
2403 set progressitem [$progresscanv create rect -1 0 0 $h -fill "#00ff00"]
2404 set fprogitem [$progresscanv create rect -1 0 0 $h -fill yellow]
2405 set rprogitem [$progresscanv create rect -1 0 0 $h -fill red]
2406 }
2407 pack $progresscanv -side right -expand 1 -fill x -padx {0 2}
2408 set progresscoords {0 0}
2409 set fprogcoord 0
2410 set rprogcoord 0
2411 bind $progresscanv <Configure> adjustprogress
2412 set lastprogupdate [clock clicks -milliseconds]
2413 set progupdatepending 0
2414
2415 # build up the bottom bar of upper window
2416 ${NS}::label .tf.lbar.flabel -text "[mc "Find"] "
2417
2418 set bm_down_data {
2419 #define down_width 16
2420 #define down_height 16
2421 static unsigned char down_bits[] = {
2422 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01,
2423 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01,
2424 0x87, 0xe1, 0x8e, 0x71, 0x9c, 0x39, 0xb8, 0x1d,
2425 0xf0, 0x0f, 0xe0, 0x07, 0xc0, 0x03, 0x80, 0x01};
2426 }
2427 image create bitmap bm-down -data $bm_down_data -foreground $uifgcolor
2428 ${NS}::button .tf.lbar.fnext -width 26 -command {dofind 1 1}
2429 .tf.lbar.fnext configure -image bm-down
2430
2431 set bm_up_data {
2432 #define up_width 16
2433 #define up_height 16
2434 static unsigned char up_bits[] = {
2435 0x80, 0x01, 0xc0, 0x03, 0xe0, 0x07, 0xf0, 0x0f,
2436 0xb8, 0x1d, 0x9c, 0x39, 0x8e, 0x71, 0x87, 0xe1,
2437 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01,
2438 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01};
2439 }
2440 image create bitmap bm-up -data $bm_up_data -foreground $uifgcolor
2441 ${NS}::button .tf.lbar.fprev -width 26 -command {dofind -1 1}
2442 .tf.lbar.fprev configure -image bm-up
2443
2444 ${NS}::label .tf.lbar.flab2 -text " [mc "commit"] "
2445
2446 pack .tf.lbar.flabel .tf.lbar.fnext .tf.lbar.fprev .tf.lbar.flab2 \
2447 -side left -fill y
2448 set gdttype [mc "containing:"]
2449 set gm [makedroplist .tf.lbar.gdttype gdttype \
2450 [mc "containing:"] \
2451 [mc "touching paths:"] \
2452 [mc "adding/removing string:"] \
2453 [mc "changing lines matching:"]]
2454 trace add variable gdttype write gdttype_change
2455 pack .tf.lbar.gdttype -side left -fill y
2456
2457 set findstring {}
2458 set fstring .tf.lbar.findstring
2459 lappend entries $fstring
2460 ${NS}::entry $fstring -width 30 -textvariable findstring
2461 trace add variable findstring write find_change
2462 set findtype [mc "Exact"]
2463 set findtypemenu [makedroplist .tf.lbar.findtype \
2464 findtype [mc "Exact"] [mc "IgnCase"] [mc "Regexp"]]
2465 trace add variable findtype write findcom_change
2466 set findloc [mc "All fields"]
2467 makedroplist .tf.lbar.findloc findloc [mc "All fields"] [mc "Headline"] \
2468 [mc "Comments"] [mc "Author"] [mc "Committer"]
2469 trace add variable findloc write find_change
2470 pack .tf.lbar.findloc -side right
2471 pack .tf.lbar.findtype -side right
2472 pack $fstring -side left -expand 1 -fill x
2473
2474 # Finish putting the upper half of the viewer together
2475 pack .tf.lbar -in .tf -side bottom -fill x
2476 pack .tf.bar -in .tf -side bottom -fill x
2477 pack .tf.histframe -fill both -side top -expand 1
2478 .ctop add .tf
2479 if {!$use_ttk} {
2480 .ctop paneconfigure .tf -height $geometry(topheight)
2481 .ctop paneconfigure .tf -width $geometry(topwidth)
2482 }
2483
2484 # now build up the bottom
2485 ${NS}::panedwindow .pwbottom -orient horizontal
2486
2487 # lower left, a text box over search bar, scroll bar to the right
2488 # if we know window height, then that will set the lower text height, otherwise
2489 # we set lower text height which will drive window height
2490 if {[info exists geometry(main)]} {
2491 ${NS}::frame .bleft -width $geometry(botwidth)
2492 } else {
2493 ${NS}::frame .bleft -width $geometry(botwidth) -height $geometry(botheight)
2494 }
2495 ${NS}::frame .bleft.top
2496 ${NS}::frame .bleft.mid
2497 ${NS}::frame .bleft.bottom
2498
2499 # gap between sub-widgets
2500 set wgap [font measure uifont "i"]
2501
2502 ${NS}::button .bleft.top.search -text [mc "Search"] -command dosearch
2503 pack .bleft.top.search -side left -padx 5
2504 set sstring .bleft.top.sstring
2505 set searchstring ""
2506 ${NS}::entry $sstring -width 20 -textvariable searchstring
2507 lappend entries $sstring
2508 trace add variable searchstring write incrsearch
2509 pack $sstring -side left -expand 1 -fill x
2510 ${NS}::radiobutton .bleft.mid.diff -text [mc "Diff"] \
2511 -command changediffdisp -variable diffelide -value {0 0}
2512 ${NS}::radiobutton .bleft.mid.old -text [mc "Old version"] \
2513 -command changediffdisp -variable diffelide -value {0 1}
2514 ${NS}::radiobutton .bleft.mid.new -text [mc "New version"] \
2515 -command changediffdisp -variable diffelide -value {1 0}
2516
2517 ${NS}::label .bleft.mid.labeldiffcontext -text " [mc "Lines of context"]: "
2518 pack .bleft.mid.diff .bleft.mid.old .bleft.mid.new -side left -ipadx $wgap
2519 spinbox .bleft.mid.diffcontext -width 5 \
2520 -from 0 -increment 1 -to 10000000 \
2521 -validate all -validatecommand "diffcontextvalidate %P" \
2522 -textvariable diffcontextstring
2523 .bleft.mid.diffcontext set $diffcontext
2524 trace add variable diffcontextstring write diffcontextchange
2525 lappend entries .bleft.mid.diffcontext
2526 pack .bleft.mid.labeldiffcontext .bleft.mid.diffcontext -side left -ipadx $wgap
2527 ${NS}::checkbutton .bleft.mid.ignspace -text [mc "Ignore space change"] \
2528 -command changeignorespace -variable ignorespace
2529 pack .bleft.mid.ignspace -side left -padx 5
2530
2531 set worddiff [mc "Line diff"]
2532 if {[package vcompare $git_version "1.7.2"] >= 0} {
2533 makedroplist .bleft.mid.worddiff worddiff [mc "Line diff"] \
2534 [mc "Markup words"] [mc "Color words"]
2535 trace add variable worddiff write changeworddiff
2536 pack .bleft.mid.worddiff -side left -padx 5
2537 }
2538
2539 set ctext .bleft.bottom.ctext
2540 text $ctext -background $bgcolor -foreground $fgcolor \
2541 -state disabled -undo 0 -font textfont \
2542 -yscrollcommand scrolltext -wrap $wrapdefault \
2543 -xscrollcommand ".bleft.bottom.sbhorizontal set"
2544 if {$have_tk85} {
2545 $ctext conf -tabstyle wordprocessor
2546 }
2547 ${NS}::scrollbar .bleft.bottom.sb -command "$ctext yview"
2548 ${NS}::scrollbar .bleft.bottom.sbhorizontal -command "$ctext xview" -orient h
2549 pack .bleft.top -side top -fill x
2550 pack .bleft.mid -side top -fill x
2551 grid $ctext .bleft.bottom.sb -sticky nsew
2552 grid .bleft.bottom.sbhorizontal -sticky ew
2553 grid columnconfigure .bleft.bottom 0 -weight 1
2554 grid rowconfigure .bleft.bottom 0 -weight 1
2555 grid rowconfigure .bleft.bottom 1 -weight 0
2556 pack .bleft.bottom -side top -fill both -expand 1
2557 lappend bglist $ctext
2558 lappend fglist $ctext
2559
2560 $ctext tag conf comment -wrap $wrapcomment
2561 $ctext tag conf filesep -font textfontbold -fore $filesepfgcolor -back $filesepbgcolor
2562 $ctext tag conf hunksep -fore [lindex $diffcolors 2]
2563 $ctext tag conf d0 -fore [lindex $diffcolors 0]
2564 $ctext tag conf d0 -back [lindex $diffbgcolors 0]
2565 $ctext tag conf dresult -fore [lindex $diffcolors 1]
2566 $ctext tag conf dresult -back [lindex $diffbgcolors 1]
2567 $ctext tag conf m0 -fore [lindex $mergecolors 0]
2568 $ctext tag conf m1 -fore [lindex $mergecolors 1]
2569 $ctext tag conf m2 -fore [lindex $mergecolors 2]
2570 $ctext tag conf m3 -fore [lindex $mergecolors 3]
2571 $ctext tag conf m4 -fore [lindex $mergecolors 4]
2572 $ctext tag conf m5 -fore [lindex $mergecolors 5]
2573 $ctext tag conf m6 -fore [lindex $mergecolors 6]
2574 $ctext tag conf m7 -fore [lindex $mergecolors 7]
2575 $ctext tag conf m8 -fore [lindex $mergecolors 8]
2576 $ctext tag conf m9 -fore [lindex $mergecolors 9]
2577 $ctext tag conf m10 -fore [lindex $mergecolors 10]
2578 $ctext tag conf m11 -fore [lindex $mergecolors 11]
2579 $ctext tag conf m12 -fore [lindex $mergecolors 12]
2580 $ctext tag conf m13 -fore [lindex $mergecolors 13]
2581 $ctext tag conf m14 -fore [lindex $mergecolors 14]
2582 $ctext tag conf m15 -fore [lindex $mergecolors 15]
2583 $ctext tag conf mmax -fore darkgrey
2584 set mergemax 16
2585 $ctext tag conf mresult -font textfontbold
2586 $ctext tag conf msep -font textfontbold
2587 $ctext tag conf found -back $foundbgcolor
2588 $ctext tag conf currentsearchhit -back $currentsearchhitbgcolor
2589 $ctext tag conf wwrap -wrap word -lmargin2 1c
2590 $ctext tag conf bold -font textfontbold
2591 # set these to the lowest priority:
2592 $ctext tag lower currentsearchhit
2593 $ctext tag lower found
2594 $ctext tag lower filesep
2595 $ctext tag lower dresult
2596 $ctext tag lower d0
2597
2598 .pwbottom add .bleft
2599 if {!$use_ttk} {
2600 .pwbottom paneconfigure .bleft -width $geometry(botwidth)
2601 }
2602
2603 # lower right
2604 ${NS}::frame .bright
2605 ${NS}::frame .bright.mode
2606 ${NS}::radiobutton .bright.mode.patch -text [mc "Patch"] \
2607 -command reselectline -variable cmitmode -value "patch"
2608 ${NS}::radiobutton .bright.mode.tree -text [mc "Tree"] \
2609 -command reselectline -variable cmitmode -value "tree"
2610 grid .bright.mode.patch .bright.mode.tree -sticky ew
2611 pack .bright.mode -side top -fill x
2612 set cflist .bright.cfiles
2613 set indent [font measure mainfont "nn"]
2614 text $cflist \
2615 -selectbackground $selectbgcolor \
2616 -background $bgcolor -foreground $fgcolor \
2617 -font mainfont \
2618 -tabs [list $indent [expr {2 * $indent}]] \
2619 -yscrollcommand ".bright.sb set" \
2620 -cursor [. cget -cursor] \
2621 -spacing1 1 -spacing3 1
2622 lappend bglist $cflist
2623 lappend fglist $cflist
2624 ${NS}::scrollbar .bright.sb -command "$cflist yview"
2625 pack .bright.sb -side right -fill y
2626 pack $cflist -side left -fill both -expand 1
2627 $cflist tag configure highlight \
2628 -background [$cflist cget -selectbackground]
2629 $cflist tag configure bold -font mainfontbold
2630
2631 .pwbottom add .bright
2632 .ctop add .pwbottom
2633
2634 # restore window width & height if known
2635 if {[info exists geometry(main)]} {
2636 if {[scan $geometry(main) "%dx%d" w h] >= 2} {
2637 if {$w > [winfo screenwidth .]} {
2638 set w [winfo screenwidth .]
2639 }
2640 if {$h > [winfo screenheight .]} {
2641 set h [winfo screenheight .]
2642 }
2643 wm geometry . "${w}x$h"
2644 }
2645 }
2646
2647 if {[info exists geometry(state)] && $geometry(state) eq "zoomed"} {
2648 wm state . $geometry(state)
2649 }
2650
2651 if {[tk windowingsystem] eq {aqua}} {
2652 set M1B M1
2653 set ::BM "3"
2654 } else {
2655 set M1B Control
2656 set ::BM "2"
2657 }
2658
2659 if {$use_ttk} {
2660 bind .ctop <Map> {
2661 bind %W <Map> {}
2662 %W sashpos 0 $::geometry(topheight)
2663 }
2664 bind .pwbottom <Map> {
2665 bind %W <Map> {}
2666 %W sashpos 0 $::geometry(botwidth)
2667 }
2668 bind .pwbottom <Configure> {resizecdetpanes %W %w}
2669 }
2670
2671 pack .ctop -fill both -expand 1
2672 bindall <1> {selcanvline %W %x %y}
2673 #bindall <B1-Motion> {selcanvline %W %x %y}
2674 if {[tk windowingsystem] == "win32"} {
2675 bind . <MouseWheel> { windows_mousewheel_redirector %W %X %Y %D }
2676 bind $ctext <MouseWheel> { windows_mousewheel_redirector %W %X %Y %D ; break }
2677 } else {
2678 bindall <ButtonRelease-4> "allcanvs yview scroll -5 units"
2679 bindall <ButtonRelease-5> "allcanvs yview scroll 5 units"
2680 bind $ctext <Button> {
2681 if {"%b" eq 6} {
2682 $ctext xview scroll -5 units
2683 } elseif {"%b" eq 7} {
2684 $ctext xview scroll 5 units
2685 }
2686 }
2687 if {[tk windowingsystem] eq "aqua"} {
2688 bindall <MouseWheel> {
2689 set delta [expr {- (%D)}]
2690 allcanvs yview scroll $delta units
2691 }
2692 bindall <Shift-MouseWheel> {
2693 set delta [expr {- (%D)}]
2694 $canv xview scroll $delta units
2695 }
2696 }
2697 }
2698 bindall <$::BM> "canvscan mark %W %x %y"
2699 bindall <B$::BM-Motion> "canvscan dragto %W %x %y"
2700 bind all <$M1B-Key-w> {destroy [winfo toplevel %W]}
2701 bind . <$M1B-Key-w> doquit
2702 bindkey <Home> selfirstline
2703 bindkey <End> sellastline
2704 bind . <Key-Up> "selnextline -1"
2705 bind . <Key-Down> "selnextline 1"
2706 bind . <Shift-Key-Up> "dofind -1 0"
2707 bind . <Shift-Key-Down> "dofind 1 0"
2708 if {$have_tk86} {
2709 bindkey <<NextChar>> "goforw"
2710 bindkey <<PrevChar>> "goback"
2711 } else {
2712 bindkey <Key-Right> "goforw"
2713 bindkey <Key-Left> "goback"
2714 }
2715 bind . <Key-Prior> "selnextpage -1"
2716 bind . <Key-Next> "selnextpage 1"
2717 bind . <$M1B-Home> "allcanvs yview moveto 0.0"
2718 bind . <$M1B-End> "allcanvs yview moveto 1.0"
2719 bind . <$M1B-Key-Up> "allcanvs yview scroll -1 units"
2720 bind . <$M1B-Key-Down> "allcanvs yview scroll 1 units"
2721 bind . <$M1B-Key-Prior> "allcanvs yview scroll -1 pages"
2722 bind . <$M1B-Key-Next> "allcanvs yview scroll 1 pages"
2723 bindkey <Key-Delete> "$ctext yview scroll -1 pages"
2724 bindkey <Key-BackSpace> "$ctext yview scroll -1 pages"
2725 bindkey <Key-space> "$ctext yview scroll 1 pages"
2726 bindkey p "selnextline -1"
2727 bindkey n "selnextline 1"
2728 bindkey z "goback"
2729 bindkey x "goforw"
2730 bindkey k "selnextline -1"
2731 bindkey j "selnextline 1"
2732 bindkey h "goback"
2733 bindkey l "goforw"
2734 bindkey b prevfile
2735 bindkey d "$ctext yview scroll 18 units"
2736 bindkey u "$ctext yview scroll -18 units"
2737 bindkey g {$sha1entry delete 0 end; focus $sha1entry}
2738 bindkey / {focus $fstring}
2739 bindkey <Key-KP_Divide> {focus $fstring}
2740 bindkey <Key-Return> {dofind 1 1}
2741 bindkey ? {dofind -1 1}
2742 bindkey f nextfile
2743 bind . <F5> updatecommits
2744 bindmodfunctionkey Shift 5 reloadcommits
2745 bind . <F2> showrefs
2746 bindmodfunctionkey Shift 4 {newview 0}
2747 bind . <F4> edit_or_newview
2748 bind . <$M1B-q> doquit
2749 bind . <$M1B-f> {dofind 1 1}
2750 bind . <$M1B-g> {dofind 1 0}
2751 bind . <$M1B-r> dosearchback
2752 bind . <$M1B-s> dosearch
2753 bind . <$M1B-equal> {incrfont 1}
2754 bind . <$M1B-plus> {incrfont 1}
2755 bind . <$M1B-KP_Add> {incrfont 1}
2756 bind . <$M1B-minus> {incrfont -1}
2757 bind . <$M1B-KP_Subtract> {incrfont -1}
2758 wm protocol . WM_DELETE_WINDOW doquit
2759 bind . <Destroy> {stop_backends}
2760 bind . <Button-1> "click %W"
2761 bind $fstring <Key-Return> {dofind 1 1}
2762 bind $sha1entry <Key-Return> {gotocommit; break}
2763 bind $sha1entry <<PasteSelection>> clearsha1
2764 bind $sha1entry <<Paste>> clearsha1
2765 bind $cflist <1> {sel_flist %W %x %y; break}
2766 bind $cflist <B1-Motion> {sel_flist %W %x %y; break}
2767 bind $cflist <ButtonRelease-1> {treeclick %W %x %y}
2768 global ctxbut
2769 bind $cflist $ctxbut {pop_flist_menu %W %X %Y %x %y}
2770 bind $ctext $ctxbut {pop_diff_menu %W %X %Y %x %y}
2771 bind $ctext <Button-1> {focus %W}
2772 bind $ctext <<Selection>> rehighlight_search_results
2773 for {set i 1} {$i < 10} {incr i} {
2774 bind . <$M1B-Key-$i> [list go_to_parent $i]
2775 }
2776
2777 set maincursor [. cget -cursor]
2778 set textcursor [$ctext cget -cursor]
2779 set curtextcursor $textcursor
2780
2781 set rowctxmenu .rowctxmenu
2782 makemenu $rowctxmenu {
2783 {mc "Diff this -> selected" command {diffvssel 0}}
2784 {mc "Diff selected -> this" command {diffvssel 1}}
2785 {mc "Make patch" command mkpatch}
2786 {mc "Create tag" command mktag}
2787 {mc "Copy commit reference" command copyreference}
2788 {mc "Write commit to file" command writecommit}
2789 {mc "Create new branch" command mkbranch}
2790 {mc "Cherry-pick this commit" command cherrypick}
2791 {mc "Reset HEAD branch to here" command resethead}
2792 {mc "Mark this commit" command markhere}
2793 {mc "Return to mark" command gotomark}
2794 {mc "Find descendant of this and mark" command find_common_desc}
2795 {mc "Compare with marked commit" command compare_commits}
2796 {mc "Diff this -> marked commit" command {diffvsmark 0}}
2797 {mc "Diff marked commit -> this" command {diffvsmark 1}}
2798 {mc "Revert this commit" command revert}
2799 }
2800 $rowctxmenu configure -tearoff 0
2801
2802 set fakerowmenu .fakerowmenu
2803 makemenu $fakerowmenu {
2804 {mc "Diff this -> selected" command {diffvssel 0}}
2805 {mc "Diff selected -> this" command {diffvssel 1}}
2806 {mc "Make patch" command mkpatch}
2807 {mc "Diff this -> marked commit" command {diffvsmark 0}}
2808 {mc "Diff marked commit -> this" command {diffvsmark 1}}
2809 }
2810 $fakerowmenu configure -tearoff 0
2811
2812 set headctxmenu .headctxmenu
2813 makemenu $headctxmenu {
2814 {mc "Check out this branch" command cobranch}
2815 {mc "Rename this branch" command mvbranch}
2816 {mc "Remove this branch" command rmbranch}
2817 {mc "Copy branch name" command {clipboard clear; clipboard append $headmenuhead}}
2818 }
2819 $headctxmenu configure -tearoff 0
2820
2821 global flist_menu
2822 set flist_menu .flistctxmenu
2823 makemenu $flist_menu {
2824 {mc "Highlight this too" command {flist_hl 0}}
2825 {mc "Highlight this only" command {flist_hl 1}}
2826 {mc "External diff" command {external_diff}}
2827 {mc "Blame parent commit" command {external_blame 1}}
2828 {mc "Copy path" command {clipboard clear; clipboard append $flist_menu_file}}
2829 }
2830 $flist_menu configure -tearoff 0
2831
2832 global diff_menu
2833 set diff_menu .diffctxmenu
2834 makemenu $diff_menu {
2835 {mc "Show origin of this line" command show_line_source}
2836 {mc "Run git gui blame on this line" command {external_blame_diff}}
2837 }
2838 $diff_menu configure -tearoff 0
2839 }
2840
2841 # Windows sends all mouse wheel events to the current focused window, not
2842 # the one where the mouse hovers, so bind those events here and redirect
2843 # to the correct window
2844 proc windows_mousewheel_redirector {W X Y D} {
2845 global canv canv2 canv3
2846 set w [winfo containing -displayof $W $X $Y]
2847 if {$w ne ""} {
2848 set u [expr {$D < 0 ? 5 : -5}]
2849 if {$w == $canv || $w == $canv2 || $w == $canv3} {
2850 allcanvs yview scroll $u units
2851 } else {
2852 catch {
2853 $w yview scroll $u units
2854 }
2855 }
2856 }
2857 }
2858
2859 # Update row number label when selectedline changes
2860 proc selectedline_change {n1 n2 op} {
2861 global selectedline rownumsel
2862
2863 if {$selectedline eq {}} {
2864 set rownumsel {}
2865 } else {
2866 set rownumsel [expr {$selectedline + 1}]
2867 }
2868 }
2869
2870 # mouse-2 makes all windows scan vertically, but only the one
2871 # the cursor is in scans horizontally
2872 proc canvscan {op w x y} {
2873 global canv canv2 canv3
2874 foreach c [list $canv $canv2 $canv3] {
2875 if {$c == $w} {
2876 $c scan $op $x $y
2877 } else {
2878 $c scan $op 0 $y
2879 }
2880 }
2881 }
2882
2883 proc scrollcanv {cscroll f0 f1} {
2884 $cscroll set $f0 $f1
2885 drawvisible
2886 flushhighlights
2887 }
2888
2889 # when we make a key binding for the toplevel, make sure
2890 # it doesn't get triggered when that key is pressed in the
2891 # find string entry widget.
2892 proc bindkey {ev script} {
2893 global entries
2894 bind . $ev $script
2895 set escript [bind Entry $ev]
2896 if {$escript == {}} {
2897 set escript [bind Entry <Key>]
2898 }
2899 foreach e $entries {
2900 bind $e $ev "$escript; break"
2901 }
2902 }
2903
2904 proc bindmodfunctionkey {mod n script} {
2905 bind . <$mod-F$n> $script
2906 catch { bind . <$mod-XF86_Switch_VT_$n> $script }
2907 }
2908
2909 # set the focus back to the toplevel for any click outside
2910 # the entry widgets
2911 proc click {w} {
2912 global ctext entries
2913 foreach e [concat $entries $ctext] {
2914 if {$w == $e} return
2915 }
2916 focus .
2917 }
2918
2919 # Adjust the progress bar for a change in requested extent or canvas size
2920 proc adjustprogress {} {
2921 global progresscanv progressitem progresscoords
2922 global fprogitem fprogcoord lastprogupdate progupdatepending
2923 global rprogitem rprogcoord use_ttk
2924
2925 if {$use_ttk} {
2926 $progresscanv configure -value [expr {int($fprogcoord * 100)}]
2927 return
2928 }
2929
2930 set w [expr {[winfo width $progresscanv] - 4}]
2931 set x0 [expr {$w * [lindex $progresscoords 0]}]
2932 set x1 [expr {$w * [lindex $progresscoords 1]}]
2933 set h [winfo height $progresscanv]
2934 $progresscanv coords $progressitem $x0 0 $x1 $h
2935 $progresscanv coords $fprogitem 0 0 [expr {$w * $fprogcoord}] $h
2936 $progresscanv coords $rprogitem 0 0 [expr {$w * $rprogcoord}] $h
2937 set now [clock clicks -milliseconds]
2938 if {$now >= $lastprogupdate + 100} {
2939 set progupdatepending 0
2940 update
2941 } elseif {!$progupdatepending} {
2942 set progupdatepending 1
2943 after [expr {$lastprogupdate + 100 - $now}] doprogupdate
2944 }
2945 }
2946
2947 proc doprogupdate {} {
2948 global lastprogupdate progupdatepending
2949
2950 if {$progupdatepending} {
2951 set progupdatepending 0
2952 set lastprogupdate [clock clicks -milliseconds]
2953 update
2954 }
2955 }
2956
2957 proc config_check_tmp_exists {tries_left} {
2958 global config_file_tmp
2959
2960 if {[file exists $config_file_tmp]} {
2961 incr tries_left -1
2962 if {$tries_left > 0} {
2963 after 100 [list config_check_tmp_exists $tries_left]
2964 } else {
2965 error_popup "There appears to be a stale $config_file_tmp\
2966 file, which will prevent gitk from saving its configuration on exit.\
2967 Please remove it if it is not being used by any existing gitk process."
2968 }
2969 }
2970 }
2971
2972 proc config_init_trace {name} {
2973 global config_variable_changed config_variable_original
2974
2975 upvar #0 $name var
2976 set config_variable_changed($name) 0
2977 set config_variable_original($name) $var
2978 }
2979
2980 proc config_variable_change_cb {name name2 op} {
2981 global config_variable_changed config_variable_original
2982
2983 upvar #0 $name var
2984 if {$op eq "write" &&
2985 (![info exists config_variable_original($name)] ||
2986 $config_variable_original($name) ne $var)} {
2987 set config_variable_changed($name) 1
2988 }
2989 }
2990
2991 proc savestuff {w} {
2992 global stuffsaved
2993 global config_file config_file_tmp
2994 global config_variables config_variable_changed
2995 global viewchanged
2996
2997 upvar #0 viewname current_viewname
2998 upvar #0 viewfiles current_viewfiles
2999 upvar #0 viewargs current_viewargs
3000 upvar #0 viewargscmd current_viewargscmd
3001 upvar #0 viewperm current_viewperm
3002 upvar #0 nextviewnum current_nextviewnum
3003 upvar #0 use_ttk current_use_ttk
3004
3005 if {$stuffsaved} return
3006 if {![winfo viewable .]} return
3007 set remove_tmp 0
3008 if {[catch {
3009 set try_count 0
3010 while {[catch {set f [open $config_file_tmp {WRONLY CREAT EXCL}]}]} {
3011 if {[incr try_count] > 50} {
3012 error "Unable to write config file: $config_file_tmp exists"
3013 }
3014 after 100
3015 }
3016 set remove_tmp 1
3017 if {$::tcl_platform(platform) eq {windows}} {
3018 file attributes $config_file_tmp -hidden true
3019 }
3020 if {[file exists $config_file]} {
3021 source $config_file
3022 }
3023 foreach var_name $config_variables {
3024 upvar #0 $var_name var
3025 upvar 0 $var_name old_var
3026 if {!$config_variable_changed($var_name) && [info exists old_var]} {
3027 puts $f [list set $var_name $old_var]
3028 } else {
3029 puts $f [list set $var_name $var]
3030 }
3031 }
3032
3033 puts $f "set geometry(main) [wm geometry .]"
3034 puts $f "set geometry(state) [wm state .]"
3035 puts $f "set geometry(topwidth) [winfo width .tf]"
3036 puts $f "set geometry(topheight) [winfo height .tf]"
3037 if {$current_use_ttk} {
3038 puts $f "set geometry(pwsash0) \"[.tf.histframe.pwclist sashpos 0] 1\""
3039 puts $f "set geometry(pwsash1) \"[.tf.histframe.pwclist sashpos 1] 1\""
3040 } else {
3041 puts $f "set geometry(pwsash0) \"[.tf.histframe.pwclist sash coord 0]\""
3042 puts $f "set geometry(pwsash1) \"[.tf.histframe.pwclist sash coord 1]\""
3043 }
3044 puts $f "set geometry(botwidth) [winfo width .bleft]"
3045 puts $f "set geometry(botheight) [winfo height .bleft]"
3046
3047 array set view_save {}
3048 array set views {}
3049 if {![info exists permviews]} { set permviews {} }
3050 foreach view $permviews {
3051 set view_save([lindex $view 0]) 1
3052 set views([lindex $view 0]) $view
3053 }
3054 puts -nonewline $f "set permviews {"
3055 for {set v 1} {$v < $current_nextviewnum} {incr v} {
3056 if {$viewchanged($v)} {
3057 if {$current_viewperm($v)} {
3058 set views($current_viewname($v)) [list $current_viewname($v) $current_viewfiles($v) $current_viewargs($v) $current_viewargscmd($v)]
3059 } else {
3060 set view_save($current_viewname($v)) 0
3061 }
3062 }
3063 }
3064 # write old and updated view to their places and append remaining to the end
3065 foreach view $permviews {
3066 set view_name [lindex $view 0]
3067 if {$view_save($view_name)} {
3068 puts $f "{$views($view_name)}"
3069 }
3070 unset views($view_name)
3071 }
3072 foreach view_name [array names views] {
3073 puts $f "{$views($view_name)}"
3074 }
3075 puts $f "}"
3076 close $f
3077 file rename -force $config_file_tmp $config_file
3078 set remove_tmp 0
3079 } err]} {
3080 puts "Error saving config: $err"
3081 }
3082 if {$remove_tmp} {
3083 file delete -force $config_file_tmp
3084 }
3085 set stuffsaved 1
3086 }
3087
3088 proc resizeclistpanes {win w} {
3089 global oldwidth oldsash use_ttk
3090 if {[info exists oldwidth($win)]} {
3091 if {[info exists oldsash($win)]} {
3092 set s0 [lindex $oldsash($win) 0]
3093 set s1 [lindex $oldsash($win) 1]
3094 } elseif {$use_ttk} {
3095 set s0 [$win sashpos 0]
3096 set s1 [$win sashpos 1]
3097 } else {
3098 set s0 [$win sash coord 0]
3099 set s1 [$win sash coord 1]
3100 }
3101 if {$w < 60} {
3102 set sash0 [expr {int($w/2 - 2)}]
3103 set sash1 [expr {int($w*5/6 - 2)}]
3104 } else {
3105 set factor [expr {1.0 * $w / $oldwidth($win)}]
3106 set sash0 [expr {int($factor * [lindex $s0 0])}]
3107 set sash1 [expr {int($factor * [lindex $s1 0])}]
3108 if {$sash0 < 30} {
3109 set sash0 30
3110 }
3111 if {$sash1 < $sash0 + 20} {
3112 set sash1 [expr {$sash0 + 20}]
3113 }
3114 if {$sash1 > $w - 10} {
3115 set sash1 [expr {$w - 10}]
3116 if {$sash0 > $sash1 - 20} {
3117 set sash0 [expr {$sash1 - 20}]
3118 }
3119 }
3120 }
3121 if {$use_ttk} {
3122 $win sashpos 0 $sash0
3123 $win sashpos 1 $sash1
3124 } else {
3125 $win sash place 0 $sash0 [lindex $s0 1]
3126 $win sash place 1 $sash1 [lindex $s1 1]
3127 set sash0 [list $sash0 [lindex $s0 1]]
3128 set sash1 [list $sash1 [lindex $s1 1]]
3129 }
3130 set oldsash($win) [list $sash0 $sash1]
3131 }
3132 set oldwidth($win) $w
3133 }
3134
3135 proc resizecdetpanes {win w} {
3136 global oldwidth oldsash use_ttk
3137 if {[info exists oldwidth($win)]} {
3138 if {[info exists oldsash($win)]} {
3139 set s0 $oldsash($win)
3140 } elseif {$use_ttk} {
3141 set s0 [$win sashpos 0]
3142 } else {
3143 set s0 [$win sash coord 0]
3144 }
3145 if {$w < 60} {
3146 set sash0 [expr {int($w*3/4 - 2)}]
3147 } else {
3148 set factor [expr {1.0 * $w / $oldwidth($win)}]
3149 set sash0 [expr {int($factor * [lindex $s0 0])}]
3150 if {$sash0 < 45} {
3151 set sash0 45
3152 }
3153 if {$sash0 > $w - 15} {
3154 set sash0 [expr {$w - 15}]
3155 }
3156 }
3157 if {$use_ttk} {
3158 $win sashpos 0 $sash0
3159 } else {
3160 $win sash place 0 $sash0 [lindex $s0 1]
3161 set sash0 [list $sash0 [lindex $s0 1]]
3162 }
3163 set oldsash($win) $sash0
3164 }
3165 set oldwidth($win) $w
3166 }
3167
3168 proc allcanvs args {
3169 global canv canv2 canv3
3170 eval $canv $args
3171 eval $canv2 $args
3172 eval $canv3 $args
3173 }
3174
3175 proc bindall {event action} {
3176 global canv canv2 canv3
3177 bind $canv $event $action
3178 bind $canv2 $event $action
3179 bind $canv3 $event $action
3180 }
3181
3182 proc about {} {
3183 global bgcolor NS
3184 set w .about
3185 if {[winfo exists $w]} {
3186 raise $w
3187 return
3188 }
3189 ttk_toplevel $w
3190 wm title $w [mc "About gitk"]
3191 make_transient $w .
3192 message $w.m -text [mc "
3193 Gitk - a commit viewer for git
3194
3195 Copyright \u00a9 2005-2016 Paul Mackerras
3196
3197 Use and redistribute under the terms of the GNU General Public License"] \
3198 -justify center -aspect 400 -border 2 -bg $bgcolor -relief groove
3199 pack $w.m -side top -fill x -padx 2 -pady 2
3200 ${NS}::button $w.ok -text [mc "Close"] -command "destroy $w" -default active
3201 pack $w.ok -side bottom
3202 bind $w <Visibility> "focus $w.ok"
3203 bind $w <Key-Escape> "destroy $w"
3204 bind $w <Key-Return> "destroy $w"
3205 tk::PlaceWindow $w widget .
3206 }
3207
3208 proc keys {} {
3209 global bgcolor NS
3210 set w .keys
3211 if {[winfo exists $w]} {
3212 raise $w
3213 return
3214 }
3215 if {[tk windowingsystem] eq {aqua}} {
3216 set M1T Cmd
3217 } else {
3218 set M1T Ctrl
3219 }
3220 ttk_toplevel $w
3221 wm title $w [mc "Gitk key bindings"]
3222 make_transient $w .
3223 message $w.m -text "
3224 [mc "Gitk key bindings:"]
3225
3226 [mc "<%s-Q> Quit" $M1T]
3227 [mc "<%s-W> Close window" $M1T]
3228 [mc "<Home> Move to first commit"]
3229 [mc "<End> Move to last commit"]
3230 [mc "<Up>, p, k Move up one commit"]
3231 [mc "<Down>, n, j Move down one commit"]
3232 [mc "<Left>, z, h Go back in history list"]
3233 [mc "<Right>, x, l Go forward in history list"]
3234 [mc "<%s-n> Go to n-th parent of current commit in history list" $M1T]
3235 [mc "<PageUp> Move up one page in commit list"]
3236 [mc "<PageDown> Move down one page in commit list"]
3237 [mc "<%s-Home> Scroll to top of commit list" $M1T]
3238 [mc "<%s-End> Scroll to bottom of commit list" $M1T]
3239 [mc "<%s-Up> Scroll commit list up one line" $M1T]
3240 [mc "<%s-Down> Scroll commit list down one line" $M1T]
3241 [mc "<%s-PageUp> Scroll commit list up one page" $M1T]
3242 [mc "<%s-PageDown> Scroll commit list down one page" $M1T]
3243 [mc "<Shift-Up> Find backwards (upwards, later commits)"]
3244 [mc "<Shift-Down> Find forwards (downwards, earlier commits)"]
3245 [mc "<Delete>, b Scroll diff view up one page"]
3246 [mc "<Backspace> Scroll diff view up one page"]
3247 [mc "<Space> Scroll diff view down one page"]
3248 [mc "u Scroll diff view up 18 lines"]
3249 [mc "d Scroll diff view down 18 lines"]
3250 [mc "<%s-F> Find" $M1T]
3251 [mc "<%s-G> Move to next find hit" $M1T]
3252 [mc "<Return> Move to next find hit"]
3253 [mc "g Go to commit"]
3254 [mc "/ Focus the search box"]
3255 [mc "? Move to previous find hit"]
3256 [mc "f Scroll diff view to next file"]
3257 [mc "<%s-S> Search for next hit in diff view" $M1T]
3258 [mc "<%s-R> Search for previous hit in diff view" $M1T]
3259 [mc "<%s-KP+> Increase font size" $M1T]
3260 [mc "<%s-plus> Increase font size" $M1T]
3261 [mc "<%s-KP-> Decrease font size" $M1T]
3262 [mc "<%s-minus> Decrease font size" $M1T]
3263 [mc "<F5> Update"]
3264 " \
3265 -justify left -bg $bgcolor -border 2 -relief groove
3266 pack $w.m -side top -fill both -padx 2 -pady 2
3267 ${NS}::button $w.ok -text [mc "Close"] -command "destroy $w" -default active
3268 bind $w <Key-Escape> [list destroy $w]
3269 pack $w.ok -side bottom
3270 bind $w <Visibility> "focus $w.ok"
3271 bind $w <Key-Escape> "destroy $w"
3272 bind $w <Key-Return> "destroy $w"
3273 }
3274
3275 # Procedures for manipulating the file list window at the
3276 # bottom right of the overall window.
3277
3278 proc treeview {w l openlevs} {
3279 global treecontents treediropen treeheight treeparent treeindex
3280
3281 set ix 0
3282 set treeindex() 0
3283 set lev 0
3284 set prefix {}
3285 set prefixend -1
3286 set prefendstack {}
3287 set htstack {}
3288 set ht 0
3289 set treecontents() {}
3290 $w conf -state normal
3291 foreach f $l {
3292 while {[string range $f 0 $prefixend] ne $prefix} {
3293 if {$lev <= $openlevs} {
3294 $w mark set e:$treeindex($prefix) "end -1c"
3295 $w mark gravity e:$treeindex($prefix) left
3296 }
3297 set treeheight($prefix) $ht
3298 incr ht [lindex $htstack end]
3299 set htstack [lreplace $htstack end end]
3300 set prefixend [lindex $prefendstack end]
3301 set prefendstack [lreplace $prefendstack end end]
3302 set prefix [string range $prefix 0 $prefixend]
3303 incr lev -1
3304 }
3305 set tail [string range $f [expr {$prefixend+1}] end]
3306 while {[set slash [string first "/" $tail]] >= 0} {
3307 lappend htstack $ht
3308 set ht 0
3309 lappend prefendstack $prefixend
3310 incr prefixend [expr {$slash + 1}]
3311 set d [string range $tail 0 $slash]
3312 lappend treecontents($prefix) $d
3313 set oldprefix $prefix
3314 append prefix $d
3315 set treecontents($prefix) {}
3316 set treeindex($prefix) [incr ix]
3317 set treeparent($prefix) $oldprefix
3318 set tail [string range $tail [expr {$slash+1}] end]
3319 if {$lev <= $openlevs} {
3320 set ht 1
3321 set treediropen($prefix) [expr {$lev < $openlevs}]
3322 set bm [expr {$lev == $openlevs? "tri-rt": "tri-dn"}]
3323 $w mark set d:$ix "end -1c"
3324 $w mark gravity d:$ix left
3325 set str "\n"
3326 for {set i 0} {$i < $lev} {incr i} {append str "\t"}
3327 $w insert end $str
3328 $w image create end -align center -image $bm -padx 1 \
3329 -name a:$ix
3330 $w insert end $d [highlight_tag $prefix]
3331 $w mark set s:$ix "end -1c"
3332 $w mark gravity s:$ix left
3333 }
3334 incr lev
3335 }
3336 if {$tail ne {}} {
3337 if {$lev <= $openlevs} {
3338 incr ht
3339 set str "\n"
3340 for {set i 0} {$i < $lev} {incr i} {append str "\t"}
3341 $w insert end $str
3342 $w insert end $tail [highlight_tag $f]
3343 }
3344 lappend treecontents($prefix) $tail
3345 }
3346 }
3347 while {$htstack ne {}} {
3348 set treeheight($prefix) $ht
3349 incr ht [lindex $htstack end]
3350 set htstack [lreplace $htstack end end]
3351 set prefixend [lindex $prefendstack end]
3352 set prefendstack [lreplace $prefendstack end end]
3353 set prefix [string range $prefix 0 $prefixend]
3354 }
3355 $w conf -state disabled
3356 }
3357
3358 proc linetoelt {l} {
3359 global treeheight treecontents
3360
3361 set y 2
3362 set prefix {}
3363 while {1} {
3364 foreach e $treecontents($prefix) {
3365 if {$y == $l} {
3366 return "$prefix$e"
3367 }
3368 set n 1
3369 if {[string index $e end] eq "/"} {
3370 set n $treeheight($prefix$e)
3371 if {$y + $n > $l} {
3372 append prefix $e
3373 incr y
3374 break
3375 }
3376 }
3377 incr y $n
3378 }
3379 }
3380 }
3381
3382 proc highlight_tree {y prefix} {
3383 global treeheight treecontents cflist
3384
3385 foreach e $treecontents($prefix) {
3386 set path $prefix$e
3387 if {[highlight_tag $path] ne {}} {
3388 $cflist tag add bold $y.0 "$y.0 lineend"
3389 }
3390 incr y
3391 if {[string index $e end] eq "/" && $treeheight($path) > 1} {
3392 set y [highlight_tree $y $path]
3393 }
3394 }
3395 return $y
3396 }
3397
3398 proc treeclosedir {w dir} {
3399 global treediropen treeheight treeparent treeindex
3400
3401 set ix $treeindex($dir)
3402 $w conf -state normal
3403 $w delete s:$ix e:$ix
3404 set treediropen($dir) 0
3405 $w image configure a:$ix -image tri-rt
3406 $w conf -state disabled
3407 set n [expr {1 - $treeheight($dir)}]
3408 while {$dir ne {}} {
3409 incr treeheight($dir) $n
3410 set dir $treeparent($dir)
3411 }
3412 }
3413
3414 proc treeopendir {w dir} {
3415 global treediropen treeheight treeparent treecontents treeindex
3416
3417 set ix $treeindex($dir)
3418 $w conf -state normal
3419 $w image configure a:$ix -image tri-dn
3420 $w mark set e:$ix s:$ix
3421 $w mark gravity e:$ix right
3422 set lev 0
3423 set str "\n"
3424 set n [llength $treecontents($dir)]
3425 for {set x $dir} {$x ne {}} {set x $treeparent($x)} {
3426 incr lev
3427 append str "\t"
3428 incr treeheight($x) $n
3429 }
3430 foreach e $treecontents($dir) {
3431 set de $dir$e
3432 if {[string index $e end] eq "/"} {
3433 set iy $treeindex($de)
3434 $w mark set d:$iy e:$ix
3435 $w mark gravity d:$iy left
3436 $w insert e:$ix $str
3437 set treediropen($de) 0
3438 $w image create e:$ix -align center -image tri-rt -padx 1 \
3439 -name a:$iy
3440 $w insert e:$ix $e [highlight_tag $de]
3441 $w mark set s:$iy e:$ix
3442 $w mark gravity s:$iy left
3443 set treeheight($de) 1
3444 } else {
3445 $w insert e:$ix $str
3446 $w insert e:$ix $e [highlight_tag $de]
3447 }
3448 }
3449 $w mark gravity e:$ix right
3450 $w conf -state disabled
3451 set treediropen($dir) 1
3452 set top [lindex [split [$w index @0,0] .] 0]
3453 set ht [$w cget -height]
3454 set l [lindex [split [$w index s:$ix] .] 0]
3455 if {$l < $top} {
3456 $w yview $l.0
3457 } elseif {$l + $n + 1 > $top + $ht} {
3458 set top [expr {$l + $n + 2 - $ht}]
3459 if {$l < $top} {
3460 set top $l
3461 }
3462 $w yview $top.0
3463 }
3464 }
3465
3466 proc treeclick {w x y} {
3467 global treediropen cmitmode ctext cflist cflist_top
3468
3469 if {$cmitmode ne "tree"} return
3470 if {![info exists cflist_top]} return
3471 set l [lindex [split [$w index "@$x,$y"] "."] 0]
3472 $cflist tag remove highlight $cflist_top.0 "$cflist_top.0 lineend"
3473 $cflist tag add highlight $l.0 "$l.0 lineend"
3474 set cflist_top $l
3475 if {$l == 1} {
3476 $ctext yview 1.0
3477 return
3478 }
3479 set e [linetoelt $l]
3480 if {[string index $e end] ne "/"} {
3481 showfile $e
3482 } elseif {$treediropen($e)} {
3483 treeclosedir $w $e
3484 } else {
3485 treeopendir $w $e
3486 }
3487 }
3488
3489 proc setfilelist {id} {
3490 global treefilelist cflist jump_to_here
3491
3492 treeview $cflist $treefilelist($id) 0
3493 if {$jump_to_here ne {}} {
3494 set f [lindex $jump_to_here 0]
3495 if {[lsearch -exact $treefilelist($id) $f] >= 0} {
3496 showfile $f
3497 }
3498 }
3499 }
3500
3501 image create bitmap tri-rt -background black -foreground blue -data {
3502 #define tri-rt_width 13
3503 #define tri-rt_height 13
3504 static unsigned char tri-rt_bits[] = {
3505 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x30, 0x00, 0x70, 0x00, 0xf0, 0x00,
3506 0xf0, 0x01, 0xf0, 0x00, 0x70, 0x00, 0x30, 0x00, 0x10, 0x00, 0x00, 0x00,
3507 0x00, 0x00};
3508 } -maskdata {
3509 #define tri-rt-mask_width 13
3510 #define tri-rt-mask_height 13
3511 static unsigned char tri-rt-mask_bits[] = {
3512 0x08, 0x00, 0x18, 0x00, 0x38, 0x00, 0x78, 0x00, 0xf8, 0x00, 0xf8, 0x01,
3513 0xf8, 0x03, 0xf8, 0x01, 0xf8, 0x00, 0x78, 0x00, 0x38, 0x00, 0x18, 0x00,
3514 0x08, 0x00};
3515 }
3516 image create bitmap tri-dn -background black -foreground blue -data {
3517 #define tri-dn_width 13
3518 #define tri-dn_height 13
3519 static unsigned char tri-dn_bits[] = {
3520 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0x07, 0xf8, 0x03,
3521 0xf0, 0x01, 0xe0, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3522 0x00, 0x00};
3523 } -maskdata {
3524 #define tri-dn-mask_width 13
3525 #define tri-dn-mask_height 13
3526 static unsigned char tri-dn-mask_bits[] = {
3527 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x1f, 0xfe, 0x0f, 0xfc, 0x07,
3528 0xf8, 0x03, 0xf0, 0x01, 0xe0, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00,
3529 0x00, 0x00};
3530 }
3531
3532 image create bitmap reficon-T -background black -foreground yellow -data {
3533 #define tagicon_width 13
3534 #define tagicon_height 9
3535 static unsigned char tagicon_bits[] = {
3536 0x00, 0x00, 0x00, 0x00, 0xf0, 0x07, 0xf8, 0x07,
3537 0xfc, 0x07, 0xf8, 0x07, 0xf0, 0x07, 0x00, 0x00, 0x00, 0x00};
3538 } -maskdata {
3539 #define tagicon-mask_width 13
3540 #define tagicon-mask_height 9
3541 static unsigned char tagicon-mask_bits[] = {
3542 0x00, 0x00, 0xf0, 0x0f, 0xf8, 0x0f, 0xfc, 0x0f,
3543 0xfe, 0x0f, 0xfc, 0x0f, 0xf8, 0x0f, 0xf0, 0x0f, 0x00, 0x00};
3544 }
3545 set rectdata {
3546 #define headicon_width 13
3547 #define headicon_height 9
3548 static unsigned char headicon_bits[] = {
3549 0x00, 0x00, 0x00, 0x00, 0xf8, 0x07, 0xf8, 0x07,
3550 0xf8, 0x07, 0xf8, 0x07, 0xf8, 0x07, 0x00, 0x00, 0x00, 0x00};
3551 }
3552 set rectmask {
3553 #define headicon-mask_width 13
3554 #define headicon-mask_height 9
3555 static unsigned char headicon-mask_bits[] = {
3556 0x00, 0x00, 0xfc, 0x0f, 0xfc, 0x0f, 0xfc, 0x0f,
3557 0xfc, 0x0f, 0xfc, 0x0f, 0xfc, 0x0f, 0xfc, 0x0f, 0x00, 0x00};
3558 }
3559 image create bitmap reficon-H -background black -foreground "#00ff00" \
3560 -data $rectdata -maskdata $rectmask
3561 image create bitmap reficon-R -background black -foreground "#ffddaa" \
3562 -data $rectdata -maskdata $rectmask
3563 image create bitmap reficon-o -background black -foreground "#ddddff" \
3564 -data $rectdata -maskdata $rectmask
3565
3566 proc init_flist {first} {
3567 global cflist cflist_top difffilestart
3568
3569 $cflist conf -state normal
3570 $cflist delete 0.0 end
3571 if {$first ne {}} {
3572 $cflist insert end $first
3573 set cflist_top 1
3574 $cflist tag add highlight 1.0 "1.0 lineend"
3575 } else {
3576 unset -nocomplain cflist_top
3577 }
3578 $cflist conf -state disabled
3579 set difffilestart {}
3580 }
3581
3582 proc highlight_tag {f} {
3583 global highlight_paths
3584
3585 foreach p $highlight_paths {
3586 if {[string match $p $f]} {
3587 return "bold"
3588 }
3589 }
3590 return {}
3591 }
3592
3593 proc highlight_filelist {} {
3594 global cmitmode cflist
3595
3596 $cflist conf -state normal
3597 if {$cmitmode ne "tree"} {
3598 set end [lindex [split [$cflist index end] .] 0]
3599 for {set l 2} {$l < $end} {incr l} {
3600 set line [$cflist get $l.0 "$l.0 lineend"]
3601 if {[highlight_tag $line] ne {}} {
3602 $cflist tag add bold $l.0 "$l.0 lineend"
3603 }
3604 }
3605 } else {
3606 highlight_tree 2 {}
3607 }
3608 $cflist conf -state disabled
3609 }
3610
3611 proc unhighlight_filelist {} {
3612 global cflist
3613
3614 $cflist conf -state normal
3615 $cflist tag remove bold 1.0 end
3616 $cflist conf -state disabled
3617 }
3618
3619 proc add_flist {fl} {
3620 global cflist
3621
3622 $cflist conf -state normal
3623 foreach f $fl {
3624 $cflist insert end "\n"
3625 $cflist insert end $f [highlight_tag $f]
3626 }
3627 $cflist conf -state disabled
3628 }
3629
3630 proc sel_flist {w x y} {
3631 global ctext difffilestart cflist cflist_top cmitmode
3632
3633 if {$cmitmode eq "tree"} return
3634 if {![info exists cflist_top]} return
3635 set l [lindex [split [$w index "@$x,$y"] "."] 0]
3636 $cflist tag remove highlight $cflist_top.0 "$cflist_top.0 lineend"
3637 $cflist tag add highlight $l.0 "$l.0 lineend"
3638 set cflist_top $l
3639 if {$l == 1} {
3640 $ctext yview 1.0
3641 } else {
3642 catch {$ctext yview [lindex $difffilestart [expr {$l - 2}]]}
3643 }
3644 suppress_highlighting_file_for_current_scrollpos
3645 }
3646
3647 proc pop_flist_menu {w X Y x y} {
3648 global ctext cflist cmitmode flist_menu flist_menu_file
3649 global treediffs diffids
3650
3651 stopfinding
3652 set l [lindex [split [$w index "@$x,$y"] "."] 0]
3653 if {$l <= 1} return
3654 if {$cmitmode eq "tree"} {
3655 set e [linetoelt $l]
3656 if {[string index $e end] eq "/"} return
3657 } else {
3658 set e [lindex $treediffs($diffids) [expr {$l-2}]]
3659 }
3660 set flist_menu_file $e
3661 set xdiffstate "normal"
3662 if {$cmitmode eq "tree"} {
3663 set xdiffstate "disabled"
3664 }
3665 # Disable "External diff" item in tree mode
3666 $flist_menu entryconf 2 -state $xdiffstate
3667 tk_popup $flist_menu $X $Y
3668 }
3669
3670 proc find_ctext_fileinfo {line} {
3671 global ctext_file_names ctext_file_lines
3672
3673 set ok [bsearch $ctext_file_lines $line]
3674 set tline [lindex $ctext_file_lines $ok]
3675
3676 if {$ok >= [llength $ctext_file_lines] || $line < $tline} {
3677 return {}
3678 } else {
3679 return [list [lindex $ctext_file_names $ok] $tline]
3680 }
3681 }
3682
3683 proc pop_diff_menu {w X Y x y} {
3684 global ctext diff_menu flist_menu_file
3685 global diff_menu_txtpos diff_menu_line
3686 global diff_menu_filebase
3687
3688 set diff_menu_txtpos [split [$w index "@$x,$y"] "."]
3689 set diff_menu_line [lindex $diff_menu_txtpos 0]
3690 # don't pop up the menu on hunk-separator or file-separator lines
3691 if {[lsearch -glob [$ctext tag names $diff_menu_line.0] "*sep"] >= 0} {
3692 return
3693 }
3694 stopfinding
3695 set f [find_ctext_fileinfo $diff_menu_line]
3696 if {$f eq {}} return
3697 set flist_menu_file [lindex $f 0]
3698 set diff_menu_filebase [lindex $f 1]
3699 tk_popup $diff_menu $X $Y
3700 }
3701
3702 proc flist_hl {only} {
3703 global flist_menu_file findstring gdttype
3704
3705 set x [shellquote $flist_menu_file]
3706 if {$only || $findstring eq {} || $gdttype ne [mc "touching paths:"]} {
3707 set findstring $x
3708 } else {
3709 append findstring " " $x
3710 }
3711 set gdttype [mc "touching paths:"]
3712 }
3713
3714 proc gitknewtmpdir {} {
3715 global diffnum gitktmpdir gitdir env
3716
3717 if {![info exists gitktmpdir]} {
3718 if {[info exists env(GITK_TMPDIR)]} {
3719 set tmpdir $env(GITK_TMPDIR)
3720 } elseif {[info exists env(TMPDIR)]} {
3721 set tmpdir $env(TMPDIR)
3722 } else {
3723 set tmpdir $gitdir
3724 }
3725 set gitktmpformat [file join $tmpdir ".gitk-tmp.XXXXXX"]
3726 if {[catch {set gitktmpdir [exec mktemp -d $gitktmpformat]}]} {
3727 set gitktmpdir [file join $gitdir [format ".gitk-tmp.%s" [pid]]]
3728 }
3729 if {[catch {file mkdir $gitktmpdir} err]} {
3730 error_popup "[mc "Error creating temporary directory %s:" $gitktmpdir] $err"
3731 unset gitktmpdir
3732 return {}
3733 }
3734 set diffnum 0
3735 }
3736 incr diffnum
3737 set diffdir [file join $gitktmpdir $diffnum]
3738 if {[catch {file mkdir $diffdir} err]} {
3739 error_popup "[mc "Error creating temporary directory %s:" $diffdir] $err"
3740 return {}
3741 }
3742 return $diffdir
3743 }
3744
3745 proc save_file_from_commit {filename output what} {
3746 global nullfile
3747
3748 if {[catch {exec git show $filename -- > $output} err]} {
3749 if {[string match "fatal: bad revision *" $err]} {
3750 return $nullfile
3751 }
3752 error_popup "[mc "Error getting \"%s\" from %s:" $filename $what] $err"
3753 return {}
3754 }
3755 return $output
3756 }
3757
3758 proc external_diff_get_one_file {diffid filename diffdir} {
3759 global nullid nullid2 nullfile
3760 global worktree
3761
3762 if {$diffid == $nullid} {
3763 set difffile [file join $worktree $filename]
3764 if {[file exists $difffile]} {
3765 return $difffile
3766 }
3767 return $nullfile
3768 }
3769 if {$diffid == $nullid2} {
3770 set difffile [file join $diffdir "\[index\] [file tail $filename]"]
3771 return [save_file_from_commit :$filename $difffile index]
3772 }
3773 set difffile [file join $diffdir "\[$diffid\] [file tail $filename]"]
3774 return [save_file_from_commit $diffid:$filename $difffile \
3775 "revision $diffid"]
3776 }
3777
3778 proc external_diff {} {
3779 global nullid nullid2
3780 global flist_menu_file
3781 global diffids
3782 global extdifftool
3783
3784 if {[llength $diffids] == 1} {
3785 # no reference commit given
3786 set diffidto [lindex $diffids 0]
3787 if {$diffidto eq $nullid} {
3788 # diffing working copy with index
3789 set diffidfrom $nullid2
3790 } elseif {$diffidto eq $nullid2} {
3791 # diffing index with HEAD
3792 set diffidfrom "HEAD"
3793 } else {
3794 # use first parent commit
3795 global parentlist selectedline
3796 set diffidfrom [lindex $parentlist $selectedline 0]
3797 }
3798 } else {
3799 set diffidfrom [lindex $diffids 0]
3800 set diffidto [lindex $diffids 1]
3801 }
3802
3803 # make sure that several diffs wont collide
3804 set diffdir [gitknewtmpdir]
3805 if {$diffdir eq {}} return
3806
3807 # gather files to diff
3808 set difffromfile [external_diff_get_one_file $diffidfrom $flist_menu_file $diffdir]
3809 set difftofile [external_diff_get_one_file $diffidto $flist_menu_file $diffdir]
3810
3811 if {$difffromfile ne {} && $difftofile ne {}} {
3812 set cmd [list [shellsplit $extdifftool] $difffromfile $difftofile]
3813 if {[catch {set fl [open |$cmd r]} err]} {
3814 file delete -force $diffdir
3815 error_popup "$extdifftool: [mc "command failed:"] $err"
3816 } else {
3817 fconfigure $fl -blocking 0
3818 filerun $fl [list delete_at_eof $fl $diffdir]
3819 }
3820 }
3821 }
3822
3823 proc find_hunk_blamespec {base line} {
3824 global ctext
3825
3826 # Find and parse the hunk header
3827 set s_lix [$ctext search -backwards -regexp ^@@ "$line.0 lineend" $base.0]
3828 if {$s_lix eq {}} return
3829
3830 set s_line [$ctext get $s_lix "$s_lix + 1 lines"]
3831 if {![regexp {^@@@*(( -\d+(,\d+)?)+) \+(\d+)(,\d+)? @@} $s_line \
3832 s_line old_specs osz osz1 new_line nsz]} {
3833 return
3834 }
3835
3836 # base lines for the parents
3837 set base_lines [list $new_line]
3838 foreach old_spec [lrange [split $old_specs " "] 1 end] {
3839 if {![regexp -- {-(\d+)(,\d+)?} $old_spec \
3840 old_spec old_line osz]} {
3841 return
3842 }
3843 lappend base_lines $old_line
3844 }
3845
3846 # Now scan the lines to determine offset within the hunk
3847 set max_parent [expr {[llength $base_lines]-2}]
3848 set dline 0
3849 set s_lno [lindex [split $s_lix "."] 0]
3850
3851 # Determine if the line is removed
3852 set chunk [$ctext get $line.0 "$line.1 + $max_parent chars"]
3853 if {[string match {[-+ ]*} $chunk]} {
3854 set removed_idx [string first "-" $chunk]
3855 # Choose a parent index
3856 if {$removed_idx >= 0} {
3857 set parent $removed_idx
3858 } else {
3859 set unchanged_idx [string first " " $chunk]
3860 if {$unchanged_idx >= 0} {
3861 set parent $unchanged_idx
3862 } else {
3863 # blame the current commit
3864 set parent -1
3865 }
3866 }
3867 # then count other lines that belong to it
3868 for {set i $line} {[incr i -1] > $s_lno} {} {
3869 set chunk [$ctext get $i.0 "$i.1 + $max_parent chars"]
3870 # Determine if the line is removed
3871 set removed_idx [string first "-" $chunk]
3872 if {$parent >= 0} {
3873 set code [string index $chunk $parent]
3874 if {$code eq "-" || ($removed_idx < 0 && $code ne "+")} {
3875 incr dline
3876 }
3877 } else {
3878 if {$removed_idx < 0} {
3879 incr dline
3880 }
3881 }
3882 }
3883 incr parent
3884 } else {
3885 set parent 0
3886 }
3887
3888 incr dline [lindex $base_lines $parent]
3889 return [list $parent $dline]
3890 }
3891
3892 proc external_blame_diff {} {
3893 global currentid cmitmode
3894 global diff_menu_txtpos diff_menu_line
3895 global diff_menu_filebase flist_menu_file
3896
3897 if {$cmitmode eq "tree"} {
3898 set parent_idx 0
3899 set line [expr {$diff_menu_line - $diff_menu_filebase}]
3900 } else {
3901 set hinfo [find_hunk_blamespec $diff_menu_filebase $diff_menu_line]
3902 if {$hinfo ne {}} {
3903 set parent_idx [lindex $hinfo 0]
3904 set line [lindex $hinfo 1]
3905 } else {
3906 set parent_idx 0
3907 set line 0
3908 }
3909 }
3910
3911 external_blame $parent_idx $line
3912 }
3913
3914 # Find the SHA1 ID of the blob for file $fname in the index
3915 # at stage 0 or 2
3916 proc index_sha1 {fname} {
3917 set f [open [list | git ls-files -s $fname] r]
3918 while {[gets $f line] >= 0} {
3919 set info [lindex [split $line "\t"] 0]
3920 set stage [lindex $info 2]
3921 if {$stage eq "0" || $stage eq "2"} {
3922 close $f
3923 return [lindex $info 1]
3924 }
3925 }
3926 close $f
3927 return {}
3928 }
3929
3930 # Turn an absolute path into one relative to the current directory
3931 proc make_relative {f} {
3932 if {[file pathtype $f] eq "relative"} {
3933 return $f
3934 }
3935 set elts [file split $f]
3936 set here [file split [pwd]]
3937 set ei 0
3938 set hi 0
3939 set res {}
3940 foreach d $here {
3941 if {$ei < $hi || $ei >= [llength $elts] || [lindex $elts $ei] ne $d} {
3942 lappend res ".."
3943 } else {
3944 incr ei
3945 }
3946 incr hi
3947 }
3948 set elts [concat $res [lrange $elts $ei end]]
3949 return [eval file join $elts]
3950 }
3951
3952 proc external_blame {parent_idx {line {}}} {
3953 global flist_menu_file cdup
3954 global nullid nullid2
3955 global parentlist selectedline currentid
3956
3957 if {$parent_idx > 0} {
3958 set base_commit [lindex $parentlist $selectedline [expr {$parent_idx-1}]]
3959 } else {
3960 set base_commit $currentid
3961 }
3962
3963 if {$base_commit eq {} || $base_commit eq $nullid || $base_commit eq $nullid2} {
3964 error_popup [mc "No such commit"]
3965 return
3966 }
3967
3968 set cmdline [list git gui blame]
3969 if {$line ne {} && $line > 1} {
3970 lappend cmdline "--line=$line"
3971 }
3972 set f [file join $cdup $flist_menu_file]
3973 # Unfortunately it seems git gui blame doesn't like
3974 # being given an absolute path...
3975 set f [make_relative $f]
3976 lappend cmdline $base_commit $f
3977 if {[catch {eval exec $cmdline &} err]} {
3978 error_popup "[mc "git gui blame: command failed:"] $err"
3979 }
3980 }
3981
3982 proc show_line_source {} {
3983 global cmitmode currentid parents curview blamestuff blameinst
3984 global diff_menu_line diff_menu_filebase flist_menu_file
3985 global nullid nullid2 gitdir cdup
3986
3987 set from_index {}
3988 if {$cmitmode eq "tree"} {
3989 set id $currentid
3990 set line [expr {$diff_menu_line - $diff_menu_filebase}]
3991 } else {
3992 set h [find_hunk_blamespec $diff_menu_filebase $diff_menu_line]
3993 if {$h eq {}} return
3994 set pi [lindex $h 0]
3995 if {$pi == 0} {
3996 mark_ctext_line $diff_menu_line
3997 return
3998 }
3999 incr pi -1
4000 if {$currentid eq $nullid} {
4001 if {$pi > 0} {
4002 # must be a merge in progress...
4003 if {[catch {
4004 # get the last line from .git/MERGE_HEAD
4005 set f [open [file join $gitdir MERGE_HEAD] r]
4006 set id [lindex [split [read $f] "\n"] end-1]
4007 close $f
4008 } err]} {
4009 error_popup [mc "Couldn't read merge head: %s" $err]
4010 return
4011 }
4012 } elseif {$parents($curview,$currentid) eq $nullid2} {
4013 # need to do the blame from the index
4014 if {[catch {
4015 set from_index [index_sha1 $flist_menu_file]
4016 } err]} {
4017 error_popup [mc "Error reading index: %s" $err]
4018 return
4019 }
4020 } else {
4021 set id $parents($curview,$currentid)
4022 }
4023 } else {
4024 set id [lindex $parents($curview,$currentid) $pi]
4025 }
4026 set line [lindex $h 1]
4027 }
4028 set blameargs {}
4029 if {$from_index ne {}} {
4030 lappend blameargs | git cat-file blob $from_index
4031 }
4032 lappend blameargs | git blame -p -L$line,+1
4033 if {$from_index ne {}} {
4034 lappend blameargs --contents -
4035 } else {
4036 lappend blameargs $id
4037 }
4038 lappend blameargs -- [file join $cdup $flist_menu_file]
4039 if {[catch {
4040 set f [open $blameargs r]
4041 } err]} {
4042 error_popup [mc "Couldn't start git blame: %s" $err]
4043 return
4044 }
4045 nowbusy blaming [mc "Searching"]
4046 fconfigure $f -blocking 0
4047 set i [reg_instance $f]
4048 set blamestuff($i) {}
4049 set blameinst $i
4050 filerun $f [list read_line_source $f $i]
4051 }
4052
4053 proc stopblaming {} {
4054 global blameinst
4055
4056 if {[info exists blameinst]} {
4057 stop_instance $blameinst
4058 unset blameinst
4059 notbusy blaming
4060 }
4061 }
4062
4063 proc read_line_source {fd inst} {
4064 global blamestuff curview commfd blameinst nullid nullid2
4065
4066 while {[gets $fd line] >= 0} {
4067 lappend blamestuff($inst) $line
4068 }
4069 if {![eof $fd]} {
4070 return 1
4071 }
4072 unset commfd($inst)
4073 unset blameinst
4074 notbusy blaming
4075 fconfigure $fd -blocking 1
4076 if {[catch {close $fd} err]} {
4077 error_popup [mc "Error running git blame: %s" $err]
4078 return 0
4079 }
4080
4081 set fname {}
4082 set line [split [lindex $blamestuff($inst) 0] " "]
4083 set id [lindex $line 0]
4084 set lnum [lindex $line 1]
4085 if {[string length $id] == 40 && [string is xdigit $id] &&
4086 [string is digit -strict $lnum]} {
4087 # look for "filename" line
4088 foreach l $blamestuff($inst) {
4089 if {[string match "filename *" $l]} {
4090 set fname [string range $l 9 end]
4091 break
4092 }
4093 }
4094 }
4095 if {$fname ne {}} {
4096 # all looks good, select it
4097 if {$id eq $nullid} {
4098 # blame uses all-zeroes to mean not committed,
4099 # which would mean a change in the index
4100 set id $nullid2
4101 }
4102 if {[commitinview $id $curview]} {
4103 selectline [rowofcommit $id] 1 [list $fname $lnum] 1
4104 } else {
4105 error_popup [mc "That line comes from commit %s, \
4106 which is not in this view" [shortids $id]]
4107 }
4108 } else {
4109 puts "oops couldn't parse git blame output"
4110 }
4111 return 0
4112 }
4113
4114 # delete $dir when we see eof on $f (presumably because the child has exited)
4115 proc delete_at_eof {f dir} {
4116 while {[gets $f line] >= 0} {}
4117 if {[eof $f]} {
4118 if {[catch {close $f} err]} {
4119 error_popup "[mc "External diff viewer failed:"] $err"
4120 }
4121 file delete -force $dir
4122 return 0
4123 }
4124 return 1
4125 }
4126
4127 # Functions for adding and removing shell-type quoting
4128
4129 proc shellquote {str} {
4130 if {![string match "*\['\"\\ \t]*" $str]} {
4131 return $str
4132 }
4133 if {![string match "*\['\"\\]*" $str]} {
4134 return "\"$str\""
4135 }
4136 if {![string match "*'*" $str]} {
4137 return "'$str'"
4138 }
4139 return "\"[string map {\" \\\" \\ \\\\} $str]\""
4140 }
4141
4142 proc shellarglist {l} {
4143 set str {}
4144 foreach a $l {
4145 if {$str ne {}} {
4146 append str " "
4147 }
4148 append str [shellquote $a]
4149 }
4150 return $str
4151 }
4152
4153 proc shelldequote {str} {
4154 set ret {}
4155 set used -1
4156 while {1} {
4157 incr used
4158 if {![regexp -start $used -indices "\['\"\\\\ \t]" $str first]} {
4159 append ret [string range $str $used end]
4160 set used [string length $str]
4161 break
4162 }
4163 set first [lindex $first 0]
4164 set ch [string index $str $first]
4165 if {$first > $used} {
4166 append ret [string range $str $used [expr {$first - 1}]]
4167 set used $first
4168 }
4169 if {$ch eq " " || $ch eq "\t"} break
4170 incr used
4171 if {$ch eq "'"} {
4172 set first [string first "'" $str $used]
4173 if {$first < 0} {
4174 error "unmatched single-quote"
4175 }
4176 append ret [string range $str $used [expr {$first - 1}]]
4177 set used $first
4178 continue
4179 }
4180 if {$ch eq "\\"} {
4181 if {$used >= [string length $str]} {
4182 error "trailing backslash"
4183 }
4184 append ret [string index $str $used]
4185 continue
4186 }
4187 # here ch == "\""
4188 while {1} {
4189 if {![regexp -start $used -indices "\[\"\\\\]" $str first]} {
4190 error "unmatched double-quote"
4191 }
4192 set first [lindex $first 0]
4193 set ch [string index $str $first]
4194 if {$first > $used} {
4195 append ret [string range $str $used [expr {$first - 1}]]
4196 set used $first
4197 }
4198 if {$ch eq "\""} break
4199 incr used
4200 append ret [string index $str $used]
4201 incr used
4202 }
4203 }
4204 return [list $used $ret]
4205 }
4206
4207 proc shellsplit {str} {
4208 set l {}
4209 while {1} {
4210 set str [string trimleft $str]
4211 if {$str eq {}} break
4212 set dq [shelldequote $str]
4213 set n [lindex $dq 0]
4214 set word [lindex $dq 1]
4215 set str [string range $str $n end]
4216 lappend l $word
4217 }
4218 return $l
4219 }
4220
4221 proc set_window_title {} {
4222 global appname curview viewname vrevs
4223 set rev [mc "All files"]
4224 if {$curview ne 0} {
4225 if {$viewname($curview) eq [mc "Command line"]} {
4226 set rev [string map {"--gitk-symmetric-diff-marker" "--merge"} $vrevs($curview)]
4227 } else {
4228 set rev $viewname($curview)
4229 }
4230 }
4231 wm title . "[reponame]: $rev - $appname"
4232 }
4233
4234 # Code to implement multiple views
4235
4236 proc newview {ishighlight} {
4237 global nextviewnum newviewname newishighlight
4238 global revtreeargs viewargscmd newviewopts curview
4239
4240 set newishighlight $ishighlight
4241 set top .gitkview
4242 if {[winfo exists $top]} {
4243 raise $top
4244 return
4245 }
4246 decode_view_opts $nextviewnum $revtreeargs
4247 set newviewname($nextviewnum) "[mc "View"] $nextviewnum"
4248 set newviewopts($nextviewnum,perm) 0
4249 set newviewopts($nextviewnum,cmd) $viewargscmd($curview)
4250 vieweditor $top $nextviewnum [mc "Gitk view definition"]
4251 }
4252
4253 set known_view_options {
4254 {perm b . {} {mc "Remember this view"}}
4255 {reflabel l + {} {mc "References (space separated list):"}}
4256 {refs t15 .. {} {mc "Branches & tags:"}}
4257 {allrefs b *. "--all" {mc "All refs"}}
4258 {branches b . "--branches" {mc "All (local) branches"}}
4259 {tags b . "--tags" {mc "All tags"}}
4260 {remotes b . "--remotes" {mc "All remote-tracking branches"}}
4261 {commitlbl l + {} {mc "Commit Info (regular expressions):"}}
4262 {author t15 .. "--author=*" {mc "Author:"}}
4263 {committer t15 . "--committer=*" {mc "Committer:"}}
4264 {loginfo t15 .. "--grep=*" {mc "Commit Message:"}}
4265 {allmatch b .. "--all-match" {mc "Matches all Commit Info criteria"}}
4266 {igrep b .. "--invert-grep" {mc "Matches no Commit Info criteria"}}
4267 {changes_l l + {} {mc "Changes to Files:"}}
4268 {pickaxe_s r0 . {} {mc "Fixed String"}}
4269 {pickaxe_t r1 . "--pickaxe-regex" {mc "Regular Expression"}}
4270 {pickaxe t15 .. "-S*" {mc "Search string:"}}
4271 {datelabel l + {} {mc "Commit Dates (\"2 weeks ago\", \"2009-03-17 15:27:38\", \"March 17, 2009 15:27:38\"):"}}
4272 {since t15 .. {"--since=*" "--after=*"} {mc "Since:"}}
4273 {until t15 . {"--until=*" "--before=*"} {mc "Until:"}}
4274 {limit_lbl l + {} {mc "Limit and/or skip a number of revisions (positive integer):"}}
4275 {limit t10 *. "--max-count=*" {mc "Number to show:"}}
4276 {skip t10 . "--skip=*" {mc "Number to skip:"}}
4277 {misc_lbl l + {} {mc "Miscellaneous options:"}}
4278 {dorder b *. {"--date-order" "-d"} {mc "Strictly sort by date"}}
4279 {lright b . "--left-right" {mc "Mark branch sides"}}
4280 {first b . "--first-parent" {mc "Limit to first parent"}}
4281 {smplhst b . "--simplify-by-decoration" {mc "Simple history"}}
4282 {args t50 *. {} {mc "Additional arguments to git log:"}}
4283 {allpaths path + {} {mc "Enter files and directories to include, one per line:"}}
4284 {cmd t50= + {} {mc "Command to generate more commits to include:"}}
4285 }
4286
4287 # Convert $newviewopts($n, ...) into args for git log.
4288 proc encode_view_opts {n} {
4289 global known_view_options newviewopts
4290
4291 set rargs [list]
4292 foreach opt $known_view_options {
4293 set patterns [lindex $opt 3]
4294 if {$patterns eq {}} continue
4295 set pattern [lindex $patterns 0]
4296
4297 if {[lindex $opt 1] eq "b"} {
4298 set val $newviewopts($n,[lindex $opt 0])
4299 if {$val} {
4300 lappend rargs $pattern
4301 }
4302 } elseif {[regexp {^r(\d+)$} [lindex $opt 1] type value]} {
4303 regexp {^(.*_)} [lindex $opt 0] uselessvar button_id
4304 set val $newviewopts($n,$button_id)
4305 if {$val eq $value} {
4306 lappend rargs $pattern
4307 }
4308 } else {
4309 set val $newviewopts($n,[lindex $opt 0])
4310 set val [string trim $val]
4311 if {$val ne {}} {
4312 set pfix [string range $pattern 0 end-1]
4313 lappend rargs $pfix$val
4314 }
4315 }
4316 }
4317 set rargs [concat $rargs [shellsplit $newviewopts($n,refs)]]
4318 return [concat $rargs [shellsplit $newviewopts($n,args)]]
4319 }
4320
4321 # Fill $newviewopts($n, ...) based on args for git log.
4322 proc decode_view_opts {n view_args} {
4323 global known_view_options newviewopts
4324
4325 foreach opt $known_view_options {
4326 set id [lindex $opt 0]
4327 if {[lindex $opt 1] eq "b"} {
4328 # Checkboxes
4329 set val 0
4330 } elseif {[regexp {^r(\d+)$} [lindex $opt 1]]} {
4331 # Radiobuttons
4332 regexp {^(.*_)} $id uselessvar id
4333 set val 0
4334 } else {
4335 # Text fields
4336 set val {}
4337 }
4338 set newviewopts($n,$id) $val
4339 }
4340 set oargs [list]
4341 set refargs [list]
4342 foreach arg $view_args {
4343 if {[regexp -- {^-([0-9]+)$} $arg arg cnt]
4344 && ![info exists found(limit)]} {
4345 set newviewopts($n,limit) $cnt
4346 set found(limit) 1
4347 continue
4348 }
4349 catch { unset val }
4350 foreach opt $known_view_options {
4351 set id [lindex $opt 0]
4352 if {[info exists found($id)]} continue
4353 foreach pattern [lindex $opt 3] {
4354 if {![string match $pattern $arg]} continue
4355 if {[lindex $opt 1] eq "b"} {
4356 # Check buttons
4357 set val 1
4358 } elseif {[regexp {^r(\d+)$} [lindex $opt 1] match num]} {
4359 # Radio buttons
4360 regexp {^(.*_)} $id uselessvar id
4361 set val $num
4362 } else {
4363 # Text input fields
4364 set size [string length $pattern]
4365 set val [string range $arg [expr {$size-1}] end]
4366 }
4367 set newviewopts($n,$id) $val
4368 set found($id) 1
4369 break
4370 }
4371 if {[info exists val]} break
4372 }
4373 if {[info exists val]} continue
4374 if {[regexp {^-} $arg]} {
4375 lappend oargs $arg
4376 } else {
4377 lappend refargs $arg
4378 }
4379 }
4380 set newviewopts($n,refs) [shellarglist $refargs]
4381 set newviewopts($n,args) [shellarglist $oargs]
4382 }
4383
4384 proc edit_or_newview {} {
4385 global curview
4386
4387 if {$curview > 0} {
4388 editview
4389 } else {
4390 newview 0
4391 }
4392 }
4393
4394 proc editview {} {
4395 global curview
4396 global viewname viewperm newviewname newviewopts
4397 global viewargs viewargscmd
4398
4399 set top .gitkvedit-$curview
4400 if {[winfo exists $top]} {
4401 raise $top
4402 return
4403 }
4404 decode_view_opts $curview $viewargs($curview)
4405 set newviewname($curview) $viewname($curview)
4406 set newviewopts($curview,perm) $viewperm($curview)
4407 set newviewopts($curview,cmd) $viewargscmd($curview)
4408 vieweditor $top $curview "[mc "Gitk: edit view"] $viewname($curview)"
4409 }
4410
4411 proc vieweditor {top n title} {
4412 global newviewname newviewopts viewfiles bgcolor
4413 global known_view_options NS
4414
4415 ttk_toplevel $top
4416 wm title $top [concat $title [mc "-- criteria for selecting revisions"]]
4417 make_transient $top .
4418
4419 # View name
4420 ${NS}::frame $top.nfr
4421 ${NS}::label $top.nl -text [mc "View Name"]
4422 ${NS}::entry $top.name -width 20 -textvariable newviewname($n)
4423 pack $top.nfr -in $top -fill x -pady 5 -padx 3
4424 pack $top.nl -in $top.nfr -side left -padx {0 5}
4425 pack $top.name -in $top.nfr -side left -padx {0 25}
4426
4427 # View options
4428 set cframe $top.nfr
4429 set cexpand 0
4430 set cnt 0
4431 foreach opt $known_view_options {
4432 set id [lindex $opt 0]
4433 set type [lindex $opt 1]
4434 set flags [lindex $opt 2]
4435 set title [eval [lindex $opt 4]]
4436 set lxpad 0
4437
4438 if {$flags eq "+" || $flags eq "*"} {
4439 set cframe $top.fr$cnt
4440 incr cnt
4441 ${NS}::frame $cframe
4442 pack $cframe -in $top -fill x -pady 3 -padx 3
4443 set cexpand [expr {$flags eq "*"}]
4444 } elseif {$flags eq ".." || $flags eq "*."} {
4445 set cframe $top.fr$cnt
4446 incr cnt
4447 ${NS}::frame $cframe
4448 pack $cframe -in $top -fill x -pady 3 -padx [list 15 3]
4449 set cexpand [expr {$flags eq "*."}]
4450 } else {
4451 set lxpad 5
4452 }
4453
4454 if {$type eq "l"} {
4455 ${NS}::label $cframe.l_$id -text $title
4456 pack $cframe.l_$id -in $cframe -side left -pady [list 3 0] -anchor w
4457 } elseif {$type eq "b"} {
4458 ${NS}::checkbutton $cframe.c_$id -text $title -variable newviewopts($n,$id)
4459 pack $cframe.c_$id -in $cframe -side left \
4460 -padx [list $lxpad 0] -expand $cexpand -anchor w
4461 } elseif {[regexp {^r(\d+)$} $type type sz]} {
4462 regexp {^(.*_)} $id uselessvar button_id
4463 ${NS}::radiobutton $cframe.c_$id -text $title -variable newviewopts($n,$button_id) -value $sz
4464 pack $cframe.c_$id -in $cframe -side left \
4465 -padx [list $lxpad 0] -expand $cexpand -anchor w
4466 } elseif {[regexp {^t(\d+)$} $type type sz]} {
4467 ${NS}::label $cframe.l_$id -text $title
4468 ${NS}::entry $cframe.e_$id -width $sz -background $bgcolor \
4469 -textvariable newviewopts($n,$id)
4470 pack $cframe.l_$id -in $cframe -side left -padx [list $lxpad 0]
4471 pack $cframe.e_$id -in $cframe -side left -expand 1 -fill x
4472 } elseif {[regexp {^t(\d+)=$} $type type sz]} {
4473 ${NS}::label $cframe.l_$id -text $title
4474 ${NS}::entry $cframe.e_$id -width $sz -background $bgcolor \
4475 -textvariable newviewopts($n,$id)
4476 pack $cframe.l_$id -in $cframe -side top -pady [list 3 0] -anchor w
4477 pack $cframe.e_$id -in $cframe -side top -fill x
4478 } elseif {$type eq "path"} {
4479 ${NS}::label $top.l -text $title
4480 pack $top.l -in $top -side top -pady [list 3 0] -anchor w -padx 3
4481 text $top.t -width 40 -height 5 -background $bgcolor
4482 if {[info exists viewfiles($n)]} {
4483 foreach f $viewfiles($n) {
4484 $top.t insert end $f
4485 $top.t insert end "\n"
4486 }
4487 $top.t delete {end - 1c} end
4488 $top.t mark set insert 0.0
4489 }
4490 pack $top.t -in $top -side top -pady [list 0 5] -fill both -expand 1 -padx 3
4491 }
4492 }
4493
4494 ${NS}::frame $top.buts
4495 ${NS}::button $top.buts.ok -text [mc "OK"] -command [list newviewok $top $n]
4496 ${NS}::button $top.buts.apply -text [mc "Apply (F5)"] -command [list newviewok $top $n 1]
4497 ${NS}::button $top.buts.can -text [mc "Cancel"] -command [list destroy $top]
4498 bind $top <Control-Return> [list newviewok $top $n]
4499 bind $top <F5> [list newviewok $top $n 1]
4500 bind $top <Escape> [list destroy $top]
4501 grid $top.buts.ok $top.buts.apply $top.buts.can
4502 grid columnconfigure $top.buts 0 -weight 1 -uniform a
4503 grid columnconfigure $top.buts 1 -weight 1 -uniform a
4504 grid columnconfigure $top.buts 2 -weight 1 -uniform a
4505 pack $top.buts -in $top -side top -fill x
4506 focus $top.t
4507 }
4508
4509 proc doviewmenu {m first cmd op argv} {
4510 set nmenu [$m index end]
4511 for {set i $first} {$i <= $nmenu} {incr i} {
4512 if {[$m entrycget $i -command] eq $cmd} {
4513 eval $m $op $i $argv
4514 break
4515 }
4516 }
4517 }
4518
4519 proc allviewmenus {n op args} {
4520 # global viewhlmenu
4521
4522 doviewmenu .bar.view 5 [list showview $n] $op $args
4523 # doviewmenu $viewhlmenu 1 [list addvhighlight $n] $op $args
4524 }
4525
4526 proc newviewok {top n {apply 0}} {
4527 global nextviewnum newviewperm newviewname newishighlight
4528 global viewname viewfiles viewperm viewchanged selectedview curview
4529 global viewargs viewargscmd newviewopts viewhlmenu
4530
4531 if {[catch {
4532 set newargs [encode_view_opts $n]
4533 } err]} {
4534 error_popup "[mc "Error in commit selection arguments:"] $err" $top
4535 return
4536 }
4537 set files {}
4538 foreach f [split [$top.t get 0.0 end] "\n"] {
4539 set ft [string trim $f]
4540 if {$ft ne {}} {
4541 lappend files $ft
4542 }
4543 }
4544 if {![info exists viewfiles($n)]} {
4545 # creating a new view
4546 incr nextviewnum
4547 set viewname($n) $newviewname($n)
4548 set viewperm($n) $newviewopts($n,perm)
4549 set viewchanged($n) 1
4550 set viewfiles($n) $files
4551 set viewargs($n) $newargs
4552 set viewargscmd($n) $newviewopts($n,cmd)
4553 addviewmenu $n
4554 if {!$newishighlight} {
4555 run showview $n
4556 } else {
4557 run addvhighlight $n
4558 }
4559 } else {
4560 # editing an existing view
4561 set viewperm($n) $newviewopts($n,perm)
4562 set viewchanged($n) 1
4563 if {$newviewname($n) ne $viewname($n)} {
4564 set viewname($n) $newviewname($n)
4565 doviewmenu .bar.view 5 [list showview $n] \
4566 entryconf [list -label $viewname($n)]
4567 # doviewmenu $viewhlmenu 1 [list addvhighlight $n] \
4568 # entryconf [list -label $viewname($n) -value $viewname($n)]
4569 }
4570 if {$files ne $viewfiles($n) || $newargs ne $viewargs($n) || \
4571 $newviewopts($n,cmd) ne $viewargscmd($n)} {
4572 set viewfiles($n) $files
4573 set viewargs($n) $newargs
4574 set viewargscmd($n) $newviewopts($n,cmd)
4575 if {$curview == $n} {
4576 run reloadcommits
4577 }
4578 }
4579 }
4580 if {$apply} return
4581 catch {destroy $top}
4582 }
4583
4584 proc delview {} {
4585 global curview viewperm hlview selectedhlview viewchanged
4586
4587 if {$curview == 0} return
4588 if {[info exists hlview] && $hlview == $curview} {
4589 set selectedhlview [mc "None"]
4590 unset hlview
4591 }
4592 allviewmenus $curview delete
4593 set viewperm($curview) 0
4594 set viewchanged($curview) 1
4595 showview 0
4596 }
4597
4598 proc addviewmenu {n} {
4599 global viewname viewhlmenu
4600
4601 .bar.view add radiobutton -label $viewname($n) \
4602 -command [list showview $n] -variable selectedview -value $n
4603 #$viewhlmenu add radiobutton -label $viewname($n) \
4604 # -command [list addvhighlight $n] -variable selectedhlview
4605 }
4606
4607 proc showview {n} {
4608 global curview cached_commitrow ordertok
4609 global displayorder parentlist rowidlist rowisopt rowfinal
4610 global colormap rowtextx nextcolor canvxmax
4611 global numcommits viewcomplete
4612 global selectedline currentid canv canvy0
4613 global treediffs
4614 global pending_select mainheadid
4615 global commitidx
4616 global selectedview
4617 global hlview selectedhlview commitinterest
4618
4619 if {$n == $curview} return
4620 set selid {}
4621 set ymax [lindex [$canv cget -scrollregion] 3]
4622 set span [$canv yview]
4623 set ytop [expr {[lindex $span 0] * $ymax}]
4624 set ybot [expr {[lindex $span 1] * $ymax}]
4625 set yscreen [expr {($ybot - $ytop) / 2}]
4626 if {$selectedline ne {}} {
4627 set selid $currentid
4628 set y [yc $selectedline]
4629 if {$ytop < $y && $y < $ybot} {
4630 set yscreen [expr {$y - $ytop}]
4631 }
4632 } elseif {[info exists pending_select]} {
4633 set selid $pending_select
4634 unset pending_select
4635 }
4636 unselectline
4637 normalline
4638 unset -nocomplain treediffs
4639 clear_display
4640 if {[info exists hlview] && $hlview == $n} {
4641 unset hlview
4642 set selectedhlview [mc "None"]
4643 }
4644 unset -nocomplain commitinterest
4645 unset -nocomplain cached_commitrow
4646 unset -nocomplain ordertok
4647
4648 set curview $n
4649 set selectedview $n
4650 .bar.view entryconf [mca "&Edit view..."] -state [expr {$n == 0? "disabled": "normal"}]
4651 .bar.view entryconf [mca "&Delete view"] -state [expr {$n == 0? "disabled": "normal"}]
4652
4653 run refill_reflist
4654 if {![info exists viewcomplete($n)]} {
4655 getcommits $selid
4656 return
4657 }
4658
4659 set displayorder {}
4660 set parentlist {}
4661 set rowidlist {}
4662 set rowisopt {}
4663 set rowfinal {}
4664 set numcommits $commitidx($n)
4665
4666 unset -nocomplain colormap
4667 unset -nocomplain rowtextx
4668 set nextcolor 0
4669 set canvxmax [$canv cget -width]
4670 set curview $n
4671 set row 0
4672 setcanvscroll
4673 set yf 0
4674 set row {}
4675 if {$selid ne {} && [commitinview $selid $n]} {
4676 set row [rowofcommit $selid]
4677 # try to get the selected row in the same position on the screen
4678 set ymax [lindex [$canv cget -scrollregion] 3]
4679 set ytop [expr {[yc $row] - $yscreen}]
4680 if {$ytop < 0} {
4681 set ytop 0
4682 }
4683 set yf [expr {$ytop * 1.0 / $ymax}]
4684 }
4685 allcanvs yview moveto $yf
4686 drawvisible
4687 if {$row ne {}} {
4688 selectline $row 0
4689 } elseif {!$viewcomplete($n)} {
4690 reset_pending_select $selid
4691 } else {
4692 reset_pending_select {}
4693
4694 if {[commitinview $pending_select $curview]} {
4695 selectline [rowofcommit $pending_select] 1
4696 } else {
4697 set row [first_real_row]
4698 if {$row < $numcommits} {
4699 selectline $row 0
4700 }
4701 }
4702 }
4703 if {!$viewcomplete($n)} {
4704 if {$numcommits == 0} {
4705 show_status [mc "Reading commits..."]
4706 }
4707 } elseif {$numcommits == 0} {
4708 show_status [mc "No commits selected"]
4709 }
4710 set_window_title
4711 }
4712
4713 # Stuff relating to the highlighting facility
4714
4715 proc ishighlighted {id} {
4716 global vhighlights fhighlights nhighlights rhighlights
4717
4718 if {[info exists nhighlights($id)] && $nhighlights($id) > 0} {
4719 return $nhighlights($id)
4720 }
4721 if {[info exists vhighlights($id)] && $vhighlights($id) > 0} {
4722 return $vhighlights($id)
4723 }
4724 if {[info exists fhighlights($id)] && $fhighlights($id) > 0} {
4725 return $fhighlights($id)
4726 }
4727 if {[info exists rhighlights($id)] && $rhighlights($id) > 0} {
4728 return $rhighlights($id)
4729 }
4730 return 0
4731 }
4732
4733 proc bolden {id font} {
4734 global canv linehtag currentid boldids need_redisplay markedid
4735
4736 # need_redisplay = 1 means the display is stale and about to be redrawn
4737 if {$need_redisplay} return
4738 lappend boldids $id
4739 $canv itemconf $linehtag($id) -font $font
4740 if {[info exists currentid] && $id eq $currentid} {
4741 $canv delete secsel
4742 set t [eval $canv create rect [$canv bbox $linehtag($id)] \
4743 -outline {{}} -tags secsel \
4744 -fill [$canv cget -selectbackground]]
4745 $canv lower $t
4746 }
4747 if {[info exists markedid] && $id eq $markedid} {
4748 make_idmark $id
4749 }
4750 }
4751
4752 proc bolden_name {id font} {
4753 global canv2 linentag currentid boldnameids need_redisplay
4754
4755 if {$need_redisplay} return
4756 lappend boldnameids $id
4757 $canv2 itemconf $linentag($id) -font $font
4758 if {[info exists currentid] && $id eq $currentid} {
4759 $canv2 delete secsel
4760 set t [eval $canv2 create rect [$canv2 bbox $linentag($id)] \
4761 -outline {{}} -tags secsel \
4762 -fill [$canv2 cget -selectbackground]]
4763 $canv2 lower $t
4764 }
4765 }
4766
4767 proc unbolden {} {
4768 global boldids
4769
4770 set stillbold {}
4771 foreach id $boldids {
4772 if {![ishighlighted $id]} {
4773 bolden $id mainfont
4774 } else {
4775 lappend stillbold $id
4776 }
4777 }
4778 set boldids $stillbold
4779 }
4780
4781 proc addvhighlight {n} {
4782 global hlview viewcomplete curview vhl_done commitidx
4783
4784 if {[info exists hlview]} {
4785 delvhighlight
4786 }
4787 set hlview $n
4788 if {$n != $curview && ![info exists viewcomplete($n)]} {
4789 start_rev_list $n
4790 }
4791 set vhl_done $commitidx($hlview)
4792 if {$vhl_done > 0} {
4793 drawvisible
4794 }
4795 }
4796
4797 proc delvhighlight {} {
4798 global hlview vhighlights
4799
4800 if {![info exists hlview]} return
4801 unset hlview
4802 unset -nocomplain vhighlights
4803 unbolden
4804 }
4805
4806 proc vhighlightmore {} {
4807 global hlview vhl_done commitidx vhighlights curview
4808
4809 set max $commitidx($hlview)
4810 set vr [visiblerows]
4811 set r0 [lindex $vr 0]
4812 set r1 [lindex $vr 1]
4813 for {set i $vhl_done} {$i < $max} {incr i} {
4814 set id [commitonrow $i $hlview]
4815 if {[commitinview $id $curview]} {
4816 set row [rowofcommit $id]
4817 if {$r0 <= $row && $row <= $r1} {
4818 if {![highlighted $row]} {
4819 bolden $id mainfontbold
4820 }
4821 set vhighlights($id) 1
4822 }
4823 }
4824 }
4825 set vhl_done $max
4826 return 0
4827 }
4828
4829 proc askvhighlight {row id} {
4830 global hlview vhighlights iddrawn
4831
4832 if {[commitinview $id $hlview]} {
4833 if {[info exists iddrawn($id)] && ![ishighlighted $id]} {
4834 bolden $id mainfontbold
4835 }
4836 set vhighlights($id) 1
4837 } else {
4838 set vhighlights($id) 0
4839 }
4840 }
4841
4842 proc hfiles_change {} {
4843 global highlight_files filehighlight fhighlights fh_serial
4844 global highlight_paths
4845
4846 if {[info exists filehighlight]} {
4847 # delete previous highlights
4848 catch {close $filehighlight}
4849 unset filehighlight
4850 unset -nocomplain fhighlights
4851 unbolden
4852 unhighlight_filelist
4853 }
4854 set highlight_paths {}
4855 after cancel do_file_hl $fh_serial
4856 incr fh_serial
4857 if {$highlight_files ne {}} {
4858 after 300 do_file_hl $fh_serial
4859 }
4860 }
4861
4862 proc gdttype_change {name ix op} {
4863 global gdttype highlight_files findstring findpattern
4864
4865 stopfinding
4866 if {$findstring ne {}} {
4867 if {$gdttype eq [mc "containing:"]} {
4868 if {$highlight_files ne {}} {
4869 set highlight_files {}
4870 hfiles_change
4871 }
4872 findcom_change
4873 } else {
4874 if {$findpattern ne {}} {
4875 set findpattern {}
4876 findcom_change
4877 }
4878 set highlight_files $findstring
4879 hfiles_change
4880 }
4881 drawvisible
4882 }
4883 # enable/disable findtype/findloc menus too
4884 }
4885
4886 proc find_change {name ix op} {
4887 global gdttype findstring highlight_files
4888
4889 stopfinding
4890 if {$gdttype eq [mc "containing:"]} {
4891 findcom_change
4892 } else {
4893 if {$highlight_files ne $findstring} {
4894 set highlight_files $findstring
4895 hfiles_change
4896 }
4897 }
4898 drawvisible
4899 }
4900
4901 proc findcom_change args {
4902 global nhighlights boldnameids
4903 global findpattern findtype findstring gdttype
4904
4905 stopfinding
4906 # delete previous highlights, if any
4907 foreach id $boldnameids {
4908 bolden_name $id mainfont
4909 }
4910 set boldnameids {}
4911 unset -nocomplain nhighlights
4912 unbolden
4913 unmarkmatches
4914 if {$gdttype ne [mc "containing:"] || $findstring eq {}} {
4915 set findpattern {}
4916 } elseif {$findtype eq [mc "Regexp"]} {
4917 set findpattern $findstring
4918 } else {
4919 set e [string map {"*" "\\*" "?" "\\?" "\[" "\\\[" "\\" "\\\\"} \
4920 $findstring]
4921 set findpattern "*$e*"
4922 }
4923 }
4924
4925 proc makepatterns {l} {
4926 set ret {}
4927 foreach e $l {
4928 set ee [string map {"*" "\\*" "?" "\\?" "\[" "\\\[" "\\" "\\\\"} $e]
4929 if {[string index $ee end] eq "/"} {
4930 lappend ret "$ee*"
4931 } else {
4932 lappend ret $ee
4933 lappend ret "$ee/*"
4934 }
4935 }
4936 return $ret
4937 }
4938
4939 proc do_file_hl {serial} {
4940 global highlight_files filehighlight highlight_paths gdttype fhl_list
4941 global cdup findtype
4942
4943 if {$gdttype eq [mc "touching paths:"]} {
4944 # If "exact" match then convert backslashes to forward slashes.
4945 # Most useful to support Windows-flavoured file paths.
4946 if {$findtype eq [mc "Exact"]} {
4947 set highlight_files [string map {"\\" "/"} $highlight_files]
4948 }
4949 if {[catch {set paths [shellsplit $highlight_files]}]} return
4950 set highlight_paths [makepatterns $paths]
4951 highlight_filelist
4952 set relative_paths {}
4953 foreach path $paths {
4954 lappend relative_paths [file join $cdup $path]
4955 }
4956 set gdtargs [concat -- $relative_paths]
4957 } elseif {$gdttype eq [mc "adding/removing string:"]} {
4958 set gdtargs [list "-S$highlight_files"]
4959 } elseif {$gdttype eq [mc "changing lines matching:"]} {
4960 set gdtargs [list "-G$highlight_files"]
4961 } else {
4962 # must be "containing:", i.e. we're searching commit info
4963 return
4964 }
4965 set cmd [concat | git diff-tree -r -s --stdin $gdtargs]
4966 set filehighlight [open $cmd r+]
4967 fconfigure $filehighlight -blocking 0
4968 filerun $filehighlight readfhighlight
4969 set fhl_list {}
4970 drawvisible
4971 flushhighlights
4972 }
4973
4974 proc flushhighlights {} {
4975 global filehighlight fhl_list
4976
4977 if {[info exists filehighlight]} {
4978 lappend fhl_list {}
4979 puts $filehighlight ""
4980 flush $filehighlight
4981 }
4982 }
4983
4984 proc askfilehighlight {row id} {
4985 global filehighlight fhighlights fhl_list
4986
4987 lappend fhl_list $id
4988 set fhighlights($id) -1
4989 puts $filehighlight $id
4990 }
4991
4992 proc readfhighlight {} {
4993 global filehighlight fhighlights curview iddrawn
4994 global fhl_list find_dirn
4995
4996 if {![info exists filehighlight]} {
4997 return 0
4998 }
4999 set nr 0
5000 while {[incr nr] <= 100 && [gets $filehighlight line] >= 0} {
5001 set line [string trim $line]
5002 set i [lsearch -exact $fhl_list $line]
5003 if {$i < 0} continue
5004 for {set j 0} {$j < $i} {incr j} {
5005 set id [lindex $fhl_list $j]
5006 set fhighlights($id) 0
5007 }
5008 set fhl_list [lrange $fhl_list [expr {$i+1}] end]
5009 if {$line eq {}} continue
5010 if {![commitinview $line $curview]} continue
5011 if {[info exists iddrawn($line)] && ![ishighlighted $line]} {
5012 bolden $line mainfontbold
5013 }
5014 set fhighlights($line) 1
5015 }
5016 if {[eof $filehighlight]} {
5017 # strange...
5018 puts "oops, git diff-tree died"
5019 catch {close $filehighlight}
5020 unset filehighlight
5021 return 0
5022 }
5023 if {[info exists find_dirn]} {
5024 run findmore
5025 }
5026 return 1
5027 }
5028
5029 proc doesmatch {f} {
5030 global findtype findpattern
5031
5032 if {$findtype eq [mc "Regexp"]} {
5033 return [regexp $findpattern $f]
5034 } elseif {$findtype eq [mc "IgnCase"]} {
5035 return [string match -nocase $findpattern $f]
5036 } else {
5037 return [string match $findpattern $f]
5038 }
5039 }
5040
5041 proc askfindhighlight {row id} {
5042 global nhighlights commitinfo iddrawn
5043 global findloc
5044 global markingmatches
5045
5046 if {![info exists commitinfo($id)]} {
5047 getcommit $id
5048 }
5049 set info $commitinfo($id)
5050 set isbold 0
5051 set fldtypes [list [mc Headline] [mc Author] "" [mc Committer] "" [mc Comments]]
5052 foreach f $info ty $fldtypes {
5053 if {$ty eq ""} continue
5054 if {($findloc eq [mc "All fields"] || $findloc eq $ty) &&
5055 [doesmatch $f]} {
5056 if {$ty eq [mc "Author"]} {
5057 set isbold 2
5058 break
5059 }
5060 set isbold 1
5061 }
5062 }
5063 if {$isbold && [info exists iddrawn($id)]} {
5064 if {![ishighlighted $id]} {
5065 bolden $id mainfontbold
5066 if {$isbold > 1} {
5067 bolden_name $id mainfontbold
5068 }
5069 }
5070 if {$markingmatches} {
5071 markrowmatches $row $id
5072 }
5073 }
5074 set nhighlights($id) $isbold
5075 }
5076
5077 proc markrowmatches {row id} {
5078 global canv canv2 linehtag linentag commitinfo findloc
5079
5080 set headline [lindex $commitinfo($id) 0]
5081 set author [lindex $commitinfo($id) 1]
5082 $canv delete match$row
5083 $canv2 delete match$row
5084 if {$findloc eq [mc "All fields"] || $findloc eq [mc "Headline"]} {
5085 set m [findmatches $headline]
5086 if {$m ne {}} {
5087 markmatches $canv $row $headline $linehtag($id) $m \
5088 [$canv itemcget $linehtag($id) -font] $row
5089 }
5090 }
5091 if {$findloc eq [mc "All fields"] || $findloc eq [mc "Author"]} {
5092 set m [findmatches $author]
5093 if {$m ne {}} {
5094 markmatches $canv2 $row $author $linentag($id) $m \
5095 [$canv2 itemcget $linentag($id) -font] $row
5096 }
5097 }
5098 }
5099
5100 proc vrel_change {name ix op} {
5101 global highlight_related
5102
5103 rhighlight_none
5104 if {$highlight_related ne [mc "None"]} {
5105 run drawvisible
5106 }
5107 }
5108
5109 # prepare for testing whether commits are descendents or ancestors of a
5110 proc rhighlight_sel {a} {
5111 global descendent desc_todo ancestor anc_todo
5112 global highlight_related
5113
5114 unset -nocomplain descendent
5115 set desc_todo [list $a]
5116 unset -nocomplain ancestor
5117 set anc_todo [list $a]
5118 if {$highlight_related ne [mc "None"]} {
5119 rhighlight_none
5120 run drawvisible
5121 }
5122 }
5123
5124 proc rhighlight_none {} {
5125 global rhighlights
5126
5127 unset -nocomplain rhighlights
5128 unbolden
5129 }
5130
5131 proc is_descendent {a} {
5132 global curview children descendent desc_todo
5133
5134 set v $curview
5135 set la [rowofcommit $a]
5136 set todo $desc_todo
5137 set leftover {}
5138 set done 0
5139 for {set i 0} {$i < [llength $todo]} {incr i} {
5140 set do [lindex $todo $i]
5141 if {[rowofcommit $do] < $la} {
5142 lappend leftover $do
5143 continue
5144 }
5145 foreach nk $children($v,$do) {
5146 if {![info exists descendent($nk)]} {
5147 set descendent($nk) 1
5148 lappend todo $nk
5149 if {$nk eq $a} {
5150 set done 1
5151 }
5152 }
5153 }
5154 if {$done} {
5155 set desc_todo [concat $leftover [lrange $todo [expr {$i+1}] end]]
5156 return
5157 }
5158 }
5159 set descendent($a) 0
5160 set desc_todo $leftover
5161 }
5162
5163 proc is_ancestor {a} {
5164 global curview parents ancestor anc_todo
5165
5166 set v $curview
5167 set la [rowofcommit $a]
5168 set todo $anc_todo
5169 set leftover {}
5170 set done 0
5171 for {set i 0} {$i < [llength $todo]} {incr i} {
5172 set do [lindex $todo $i]
5173 if {![commitinview $do $v] || [rowofcommit $do] > $la} {
5174 lappend leftover $do
5175 continue
5176 }
5177 foreach np $parents($v,$do) {
5178 if {![info exists ancestor($np)]} {
5179 set ancestor($np) 1
5180 lappend todo $np
5181 if {$np eq $a} {
5182 set done 1
5183 }
5184 }
5185 }
5186 if {$done} {
5187 set anc_todo [concat $leftover [lrange $todo [expr {$i+1}] end]]
5188 return
5189 }
5190 }
5191 set ancestor($a) 0
5192 set anc_todo $leftover
5193 }
5194
5195 proc askrelhighlight {row id} {
5196 global descendent highlight_related iddrawn rhighlights
5197 global selectedline ancestor
5198
5199 if {$selectedline eq {}} return
5200 set isbold 0
5201 if {$highlight_related eq [mc "Descendant"] ||
5202 $highlight_related eq [mc "Not descendant"]} {
5203 if {![info exists descendent($id)]} {
5204 is_descendent $id
5205 }
5206 if {$descendent($id) == ($highlight_related eq [mc "Descendant"])} {
5207 set isbold 1
5208 }
5209 } elseif {$highlight_related eq [mc "Ancestor"] ||
5210 $highlight_related eq [mc "Not ancestor"]} {
5211 if {![info exists ancestor($id)]} {
5212 is_ancestor $id
5213 }
5214 if {$ancestor($id) == ($highlight_related eq [mc "Ancestor"])} {
5215 set isbold 1
5216 }
5217 }
5218 if {[info exists iddrawn($id)]} {
5219 if {$isbold && ![ishighlighted $id]} {
5220 bolden $id mainfontbold
5221 }
5222 }
5223 set rhighlights($id) $isbold
5224 }
5225
5226 # Graph layout functions
5227
5228 proc shortids {ids} {
5229 set res {}
5230 foreach id $ids {
5231 if {[llength $id] > 1} {
5232 lappend res [shortids $id]
5233 } elseif {[regexp {^[0-9a-f]{40}$} $id]} {
5234 lappend res [string range $id 0 7]
5235 } else {
5236 lappend res $id
5237 }
5238 }
5239 return $res
5240 }
5241
5242 proc ntimes {n o} {
5243 set ret {}
5244 set o [list $o]
5245 for {set mask 1} {$mask <= $n} {incr mask $mask} {
5246 if {($n & $mask) != 0} {
5247 set ret [concat $ret $o]
5248 }
5249 set o [concat $o $o]
5250 }
5251 return $ret
5252 }
5253
5254 proc ordertoken {id} {
5255 global ordertok curview varcid varcstart varctok curview parents children
5256 global nullid nullid2
5257
5258 if {[info exists ordertok($id)]} {
5259 return $ordertok($id)
5260 }
5261 set origid $id
5262 set todo {}
5263 while {1} {
5264 if {[info exists varcid($curview,$id)]} {
5265 set a $varcid($curview,$id)
5266 set p [lindex $varcstart($curview) $a]
5267 } else {
5268 set p [lindex $children($curview,$id) 0]
5269 }
5270 if {[info exists ordertok($p)]} {
5271 set tok $ordertok($p)
5272 break
5273 }
5274 set id [first_real_child $curview,$p]
5275 if {$id eq {}} {
5276 # it's a root
5277 set tok [lindex $varctok($curview) $varcid($curview,$p)]
5278 break
5279 }
5280 if {[llength $parents($curview,$id)] == 1} {
5281 lappend todo [list $p {}]
5282 } else {
5283 set j [lsearch -exact $parents($curview,$id) $p]
5284 if {$j < 0} {
5285 puts "oops didn't find [shortids $p] in parents of [shortids $id]"
5286 }
5287 lappend todo [list $p [strrep $j]]
5288 }
5289 }
5290 for {set i [llength $todo]} {[incr i -1] >= 0} {} {
5291 set p [lindex $todo $i 0]
5292 append tok [lindex $todo $i 1]
5293 set ordertok($p) $tok
5294 }
5295 set ordertok($origid) $tok
5296 return $tok
5297 }
5298
5299 # Work out where id should go in idlist so that order-token
5300 # values increase from left to right
5301 proc idcol {idlist id {i 0}} {
5302 set t [ordertoken $id]
5303 if {$i < 0} {
5304 set i 0
5305 }
5306 if {$i >= [llength $idlist] || $t < [ordertoken [lindex $idlist $i]]} {
5307 if {$i > [llength $idlist]} {
5308 set i [llength $idlist]
5309 }
5310 while {[incr i -1] >= 0 && $t < [ordertoken [lindex $idlist $i]]} {}
5311 incr i
5312 } else {
5313 if {$t > [ordertoken [lindex $idlist $i]]} {
5314 while {[incr i] < [llength $idlist] &&
5315 $t >= [ordertoken [lindex $idlist $i]]} {}
5316 }
5317 }
5318 return $i
5319 }
5320
5321 proc initlayout {} {
5322 global rowidlist rowisopt rowfinal displayorder parentlist
5323 global numcommits canvxmax canv
5324 global nextcolor
5325 global colormap rowtextx
5326
5327 set numcommits 0
5328 set displayorder {}
5329 set parentlist {}
5330 set nextcolor 0
5331 set rowidlist {}
5332 set rowisopt {}
5333 set rowfinal {}
5334 set canvxmax [$canv cget -width]
5335 unset -nocomplain colormap
5336 unset -nocomplain rowtextx
5337 setcanvscroll
5338 }
5339
5340 proc setcanvscroll {} {
5341 global canv canv2 canv3 numcommits linespc canvxmax canvy0
5342 global lastscrollset lastscrollrows
5343
5344 set ymax [expr {$canvy0 + ($numcommits - 0.5) * $linespc + 2}]
5345 $canv conf -scrollregion [list 0 0 $canvxmax $ymax]
5346 $canv2 conf -scrollregion [list 0 0 0 $ymax]
5347 $canv3 conf -scrollregion [list 0 0 0 $ymax]
5348 set lastscrollset [clock clicks -milliseconds]
5349 set lastscrollrows $numcommits
5350 }
5351
5352 proc visiblerows {} {
5353 global canv numcommits linespc
5354
5355 set ymax [lindex [$canv cget -scrollregion] 3]
5356 if {$ymax eq {} || $ymax == 0} return
5357 set f [$canv yview]
5358 set y0 [expr {int([lindex $f 0] * $ymax)}]
5359 set r0 [expr {int(($y0 - 3) / $linespc) - 1}]
5360 if {$r0 < 0} {
5361 set r0 0
5362 }
5363 set y1 [expr {int([lindex $f 1] * $ymax)}]
5364 set r1 [expr {int(($y1 - 3) / $linespc) + 1}]
5365 if {$r1 >= $numcommits} {
5366 set r1 [expr {$numcommits - 1}]
5367 }
5368 return [list $r0 $r1]
5369 }
5370
5371 proc layoutmore {} {
5372 global commitidx viewcomplete curview
5373 global numcommits pending_select curview
5374 global lastscrollset lastscrollrows
5375
5376 if {$lastscrollrows < 100 || $viewcomplete($curview) ||
5377 [clock clicks -milliseconds] - $lastscrollset > 500} {
5378 setcanvscroll
5379 }
5380 if {[info exists pending_select] &&
5381 [commitinview $pending_select $curview]} {
5382 update
5383 selectline [rowofcommit $pending_select] 1
5384 }
5385 drawvisible
5386 }
5387
5388 # With path limiting, we mightn't get the actual HEAD commit,
5389 # so ask git rev-list what is the first ancestor of HEAD that
5390 # touches a file in the path limit.
5391 proc get_viewmainhead {view} {
5392 global viewmainheadid vfilelimit viewinstances mainheadid
5393
5394 catch {
5395 set rfd [open [concat | git rev-list -1 $mainheadid \
5396 -- $vfilelimit($view)] r]
5397 set j [reg_instance $rfd]
5398 lappend viewinstances($view) $j
5399 fconfigure $rfd -blocking 0
5400 filerun $rfd [list getviewhead $rfd $j $view]
5401 set viewmainheadid($curview) {}
5402 }
5403 }
5404
5405 # git rev-list should give us just 1 line to use as viewmainheadid($view)
5406 proc getviewhead {fd inst view} {
5407 global viewmainheadid commfd curview viewinstances showlocalchanges
5408
5409 set id {}
5410 if {[gets $fd line] < 0} {
5411 if {![eof $fd]} {
5412 return 1
5413 }
5414 } elseif {[string length $line] == 40 && [string is xdigit $line]} {
5415 set id $line
5416 }
5417 set viewmainheadid($view) $id
5418 close $fd
5419 unset commfd($inst)
5420 set i [lsearch -exact $viewinstances($view) $inst]
5421 if {$i >= 0} {
5422 set viewinstances($view) [lreplace $viewinstances($view) $i $i]
5423 }
5424 if {$showlocalchanges && $id ne {} && $view == $curview} {
5425 doshowlocalchanges
5426 }
5427 return 0
5428 }
5429
5430 proc doshowlocalchanges {} {
5431 global curview viewmainheadid
5432
5433 if {$viewmainheadid($curview) eq {}} return
5434 if {[commitinview $viewmainheadid($curview) $curview]} {
5435 dodiffindex
5436 } else {
5437 interestedin $viewmainheadid($curview) dodiffindex
5438 }
5439 }
5440
5441 proc dohidelocalchanges {} {
5442 global nullid nullid2 lserial curview
5443
5444 if {[commitinview $nullid $curview]} {
5445 removefakerow $nullid
5446 }
5447 if {[commitinview $nullid2 $curview]} {
5448 removefakerow $nullid2
5449 }
5450 incr lserial
5451 }
5452
5453 # spawn off a process to do git diff-index --cached HEAD
5454 proc dodiffindex {} {
5455 global lserial showlocalchanges vfilelimit curview
5456 global hasworktree git_version
5457
5458 if {!$showlocalchanges || !$hasworktree} return
5459 incr lserial
5460 if {[package vcompare $git_version "1.7.2"] >= 0} {
5461 set cmd "|git diff-index --cached --ignore-submodules=dirty HEAD"
5462 } else {
5463 set cmd "|git diff-index --cached HEAD"
5464 }
5465 if {$vfilelimit($curview) ne {}} {
5466 set cmd [concat $cmd -- $vfilelimit($curview)]
5467 }
5468 set fd [open $cmd r]
5469 fconfigure $fd -blocking 0
5470 set i [reg_instance $fd]
5471 filerun $fd [list readdiffindex $fd $lserial $i]
5472 }
5473
5474 proc readdiffindex {fd serial inst} {
5475 global viewmainheadid nullid nullid2 curview commitinfo commitdata lserial
5476 global vfilelimit
5477
5478 set isdiff 1
5479 if {[gets $fd line] < 0} {
5480 if {![eof $fd]} {
5481 return 1
5482 }
5483 set isdiff 0
5484 }
5485 # we only need to see one line and we don't really care what it says...
5486 stop_instance $inst
5487
5488 if {$serial != $lserial} {
5489 return 0
5490 }
5491
5492 # now see if there are any local changes not checked in to the index
5493 set cmd "|git diff-files"
5494 if {$vfilelimit($curview) ne {}} {
5495 set cmd [concat $cmd -- $vfilelimit($curview)]
5496 }
5497 set fd [open $cmd r]
5498 fconfigure $fd -blocking 0
5499 set i [reg_instance $fd]
5500 filerun $fd [list readdifffiles $fd $serial $i]
5501
5502 if {$isdiff && ![commitinview $nullid2 $curview]} {
5503 # add the line for the changes in the index to the graph
5504 set hl [mc "Local changes checked in to index but not committed"]
5505 set commitinfo($nullid2) [list $hl {} {} {} {} " $hl\n"]
5506 set commitdata($nullid2) "\n $hl\n"
5507 if {[commitinview $nullid $curview]} {
5508 removefakerow $nullid
5509 }
5510 insertfakerow $nullid2 $viewmainheadid($curview)
5511 } elseif {!$isdiff && [commitinview $nullid2 $curview]} {
5512 if {[commitinview $nullid $curview]} {
5513 removefakerow $nullid
5514 }
5515 removefakerow $nullid2
5516 }
5517 return 0
5518 }
5519
5520 proc readdifffiles {fd serial inst} {
5521 global viewmainheadid nullid nullid2 curview
5522 global commitinfo commitdata lserial
5523
5524 set isdiff 1
5525 if {[gets $fd line] < 0} {
5526 if {![eof $fd]} {
5527 return 1
5528 }
5529 set isdiff 0
5530 }
5531 # we only need to see one line and we don't really care what it says...
5532 stop_instance $inst
5533
5534 if {$serial != $lserial} {
5535 return 0
5536 }
5537
5538 if {$isdiff && ![commitinview $nullid $curview]} {
5539 # add the line for the local diff to the graph
5540 set hl [mc "Local uncommitted changes, not checked in to index"]
5541 set commitinfo($nullid) [list $hl {} {} {} {} " $hl\n"]
5542 set commitdata($nullid) "\n $hl\n"
5543 if {[commitinview $nullid2 $curview]} {
5544 set p $nullid2
5545 } else {
5546 set p $viewmainheadid($curview)
5547 }
5548 insertfakerow $nullid $p
5549 } elseif {!$isdiff && [commitinview $nullid $curview]} {
5550 removefakerow $nullid
5551 }
5552 return 0
5553 }
5554
5555 proc nextuse {id row} {
5556 global curview children
5557
5558 if {[info exists children($curview,$id)]} {
5559 foreach kid $children($curview,$id) {
5560 if {![commitinview $kid $curview]} {
5561 return -1
5562 }
5563 if {[rowofcommit $kid] > $row} {
5564 return [rowofcommit $kid]
5565 }
5566 }
5567 }
5568 if {[commitinview $id $curview]} {
5569 return [rowofcommit $id]
5570 }
5571 return -1
5572 }
5573
5574 proc prevuse {id row} {
5575 global curview children
5576
5577 set ret -1
5578 if {[info exists children($curview,$id)]} {
5579 foreach kid $children($curview,$id) {
5580 if {![commitinview $kid $curview]} break
5581 if {[rowofcommit $kid] < $row} {
5582 set ret [rowofcommit $kid]
5583 }
5584 }
5585 }
5586 return $ret
5587 }
5588
5589 proc make_idlist {row} {
5590 global displayorder parentlist uparrowlen downarrowlen mingaplen
5591 global commitidx curview children
5592
5593 set r [expr {$row - $mingaplen - $downarrowlen - 1}]
5594 if {$r < 0} {
5595 set r 0
5596 }
5597 set ra [expr {$row - $downarrowlen}]
5598 if {$ra < 0} {
5599 set ra 0
5600 }
5601 set rb [expr {$row + $uparrowlen}]
5602 if {$rb > $commitidx($curview)} {
5603 set rb $commitidx($curview)
5604 }
5605 make_disporder $r [expr {$rb + 1}]
5606 set ids {}
5607 for {} {$r < $ra} {incr r} {
5608 set nextid [lindex $displayorder [expr {$r + 1}]]
5609 foreach p [lindex $parentlist $r] {
5610 if {$p eq $nextid} continue
5611 set rn [nextuse $p $r]
5612 if {$rn >= $row &&
5613 $rn <= $r + $downarrowlen + $mingaplen + $uparrowlen} {
5614 lappend ids [list [ordertoken $p] $p]
5615 }
5616 }
5617 }
5618 for {} {$r < $row} {incr r} {
5619 set nextid [lindex $displayorder [expr {$r + 1}]]
5620 foreach p [lindex $parentlist $r] {
5621 if {$p eq $nextid} continue
5622 set rn [nextuse $p $r]
5623 if {$rn < 0 || $rn >= $row} {
5624 lappend ids [list [ordertoken $p] $p]
5625 }
5626 }
5627 }
5628 set id [lindex $displayorder $row]
5629 lappend ids [list [ordertoken $id] $id]
5630 while {$r < $rb} {
5631 foreach p [lindex $parentlist $r] {
5632 set firstkid [lindex $children($curview,$p) 0]
5633 if {[rowofcommit $firstkid] < $row} {
5634 lappend ids [list [ordertoken $p] $p]
5635 }
5636 }
5637 incr r
5638 set id [lindex $displayorder $r]
5639 if {$id ne {}} {
5640 set firstkid [lindex $children($curview,$id) 0]
5641 if {$firstkid ne {} && [rowofcommit $firstkid] < $row} {
5642 lappend ids [list [ordertoken $id] $id]
5643 }
5644 }
5645 }
5646 set idlist {}
5647 foreach idx [lsort -unique $ids] {
5648 lappend idlist [lindex $idx 1]
5649 }
5650 return $idlist
5651 }
5652
5653 proc rowsequal {a b} {
5654 while {[set i [lsearch -exact $a {}]] >= 0} {
5655 set a [lreplace $a $i $i]
5656 }
5657 while {[set i [lsearch -exact $b {}]] >= 0} {
5658 set b [lreplace $b $i $i]
5659 }
5660 return [expr {$a eq $b}]
5661 }
5662
5663 proc makeupline {id row rend col} {
5664 global rowidlist uparrowlen downarrowlen mingaplen
5665
5666 for {set r $rend} {1} {set r $rstart} {
5667 set rstart [prevuse $id $r]
5668 if {$rstart < 0} return
5669 if {$rstart < $row} break
5670 }
5671 if {$rstart + $uparrowlen + $mingaplen + $downarrowlen < $rend} {
5672 set rstart [expr {$rend - $uparrowlen - 1}]
5673 }
5674 for {set r $rstart} {[incr r] <= $row} {} {
5675 set idlist [lindex $rowidlist $r]
5676 if {$idlist ne {} && [lsearch -exact $idlist $id] < 0} {
5677 set col [idcol $idlist $id $col]
5678 lset rowidlist $r [linsert $idlist $col $id]
5679 changedrow $r
5680 }
5681 }
5682 }
5683
5684 proc layoutrows {row endrow} {
5685 global rowidlist rowisopt rowfinal displayorder
5686 global uparrowlen downarrowlen maxwidth mingaplen
5687 global children parentlist
5688 global commitidx viewcomplete curview
5689
5690 make_disporder [expr {$row - 1}] [expr {$endrow + $uparrowlen}]
5691 set idlist {}
5692 if {$row > 0} {
5693 set rm1 [expr {$row - 1}]
5694 foreach id [lindex $rowidlist $rm1] {
5695 if {$id ne {}} {
5696 lappend idlist $id
5697 }
5698 }
5699 set final [lindex $rowfinal $rm1]
5700 }
5701 for {} {$row < $endrow} {incr row} {
5702 set rm1 [expr {$row - 1}]
5703 if {$rm1 < 0 || $idlist eq {}} {
5704 set idlist [make_idlist $row]
5705 set final 1
5706 } else {
5707 set id [lindex $displayorder $rm1]
5708 set col [lsearch -exact $idlist $id]
5709 set idlist [lreplace $idlist $col $col]
5710 foreach p [lindex $parentlist $rm1] {
5711 if {[lsearch -exact $idlist $p] < 0} {
5712 set col [idcol $idlist $p $col]
5713 set idlist [linsert $idlist $col $p]
5714 # if not the first child, we have to insert a line going up
5715 if {$id ne [lindex $children($curview,$p) 0]} {
5716 makeupline $p $rm1 $row $col
5717 }
5718 }
5719 }
5720 set id [lindex $displayorder $row]
5721 if {$row > $downarrowlen} {
5722 set termrow [expr {$row - $downarrowlen - 1}]
5723 foreach p [lindex $parentlist $termrow] {
5724 set i [lsearch -exact $idlist $p]
5725 if {$i < 0} continue
5726 set nr [nextuse $p $termrow]
5727 if {$nr < 0 || $nr >= $row + $mingaplen + $uparrowlen} {
5728 set idlist [lreplace $idlist $i $i]
5729 }
5730 }
5731 }
5732 set col [lsearch -exact $idlist $id]
5733 if {$col < 0} {
5734 set col [idcol $idlist $id]
5735 set idlist [linsert $idlist $col $id]
5736 if {$children($curview,$id) ne {}} {
5737 makeupline $id $rm1 $row $col
5738 }
5739 }
5740 set r [expr {$row + $uparrowlen - 1}]
5741 if {$r < $commitidx($curview)} {
5742 set x $col
5743 foreach p [lindex $parentlist $r] {
5744 if {[lsearch -exact $idlist $p] >= 0} continue
5745 set fk [lindex $children($curview,$p) 0]
5746 if {[rowofcommit $fk] < $row} {
5747 set x [idcol $idlist $p $x]
5748 set idlist [linsert $idlist $x $p]
5749 }
5750 }
5751 if {[incr r] < $commitidx($curview)} {
5752 set p [lindex $displayorder $r]
5753 if {[lsearch -exact $idlist $p] < 0} {
5754 set fk [lindex $children($curview,$p) 0]
5755 if {$fk ne {} && [rowofcommit $fk] < $row} {
5756 set x [idcol $idlist $p $x]
5757 set idlist [linsert $idlist $x $p]
5758 }
5759 }
5760 }
5761 }
5762 }
5763 if {$final && !$viewcomplete($curview) &&
5764 $row + $uparrowlen + $mingaplen + $downarrowlen
5765 >= $commitidx($curview)} {
5766 set final 0
5767 }
5768 set l [llength $rowidlist]
5769 if {$row == $l} {
5770 lappend rowidlist $idlist
5771 lappend rowisopt 0
5772 lappend rowfinal $final
5773 } elseif {$row < $l} {
5774 if {![rowsequal $idlist [lindex $rowidlist $row]]} {
5775 lset rowidlist $row $idlist
5776 changedrow $row
5777 }
5778 lset rowfinal $row $final
5779 } else {
5780 set pad [ntimes [expr {$row - $l}] {}]
5781 set rowidlist [concat $rowidlist $pad]
5782 lappend rowidlist $idlist
5783 set rowfinal [concat $rowfinal $pad]
5784 lappend rowfinal $final
5785 set rowisopt [concat $rowisopt [ntimes [expr {$row - $l + 1}] 0]]
5786 }
5787 }
5788 return $row
5789 }
5790
5791 proc changedrow {row} {
5792 global displayorder iddrawn rowisopt need_redisplay
5793
5794 set l [llength $rowisopt]
5795 if {$row < $l} {
5796 lset rowisopt $row 0
5797 if {$row + 1 < $l} {
5798 lset rowisopt [expr {$row + 1}] 0
5799 if {$row + 2 < $l} {
5800 lset rowisopt [expr {$row + 2}] 0
5801 }
5802 }
5803 }
5804 set id [lindex $displayorder $row]
5805 if {[info exists iddrawn($id)]} {
5806 set need_redisplay 1
5807 }
5808 }
5809
5810 proc insert_pad {row col npad} {
5811 global rowidlist
5812
5813 set pad [ntimes $npad {}]
5814 set idlist [lindex $rowidlist $row]
5815 set bef [lrange $idlist 0 [expr {$col - 1}]]
5816 set aft [lrange $idlist $col end]
5817 set i [lsearch -exact $aft {}]
5818 if {$i > 0} {
5819 set aft [lreplace $aft $i $i]
5820 }
5821 lset rowidlist $row [concat $bef $pad $aft]
5822 changedrow $row
5823 }
5824
5825 proc optimize_rows {row col endrow} {
5826 global rowidlist rowisopt displayorder curview children
5827
5828 if {$row < 1} {
5829 set row 1
5830 }
5831 for {} {$row < $endrow} {incr row; set col 0} {
5832 if {[lindex $rowisopt $row]} continue
5833 set haspad 0
5834 set y0 [expr {$row - 1}]
5835 set ym [expr {$row - 2}]
5836 set idlist [lindex $rowidlist $row]
5837 set previdlist [lindex $rowidlist $y0]
5838 if {$idlist eq {} || $previdlist eq {}} continue
5839 if {$ym >= 0} {
5840 set pprevidlist [lindex $rowidlist $ym]
5841 if {$pprevidlist eq {}} continue
5842 } else {
5843 set pprevidlist {}
5844 }
5845 set x0 -1
5846 set xm -1
5847 for {} {$col < [llength $idlist]} {incr col} {
5848 set id [lindex $idlist $col]
5849 if {[lindex $previdlist $col] eq $id} continue
5850 if {$id eq {}} {
5851 set haspad 1
5852 continue
5853 }
5854 set x0 [lsearch -exact $previdlist $id]
5855 if {$x0 < 0} continue
5856 set z [expr {$x0 - $col}]
5857 set isarrow 0
5858 set z0 {}
5859 if {$ym >= 0} {
5860 set xm [lsearch -exact $pprevidlist $id]
5861 if {$xm >= 0} {
5862 set z0 [expr {$xm - $x0}]
5863 }
5864 }
5865 if {$z0 eq {}} {
5866 # if row y0 is the first child of $id then it's not an arrow
5867 if {[lindex $children($curview,$id) 0] ne
5868 [lindex $displayorder $y0]} {
5869 set isarrow 1
5870 }
5871 }
5872 if {!$isarrow && $id ne [lindex $displayorder $row] &&
5873 [lsearch -exact [lindex $rowidlist [expr {$row+1}]] $id] < 0} {
5874 set isarrow 1
5875 }
5876 # Looking at lines from this row to the previous row,
5877 # make them go straight up if they end in an arrow on
5878 # the previous row; otherwise make them go straight up
5879 # or at 45 degrees.
5880 if {$z < -1 || ($z < 0 && $isarrow)} {
5881 # Line currently goes left too much;
5882 # insert pads in the previous row, then optimize it
5883 set npad [expr {-1 - $z + $isarrow}]
5884 insert_pad $y0 $x0 $npad
5885 if {$y0 > 0} {
5886 optimize_rows $y0 $x0 $row
5887 }
5888 set previdlist [lindex $rowidlist $y0]
5889 set x0 [lsearch -exact $previdlist $id]
5890 set z [expr {$x0 - $col}]
5891 if {$z0 ne {}} {
5892 set pprevidlist [lindex $rowidlist $ym]
5893 set xm [lsearch -exact $pprevidlist $id]
5894 set z0 [expr {$xm - $x0}]
5895 }
5896 } elseif {$z > 1 || ($z > 0 && $isarrow)} {
5897 # Line currently goes right too much;
5898 # insert pads in this line
5899 set npad [expr {$z - 1 + $isarrow}]
5900 insert_pad $row $col $npad
5901 set idlist [lindex $rowidlist $row]
5902 incr col $npad
5903 set z [expr {$x0 - $col}]
5904 set haspad 1
5905 }
5906 if {$z0 eq {} && !$isarrow && $ym >= 0} {
5907 # this line links to its first child on row $row-2
5908 set id [lindex $displayorder $ym]
5909 set xc [lsearch -exact $pprevidlist $id]
5910 if {$xc >= 0} {
5911 set z0 [expr {$xc - $x0}]
5912 }
5913 }
5914 # avoid lines jigging left then immediately right
5915 if {$z0 ne {} && $z < 0 && $z0 > 0} {
5916 insert_pad $y0 $x0 1
5917 incr x0
5918 optimize_rows $y0 $x0 $row
5919 set previdlist [lindex $rowidlist $y0]
5920 }
5921 }
5922 if {!$haspad} {
5923 # Find the first column that doesn't have a line going right
5924 for {set col [llength $idlist]} {[incr col -1] >= 0} {} {
5925 set id [lindex $idlist $col]
5926 if {$id eq {}} break
5927 set x0 [lsearch -exact $previdlist $id]
5928 if {$x0 < 0} {
5929 # check if this is the link to the first child
5930 set kid [lindex $displayorder $y0]
5931 if {[lindex $children($curview,$id) 0] eq $kid} {
5932 # it is, work out offset to child
5933 set x0 [lsearch -exact $previdlist $kid]
5934 }
5935 }
5936 if {$x0 <= $col} break
5937 }
5938 # Insert a pad at that column as long as it has a line and
5939 # isn't the last column
5940 if {$x0 >= 0 && [incr col] < [llength $idlist]} {
5941 set idlist [linsert $idlist $col {}]
5942 lset rowidlist $row $idlist
5943 changedrow $row
5944 }
5945 }
5946 }
5947 }
5948
5949 proc xc {row col} {
5950 global canvx0 linespc
5951 return [expr {$canvx0 + $col * $linespc}]
5952 }
5953
5954 proc yc {row} {
5955 global canvy0 linespc
5956 return [expr {$canvy0 + $row * $linespc}]
5957 }
5958
5959 proc linewidth {id} {
5960 global thickerline lthickness
5961
5962 set wid $lthickness
5963 if {[info exists thickerline] && $id eq $thickerline} {
5964 set wid [expr {2 * $lthickness}]
5965 }
5966 return $wid
5967 }
5968
5969 proc rowranges {id} {
5970 global curview children uparrowlen downarrowlen
5971 global rowidlist
5972
5973 set kids $children($curview,$id)
5974 if {$kids eq {}} {
5975 return {}
5976 }
5977 set ret {}
5978 lappend kids $id
5979 foreach child $kids {
5980 if {![commitinview $child $curview]} break
5981 set row [rowofcommit $child]
5982 if {![info exists prev]} {
5983 lappend ret [expr {$row + 1}]
5984 } else {
5985 if {$row <= $prevrow} {
5986 puts "oops children of [shortids $id] out of order [shortids $child] $row <= [shortids $prev] $prevrow"
5987 }
5988 # see if the line extends the whole way from prevrow to row
5989 if {$row > $prevrow + $uparrowlen + $downarrowlen &&
5990 [lsearch -exact [lindex $rowidlist \
5991 [expr {int(($row + $prevrow) / 2)}]] $id] < 0} {
5992 # it doesn't, see where it ends
5993 set r [expr {$prevrow + $downarrowlen}]
5994 if {[lsearch -exact [lindex $rowidlist $r] $id] < 0} {
5995 while {[incr r -1] > $prevrow &&
5996 [lsearch -exact [lindex $rowidlist $r] $id] < 0} {}
5997 } else {
5998 while {[incr r] <= $row &&
5999 [lsearch -exact [lindex $rowidlist $r] $id] >= 0} {}
6000 incr r -1
6001 }
6002 lappend ret $r
6003 # see where it starts up again
6004 set r [expr {$row - $uparrowlen}]
6005 if {[lsearch -exact [lindex $rowidlist $r] $id] < 0} {
6006 while {[incr r] < $row &&
6007 [lsearch -exact [lindex $rowidlist $r] $id] < 0} {}
6008 } else {
6009 while {[incr r -1] >= $prevrow &&
6010 [lsearch -exact [lindex $rowidlist $r] $id] >= 0} {}
6011 incr r
6012 }
6013 lappend ret $r
6014 }
6015 }
6016 if {$child eq $id} {
6017 lappend ret $row
6018 }
6019 set prev $child
6020 set prevrow $row
6021 }
6022 return $ret
6023 }
6024
6025 proc drawlineseg {id row endrow arrowlow} {
6026 global rowidlist displayorder iddrawn linesegs
6027 global canv colormap linespc curview maxlinelen parentlist
6028
6029 set cols [list [lsearch -exact [lindex $rowidlist $row] $id]]
6030 set le [expr {$row + 1}]
6031 set arrowhigh 1
6032 while {1} {
6033 set c [lsearch -exact [lindex $rowidlist $le] $id]
6034 if {$c < 0} {
6035 incr le -1
6036 break
6037 }
6038 lappend cols $c
6039 set x [lindex $displayorder $le]
6040 if {$x eq $id} {
6041 set arrowhigh 0
6042 break
6043 }
6044 if {[info exists iddrawn($x)] || $le == $endrow} {
6045 set c [lsearch -exact [lindex $rowidlist [expr {$le+1}]] $id]
6046 if {$c >= 0} {
6047 lappend cols $c
6048 set arrowhigh 0
6049 }
6050 break
6051 }
6052 incr le
6053 }
6054 if {$le <= $row} {
6055 return $row
6056 }
6057
6058 set lines {}
6059 set i 0
6060 set joinhigh 0
6061 if {[info exists linesegs($id)]} {
6062 set lines $linesegs($id)
6063 foreach li $lines {
6064 set r0 [lindex $li 0]
6065 if {$r0 > $row} {
6066 if {$r0 == $le && [lindex $li 1] - $row <= $maxlinelen} {
6067 set joinhigh 1
6068 }
6069 break
6070 }
6071 incr i
6072 }
6073 }
6074 set joinlow 0
6075 if {$i > 0} {
6076 set li [lindex $lines [expr {$i-1}]]
6077 set r1 [lindex $li 1]
6078 if {$r1 == $row && $le - [lindex $li 0] <= $maxlinelen} {
6079 set joinlow 1
6080 }
6081 }
6082
6083 set x [lindex $cols [expr {$le - $row}]]
6084 set xp [lindex $cols [expr {$le - 1 - $row}]]
6085 set dir [expr {$xp - $x}]
6086 if {$joinhigh} {
6087 set ith [lindex $lines $i 2]
6088 set coords [$canv coords $ith]
6089 set ah [$canv itemcget $ith -arrow]
6090 set arrowhigh [expr {$ah eq "first" || $ah eq "both"}]
6091 set x2 [lindex $cols [expr {$le + 1 - $row}]]
6092 if {$x2 ne {} && $x - $x2 == $dir} {
6093 set coords [lrange $coords 0 end-2]
6094 }
6095 } else {
6096 set coords [list [xc $le $x] [yc $le]]
6097 }
6098 if {$joinlow} {
6099 set itl [lindex $lines [expr {$i-1}] 2]
6100 set al [$canv itemcget $itl -arrow]
6101 set arrowlow [expr {$al eq "last" || $al eq "both"}]
6102 } elseif {$arrowlow} {
6103 if {[lsearch -exact [lindex $rowidlist [expr {$row-1}]] $id] >= 0 ||
6104 [lsearch -exact [lindex $parentlist [expr {$row-1}]] $id] >= 0} {
6105 set arrowlow 0
6106 }
6107 }
6108 set arrow [lindex {none first last both} [expr {$arrowhigh + 2*$arrowlow}]]
6109 for {set y $le} {[incr y -1] > $row} {} {
6110 set x $xp
6111 set xp [lindex $cols [expr {$y - 1 - $row}]]
6112 set ndir [expr {$xp - $x}]
6113 if {$dir != $ndir || $xp < 0} {
6114 lappend coords [xc $y $x] [yc $y]
6115 }
6116 set dir $ndir
6117 }
6118 if {!$joinlow} {
6119 if {$xp < 0} {
6120 # join parent line to first child
6121 set ch [lindex $displayorder $row]
6122 set xc [lsearch -exact [lindex $rowidlist $row] $ch]
6123 if {$xc < 0} {
6124 puts "oops: drawlineseg: child $ch not on row $row"
6125 } elseif {$xc != $x} {
6126 if {($arrowhigh && $le == $row + 1) || $dir == 0} {
6127 set d [expr {int(0.5 * $linespc)}]
6128 set x1 [xc $row $x]
6129 if {$xc < $x} {
6130 set x2 [expr {$x1 - $d}]
6131 } else {
6132 set x2 [expr {$x1 + $d}]
6133 }
6134 set y2 [yc $row]
6135 set y1 [expr {$y2 + $d}]
6136 lappend coords $x1 $y1 $x2 $y2
6137 } elseif {$xc < $x - 1} {
6138 lappend coords [xc $row [expr {$x-1}]] [yc $row]
6139 } elseif {$xc > $x + 1} {
6140 lappend coords [xc $row [expr {$x+1}]] [yc $row]
6141 }
6142 set x $xc
6143 }
6144 lappend coords [xc $row $x] [yc $row]
6145 } else {
6146 set xn [xc $row $xp]
6147 set yn [yc $row]
6148 lappend coords $xn $yn
6149 }
6150 if {!$joinhigh} {
6151 assigncolor $id
6152 set t [$canv create line $coords -width [linewidth $id] \
6153 -fill $colormap($id) -tags lines.$id -arrow $arrow]
6154 $canv lower $t
6155 bindline $t $id
6156 set lines [linsert $lines $i [list $row $le $t]]
6157 } else {
6158 $canv coords $ith $coords
6159 if {$arrow ne $ah} {
6160 $canv itemconf $ith -arrow $arrow
6161 }
6162 lset lines $i 0 $row
6163 }
6164 } else {
6165 set xo [lsearch -exact [lindex $rowidlist [expr {$row - 1}]] $id]
6166 set ndir [expr {$xo - $xp}]
6167 set clow [$canv coords $itl]
6168 if {$dir == $ndir} {
6169 set clow [lrange $clow 2 end]
6170 }
6171 set coords [concat $coords $clow]
6172 if {!$joinhigh} {
6173 lset lines [expr {$i-1}] 1 $le
6174 } else {
6175 # coalesce two pieces
6176 $canv delete $ith
6177 set b [lindex $lines [expr {$i-1}] 0]
6178 set e [lindex $lines $i 1]
6179 set lines [lreplace $lines [expr {$i-1}] $i [list $b $e $itl]]
6180 }
6181 $canv coords $itl $coords
6182 if {$arrow ne $al} {
6183 $canv itemconf $itl -arrow $arrow
6184 }
6185 }
6186
6187 set linesegs($id) $lines
6188 return $le
6189 }
6190
6191 proc drawparentlinks {id row} {
6192 global rowidlist canv colormap curview parentlist
6193 global idpos linespc
6194
6195 set rowids [lindex $rowidlist $row]
6196 set col [lsearch -exact $rowids $id]
6197 if {$col < 0} return
6198 set olds [lindex $parentlist $row]
6199 set row2 [expr {$row + 1}]
6200 set x [xc $row $col]
6201 set y [yc $row]
6202 set y2 [yc $row2]
6203 set d [expr {int(0.5 * $linespc)}]
6204 set ymid [expr {$y + $d}]
6205 set ids [lindex $rowidlist $row2]
6206 # rmx = right-most X coord used
6207 set rmx 0
6208 foreach p $olds {
6209 set i [lsearch -exact $ids $p]
6210 if {$i < 0} {
6211 puts "oops, parent $p of $id not in list"
6212 continue
6213 }
6214 set x2 [xc $row2 $i]
6215 if {$x2 > $rmx} {
6216 set rmx $x2
6217 }
6218 set j [lsearch -exact $rowids $p]
6219 if {$j < 0} {
6220 # drawlineseg will do this one for us
6221 continue
6222 }
6223 assigncolor $p
6224 # should handle duplicated parents here...
6225 set coords [list $x $y]
6226 if {$i != $col} {
6227 # if attaching to a vertical segment, draw a smaller
6228 # slant for visual distinctness
6229 if {$i == $j} {
6230 if {$i < $col} {
6231 lappend coords [expr {$x2 + $d}] $y $x2 $ymid
6232 } else {
6233 lappend coords [expr {$x2 - $d}] $y $x2 $ymid
6234 }
6235 } elseif {$i < $col && $i < $j} {
6236 # segment slants towards us already
6237 lappend coords [xc $row $j] $y
6238 } else {
6239 if {$i < $col - 1} {
6240 lappend coords [expr {$x2 + $linespc}] $y
6241 } elseif {$i > $col + 1} {
6242 lappend coords [expr {$x2 - $linespc}] $y
6243 }
6244 lappend coords $x2 $y2
6245 }
6246 } else {
6247 lappend coords $x2 $y2
6248 }
6249 set t [$canv create line $coords -width [linewidth $p] \
6250 -fill $colormap($p) -tags lines.$p]
6251 $canv lower $t
6252 bindline $t $p
6253 }
6254 if {$rmx > [lindex $idpos($id) 1]} {
6255 lset idpos($id) 1 $rmx
6256 redrawtags $id
6257 }
6258 }
6259
6260 proc drawlines {id} {
6261 global canv
6262
6263 $canv itemconf lines.$id -width [linewidth $id]
6264 }
6265
6266 proc drawcmittext {id row col} {
6267 global linespc canv canv2 canv3 fgcolor curview
6268 global cmitlisted commitinfo rowidlist parentlist
6269 global rowtextx idpos idtags idheads idotherrefs
6270 global linehtag linentag linedtag selectedline
6271 global canvxmax boldids boldnameids fgcolor markedid
6272 global mainheadid nullid nullid2 circleitem circlecolors ctxbut
6273 global mainheadcirclecolor workingfilescirclecolor indexcirclecolor
6274 global circleoutlinecolor
6275
6276 # listed is 0 for boundary, 1 for normal, 2 for negative, 3 for left, 4 for right
6277 set listed $cmitlisted($curview,$id)
6278 if {$id eq $nullid} {
6279 set ofill $workingfilescirclecolor
6280 } elseif {$id eq $nullid2} {
6281 set ofill $indexcirclecolor
6282 } elseif {$id eq $mainheadid} {
6283 set ofill $mainheadcirclecolor
6284 } else {
6285 set ofill [lindex $circlecolors $listed]
6286 }
6287 set x [xc $row $col]
6288 set y [yc $row]
6289 set orad [expr {$linespc / 3}]
6290 if {$listed <= 2} {
6291 set t [$canv create oval [expr {$x - $orad}] [expr {$y - $orad}] \
6292 [expr {$x + $orad - 1}] [expr {$y + $orad - 1}] \
6293 -fill $ofill -outline $circleoutlinecolor -width 1 -tags circle]
6294 } elseif {$listed == 3} {
6295 # triangle pointing left for left-side commits
6296 set t [$canv create polygon \
6297 [expr {$x - $orad}] $y \
6298 [expr {$x + $orad - 1}] [expr {$y - $orad}] \
6299 [expr {$x + $orad - 1}] [expr {$y + $orad - 1}] \
6300 -fill $ofill -outline $circleoutlinecolor -width 1 -tags circle]
6301 } else {
6302 # triangle pointing right for right-side commits
6303 set t [$canv create polygon \
6304 [expr {$x + $orad - 1}] $y \
6305 [expr {$x - $orad}] [expr {$y - $orad}] \
6306 [expr {$x - $orad}] [expr {$y + $orad - 1}] \
6307 -fill $ofill -outline $circleoutlinecolor -width 1 -tags circle]
6308 }
6309 set circleitem($row) $t
6310 $canv raise $t
6311 $canv bind $t <1> {selcanvline {} %x %y}
6312 set rmx [llength [lindex $rowidlist $row]]
6313 set olds [lindex $parentlist $row]
6314 if {$olds ne {}} {
6315 set nextids [lindex $rowidlist [expr {$row + 1}]]
6316 foreach p $olds {
6317 set i [lsearch -exact $nextids $p]
6318 if {$i > $rmx} {
6319 set rmx $i
6320 }
6321 }
6322 }
6323 set xt [xc $row $rmx]
6324 set rowtextx($row) $xt
6325 set idpos($id) [list $x $xt $y]
6326 if {[info exists idtags($id)] || [info exists idheads($id)]
6327 || [info exists idotherrefs($id)]} {
6328 set xt [drawtags $id $x $xt $y]
6329 }
6330 if {[lindex $commitinfo($id) 6] > 0} {
6331 set xt [drawnotesign $xt $y]
6332 }
6333 set headline [lindex $commitinfo($id) 0]
6334 set name [lindex $commitinfo($id) 1]
6335 set date [lindex $commitinfo($id) 2]
6336 set date [formatdate $date]
6337 set font mainfont
6338 set nfont mainfont
6339 set isbold [ishighlighted $id]
6340 if {$isbold > 0} {
6341 lappend boldids $id
6342 set font mainfontbold
6343 if {$isbold > 1} {
6344 lappend boldnameids $id
6345 set nfont mainfontbold
6346 }
6347 }
6348 set linehtag($id) [$canv create text $xt $y -anchor w -fill $fgcolor \
6349 -text $headline -font $font -tags text]
6350 $canv bind $linehtag($id) $ctxbut "rowmenu %X %Y $id"
6351 set linentag($id) [$canv2 create text 3 $y -anchor w -fill $fgcolor \
6352 -text $name -font $nfont -tags text]
6353 set linedtag($id) [$canv3 create text 3 $y -anchor w -fill $fgcolor \
6354 -text $date -font mainfont -tags text]
6355 if {$selectedline == $row} {
6356 make_secsel $id
6357 }
6358 if {[info exists markedid] && $markedid eq $id} {
6359 make_idmark $id
6360 }
6361 set xr [expr {$xt + [font measure $font $headline]}]
6362 if {$xr > $canvxmax} {
6363 set canvxmax $xr
6364 setcanvscroll
6365 }
6366 }
6367
6368 proc drawcmitrow {row} {
6369 global displayorder rowidlist nrows_drawn
6370 global iddrawn markingmatches
6371 global commitinfo numcommits
6372 global filehighlight fhighlights findpattern nhighlights
6373 global hlview vhighlights
6374 global highlight_related rhighlights
6375
6376 if {$row >= $numcommits} return
6377
6378 set id [lindex $displayorder $row]
6379 if {[info exists hlview] && ![info exists vhighlights($id)]} {
6380 askvhighlight $row $id
6381 }
6382 if {[info exists filehighlight] && ![info exists fhighlights($id)]} {
6383 askfilehighlight $row $id
6384 }
6385 if {$findpattern ne {} && ![info exists nhighlights($id)]} {
6386 askfindhighlight $row $id
6387 }
6388 if {$highlight_related ne [mc "None"] && ![info exists rhighlights($id)]} {
6389 askrelhighlight $row $id
6390 }
6391 if {![info exists iddrawn($id)]} {
6392 set col [lsearch -exact [lindex $rowidlist $row] $id]
6393 if {$col < 0} {
6394 puts "oops, row $row id $id not in list"
6395 return
6396 }
6397 if {![info exists commitinfo($id)]} {
6398 getcommit $id
6399 }
6400 assigncolor $id
6401 drawcmittext $id $row $col
6402 set iddrawn($id) 1
6403 incr nrows_drawn
6404 }
6405 if {$markingmatches} {
6406 markrowmatches $row $id
6407 }
6408 }
6409
6410 proc drawcommits {row {endrow {}}} {
6411 global numcommits iddrawn displayorder curview need_redisplay
6412 global parentlist rowidlist rowfinal uparrowlen downarrowlen nrows_drawn
6413
6414 if {$row < 0} {
6415 set row 0
6416 }
6417 if {$endrow eq {}} {
6418 set endrow $row
6419 }
6420 if {$endrow >= $numcommits} {
6421 set endrow [expr {$numcommits - 1}]
6422 }
6423
6424 set rl1 [expr {$row - $downarrowlen - 3}]
6425 if {$rl1 < 0} {
6426 set rl1 0
6427 }
6428 set ro1 [expr {$row - 3}]
6429 if {$ro1 < 0} {
6430 set ro1 0
6431 }
6432 set r2 [expr {$endrow + $uparrowlen + 3}]
6433 if {$r2 > $numcommits} {
6434 set r2 $numcommits
6435 }
6436 for {set r $rl1} {$r < $r2} {incr r} {
6437 if {[lindex $rowidlist $r] ne {} && [lindex $rowfinal $r]} {
6438 if {$rl1 < $r} {
6439 layoutrows $rl1 $r
6440 }
6441 set rl1 [expr {$r + 1}]
6442 }
6443 }
6444 if {$rl1 < $r} {
6445 layoutrows $rl1 $r
6446 }
6447 optimize_rows $ro1 0 $r2
6448 if {$need_redisplay || $nrows_drawn > 2000} {
6449 clear_display
6450 }
6451
6452 # make the lines join to already-drawn rows either side
6453 set r [expr {$row - 1}]
6454 if {$r < 0 || ![info exists iddrawn([lindex $displayorder $r])]} {
6455 set r $row
6456 }
6457 set er [expr {$endrow + 1}]
6458 if {$er >= $numcommits ||
6459 ![info exists iddrawn([lindex $displayorder $er])]} {
6460 set er $endrow
6461 }
6462 for {} {$r <= $er} {incr r} {
6463 set id [lindex $displayorder $r]
6464 set wasdrawn [info exists iddrawn($id)]
6465 drawcmitrow $r
6466 if {$r == $er} break
6467 set nextid [lindex $displayorder [expr {$r + 1}]]
6468 if {$wasdrawn && [info exists iddrawn($nextid)]} continue
6469 drawparentlinks $id $r
6470
6471 set rowids [lindex $rowidlist $r]
6472 foreach lid $rowids {
6473 if {$lid eq {}} continue
6474 if {[info exists lineend($lid)] && $lineend($lid) > $r} continue
6475 if {$lid eq $id} {
6476 # see if this is the first child of any of its parents
6477 foreach p [lindex $parentlist $r] {
6478 if {[lsearch -exact $rowids $p] < 0} {
6479 # make this line extend up to the child
6480 set lineend($p) [drawlineseg $p $r $er 0]
6481 }
6482 }
6483 } else {
6484 set lineend($lid) [drawlineseg $lid $r $er 1]
6485 }
6486 }
6487 }
6488 }
6489
6490 proc undolayout {row} {
6491 global uparrowlen mingaplen downarrowlen
6492 global rowidlist rowisopt rowfinal need_redisplay
6493
6494 set r [expr {$row - ($uparrowlen + $mingaplen + $downarrowlen)}]
6495 if {$r < 0} {
6496 set r 0
6497 }
6498 if {[llength $rowidlist] > $r} {
6499 incr r -1
6500 set rowidlist [lrange $rowidlist 0 $r]
6501 set rowfinal [lrange $rowfinal 0 $r]
6502 set rowisopt [lrange $rowisopt 0 $r]
6503 set need_redisplay 1
6504 run drawvisible
6505 }
6506 }
6507
6508 proc drawvisible {} {
6509 global canv linespc curview vrowmod selectedline targetrow targetid
6510 global need_redisplay cscroll numcommits
6511
6512 set fs [$canv yview]
6513 set ymax [lindex [$canv cget -scrollregion] 3]
6514 if {$ymax eq {} || $ymax == 0 || $numcommits == 0} return
6515 set f0 [lindex $fs 0]
6516 set f1 [lindex $fs 1]
6517 set y0 [expr {int($f0 * $ymax)}]
6518 set y1 [expr {int($f1 * $ymax)}]
6519
6520 if {[info exists targetid]} {
6521 if {[commitinview $targetid $curview]} {
6522 set r [rowofcommit $targetid]
6523 if {$r != $targetrow} {
6524 # Fix up the scrollregion and change the scrolling position
6525 # now that our target row has moved.
6526 set diff [expr {($r - $targetrow) * $linespc}]
6527 set targetrow $r
6528 setcanvscroll
6529 set ymax [lindex [$canv cget -scrollregion] 3]
6530 incr y0 $diff
6531 incr y1 $diff
6532 set f0 [expr {$y0 / $ymax}]
6533 set f1 [expr {$y1 / $ymax}]
6534 allcanvs yview moveto $f0
6535 $cscroll set $f0 $f1
6536 set need_redisplay 1
6537 }
6538 } else {
6539 unset targetid
6540 }
6541 }
6542
6543 set row [expr {int(($y0 - 3) / $linespc) - 1}]
6544 set endrow [expr {int(($y1 - 3) / $linespc) + 1}]
6545 if {$endrow >= $vrowmod($curview)} {
6546 update_arcrows $curview
6547 }
6548 if {$selectedline ne {} &&
6549 $row <= $selectedline && $selectedline <= $endrow} {
6550 set targetrow $selectedline
6551 } elseif {[info exists targetid]} {
6552 set targetrow [expr {int(($row + $endrow) / 2)}]
6553 }
6554 if {[info exists targetrow]} {
6555 if {$targetrow >= $numcommits} {
6556 set targetrow [expr {$numcommits - 1}]
6557 }
6558 set targetid [commitonrow $targetrow]
6559 }
6560 drawcommits $row $endrow
6561 }
6562
6563 proc clear_display {} {
6564 global iddrawn linesegs need_redisplay nrows_drawn
6565 global vhighlights fhighlights nhighlights rhighlights
6566 global linehtag linentag linedtag boldids boldnameids
6567
6568 allcanvs delete all
6569 unset -nocomplain iddrawn
6570 unset -nocomplain linesegs
6571 unset -nocomplain linehtag
6572 unset -nocomplain linentag
6573 unset -nocomplain linedtag
6574 set boldids {}
6575 set boldnameids {}
6576 unset -nocomplain vhighlights
6577 unset -nocomplain fhighlights
6578 unset -nocomplain nhighlights
6579 unset -nocomplain rhighlights
6580 set need_redisplay 0
6581 set nrows_drawn 0
6582 }
6583
6584 proc findcrossings {id} {
6585 global rowidlist parentlist numcommits displayorder
6586
6587 set cross {}
6588 set ccross {}
6589 foreach {s e} [rowranges $id] {
6590 if {$e >= $numcommits} {
6591 set e [expr {$numcommits - 1}]
6592 }
6593 if {$e <= $s} continue
6594 for {set row $e} {[incr row -1] >= $s} {} {
6595 set x [lsearch -exact [lindex $rowidlist $row] $id]
6596 if {$x < 0} break
6597 set olds [lindex $parentlist $row]
6598 set kid [lindex $displayorder $row]
6599 set kidx [lsearch -exact [lindex $rowidlist $row] $kid]
6600 if {$kidx < 0} continue
6601 set nextrow [lindex $rowidlist [expr {$row + 1}]]
6602 foreach p $olds {
6603 set px [lsearch -exact $nextrow $p]
6604 if {$px < 0} continue
6605 if {($kidx < $x && $x < $px) || ($px < $x && $x < $kidx)} {
6606 if {[lsearch -exact $ccross $p] >= 0} continue
6607 if {$x == $px + ($kidx < $px? -1: 1)} {
6608 lappend ccross $p
6609 } elseif {[lsearch -exact $cross $p] < 0} {
6610 lappend cross $p
6611 }
6612 }
6613 }
6614 }
6615 }
6616 return [concat $ccross {{}} $cross]
6617 }
6618
6619 proc assigncolor {id} {
6620 global colormap colors nextcolor
6621 global parents children children curview
6622
6623 if {[info exists colormap($id)]} return
6624 set ncolors [llength $colors]
6625 if {[info exists children($curview,$id)]} {
6626 set kids $children($curview,$id)
6627 } else {
6628 set kids {}
6629 }
6630 if {[llength $kids] == 1} {
6631 set child [lindex $kids 0]
6632 if {[info exists colormap($child)]
6633 && [llength $parents($curview,$child)] == 1} {
6634 set colormap($id) $colormap($child)
6635 return
6636 }
6637 }
6638 set badcolors {}
6639 set origbad {}
6640 foreach x [findcrossings $id] {
6641 if {$x eq {}} {
6642 # delimiter between corner crossings and other crossings
6643 if {[llength $badcolors] >= $ncolors - 1} break
6644 set origbad $badcolors
6645 }
6646 if {[info exists colormap($x)]
6647 && [lsearch -exact $badcolors $colormap($x)] < 0} {
6648 lappend badcolors $colormap($x)
6649 }
6650 }
6651 if {[llength $badcolors] >= $ncolors} {
6652 set badcolors $origbad
6653 }
6654 set origbad $badcolors
6655 if {[llength $badcolors] < $ncolors - 1} {
6656 foreach child $kids {
6657 if {[info exists colormap($child)]
6658 && [lsearch -exact $badcolors $colormap($child)] < 0} {
6659 lappend badcolors $colormap($child)
6660 }
6661 foreach p $parents($curview,$child) {
6662 if {[info exists colormap($p)]
6663 && [lsearch -exact $badcolors $colormap($p)] < 0} {
6664 lappend badcolors $colormap($p)
6665 }
6666 }
6667 }
6668 if {[llength $badcolors] >= $ncolors} {
6669 set badcolors $origbad
6670 }
6671 }
6672 for {set i 0} {$i <= $ncolors} {incr i} {
6673 set c [lindex $colors $nextcolor]
6674 if {[incr nextcolor] >= $ncolors} {
6675 set nextcolor 0
6676 }
6677 if {[lsearch -exact $badcolors $c]} break
6678 }
6679 set colormap($id) $c
6680 }
6681
6682 proc bindline {t id} {
6683 global canv
6684
6685 $canv bind $t <Enter> "lineenter %x %y $id"
6686 $canv bind $t <Motion> "linemotion %x %y $id"
6687 $canv bind $t <Leave> "lineleave $id"
6688 $canv bind $t <Button-1> "lineclick %x %y $id 1"
6689 }
6690
6691 proc graph_pane_width {} {
6692 global use_ttk
6693
6694 if {$use_ttk} {
6695 set g [.tf.histframe.pwclist sashpos 0]
6696 } else {
6697 set g [.tf.histframe.pwclist sash coord 0]
6698 }
6699 return [lindex $g 0]
6700 }
6701
6702 proc totalwidth {l font extra} {
6703 set tot 0
6704 foreach str $l {
6705 set tot [expr {$tot + [font measure $font $str] + $extra}]
6706 }
6707 return $tot
6708 }
6709
6710 proc drawtags {id x xt y1} {
6711 global idtags idheads idotherrefs mainhead
6712 global linespc lthickness
6713 global canv rowtextx curview fgcolor bgcolor ctxbut
6714 global headbgcolor headfgcolor headoutlinecolor remotebgcolor
6715 global tagbgcolor tagfgcolor tagoutlinecolor
6716 global reflinecolor
6717
6718 set marks {}
6719 set ntags 0
6720 set nheads 0
6721 set singletag 0
6722 set maxtags 3
6723 set maxtagpct 25
6724 set maxwidth [expr {[graph_pane_width] * $maxtagpct / 100}]
6725 set delta [expr {int(0.5 * ($linespc - $lthickness))}]
6726 set extra [expr {$delta + $lthickness + $linespc}]
6727
6728 if {[info exists idtags($id)]} {
6729 set marks $idtags($id)
6730 set ntags [llength $marks]
6731 if {$ntags > $maxtags ||
6732 [totalwidth $marks mainfont $extra] > $maxwidth} {
6733 # show just a single "n tags..." tag
6734 set singletag 1
6735 if {$ntags == 1} {
6736 set marks [list "tag..."]
6737 } else {
6738 set marks [list [format "%d tags..." $ntags]]
6739 }
6740 set ntags 1
6741 }
6742 }
6743 if {[info exists idheads($id)]} {
6744 set marks [concat $marks $idheads($id)]
6745 set nheads [llength $idheads($id)]
6746 }
6747 if {[info exists idotherrefs($id)]} {
6748 set marks [concat $marks $idotherrefs($id)]
6749 }
6750 if {$marks eq {}} {
6751 return $xt
6752 }
6753
6754 set yt [expr {$y1 - 0.5 * $linespc}]
6755 set yb [expr {$yt + $linespc - 1}]
6756 set xvals {}
6757 set wvals {}
6758 set i -1
6759 foreach tag $marks {
6760 incr i
6761 if {$i >= $ntags && $i < $ntags + $nheads && $tag eq $mainhead} {
6762 set wid [font measure mainfontbold $tag]
6763 } else {
6764 set wid [font measure mainfont $tag]
6765 }
6766 lappend xvals $xt
6767 lappend wvals $wid
6768 set xt [expr {$xt + $wid + $extra}]
6769 }
6770 set t [$canv create line $x $y1 [lindex $xvals end] $y1 \
6771 -width $lthickness -fill $reflinecolor -tags tag.$id]
6772 $canv lower $t
6773 foreach tag $marks x $xvals wid $wvals {
6774 set tag_quoted [string map {% %%} $tag]
6775 set xl [expr {$x + $delta}]
6776 set xr [expr {$x + $delta + $wid + $lthickness}]
6777 set font mainfont
6778 if {[incr ntags -1] >= 0} {
6779 # draw a tag
6780 set t [$canv create polygon $x [expr {$yt + $delta}] $xl $yt \
6781 $xr $yt $xr $yb $xl $yb $x [expr {$yb - $delta}] \
6782 -width 1 -outline $tagoutlinecolor -fill $tagbgcolor \
6783 -tags tag.$id]
6784 if {$singletag} {
6785 set tagclick [list showtags $id 1]
6786 } else {
6787 set tagclick [list showtag $tag_quoted 1]
6788 }
6789 $canv bind $t <1> $tagclick
6790 set rowtextx([rowofcommit $id]) [expr {$xr + $linespc}]
6791 } else {
6792 # draw a head or other ref
6793 if {[incr nheads -1] >= 0} {
6794 set col $headbgcolor
6795 if {$tag eq $mainhead} {
6796 set font mainfontbold
6797 }
6798 } else {
6799 set col "#ddddff"
6800 }
6801 set xl [expr {$xl - $delta/2}]
6802 $canv create polygon $x $yt $xr $yt $xr $yb $x $yb \
6803 -width 1 -outline black -fill $col -tags tag.$id
6804 if {[regexp {^(remotes/.*/|remotes/)} $tag match remoteprefix]} {
6805 set rwid [font measure mainfont $remoteprefix]
6806 set xi [expr {$x + 1}]
6807 set yti [expr {$yt + 1}]
6808 set xri [expr {$x + $rwid}]
6809 $canv create polygon $xi $yti $xri $yti $xri $yb $xi $yb \
6810 -width 0 -fill $remotebgcolor -tags tag.$id
6811 }
6812 }
6813 set t [$canv create text $xl $y1 -anchor w -text $tag -fill $headfgcolor \
6814 -font $font -tags [list tag.$id text]]
6815 if {$ntags >= 0} {
6816 $canv bind $t <1> $tagclick
6817 } elseif {$nheads >= 0} {
6818 $canv bind $t $ctxbut [list headmenu %X %Y $id $tag_quoted]
6819 }
6820 }
6821 return $xt
6822 }
6823
6824 proc drawnotesign {xt y} {
6825 global linespc canv fgcolor
6826
6827 set orad [expr {$linespc / 3}]
6828 set t [$canv create rectangle [expr {$xt - $orad}] [expr {$y - $orad}] \
6829 [expr {$xt + $orad - 1}] [expr {$y + $orad - 1}] \
6830 -fill yellow -outline $fgcolor -width 1 -tags circle]
6831 set xt [expr {$xt + $orad * 3}]
6832 return $xt
6833 }
6834
6835 proc xcoord {i level ln} {
6836 global canvx0 xspc1 xspc2
6837
6838 set x [expr {$canvx0 + $i * $xspc1($ln)}]
6839 if {$i > 0 && $i == $level} {
6840 set x [expr {$x + 0.5 * ($xspc2 - $xspc1($ln))}]
6841 } elseif {$i > $level} {
6842 set x [expr {$x + $xspc2 - $xspc1($ln)}]
6843 }
6844 return $x
6845 }
6846
6847 proc show_status {msg} {
6848 global canv fgcolor
6849
6850 clear_display
6851 set_window_title
6852 $canv create text 3 3 -anchor nw -text $msg -font mainfont \
6853 -tags text -fill $fgcolor
6854 }
6855
6856 # Don't change the text pane cursor if it is currently the hand cursor,
6857 # showing that we are over a sha1 ID link.
6858 proc settextcursor {c} {
6859 global ctext curtextcursor
6860
6861 if {[$ctext cget -cursor] == $curtextcursor} {
6862 $ctext config -cursor $c
6863 }
6864 set curtextcursor $c
6865 }
6866
6867 proc nowbusy {what {name {}}} {
6868 global isbusy busyname statusw
6869
6870 if {[array names isbusy] eq {}} {
6871 . config -cursor watch
6872 settextcursor watch
6873 }
6874 set isbusy($what) 1
6875 set busyname($what) $name
6876 if {$name ne {}} {
6877 $statusw conf -text $name
6878 }
6879 }
6880
6881 proc notbusy {what} {
6882 global isbusy maincursor textcursor busyname statusw
6883
6884 catch {
6885 unset isbusy($what)
6886 if {$busyname($what) ne {} &&
6887 [$statusw cget -text] eq $busyname($what)} {
6888 $statusw conf -text {}
6889 }
6890 }
6891 if {[array names isbusy] eq {}} {
6892 . config -cursor $maincursor
6893 settextcursor $textcursor
6894 }
6895 }
6896
6897 proc findmatches {f} {
6898 global findtype findstring
6899 if {$findtype == [mc "Regexp"]} {
6900 set matches [regexp -indices -all -inline $findstring $f]
6901 } else {
6902 set fs $findstring
6903 if {$findtype == [mc "IgnCase"]} {
6904 set f [string tolower $f]
6905 set fs [string tolower $fs]
6906 }
6907 set matches {}
6908 set i 0
6909 set l [string length $fs]
6910 while {[set j [string first $fs $f $i]] >= 0} {
6911 lappend matches [list $j [expr {$j+$l-1}]]
6912 set i [expr {$j + $l}]
6913 }
6914 }
6915 return $matches
6916 }
6917
6918 proc dofind {{dirn 1} {wrap 1}} {
6919 global findstring findstartline findcurline selectedline numcommits
6920 global gdttype filehighlight fh_serial find_dirn findallowwrap
6921
6922 if {[info exists find_dirn]} {
6923 if {$find_dirn == $dirn} return
6924 stopfinding
6925 }
6926 focus .
6927 if {$findstring eq {} || $numcommits == 0} return
6928 if {$selectedline eq {}} {
6929 set findstartline [lindex [visiblerows] [expr {$dirn < 0}]]
6930 } else {
6931 set findstartline $selectedline
6932 }
6933 set findcurline $findstartline
6934 nowbusy finding [mc "Searching"]
6935 if {$gdttype ne [mc "containing:"] && ![info exists filehighlight]} {
6936 after cancel do_file_hl $fh_serial
6937 do_file_hl $fh_serial
6938 }
6939 set find_dirn $dirn
6940 set findallowwrap $wrap
6941 run findmore
6942 }
6943
6944 proc stopfinding {} {
6945 global find_dirn findcurline fprogcoord
6946
6947 if {[info exists find_dirn]} {
6948 unset find_dirn
6949 unset findcurline
6950 notbusy finding
6951 set fprogcoord 0
6952 adjustprogress
6953 }
6954 stopblaming
6955 }
6956
6957 proc findmore {} {
6958 global commitdata commitinfo numcommits findpattern findloc
6959 global findstartline findcurline findallowwrap
6960 global find_dirn gdttype fhighlights fprogcoord
6961 global curview varcorder vrownum varccommits vrowmod
6962
6963 if {![info exists find_dirn]} {
6964 return 0
6965 }
6966 set fldtypes [list [mc "Headline"] [mc "Author"] "" [mc "Committer"] "" [mc "Comments"]]
6967 set l $findcurline
6968 set moretodo 0
6969 if {$find_dirn > 0} {
6970 incr l
6971 if {$l >= $numcommits} {
6972 set l 0
6973 }
6974 if {$l <= $findstartline} {
6975 set lim [expr {$findstartline + 1}]
6976 } else {
6977 set lim $numcommits
6978 set moretodo $findallowwrap
6979 }
6980 } else {
6981 if {$l == 0} {
6982 set l $numcommits
6983 }
6984 incr l -1
6985 if {$l >= $findstartline} {
6986 set lim [expr {$findstartline - 1}]
6987 } else {
6988 set lim -1
6989 set moretodo $findallowwrap
6990 }
6991 }
6992 set n [expr {($lim - $l) * $find_dirn}]
6993 if {$n > 500} {
6994 set n 500
6995 set moretodo 1
6996 }
6997 if {$l + ($find_dirn > 0? $n: 1) > $vrowmod($curview)} {
6998 update_arcrows $curview
6999 }
7000 set found 0
7001 set domore 1
7002 set ai [bsearch $vrownum($curview) $l]
7003 set a [lindex $varcorder($curview) $ai]
7004 set arow [lindex $vrownum($curview) $ai]
7005 set ids [lindex $varccommits($curview,$a)]
7006 set arowend [expr {$arow + [llength $ids]}]
7007 if {$gdttype eq [mc "containing:"]} {
7008 for {} {$n > 0} {incr n -1; incr l $find_dirn} {
7009 if {$l < $arow || $l >= $arowend} {
7010 incr ai $find_dirn
7011 set a [lindex $varcorder($curview) $ai]
7012 set arow [lindex $vrownum($curview) $ai]
7013 set ids [lindex $varccommits($curview,$a)]
7014 set arowend [expr {$arow + [llength $ids]}]
7015 }
7016 set id [lindex $ids [expr {$l - $arow}]]
7017 # shouldn't happen unless git log doesn't give all the commits...
7018 if {![info exists commitdata($id)] ||
7019 ![doesmatch $commitdata($id)]} {
7020 continue
7021 }
7022 if {![info exists commitinfo($id)]} {
7023 getcommit $id
7024 }
7025 set info $commitinfo($id)
7026 foreach f $info ty $fldtypes {
7027 if {$ty eq ""} continue
7028 if {($findloc eq [mc "All fields"] || $findloc eq $ty) &&
7029 [doesmatch $f]} {
7030 set found 1
7031 break
7032 }
7033 }
7034 if {$found} break
7035 }
7036 } else {
7037 for {} {$n > 0} {incr n -1; incr l $find_dirn} {
7038 if {$l < $arow || $l >= $arowend} {
7039 incr ai $find_dirn
7040 set a [lindex $varcorder($curview) $ai]
7041 set arow [lindex $vrownum($curview) $ai]
7042 set ids [lindex $varccommits($curview,$a)]
7043 set arowend [expr {$arow + [llength $ids]}]
7044 }
7045 set id [lindex $ids [expr {$l - $arow}]]
7046 if {![info exists fhighlights($id)]} {
7047 # this sets fhighlights($id) to -1
7048 askfilehighlight $l $id
7049 }
7050 if {$fhighlights($id) > 0} {
7051 set found $domore
7052 break
7053 }
7054 if {$fhighlights($id) < 0} {
7055 if {$domore} {
7056 set domore 0
7057 set findcurline [expr {$l - $find_dirn}]
7058 }
7059 }
7060 }
7061 }
7062 if {$found || ($domore && !$moretodo)} {
7063 unset findcurline
7064 unset find_dirn
7065 notbusy finding
7066 set fprogcoord 0
7067 adjustprogress
7068 if {$found} {
7069 findselectline $l
7070 } else {
7071 bell
7072 }
7073 return 0
7074 }
7075 if {!$domore} {
7076 flushhighlights
7077 } else {
7078 set findcurline [expr {$l - $find_dirn}]
7079 }
7080 set n [expr {($findcurline - $findstartline) * $find_dirn - 1}]
7081 if {$n < 0} {
7082 incr n $numcommits
7083 }
7084 set fprogcoord [expr {$n * 1.0 / $numcommits}]
7085 adjustprogress
7086 return $domore
7087 }
7088
7089 proc findselectline {l} {
7090 global findloc commentend ctext findcurline markingmatches gdttype
7091
7092 set markingmatches [expr {$gdttype eq [mc "containing:"]}]
7093 set findcurline $l
7094 selectline $l 1
7095 if {$markingmatches &&
7096 ($findloc eq [mc "All fields"] || $findloc eq [mc "Comments"])} {
7097 # highlight the matches in the comments
7098 set f [$ctext get 1.0 $commentend]
7099 set matches [findmatches $f]
7100 foreach match $matches {
7101 set start [lindex $match 0]
7102 set end [expr {[lindex $match 1] + 1}]
7103 $ctext tag add found "1.0 + $start c" "1.0 + $end c"
7104 }
7105 }
7106 drawvisible
7107 }
7108
7109 # mark the bits of a headline or author that match a find string
7110 proc markmatches {canv l str tag matches font row} {
7111 global selectedline foundbgcolor
7112
7113 set bbox [$canv bbox $tag]
7114 set x0 [lindex $bbox 0]
7115 set y0 [lindex $bbox 1]
7116 set y1 [lindex $bbox 3]
7117 foreach match $matches {
7118 set start [lindex $match 0]
7119 set end [lindex $match 1]
7120 if {$start > $end} continue
7121 set xoff [font measure $font [string range $str 0 [expr {$start-1}]]]
7122 set xlen [font measure $font [string range $str 0 [expr {$end}]]]
7123 set t [$canv create rect [expr {$x0+$xoff}] $y0 \
7124 [expr {$x0+$xlen+2}] $y1 \
7125 -outline {} -tags [list match$l matches] -fill $foundbgcolor]
7126 $canv lower $t
7127 if {$row == $selectedline} {
7128 $canv raise $t secsel
7129 }
7130 }
7131 }
7132
7133 proc unmarkmatches {} {
7134 global markingmatches
7135
7136 allcanvs delete matches
7137 set markingmatches 0
7138 stopfinding
7139 }
7140
7141 proc selcanvline {w x y} {
7142 global canv canvy0 ctext linespc
7143 global rowtextx
7144 set ymax [lindex [$canv cget -scrollregion] 3]
7145 if {$ymax == {}} return
7146 set yfrac [lindex [$canv yview] 0]
7147 set y [expr {$y + $yfrac * $ymax}]
7148 set l [expr {int(($y - $canvy0) / $linespc + 0.5)}]
7149 if {$l < 0} {
7150 set l 0
7151 }
7152 if {$w eq $canv} {
7153 set xmax [lindex [$canv cget -scrollregion] 2]
7154 set xleft [expr {[lindex [$canv xview] 0] * $xmax}]
7155 if {![info exists rowtextx($l)] || $xleft + $x < $rowtextx($l)} return
7156 }
7157 unmarkmatches
7158 selectline $l 1
7159 }
7160
7161 proc commit_descriptor {p} {
7162 global commitinfo
7163 if {![info exists commitinfo($p)]} {
7164 getcommit $p
7165 }
7166 set l "..."
7167 if {[llength $commitinfo($p)] > 1} {
7168 set l [lindex $commitinfo($p) 0]
7169 }
7170 return "$p ($l)\n"
7171 }
7172
7173 # append some text to the ctext widget, and make any SHA1 ID
7174 # that we know about be a clickable link.
7175 # Also look for URLs of the form "http[s]://..." and make them web links.
7176 proc appendwithlinks {text tags} {
7177 global ctext linknum curview
7178
7179 set start [$ctext index "end - 1c"]
7180 $ctext insert end $text $tags
7181 set links [regexp -indices -all -inline {(?:\m|-g)[0-9a-f]{6,40}\M} $text]
7182 foreach l $links {
7183 set s [lindex $l 0]
7184 set e [lindex $l 1]
7185 set linkid [string range $text $s $e]
7186 incr e
7187 $ctext tag delete link$linknum
7188 $ctext tag add link$linknum "$start + $s c" "$start + $e c"
7189 setlink $linkid link$linknum
7190 incr linknum
7191 }
7192 set wlinks [regexp -indices -all -inline -line \
7193 {https?://[^[:space:]]+} $text]
7194 foreach l $wlinks {
7195 set s2 [lindex $l 0]
7196 set e2 [lindex $l 1]
7197 set url [string range $text $s2 $e2]
7198 incr e2
7199 $ctext tag delete link$linknum
7200 $ctext tag add link$linknum "$start + $s2 c" "$start + $e2 c"
7201 setwlink $url link$linknum
7202 incr linknum
7203 }
7204 }
7205
7206 proc setlink {id lk} {
7207 global curview ctext pendinglinks
7208 global linkfgcolor
7209
7210 if {[string range $id 0 1] eq "-g"} {
7211 set id [string range $id 2 end]
7212 }
7213
7214 set known 0
7215 if {[string length $id] < 40} {
7216 set matches [longid $id]
7217 if {[llength $matches] > 0} {
7218 if {[llength $matches] > 1} return
7219 set known 1
7220 set id [lindex $matches 0]
7221 }
7222 } else {
7223 set known [commitinview $id $curview]
7224 }
7225 if {$known} {
7226 $ctext tag conf $lk -foreground $linkfgcolor -underline 1
7227 $ctext tag bind $lk <1> [list selbyid $id]
7228 $ctext tag bind $lk <Enter> {linkcursor %W 1}
7229 $ctext tag bind $lk <Leave> {linkcursor %W -1}
7230 } else {
7231 lappend pendinglinks($id) $lk
7232 interestedin $id {makelink %P}
7233 }
7234 }
7235
7236 proc setwlink {url lk} {
7237 global ctext
7238 global linkfgcolor
7239 global web_browser
7240
7241 if {$web_browser eq {}} return
7242 $ctext tag conf $lk -foreground $linkfgcolor -underline 1
7243 $ctext tag bind $lk <1> [list browseweb $url]
7244 $ctext tag bind $lk <Enter> {linkcursor %W 1}
7245 $ctext tag bind $lk <Leave> {linkcursor %W -1}
7246 }
7247
7248 proc appendshortlink {id {pre {}} {post {}}} {
7249 global ctext linknum
7250
7251 $ctext insert end $pre
7252 $ctext tag delete link$linknum
7253 $ctext insert end [string range $id 0 7] link$linknum
7254 $ctext insert end $post
7255 setlink $id link$linknum
7256 incr linknum
7257 }
7258
7259 proc makelink {id} {
7260 global pendinglinks
7261
7262 if {![info exists pendinglinks($id)]} return
7263 foreach lk $pendinglinks($id) {
7264 setlink $id $lk
7265 }
7266 unset pendinglinks($id)
7267 }
7268
7269 proc linkcursor {w inc} {
7270 global linkentercount curtextcursor
7271
7272 if {[incr linkentercount $inc] > 0} {
7273 $w configure -cursor hand2
7274 } else {
7275 $w configure -cursor $curtextcursor
7276 if {$linkentercount < 0} {
7277 set linkentercount 0
7278 }
7279 }
7280 }
7281
7282 proc browseweb {url} {
7283 global web_browser
7284
7285 if {$web_browser eq {}} return
7286 # Use eval here in case $web_browser is a command plus some arguments
7287 if {[catch {eval exec $web_browser [list $url] &} err]} {
7288 error_popup "[mc "Error starting web browser:"] $err"
7289 }
7290 }
7291
7292 proc viewnextline {dir} {
7293 global canv linespc
7294
7295 $canv delete hover
7296 set ymax [lindex [$canv cget -scrollregion] 3]
7297 set wnow [$canv yview]
7298 set wtop [expr {[lindex $wnow 0] * $ymax}]
7299 set newtop [expr {$wtop + $dir * $linespc}]
7300 if {$newtop < 0} {
7301 set newtop 0
7302 } elseif {$newtop > $ymax} {
7303 set newtop $ymax
7304 }
7305 allcanvs yview moveto [expr {$newtop * 1.0 / $ymax}]
7306 }
7307
7308 # add a list of tag or branch names at position pos
7309 # returns the number of names inserted
7310 proc appendrefs {pos ids var} {
7311 global ctext linknum curview $var maxrefs visiblerefs mainheadid
7312
7313 if {[catch {$ctext index $pos}]} {
7314 return 0
7315 }
7316 $ctext conf -state normal
7317 $ctext delete $pos "$pos lineend"
7318 set tags {}
7319 foreach id $ids {
7320 foreach tag [set $var\($id\)] {
7321 lappend tags [list $tag $id]
7322 }
7323 }
7324
7325 set sep {}
7326 set tags [lsort -index 0 -decreasing $tags]
7327 set nutags 0
7328
7329 if {[llength $tags] > $maxrefs} {
7330 # If we are displaying heads, and there are too many,
7331 # see if there are some important heads to display.
7332 # Currently that are the current head and heads listed in $visiblerefs option
7333 set itags {}
7334 if {$var eq "idheads"} {
7335 set utags {}
7336 foreach ti $tags {
7337 set hname [lindex $ti 0]
7338 set id [lindex $ti 1]
7339 if {([lsearch -exact $visiblerefs $hname] != -1 || $id eq $mainheadid) &&
7340 [llength $itags] < $maxrefs} {
7341 lappend itags $ti
7342 } else {
7343 lappend utags $ti
7344 }
7345 }
7346 set tags $utags
7347 }
7348 if {$itags ne {}} {
7349 set str [mc "and many more"]
7350 set sep " "
7351 } else {
7352 set str [mc "many"]
7353 }
7354 $ctext insert $pos "$str ([llength $tags])"
7355 set nutags [llength $tags]
7356 set tags $itags
7357 }
7358
7359 foreach ti $tags {
7360 set id [lindex $ti 1]
7361 set lk link$linknum
7362 incr linknum
7363 $ctext tag delete $lk
7364 $ctext insert $pos $sep
7365 $ctext insert $pos [lindex $ti 0] $lk
7366 setlink $id $lk
7367 set sep ", "
7368 }
7369 $ctext tag add wwrap "$pos linestart" "$pos lineend"
7370 $ctext conf -state disabled
7371 return [expr {[llength $tags] + $nutags}]
7372 }
7373
7374 # called when we have finished computing the nearby tags
7375 proc dispneartags {delay} {
7376 global selectedline currentid showneartags tagphase
7377
7378 if {$selectedline eq {} || !$showneartags} return
7379 after cancel dispnexttag
7380 if {$delay} {
7381 after 200 dispnexttag
7382 set tagphase -1
7383 } else {
7384 after idle dispnexttag
7385 set tagphase 0
7386 }
7387 }
7388
7389 proc dispnexttag {} {
7390 global selectedline currentid showneartags tagphase ctext
7391
7392 if {$selectedline eq {} || !$showneartags} return
7393 switch -- $tagphase {
7394 0 {
7395 set dtags [desctags $currentid]
7396 if {$dtags ne {}} {
7397 appendrefs precedes $dtags idtags
7398 }
7399 }
7400 1 {
7401 set atags [anctags $currentid]
7402 if {$atags ne {}} {
7403 appendrefs follows $atags idtags
7404 }
7405 }
7406 2 {
7407 set dheads [descheads $currentid]
7408 if {$dheads ne {}} {
7409 if {[appendrefs branch $dheads idheads] > 1
7410 && [$ctext get "branch -3c"] eq "h"} {
7411 # turn "Branch" into "Branches"
7412 $ctext conf -state normal
7413 $ctext insert "branch -2c" "es"
7414 $ctext conf -state disabled
7415 }
7416 }
7417 }
7418 }
7419 if {[incr tagphase] <= 2} {
7420 after idle dispnexttag
7421 }
7422 }
7423
7424 proc make_secsel {id} {
7425 global linehtag linentag linedtag canv canv2 canv3
7426
7427 if {![info exists linehtag($id)]} return
7428 $canv delete secsel
7429 set t [eval $canv create rect [$canv bbox $linehtag($id)] -outline {{}} \
7430 -tags secsel -fill [$canv cget -selectbackground]]
7431 $canv lower $t
7432 $canv2 delete secsel
7433 set t [eval $canv2 create rect [$canv2 bbox $linentag($id)] -outline {{}} \
7434 -tags secsel -fill [$canv2 cget -selectbackground]]
7435 $canv2 lower $t
7436 $canv3 delete secsel
7437 set t [eval $canv3 create rect [$canv3 bbox $linedtag($id)] -outline {{}} \
7438 -tags secsel -fill [$canv3 cget -selectbackground]]
7439 $canv3 lower $t
7440 }
7441
7442 proc make_idmark {id} {
7443 global linehtag canv fgcolor
7444
7445 if {![info exists linehtag($id)]} return
7446 $canv delete markid
7447 set t [eval $canv create rect [$canv bbox $linehtag($id)] \
7448 -tags markid -outline $fgcolor]
7449 $canv raise $t
7450 }
7451
7452 proc selectline {l isnew {desired_loc {}} {switch_to_patch 0}} {
7453 global canv ctext commitinfo selectedline
7454 global canvy0 linespc parents children curview
7455 global currentid sha1entry
7456 global commentend idtags linknum
7457 global mergemax numcommits pending_select
7458 global cmitmode showneartags allcommits
7459 global targetrow targetid lastscrollrows
7460 global autocopy autoselect autosellen jump_to_here
7461 global vinlinediff
7462
7463 unset -nocomplain pending_select
7464 $canv delete hover
7465 normalline
7466 unsel_reflist
7467 stopfinding
7468 if {$l < 0 || $l >= $numcommits} return
7469 set id [commitonrow $l]
7470 set targetid $id
7471 set targetrow $l
7472 set selectedline $l
7473 set currentid $id
7474 if {$lastscrollrows < $numcommits} {
7475 setcanvscroll
7476 }
7477
7478 if {$cmitmode ne "patch" && $switch_to_patch} {
7479 set cmitmode "patch"
7480 }
7481
7482 set y [expr {$canvy0 + $l * $linespc}]
7483 set ymax [lindex [$canv cget -scrollregion] 3]
7484 set ytop [expr {$y - $linespc - 1}]
7485 set ybot [expr {$y + $linespc + 1}]
7486 set wnow [$canv yview]
7487 set wtop [expr {[lindex $wnow 0] * $ymax}]
7488 set wbot [expr {[lindex $wnow 1] * $ymax}]
7489 set wh [expr {$wbot - $wtop}]
7490 set newtop $wtop
7491 if {$ytop < $wtop} {
7492 if {$ybot < $wtop} {
7493 set newtop [expr {$y - $wh / 2.0}]
7494 } else {
7495 set newtop $ytop
7496 if {$newtop > $wtop - $linespc} {
7497 set newtop [expr {$wtop - $linespc}]
7498 }
7499 }
7500 } elseif {$ybot > $wbot} {
7501 if {$ytop > $wbot} {
7502 set newtop [expr {$y - $wh / 2.0}]
7503 } else {
7504 set newtop [expr {$ybot - $wh}]
7505 if {$newtop < $wtop + $linespc} {
7506 set newtop [expr {$wtop + $linespc}]
7507 }
7508 }
7509 }
7510 if {$newtop != $wtop} {
7511 if {$newtop < 0} {
7512 set newtop 0
7513 }
7514 allcanvs yview moveto [expr {$newtop * 1.0 / $ymax}]
7515 drawvisible
7516 }
7517
7518 make_secsel $id
7519
7520 if {$isnew} {
7521 addtohistory [list selbyid $id 0] savecmitpos
7522 }
7523
7524 $sha1entry delete 0 end
7525 $sha1entry insert 0 $id
7526 if {$autoselect && [haveselectionclipboard]} {
7527 $sha1entry selection range 0 $autosellen
7528 }
7529 if {$autocopy} {
7530 clipboard clear
7531 clipboard append [string range $id 0 [expr $autosellen - 1]]
7532 }
7533 rhighlight_sel $id
7534
7535 $ctext conf -state normal
7536 clear_ctext
7537 set linknum 0
7538 if {![info exists commitinfo($id)]} {
7539 getcommit $id
7540 }
7541 set info $commitinfo($id)
7542 set date [formatdate [lindex $info 2]]
7543 $ctext insert end "[mc "Author"]: [lindex $info 1] $date\n"
7544 set date [formatdate [lindex $info 4]]
7545 $ctext insert end "[mc "Committer"]: [lindex $info 3] $date\n"
7546 if {[info exists idtags($id)]} {
7547 $ctext insert end [mc "Tags:"]
7548 foreach tag $idtags($id) {
7549 $ctext insert end " $tag"
7550 }
7551 $ctext insert end "\n"
7552 }
7553
7554 set headers {}
7555 set olds $parents($curview,$id)
7556 if {[llength $olds] > 1} {
7557 set np 0
7558 foreach p $olds {
7559 if {$np >= $mergemax} {
7560 set tag mmax
7561 } else {
7562 set tag m$np
7563 }
7564 $ctext insert end "[mc "Parent"]: " $tag
7565 appendwithlinks [commit_descriptor $p] {}
7566 incr np
7567 }
7568 } else {
7569 foreach p $olds {
7570 append headers "[mc "Parent"]: [commit_descriptor $p]"
7571 }
7572 }
7573
7574 foreach c $children($curview,$id) {
7575 append headers "[mc "Child"]: [commit_descriptor $c]"
7576 }
7577
7578 # make anything that looks like a SHA1 ID be a clickable link
7579 appendwithlinks $headers {}
7580 if {$showneartags} {
7581 if {![info exists allcommits]} {
7582 getallcommits
7583 }
7584 $ctext insert end "[mc "Branch"]: "
7585 $ctext mark set branch "end -1c"
7586 $ctext mark gravity branch left
7587 $ctext insert end "\n[mc "Follows"]: "
7588 $ctext mark set follows "end -1c"
7589 $ctext mark gravity follows left
7590 $ctext insert end "\n[mc "Precedes"]: "
7591 $ctext mark set precedes "end -1c"
7592 $ctext mark gravity precedes left
7593 $ctext insert end "\n"
7594 dispneartags 1
7595 }
7596 $ctext insert end "\n"
7597 set comment [lindex $info 5]
7598 if {[string first "\r" $comment] >= 0} {
7599 set comment [string map {"\r" "\n "} $comment]
7600 }
7601 appendwithlinks $comment {comment}
7602
7603 $ctext tag remove found 1.0 end
7604 $ctext conf -state disabled
7605 set commentend [$ctext index "end - 1c"]
7606
7607 set jump_to_here $desired_loc
7608 init_flist [mc "Comments"]
7609 if {$cmitmode eq "tree"} {
7610 gettree $id
7611 } elseif {$vinlinediff($curview) == 1} {
7612 showinlinediff $id
7613 } elseif {[llength $olds] <= 1} {
7614 startdiff $id
7615 } else {
7616 mergediff $id
7617 }
7618 }
7619
7620 proc selfirstline {} {
7621 unmarkmatches
7622 selectline 0 1
7623 }
7624
7625 proc sellastline {} {
7626 global numcommits
7627 unmarkmatches
7628 set l [expr {$numcommits - 1}]
7629 selectline $l 1
7630 }
7631
7632 proc selnextline {dir} {
7633 global selectedline
7634 focus .
7635 if {$selectedline eq {}} return
7636 set l [expr {$selectedline + $dir}]
7637 unmarkmatches
7638 selectline $l 1
7639 }
7640
7641 proc selnextpage {dir} {
7642 global canv linespc selectedline numcommits
7643
7644 set lpp [expr {([winfo height $canv] - 2) / $linespc}]
7645 if {$lpp < 1} {
7646 set lpp 1
7647 }
7648 allcanvs yview scroll [expr {$dir * $lpp}] units
7649 drawvisible
7650 if {$selectedline eq {}} return
7651 set l [expr {$selectedline + $dir * $lpp}]
7652 if {$l < 0} {
7653 set l 0
7654 } elseif {$l >= $numcommits} {
7655 set l [expr $numcommits - 1]
7656 }
7657 unmarkmatches
7658 selectline $l 1
7659 }
7660
7661 proc unselectline {} {
7662 global selectedline currentid
7663
7664 set selectedline {}
7665 unset -nocomplain currentid
7666 allcanvs delete secsel
7667 rhighlight_none
7668 }
7669
7670 proc reselectline {} {
7671 global selectedline
7672
7673 if {$selectedline ne {}} {
7674 selectline $selectedline 0
7675 }
7676 }
7677
7678 proc addtohistory {cmd {saveproc {}}} {
7679 global history historyindex curview
7680
7681 unset_posvars
7682 save_position
7683 set elt [list $curview $cmd $saveproc {}]
7684 if {$historyindex > 0
7685 && [lindex $history [expr {$historyindex - 1}]] == $elt} {
7686 return
7687 }
7688
7689 if {$historyindex < [llength $history]} {
7690 set history [lreplace $history $historyindex end $elt]
7691 } else {
7692 lappend history $elt
7693 }
7694 incr historyindex
7695 if {$historyindex > 1} {
7696 .tf.bar.leftbut conf -state normal
7697 } else {
7698 .tf.bar.leftbut conf -state disabled
7699 }
7700 .tf.bar.rightbut conf -state disabled
7701 }
7702
7703 # save the scrolling position of the diff display pane
7704 proc save_position {} {
7705 global historyindex history
7706
7707 if {$historyindex < 1} return
7708 set hi [expr {$historyindex - 1}]
7709 set fn [lindex $history $hi 2]
7710 if {$fn ne {}} {
7711 lset history $hi 3 [eval $fn]
7712 }
7713 }
7714
7715 proc unset_posvars {} {
7716 global last_posvars
7717
7718 if {[info exists last_posvars]} {
7719 foreach {var val} $last_posvars {
7720 global $var
7721 unset -nocomplain $var
7722 }
7723 unset last_posvars
7724 }
7725 }
7726
7727 proc godo {elt} {
7728 global curview last_posvars
7729
7730 set view [lindex $elt 0]
7731 set cmd [lindex $elt 1]
7732 set pv [lindex $elt 3]
7733 if {$curview != $view} {
7734 showview $view
7735 }
7736 unset_posvars
7737 foreach {var val} $pv {
7738 global $var
7739 set $var $val
7740 }
7741 set last_posvars $pv
7742 eval $cmd
7743 }
7744
7745 proc goback {} {
7746 global history historyindex
7747 focus .
7748
7749 if {$historyindex > 1} {
7750 save_position
7751 incr historyindex -1
7752 godo [lindex $history [expr {$historyindex - 1}]]
7753 .tf.bar.rightbut conf -state normal
7754 }
7755 if {$historyindex <= 1} {
7756 .tf.bar.leftbut conf -state disabled
7757 }
7758 }
7759
7760 proc goforw {} {
7761 global history historyindex
7762 focus .
7763
7764 if {$historyindex < [llength $history]} {
7765 save_position
7766 set cmd [lindex $history $historyindex]
7767 incr historyindex
7768 godo $cmd
7769 .tf.bar.leftbut conf -state normal
7770 }
7771 if {$historyindex >= [llength $history]} {
7772 .tf.bar.rightbut conf -state disabled
7773 }
7774 }
7775
7776 proc go_to_parent {i} {
7777 global parents curview targetid
7778 set ps $parents($curview,$targetid)
7779 if {[llength $ps] >= $i} {
7780 selbyid [lindex $ps [expr $i - 1]]
7781 }
7782 }
7783
7784 proc gettree {id} {
7785 global treefilelist treeidlist diffids diffmergeid treepending
7786 global nullid nullid2
7787
7788 set diffids $id
7789 unset -nocomplain diffmergeid
7790 if {![info exists treefilelist($id)]} {
7791 if {![info exists treepending]} {
7792 if {$id eq $nullid} {
7793 set cmd [list | git ls-files]
7794 } elseif {$id eq $nullid2} {
7795 set cmd [list | git ls-files --stage -t]
7796 } else {
7797 set cmd [list | git ls-tree -r $id]
7798 }
7799 if {[catch {set gtf [open $cmd r]}]} {
7800 return
7801 }
7802 set treepending $id
7803 set treefilelist($id) {}
7804 set treeidlist($id) {}
7805 fconfigure $gtf -blocking 0 -encoding binary
7806 filerun $gtf [list gettreeline $gtf $id]
7807 }
7808 } else {
7809 setfilelist $id
7810 }
7811 }
7812
7813 proc gettreeline {gtf id} {
7814 global treefilelist treeidlist treepending cmitmode diffids nullid nullid2
7815
7816 set nl 0
7817 while {[incr nl] <= 1000 && [gets $gtf line] >= 0} {
7818 if {$diffids eq $nullid} {
7819 set fname $line
7820 } else {
7821 set i [string first "\t" $line]
7822 if {$i < 0} continue
7823 set fname [string range $line [expr {$i+1}] end]
7824 set line [string range $line 0 [expr {$i-1}]]
7825 if {$diffids ne $nullid2 && [lindex $line 1] ne "blob"} continue
7826 set sha1 [lindex $line 2]
7827 lappend treeidlist($id) $sha1
7828 }
7829 if {[string index $fname 0] eq "\""} {
7830 set fname [lindex $fname 0]
7831 }
7832 set fname [encoding convertfrom utf-8 $fname]
7833 lappend treefilelist($id) $fname
7834 }
7835 if {![eof $gtf]} {
7836 return [expr {$nl >= 1000? 2: 1}]
7837 }
7838 close $gtf
7839 unset treepending
7840 if {$cmitmode ne "tree"} {
7841 if {![info exists diffmergeid]} {
7842 gettreediffs $diffids
7843 }
7844 } elseif {$id ne $diffids} {
7845 gettree $diffids
7846 } else {
7847 setfilelist $id
7848 }
7849 return 0
7850 }
7851
7852 proc showfile {f} {
7853 global treefilelist treeidlist diffids nullid nullid2
7854 global ctext_file_names ctext_file_lines
7855 global ctext commentend
7856
7857 set i [lsearch -exact $treefilelist($diffids) $f]
7858 if {$i < 0} {
7859 puts "oops, $f not in list for id $diffids"
7860 return
7861 }
7862 if {$diffids eq $nullid} {
7863 if {[catch {set bf [open $f r]} err]} {
7864 puts "oops, can't read $f: $err"
7865 return
7866 }
7867 } else {
7868 set blob [lindex $treeidlist($diffids) $i]
7869 if {[catch {set bf [open [concat | git cat-file blob $blob] r]} err]} {
7870 puts "oops, error reading blob $blob: $err"
7871 return
7872 }
7873 }
7874 fconfigure $bf -blocking 0 -encoding [get_path_encoding $f]
7875 filerun $bf [list getblobline $bf $diffids]
7876 $ctext config -state normal
7877 clear_ctext $commentend
7878 lappend ctext_file_names $f
7879 lappend ctext_file_lines [lindex [split $commentend "."] 0]
7880 $ctext insert end "\n"
7881 $ctext insert end "$f\n" filesep
7882 $ctext config -state disabled
7883 $ctext yview $commentend
7884 settabs 0
7885 }
7886
7887 proc getblobline {bf id} {
7888 global diffids cmitmode ctext
7889
7890 if {$id ne $diffids || $cmitmode ne "tree"} {
7891 catch {close $bf}
7892 return 0
7893 }
7894 $ctext config -state normal
7895 set nl 0
7896 while {[incr nl] <= 1000 && [gets $bf line] >= 0} {
7897 $ctext insert end "$line\n"
7898 }
7899 if {[eof $bf]} {
7900 global jump_to_here ctext_file_names commentend
7901
7902 # delete last newline
7903 $ctext delete "end - 2c" "end - 1c"
7904 close $bf
7905 if {$jump_to_here ne {} &&
7906 [lindex $jump_to_here 0] eq [lindex $ctext_file_names 0]} {
7907 set lnum [expr {[lindex $jump_to_here 1] +
7908 [lindex [split $commentend .] 0]}]
7909 mark_ctext_line $lnum
7910 }
7911 $ctext config -state disabled
7912 return 0
7913 }
7914 $ctext config -state disabled
7915 return [expr {$nl >= 1000? 2: 1}]
7916 }
7917
7918 proc mark_ctext_line {lnum} {
7919 global ctext markbgcolor
7920
7921 $ctext tag delete omark
7922 $ctext tag add omark $lnum.0 "$lnum.0 + 1 line"
7923 $ctext tag conf omark -background $markbgcolor
7924 $ctext see $lnum.0
7925 }
7926
7927 proc mergediff {id} {
7928 global diffmergeid
7929 global diffids treediffs
7930 global parents curview
7931
7932 set diffmergeid $id
7933 set diffids $id
7934 set treediffs($id) {}
7935 set np [llength $parents($curview,$id)]
7936 settabs $np
7937 getblobdiffs $id
7938 }
7939
7940 proc startdiff {ids} {
7941 global treediffs diffids treepending diffmergeid nullid nullid2
7942
7943 settabs 1
7944 set diffids $ids
7945 unset -nocomplain diffmergeid
7946 if {![info exists treediffs($ids)] ||
7947 [lsearch -exact $ids $nullid] >= 0 ||
7948 [lsearch -exact $ids $nullid2] >= 0} {
7949 if {![info exists treepending]} {
7950 gettreediffs $ids
7951 }
7952 } else {
7953 addtocflist $ids
7954 }
7955 }
7956
7957 proc showinlinediff {ids} {
7958 global commitinfo commitdata ctext
7959 global treediffs
7960
7961 set info $commitinfo($ids)
7962 set diff [lindex $info 7]
7963 set difflines [split $diff "\n"]
7964
7965 initblobdiffvars
7966 set treediff {}
7967
7968 set inhdr 0
7969 foreach line $difflines {
7970 if {![string compare -length 5 "diff " $line]} {
7971 set inhdr 1
7972 } elseif {$inhdr && ![string compare -length 4 "+++ " $line]} {
7973 # offset also accounts for the b/ prefix
7974 lappend treediff [string range $line 6 end]
7975 set inhdr 0
7976 }
7977 }
7978
7979 set treediffs($ids) $treediff
7980 add_flist $treediff
7981
7982 $ctext conf -state normal
7983 foreach line $difflines {
7984 parseblobdiffline $ids $line
7985 }
7986 maybe_scroll_ctext 1
7987 $ctext conf -state disabled
7988 }
7989
7990 # If the filename (name) is under any of the passed filter paths
7991 # then return true to include the file in the listing.
7992 proc path_filter {filter name} {
7993 set worktree [gitworktree]
7994 foreach p $filter {
7995 set fq_p [file normalize $p]
7996 set fq_n [file normalize [file join $worktree $name]]
7997 if {[string match [file normalize $fq_p]* $fq_n]} {
7998 return 1
7999 }
8000 }
8001 return 0
8002 }
8003
8004 proc addtocflist {ids} {
8005 global treediffs
8006
8007 add_flist $treediffs($ids)
8008 getblobdiffs $ids
8009 }
8010
8011 proc diffcmd {ids flags} {
8012 global log_showroot nullid nullid2 git_version
8013
8014 set i [lsearch -exact $ids $nullid]
8015 set j [lsearch -exact $ids $nullid2]
8016 if {$i >= 0} {
8017 if {[llength $ids] > 1 && $j < 0} {
8018 # comparing working directory with some specific revision
8019 set cmd [concat | git diff-index $flags]
8020 if {$i == 0} {
8021 lappend cmd -R [lindex $ids 1]
8022 } else {
8023 lappend cmd [lindex $ids 0]
8024 }
8025 } else {
8026 # comparing working directory with index
8027 set cmd [concat | git diff-files $flags]
8028 if {$j == 1} {
8029 lappend cmd -R
8030 }
8031 }
8032 } elseif {$j >= 0} {
8033 if {[package vcompare $git_version "1.7.2"] >= 0} {
8034 set flags "$flags --ignore-submodules=dirty"
8035 }
8036 set cmd [concat | git diff-index --cached $flags]
8037 if {[llength $ids] > 1} {
8038 # comparing index with specific revision
8039 if {$j == 0} {
8040 lappend cmd -R [lindex $ids 1]
8041 } else {
8042 lappend cmd [lindex $ids 0]
8043 }
8044 } else {
8045 # comparing index with HEAD
8046 lappend cmd HEAD
8047 }
8048 } else {
8049 if {$log_showroot} {
8050 lappend flags --root
8051 }
8052 set cmd [concat | git diff-tree -r $flags $ids]
8053 }
8054 return $cmd
8055 }
8056
8057 proc gettreediffs {ids} {
8058 global treediff treepending limitdiffs vfilelimit curview
8059
8060 set cmd [diffcmd $ids {--no-commit-id}]
8061 if {$limitdiffs && $vfilelimit($curview) ne {}} {
8062 set cmd [concat $cmd -- $vfilelimit($curview)]
8063 }
8064 if {[catch {set gdtf [open $cmd r]}]} return
8065
8066 set treepending $ids
8067 set treediff {}
8068 fconfigure $gdtf -blocking 0 -encoding binary
8069 filerun $gdtf [list gettreediffline $gdtf $ids]
8070 }
8071
8072 proc gettreediffline {gdtf ids} {
8073 global treediff treediffs treepending diffids diffmergeid
8074 global cmitmode vfilelimit curview limitdiffs perfile_attrs
8075
8076 set nr 0
8077 set sublist {}
8078 set max 1000
8079 if {$perfile_attrs} {
8080 # cache_gitattr is slow, and even slower on win32 where we
8081 # have to invoke it for only about 30 paths at a time
8082 set max 500
8083 if {[tk windowingsystem] == "win32"} {
8084 set max 120
8085 }
8086 }
8087 while {[incr nr] <= $max && [gets $gdtf line] >= 0} {
8088 set i [string first "\t" $line]
8089 if {$i >= 0} {
8090 set file [string range $line [expr {$i+1}] end]
8091 if {[string index $file 0] eq "\""} {
8092 set file [lindex $file 0]
8093 }
8094 set file [encoding convertfrom utf-8 $file]
8095 if {$file ne [lindex $treediff end]} {
8096 lappend treediff $file
8097 lappend sublist $file
8098 }
8099 }
8100 }
8101 if {$perfile_attrs} {
8102 cache_gitattr encoding $sublist
8103 }
8104 if {![eof $gdtf]} {
8105 return [expr {$nr >= $max? 2: 1}]
8106 }
8107 close $gdtf
8108 set treediffs($ids) $treediff
8109 unset treepending
8110 if {$cmitmode eq "tree" && [llength $diffids] == 1} {
8111 gettree $diffids
8112 } elseif {$ids != $diffids} {
8113 if {![info exists diffmergeid]} {
8114 gettreediffs $diffids
8115 }
8116 } else {
8117 addtocflist $ids
8118 }
8119 return 0
8120 }
8121
8122 # empty string or positive integer
8123 proc diffcontextvalidate {v} {
8124 return [regexp {^(|[1-9][0-9]*)$} $v]
8125 }
8126
8127 proc diffcontextchange {n1 n2 op} {
8128 global diffcontextstring diffcontext
8129
8130 if {[string is integer -strict $diffcontextstring]} {
8131 if {$diffcontextstring >= 0} {
8132 set diffcontext $diffcontextstring
8133 reselectline
8134 }
8135 }
8136 }
8137
8138 proc changeignorespace {} {
8139 reselectline
8140 }
8141
8142 proc changeworddiff {name ix op} {
8143 reselectline
8144 }
8145
8146 proc initblobdiffvars {} {
8147 global diffencoding targetline diffnparents
8148 global diffinhdr currdiffsubmod diffseehere
8149 set targetline {}
8150 set diffnparents 0
8151 set diffinhdr 0
8152 set diffencoding [get_path_encoding {}]
8153 set currdiffsubmod ""
8154 set diffseehere -1
8155 }
8156
8157 proc getblobdiffs {ids} {
8158 global blobdifffd diffids env
8159 global treediffs
8160 global diffcontext
8161 global ignorespace
8162 global worddiff
8163 global limitdiffs vfilelimit curview
8164 global git_version
8165
8166 set textconv {}
8167 if {[package vcompare $git_version "1.6.1"] >= 0} {
8168 set textconv "--textconv"
8169 }
8170 set submodule {}
8171 if {[package vcompare $git_version "1.6.6"] >= 0} {
8172 set submodule "--submodule"
8173 }
8174 set cmd [diffcmd $ids "-p $textconv $submodule -C --cc --no-commit-id -U$diffcontext"]
8175 if {$ignorespace} {
8176 append cmd " -w"
8177 }
8178 if {$worddiff ne [mc "Line diff"]} {
8179 append cmd " --word-diff=porcelain"
8180 }
8181 if {$limitdiffs && $vfilelimit($curview) ne {}} {
8182 set cmd [concat $cmd -- $vfilelimit($curview)]
8183 }
8184 if {[catch {set bdf [open $cmd r]} err]} {
8185 error_popup [mc "Error getting diffs: %s" $err]
8186 return
8187 }
8188 fconfigure $bdf -blocking 0 -encoding binary -eofchar {}
8189 set blobdifffd($ids) $bdf
8190 initblobdiffvars
8191 filerun $bdf [list getblobdiffline $bdf $diffids]
8192 }
8193
8194 proc savecmitpos {} {
8195 global ctext cmitmode
8196
8197 if {$cmitmode eq "tree"} {
8198 return {}
8199 }
8200 return [list target_scrollpos [$ctext index @0,0]]
8201 }
8202
8203 proc savectextpos {} {
8204 global ctext
8205
8206 return [list target_scrollpos [$ctext index @0,0]]
8207 }
8208
8209 proc maybe_scroll_ctext {ateof} {
8210 global ctext target_scrollpos
8211
8212 if {![info exists target_scrollpos]} return
8213 if {!$ateof} {
8214 set nlines [expr {[winfo height $ctext]
8215 / [font metrics textfont -linespace]}]
8216 if {[$ctext compare "$target_scrollpos + $nlines lines" <= end]} return
8217 }
8218 $ctext yview $target_scrollpos
8219 unset target_scrollpos
8220 }
8221
8222 proc setinlist {var i val} {
8223 global $var
8224
8225 while {[llength [set $var]] < $i} {
8226 lappend $var {}
8227 }
8228 if {[llength [set $var]] == $i} {
8229 lappend $var $val
8230 } else {
8231 lset $var $i $val
8232 }
8233 }
8234
8235 proc makediffhdr {fname ids} {
8236 global ctext curdiffstart treediffs diffencoding
8237 global ctext_file_names jump_to_here targetline diffline
8238
8239 set fname [encoding convertfrom utf-8 $fname]
8240 set diffencoding [get_path_encoding $fname]
8241 set i [lsearch -exact $treediffs($ids) $fname]
8242 if {$i >= 0} {
8243 setinlist difffilestart $i $curdiffstart
8244 }
8245 lset ctext_file_names end $fname
8246 set l [expr {(78 - [string length $fname]) / 2}]
8247 set pad [string range "----------------------------------------" 1 $l]
8248 $ctext insert $curdiffstart "$pad $fname $pad" filesep
8249 set targetline {}
8250 if {$jump_to_here ne {} && [lindex $jump_to_here 0] eq $fname} {
8251 set targetline [lindex $jump_to_here 1]
8252 }
8253 set diffline 0
8254 }
8255
8256 proc blobdiffmaybeseehere {ateof} {
8257 global diffseehere
8258 if {$diffseehere >= 0} {
8259 mark_ctext_line [lindex [split $diffseehere .] 0]
8260 }
8261 maybe_scroll_ctext $ateof
8262 }
8263
8264 proc getblobdiffline {bdf ids} {
8265 global diffids blobdifffd
8266 global ctext
8267
8268 set nr 0
8269 $ctext conf -state normal
8270 while {[incr nr] <= 1000 && [gets $bdf line] >= 0} {
8271 if {$ids != $diffids || $bdf != $blobdifffd($ids)} {
8272 # Older diff read. Abort it.
8273 catch {close $bdf}
8274 if {$ids != $diffids} {
8275 array unset blobdifffd $ids
8276 }
8277 return 0
8278 }
8279 parseblobdiffline $ids $line
8280 }
8281 $ctext conf -state disabled
8282 blobdiffmaybeseehere [eof $bdf]
8283 if {[eof $bdf]} {
8284 catch {close $bdf}
8285 array unset blobdifffd $ids
8286 return 0
8287 }
8288 return [expr {$nr >= 1000? 2: 1}]
8289 }
8290
8291 proc parseblobdiffline {ids line} {
8292 global ctext curdiffstart
8293 global diffnexthead diffnextnote difffilestart
8294 global ctext_file_names ctext_file_lines
8295 global diffinhdr treediffs mergemax diffnparents
8296 global diffencoding jump_to_here targetline diffline currdiffsubmod
8297 global worddiff diffseehere
8298
8299 if {![string compare -length 5 "diff " $line]} {
8300 if {![regexp {^diff (--cc|--git) } $line m type]} {
8301 set line [encoding convertfrom utf-8 $line]
8302 $ctext insert end "$line\n" hunksep
8303 continue
8304 }
8305 # start of a new file
8306 set diffinhdr 1
8307 set currdiffsubmod ""
8308
8309 $ctext insert end "\n"
8310 set curdiffstart [$ctext index "end - 1c"]
8311 lappend ctext_file_names ""
8312 lappend ctext_file_lines [lindex [split $curdiffstart "."] 0]
8313 $ctext insert end "\n" filesep
8314
8315 if {$type eq "--cc"} {
8316 # start of a new file in a merge diff
8317 set fname [string range $line 10 end]
8318 if {[lsearch -exact $treediffs($ids) $fname] < 0} {
8319 lappend treediffs($ids) $fname
8320 add_flist [list $fname]
8321 }
8322
8323 } else {
8324 set line [string range $line 11 end]
8325 # If the name hasn't changed the length will be odd,
8326 # the middle char will be a space, and the two bits either
8327 # side will be a/name and b/name, or "a/name" and "b/name".
8328 # If the name has changed we'll get "rename from" and
8329 # "rename to" or "copy from" and "copy to" lines following
8330 # this, and we'll use them to get the filenames.
8331 # This complexity is necessary because spaces in the
8332 # filename(s) don't get escaped.
8333 set l [string length $line]
8334 set i [expr {$l / 2}]
8335 if {!(($l & 1) && [string index $line $i] eq " " &&
8336 [string range $line 2 [expr {$i - 1}]] eq \
8337 [string range $line [expr {$i + 3}] end])} {
8338 return
8339 }
8340 # unescape if quoted and chop off the a/ from the front
8341 if {[string index $line 0] eq "\""} {
8342 set fname [string range [lindex $line 0] 2 end]
8343 } else {
8344 set fname [string range $line 2 [expr {$i - 1}]]
8345 }
8346 }
8347 makediffhdr $fname $ids
8348
8349 } elseif {![string compare -length 16 "* Unmerged path " $line]} {
8350 set fname [encoding convertfrom utf-8 [string range $line 16 end]]
8351 $ctext insert end "\n"
8352 set curdiffstart [$ctext index "end - 1c"]
8353 lappend ctext_file_names $fname
8354 lappend ctext_file_lines [lindex [split $curdiffstart "."] 0]
8355 $ctext insert end "$line\n" filesep
8356 set i [lsearch -exact $treediffs($ids) $fname]
8357 if {$i >= 0} {
8358 setinlist difffilestart $i $curdiffstart
8359 }
8360
8361 } elseif {![string compare -length 2 "@@" $line]} {
8362 regexp {^@@+} $line ats
8363 set line [encoding convertfrom $diffencoding $line]
8364 $ctext insert end "$line\n" hunksep
8365 if {[regexp { \+(\d+),\d+ @@} $line m nl]} {
8366 set diffline $nl
8367 }
8368 set diffnparents [expr {[string length $ats] - 1}]
8369 set diffinhdr 0
8370
8371 } elseif {![string compare -length 10 "Submodule " $line]} {
8372 # start of a new submodule
8373 if {[regexp -indices "\[0-9a-f\]+\\.\\." $line nameend]} {
8374 set fname [string range $line 10 [expr [lindex $nameend 0] - 2]]
8375 } else {
8376 set fname [string range $line 10 [expr [string first "contains " $line] - 2]]
8377 }
8378 if {$currdiffsubmod != $fname} {
8379 $ctext insert end "\n"; # Add newline after commit message
8380 }
8381 if {$currdiffsubmod != $fname} {
8382 set curdiffstart [$ctext index "end - 1c"]
8383 lappend ctext_file_names ""
8384 lappend ctext_file_lines [lindex [split $curdiffstart "."] 0]
8385 makediffhdr $fname $ids
8386 set currdiffsubmod $fname
8387 $ctext insert end "\n$line\n" filesep
8388 } else {
8389 $ctext insert end "$line\n" filesep
8390 }
8391 } elseif {$currdiffsubmod != "" && ![string compare -length 3 " >" $line]} {
8392 set line [encoding convertfrom $diffencoding $line]
8393 $ctext insert end "$line\n" dresult
8394 } elseif {$currdiffsubmod != "" && ![string compare -length 3 " <" $line]} {
8395 set line [encoding convertfrom $diffencoding $line]
8396 $ctext insert end "$line\n" d0
8397 } elseif {$diffinhdr} {
8398 if {![string compare -length 12 "rename from " $line]} {
8399 set fname [string range $line [expr 6 + [string first " from " $line] ] end]
8400 if {[string index $fname 0] eq "\""} {
8401 set fname [lindex $fname 0]
8402 }
8403 set fname [encoding convertfrom utf-8 $fname]
8404 set i [lsearch -exact $treediffs($ids) $fname]
8405 if {$i >= 0} {
8406 setinlist difffilestart $i $curdiffstart
8407 }
8408 } elseif {![string compare -length 10 $line "rename to "] ||
8409 ![string compare -length 8 $line "copy to "]} {
8410 set fname [string range $line [expr 4 + [string first " to " $line] ] end]
8411 if {[string index $fname 0] eq "\""} {
8412 set fname [lindex $fname 0]
8413 }
8414 makediffhdr $fname $ids
8415 } elseif {[string compare -length 3 $line "---"] == 0} {
8416 # do nothing
8417 return
8418 } elseif {[string compare -length 3 $line "+++"] == 0} {
8419 set diffinhdr 0
8420 return
8421 }
8422 set line [encoding convertfrom utf-8 $line]
8423 $ctext insert end "$line\n" filesep
8424
8425 } else {
8426 set line [string map {\x1A ^Z} \
8427 [encoding convertfrom $diffencoding $line]]
8428 # parse the prefix - one ' ', '-' or '+' for each parent
8429 set prefix [string range $line 0 [expr {$diffnparents - 1}]]
8430 set tag [expr {$diffnparents > 1? "m": "d"}]
8431 set dowords [expr {$worddiff ne [mc "Line diff"] && $diffnparents == 1}]
8432 set words_pre_markup ""
8433 set words_post_markup ""
8434 if {[string trim $prefix " -+"] eq {}} {
8435 # prefix only has " ", "-" and "+" in it: normal diff line
8436 set num [string first "-" $prefix]
8437 if {$dowords} {
8438 set line [string range $line 1 end]
8439 }
8440 if {$num >= 0} {
8441 # removed line, first parent with line is $num
8442 if {$num >= $mergemax} {
8443 set num "max"
8444 }
8445 if {$dowords && $worddiff eq [mc "Markup words"]} {
8446 $ctext insert end "\[-$line-\]" $tag$num
8447 } else {
8448 $ctext insert end "$line" $tag$num
8449 }
8450 if {!$dowords} {
8451 $ctext insert end "\n" $tag$num
8452 }
8453 } else {
8454 set tags {}
8455 if {[string first "+" $prefix] >= 0} {
8456 # added line
8457 lappend tags ${tag}result
8458 if {$diffnparents > 1} {
8459 set num [string first " " $prefix]
8460 if {$num >= 0} {
8461 if {$num >= $mergemax} {
8462 set num "max"
8463 }
8464 lappend tags m$num
8465 }
8466 }
8467 set words_pre_markup "{+"
8468 set words_post_markup "+}"
8469 }
8470 if {$targetline ne {}} {
8471 if {$diffline == $targetline} {
8472 set diffseehere [$ctext index "end - 1 chars"]
8473 set targetline {}
8474 } else {
8475 incr diffline
8476 }
8477 }
8478 if {$dowords && $worddiff eq [mc "Markup words"]} {
8479 $ctext insert end "$words_pre_markup$line$words_post_markup" $tags
8480 } else {
8481 $ctext insert end "$line" $tags
8482 }
8483 if {!$dowords} {
8484 $ctext insert end "\n" $tags
8485 }
8486 }
8487 } elseif {$dowords && $prefix eq "~"} {
8488 $ctext insert end "\n" {}
8489 } else {
8490 # "\ No newline at end of file",
8491 # or something else we don't recognize
8492 $ctext insert end "$line\n" hunksep
8493 }
8494 }
8495 }
8496
8497 proc changediffdisp {} {
8498 global ctext diffelide
8499
8500 $ctext tag conf d0 -elide [lindex $diffelide 0]
8501 $ctext tag conf dresult -elide [lindex $diffelide 1]
8502 }
8503
8504 proc highlightfile {cline} {
8505 global cflist cflist_top
8506
8507 if {![info exists cflist_top]} return
8508
8509 $cflist tag remove highlight $cflist_top.0 "$cflist_top.0 lineend"
8510 $cflist tag add highlight $cline.0 "$cline.0 lineend"
8511 $cflist see $cline.0
8512 set cflist_top $cline
8513 }
8514
8515 proc highlightfile_for_scrollpos {topidx} {
8516 global cmitmode difffilestart
8517
8518 if {$cmitmode eq "tree"} return
8519 if {![info exists difffilestart]} return
8520
8521 set top [lindex [split $topidx .] 0]
8522 if {$difffilestart eq {} || $top < [lindex $difffilestart 0]} {
8523 highlightfile 0
8524 } else {
8525 highlightfile [expr {[bsearch $difffilestart $top] + 2}]
8526 }
8527 }
8528
8529 proc prevfile {} {
8530 global difffilestart ctext cmitmode
8531
8532 if {$cmitmode eq "tree"} return
8533 set prev 0.0
8534 set here [$ctext index @0,0]
8535 foreach loc $difffilestart {
8536 if {[$ctext compare $loc >= $here]} {
8537 $ctext yview $prev
8538 return
8539 }
8540 set prev $loc
8541 }
8542 $ctext yview $prev
8543 }
8544
8545 proc nextfile {} {
8546 global difffilestart ctext cmitmode
8547
8548 if {$cmitmode eq "tree"} return
8549 set here [$ctext index @0,0]
8550 foreach loc $difffilestart {
8551 if {[$ctext compare $loc > $here]} {
8552 $ctext yview $loc
8553 return
8554 }
8555 }
8556 }
8557
8558 proc clear_ctext {{first 1.0}} {
8559 global ctext smarktop smarkbot
8560 global ctext_file_names ctext_file_lines
8561 global pendinglinks
8562
8563 set l [lindex [split $first .] 0]
8564 if {![info exists smarktop] || [$ctext compare $first < $smarktop.0]} {
8565 set smarktop $l
8566 }
8567 if {![info exists smarkbot] || [$ctext compare $first < $smarkbot.0]} {
8568 set smarkbot $l
8569 }
8570 $ctext delete $first end
8571 if {$first eq "1.0"} {
8572 unset -nocomplain pendinglinks
8573 }
8574 set ctext_file_names {}
8575 set ctext_file_lines {}
8576 }
8577
8578 proc settabs {{firstab {}}} {
8579 global firsttabstop tabstop ctext have_tk85
8580
8581 if {$firstab ne {} && $have_tk85} {
8582 set firsttabstop $firstab
8583 }
8584 set w [font measure textfont "0"]
8585 if {$firsttabstop != 0} {
8586 $ctext conf -tabs [list [expr {($firsttabstop + $tabstop) * $w}] \
8587 [expr {($firsttabstop + 2 * $tabstop) * $w}]]
8588 } elseif {$have_tk85 || $tabstop != 8} {
8589 $ctext conf -tabs [expr {$tabstop * $w}]
8590 } else {
8591 $ctext conf -tabs {}
8592 }
8593 }
8594
8595 proc incrsearch {name ix op} {
8596 global ctext searchstring searchdirn
8597
8598 if {[catch {$ctext index anchor}]} {
8599 # no anchor set, use start of selection, or of visible area
8600 set sel [$ctext tag ranges sel]
8601 if {$sel ne {}} {
8602 $ctext mark set anchor [lindex $sel 0]
8603 } elseif {$searchdirn eq "-forwards"} {
8604 $ctext mark set anchor @0,0
8605 } else {
8606 $ctext mark set anchor @0,[winfo height $ctext]
8607 }
8608 }
8609 if {$searchstring ne {}} {
8610 set here [$ctext search -count mlen $searchdirn -- $searchstring anchor]
8611 if {$here ne {}} {
8612 $ctext see $here
8613 set mend "$here + $mlen c"
8614 $ctext tag remove sel 1.0 end
8615 $ctext tag add sel $here $mend
8616 suppress_highlighting_file_for_current_scrollpos
8617 highlightfile_for_scrollpos $here
8618 }
8619 }
8620 rehighlight_search_results
8621 }
8622
8623 proc dosearch {} {
8624 global sstring ctext searchstring searchdirn
8625
8626 focus $sstring
8627 $sstring icursor end
8628 set searchdirn -forwards
8629 if {$searchstring ne {}} {
8630 set sel [$ctext tag ranges sel]
8631 if {$sel ne {}} {
8632 set start "[lindex $sel 0] + 1c"
8633 } elseif {[catch {set start [$ctext index anchor]}]} {
8634 set start "@0,0"
8635 }
8636 set match [$ctext search -count mlen -- $searchstring $start]
8637 $ctext tag remove sel 1.0 end
8638 if {$match eq {}} {
8639 bell
8640 return
8641 }
8642 $ctext see $match
8643 suppress_highlighting_file_for_current_scrollpos
8644 highlightfile_for_scrollpos $match
8645 set mend "$match + $mlen c"
8646 $ctext tag add sel $match $mend
8647 $ctext mark unset anchor
8648 rehighlight_search_results
8649 }
8650 }
8651
8652 proc dosearchback {} {
8653 global sstring ctext searchstring searchdirn
8654
8655 focus $sstring
8656 $sstring icursor end
8657 set searchdirn -backwards
8658 if {$searchstring ne {}} {
8659 set sel [$ctext tag ranges sel]
8660 if {$sel ne {}} {
8661 set start [lindex $sel 0]
8662 } elseif {[catch {set start [$ctext index anchor]}]} {
8663 set start @0,[winfo height $ctext]
8664 }
8665 set match [$ctext search -backwards -count ml -- $searchstring $start]
8666 $ctext tag remove sel 1.0 end
8667 if {$match eq {}} {
8668 bell
8669 return
8670 }
8671 $ctext see $match
8672 suppress_highlighting_file_for_current_scrollpos
8673 highlightfile_for_scrollpos $match
8674 set mend "$match + $ml c"
8675 $ctext tag add sel $match $mend
8676 $ctext mark unset anchor
8677 rehighlight_search_results
8678 }
8679 }
8680
8681 proc rehighlight_search_results {} {
8682 global ctext searchstring
8683
8684 $ctext tag remove found 1.0 end
8685 $ctext tag remove currentsearchhit 1.0 end
8686
8687 if {$searchstring ne {}} {
8688 searchmarkvisible 1
8689 }
8690 }
8691
8692 proc searchmark {first last} {
8693 global ctext searchstring
8694
8695 set sel [$ctext tag ranges sel]
8696
8697 set mend $first.0
8698 while {1} {
8699 set match [$ctext search -count mlen -- $searchstring $mend $last.end]
8700 if {$match eq {}} break
8701 set mend "$match + $mlen c"
8702 if {$sel ne {} && [$ctext compare $match == [lindex $sel 0]]} {
8703 $ctext tag add currentsearchhit $match $mend
8704 } else {
8705 $ctext tag add found $match $mend
8706 }
8707 }
8708 }
8709
8710 proc searchmarkvisible {doall} {
8711 global ctext smarktop smarkbot
8712
8713 set topline [lindex [split [$ctext index @0,0] .] 0]
8714 set botline [lindex [split [$ctext index @0,[winfo height $ctext]] .] 0]
8715 if {$doall || $botline < $smarktop || $topline > $smarkbot} {
8716 # no overlap with previous
8717 searchmark $topline $botline
8718 set smarktop $topline
8719 set smarkbot $botline
8720 } else {
8721 if {$topline < $smarktop} {
8722 searchmark $topline [expr {$smarktop-1}]
8723 set smarktop $topline
8724 }
8725 if {$botline > $smarkbot} {
8726 searchmark [expr {$smarkbot+1}] $botline
8727 set smarkbot $botline
8728 }
8729 }
8730 }
8731
8732 proc suppress_highlighting_file_for_current_scrollpos {} {
8733 global ctext suppress_highlighting_file_for_this_scrollpos
8734
8735 set suppress_highlighting_file_for_this_scrollpos [$ctext index @0,0]
8736 }
8737
8738 proc scrolltext {f0 f1} {
8739 global searchstring cmitmode ctext
8740 global suppress_highlighting_file_for_this_scrollpos
8741
8742 set topidx [$ctext index @0,0]
8743 if {![info exists suppress_highlighting_file_for_this_scrollpos]
8744 || $topidx ne $suppress_highlighting_file_for_this_scrollpos} {
8745 highlightfile_for_scrollpos $topidx
8746 }
8747
8748 unset -nocomplain suppress_highlighting_file_for_this_scrollpos
8749
8750 .bleft.bottom.sb set $f0 $f1
8751 if {$searchstring ne {}} {
8752 searchmarkvisible 0
8753 }
8754 }
8755
8756 proc setcoords {} {
8757 global linespc charspc canvx0 canvy0
8758 global xspc1 xspc2 lthickness
8759
8760 set linespc [font metrics mainfont -linespace]
8761 set charspc [font measure mainfont "m"]
8762 set canvy0 [expr {int(3 + 0.5 * $linespc)}]
8763 set canvx0 [expr {int(3 + 0.5 * $linespc)}]
8764 set lthickness [expr {int($linespc / 9) + 1}]
8765 set xspc1(0) $linespc
8766 set xspc2 $linespc
8767 }
8768
8769 proc redisplay {} {
8770 global canv
8771 global selectedline
8772
8773 set ymax [lindex [$canv cget -scrollregion] 3]
8774 if {$ymax eq {} || $ymax == 0} return
8775 set span [$canv yview]
8776 clear_display
8777 setcanvscroll
8778 allcanvs yview moveto [lindex $span 0]
8779 drawvisible
8780 if {$selectedline ne {}} {
8781 selectline $selectedline 0
8782 allcanvs yview moveto [lindex $span 0]
8783 }
8784 }
8785
8786 proc parsefont {f n} {
8787 global fontattr
8788
8789 set fontattr($f,family) [lindex $n 0]
8790 set s [lindex $n 1]
8791 if {$s eq {} || $s == 0} {
8792 set s 10
8793 } elseif {$s < 0} {
8794 set s [expr {int(-$s / [winfo fpixels . 1p] + 0.5)}]
8795 }
8796 set fontattr($f,size) $s
8797 set fontattr($f,weight) normal
8798 set fontattr($f,slant) roman
8799 foreach style [lrange $n 2 end] {
8800 switch -- $style {
8801 "normal" -
8802 "bold" {set fontattr($f,weight) $style}
8803 "roman" -
8804 "italic" {set fontattr($f,slant) $style}
8805 }
8806 }
8807 }
8808
8809 proc fontflags {f {isbold 0}} {
8810 global fontattr
8811
8812 return [list -family $fontattr($f,family) -size $fontattr($f,size) \
8813 -weight [expr {$isbold? "bold": $fontattr($f,weight)}] \
8814 -slant $fontattr($f,slant)]
8815 }
8816
8817 proc fontname {f} {
8818 global fontattr
8819
8820 set n [list $fontattr($f,family) $fontattr($f,size)]
8821 if {$fontattr($f,weight) eq "bold"} {
8822 lappend n "bold"
8823 }
8824 if {$fontattr($f,slant) eq "italic"} {
8825 lappend n "italic"
8826 }
8827 return $n
8828 }
8829
8830 proc incrfont {inc} {
8831 global mainfont textfont ctext canv cflist showrefstop
8832 global stopped entries fontattr
8833
8834 unmarkmatches
8835 set s $fontattr(mainfont,size)
8836 incr s $inc
8837 if {$s < 1} {
8838 set s 1
8839 }
8840 set fontattr(mainfont,size) $s
8841 font config mainfont -size $s
8842 font config mainfontbold -size $s
8843 set mainfont [fontname mainfont]
8844 set s $fontattr(textfont,size)
8845 incr s $inc
8846 if {$s < 1} {
8847 set s 1
8848 }
8849 set fontattr(textfont,size) $s
8850 font config textfont -size $s
8851 font config textfontbold -size $s
8852 set textfont [fontname textfont]
8853 setcoords
8854 settabs
8855 redisplay
8856 }
8857
8858 proc clearsha1 {} {
8859 global sha1entry sha1string
8860 if {[string length $sha1string] == 40} {
8861 $sha1entry delete 0 end
8862 }
8863 }
8864
8865 proc sha1change {n1 n2 op} {
8866 global sha1string currentid sha1but
8867 if {$sha1string == {}
8868 || ([info exists currentid] && $sha1string == $currentid)} {
8869 set state disabled
8870 } else {
8871 set state normal
8872 }
8873 if {[$sha1but cget -state] == $state} return
8874 if {$state == "normal"} {
8875 $sha1but conf -state normal -relief raised -text "[mc "Goto:"] "
8876 } else {
8877 $sha1but conf -state disabled -relief flat -text "[mc "Commit ID:"] "
8878 }
8879 }
8880
8881 proc gotocommit {} {
8882 global sha1string tagids headids curview varcid
8883
8884 if {$sha1string == {}
8885 || ([info exists currentid] && $sha1string == $currentid)} return
8886 if {[info exists tagids($sha1string)]} {
8887 set id $tagids($sha1string)
8888 } elseif {[info exists headids($sha1string)]} {
8889 set id $headids($sha1string)
8890 } else {
8891 set id [string tolower $sha1string]
8892 if {[regexp {^[0-9a-f]{4,39}$} $id]} {
8893 set matches [longid $id]
8894 if {$matches ne {}} {
8895 if {[llength $matches] > 1} {
8896 error_popup [mc "Short commit ID %s is ambiguous" $id]
8897 return
8898 }
8899 set id [lindex $matches 0]
8900 }
8901 } else {
8902 if {[catch {set id [exec git rev-parse --verify $sha1string]}]} {
8903 error_popup [mc "Revision %s is not known" $sha1string]
8904 return
8905 }
8906 }
8907 }
8908 if {[commitinview $id $curview]} {
8909 selectline [rowofcommit $id] 1
8910 return
8911 }
8912 if {[regexp {^[0-9a-fA-F]{4,}$} $sha1string]} {
8913 set msg [mc "Commit ID %s is not known" $sha1string]
8914 } else {
8915 set msg [mc "Revision %s is not in the current view" $sha1string]
8916 }
8917 error_popup $msg
8918 }
8919
8920 proc lineenter {x y id} {
8921 global hoverx hovery hoverid hovertimer
8922 global commitinfo canv
8923
8924 if {![info exists commitinfo($id)] && ![getcommit $id]} return
8925 set hoverx $x
8926 set hovery $y
8927 set hoverid $id
8928 if {[info exists hovertimer]} {
8929 after cancel $hovertimer
8930 }
8931 set hovertimer [after 500 linehover]
8932 $canv delete hover
8933 }
8934
8935 proc linemotion {x y id} {
8936 global hoverx hovery hoverid hovertimer
8937
8938 if {[info exists hoverid] && $id == $hoverid} {
8939 set hoverx $x
8940 set hovery $y
8941 if {[info exists hovertimer]} {
8942 after cancel $hovertimer
8943 }
8944 set hovertimer [after 500 linehover]
8945 }
8946 }
8947
8948 proc lineleave {id} {
8949 global hoverid hovertimer canv
8950
8951 if {[info exists hoverid] && $id == $hoverid} {
8952 $canv delete hover
8953 if {[info exists hovertimer]} {
8954 after cancel $hovertimer
8955 unset hovertimer
8956 }
8957 unset hoverid
8958 }
8959 }
8960
8961 proc linehover {} {
8962 global hoverx hovery hoverid hovertimer
8963 global canv linespc lthickness
8964 global linehoverbgcolor linehoverfgcolor linehoveroutlinecolor
8965
8966 global commitinfo
8967
8968 set text [lindex $commitinfo($hoverid) 0]
8969 set ymax [lindex [$canv cget -scrollregion] 3]
8970 if {$ymax == {}} return
8971 set yfrac [lindex [$canv yview] 0]
8972 set x [expr {$hoverx + 2 * $linespc}]
8973 set y [expr {$hovery + $yfrac * $ymax - $linespc / 2}]
8974 set x0 [expr {$x - 2 * $lthickness}]
8975 set y0 [expr {$y - 2 * $lthickness}]
8976 set x1 [expr {$x + [font measure mainfont $text] + 2 * $lthickness}]
8977 set y1 [expr {$y + $linespc + 2 * $lthickness}]
8978 set t [$canv create rectangle $x0 $y0 $x1 $y1 \
8979 -fill $linehoverbgcolor -outline $linehoveroutlinecolor \
8980 -width 1 -tags hover]
8981 $canv raise $t
8982 set t [$canv create text $x $y -anchor nw -text $text -tags hover \
8983 -font mainfont -fill $linehoverfgcolor]
8984 $canv raise $t
8985 }
8986
8987 proc clickisonarrow {id y} {
8988 global lthickness
8989
8990 set ranges [rowranges $id]
8991 set thresh [expr {2 * $lthickness + 6}]
8992 set n [expr {[llength $ranges] - 1}]
8993 for {set i 1} {$i < $n} {incr i} {
8994 set row [lindex $ranges $i]
8995 if {abs([yc $row] - $y) < $thresh} {
8996 return $i
8997 }
8998 }
8999 return {}
9000 }
9001
9002 proc arrowjump {id n y} {
9003 global canv
9004
9005 # 1 <-> 2, 3 <-> 4, etc...
9006 set n [expr {(($n - 1) ^ 1) + 1}]
9007 set row [lindex [rowranges $id] $n]
9008 set yt [yc $row]
9009 set ymax [lindex [$canv cget -scrollregion] 3]
9010 if {$ymax eq {} || $ymax <= 0} return
9011 set view [$canv yview]
9012 set yspan [expr {[lindex $view 1] - [lindex $view 0]}]
9013 set yfrac [expr {$yt / $ymax - $yspan / 2}]
9014 if {$yfrac < 0} {
9015 set yfrac 0
9016 }
9017 allcanvs yview moveto $yfrac
9018 }
9019
9020 proc lineclick {x y id isnew} {
9021 global ctext commitinfo children canv thickerline curview
9022
9023 if {![info exists commitinfo($id)] && ![getcommit $id]} return
9024 unmarkmatches
9025 unselectline
9026 normalline
9027 $canv delete hover
9028 # draw this line thicker than normal
9029 set thickerline $id
9030 drawlines $id
9031 if {$isnew} {
9032 set ymax [lindex [$canv cget -scrollregion] 3]
9033 if {$ymax eq {}} return
9034 set yfrac [lindex [$canv yview] 0]
9035 set y [expr {$y + $yfrac * $ymax}]
9036 }
9037 set dirn [clickisonarrow $id $y]
9038 if {$dirn ne {}} {
9039 arrowjump $id $dirn $y
9040 return
9041 }
9042
9043 if {$isnew} {
9044 addtohistory [list lineclick $x $y $id 0] savectextpos
9045 }
9046 # fill the details pane with info about this line
9047 $ctext conf -state normal
9048 clear_ctext
9049 settabs 0
9050 $ctext insert end "[mc "Parent"]:\t"
9051 $ctext insert end $id link0
9052 setlink $id link0
9053 set info $commitinfo($id)
9054 $ctext insert end "\n\t[lindex $info 0]\n"
9055 $ctext insert end "\t[mc "Author"]:\t[lindex $info 1]\n"
9056 set date [formatdate [lindex $info 2]]
9057 $ctext insert end "\t[mc "Date"]:\t$date\n"
9058 set kids $children($curview,$id)
9059 if {$kids ne {}} {
9060 $ctext insert end "\n[mc "Children"]:"
9061 set i 0
9062 foreach child $kids {
9063 incr i
9064 if {![info exists commitinfo($child)] && ![getcommit $child]} continue
9065 set info $commitinfo($child)
9066 $ctext insert end "\n\t"
9067 $ctext insert end $child link$i
9068 setlink $child link$i
9069 $ctext insert end "\n\t[lindex $info 0]"
9070 $ctext insert end "\n\t[mc "Author"]:\t[lindex $info 1]"
9071 set date [formatdate [lindex $info 2]]
9072 $ctext insert end "\n\t[mc "Date"]:\t$date\n"
9073 }
9074 }
9075 maybe_scroll_ctext 1
9076 $ctext conf -state disabled
9077 init_flist {}
9078 }
9079
9080 proc normalline {} {
9081 global thickerline
9082 if {[info exists thickerline]} {
9083 set id $thickerline
9084 unset thickerline
9085 drawlines $id
9086 }
9087 }
9088
9089 proc selbyid {id {isnew 1}} {
9090 global curview
9091 if {[commitinview $id $curview]} {
9092 selectline [rowofcommit $id] $isnew
9093 }
9094 }
9095
9096 proc mstime {} {
9097 global startmstime
9098 if {![info exists startmstime]} {
9099 set startmstime [clock clicks -milliseconds]
9100 }
9101 return [format "%.3f" [expr {([clock click -milliseconds] - $startmstime) / 1000.0}]]
9102 }
9103
9104 proc rowmenu {x y id} {
9105 global rowctxmenu selectedline rowmenuid curview
9106 global nullid nullid2 fakerowmenu mainhead markedid
9107
9108 stopfinding
9109 set rowmenuid $id
9110 if {$selectedline eq {} || [rowofcommit $id] eq $selectedline} {
9111 set state disabled
9112 } else {
9113 set state normal
9114 }
9115 if {[info exists markedid] && $markedid ne $id} {
9116 set mstate normal
9117 } else {
9118 set mstate disabled
9119 }
9120 if {$id ne $nullid && $id ne $nullid2} {
9121 set menu $rowctxmenu
9122 if {$mainhead ne {}} {
9123 $menu entryconfigure 8 -label [mc "Reset %s branch to here" $mainhead] -state normal
9124 } else {
9125 $menu entryconfigure 8 -label [mc "Detached head: can't reset" $mainhead] -state disabled
9126 }
9127 $menu entryconfigure 10 -state $mstate
9128 $menu entryconfigure 11 -state $mstate
9129 $menu entryconfigure 12 -state $mstate
9130 } else {
9131 set menu $fakerowmenu
9132 }
9133 $menu entryconfigure [mca "Diff this -> selected"] -state $state
9134 $menu entryconfigure [mca "Diff selected -> this"] -state $state
9135 $menu entryconfigure [mca "Make patch"] -state $state
9136 $menu entryconfigure [mca "Diff this -> marked commit"] -state $mstate
9137 $menu entryconfigure [mca "Diff marked commit -> this"] -state $mstate
9138 tk_popup $menu $x $y
9139 }
9140
9141 proc markhere {} {
9142 global rowmenuid markedid canv
9143
9144 set markedid $rowmenuid
9145 make_idmark $markedid
9146 }
9147
9148 proc gotomark {} {
9149 global markedid
9150
9151 if {[info exists markedid]} {
9152 selbyid $markedid
9153 }
9154 }
9155
9156 proc replace_by_kids {l r} {
9157 global curview children
9158
9159 set id [commitonrow $r]
9160 set l [lreplace $l 0 0]
9161 foreach kid $children($curview,$id) {
9162 lappend l [rowofcommit $kid]
9163 }
9164 return [lsort -integer -decreasing -unique $l]
9165 }
9166
9167 proc find_common_desc {} {
9168 global markedid rowmenuid curview children
9169
9170 if {![info exists markedid]} return
9171 if {![commitinview $markedid $curview] ||
9172 ![commitinview $rowmenuid $curview]} return
9173 #set t1 [clock clicks -milliseconds]
9174 set l1 [list [rowofcommit $markedid]]
9175 set l2 [list [rowofcommit $rowmenuid]]
9176 while 1 {
9177 set r1 [lindex $l1 0]
9178 set r2 [lindex $l2 0]
9179 if {$r1 eq {} || $r2 eq {}} break
9180 if {$r1 == $r2} {
9181 selectline $r1 1
9182 break
9183 }
9184 if {$r1 > $r2} {
9185 set l1 [replace_by_kids $l1 $r1]
9186 } else {
9187 set l2 [replace_by_kids $l2 $r2]
9188 }
9189 }
9190 #set t2 [clock clicks -milliseconds]
9191 #puts "took [expr {$t2-$t1}]ms"
9192 }
9193
9194 proc compare_commits {} {
9195 global markedid rowmenuid curview children
9196
9197 if {![info exists markedid]} return
9198 if {![commitinview $markedid $curview]} return
9199 addtohistory [list do_cmp_commits $markedid $rowmenuid]
9200 do_cmp_commits $markedid $rowmenuid
9201 }
9202
9203 proc getpatchid {id} {
9204 global patchids
9205
9206 if {![info exists patchids($id)]} {
9207 set cmd [diffcmd [list $id] {-p --root}]
9208 # trim off the initial "|"
9209 set cmd [lrange $cmd 1 end]
9210 if {[catch {
9211 set x [eval exec $cmd | git patch-id]
9212 set patchids($id) [lindex $x 0]
9213 }]} {
9214 set patchids($id) "error"
9215 }
9216 }
9217 return $patchids($id)
9218 }
9219
9220 proc do_cmp_commits {a b} {
9221 global ctext curview parents children patchids commitinfo
9222
9223 $ctext conf -state normal
9224 clear_ctext
9225 init_flist {}
9226 for {set i 0} {$i < 100} {incr i} {
9227 set skipa 0
9228 set skipb 0
9229 if {[llength $parents($curview,$a)] > 1} {
9230 appendshortlink $a [mc "Skipping merge commit "] "\n"
9231 set skipa 1
9232 } else {
9233 set patcha [getpatchid $a]
9234 }
9235 if {[llength $parents($curview,$b)] > 1} {
9236 appendshortlink $b [mc "Skipping merge commit "] "\n"
9237 set skipb 1
9238 } else {
9239 set patchb [getpatchid $b]
9240 }
9241 if {!$skipa && !$skipb} {
9242 set heada [lindex $commitinfo($a) 0]
9243 set headb [lindex $commitinfo($b) 0]
9244 if {$patcha eq "error"} {
9245 appendshortlink $a [mc "Error getting patch ID for "] \
9246 [mc " - stopping\n"]
9247 break
9248 }
9249 if {$patchb eq "error"} {
9250 appendshortlink $b [mc "Error getting patch ID for "] \
9251 [mc " - stopping\n"]
9252 break
9253 }
9254 if {$patcha eq $patchb} {
9255 if {$heada eq $headb} {
9256 appendshortlink $a [mc "Commit "]
9257 appendshortlink $b " == " " $heada\n"
9258 } else {
9259 appendshortlink $a [mc "Commit "] " $heada\n"
9260 appendshortlink $b [mc " is the same patch as\n "] \
9261 " $headb\n"
9262 }
9263 set skipa 1
9264 set skipb 1
9265 } else {
9266 $ctext insert end "\n"
9267 appendshortlink $a [mc "Commit "] " $heada\n"
9268 appendshortlink $b [mc " differs from\n "] \
9269 " $headb\n"
9270 $ctext insert end [mc "Diff of commits:\n\n"]
9271 $ctext conf -state disabled
9272 update
9273 diffcommits $a $b
9274 return
9275 }
9276 }
9277 if {$skipa} {
9278 set kids [real_children $curview,$a]
9279 if {[llength $kids] != 1} {
9280 $ctext insert end "\n"
9281 appendshortlink $a [mc "Commit "] \
9282 [mc " has %s children - stopping\n" [llength $kids]]
9283 break
9284 }
9285 set a [lindex $kids 0]
9286 }
9287 if {$skipb} {
9288 set kids [real_children $curview,$b]
9289 if {[llength $kids] != 1} {
9290 appendshortlink $b [mc "Commit "] \
9291 [mc " has %s children - stopping\n" [llength $kids]]
9292 break
9293 }
9294 set b [lindex $kids 0]
9295 }
9296 }
9297 $ctext conf -state disabled
9298 }
9299
9300 proc diffcommits {a b} {
9301 global diffcontext diffids blobdifffd diffinhdr currdiffsubmod
9302
9303 set tmpdir [gitknewtmpdir]
9304 set fna [file join $tmpdir "commit-[string range $a 0 7]"]
9305 set fnb [file join $tmpdir "commit-[string range $b 0 7]"]
9306 if {[catch {
9307 exec git diff-tree -p --pretty $a >$fna
9308 exec git diff-tree -p --pretty $b >$fnb
9309 } err]} {
9310 error_popup [mc "Error writing commit to file: %s" $err]
9311 return
9312 }
9313 if {[catch {
9314 set fd [open "| diff -U$diffcontext $fna $fnb" r]
9315 } err]} {
9316 error_popup [mc "Error diffing commits: %s" $err]
9317 return
9318 }
9319 set diffids [list commits $a $b]
9320 set blobdifffd($diffids) $fd
9321 set diffinhdr 0
9322 set currdiffsubmod ""
9323 filerun $fd [list getblobdiffline $fd $diffids]
9324 }
9325
9326 proc diffvssel {dirn} {
9327 global rowmenuid selectedline
9328
9329 if {$selectedline eq {}} return
9330 if {$dirn} {
9331 set oldid [commitonrow $selectedline]
9332 set newid $rowmenuid
9333 } else {
9334 set oldid $rowmenuid
9335 set newid [commitonrow $selectedline]
9336 }
9337 addtohistory [list doseldiff $oldid $newid] savectextpos
9338 doseldiff $oldid $newid
9339 }
9340
9341 proc diffvsmark {dirn} {
9342 global rowmenuid markedid
9343
9344 if {![info exists markedid]} return
9345 if {$dirn} {
9346 set oldid $markedid
9347 set newid $rowmenuid
9348 } else {
9349 set oldid $rowmenuid
9350 set newid $markedid
9351 }
9352 addtohistory [list doseldiff $oldid $newid] savectextpos
9353 doseldiff $oldid $newid
9354 }
9355
9356 proc doseldiff {oldid newid} {
9357 global ctext
9358 global commitinfo
9359
9360 $ctext conf -state normal
9361 clear_ctext
9362 init_flist [mc "Top"]
9363 $ctext insert end "[mc "From"] "
9364 $ctext insert end $oldid link0
9365 setlink $oldid link0
9366 $ctext insert end "\n "
9367 $ctext insert end [lindex $commitinfo($oldid) 0]
9368 $ctext insert end "\n\n[mc "To"] "
9369 $ctext insert end $newid link1
9370 setlink $newid link1
9371 $ctext insert end "\n "
9372 $ctext insert end [lindex $commitinfo($newid) 0]
9373 $ctext insert end "\n"
9374 $ctext conf -state disabled
9375 $ctext tag remove found 1.0 end
9376 startdiff [list $oldid $newid]
9377 }
9378
9379 proc mkpatch {} {
9380 global rowmenuid currentid commitinfo patchtop patchnum NS
9381
9382 if {![info exists currentid]} return
9383 set oldid $currentid
9384 set oldhead [lindex $commitinfo($oldid) 0]
9385 set newid $rowmenuid
9386 set newhead [lindex $commitinfo($newid) 0]
9387 set top .patch
9388 set patchtop $top
9389 catch {destroy $top}
9390 ttk_toplevel $top
9391 make_transient $top .
9392 ${NS}::label $top.title -text [mc "Generate patch"]
9393 grid $top.title - -pady 10
9394 ${NS}::label $top.from -text [mc "From:"]
9395 ${NS}::entry $top.fromsha1 -width 40
9396 $top.fromsha1 insert 0 $oldid
9397 $top.fromsha1 conf -state readonly
9398 grid $top.from $top.fromsha1 -sticky w
9399 ${NS}::entry $top.fromhead -width 60
9400 $top.fromhead insert 0 $oldhead
9401 $top.fromhead conf -state readonly
9402 grid x $top.fromhead -sticky w
9403 ${NS}::label $top.to -text [mc "To:"]
9404 ${NS}::entry $top.tosha1 -width 40
9405 $top.tosha1 insert 0 $newid
9406 $top.tosha1 conf -state readonly
9407 grid $top.to $top.tosha1 -sticky w
9408 ${NS}::entry $top.tohead -width 60
9409 $top.tohead insert 0 $newhead
9410 $top.tohead conf -state readonly
9411 grid x $top.tohead -sticky w
9412 ${NS}::button $top.rev -text [mc "Reverse"] -command mkpatchrev
9413 grid $top.rev x -pady 10 -padx 5
9414 ${NS}::label $top.flab -text [mc "Output file:"]
9415 ${NS}::entry $top.fname -width 60
9416 $top.fname insert 0 [file normalize "patch$patchnum.patch"]
9417 incr patchnum
9418 grid $top.flab $top.fname -sticky w
9419 ${NS}::frame $top.buts
9420 ${NS}::button $top.buts.gen -text [mc "Generate"] -command mkpatchgo
9421 ${NS}::button $top.buts.can -text [mc "Cancel"] -command mkpatchcan
9422 bind $top <Key-Return> mkpatchgo
9423 bind $top <Key-Escape> mkpatchcan
9424 grid $top.buts.gen $top.buts.can
9425 grid columnconfigure $top.buts 0 -weight 1 -uniform a
9426 grid columnconfigure $top.buts 1 -weight 1 -uniform a
9427 grid $top.buts - -pady 10 -sticky ew
9428 focus $top.fname
9429 }
9430
9431 proc mkpatchrev {} {
9432 global patchtop
9433
9434 set oldid [$patchtop.fromsha1 get]
9435 set oldhead [$patchtop.fromhead get]
9436 set newid [$patchtop.tosha1 get]
9437 set newhead [$patchtop.tohead get]
9438 foreach e [list fromsha1 fromhead tosha1 tohead] \
9439 v [list $newid $newhead $oldid $oldhead] {
9440 $patchtop.$e conf -state normal
9441 $patchtop.$e delete 0 end
9442 $patchtop.$e insert 0 $v
9443 $patchtop.$e conf -state readonly
9444 }
9445 }
9446
9447 proc mkpatchgo {} {
9448 global patchtop nullid nullid2
9449
9450 set oldid [$patchtop.fromsha1 get]
9451 set newid [$patchtop.tosha1 get]
9452 set fname [$patchtop.fname get]
9453 set cmd [diffcmd [list $oldid $newid] -p]
9454 # trim off the initial "|"
9455 set cmd [lrange $cmd 1 end]
9456 lappend cmd >$fname &
9457 if {[catch {eval exec $cmd} err]} {
9458 error_popup "[mc "Error creating patch:"] $err" $patchtop
9459 }
9460 catch {destroy $patchtop}
9461 unset patchtop
9462 }
9463
9464 proc mkpatchcan {} {
9465 global patchtop
9466
9467 catch {destroy $patchtop}
9468 unset patchtop
9469 }
9470
9471 proc mktag {} {
9472 global rowmenuid mktagtop commitinfo NS
9473
9474 set top .maketag
9475 set mktagtop $top
9476 catch {destroy $top}
9477 ttk_toplevel $top
9478 make_transient $top .
9479 ${NS}::label $top.title -text [mc "Create tag"]
9480 grid $top.title - -pady 10
9481 ${NS}::label $top.id -text [mc "ID:"]
9482 ${NS}::entry $top.sha1 -width 40
9483 $top.sha1 insert 0 $rowmenuid
9484 $top.sha1 conf -state readonly
9485 grid $top.id $top.sha1 -sticky w
9486 ${NS}::entry $top.head -width 60
9487 $top.head insert 0 [lindex $commitinfo($rowmenuid) 0]
9488 $top.head conf -state readonly
9489 grid x $top.head -sticky w
9490 ${NS}::label $top.tlab -text [mc "Tag name:"]
9491 ${NS}::entry $top.tag -width 60
9492 grid $top.tlab $top.tag -sticky w
9493 ${NS}::label $top.op -text [mc "Tag message is optional"]
9494 grid $top.op -columnspan 2 -sticky we
9495 ${NS}::label $top.mlab -text [mc "Tag message:"]
9496 ${NS}::entry $top.msg -width 60
9497 grid $top.mlab $top.msg -sticky w
9498 ${NS}::frame $top.buts
9499 ${NS}::button $top.buts.gen -text [mc "Create"] -command mktaggo
9500 ${NS}::button $top.buts.can -text [mc "Cancel"] -command mktagcan
9501 bind $top <Key-Return> mktaggo
9502 bind $top <Key-Escape> mktagcan
9503 grid $top.buts.gen $top.buts.can
9504 grid columnconfigure $top.buts 0 -weight 1 -uniform a
9505 grid columnconfigure $top.buts 1 -weight 1 -uniform a
9506 grid $top.buts - -pady 10 -sticky ew
9507 focus $top.tag
9508 }
9509
9510 proc domktag {} {
9511 global mktagtop env tagids idtags
9512
9513 set id [$mktagtop.sha1 get]
9514 set tag [$mktagtop.tag get]
9515 set msg [$mktagtop.msg get]
9516 if {$tag == {}} {
9517 error_popup [mc "No tag name specified"] $mktagtop
9518 return 0
9519 }
9520 if {[info exists tagids($tag)]} {
9521 error_popup [mc "Tag \"%s\" already exists" $tag] $mktagtop
9522 return 0
9523 }
9524 if {[catch {
9525 if {$msg != {}} {
9526 exec git tag -a -m $msg $tag $id
9527 } else {
9528 exec git tag $tag $id
9529 }
9530 } err]} {
9531 error_popup "[mc "Error creating tag:"] $err" $mktagtop
9532 return 0
9533 }
9534
9535 set tagids($tag) $id
9536 lappend idtags($id) $tag
9537 redrawtags $id
9538 addedtag $id
9539 dispneartags 0
9540 run refill_reflist
9541 return 1
9542 }
9543
9544 proc redrawtags {id} {
9545 global canv linehtag idpos currentid curview cmitlisted markedid
9546 global canvxmax iddrawn circleitem mainheadid circlecolors
9547 global mainheadcirclecolor
9548
9549 if {![commitinview $id $curview]} return
9550 if {![info exists iddrawn($id)]} return
9551 set row [rowofcommit $id]
9552 if {$id eq $mainheadid} {
9553 set ofill $mainheadcirclecolor
9554 } else {
9555 set ofill [lindex $circlecolors $cmitlisted($curview,$id)]
9556 }
9557 $canv itemconf $circleitem($row) -fill $ofill
9558 $canv delete tag.$id
9559 set xt [eval drawtags $id $idpos($id)]
9560 $canv coords $linehtag($id) $xt [lindex $idpos($id) 2]
9561 set text [$canv itemcget $linehtag($id) -text]
9562 set font [$canv itemcget $linehtag($id) -font]
9563 set xr [expr {$xt + [font measure $font $text]}]
9564 if {$xr > $canvxmax} {
9565 set canvxmax $xr
9566 setcanvscroll
9567 }
9568 if {[info exists currentid] && $currentid == $id} {
9569 make_secsel $id
9570 }
9571 if {[info exists markedid] && $markedid eq $id} {
9572 make_idmark $id
9573 }
9574 }
9575
9576 proc mktagcan {} {
9577 global mktagtop
9578
9579 catch {destroy $mktagtop}
9580 unset mktagtop
9581 }
9582
9583 proc mktaggo {} {
9584 if {![domktag]} return
9585 mktagcan
9586 }
9587
9588 proc copyreference {} {
9589 global rowmenuid autosellen
9590
9591 set format "%h (\"%s\", %ad)"
9592 set cmd [list git show -s --pretty=format:$format --date=short]
9593 if {$autosellen < 40} {
9594 lappend cmd --abbrev=$autosellen
9595 }
9596 set reference [eval exec $cmd $rowmenuid]
9597
9598 clipboard clear
9599 clipboard append $reference
9600 }
9601
9602 proc writecommit {} {
9603 global rowmenuid wrcomtop commitinfo wrcomcmd NS
9604
9605 set top .writecommit
9606 set wrcomtop $top
9607 catch {destroy $top}
9608 ttk_toplevel $top
9609 make_transient $top .
9610 ${NS}::label $top.title -text [mc "Write commit to file"]
9611 grid $top.title - -pady 10
9612 ${NS}::label $top.id -text [mc "ID:"]
9613 ${NS}::entry $top.sha1 -width 40
9614 $top.sha1 insert 0 $rowmenuid
9615 $top.sha1 conf -state readonly
9616 grid $top.id $top.sha1 -sticky w
9617 ${NS}::entry $top.head -width 60
9618 $top.head insert 0 [lindex $commitinfo($rowmenuid) 0]
9619 $top.head conf -state readonly
9620 grid x $top.head -sticky w
9621 ${NS}::label $top.clab -text [mc "Command:"]
9622 ${NS}::entry $top.cmd -width 60 -textvariable wrcomcmd
9623 grid $top.clab $top.cmd -sticky w -pady 10
9624 ${NS}::label $top.flab -text [mc "Output file:"]
9625 ${NS}::entry $top.fname -width 60
9626 $top.fname insert 0 [file normalize "commit-[string range $rowmenuid 0 6]"]
9627 grid $top.flab $top.fname -sticky w
9628 ${NS}::frame $top.buts
9629 ${NS}::button $top.buts.gen -text [mc "Write"] -command wrcomgo
9630 ${NS}::button $top.buts.can -text [mc "Cancel"] -command wrcomcan
9631 bind $top <Key-Return> wrcomgo
9632 bind $top <Key-Escape> wrcomcan
9633 grid $top.buts.gen $top.buts.can
9634 grid columnconfigure $top.buts 0 -weight 1 -uniform a
9635 grid columnconfigure $top.buts 1 -weight 1 -uniform a
9636 grid $top.buts - -pady 10 -sticky ew
9637 focus $top.fname
9638 }
9639
9640 proc wrcomgo {} {
9641 global wrcomtop
9642
9643 set id [$wrcomtop.sha1 get]
9644 set cmd "echo $id | [$wrcomtop.cmd get]"
9645 set fname [$wrcomtop.fname get]
9646 if {[catch {exec sh -c $cmd >$fname &} err]} {
9647 error_popup "[mc "Error writing commit:"] $err" $wrcomtop
9648 }
9649 catch {destroy $wrcomtop}
9650 unset wrcomtop
9651 }
9652
9653 proc wrcomcan {} {
9654 global wrcomtop
9655
9656 catch {destroy $wrcomtop}
9657 unset wrcomtop
9658 }
9659
9660 proc mkbranch {} {
9661 global NS rowmenuid
9662
9663 set top .branchdialog
9664
9665 set val(name) ""
9666 set val(id) $rowmenuid
9667 set val(command) [list mkbrgo $top]
9668
9669 set ui(title) [mc "Create branch"]
9670 set ui(accept) [mc "Create"]
9671
9672 branchdia $top val ui
9673 }
9674
9675 proc mvbranch {} {
9676 global NS
9677 global headmenuid headmenuhead
9678
9679 set top .branchdialog
9680
9681 set val(name) $headmenuhead
9682 set val(id) $headmenuid
9683 set val(command) [list mvbrgo $top $headmenuhead]
9684
9685 set ui(title) [mc "Rename branch %s" $headmenuhead]
9686 set ui(accept) [mc "Rename"]
9687
9688 branchdia $top val ui
9689 }
9690
9691 proc branchdia {top valvar uivar} {
9692 global NS commitinfo
9693 upvar $valvar val $uivar ui
9694
9695 catch {destroy $top}
9696 ttk_toplevel $top
9697 make_transient $top .
9698 ${NS}::label $top.title -text $ui(title)
9699 grid $top.title - -pady 10
9700 ${NS}::label $top.id -text [mc "ID:"]
9701 ${NS}::entry $top.sha1 -width 40
9702 $top.sha1 insert 0 $val(id)
9703 $top.sha1 conf -state readonly
9704 grid $top.id $top.sha1 -sticky w
9705 ${NS}::entry $top.head -width 60
9706 $top.head insert 0 [lindex $commitinfo($val(id)) 0]
9707 $top.head conf -state readonly
9708 grid x $top.head -sticky ew
9709 grid columnconfigure $top 1 -weight 1
9710 ${NS}::label $top.nlab -text [mc "Name:"]
9711 ${NS}::entry $top.name -width 40
9712 $top.name insert 0 $val(name)
9713 grid $top.nlab $top.name -sticky w
9714 ${NS}::frame $top.buts
9715 ${NS}::button $top.buts.go -text $ui(accept) -command $val(command)
9716 ${NS}::button $top.buts.can -text [mc "Cancel"] -command "catch {destroy $top}"
9717 bind $top <Key-Return> $val(command)
9718 bind $top <Key-Escape> "catch {destroy $top}"
9719 grid $top.buts.go $top.buts.can
9720 grid columnconfigure $top.buts 0 -weight 1 -uniform a
9721 grid columnconfigure $top.buts 1 -weight 1 -uniform a
9722 grid $top.buts - -pady 10 -sticky ew
9723 focus $top.name
9724 }
9725
9726 proc mkbrgo {top} {
9727 global headids idheads
9728
9729 set name [$top.name get]
9730 set id [$top.sha1 get]
9731 set cmdargs {}
9732 set old_id {}
9733 if {$name eq {}} {
9734 error_popup [mc "Please specify a name for the new branch"] $top
9735 return
9736 }
9737 if {[info exists headids($name)]} {
9738 if {![confirm_popup [mc \
9739 "Branch '%s' already exists. Overwrite?" $name] $top]} {
9740 return
9741 }
9742 set old_id $headids($name)
9743 lappend cmdargs -f
9744 }
9745 catch {destroy $top}
9746 lappend cmdargs $name $id
9747 nowbusy newbranch
9748 update
9749 if {[catch {
9750 eval exec git branch $cmdargs
9751 } err]} {
9752 notbusy newbranch
9753 error_popup $err
9754 } else {
9755 notbusy newbranch
9756 if {$old_id ne {}} {
9757 movehead $id $name
9758 movedhead $id $name
9759 redrawtags $old_id
9760 redrawtags $id
9761 } else {
9762 set headids($name) $id
9763 lappend idheads($id) $name
9764 addedhead $id $name
9765 redrawtags $id
9766 }
9767 dispneartags 0
9768 run refill_reflist
9769 }
9770 }
9771
9772 proc mvbrgo {top prevname} {
9773 global headids idheads mainhead mainheadid
9774
9775 set name [$top.name get]
9776 set id [$top.sha1 get]
9777 set cmdargs {}
9778 if {$name eq $prevname} {
9779 catch {destroy $top}
9780 return
9781 }
9782 if {$name eq {}} {
9783 error_popup [mc "Please specify a new name for the branch"] $top
9784 return
9785 }
9786 catch {destroy $top}
9787 lappend cmdargs -m $prevname $name
9788 nowbusy renamebranch
9789 update
9790 if {[catch {
9791 eval exec git branch $cmdargs
9792 } err]} {
9793 notbusy renamebranch
9794 error_popup $err
9795 } else {
9796 notbusy renamebranch
9797 removehead $id $prevname
9798 removedhead $id $prevname
9799 set headids($name) $id
9800 lappend idheads($id) $name
9801 addedhead $id $name
9802 if {$prevname eq $mainhead} {
9803 set mainhead $name
9804 set mainheadid $id
9805 }
9806 redrawtags $id
9807 dispneartags 0
9808 run refill_reflist
9809 }
9810 }
9811
9812 proc exec_citool {tool_args {baseid {}}} {
9813 global commitinfo env
9814
9815 set save_env [array get env GIT_AUTHOR_*]
9816
9817 if {$baseid ne {}} {
9818 if {![info exists commitinfo($baseid)]} {
9819 getcommit $baseid
9820 }
9821 set author [lindex $commitinfo($baseid) 1]
9822 set date [lindex $commitinfo($baseid) 2]
9823 if {[regexp {^\s*(\S.*\S|\S)\s*<(.*)>\s*$} \
9824 $author author name email]
9825 && $date ne {}} {
9826 set env(GIT_AUTHOR_NAME) $name
9827 set env(GIT_AUTHOR_EMAIL) $email
9828 set env(GIT_AUTHOR_DATE) $date
9829 }
9830 }
9831
9832 eval exec git citool $tool_args &
9833
9834 array unset env GIT_AUTHOR_*
9835 array set env $save_env
9836 }
9837
9838 proc cherrypick {} {
9839 global rowmenuid curview
9840 global mainhead mainheadid
9841 global gitdir
9842
9843 set oldhead [exec git rev-parse HEAD]
9844 set dheads [descheads $rowmenuid]
9845 if {$dheads ne {} && [lsearch -exact $dheads $oldhead] >= 0} {
9846 set ok [confirm_popup [mc "Commit %s is already\
9847 included in branch %s -- really re-apply it?" \
9848 [string range $rowmenuid 0 7] $mainhead]]
9849 if {!$ok} return
9850 }
9851 nowbusy cherrypick [mc "Cherry-picking"]
9852 update
9853 # Unfortunately git-cherry-pick writes stuff to stderr even when
9854 # no error occurs, and exec takes that as an indication of error...
9855 if {[catch {exec sh -c "git cherry-pick -r $rowmenuid 2>&1"} err]} {
9856 notbusy cherrypick
9857 if {[regexp -line \
9858 {Entry '(.*)' (would be overwritten by merge|not uptodate)} \
9859 $err msg fname]} {
9860 error_popup [mc "Cherry-pick failed because of local changes\
9861 to file '%s'.\nPlease commit, reset or stash\
9862 your changes and try again." $fname]
9863 } elseif {[regexp -line \
9864 {^(CONFLICT \(.*\):|Automatic cherry-pick failed|error: could not apply)} \
9865 $err]} {
9866 if {[confirm_popup [mc "Cherry-pick failed because of merge\
9867 conflict.\nDo you wish to run git citool to\
9868 resolve it?"]]} {
9869 # Force citool to read MERGE_MSG
9870 file delete [file join $gitdir "GITGUI_MSG"]
9871 exec_citool {} $rowmenuid
9872 }
9873 } else {
9874 error_popup $err
9875 }
9876 run updatecommits
9877 return
9878 }
9879 set newhead [exec git rev-parse HEAD]
9880 if {$newhead eq $oldhead} {
9881 notbusy cherrypick
9882 error_popup [mc "No changes committed"]
9883 return
9884 }
9885 addnewchild $newhead $oldhead
9886 if {[commitinview $oldhead $curview]} {
9887 # XXX this isn't right if we have a path limit...
9888 insertrow $newhead $oldhead $curview
9889 if {$mainhead ne {}} {
9890 movehead $newhead $mainhead
9891 movedhead $newhead $mainhead
9892 }
9893 set mainheadid $newhead
9894 redrawtags $oldhead
9895 redrawtags $newhead
9896 selbyid $newhead
9897 }
9898 notbusy cherrypick
9899 }
9900
9901 proc revert {} {
9902 global rowmenuid curview
9903 global mainhead mainheadid
9904 global gitdir
9905
9906 set oldhead [exec git rev-parse HEAD]
9907 set dheads [descheads $rowmenuid]
9908 if { $dheads eq {} || [lsearch -exact $dheads $oldhead] == -1 } {
9909 set ok [confirm_popup [mc "Commit %s is not\
9910 included in branch %s -- really revert it?" \
9911 [string range $rowmenuid 0 7] $mainhead]]
9912 if {!$ok} return
9913 }
9914 nowbusy revert [mc "Reverting"]
9915 update
9916
9917 if [catch {exec git revert --no-edit $rowmenuid} err] {
9918 notbusy revert
9919 if [regexp {files would be overwritten by merge:(\n(( |\t)+[^\n]+\n)+)}\
9920 $err match files] {
9921 regsub {\n( |\t)+} $files "\n" files
9922 error_popup [mc "Revert failed because of local changes to\
9923 the following files:%s Please commit, reset or stash \
9924 your changes and try again." $files]
9925 } elseif [regexp {error: could not revert} $err] {
9926 if [confirm_popup [mc "Revert failed because of merge conflict.\n\
9927 Do you wish to run git citool to resolve it?"]] {
9928 # Force citool to read MERGE_MSG
9929 file delete [file join $gitdir "GITGUI_MSG"]
9930 exec_citool {} $rowmenuid
9931 }
9932 } else { error_popup $err }
9933 run updatecommits
9934 return
9935 }
9936
9937 set newhead [exec git rev-parse HEAD]
9938 if { $newhead eq $oldhead } {
9939 notbusy revert
9940 error_popup [mc "No changes committed"]
9941 return
9942 }
9943
9944 addnewchild $newhead $oldhead
9945
9946 if [commitinview $oldhead $curview] {
9947 # XXX this isn't right if we have a path limit...
9948 insertrow $newhead $oldhead $curview
9949 if {$mainhead ne {}} {
9950 movehead $newhead $mainhead
9951 movedhead $newhead $mainhead
9952 }
9953 set mainheadid $newhead
9954 redrawtags $oldhead
9955 redrawtags $newhead
9956 selbyid $newhead
9957 }
9958
9959 notbusy revert
9960 }
9961
9962 proc resethead {} {
9963 global mainhead rowmenuid confirm_ok resettype NS
9964
9965 set confirm_ok 0
9966 set w ".confirmreset"
9967 ttk_toplevel $w
9968 make_transient $w .
9969 wm title $w [mc "Confirm reset"]
9970 ${NS}::label $w.m -text \
9971 [mc "Reset branch %s to %s?" $mainhead [string range $rowmenuid 0 7]]
9972 pack $w.m -side top -fill x -padx 20 -pady 20
9973 ${NS}::labelframe $w.f -text [mc "Reset type:"]
9974 set resettype mixed
9975 ${NS}::radiobutton $w.f.soft -value soft -variable resettype \
9976 -text [mc "Soft: Leave working tree and index untouched"]
9977 grid $w.f.soft -sticky w
9978 ${NS}::radiobutton $w.f.mixed -value mixed -variable resettype \
9979 -text [mc "Mixed: Leave working tree untouched, reset index"]
9980 grid $w.f.mixed -sticky w
9981 ${NS}::radiobutton $w.f.hard -value hard -variable resettype \
9982 -text [mc "Hard: Reset working tree and index\n(discard ALL local changes)"]
9983 grid $w.f.hard -sticky w
9984 pack $w.f -side top -fill x -padx 4
9985 ${NS}::button $w.ok -text [mc OK] -command "set confirm_ok 1; destroy $w"
9986 pack $w.ok -side left -fill x -padx 20 -pady 20
9987 ${NS}::button $w.cancel -text [mc Cancel] -command "destroy $w"
9988 bind $w <Key-Escape> [list destroy $w]
9989 pack $w.cancel -side right -fill x -padx 20 -pady 20
9990 bind $w <Visibility> "grab $w; focus $w"
9991 tkwait window $w
9992 if {!$confirm_ok} return
9993 if {[catch {set fd [open \
9994 [list | git reset --$resettype $rowmenuid 2>@1] r]} err]} {
9995 error_popup $err
9996 } else {
9997 dohidelocalchanges
9998 filerun $fd [list readresetstat $fd]
9999 nowbusy reset [mc "Resetting"]
10000 selbyid $rowmenuid
10001 }
10002 }
10003
10004 proc readresetstat {fd} {
10005 global mainhead mainheadid showlocalchanges rprogcoord
10006
10007 if {[gets $fd line] >= 0} {
10008 if {[regexp {([0-9]+)% \(([0-9]+)/([0-9]+)\)} $line match p m n]} {
10009 set rprogcoord [expr {1.0 * $m / $n}]
10010 adjustprogress
10011 }
10012 return 1
10013 }
10014 set rprogcoord 0
10015 adjustprogress
10016 notbusy reset
10017 if {[catch {close $fd} err]} {
10018 error_popup $err
10019 }
10020 set oldhead $mainheadid
10021 set newhead [exec git rev-parse HEAD]
10022 if {$newhead ne $oldhead} {
10023 movehead $newhead $mainhead
10024 movedhead $newhead $mainhead
10025 set mainheadid $newhead
10026 redrawtags $oldhead
10027 redrawtags $newhead
10028 }
10029 if {$showlocalchanges} {
10030 doshowlocalchanges
10031 }
10032 return 0
10033 }
10034
10035 # context menu for a head
10036 proc headmenu {x y id head} {
10037 global headmenuid headmenuhead headctxmenu mainhead headids
10038
10039 stopfinding
10040 set headmenuid $id
10041 set headmenuhead $head
10042 array set state {0 normal 1 normal 2 normal}
10043 if {[string match "remotes/*" $head]} {
10044 set localhead [string range $head [expr [string last / $head] + 1] end]
10045 if {[info exists headids($localhead)]} {
10046 set state(0) disabled
10047 }
10048 array set state {1 disabled 2 disabled}
10049 }
10050 if {$head eq $mainhead} {
10051 array set state {0 disabled 2 disabled}
10052 }
10053 foreach i {0 1 2} {
10054 $headctxmenu entryconfigure $i -state $state($i)
10055 }
10056 tk_popup $headctxmenu $x $y
10057 }
10058
10059 proc cobranch {} {
10060 global headmenuid headmenuhead headids
10061 global showlocalchanges
10062
10063 # check the tree is clean first??
10064 set newhead $headmenuhead
10065 set command [list | git checkout]
10066 if {[string match "remotes/*" $newhead]} {
10067 set remote $newhead
10068 set newhead [string range $newhead [expr [string last / $newhead] + 1] end]
10069 # The following check is redundant - the menu option should
10070 # be disabled to begin with...
10071 if {[info exists headids($newhead)]} {
10072 error_popup [mc "A local branch named %s exists already" $newhead]
10073 return
10074 }
10075 lappend command -b $newhead --track $remote
10076 } else {
10077 lappend command $newhead
10078 }
10079 lappend command 2>@1
10080 nowbusy checkout [mc "Checking out"]
10081 update
10082 dohidelocalchanges
10083 if {[catch {
10084 set fd [open $command r]
10085 } err]} {
10086 notbusy checkout
10087 error_popup $err
10088 if {$showlocalchanges} {
10089 dodiffindex
10090 }
10091 } else {
10092 filerun $fd [list readcheckoutstat $fd $newhead $headmenuid]
10093 }
10094 }
10095
10096 proc readcheckoutstat {fd newhead newheadid} {
10097 global mainhead mainheadid headids idheads showlocalchanges progresscoords
10098 global viewmainheadid curview
10099
10100 if {[gets $fd line] >= 0} {
10101 if {[regexp {([0-9]+)% \(([0-9]+)/([0-9]+)\)} $line match p m n]} {
10102 set progresscoords [list 0 [expr {1.0 * $m / $n}]]
10103 adjustprogress
10104 }
10105 return 1
10106 }
10107 set progresscoords {0 0}
10108 adjustprogress
10109 notbusy checkout
10110 if {[catch {close $fd} err]} {
10111 error_popup $err
10112 return
10113 }
10114 set oldmainid $mainheadid
10115 if {! [info exists headids($newhead)]} {
10116 set headids($newhead) $newheadid
10117 lappend idheads($newheadid) $newhead
10118 addedhead $newheadid $newhead
10119 }
10120 set mainhead $newhead
10121 set mainheadid $newheadid
10122 set viewmainheadid($curview) $newheadid
10123 redrawtags $oldmainid
10124 redrawtags $newheadid
10125 selbyid $newheadid
10126 if {$showlocalchanges} {
10127 dodiffindex
10128 }
10129 }
10130
10131 proc rmbranch {} {
10132 global headmenuid headmenuhead mainhead
10133 global idheads
10134
10135 set head $headmenuhead
10136 set id $headmenuid
10137 # this check shouldn't be needed any more...
10138 if {$head eq $mainhead} {
10139 error_popup [mc "Cannot delete the currently checked-out branch"]
10140 return
10141 }
10142 set dheads [descheads $id]
10143 if {[llength $dheads] == 1 && $idheads($dheads) eq $head} {
10144 # the stuff on this branch isn't on any other branch
10145 if {![confirm_popup [mc "The commits on branch %s aren't on any other\
10146 branch.\nReally delete branch %s?" $head $head]]} return
10147 }
10148 nowbusy rmbranch
10149 update
10150 if {[catch {exec git branch -D $head} err]} {
10151 notbusy rmbranch
10152 error_popup $err
10153 return
10154 }
10155 removehead $id $head
10156 removedhead $id $head
10157 redrawtags $id
10158 notbusy rmbranch
10159 dispneartags 0
10160 run refill_reflist
10161 }
10162
10163 # Display a list of tags and heads
10164 proc showrefs {} {
10165 global showrefstop bgcolor fgcolor selectbgcolor NS
10166 global bglist fglist reflistfilter reflist maincursor
10167
10168 set top .showrefs
10169 set showrefstop $top
10170 if {[winfo exists $top]} {
10171 raise $top
10172 refill_reflist
10173 return
10174 }
10175 ttk_toplevel $top
10176 wm title $top [mc "Tags and heads: %s" [file tail [pwd]]]
10177 make_transient $top .
10178 text $top.list -background $bgcolor -foreground $fgcolor \
10179 -selectbackground $selectbgcolor -font mainfont \
10180 -xscrollcommand "$top.xsb set" -yscrollcommand "$top.ysb set" \
10181 -width 60 -height 20 -cursor $maincursor \
10182 -spacing1 1 -spacing3 1 -state disabled
10183 $top.list tag configure highlight -background $selectbgcolor
10184 if {![lsearch -exact $bglist $top.list]} {
10185 lappend bglist $top.list
10186 lappend fglist $top.list
10187 }
10188 ${NS}::scrollbar $top.ysb -command "$top.list yview" -orient vertical
10189 ${NS}::scrollbar $top.xsb -command "$top.list xview" -orient horizontal
10190 grid $top.list $top.ysb -sticky nsew
10191 grid $top.xsb x -sticky ew
10192 ${NS}::frame $top.f
10193 ${NS}::label $top.f.l -text "[mc "Filter"]: "
10194 ${NS}::entry $top.f.e -width 20 -textvariable reflistfilter
10195 set reflistfilter "*"
10196 trace add variable reflistfilter write reflistfilter_change
10197 pack $top.f.e -side right -fill x -expand 1
10198 pack $top.f.l -side left
10199 grid $top.f - -sticky ew -pady 2
10200 ${NS}::button $top.close -command [list destroy $top] -text [mc "Close"]
10201 bind $top <Key-Escape> [list destroy $top]
10202 grid $top.close -
10203 grid columnconfigure $top 0 -weight 1
10204 grid rowconfigure $top 0 -weight 1
10205 bind $top.list <1> {break}
10206 bind $top.list <B1-Motion> {break}
10207 bind $top.list <ButtonRelease-1> {sel_reflist %W %x %y; break}
10208 set reflist {}
10209 refill_reflist
10210 }
10211
10212 proc sel_reflist {w x y} {
10213 global showrefstop reflist headids tagids otherrefids
10214
10215 if {![winfo exists $showrefstop]} return
10216 set l [lindex [split [$w index "@$x,$y"] "."] 0]
10217 set ref [lindex $reflist [expr {$l-1}]]
10218 set n [lindex $ref 0]
10219 switch -- [lindex $ref 1] {
10220 "H" {selbyid $headids($n)}
10221 "R" {selbyid $headids($n)}
10222 "T" {selbyid $tagids($n)}
10223 "o" {selbyid $otherrefids($n)}
10224 }
10225 $showrefstop.list tag add highlight $l.0 "$l.0 lineend"
10226 }
10227
10228 proc unsel_reflist {} {
10229 global showrefstop
10230
10231 if {![info exists showrefstop] || ![winfo exists $showrefstop]} return
10232 $showrefstop.list tag remove highlight 0.0 end
10233 }
10234
10235 proc reflistfilter_change {n1 n2 op} {
10236 global reflistfilter
10237
10238 after cancel refill_reflist
10239 after 200 refill_reflist
10240 }
10241
10242 proc refill_reflist {} {
10243 global reflist reflistfilter showrefstop headids tagids otherrefids
10244 global curview
10245
10246 if {![info exists showrefstop] || ![winfo exists $showrefstop]} return
10247 set refs {}
10248 foreach n [array names headids] {
10249 if {[string match $reflistfilter $n]} {
10250 if {[commitinview $headids($n) $curview]} {
10251 if {[string match "remotes/*" $n]} {
10252 lappend refs [list $n R]
10253 } else {
10254 lappend refs [list $n H]
10255 }
10256 } else {
10257 interestedin $headids($n) {run refill_reflist}
10258 }
10259 }
10260 }
10261 foreach n [array names tagids] {
10262 if {[string match $reflistfilter $n]} {
10263 if {[commitinview $tagids($n) $curview]} {
10264 lappend refs [list $n T]
10265 } else {
10266 interestedin $tagids($n) {run refill_reflist}
10267 }
10268 }
10269 }
10270 foreach n [array names otherrefids] {
10271 if {[string match $reflistfilter $n]} {
10272 if {[commitinview $otherrefids($n) $curview]} {
10273 lappend refs [list $n o]
10274 } else {
10275 interestedin $otherrefids($n) {run refill_reflist}
10276 }
10277 }
10278 }
10279 set refs [lsort -index 0 $refs]
10280 if {$refs eq $reflist} return
10281
10282 # Update the contents of $showrefstop.list according to the
10283 # differences between $reflist (old) and $refs (new)
10284 $showrefstop.list conf -state normal
10285 $showrefstop.list insert end "\n"
10286 set i 0
10287 set j 0
10288 while {$i < [llength $reflist] || $j < [llength $refs]} {
10289 if {$i < [llength $reflist]} {
10290 if {$j < [llength $refs]} {
10291 set cmp [string compare [lindex $reflist $i 0] \
10292 [lindex $refs $j 0]]
10293 if {$cmp == 0} {
10294 set cmp [string compare [lindex $reflist $i 1] \
10295 [lindex $refs $j 1]]
10296 }
10297 } else {
10298 set cmp -1
10299 }
10300 } else {
10301 set cmp 1
10302 }
10303 switch -- $cmp {
10304 -1 {
10305 $showrefstop.list delete "[expr {$j+1}].0" "[expr {$j+2}].0"
10306 incr i
10307 }
10308 0 {
10309 incr i
10310 incr j
10311 }
10312 1 {
10313 set l [expr {$j + 1}]
10314 $showrefstop.list image create $l.0 -align baseline \
10315 -image reficon-[lindex $refs $j 1] -padx 2
10316 $showrefstop.list insert $l.1 "[lindex $refs $j 0]\n"
10317 incr j
10318 }
10319 }
10320 }
10321 set reflist $refs
10322 # delete last newline
10323 $showrefstop.list delete end-2c end-1c
10324 $showrefstop.list conf -state disabled
10325 }
10326
10327 # Stuff for finding nearby tags
10328 proc getallcommits {} {
10329 global allcommits nextarc seeds allccache allcwait cachedarcs allcupdate
10330 global idheads idtags idotherrefs allparents tagobjid
10331 global gitdir
10332
10333 if {![info exists allcommits]} {
10334 set nextarc 0
10335 set allcommits 0
10336 set seeds {}
10337 set allcwait 0
10338 set cachedarcs 0
10339 set allccache [file join $gitdir "gitk.cache"]
10340 if {![catch {
10341 set f [open $allccache r]
10342 set allcwait 1
10343 getcache $f
10344 }]} return
10345 }
10346
10347 if {$allcwait} {
10348 return
10349 }
10350 set cmd [list | git rev-list --parents]
10351 set allcupdate [expr {$seeds ne {}}]
10352 if {!$allcupdate} {
10353 set ids "--all"
10354 } else {
10355 set refs [concat [array names idheads] [array names idtags] \
10356 [array names idotherrefs]]
10357 set ids {}
10358 set tagobjs {}
10359 foreach name [array names tagobjid] {
10360 lappend tagobjs $tagobjid($name)
10361 }
10362 foreach id [lsort -unique $refs] {
10363 if {![info exists allparents($id)] &&
10364 [lsearch -exact $tagobjs $id] < 0} {
10365 lappend ids $id
10366 }
10367 }
10368 if {$ids ne {}} {
10369 foreach id $seeds {
10370 lappend ids "^$id"
10371 }
10372 lappend ids "--"
10373 }
10374 }
10375 if {$ids ne {}} {
10376 if {$ids eq "--all"} {
10377 set cmd [concat $cmd "--all"]
10378 } else {
10379 set cmd [concat $cmd --stdin "<<[join $ids "\\n"]"]
10380 }
10381 set fd [open $cmd r]
10382 fconfigure $fd -blocking 0
10383 incr allcommits
10384 nowbusy allcommits
10385 filerun $fd [list getallclines $fd]
10386 } else {
10387 dispneartags 0
10388 }
10389 }
10390
10391 # Since most commits have 1 parent and 1 child, we group strings of
10392 # such commits into "arcs" joining branch/merge points (BMPs), which
10393 # are commits that either don't have 1 parent or don't have 1 child.
10394 #
10395 # arcnos(id) - incoming arcs for BMP, arc we're on for other nodes
10396 # arcout(id) - outgoing arcs for BMP
10397 # arcids(a) - list of IDs on arc including end but not start
10398 # arcstart(a) - BMP ID at start of arc
10399 # arcend(a) - BMP ID at end of arc
10400 # growing(a) - arc a is still growing
10401 # arctags(a) - IDs out of arcids (excluding end) that have tags
10402 # archeads(a) - IDs out of arcids (excluding end) that have heads
10403 # The start of an arc is at the descendent end, so "incoming" means
10404 # coming from descendents, and "outgoing" means going towards ancestors.
10405
10406 proc getallclines {fd} {
10407 global allparents allchildren idtags idheads nextarc
10408 global arcnos arcids arctags arcout arcend arcstart archeads growing
10409 global seeds allcommits cachedarcs allcupdate
10410
10411 set nid 0
10412 while {[incr nid] <= 1000 && [gets $fd line] >= 0} {
10413 set id [lindex $line 0]
10414 if {[info exists allparents($id)]} {
10415 # seen it already
10416 continue
10417 }
10418 set cachedarcs 0
10419 set olds [lrange $line 1 end]
10420 set allparents($id) $olds
10421 if {![info exists allchildren($id)]} {
10422 set allchildren($id) {}
10423 set arcnos($id) {}
10424 lappend seeds $id
10425 } else {
10426 set a $arcnos($id)
10427 if {[llength $olds] == 1 && [llength $a] == 1} {
10428 lappend arcids($a) $id
10429 if {[info exists idtags($id)]} {
10430 lappend arctags($a) $id
10431 }
10432 if {[info exists idheads($id)]} {
10433 lappend archeads($a) $id
10434 }
10435 if {[info exists allparents($olds)]} {
10436 # seen parent already
10437 if {![info exists arcout($olds)]} {
10438 splitarc $olds
10439 }
10440 lappend arcids($a) $olds
10441 set arcend($a) $olds
10442 unset growing($a)
10443 }
10444 lappend allchildren($olds) $id
10445 lappend arcnos($olds) $a
10446 continue
10447 }
10448 }
10449 foreach a $arcnos($id) {
10450 lappend arcids($a) $id
10451 set arcend($a) $id
10452 unset growing($a)
10453 }
10454
10455 set ao {}
10456 foreach p $olds {
10457 lappend allchildren($p) $id
10458 set a [incr nextarc]
10459 set arcstart($a) $id
10460 set archeads($a) {}
10461 set arctags($a) {}
10462 set archeads($a) {}
10463 set arcids($a) {}
10464 lappend ao $a
10465 set growing($a) 1
10466 if {[info exists allparents($p)]} {
10467 # seen it already, may need to make a new branch
10468 if {![info exists arcout($p)]} {
10469 splitarc $p
10470 }
10471 lappend arcids($a) $p
10472 set arcend($a) $p
10473 unset growing($a)
10474 }
10475 lappend arcnos($p) $a
10476 }
10477 set arcout($id) $ao
10478 }
10479 if {$nid > 0} {
10480 global cached_dheads cached_dtags cached_atags
10481 unset -nocomplain cached_dheads
10482 unset -nocomplain cached_dtags
10483 unset -nocomplain cached_atags
10484 }
10485 if {![eof $fd]} {
10486 return [expr {$nid >= 1000? 2: 1}]
10487 }
10488 set cacheok 1
10489 if {[catch {
10490 fconfigure $fd -blocking 1
10491 close $fd
10492 } err]} {
10493 # got an error reading the list of commits
10494 # if we were updating, try rereading the whole thing again
10495 if {$allcupdate} {
10496 incr allcommits -1
10497 dropcache $err
10498 return
10499 }
10500 error_popup "[mc "Error reading commit topology information;\
10501 branch and preceding/following tag information\
10502 will be incomplete."]\n($err)"
10503 set cacheok 0
10504 }
10505 if {[incr allcommits -1] == 0} {
10506 notbusy allcommits
10507 if {$cacheok} {
10508 run savecache
10509 }
10510 }
10511 dispneartags 0
10512 return 0
10513 }
10514
10515 proc recalcarc {a} {
10516 global arctags archeads arcids idtags idheads
10517
10518 set at {}
10519 set ah {}
10520 foreach id [lrange $arcids($a) 0 end-1] {
10521 if {[info exists idtags($id)]} {
10522 lappend at $id
10523 }
10524 if {[info exists idheads($id)]} {
10525 lappend ah $id
10526 }
10527 }
10528 set arctags($a) $at
10529 set archeads($a) $ah
10530 }
10531
10532 proc splitarc {p} {
10533 global arcnos arcids nextarc arctags archeads idtags idheads
10534 global arcstart arcend arcout allparents growing
10535
10536 set a $arcnos($p)
10537 if {[llength $a] != 1} {
10538 puts "oops splitarc called but [llength $a] arcs already"
10539 return
10540 }
10541 set a [lindex $a 0]
10542 set i [lsearch -exact $arcids($a) $p]
10543 if {$i < 0} {
10544 puts "oops splitarc $p not in arc $a"
10545 return
10546 }
10547 set na [incr nextarc]
10548 if {[info exists arcend($a)]} {
10549 set arcend($na) $arcend($a)
10550 } else {
10551 set l [lindex $allparents([lindex $arcids($a) end]) 0]
10552 set j [lsearch -exact $arcnos($l) $a]
10553 set arcnos($l) [lreplace $arcnos($l) $j $j $na]
10554 }
10555 set tail [lrange $arcids($a) [expr {$i+1}] end]
10556 set arcids($a) [lrange $arcids($a) 0 $i]
10557 set arcend($a) $p
10558 set arcstart($na) $p
10559 set arcout($p) $na
10560 set arcids($na) $tail
10561 if {[info exists growing($a)]} {
10562 set growing($na) 1
10563 unset growing($a)
10564 }
10565
10566 foreach id $tail {
10567 if {[llength $arcnos($id)] == 1} {
10568 set arcnos($id) $na
10569 } else {
10570 set j [lsearch -exact $arcnos($id) $a]
10571 set arcnos($id) [lreplace $arcnos($id) $j $j $na]
10572 }
10573 }
10574
10575 # reconstruct tags and heads lists
10576 if {$arctags($a) ne {} || $archeads($a) ne {}} {
10577 recalcarc $a
10578 recalcarc $na
10579 } else {
10580 set arctags($na) {}
10581 set archeads($na) {}
10582 }
10583 }
10584
10585 # Update things for a new commit added that is a child of one
10586 # existing commit. Used when cherry-picking.
10587 proc addnewchild {id p} {
10588 global allparents allchildren idtags nextarc
10589 global arcnos arcids arctags arcout arcend arcstart archeads growing
10590 global seeds allcommits
10591
10592 if {![info exists allcommits] || ![info exists arcnos($p)]} return
10593 set allparents($id) [list $p]
10594 set allchildren($id) {}
10595 set arcnos($id) {}
10596 lappend seeds $id
10597 lappend allchildren($p) $id
10598 set a [incr nextarc]
10599 set arcstart($a) $id
10600 set archeads($a) {}
10601 set arctags($a) {}
10602 set arcids($a) [list $p]
10603 set arcend($a) $p
10604 if {![info exists arcout($p)]} {
10605 splitarc $p
10606 }
10607 lappend arcnos($p) $a
10608 set arcout($id) [list $a]
10609 }
10610
10611 # This implements a cache for the topology information.
10612 # The cache saves, for each arc, the start and end of the arc,
10613 # the ids on the arc, and the outgoing arcs from the end.
10614 proc readcache {f} {
10615 global arcnos arcids arcout arcstart arcend arctags archeads nextarc
10616 global idtags idheads allparents cachedarcs possible_seeds seeds growing
10617 global allcwait
10618
10619 set a $nextarc
10620 set lim $cachedarcs
10621 if {$lim - $a > 500} {
10622 set lim [expr {$a + 500}]
10623 }
10624 if {[catch {
10625 if {$a == $lim} {
10626 # finish reading the cache and setting up arctags, etc.
10627 set line [gets $f]
10628 if {$line ne "1"} {error "bad final version"}
10629 close $f
10630 foreach id [array names idtags] {
10631 if {[info exists arcnos($id)] && [llength $arcnos($id)] == 1 &&
10632 [llength $allparents($id)] == 1} {
10633 set a [lindex $arcnos($id) 0]
10634 if {$arctags($a) eq {}} {
10635 recalcarc $a
10636 }
10637 }
10638 }
10639 foreach id [array names idheads] {
10640 if {[info exists arcnos($id)] && [llength $arcnos($id)] == 1 &&
10641 [llength $allparents($id)] == 1} {
10642 set a [lindex $arcnos($id) 0]
10643 if {$archeads($a) eq {}} {
10644 recalcarc $a
10645 }
10646 }
10647 }
10648 foreach id [lsort -unique $possible_seeds] {
10649 if {$arcnos($id) eq {}} {
10650 lappend seeds $id
10651 }
10652 }
10653 set allcwait 0
10654 } else {
10655 while {[incr a] <= $lim} {
10656 set line [gets $f]
10657 if {[llength $line] != 3} {error "bad line"}
10658 set s [lindex $line 0]
10659 set arcstart($a) $s
10660 lappend arcout($s) $a
10661 if {![info exists arcnos($s)]} {
10662 lappend possible_seeds $s
10663 set arcnos($s) {}
10664 }
10665 set e [lindex $line 1]
10666 if {$e eq {}} {
10667 set growing($a) 1
10668 } else {
10669 set arcend($a) $e
10670 if {![info exists arcout($e)]} {
10671 set arcout($e) {}
10672 }
10673 }
10674 set arcids($a) [lindex $line 2]
10675 foreach id $arcids($a) {
10676 lappend allparents($s) $id
10677 set s $id
10678 lappend arcnos($id) $a
10679 }
10680 if {![info exists allparents($s)]} {
10681 set allparents($s) {}
10682 }
10683 set arctags($a) {}
10684 set archeads($a) {}
10685 }
10686 set nextarc [expr {$a - 1}]
10687 }
10688 } err]} {
10689 dropcache $err
10690 return 0
10691 }
10692 if {!$allcwait} {
10693 getallcommits
10694 }
10695 return $allcwait
10696 }
10697
10698 proc getcache {f} {
10699 global nextarc cachedarcs possible_seeds
10700
10701 if {[catch {
10702 set line [gets $f]
10703 if {[llength $line] != 2 || [lindex $line 0] ne "1"} {error "bad version"}
10704 # make sure it's an integer
10705 set cachedarcs [expr {int([lindex $line 1])}]
10706 if {$cachedarcs < 0} {error "bad number of arcs"}
10707 set nextarc 0
10708 set possible_seeds {}
10709 run readcache $f
10710 } err]} {
10711 dropcache $err
10712 }
10713 return 0
10714 }
10715
10716 proc dropcache {err} {
10717 global allcwait nextarc cachedarcs seeds
10718
10719 #puts "dropping cache ($err)"
10720 foreach v {arcnos arcout arcids arcstart arcend growing \
10721 arctags archeads allparents allchildren} {
10722 global $v
10723 unset -nocomplain $v
10724 }
10725 set allcwait 0
10726 set nextarc 0
10727 set cachedarcs 0
10728 set seeds {}
10729 getallcommits
10730 }
10731
10732 proc writecache {f} {
10733 global cachearc cachedarcs allccache
10734 global arcstart arcend arcnos arcids arcout
10735
10736 set a $cachearc
10737 set lim $cachedarcs
10738 if {$lim - $a > 1000} {
10739 set lim [expr {$a + 1000}]
10740 }
10741 if {[catch {
10742 while {[incr a] <= $lim} {
10743 if {[info exists arcend($a)]} {
10744 puts $f [list $arcstart($a) $arcend($a) $arcids($a)]
10745 } else {
10746 puts $f [list $arcstart($a) {} $arcids($a)]
10747 }
10748 }
10749 } err]} {
10750 catch {close $f}
10751 catch {file delete $allccache}
10752 #puts "writing cache failed ($err)"
10753 return 0
10754 }
10755 set cachearc [expr {$a - 1}]
10756 if {$a > $cachedarcs} {
10757 puts $f "1"
10758 close $f
10759 return 0
10760 }
10761 return 1
10762 }
10763
10764 proc savecache {} {
10765 global nextarc cachedarcs cachearc allccache
10766
10767 if {$nextarc == $cachedarcs} return
10768 set cachearc 0
10769 set cachedarcs $nextarc
10770 catch {
10771 set f [open $allccache w]
10772 puts $f [list 1 $cachedarcs]
10773 run writecache $f
10774 }
10775 }
10776
10777 # Returns 1 if a is an ancestor of b, -1 if b is an ancestor of a,
10778 # or 0 if neither is true.
10779 proc anc_or_desc {a b} {
10780 global arcout arcstart arcend arcnos cached_isanc
10781
10782 if {$arcnos($a) eq $arcnos($b)} {
10783 # Both are on the same arc(s); either both are the same BMP,
10784 # or if one is not a BMP, the other is also not a BMP or is
10785 # the BMP at end of the arc (and it only has 1 incoming arc).
10786 # Or both can be BMPs with no incoming arcs.
10787 if {$a eq $b || $arcnos($a) eq {}} {
10788 return 0
10789 }
10790 # assert {[llength $arcnos($a)] == 1}
10791 set arc [lindex $arcnos($a) 0]
10792 set i [lsearch -exact $arcids($arc) $a]
10793 set j [lsearch -exact $arcids($arc) $b]
10794 if {$i < 0 || $i > $j} {
10795 return 1
10796 } else {
10797 return -1
10798 }
10799 }
10800
10801 if {![info exists arcout($a)]} {
10802 set arc [lindex $arcnos($a) 0]
10803 if {[info exists arcend($arc)]} {
10804 set aend $arcend($arc)
10805 } else {
10806 set aend {}
10807 }
10808 set a $arcstart($arc)
10809 } else {
10810 set aend $a
10811 }
10812 if {![info exists arcout($b)]} {
10813 set arc [lindex $arcnos($b) 0]
10814 if {[info exists arcend($arc)]} {
10815 set bend $arcend($arc)
10816 } else {
10817 set bend {}
10818 }
10819 set b $arcstart($arc)
10820 } else {
10821 set bend $b
10822 }
10823 if {$a eq $bend} {
10824 return 1
10825 }
10826 if {$b eq $aend} {
10827 return -1
10828 }
10829 if {[info exists cached_isanc($a,$bend)]} {
10830 if {$cached_isanc($a,$bend)} {
10831 return 1
10832 }
10833 }
10834 if {[info exists cached_isanc($b,$aend)]} {
10835 if {$cached_isanc($b,$aend)} {
10836 return -1
10837 }
10838 if {[info exists cached_isanc($a,$bend)]} {
10839 return 0
10840 }
10841 }
10842
10843 set todo [list $a $b]
10844 set anc($a) a
10845 set anc($b) b
10846 for {set i 0} {$i < [llength $todo]} {incr i} {
10847 set x [lindex $todo $i]
10848 if {$anc($x) eq {}} {
10849 continue
10850 }
10851 foreach arc $arcnos($x) {
10852 set xd $arcstart($arc)
10853 if {$xd eq $bend} {
10854 set cached_isanc($a,$bend) 1
10855 set cached_isanc($b,$aend) 0
10856 return 1
10857 } elseif {$xd eq $aend} {
10858 set cached_isanc($b,$aend) 1
10859 set cached_isanc($a,$bend) 0
10860 return -1
10861 }
10862 if {![info exists anc($xd)]} {
10863 set anc($xd) $anc($x)
10864 lappend todo $xd
10865 } elseif {$anc($xd) ne $anc($x)} {
10866 set anc($xd) {}
10867 }
10868 }
10869 }
10870 set cached_isanc($a,$bend) 0
10871 set cached_isanc($b,$aend) 0
10872 return 0
10873 }
10874
10875 # This identifies whether $desc has an ancestor that is
10876 # a growing tip of the graph and which is not an ancestor of $anc
10877 # and returns 0 if so and 1 if not.
10878 # If we subsequently discover a tag on such a growing tip, and that
10879 # turns out to be a descendent of $anc (which it could, since we
10880 # don't necessarily see children before parents), then $desc
10881 # isn't a good choice to display as a descendent tag of
10882 # $anc (since it is the descendent of another tag which is
10883 # a descendent of $anc). Similarly, $anc isn't a good choice to
10884 # display as a ancestor tag of $desc.
10885 #
10886 proc is_certain {desc anc} {
10887 global arcnos arcout arcstart arcend growing problems
10888
10889 set certain {}
10890 if {[llength $arcnos($anc)] == 1} {
10891 # tags on the same arc are certain
10892 if {$arcnos($desc) eq $arcnos($anc)} {
10893 return 1
10894 }
10895 if {![info exists arcout($anc)]} {
10896 # if $anc is partway along an arc, use the start of the arc instead
10897 set a [lindex $arcnos($anc) 0]
10898 set anc $arcstart($a)
10899 }
10900 }
10901 if {[llength $arcnos($desc)] > 1 || [info exists arcout($desc)]} {
10902 set x $desc
10903 } else {
10904 set a [lindex $arcnos($desc) 0]
10905 set x $arcend($a)
10906 }
10907 if {$x == $anc} {
10908 return 1
10909 }
10910 set anclist [list $x]
10911 set dl($x) 1
10912 set nnh 1
10913 set ngrowanc 0
10914 for {set i 0} {$i < [llength $anclist] && ($nnh > 0 || $ngrowanc > 0)} {incr i} {
10915 set x [lindex $anclist $i]
10916 if {$dl($x)} {
10917 incr nnh -1
10918 }
10919 set done($x) 1
10920 foreach a $arcout($x) {
10921 if {[info exists growing($a)]} {
10922 if {![info exists growanc($x)] && $dl($x)} {
10923 set growanc($x) 1
10924 incr ngrowanc
10925 }
10926 } else {
10927 set y $arcend($a)
10928 if {[info exists dl($y)]} {
10929 if {$dl($y)} {
10930 if {!$dl($x)} {
10931 set dl($y) 0
10932 if {![info exists done($y)]} {
10933 incr nnh -1
10934 }
10935 if {[info exists growanc($x)]} {
10936 incr ngrowanc -1
10937 }
10938 set xl [list $y]
10939 for {set k 0} {$k < [llength $xl]} {incr k} {
10940 set z [lindex $xl $k]
10941 foreach c $arcout($z) {
10942 if {[info exists arcend($c)]} {
10943 set v $arcend($c)
10944 if {[info exists dl($v)] && $dl($v)} {
10945 set dl($v) 0
10946 if {![info exists done($v)]} {
10947 incr nnh -1
10948 }
10949 if {[info exists growanc($v)]} {
10950 incr ngrowanc -1
10951 }
10952 lappend xl $v
10953 }
10954 }
10955 }
10956 }
10957 }
10958 }
10959 } elseif {$y eq $anc || !$dl($x)} {
10960 set dl($y) 0
10961 lappend anclist $y
10962 } else {
10963 set dl($y) 1
10964 lappend anclist $y
10965 incr nnh
10966 }
10967 }
10968 }
10969 }
10970 foreach x [array names growanc] {
10971 if {$dl($x)} {
10972 return 0
10973 }
10974 return 0
10975 }
10976 return 1
10977 }
10978
10979 proc validate_arctags {a} {
10980 global arctags idtags
10981
10982 set i -1
10983 set na $arctags($a)
10984 foreach id $arctags($a) {
10985 incr i
10986 if {![info exists idtags($id)]} {
10987 set na [lreplace $na $i $i]
10988 incr i -1
10989 }
10990 }
10991 set arctags($a) $na
10992 }
10993
10994 proc validate_archeads {a} {
10995 global archeads idheads
10996
10997 set i -1
10998 set na $archeads($a)
10999 foreach id $archeads($a) {
11000 incr i
11001 if {![info exists idheads($id)]} {
11002 set na [lreplace $na $i $i]
11003 incr i -1
11004 }
11005 }
11006 set archeads($a) $na
11007 }
11008
11009 # Return the list of IDs that have tags that are descendents of id,
11010 # ignoring IDs that are descendents of IDs already reported.
11011 proc desctags {id} {
11012 global arcnos arcstart arcids arctags idtags allparents
11013 global growing cached_dtags
11014
11015 if {![info exists allparents($id)]} {
11016 return {}
11017 }
11018 set t1 [clock clicks -milliseconds]
11019 set argid $id
11020 if {[llength $arcnos($id)] == 1 && [llength $allparents($id)] == 1} {
11021 # part-way along an arc; check that arc first
11022 set a [lindex $arcnos($id) 0]
11023 if {$arctags($a) ne {}} {
11024 validate_arctags $a
11025 set i [lsearch -exact $arcids($a) $id]
11026 set tid {}
11027 foreach t $arctags($a) {
11028 set j [lsearch -exact $arcids($a) $t]
11029 if {$j >= $i} break
11030 set tid $t
11031 }
11032 if {$tid ne {}} {
11033 return $tid
11034 }
11035 }
11036 set id $arcstart($a)
11037 if {[info exists idtags($id)]} {
11038 return $id
11039 }
11040 }
11041 if {[info exists cached_dtags($id)]} {
11042 return $cached_dtags($id)
11043 }
11044
11045 set origid $id
11046 set todo [list $id]
11047 set queued($id) 1
11048 set nc 1
11049 for {set i 0} {$i < [llength $todo] && $nc > 0} {incr i} {
11050 set id [lindex $todo $i]
11051 set done($id) 1
11052 set ta [info exists hastaggedancestor($id)]
11053 if {!$ta} {
11054 incr nc -1
11055 }
11056 # ignore tags on starting node
11057 if {!$ta && $i > 0} {
11058 if {[info exists idtags($id)]} {
11059 set tagloc($id) $id
11060 set ta 1
11061 } elseif {[info exists cached_dtags($id)]} {
11062 set tagloc($id) $cached_dtags($id)
11063 set ta 1
11064 }
11065 }
11066 foreach a $arcnos($id) {
11067 set d $arcstart($a)
11068 if {!$ta && $arctags($a) ne {}} {
11069 validate_arctags $a
11070 if {$arctags($a) ne {}} {
11071 lappend tagloc($id) [lindex $arctags($a) end]
11072 }
11073 }
11074 if {$ta || $arctags($a) ne {}} {
11075 set tomark [list $d]
11076 for {set j 0} {$j < [llength $tomark]} {incr j} {
11077 set dd [lindex $tomark $j]
11078 if {![info exists hastaggedancestor($dd)]} {
11079 if {[info exists done($dd)]} {
11080 foreach b $arcnos($dd) {
11081 lappend tomark $arcstart($b)
11082 }
11083 if {[info exists tagloc($dd)]} {
11084 unset tagloc($dd)
11085 }
11086 } elseif {[info exists queued($dd)]} {
11087 incr nc -1
11088 }
11089 set hastaggedancestor($dd) 1
11090 }
11091 }
11092 }
11093 if {![info exists queued($d)]} {
11094 lappend todo $d
11095 set queued($d) 1
11096 if {![info exists hastaggedancestor($d)]} {
11097 incr nc
11098 }
11099 }
11100 }
11101 }
11102 set tags {}
11103 foreach id [array names tagloc] {
11104 if {![info exists hastaggedancestor($id)]} {
11105 foreach t $tagloc($id) {
11106 if {[lsearch -exact $tags $t] < 0} {
11107 lappend tags $t
11108 }
11109 }
11110 }
11111 }
11112 set t2 [clock clicks -milliseconds]
11113 set loopix $i
11114
11115 # remove tags that are descendents of other tags
11116 for {set i 0} {$i < [llength $tags]} {incr i} {
11117 set a [lindex $tags $i]
11118 for {set j 0} {$j < $i} {incr j} {
11119 set b [lindex $tags $j]
11120 set r [anc_or_desc $a $b]
11121 if {$r == 1} {
11122 set tags [lreplace $tags $j $j]
11123 incr j -1
11124 incr i -1
11125 } elseif {$r == -1} {
11126 set tags [lreplace $tags $i $i]
11127 incr i -1
11128 break
11129 }
11130 }
11131 }
11132
11133 if {[array names growing] ne {}} {
11134 # graph isn't finished, need to check if any tag could get
11135 # eclipsed by another tag coming later. Simply ignore any
11136 # tags that could later get eclipsed.
11137 set ctags {}
11138 foreach t $tags {
11139 if {[is_certain $t $origid]} {
11140 lappend ctags $t
11141 }
11142 }
11143 if {$tags eq $ctags} {
11144 set cached_dtags($origid) $tags
11145 } else {
11146 set tags $ctags
11147 }
11148 } else {
11149 set cached_dtags($origid) $tags
11150 }
11151 set t3 [clock clicks -milliseconds]
11152 if {0 && $t3 - $t1 >= 100} {
11153 puts "iterating descendents ($loopix/[llength $todo] nodes) took\
11154 [expr {$t2-$t1}]+[expr {$t3-$t2}]ms, $nc candidates left"
11155 }
11156 return $tags
11157 }
11158
11159 proc anctags {id} {
11160 global arcnos arcids arcout arcend arctags idtags allparents
11161 global growing cached_atags
11162
11163 if {![info exists allparents($id)]} {
11164 return {}
11165 }
11166 set t1 [clock clicks -milliseconds]
11167 set argid $id
11168 if {[llength $arcnos($id)] == 1 && [llength $allparents($id)] == 1} {
11169 # part-way along an arc; check that arc first
11170 set a [lindex $arcnos($id) 0]
11171 if {$arctags($a) ne {}} {
11172 validate_arctags $a
11173 set i [lsearch -exact $arcids($a) $id]
11174 foreach t $arctags($a) {
11175 set j [lsearch -exact $arcids($a) $t]
11176 if {$j > $i} {
11177 return $t
11178 }
11179 }
11180 }
11181 if {![info exists arcend($a)]} {
11182 return {}
11183 }
11184 set id $arcend($a)
11185 if {[info exists idtags($id)]} {
11186 return $id
11187 }
11188 }
11189 if {[info exists cached_atags($id)]} {
11190 return $cached_atags($id)
11191 }
11192
11193 set origid $id
11194 set todo [list $id]
11195 set queued($id) 1
11196 set taglist {}
11197 set nc 1
11198 for {set i 0} {$i < [llength $todo] && $nc > 0} {incr i} {
11199 set id [lindex $todo $i]
11200 set done($id) 1
11201 set td [info exists hastaggeddescendent($id)]
11202 if {!$td} {
11203 incr nc -1
11204 }
11205 # ignore tags on starting node
11206 if {!$td && $i > 0} {
11207 if {[info exists idtags($id)]} {
11208 set tagloc($id) $id
11209 set td 1
11210 } elseif {[info exists cached_atags($id)]} {
11211 set tagloc($id) $cached_atags($id)
11212 set td 1
11213 }
11214 }
11215 foreach a $arcout($id) {
11216 if {!$td && $arctags($a) ne {}} {
11217 validate_arctags $a
11218 if {$arctags($a) ne {}} {
11219 lappend tagloc($id) [lindex $arctags($a) 0]
11220 }
11221 }
11222 if {![info exists arcend($a)]} continue
11223 set d $arcend($a)
11224 if {$td || $arctags($a) ne {}} {
11225 set tomark [list $d]
11226 for {set j 0} {$j < [llength $tomark]} {incr j} {
11227 set dd [lindex $tomark $j]
11228 if {![info exists hastaggeddescendent($dd)]} {
11229 if {[info exists done($dd)]} {
11230 foreach b $arcout($dd) {
11231 if {[info exists arcend($b)]} {
11232 lappend tomark $arcend($b)
11233 }
11234 }
11235 if {[info exists tagloc($dd)]} {
11236 unset tagloc($dd)
11237 }
11238 } elseif {[info exists queued($dd)]} {
11239 incr nc -1
11240 }
11241 set hastaggeddescendent($dd) 1
11242 }
11243 }
11244 }
11245 if {![info exists queued($d)]} {
11246 lappend todo $d
11247 set queued($d) 1
11248 if {![info exists hastaggeddescendent($d)]} {
11249 incr nc
11250 }
11251 }
11252 }
11253 }
11254 set t2 [clock clicks -milliseconds]
11255 set loopix $i
11256 set tags {}
11257 foreach id [array names tagloc] {
11258 if {![info exists hastaggeddescendent($id)]} {
11259 foreach t $tagloc($id) {
11260 if {[lsearch -exact $tags $t] < 0} {
11261 lappend tags $t
11262 }
11263 }
11264 }
11265 }
11266
11267 # remove tags that are ancestors of other tags
11268 for {set i 0} {$i < [llength $tags]} {incr i} {
11269 set a [lindex $tags $i]
11270 for {set j 0} {$j < $i} {incr j} {
11271 set b [lindex $tags $j]
11272 set r [anc_or_desc $a $b]
11273 if {$r == -1} {
11274 set tags [lreplace $tags $j $j]
11275 incr j -1
11276 incr i -1
11277 } elseif {$r == 1} {
11278 set tags [lreplace $tags $i $i]
11279 incr i -1
11280 break
11281 }
11282 }
11283 }
11284
11285 if {[array names growing] ne {}} {
11286 # graph isn't finished, need to check if any tag could get
11287 # eclipsed by another tag coming later. Simply ignore any
11288 # tags that could later get eclipsed.
11289 set ctags {}
11290 foreach t $tags {
11291 if {[is_certain $origid $t]} {
11292 lappend ctags $t
11293 }
11294 }
11295 if {$tags eq $ctags} {
11296 set cached_atags($origid) $tags
11297 } else {
11298 set tags $ctags
11299 }
11300 } else {
11301 set cached_atags($origid) $tags
11302 }
11303 set t3 [clock clicks -milliseconds]
11304 if {0 && $t3 - $t1 >= 100} {
11305 puts "iterating ancestors ($loopix/[llength $todo] nodes) took\
11306 [expr {$t2-$t1}]+[expr {$t3-$t2}]ms, $nc candidates left"
11307 }
11308 return $tags
11309 }
11310
11311 # Return the list of IDs that have heads that are descendents of id,
11312 # including id itself if it has a head.
11313 proc descheads {id} {
11314 global arcnos arcstart arcids archeads idheads cached_dheads
11315 global allparents arcout
11316
11317 if {![info exists allparents($id)]} {
11318 return {}
11319 }
11320 set aret {}
11321 if {![info exists arcout($id)]} {
11322 # part-way along an arc; check it first
11323 set a [lindex $arcnos($id) 0]
11324 if {$archeads($a) ne {}} {
11325 validate_archeads $a
11326 set i [lsearch -exact $arcids($a) $id]
11327 foreach t $archeads($a) {
11328 set j [lsearch -exact $arcids($a) $t]
11329 if {$j > $i} break
11330 lappend aret $t
11331 }
11332 }
11333 set id $arcstart($a)
11334 }
11335 set origid $id
11336 set todo [list $id]
11337 set seen($id) 1
11338 set ret {}
11339 for {set i 0} {$i < [llength $todo]} {incr i} {
11340 set id [lindex $todo $i]
11341 if {[info exists cached_dheads($id)]} {
11342 set ret [concat $ret $cached_dheads($id)]
11343 } else {
11344 if {[info exists idheads($id)]} {
11345 lappend ret $id
11346 }
11347 foreach a $arcnos($id) {
11348 if {$archeads($a) ne {}} {
11349 validate_archeads $a
11350 if {$archeads($a) ne {}} {
11351 set ret [concat $ret $archeads($a)]
11352 }
11353 }
11354 set d $arcstart($a)
11355 if {![info exists seen($d)]} {
11356 lappend todo $d
11357 set seen($d) 1
11358 }
11359 }
11360 }
11361 }
11362 set ret [lsort -unique $ret]
11363 set cached_dheads($origid) $ret
11364 return [concat $ret $aret]
11365 }
11366
11367 proc addedtag {id} {
11368 global arcnos arcout cached_dtags cached_atags
11369
11370 if {![info exists arcnos($id)]} return
11371 if {![info exists arcout($id)]} {
11372 recalcarc [lindex $arcnos($id) 0]
11373 }
11374 unset -nocomplain cached_dtags
11375 unset -nocomplain cached_atags
11376 }
11377
11378 proc addedhead {hid head} {
11379 global arcnos arcout cached_dheads
11380
11381 if {![info exists arcnos($hid)]} return
11382 if {![info exists arcout($hid)]} {
11383 recalcarc [lindex $arcnos($hid) 0]
11384 }
11385 unset -nocomplain cached_dheads
11386 }
11387
11388 proc removedhead {hid head} {
11389 global cached_dheads
11390
11391 unset -nocomplain cached_dheads
11392 }
11393
11394 proc movedhead {hid head} {
11395 global arcnos arcout cached_dheads
11396
11397 if {![info exists arcnos($hid)]} return
11398 if {![info exists arcout($hid)]} {
11399 recalcarc [lindex $arcnos($hid) 0]
11400 }
11401 unset -nocomplain cached_dheads
11402 }
11403
11404 proc changedrefs {} {
11405 global cached_dheads cached_dtags cached_atags cached_tagcontent
11406 global arctags archeads arcnos arcout idheads idtags
11407
11408 foreach id [concat [array names idheads] [array names idtags]] {
11409 if {[info exists arcnos($id)] && ![info exists arcout($id)]} {
11410 set a [lindex $arcnos($id) 0]
11411 if {![info exists donearc($a)]} {
11412 recalcarc $a
11413 set donearc($a) 1
11414 }
11415 }
11416 }
11417 unset -nocomplain cached_tagcontent
11418 unset -nocomplain cached_dtags
11419 unset -nocomplain cached_atags
11420 unset -nocomplain cached_dheads
11421 }
11422
11423 proc rereadrefs {} {
11424 global idtags idheads idotherrefs mainheadid
11425
11426 set refids [concat [array names idtags] \
11427 [array names idheads] [array names idotherrefs]]
11428 foreach id $refids {
11429 if {![info exists ref($id)]} {
11430 set ref($id) [listrefs $id]
11431 }
11432 }
11433 set oldmainhead $mainheadid
11434 readrefs
11435 changedrefs
11436 set refids [lsort -unique [concat $refids [array names idtags] \
11437 [array names idheads] [array names idotherrefs]]]
11438 foreach id $refids {
11439 set v [listrefs $id]
11440 if {![info exists ref($id)] || $ref($id) != $v} {
11441 redrawtags $id
11442 }
11443 }
11444 if {$oldmainhead ne $mainheadid} {
11445 redrawtags $oldmainhead
11446 redrawtags $mainheadid
11447 }
11448 run refill_reflist
11449 }
11450
11451 proc listrefs {id} {
11452 global idtags idheads idotherrefs
11453
11454 set x {}
11455 if {[info exists idtags($id)]} {
11456 set x $idtags($id)
11457 }
11458 set y {}
11459 if {[info exists idheads($id)]} {
11460 set y $idheads($id)
11461 }
11462 set z {}
11463 if {[info exists idotherrefs($id)]} {
11464 set z $idotherrefs($id)
11465 }
11466 return [list $x $y $z]
11467 }
11468
11469 proc add_tag_ctext {tag} {
11470 global ctext cached_tagcontent tagids
11471
11472 if {![info exists cached_tagcontent($tag)]} {
11473 catch {
11474 set cached_tagcontent($tag) [exec git cat-file -p $tag]
11475 }
11476 }
11477 $ctext insert end "[mc "Tag"]: $tag\n" bold
11478 if {[info exists cached_tagcontent($tag)]} {
11479 set text $cached_tagcontent($tag)
11480 } else {
11481 set text "[mc "Id"]: $tagids($tag)"
11482 }
11483 appendwithlinks $text {}
11484 }
11485
11486 proc showtag {tag isnew} {
11487 global ctext cached_tagcontent tagids linknum tagobjid
11488
11489 if {$isnew} {
11490 addtohistory [list showtag $tag 0] savectextpos
11491 }
11492 $ctext conf -state normal
11493 clear_ctext
11494 settabs 0
11495 set linknum 0
11496 add_tag_ctext $tag
11497 maybe_scroll_ctext 1
11498 $ctext conf -state disabled
11499 init_flist {}
11500 }
11501
11502 proc showtags {id isnew} {
11503 global idtags ctext linknum
11504
11505 if {$isnew} {
11506 addtohistory [list showtags $id 0] savectextpos
11507 }
11508 $ctext conf -state normal
11509 clear_ctext
11510 settabs 0
11511 set linknum 0
11512 set sep {}
11513 foreach tag $idtags($id) {
11514 $ctext insert end $sep
11515 add_tag_ctext $tag
11516 set sep "\n\n"
11517 }
11518 maybe_scroll_ctext 1
11519 $ctext conf -state disabled
11520 init_flist {}
11521 }
11522
11523 proc doquit {} {
11524 global stopped
11525 global gitktmpdir
11526
11527 set stopped 100
11528 savestuff .
11529 destroy .
11530
11531 if {[info exists gitktmpdir]} {
11532 catch {file delete -force $gitktmpdir}
11533 }
11534 }
11535
11536 proc mkfontdisp {font top which} {
11537 global fontattr fontpref $font NS use_ttk
11538
11539 set fontpref($font) [set $font]
11540 ${NS}::button $top.${font}but -text $which \
11541 -command [list choosefont $font $which]
11542 ${NS}::label $top.$font -relief flat -font $font \
11543 -text $fontattr($font,family) -justify left
11544 grid x $top.${font}but $top.$font -sticky w
11545 }
11546
11547 proc choosefont {font which} {
11548 global fontparam fontlist fonttop fontattr
11549 global prefstop NS
11550
11551 set fontparam(which) $which
11552 set fontparam(font) $font
11553 set fontparam(family) [font actual $font -family]
11554 set fontparam(size) $fontattr($font,size)
11555 set fontparam(weight) $fontattr($font,weight)
11556 set fontparam(slant) $fontattr($font,slant)
11557 set top .gitkfont
11558 set fonttop $top
11559 if {![winfo exists $top]} {
11560 font create sample
11561 eval font config sample [font actual $font]
11562 ttk_toplevel $top
11563 make_transient $top $prefstop
11564 wm title $top [mc "Gitk font chooser"]
11565 ${NS}::label $top.l -textvariable fontparam(which)
11566 pack $top.l -side top
11567 set fontlist [lsort [font families]]
11568 ${NS}::frame $top.f
11569 listbox $top.f.fam -listvariable fontlist \
11570 -yscrollcommand [list $top.f.sb set]
11571 bind $top.f.fam <<ListboxSelect>> selfontfam
11572 ${NS}::scrollbar $top.f.sb -command [list $top.f.fam yview]
11573 pack $top.f.sb -side right -fill y
11574 pack $top.f.fam -side left -fill both -expand 1
11575 pack $top.f -side top -fill both -expand 1
11576 ${NS}::frame $top.g
11577 spinbox $top.g.size -from 4 -to 40 -width 4 \
11578 -textvariable fontparam(size) \
11579 -validatecommand {string is integer -strict %s}
11580 checkbutton $top.g.bold -padx 5 \
11581 -font {{Times New Roman} 12 bold} -text [mc "B"] -indicatoron 0 \
11582 -variable fontparam(weight) -onvalue bold -offvalue normal
11583 checkbutton $top.g.ital -padx 5 \
11584 -font {{Times New Roman} 12 italic} -text [mc "I"] -indicatoron 0 \
11585 -variable fontparam(slant) -onvalue italic -offvalue roman
11586 pack $top.g.size $top.g.bold $top.g.ital -side left
11587 pack $top.g -side top
11588 canvas $top.c -width 150 -height 50 -border 2 -relief sunk \
11589 -background white
11590 $top.c create text 100 25 -anchor center -text $which -font sample \
11591 -fill black -tags text
11592 bind $top.c <Configure> [list centertext $top.c]
11593 pack $top.c -side top -fill x
11594 ${NS}::frame $top.buts
11595 ${NS}::button $top.buts.ok -text [mc "OK"] -command fontok -default active
11596 ${NS}::button $top.buts.can -text [mc "Cancel"] -command fontcan -default normal
11597 bind $top <Key-Return> fontok
11598 bind $top <Key-Escape> fontcan
11599 grid $top.buts.ok $top.buts.can
11600 grid columnconfigure $top.buts 0 -weight 1 -uniform a
11601 grid columnconfigure $top.buts 1 -weight 1 -uniform a
11602 pack $top.buts -side bottom -fill x
11603 trace add variable fontparam write chg_fontparam
11604 } else {
11605 raise $top
11606 $top.c itemconf text -text $which
11607 }
11608 set i [lsearch -exact $fontlist $fontparam(family)]
11609 if {$i >= 0} {
11610 $top.f.fam selection set $i
11611 $top.f.fam see $i
11612 }
11613 }
11614
11615 proc centertext {w} {
11616 $w coords text [expr {[winfo width $w] / 2}] [expr {[winfo height $w] / 2}]
11617 }
11618
11619 proc fontok {} {
11620 global fontparam fontpref prefstop
11621
11622 set f $fontparam(font)
11623 set fontpref($f) [list $fontparam(family) $fontparam(size)]
11624 if {$fontparam(weight) eq "bold"} {
11625 lappend fontpref($f) "bold"
11626 }
11627 if {$fontparam(slant) eq "italic"} {
11628 lappend fontpref($f) "italic"
11629 }
11630 set w $prefstop.notebook.fonts.$f
11631 $w conf -text $fontparam(family) -font $fontpref($f)
11632
11633 fontcan
11634 }
11635
11636 proc fontcan {} {
11637 global fonttop fontparam
11638
11639 if {[info exists fonttop]} {
11640 catch {destroy $fonttop}
11641 catch {font delete sample}
11642 unset fonttop
11643 unset fontparam
11644 }
11645 }
11646
11647 if {[package vsatisfies [package provide Tk] 8.6]} {
11648 # In Tk 8.6 we have a native font chooser dialog. Overwrite the above
11649 # function to make use of it.
11650 proc choosefont {font which} {
11651 tk fontchooser configure -title $which -font $font \
11652 -command [list on_choosefont $font $which]
11653 tk fontchooser show
11654 }
11655 proc on_choosefont {font which newfont} {
11656 global fontparam
11657 puts stderr "$font $newfont"
11658 array set f [font actual $newfont]
11659 set fontparam(which) $which
11660 set fontparam(font) $font
11661 set fontparam(family) $f(-family)
11662 set fontparam(size) $f(-size)
11663 set fontparam(weight) $f(-weight)
11664 set fontparam(slant) $f(-slant)
11665 fontok
11666 }
11667 }
11668
11669 proc selfontfam {} {
11670 global fonttop fontparam
11671
11672 set i [$fonttop.f.fam curselection]
11673 if {$i ne {}} {
11674 set fontparam(family) [$fonttop.f.fam get $i]
11675 }
11676 }
11677
11678 proc chg_fontparam {v sub op} {
11679 global fontparam
11680
11681 font config sample -$sub $fontparam($sub)
11682 }
11683
11684 # Create a property sheet tab page
11685 proc create_prefs_page {w} {
11686 global NS
11687 set parent [join [lrange [split $w .] 0 end-1] .]
11688 if {[winfo class $parent] eq "TNotebook"} {
11689 ${NS}::frame $w
11690 } else {
11691 ${NS}::labelframe $w
11692 }
11693 }
11694
11695 proc prefspage_general {notebook} {
11696 global NS maxwidth maxgraphpct showneartags showlocalchanges
11697 global tabstop wrapcomment wrapdefault limitdiffs
11698 global autocopy autoselect autosellen extdifftool perfile_attrs
11699 global hideremotes want_ttk have_ttk maxrefs web_browser
11700
11701 set page [create_prefs_page $notebook.general]
11702
11703 ${NS}::label $page.ldisp -text [mc "Commit list display options"] -font mainfontbold
11704 grid $page.ldisp - -sticky w -pady 10
11705 ${NS}::label $page.spacer -text " "
11706 ${NS}::label $page.maxwidthl -text [mc "Maximum graph width (lines)"]
11707 spinbox $page.maxwidth -from 0 -to 100 -width 4 -textvariable maxwidth
11708 grid $page.spacer $page.maxwidthl $page.maxwidth -sticky w
11709 #xgettext:no-tcl-format
11710 ${NS}::label $page.maxpctl -text [mc "Maximum graph width (% of pane)"]
11711 spinbox $page.maxpct -from 1 -to 100 -width 4 -textvariable maxgraphpct
11712 grid x $page.maxpctl $page.maxpct -sticky w
11713 ${NS}::checkbutton $page.showlocal -text [mc "Show local changes"] \
11714 -variable showlocalchanges
11715 grid x $page.showlocal -sticky w
11716 ${NS}::checkbutton $page.hideremotes -text [mc "Hide remote refs"] \
11717 -variable hideremotes
11718 grid x $page.hideremotes -sticky w
11719
11720 ${NS}::checkbutton $page.autocopy -text [mc "Copy commit ID to clipboard"] \
11721 -variable autocopy
11722 grid x $page.autocopy -sticky w
11723 if {[haveselectionclipboard]} {
11724 ${NS}::checkbutton $page.autoselect -text [mc "Copy commit ID to X11 selection"] \
11725 -variable autoselect
11726 grid x $page.autoselect -sticky w
11727 }
11728 spinbox $page.autosellen -from 1 -to 40 -width 4 -textvariable autosellen
11729 ${NS}::label $page.autosellenl -text [mc "Length of commit ID to copy"]
11730 grid x $page.autosellenl $page.autosellen -sticky w
11731
11732 ${NS}::label $page.ddisp -text [mc "Diff display options"] -font mainfontbold
11733 grid $page.ddisp - -sticky w -pady 10
11734 ${NS}::label $page.tabstopl -text [mc "Tab spacing"]
11735 spinbox $page.tabstop -from 1 -to 20 -width 4 -textvariable tabstop
11736 grid x $page.tabstopl $page.tabstop -sticky w
11737
11738 ${NS}::label $page.wrapcommentl -text [mc "Wrap comment text"]
11739 makedroplist $page.wrapcomment wrapcomment none char word
11740 grid x $page.wrapcommentl $page.wrapcomment -sticky w
11741
11742 ${NS}::label $page.wrapdefaultl -text [mc "Wrap other text"]
11743 makedroplist $page.wrapdefault wrapdefault none char word
11744 grid x $page.wrapdefaultl $page.wrapdefault -sticky w
11745
11746 ${NS}::checkbutton $page.ntag -text [mc "Display nearby tags/heads"] \
11747 -variable showneartags
11748 grid x $page.ntag -sticky w
11749 ${NS}::label $page.maxrefsl -text [mc "Maximum # tags/heads to show"]
11750 spinbox $page.maxrefs -from 1 -to 1000 -width 4 -textvariable maxrefs
11751 grid x $page.maxrefsl $page.maxrefs -sticky w
11752 ${NS}::checkbutton $page.ldiff -text [mc "Limit diffs to listed paths"] \
11753 -variable limitdiffs
11754 grid x $page.ldiff -sticky w
11755 ${NS}::checkbutton $page.lattr -text [mc "Support per-file encodings"] \
11756 -variable perfile_attrs
11757 grid x $page.lattr -sticky w
11758
11759 ${NS}::entry $page.extdifft -textvariable extdifftool
11760 ${NS}::frame $page.extdifff
11761 ${NS}::label $page.extdifff.l -text [mc "External diff tool" ]
11762 ${NS}::button $page.extdifff.b -text [mc "Choose..."] -command choose_extdiff
11763 pack $page.extdifff.l $page.extdifff.b -side left
11764 pack configure $page.extdifff.l -padx 10
11765 grid x $page.extdifff $page.extdifft -sticky ew
11766
11767 ${NS}::entry $page.webbrowser -textvariable web_browser
11768 ${NS}::frame $page.webbrowserf
11769 ${NS}::label $page.webbrowserf.l -text [mc "Web browser" ]
11770 pack $page.webbrowserf.l -side left
11771 pack configure $page.webbrowserf.l -padx 10
11772 grid x $page.webbrowserf $page.webbrowser -sticky ew
11773
11774 ${NS}::label $page.lgen -text [mc "General options"] -font mainfontbold
11775 grid $page.lgen - -sticky w -pady 10
11776 ${NS}::checkbutton $page.want_ttk -variable want_ttk \
11777 -text [mc "Use themed widgets"]
11778 if {$have_ttk} {
11779 ${NS}::label $page.ttk_note -text [mc "(change requires restart)"]
11780 } else {
11781 ${NS}::label $page.ttk_note -text [mc "(currently unavailable)"]
11782 }
11783 grid x $page.want_ttk $page.ttk_note -sticky w
11784 return $page
11785 }
11786
11787 proc prefspage_colors {notebook} {
11788 global NS uicolor bgcolor fgcolor ctext diffcolors selectbgcolor markbgcolor
11789 global diffbgcolors
11790
11791 set page [create_prefs_page $notebook.colors]
11792
11793 ${NS}::label $page.cdisp -text [mc "Colors: press to choose"] -font mainfontbold
11794 grid $page.cdisp - -sticky w -pady 10
11795 label $page.ui -padx 40 -relief sunk -background $uicolor
11796 ${NS}::button $page.uibut -text [mc "Interface"] \
11797 -command [list choosecolor uicolor {} $page.ui [mc "interface"] setui]
11798 grid x $page.uibut $page.ui -sticky w
11799 label $page.bg -padx 40 -relief sunk -background $bgcolor
11800 ${NS}::button $page.bgbut -text [mc "Background"] \
11801 -command [list choosecolor bgcolor {} $page.bg [mc "background"] setbg]
11802 grid x $page.bgbut $page.bg -sticky w
11803 label $page.fg -padx 40 -relief sunk -background $fgcolor
11804 ${NS}::button $page.fgbut -text [mc "Foreground"] \
11805 -command [list choosecolor fgcolor {} $page.fg [mc "foreground"] setfg]
11806 grid x $page.fgbut $page.fg -sticky w
11807 label $page.diffold -padx 40 -relief sunk -background [lindex $diffcolors 0]
11808 ${NS}::button $page.diffoldbut -text [mc "Diff: old lines"] \
11809 -command [list choosecolor diffcolors 0 $page.diffold [mc "diff old lines"] \
11810 [list $ctext tag conf d0 -foreground]]
11811 grid x $page.diffoldbut $page.diffold -sticky w
11812 label $page.diffoldbg -padx 40 -relief sunk -background [lindex $diffbgcolors 0]
11813 ${NS}::button $page.diffoldbgbut -text [mc "Diff: old lines bg"] \
11814 -command [list choosecolor diffbgcolors 0 $page.diffoldbg \
11815 [mc "diff old lines bg"] \
11816 [list $ctext tag conf d0 -background]]
11817 grid x $page.diffoldbgbut $page.diffoldbg -sticky w
11818 label $page.diffnew -padx 40 -relief sunk -background [lindex $diffcolors 1]
11819 ${NS}::button $page.diffnewbut -text [mc "Diff: new lines"] \
11820 -command [list choosecolor diffcolors 1 $page.diffnew [mc "diff new lines"] \
11821 [list $ctext tag conf dresult -foreground]]
11822 grid x $page.diffnewbut $page.diffnew -sticky w
11823 label $page.diffnewbg -padx 40 -relief sunk -background [lindex $diffbgcolors 1]
11824 ${NS}::button $page.diffnewbgbut -text [mc "Diff: new lines bg"] \
11825 -command [list choosecolor diffbgcolors 1 $page.diffnewbg \
11826 [mc "diff new lines bg"] \
11827 [list $ctext tag conf dresult -background]]
11828 grid x $page.diffnewbgbut $page.diffnewbg -sticky w
11829 label $page.hunksep -padx 40 -relief sunk -background [lindex $diffcolors 2]
11830 ${NS}::button $page.hunksepbut -text [mc "Diff: hunk header"] \
11831 -command [list choosecolor diffcolors 2 $page.hunksep \
11832 [mc "diff hunk header"] \
11833 [list $ctext tag conf hunksep -foreground]]
11834 grid x $page.hunksepbut $page.hunksep -sticky w
11835 label $page.markbgsep -padx 40 -relief sunk -background $markbgcolor
11836 ${NS}::button $page.markbgbut -text [mc "Marked line bg"] \
11837 -command [list choosecolor markbgcolor {} $page.markbgsep \
11838 [mc "marked line background"] \
11839 [list $ctext tag conf omark -background]]
11840 grid x $page.markbgbut $page.markbgsep -sticky w
11841 label $page.selbgsep -padx 40 -relief sunk -background $selectbgcolor
11842 ${NS}::button $page.selbgbut -text [mc "Select bg"] \
11843 -command [list choosecolor selectbgcolor {} $page.selbgsep [mc "background"] setselbg]
11844 grid x $page.selbgbut $page.selbgsep -sticky w
11845 return $page
11846 }
11847
11848 proc prefspage_fonts {notebook} {
11849 global NS
11850 set page [create_prefs_page $notebook.fonts]
11851 ${NS}::label $page.cfont -text [mc "Fonts: press to choose"] -font mainfontbold
11852 grid $page.cfont - -sticky w -pady 10
11853 mkfontdisp mainfont $page [mc "Main font"]
11854 mkfontdisp textfont $page [mc "Diff display font"]
11855 mkfontdisp uifont $page [mc "User interface font"]
11856 return $page
11857 }
11858
11859 proc doprefs {} {
11860 global maxwidth maxgraphpct use_ttk NS
11861 global oldprefs prefstop showneartags showlocalchanges
11862 global uicolor bgcolor fgcolor ctext diffcolors selectbgcolor markbgcolor
11863 global tabstop limitdiffs autoselect autosellen extdifftool perfile_attrs
11864 global hideremotes want_ttk have_ttk wrapcomment wrapdefault
11865
11866 set top .gitkprefs
11867 set prefstop $top
11868 if {[winfo exists $top]} {
11869 raise $top
11870 return
11871 }
11872 foreach v {maxwidth maxgraphpct showneartags showlocalchanges \
11873 limitdiffs tabstop perfile_attrs hideremotes want_ttk wrapcomment wrapdefault} {
11874 set oldprefs($v) [set $v]
11875 }
11876 ttk_toplevel $top
11877 wm title $top [mc "Gitk preferences"]
11878 make_transient $top .
11879
11880 if {[set use_notebook [expr {$use_ttk && [info command ::ttk::notebook] ne ""}]]} {
11881 set notebook [ttk::notebook $top.notebook]
11882 } else {
11883 set notebook [${NS}::frame $top.notebook -borderwidth 0 -relief flat]
11884 }
11885
11886 lappend pages [prefspage_general $notebook] [mc "General"]
11887 lappend pages [prefspage_colors $notebook] [mc "Colors"]
11888 lappend pages [prefspage_fonts $notebook] [mc "Fonts"]
11889 set col 0
11890 foreach {page title} $pages {
11891 if {$use_notebook} {
11892 $notebook add $page -text $title
11893 } else {
11894 set btn [${NS}::button $notebook.b_[string map {. X} $page] \
11895 -text $title -command [list raise $page]]
11896 $page configure -text $title
11897 grid $btn -row 0 -column [incr col] -sticky w
11898 grid $page -row 1 -column 0 -sticky news -columnspan 100
11899 }
11900 }
11901
11902 if {!$use_notebook} {
11903 grid columnconfigure $notebook 0 -weight 1
11904 grid rowconfigure $notebook 1 -weight 1
11905 raise [lindex $pages 0]
11906 }
11907
11908 grid $notebook -sticky news -padx 2 -pady 2
11909 grid rowconfigure $top 0 -weight 1
11910 grid columnconfigure $top 0 -weight 1
11911
11912 ${NS}::frame $top.buts
11913 ${NS}::button $top.buts.ok -text [mc "OK"] -command prefsok -default active
11914 ${NS}::button $top.buts.can -text [mc "Cancel"] -command prefscan -default normal
11915 bind $top <Key-Return> prefsok
11916 bind $top <Key-Escape> prefscan
11917 grid $top.buts.ok $top.buts.can
11918 grid columnconfigure $top.buts 0 -weight 1 -uniform a
11919 grid columnconfigure $top.buts 1 -weight 1 -uniform a
11920 grid $top.buts - - -pady 10 -sticky ew
11921 grid columnconfigure $top 2 -weight 1
11922 bind $top <Visibility> [list focus $top.buts.ok]
11923 }
11924
11925 proc choose_extdiff {} {
11926 global extdifftool
11927
11928 set prog [tk_getOpenFile -title [mc "External diff tool"] -multiple false]
11929 if {$prog ne {}} {
11930 set extdifftool $prog
11931 }
11932 }
11933
11934 proc choosecolor {v vi w x cmd} {
11935 global $v
11936
11937 set c [tk_chooseColor -initialcolor [lindex [set $v] $vi] \
11938 -title [mc "Gitk: choose color for %s" $x]]
11939 if {$c eq {}} return
11940 $w conf -background $c
11941 lset $v $vi $c
11942 eval $cmd $c
11943 }
11944
11945 proc setselbg {c} {
11946 global bglist cflist
11947 foreach w $bglist {
11948 if {[winfo exists $w]} {
11949 $w configure -selectbackground $c
11950 }
11951 }
11952 $cflist tag configure highlight \
11953 -background [$cflist cget -selectbackground]
11954 allcanvs itemconf secsel -fill $c
11955 }
11956
11957 # This sets the background color and the color scheme for the whole UI.
11958 # For some reason, tk_setPalette chooses a nasty dark red for selectColor
11959 # if we don't specify one ourselves, which makes the checkbuttons and
11960 # radiobuttons look bad. This chooses white for selectColor if the
11961 # background color is light, or black if it is dark.
11962 proc setui {c} {
11963 if {[tk windowingsystem] eq "win32"} { return }
11964 set bg [winfo rgb . $c]
11965 set selc black
11966 if {[lindex $bg 0] + 1.5 * [lindex $bg 1] + 0.5 * [lindex $bg 2] > 100000} {
11967 set selc white
11968 }
11969 tk_setPalette background $c selectColor $selc
11970 }
11971
11972 proc setbg {c} {
11973 global bglist
11974
11975 foreach w $bglist {
11976 if {[winfo exists $w]} {
11977 $w conf -background $c
11978 }
11979 }
11980 }
11981
11982 proc setfg {c} {
11983 global fglist canv
11984
11985 foreach w $fglist {
11986 if {[winfo exists $w]} {
11987 $w conf -foreground $c
11988 }
11989 }
11990 allcanvs itemconf text -fill $c
11991 $canv itemconf circle -outline $c
11992 $canv itemconf markid -outline $c
11993 }
11994
11995 proc prefscan {} {
11996 global oldprefs prefstop
11997
11998 foreach v {maxwidth maxgraphpct showneartags showlocalchanges \
11999 limitdiffs tabstop perfile_attrs hideremotes want_ttk wrapcomment wrapdefault} {
12000 global $v
12001 set $v $oldprefs($v)
12002 }
12003 catch {destroy $prefstop}
12004 unset prefstop
12005 fontcan
12006 }
12007
12008 proc prefsok {} {
12009 global maxwidth maxgraphpct
12010 global oldprefs prefstop showneartags showlocalchanges
12011 global fontpref mainfont textfont uifont
12012 global limitdiffs treediffs perfile_attrs
12013 global hideremotes wrapcomment wrapdefault
12014 global ctext
12015
12016 catch {destroy $prefstop}
12017 unset prefstop
12018 fontcan
12019 set fontchanged 0
12020 if {$mainfont ne $fontpref(mainfont)} {
12021 set mainfont $fontpref(mainfont)
12022 parsefont mainfont $mainfont
12023 eval font configure mainfont [fontflags mainfont]
12024 eval font configure mainfontbold [fontflags mainfont 1]
12025 setcoords
12026 set fontchanged 1
12027 }
12028 if {$textfont ne $fontpref(textfont)} {
12029 set textfont $fontpref(textfont)
12030 parsefont textfont $textfont
12031 eval font configure textfont [fontflags textfont]
12032 eval font configure textfontbold [fontflags textfont 1]
12033 }
12034 if {$uifont ne $fontpref(uifont)} {
12035 set uifont $fontpref(uifont)
12036 parsefont uifont $uifont
12037 eval font configure uifont [fontflags uifont]
12038 }
12039 settabs
12040 if {$showlocalchanges != $oldprefs(showlocalchanges)} {
12041 if {$showlocalchanges} {
12042 doshowlocalchanges
12043 } else {
12044 dohidelocalchanges
12045 }
12046 }
12047 if {$limitdiffs != $oldprefs(limitdiffs) ||
12048 ($perfile_attrs && !$oldprefs(perfile_attrs))} {
12049 # treediffs elements are limited by path;
12050 # won't have encodings cached if perfile_attrs was just turned on
12051 unset -nocomplain treediffs
12052 }
12053 if {$fontchanged || $maxwidth != $oldprefs(maxwidth)
12054 || $maxgraphpct != $oldprefs(maxgraphpct)} {
12055 redisplay
12056 } elseif {$showneartags != $oldprefs(showneartags) ||
12057 $limitdiffs != $oldprefs(limitdiffs)} {
12058 reselectline
12059 }
12060 if {$hideremotes != $oldprefs(hideremotes)} {
12061 rereadrefs
12062 }
12063 if {$wrapcomment != $oldprefs(wrapcomment)} {
12064 $ctext tag conf comment -wrap $wrapcomment
12065 }
12066 if {$wrapdefault != $oldprefs(wrapdefault)} {
12067 $ctext configure -wrap $wrapdefault
12068 }
12069 }
12070
12071 proc formatdate {d} {
12072 global datetimeformat
12073 if {$d ne {}} {
12074 # If $datetimeformat includes a timezone, display in the
12075 # timezone of the argument. Otherwise, display in local time.
12076 if {[string match {*%[zZ]*} $datetimeformat]} {
12077 if {[catch {set d [clock format [lindex $d 0] -timezone [lindex $d 1] -format $datetimeformat]}]} {
12078 # Tcl < 8.5 does not support -timezone. Emulate it by
12079 # setting TZ (e.g. TZ=<-0430>+04:30).
12080 global env
12081 if {[info exists env(TZ)]} {
12082 set savedTZ $env(TZ)
12083 }
12084 set zone [lindex $d 1]
12085 set sign [string map {+ - - +} [string index $zone 0]]
12086 set env(TZ) <$zone>$sign[string range $zone 1 2]:[string range $zone 3 4]
12087 set d [clock format [lindex $d 0] -format $datetimeformat]
12088 if {[info exists savedTZ]} {
12089 set env(TZ) $savedTZ
12090 } else {
12091 unset env(TZ)
12092 }
12093 }
12094 } else {
12095 set d [clock format [lindex $d 0] -format $datetimeformat]
12096 }
12097 }
12098 return $d
12099 }
12100
12101 # This list of encoding names and aliases is distilled from
12102 # https://www.iana.org/assignments/character-sets.
12103 # Not all of them are supported by Tcl.
12104 set encoding_aliases {
12105 { ANSI_X3.4-1968 iso-ir-6 ANSI_X3.4-1986 ISO_646.irv:1991 ASCII
12106 ISO646-US US-ASCII us IBM367 cp367 csASCII }
12107 { ISO-10646-UTF-1 csISO10646UTF1 }
12108 { ISO_646.basic:1983 ref csISO646basic1983 }
12109 { INVARIANT csINVARIANT }
12110 { ISO_646.irv:1983 iso-ir-2 irv csISO2IntlRefVersion }
12111 { BS_4730 iso-ir-4 ISO646-GB gb uk csISO4UnitedKingdom }
12112 { NATS-SEFI iso-ir-8-1 csNATSSEFI }
12113 { NATS-SEFI-ADD iso-ir-8-2 csNATSSEFIADD }
12114 { NATS-DANO iso-ir-9-1 csNATSDANO }
12115 { NATS-DANO-ADD iso-ir-9-2 csNATSDANOADD }
12116 { SEN_850200_B iso-ir-10 FI ISO646-FI ISO646-SE se csISO10Swedish }
12117 { SEN_850200_C iso-ir-11 ISO646-SE2 se2 csISO11SwedishForNames }
12118 { KS_C_5601-1987 iso-ir-149 KS_C_5601-1989 KSC_5601 korean csKSC56011987 }
12119 { ISO-2022-KR csISO2022KR }
12120 { EUC-KR csEUCKR }
12121 { ISO-2022-JP csISO2022JP }
12122 { ISO-2022-JP-2 csISO2022JP2 }
12123 { JIS_C6220-1969-jp JIS_C6220-1969 iso-ir-13 katakana x0201-7
12124 csISO13JISC6220jp }
12125 { JIS_C6220-1969-ro iso-ir-14 jp ISO646-JP csISO14JISC6220ro }
12126 { IT iso-ir-15 ISO646-IT csISO15Italian }
12127 { PT iso-ir-16 ISO646-PT csISO16Portuguese }
12128 { ES iso-ir-17 ISO646-ES csISO17Spanish }
12129 { greek7-old iso-ir-18 csISO18Greek7Old }
12130 { latin-greek iso-ir-19 csISO19LatinGreek }
12131 { DIN_66003 iso-ir-21 de ISO646-DE csISO21German }
12132 { NF_Z_62-010_(1973) iso-ir-25 ISO646-FR1 csISO25French }
12133 { Latin-greek-1 iso-ir-27 csISO27LatinGreek1 }
12134 { ISO_5427 iso-ir-37 csISO5427Cyrillic }
12135 { JIS_C6226-1978 iso-ir-42 csISO42JISC62261978 }
12136 { BS_viewdata iso-ir-47 csISO47BSViewdata }
12137 { INIS iso-ir-49 csISO49INIS }
12138 { INIS-8 iso-ir-50 csISO50INIS8 }
12139 { INIS-cyrillic iso-ir-51 csISO51INISCyrillic }
12140 { ISO_5427:1981 iso-ir-54 ISO5427Cyrillic1981 }
12141 { ISO_5428:1980 iso-ir-55 csISO5428Greek }
12142 { GB_1988-80 iso-ir-57 cn ISO646-CN csISO57GB1988 }
12143 { GB_2312-80 iso-ir-58 chinese csISO58GB231280 }
12144 { NS_4551-1 iso-ir-60 ISO646-NO no csISO60DanishNorwegian
12145 csISO60Norwegian1 }
12146 { NS_4551-2 ISO646-NO2 iso-ir-61 no2 csISO61Norwegian2 }
12147 { NF_Z_62-010 iso-ir-69 ISO646-FR fr csISO69French }
12148 { videotex-suppl iso-ir-70 csISO70VideotexSupp1 }
12149 { PT2 iso-ir-84 ISO646-PT2 csISO84Portuguese2 }
12150 { ES2 iso-ir-85 ISO646-ES2 csISO85Spanish2 }
12151 { MSZ_7795.3 iso-ir-86 ISO646-HU hu csISO86Hungarian }
12152 { JIS_C6226-1983 iso-ir-87 x0208 JIS_X0208-1983 csISO87JISX0208 }
12153 { greek7 iso-ir-88 csISO88Greek7 }
12154 { ASMO_449 ISO_9036 arabic7 iso-ir-89 csISO89ASMO449 }
12155 { iso-ir-90 csISO90 }
12156 { JIS_C6229-1984-a iso-ir-91 jp-ocr-a csISO91JISC62291984a }
12157 { JIS_C6229-1984-b iso-ir-92 ISO646-JP-OCR-B jp-ocr-b
12158 csISO92JISC62991984b }
12159 { JIS_C6229-1984-b-add iso-ir-93 jp-ocr-b-add csISO93JIS62291984badd }
12160 { JIS_C6229-1984-hand iso-ir-94 jp-ocr-hand csISO94JIS62291984hand }
12161 { JIS_C6229-1984-hand-add iso-ir-95 jp-ocr-hand-add
12162 csISO95JIS62291984handadd }
12163 { JIS_C6229-1984-kana iso-ir-96 csISO96JISC62291984kana }
12164 { ISO_2033-1983 iso-ir-98 e13b csISO2033 }
12165 { ANSI_X3.110-1983 iso-ir-99 CSA_T500-1983 NAPLPS csISO99NAPLPS }
12166 { ISO_8859-1:1987 iso-ir-100 ISO_8859-1 ISO-8859-1 latin1 l1 IBM819
12167 CP819 csISOLatin1 }
12168 { ISO_8859-2:1987 iso-ir-101 ISO_8859-2 ISO-8859-2 latin2 l2 csISOLatin2 }
12169 { T.61-7bit iso-ir-102 csISO102T617bit }
12170 { T.61-8bit T.61 iso-ir-103 csISO103T618bit }
12171 { ISO_8859-3:1988 iso-ir-109 ISO_8859-3 ISO-8859-3 latin3 l3 csISOLatin3 }
12172 { ISO_8859-4:1988 iso-ir-110 ISO_8859-4 ISO-8859-4 latin4 l4 csISOLatin4 }
12173 { ECMA-cyrillic iso-ir-111 KOI8-E csISO111ECMACyrillic }
12174 { CSA_Z243.4-1985-1 iso-ir-121 ISO646-CA csa7-1 ca csISO121Canadian1 }
12175 { CSA_Z243.4-1985-2 iso-ir-122 ISO646-CA2 csa7-2 csISO122Canadian2 }
12176 { CSA_Z243.4-1985-gr iso-ir-123 csISO123CSAZ24341985gr }
12177 { ISO_8859-6:1987 iso-ir-127 ISO_8859-6 ISO-8859-6 ECMA-114 ASMO-708
12178 arabic csISOLatinArabic }
12179 { ISO_8859-6-E csISO88596E ISO-8859-6-E }
12180 { ISO_8859-6-I csISO88596I ISO-8859-6-I }
12181 { ISO_8859-7:1987 iso-ir-126 ISO_8859-7 ISO-8859-7 ELOT_928 ECMA-118
12182 greek greek8 csISOLatinGreek }
12183 { T.101-G2 iso-ir-128 csISO128T101G2 }
12184 { ISO_8859-8:1988 iso-ir-138 ISO_8859-8 ISO-8859-8 hebrew
12185 csISOLatinHebrew }
12186 { ISO_8859-8-E csISO88598E ISO-8859-8-E }
12187 { ISO_8859-8-I csISO88598I ISO-8859-8-I }
12188 { CSN_369103 iso-ir-139 csISO139CSN369103 }
12189 { JUS_I.B1.002 iso-ir-141 ISO646-YU js yu csISO141JUSIB1002 }
12190 { ISO_6937-2-add iso-ir-142 csISOTextComm }
12191 { IEC_P27-1 iso-ir-143 csISO143IECP271 }
12192 { ISO_8859-5:1988 iso-ir-144 ISO_8859-5 ISO-8859-5 cyrillic
12193 csISOLatinCyrillic }
12194 { JUS_I.B1.003-serb iso-ir-146 serbian csISO146Serbian }
12195 { JUS_I.B1.003-mac macedonian iso-ir-147 csISO147Macedonian }
12196 { ISO_8859-9:1989 iso-ir-148 ISO_8859-9 ISO-8859-9 latin5 l5 csISOLatin5 }
12197 { greek-ccitt iso-ir-150 csISO150 csISO150GreekCCITT }
12198 { NC_NC00-10:81 cuba iso-ir-151 ISO646-CU csISO151Cuba }
12199 { ISO_6937-2-25 iso-ir-152 csISO6937Add }
12200 { GOST_19768-74 ST_SEV_358-88 iso-ir-153 csISO153GOST1976874 }
12201 { ISO_8859-supp iso-ir-154 latin1-2-5 csISO8859Supp }
12202 { ISO_10367-box iso-ir-155 csISO10367Box }
12203 { ISO-8859-10 iso-ir-157 l6 ISO_8859-10:1992 csISOLatin6 latin6 }
12204 { latin-lap lap iso-ir-158 csISO158Lap }
12205 { JIS_X0212-1990 x0212 iso-ir-159 csISO159JISX02121990 }
12206 { DS_2089 DS2089 ISO646-DK dk csISO646Danish }
12207 { us-dk csUSDK }
12208 { dk-us csDKUS }
12209 { JIS_X0201 X0201 csHalfWidthKatakana }
12210 { KSC5636 ISO646-KR csKSC5636 }
12211 { ISO-10646-UCS-2 csUnicode }
12212 { ISO-10646-UCS-4 csUCS4 }
12213 { DEC-MCS dec csDECMCS }
12214 { hp-roman8 roman8 r8 csHPRoman8 }
12215 { macintosh mac csMacintosh }
12216 { IBM037 cp037 ebcdic-cp-us ebcdic-cp-ca ebcdic-cp-wt ebcdic-cp-nl
12217 csIBM037 }
12218 { IBM038 EBCDIC-INT cp038 csIBM038 }
12219 { IBM273 CP273 csIBM273 }
12220 { IBM274 EBCDIC-BE CP274 csIBM274 }
12221 { IBM275 EBCDIC-BR cp275 csIBM275 }
12222 { IBM277 EBCDIC-CP-DK EBCDIC-CP-NO csIBM277 }
12223 { IBM278 CP278 ebcdic-cp-fi ebcdic-cp-se csIBM278 }
12224 { IBM280 CP280 ebcdic-cp-it csIBM280 }
12225 { IBM281 EBCDIC-JP-E cp281 csIBM281 }
12226 { IBM284 CP284 ebcdic-cp-es csIBM284 }
12227 { IBM285 CP285 ebcdic-cp-gb csIBM285 }
12228 { IBM290 cp290 EBCDIC-JP-kana csIBM290 }
12229 { IBM297 cp297 ebcdic-cp-fr csIBM297 }
12230 { IBM420 cp420 ebcdic-cp-ar1 csIBM420 }
12231 { IBM423 cp423 ebcdic-cp-gr csIBM423 }
12232 { IBM424 cp424 ebcdic-cp-he csIBM424 }
12233 { IBM437 cp437 437 csPC8CodePage437 }
12234 { IBM500 CP500 ebcdic-cp-be ebcdic-cp-ch csIBM500 }
12235 { IBM775 cp775 csPC775Baltic }
12236 { IBM850 cp850 850 csPC850Multilingual }
12237 { IBM851 cp851 851 csIBM851 }
12238 { IBM852 cp852 852 csPCp852 }
12239 { IBM855 cp855 855 csIBM855 }
12240 { IBM857 cp857 857 csIBM857 }
12241 { IBM860 cp860 860 csIBM860 }
12242 { IBM861 cp861 861 cp-is csIBM861 }
12243 { IBM862 cp862 862 csPC862LatinHebrew }
12244 { IBM863 cp863 863 csIBM863 }
12245 { IBM864 cp864 csIBM864 }
12246 { IBM865 cp865 865 csIBM865 }
12247 { IBM866 cp866 866 csIBM866 }
12248 { IBM868 CP868 cp-ar csIBM868 }
12249 { IBM869 cp869 869 cp-gr csIBM869 }
12250 { IBM870 CP870 ebcdic-cp-roece ebcdic-cp-yu csIBM870 }
12251 { IBM871 CP871 ebcdic-cp-is csIBM871 }
12252 { IBM880 cp880 EBCDIC-Cyrillic csIBM880 }
12253 { IBM891 cp891 csIBM891 }
12254 { IBM903 cp903 csIBM903 }
12255 { IBM904 cp904 904 csIBBM904 }
12256 { IBM905 CP905 ebcdic-cp-tr csIBM905 }
12257 { IBM918 CP918 ebcdic-cp-ar2 csIBM918 }
12258 { IBM1026 CP1026 csIBM1026 }
12259 { EBCDIC-AT-DE csIBMEBCDICATDE }
12260 { EBCDIC-AT-DE-A csEBCDICATDEA }
12261 { EBCDIC-CA-FR csEBCDICCAFR }
12262 { EBCDIC-DK-NO csEBCDICDKNO }
12263 { EBCDIC-DK-NO-A csEBCDICDKNOA }
12264 { EBCDIC-FI-SE csEBCDICFISE }
12265 { EBCDIC-FI-SE-A csEBCDICFISEA }
12266 { EBCDIC-FR csEBCDICFR }
12267 { EBCDIC-IT csEBCDICIT }
12268 { EBCDIC-PT csEBCDICPT }
12269 { EBCDIC-ES csEBCDICES }
12270 { EBCDIC-ES-A csEBCDICESA }
12271 { EBCDIC-ES-S csEBCDICESS }
12272 { EBCDIC-UK csEBCDICUK }
12273 { EBCDIC-US csEBCDICUS }
12274 { UNKNOWN-8BIT csUnknown8BiT }
12275 { MNEMONIC csMnemonic }
12276 { MNEM csMnem }
12277 { VISCII csVISCII }
12278 { VIQR csVIQR }
12279 { KOI8-R csKOI8R }
12280 { IBM00858 CCSID00858 CP00858 PC-Multilingual-850+euro }
12281 { IBM00924 CCSID00924 CP00924 ebcdic-Latin9--euro }
12282 { IBM01140 CCSID01140 CP01140 ebcdic-us-37+euro }
12283 { IBM01141 CCSID01141 CP01141 ebcdic-de-273+euro }
12284 { IBM01142 CCSID01142 CP01142 ebcdic-dk-277+euro ebcdic-no-277+euro }
12285 { IBM01143 CCSID01143 CP01143 ebcdic-fi-278+euro ebcdic-se-278+euro }
12286 { IBM01144 CCSID01144 CP01144 ebcdic-it-280+euro }
12287 { IBM01145 CCSID01145 CP01145 ebcdic-es-284+euro }
12288 { IBM01146 CCSID01146 CP01146 ebcdic-gb-285+euro }
12289 { IBM01147 CCSID01147 CP01147 ebcdic-fr-297+euro }
12290 { IBM01148 CCSID01148 CP01148 ebcdic-international-500+euro }
12291 { IBM01149 CCSID01149 CP01149 ebcdic-is-871+euro }
12292 { IBM1047 IBM-1047 }
12293 { PTCP154 csPTCP154 PT154 CP154 Cyrillic-Asian }
12294 { Amiga-1251 Ami1251 Amiga1251 Ami-1251 }
12295 { UNICODE-1-1 csUnicode11 }
12296 { CESU-8 csCESU-8 }
12297 { BOCU-1 csBOCU-1 }
12298 { UNICODE-1-1-UTF-7 csUnicode11UTF7 }
12299 { ISO-8859-14 iso-ir-199 ISO_8859-14:1998 ISO_8859-14 latin8 iso-celtic
12300 l8 }
12301 { ISO-8859-15 ISO_8859-15 Latin-9 }
12302 { ISO-8859-16 iso-ir-226 ISO_8859-16:2001 ISO_8859-16 latin10 l10 }
12303 { GBK CP936 MS936 windows-936 }
12304 { JIS_Encoding csJISEncoding }
12305 { Shift_JIS MS_Kanji csShiftJIS ShiftJIS Shift-JIS }
12306 { Extended_UNIX_Code_Packed_Format_for_Japanese csEUCPkdFmtJapanese
12307 EUC-JP }
12308 { Extended_UNIX_Code_Fixed_Width_for_Japanese csEUCFixWidJapanese }
12309 { ISO-10646-UCS-Basic csUnicodeASCII }
12310 { ISO-10646-Unicode-Latin1 csUnicodeLatin1 ISO-10646 }
12311 { ISO-Unicode-IBM-1261 csUnicodeIBM1261 }
12312 { ISO-Unicode-IBM-1268 csUnicodeIBM1268 }
12313 { ISO-Unicode-IBM-1276 csUnicodeIBM1276 }
12314 { ISO-Unicode-IBM-1264 csUnicodeIBM1264 }
12315 { ISO-Unicode-IBM-1265 csUnicodeIBM1265 }
12316 { ISO-8859-1-Windows-3.0-Latin-1 csWindows30Latin1 }
12317 { ISO-8859-1-Windows-3.1-Latin-1 csWindows31Latin1 }
12318 { ISO-8859-2-Windows-Latin-2 csWindows31Latin2 }
12319 { ISO-8859-9-Windows-Latin-5 csWindows31Latin5 }
12320 { Adobe-Standard-Encoding csAdobeStandardEncoding }
12321 { Ventura-US csVenturaUS }
12322 { Ventura-International csVenturaInternational }
12323 { PC8-Danish-Norwegian csPC8DanishNorwegian }
12324 { PC8-Turkish csPC8Turkish }
12325 { IBM-Symbols csIBMSymbols }
12326 { IBM-Thai csIBMThai }
12327 { HP-Legal csHPLegal }
12328 { HP-Pi-font csHPPiFont }
12329 { HP-Math8 csHPMath8 }
12330 { Adobe-Symbol-Encoding csHPPSMath }
12331 { HP-DeskTop csHPDesktop }
12332 { Ventura-Math csVenturaMath }
12333 { Microsoft-Publishing csMicrosoftPublishing }
12334 { Windows-31J csWindows31J }
12335 { GB2312 csGB2312 }
12336 { Big5 csBig5 }
12337 }
12338
12339 proc tcl_encoding {enc} {
12340 global encoding_aliases tcl_encoding_cache
12341 if {[info exists tcl_encoding_cache($enc)]} {
12342 return $tcl_encoding_cache($enc)
12343 }
12344 set names [encoding names]
12345 set lcnames [string tolower $names]
12346 set enc [string tolower $enc]
12347 set i [lsearch -exact $lcnames $enc]
12348 if {$i < 0} {
12349 # look for "isonnn" instead of "iso-nnn" or "iso_nnn"
12350 if {[regsub {^(iso|cp|ibm|jis)[-_]} $enc {\1} encx]} {
12351 set i [lsearch -exact $lcnames $encx]
12352 }
12353 }
12354 if {$i < 0} {
12355 foreach l $encoding_aliases {
12356 set ll [string tolower $l]
12357 if {[lsearch -exact $ll $enc] < 0} continue
12358 # look through the aliases for one that tcl knows about
12359 foreach e $ll {
12360 set i [lsearch -exact $lcnames $e]
12361 if {$i < 0} {
12362 if {[regsub {^(iso|cp|ibm|jis)[-_]} $e {\1} ex]} {
12363 set i [lsearch -exact $lcnames $ex]
12364 }
12365 }
12366 if {$i >= 0} break
12367 }
12368 break
12369 }
12370 }
12371 set tclenc {}
12372 if {$i >= 0} {
12373 set tclenc [lindex $names $i]
12374 }
12375 set tcl_encoding_cache($enc) $tclenc
12376 return $tclenc
12377 }
12378
12379 proc gitattr {path attr default} {
12380 global path_attr_cache
12381 if {[info exists path_attr_cache($attr,$path)]} {
12382 set r $path_attr_cache($attr,$path)
12383 } else {
12384 set r "unspecified"
12385 if {![catch {set line [exec git check-attr $attr -- $path]}]} {
12386 regexp "(.*): $attr: (.*)" $line m f r
12387 }
12388 set path_attr_cache($attr,$path) $r
12389 }
12390 if {$r eq "unspecified"} {
12391 return $default
12392 }
12393 return $r
12394 }
12395
12396 proc cache_gitattr {attr pathlist} {
12397 global path_attr_cache
12398 set newlist {}
12399 foreach path $pathlist {
12400 if {![info exists path_attr_cache($attr,$path)]} {
12401 lappend newlist $path
12402 }
12403 }
12404 set lim 1000
12405 if {[tk windowingsystem] == "win32"} {
12406 # windows has a 32k limit on the arguments to a command...
12407 set lim 30
12408 }
12409 while {$newlist ne {}} {
12410 set head [lrange $newlist 0 [expr {$lim - 1}]]
12411 set newlist [lrange $newlist $lim end]
12412 if {![catch {set rlist [eval exec git check-attr $attr -- $head]}]} {
12413 foreach row [split $rlist "\n"] {
12414 if {[regexp "(.*): $attr: (.*)" $row m path value]} {
12415 if {[string index $path 0] eq "\""} {
12416 set path [encoding convertfrom utf-8 [lindex $path 0]]
12417 }
12418 set path_attr_cache($attr,$path) $value
12419 }
12420 }
12421 }
12422 }
12423 }
12424
12425 proc get_path_encoding {path} {
12426 global gui_encoding perfile_attrs
12427 set tcl_enc $gui_encoding
12428 if {$path ne {} && $perfile_attrs} {
12429 set enc2 [tcl_encoding [gitattr $path encoding $tcl_enc]]
12430 if {$enc2 ne {}} {
12431 set tcl_enc $enc2
12432 }
12433 }
12434 return $tcl_enc
12435 }
12436
12437 ## For msgcat loading, first locate the installation location.
12438 if { [info exists ::env(GITK_MSGSDIR)] } {
12439 ## Msgsdir was manually set in the environment.
12440 set gitk_msgsdir $::env(GITK_MSGSDIR)
12441 } else {
12442 ## Let's guess the prefix from argv0.
12443 set gitk_prefix [file dirname [file dirname [file normalize $argv0]]]
12444 set gitk_libdir [file join $gitk_prefix share gitk lib]
12445 set gitk_msgsdir [file join $gitk_libdir msgs]
12446 }
12447
12448 ## Internationalization (i18n) through msgcat and gettext. See
12449 ## http://www.gnu.org/software/gettext/manual/html_node/Tcl.html
12450 package require msgcat
12451 namespace import ::msgcat::mc
12452 ## And eventually load the actual message catalog
12453 ::msgcat::mcload $gitk_msgsdir
12454
12455 # First check that Tcl/Tk is recent enough
12456 if {[catch {package require Tk 8.4} err]} {
12457 show_error {} . [mc "Sorry, gitk cannot run with this version of Tcl/Tk.\n\
12458 Gitk requires at least Tcl/Tk 8.4."]
12459 exit 1
12460 }
12461
12462 # on OSX bring the current Wish process window to front
12463 if {[tk windowingsystem] eq "aqua"} {
12464 exec osascript -e [format {
12465 tell application "System Events"
12466 set frontmost of processes whose unix id is %d to true
12467 end tell
12468 } [pid] ]
12469 }
12470
12471 # Unset GIT_TRACE var if set
12472 if { [info exists ::env(GIT_TRACE)] } {
12473 unset ::env(GIT_TRACE)
12474 }
12475
12476 # defaults...
12477 set wrcomcmd "git diff-tree --stdin -p --pretty=email"
12478
12479 set gitencoding {}
12480 catch {
12481 set gitencoding [exec git config --get i18n.commitencoding]
12482 }
12483 catch {
12484 set gitencoding [exec git config --get i18n.logoutputencoding]
12485 }
12486 if {$gitencoding == ""} {
12487 set gitencoding "utf-8"
12488 }
12489 set tclencoding [tcl_encoding $gitencoding]
12490 if {$tclencoding == {}} {
12491 puts stderr "Warning: encoding $gitencoding is not supported by Tcl/Tk"
12492 }
12493
12494 set gui_encoding [encoding system]
12495 catch {
12496 set enc [exec git config --get gui.encoding]
12497 if {$enc ne {}} {
12498 set tclenc [tcl_encoding $enc]
12499 if {$tclenc ne {}} {
12500 set gui_encoding $tclenc
12501 } else {
12502 puts stderr "Warning: encoding $enc is not supported by Tcl/Tk"
12503 }
12504 }
12505 }
12506
12507 set log_showroot true
12508 catch {
12509 set log_showroot [exec git config --bool --get log.showroot]
12510 }
12511
12512 if {[tk windowingsystem] eq "aqua"} {
12513 set mainfont {{Lucida Grande} 9}
12514 set textfont {Monaco 9}
12515 set uifont {{Lucida Grande} 9 bold}
12516 } elseif {![catch {::tk::pkgconfig get fontsystem} xft] && $xft eq "xft"} {
12517 # fontconfig!
12518 set mainfont {sans 9}
12519 set textfont {monospace 9}
12520 set uifont {sans 9 bold}
12521 } else {
12522 set mainfont {Helvetica 9}
12523 set textfont {Courier 9}
12524 set uifont {Helvetica 9 bold}
12525 }
12526 set tabstop 8
12527 set findmergefiles 0
12528 set maxgraphpct 50
12529 set maxwidth 16
12530 set revlistorder 0
12531 set fastdate 0
12532 set uparrowlen 5
12533 set downarrowlen 5
12534 set mingaplen 100
12535 set cmitmode "patch"
12536 set wrapcomment "none"
12537 set wrapdefault "none"
12538 set showneartags 1
12539 set hideremotes 0
12540 set maxrefs 20
12541 set visiblerefs {"master"}
12542 set maxlinelen 200
12543 set showlocalchanges 1
12544 set limitdiffs 1
12545 set datetimeformat "%Y-%m-%d %H:%M:%S"
12546 set autocopy 0
12547 set autoselect 1
12548 set autosellen 40
12549 set perfile_attrs 0
12550 set want_ttk 1
12551
12552 if {[tk windowingsystem] eq "aqua"} {
12553 set extdifftool "opendiff"
12554 } else {
12555 set extdifftool "meld"
12556 }
12557
12558 set colors {"#00ff00" red blue magenta darkgrey brown orange}
12559 if {[tk windowingsystem] eq "win32"} {
12560 set uicolor SystemButtonFace
12561 set uifgcolor SystemButtonText
12562 set uifgdisabledcolor SystemDisabledText
12563 set bgcolor SystemWindow
12564 set fgcolor SystemWindowText
12565 set selectbgcolor SystemHighlight
12566 set web_browser "cmd /c start"
12567 } else {
12568 set uicolor grey85
12569 set uifgcolor black
12570 set uifgdisabledcolor "#999"
12571 set bgcolor white
12572 set fgcolor black
12573 set selectbgcolor gray85
12574 if {[tk windowingsystem] eq "aqua"} {
12575 set web_browser "open"
12576 } else {
12577 set web_browser "xdg-open"
12578 }
12579 }
12580 set diffcolors {"#c30000" "#009800" blue}
12581 set diffbgcolors {"#fff3f3" "#f0fff0"}
12582 set diffcontext 3
12583 set mergecolors {red blue "#00ff00" purple brown "#009090" magenta "#808000" "#009000" "#ff0080" cyan "#b07070" "#70b0f0" "#70f0b0" "#f0b070" "#ff70b0"}
12584 set ignorespace 0
12585 set worddiff ""
12586 set markbgcolor "#e0e0ff"
12587
12588 set headbgcolor "#00ff00"
12589 set headfgcolor black
12590 set headoutlinecolor black
12591 set remotebgcolor #ffddaa
12592 set tagbgcolor yellow
12593 set tagfgcolor black
12594 set tagoutlinecolor black
12595 set reflinecolor black
12596 set filesepbgcolor #aaaaaa
12597 set filesepfgcolor black
12598 set linehoverbgcolor #ffff80
12599 set linehoverfgcolor black
12600 set linehoveroutlinecolor black
12601 set mainheadcirclecolor yellow
12602 set workingfilescirclecolor red
12603 set indexcirclecolor "#00ff00"
12604 set circlecolors {white blue gray blue blue}
12605 set linkfgcolor blue
12606 set circleoutlinecolor $fgcolor
12607 set foundbgcolor yellow
12608 set currentsearchhitbgcolor orange
12609
12610 # button for popping up context menus
12611 if {[tk windowingsystem] eq "aqua"} {
12612 set ctxbut <Button-2>
12613 } else {
12614 set ctxbut <Button-3>
12615 }
12616
12617 catch {
12618 # follow the XDG base directory specification by default. See
12619 # https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
12620 if {[info exists env(XDG_CONFIG_HOME)] && $env(XDG_CONFIG_HOME) ne ""} {
12621 # XDG_CONFIG_HOME environment variable is set
12622 set config_file [file join $env(XDG_CONFIG_HOME) git gitk]
12623 set config_file_tmp [file join $env(XDG_CONFIG_HOME) git gitk-tmp]
12624 } else {
12625 # default XDG_CONFIG_HOME
12626 set config_file "~/.config/git/gitk"
12627 set config_file_tmp "~/.config/git/gitk-tmp"
12628 }
12629 if {![file exists $config_file]} {
12630 # for backward compatibility use the old config file if it exists
12631 if {[file exists "~/.gitk"]} {
12632 set config_file "~/.gitk"
12633 set config_file_tmp "~/.gitk-tmp"
12634 } elseif {![file exists [file dirname $config_file]]} {
12635 file mkdir [file dirname $config_file]
12636 }
12637 }
12638 source $config_file
12639 }
12640 config_check_tmp_exists 50
12641
12642 set config_variables {
12643 mainfont textfont uifont tabstop findmergefiles maxgraphpct maxwidth
12644 cmitmode wrapcomment wrapdefault autocopy autoselect autosellen
12645 showneartags maxrefs visiblerefs
12646 hideremotes showlocalchanges datetimeformat limitdiffs uicolor want_ttk
12647 bgcolor fgcolor uifgcolor uifgdisabledcolor colors diffcolors mergecolors
12648 markbgcolor diffcontext selectbgcolor foundbgcolor currentsearchhitbgcolor
12649 extdifftool perfile_attrs headbgcolor headfgcolor headoutlinecolor
12650 remotebgcolor tagbgcolor tagfgcolor tagoutlinecolor reflinecolor
12651 filesepbgcolor filesepfgcolor linehoverbgcolor linehoverfgcolor
12652 linehoveroutlinecolor mainheadcirclecolor workingfilescirclecolor
12653 indexcirclecolor circlecolors linkfgcolor circleoutlinecolor diffbgcolors
12654 web_browser
12655 }
12656 foreach var $config_variables {
12657 config_init_trace $var
12658 trace add variable $var write config_variable_change_cb
12659 }
12660
12661 parsefont mainfont $mainfont
12662 eval font create mainfont [fontflags mainfont]
12663 eval font create mainfontbold [fontflags mainfont 1]
12664
12665 parsefont textfont $textfont
12666 eval font create textfont [fontflags textfont]
12667 eval font create textfontbold [fontflags textfont 1]
12668
12669 parsefont uifont $uifont
12670 eval font create uifont [fontflags uifont]
12671
12672 setui $uicolor
12673
12674 setoptions
12675
12676 # check that we can find a .git directory somewhere...
12677 if {[catch {set gitdir [exec git rev-parse --git-dir]}]} {
12678 show_error {} . [mc "Cannot find a git repository here."]
12679 exit 1
12680 }
12681
12682 set selecthead {}
12683 set selectheadid {}
12684
12685 set revtreeargs {}
12686 set cmdline_files {}
12687 set i 0
12688 set revtreeargscmd {}
12689 foreach arg $argv {
12690 switch -glob -- $arg {
12691 "" { }
12692 "--" {
12693 set cmdline_files [lrange $argv [expr {$i + 1}] end]
12694 break
12695 }
12696 "--select-commit=*" {
12697 set selecthead [string range $arg 16 end]
12698 }
12699 "--argscmd=*" {
12700 set revtreeargscmd [string range $arg 10 end]
12701 }
12702 default {
12703 lappend revtreeargs $arg
12704 }
12705 }
12706 incr i
12707 }
12708
12709 if {$selecthead eq "HEAD"} {
12710 set selecthead {}
12711 }
12712
12713 if {$i >= [llength $argv] && $revtreeargs ne {}} {
12714 # no -- on command line, but some arguments (other than --argscmd)
12715 if {[catch {
12716 set f [eval exec git rev-parse --no-revs --no-flags $revtreeargs]
12717 set cmdline_files [split $f "\n"]
12718 set n [llength $cmdline_files]
12719 set revtreeargs [lrange $revtreeargs 0 end-$n]
12720 # Unfortunately git rev-parse doesn't produce an error when
12721 # something is both a revision and a filename. To be consistent
12722 # with git log and git rev-list, check revtreeargs for filenames.
12723 foreach arg $revtreeargs {
12724 if {[file exists $arg]} {
12725 show_error {} . [mc "Ambiguous argument '%s': both revision\
12726 and filename" $arg]
12727 exit 1
12728 }
12729 }
12730 } err]} {
12731 # unfortunately we get both stdout and stderr in $err,
12732 # so look for "fatal:".
12733 set i [string first "fatal:" $err]
12734 if {$i > 0} {
12735 set err [string range $err [expr {$i + 6}] end]
12736 }
12737 show_error {} . "[mc "Bad arguments to gitk:"]\n$err"
12738 exit 1
12739 }
12740 }
12741
12742 set nullid "0000000000000000000000000000000000000000"
12743 set nullid2 "0000000000000000000000000000000000000001"
12744 set nullfile "/dev/null"
12745
12746 set have_tk85 [expr {[package vcompare $tk_version "8.5"] >= 0}]
12747 set have_tk86 [expr {[package vcompare $tk_version "8.6"] >= 0}]
12748 if {![info exists have_ttk]} {
12749 set have_ttk [llength [info commands ::ttk::style]]
12750 }
12751 set use_ttk [expr {$have_ttk && $want_ttk}]
12752 set NS [expr {$use_ttk ? "ttk" : ""}]
12753
12754 if {$use_ttk} {
12755 setttkstyle
12756 }
12757
12758 regexp {^git version ([\d.]*\d)} [exec git version] _ git_version
12759
12760 set show_notes {}
12761 if {[package vcompare $git_version "1.6.6.2"] >= 0} {
12762 set show_notes "--show-notes"
12763 }
12764
12765 set appname "gitk"
12766
12767 set runq {}
12768 set history {}
12769 set historyindex 0
12770 set fh_serial 0
12771 set nhl_names {}
12772 set highlight_paths {}
12773 set findpattern {}
12774 set searchdirn -forwards
12775 set boldids {}
12776 set boldnameids {}
12777 set diffelide {0 0}
12778 set markingmatches 0
12779 set linkentercount 0
12780 set need_redisplay 0
12781 set nrows_drawn 0
12782 set firsttabstop 0
12783
12784 set nextviewnum 1
12785 set curview 0
12786 set selectedview 0
12787 set selectedhlview [mc "None"]
12788 set highlight_related [mc "None"]
12789 set highlight_files {}
12790 set viewfiles(0) {}
12791 set viewperm(0) 0
12792 set viewchanged(0) 0
12793 set viewargs(0) {}
12794 set viewargscmd(0) {}
12795
12796 set selectedline {}
12797 set numcommits 0
12798 set loginstance 0
12799 set cmdlineok 0
12800 set stopped 0
12801 set stuffsaved 0
12802 set patchnum 0
12803 set lserial 0
12804 set hasworktree [hasworktree]
12805 set cdup {}
12806 if {[expr {[exec git rev-parse --is-inside-work-tree] == "true"}]} {
12807 set cdup [exec git rev-parse --show-cdup]
12808 }
12809 set worktree [gitworktree]
12810 setcoords
12811 makewindow
12812 if {$::tcl_platform(platform) eq {windows} && [file exists $gitk_prefix/etc/git.ico]} {
12813 wm iconbitmap . -default $gitk_prefix/etc/git.ico
12814 } else {
12815 catch {
12816 image create photo gitlogo -width 16 -height 16
12817
12818 image create photo gitlogominus -width 4 -height 2
12819 gitlogominus put #C00000 -to 0 0 4 2
12820 gitlogo copy gitlogominus -to 1 5
12821 gitlogo copy gitlogominus -to 6 5
12822 gitlogo copy gitlogominus -to 11 5
12823 image delete gitlogominus
12824
12825 image create photo gitlogoplus -width 4 -height 4
12826 gitlogoplus put #008000 -to 1 0 3 4
12827 gitlogoplus put #008000 -to 0 1 4 3
12828 gitlogo copy gitlogoplus -to 1 9
12829 gitlogo copy gitlogoplus -to 6 9
12830 gitlogo copy gitlogoplus -to 11 9
12831 image delete gitlogoplus
12832
12833 image create photo gitlogo32 -width 32 -height 32
12834 gitlogo32 copy gitlogo -zoom 2 2
12835
12836 wm iconphoto . -default gitlogo gitlogo32
12837 }
12838 }
12839 # wait for the window to become visible
12840 if {![winfo viewable .]} {tkwait visibility .}
12841 set_window_title
12842 update
12843 readrefs
12844
12845 if {$cmdline_files ne {} || $revtreeargs ne {} || $revtreeargscmd ne {}} {
12846 # create a view for the files/dirs specified on the command line
12847 set curview 1
12848 set selectedview 1
12849 set nextviewnum 2
12850 set viewname(1) [mc "Command line"]
12851 set viewfiles(1) $cmdline_files
12852 set viewargs(1) $revtreeargs
12853 set viewargscmd(1) $revtreeargscmd
12854 set viewperm(1) 0
12855 set viewchanged(1) 0
12856 set vdatemode(1) 0
12857 addviewmenu 1
12858 .bar.view entryconf [mca "&Edit view..."] -state normal
12859 .bar.view entryconf [mca "&Delete view"] -state normal
12860 }
12861
12862 if {[info exists permviews]} {
12863 foreach v $permviews {
12864 set n $nextviewnum
12865 incr nextviewnum
12866 set viewname($n) [lindex $v 0]
12867 set viewfiles($n) [lindex $v 1]
12868 set viewargs($n) [lindex $v 2]
12869 set viewargscmd($n) [lindex $v 3]
12870 set viewperm($n) 1
12871 set viewchanged($n) 0
12872 addviewmenu $n
12873 }
12874 }
12875
12876 if {[tk windowingsystem] eq "win32"} {
12877 focus -force .
12878 }
12879
12880 getcommits {}
12881
12882 # Local variables:
12883 # mode: tcl
12884 # indent-tabs-mode: t
12885 # tab-width: 8
12886 # End: