Slightly older version of
master from https://github.com/j6t/gitk
1#!/bin/sh
2# Tcl ignores the next line -*- tcl -*- \
3exec 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
10if {[catch {package require Tcl 8.6-8.8} err]} {
11 catch {wm withdraw .}
12 tk_messageBox \
13 -icon error \
14 -type ok \
15 -title "gitk: fatal error" \
16 -message $err
17 exit 1
18}
19
20set MIN_GIT_VERSION 2.20
21regexp {^git version ([\d.]*\d)} [exec git version] _ git_version
22if {[package vcompare $git_version $MIN_GIT_VERSION] < 0} {
23 set message "The git executable found is too old.
24The minimum required version is $MIN_GIT_VERSION.0.
25The version of git found is $git_version."
26
27 catch {wm withdraw .}
28 tk_messageBox \
29 -icon error \
30 -type ok \
31 -title "gitk: fatal error" \
32 -message $message
33 exit 1
34}
35
36######################################################################
37##
38## Enabling platform-specific code paths
39
40proc is_Windows {} {
41 if {$::tcl_platform(platform) eq {windows}} {
42 return 1
43 }
44 return 0
45}
46
47######################################################################
48##
49## PATH lookup
50
51if {[is_Windows]} {
52 set _search_path {}
53 proc _which {what args} {
54 global env _search_path
55
56 if {$_search_path eq {}} {
57 set gitguidir [file dirname [info script]]
58 regsub -all ";" $gitguidir "\\;" gitguidir
59 set env(PATH) "$gitguidir;$env(PATH)"
60 set _search_path [split $env(PATH) {;}]
61 # Skip empty `PATH` elements
62 set _search_path [lsearch -all -inline -not -exact \
63 $_search_path ""]
64 }
65
66 if {[lsearch -exact $args -script] >= 0} {
67 set suffix {}
68 } else {
69 set suffix .exe
70 }
71
72 foreach p $_search_path {
73 set p [file join $p $what$suffix]
74 if {[file exists $p]} {
75 return [file normalize $p]
76 }
77 }
78 return {}
79 }
80
81 proc sanitize_command_line {command_line from_index} {
82 set i $from_index
83 while {$i < [llength $command_line]} {
84 set cmd [lindex $command_line $i]
85 if {[llength [file split $cmd]] < 2} {
86 set fullpath [_which $cmd]
87 if {$fullpath eq ""} {
88 throw {NOT-FOUND} "$cmd not found in PATH"
89 }
90 lset command_line $i $fullpath
91 }
92
93 # handle piped commands, e.g. `exec A | B`
94 for {incr i} {$i < [llength $command_line]} {incr i} {
95 if {[lindex $command_line $i] eq "|"} {
96 incr i
97 break
98 }
99 }
100 }
101 return $command_line
102 }
103
104 # Override `exec` to avoid unsafe PATH lookup
105
106 rename exec real_exec
107
108 proc exec {args} {
109 # skip options
110 for {set i 0} {$i < [llength $args]} {incr i} {
111 set arg [lindex $args $i]
112 if {$arg eq "--"} {
113 incr i
114 break
115 }
116 if {[string range $arg 0 0] ne "-"} {
117 break
118 }
119 }
120 set args [sanitize_command_line $args $i]
121 uplevel 1 real_exec $args
122 }
123
124 # Override `open` to avoid unsafe PATH lookup
125
126 rename open real_open
127
128 proc open {args} {
129 set arg0 [lindex $args 0]
130 if {[string range $arg0 0 0] eq "|"} {
131 set command_line [string trim [string range $arg0 1 end]]
132 lset args 0 "| [sanitize_command_line $command_line 0]"
133 }
134 uplevel 1 real_open $args
135 }
136}
137
138# End of safe PATH lookup stuff
139
140# Wrap exec/open to sanitize arguments
141
142# unsafe arguments begin with redirections or the pipe or background operators
143proc is_arg_unsafe {arg} {
144 regexp {^([<|>&]|2>)} $arg
145}
146
147proc make_arg_safe {arg} {
148 if {[is_arg_unsafe $arg]} {
149 set arg [file join . $arg]
150 }
151 return $arg
152}
153
154proc make_arglist_safe {arglist} {
155 set res {}
156 foreach arg $arglist {
157 lappend res [make_arg_safe $arg]
158 }
159 return $res
160}
161
162# executes one command
163# no redirections or pipelines are possible
164# cmd is a list that specifies the command and its arguments
165# calls `exec` and returns its value
166proc safe_exec {cmd} {
167 eval exec [make_arglist_safe $cmd]
168}
169
170# executes one command with redirections
171# no pipelines are possible
172# cmd is a list that specifies the command and its arguments
173# redir is a list that specifies redirections (output, background, constant(!) commands)
174# calls `exec` and returns its value
175proc safe_exec_redirect {cmd redir} {
176 eval exec [make_arglist_safe $cmd] $redir
177}
178
179proc safe_open_file {filename flags} {
180 # a file name starting with "|" would attempt to run a process
181 # but such a file name must be treated as a relative path
182 # hide the "|" behind "./"
183 if {[string index $filename 0] eq "|"} {
184 set filename [file join . $filename]
185 }
186 open $filename $flags
187}
188
189# opens a command pipeline for reading
190# cmd is a list that specifies the command and its arguments
191# calls `open` and returns the file id
192proc safe_open_command {cmd} {
193 open |[make_arglist_safe $cmd] r
194}
195
196# opens a command pipeline for reading and writing
197# cmd is a list that specifies the command and its arguments
198# calls `open` and returns the file id
199proc safe_open_command_rw {cmd} {
200 open |[make_arglist_safe $cmd] r+
201}
202
203# opens a command pipeline for reading with redirections
204# cmd is a list that specifies the command and its arguments
205# redir is a list that specifies redirections
206# calls `open` and returns the file id
207proc safe_open_command_redirect {cmd redir} {
208 set cmd [make_arglist_safe $cmd]
209 open |[concat $cmd $redir] r
210}
211
212# opens a pipeline with several commands for reading
213# cmds is a list of lists, each of which specifies a command and its arguments
214# calls `open` and returns the file id
215proc safe_open_pipeline {cmds} {
216 set cmd {}
217 foreach subcmd $cmds {
218 set cmd [concat $cmd | [make_arglist_safe $subcmd]]
219 }
220 open $cmd r
221}
222
223# End exec/open wrappers
224
225proc hasworktree {} {
226 return [expr {[exec git rev-parse --is-bare-repository] == "false" &&
227 [exec git rev-parse --is-inside-git-dir] == "false"}]
228}
229
230proc reponame {} {
231 global gitdir
232 set n [file normalize $gitdir]
233 if {[string match "*/.git" $n]} {
234 set n [string range $n 0 end-5]
235 }
236 return [file tail $n]
237}
238
239proc gitworktree {} {
240 variable _gitworktree
241 if {[info exists _gitworktree]} {
242 return $_gitworktree
243 }
244 # v1.7.0 introduced --show-toplevel to return the canonical work-tree
245 if {[catch {set _gitworktree [exec git rev-parse --show-toplevel]}]} {
246 # try to set work tree from environment, core.worktree or use
247 # cdup to obtain a relative path to the top of the worktree. If
248 # run from the top, the ./ prefix ensures normalize expands pwd.
249 if {[catch { set _gitworktree $env(GIT_WORK_TREE) }]} {
250 if {[catch {set _gitworktree [exec git config --get core.worktree]}]} {
251 set _gitworktree [file normalize ./[exec git rev-parse --show-cdup]]
252 }
253 }
254 }
255 return $_gitworktree
256}
257
258# A simple scheduler for compute-intensive stuff.
259# The aim is to make sure that event handlers for GUI actions can
260# run at least every 50-100 ms. Unfortunately fileevent handlers are
261# run before X event handlers, so reading from a fast source can
262# make the GUI completely unresponsive.
263proc run args {
264 global isonrunq runq currunq
265
266 set script $args
267 if {[info exists isonrunq($script)]} return
268 if {$runq eq {} && ![info exists currunq]} {
269 after idle dorunq
270 }
271 lappend runq [list {} $script]
272 set isonrunq($script) 1
273}
274
275proc filerun {fd script} {
276 fileevent $fd readable [list filereadable $fd $script]
277}
278
279proc filereadable {fd script} {
280 global runq currunq
281
282 fileevent $fd readable {}
283 if {$runq eq {} && ![info exists currunq]} {
284 after idle dorunq
285 }
286 lappend runq [list $fd $script]
287}
288
289proc nukefile {fd} {
290 global runq
291
292 for {set i 0} {$i < [llength $runq]} {} {
293 if {[lindex $runq $i 0] eq $fd} {
294 set runq [lreplace $runq $i $i]
295 } else {
296 incr i
297 }
298 }
299}
300
301proc dorunq {} {
302 global isonrunq runq currunq
303
304 set tstart [clock clicks -milliseconds]
305 set t0 $tstart
306 while {[llength $runq] > 0} {
307 set fd [lindex $runq 0 0]
308 set script [lindex $runq 0 1]
309 set currunq [lindex $runq 0]
310 set runq [lrange $runq 1 end]
311 set repeat [eval $script]
312 unset currunq
313 set t1 [clock clicks -milliseconds]
314 set t [expr {$t1 - $t0}]
315 if {$repeat ne {} && $repeat} {
316 if {$fd eq {} || $repeat == 2} {
317 # script returns 1 if it wants to be readded
318 # file readers return 2 if they could do more straight away
319 lappend runq [list $fd $script]
320 } else {
321 fileevent $fd readable [list filereadable $fd $script]
322 }
323 } elseif {$fd eq {}} {
324 unset isonrunq($script)
325 }
326 set t0 $t1
327 if {$t1 - $tstart >= 80} break
328 }
329 if {$runq ne {}} {
330 after idle dorunq
331 }
332}
333
334proc reg_instance {fd} {
335 global commfd leftover loginstance
336
337 set i [incr loginstance]
338 set commfd($i) $fd
339 set leftover($i) {}
340 return $i
341}
342
343proc unmerged_files {files} {
344 global nr_unmerged
345
346 # find the list of unmerged files
347 set mlist {}
348 set nr_unmerged 0
349 if {[catch {
350 set fd [safe_open_command {git ls-files -u}]
351 } err]} {
352 show_error {} . "[mc "Couldn't get list of unmerged files:"] $err"
353 exit 1
354 }
355 while {[gets $fd line] >= 0} {
356 set i [string first "\t" $line]
357 if {$i < 0} continue
358 set fname [string range $line [expr {$i+1}] end]
359 if {[lsearch -exact $mlist $fname] >= 0} continue
360 incr nr_unmerged
361 if {$files eq {} || [path_filter $files $fname]} {
362 lappend mlist $fname
363 }
364 }
365 catch {close $fd}
366 return $mlist
367}
368
369proc parseviewargs {n arglist} {
370 global vdatemode vmergeonly vflags vdflags vrevs vfiltered vorigargs env
371 global vinlinediff
372 global worddiff
373
374 set vdatemode($n) 0
375 set vmergeonly($n) 0
376 set vinlinediff($n) 0
377 set glflags {}
378 set diffargs {}
379 set nextisval 0
380 set revargs {}
381 set origargs $arglist
382 set allknown 1
383 set filtered 0
384 set i -1
385 foreach arg $arglist {
386 incr i
387 if {$nextisval} {
388 lappend glflags $arg
389 set nextisval 0
390 continue
391 }
392 switch -glob -- $arg {
393 "-d" -
394 "--date-order" {
395 set vdatemode($n) 1
396 # remove from origargs in case we hit an unknown option
397 set origargs [lreplace $origargs $i $i]
398 incr i -1
399 }
400 "-[puabwcrRBMC]" -
401 "--no-renames" - "--full-index" - "--binary" - "--abbrev=*" -
402 "--find-copies-harder" - "-l*" - "--ext-diff" - "--no-ext-diff" -
403 "--src-prefix=*" - "--dst-prefix=*" - "--no-prefix" -
404 "-O*" - "--text" - "--full-diff" - "--ignore-space-at-eol" -
405 "--ignore-space-change" - "-U*" - "--unified=*" {
406 # These request or affect diff output, which we don't want.
407 # Some could be used to set our defaults for diff display.
408 lappend diffargs $arg
409 }
410 "--raw" - "--patch-with-raw" - "--patch-with-stat" -
411 "--name-only" - "--name-status" - "--color" -
412 "--log-size" - "--pretty=*" - "--decorate" - "--abbrev-commit" -
413 "--cc" - "-z" - "--header" - "--parents" - "--boundary" -
414 "--no-color" - "-g" - "--walk-reflogs" - "--no-walk" -
415 "--timestamp" - "relative-date" - "--date=*" - "--stdin" -
416 "--objects" - "--objects-edge" - "--reverse" {
417 # These cause our parsing of git log's output to fail, or else
418 # they're options we want to set ourselves, so ignore them.
419 }
420 "--color-words*" - "--word-diff=color" {
421 # These trigger a word diff in the console interface,
422 # so help the user by enabling our own support
423 set worddiff [mc "Color words"]
424 }
425 "--word-diff*" {
426 set worddiff [mc "Markup words"]
427 }
428 "--stat=*" - "--numstat" - "--shortstat" - "--summary" -
429 "--check" - "--exit-code" - "--quiet" - "--topo-order" -
430 "--full-history" - "--dense" - "--sparse" -
431 "--follow" - "--left-right" - "--encoding=*" {
432 # These are harmless, and some are even useful
433 lappend glflags $arg
434 }
435 "--diff-filter=*" - "--no-merges" - "--unpacked" -
436 "--max-count=*" - "--skip=*" - "--since=*" - "--after=*" -
437 "--until=*" - "--before=*" - "--max-age=*" - "--min-age=*" -
438 "--author=*" - "--committer=*" - "--grep=*" - "-[iE]" -
439 "--remove-empty" - "--first-parent" - "--cherry-pick" -
440 "-S*" - "-G*" - "--pickaxe-all" - "--pickaxe-regex" -
441 "--simplify-by-decoration" {
442 # These mean that we get a subset of the commits
443 set filtered 1
444 lappend glflags $arg
445 }
446 "-L*" {
447 # Line-log with 'stuck' argument (unstuck form is
448 # not supported)
449 set filtered 1
450 set vinlinediff($n) 1
451 set allknown 0
452 lappend glflags $arg
453 }
454 "-n" {
455 # This appears to be the only one that has a value as a
456 # separate word following it
457 set filtered 1
458 set nextisval 1
459 lappend glflags $arg
460 }
461 "--not" - "--all" {
462 lappend revargs $arg
463 }
464 "--merge" {
465 set vmergeonly($n) 1
466 # git rev-parse doesn't understand --merge
467 lappend revargs --gitk-symmetric-diff-marker MERGE_HEAD...HEAD
468 }
469 "--no-replace-objects" {
470 set env(GIT_NO_REPLACE_OBJECTS) "1"
471 }
472 "-*" {
473 # Other flag arguments including -<n>
474 if {[string is digit -strict [string range $arg 1 end]]} {
475 set filtered 1
476 } else {
477 # a flag argument that we don't recognize;
478 # that means we can't optimize
479 set allknown 0
480 }
481 lappend glflags $arg
482 }
483 default {
484 # Non-flag arguments specify commits or ranges of commits
485 if {[string match "*...*" $arg]} {
486 lappend revargs --gitk-symmetric-diff-marker
487 }
488 lappend revargs $arg
489 }
490 }
491 }
492 set vdflags($n) $diffargs
493 set vflags($n) $glflags
494 set vrevs($n) $revargs
495 set vfiltered($n) $filtered
496 set vorigargs($n) $origargs
497 return $allknown
498}
499
500proc parseviewrevs {view revs} {
501 global vposids vnegids
502 global hashlength
503
504 if {$revs eq {}} {
505 set revs HEAD
506 } elseif {[lsearch -exact $revs --all] >= 0} {
507 lappend revs HEAD
508 }
509 if {[catch {set ids [safe_exec [concat git rev-parse $revs]]} err]} {
510 # we get stdout followed by stderr in $err
511 # for an unknown rev, git rev-parse echoes it and then errors out
512 set errlines [split $err "\n"]
513 set badrev {}
514 for {set l 0} {$l < [llength $errlines]} {incr l} {
515 set line [lindex $errlines $l]
516 if {!([string length $line] == $hashlength && [string is xdigit $line])} {
517 if {[string match "fatal:*" $line]} {
518 if {[string match "fatal: ambiguous argument*" $line]
519 && $badrev ne {}} {
520 if {[llength $badrev] == 1} {
521 set err "unknown revision $badrev"
522 } else {
523 set err "unknown revisions: [join $badrev ", "]"
524 }
525 } else {
526 set err [join [lrange $errlines $l end] "\n"]
527 }
528 break
529 }
530 lappend badrev $line
531 }
532 }
533 error_popup "[mc "Error parsing revisions:"] $err"
534 return {}
535 }
536 set ret {}
537 set pos {}
538 set neg {}
539 set sdm 0
540 foreach id [split $ids "\n"] {
541 if {$id eq "--gitk-symmetric-diff-marker"} {
542 set sdm 4
543 } elseif {[string match "^*" $id]} {
544 if {$sdm != 1} {
545 lappend ret $id
546 if {$sdm == 3} {
547 set sdm 0
548 }
549 }
550 lappend neg [string range $id 1 end]
551 } else {
552 if {$sdm != 2} {
553 lappend ret $id
554 } else {
555 lset ret end $id...[lindex $ret end]
556 }
557 lappend pos $id
558 }
559 incr sdm -1
560 }
561 set vposids($view) $pos
562 set vnegids($view) $neg
563 return $ret
564}
565
566# Start off a git log process and arrange to read its output
567proc start_rev_list {view} {
568 global startmsecs commitidx viewcomplete curview
569 global tclencoding
570 global viewargs viewargscmd viewfiles vfilelimit
571 global showlocalchanges
572 global viewactive viewinstances vmergeonly
573 global mainheadid viewmainheadid viewmainheadid_orig
574 global vcanopt vflags vrevs vorigargs
575
576 set startmsecs [clock clicks -milliseconds]
577 set commitidx($view) 0
578 # these are set this way for the error exits
579 set viewcomplete($view) 1
580 set viewactive($view) 0
581 varcinit $view
582
583 set args $viewargs($view)
584 if {$viewargscmd($view) ne {}} {
585 if {[catch {
586 set str [safe_exec [list sh -c $viewargscmd($view)]]
587 } err]} {
588 error_popup "[mc "Error executing --argscmd command:"] $err"
589 return 0
590 }
591 set args [concat $args [split $str "\n"]]
592 }
593 set vcanopt($view) [parseviewargs $view $args]
594
595 set files $viewfiles($view)
596 if {$vmergeonly($view)} {
597 set files [unmerged_files $files]
598 if {$files eq {}} {
599 global nr_unmerged
600 if {$nr_unmerged == 0} {
601 error_popup [mc "No files selected: --merge specified but\
602 no files are unmerged."]
603 } else {
604 error_popup [mc "No files selected: --merge specified but\
605 no unmerged files are within file limit."]
606 }
607 return 0
608 }
609 }
610 set vfilelimit($view) $files
611
612 if {$vcanopt($view)} {
613 set revs [parseviewrevs $view $vrevs($view)]
614 if {$revs eq {}} {
615 return 0
616 }
617 set args $vflags($view)
618 } else {
619 set revs {}
620 set args $vorigargs($view)
621 }
622
623 if {[catch {
624 set fd [safe_open_command_redirect [concat git log --no-color -z --pretty=raw --show-notes \
625 --parents --boundary $args --stdin] \
626 [list "<<[join [concat $revs "--" $files] "\n"]"]]
627 } err]} {
628 error_popup "[mc "Error executing git log:"] $err"
629 return 0
630 }
631 set i [reg_instance $fd]
632 set viewinstances($view) [list $i]
633 set viewmainheadid($view) $mainheadid
634 set viewmainheadid_orig($view) $mainheadid
635 if {$files ne {} && $mainheadid ne {}} {
636 get_viewmainhead $view
637 }
638 if {$showlocalchanges && $viewmainheadid($view) ne {}} {
639 interestedin $viewmainheadid($view) dodiffindex
640 }
641 fconfigure $fd -blocking 0 -translation lf -eofchar {}
642 if {$tclencoding != {}} {
643 fconfigure $fd -encoding $tclencoding
644 }
645 filerun $fd [list getcommitlines $fd $i $view 0]
646 nowbusy $view [mc "Reading"]
647 set viewcomplete($view) 0
648 set viewactive($view) 1
649 return 1
650}
651
652proc stop_instance {inst} {
653 global commfd leftover
654
655 set fd $commfd($inst)
656 catch {
657 set pid [pid $fd]
658
659 if {$::tcl_platform(platform) eq {windows}} {
660 safe_exec [list taskkill /pid $pid]
661 } else {
662 safe_exec [list kill $pid]
663 }
664 }
665 catch {close $fd}
666 nukefile $fd
667 unset commfd($inst)
668 unset leftover($inst)
669}
670
671proc stop_backends {} {
672 global commfd
673
674 foreach inst [array names commfd] {
675 stop_instance $inst
676 }
677}
678
679proc stop_rev_list {view} {
680 global viewinstances
681
682 foreach inst $viewinstances($view) {
683 stop_instance $inst
684 }
685 set viewinstances($view) {}
686}
687
688proc reset_pending_select {selid} {
689 global pending_select mainheadid selectheadid
690
691 if {$selid ne {}} {
692 set pending_select $selid
693 } elseif {$selectheadid ne {}} {
694 set pending_select $selectheadid
695 } else {
696 set pending_select $mainheadid
697 }
698}
699
700proc getcommits {selid} {
701 global canv curview need_redisplay viewactive
702
703 initlayout
704 if {[start_rev_list $curview]} {
705 reset_pending_select $selid
706 show_status [mc "Reading commits..."]
707 set need_redisplay 1
708 } else {
709 show_status [mc "No commits selected"]
710 }
711}
712
713proc updatecommits {} {
714 global curview vcanopt vorigargs vfilelimit viewinstances
715 global viewactive viewcomplete tclencoding
716 global startmsecs showneartags showlocalchanges
717 global mainheadid viewmainheadid viewmainheadid_orig pending_select
718 global hasworktree
719 global varcid vposids vnegids vflags vrevs
720 global hashlength
721
722 set hasworktree [hasworktree]
723 rereadrefs
724 set view $curview
725 if {$mainheadid ne $viewmainheadid_orig($view)} {
726 if {$showlocalchanges} {
727 dohidelocalchanges
728 }
729 set viewmainheadid($view) $mainheadid
730 set viewmainheadid_orig($view) $mainheadid
731 if {$vfilelimit($view) ne {}} {
732 get_viewmainhead $view
733 }
734 }
735 if {$showlocalchanges} {
736 doshowlocalchanges
737 }
738 if {$vcanopt($view)} {
739 set oldpos $vposids($view)
740 set oldneg $vnegids($view)
741 set revs [parseviewrevs $view $vrevs($view)]
742 if {$revs eq {}} {
743 return
744 }
745 # note: getting the delta when negative refs change is hard,
746 # and could require multiple git log invocations, so in that
747 # case we ask git log for all the commits (not just the delta)
748 if {$oldneg eq $vnegids($view)} {
749 set newrevs {}
750 set npos 0
751 # take out positive refs that we asked for before or
752 # that we have already seen
753 foreach rev $revs {
754 if {[string length $rev] == $hashlength} {
755 if {[lsearch -exact $oldpos $rev] < 0
756 && ![info exists varcid($view,$rev)]} {
757 lappend newrevs $rev
758 incr npos
759 }
760 } else {
761 lappend $newrevs $rev
762 }
763 }
764 if {$npos == 0} return
765 set revs $newrevs
766 set vposids($view) [lsort -unique [concat $oldpos $vposids($view)]]
767 }
768 set args $vflags($view)
769 foreach r $oldpos {
770 lappend revs "^$r"
771 }
772 } else {
773 set revs {}
774 set args $vorigargs($view)
775 }
776 if {[catch {
777 set fd [safe_open_command_redirect [concat git log --no-color -z --pretty=raw --show-notes \
778 --parents --boundary $args --stdin] \
779 [list "<<[join [concat $revs "--" $vfilelimit($view)] "\n"]"]]
780 } err]} {
781 error_popup "[mc "Error executing git log:"] $err"
782 return
783 }
784 if {$viewactive($view) == 0} {
785 set startmsecs [clock clicks -milliseconds]
786 }
787 set i [reg_instance $fd]
788 lappend viewinstances($view) $i
789 fconfigure $fd -blocking 0 -translation lf -eofchar {}
790 if {$tclencoding != {}} {
791 fconfigure $fd -encoding $tclencoding
792 }
793 filerun $fd [list getcommitlines $fd $i $view 1]
794 incr viewactive($view)
795 set viewcomplete($view) 0
796 reset_pending_select {}
797 nowbusy $view [mc "Reading"]
798 if {$showneartags} {
799 getallcommits
800 }
801}
802
803proc reloadcommits {} {
804 global curview viewcomplete selectedline currentid thickerline
805 global showneartags treediffs commitinterest cached_commitrow
806 global targetid commitinfo
807
808 set selid {}
809 if {$selectedline ne {}} {
810 set selid $currentid
811 }
812
813 if {!$viewcomplete($curview)} {
814 stop_rev_list $curview
815 }
816 resetvarcs $curview
817 set selectedline {}
818 unset -nocomplain currentid
819 unset -nocomplain thickerline
820 unset -nocomplain treediffs
821 readrefs
822 changedrefs
823 if {$showneartags} {
824 getallcommits
825 }
826 clear_display
827 unset -nocomplain commitinfo
828 unset -nocomplain commitinterest
829 unset -nocomplain cached_commitrow
830 unset -nocomplain targetid
831 setcanvscroll
832 getcommits $selid
833 return 0
834}
835
836# This makes a string representation of a positive integer which
837# sorts as a string in numerical order
838proc strrep {n} {
839 if {$n < 16} {
840 return [format "%x" $n]
841 } elseif {$n < 256} {
842 return [format "x%.2x" $n]
843 } elseif {$n < 65536} {
844 return [format "y%.4x" $n]
845 }
846 return [format "z%.8x" $n]
847}
848
849# Procedures used in reordering commits from git log (without
850# --topo-order) into the order for display.
851
852proc varcinit {view} {
853 global varcstart vupptr vdownptr vleftptr vbackptr varctok varcrow
854 global vtokmod varcmod vrowmod varcix vlastins
855
856 set varcstart($view) {{}}
857 set vupptr($view) {0}
858 set vdownptr($view) {0}
859 set vleftptr($view) {0}
860 set vbackptr($view) {0}
861 set varctok($view) {{}}
862 set varcrow($view) {{}}
863 set vtokmod($view) {}
864 set varcmod($view) 0
865 set vrowmod($view) 0
866 set varcix($view) {{}}
867 set vlastins($view) {0}
868}
869
870proc resetvarcs {view} {
871 global varcid varccommits parents children vseedcount ordertok
872 global vshortids
873
874 foreach vid [array names varcid $view,*] {
875 unset varcid($vid)
876 unset children($vid)
877 unset parents($vid)
878 }
879 foreach vid [array names vshortids $view,*] {
880 unset vshortids($vid)
881 }
882 # some commits might have children but haven't been seen yet
883 foreach vid [array names children $view,*] {
884 unset children($vid)
885 }
886 foreach va [array names varccommits $view,*] {
887 unset varccommits($va)
888 }
889 foreach vd [array names vseedcount $view,*] {
890 unset vseedcount($vd)
891 }
892 unset -nocomplain ordertok
893}
894
895# returns a list of the commits with no children
896proc seeds {v} {
897 global vdownptr vleftptr varcstart
898
899 set ret {}
900 set a [lindex $vdownptr($v) 0]
901 while {$a != 0} {
902 lappend ret [lindex $varcstart($v) $a]
903 set a [lindex $vleftptr($v) $a]
904 }
905 return $ret
906}
907
908proc newvarc {view id} {
909 global varcid varctok parents children vdatemode
910 global vupptr vdownptr vleftptr vbackptr varcrow varcix varcstart
911 global commitdata commitinfo vseedcount varccommits vlastins
912
913 set a [llength $varctok($view)]
914 set vid $view,$id
915 if {[llength $children($vid)] == 0 || $vdatemode($view)} {
916 if {![info exists commitinfo($id)]} {
917 parsecommit $id $commitdata($id) 1
918 }
919 set cdate [lindex [lindex $commitinfo($id) 4] 0]
920 if {![string is integer -strict $cdate]} {
921 set cdate 0
922 }
923 if {![info exists vseedcount($view,$cdate)]} {
924 set vseedcount($view,$cdate) -1
925 }
926 set c [incr vseedcount($view,$cdate)]
927 set cdate [expr {$cdate ^ 0xffffffff}]
928 set tok "s[strrep $cdate][strrep $c]"
929 } else {
930 set tok {}
931 }
932 set ka 0
933 if {[llength $children($vid)] > 0} {
934 set kid [lindex $children($vid) end]
935 set k $varcid($view,$kid)
936 if {[string compare [lindex $varctok($view) $k] $tok] > 0} {
937 set ki $kid
938 set ka $k
939 set tok [lindex $varctok($view) $k]
940 }
941 }
942 if {$ka != 0} {
943 set i [lsearch -exact $parents($view,$ki) $id]
944 set j [expr {[llength $parents($view,$ki)] - 1 - $i}]
945 append tok [strrep $j]
946 }
947 set c [lindex $vlastins($view) $ka]
948 if {$c == 0 || [string compare $tok [lindex $varctok($view) $c]] < 0} {
949 set c $ka
950 set b [lindex $vdownptr($view) $ka]
951 } else {
952 set b [lindex $vleftptr($view) $c]
953 }
954 while {$b != 0 && [string compare $tok [lindex $varctok($view) $b]] >= 0} {
955 set c $b
956 set b [lindex $vleftptr($view) $c]
957 }
958 if {$c == $ka} {
959 lset vdownptr($view) $ka $a
960 lappend vbackptr($view) 0
961 } else {
962 lset vleftptr($view) $c $a
963 lappend vbackptr($view) $c
964 }
965 lset vlastins($view) $ka $a
966 lappend vupptr($view) $ka
967 lappend vleftptr($view) $b
968 if {$b != 0} {
969 lset vbackptr($view) $b $a
970 }
971 lappend varctok($view) $tok
972 lappend varcstart($view) $id
973 lappend vdownptr($view) 0
974 lappend varcrow($view) {}
975 lappend varcix($view) {}
976 set varccommits($view,$a) {}
977 lappend vlastins($view) 0
978 return $a
979}
980
981proc splitvarc {p v} {
982 global varcid varcstart varccommits varctok vtokmod
983 global vupptr vdownptr vleftptr vbackptr varcix varcrow vlastins
984
985 set oa $varcid($v,$p)
986 set otok [lindex $varctok($v) $oa]
987 set ac $varccommits($v,$oa)
988 set i [lsearch -exact $varccommits($v,$oa) $p]
989 if {$i <= 0} return
990 set na [llength $varctok($v)]
991 # "%" sorts before "0"...
992 set tok "$otok%[strrep $i]"
993 lappend varctok($v) $tok
994 lappend varcrow($v) {}
995 lappend varcix($v) {}
996 set varccommits($v,$oa) [lrange $ac 0 [expr {$i - 1}]]
997 set varccommits($v,$na) [lrange $ac $i end]
998 lappend varcstart($v) $p
999 foreach id $varccommits($v,$na) {
1000 set varcid($v,$id) $na
1001 }
1002 lappend vdownptr($v) [lindex $vdownptr($v) $oa]
1003 lappend vlastins($v) [lindex $vlastins($v) $oa]
1004 lset vdownptr($v) $oa $na
1005 lset vlastins($v) $oa 0
1006 lappend vupptr($v) $oa
1007 lappend vleftptr($v) 0
1008 lappend vbackptr($v) 0
1009 for {set b [lindex $vdownptr($v) $na]} {$b != 0} {set b [lindex $vleftptr($v) $b]} {
1010 lset vupptr($v) $b $na
1011 }
1012 if {[string compare $otok $vtokmod($v)] <= 0} {
1013 modify_arc $v $oa
1014 }
1015}
1016
1017proc renumbervarc {a v} {
1018 global parents children varctok varcstart varccommits
1019 global vupptr vdownptr vleftptr vbackptr vlastins varcid vtokmod vdatemode
1020
1021 set t1 [clock clicks -milliseconds]
1022 set todo {}
1023 set isrelated($a) 1
1024 set kidchanged($a) 1
1025 set ntot 0
1026 while {$a != 0} {
1027 if {[info exists isrelated($a)]} {
1028 lappend todo $a
1029 set id [lindex $varccommits($v,$a) end]
1030 foreach p $parents($v,$id) {
1031 if {[info exists varcid($v,$p)]} {
1032 set isrelated($varcid($v,$p)) 1
1033 }
1034 }
1035 }
1036 incr ntot
1037 set b [lindex $vdownptr($v) $a]
1038 if {$b == 0} {
1039 while {$a != 0} {
1040 set b [lindex $vleftptr($v) $a]
1041 if {$b != 0} break
1042 set a [lindex $vupptr($v) $a]
1043 }
1044 }
1045 set a $b
1046 }
1047 foreach a $todo {
1048 if {![info exists kidchanged($a)]} continue
1049 set id [lindex $varcstart($v) $a]
1050 if {[llength $children($v,$id)] > 1} {
1051 set children($v,$id) [lsort -command [list vtokcmp $v] \
1052 $children($v,$id)]
1053 }
1054 set oldtok [lindex $varctok($v) $a]
1055 if {!$vdatemode($v)} {
1056 set tok {}
1057 } else {
1058 set tok $oldtok
1059 }
1060 set ka 0
1061 set kid [last_real_child $v,$id]
1062 if {$kid ne {}} {
1063 set k $varcid($v,$kid)
1064 if {[string compare [lindex $varctok($v) $k] $tok] > 0} {
1065 set ki $kid
1066 set ka $k
1067 set tok [lindex $varctok($v) $k]
1068 }
1069 }
1070 if {$ka != 0} {
1071 set i [lsearch -exact $parents($v,$ki) $id]
1072 set j [expr {[llength $parents($v,$ki)] - 1 - $i}]
1073 append tok [strrep $j]
1074 }
1075 if {$tok eq $oldtok} {
1076 continue
1077 }
1078 set id [lindex $varccommits($v,$a) end]
1079 foreach p $parents($v,$id) {
1080 if {[info exists varcid($v,$p)]} {
1081 set kidchanged($varcid($v,$p)) 1
1082 } else {
1083 set sortkids($p) 1
1084 }
1085 }
1086 lset varctok($v) $a $tok
1087 set b [lindex $vupptr($v) $a]
1088 if {$b != $ka} {
1089 if {[string compare [lindex $varctok($v) $ka] $vtokmod($v)] < 0} {
1090 modify_arc $v $ka
1091 }
1092 if {[string compare [lindex $varctok($v) $b] $vtokmod($v)] < 0} {
1093 modify_arc $v $b
1094 }
1095 set c [lindex $vbackptr($v) $a]
1096 set d [lindex $vleftptr($v) $a]
1097 if {$c == 0} {
1098 lset vdownptr($v) $b $d
1099 } else {
1100 lset vleftptr($v) $c $d
1101 }
1102 if {$d != 0} {
1103 lset vbackptr($v) $d $c
1104 }
1105 if {[lindex $vlastins($v) $b] == $a} {
1106 lset vlastins($v) $b $c
1107 }
1108 lset vupptr($v) $a $ka
1109 set c [lindex $vlastins($v) $ka]
1110 if {$c == 0 || \
1111 [string compare $tok [lindex $varctok($v) $c]] < 0} {
1112 set c $ka
1113 set b [lindex $vdownptr($v) $ka]
1114 } else {
1115 set b [lindex $vleftptr($v) $c]
1116 }
1117 while {$b != 0 && \
1118 [string compare $tok [lindex $varctok($v) $b]] >= 0} {
1119 set c $b
1120 set b [lindex $vleftptr($v) $c]
1121 }
1122 if {$c == $ka} {
1123 lset vdownptr($v) $ka $a
1124 lset vbackptr($v) $a 0
1125 } else {
1126 lset vleftptr($v) $c $a
1127 lset vbackptr($v) $a $c
1128 }
1129 lset vleftptr($v) $a $b
1130 if {$b != 0} {
1131 lset vbackptr($v) $b $a
1132 }
1133 lset vlastins($v) $ka $a
1134 }
1135 }
1136 foreach id [array names sortkids] {
1137 if {[llength $children($v,$id)] > 1} {
1138 set children($v,$id) [lsort -command [list vtokcmp $v] \
1139 $children($v,$id)]
1140 }
1141 }
1142 set t2 [clock clicks -milliseconds]
1143 #puts "renumbervarc did [llength $todo] of $ntot arcs in [expr {$t2-$t1}]ms"
1144}
1145
1146# Fix up the graph after we have found out that in view $v,
1147# $p (a commit that we have already seen) is actually the parent
1148# of the last commit in arc $a.
1149proc fix_reversal {p a v} {
1150 global varcid varcstart varctok vupptr
1151
1152 set pa $varcid($v,$p)
1153 if {$p ne [lindex $varcstart($v) $pa]} {
1154 splitvarc $p $v
1155 set pa $varcid($v,$p)
1156 }
1157 # seeds always need to be renumbered
1158 if {[lindex $vupptr($v) $pa] == 0 ||
1159 [string compare [lindex $varctok($v) $a] \
1160 [lindex $varctok($v) $pa]] > 0} {
1161 renumbervarc $pa $v
1162 }
1163}
1164
1165proc insertrow {id p v} {
1166 global cmitlisted children parents varcid varctok vtokmod
1167 global varccommits ordertok commitidx numcommits curview
1168 global targetid targetrow vshortids
1169
1170 readcommit $id
1171 set vid $v,$id
1172 set cmitlisted($vid) 1
1173 set children($vid) {}
1174 set parents($vid) [list $p]
1175 set a [newvarc $v $id]
1176 set varcid($vid) $a
1177 lappend vshortids($v,[string range $id 0 3]) $id
1178 if {[string compare [lindex $varctok($v) $a] $vtokmod($v)] < 0} {
1179 modify_arc $v $a
1180 }
1181 lappend varccommits($v,$a) $id
1182 set vp $v,$p
1183 if {[llength [lappend children($vp) $id]] > 1} {
1184 set children($vp) [lsort -command [list vtokcmp $v] $children($vp)]
1185 unset -nocomplain ordertok
1186 }
1187 fix_reversal $p $a $v
1188 incr commitidx($v)
1189 if {$v == $curview} {
1190 set numcommits $commitidx($v)
1191 setcanvscroll
1192 if {[info exists targetid]} {
1193 if {![comes_before $targetid $p]} {
1194 incr targetrow
1195 }
1196 }
1197 }
1198}
1199
1200proc insertfakerow {id p} {
1201 global varcid varccommits parents children cmitlisted
1202 global commitidx varctok vtokmod targetid targetrow curview numcommits
1203
1204 set v $curview
1205 set a $varcid($v,$p)
1206 set i [lsearch -exact $varccommits($v,$a) $p]
1207 if {$i < 0} {
1208 puts "oops: insertfakerow can't find [shortids $p] on arc $a"
1209 return
1210 }
1211 set children($v,$id) {}
1212 set parents($v,$id) [list $p]
1213 set varcid($v,$id) $a
1214 lappend children($v,$p) $id
1215 set cmitlisted($v,$id) 1
1216 set numcommits [incr commitidx($v)]
1217 # note we deliberately don't update varcstart($v) even if $i == 0
1218 set varccommits($v,$a) [linsert $varccommits($v,$a) $i $id]
1219 modify_arc $v $a $i
1220 if {[info exists targetid]} {
1221 if {![comes_before $targetid $p]} {
1222 incr targetrow
1223 }
1224 }
1225 setcanvscroll
1226 drawvisible
1227}
1228
1229proc removefakerow {id} {
1230 global varcid varccommits parents children commitidx
1231 global varctok vtokmod cmitlisted currentid selectedline
1232 global targetid curview numcommits
1233
1234 set v $curview
1235 if {[llength $parents($v,$id)] != 1} {
1236 puts "oops: removefakerow [shortids $id] has [llength $parents($v,$id)] parents"
1237 return
1238 }
1239 set p [lindex $parents($v,$id) 0]
1240 set a $varcid($v,$id)
1241 set i [lsearch -exact $varccommits($v,$a) $id]
1242 if {$i < 0} {
1243 puts "oops: removefakerow can't find [shortids $id] on arc $a"
1244 return
1245 }
1246 unset varcid($v,$id)
1247 set varccommits($v,$a) [lreplace $varccommits($v,$a) $i $i]
1248 unset parents($v,$id)
1249 unset children($v,$id)
1250 unset cmitlisted($v,$id)
1251 set numcommits [incr commitidx($v) -1]
1252 set j [lsearch -exact $children($v,$p) $id]
1253 if {$j >= 0} {
1254 set children($v,$p) [lreplace $children($v,$p) $j $j]
1255 }
1256 modify_arc $v $a $i
1257 if {[info exist currentid] && $id eq $currentid} {
1258 unset currentid
1259 set selectedline {}
1260 }
1261 if {[info exists targetid] && $targetid eq $id} {
1262 set targetid $p
1263 }
1264 setcanvscroll
1265 drawvisible
1266}
1267
1268proc real_children {vp} {
1269 global children nullid nullid2
1270
1271 set kids {}
1272 foreach id $children($vp) {
1273 if {$id ne $nullid && $id ne $nullid2} {
1274 lappend kids $id
1275 }
1276 }
1277 return $kids
1278}
1279
1280proc first_real_child {vp} {
1281 global children nullid nullid2
1282
1283 foreach id $children($vp) {
1284 if {$id ne $nullid && $id ne $nullid2} {
1285 return $id
1286 }
1287 }
1288 return {}
1289}
1290
1291proc last_real_child {vp} {
1292 global children nullid nullid2
1293
1294 set kids $children($vp)
1295 for {set i [llength $kids]} {[incr i -1] >= 0} {} {
1296 set id [lindex $kids $i]
1297 if {$id ne $nullid && $id ne $nullid2} {
1298 return $id
1299 }
1300 }
1301 return {}
1302}
1303
1304proc vtokcmp {v a b} {
1305 global varctok varcid
1306
1307 return [string compare [lindex $varctok($v) $varcid($v,$a)] \
1308 [lindex $varctok($v) $varcid($v,$b)]]
1309}
1310
1311# This assumes that if lim is not given, the caller has checked that
1312# arc a's token is less than $vtokmod($v)
1313proc modify_arc {v a {lim {}}} {
1314 global varctok vtokmod varcmod varcrow vupptr curview vrowmod varccommits
1315
1316 if {$lim ne {}} {
1317 set c [string compare [lindex $varctok($v) $a] $vtokmod($v)]
1318 if {$c > 0} return
1319 if {$c == 0} {
1320 set r [lindex $varcrow($v) $a]
1321 if {$r ne {} && $vrowmod($v) <= $r + $lim} return
1322 }
1323 }
1324 set vtokmod($v) [lindex $varctok($v) $a]
1325 set varcmod($v) $a
1326 if {$v == $curview} {
1327 while {$a != 0 && [lindex $varcrow($v) $a] eq {}} {
1328 set a [lindex $vupptr($v) $a]
1329 set lim {}
1330 }
1331 set r 0
1332 if {$a != 0} {
1333 if {$lim eq {}} {
1334 set lim [llength $varccommits($v,$a)]
1335 }
1336 set r [expr {[lindex $varcrow($v) $a] + $lim}]
1337 }
1338 set vrowmod($v) $r
1339 undolayout $r
1340 }
1341}
1342
1343proc update_arcrows {v} {
1344 global vtokmod varcmod vrowmod varcrow commitidx currentid selectedline
1345 global varcid vrownum varcorder varcix varccommits
1346 global vupptr vdownptr vleftptr varctok
1347 global displayorder parentlist curview cached_commitrow
1348
1349 if {$vrowmod($v) == $commitidx($v)} return
1350 if {$v == $curview} {
1351 if {[llength $displayorder] > $vrowmod($v)} {
1352 set displayorder [lrange $displayorder 0 [expr {$vrowmod($v) - 1}]]
1353 set parentlist [lrange $parentlist 0 [expr {$vrowmod($v) - 1}]]
1354 }
1355 unset -nocomplain cached_commitrow
1356 }
1357 set narctot [expr {[llength $varctok($v)] - 1}]
1358 set a $varcmod($v)
1359 while {$a != 0 && [lindex $varcix($v) $a] eq {}} {
1360 # go up the tree until we find something that has a row number,
1361 # or we get to a seed
1362 set a [lindex $vupptr($v) $a]
1363 }
1364 if {$a == 0} {
1365 set a [lindex $vdownptr($v) 0]
1366 if {$a == 0} return
1367 set vrownum($v) {0}
1368 set varcorder($v) [list $a]
1369 lset varcix($v) $a 0
1370 lset varcrow($v) $a 0
1371 set arcn 0
1372 set row 0
1373 } else {
1374 set arcn [lindex $varcix($v) $a]
1375 if {[llength $vrownum($v)] > $arcn + 1} {
1376 set vrownum($v) [lrange $vrownum($v) 0 $arcn]
1377 set varcorder($v) [lrange $varcorder($v) 0 $arcn]
1378 }
1379 set row [lindex $varcrow($v) $a]
1380 }
1381 while {1} {
1382 set p $a
1383 incr row [llength $varccommits($v,$a)]
1384 # go down if possible
1385 set b [lindex $vdownptr($v) $a]
1386 if {$b == 0} {
1387 # if not, go left, or go up until we can go left
1388 while {$a != 0} {
1389 set b [lindex $vleftptr($v) $a]
1390 if {$b != 0} break
1391 set a [lindex $vupptr($v) $a]
1392 }
1393 if {$a == 0} break
1394 }
1395 set a $b
1396 incr arcn
1397 lappend vrownum($v) $row
1398 lappend varcorder($v) $a
1399 lset varcix($v) $a $arcn
1400 lset varcrow($v) $a $row
1401 }
1402 set vtokmod($v) [lindex $varctok($v) $p]
1403 set varcmod($v) $p
1404 set vrowmod($v) $row
1405 if {[info exists currentid]} {
1406 set selectedline [rowofcommit $currentid]
1407 }
1408}
1409
1410# Test whether view $v contains commit $id
1411proc commitinview {id v} {
1412 global varcid
1413
1414 return [info exists varcid($v,$id)]
1415}
1416
1417# Return the row number for commit $id in the current view
1418proc rowofcommit {id} {
1419 global varcid varccommits varcrow curview cached_commitrow
1420 global varctok vtokmod
1421
1422 set v $curview
1423 if {![info exists varcid($v,$id)]} {
1424 puts "oops rowofcommit no arc for [shortids $id]"
1425 return {}
1426 }
1427 set a $varcid($v,$id)
1428 if {[string compare [lindex $varctok($v) $a] $vtokmod($v)] >= 0} {
1429 update_arcrows $v
1430 }
1431 if {[info exists cached_commitrow($id)]} {
1432 return $cached_commitrow($id)
1433 }
1434 set i [lsearch -exact $varccommits($v,$a) $id]
1435 if {$i < 0} {
1436 puts "oops didn't find commit [shortids $id] in arc $a"
1437 return {}
1438 }
1439 incr i [lindex $varcrow($v) $a]
1440 set cached_commitrow($id) $i
1441 return $i
1442}
1443
1444# Returns 1 if a is on an earlier row than b, otherwise 0
1445proc comes_before {a b} {
1446 global varcid varctok curview
1447
1448 set v $curview
1449 if {$a eq $b || ![info exists varcid($v,$a)] || \
1450 ![info exists varcid($v,$b)]} {
1451 return 0
1452 }
1453 if {$varcid($v,$a) != $varcid($v,$b)} {
1454 return [expr {[string compare [lindex $varctok($v) $varcid($v,$a)] \
1455 [lindex $varctok($v) $varcid($v,$b)]] < 0}]
1456 }
1457 return [expr {[rowofcommit $a] < [rowofcommit $b]}]
1458}
1459
1460proc bsearch {l elt} {
1461 if {[llength $l] == 0 || $elt <= [lindex $l 0]} {
1462 return 0
1463 }
1464 set lo 0
1465 set hi [llength $l]
1466 while {$hi - $lo > 1} {
1467 set mid [expr {int(($lo + $hi) / 2)}]
1468 set t [lindex $l $mid]
1469 if {$elt < $t} {
1470 set hi $mid
1471 } elseif {$elt > $t} {
1472 set lo $mid
1473 } else {
1474 return $mid
1475 }
1476 }
1477 return $lo
1478}
1479
1480# Make sure rows $start..$end-1 are valid in displayorder and parentlist
1481proc make_disporder {start end} {
1482 global vrownum curview commitidx displayorder parentlist
1483 global varccommits varcorder parents vrowmod varcrow
1484 global d_valid_start d_valid_end
1485
1486 if {$end > $vrowmod($curview)} {
1487 update_arcrows $curview
1488 }
1489 set ai [bsearch $vrownum($curview) $start]
1490 set start [lindex $vrownum($curview) $ai]
1491 set narc [llength $vrownum($curview)]
1492 for {set r $start} {$ai < $narc && $r < $end} {incr ai} {
1493 set a [lindex $varcorder($curview) $ai]
1494 set l [llength $displayorder]
1495 set al [llength $varccommits($curview,$a)]
1496 if {$l < $r + $al} {
1497 if {$l < $r} {
1498 set pad [ntimes [expr {$r - $l}] {}]
1499 set displayorder [concat $displayorder $pad]
1500 set parentlist [concat $parentlist $pad]
1501 } elseif {$l > $r} {
1502 set displayorder [lrange $displayorder 0 [expr {$r - 1}]]
1503 set parentlist [lrange $parentlist 0 [expr {$r - 1}]]
1504 }
1505 foreach id $varccommits($curview,$a) {
1506 lappend displayorder $id
1507 lappend parentlist $parents($curview,$id)
1508 }
1509 } elseif {[lindex $displayorder [expr {$r + $al - 1}]] eq {}} {
1510 set i $r
1511 foreach id $varccommits($curview,$a) {
1512 lset displayorder $i $id
1513 lset parentlist $i $parents($curview,$id)
1514 incr i
1515 }
1516 }
1517 incr r $al
1518 }
1519}
1520
1521proc commitonrow {row} {
1522 global displayorder
1523
1524 set id [lindex $displayorder $row]
1525 if {$id eq {}} {
1526 make_disporder $row [expr {$row + 1}]
1527 set id [lindex $displayorder $row]
1528 }
1529 return $id
1530}
1531
1532proc closevarcs {v} {
1533 global varctok varccommits varcid parents children
1534 global cmitlisted commitidx vtokmod curview numcommits
1535
1536 set missing_parents 0
1537 set scripts {}
1538 set narcs [llength $varctok($v)]
1539 for {set a 1} {$a < $narcs} {incr a} {
1540 set id [lindex $varccommits($v,$a) end]
1541 foreach p $parents($v,$id) {
1542 if {[info exists varcid($v,$p)]} continue
1543 # add p as a new commit
1544 incr missing_parents
1545 set cmitlisted($v,$p) 0
1546 set parents($v,$p) {}
1547 if {[llength $children($v,$p)] == 1 &&
1548 [llength $parents($v,$id)] == 1} {
1549 set b $a
1550 } else {
1551 set b [newvarc $v $p]
1552 }
1553 set varcid($v,$p) $b
1554 if {[string compare [lindex $varctok($v) $b] $vtokmod($v)] < 0} {
1555 modify_arc $v $b
1556 }
1557 lappend varccommits($v,$b) $p
1558 incr commitidx($v)
1559 if {$v == $curview} {
1560 set numcommits $commitidx($v)
1561 }
1562 set scripts [check_interest $p $scripts]
1563 }
1564 }
1565 if {$missing_parents > 0} {
1566 foreach s $scripts {
1567 eval $s
1568 }
1569 }
1570}
1571
1572# Use $rwid as a substitute for $id, i.e. reparent $id's children to $rwid
1573# Assumes we already have an arc for $rwid.
1574proc rewrite_commit {v id rwid} {
1575 global children parents varcid varctok vtokmod varccommits
1576
1577 foreach ch $children($v,$id) {
1578 # make $rwid be $ch's parent in place of $id
1579 set i [lsearch -exact $parents($v,$ch) $id]
1580 if {$i < 0} {
1581 puts "oops rewrite_commit didn't find $id in parent list for $ch"
1582 }
1583 set parents($v,$ch) [lreplace $parents($v,$ch) $i $i $rwid]
1584 # add $ch to $rwid's children and sort the list if necessary
1585 if {[llength [lappend children($v,$rwid) $ch]] > 1} {
1586 set children($v,$rwid) [lsort -command [list vtokcmp $v] \
1587 $children($v,$rwid)]
1588 }
1589 # fix the graph after joining $id to $rwid
1590 set a $varcid($v,$ch)
1591 fix_reversal $rwid $a $v
1592 # parentlist is wrong for the last element of arc $a
1593 # even if displayorder is right, hence the 3rd arg here
1594 modify_arc $v $a [expr {[llength $varccommits($v,$a)] - 1}]
1595 }
1596}
1597
1598# Mechanism for registering a command to be executed when we come
1599# across a particular commit. To handle the case when only the
1600# prefix of the commit is known, the commitinterest array is now
1601# indexed by the first 4 characters of the ID. Each element is a
1602# list of id, cmd pairs.
1603proc interestedin {id cmd} {
1604 global commitinterest
1605
1606 lappend commitinterest([string range $id 0 3]) $id $cmd
1607}
1608
1609proc check_interest {id scripts} {
1610 global commitinterest
1611
1612 set prefix [string range $id 0 3]
1613 if {[info exists commitinterest($prefix)]} {
1614 set newlist {}
1615 foreach {i script} $commitinterest($prefix) {
1616 if {[string match "$i*" $id]} {
1617 lappend scripts [string map [list "%I" $id "%P" $i] $script]
1618 } else {
1619 lappend newlist $i $script
1620 }
1621 }
1622 if {$newlist ne {}} {
1623 set commitinterest($prefix) $newlist
1624 } else {
1625 unset commitinterest($prefix)
1626 }
1627 }
1628 return $scripts
1629}
1630
1631proc getcommitlines {fd inst view updating} {
1632 global cmitlisted leftover
1633 global commitidx commitdata vdatemode
1634 global parents children curview hlview
1635 global idpending ordertok
1636 global varccommits varcid varctok vtokmod vfilelimit vshortids
1637 global hashlength
1638
1639 set stuff [read $fd 500000]
1640 # git log doesn't terminate the last commit with a null...
1641 if {$stuff == {} && $leftover($inst) ne {} && [eof $fd]} {
1642 set stuff "\0"
1643 }
1644 if {$stuff == {}} {
1645 if {![eof $fd]} {
1646 return 1
1647 }
1648 global commfd viewcomplete viewactive viewname
1649 global viewinstances
1650 unset commfd($inst)
1651 set i [lsearch -exact $viewinstances($view) $inst]
1652 if {$i >= 0} {
1653 set viewinstances($view) [lreplace $viewinstances($view) $i $i]
1654 }
1655 # set it blocking so we wait for the process to terminate
1656 fconfigure $fd -blocking 1
1657 if {[catch {close $fd} err]} {
1658 set fv {}
1659 if {$view != $curview} {
1660 set fv " for the \"$viewname($view)\" view"
1661 }
1662 if {[string range $err 0 4] == "usage"} {
1663 set err "Gitk: error reading commits$fv:\
1664 bad arguments to git log."
1665 if {$viewname($view) eq [mc "Command line"]} {
1666 append err \
1667 " (Note: arguments to gitk are passed to git log\
1668 to allow selection of commits to be displayed.)"
1669 }
1670 } else {
1671 set err "Error reading commits$fv: $err"
1672 }
1673 error_popup $err
1674 }
1675 if {[incr viewactive($view) -1] <= 0} {
1676 set viewcomplete($view) 1
1677 # Check if we have seen any ids listed as parents that haven't
1678 # appeared in the list
1679 closevarcs $view
1680 notbusy $view
1681 }
1682 if {$view == $curview} {
1683 run chewcommits
1684 }
1685 return 0
1686 }
1687 set start 0
1688 set gotsome 0
1689 set scripts {}
1690 while 1 {
1691 set i [string first "\0" $stuff $start]
1692 if {$i < 0} {
1693 append leftover($inst) [string range $stuff $start end]
1694 break
1695 }
1696 if {$start == 0} {
1697 set cmit $leftover($inst)
1698 append cmit [string range $stuff 0 [expr {$i - 1}]]
1699 set leftover($inst) {}
1700 } else {
1701 set cmit [string range $stuff $start [expr {$i - 1}]]
1702 }
1703 set start [expr {$i + 1}]
1704 set j [string first "\n" $cmit]
1705 set ok 0
1706 set listed 1
1707 if {$j >= 0 && [string match "commit *" $cmit]} {
1708 set ids [string range $cmit 7 [expr {$j - 1}]]
1709 if {[string match {[-^<>]*} $ids]} {
1710 switch -- [string index $ids 0] {
1711 "-" {set listed 0}
1712 "^" {set listed 2}
1713 "<" {set listed 3}
1714 ">" {set listed 4}
1715 }
1716 set ids [string range $ids 1 end]
1717 }
1718 set ok 1
1719 foreach id $ids {
1720 if {[string length $id] != $hashlength} {
1721 set ok 0
1722 break
1723 }
1724 }
1725 }
1726 if {!$ok} {
1727 set shortcmit $cmit
1728 if {[string length $shortcmit] > 80} {
1729 set shortcmit "[string range $shortcmit 0 80]..."
1730 }
1731 error_popup "[mc "Can't parse git log output:"] {$shortcmit}"
1732 exit 1
1733 }
1734 set id [lindex $ids 0]
1735 set vid $view,$id
1736
1737 lappend vshortids($view,[string range $id 0 3]) $id
1738
1739 if {!$listed && $updating && ![info exists varcid($vid)] &&
1740 $vfilelimit($view) ne {}} {
1741 # git log doesn't rewrite parents for unlisted commits
1742 # when doing path limiting, so work around that here
1743 # by working out the rewritten parent with git rev-list
1744 # and if we already know about it, using the rewritten
1745 # parent as a substitute parent for $id's children.
1746 if {![catch {
1747 set rwid [safe_exec [list git rev-list --first-parent --max-count=1 \
1748 $id -- $vfilelimit($view)]]
1749 }]} {
1750 if {$rwid ne {} && [info exists varcid($view,$rwid)]} {
1751 # use $rwid in place of $id
1752 rewrite_commit $view $id $rwid
1753 continue
1754 }
1755 }
1756 }
1757
1758 set a 0
1759 if {[info exists varcid($vid)]} {
1760 if {$cmitlisted($vid) || !$listed} continue
1761 set a $varcid($vid)
1762 }
1763 if {$listed} {
1764 set olds [lrange $ids 1 end]
1765 } else {
1766 set olds {}
1767 }
1768 set commitdata($id) [string range $cmit [expr {$j + 1}] end]
1769 set cmitlisted($vid) $listed
1770 set parents($vid) $olds
1771 if {![info exists children($vid)]} {
1772 set children($vid) {}
1773 } elseif {$a == 0 && [llength $children($vid)] == 1} {
1774 set k [lindex $children($vid) 0]
1775 if {[llength $parents($view,$k)] == 1 &&
1776 (!$vdatemode($view) ||
1777 $varcid($view,$k) == [llength $varctok($view)] - 1)} {
1778 set a $varcid($view,$k)
1779 }
1780 }
1781 if {$a == 0} {
1782 # new arc
1783 set a [newvarc $view $id]
1784 }
1785 if {[string compare [lindex $varctok($view) $a] $vtokmod($view)] < 0} {
1786 modify_arc $view $a
1787 }
1788 if {![info exists varcid($vid)]} {
1789 set varcid($vid) $a
1790 lappend varccommits($view,$a) $id
1791 incr commitidx($view)
1792 }
1793
1794 set i 0
1795 foreach p $olds {
1796 if {$i == 0 || [lsearch -exact $olds $p] >= $i} {
1797 set vp $view,$p
1798 if {[llength [lappend children($vp) $id]] > 1 &&
1799 [vtokcmp $view [lindex $children($vp) end-1] $id] > 0} {
1800 set children($vp) [lsort -command [list vtokcmp $view] \
1801 $children($vp)]
1802 unset -nocomplain ordertok
1803 }
1804 if {[info exists varcid($view,$p)]} {
1805 fix_reversal $p $a $view
1806 }
1807 }
1808 incr i
1809 }
1810
1811 set scripts [check_interest $id $scripts]
1812 set gotsome 1
1813 }
1814 if {$gotsome} {
1815 global numcommits hlview
1816
1817 if {$view == $curview} {
1818 set numcommits $commitidx($view)
1819 run chewcommits
1820 }
1821 if {[info exists hlview] && $view == $hlview} {
1822 # we never actually get here...
1823 run vhighlightmore
1824 }
1825 foreach s $scripts {
1826 eval $s
1827 }
1828 }
1829 return 2
1830}
1831
1832proc chewcommits {} {
1833 global curview hlview viewcomplete
1834 global pending_select
1835
1836 layoutmore
1837 if {$viewcomplete($curview)} {
1838 global commitidx varctok
1839 global numcommits startmsecs
1840
1841 if {[info exists pending_select]} {
1842 update
1843 reset_pending_select {}
1844
1845 if {[commitinview $pending_select $curview]} {
1846 selectline [rowofcommit $pending_select] 1
1847 } else {
1848 set row [first_real_row]
1849 selectline $row 1
1850 }
1851 }
1852 if {$commitidx($curview) > 0} {
1853 #set ms [expr {[clock clicks -milliseconds] - $startmsecs}]
1854 #puts "overall $ms ms for $numcommits commits"
1855 #puts "[llength $varctok($view)] arcs, $commitidx($view) commits"
1856 } else {
1857 show_status [mc "No commits selected"]
1858 }
1859 notbusy layout
1860 }
1861 return 0
1862}
1863
1864proc do_readcommit {id} {
1865 global tclencoding
1866
1867 # Invoke git-log to handle automatic encoding conversion
1868 set fd [safe_open_command [concat git log --no-color --pretty=raw -1 $id]]
1869 # Read the results using i18n.logoutputencoding
1870 fconfigure $fd -translation lf -eofchar {}
1871 if {$tclencoding != {}} {
1872 fconfigure $fd -encoding $tclencoding
1873 }
1874 set contents [read $fd]
1875 close $fd
1876 # Remove the heading line
1877 regsub {^commit [0-9a-f]+\n} $contents {} contents
1878
1879 return $contents
1880}
1881
1882proc readcommit {id} {
1883 if {[catch {set contents [do_readcommit $id]}]} return
1884 parsecommit $id $contents 1
1885}
1886
1887proc parsecommit {id contents listed} {
1888 global commitinfo
1889
1890 set inhdr 1
1891 set comment {}
1892 set headline {}
1893 set auname {}
1894 set audate {}
1895 set comname {}
1896 set comdate {}
1897 set hdrend [string first "\n\n" $contents]
1898 if {$hdrend < 0} {
1899 # should never happen...
1900 set hdrend [string length $contents]
1901 }
1902 set header [string range $contents 0 [expr {$hdrend - 1}]]
1903 set comment [string range $contents [expr {$hdrend + 2}] end]
1904 foreach line [split $header "\n"] {
1905 set line [split $line " "]
1906 set tag [lindex $line 0]
1907 if {$tag == "author"} {
1908 set audate [lrange $line end-1 end]
1909 set auname [join [lrange $line 1 end-2] " "]
1910 } elseif {$tag == "committer"} {
1911 set comdate [lrange $line end-1 end]
1912 set comname [join [lrange $line 1 end-2] " "]
1913 }
1914 }
1915 set headline {}
1916 # take the first non-blank line of the comment as the headline
1917 set headline [string trimleft $comment]
1918 set i [string first "\n" $headline]
1919 if {$i >= 0} {
1920 set headline [string range $headline 0 $i]
1921 }
1922 set headline [string trimright $headline]
1923 set i [string first "\r" $headline]
1924 if {$i >= 0} {
1925 set headline [string trimright [string range $headline 0 $i]]
1926 }
1927 if {!$listed} {
1928 # git log indents the comment by 4 spaces;
1929 # if we got this via git cat-file, add the indentation
1930 set newcomment {}
1931 foreach line [split $comment "\n"] {
1932 append newcomment " "
1933 append newcomment $line
1934 append newcomment "\n"
1935 }
1936 set comment $newcomment
1937 }
1938 set hasnote [string first "\nNotes:\n" $contents]
1939 set diff ""
1940 # If there is diff output shown in the git-log stream, split it
1941 # out. But get rid of the empty line that always precedes the
1942 # diff.
1943 set i [string first "\n\ndiff" $comment]
1944 if {$i >= 0} {
1945 set diff [string range $comment $i+1 end]
1946 set comment [string range $comment 0 $i-1]
1947 }
1948 set commitinfo($id) [list $headline $auname $audate \
1949 $comname $comdate $comment $hasnote $diff]
1950}
1951
1952proc getcommit {id} {
1953 global commitdata commitinfo
1954
1955 if {[info exists commitdata($id)]} {
1956 parsecommit $id $commitdata($id) 1
1957 } else {
1958 readcommit $id
1959 if {![info exists commitinfo($id)]} {
1960 set commitinfo($id) [list [mc "No commit information available"]]
1961 }
1962 }
1963 return 1
1964}
1965
1966# Expand an abbreviated commit ID to a list of full 40-char (or 64-char
1967# for SHA256 repo) IDs that match and are present in the current view.
1968# This is fairly slow...
1969proc longid {prefix} {
1970 global varcid curview vshortids
1971
1972 set ids {}
1973 if {[string length $prefix] >= 4} {
1974 set vshortid $curview,[string range $prefix 0 3]
1975 if {[info exists vshortids($vshortid)]} {
1976 foreach id $vshortids($vshortid) {
1977 if {[string match "$prefix*" $id]} {
1978 if {[lsearch -exact $ids $id] < 0} {
1979 lappend ids $id
1980 if {[llength $ids] >= 2} break
1981 }
1982 }
1983 }
1984 }
1985 } else {
1986 foreach match [array names varcid "$curview,$prefix*"] {
1987 lappend ids [lindex [split $match ","] 1]
1988 if {[llength $ids] >= 2} break
1989 }
1990 }
1991 return $ids
1992}
1993
1994proc readrefs {} {
1995 global tagids idtags headids idheads tagobjid upstreamofref
1996 global otherrefids idotherrefs mainhead mainheadid
1997 global selecthead selectheadid
1998 global hideremotes
1999 global tclencoding
2000 global hashlength
2001
2002 foreach v {tagids idtags headids idheads otherrefids idotherrefs upstreamofref} {
2003 unset -nocomplain $v
2004 }
2005 set refd [safe_open_command [list git show-ref -d]]
2006 if {$tclencoding != {}} {
2007 fconfigure $refd -encoding $tclencoding
2008 }
2009 while {[gets $refd line] >= 0} {
2010 if {[string index $line $hashlength] ne " "} continue
2011 set id [string range $line 0 [expr {$hashlength - 1}]]
2012 set ref [string range $line [expr {$hashlength + 1}] end]
2013 if {![string match "refs/*" $ref]} continue
2014 set name [string range $ref 5 end]
2015 if {[string match "remotes/*" $name]} {
2016 if {![string match "*/HEAD" $name] && !$hideremotes} {
2017 set headids($name) $id
2018 lappend idheads($id) $name
2019 }
2020 } elseif {[string match "heads/*" $name]} {
2021 set name [string range $name 6 end]
2022 set headids($name) $id
2023 lappend idheads($id) $name
2024 } elseif {[string match "tags/*" $name]} {
2025 # this lets refs/tags/foo^{} overwrite refs/tags/foo,
2026 # which is what we want since the former is the commit ID
2027 set name [string range $name 5 end]
2028 if {[string match "*^{}" $name]} {
2029 set name [string range $name 0 end-3]
2030 } else {
2031 set tagobjid($name) $id
2032 }
2033 set tagids($name) $id
2034 lappend idtags($id) $name
2035 } else {
2036 set otherrefids($name) $id
2037 lappend idotherrefs($id) $name
2038 }
2039 }
2040 catch {close $refd}
2041 set mainhead {}
2042 set mainheadid {}
2043 catch {
2044 set mainheadid [exec git rev-parse HEAD]
2045 set thehead [exec git symbolic-ref HEAD]
2046 if {[string match "refs/heads/*" $thehead]} {
2047 set mainhead [string range $thehead 11 end]
2048 }
2049 }
2050 set selectheadid {}
2051 if {$selecthead ne {}} {
2052 catch {
2053 set selectheadid [safe_exec [list git rev-parse --verify $selecthead]]
2054 }
2055 }
2056 #load the local_branch->upstream mapping
2057 # the result of the for-each-ref command produces: local_branch NUL upstream
2058 set refd [safe_open_command [list git for-each-ref {--format=%(refname:short)%00%(upstream)} refs/heads/]]
2059 while {[gets $refd local_tracking] >= 0} {
2060 set line [split $local_tracking \0]
2061 if {[lindex $line 1] ne {}} {
2062 set upstream_ref [string map {"refs/" ""} [lindex $line 1]]
2063 set upstreamofref([lindex $line 0]) $upstream_ref
2064 }
2065 }
2066 catch {close $refd}
2067}
2068
2069# skip over fake commits
2070proc first_real_row {} {
2071 global nullid nullid2 numcommits
2072
2073 for {set row 0} {$row < $numcommits} {incr row} {
2074 set id [commitonrow $row]
2075 if {$id ne $nullid && $id ne $nullid2} {
2076 break
2077 }
2078 }
2079 return $row
2080}
2081
2082# update things for a head moved to a child of its previous location
2083proc movehead {id name} {
2084 global headids idheads
2085
2086 removehead $headids($name) $name
2087 set headids($name) $id
2088 lappend idheads($id) $name
2089}
2090
2091# update things when a head has been removed
2092proc removehead {id name} {
2093 global headids idheads
2094
2095 if {$idheads($id) eq $name} {
2096 unset idheads($id)
2097 } else {
2098 set i [lsearch -exact $idheads($id) $name]
2099 if {$i >= 0} {
2100 set idheads($id) [lreplace $idheads($id) $i $i]
2101 }
2102 }
2103 unset headids($name)
2104}
2105
2106proc ttk_toplevel {w args} {
2107 eval [linsert $args 0 ::toplevel $w]
2108 place [ttk::frame $w._toplevel_background] -x 0 -y 0 -relwidth 1 -relheight 1
2109 return $w
2110}
2111
2112proc make_transient {window origin} {
2113 wm transient $window $origin
2114
2115 # Windows fails to place transient windows normally, so
2116 # schedule a callback to center them on the parent.
2117 if {[tk windowingsystem] eq {win32}} {
2118 after idle [list tk::PlaceWindow $window widget $origin]
2119 }
2120}
2121
2122proc show_error {w top msg} {
2123 if {[wm state $top] eq "withdrawn"} { wm deiconify $top }
2124 message $w.m -text $msg -justify center -aspect 400
2125 pack $w.m -side top -fill x -padx 20 -pady 20
2126 ttk::button $w.ok -default active -text [mc OK] -command "destroy $top"
2127 pack $w.ok -side bottom -fill x
2128 bind $top <Visibility> "grab $top; focus $top"
2129 bind $top <Key-Return> "destroy $top"
2130 bind $top <Key-space> "destroy $top"
2131 bind $top <Key-Escape> "destroy $top"
2132 tkwait window $top
2133}
2134
2135proc error_popup {msg {owner .}} {
2136 if {[tk windowingsystem] eq "win32"} {
2137 tk_messageBox -icon error -type ok -title [wm title .] \
2138 -parent $owner -message $msg
2139 } else {
2140 set w .error
2141 ttk_toplevel $w
2142 make_transient $w $owner
2143 show_error $w $w $msg
2144 }
2145}
2146
2147proc confirm_popup {msg {owner .}} {
2148 global confirm_ok
2149 set confirm_ok 0
2150 set w .confirm
2151 ttk_toplevel $w
2152 make_transient $w $owner
2153 message $w.m -text $msg -justify center -aspect 400
2154 pack $w.m -side top -fill x -padx 20 -pady 20
2155 ttk::button $w.ok -text [mc OK] -command "set confirm_ok 1; destroy $w"
2156 pack $w.ok -side left -fill x
2157 ttk::button $w.cancel -text [mc Cancel] -command "destroy $w"
2158 pack $w.cancel -side right -fill x
2159 bind $w <Visibility> "grab $w; focus $w"
2160 bind $w <Key-Return> "set confirm_ok 1; destroy $w"
2161 bind $w <Key-space> "set confirm_ok 1; destroy $w"
2162 bind $w <Key-Escape> "destroy $w"
2163 tk::PlaceWindow $w widget $owner
2164 tkwait window $w
2165 return $confirm_ok
2166}
2167
2168proc haveselectionclipboard {} {
2169 return [expr {[tk windowingsystem] eq "x11"}]
2170}
2171
2172proc setoptions {} {
2173 if {[tk windowingsystem] ne "win32"} {
2174 option add *Panedwindow.showHandle 1 startupFile
2175 option add *Panedwindow.sashRelief raised startupFile
2176 if {[tk windowingsystem] ne "aqua"} {
2177 option add *Menu.font uifont startupFile
2178 }
2179 } else {
2180 option add *Menu.TearOff 0 startupFile
2181 }
2182 option add *Button.font uifont startupFile
2183 option add *Checkbutton.font uifont startupFile
2184 option add *Radiobutton.font uifont startupFile
2185 option add *Menubutton.font uifont startupFile
2186 option add *Label.font uifont startupFile
2187 option add *Message.font uifont startupFile
2188 option add *Entry.font textfont startupFile
2189 option add *Text.font textfont startupFile
2190 option add *Labelframe.font uifont startupFile
2191 option add *Spinbox.font textfont startupFile
2192 option add *Listbox.font mainfont startupFile
2193}
2194
2195proc setttkstyle {} {
2196 eval font configure TkDefaultFont [fontflags mainfont]
2197 eval font configure TkTextFont [fontflags textfont]
2198 eval font configure TkHeadingFont [fontflags mainfont]
2199 eval font configure TkCaptionFont [fontflags mainfont] -weight bold
2200 eval font configure TkTooltipFont [fontflags uifont]
2201 eval font configure TkFixedFont [fontflags textfont]
2202 eval font configure TkIconFont [fontflags uifont]
2203 eval font configure TkMenuFont [fontflags uifont]
2204 eval font configure TkSmallCaptionFont [fontflags uifont]
2205}
2206
2207# Make a menu and submenus.
2208# m is the window name for the menu, items is the list of menu items to add.
2209# Each item is a list {mc label type description options...}
2210# mc is ignored; it's so we can put mc there to alert xgettext
2211# label is the string that appears in the menu
2212# type is cascade, command or radiobutton (should add checkbutton)
2213# description depends on type; it's the sublist for cascade, the
2214# command to invoke for command, or {variable value} for radiobutton
2215proc makemenu {m items} {
2216 menu $m
2217 if {[tk windowingsystem] eq {aqua}} {
2218 set Meta1 Cmd
2219 } else {
2220 set Meta1 Ctrl
2221 }
2222 foreach i $items {
2223 set name [mc [lindex $i 1]]
2224 set type [lindex $i 2]
2225 set thing [lindex $i 3]
2226 set params [list $type]
2227 if {$name ne {}} {
2228 set u [string first "&" [string map {&& x} $name]]
2229 lappend params -label [string map {&& & & {}} $name]
2230 if {$u >= 0} {
2231 lappend params -underline $u
2232 }
2233 }
2234 switch -- $type {
2235 "cascade" {
2236 set submenu [string tolower [string map {& ""} [lindex $i 1]]]
2237 lappend params -menu $m.$submenu
2238 }
2239 "command" {
2240 lappend params -command $thing
2241 }
2242 "radiobutton" {
2243 lappend params -variable [lindex $thing 0] \
2244 -value [lindex $thing 1]
2245 }
2246 }
2247 set tail [lrange $i 4 end]
2248 regsub -all {\yMeta1\y} $tail $Meta1 tail
2249 eval $m add $params $tail
2250 if {$type eq "cascade"} {
2251 makemenu $m.$submenu $thing
2252 }
2253 }
2254}
2255
2256# translate string and remove ampersands
2257proc mca {str} {
2258 return [string map {&& & & {}} [mc $str]]
2259}
2260
2261proc cleardropsel {w} {
2262 $w selection clear
2263}
2264proc makedroplist {w varname args} {
2265 set width 0
2266 foreach label $args {
2267 set cx [string length $label]
2268 if {$cx > $width} {set width $cx}
2269 }
2270 set gm [ttk::combobox $w -width $width -state readonly\
2271 -textvariable $varname -values $args \
2272 -exportselection false]
2273 bind $gm <<ComboboxSelected>> [list $gm selection clear]
2274 return $gm
2275}
2276
2277proc scrollval {D {koff 0}} {
2278 global kscroll scroll_D0
2279 return [expr int(-($D / $scroll_D0) * max(1, $kscroll-$koff))]
2280}
2281
2282proc bind_mousewheel {} {
2283 global canv cflist ctext
2284 bindall <MouseWheel> {allcanvs yview scroll [scrollval %D] units}
2285 bindall <Shift-MouseWheel> break
2286 bind $ctext <MouseWheel> {$ctext yview scroll [scrollval %D 2] units}
2287 bind $ctext <Shift-MouseWheel> {$ctext xview scroll [scrollval %D 2] units}
2288 bind $cflist <MouseWheel> {$cflist yview scroll [scrollval %D 2] units}
2289 bind $cflist <Shift-MouseWheel> break
2290 bind $canv <Shift-MouseWheel> {$canv xview scroll [scrollval %D] units}
2291}
2292
2293proc bind_mousewheel_buttons {} {
2294 global canv cflist ctext
2295 bindall <ButtonRelease-4> {allcanvs yview scroll [scrollval 1] units}
2296 bindall <ButtonRelease-5> {allcanvs yview scroll [scrollval -1] units}
2297 bindall <Shift-ButtonRelease-4> break
2298 bindall <Shift-ButtonRelease-5> break
2299 bind $ctext <ButtonRelease-4> {$ctext yview scroll [scrollval 1 2] units}
2300 bind $ctext <ButtonRelease-5> {$ctext yview scroll [scrollval -1 2] units}
2301 bind $ctext <Shift-ButtonRelease-4> {$ctext xview scroll [scrollval 1 2] units}
2302 bind $ctext <Shift-ButtonRelease-5> {$ctext xview scroll [scrollval -1 2] units}
2303 bind $cflist <ButtonRelease-4> {$cflist yview scroll [scrollval 1 2] units}
2304 bind $cflist <ButtonRelease-5> {$cflist yview scroll [scrollval -1 2] units}
2305 bind $cflist <Shift-ButtonRelease-4> break
2306 bind $cflist <Shift-ButtonRelease-5> break
2307 bind $canv <Shift-ButtonRelease-4> {$canv xview scroll [scrollval 1] units}
2308 bind $canv <Shift-ButtonRelease-5> {$canv xview scroll [scrollval -1] units}
2309}
2310
2311proc makewindow {} {
2312 global canv canv2 canv3 linespc charspc ctext cflist cscroll
2313 global tabstop
2314 global findtype findtypemenu findloc findstring fstring geometry
2315 global entries sha1entry sha1string sha1but
2316 global diffcontextstring diffcontext
2317 global ignorespace
2318 global maincursor textcursor curtextcursor
2319 global rowctxmenu fakerowmenu mergemax wrapcomment wrapdefault
2320 global highlight_files gdttype
2321 global searchstring sstring
2322 global bgcolor fgcolor bglist fglist diffcolors diffbgcolors selectbgcolor
2323 global uifgcolor uifgdisabledcolor
2324 global filesepbgcolor filesepfgcolor
2325 global mergecolors foundbgcolor currentsearchhitbgcolor
2326 global headctxmenu progresscanv progressitem progresscoords statusw
2327 global fprogitem fprogcoord lastprogupdate progupdatepending
2328 global rprogitem rprogcoord rownumsel numcommits
2329 global worddiff
2330 global hashlength scroll_D0
2331
2332 # The "mc" arguments here are purely so that xgettext
2333 # sees the following string as needing to be translated
2334 set file {
2335 mc "&File" cascade {
2336 {mc "&Update" command updatecommits -accelerator F5}
2337 {mc "&Reload" command reloadcommits -accelerator Shift-F5}
2338 {mc "Reread re&ferences" command rereadrefs}
2339 {mc "&List references" command showrefs -accelerator F2}
2340 {xx "" separator}
2341 {mc "Start git &gui" command {safe_exec_redirect [list git gui] [list &]}}
2342 {xx "" separator}
2343 {mc "&Quit" command doquit -accelerator Meta1-Q}
2344 }}
2345 set edit {
2346 mc "&Edit" cascade {
2347 {mc "&Preferences" command doprefs}
2348 }}
2349 set view {
2350 mc "&View" cascade {
2351 {mc "&New view..." command {newview 0} -accelerator Shift-F4}
2352 {mc "&Edit view..." command editview -state disabled -accelerator F4}
2353 {mc "&Delete view" command delview -state disabled}
2354 {xx "" separator}
2355 {mc "&All files" radiobutton {selectedview 0} -command {showview 0}}
2356 }}
2357 if {[tk windowingsystem] ne "aqua"} {
2358 set help {
2359 mc "&Help" cascade {
2360 {mc "&About gitk" command about}
2361 {mc "&Key bindings" command keys}
2362 }}
2363 set bar [list $file $edit $view $help]
2364 } else {
2365 proc ::tk::mac::ShowPreferences {} {doprefs}
2366 proc ::tk::mac::Quit {} {doquit}
2367 lset file end [lreplace [lindex $file end] end-1 end]
2368 set apple {
2369 xx "&Apple" cascade {
2370 {mc "&About gitk" command about}
2371 {xx "" separator}
2372 }}
2373 set help {
2374 mc "&Help" cascade {
2375 {mc "&Key bindings" command keys}
2376 }}
2377 set bar [list $apple $file $view $help]
2378 }
2379 makemenu .bar $bar
2380 . configure -menu .bar
2381
2382 # cover the non-themed toplevel with a themed frame.
2383 place [ttk::frame ._main_background] -x 0 -y 0 -relwidth 1 -relheight 1
2384
2385 # the gui has upper and lower half, parts of a paned window.
2386 ttk::panedwindow .ctop -orient vertical
2387
2388 # possibly use assumed geometry
2389 if {![info exists geometry(pwsash0)]} {
2390 set geometry(topheight) [expr {15 * $linespc}]
2391 set geometry(topwidth) [expr {80 * $charspc}]
2392 set geometry(botheight) [expr {15 * $linespc}]
2393 set geometry(botwidth) [expr {50 * $charspc}]
2394 set geometry(pwsash0) [list [expr {40 * $charspc}] 2]
2395 set geometry(pwsash1) [list [expr {60 * $charspc}] 2]
2396 }
2397
2398 # the upper half will have a paned window, a scroll bar to the right, and some stuff below
2399 ttk::frame .tf -height $geometry(topheight) -width $geometry(topwidth)
2400 ttk::frame .tf.histframe
2401 ttk::panedwindow .tf.histframe.pwclist -orient horizontal
2402
2403 # create three canvases
2404 set cscroll .tf.histframe.csb
2405 set canv .tf.histframe.pwclist.canv
2406 canvas $canv \
2407 -selectbackground $selectbgcolor \
2408 -background $bgcolor -bd 0 \
2409 -xscrollincr $linespc \
2410 -yscrollincr $linespc -yscrollcommand "scrollcanv $cscroll"
2411 .tf.histframe.pwclist add $canv
2412 set canv2 .tf.histframe.pwclist.canv2
2413 canvas $canv2 \
2414 -selectbackground $selectbgcolor \
2415 -background $bgcolor -bd 0 -yscrollincr $linespc
2416 .tf.histframe.pwclist add $canv2
2417 set canv3 .tf.histframe.pwclist.canv3
2418 canvas $canv3 \
2419 -selectbackground $selectbgcolor \
2420 -background $bgcolor -bd 0 -yscrollincr $linespc
2421 .tf.histframe.pwclist add $canv3
2422 bind .tf.histframe.pwclist <Map> {
2423 bind %W <Map> {}
2424 .tf.histframe.pwclist sashpos 1 [lindex $::geometry(pwsash1) 0]
2425 .tf.histframe.pwclist sashpos 0 [lindex $::geometry(pwsash0) 0]
2426 }
2427
2428 # a scroll bar to rule them
2429 ttk::scrollbar $cscroll -command {allcanvs yview}
2430 pack $cscroll -side right -fill y
2431 bind .tf.histframe.pwclist <Configure> {resizeclistpanes %W %w}
2432 lappend bglist $canv $canv2 $canv3
2433 pack .tf.histframe.pwclist -fill both -expand 1 -side left
2434
2435 # we have two button bars at bottom of top frame. Bar 1
2436 ttk::frame .tf.bar
2437 ttk::frame .tf.lbar -height 15
2438
2439 set sha1entry .tf.bar.sha1
2440 set entries $sha1entry
2441 set sha1but .tf.bar.sha1label
2442 button $sha1but -text "[mc "Commit ID:"] " -state disabled -relief flat \
2443 -command gotocommit -width 8
2444 $sha1but conf -disabledforeground [$sha1but cget -foreground]
2445 pack .tf.bar.sha1label -side left
2446 ttk::entry $sha1entry -width $hashlength -font textfont -textvariable sha1string
2447 trace add variable sha1string write sha1change
2448 pack $sha1entry -side left -pady 2
2449
2450 set bm_left_data {
2451 #define left_width 16
2452 #define left_height 16
2453 static unsigned char left_bits[] = {
2454 0x00, 0x00, 0xc0, 0x01, 0xe0, 0x00, 0x70, 0x00, 0x38, 0x00, 0x1c, 0x00,
2455 0x0e, 0x00, 0xff, 0x7f, 0xff, 0x7f, 0xff, 0x7f, 0x0e, 0x00, 0x1c, 0x00,
2456 0x38, 0x00, 0x70, 0x00, 0xe0, 0x00, 0xc0, 0x01};
2457 }
2458 set bm_right_data {
2459 #define right_width 16
2460 #define right_height 16
2461 static unsigned char right_bits[] = {
2462 0x00, 0x00, 0xc0, 0x01, 0x80, 0x03, 0x00, 0x07, 0x00, 0x0e, 0x00, 0x1c,
2463 0x00, 0x38, 0xff, 0x7f, 0xff, 0x7f, 0xff, 0x7f, 0x00, 0x38, 0x00, 0x1c,
2464 0x00, 0x0e, 0x00, 0x07, 0x80, 0x03, 0xc0, 0x01};
2465 }
2466 image create bitmap bm-left -data $bm_left_data -foreground $uifgcolor
2467 image create bitmap bm-left-gray -data $bm_left_data -foreground $uifgdisabledcolor
2468 image create bitmap bm-right -data $bm_right_data -foreground $uifgcolor
2469 image create bitmap bm-right-gray -data $bm_right_data -foreground $uifgdisabledcolor
2470
2471 ttk::button .tf.bar.leftbut -command goback -state disabled -width 26
2472 .tf.bar.leftbut configure -image [list bm-left disabled bm-left-gray]
2473 pack .tf.bar.leftbut -side left -fill y
2474 ttk::button .tf.bar.rightbut -command goforw -state disabled -width 26
2475 .tf.bar.rightbut configure -image [list bm-right disabled bm-right-gray]
2476 pack .tf.bar.rightbut -side left -fill y
2477
2478 ttk::label .tf.bar.rowlabel -text [mc "Row"]
2479 set rownumsel {}
2480 ttk::label .tf.bar.rownum -width 7 -textvariable rownumsel \
2481 -relief sunken -anchor e
2482 ttk::label .tf.bar.rowlabel2 -text "/"
2483 ttk::label .tf.bar.numcommits -width 7 -textvariable numcommits \
2484 -relief sunken -anchor e
2485 pack .tf.bar.rowlabel .tf.bar.rownum .tf.bar.rowlabel2 .tf.bar.numcommits \
2486 -side left
2487 global selectedline
2488 trace add variable selectedline write selectedline_change
2489
2490 # Status label and progress bar
2491 set statusw .tf.bar.status
2492 ttk::label $statusw -width 15 -relief sunken
2493 pack $statusw -side left -padx 5
2494 set progresscanv [ttk::progressbar .tf.bar.progress]
2495 pack $progresscanv -side right -expand 1 -fill x -padx {0 2}
2496 set progresscoords {0 0}
2497 set fprogcoord 0
2498 set rprogcoord 0
2499 bind $progresscanv <Configure> adjustprogress
2500 set lastprogupdate [clock clicks -milliseconds]
2501 set progupdatepending 0
2502
2503 # build up the bottom bar of upper window
2504 ttk::label .tf.lbar.flabel -text "[mc "Find"] "
2505
2506 set bm_down_data {
2507 #define down_width 16
2508 #define down_height 16
2509 static unsigned char down_bits[] = {
2510 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01,
2511 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01,
2512 0x87, 0xe1, 0x8e, 0x71, 0x9c, 0x39, 0xb8, 0x1d,
2513 0xf0, 0x0f, 0xe0, 0x07, 0xc0, 0x03, 0x80, 0x01};
2514 }
2515 image create bitmap bm-down -data $bm_down_data -foreground $uifgcolor
2516 ttk::button .tf.lbar.fnext -width 26 -command {dofind 1 1}
2517 .tf.lbar.fnext configure -image bm-down
2518
2519 set bm_up_data {
2520 #define up_width 16
2521 #define up_height 16
2522 static unsigned char up_bits[] = {
2523 0x80, 0x01, 0xc0, 0x03, 0xe0, 0x07, 0xf0, 0x0f,
2524 0xb8, 0x1d, 0x9c, 0x39, 0x8e, 0x71, 0x87, 0xe1,
2525 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01,
2526 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01};
2527 }
2528 image create bitmap bm-up -data $bm_up_data -foreground $uifgcolor
2529 ttk::button .tf.lbar.fprev -width 26 -command {dofind -1 1}
2530 .tf.lbar.fprev configure -image bm-up
2531
2532 ttk::label .tf.lbar.flab2 -text " [mc "commit"] "
2533
2534 pack .tf.lbar.flabel .tf.lbar.fnext .tf.lbar.fprev .tf.lbar.flab2 \
2535 -side left -fill y
2536 set gdttype [mc "containing:"]
2537 set gm [makedroplist .tf.lbar.gdttype gdttype \
2538 [mc "containing:"] \
2539 [mc "touching paths:"] \
2540 [mc "adding/removing string:"] \
2541 [mc "changing lines matching:"]]
2542 trace add variable gdttype write gdttype_change
2543 pack .tf.lbar.gdttype -side left -fill y
2544
2545 set findstring {}
2546 set fstring .tf.lbar.findstring
2547 lappend entries $fstring
2548 ttk::entry $fstring -width 30 -textvariable findstring
2549 trace add variable findstring write find_change
2550 set findtype [mc "Exact"]
2551 set findtypemenu [makedroplist .tf.lbar.findtype \
2552 findtype [mc "Exact"] [mc "IgnCase"] [mc "Regexp"]]
2553 trace add variable findtype write findcom_change
2554 set findloc [mc "All fields"]
2555 makedroplist .tf.lbar.findloc findloc [mc "All fields"] [mc "Headline"] \
2556 [mc "Comments"] [mc "Author"] [mc "Committer"]
2557 trace add variable findloc write find_change
2558 pack .tf.lbar.findloc -side right
2559 pack .tf.lbar.findtype -side right
2560 pack $fstring -side left -expand 1 -fill x
2561
2562 # Finish putting the upper half of the viewer together
2563 pack .tf.lbar -in .tf -side bottom -fill x
2564 pack .tf.bar -in .tf -side bottom -fill x
2565 pack .tf.histframe -fill both -side top -expand 1
2566 .ctop add .tf
2567
2568 # now build up the bottom
2569 ttk::panedwindow .pwbottom -orient horizontal
2570
2571 # lower left, a text box over search bar, scroll bar to the right
2572 # if we know window height, then that will set the lower text height, otherwise
2573 # we set lower text height which will drive window height
2574 if {[info exists geometry(main)]} {
2575 ttk::frame .bleft -width $geometry(botwidth)
2576 } else {
2577 ttk::frame .bleft -width $geometry(botwidth) -height $geometry(botheight)
2578 }
2579 ttk::frame .bleft.top
2580 ttk::frame .bleft.mid
2581 ttk::frame .bleft.bottom
2582
2583 # gap between sub-widgets
2584 set wgap [font measure uifont "i"]
2585
2586 ttk::button .bleft.top.search -text [mc "Search"] -command dosearch
2587 pack .bleft.top.search -side left -padx 5
2588 set sstring .bleft.top.sstring
2589 set searchstring ""
2590 ttk::entry $sstring -width 20 -textvariable searchstring
2591 lappend entries $sstring
2592 trace add variable searchstring write incrsearch
2593 pack $sstring -side left -expand 1 -fill x
2594 ttk::radiobutton .bleft.mid.diff -text [mc "Diff"] \
2595 -command changediffdisp -variable diffelide -value {0 0}
2596 ttk::radiobutton .bleft.mid.old -text [mc "Old version"] \
2597 -command changediffdisp -variable diffelide -value {0 1}
2598 ttk::radiobutton .bleft.mid.new -text [mc "New version"] \
2599 -command changediffdisp -variable diffelide -value {1 0}
2600
2601 ttk::label .bleft.mid.labeldiffcontext -text " [mc "Lines of context"]: "
2602 pack .bleft.mid.diff .bleft.mid.old .bleft.mid.new -side left -ipadx $wgap
2603 spinbox .bleft.mid.diffcontext -width 5 \
2604 -from 0 -increment 1 -to 10000000 \
2605 -validate all -validatecommand "diffcontextvalidate %P" \
2606 -textvariable diffcontextstring
2607 .bleft.mid.diffcontext set $diffcontext
2608 trace add variable diffcontextstring write diffcontextchange
2609 lappend entries .bleft.mid.diffcontext
2610 pack .bleft.mid.labeldiffcontext .bleft.mid.diffcontext -side left -ipadx $wgap
2611 ttk::checkbutton .bleft.mid.ignspace -text [mc "Ignore space change"] \
2612 -command changeignorespace -variable ignorespace
2613 pack .bleft.mid.ignspace -side left -padx 5
2614
2615 set worddiff [mc "Line diff"]
2616 makedroplist .bleft.mid.worddiff worddiff [mc "Line diff"] \
2617 [mc "Markup words"] [mc "Color words"]
2618 trace add variable worddiff write changeworddiff
2619 pack .bleft.mid.worddiff -side left -padx 5
2620
2621 set ctext .bleft.bottom.ctext
2622 text $ctext -background $bgcolor -foreground $fgcolor \
2623 -state disabled -undo 0 -font textfont \
2624 -yscrollcommand scrolltext -wrap $wrapdefault \
2625 -xscrollcommand ".bleft.bottom.sbhorizontal set"
2626 $ctext conf -tabstyle wordprocessor
2627 ttk::scrollbar .bleft.bottom.sb -command "$ctext yview"
2628 ttk::scrollbar .bleft.bottom.sbhorizontal -command "$ctext xview" -orient h
2629 pack .bleft.top -side top -fill x
2630 pack .bleft.mid -side top -fill x
2631 grid $ctext .bleft.bottom.sb -sticky nsew
2632 grid .bleft.bottom.sbhorizontal -sticky ew
2633 grid columnconfigure .bleft.bottom 0 -weight 1
2634 grid rowconfigure .bleft.bottom 0 -weight 1
2635 grid rowconfigure .bleft.bottom 1 -weight 0
2636 pack .bleft.bottom -side top -fill both -expand 1
2637 lappend bglist $ctext
2638 lappend fglist $ctext
2639
2640 $ctext tag conf comment -wrap $wrapcomment
2641 $ctext tag conf filesep -font textfontbold -fore $filesepfgcolor -back $filesepbgcolor
2642 $ctext tag conf hunksep -fore [lindex $diffcolors 2]
2643 $ctext tag conf d0 -fore [lindex $diffcolors 0]
2644 $ctext tag conf d0 -back [lindex $diffbgcolors 0]
2645 $ctext tag conf dresult -fore [lindex $diffcolors 1]
2646 $ctext tag conf dresult -back [lindex $diffbgcolors 1]
2647 $ctext tag conf m0 -fore [lindex $mergecolors 0]
2648 $ctext tag conf m1 -fore [lindex $mergecolors 1]
2649 $ctext tag conf m2 -fore [lindex $mergecolors 2]
2650 $ctext tag conf m3 -fore [lindex $mergecolors 3]
2651 $ctext tag conf m4 -fore [lindex $mergecolors 4]
2652 $ctext tag conf m5 -fore [lindex $mergecolors 5]
2653 $ctext tag conf m6 -fore [lindex $mergecolors 6]
2654 $ctext tag conf m7 -fore [lindex $mergecolors 7]
2655 $ctext tag conf m8 -fore [lindex $mergecolors 8]
2656 $ctext tag conf m9 -fore [lindex $mergecolors 9]
2657 $ctext tag conf m10 -fore [lindex $mergecolors 10]
2658 $ctext tag conf m11 -fore [lindex $mergecolors 11]
2659 $ctext tag conf m12 -fore [lindex $mergecolors 12]
2660 $ctext tag conf m13 -fore [lindex $mergecolors 13]
2661 $ctext tag conf m14 -fore [lindex $mergecolors 14]
2662 $ctext tag conf m15 -fore [lindex $mergecolors 15]
2663 $ctext tag conf mmax -fore darkgrey
2664 set mergemax 16
2665 $ctext tag conf mresult -font textfontbold
2666 $ctext tag conf msep -font textfontbold
2667 $ctext tag conf found -back $foundbgcolor
2668 $ctext tag conf currentsearchhit -back $currentsearchhitbgcolor
2669 $ctext tag conf wwrap -wrap word -lmargin2 1c
2670 $ctext tag conf bold -font textfontbold
2671 # set these to the lowest priority:
2672 $ctext tag lower currentsearchhit
2673 $ctext tag lower found
2674 $ctext tag lower filesep
2675 $ctext tag lower dresult
2676 $ctext tag lower d0
2677
2678 .pwbottom add .bleft
2679
2680 # lower right
2681 ttk::frame .bright
2682 ttk::frame .bright.mode
2683 ttk::radiobutton .bright.mode.patch -text [mc "Patch"] \
2684 -command reselectline -variable cmitmode -value "patch"
2685 ttk::radiobutton .bright.mode.tree -text [mc "Tree"] \
2686 -command reselectline -variable cmitmode -value "tree"
2687 grid .bright.mode.patch .bright.mode.tree -sticky ew
2688 pack .bright.mode -side top -fill x
2689 set cflist .bright.cfiles
2690 set indent [font measure mainfont "nn"]
2691 text $cflist \
2692 -selectbackground $selectbgcolor \
2693 -background $bgcolor -foreground $fgcolor \
2694 -font mainfont \
2695 -tabs [list $indent [expr {2 * $indent}]] \
2696 -yscrollcommand ".bright.sb set" \
2697 -cursor [. cget -cursor] \
2698 -spacing1 1 -spacing3 1
2699 lappend bglist $cflist
2700 lappend fglist $cflist
2701 ttk::scrollbar .bright.sb -command "$cflist yview"
2702 pack .bright.sb -side right -fill y
2703 pack $cflist -side left -fill both -expand 1
2704 $cflist tag configure highlight \
2705 -background [$cflist cget -selectbackground]
2706 $cflist tag configure bold -font mainfontbold
2707
2708 .pwbottom add .bright
2709 .ctop add .pwbottom
2710
2711 # restore window width & height if known
2712 if {[info exists geometry(main)]} {
2713 if {[scan $geometry(main) "%dx%d" w h] >= 2} {
2714 if {$w > [winfo screenwidth .]} {
2715 set w [winfo screenwidth .]
2716 }
2717 if {$h > [winfo screenheight .]} {
2718 set h [winfo screenheight .]
2719 }
2720 wm geometry . "${w}x$h"
2721 }
2722 }
2723
2724 if {[info exists geometry(state)] && $geometry(state) eq "zoomed"} {
2725 wm state . $geometry(state)
2726 }
2727
2728 if {[tk windowingsystem] eq {aqua}} {
2729 set M1B M1
2730 set ::BM "3"
2731 } else {
2732 set M1B Control
2733 set ::BM "2"
2734 }
2735
2736 bind .ctop <Map> {
2737 bind %W <Map> {}
2738 %W sashpos 0 $::geometry(topheight)
2739 }
2740 bind .pwbottom <Map> {
2741 bind %W <Map> {}
2742 %W sashpos 0 $::geometry(botwidth)
2743 }
2744 bind .pwbottom <Configure> {resizecdetpanes %W %w}
2745
2746 pack .ctop -fill both -expand 1
2747 bindall <1> {selcanvline %W %x %y}
2748
2749 #Mouse / touchpad scrolling
2750 if {[tk windowingsystem] == "win32"} {
2751 set scroll_D0 120
2752 bind_mousewheel
2753 } elseif {[tk windowingsystem] == "x11"} {
2754 set scroll_D0 1
2755 bind_mousewheel_buttons
2756 } elseif {[tk windowingsystem] == "aqua"} {
2757 set scroll_D0 1
2758 bind_mousewheel
2759 } else {
2760 puts stderr [mc "Unknown windowing system, cannot bind mouse"]
2761 }
2762 bindall <$::BM> "canvscan mark %W %x %y"
2763 bindall <B$::BM-Motion> "canvscan dragto %W %x %y"
2764 bind all <$M1B-Key-w> {destroy [winfo toplevel %W]}
2765 bind . <$M1B-Key-w> doquit
2766 bindkey <Home> selfirstline
2767 bindkey <End> sellastline
2768 bind . <Key-Up> "selnextline -1"
2769 bind . <Key-Down> "selnextline 1"
2770 bind . <Shift-Key-Up> "dofind -1 0"
2771 bind . <Shift-Key-Down> "dofind 1 0"
2772 bindkey <<NextChar>> "goforw"
2773 bindkey <<PrevChar>> "goback"
2774 bind . <Key-Prior> "selnextpage -1"
2775 bind . <Key-Next> "selnextpage 1"
2776 bind . <$M1B-Home> "allcanvs yview moveto 0.0"
2777 bind . <$M1B-End> "allcanvs yview moveto 1.0"
2778 bind . <$M1B-Key-Up> "allcanvs yview scroll -1 units"
2779 bind . <$M1B-Key-Down> "allcanvs yview scroll 1 units"
2780 bind . <$M1B-Key-Prior> "allcanvs yview scroll -1 pages"
2781 bind . <$M1B-Key-Next> "allcanvs yview scroll 1 pages"
2782 bindkey <Key-Delete> "$ctext yview scroll -1 pages"
2783 bindkey <Key-BackSpace> "$ctext yview scroll -1 pages"
2784 bindkey <Key-space> "$ctext yview scroll 1 pages"
2785 bindkey p "selnextline -1"
2786 bindkey n "selnextline 1"
2787 bindkey z "goback"
2788 bindkey x "goforw"
2789 bindkey k "selnextline -1"
2790 bindkey j "selnextline 1"
2791 bindkey h "goback"
2792 bindkey l "goforw"
2793 bindkey b prevfile
2794 bindkey d "$ctext yview scroll 18 units"
2795 bindkey u "$ctext yview scroll -18 units"
2796 bindkey g {$sha1entry delete 0 end; focus $sha1entry}
2797 bindkey / {focus $fstring}
2798 bindkey <Key-KP_Divide> {focus $fstring}
2799 bindkey <Key-Return> {dofind 1 1}
2800 bindkey ? {dofind -1 1}
2801 bindkey f nextfile
2802 bind . <F5> updatecommits
2803 bindmodfunctionkey Shift 5 reloadcommits
2804 bind . <F2> showrefs
2805 bindmodfunctionkey Shift 4 {newview 0}
2806 bind . <F4> edit_or_newview
2807 bind . <$M1B-q> doquit
2808 bind . <$M1B-f> {dofind 1 1}
2809 bind . <$M1B-g> {dofind 1 0}
2810 bind . <$M1B-r> dosearchback
2811 bind . <$M1B-s> dosearch
2812 bind . <$M1B-equal> {incrfont 1}
2813 bind . <$M1B-plus> {incrfont 1}
2814 bind . <$M1B-KP_Add> {incrfont 1}
2815 bind . <$M1B-minus> {incrfont -1}
2816 bind . <$M1B-KP_Subtract> {incrfont -1}
2817 wm protocol . WM_DELETE_WINDOW doquit
2818 bind . <Destroy> {stop_backends}
2819 bind . <Button-1> "click %W"
2820 bind $fstring <Key-Return> {dofind 1 1}
2821 bind $sha1entry <Key-Return> {gotocommit; break}
2822 bind $sha1entry <<PasteSelection>> clearsha1
2823 bind $sha1entry <<Paste>> clearsha1
2824 bind $cflist <1> {sel_flist %W %x %y; break}
2825 bind $cflist <B1-Motion> {sel_flist %W %x %y; break}
2826 bind $cflist <ButtonRelease-1> {treeclick %W %x %y}
2827 global ctxbut
2828 bind $cflist $ctxbut {pop_flist_menu %W %X %Y %x %y}
2829 bind $ctext $ctxbut {pop_diff_menu %W %X %Y %x %y}
2830 bind $ctext <Button-1> {focus %W}
2831 bind $ctext <<Selection>> rehighlight_search_results
2832 for {set i 1} {$i < 10} {incr i} {
2833 bind . <$M1B-Key-$i> [list go_to_parent $i]
2834 }
2835
2836 set maincursor [. cget -cursor]
2837 set textcursor [$ctext cget -cursor]
2838 set curtextcursor $textcursor
2839
2840 set rowctxmenu .rowctxmenu
2841 makemenu $rowctxmenu {
2842 {mc "Diff this -> selected" command {diffvssel 0}}
2843 {mc "Diff selected -> this" command {diffvssel 1}}
2844 {mc "Make patch" command mkpatch}
2845 {mc "Create tag" command mktag}
2846 {mc "Copy commit reference" command copyreference}
2847 {mc "Write commit to file" command writecommit}
2848 {mc "Create new branch" command mkbranch}
2849 {mc "Cherry-pick this commit" command cherrypick}
2850 {mc "Reset HEAD branch to here" command resethead}
2851 {mc "Mark this commit" command markhere}
2852 {mc "Return to mark" command gotomark}
2853 {mc "Find descendant of this and mark" command find_common_desc}
2854 {mc "Compare with marked commit" command compare_commits}
2855 {mc "Diff this -> marked commit" command {diffvsmark 0}}
2856 {mc "Diff marked commit -> this" command {diffvsmark 1}}
2857 {mc "Revert this commit" command revert}
2858 }
2859 $rowctxmenu configure -tearoff 0
2860
2861 set fakerowmenu .fakerowmenu
2862 makemenu $fakerowmenu {
2863 {mc "Diff this -> selected" command {diffvssel 0}}
2864 {mc "Diff selected -> this" command {diffvssel 1}}
2865 {mc "Make patch" command mkpatch}
2866 {mc "Diff this -> marked commit" command {diffvsmark 0}}
2867 {mc "Diff marked commit -> this" command {diffvsmark 1}}
2868 }
2869 $fakerowmenu configure -tearoff 0
2870
2871 set headctxmenu .headctxmenu
2872 makemenu $headctxmenu {
2873 {mc "Check out this branch" command cobranch}
2874 {mc "Rename this branch" command mvbranch}
2875 {mc "Remove this branch" command rmbranch}
2876 {mc "Copy branch name" command {clipboard clear; clipboard append $headmenuhead}}
2877 }
2878 $headctxmenu configure -tearoff 0
2879
2880 global flist_menu
2881 set flist_menu .flistctxmenu
2882 makemenu $flist_menu {
2883 {mc "Highlight this too" command {flist_hl 0}}
2884 {mc "Highlight this only" command {flist_hl 1}}
2885 {mc "External diff" command {external_diff}}
2886 {mc "Blame parent commit" command {external_blame 1}}
2887 {mc "Copy path" command {clipboard clear; clipboard append $flist_menu_file}}
2888 }
2889 $flist_menu configure -tearoff 0
2890
2891 global diff_menu
2892 set diff_menu .diffctxmenu
2893 makemenu $diff_menu {
2894 {mc "Show origin of this line" command show_line_source}
2895 {mc "Run git gui blame on this line" command {external_blame_diff}}
2896 }
2897 $diff_menu configure -tearoff 0
2898}
2899
2900# Update row number label when selectedline changes
2901proc selectedline_change {n1 n2 op} {
2902 global selectedline rownumsel
2903
2904 if {$selectedline eq {}} {
2905 set rownumsel {}
2906 } else {
2907 set rownumsel [expr {$selectedline + 1}]
2908 }
2909}
2910
2911# mouse-2 makes all windows scan vertically, but only the one
2912# the cursor is in scans horizontally
2913proc canvscan {op w x y} {
2914 global canv canv2 canv3
2915 foreach c [list $canv $canv2 $canv3] {
2916 if {$c == $w} {
2917 $c scan $op $x $y
2918 } else {
2919 $c scan $op 0 $y
2920 }
2921 }
2922}
2923
2924proc scrollcanv {cscroll f0 f1} {
2925 $cscroll set $f0 $f1
2926 drawvisible
2927 flushhighlights
2928}
2929
2930# when we make a key binding for the toplevel, make sure
2931# it doesn't get triggered when that key is pressed in the
2932# find string entry widget.
2933proc bindkey {ev script} {
2934 global entries
2935 bind . $ev $script
2936 set escript [bind Entry $ev]
2937 if {$escript == {}} {
2938 set escript [bind Entry <Key>]
2939 }
2940 foreach e $entries {
2941 bind $e $ev "$escript; break"
2942 }
2943}
2944
2945proc bindmodfunctionkey {mod n script} {
2946 bind . <$mod-F$n> $script
2947 catch { bind . <$mod-XF86_Switch_VT_$n> $script }
2948}
2949
2950# set the focus back to the toplevel for any click outside
2951# the entry widgets
2952proc click {w} {
2953 global ctext entries
2954 foreach e [concat $entries $ctext] {
2955 if {$w == $e} return
2956 }
2957 focus .
2958}
2959
2960# Adjust the progress bar for a change in requested extent or canvas size
2961proc adjustprogress {} {
2962 global progresscanv
2963 global fprogcoord
2964
2965 $progresscanv configure -value [expr {int($fprogcoord * 100)}]
2966}
2967
2968proc doprogupdate {} {
2969 global lastprogupdate progupdatepending
2970
2971 if {$progupdatepending} {
2972 set progupdatepending 0
2973 set lastprogupdate [clock clicks -milliseconds]
2974 update
2975 }
2976}
2977
2978proc config_check_tmp_exists {tries_left} {
2979 global config_file_tmp
2980
2981 if {[file exists $config_file_tmp]} {
2982 incr tries_left -1
2983 if {$tries_left > 0} {
2984 after 100 [list config_check_tmp_exists $tries_left]
2985 } else {
2986 error_popup "There appears to be a stale $config_file_tmp\
2987 file, which will prevent gitk from saving its configuration on exit.\
2988 Please remove it if it is not being used by any existing gitk process."
2989 }
2990 }
2991}
2992
2993proc config_init_trace {name} {
2994 global config_variable_changed config_variable_original
2995
2996 upvar #0 $name var
2997 set config_variable_changed($name) 0
2998 set config_variable_original($name) $var
2999}
3000
3001proc config_variable_change_cb {name name2 op} {
3002 global config_variable_changed config_variable_original
3003
3004 upvar #0 $name var
3005 if {$op eq "write" &&
3006 (![info exists config_variable_original($name)] ||
3007 $config_variable_original($name) ne $var)} {
3008 set config_variable_changed($name) 1
3009 }
3010}
3011
3012proc savestuff {w} {
3013 global stuffsaved
3014 global config_file config_file_tmp
3015 global config_variables config_variable_changed
3016 global viewchanged
3017
3018 upvar #0 viewname current_viewname
3019 upvar #0 viewfiles current_viewfiles
3020 upvar #0 viewargs current_viewargs
3021 upvar #0 viewargscmd current_viewargscmd
3022 upvar #0 viewperm current_viewperm
3023 upvar #0 nextviewnum current_nextviewnum
3024
3025 if {$stuffsaved} return
3026 if {![winfo viewable .]} return
3027 set remove_tmp 0
3028 if {[catch {
3029 set try_count 0
3030 while {[catch {set f [safe_open_file $config_file_tmp {WRONLY CREAT EXCL}]}]} {
3031 if {[incr try_count] > 50} {
3032 error "Unable to write config file: $config_file_tmp exists"
3033 }
3034 after 100
3035 }
3036 set remove_tmp 1
3037 if {$::tcl_platform(platform) eq {windows}} {
3038 file attributes $config_file_tmp -hidden true
3039 }
3040 if {[file exists $config_file]} {
3041 source $config_file
3042 }
3043 foreach var_name $config_variables {
3044 upvar #0 $var_name var
3045 upvar 0 $var_name old_var
3046 if {!$config_variable_changed($var_name) && [info exists old_var]} {
3047 puts $f [list set $var_name $old_var]
3048 } else {
3049 puts $f [list set $var_name $var]
3050 }
3051 }
3052
3053 puts $f "set geometry(main) [wm geometry .]"
3054 puts $f "set geometry(state) [wm state .]"
3055 puts $f "set geometry(topwidth) [winfo width .tf]"
3056 puts $f "set geometry(topheight) [winfo height .tf]"
3057 puts $f "set geometry(pwsash0) \"[.tf.histframe.pwclist sashpos 0] 1\""
3058 puts $f "set geometry(pwsash1) \"[.tf.histframe.pwclist sashpos 1] 1\""
3059 puts $f "set geometry(botwidth) [winfo width .bleft]"
3060 puts $f "set geometry(botheight) [winfo height .bleft]"
3061
3062 array set view_save {}
3063 array set views {}
3064 if {![info exists permviews]} { set permviews {} }
3065 foreach view $permviews {
3066 set view_save([lindex $view 0]) 1
3067 set views([lindex $view 0]) $view
3068 }
3069 puts -nonewline $f "set permviews {"
3070 for {set v 1} {$v < $current_nextviewnum} {incr v} {
3071 if {$viewchanged($v)} {
3072 if {$current_viewperm($v)} {
3073 set views($current_viewname($v)) [list $current_viewname($v) $current_viewfiles($v) $current_viewargs($v) $current_viewargscmd($v)]
3074 } else {
3075 set view_save($current_viewname($v)) 0
3076 }
3077 }
3078 }
3079 # write old and updated view to their places and append remaining to the end
3080 foreach view $permviews {
3081 set view_name [lindex $view 0]
3082 if {$view_save($view_name)} {
3083 puts $f "{$views($view_name)}"
3084 }
3085 unset views($view_name)
3086 }
3087 foreach view_name [array names views] {
3088 puts $f "{$views($view_name)}"
3089 }
3090 puts $f "}"
3091 close $f
3092 file rename -force $config_file_tmp $config_file
3093 set remove_tmp 0
3094 } err]} {
3095 puts "Error saving config: $err"
3096 }
3097 if {$remove_tmp} {
3098 file delete -force $config_file_tmp
3099 }
3100 set stuffsaved 1
3101}
3102
3103proc resizeclistpanes {win w} {
3104 global oldwidth oldsash
3105 if {[info exists oldwidth($win)]} {
3106 if {[info exists oldsash($win)]} {
3107 set s0 [lindex $oldsash($win) 0]
3108 set s1 [lindex $oldsash($win) 1]
3109 } else {
3110 set s0 [$win sashpos 0]
3111 set s1 [$win sashpos 1]
3112 }
3113 if {$w < 60} {
3114 set sash0 [expr {int($w/2 - 2)}]
3115 set sash1 [expr {int($w*5/6 - 2)}]
3116 } else {
3117 set factor [expr {1.0 * $w / $oldwidth($win)}]
3118 set sash0 [expr {int($factor * [lindex $s0 0])}]
3119 set sash1 [expr {int($factor * [lindex $s1 0])}]
3120 if {$sash0 < 30} {
3121 set sash0 30
3122 }
3123 if {$sash1 < $sash0 + 20} {
3124 set sash1 [expr {$sash0 + 20}]
3125 }
3126 if {$sash1 > $w - 10} {
3127 set sash1 [expr {$w - 10}]
3128 if {$sash0 > $sash1 - 20} {
3129 set sash0 [expr {$sash1 - 20}]
3130 }
3131 }
3132 }
3133 $win sashpos 0 $sash0
3134 $win sashpos 1 $sash1
3135 set oldsash($win) [list $sash0 $sash1]
3136 }
3137 set oldwidth($win) $w
3138}
3139
3140proc resizecdetpanes {win w} {
3141 global oldwidth oldsash
3142 if {[info exists oldwidth($win)]} {
3143 if {[info exists oldsash($win)]} {
3144 set s0 $oldsash($win)
3145 } else {
3146 set s0 [$win sashpos 0]
3147 }
3148 if {$w < 60} {
3149 set sash0 [expr {int($w*3/4 - 2)}]
3150 } else {
3151 set factor [expr {1.0 * $w / $oldwidth($win)}]
3152 set sash0 [expr {int($factor * [lindex $s0 0])}]
3153 if {$sash0 < 45} {
3154 set sash0 45
3155 }
3156 if {$sash0 > $w - 15} {
3157 set sash0 [expr {$w - 15}]
3158 }
3159 }
3160 $win sashpos 0 $sash0
3161 set oldsash($win) $sash0
3162 }
3163 set oldwidth($win) $w
3164}
3165
3166proc allcanvs args {
3167 global canv canv2 canv3
3168 eval $canv $args
3169 eval $canv2 $args
3170 eval $canv3 $args
3171}
3172
3173proc bindall {event action} {
3174 global canv canv2 canv3
3175 bind $canv $event $action
3176 bind $canv2 $event $action
3177 bind $canv3 $event $action
3178}
3179
3180proc about {} {
3181 global bgcolor
3182 set w .about
3183 if {[winfo exists $w]} {
3184 raise $w
3185 return
3186 }
3187 ttk_toplevel $w
3188 wm title $w [mc "About gitk"]
3189 make_transient $w .
3190 message $w.m -text [mc "
3191Gitk - a commit viewer for git
3192
3193Copyright \u00a9 2005-2016 Paul Mackerras
3194
3195Use and redistribute under the terms of the GNU General Public License"] \
3196 -justify center -aspect 400 -border 2 -bg $bgcolor -relief groove
3197 pack $w.m -side top -fill x -padx 2 -pady 2
3198 ttk::button $w.ok -text [mc "Close"] -command "destroy $w" -default active
3199 pack $w.ok -side bottom
3200 bind $w <Visibility> "focus $w.ok"
3201 bind $w <Key-Escape> "destroy $w"
3202 bind $w <Key-Return> "destroy $w"
3203 tk::PlaceWindow $w widget .
3204}
3205
3206proc keys {} {
3207 global bgcolor
3208 set w .keys
3209 if {[winfo exists $w]} {
3210 raise $w
3211 return
3212 }
3213 if {[tk windowingsystem] eq {aqua}} {
3214 set M1T Cmd
3215 } else {
3216 set M1T Ctrl
3217 }
3218 ttk_toplevel $w
3219 wm title $w [mc "Gitk key bindings"]
3220 make_transient $w .
3221 message $w.m -text "
3222[mc "Gitk key bindings:"]
3223
3224[mc "<%s-Q> Quit" $M1T]
3225[mc "<%s-W> Close window" $M1T]
3226[mc "<Home> Move to first commit"]
3227[mc "<End> Move to last commit"]
3228[mc "<Up>, p, k Move up one commit"]
3229[mc "<Down>, n, j Move down one commit"]
3230[mc "<Left>, z, h Go back in history list"]
3231[mc "<Right>, x, l Go forward in history list"]
3232[mc "<%s-n> Go to n-th parent of current commit in history list" $M1T]
3233[mc "<PageUp> Move up one page in commit list"]
3234[mc "<PageDown> Move down one page in commit list"]
3235[mc "<%s-Home> Scroll to top of commit list" $M1T]
3236[mc "<%s-End> Scroll to bottom of commit list" $M1T]
3237[mc "<%s-Up> Scroll commit list up one line" $M1T]
3238[mc "<%s-Down> Scroll commit list down one line" $M1T]
3239[mc "<%s-PageUp> Scroll commit list up one page" $M1T]
3240[mc "<%s-PageDown> Scroll commit list down one page" $M1T]
3241[mc "<Shift-Up> Find backwards (upwards, later commits)"]
3242[mc "<Shift-Down> Find forwards (downwards, earlier commits)"]
3243[mc "<Delete>, b Scroll diff view up one page"]
3244[mc "<Backspace> Scroll diff view up one page"]
3245[mc "<Space> Scroll diff view down one page"]
3246[mc "u Scroll diff view up 18 lines"]
3247[mc "d Scroll diff view down 18 lines"]
3248[mc "<%s-F> Find" $M1T]
3249[mc "<%s-G> Move to next find hit" $M1T]
3250[mc "<Return> Move to next find hit"]
3251[mc "g Go to commit"]
3252[mc "/ Focus the search box"]
3253[mc "? Move to previous find hit"]
3254[mc "f Scroll diff view to next file"]
3255[mc "<%s-S> Search for next hit in diff view" $M1T]
3256[mc "<%s-R> Search for previous hit in diff view" $M1T]
3257[mc "<%s-KP+> Increase font size" $M1T]
3258[mc "<%s-plus> Increase font size" $M1T]
3259[mc "<%s-KP-> Decrease font size" $M1T]
3260[mc "<%s-minus> Decrease font size" $M1T]
3261[mc "<F5> Update"]
3262" \
3263 -justify left -bg $bgcolor -border 2 -relief groove
3264 pack $w.m -side top -fill both -padx 2 -pady 2
3265 ttk::button $w.ok -text [mc "Close"] -command "destroy $w" -default active
3266 bind $w <Key-Escape> [list destroy $w]
3267 pack $w.ok -side bottom
3268 bind $w <Visibility> "focus $w.ok"
3269 bind $w <Key-Escape> "destroy $w"
3270 bind $w <Key-Return> "destroy $w"
3271}
3272
3273# Procedures for manipulating the file list window at the
3274# bottom right of the overall window.
3275
3276proc treeview {w l openlevs} {
3277 global treecontents treediropen treeheight treeparent treeindex
3278
3279 set ix 0
3280 set treeindex() 0
3281 set lev 0
3282 set prefix {}
3283 set prefixend -1
3284 set prefendstack {}
3285 set htstack {}
3286 set ht 0
3287 set treecontents() {}
3288 $w conf -state normal
3289 foreach f $l {
3290 while {[string range $f 0 $prefixend] ne $prefix} {
3291 if {$lev <= $openlevs} {
3292 $w mark set e:$treeindex($prefix) "end -1c"
3293 $w mark gravity e:$treeindex($prefix) left
3294 }
3295 set treeheight($prefix) $ht
3296 incr ht [lindex $htstack end]
3297 set htstack [lreplace $htstack end end]
3298 set prefixend [lindex $prefendstack end]
3299 set prefendstack [lreplace $prefendstack end end]
3300 set prefix [string range $prefix 0 $prefixend]
3301 incr lev -1
3302 }
3303 set tail [string range $f [expr {$prefixend+1}] end]
3304 while {[set slash [string first "/" $tail]] >= 0} {
3305 lappend htstack $ht
3306 set ht 0
3307 lappend prefendstack $prefixend
3308 incr prefixend [expr {$slash + 1}]
3309 set d [string range $tail 0 $slash]
3310 lappend treecontents($prefix) $d
3311 set oldprefix $prefix
3312 append prefix $d
3313 set treecontents($prefix) {}
3314 set treeindex($prefix) [incr ix]
3315 set treeparent($prefix) $oldprefix
3316 set tail [string range $tail [expr {$slash+1}] end]
3317 if {$lev <= $openlevs} {
3318 set ht 1
3319 set treediropen($prefix) [expr {$lev < $openlevs}]
3320 set bm [expr {$lev == $openlevs? "tri-rt": "tri-dn"}]
3321 $w mark set d:$ix "end -1c"
3322 $w mark gravity d:$ix left
3323 set str "\n"
3324 for {set i 0} {$i < $lev} {incr i} {append str "\t"}
3325 $w insert end $str
3326 $w image create end -align center -image $bm -padx 1 \
3327 -name a:$ix
3328 $w insert end $d [highlight_tag $prefix]
3329 $w mark set s:$ix "end -1c"
3330 $w mark gravity s:$ix left
3331 }
3332 incr lev
3333 }
3334 if {$tail ne {}} {
3335 if {$lev <= $openlevs} {
3336 incr ht
3337 set str "\n"
3338 for {set i 0} {$i < $lev} {incr i} {append str "\t"}
3339 $w insert end $str
3340 $w insert end $tail [highlight_tag $f]
3341 }
3342 lappend treecontents($prefix) $tail
3343 }
3344 }
3345 while {$htstack ne {}} {
3346 set treeheight($prefix) $ht
3347 incr ht [lindex $htstack end]
3348 set htstack [lreplace $htstack end end]
3349 set prefixend [lindex $prefendstack end]
3350 set prefendstack [lreplace $prefendstack end end]
3351 set prefix [string range $prefix 0 $prefixend]
3352 }
3353 $w conf -state disabled
3354}
3355
3356proc linetoelt {l} {
3357 global treeheight treecontents
3358
3359 set y 2
3360 set prefix {}
3361 while {1} {
3362 foreach e $treecontents($prefix) {
3363 if {$y == $l} {
3364 return "$prefix$e"
3365 }
3366 set n 1
3367 if {[string index $e end] eq "/"} {
3368 set n $treeheight($prefix$e)
3369 if {$y + $n > $l} {
3370 append prefix $e
3371 incr y
3372 break
3373 }
3374 }
3375 incr y $n
3376 }
3377 }
3378}
3379
3380proc highlight_tree {y prefix} {
3381 global treeheight treecontents cflist
3382
3383 foreach e $treecontents($prefix) {
3384 set path $prefix$e
3385 if {[highlight_tag $path] ne {}} {
3386 $cflist tag add bold $y.0 "$y.0 lineend"
3387 }
3388 incr y
3389 if {[string index $e end] eq "/" && $treeheight($path) > 1} {
3390 set y [highlight_tree $y $path]
3391 }
3392 }
3393 return $y
3394}
3395
3396proc treeclosedir {w dir} {
3397 global treediropen treeheight treeparent treeindex
3398
3399 set ix $treeindex($dir)
3400 $w conf -state normal
3401 $w delete s:$ix e:$ix
3402 set treediropen($dir) 0
3403 $w image configure a:$ix -image tri-rt
3404 $w conf -state disabled
3405 set n [expr {1 - $treeheight($dir)}]
3406 while {$dir ne {}} {
3407 incr treeheight($dir) $n
3408 set dir $treeparent($dir)
3409 }
3410}
3411
3412proc treeopendir {w dir} {
3413 global treediropen treeheight treeparent treecontents treeindex
3414
3415 set ix $treeindex($dir)
3416 $w conf -state normal
3417 $w image configure a:$ix -image tri-dn
3418 $w mark set e:$ix s:$ix
3419 $w mark gravity e:$ix right
3420 set lev 0
3421 set str "\n"
3422 set n [llength $treecontents($dir)]
3423 for {set x $dir} {$x ne {}} {set x $treeparent($x)} {
3424 incr lev
3425 append str "\t"
3426 incr treeheight($x) $n
3427 }
3428 foreach e $treecontents($dir) {
3429 set de $dir$e
3430 if {[string index $e end] eq "/"} {
3431 set iy $treeindex($de)
3432 $w mark set d:$iy e:$ix
3433 $w mark gravity d:$iy left
3434 $w insert e:$ix $str
3435 set treediropen($de) 0
3436 $w image create e:$ix -align center -image tri-rt -padx 1 \
3437 -name a:$iy
3438 $w insert e:$ix $e [highlight_tag $de]
3439 $w mark set s:$iy e:$ix
3440 $w mark gravity s:$iy left
3441 set treeheight($de) 1
3442 } else {
3443 $w insert e:$ix $str
3444 $w insert e:$ix $e [highlight_tag $de]
3445 }
3446 }
3447 $w mark gravity e:$ix right
3448 $w conf -state disabled
3449 set treediropen($dir) 1
3450 set top [lindex [split [$w index @0,0] .] 0]
3451 set ht [$w cget -height]
3452 set l [lindex [split [$w index s:$ix] .] 0]
3453 if {$l < $top} {
3454 $w yview $l.0
3455 } elseif {$l + $n + 1 > $top + $ht} {
3456 set top [expr {$l + $n + 2 - $ht}]
3457 if {$l < $top} {
3458 set top $l
3459 }
3460 $w yview $top.0
3461 }
3462}
3463
3464proc treeclick {w x y} {
3465 global treediropen cmitmode ctext cflist cflist_top
3466
3467 if {$cmitmode ne "tree"} return
3468 if {![info exists cflist_top]} return
3469 set l [lindex [split [$w index "@$x,$y"] "."] 0]
3470 $cflist tag remove highlight $cflist_top.0 "$cflist_top.0 lineend"
3471 $cflist tag add highlight $l.0 "$l.0 lineend"
3472 set cflist_top $l
3473 if {$l == 1} {
3474 $ctext yview 1.0
3475 return
3476 }
3477 set e [linetoelt $l]
3478 if {[string index $e end] ne "/"} {
3479 showfile $e
3480 } elseif {$treediropen($e)} {
3481 treeclosedir $w $e
3482 } else {
3483 treeopendir $w $e
3484 }
3485}
3486
3487proc setfilelist {id} {
3488 global treefilelist cflist jump_to_here
3489
3490 treeview $cflist $treefilelist($id) 0
3491 if {$jump_to_here ne {}} {
3492 set f [lindex $jump_to_here 0]
3493 if {[lsearch -exact $treefilelist($id) $f] >= 0} {
3494 showfile $f
3495 }
3496 }
3497}
3498
3499image create bitmap tri-rt -background black -foreground blue -data {
3500 #define tri-rt_width 13
3501 #define tri-rt_height 13
3502 static unsigned char tri-rt_bits[] = {
3503 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x30, 0x00, 0x70, 0x00, 0xf0, 0x00,
3504 0xf0, 0x01, 0xf0, 0x00, 0x70, 0x00, 0x30, 0x00, 0x10, 0x00, 0x00, 0x00,
3505 0x00, 0x00};
3506} -maskdata {
3507 #define tri-rt-mask_width 13
3508 #define tri-rt-mask_height 13
3509 static unsigned char tri-rt-mask_bits[] = {
3510 0x08, 0x00, 0x18, 0x00, 0x38, 0x00, 0x78, 0x00, 0xf8, 0x00, 0xf8, 0x01,
3511 0xf8, 0x03, 0xf8, 0x01, 0xf8, 0x00, 0x78, 0x00, 0x38, 0x00, 0x18, 0x00,
3512 0x08, 0x00};
3513}
3514image create bitmap tri-dn -background black -foreground blue -data {
3515 #define tri-dn_width 13
3516 #define tri-dn_height 13
3517 static unsigned char tri-dn_bits[] = {
3518 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0x07, 0xf8, 0x03,
3519 0xf0, 0x01, 0xe0, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3520 0x00, 0x00};
3521} -maskdata {
3522 #define tri-dn-mask_width 13
3523 #define tri-dn-mask_height 13
3524 static unsigned char tri-dn-mask_bits[] = {
3525 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x1f, 0xfe, 0x0f, 0xfc, 0x07,
3526 0xf8, 0x03, 0xf0, 0x01, 0xe0, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00,
3527 0x00, 0x00};
3528}
3529
3530image create bitmap reficon-T -background black -foreground yellow -data {
3531 #define tagicon_width 13
3532 #define tagicon_height 9
3533 static unsigned char tagicon_bits[] = {
3534 0x00, 0x00, 0x00, 0x00, 0xf0, 0x07, 0xf8, 0x07,
3535 0xfc, 0x07, 0xf8, 0x07, 0xf0, 0x07, 0x00, 0x00, 0x00, 0x00};
3536} -maskdata {
3537 #define tagicon-mask_width 13
3538 #define tagicon-mask_height 9
3539 static unsigned char tagicon-mask_bits[] = {
3540 0x00, 0x00, 0xf0, 0x0f, 0xf8, 0x0f, 0xfc, 0x0f,
3541 0xfe, 0x0f, 0xfc, 0x0f, 0xf8, 0x0f, 0xf0, 0x0f, 0x00, 0x00};
3542}
3543set rectdata {
3544 #define headicon_width 13
3545 #define headicon_height 9
3546 static unsigned char headicon_bits[] = {
3547 0x00, 0x00, 0x00, 0x00, 0xf8, 0x07, 0xf8, 0x07,
3548 0xf8, 0x07, 0xf8, 0x07, 0xf8, 0x07, 0x00, 0x00, 0x00, 0x00};
3549}
3550set rectmask {
3551 #define headicon-mask_width 13
3552 #define headicon-mask_height 9
3553 static unsigned char headicon-mask_bits[] = {
3554 0x00, 0x00, 0xfc, 0x0f, 0xfc, 0x0f, 0xfc, 0x0f,
3555 0xfc, 0x0f, 0xfc, 0x0f, 0xfc, 0x0f, 0xfc, 0x0f, 0x00, 0x00};
3556}
3557image create bitmap reficon-H -background black -foreground "#00ff00" \
3558 -data $rectdata -maskdata $rectmask
3559image create bitmap reficon-R -background black -foreground "#ffddaa" \
3560 -data $rectdata -maskdata $rectmask
3561image create bitmap reficon-o -background black -foreground "#ddddff" \
3562 -data $rectdata -maskdata $rectmask
3563
3564proc init_flist {first} {
3565 global cflist cflist_top difffilestart
3566
3567 $cflist conf -state normal
3568 $cflist delete 0.0 end
3569 if {$first ne {}} {
3570 $cflist insert end $first
3571 set cflist_top 1
3572 $cflist tag add highlight 1.0 "1.0 lineend"
3573 } else {
3574 unset -nocomplain cflist_top
3575 }
3576 $cflist conf -state disabled
3577 set difffilestart {}
3578}
3579
3580proc highlight_tag {f} {
3581 global highlight_paths
3582
3583 foreach p $highlight_paths {
3584 if {[string match $p $f]} {
3585 return "bold"
3586 }
3587 }
3588 return {}
3589}
3590
3591proc highlight_filelist {} {
3592 global cmitmode cflist
3593
3594 $cflist conf -state normal
3595 if {$cmitmode ne "tree"} {
3596 set end [lindex [split [$cflist index end] .] 0]
3597 for {set l 2} {$l < $end} {incr l} {
3598 set line [$cflist get $l.0 "$l.0 lineend"]
3599 if {[highlight_tag $line] ne {}} {
3600 $cflist tag add bold $l.0 "$l.0 lineend"
3601 }
3602 }
3603 } else {
3604 highlight_tree 2 {}
3605 }
3606 $cflist conf -state disabled
3607}
3608
3609proc unhighlight_filelist {} {
3610 global cflist
3611
3612 $cflist conf -state normal
3613 $cflist tag remove bold 1.0 end
3614 $cflist conf -state disabled
3615}
3616
3617proc add_flist {fl} {
3618 global cflist
3619
3620 $cflist conf -state normal
3621 foreach f $fl {
3622 $cflist insert end "\n"
3623 $cflist insert end $f [highlight_tag $f]
3624 }
3625 $cflist conf -state disabled
3626}
3627
3628proc sel_flist {w x y} {
3629 global ctext difffilestart cflist cflist_top cmitmode
3630
3631 if {$cmitmode eq "tree"} return
3632 if {![info exists cflist_top]} return
3633 set l [lindex [split [$w index "@$x,$y"] "."] 0]
3634 $cflist tag remove highlight $cflist_top.0 "$cflist_top.0 lineend"
3635 $cflist tag add highlight $l.0 "$l.0 lineend"
3636 set cflist_top $l
3637 if {$l == 1} {
3638 $ctext yview 1.0
3639 } else {
3640 catch {$ctext yview [lindex $difffilestart [expr {$l - 2}]]}
3641 }
3642 suppress_highlighting_file_for_current_scrollpos
3643}
3644
3645proc pop_flist_menu {w X Y x y} {
3646 global ctext cflist cmitmode flist_menu flist_menu_file
3647 global treediffs diffids
3648
3649 stopfinding
3650 set l [lindex [split [$w index "@$x,$y"] "."] 0]
3651 if {$l <= 1} return
3652 if {$cmitmode eq "tree"} {
3653 set e [linetoelt $l]
3654 if {[string index $e end] eq "/"} return
3655 } else {
3656 set e [lindex $treediffs($diffids) [expr {$l-2}]]
3657 }
3658 set flist_menu_file $e
3659 set xdiffstate "normal"
3660 if {$cmitmode eq "tree"} {
3661 set xdiffstate "disabled"
3662 }
3663 # Disable "External diff" item in tree mode
3664 $flist_menu entryconf 2 -state $xdiffstate
3665 tk_popup $flist_menu $X $Y
3666}
3667
3668proc find_ctext_fileinfo {line} {
3669 global ctext_file_names ctext_file_lines
3670
3671 set ok [bsearch $ctext_file_lines $line]
3672 set tline [lindex $ctext_file_lines $ok]
3673
3674 if {$ok >= [llength $ctext_file_lines] || $line < $tline} {
3675 return {}
3676 } else {
3677 return [list [lindex $ctext_file_names $ok] $tline]
3678 }
3679}
3680
3681proc pop_diff_menu {w X Y x y} {
3682 global ctext diff_menu flist_menu_file
3683 global diff_menu_txtpos diff_menu_line
3684 global diff_menu_filebase
3685
3686 set diff_menu_txtpos [split [$w index "@$x,$y"] "."]
3687 set diff_menu_line [lindex $diff_menu_txtpos 0]
3688 # don't pop up the menu on hunk-separator or file-separator lines
3689 if {[lsearch -glob [$ctext tag names $diff_menu_line.0] "*sep"] >= 0} {
3690 return
3691 }
3692 stopfinding
3693 set f [find_ctext_fileinfo $diff_menu_line]
3694 if {$f eq {}} return
3695 set flist_menu_file [lindex $f 0]
3696 set diff_menu_filebase [lindex $f 1]
3697 tk_popup $diff_menu $X $Y
3698}
3699
3700proc flist_hl {only} {
3701 global flist_menu_file findstring gdttype
3702
3703 set x [shellquote $flist_menu_file]
3704 if {$only || $findstring eq {} || $gdttype ne [mc "touching paths:"]} {
3705 set findstring $x
3706 } else {
3707 append findstring " " $x
3708 }
3709 set gdttype [mc "touching paths:"]
3710}
3711
3712proc gitknewtmpdir {} {
3713 global diffnum gitktmpdir gitdir env
3714
3715 if {![info exists gitktmpdir]} {
3716 if {[info exists env(GITK_TMPDIR)]} {
3717 set tmpdir $env(GITK_TMPDIR)
3718 } elseif {[info exists env(TMPDIR)]} {
3719 set tmpdir $env(TMPDIR)
3720 } else {
3721 set tmpdir $gitdir
3722 }
3723 set gitktmpformat [file join $tmpdir ".gitk-tmp.XXXXXX"]
3724 if {[catch {set gitktmpdir [safe_exec [list mktemp -d $gitktmpformat]]}]} {
3725 set gitktmpdir [file join $gitdir [format ".gitk-tmp.%s" [pid]]]
3726 }
3727 if {[catch {file mkdir $gitktmpdir} err]} {
3728 error_popup "[mc "Error creating temporary directory %s:" $gitktmpdir] $err"
3729 unset gitktmpdir
3730 return {}
3731 }
3732 set diffnum 0
3733 }
3734 incr diffnum
3735 set diffdir [file join $gitktmpdir $diffnum]
3736 if {[catch {file mkdir $diffdir} err]} {
3737 error_popup "[mc "Error creating temporary directory %s:" $diffdir] $err"
3738 return {}
3739 }
3740 return $diffdir
3741}
3742
3743proc save_file_from_commit {filename output what} {
3744 global nullfile
3745
3746 if {[catch {safe_exec_redirect [list git show $filename --] [list > $output]} err]} {
3747 if {[string match "fatal: bad revision *" $err]} {
3748 return $nullfile
3749 }
3750 error_popup "[mc "Error getting \"%s\" from %s:" $filename $what] $err"
3751 return {}
3752 }
3753 return $output
3754}
3755
3756proc external_diff_get_one_file {diffid filename diffdir} {
3757 global nullid nullid2 nullfile
3758 global worktree
3759
3760 if {$diffid == $nullid} {
3761 set difffile [file join $worktree $filename]
3762 if {[file exists $difffile]} {
3763 return $difffile
3764 }
3765 return $nullfile
3766 }
3767 if {$diffid == $nullid2} {
3768 set difffile [file join $diffdir "\[index\] [file tail $filename]"]
3769 return [save_file_from_commit :$filename $difffile index]
3770 }
3771 set difffile [file join $diffdir "\[$diffid\] [file tail $filename]"]
3772 return [save_file_from_commit $diffid:$filename $difffile \
3773 "revision $diffid"]
3774}
3775
3776proc external_diff {} {
3777 global nullid nullid2
3778 global flist_menu_file
3779 global diffids
3780 global extdifftool
3781
3782 if {[llength $diffids] == 1} {
3783 # no reference commit given
3784 set diffidto [lindex $diffids 0]
3785 if {$diffidto eq $nullid} {
3786 # diffing working copy with index
3787 set diffidfrom $nullid2
3788 } elseif {$diffidto eq $nullid2} {
3789 # diffing index with HEAD
3790 set diffidfrom "HEAD"
3791 } else {
3792 # use first parent commit
3793 global parentlist selectedline
3794 set diffidfrom [lindex $parentlist $selectedline 0]
3795 }
3796 } else {
3797 set diffidfrom [lindex $diffids 0]
3798 set diffidto [lindex $diffids 1]
3799 }
3800
3801 # make sure that several diffs wont collide
3802 set diffdir [gitknewtmpdir]
3803 if {$diffdir eq {}} return
3804
3805 # gather files to diff
3806 set difffromfile [external_diff_get_one_file $diffidfrom $flist_menu_file $diffdir]
3807 set difftofile [external_diff_get_one_file $diffidto $flist_menu_file $diffdir]
3808
3809 if {$difffromfile ne {} && $difftofile ne {}} {
3810 set cmd [list [shellsplit $extdifftool] $difffromfile $difftofile]
3811 if {[catch {set fl [safe_open_command $cmd]} err]} {
3812 file delete -force $diffdir
3813 error_popup "$extdifftool: [mc "command failed:"] $err"
3814 } else {
3815 fconfigure $fl -blocking 0
3816 filerun $fl [list delete_at_eof $fl $diffdir]
3817 }
3818 }
3819}
3820
3821proc find_hunk_blamespec {base line} {
3822 global ctext
3823
3824 # Find and parse the hunk header
3825 set s_lix [$ctext search -backwards -regexp ^@@ "$line.0 lineend" $base.0]
3826 if {$s_lix eq {}} return
3827
3828 set s_line [$ctext get $s_lix "$s_lix + 1 lines"]
3829 if {![regexp {^@@@*(( -\d+(,\d+)?)+) \+(\d+)(,\d+)? @@} $s_line \
3830 s_line old_specs osz osz1 new_line nsz]} {
3831 return
3832 }
3833
3834 # base lines for the parents
3835 set base_lines [list $new_line]
3836 foreach old_spec [lrange [split $old_specs " "] 1 end] {
3837 if {![regexp -- {-(\d+)(,\d+)?} $old_spec \
3838 old_spec old_line osz]} {
3839 return
3840 }
3841 lappend base_lines $old_line
3842 }
3843
3844 # Now scan the lines to determine offset within the hunk
3845 set max_parent [expr {[llength $base_lines]-2}]
3846 set dline 0
3847 set s_lno [lindex [split $s_lix "."] 0]
3848
3849 # Determine if the line is removed
3850 set chunk [$ctext get $line.0 "$line.1 + $max_parent chars"]
3851 if {[string match {[-+ ]*} $chunk]} {
3852 set removed_idx [string first "-" $chunk]
3853 # Choose a parent index
3854 if {$removed_idx >= 0} {
3855 set parent $removed_idx
3856 } else {
3857 set unchanged_idx [string first " " $chunk]
3858 if {$unchanged_idx >= 0} {
3859 set parent $unchanged_idx
3860 } else {
3861 # blame the current commit
3862 set parent -1
3863 }
3864 }
3865 # then count other lines that belong to it
3866 for {set i $line} {[incr i -1] > $s_lno} {} {
3867 set chunk [$ctext get $i.0 "$i.1 + $max_parent chars"]
3868 # Determine if the line is removed
3869 set removed_idx [string first "-" $chunk]
3870 if {$parent >= 0} {
3871 set code [string index $chunk $parent]
3872 if {$code eq "-" || ($removed_idx < 0 && $code ne "+")} {
3873 incr dline
3874 }
3875 } else {
3876 if {$removed_idx < 0} {
3877 incr dline
3878 }
3879 }
3880 }
3881 incr parent
3882 } else {
3883 set parent 0
3884 }
3885
3886 incr dline [lindex $base_lines $parent]
3887 return [list $parent $dline]
3888}
3889
3890proc external_blame_diff {} {
3891 global currentid cmitmode
3892 global diff_menu_txtpos diff_menu_line
3893 global diff_menu_filebase flist_menu_file
3894
3895 if {$cmitmode eq "tree"} {
3896 set parent_idx 0
3897 set line [expr {$diff_menu_line - $diff_menu_filebase}]
3898 } else {
3899 set hinfo [find_hunk_blamespec $diff_menu_filebase $diff_menu_line]
3900 if {$hinfo ne {}} {
3901 set parent_idx [lindex $hinfo 0]
3902 set line [lindex $hinfo 1]
3903 } else {
3904 set parent_idx 0
3905 set line 0
3906 }
3907 }
3908
3909 external_blame $parent_idx $line
3910}
3911
3912# Find the SHA1 ID of the blob for file $fname in the index
3913# at stage 0 or 2
3914proc index_sha1 {fname} {
3915 set f [safe_open_command [list git ls-files -s $fname]]
3916 while {[gets $f line] >= 0} {
3917 set info [lindex [split $line "\t"] 0]
3918 set stage [lindex $info 2]
3919 if {$stage eq "0" || $stage eq "2"} {
3920 close $f
3921 return [lindex $info 1]
3922 }
3923 }
3924 close $f
3925 return {}
3926}
3927
3928# Turn an absolute path into one relative to the current directory
3929proc make_relative {f} {
3930 if {[file pathtype $f] eq "relative"} {
3931 return $f
3932 }
3933 set elts [file split $f]
3934 set here [file split [pwd]]
3935 set ei 0
3936 set hi 0
3937 set res {}
3938 foreach d $here {
3939 if {$ei < $hi || $ei >= [llength $elts] || [lindex $elts $ei] ne $d} {
3940 lappend res ".."
3941 } else {
3942 incr ei
3943 }
3944 incr hi
3945 }
3946 set elts [concat $res [lrange $elts $ei end]]
3947 return [eval file join $elts]
3948}
3949
3950proc external_blame {parent_idx {line {}}} {
3951 global flist_menu_file cdup
3952 global nullid nullid2
3953 global parentlist selectedline currentid
3954
3955 if {$parent_idx > 0} {
3956 set base_commit [lindex $parentlist $selectedline [expr {$parent_idx-1}]]
3957 } else {
3958 set base_commit $currentid
3959 }
3960
3961 if {$base_commit eq {} || $base_commit eq $nullid || $base_commit eq $nullid2} {
3962 error_popup [mc "No such commit"]
3963 return
3964 }
3965
3966 set cmdline [list git gui blame]
3967 if {$line ne {} && $line > 1} {
3968 lappend cmdline "--line=$line"
3969 }
3970 set f [file join $cdup $flist_menu_file]
3971 # Unfortunately it seems git gui blame doesn't like
3972 # being given an absolute path...
3973 set f [make_relative $f]
3974 lappend cmdline $base_commit $f
3975 if {[catch {safe_exec_redirect $cmdline [list &]} err]} {
3976 error_popup "[mc "git gui blame: command failed:"] $err"
3977 }
3978}
3979
3980proc show_line_source {} {
3981 global cmitmode currentid parents curview blamestuff blameinst
3982 global diff_menu_line diff_menu_filebase flist_menu_file
3983 global nullid nullid2 gitdir cdup
3984
3985 set from_index {}
3986 if {$cmitmode eq "tree"} {
3987 set id $currentid
3988 set line [expr {$diff_menu_line - $diff_menu_filebase}]
3989 } else {
3990 set h [find_hunk_blamespec $diff_menu_filebase $diff_menu_line]
3991 if {$h eq {}} return
3992 set pi [lindex $h 0]
3993 if {$pi == 0} {
3994 mark_ctext_line $diff_menu_line
3995 return
3996 }
3997 incr pi -1
3998 if {$currentid eq $nullid} {
3999 if {$pi > 0} {
4000 # must be a merge in progress...
4001 if {[catch {
4002 # get the last line from .git/MERGE_HEAD
4003 set f [safe_open_file [file join $gitdir MERGE_HEAD] r]
4004 set id [lindex [split [read $f] "\n"] end-1]
4005 close $f
4006 } err]} {
4007 error_popup [mc "Couldn't read merge head: %s" $err]
4008 return
4009 }
4010 } elseif {$parents($curview,$currentid) eq $nullid2} {
4011 # need to do the blame from the index
4012 if {[catch {
4013 set from_index [index_sha1 $flist_menu_file]
4014 } err]} {
4015 error_popup [mc "Error reading index: %s" $err]
4016 return
4017 }
4018 } else {
4019 set id $parents($curview,$currentid)
4020 }
4021 } else {
4022 set id [lindex $parents($curview,$currentid) $pi]
4023 }
4024 set line [lindex $h 1]
4025 }
4026 set blamefile [file join $cdup $flist_menu_file]
4027 if {$from_index ne {}} {
4028 set blameargs [list \
4029 [list git cat-file blob $from_index] \
4030 [list git blame -p -L$line,+1 --contents - -- $blamefile]]
4031 } else {
4032 set blameargs [list \
4033 [list git blame -p -L$line,+1 $id -- $blamefile]]
4034 }
4035 if {[catch {
4036 set f [safe_open_pipeline $blameargs]
4037 } err]} {
4038 error_popup [mc "Couldn't start git blame: %s" $err]
4039 return
4040 }
4041 nowbusy blaming [mc "Searching"]
4042 fconfigure $f -blocking 0
4043 set i [reg_instance $f]
4044 set blamestuff($i) {}
4045 set blameinst $i
4046 filerun $f [list read_line_source $f $i]
4047}
4048
4049proc stopblaming {} {
4050 global blameinst
4051
4052 if {[info exists blameinst]} {
4053 stop_instance $blameinst
4054 unset blameinst
4055 notbusy blaming
4056 }
4057}
4058
4059proc read_line_source {fd inst} {
4060 global blamestuff curview commfd blameinst nullid nullid2
4061 global hashlength
4062
4063 while {[gets $fd line] >= 0} {
4064 lappend blamestuff($inst) $line
4065 }
4066 if {![eof $fd]} {
4067 return 1
4068 }
4069 unset commfd($inst)
4070 unset blameinst
4071 notbusy blaming
4072 fconfigure $fd -blocking 1
4073 if {[catch {close $fd} err]} {
4074 error_popup [mc "Error running git blame: %s" $err]
4075 return 0
4076 }
4077
4078 set fname {}
4079 set line [split [lindex $blamestuff($inst) 0] " "]
4080 set id [lindex $line 0]
4081 set lnum [lindex $line 1]
4082 if {[string length $id] == $hashlength && [string is xdigit $id] &&
4083 [string is digit -strict $lnum]} {
4084 # look for "filename" line
4085 foreach l $blamestuff($inst) {
4086 if {[string match "filename *" $l]} {
4087 set fname [string range $l 9 end]
4088 break
4089 }
4090 }
4091 }
4092 if {$fname ne {}} {
4093 # all looks good, select it
4094 if {$id eq $nullid} {
4095 # blame uses all-zeroes to mean not committed,
4096 # which would mean a change in the index
4097 set id $nullid2
4098 }
4099 if {[commitinview $id $curview]} {
4100 selectline [rowofcommit $id] 1 [list $fname $lnum] 1
4101 } else {
4102 error_popup [mc "That line comes from commit %s, \
4103 which is not in this view" [shortids $id]]
4104 }
4105 } else {
4106 puts "oops couldn't parse git blame output"
4107 }
4108 return 0
4109}
4110
4111# delete $dir when we see eof on $f (presumably because the child has exited)
4112proc delete_at_eof {f dir} {
4113 while {[gets $f line] >= 0} {}
4114 if {[eof $f]} {
4115 if {[catch {close $f} err]} {
4116 error_popup "[mc "External diff viewer failed:"] $err"
4117 }
4118 file delete -force $dir
4119 return 0
4120 }
4121 return 1
4122}
4123
4124# Functions for adding and removing shell-type quoting
4125
4126proc shellquote {str} {
4127 if {![string match "*\['\"\\ \t]*" $str]} {
4128 return $str
4129 }
4130 if {![string match "*\['\"\\]*" $str]} {
4131 return "\"$str\""
4132 }
4133 if {![string match "*'*" $str]} {
4134 return "'$str'"
4135 }
4136 return "\"[string map {\" \\\" \\ \\\\} $str]\""
4137}
4138
4139proc shellarglist {l} {
4140 set str {}
4141 foreach a $l {
4142 if {$str ne {}} {
4143 append str " "
4144 }
4145 append str [shellquote $a]
4146 }
4147 return $str
4148}
4149
4150proc shelldequote {str} {
4151 set ret {}
4152 set used -1
4153 while {1} {
4154 incr used
4155 if {![regexp -start $used -indices "\['\"\\\\ \t]" $str first]} {
4156 append ret [string range $str $used end]
4157 set used [string length $str]
4158 break
4159 }
4160 set first [lindex $first 0]
4161 set ch [string index $str $first]
4162 if {$first > $used} {
4163 append ret [string range $str $used [expr {$first - 1}]]
4164 set used $first
4165 }
4166 if {$ch eq " " || $ch eq "\t"} break
4167 incr used
4168 if {$ch eq "'"} {
4169 set first [string first "'" $str $used]
4170 if {$first < 0} {
4171 error "unmatched single-quote"
4172 }
4173 append ret [string range $str $used [expr {$first - 1}]]
4174 set used $first
4175 continue
4176 }
4177 if {$ch eq "\\"} {
4178 if {$used >= [string length $str]} {
4179 error "trailing backslash"
4180 }
4181 append ret [string index $str $used]
4182 continue
4183 }
4184 # here ch == "\""
4185 while {1} {
4186 if {![regexp -start $used -indices "\[\"\\\\]" $str first]} {
4187 error "unmatched double-quote"
4188 }
4189 set first [lindex $first 0]
4190 set ch [string index $str $first]
4191 if {$first > $used} {
4192 append ret [string range $str $used [expr {$first - 1}]]
4193 set used $first
4194 }
4195 if {$ch eq "\""} break
4196 incr used
4197 append ret [string index $str $used]
4198 incr used
4199 }
4200 }
4201 return [list $used $ret]
4202}
4203
4204proc shellsplit {str} {
4205 set l {}
4206 while {1} {
4207 set str [string trimleft $str]
4208 if {$str eq {}} break
4209 set dq [shelldequote $str]
4210 set n [lindex $dq 0]
4211 set word [lindex $dq 1]
4212 set str [string range $str $n end]
4213 lappend l $word
4214 }
4215 return $l
4216}
4217
4218proc set_window_title {} {
4219 global appname curview viewname vrevs
4220 set rev [mc "All files"]
4221 if {$curview ne 0} {
4222 if {$viewname($curview) eq [mc "Command line"]} {
4223 set rev [string map {"--gitk-symmetric-diff-marker" "--merge"} $vrevs($curview)]
4224 } else {
4225 set rev $viewname($curview)
4226 }
4227 }
4228 wm title . "[reponame]: $rev - $appname"
4229}
4230
4231# Code to implement multiple views
4232
4233proc newview {ishighlight} {
4234 global nextviewnum newviewname newishighlight
4235 global revtreeargs viewargscmd newviewopts curview
4236
4237 set newishighlight $ishighlight
4238 set top .gitkview
4239 if {[winfo exists $top]} {
4240 raise $top
4241 return
4242 }
4243 decode_view_opts $nextviewnum $revtreeargs
4244 set newviewname($nextviewnum) "[mc "View"] $nextviewnum"
4245 set newviewopts($nextviewnum,perm) 0
4246 set newviewopts($nextviewnum,cmd) $viewargscmd($curview)
4247 vieweditor $top $nextviewnum [mc "Gitk view definition"]
4248}
4249
4250set known_view_options {
4251 {perm b . {} {mc "Remember this view"}}
4252 {reflabel l + {} {mc "References (space separated list):"}}
4253 {refs t15 .. {} {mc "Branches & tags:"}}
4254 {allrefs b *. "--all" {mc "All refs"}}
4255 {branches b . "--branches" {mc "All (local) branches"}}
4256 {tags b . "--tags" {mc "All tags"}}
4257 {remotes b . "--remotes" {mc "All remote-tracking branches"}}
4258 {commitlbl l + {} {mc "Commit Info (regular expressions):"}}
4259 {author t15 .. "--author=*" {mc "Author:"}}
4260 {committer t15 . "--committer=*" {mc "Committer:"}}
4261 {loginfo t15 .. "--grep=*" {mc "Commit Message:"}}
4262 {allmatch b .. "--all-match" {mc "Matches all Commit Info criteria"}}
4263 {igrep b .. "--invert-grep" {mc "Matches no Commit Info criteria"}}
4264 {changes_l l + {} {mc "Changes to Files:"}}
4265 {pickaxe_s r0 . {} {mc "Fixed String"}}
4266 {pickaxe_t r1 . "--pickaxe-regex" {mc "Regular Expression"}}
4267 {pickaxe t15 .. "-S*" {mc "Search string:"}}
4268 {datelabel l + {} {mc "Commit Dates (\"2 weeks ago\", \"2009-03-17 15:27:38\", \"March 17, 2009 15:27:38\"):"}}
4269 {since t15 .. {"--since=*" "--after=*"} {mc "Since:"}}
4270 {until t15 . {"--until=*" "--before=*"} {mc "Until:"}}
4271 {limit_lbl l + {} {mc "Limit and/or skip a number of revisions (positive integer):"}}
4272 {limit t10 *. "--max-count=*" {mc "Number to show:"}}
4273 {skip t10 . "--skip=*" {mc "Number to skip:"}}
4274 {misc_lbl l + {} {mc "Miscellaneous options:"}}
4275 {dorder b *. {"--date-order" "-d"} {mc "Strictly sort by date"}}
4276 {lright b . "--left-right" {mc "Mark branch sides"}}
4277 {first b . "--first-parent" {mc "Limit to first parent"}}
4278 {smplhst b . "--simplify-by-decoration" {mc "Simple history"}}
4279 {args t50 *. {} {mc "Additional arguments to git log:"}}
4280 {allpaths path + {} {mc "Enter files and directories to include, one per line:"}}
4281 {cmd t50= + {} {mc "Command to generate more commits to include:"}}
4282 }
4283
4284# Convert $newviewopts($n, ...) into args for git log.
4285proc encode_view_opts {n} {
4286 global known_view_options newviewopts
4287
4288 set rargs [list]
4289 foreach opt $known_view_options {
4290 set patterns [lindex $opt 3]
4291 if {$patterns eq {}} continue
4292 set pattern [lindex $patterns 0]
4293
4294 if {[lindex $opt 1] eq "b"} {
4295 set val $newviewopts($n,[lindex $opt 0])
4296 if {$val} {
4297 lappend rargs $pattern
4298 }
4299 } elseif {[regexp {^r(\d+)$} [lindex $opt 1] type value]} {
4300 regexp {^(.*_)} [lindex $opt 0] uselessvar button_id
4301 set val $newviewopts($n,$button_id)
4302 if {$val eq $value} {
4303 lappend rargs $pattern
4304 }
4305 } else {
4306 set val $newviewopts($n,[lindex $opt 0])
4307 set val [string trim $val]
4308 if {$val ne {}} {
4309 set pfix [string range $pattern 0 end-1]
4310 lappend rargs $pfix$val
4311 }
4312 }
4313 }
4314 set rargs [concat $rargs [shellsplit $newviewopts($n,refs)]]
4315 return [concat $rargs [shellsplit $newviewopts($n,args)]]
4316}
4317
4318# Fill $newviewopts($n, ...) based on args for git log.
4319proc decode_view_opts {n view_args} {
4320 global known_view_options newviewopts
4321
4322 foreach opt $known_view_options {
4323 set id [lindex $opt 0]
4324 if {[lindex $opt 1] eq "b"} {
4325 # Checkboxes
4326 set val 0
4327 } elseif {[regexp {^r(\d+)$} [lindex $opt 1]]} {
4328 # Radiobuttons
4329 regexp {^(.*_)} $id uselessvar id
4330 set val 0
4331 } else {
4332 # Text fields
4333 set val {}
4334 }
4335 set newviewopts($n,$id) $val
4336 }
4337 set oargs [list]
4338 set refargs [list]
4339 foreach arg $view_args {
4340 if {[regexp -- {^-([0-9]+)$} $arg arg cnt]
4341 && ![info exists found(limit)]} {
4342 set newviewopts($n,limit) $cnt
4343 set found(limit) 1
4344 continue
4345 }
4346 catch { unset val }
4347 foreach opt $known_view_options {
4348 set id [lindex $opt 0]
4349 if {[info exists found($id)]} continue
4350 foreach pattern [lindex $opt 3] {
4351 if {![string match $pattern $arg]} continue
4352 if {[lindex $opt 1] eq "b"} {
4353 # Check buttons
4354 set val 1
4355 } elseif {[regexp {^r(\d+)$} [lindex $opt 1] match num]} {
4356 # Radio buttons
4357 regexp {^(.*_)} $id uselessvar id
4358 set val $num
4359 } else {
4360 # Text input fields
4361 set size [string length $pattern]
4362 set val [string range $arg [expr {$size-1}] end]
4363 }
4364 set newviewopts($n,$id) $val
4365 set found($id) 1
4366 break
4367 }
4368 if {[info exists val]} break
4369 }
4370 if {[info exists val]} continue
4371 if {[regexp {^-} $arg]} {
4372 lappend oargs $arg
4373 } else {
4374 lappend refargs $arg
4375 }
4376 }
4377 set newviewopts($n,refs) [shellarglist $refargs]
4378 set newviewopts($n,args) [shellarglist $oargs]
4379}
4380
4381proc edit_or_newview {} {
4382 global curview
4383
4384 if {$curview > 0} {
4385 editview
4386 } else {
4387 newview 0
4388 }
4389}
4390
4391proc editview {} {
4392 global curview
4393 global viewname viewperm newviewname newviewopts
4394 global viewargs viewargscmd
4395
4396 set top .gitkvedit-$curview
4397 if {[winfo exists $top]} {
4398 raise $top
4399 return
4400 }
4401 decode_view_opts $curview $viewargs($curview)
4402 set newviewname($curview) $viewname($curview)
4403 set newviewopts($curview,perm) $viewperm($curview)
4404 set newviewopts($curview,cmd) $viewargscmd($curview)
4405 vieweditor $top $curview "[mc "Gitk: edit view"] $viewname($curview)"
4406}
4407
4408proc vieweditor {top n title} {
4409 global newviewname newviewopts viewfiles bgcolor
4410 global known_view_options
4411
4412 ttk_toplevel $top
4413 wm title $top [concat $title [mc "-- criteria for selecting revisions"]]
4414 make_transient $top .
4415
4416 # View name
4417 ttk::frame $top.nfr
4418 ttk::label $top.nl -text [mc "View Name"]
4419 ttk::entry $top.name -width 20 -textvariable newviewname($n)
4420 pack $top.nfr -in $top -fill x -pady 5 -padx 3
4421 pack $top.nl -in $top.nfr -side left -padx {0 5}
4422 pack $top.name -in $top.nfr -side left -padx {0 25}
4423
4424 # View options
4425 set cframe $top.nfr
4426 set cexpand 0
4427 set cnt 0
4428 foreach opt $known_view_options {
4429 set id [lindex $opt 0]
4430 set type [lindex $opt 1]
4431 set flags [lindex $opt 2]
4432 set title [eval [lindex $opt 4]]
4433 set lxpad 0
4434
4435 if {$flags eq "+" || $flags eq "*"} {
4436 set cframe $top.fr$cnt
4437 incr cnt
4438 ttk::frame $cframe
4439 pack $cframe -in $top -fill x -pady 3 -padx 3
4440 set cexpand [expr {$flags eq "*"}]
4441 } elseif {$flags eq ".." || $flags eq "*."} {
4442 set cframe $top.fr$cnt
4443 incr cnt
4444 ttk::frame $cframe
4445 pack $cframe -in $top -fill x -pady 3 -padx [list 15 3]
4446 set cexpand [expr {$flags eq "*."}]
4447 } else {
4448 set lxpad 5
4449 }
4450
4451 if {$type eq "l"} {
4452 ttk::label $cframe.l_$id -text $title
4453 pack $cframe.l_$id -in $cframe -side left -pady [list 3 0] -anchor w
4454 } elseif {$type eq "b"} {
4455 ttk::checkbutton $cframe.c_$id -text $title -variable newviewopts($n,$id)
4456 pack $cframe.c_$id -in $cframe -side left \
4457 -padx [list $lxpad 0] -expand $cexpand -anchor w
4458 } elseif {[regexp {^r(\d+)$} $type type sz]} {
4459 regexp {^(.*_)} $id uselessvar button_id
4460 ttk::radiobutton $cframe.c_$id -text $title -variable newviewopts($n,$button_id) -value $sz
4461 pack $cframe.c_$id -in $cframe -side left \
4462 -padx [list $lxpad 0] -expand $cexpand -anchor w
4463 } elseif {[regexp {^t(\d+)$} $type type sz]} {
4464 ttk::label $cframe.l_$id -text $title
4465 ttk::entry $cframe.e_$id -width $sz -background $bgcolor \
4466 -textvariable newviewopts($n,$id)
4467 pack $cframe.l_$id -in $cframe -side left -padx [list $lxpad 0]
4468 pack $cframe.e_$id -in $cframe -side left -expand 1 -fill x
4469 } elseif {[regexp {^t(\d+)=$} $type type sz]} {
4470 ttk::label $cframe.l_$id -text $title
4471 ttk::entry $cframe.e_$id -width $sz -background $bgcolor \
4472 -textvariable newviewopts($n,$id)
4473 pack $cframe.l_$id -in $cframe -side top -pady [list 3 0] -anchor w
4474 pack $cframe.e_$id -in $cframe -side top -fill x
4475 } elseif {$type eq "path"} {
4476 ttk::label $top.l -text $title
4477 pack $top.l -in $top -side top -pady [list 3 0] -anchor w -padx 3
4478 text $top.t -width 40 -height 5 -background $bgcolor
4479 if {[info exists viewfiles($n)]} {
4480 foreach f $viewfiles($n) {
4481 $top.t insert end $f
4482 $top.t insert end "\n"
4483 }
4484 $top.t delete {end - 1c} end
4485 $top.t mark set insert 0.0
4486 }
4487 pack $top.t -in $top -side top -pady [list 0 5] -fill both -expand 1 -padx 3
4488 }
4489 }
4490
4491 ttk::frame $top.buts
4492 ttk::button $top.buts.ok -text [mc "OK"] -command [list newviewok $top $n]
4493 ttk::button $top.buts.apply -text [mc "Apply (F5)"] -command [list newviewok $top $n 1]
4494 ttk::button $top.buts.can -text [mc "Cancel"] -command [list destroy $top]
4495 bind $top <Control-Return> [list newviewok $top $n]
4496 bind $top <F5> [list newviewok $top $n 1]
4497 bind $top <Escape> [list destroy $top]
4498 grid $top.buts.ok $top.buts.apply $top.buts.can
4499 grid columnconfigure $top.buts 0 -weight 1 -uniform a
4500 grid columnconfigure $top.buts 1 -weight 1 -uniform a
4501 grid columnconfigure $top.buts 2 -weight 1 -uniform a
4502 pack $top.buts -in $top -side top -fill x
4503 focus $top.t
4504}
4505
4506proc doviewmenu {m first cmd op argv} {
4507 set nmenu [$m index end]
4508 for {set i $first} {$i <= $nmenu} {incr i} {
4509 if {[$m entrycget $i -command] eq $cmd} {
4510 eval $m $op $i $argv
4511 break
4512 }
4513 }
4514}
4515
4516proc allviewmenus {n op args} {
4517 # global viewhlmenu
4518
4519 doviewmenu .bar.view 5 [list showview $n] $op $args
4520 # doviewmenu $viewhlmenu 1 [list addvhighlight $n] $op $args
4521}
4522
4523proc newviewok {top n {apply 0}} {
4524 global nextviewnum newviewperm newviewname newishighlight
4525 global viewname viewfiles viewperm viewchanged selectedview curview
4526 global viewargs viewargscmd newviewopts viewhlmenu
4527
4528 if {[catch {
4529 set newargs [encode_view_opts $n]
4530 } err]} {
4531 error_popup "[mc "Error in commit selection arguments:"] $err" $top
4532 return
4533 }
4534 set files {}
4535 foreach f [split [$top.t get 0.0 end] "\n"] {
4536 set ft [string trim $f]
4537 if {$ft ne {}} {
4538 lappend files $ft
4539 }
4540 }
4541 if {![info exists viewfiles($n)]} {
4542 # creating a new view
4543 incr nextviewnum
4544 set viewname($n) $newviewname($n)
4545 set viewperm($n) $newviewopts($n,perm)
4546 set viewchanged($n) 1
4547 set viewfiles($n) $files
4548 set viewargs($n) $newargs
4549 set viewargscmd($n) $newviewopts($n,cmd)
4550 addviewmenu $n
4551 if {!$newishighlight} {
4552 run showview $n
4553 } else {
4554 run addvhighlight $n
4555 }
4556 } else {
4557 # editing an existing view
4558 set viewperm($n) $newviewopts($n,perm)
4559 set viewchanged($n) 1
4560 if {$newviewname($n) ne $viewname($n)} {
4561 set viewname($n) $newviewname($n)
4562 doviewmenu .bar.view 5 [list showview $n] \
4563 entryconf [list -label $viewname($n)]
4564 # doviewmenu $viewhlmenu 1 [list addvhighlight $n] \
4565 # entryconf [list -label $viewname($n) -value $viewname($n)]
4566 }
4567 if {$files ne $viewfiles($n) || $newargs ne $viewargs($n) || \
4568 $newviewopts($n,cmd) ne $viewargscmd($n)} {
4569 set viewfiles($n) $files
4570 set viewargs($n) $newargs
4571 set viewargscmd($n) $newviewopts($n,cmd)
4572 if {$curview == $n} {
4573 run reloadcommits
4574 }
4575 }
4576 }
4577 if {$apply} return
4578 catch {destroy $top}
4579}
4580
4581proc delview {} {
4582 global curview viewperm hlview selectedhlview viewchanged
4583
4584 if {$curview == 0} return
4585 if {[info exists hlview] && $hlview == $curview} {
4586 set selectedhlview [mc "None"]
4587 unset hlview
4588 }
4589 allviewmenus $curview delete
4590 set viewperm($curview) 0
4591 set viewchanged($curview) 1
4592 showview 0
4593}
4594
4595proc addviewmenu {n} {
4596 global viewname viewhlmenu
4597
4598 .bar.view add radiobutton -label $viewname($n) \
4599 -command [list showview $n] -variable selectedview -value $n
4600 #$viewhlmenu add radiobutton -label $viewname($n) \
4601 # -command [list addvhighlight $n] -variable selectedhlview
4602}
4603
4604proc showview {n} {
4605 global curview cached_commitrow ordertok
4606 global displayorder parentlist rowidlist rowisopt rowfinal
4607 global colormap rowtextx nextcolor canvxmax
4608 global numcommits viewcomplete
4609 global selectedline currentid canv canvy0
4610 global treediffs
4611 global pending_select mainheadid
4612 global commitidx
4613 global selectedview
4614 global hlview selectedhlview commitinterest
4615
4616 if {$n == $curview} return
4617 set selid {}
4618 set ymax [lindex [$canv cget -scrollregion] 3]
4619 set span [$canv yview]
4620 set ytop [expr {[lindex $span 0] * $ymax}]
4621 set ybot [expr {[lindex $span 1] * $ymax}]
4622 set yscreen [expr {($ybot - $ytop) / 2}]
4623 if {$selectedline ne {}} {
4624 set selid $currentid
4625 set y [yc $selectedline]
4626 if {$ytop < $y && $y < $ybot} {
4627 set yscreen [expr {$y - $ytop}]
4628 }
4629 } elseif {[info exists pending_select]} {
4630 set selid $pending_select
4631 unset pending_select
4632 }
4633 unselectline
4634 normalline
4635 unset -nocomplain treediffs
4636 clear_display
4637 if {[info exists hlview] && $hlview == $n} {
4638 unset hlview
4639 set selectedhlview [mc "None"]
4640 }
4641 unset -nocomplain commitinterest
4642 unset -nocomplain cached_commitrow
4643 unset -nocomplain ordertok
4644
4645 set curview $n
4646 set selectedview $n
4647 .bar.view entryconf [mca "&Edit view..."] -state [expr {$n == 0? "disabled": "normal"}]
4648 .bar.view entryconf [mca "&Delete view"] -state [expr {$n == 0? "disabled": "normal"}]
4649
4650 run refill_reflist
4651 if {![info exists viewcomplete($n)]} {
4652 getcommits $selid
4653 return
4654 }
4655
4656 set displayorder {}
4657 set parentlist {}
4658 set rowidlist {}
4659 set rowisopt {}
4660 set rowfinal {}
4661 set numcommits $commitidx($n)
4662
4663 unset -nocomplain colormap
4664 unset -nocomplain rowtextx
4665 set nextcolor 0
4666 set canvxmax [$canv cget -width]
4667 set curview $n
4668 set row 0
4669 setcanvscroll
4670 set yf 0
4671 set row {}
4672 if {$selid ne {} && [commitinview $selid $n]} {
4673 set row [rowofcommit $selid]
4674 # try to get the selected row in the same position on the screen
4675 set ymax [lindex [$canv cget -scrollregion] 3]
4676 set ytop [expr {[yc $row] - $yscreen}]
4677 if {$ytop < 0} {
4678 set ytop 0
4679 }
4680 set yf [expr {$ytop * 1.0 / $ymax}]
4681 }
4682 allcanvs yview moveto $yf
4683 drawvisible
4684 if {$row ne {}} {
4685 selectline $row 0
4686 } elseif {!$viewcomplete($n)} {
4687 reset_pending_select $selid
4688 } else {
4689 reset_pending_select {}
4690
4691 if {[commitinview $pending_select $curview]} {
4692 selectline [rowofcommit $pending_select] 1
4693 } else {
4694 set row [first_real_row]
4695 if {$row < $numcommits} {
4696 selectline $row 0
4697 }
4698 }
4699 }
4700 if {!$viewcomplete($n)} {
4701 if {$numcommits == 0} {
4702 show_status [mc "Reading commits..."]
4703 }
4704 } elseif {$numcommits == 0} {
4705 show_status [mc "No commits selected"]
4706 }
4707 set_window_title
4708}
4709
4710# Stuff relating to the highlighting facility
4711
4712proc ishighlighted {id} {
4713 global vhighlights fhighlights nhighlights rhighlights
4714
4715 if {[info exists nhighlights($id)] && $nhighlights($id) > 0} {
4716 return $nhighlights($id)
4717 }
4718 if {[info exists vhighlights($id)] && $vhighlights($id) > 0} {
4719 return $vhighlights($id)
4720 }
4721 if {[info exists fhighlights($id)] && $fhighlights($id) > 0} {
4722 return $fhighlights($id)
4723 }
4724 if {[info exists rhighlights($id)] && $rhighlights($id) > 0} {
4725 return $rhighlights($id)
4726 }
4727 return 0
4728}
4729
4730proc bolden {id font} {
4731 global canv linehtag currentid boldids need_redisplay markedid
4732
4733 # need_redisplay = 1 means the display is stale and about to be redrawn
4734 if {$need_redisplay} return
4735 lappend boldids $id
4736 $canv itemconf $linehtag($id) -font $font
4737 if {[info exists currentid] && $id eq $currentid} {
4738 $canv delete secsel
4739 set t [eval $canv create rect [$canv bbox $linehtag($id)] \
4740 -outline {{}} -tags secsel \
4741 -fill [$canv cget -selectbackground]]
4742 $canv lower $t
4743 }
4744 if {[info exists markedid] && $id eq $markedid} {
4745 make_idmark $id
4746 }
4747}
4748
4749proc bolden_name {id font} {
4750 global canv2 linentag currentid boldnameids need_redisplay
4751
4752 if {$need_redisplay} return
4753 lappend boldnameids $id
4754 $canv2 itemconf $linentag($id) -font $font
4755 if {[info exists currentid] && $id eq $currentid} {
4756 $canv2 delete secsel
4757 set t [eval $canv2 create rect [$canv2 bbox $linentag($id)] \
4758 -outline {{}} -tags secsel \
4759 -fill [$canv2 cget -selectbackground]]
4760 $canv2 lower $t
4761 }
4762}
4763
4764proc unbolden {} {
4765 global boldids
4766
4767 set stillbold {}
4768 foreach id $boldids {
4769 if {![ishighlighted $id]} {
4770 bolden $id mainfont
4771 } else {
4772 lappend stillbold $id
4773 }
4774 }
4775 set boldids $stillbold
4776}
4777
4778proc addvhighlight {n} {
4779 global hlview viewcomplete curview vhl_done commitidx
4780
4781 if {[info exists hlview]} {
4782 delvhighlight
4783 }
4784 set hlview $n
4785 if {$n != $curview && ![info exists viewcomplete($n)]} {
4786 start_rev_list $n
4787 }
4788 set vhl_done $commitidx($hlview)
4789 if {$vhl_done > 0} {
4790 drawvisible
4791 }
4792}
4793
4794proc delvhighlight {} {
4795 global hlview vhighlights
4796
4797 if {![info exists hlview]} return
4798 unset hlview
4799 unset -nocomplain vhighlights
4800 unbolden
4801}
4802
4803proc vhighlightmore {} {
4804 global hlview vhl_done commitidx vhighlights curview
4805
4806 set max $commitidx($hlview)
4807 set vr [visiblerows]
4808 set r0 [lindex $vr 0]
4809 set r1 [lindex $vr 1]
4810 for {set i $vhl_done} {$i < $max} {incr i} {
4811 set id [commitonrow $i $hlview]
4812 if {[commitinview $id $curview]} {
4813 set row [rowofcommit $id]
4814 if {$r0 <= $row && $row <= $r1} {
4815 if {![highlighted $row]} {
4816 bolden $id mainfontbold
4817 }
4818 set vhighlights($id) 1
4819 }
4820 }
4821 }
4822 set vhl_done $max
4823 return 0
4824}
4825
4826proc askvhighlight {row id} {
4827 global hlview vhighlights iddrawn
4828
4829 if {[commitinview $id $hlview]} {
4830 if {[info exists iddrawn($id)] && ![ishighlighted $id]} {
4831 bolden $id mainfontbold
4832 }
4833 set vhighlights($id) 1
4834 } else {
4835 set vhighlights($id) 0
4836 }
4837}
4838
4839proc hfiles_change {} {
4840 global highlight_files filehighlight fhighlights fh_serial
4841 global highlight_paths
4842
4843 if {[info exists filehighlight]} {
4844 # delete previous highlights
4845 catch {close $filehighlight}
4846 unset filehighlight
4847 unset -nocomplain fhighlights
4848 unbolden
4849 unhighlight_filelist
4850 }
4851 set highlight_paths {}
4852 after cancel do_file_hl $fh_serial
4853 incr fh_serial
4854 if {$highlight_files ne {}} {
4855 after 300 do_file_hl $fh_serial
4856 }
4857}
4858
4859proc gdttype_change {name ix op} {
4860 global gdttype highlight_files findstring findpattern
4861
4862 stopfinding
4863 if {$findstring ne {}} {
4864 if {$gdttype eq [mc "containing:"]} {
4865 if {$highlight_files ne {}} {
4866 set highlight_files {}
4867 hfiles_change
4868 }
4869 findcom_change
4870 } else {
4871 if {$findpattern ne {}} {
4872 set findpattern {}
4873 findcom_change
4874 }
4875 set highlight_files $findstring
4876 hfiles_change
4877 }
4878 drawvisible
4879 }
4880 # enable/disable findtype/findloc menus too
4881}
4882
4883proc find_change {name ix op} {
4884 global gdttype findstring highlight_files
4885
4886 stopfinding
4887 if {$gdttype eq [mc "containing:"]} {
4888 findcom_change
4889 } else {
4890 if {$highlight_files ne $findstring} {
4891 set highlight_files $findstring
4892 hfiles_change
4893 }
4894 }
4895 drawvisible
4896}
4897
4898proc findcom_change args {
4899 global nhighlights boldnameids
4900 global findpattern findtype findstring gdttype
4901
4902 stopfinding
4903 # delete previous highlights, if any
4904 foreach id $boldnameids {
4905 bolden_name $id mainfont
4906 }
4907 set boldnameids {}
4908 unset -nocomplain nhighlights
4909 unbolden
4910 unmarkmatches
4911 if {$gdttype ne [mc "containing:"] || $findstring eq {}} {
4912 set findpattern {}
4913 } elseif {$findtype eq [mc "Regexp"]} {
4914 set findpattern $findstring
4915 } else {
4916 set e [string map {"*" "\\*" "?" "\\?" "\[" "\\\[" "\\" "\\\\"} \
4917 $findstring]
4918 set findpattern "*$e*"
4919 }
4920}
4921
4922proc makepatterns {l} {
4923 set ret {}
4924 foreach e $l {
4925 set ee [string map {"*" "\\*" "?" "\\?" "\[" "\\\[" "\\" "\\\\"} $e]
4926 if {[string index $ee end] eq "/"} {
4927 lappend ret "$ee*"
4928 } else {
4929 lappend ret $ee
4930 lappend ret "$ee/*"
4931 }
4932 }
4933 return $ret
4934}
4935
4936proc do_file_hl {serial} {
4937 global highlight_files filehighlight highlight_paths gdttype fhl_list
4938 global cdup findtype
4939
4940 if {$gdttype eq [mc "touching paths:"]} {
4941 # If "exact" match then convert backslashes to forward slashes.
4942 # Most useful to support Windows-flavoured file paths.
4943 if {$findtype eq [mc "Exact"]} {
4944 set highlight_files [string map {"\\" "/"} $highlight_files]
4945 }
4946 if {[catch {set paths [shellsplit $highlight_files]}]} return
4947 set highlight_paths [makepatterns $paths]
4948 highlight_filelist
4949 set relative_paths {}
4950 foreach path $paths {
4951 lappend relative_paths [file join $cdup $path]
4952 }
4953 set gdtargs [concat -- $relative_paths]
4954 } elseif {$gdttype eq [mc "adding/removing string:"]} {
4955 set gdtargs [list "-S$highlight_files"]
4956 } elseif {$gdttype eq [mc "changing lines matching:"]} {
4957 set gdtargs [list "-G$highlight_files"]
4958 } else {
4959 # must be "containing:", i.e. we're searching commit info
4960 return
4961 }
4962 set cmd [concat git diff-tree -r -s --stdin $gdtargs]
4963 set filehighlight [safe_open_command_rw $cmd]
4964 fconfigure $filehighlight -blocking 0
4965 filerun $filehighlight readfhighlight
4966 set fhl_list {}
4967 drawvisible
4968 flushhighlights
4969}
4970
4971proc flushhighlights {} {
4972 global filehighlight fhl_list
4973
4974 if {[info exists filehighlight]} {
4975 lappend fhl_list {}
4976 puts $filehighlight ""
4977 flush $filehighlight
4978 }
4979}
4980
4981proc askfilehighlight {row id} {
4982 global filehighlight fhighlights fhl_list
4983
4984 lappend fhl_list $id
4985 set fhighlights($id) -1
4986 puts $filehighlight $id
4987}
4988
4989proc readfhighlight {} {
4990 global filehighlight fhighlights curview iddrawn
4991 global fhl_list find_dirn
4992
4993 if {![info exists filehighlight]} {
4994 return 0
4995 }
4996 set nr 0
4997 while {[incr nr] <= 100 && [gets $filehighlight line] >= 0} {
4998 set line [string trim $line]
4999 set i [lsearch -exact $fhl_list $line]
5000 if {$i < 0} continue
5001 for {set j 0} {$j < $i} {incr j} {
5002 set id [lindex $fhl_list $j]
5003 set fhighlights($id) 0
5004 }
5005 set fhl_list [lrange $fhl_list [expr {$i+1}] end]
5006 if {$line eq {}} continue
5007 if {![commitinview $line $curview]} continue
5008 if {[info exists iddrawn($line)] && ![ishighlighted $line]} {
5009 bolden $line mainfontbold
5010 }
5011 set fhighlights($line) 1
5012 }
5013 if {[eof $filehighlight]} {
5014 # strange...
5015 puts "oops, git diff-tree died"
5016 catch {close $filehighlight}
5017 unset filehighlight
5018 return 0
5019 }
5020 if {[info exists find_dirn]} {
5021 run findmore
5022 }
5023 return 1
5024}
5025
5026proc doesmatch {f} {
5027 global findtype findpattern
5028
5029 if {$findtype eq [mc "Regexp"]} {
5030 return [regexp $findpattern $f]
5031 } elseif {$findtype eq [mc "IgnCase"]} {
5032 return [string match -nocase $findpattern $f]
5033 } else {
5034 return [string match $findpattern $f]
5035 }
5036}
5037
5038proc askfindhighlight {row id} {
5039 global nhighlights commitinfo iddrawn
5040 global findloc
5041 global markingmatches
5042
5043 if {![info exists commitinfo($id)]} {
5044 getcommit $id
5045 }
5046 set info $commitinfo($id)
5047 set isbold 0
5048 set fldtypes [list [mc Headline] [mc Author] "" [mc Committer] "" [mc Comments]]
5049 foreach f $info ty $fldtypes {
5050 if {$ty eq ""} continue
5051 if {($findloc eq [mc "All fields"] || $findloc eq $ty) &&
5052 [doesmatch $f]} {
5053 if {$ty eq [mc "Author"]} {
5054 set isbold 2
5055 break
5056 }
5057 set isbold 1
5058 }
5059 }
5060 if {$isbold && [info exists iddrawn($id)]} {
5061 if {![ishighlighted $id]} {
5062 bolden $id mainfontbold
5063 if {$isbold > 1} {
5064 bolden_name $id mainfontbold
5065 }
5066 }
5067 if {$markingmatches} {
5068 markrowmatches $row $id
5069 }
5070 }
5071 set nhighlights($id) $isbold
5072}
5073
5074proc markrowmatches {row id} {
5075 global canv canv2 linehtag linentag commitinfo findloc
5076
5077 set headline [lindex $commitinfo($id) 0]
5078 set author [lindex $commitinfo($id) 1]
5079 $canv delete match$row
5080 $canv2 delete match$row
5081 if {$findloc eq [mc "All fields"] || $findloc eq [mc "Headline"]} {
5082 set m [findmatches $headline]
5083 if {$m ne {}} {
5084 markmatches $canv $row $headline $linehtag($id) $m \
5085 [$canv itemcget $linehtag($id) -font] $row
5086 }
5087 }
5088 if {$findloc eq [mc "All fields"] || $findloc eq [mc "Author"]} {
5089 set m [findmatches $author]
5090 if {$m ne {}} {
5091 markmatches $canv2 $row $author $linentag($id) $m \
5092 [$canv2 itemcget $linentag($id) -font] $row
5093 }
5094 }
5095}
5096
5097proc vrel_change {name ix op} {
5098 global highlight_related
5099
5100 rhighlight_none
5101 if {$highlight_related ne [mc "None"]} {
5102 run drawvisible
5103 }
5104}
5105
5106# prepare for testing whether commits are descendents or ancestors of a
5107proc rhighlight_sel {a} {
5108 global descendent desc_todo ancestor anc_todo
5109 global highlight_related
5110
5111 unset -nocomplain descendent
5112 set desc_todo [list $a]
5113 unset -nocomplain ancestor
5114 set anc_todo [list $a]
5115 if {$highlight_related ne [mc "None"]} {
5116 rhighlight_none
5117 run drawvisible
5118 }
5119}
5120
5121proc rhighlight_none {} {
5122 global rhighlights
5123
5124 unset -nocomplain rhighlights
5125 unbolden
5126}
5127
5128proc is_descendent {a} {
5129 global curview children descendent desc_todo
5130
5131 set v $curview
5132 set la [rowofcommit $a]
5133 set todo $desc_todo
5134 set leftover {}
5135 set done 0
5136 for {set i 0} {$i < [llength $todo]} {incr i} {
5137 set do [lindex $todo $i]
5138 if {[rowofcommit $do] < $la} {
5139 lappend leftover $do
5140 continue
5141 }
5142 foreach nk $children($v,$do) {
5143 if {![info exists descendent($nk)]} {
5144 set descendent($nk) 1
5145 lappend todo $nk
5146 if {$nk eq $a} {
5147 set done 1
5148 }
5149 }
5150 }
5151 if {$done} {
5152 set desc_todo [concat $leftover [lrange $todo [expr {$i+1}] end]]
5153 return
5154 }
5155 }
5156 set descendent($a) 0
5157 set desc_todo $leftover
5158}
5159
5160proc is_ancestor {a} {
5161 global curview parents ancestor anc_todo
5162
5163 set v $curview
5164 set la [rowofcommit $a]
5165 set todo $anc_todo
5166 set leftover {}
5167 set done 0
5168 for {set i 0} {$i < [llength $todo]} {incr i} {
5169 set do [lindex $todo $i]
5170 if {![commitinview $do $v] || [rowofcommit $do] > $la} {
5171 lappend leftover $do
5172 continue
5173 }
5174 foreach np $parents($v,$do) {
5175 if {![info exists ancestor($np)]} {
5176 set ancestor($np) 1
5177 lappend todo $np
5178 if {$np eq $a} {
5179 set done 1
5180 }
5181 }
5182 }
5183 if {$done} {
5184 set anc_todo [concat $leftover [lrange $todo [expr {$i+1}] end]]
5185 return
5186 }
5187 }
5188 set ancestor($a) 0
5189 set anc_todo $leftover
5190}
5191
5192proc askrelhighlight {row id} {
5193 global descendent highlight_related iddrawn rhighlights
5194 global selectedline ancestor
5195
5196 if {$selectedline eq {}} return
5197 set isbold 0
5198 if {$highlight_related eq [mc "Descendant"] ||
5199 $highlight_related eq [mc "Not descendant"]} {
5200 if {![info exists descendent($id)]} {
5201 is_descendent $id
5202 }
5203 if {$descendent($id) == ($highlight_related eq [mc "Descendant"])} {
5204 set isbold 1
5205 }
5206 } elseif {$highlight_related eq [mc "Ancestor"] ||
5207 $highlight_related eq [mc "Not ancestor"]} {
5208 if {![info exists ancestor($id)]} {
5209 is_ancestor $id
5210 }
5211 if {$ancestor($id) == ($highlight_related eq [mc "Ancestor"])} {
5212 set isbold 1
5213 }
5214 }
5215 if {[info exists iddrawn($id)]} {
5216 if {$isbold && ![ishighlighted $id]} {
5217 bolden $id mainfontbold
5218 }
5219 }
5220 set rhighlights($id) $isbold
5221}
5222
5223# Graph layout functions
5224
5225proc shortids {ids} {
5226 global hashlength
5227
5228 set res {}
5229 foreach id $ids {
5230 if {[llength $id] > 1} {
5231 lappend res [shortids $id]
5232 } elseif {[regexp [string map "@@ $hashlength" {^[0-9a-f]{@@}$}] $id]} {
5233 lappend res [string range $id 0 7]
5234 } else {
5235 lappend res $id
5236 }
5237 }
5238 return $res
5239}
5240
5241proc ntimes {n o} {
5242 set ret {}
5243 set o [list $o]
5244 for {set mask 1} {$mask <= $n} {incr mask $mask} {
5245 if {($n & $mask) != 0} {
5246 set ret [concat $ret $o]
5247 }
5248 set o [concat $o $o]
5249 }
5250 return $ret
5251}
5252
5253proc ordertoken {id} {
5254 global ordertok curview varcid varcstart varctok curview parents children
5255 global nullid nullid2
5256
5257 if {[info exists ordertok($id)]} {
5258 return $ordertok($id)
5259 }
5260 set origid $id
5261 set todo {}
5262 while {1} {
5263 if {[info exists varcid($curview,$id)]} {
5264 set a $varcid($curview,$id)
5265 set p [lindex $varcstart($curview) $a]
5266 } else {
5267 set p [lindex $children($curview,$id) 0]
5268 }
5269 if {[info exists ordertok($p)]} {
5270 set tok $ordertok($p)
5271 break
5272 }
5273 set id [first_real_child $curview,$p]
5274 if {$id eq {}} {
5275 # it's a root
5276 set tok [lindex $varctok($curview) $varcid($curview,$p)]
5277 break
5278 }
5279 if {[llength $parents($curview,$id)] == 1} {
5280 lappend todo [list $p {}]
5281 } else {
5282 set j [lsearch -exact $parents($curview,$id) $p]
5283 if {$j < 0} {
5284 puts "oops didn't find [shortids $p] in parents of [shortids $id]"
5285 }
5286 lappend todo [list $p [strrep $j]]
5287 }
5288 }
5289 for {set i [llength $todo]} {[incr i -1] >= 0} {} {
5290 set p [lindex $todo $i 0]
5291 append tok [lindex $todo $i 1]
5292 set ordertok($p) $tok
5293 }
5294 set ordertok($origid) $tok
5295 return $tok
5296}
5297
5298# Work out where id should go in idlist so that order-token
5299# values increase from left to right
5300proc idcol {idlist id {i 0}} {
5301 set t [ordertoken $id]
5302 if {$i < 0} {
5303 set i 0
5304 }
5305 if {$i >= [llength $idlist] || $t < [ordertoken [lindex $idlist $i]]} {
5306 if {$i > [llength $idlist]} {
5307 set i [llength $idlist]
5308 }
5309 while {[incr i -1] >= 0 && $t < [ordertoken [lindex $idlist $i]]} {}
5310 incr i
5311 } else {
5312 if {$t > [ordertoken [lindex $idlist $i]]} {
5313 while {[incr i] < [llength $idlist] &&
5314 $t >= [ordertoken [lindex $idlist $i]]} {}
5315 }
5316 }
5317 return $i
5318}
5319
5320proc initlayout {} {
5321 global rowidlist rowisopt rowfinal displayorder parentlist
5322 global numcommits canvxmax canv
5323 global nextcolor
5324 global colormap rowtextx
5325
5326 set numcommits 0
5327 set displayorder {}
5328 set parentlist {}
5329 set nextcolor 0
5330 set rowidlist {}
5331 set rowisopt {}
5332 set rowfinal {}
5333 set canvxmax [$canv cget -width]
5334 unset -nocomplain colormap
5335 unset -nocomplain rowtextx
5336 setcanvscroll
5337}
5338
5339proc setcanvscroll {} {
5340 global canv canv2 canv3 numcommits linespc canvxmax canvy0
5341 global lastscrollset lastscrollrows
5342
5343 set ymax [expr {$canvy0 + ($numcommits - 0.5) * $linespc + 2}]
5344 $canv conf -scrollregion [list 0 0 $canvxmax $ymax]
5345 $canv2 conf -scrollregion [list 0 0 0 $ymax]
5346 $canv3 conf -scrollregion [list 0 0 0 $ymax]
5347 set lastscrollset [clock clicks -milliseconds]
5348 set lastscrollrows $numcommits
5349}
5350
5351proc visiblerows {} {
5352 global canv numcommits linespc
5353
5354 set ymax [lindex [$canv cget -scrollregion] 3]
5355 if {$ymax eq {} || $ymax == 0} return
5356 set f [$canv yview]
5357 set y0 [expr {int([lindex $f 0] * $ymax)}]
5358 set r0 [expr {int(($y0 - 3) / $linespc) - 1}]
5359 if {$r0 < 0} {
5360 set r0 0
5361 }
5362 set y1 [expr {int([lindex $f 1] * $ymax)}]
5363 set r1 [expr {int(($y1 - 3) / $linespc) + 1}]
5364 if {$r1 >= $numcommits} {
5365 set r1 [expr {$numcommits - 1}]
5366 }
5367 return [list $r0 $r1]
5368}
5369
5370proc layoutmore {} {
5371 global commitidx viewcomplete curview
5372 global numcommits pending_select curview
5373 global lastscrollset lastscrollrows
5374
5375 if {$lastscrollrows < 100 || $viewcomplete($curview) ||
5376 [clock clicks -milliseconds] - $lastscrollset > 500} {
5377 setcanvscroll
5378 }
5379 if {[info exists pending_select] &&
5380 [commitinview $pending_select $curview]} {
5381 update
5382 selectline [rowofcommit $pending_select] 1
5383 }
5384 drawvisible
5385}
5386
5387# With path limiting, we mightn't get the actual HEAD commit,
5388# so ask git rev-list what is the first ancestor of HEAD that
5389# touches a file in the path limit.
5390proc get_viewmainhead {view} {
5391 global viewmainheadid vfilelimit viewinstances mainheadid
5392
5393 catch {
5394 set rfd [safe_open_command [concat git rev-list -1 $mainheadid \
5395 -- $vfilelimit($view)]]
5396 set j [reg_instance $rfd]
5397 lappend viewinstances($view) $j
5398 fconfigure $rfd -blocking 0
5399 filerun $rfd [list getviewhead $rfd $j $view]
5400 set viewmainheadid($curview) {}
5401 }
5402}
5403
5404# git rev-list should give us just 1 line to use as viewmainheadid($view)
5405proc getviewhead {fd inst view} {
5406 global viewmainheadid commfd curview viewinstances showlocalchanges
5407 global hashlength
5408
5409 set id {}
5410 if {[gets $fd line] < 0} {
5411 if {![eof $fd]} {
5412 return 1
5413 }
5414 } elseif {[string length $line] == $hashlength && [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
5430proc 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
5441proc 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
5454proc dodiffindex {} {
5455 global lserial showlocalchanges vfilelimit curview
5456 global hasworktree
5457
5458 if {!$showlocalchanges || !$hasworktree} return
5459 incr lserial
5460 set cmd "git diff-index --cached --ignore-submodules=dirty HEAD"
5461 if {$vfilelimit($curview) ne {}} {
5462 set cmd [concat $cmd -- $vfilelimit($curview)]
5463 }
5464 set fd [safe_open_command $cmd]
5465 fconfigure $fd -blocking 0
5466 set i [reg_instance $fd]
5467 filerun $fd [list readdiffindex $fd $lserial $i]
5468}
5469
5470proc readdiffindex {fd serial inst} {
5471 global viewmainheadid nullid nullid2 curview commitinfo commitdata lserial
5472 global vfilelimit
5473
5474 set isdiff 1
5475 if {[gets $fd line] < 0} {
5476 if {![eof $fd]} {
5477 return 1
5478 }
5479 set isdiff 0
5480 }
5481 # we only need to see one line and we don't really care what it says...
5482 stop_instance $inst
5483
5484 if {$serial != $lserial} {
5485 return 0
5486 }
5487
5488 # now see if there are any local changes not checked in to the index
5489 set cmd "git diff-files"
5490 if {$vfilelimit($curview) ne {}} {
5491 set cmd [concat $cmd -- $vfilelimit($curview)]
5492 }
5493 set fd [safe_open_command $cmd]
5494 fconfigure $fd -blocking 0
5495 set i [reg_instance $fd]
5496 filerun $fd [list readdifffiles $fd $serial $i]
5497
5498 if {$isdiff && ![commitinview $nullid2 $curview]} {
5499 # add the line for the changes in the index to the graph
5500 set hl [mc "Local changes checked in to index but not committed"]
5501 set commitinfo($nullid2) [list $hl {} {} {} {} " $hl\n"]
5502 set commitdata($nullid2) "\n $hl\n"
5503 if {[commitinview $nullid $curview]} {
5504 removefakerow $nullid
5505 }
5506 insertfakerow $nullid2 $viewmainheadid($curview)
5507 } elseif {!$isdiff && [commitinview $nullid2 $curview]} {
5508 if {[commitinview $nullid $curview]} {
5509 removefakerow $nullid
5510 }
5511 removefakerow $nullid2
5512 }
5513 return 0
5514}
5515
5516proc readdifffiles {fd serial inst} {
5517 global viewmainheadid nullid nullid2 curview
5518 global commitinfo commitdata lserial
5519
5520 set isdiff 1
5521 if {[gets $fd line] < 0} {
5522 if {![eof $fd]} {
5523 return 1
5524 }
5525 set isdiff 0
5526 }
5527 # we only need to see one line and we don't really care what it says...
5528 stop_instance $inst
5529
5530 if {$serial != $lserial} {
5531 return 0
5532 }
5533
5534 if {$isdiff && ![commitinview $nullid $curview]} {
5535 # add the line for the local diff to the graph
5536 set hl [mc "Local uncommitted changes, not checked in to index"]
5537 set commitinfo($nullid) [list $hl {} {} {} {} " $hl\n"]
5538 set commitdata($nullid) "\n $hl\n"
5539 if {[commitinview $nullid2 $curview]} {
5540 set p $nullid2
5541 } else {
5542 set p $viewmainheadid($curview)
5543 }
5544 insertfakerow $nullid $p
5545 } elseif {!$isdiff && [commitinview $nullid $curview]} {
5546 removefakerow $nullid
5547 }
5548 return 0
5549}
5550
5551proc nextuse {id row} {
5552 global curview children
5553
5554 if {[info exists children($curview,$id)]} {
5555 foreach kid $children($curview,$id) {
5556 if {![commitinview $kid $curview]} {
5557 return -1
5558 }
5559 if {[rowofcommit $kid] > $row} {
5560 return [rowofcommit $kid]
5561 }
5562 }
5563 }
5564 if {[commitinview $id $curview]} {
5565 return [rowofcommit $id]
5566 }
5567 return -1
5568}
5569
5570proc prevuse {id row} {
5571 global curview children
5572
5573 set ret -1
5574 if {[info exists children($curview,$id)]} {
5575 foreach kid $children($curview,$id) {
5576 if {![commitinview $kid $curview]} break
5577 if {[rowofcommit $kid] < $row} {
5578 set ret [rowofcommit $kid]
5579 }
5580 }
5581 }
5582 return $ret
5583}
5584
5585proc make_idlist {row} {
5586 global displayorder parentlist uparrowlen downarrowlen mingaplen
5587 global commitidx curview children
5588
5589 set r [expr {$row - $mingaplen - $downarrowlen - 1}]
5590 if {$r < 0} {
5591 set r 0
5592 }
5593 set ra [expr {$row - $downarrowlen}]
5594 if {$ra < 0} {
5595 set ra 0
5596 }
5597 set rb [expr {$row + $uparrowlen}]
5598 if {$rb > $commitidx($curview)} {
5599 set rb $commitidx($curview)
5600 }
5601 make_disporder $r [expr {$rb + 1}]
5602 set ids {}
5603 for {} {$r < $ra} {incr r} {
5604 set nextid [lindex $displayorder [expr {$r + 1}]]
5605 foreach p [lindex $parentlist $r] {
5606 if {$p eq $nextid} continue
5607 set rn [nextuse $p $r]
5608 if {$rn >= $row &&
5609 $rn <= $r + $downarrowlen + $mingaplen + $uparrowlen} {
5610 lappend ids [list [ordertoken $p] $p]
5611 }
5612 }
5613 }
5614 for {} {$r < $row} {incr r} {
5615 set nextid [lindex $displayorder [expr {$r + 1}]]
5616 foreach p [lindex $parentlist $r] {
5617 if {$p eq $nextid} continue
5618 set rn [nextuse $p $r]
5619 if {$rn < 0 || $rn >= $row} {
5620 lappend ids [list [ordertoken $p] $p]
5621 }
5622 }
5623 }
5624 set id [lindex $displayorder $row]
5625 lappend ids [list [ordertoken $id] $id]
5626 while {$r < $rb} {
5627 foreach p [lindex $parentlist $r] {
5628 set firstkid [lindex $children($curview,$p) 0]
5629 if {[rowofcommit $firstkid] < $row} {
5630 lappend ids [list [ordertoken $p] $p]
5631 }
5632 }
5633 incr r
5634 set id [lindex $displayorder $r]
5635 if {$id ne {}} {
5636 set firstkid [lindex $children($curview,$id) 0]
5637 if {$firstkid ne {} && [rowofcommit $firstkid] < $row} {
5638 lappend ids [list [ordertoken $id] $id]
5639 }
5640 }
5641 }
5642 set idlist {}
5643 foreach idx [lsort -unique $ids] {
5644 lappend idlist [lindex $idx 1]
5645 }
5646 return $idlist
5647}
5648
5649proc rowsequal {a b} {
5650 while {[set i [lsearch -exact $a {}]] >= 0} {
5651 set a [lreplace $a $i $i]
5652 }
5653 while {[set i [lsearch -exact $b {}]] >= 0} {
5654 set b [lreplace $b $i $i]
5655 }
5656 return [expr {$a eq $b}]
5657}
5658
5659proc makeupline {id row rend col} {
5660 global rowidlist uparrowlen downarrowlen mingaplen
5661
5662 for {set r $rend} {1} {set r $rstart} {
5663 set rstart [prevuse $id $r]
5664 if {$rstart < 0} return
5665 if {$rstart < $row} break
5666 }
5667 if {$rstart + $uparrowlen + $mingaplen + $downarrowlen < $rend} {
5668 set rstart [expr {$rend - $uparrowlen - 1}]
5669 }
5670 for {set r $rstart} {[incr r] <= $row} {} {
5671 set idlist [lindex $rowidlist $r]
5672 if {$idlist ne {} && [lsearch -exact $idlist $id] < 0} {
5673 set col [idcol $idlist $id $col]
5674 lset rowidlist $r [linsert $idlist $col $id]
5675 changedrow $r
5676 }
5677 }
5678}
5679
5680proc layoutrows {row endrow} {
5681 global rowidlist rowisopt rowfinal displayorder
5682 global uparrowlen downarrowlen maxwidth mingaplen
5683 global children parentlist
5684 global commitidx viewcomplete curview
5685
5686 make_disporder [expr {$row - 1}] [expr {$endrow + $uparrowlen}]
5687 set idlist {}
5688 if {$row > 0} {
5689 set rm1 [expr {$row - 1}]
5690 foreach id [lindex $rowidlist $rm1] {
5691 if {$id ne {}} {
5692 lappend idlist $id
5693 }
5694 }
5695 set final [lindex $rowfinal $rm1]
5696 }
5697 for {} {$row < $endrow} {incr row} {
5698 set rm1 [expr {$row - 1}]
5699 if {$rm1 < 0 || $idlist eq {}} {
5700 set idlist [make_idlist $row]
5701 set final 1
5702 } else {
5703 set id [lindex $displayorder $rm1]
5704 set col [lsearch -exact $idlist $id]
5705 set idlist [lreplace $idlist $col $col]
5706 foreach p [lindex $parentlist $rm1] {
5707 if {[lsearch -exact $idlist $p] < 0} {
5708 set col [idcol $idlist $p $col]
5709 set idlist [linsert $idlist $col $p]
5710 # if not the first child, we have to insert a line going up
5711 if {$id ne [lindex $children($curview,$p) 0]} {
5712 makeupline $p $rm1 $row $col
5713 }
5714 }
5715 }
5716 set id [lindex $displayorder $row]
5717 if {$row > $downarrowlen} {
5718 set termrow [expr {$row - $downarrowlen - 1}]
5719 foreach p [lindex $parentlist $termrow] {
5720 set i [lsearch -exact $idlist $p]
5721 if {$i < 0} continue
5722 set nr [nextuse $p $termrow]
5723 if {$nr < 0 || $nr >= $row + $mingaplen + $uparrowlen} {
5724 set idlist [lreplace $idlist $i $i]
5725 }
5726 }
5727 }
5728 set col [lsearch -exact $idlist $id]
5729 if {$col < 0} {
5730 set col [idcol $idlist $id]
5731 set idlist [linsert $idlist $col $id]
5732 if {$children($curview,$id) ne {}} {
5733 makeupline $id $rm1 $row $col
5734 }
5735 }
5736 set r [expr {$row + $uparrowlen - 1}]
5737 if {$r < $commitidx($curview)} {
5738 set x $col
5739 foreach p [lindex $parentlist $r] {
5740 if {[lsearch -exact $idlist $p] >= 0} continue
5741 set fk [lindex $children($curview,$p) 0]
5742 if {[rowofcommit $fk] < $row} {
5743 set x [idcol $idlist $p $x]
5744 set idlist [linsert $idlist $x $p]
5745 }
5746 }
5747 if {[incr r] < $commitidx($curview)} {
5748 set p [lindex $displayorder $r]
5749 if {[lsearch -exact $idlist $p] < 0} {
5750 set fk [lindex $children($curview,$p) 0]
5751 if {$fk ne {} && [rowofcommit $fk] < $row} {
5752 set x [idcol $idlist $p $x]
5753 set idlist [linsert $idlist $x $p]
5754 }
5755 }
5756 }
5757 }
5758 }
5759 if {$final && !$viewcomplete($curview) &&
5760 $row + $uparrowlen + $mingaplen + $downarrowlen
5761 >= $commitidx($curview)} {
5762 set final 0
5763 }
5764 set l [llength $rowidlist]
5765 if {$row == $l} {
5766 lappend rowidlist $idlist
5767 lappend rowisopt 0
5768 lappend rowfinal $final
5769 } elseif {$row < $l} {
5770 if {![rowsequal $idlist [lindex $rowidlist $row]]} {
5771 lset rowidlist $row $idlist
5772 changedrow $row
5773 }
5774 lset rowfinal $row $final
5775 } else {
5776 set pad [ntimes [expr {$row - $l}] {}]
5777 set rowidlist [concat $rowidlist $pad]
5778 lappend rowidlist $idlist
5779 set rowfinal [concat $rowfinal $pad]
5780 lappend rowfinal $final
5781 set rowisopt [concat $rowisopt [ntimes [expr {$row - $l + 1}] 0]]
5782 }
5783 }
5784 return $row
5785}
5786
5787proc changedrow {row} {
5788 global displayorder iddrawn rowisopt need_redisplay
5789
5790 set l [llength $rowisopt]
5791 if {$row < $l} {
5792 lset rowisopt $row 0
5793 if {$row + 1 < $l} {
5794 lset rowisopt [expr {$row + 1}] 0
5795 if {$row + 2 < $l} {
5796 lset rowisopt [expr {$row + 2}] 0
5797 }
5798 }
5799 }
5800 set id [lindex $displayorder $row]
5801 if {[info exists iddrawn($id)]} {
5802 set need_redisplay 1
5803 }
5804}
5805
5806proc insert_pad {row col npad} {
5807 global rowidlist
5808
5809 set pad [ntimes $npad {}]
5810 set idlist [lindex $rowidlist $row]
5811 set bef [lrange $idlist 0 [expr {$col - 1}]]
5812 set aft [lrange $idlist $col end]
5813 set i [lsearch -exact $aft {}]
5814 if {$i > 0} {
5815 set aft [lreplace $aft $i $i]
5816 }
5817 lset rowidlist $row [concat $bef $pad $aft]
5818 changedrow $row
5819}
5820
5821proc optimize_rows {row col endrow} {
5822 global rowidlist rowisopt displayorder curview children
5823
5824 if {$row < 1} {
5825 set row 1
5826 }
5827 for {} {$row < $endrow} {incr row; set col 0} {
5828 if {[lindex $rowisopt $row]} continue
5829 set haspad 0
5830 set y0 [expr {$row - 1}]
5831 set ym [expr {$row - 2}]
5832 set idlist [lindex $rowidlist $row]
5833 set previdlist [lindex $rowidlist $y0]
5834 if {$idlist eq {} || $previdlist eq {}} continue
5835 if {$ym >= 0} {
5836 set pprevidlist [lindex $rowidlist $ym]
5837 if {$pprevidlist eq {}} continue
5838 } else {
5839 set pprevidlist {}
5840 }
5841 set x0 -1
5842 set xm -1
5843 for {} {$col < [llength $idlist]} {incr col} {
5844 set id [lindex $idlist $col]
5845 if {[lindex $previdlist $col] eq $id} continue
5846 if {$id eq {}} {
5847 set haspad 1
5848 continue
5849 }
5850 set x0 [lsearch -exact $previdlist $id]
5851 if {$x0 < 0} continue
5852 set z [expr {$x0 - $col}]
5853 set isarrow 0
5854 set z0 {}
5855 if {$ym >= 0} {
5856 set xm [lsearch -exact $pprevidlist $id]
5857 if {$xm >= 0} {
5858 set z0 [expr {$xm - $x0}]
5859 }
5860 }
5861 if {$z0 eq {}} {
5862 # if row y0 is the first child of $id then it's not an arrow
5863 if {[lindex $children($curview,$id) 0] ne
5864 [lindex $displayorder $y0]} {
5865 set isarrow 1
5866 }
5867 }
5868 if {!$isarrow && $id ne [lindex $displayorder $row] &&
5869 [lsearch -exact [lindex $rowidlist [expr {$row+1}]] $id] < 0} {
5870 set isarrow 1
5871 }
5872 # Looking at lines from this row to the previous row,
5873 # make them go straight up if they end in an arrow on
5874 # the previous row; otherwise make them go straight up
5875 # or at 45 degrees.
5876 if {$z < -1 || ($z < 0 && $isarrow)} {
5877 # Line currently goes left too much;
5878 # insert pads in the previous row, then optimize it
5879 set npad [expr {-1 - $z + $isarrow}]
5880 insert_pad $y0 $x0 $npad
5881 if {$y0 > 0} {
5882 optimize_rows $y0 $x0 $row
5883 }
5884 set previdlist [lindex $rowidlist $y0]
5885 set x0 [lsearch -exact $previdlist $id]
5886 set z [expr {$x0 - $col}]
5887 if {$z0 ne {}} {
5888 set pprevidlist [lindex $rowidlist $ym]
5889 set xm [lsearch -exact $pprevidlist $id]
5890 set z0 [expr {$xm - $x0}]
5891 }
5892 } elseif {$z > 1 || ($z > 0 && $isarrow)} {
5893 # Line currently goes right too much;
5894 # insert pads in this line
5895 set npad [expr {$z - 1 + $isarrow}]
5896 insert_pad $row $col $npad
5897 set idlist [lindex $rowidlist $row]
5898 incr col $npad
5899 set z [expr {$x0 - $col}]
5900 set haspad 1
5901 }
5902 if {$z0 eq {} && !$isarrow && $ym >= 0} {
5903 # this line links to its first child on row $row-2
5904 set id [lindex $displayorder $ym]
5905 set xc [lsearch -exact $pprevidlist $id]
5906 if {$xc >= 0} {
5907 set z0 [expr {$xc - $x0}]
5908 }
5909 }
5910 # avoid lines jigging left then immediately right
5911 if {$z0 ne {} && $z < 0 && $z0 > 0} {
5912 insert_pad $y0 $x0 1
5913 incr x0
5914 optimize_rows $y0 $x0 $row
5915 set previdlist [lindex $rowidlist $y0]
5916 }
5917 }
5918 if {!$haspad} {
5919 # Find the first column that doesn't have a line going right
5920 for {set col [llength $idlist]} {[incr col -1] >= 0} {} {
5921 set id [lindex $idlist $col]
5922 if {$id eq {}} break
5923 set x0 [lsearch -exact $previdlist $id]
5924 if {$x0 < 0} {
5925 # check if this is the link to the first child
5926 set kid [lindex $displayorder $y0]
5927 if {[lindex $children($curview,$id) 0] eq $kid} {
5928 # it is, work out offset to child
5929 set x0 [lsearch -exact $previdlist $kid]
5930 }
5931 }
5932 if {$x0 <= $col} break
5933 }
5934 # Insert a pad at that column as long as it has a line and
5935 # isn't the last column
5936 if {$x0 >= 0 && [incr col] < [llength $idlist]} {
5937 set idlist [linsert $idlist $col {}]
5938 lset rowidlist $row $idlist
5939 changedrow $row
5940 }
5941 }
5942 }
5943}
5944
5945proc xc {row col} {
5946 global canvx0 linespc
5947 return [expr {$canvx0 + $col * $linespc}]
5948}
5949
5950proc yc {row} {
5951 global canvy0 linespc
5952 return [expr {$canvy0 + $row * $linespc}]
5953}
5954
5955proc linewidth {id} {
5956 global thickerline lthickness
5957
5958 set wid $lthickness
5959 if {[info exists thickerline] && $id eq $thickerline} {
5960 set wid [expr {2 * $lthickness}]
5961 }
5962 return $wid
5963}
5964
5965proc rowranges {id} {
5966 global curview children uparrowlen downarrowlen
5967 global rowidlist
5968
5969 set kids $children($curview,$id)
5970 if {$kids eq {}} {
5971 return {}
5972 }
5973 set ret {}
5974 lappend kids $id
5975 foreach child $kids {
5976 if {![commitinview $child $curview]} break
5977 set row [rowofcommit $child]
5978 if {![info exists prev]} {
5979 lappend ret [expr {$row + 1}]
5980 } else {
5981 if {$row <= $prevrow} {
5982 puts "oops children of [shortids $id] out of order [shortids $child] $row <= [shortids $prev] $prevrow"
5983 }
5984 # see if the line extends the whole way from prevrow to row
5985 if {$row > $prevrow + $uparrowlen + $downarrowlen &&
5986 [lsearch -exact [lindex $rowidlist \
5987 [expr {int(($row + $prevrow) / 2)}]] $id] < 0} {
5988 # it doesn't, see where it ends
5989 set r [expr {$prevrow + $downarrowlen}]
5990 if {[lsearch -exact [lindex $rowidlist $r] $id] < 0} {
5991 while {[incr r -1] > $prevrow &&
5992 [lsearch -exact [lindex $rowidlist $r] $id] < 0} {}
5993 } else {
5994 while {[incr r] <= $row &&
5995 [lsearch -exact [lindex $rowidlist $r] $id] >= 0} {}
5996 incr r -1
5997 }
5998 lappend ret $r
5999 # see where it starts up again
6000 set r [expr {$row - $uparrowlen}]
6001 if {[lsearch -exact [lindex $rowidlist $r] $id] < 0} {
6002 while {[incr r] < $row &&
6003 [lsearch -exact [lindex $rowidlist $r] $id] < 0} {}
6004 } else {
6005 while {[incr r -1] >= $prevrow &&
6006 [lsearch -exact [lindex $rowidlist $r] $id] >= 0} {}
6007 incr r
6008 }
6009 lappend ret $r
6010 }
6011 }
6012 if {$child eq $id} {
6013 lappend ret $row
6014 }
6015 set prev $child
6016 set prevrow $row
6017 }
6018 return $ret
6019}
6020
6021proc drawlineseg {id row endrow arrowlow} {
6022 global rowidlist displayorder iddrawn linesegs
6023 global canv colormap linespc curview maxlinelen parentlist
6024
6025 set cols [list [lsearch -exact [lindex $rowidlist $row] $id]]
6026 set le [expr {$row + 1}]
6027 set arrowhigh 1
6028 while {1} {
6029 set c [lsearch -exact [lindex $rowidlist $le] $id]
6030 if {$c < 0} {
6031 incr le -1
6032 break
6033 }
6034 lappend cols $c
6035 set x [lindex $displayorder $le]
6036 if {$x eq $id} {
6037 set arrowhigh 0
6038 break
6039 }
6040 if {[info exists iddrawn($x)] || $le == $endrow} {
6041 set c [lsearch -exact [lindex $rowidlist [expr {$le+1}]] $id]
6042 if {$c >= 0} {
6043 lappend cols $c
6044 set arrowhigh 0
6045 }
6046 break
6047 }
6048 incr le
6049 }
6050 if {$le <= $row} {
6051 return $row
6052 }
6053
6054 set lines {}
6055 set i 0
6056 set joinhigh 0
6057 if {[info exists linesegs($id)]} {
6058 set lines $linesegs($id)
6059 foreach li $lines {
6060 set r0 [lindex $li 0]
6061 if {$r0 > $row} {
6062 if {$r0 == $le && [lindex $li 1] - $row <= $maxlinelen} {
6063 set joinhigh 1
6064 }
6065 break
6066 }
6067 incr i
6068 }
6069 }
6070 set joinlow 0
6071 if {$i > 0} {
6072 set li [lindex $lines [expr {$i-1}]]
6073 set r1 [lindex $li 1]
6074 if {$r1 == $row && $le - [lindex $li 0] <= $maxlinelen} {
6075 set joinlow 1
6076 }
6077 }
6078
6079 set x [lindex $cols [expr {$le - $row}]]
6080 set xp [lindex $cols [expr {$le - 1 - $row}]]
6081 set dir [expr {$xp - $x}]
6082 if {$joinhigh} {
6083 set ith [lindex $lines $i 2]
6084 set coords [$canv coords $ith]
6085 set ah [$canv itemcget $ith -arrow]
6086 set arrowhigh [expr {$ah eq "first" || $ah eq "both"}]
6087 set x2 [lindex $cols [expr {$le + 1 - $row}]]
6088 if {$x2 ne {} && $x - $x2 == $dir} {
6089 set coords [lrange $coords 0 end-2]
6090 }
6091 } else {
6092 set coords [list [xc $le $x] [yc $le]]
6093 }
6094 if {$joinlow} {
6095 set itl [lindex $lines [expr {$i-1}] 2]
6096 set al [$canv itemcget $itl -arrow]
6097 set arrowlow [expr {$al eq "last" || $al eq "both"}]
6098 } elseif {$arrowlow} {
6099 if {[lsearch -exact [lindex $rowidlist [expr {$row-1}]] $id] >= 0 ||
6100 [lsearch -exact [lindex $parentlist [expr {$row-1}]] $id] >= 0} {
6101 set arrowlow 0
6102 }
6103 }
6104 set arrow [lindex {none first last both} [expr {$arrowhigh + 2*$arrowlow}]]
6105 for {set y $le} {[incr y -1] > $row} {} {
6106 set x $xp
6107 set xp [lindex $cols [expr {$y - 1 - $row}]]
6108 set ndir [expr {$xp - $x}]
6109 if {$dir != $ndir || $xp < 0} {
6110 lappend coords [xc $y $x] [yc $y]
6111 }
6112 set dir $ndir
6113 }
6114 if {!$joinlow} {
6115 if {$xp < 0} {
6116 # join parent line to first child
6117 set ch [lindex $displayorder $row]
6118 set xc [lsearch -exact [lindex $rowidlist $row] $ch]
6119 if {$xc < 0} {
6120 puts "oops: drawlineseg: child $ch not on row $row"
6121 } elseif {$xc != $x} {
6122 if {($arrowhigh && $le == $row + 1) || $dir == 0} {
6123 set d [expr {int(0.5 * $linespc)}]
6124 set x1 [xc $row $x]
6125 if {$xc < $x} {
6126 set x2 [expr {$x1 - $d}]
6127 } else {
6128 set x2 [expr {$x1 + $d}]
6129 }
6130 set y2 [yc $row]
6131 set y1 [expr {$y2 + $d}]
6132 lappend coords $x1 $y1 $x2 $y2
6133 } elseif {$xc < $x - 1} {
6134 lappend coords [xc $row [expr {$x-1}]] [yc $row]
6135 } elseif {$xc > $x + 1} {
6136 lappend coords [xc $row [expr {$x+1}]] [yc $row]
6137 }
6138 set x $xc
6139 }
6140 lappend coords [xc $row $x] [yc $row]
6141 } else {
6142 set xn [xc $row $xp]
6143 set yn [yc $row]
6144 lappend coords $xn $yn
6145 }
6146 if {!$joinhigh} {
6147 assigncolor $id
6148 set t [$canv create line $coords -width [linewidth $id] \
6149 -fill $colormap($id) -tags lines.$id -arrow $arrow]
6150 $canv lower $t
6151 bindline $t $id
6152 set lines [linsert $lines $i [list $row $le $t]]
6153 } else {
6154 $canv coords $ith $coords
6155 if {$arrow ne $ah} {
6156 $canv itemconf $ith -arrow $arrow
6157 }
6158 lset lines $i 0 $row
6159 }
6160 } else {
6161 set xo [lsearch -exact [lindex $rowidlist [expr {$row - 1}]] $id]
6162 set ndir [expr {$xo - $xp}]
6163 set clow [$canv coords $itl]
6164 if {$dir == $ndir} {
6165 set clow [lrange $clow 2 end]
6166 }
6167 set coords [concat $coords $clow]
6168 if {!$joinhigh} {
6169 lset lines [expr {$i-1}] 1 $le
6170 } else {
6171 # coalesce two pieces
6172 $canv delete $ith
6173 set b [lindex $lines [expr {$i-1}] 0]
6174 set e [lindex $lines $i 1]
6175 set lines [lreplace $lines [expr {$i-1}] $i [list $b $e $itl]]
6176 }
6177 $canv coords $itl $coords
6178 if {$arrow ne $al} {
6179 $canv itemconf $itl -arrow $arrow
6180 }
6181 }
6182
6183 set linesegs($id) $lines
6184 return $le
6185}
6186
6187proc drawparentlinks {id row} {
6188 global rowidlist canv colormap curview parentlist
6189 global idpos linespc
6190
6191 set rowids [lindex $rowidlist $row]
6192 set col [lsearch -exact $rowids $id]
6193 if {$col < 0} return
6194 set olds [lindex $parentlist $row]
6195 set row2 [expr {$row + 1}]
6196 set x [xc $row $col]
6197 set y [yc $row]
6198 set y2 [yc $row2]
6199 set d [expr {int(0.5 * $linespc)}]
6200 set ymid [expr {$y + $d}]
6201 set ids [lindex $rowidlist $row2]
6202 # rmx = right-most X coord used
6203 set rmx 0
6204 foreach p $olds {
6205 set i [lsearch -exact $ids $p]
6206 if {$i < 0} {
6207 puts "oops, parent $p of $id not in list"
6208 continue
6209 }
6210 set x2 [xc $row2 $i]
6211 if {$x2 > $rmx} {
6212 set rmx $x2
6213 }
6214 set j [lsearch -exact $rowids $p]
6215 if {$j < 0} {
6216 # drawlineseg will do this one for us
6217 continue
6218 }
6219 assigncolor $p
6220 # should handle duplicated parents here...
6221 set coords [list $x $y]
6222 if {$i != $col} {
6223 # if attaching to a vertical segment, draw a smaller
6224 # slant for visual distinctness
6225 if {$i == $j} {
6226 if {$i < $col} {
6227 lappend coords [expr {$x2 + $d}] $y $x2 $ymid
6228 } else {
6229 lappend coords [expr {$x2 - $d}] $y $x2 $ymid
6230 }
6231 } elseif {$i < $col && $i < $j} {
6232 # segment slants towards us already
6233 lappend coords [xc $row $j] $y
6234 } else {
6235 if {$i < $col - 1} {
6236 lappend coords [expr {$x2 + $linespc}] $y
6237 } elseif {$i > $col + 1} {
6238 lappend coords [expr {$x2 - $linespc}] $y
6239 }
6240 lappend coords $x2 $y2
6241 }
6242 } else {
6243 lappend coords $x2 $y2
6244 }
6245 set t [$canv create line $coords -width [linewidth $p] \
6246 -fill $colormap($p) -tags lines.$p]
6247 $canv lower $t
6248 bindline $t $p
6249 }
6250 if {$rmx > [lindex $idpos($id) 1]} {
6251 lset idpos($id) 1 $rmx
6252 redrawtags $id
6253 }
6254}
6255
6256proc drawlines {id} {
6257 global canv
6258
6259 $canv itemconf lines.$id -width [linewidth $id]
6260}
6261
6262proc drawcmittext {id row col} {
6263 global linespc canv canv2 canv3 fgcolor curview
6264 global cmitlisted commitinfo rowidlist parentlist
6265 global rowtextx idpos idtags idheads idotherrefs
6266 global linehtag linentag linedtag selectedline
6267 global canvxmax boldids boldnameids fgcolor markedid
6268 global mainheadid nullid nullid2 circleitem circlecolors ctxbut
6269 global mainheadcirclecolor workingfilescirclecolor indexcirclecolor
6270 global circleoutlinecolor
6271
6272 # listed is 0 for boundary, 1 for normal, 2 for negative, 3 for left, 4 for right
6273 set listed $cmitlisted($curview,$id)
6274 if {$id eq $nullid} {
6275 set ofill $workingfilescirclecolor
6276 } elseif {$id eq $nullid2} {
6277 set ofill $indexcirclecolor
6278 } elseif {$id eq $mainheadid} {
6279 set ofill $mainheadcirclecolor
6280 } else {
6281 set ofill [lindex $circlecolors $listed]
6282 }
6283 set x [xc $row $col]
6284 set y [yc $row]
6285 set orad [expr {$linespc / 3}]
6286 if {$listed <= 2} {
6287 set t [$canv create oval [expr {$x - $orad}] [expr {$y - $orad}] \
6288 [expr {$x + $orad - 1}] [expr {$y + $orad - 1}] \
6289 -fill $ofill -outline $circleoutlinecolor -width 1 -tags circle]
6290 } elseif {$listed == 3} {
6291 # triangle pointing left for left-side commits
6292 set t [$canv create polygon \
6293 [expr {$x - $orad}] $y \
6294 [expr {$x + $orad - 1}] [expr {$y - $orad}] \
6295 [expr {$x + $orad - 1}] [expr {$y + $orad - 1}] \
6296 -fill $ofill -outline $circleoutlinecolor -width 1 -tags circle]
6297 } else {
6298 # triangle pointing right for right-side commits
6299 set t [$canv create polygon \
6300 [expr {$x + $orad - 1}] $y \
6301 [expr {$x - $orad}] [expr {$y - $orad}] \
6302 [expr {$x - $orad}] [expr {$y + $orad - 1}] \
6303 -fill $ofill -outline $circleoutlinecolor -width 1 -tags circle]
6304 }
6305 set circleitem($row) $t
6306 $canv raise $t
6307 $canv bind $t <1> {selcanvline {} %x %y}
6308 set rmx [llength [lindex $rowidlist $row]]
6309 set olds [lindex $parentlist $row]
6310 if {$olds ne {}} {
6311 set nextids [lindex $rowidlist [expr {$row + 1}]]
6312 foreach p $olds {
6313 set i [lsearch -exact $nextids $p]
6314 if {$i > $rmx} {
6315 set rmx $i
6316 }
6317 }
6318 }
6319 set xt [xc $row $rmx]
6320 set rowtextx($row) $xt
6321 set idpos($id) [list $x $xt $y]
6322 if {[info exists idtags($id)] || [info exists idheads($id)]
6323 || [info exists idotherrefs($id)]} {
6324 set xt [drawtags $id $x $xt $y]
6325 }
6326 if {[lindex $commitinfo($id) 6] > 0} {
6327 set xt [drawnotesign $xt $y]
6328 }
6329 set headline [lindex $commitinfo($id) 0]
6330 set name [lindex $commitinfo($id) 1]
6331 set date [lindex $commitinfo($id) 2]
6332 set date [formatdate $date]
6333 set font mainfont
6334 set nfont mainfont
6335 set isbold [ishighlighted $id]
6336 if {$isbold > 0} {
6337 lappend boldids $id
6338 set font mainfontbold
6339 if {$isbold > 1} {
6340 lappend boldnameids $id
6341 set nfont mainfontbold
6342 }
6343 }
6344 set linehtag($id) [$canv create text $xt $y -anchor w -fill $fgcolor \
6345 -text $headline -font $font -tags text]
6346 $canv bind $linehtag($id) $ctxbut "rowmenu %X %Y $id"
6347 set linentag($id) [$canv2 create text 3 $y -anchor w -fill $fgcolor \
6348 -text $name -font $nfont -tags text]
6349 set linedtag($id) [$canv3 create text 3 $y -anchor w -fill $fgcolor \
6350 -text $date -font mainfont -tags text]
6351 if {$selectedline == $row} {
6352 make_secsel $id
6353 }
6354 if {[info exists markedid] && $markedid eq $id} {
6355 make_idmark $id
6356 }
6357 set xr [expr {$xt + [font measure $font $headline]}]
6358 if {$xr > $canvxmax} {
6359 set canvxmax $xr
6360 setcanvscroll
6361 }
6362}
6363
6364proc drawcmitrow {row} {
6365 global displayorder rowidlist nrows_drawn
6366 global iddrawn markingmatches
6367 global commitinfo numcommits
6368 global filehighlight fhighlights findpattern nhighlights
6369 global hlview vhighlights
6370 global highlight_related rhighlights
6371
6372 if {$row >= $numcommits} return
6373
6374 set id [lindex $displayorder $row]
6375 if {[info exists hlview] && ![info exists vhighlights($id)]} {
6376 askvhighlight $row $id
6377 }
6378 if {[info exists filehighlight] && ![info exists fhighlights($id)]} {
6379 askfilehighlight $row $id
6380 }
6381 if {$findpattern ne {} && ![info exists nhighlights($id)]} {
6382 askfindhighlight $row $id
6383 }
6384 if {$highlight_related ne [mc "None"] && ![info exists rhighlights($id)]} {
6385 askrelhighlight $row $id
6386 }
6387 if {![info exists iddrawn($id)]} {
6388 set col [lsearch -exact [lindex $rowidlist $row] $id]
6389 if {$col < 0} {
6390 puts "oops, row $row id $id not in list"
6391 return
6392 }
6393 if {![info exists commitinfo($id)]} {
6394 getcommit $id
6395 }
6396 assigncolor $id
6397 drawcmittext $id $row $col
6398 set iddrawn($id) 1
6399 incr nrows_drawn
6400 }
6401 if {$markingmatches} {
6402 markrowmatches $row $id
6403 }
6404}
6405
6406proc drawcommits {row {endrow {}}} {
6407 global numcommits iddrawn displayorder curview need_redisplay
6408 global parentlist rowidlist rowfinal uparrowlen downarrowlen nrows_drawn
6409
6410 if {$row < 0} {
6411 set row 0
6412 }
6413 if {$endrow eq {}} {
6414 set endrow $row
6415 }
6416 if {$endrow >= $numcommits} {
6417 set endrow [expr {$numcommits - 1}]
6418 }
6419
6420 set rl1 [expr {$row - $downarrowlen - 3}]
6421 if {$rl1 < 0} {
6422 set rl1 0
6423 }
6424 set ro1 [expr {$row - 3}]
6425 if {$ro1 < 0} {
6426 set ro1 0
6427 }
6428 set r2 [expr {$endrow + $uparrowlen + 3}]
6429 if {$r2 > $numcommits} {
6430 set r2 $numcommits
6431 }
6432 for {set r $rl1} {$r < $r2} {incr r} {
6433 if {[lindex $rowidlist $r] ne {} && [lindex $rowfinal $r]} {
6434 if {$rl1 < $r} {
6435 layoutrows $rl1 $r
6436 }
6437 set rl1 [expr {$r + 1}]
6438 }
6439 }
6440 if {$rl1 < $r} {
6441 layoutrows $rl1 $r
6442 }
6443 optimize_rows $ro1 0 $r2
6444 if {$need_redisplay || $nrows_drawn > 2000} {
6445 clear_display
6446 }
6447
6448 # make the lines join to already-drawn rows either side
6449 set r [expr {$row - 1}]
6450 if {$r < 0 || ![info exists iddrawn([lindex $displayorder $r])]} {
6451 set r $row
6452 }
6453 set er [expr {$endrow + 1}]
6454 if {$er >= $numcommits ||
6455 ![info exists iddrawn([lindex $displayorder $er])]} {
6456 set er $endrow
6457 }
6458 for {} {$r <= $er} {incr r} {
6459 set id [lindex $displayorder $r]
6460 set wasdrawn [info exists iddrawn($id)]
6461 drawcmitrow $r
6462 if {$r == $er} break
6463 set nextid [lindex $displayorder [expr {$r + 1}]]
6464 if {$wasdrawn && [info exists iddrawn($nextid)]} continue
6465 drawparentlinks $id $r
6466
6467 set rowids [lindex $rowidlist $r]
6468 foreach lid $rowids {
6469 if {$lid eq {}} continue
6470 if {[info exists lineend($lid)] && $lineend($lid) > $r} continue
6471 if {$lid eq $id} {
6472 # see if this is the first child of any of its parents
6473 foreach p [lindex $parentlist $r] {
6474 if {[lsearch -exact $rowids $p] < 0} {
6475 # make this line extend up to the child
6476 set lineend($p) [drawlineseg $p $r $er 0]
6477 }
6478 }
6479 } else {
6480 set lineend($lid) [drawlineseg $lid $r $er 1]
6481 }
6482 }
6483 }
6484}
6485
6486proc undolayout {row} {
6487 global uparrowlen mingaplen downarrowlen
6488 global rowidlist rowisopt rowfinal need_redisplay
6489
6490 set r [expr {$row - ($uparrowlen + $mingaplen + $downarrowlen)}]
6491 if {$r < 0} {
6492 set r 0
6493 }
6494 if {[llength $rowidlist] > $r} {
6495 incr r -1
6496 set rowidlist [lrange $rowidlist 0 $r]
6497 set rowfinal [lrange $rowfinal 0 $r]
6498 set rowisopt [lrange $rowisopt 0 $r]
6499 set need_redisplay 1
6500 run drawvisible
6501 }
6502}
6503
6504proc drawvisible {} {
6505 global canv linespc curview vrowmod selectedline targetrow targetid
6506 global need_redisplay cscroll numcommits
6507
6508 set fs [$canv yview]
6509 set ymax [lindex [$canv cget -scrollregion] 3]
6510 if {$ymax eq {} || $ymax == 0 || $numcommits == 0} return
6511 set f0 [lindex $fs 0]
6512 set f1 [lindex $fs 1]
6513 set y0 [expr {int($f0 * $ymax)}]
6514 set y1 [expr {int($f1 * $ymax)}]
6515
6516 if {[info exists targetid]} {
6517 if {[commitinview $targetid $curview]} {
6518 set r [rowofcommit $targetid]
6519 if {$r != $targetrow} {
6520 # Fix up the scrollregion and change the scrolling position
6521 # now that our target row has moved.
6522 set diff [expr {($r - $targetrow) * $linespc}]
6523 set targetrow $r
6524 setcanvscroll
6525 set ymax [lindex [$canv cget -scrollregion] 3]
6526 incr y0 $diff
6527 incr y1 $diff
6528 set f0 [expr {$y0 / $ymax}]
6529 set f1 [expr {$y1 / $ymax}]
6530 allcanvs yview moveto $f0
6531 $cscroll set $f0 $f1
6532 set need_redisplay 1
6533 }
6534 } else {
6535 unset targetid
6536 }
6537 }
6538
6539 set row [expr {int(($y0 - 3) / $linespc) - 1}]
6540 set endrow [expr {int(($y1 - 3) / $linespc) + 1}]
6541 if {$endrow >= $vrowmod($curview)} {
6542 update_arcrows $curview
6543 }
6544 if {$selectedline ne {} &&
6545 $row <= $selectedline && $selectedline <= $endrow} {
6546 set targetrow $selectedline
6547 } elseif {[info exists targetid]} {
6548 set targetrow [expr {int(($row + $endrow) / 2)}]
6549 }
6550 if {[info exists targetrow]} {
6551 if {$targetrow >= $numcommits} {
6552 set targetrow [expr {$numcommits - 1}]
6553 }
6554 set targetid [commitonrow $targetrow]
6555 }
6556 drawcommits $row $endrow
6557}
6558
6559proc clear_display {} {
6560 global iddrawn linesegs need_redisplay nrows_drawn
6561 global vhighlights fhighlights nhighlights rhighlights
6562 global linehtag linentag linedtag boldids boldnameids
6563
6564 allcanvs delete all
6565 unset -nocomplain iddrawn
6566 unset -nocomplain linesegs
6567 unset -nocomplain linehtag
6568 unset -nocomplain linentag
6569 unset -nocomplain linedtag
6570 set boldids {}
6571 set boldnameids {}
6572 unset -nocomplain vhighlights
6573 unset -nocomplain fhighlights
6574 unset -nocomplain nhighlights
6575 unset -nocomplain rhighlights
6576 set need_redisplay 0
6577 set nrows_drawn 0
6578}
6579
6580proc findcrossings {id} {
6581 global rowidlist parentlist numcommits displayorder
6582
6583 set cross {}
6584 set ccross {}
6585 foreach {s e} [rowranges $id] {
6586 if {$e >= $numcommits} {
6587 set e [expr {$numcommits - 1}]
6588 }
6589 if {$e <= $s} continue
6590 for {set row $e} {[incr row -1] >= $s} {} {
6591 set x [lsearch -exact [lindex $rowidlist $row] $id]
6592 if {$x < 0} break
6593 set olds [lindex $parentlist $row]
6594 set kid [lindex $displayorder $row]
6595 set kidx [lsearch -exact [lindex $rowidlist $row] $kid]
6596 if {$kidx < 0} continue
6597 set nextrow [lindex $rowidlist [expr {$row + 1}]]
6598 foreach p $olds {
6599 set px [lsearch -exact $nextrow $p]
6600 if {$px < 0} continue
6601 if {($kidx < $x && $x < $px) || ($px < $x && $x < $kidx)} {
6602 if {[lsearch -exact $ccross $p] >= 0} continue
6603 if {$x == $px + ($kidx < $px? -1: 1)} {
6604 lappend ccross $p
6605 } elseif {[lsearch -exact $cross $p] < 0} {
6606 lappend cross $p
6607 }
6608 }
6609 }
6610 }
6611 }
6612 return [concat $ccross {{}} $cross]
6613}
6614
6615proc assigncolor {id} {
6616 global colormap colors nextcolor
6617 global parents children children curview
6618
6619 if {[info exists colormap($id)]} return
6620 set ncolors [llength $colors]
6621 if {[info exists children($curview,$id)]} {
6622 set kids $children($curview,$id)
6623 } else {
6624 set kids {}
6625 }
6626 if {[llength $kids] == 1} {
6627 set child [lindex $kids 0]
6628 if {[info exists colormap($child)]
6629 && [llength $parents($curview,$child)] == 1} {
6630 set colormap($id) $colormap($child)
6631 return
6632 }
6633 }
6634 set badcolors {}
6635 set origbad {}
6636 foreach x [findcrossings $id] {
6637 if {$x eq {}} {
6638 # delimiter between corner crossings and other crossings
6639 if {[llength $badcolors] >= $ncolors - 1} break
6640 set origbad $badcolors
6641 }
6642 if {[info exists colormap($x)]
6643 && [lsearch -exact $badcolors $colormap($x)] < 0} {
6644 lappend badcolors $colormap($x)
6645 }
6646 }
6647 if {[llength $badcolors] >= $ncolors} {
6648 set badcolors $origbad
6649 }
6650 set origbad $badcolors
6651 if {[llength $badcolors] < $ncolors - 1} {
6652 foreach child $kids {
6653 if {[info exists colormap($child)]
6654 && [lsearch -exact $badcolors $colormap($child)] < 0} {
6655 lappend badcolors $colormap($child)
6656 }
6657 foreach p $parents($curview,$child) {
6658 if {[info exists colormap($p)]
6659 && [lsearch -exact $badcolors $colormap($p)] < 0} {
6660 lappend badcolors $colormap($p)
6661 }
6662 }
6663 }
6664 if {[llength $badcolors] >= $ncolors} {
6665 set badcolors $origbad
6666 }
6667 }
6668 for {set i 0} {$i <= $ncolors} {incr i} {
6669 set c [lindex $colors $nextcolor]
6670 if {[incr nextcolor] >= $ncolors} {
6671 set nextcolor 0
6672 }
6673 if {[lsearch -exact $badcolors $c]} break
6674 }
6675 set colormap($id) $c
6676}
6677
6678proc bindline {t id} {
6679 global canv
6680
6681 $canv bind $t <Enter> "lineenter %x %y $id"
6682 $canv bind $t <Motion> "linemotion %x %y $id"
6683 $canv bind $t <Leave> "lineleave $id"
6684 $canv bind $t <Button-1> "lineclick %x %y $id 1"
6685}
6686
6687proc graph_pane_width {} {
6688 set g [.tf.histframe.pwclist sashpos 0]
6689 return [lindex $g 0]
6690}
6691
6692proc totalwidth {l font extra} {
6693 set tot 0
6694 foreach str $l {
6695 set tot [expr {$tot + [font measure $font $str] + $extra}]
6696 }
6697 return $tot
6698}
6699
6700proc drawtags {id x xt y1} {
6701 global idtags idheads idotherrefs mainhead
6702 global linespc lthickness
6703 global canv rowtextx curview fgcolor bgcolor ctxbut
6704 global headbgcolor headfgcolor headoutlinecolor remotebgcolor
6705 global tagbgcolor tagfgcolor tagoutlinecolor
6706 global reflinecolor
6707
6708 set marks {}
6709 set ntags 0
6710 set nheads 0
6711 set singletag 0
6712 set maxtags 3
6713 set maxtagpct 25
6714 set maxwidth [expr {[graph_pane_width] * $maxtagpct / 100}]
6715 set delta [expr {int(0.5 * ($linespc - $lthickness))}]
6716 set extra [expr {$delta + $lthickness + $linespc}]
6717
6718 if {[info exists idtags($id)]} {
6719 set marks $idtags($id)
6720 set ntags [llength $marks]
6721 if {$ntags > $maxtags ||
6722 [totalwidth $marks mainfont $extra] > $maxwidth} {
6723 # show just a single "n tags..." tag
6724 set singletag 1
6725 if {$ntags == 1} {
6726 set marks [list "tag..."]
6727 } else {
6728 set marks [list [format "%d tags..." $ntags]]
6729 }
6730 set ntags 1
6731 }
6732 }
6733 if {[info exists idheads($id)]} {
6734 set marks [concat $marks $idheads($id)]
6735 set nheads [llength $idheads($id)]
6736 }
6737 if {[info exists idotherrefs($id)]} {
6738 set marks [concat $marks $idotherrefs($id)]
6739 }
6740 if {$marks eq {}} {
6741 return $xt
6742 }
6743
6744 set yt [expr {$y1 - 0.5 * $linespc}]
6745 set yb [expr {$yt + $linespc - 1}]
6746 set xvals {}
6747 set wvals {}
6748 set i -1
6749 foreach tag $marks {
6750 incr i
6751 if {$i >= $ntags && $i < $ntags + $nheads && $tag eq $mainhead} {
6752 set wid [font measure mainfontbold $tag]
6753 } else {
6754 set wid [font measure mainfont $tag]
6755 }
6756 lappend xvals $xt
6757 lappend wvals $wid
6758 set xt [expr {$xt + $wid + $extra}]
6759 }
6760 set t [$canv create line $x $y1 [lindex $xvals end] $y1 \
6761 -width $lthickness -fill $reflinecolor -tags tag.$id]
6762 $canv lower $t
6763 foreach tag $marks x $xvals wid $wvals {
6764 set tag_quoted [string map {% %%} $tag]
6765 set xl [expr {$x + $delta}]
6766 set xr [expr {$x + $delta + $wid + $lthickness}]
6767 set font mainfont
6768 if {[incr ntags -1] >= 0} {
6769 # draw a tag
6770 set t [$canv create polygon $x [expr {$yt + $delta}] $xl $yt \
6771 $xr $yt $xr $yb $xl $yb $x [expr {$yb - $delta}] \
6772 -width 1 -outline $tagoutlinecolor -fill $tagbgcolor \
6773 -tags tag.$id]
6774 if {$singletag} {
6775 set tagclick [list showtags $id 1]
6776 } else {
6777 set tagclick [list showtag $tag_quoted 1]
6778 }
6779 $canv bind $t <1> $tagclick
6780 set rowtextx([rowofcommit $id]) [expr {$xr + $linespc}]
6781 } else {
6782 # draw a head or other ref
6783 if {[incr nheads -1] >= 0} {
6784 set col $headbgcolor
6785 if {$tag eq $mainhead} {
6786 set font mainfontbold
6787 }
6788 } else {
6789 set col "#ddddff"
6790 }
6791 set xl [expr {$xl - $delta/2}]
6792 $canv create polygon $x $yt $xr $yt $xr $yb $x $yb \
6793 -width 1 -outline black -fill $col -tags tag.$id
6794 if {[regexp {^(remotes/.*/|remotes/)} $tag match remoteprefix]} {
6795 set rwid [font measure mainfont $remoteprefix]
6796 set xi [expr {$x + 1}]
6797 set yti [expr {$yt + 1}]
6798 set xri [expr {$x + $rwid}]
6799 $canv create polygon $xi $yti $xri $yti $xri $yb $xi $yb \
6800 -width 0 -fill $remotebgcolor -tags tag.$id
6801 }
6802 }
6803 set t [$canv create text $xl $y1 -anchor w -text $tag -fill $headfgcolor \
6804 -font $font -tags [list tag.$id text]]
6805 if {$ntags >= 0} {
6806 $canv bind $t <1> $tagclick
6807 } elseif {$nheads >= 0} {
6808 $canv bind $t $ctxbut [list headmenu %X %Y $id $tag_quoted]
6809 }
6810 }
6811 return $xt
6812}
6813
6814proc drawnotesign {xt y} {
6815 global linespc canv fgcolor
6816
6817 set orad [expr {$linespc / 3}]
6818 set t [$canv create rectangle [expr {$xt - $orad}] [expr {$y - $orad}] \
6819 [expr {$xt + $orad - 1}] [expr {$y + $orad - 1}] \
6820 -fill yellow -outline $fgcolor -width 1 -tags circle]
6821 set xt [expr {$xt + $orad * 3}]
6822 return $xt
6823}
6824
6825proc xcoord {i level ln} {
6826 global canvx0 xspc1 xspc2
6827
6828 set x [expr {$canvx0 + $i * $xspc1($ln)}]
6829 if {$i > 0 && $i == $level} {
6830 set x [expr {$x + 0.5 * ($xspc2 - $xspc1($ln))}]
6831 } elseif {$i > $level} {
6832 set x [expr {$x + $xspc2 - $xspc1($ln)}]
6833 }
6834 return $x
6835}
6836
6837proc show_status {msg} {
6838 global canv fgcolor
6839
6840 clear_display
6841 set_window_title
6842 $canv create text 3 3 -anchor nw -text $msg -font mainfont \
6843 -tags text -fill $fgcolor
6844}
6845
6846# Don't change the text pane cursor if it is currently the hand cursor,
6847# showing that we are over a sha1 ID link.
6848proc settextcursor {c} {
6849 global ctext curtextcursor
6850
6851 if {[$ctext cget -cursor] == $curtextcursor} {
6852 $ctext config -cursor $c
6853 }
6854 set curtextcursor $c
6855}
6856
6857proc nowbusy {what {name {}}} {
6858 global isbusy busyname statusw
6859
6860 if {[array names isbusy] eq {}} {
6861 . config -cursor watch
6862 settextcursor watch
6863 }
6864 set isbusy($what) 1
6865 set busyname($what) $name
6866 if {$name ne {}} {
6867 $statusw conf -text $name
6868 }
6869}
6870
6871proc notbusy {what} {
6872 global isbusy maincursor textcursor busyname statusw
6873
6874 catch {
6875 unset isbusy($what)
6876 if {$busyname($what) ne {} &&
6877 [$statusw cget -text] eq $busyname($what)} {
6878 $statusw conf -text {}
6879 }
6880 }
6881 if {[array names isbusy] eq {}} {
6882 . config -cursor $maincursor
6883 settextcursor $textcursor
6884 }
6885}
6886
6887proc findmatches {f} {
6888 global findtype findstring
6889 if {$findtype == [mc "Regexp"]} {
6890 set matches [regexp -indices -all -inline $findstring $f]
6891 } else {
6892 set fs $findstring
6893 if {$findtype == [mc "IgnCase"]} {
6894 set f [string tolower $f]
6895 set fs [string tolower $fs]
6896 }
6897 set matches {}
6898 set i 0
6899 set l [string length $fs]
6900 while {[set j [string first $fs $f $i]] >= 0} {
6901 lappend matches [list $j [expr {$j+$l-1}]]
6902 set i [expr {$j + $l}]
6903 }
6904 }
6905 return $matches
6906}
6907
6908proc dofind {{dirn 1} {wrap 1}} {
6909 global findstring findstartline findcurline selectedline numcommits
6910 global gdttype filehighlight fh_serial find_dirn findallowwrap
6911
6912 if {[info exists find_dirn]} {
6913 if {$find_dirn == $dirn} return
6914 stopfinding
6915 }
6916 focus .
6917 if {$findstring eq {} || $numcommits == 0} return
6918 if {$selectedline eq {}} {
6919 set findstartline [lindex [visiblerows] [expr {$dirn < 0}]]
6920 } else {
6921 set findstartline $selectedline
6922 }
6923 set findcurline $findstartline
6924 nowbusy finding [mc "Searching"]
6925 if {$gdttype ne [mc "containing:"] && ![info exists filehighlight]} {
6926 after cancel do_file_hl $fh_serial
6927 do_file_hl $fh_serial
6928 }
6929 set find_dirn $dirn
6930 set findallowwrap $wrap
6931 run findmore
6932}
6933
6934proc stopfinding {} {
6935 global find_dirn findcurline fprogcoord
6936
6937 if {[info exists find_dirn]} {
6938 unset find_dirn
6939 unset findcurline
6940 notbusy finding
6941 set fprogcoord 0
6942 adjustprogress
6943 }
6944 stopblaming
6945}
6946
6947proc findmore {} {
6948 global commitdata commitinfo numcommits findpattern findloc
6949 global findstartline findcurline findallowwrap
6950 global find_dirn gdttype fhighlights fprogcoord
6951 global curview varcorder vrownum varccommits vrowmod
6952
6953 if {![info exists find_dirn]} {
6954 return 0
6955 }
6956 set fldtypes [list [mc "Headline"] [mc "Author"] "" [mc "Committer"] "" [mc "Comments"]]
6957 set l $findcurline
6958 set moretodo 0
6959 if {$find_dirn > 0} {
6960 incr l
6961 if {$l >= $numcommits} {
6962 set l 0
6963 }
6964 if {$l <= $findstartline} {
6965 set lim [expr {$findstartline + 1}]
6966 } else {
6967 set lim $numcommits
6968 set moretodo $findallowwrap
6969 }
6970 } else {
6971 if {$l == 0} {
6972 set l $numcommits
6973 }
6974 incr l -1
6975 if {$l >= $findstartline} {
6976 set lim [expr {$findstartline - 1}]
6977 } else {
6978 set lim -1
6979 set moretodo $findallowwrap
6980 }
6981 }
6982 set n [expr {($lim - $l) * $find_dirn}]
6983 if {$n > 500} {
6984 set n 500
6985 set moretodo 1
6986 }
6987 if {$l + ($find_dirn > 0? $n: 1) > $vrowmod($curview)} {
6988 update_arcrows $curview
6989 }
6990 set found 0
6991 set domore 1
6992 set ai [bsearch $vrownum($curview) $l]
6993 set a [lindex $varcorder($curview) $ai]
6994 set arow [lindex $vrownum($curview) $ai]
6995 set ids [lindex $varccommits($curview,$a)]
6996 set arowend [expr {$arow + [llength $ids]}]
6997 if {$gdttype eq [mc "containing:"]} {
6998 for {} {$n > 0} {incr n -1; incr l $find_dirn} {
6999 if {$l < $arow || $l >= $arowend} {
7000 incr ai $find_dirn
7001 set a [lindex $varcorder($curview) $ai]
7002 set arow [lindex $vrownum($curview) $ai]
7003 set ids [lindex $varccommits($curview,$a)]
7004 set arowend [expr {$arow + [llength $ids]}]
7005 }
7006 set id [lindex $ids [expr {$l - $arow}]]
7007 # shouldn't happen unless git log doesn't give all the commits...
7008 if {![info exists commitdata($id)] ||
7009 ![doesmatch $commitdata($id)]} {
7010 continue
7011 }
7012 if {![info exists commitinfo($id)]} {
7013 getcommit $id
7014 }
7015 set info $commitinfo($id)
7016 foreach f $info ty $fldtypes {
7017 if {$ty eq ""} continue
7018 if {($findloc eq [mc "All fields"] || $findloc eq $ty) &&
7019 [doesmatch $f]} {
7020 set found 1
7021 break
7022 }
7023 }
7024 if {$found} break
7025 }
7026 } else {
7027 for {} {$n > 0} {incr n -1; incr l $find_dirn} {
7028 if {$l < $arow || $l >= $arowend} {
7029 incr ai $find_dirn
7030 set a [lindex $varcorder($curview) $ai]
7031 set arow [lindex $vrownum($curview) $ai]
7032 set ids [lindex $varccommits($curview,$a)]
7033 set arowend [expr {$arow + [llength $ids]}]
7034 }
7035 set id [lindex $ids [expr {$l - $arow}]]
7036 if {![info exists fhighlights($id)]} {
7037 # this sets fhighlights($id) to -1
7038 askfilehighlight $l $id
7039 }
7040 if {$fhighlights($id) > 0} {
7041 set found $domore
7042 break
7043 }
7044 if {$fhighlights($id) < 0} {
7045 if {$domore} {
7046 set domore 0
7047 set findcurline [expr {$l - $find_dirn}]
7048 }
7049 }
7050 }
7051 }
7052 if {$found || ($domore && !$moretodo)} {
7053 unset findcurline
7054 unset find_dirn
7055 notbusy finding
7056 set fprogcoord 0
7057 adjustprogress
7058 if {$found} {
7059 findselectline $l
7060 } else {
7061 bell
7062 }
7063 return 0
7064 }
7065 if {!$domore} {
7066 flushhighlights
7067 } else {
7068 set findcurline [expr {$l - $find_dirn}]
7069 }
7070 set n [expr {($findcurline - $findstartline) * $find_dirn - 1}]
7071 if {$n < 0} {
7072 incr n $numcommits
7073 }
7074 set fprogcoord [expr {$n * 1.0 / $numcommits}]
7075 adjustprogress
7076 return $domore
7077}
7078
7079proc findselectline {l} {
7080 global findloc commentend ctext findcurline markingmatches gdttype
7081
7082 set markingmatches [expr {$gdttype eq [mc "containing:"]}]
7083 set findcurline $l
7084 selectline $l 1
7085 if {$markingmatches &&
7086 ($findloc eq [mc "All fields"] || $findloc eq [mc "Comments"])} {
7087 # highlight the matches in the comments
7088 set f [$ctext get 1.0 $commentend]
7089 set matches [findmatches $f]
7090 foreach match $matches {
7091 set start [lindex $match 0]
7092 set end [expr {[lindex $match 1] + 1}]
7093 $ctext tag add found "1.0 + $start c" "1.0 + $end c"
7094 }
7095 }
7096 drawvisible
7097}
7098
7099# mark the bits of a headline or author that match a find string
7100proc markmatches {canv l str tag matches font row} {
7101 global selectedline foundbgcolor
7102
7103 set bbox [$canv bbox $tag]
7104 set x0 [lindex $bbox 0]
7105 set y0 [lindex $bbox 1]
7106 set y1 [lindex $bbox 3]
7107 foreach match $matches {
7108 set start [lindex $match 0]
7109 set end [lindex $match 1]
7110 if {$start > $end} continue
7111 set xoff [font measure $font [string range $str 0 [expr {$start-1}]]]
7112 set xlen [font measure $font [string range $str 0 [expr {$end}]]]
7113 set t [$canv create rect [expr {$x0+$xoff}] $y0 \
7114 [expr {$x0+$xlen+2}] $y1 \
7115 -outline {} -tags [list match$l matches] -fill $foundbgcolor]
7116 $canv lower $t
7117 if {$row == $selectedline} {
7118 $canv raise $t secsel
7119 }
7120 }
7121}
7122
7123proc unmarkmatches {} {
7124 global markingmatches
7125
7126 allcanvs delete matches
7127 set markingmatches 0
7128 stopfinding
7129}
7130
7131proc selcanvline {w x y} {
7132 global canv canvy0 ctext linespc
7133 global rowtextx
7134 set ymax [lindex [$canv cget -scrollregion] 3]
7135 if {$ymax == {}} return
7136 set yfrac [lindex [$canv yview] 0]
7137 set y [expr {$y + $yfrac * $ymax}]
7138 set l [expr {int(($y - $canvy0) / $linespc + 0.5)}]
7139 if {$l < 0} {
7140 set l 0
7141 }
7142 if {$w eq $canv} {
7143 set xmax [lindex [$canv cget -scrollregion] 2]
7144 set xleft [expr {[lindex [$canv xview] 0] * $xmax}]
7145 if {![info exists rowtextx($l)] || $xleft + $x < $rowtextx($l)} return
7146 }
7147 unmarkmatches
7148 selectline $l 1
7149}
7150
7151proc commit_descriptor {p} {
7152 global commitinfo
7153 if {![info exists commitinfo($p)]} {
7154 getcommit $p
7155 }
7156 set l "..."
7157 if {[llength $commitinfo($p)] > 1} {
7158 set l [lindex $commitinfo($p) 0]
7159 }
7160 return "$p ($l)\n"
7161}
7162
7163# append some text to the ctext widget, and make any SHA1 ID
7164# that we know about be a clickable link.
7165# Also look for URLs of the form "http[s]://..." and make them web links.
7166proc appendwithlinks {text tags} {
7167 global ctext linknum curview
7168 global hashlength
7169
7170 set start [$ctext index "end - 1c"]
7171 $ctext insert end $text $tags
7172 set links [regexp -indices -all -inline [string map "@@ $hashlength" {(?:\m|-g)[0-9a-f]{6,@@}\M}] $text]
7173 foreach l $links {
7174 set s [lindex $l 0]
7175 set e [lindex $l 1]
7176 set linkid [string range $text $s $e]
7177 incr e
7178 $ctext tag delete link$linknum
7179 $ctext tag add link$linknum "$start + $s c" "$start + $e c"
7180 setlink $linkid link$linknum
7181 incr linknum
7182 }
7183 set wlinks [regexp -indices -all -inline -line \
7184 {https?://[^[:space:]]+} $text]
7185 foreach l $wlinks {
7186 set s2 [lindex $l 0]
7187 set e2 [lindex $l 1]
7188 set url [string range $text $s2 $e2]
7189 incr e2
7190 $ctext tag delete link$linknum
7191 $ctext tag add link$linknum "$start + $s2 c" "$start + $e2 c"
7192 setwlink $url link$linknum
7193 incr linknum
7194 }
7195}
7196
7197proc setlink {id lk} {
7198 global curview ctext pendinglinks
7199 global linkfgcolor
7200 global hashlength
7201
7202 if {[string range $id 0 1] eq "-g"} {
7203 set id [string range $id 2 end]
7204 }
7205
7206 set known 0
7207 if {[string length $id] < $hashlength} {
7208 set matches [longid $id]
7209 if {[llength $matches] > 0} {
7210 if {[llength $matches] > 1} return
7211 set known 1
7212 set id [lindex $matches 0]
7213 }
7214 } else {
7215 set known [commitinview $id $curview]
7216 }
7217 if {$known} {
7218 $ctext tag conf $lk -foreground $linkfgcolor -underline 1
7219 $ctext tag bind $lk <1> [list selbyid $id]
7220 $ctext tag bind $lk <Enter> {linkcursor %W 1}
7221 $ctext tag bind $lk <Leave> {linkcursor %W -1}
7222 } else {
7223 lappend pendinglinks($id) $lk
7224 interestedin $id {makelink %P}
7225 }
7226}
7227
7228proc setwlink {url lk} {
7229 global ctext
7230 global linkfgcolor
7231 global web_browser
7232
7233 if {$web_browser eq {}} return
7234 $ctext tag conf $lk -foreground $linkfgcolor -underline 1
7235 $ctext tag bind $lk <1> [list browseweb $url]
7236 $ctext tag bind $lk <Enter> {linkcursor %W 1}
7237 $ctext tag bind $lk <Leave> {linkcursor %W -1}
7238}
7239
7240proc appendshortlink {id {pre {}} {post {}}} {
7241 global ctext linknum
7242
7243 $ctext insert end $pre
7244 $ctext tag delete link$linknum
7245 $ctext insert end [string range $id 0 7] link$linknum
7246 $ctext insert end $post
7247 setlink $id link$linknum
7248 incr linknum
7249}
7250
7251proc makelink {id} {
7252 global pendinglinks
7253
7254 if {![info exists pendinglinks($id)]} return
7255 foreach lk $pendinglinks($id) {
7256 setlink $id $lk
7257 }
7258 unset pendinglinks($id)
7259}
7260
7261proc linkcursor {w inc} {
7262 global linkentercount curtextcursor
7263
7264 if {[incr linkentercount $inc] > 0} {
7265 $w configure -cursor hand2
7266 } else {
7267 $w configure -cursor $curtextcursor
7268 if {$linkentercount < 0} {
7269 set linkentercount 0
7270 }
7271 }
7272}
7273
7274proc browseweb {url} {
7275 global web_browser
7276
7277 if {$web_browser eq {}} return
7278 # Use concat here in case $web_browser is a command plus some arguments
7279 if {[catch {safe_exec_redirect [concat $web_browser [list $url]] [list &]} err]} {
7280 error_popup "[mc "Error starting web browser:"] $err"
7281 }
7282}
7283
7284proc viewnextline {dir} {
7285 global canv linespc
7286
7287 $canv delete hover
7288 set ymax [lindex [$canv cget -scrollregion] 3]
7289 set wnow [$canv yview]
7290 set wtop [expr {[lindex $wnow 0] * $ymax}]
7291 set newtop [expr {$wtop + $dir * $linespc}]
7292 if {$newtop < 0} {
7293 set newtop 0
7294 } elseif {$newtop > $ymax} {
7295 set newtop $ymax
7296 }
7297 allcanvs yview moveto [expr {$newtop * 1.0 / $ymax}]
7298}
7299
7300# add a list of tag or branch names at position pos
7301# returns the number of names inserted
7302proc appendrefs {pos ids var} {
7303 global ctext linknum curview $var maxrefs visiblerefs mainheadid
7304
7305 if {[catch {$ctext index $pos}]} {
7306 return 0
7307 }
7308 $ctext conf -state normal
7309 $ctext delete $pos "$pos lineend"
7310 set tags {}
7311 foreach id $ids {
7312 foreach tag [set $var\($id\)] {
7313 lappend tags [list $tag $id]
7314 }
7315 }
7316
7317 set sep {}
7318 set tags [lsort -index 0 -decreasing $tags]
7319 set nutags 0
7320
7321 if {[llength $tags] > $maxrefs} {
7322 # If we are displaying heads, and there are too many,
7323 # see if there are some important heads to display.
7324 # Currently that are the current head and heads listed in $visiblerefs option
7325 set itags {}
7326 if {$var eq "idheads"} {
7327 set utags {}
7328 foreach ti $tags {
7329 set hname [lindex $ti 0]
7330 set id [lindex $ti 1]
7331 if {([lsearch -exact $visiblerefs $hname] != -1 || $id eq $mainheadid) &&
7332 [llength $itags] < $maxrefs} {
7333 lappend itags $ti
7334 } else {
7335 lappend utags $ti
7336 }
7337 }
7338 set tags $utags
7339 }
7340 if {$itags ne {}} {
7341 set str [mc "and many more"]
7342 set sep " "
7343 } else {
7344 set str [mc "many"]
7345 }
7346 $ctext insert $pos "$str ([llength $tags])"
7347 set nutags [llength $tags]
7348 set tags $itags
7349 }
7350
7351 foreach ti $tags {
7352 set id [lindex $ti 1]
7353 set lk link$linknum
7354 incr linknum
7355 $ctext tag delete $lk
7356 $ctext insert $pos $sep
7357 $ctext insert $pos [lindex $ti 0] $lk
7358 setlink $id $lk
7359 set sep ", "
7360 }
7361 $ctext tag add wwrap "$pos linestart" "$pos lineend"
7362 $ctext conf -state disabled
7363 return [expr {[llength $tags] + $nutags}]
7364}
7365
7366# called when we have finished computing the nearby tags
7367proc dispneartags {delay} {
7368 global selectedline currentid showneartags tagphase
7369
7370 if {$selectedline eq {} || !$showneartags} return
7371 after cancel dispnexttag
7372 if {$delay} {
7373 after 200 dispnexttag
7374 set tagphase -1
7375 } else {
7376 after idle dispnexttag
7377 set tagphase 0
7378 }
7379}
7380
7381proc dispnexttag {} {
7382 global selectedline currentid showneartags tagphase ctext
7383
7384 if {$selectedline eq {} || !$showneartags} return
7385 switch -- $tagphase {
7386 0 {
7387 set dtags [desctags $currentid]
7388 if {$dtags ne {}} {
7389 appendrefs precedes $dtags idtags
7390 }
7391 }
7392 1 {
7393 set atags [anctags $currentid]
7394 if {$atags ne {}} {
7395 appendrefs follows $atags idtags
7396 }
7397 }
7398 2 {
7399 set dheads [descheads $currentid]
7400 if {$dheads ne {}} {
7401 if {[appendrefs branch $dheads idheads] > 1
7402 && [$ctext get "branch -3c"] eq "h"} {
7403 # turn "Branch" into "Branches"
7404 $ctext conf -state normal
7405 $ctext insert "branch -2c" "es"
7406 $ctext conf -state disabled
7407 }
7408 }
7409 }
7410 }
7411 if {[incr tagphase] <= 2} {
7412 after idle dispnexttag
7413 }
7414}
7415
7416proc make_secsel {id} {
7417 global linehtag linentag linedtag canv canv2 canv3
7418
7419 if {![info exists linehtag($id)]} return
7420 $canv delete secsel
7421 set t [eval $canv create rect [$canv bbox $linehtag($id)] -outline {{}} \
7422 -tags secsel -fill [$canv cget -selectbackground]]
7423 $canv lower $t
7424 $canv2 delete secsel
7425 set t [eval $canv2 create rect [$canv2 bbox $linentag($id)] -outline {{}} \
7426 -tags secsel -fill [$canv2 cget -selectbackground]]
7427 $canv2 lower $t
7428 $canv3 delete secsel
7429 set t [eval $canv3 create rect [$canv3 bbox $linedtag($id)] -outline {{}} \
7430 -tags secsel -fill [$canv3 cget -selectbackground]]
7431 $canv3 lower $t
7432}
7433
7434proc make_idmark {id} {
7435 global linehtag canv fgcolor
7436
7437 if {![info exists linehtag($id)]} return
7438 $canv delete markid
7439 set t [eval $canv create rect [$canv bbox $linehtag($id)] \
7440 -tags markid -outline $fgcolor]
7441 $canv raise $t
7442}
7443
7444proc selectline {l isnew {desired_loc {}} {switch_to_patch 0}} {
7445 global canv ctext commitinfo selectedline
7446 global canvy0 linespc parents children curview
7447 global currentid sha1entry
7448 global commentend idtags linknum
7449 global mergemax numcommits pending_select
7450 global cmitmode showneartags allcommits
7451 global targetrow targetid lastscrollrows
7452 global autocopy autoselect autosellen jump_to_here
7453 global vinlinediff
7454
7455 unset -nocomplain pending_select
7456 $canv delete hover
7457 normalline
7458 unsel_reflist
7459 stopfinding
7460 if {$l < 0 || $l >= $numcommits} return
7461 set id [commitonrow $l]
7462 set targetid $id
7463 set targetrow $l
7464 set selectedline $l
7465 set currentid $id
7466 if {$lastscrollrows < $numcommits} {
7467 setcanvscroll
7468 }
7469
7470 if {$cmitmode ne "patch" && $switch_to_patch} {
7471 set cmitmode "patch"
7472 }
7473
7474 set y [expr {$canvy0 + $l * $linespc}]
7475 set ymax [lindex [$canv cget -scrollregion] 3]
7476 set ytop [expr {$y - $linespc - 1}]
7477 set ybot [expr {$y + $linespc + 1}]
7478 set wnow [$canv yview]
7479 set wtop [expr {[lindex $wnow 0] * $ymax}]
7480 set wbot [expr {[lindex $wnow 1] * $ymax}]
7481 set wh [expr {$wbot - $wtop}]
7482 set newtop $wtop
7483 if {$ytop < $wtop} {
7484 if {$ybot < $wtop} {
7485 set newtop [expr {$y - $wh / 2.0}]
7486 } else {
7487 set newtop $ytop
7488 if {$newtop > $wtop - $linespc} {
7489 set newtop [expr {$wtop - $linespc}]
7490 }
7491 }
7492 } elseif {$ybot > $wbot} {
7493 if {$ytop > $wbot} {
7494 set newtop [expr {$y - $wh / 2.0}]
7495 } else {
7496 set newtop [expr {$ybot - $wh}]
7497 if {$newtop < $wtop + $linespc} {
7498 set newtop [expr {$wtop + $linespc}]
7499 }
7500 }
7501 }
7502 if {$newtop != $wtop} {
7503 if {$newtop < 0} {
7504 set newtop 0
7505 }
7506 allcanvs yview moveto [expr {$newtop * 1.0 / $ymax}]
7507 drawvisible
7508 }
7509
7510 make_secsel $id
7511
7512 if {$isnew} {
7513 addtohistory [list selbyid $id 0] savecmitpos
7514 }
7515
7516 $sha1entry delete 0 end
7517 $sha1entry insert 0 $id
7518 if {$autoselect && [haveselectionclipboard]} {
7519 $sha1entry selection range 0 $autosellen
7520 }
7521 if {$autocopy} {
7522 clipboard clear
7523 clipboard append [string range $id 0 [expr $autosellen - 1]]
7524 }
7525 rhighlight_sel $id
7526
7527 $ctext conf -state normal
7528 clear_ctext
7529 set linknum 0
7530 if {![info exists commitinfo($id)]} {
7531 getcommit $id
7532 }
7533 set info $commitinfo($id)
7534 set date [formatdate [lindex $info 2]]
7535 $ctext insert end "[mc "Author"]: [lindex $info 1] $date\n"
7536 set date [formatdate [lindex $info 4]]
7537 $ctext insert end "[mc "Committer"]: [lindex $info 3] $date\n"
7538 if {[info exists idtags($id)]} {
7539 $ctext insert end [mc "Tags:"]
7540 foreach tag $idtags($id) {
7541 $ctext insert end " $tag"
7542 }
7543 $ctext insert end "\n"
7544 }
7545
7546 set headers {}
7547 set olds $parents($curview,$id)
7548 if {[llength $olds] > 1} {
7549 set np 0
7550 foreach p $olds {
7551 if {$np >= $mergemax} {
7552 set tag mmax
7553 } else {
7554 set tag m$np
7555 }
7556 $ctext insert end "[mc "Parent"]: " $tag
7557 appendwithlinks [commit_descriptor $p] {}
7558 incr np
7559 }
7560 } else {
7561 foreach p $olds {
7562 append headers "[mc "Parent"]: [commit_descriptor $p]"
7563 }
7564 }
7565
7566 foreach c $children($curview,$id) {
7567 append headers "[mc "Child"]: [commit_descriptor $c]"
7568 }
7569
7570 # make anything that looks like a SHA1 ID be a clickable link
7571 appendwithlinks $headers {}
7572 if {$showneartags} {
7573 if {![info exists allcommits]} {
7574 getallcommits
7575 }
7576 $ctext insert end "[mc "Branch"]: "
7577 $ctext mark set branch "end -1c"
7578 $ctext mark gravity branch left
7579 $ctext insert end "\n[mc "Follows"]: "
7580 $ctext mark set follows "end -1c"
7581 $ctext mark gravity follows left
7582 $ctext insert end "\n[mc "Precedes"]: "
7583 $ctext mark set precedes "end -1c"
7584 $ctext mark gravity precedes left
7585 $ctext insert end "\n"
7586 dispneartags 1
7587 }
7588 $ctext insert end "\n"
7589 set comment [lindex $info 5]
7590 if {[string first "\r" $comment] >= 0} {
7591 set comment [string map {"\r" "\n "} $comment]
7592 }
7593 appendwithlinks $comment {comment}
7594
7595 $ctext tag remove found 1.0 end
7596 $ctext conf -state disabled
7597 set commentend [$ctext index "end - 1c"]
7598
7599 set jump_to_here $desired_loc
7600 init_flist [mc "Comments"]
7601 if {$cmitmode eq "tree"} {
7602 gettree $id
7603 } elseif {$vinlinediff($curview) == 1} {
7604 showinlinediff $id
7605 } elseif {[llength $olds] <= 1} {
7606 startdiff $id
7607 } else {
7608 mergediff $id
7609 }
7610}
7611
7612proc selfirstline {} {
7613 unmarkmatches
7614 selectline 0 1
7615}
7616
7617proc sellastline {} {
7618 global numcommits
7619 unmarkmatches
7620 set l [expr {$numcommits - 1}]
7621 selectline $l 1
7622}
7623
7624proc selnextline {dir} {
7625 global selectedline
7626 focus .
7627 if {$selectedline eq {}} return
7628 set l [expr {$selectedline + $dir}]
7629 unmarkmatches
7630 selectline $l 1
7631}
7632
7633proc selnextpage {dir} {
7634 global canv linespc selectedline numcommits
7635
7636 set lpp [expr {([winfo height $canv] - 2) / $linespc}]
7637 if {$lpp < 1} {
7638 set lpp 1
7639 }
7640 allcanvs yview scroll [expr {$dir * $lpp}] units
7641 drawvisible
7642 if {$selectedline eq {}} return
7643 set l [expr {$selectedline + $dir * $lpp}]
7644 if {$l < 0} {
7645 set l 0
7646 } elseif {$l >= $numcommits} {
7647 set l [expr $numcommits - 1]
7648 }
7649 unmarkmatches
7650 selectline $l 1
7651}
7652
7653proc unselectline {} {
7654 global selectedline currentid
7655
7656 set selectedline {}
7657 unset -nocomplain currentid
7658 allcanvs delete secsel
7659 rhighlight_none
7660}
7661
7662proc reselectline {} {
7663 global selectedline
7664
7665 if {$selectedline ne {}} {
7666 selectline $selectedline 0
7667 }
7668}
7669
7670proc addtohistory {cmd {saveproc {}}} {
7671 global history historyindex curview
7672
7673 unset_posvars
7674 save_position
7675 set elt [list $curview $cmd $saveproc {}]
7676 if {$historyindex > 0
7677 && [lindex $history [expr {$historyindex - 1}]] == $elt} {
7678 return
7679 }
7680
7681 if {$historyindex < [llength $history]} {
7682 set history [lreplace $history $historyindex end $elt]
7683 } else {
7684 lappend history $elt
7685 }
7686 incr historyindex
7687 if {$historyindex > 1} {
7688 .tf.bar.leftbut conf -state normal
7689 } else {
7690 .tf.bar.leftbut conf -state disabled
7691 }
7692 .tf.bar.rightbut conf -state disabled
7693}
7694
7695# save the scrolling position of the diff display pane
7696proc save_position {} {
7697 global historyindex history
7698
7699 if {$historyindex < 1} return
7700 set hi [expr {$historyindex - 1}]
7701 set fn [lindex $history $hi 2]
7702 if {$fn ne {}} {
7703 lset history $hi 3 [eval $fn]
7704 }
7705}
7706
7707proc unset_posvars {} {
7708 global last_posvars
7709
7710 if {[info exists last_posvars]} {
7711 foreach {var val} $last_posvars {
7712 global $var
7713 unset -nocomplain $var
7714 }
7715 unset last_posvars
7716 }
7717}
7718
7719proc godo {elt} {
7720 global curview last_posvars
7721
7722 set view [lindex $elt 0]
7723 set cmd [lindex $elt 1]
7724 set pv [lindex $elt 3]
7725 if {$curview != $view} {
7726 showview $view
7727 }
7728 unset_posvars
7729 foreach {var val} $pv {
7730 global $var
7731 set $var $val
7732 }
7733 set last_posvars $pv
7734 eval $cmd
7735}
7736
7737proc goback {} {
7738 global history historyindex
7739 focus .
7740
7741 if {$historyindex > 1} {
7742 save_position
7743 incr historyindex -1
7744 godo [lindex $history [expr {$historyindex - 1}]]
7745 .tf.bar.rightbut conf -state normal
7746 }
7747 if {$historyindex <= 1} {
7748 .tf.bar.leftbut conf -state disabled
7749 }
7750}
7751
7752proc goforw {} {
7753 global history historyindex
7754 focus .
7755
7756 if {$historyindex < [llength $history]} {
7757 save_position
7758 set cmd [lindex $history $historyindex]
7759 incr historyindex
7760 godo $cmd
7761 .tf.bar.leftbut conf -state normal
7762 }
7763 if {$historyindex >= [llength $history]} {
7764 .tf.bar.rightbut conf -state disabled
7765 }
7766}
7767
7768proc go_to_parent {i} {
7769 global parents curview targetid
7770 set ps $parents($curview,$targetid)
7771 if {[llength $ps] >= $i} {
7772 selbyid [lindex $ps [expr $i - 1]]
7773 }
7774}
7775
7776proc gettree {id} {
7777 global treefilelist treeidlist diffids diffmergeid treepending
7778 global nullid nullid2
7779
7780 set diffids $id
7781 unset -nocomplain diffmergeid
7782 if {![info exists treefilelist($id)]} {
7783 if {![info exists treepending]} {
7784 if {$id eq $nullid} {
7785 set cmd [list git ls-files]
7786 } elseif {$id eq $nullid2} {
7787 set cmd [list git ls-files --stage -t]
7788 } else {
7789 set cmd [list git ls-tree -r $id]
7790 }
7791 if {[catch {set gtf [safe_open_command $cmd]}]} {
7792 return
7793 }
7794 set treepending $id
7795 set treefilelist($id) {}
7796 set treeidlist($id) {}
7797 fconfigure $gtf -blocking 0 -encoding binary
7798 filerun $gtf [list gettreeline $gtf $id]
7799 }
7800 } else {
7801 setfilelist $id
7802 }
7803}
7804
7805proc gettreeline {gtf id} {
7806 global treefilelist treeidlist treepending cmitmode diffids nullid nullid2
7807
7808 set nl 0
7809 while {[incr nl] <= 1000 && [gets $gtf line] >= 0} {
7810 if {$diffids eq $nullid} {
7811 set fname $line
7812 } else {
7813 set i [string first "\t" $line]
7814 if {$i < 0} continue
7815 set fname [string range $line [expr {$i+1}] end]
7816 set line [string range $line 0 [expr {$i-1}]]
7817 if {$diffids ne $nullid2 && [lindex $line 1] ne "blob"} continue
7818 set sha1 [lindex $line 2]
7819 lappend treeidlist($id) $sha1
7820 }
7821 if {[string index $fname 0] eq "\""} {
7822 set fname [lindex $fname 0]
7823 }
7824 set fname [encoding convertfrom utf-8 $fname]
7825 lappend treefilelist($id) $fname
7826 }
7827 if {![eof $gtf]} {
7828 return [expr {$nl >= 1000? 2: 1}]
7829 }
7830 close $gtf
7831 unset treepending
7832 if {$cmitmode ne "tree"} {
7833 if {![info exists diffmergeid]} {
7834 gettreediffs $diffids
7835 }
7836 } elseif {$id ne $diffids} {
7837 gettree $diffids
7838 } else {
7839 setfilelist $id
7840 }
7841 return 0
7842}
7843
7844proc showfile {f} {
7845 global treefilelist treeidlist diffids nullid nullid2
7846 global ctext_file_names ctext_file_lines
7847 global ctext commentend
7848
7849 set i [lsearch -exact $treefilelist($diffids) $f]
7850 if {$i < 0} {
7851 puts "oops, $f not in list for id $diffids"
7852 return
7853 }
7854 if {$diffids eq $nullid} {
7855 if {[catch {set bf [safe_open_file $f r]} err]} {
7856 puts "oops, can't read $f: $err"
7857 return
7858 }
7859 } else {
7860 set blob [lindex $treeidlist($diffids) $i]
7861 if {[catch {set bf [safe_open_command [concat git cat-file blob $blob]]} err]} {
7862 puts "oops, error reading blob $blob: $err"
7863 return
7864 }
7865 }
7866 fconfigure $bf -blocking 0 -encoding [get_path_encoding $f]
7867 filerun $bf [list getblobline $bf $diffids]
7868 $ctext config -state normal
7869 clear_ctext $commentend
7870 lappend ctext_file_names $f
7871 lappend ctext_file_lines [lindex [split $commentend "."] 0]
7872 $ctext insert end "\n"
7873 $ctext insert end "$f\n" filesep
7874 $ctext config -state disabled
7875 $ctext yview $commentend
7876 settabs 0
7877}
7878
7879proc getblobline {bf id} {
7880 global diffids cmitmode ctext
7881
7882 if {$id ne $diffids || $cmitmode ne "tree"} {
7883 catch {close $bf}
7884 return 0
7885 }
7886 $ctext config -state normal
7887 set nl 0
7888 while {[incr nl] <= 1000 && [gets $bf line] >= 0} {
7889 $ctext insert end "$line\n"
7890 }
7891 if {[eof $bf]} {
7892 global jump_to_here ctext_file_names commentend
7893
7894 # delete last newline
7895 $ctext delete "end - 2c" "end - 1c"
7896 close $bf
7897 if {$jump_to_here ne {} &&
7898 [lindex $jump_to_here 0] eq [lindex $ctext_file_names 0]} {
7899 set lnum [expr {[lindex $jump_to_here 1] +
7900 [lindex [split $commentend .] 0]}]
7901 mark_ctext_line $lnum
7902 }
7903 $ctext config -state disabled
7904 return 0
7905 }
7906 $ctext config -state disabled
7907 return [expr {$nl >= 1000? 2: 1}]
7908}
7909
7910proc mark_ctext_line {lnum} {
7911 global ctext markbgcolor
7912
7913 $ctext tag delete omark
7914 $ctext tag add omark $lnum.0 "$lnum.0 + 1 line"
7915 $ctext tag conf omark -background $markbgcolor
7916 $ctext see $lnum.0
7917}
7918
7919proc mergediff {id} {
7920 global diffmergeid
7921 global diffids treediffs
7922 global parents curview
7923
7924 set diffmergeid $id
7925 set diffids $id
7926 set treediffs($id) {}
7927 set np [llength $parents($curview,$id)]
7928 settabs $np
7929 getblobdiffs $id
7930}
7931
7932proc startdiff {ids} {
7933 global treediffs diffids treepending diffmergeid nullid nullid2
7934
7935 settabs 1
7936 set diffids $ids
7937 unset -nocomplain diffmergeid
7938 if {![info exists treediffs($ids)] ||
7939 [lsearch -exact $ids $nullid] >= 0 ||
7940 [lsearch -exact $ids $nullid2] >= 0} {
7941 if {![info exists treepending]} {
7942 gettreediffs $ids
7943 }
7944 } else {
7945 addtocflist $ids
7946 }
7947}
7948
7949proc showinlinediff {ids} {
7950 global commitinfo commitdata ctext
7951 global treediffs
7952
7953 set info $commitinfo($ids)
7954 set diff [lindex $info 7]
7955 set difflines [split $diff "\n"]
7956
7957 initblobdiffvars
7958 set treediff {}
7959
7960 set inhdr 0
7961 foreach line $difflines {
7962 if {![string compare -length 5 "diff " $line]} {
7963 set inhdr 1
7964 } elseif {$inhdr && ![string compare -length 4 "+++ " $line]} {
7965 # offset also accounts for the b/ prefix
7966 lappend treediff [string range $line 6 end]
7967 set inhdr 0
7968 }
7969 }
7970
7971 set treediffs($ids) $treediff
7972 add_flist $treediff
7973
7974 $ctext conf -state normal
7975 foreach line $difflines {
7976 parseblobdiffline $ids $line
7977 }
7978 maybe_scroll_ctext 1
7979 $ctext conf -state disabled
7980}
7981
7982# If the filename (name) is under any of the passed filter paths
7983# then return true to include the file in the listing.
7984proc path_filter {filter name} {
7985 set worktree [gitworktree]
7986 foreach p $filter {
7987 set fq_p [file normalize $p]
7988 set fq_n [file normalize [file join $worktree $name]]
7989 if {[string match [file normalize $fq_p]* $fq_n]} {
7990 return 1
7991 }
7992 }
7993 return 0
7994}
7995
7996proc addtocflist {ids} {
7997 global treediffs
7998
7999 add_flist $treediffs($ids)
8000 getblobdiffs $ids
8001}
8002
8003proc diffcmd {ids flags} {
8004 global log_showroot nullid nullid2
8005
8006 set i [lsearch -exact $ids $nullid]
8007 set j [lsearch -exact $ids $nullid2]
8008 if {$i >= 0} {
8009 if {[llength $ids] > 1 && $j < 0} {
8010 # comparing working directory with some specific revision
8011 set cmd [concat git diff-index $flags]
8012 if {$i == 0} {
8013 lappend cmd -R [lindex $ids 1]
8014 } else {
8015 lappend cmd [lindex $ids 0]
8016 }
8017 } else {
8018 # comparing working directory with index
8019 set cmd [concat git diff-files $flags]
8020 if {$j == 1} {
8021 lappend cmd -R
8022 }
8023 }
8024 } elseif {$j >= 0} {
8025 set flags "$flags --ignore-submodules=dirty"
8026 set cmd [concat git diff-index --cached $flags]
8027 if {[llength $ids] > 1} {
8028 # comparing index with specific revision
8029 if {$j == 0} {
8030 lappend cmd -R [lindex $ids 1]
8031 } else {
8032 lappend cmd [lindex $ids 0]
8033 }
8034 } else {
8035 # comparing index with HEAD
8036 lappend cmd HEAD
8037 }
8038 } else {
8039 if {$log_showroot} {
8040 lappend flags --root
8041 }
8042 set cmd [concat git diff-tree -r $flags $ids]
8043 }
8044 return $cmd
8045}
8046
8047proc gettreediffs {ids} {
8048 global treediff treepending limitdiffs vfilelimit curview
8049
8050 set cmd [diffcmd $ids {--no-commit-id}]
8051 if {$limitdiffs && $vfilelimit($curview) ne {}} {
8052 set cmd [concat $cmd -- $vfilelimit($curview)]
8053 }
8054 if {[catch {set gdtf [safe_open_command $cmd]}]} return
8055
8056 set treepending $ids
8057 set treediff {}
8058 fconfigure $gdtf -blocking 0 -encoding binary
8059 filerun $gdtf [list gettreediffline $gdtf $ids]
8060}
8061
8062proc gettreediffline {gdtf ids} {
8063 global treediff treediffs treepending diffids diffmergeid
8064 global cmitmode vfilelimit curview limitdiffs perfile_attrs
8065
8066 set nr 0
8067 set sublist {}
8068 set max 1000
8069 if {$perfile_attrs} {
8070 # cache_gitattr is slow, and even slower on win32 where we
8071 # have to invoke it for only about 30 paths at a time
8072 set max 500
8073 if {[tk windowingsystem] == "win32"} {
8074 set max 120
8075 }
8076 }
8077 while {[incr nr] <= $max && [gets $gdtf line] >= 0} {
8078 set i [string first "\t" $line]
8079 if {$i >= 0} {
8080 set file [string range $line [expr {$i+1}] end]
8081 if {[string index $file 0] eq "\""} {
8082 set file [lindex $file 0]
8083 }
8084 set file [encoding convertfrom utf-8 $file]
8085 if {$file ne [lindex $treediff end]} {
8086 lappend treediff $file
8087 lappend sublist $file
8088 }
8089 }
8090 }
8091 if {$perfile_attrs} {
8092 cache_gitattr encoding $sublist
8093 }
8094 if {![eof $gdtf]} {
8095 return [expr {$nr >= $max? 2: 1}]
8096 }
8097 close $gdtf
8098 set treediffs($ids) $treediff
8099 unset treepending
8100 if {$cmitmode eq "tree" && [llength $diffids] == 1} {
8101 gettree $diffids
8102 } elseif {$ids != $diffids} {
8103 if {![info exists diffmergeid]} {
8104 gettreediffs $diffids
8105 }
8106 } else {
8107 addtocflist $ids
8108 }
8109 return 0
8110}
8111
8112# empty string or positive integer
8113proc diffcontextvalidate {v} {
8114 return [regexp {^(|[1-9][0-9]*)$} $v]
8115}
8116
8117proc diffcontextchange {n1 n2 op} {
8118 global diffcontextstring diffcontext
8119
8120 if {[string is integer -strict $diffcontextstring]} {
8121 if {$diffcontextstring >= 0} {
8122 set diffcontext $diffcontextstring
8123 reselectline
8124 }
8125 }
8126}
8127
8128proc changeignorespace {} {
8129 reselectline
8130}
8131
8132proc changeworddiff {name ix op} {
8133 reselectline
8134}
8135
8136proc initblobdiffvars {} {
8137 global diffencoding targetline diffnparents
8138 global diffinhdr currdiffsubmod diffseehere
8139 set targetline {}
8140 set diffnparents 0
8141 set diffinhdr 0
8142 set diffencoding [get_path_encoding {}]
8143 set currdiffsubmod ""
8144 set diffseehere -1
8145}
8146
8147proc getblobdiffs {ids} {
8148 global blobdifffd diffids env
8149 global treediffs
8150 global diffcontext
8151 global ignorespace
8152 global worddiff
8153 global limitdiffs vfilelimit curview
8154
8155 set cmd [diffcmd $ids "-p --textconv --submodule -C --cc --no-commit-id -U$diffcontext"]
8156 if {$ignorespace} {
8157 append cmd " -w"
8158 }
8159 if {$worddiff ne [mc "Line diff"]} {
8160 append cmd " --word-diff=porcelain"
8161 }
8162 if {$limitdiffs && $vfilelimit($curview) ne {}} {
8163 set cmd [concat $cmd -- $vfilelimit($curview)]
8164 }
8165 if {[catch {set bdf [safe_open_command $cmd]} err]} {
8166 error_popup [mc "Error getting diffs: %s" $err]
8167 return
8168 }
8169 fconfigure $bdf -blocking 0 -encoding binary -eofchar {}
8170 set blobdifffd($ids) $bdf
8171 initblobdiffvars
8172 filerun $bdf [list getblobdiffline $bdf $diffids]
8173}
8174
8175proc savecmitpos {} {
8176 global ctext cmitmode
8177
8178 if {$cmitmode eq "tree"} {
8179 return {}
8180 }
8181 return [list target_scrollpos [$ctext index @0,0]]
8182}
8183
8184proc savectextpos {} {
8185 global ctext
8186
8187 return [list target_scrollpos [$ctext index @0,0]]
8188}
8189
8190proc maybe_scroll_ctext {ateof} {
8191 global ctext target_scrollpos
8192
8193 if {![info exists target_scrollpos]} return
8194 if {!$ateof} {
8195 set nlines [expr {[winfo height $ctext]
8196 / [font metrics textfont -linespace]}]
8197 if {[$ctext compare "$target_scrollpos + $nlines lines" <= end]} return
8198 }
8199 $ctext yview $target_scrollpos
8200 unset target_scrollpos
8201}
8202
8203proc setinlist {var i val} {
8204 global $var
8205
8206 while {[llength [set $var]] < $i} {
8207 lappend $var {}
8208 }
8209 if {[llength [set $var]] == $i} {
8210 lappend $var $val
8211 } else {
8212 lset $var $i $val
8213 }
8214}
8215
8216proc makediffhdr {fname ids} {
8217 global ctext curdiffstart treediffs diffencoding
8218 global ctext_file_names jump_to_here targetline diffline
8219
8220 set fname [encoding convertfrom utf-8 $fname]
8221 set diffencoding [get_path_encoding $fname]
8222 set i [lsearch -exact $treediffs($ids) $fname]
8223 if {$i >= 0} {
8224 setinlist difffilestart $i $curdiffstart
8225 }
8226 lset ctext_file_names end $fname
8227 set l [expr {(78 - [string length $fname]) / 2}]
8228 set pad [string range "----------------------------------------" 1 $l]
8229 $ctext insert $curdiffstart "$pad $fname $pad" filesep
8230 set targetline {}
8231 if {$jump_to_here ne {} && [lindex $jump_to_here 0] eq $fname} {
8232 set targetline [lindex $jump_to_here 1]
8233 }
8234 set diffline 0
8235}
8236
8237proc blobdiffmaybeseehere {ateof} {
8238 global diffseehere
8239 if {$diffseehere >= 0} {
8240 mark_ctext_line [lindex [split $diffseehere .] 0]
8241 }
8242 maybe_scroll_ctext $ateof
8243}
8244
8245proc getblobdiffline {bdf ids} {
8246 global diffids blobdifffd
8247 global ctext
8248
8249 set nr 0
8250 $ctext conf -state normal
8251 while {[incr nr] <= 1000 && [gets $bdf line] >= 0} {
8252 if {$ids != $diffids || $bdf != $blobdifffd($ids)} {
8253 # Older diff read. Abort it.
8254 catch {close $bdf}
8255 if {$ids != $diffids} {
8256 array unset blobdifffd $ids
8257 }
8258 return 0
8259 }
8260 parseblobdiffline $ids $line
8261 }
8262 $ctext conf -state disabled
8263 blobdiffmaybeseehere [eof $bdf]
8264 if {[eof $bdf]} {
8265 catch {close $bdf}
8266 array unset blobdifffd $ids
8267 return 0
8268 }
8269 return [expr {$nr >= 1000? 2: 1}]
8270}
8271
8272proc parseblobdiffline {ids line} {
8273 global ctext curdiffstart
8274 global diffnexthead diffnextnote difffilestart
8275 global ctext_file_names ctext_file_lines
8276 global diffinhdr treediffs mergemax diffnparents
8277 global diffencoding jump_to_here targetline diffline currdiffsubmod
8278 global worddiff diffseehere
8279
8280 if {![string compare -length 5 "diff " $line]} {
8281 if {![regexp {^diff (--cc|--git) } $line m type]} {
8282 set line [encoding convertfrom utf-8 $line]
8283 $ctext insert end "$line\n" hunksep
8284 continue
8285 }
8286 # start of a new file
8287 set diffinhdr 1
8288 set currdiffsubmod ""
8289
8290 $ctext insert end "\n"
8291 set curdiffstart [$ctext index "end - 1c"]
8292 lappend ctext_file_names ""
8293 lappend ctext_file_lines [lindex [split $curdiffstart "."] 0]
8294 $ctext insert end "\n" filesep
8295
8296 if {$type eq "--cc"} {
8297 # start of a new file in a merge diff
8298 set fname [string range $line 10 end]
8299 if {[lsearch -exact $treediffs($ids) $fname] < 0} {
8300 lappend treediffs($ids) $fname
8301 add_flist [list $fname]
8302 }
8303
8304 } else {
8305 set line [string range $line 11 end]
8306 # If the name hasn't changed the length will be odd,
8307 # the middle char will be a space, and the two bits either
8308 # side will be a/name and b/name, or "a/name" and "b/name".
8309 # If the name has changed we'll get "rename from" and
8310 # "rename to" or "copy from" and "copy to" lines following
8311 # this, and we'll use them to get the filenames.
8312 # This complexity is necessary because spaces in the
8313 # filename(s) don't get escaped.
8314 set l [string length $line]
8315 set i [expr {$l / 2}]
8316 if {!(($l & 1) && [string index $line $i] eq " " &&
8317 [string range $line 2 [expr {$i - 1}]] eq \
8318 [string range $line [expr {$i + 3}] end])} {
8319 return
8320 }
8321 # unescape if quoted and chop off the a/ from the front
8322 if {[string index $line 0] eq "\""} {
8323 set fname [string range [lindex $line 0] 2 end]
8324 } else {
8325 set fname [string range $line 2 [expr {$i - 1}]]
8326 }
8327 }
8328 makediffhdr $fname $ids
8329
8330 } elseif {![string compare -length 16 "* Unmerged path " $line]} {
8331 set fname [encoding convertfrom utf-8 [string range $line 16 end]]
8332 $ctext insert end "\n"
8333 set curdiffstart [$ctext index "end - 1c"]
8334 lappend ctext_file_names $fname
8335 lappend ctext_file_lines [lindex [split $curdiffstart "."] 0]
8336 $ctext insert end "$line\n" filesep
8337 set i [lsearch -exact $treediffs($ids) $fname]
8338 if {$i >= 0} {
8339 setinlist difffilestart $i $curdiffstart
8340 }
8341
8342 } elseif {![string compare -length 2 "@@" $line]} {
8343 regexp {^@@+} $line ats
8344 set line [encoding convertfrom $diffencoding $line]
8345 $ctext insert end "$line\n" hunksep
8346 if {[regexp { \+(\d+),\d+ @@} $line m nl]} {
8347 set diffline $nl
8348 }
8349 set diffnparents [expr {[string length $ats] - 1}]
8350 set diffinhdr 0
8351
8352 } elseif {![string compare -length 10 "Submodule " $line]} {
8353 # start of a new submodule
8354 if {[regexp -indices "\[0-9a-f\]+\\.\\." $line nameend]} {
8355 set fname [string range $line 10 [expr [lindex $nameend 0] - 2]]
8356 } else {
8357 set fname [string range $line 10 [expr [string first "contains " $line] - 2]]
8358 }
8359 if {$currdiffsubmod != $fname} {
8360 $ctext insert end "\n"; # Add newline after commit message
8361 }
8362 if {$currdiffsubmod != $fname} {
8363 set curdiffstart [$ctext index "end - 1c"]
8364 lappend ctext_file_names ""
8365 lappend ctext_file_lines [lindex [split $curdiffstart "."] 0]
8366 makediffhdr $fname $ids
8367 set currdiffsubmod $fname
8368 $ctext insert end "\n$line\n" filesep
8369 } else {
8370 $ctext insert end "$line\n" filesep
8371 }
8372 } elseif {$currdiffsubmod != "" && ![string compare -length 3 " >" $line]} {
8373 set line [encoding convertfrom $diffencoding $line]
8374 $ctext insert end "$line\n" dresult
8375 } elseif {$currdiffsubmod != "" && ![string compare -length 3 " <" $line]} {
8376 set line [encoding convertfrom $diffencoding $line]
8377 $ctext insert end "$line\n" d0
8378 } elseif {$diffinhdr} {
8379 if {![string compare -length 12 "rename from " $line]} {
8380 set fname [string range $line [expr 6 + [string first " from " $line] ] end]
8381 if {[string index $fname 0] eq "\""} {
8382 set fname [lindex $fname 0]
8383 }
8384 set fname [encoding convertfrom utf-8 $fname]
8385 set i [lsearch -exact $treediffs($ids) $fname]
8386 if {$i >= 0} {
8387 setinlist difffilestart $i $curdiffstart
8388 }
8389 } elseif {![string compare -length 10 $line "rename to "] ||
8390 ![string compare -length 8 $line "copy to "]} {
8391 set fname [string range $line [expr 4 + [string first " to " $line] ] end]
8392 if {[string index $fname 0] eq "\""} {
8393 set fname [lindex $fname 0]
8394 }
8395 makediffhdr $fname $ids
8396 } elseif {[string compare -length 3 $line "---"] == 0} {
8397 # do nothing
8398 return
8399 } elseif {[string compare -length 3 $line "+++"] == 0} {
8400 set diffinhdr 0
8401 return
8402 }
8403 set line [encoding convertfrom utf-8 $line]
8404 $ctext insert end "$line\n" filesep
8405
8406 } else {
8407 set line [string map {\x1A ^Z} \
8408 [encoding convertfrom $diffencoding $line]]
8409 # parse the prefix - one ' ', '-' or '+' for each parent
8410 set prefix [string range $line 0 [expr {$diffnparents - 1}]]
8411 set tag [expr {$diffnparents > 1? "m": "d"}]
8412 set dowords [expr {$worddiff ne [mc "Line diff"] && $diffnparents == 1}]
8413 set words_pre_markup ""
8414 set words_post_markup ""
8415 if {[string trim $prefix " -+"] eq {}} {
8416 # prefix only has " ", "-" and "+" in it: normal diff line
8417 set num [string first "-" $prefix]
8418 if {$dowords} {
8419 set line [string range $line 1 end]
8420 }
8421 if {$num >= 0} {
8422 # removed line, first parent with line is $num
8423 if {$num >= $mergemax} {
8424 set num "max"
8425 }
8426 if {$dowords && $worddiff eq [mc "Markup words"]} {
8427 $ctext insert end "\[-$line-\]" $tag$num
8428 } else {
8429 $ctext insert end "$line" $tag$num
8430 }
8431 if {!$dowords} {
8432 $ctext insert end "\n" $tag$num
8433 }
8434 } else {
8435 set tags {}
8436 if {[string first "+" $prefix] >= 0} {
8437 # added line
8438 lappend tags ${tag}result
8439 if {$diffnparents > 1} {
8440 set num [string first " " $prefix]
8441 if {$num >= 0} {
8442 if {$num >= $mergemax} {
8443 set num "max"
8444 }
8445 lappend tags m$num
8446 }
8447 }
8448 set words_pre_markup "{+"
8449 set words_post_markup "+}"
8450 }
8451 if {$targetline ne {}} {
8452 if {$diffline == $targetline} {
8453 set diffseehere [$ctext index "end - 1 chars"]
8454 set targetline {}
8455 } else {
8456 incr diffline
8457 }
8458 }
8459 if {$dowords && $worddiff eq [mc "Markup words"]} {
8460 $ctext insert end "$words_pre_markup$line$words_post_markup" $tags
8461 } else {
8462 $ctext insert end "$line" $tags
8463 }
8464 if {!$dowords} {
8465 $ctext insert end "\n" $tags
8466 }
8467 }
8468 } elseif {$dowords && $prefix eq "~"} {
8469 $ctext insert end "\n" {}
8470 } else {
8471 # "\ No newline at end of file",
8472 # or something else we don't recognize
8473 $ctext insert end "$line\n" hunksep
8474 }
8475 }
8476}
8477
8478proc changediffdisp {} {
8479 global ctext diffelide
8480
8481 $ctext tag conf d0 -elide [lindex $diffelide 0]
8482 $ctext tag conf dresult -elide [lindex $diffelide 1]
8483}
8484
8485proc highlightfile {cline} {
8486 global cflist cflist_top
8487
8488 if {![info exists cflist_top]} return
8489
8490 $cflist tag remove highlight $cflist_top.0 "$cflist_top.0 lineend"
8491 $cflist tag add highlight $cline.0 "$cline.0 lineend"
8492 $cflist see $cline.0
8493 set cflist_top $cline
8494}
8495
8496proc highlightfile_for_scrollpos {topidx} {
8497 global cmitmode difffilestart
8498
8499 if {$cmitmode eq "tree"} return
8500 if {![info exists difffilestart]} return
8501
8502 set top [lindex [split $topidx .] 0]
8503 if {$difffilestart eq {} || $top < [lindex $difffilestart 0]} {
8504 highlightfile 0
8505 } else {
8506 highlightfile [expr {[bsearch $difffilestart $top] + 2}]
8507 }
8508}
8509
8510proc prevfile {} {
8511 global difffilestart ctext cmitmode
8512
8513 if {$cmitmode eq "tree"} return
8514 set prev 0.0
8515 set here [$ctext index @0,0]
8516 foreach loc $difffilestart {
8517 if {[$ctext compare $loc >= $here]} {
8518 $ctext yview $prev
8519 return
8520 }
8521 set prev $loc
8522 }
8523 $ctext yview $prev
8524}
8525
8526proc nextfile {} {
8527 global difffilestart ctext cmitmode
8528
8529 if {$cmitmode eq "tree"} return
8530 set here [$ctext index @0,0]
8531 foreach loc $difffilestart {
8532 if {[$ctext compare $loc > $here]} {
8533 $ctext yview $loc
8534 return
8535 }
8536 }
8537}
8538
8539proc clear_ctext {{first 1.0}} {
8540 global ctext smarktop smarkbot
8541 global ctext_file_names ctext_file_lines
8542 global pendinglinks
8543
8544 set l [lindex [split $first .] 0]
8545 if {![info exists smarktop] || [$ctext compare $first < $smarktop.0]} {
8546 set smarktop $l
8547 }
8548 if {![info exists smarkbot] || [$ctext compare $first < $smarkbot.0]} {
8549 set smarkbot $l
8550 }
8551 $ctext delete $first end
8552 if {$first eq "1.0"} {
8553 unset -nocomplain pendinglinks
8554 }
8555 set ctext_file_names {}
8556 set ctext_file_lines {}
8557}
8558
8559proc settabs {{firstab {}}} {
8560 global firsttabstop tabstop ctext
8561
8562 if {$firstab ne {}} {
8563 set firsttabstop $firstab
8564 }
8565 set w [font measure textfont "0"]
8566 if {$firsttabstop != 0} {
8567 $ctext conf -tabs [list [expr {($firsttabstop + $tabstop) * $w}] \
8568 [expr {($firsttabstop + 2 * $tabstop) * $w}]]
8569 } else {
8570 $ctext conf -tabs [expr {$tabstop * $w}]
8571 }
8572}
8573
8574proc incrsearch {name ix op} {
8575 global ctext searchstring searchdirn
8576
8577 if {[catch {$ctext index anchor}]} {
8578 # no anchor set, use start of selection, or of visible area
8579 set sel [$ctext tag ranges sel]
8580 if {$sel ne {}} {
8581 $ctext mark set anchor [lindex $sel 0]
8582 } elseif {$searchdirn eq "-forwards"} {
8583 $ctext mark set anchor @0,0
8584 } else {
8585 $ctext mark set anchor @0,[winfo height $ctext]
8586 }
8587 }
8588 if {$searchstring ne {}} {
8589 set here [$ctext search -count mlen $searchdirn -- $searchstring anchor]
8590 if {$here ne {}} {
8591 $ctext see $here
8592 set mend "$here + $mlen c"
8593 $ctext tag remove sel 1.0 end
8594 $ctext tag add sel $here $mend
8595 suppress_highlighting_file_for_current_scrollpos
8596 highlightfile_for_scrollpos $here
8597 }
8598 }
8599 rehighlight_search_results
8600}
8601
8602proc dosearch {} {
8603 global sstring ctext searchstring searchdirn
8604
8605 focus $sstring
8606 $sstring icursor end
8607 set searchdirn -forwards
8608 if {$searchstring ne {}} {
8609 set sel [$ctext tag ranges sel]
8610 if {$sel ne {}} {
8611 set start "[lindex $sel 0] + 1c"
8612 } elseif {[catch {set start [$ctext index anchor]}]} {
8613 set start "@0,0"
8614 }
8615 set match [$ctext search -count mlen -- $searchstring $start]
8616 $ctext tag remove sel 1.0 end
8617 if {$match eq {}} {
8618 bell
8619 return
8620 }
8621 $ctext see $match
8622 suppress_highlighting_file_for_current_scrollpos
8623 highlightfile_for_scrollpos $match
8624 set mend "$match + $mlen c"
8625 $ctext tag add sel $match $mend
8626 $ctext mark unset anchor
8627 rehighlight_search_results
8628 }
8629}
8630
8631proc dosearchback {} {
8632 global sstring ctext searchstring searchdirn
8633
8634 focus $sstring
8635 $sstring icursor end
8636 set searchdirn -backwards
8637 if {$searchstring ne {}} {
8638 set sel [$ctext tag ranges sel]
8639 if {$sel ne {}} {
8640 set start [lindex $sel 0]
8641 } elseif {[catch {set start [$ctext index anchor]}]} {
8642 set start @0,[winfo height $ctext]
8643 }
8644 set match [$ctext search -backwards -count ml -- $searchstring $start]
8645 $ctext tag remove sel 1.0 end
8646 if {$match eq {}} {
8647 bell
8648 return
8649 }
8650 $ctext see $match
8651 suppress_highlighting_file_for_current_scrollpos
8652 highlightfile_for_scrollpos $match
8653 set mend "$match + $ml c"
8654 $ctext tag add sel $match $mend
8655 $ctext mark unset anchor
8656 rehighlight_search_results
8657 }
8658}
8659
8660proc rehighlight_search_results {} {
8661 global ctext searchstring
8662
8663 $ctext tag remove found 1.0 end
8664 $ctext tag remove currentsearchhit 1.0 end
8665
8666 if {$searchstring ne {}} {
8667 searchmarkvisible 1
8668 }
8669}
8670
8671proc searchmark {first last} {
8672 global ctext searchstring
8673
8674 set sel [$ctext tag ranges sel]
8675
8676 set mend $first.0
8677 while {1} {
8678 set match [$ctext search -count mlen -- $searchstring $mend $last.end]
8679 if {$match eq {}} break
8680 set mend "$match + $mlen c"
8681 if {$sel ne {} && [$ctext compare $match == [lindex $sel 0]]} {
8682 $ctext tag add currentsearchhit $match $mend
8683 } else {
8684 $ctext tag add found $match $mend
8685 }
8686 }
8687}
8688
8689proc searchmarkvisible {doall} {
8690 global ctext smarktop smarkbot
8691
8692 set topline [lindex [split [$ctext index @0,0] .] 0]
8693 set botline [lindex [split [$ctext index @0,[winfo height $ctext]] .] 0]
8694 if {$doall || $botline < $smarktop || $topline > $smarkbot} {
8695 # no overlap with previous
8696 searchmark $topline $botline
8697 set smarktop $topline
8698 set smarkbot $botline
8699 } else {
8700 if {$topline < $smarktop} {
8701 searchmark $topline [expr {$smarktop-1}]
8702 set smarktop $topline
8703 }
8704 if {$botline > $smarkbot} {
8705 searchmark [expr {$smarkbot+1}] $botline
8706 set smarkbot $botline
8707 }
8708 }
8709}
8710
8711proc suppress_highlighting_file_for_current_scrollpos {} {
8712 global ctext suppress_highlighting_file_for_this_scrollpos
8713
8714 set suppress_highlighting_file_for_this_scrollpos [$ctext index @0,0]
8715}
8716
8717proc scrolltext {f0 f1} {
8718 global searchstring cmitmode ctext
8719 global suppress_highlighting_file_for_this_scrollpos
8720
8721 set topidx [$ctext index @0,0]
8722 if {![info exists suppress_highlighting_file_for_this_scrollpos]
8723 || $topidx ne $suppress_highlighting_file_for_this_scrollpos} {
8724 highlightfile_for_scrollpos $topidx
8725 }
8726
8727 unset -nocomplain suppress_highlighting_file_for_this_scrollpos
8728
8729 .bleft.bottom.sb set $f0 $f1
8730 if {$searchstring ne {}} {
8731 searchmarkvisible 0
8732 }
8733}
8734
8735proc setcoords {} {
8736 global linespc charspc canvx0 canvy0
8737 global xspc1 xspc2 lthickness
8738
8739 set linespc [font metrics mainfont -linespace]
8740 set charspc [font measure mainfont "m"]
8741 set canvy0 [expr {int(3 + 0.5 * $linespc)}]
8742 set canvx0 [expr {int(3 + 0.5 * $linespc)}]
8743 set lthickness [expr {int($linespc / 9) + 1}]
8744 set xspc1(0) $linespc
8745 set xspc2 $linespc
8746}
8747
8748proc redisplay {} {
8749 global canv
8750 global selectedline
8751
8752 set ymax [lindex [$canv cget -scrollregion] 3]
8753 if {$ymax eq {} || $ymax == 0} return
8754 set span [$canv yview]
8755 clear_display
8756 setcanvscroll
8757 allcanvs yview moveto [lindex $span 0]
8758 drawvisible
8759 if {$selectedline ne {}} {
8760 selectline $selectedline 0
8761 allcanvs yview moveto [lindex $span 0]
8762 }
8763}
8764
8765proc parsefont {f n} {
8766 global fontattr
8767
8768 set fontattr($f,family) [lindex $n 0]
8769 set s [lindex $n 1]
8770 if {$s eq {} || $s == 0} {
8771 set s 10
8772 } elseif {$s < 0} {
8773 set s [expr {int(-$s / [winfo fpixels . 1p] + 0.5)}]
8774 }
8775 set fontattr($f,size) $s
8776 set fontattr($f,weight) normal
8777 set fontattr($f,slant) roman
8778 foreach style [lrange $n 2 end] {
8779 switch -- $style {
8780 "normal" -
8781 "bold" {set fontattr($f,weight) $style}
8782 "roman" -
8783 "italic" {set fontattr($f,slant) $style}
8784 }
8785 }
8786}
8787
8788proc fontflags {f {isbold 0}} {
8789 global fontattr
8790
8791 return [list -family $fontattr($f,family) -size $fontattr($f,size) \
8792 -weight [expr {$isbold? "bold": $fontattr($f,weight)}] \
8793 -slant $fontattr($f,slant)]
8794}
8795
8796proc fontname {f} {
8797 global fontattr
8798
8799 set n [list $fontattr($f,family) $fontattr($f,size)]
8800 if {$fontattr($f,weight) eq "bold"} {
8801 lappend n "bold"
8802 }
8803 if {$fontattr($f,slant) eq "italic"} {
8804 lappend n "italic"
8805 }
8806 return $n
8807}
8808
8809proc incrfont {inc} {
8810 global mainfont textfont ctext canv cflist showrefstop
8811 global stopped entries fontattr
8812
8813 unmarkmatches
8814 set s $fontattr(mainfont,size)
8815 incr s $inc
8816 if {$s < 1} {
8817 set s 1
8818 }
8819 set fontattr(mainfont,size) $s
8820 font config mainfont -size $s
8821 font config mainfontbold -size $s
8822 set mainfont [fontname mainfont]
8823 set s $fontattr(textfont,size)
8824 incr s $inc
8825 if {$s < 1} {
8826 set s 1
8827 }
8828 set fontattr(textfont,size) $s
8829 font config textfont -size $s
8830 font config textfontbold -size $s
8831 set textfont [fontname textfont]
8832 setcoords
8833 settabs
8834 redisplay
8835}
8836
8837proc clearsha1 {} {
8838 global sha1entry sha1string
8839 global hashlength
8840
8841 if {[string length $sha1string] == $hashlength} {
8842 $sha1entry delete 0 end
8843 }
8844}
8845
8846proc sha1change {n1 n2 op} {
8847 global sha1string currentid sha1but
8848
8849 if {$sha1string == {}
8850 || ([info exists currentid] && $sha1string == $currentid)} {
8851 set state disabled
8852 } else {
8853 set state normal
8854 }
8855 if {[$sha1but cget -state] == $state} return
8856 if {$state == "normal"} {
8857 $sha1but conf -state normal -relief raised -text "[mc "Goto:"] "
8858 } else {
8859 $sha1but conf -state disabled -relief flat -text "[mc "Commit ID:"] "
8860 }
8861}
8862
8863proc gotocommit {} {
8864 global sha1string tagids headids curview varcid
8865 global hashlength
8866
8867 if {$sha1string == {}
8868 || ([info exists currentid] && $sha1string == $currentid)} return
8869 if {[info exists tagids($sha1string)]} {
8870 set id $tagids($sha1string)
8871 } elseif {[info exists headids($sha1string)]} {
8872 set id $headids($sha1string)
8873 } else {
8874 set id [string tolower $sha1string]
8875 if {[regexp {^[0-9a-f]{4,63}$} $id]} {
8876 set matches [longid $id]
8877 if {$matches ne {}} {
8878 if {[llength $matches] > 1} {
8879 error_popup [mc "Short commit ID %s is ambiguous" $id]
8880 return
8881 }
8882 set id [lindex $matches 0]
8883 }
8884 } else {
8885 if {[catch {set id [safe_exec [list git rev-parse --verify $sha1string]]}]} {
8886 error_popup [mc "Revision %s is not known" $sha1string]
8887 return
8888 }
8889 }
8890 }
8891 if {[commitinview $id $curview]} {
8892 selectline [rowofcommit $id] 1
8893 return
8894 }
8895 if {[regexp {^[0-9a-fA-F]{4,}$} $sha1string]} {
8896 set msg [mc "Commit ID %s is not known" $sha1string]
8897 } else {
8898 set msg [mc "Revision %s is not in the current view" $sha1string]
8899 }
8900 error_popup $msg
8901}
8902
8903proc lineenter {x y id} {
8904 global hoverx hovery hoverid hovertimer
8905 global commitinfo canv
8906
8907 if {![info exists commitinfo($id)] && ![getcommit $id]} return
8908 set hoverx $x
8909 set hovery $y
8910 set hoverid $id
8911 if {[info exists hovertimer]} {
8912 after cancel $hovertimer
8913 }
8914 set hovertimer [after 500 linehover]
8915 $canv delete hover
8916}
8917
8918proc linemotion {x y id} {
8919 global hoverx hovery hoverid hovertimer
8920
8921 if {[info exists hoverid] && $id == $hoverid} {
8922 set hoverx $x
8923 set hovery $y
8924 if {[info exists hovertimer]} {
8925 after cancel $hovertimer
8926 }
8927 set hovertimer [after 500 linehover]
8928 }
8929}
8930
8931proc lineleave {id} {
8932 global hoverid hovertimer canv
8933
8934 if {[info exists hoverid] && $id == $hoverid} {
8935 $canv delete hover
8936 if {[info exists hovertimer]} {
8937 after cancel $hovertimer
8938 unset hovertimer
8939 }
8940 unset hoverid
8941 }
8942}
8943
8944proc linehover {} {
8945 global hoverx hovery hoverid hovertimer
8946 global canv linespc lthickness
8947 global linehoverbgcolor linehoverfgcolor linehoveroutlinecolor
8948
8949 global commitinfo
8950
8951 set text [lindex $commitinfo($hoverid) 0]
8952 set ymax [lindex [$canv cget -scrollregion] 3]
8953 if {$ymax == {}} return
8954 set yfrac [lindex [$canv yview] 0]
8955 set x [expr {$hoverx + 2 * $linespc}]
8956 set y [expr {$hovery + $yfrac * $ymax - $linespc / 2}]
8957 set x0 [expr {$x - 2 * $lthickness}]
8958 set y0 [expr {$y - 2 * $lthickness}]
8959 set x1 [expr {$x + [font measure mainfont $text] + 2 * $lthickness}]
8960 set y1 [expr {$y + $linespc + 2 * $lthickness}]
8961 set t [$canv create rectangle $x0 $y0 $x1 $y1 \
8962 -fill $linehoverbgcolor -outline $linehoveroutlinecolor \
8963 -width 1 -tags hover]
8964 $canv raise $t
8965 set t [$canv create text $x $y -anchor nw -text $text -tags hover \
8966 -font mainfont -fill $linehoverfgcolor]
8967 $canv raise $t
8968}
8969
8970proc clickisonarrow {id y} {
8971 global lthickness
8972
8973 set ranges [rowranges $id]
8974 set thresh [expr {2 * $lthickness + 6}]
8975 set n [expr {[llength $ranges] - 1}]
8976 for {set i 1} {$i < $n} {incr i} {
8977 set row [lindex $ranges $i]
8978 if {abs([yc $row] - $y) < $thresh} {
8979 return $i
8980 }
8981 }
8982 return {}
8983}
8984
8985proc arrowjump {id n y} {
8986 global canv
8987
8988 # 1 <-> 2, 3 <-> 4, etc...
8989 set n [expr {(($n - 1) ^ 1) + 1}]
8990 set row [lindex [rowranges $id] $n]
8991 set yt [yc $row]
8992 set ymax [lindex [$canv cget -scrollregion] 3]
8993 if {$ymax eq {} || $ymax <= 0} return
8994 set view [$canv yview]
8995 set yspan [expr {[lindex $view 1] - [lindex $view 0]}]
8996 set yfrac [expr {$yt / $ymax - $yspan / 2}]
8997 if {$yfrac < 0} {
8998 set yfrac 0
8999 }
9000 allcanvs yview moveto $yfrac
9001}
9002
9003proc lineclick {x y id isnew} {
9004 global ctext commitinfo children canv thickerline curview
9005
9006 if {![info exists commitinfo($id)] && ![getcommit $id]} return
9007 unmarkmatches
9008 unselectline
9009 normalline
9010 $canv delete hover
9011 # draw this line thicker than normal
9012 set thickerline $id
9013 drawlines $id
9014 if {$isnew} {
9015 set ymax [lindex [$canv cget -scrollregion] 3]
9016 if {$ymax eq {}} return
9017 set yfrac [lindex [$canv yview] 0]
9018 set y [expr {$y + $yfrac * $ymax}]
9019 }
9020 set dirn [clickisonarrow $id $y]
9021 if {$dirn ne {}} {
9022 arrowjump $id $dirn $y
9023 return
9024 }
9025
9026 if {$isnew} {
9027 addtohistory [list lineclick $x $y $id 0] savectextpos
9028 }
9029 # fill the details pane with info about this line
9030 $ctext conf -state normal
9031 clear_ctext
9032 settabs 0
9033 $ctext insert end "[mc "Parent"]:\t"
9034 $ctext insert end $id link0
9035 setlink $id link0
9036 set info $commitinfo($id)
9037 $ctext insert end "\n\t[lindex $info 0]\n"
9038 $ctext insert end "\t[mc "Author"]:\t[lindex $info 1]\n"
9039 set date [formatdate [lindex $info 2]]
9040 $ctext insert end "\t[mc "Date"]:\t$date\n"
9041 set kids $children($curview,$id)
9042 if {$kids ne {}} {
9043 $ctext insert end "\n[mc "Children"]:"
9044 set i 0
9045 foreach child $kids {
9046 incr i
9047 if {![info exists commitinfo($child)] && ![getcommit $child]} continue
9048 set info $commitinfo($child)
9049 $ctext insert end "\n\t"
9050 $ctext insert end $child link$i
9051 setlink $child link$i
9052 $ctext insert end "\n\t[lindex $info 0]"
9053 $ctext insert end "\n\t[mc "Author"]:\t[lindex $info 1]"
9054 set date [formatdate [lindex $info 2]]
9055 $ctext insert end "\n\t[mc "Date"]:\t$date\n"
9056 }
9057 }
9058 maybe_scroll_ctext 1
9059 $ctext conf -state disabled
9060 init_flist {}
9061}
9062
9063proc normalline {} {
9064 global thickerline
9065 if {[info exists thickerline]} {
9066 set id $thickerline
9067 unset thickerline
9068 drawlines $id
9069 }
9070}
9071
9072proc selbyid {id {isnew 1}} {
9073 global curview
9074 if {[commitinview $id $curview]} {
9075 selectline [rowofcommit $id] $isnew
9076 }
9077}
9078
9079proc mstime {} {
9080 global startmstime
9081 if {![info exists startmstime]} {
9082 set startmstime [clock clicks -milliseconds]
9083 }
9084 return [format "%.3f" [expr {([clock click -milliseconds] - $startmstime) / 1000.0}]]
9085}
9086
9087proc rowmenu {x y id} {
9088 global rowctxmenu selectedline rowmenuid curview
9089 global nullid nullid2 fakerowmenu mainhead markedid
9090
9091 stopfinding
9092 set rowmenuid $id
9093 if {$selectedline eq {} || [rowofcommit $id] eq $selectedline} {
9094 set state disabled
9095 } else {
9096 set state normal
9097 }
9098 if {[info exists markedid] && $markedid ne $id} {
9099 set mstate normal
9100 } else {
9101 set mstate disabled
9102 }
9103 if {$id ne $nullid && $id ne $nullid2} {
9104 set menu $rowctxmenu
9105 if {$mainhead ne {}} {
9106 $menu entryconfigure 8 -label [mc "Reset %s branch to here" $mainhead] -state normal
9107 } else {
9108 $menu entryconfigure 8 -label [mc "Detached head: can't reset" $mainhead] -state disabled
9109 }
9110 $menu entryconfigure 10 -state $mstate
9111 $menu entryconfigure 11 -state $mstate
9112 $menu entryconfigure 12 -state $mstate
9113 } else {
9114 set menu $fakerowmenu
9115 }
9116 $menu entryconfigure [mca "Diff this -> selected"] -state $state
9117 $menu entryconfigure [mca "Diff selected -> this"] -state $state
9118 $menu entryconfigure [mca "Make patch"] -state $state
9119 $menu entryconfigure [mca "Diff this -> marked commit"] -state $mstate
9120 $menu entryconfigure [mca "Diff marked commit -> this"] -state $mstate
9121 tk_popup $menu $x $y
9122}
9123
9124proc markhere {} {
9125 global rowmenuid markedid canv
9126
9127 set markedid $rowmenuid
9128 make_idmark $markedid
9129}
9130
9131proc gotomark {} {
9132 global markedid
9133
9134 if {[info exists markedid]} {
9135 selbyid $markedid
9136 }
9137}
9138
9139proc replace_by_kids {l r} {
9140 global curview children
9141
9142 set id [commitonrow $r]
9143 set l [lreplace $l 0 0]
9144 foreach kid $children($curview,$id) {
9145 lappend l [rowofcommit $kid]
9146 }
9147 return [lsort -integer -decreasing -unique $l]
9148}
9149
9150proc find_common_desc {} {
9151 global markedid rowmenuid curview children
9152
9153 if {![info exists markedid]} return
9154 if {![commitinview $markedid $curview] ||
9155 ![commitinview $rowmenuid $curview]} return
9156 #set t1 [clock clicks -milliseconds]
9157 set l1 [list [rowofcommit $markedid]]
9158 set l2 [list [rowofcommit $rowmenuid]]
9159 while 1 {
9160 set r1 [lindex $l1 0]
9161 set r2 [lindex $l2 0]
9162 if {$r1 eq {} || $r2 eq {}} break
9163 if {$r1 == $r2} {
9164 selectline $r1 1
9165 break
9166 }
9167 if {$r1 > $r2} {
9168 set l1 [replace_by_kids $l1 $r1]
9169 } else {
9170 set l2 [replace_by_kids $l2 $r2]
9171 }
9172 }
9173 #set t2 [clock clicks -milliseconds]
9174 #puts "took [expr {$t2-$t1}]ms"
9175}
9176
9177proc compare_commits {} {
9178 global markedid rowmenuid curview children
9179
9180 if {![info exists markedid]} return
9181 if {![commitinview $markedid $curview]} return
9182 addtohistory [list do_cmp_commits $markedid $rowmenuid]
9183 do_cmp_commits $markedid $rowmenuid
9184}
9185
9186proc getpatchid {id} {
9187 global patchids
9188
9189 if {![info exists patchids($id)]} {
9190 set cmd [diffcmd [list $id] {-p --root}]
9191 if {[catch {
9192 set x [safe_exec_redirect $cmd [list | git patch-id]]
9193 set patchids($id) [lindex $x 0]
9194 }]} {
9195 set patchids($id) "error"
9196 }
9197 }
9198 return $patchids($id)
9199}
9200
9201proc do_cmp_commits {a b} {
9202 global ctext curview parents children patchids commitinfo
9203
9204 $ctext conf -state normal
9205 clear_ctext
9206 init_flist {}
9207 for {set i 0} {$i < 100} {incr i} {
9208 set skipa 0
9209 set skipb 0
9210 if {[llength $parents($curview,$a)] > 1} {
9211 appendshortlink $a [mc "Skipping merge commit "] "\n"
9212 set skipa 1
9213 } else {
9214 set patcha [getpatchid $a]
9215 }
9216 if {[llength $parents($curview,$b)] > 1} {
9217 appendshortlink $b [mc "Skipping merge commit "] "\n"
9218 set skipb 1
9219 } else {
9220 set patchb [getpatchid $b]
9221 }
9222 if {!$skipa && !$skipb} {
9223 set heada [lindex $commitinfo($a) 0]
9224 set headb [lindex $commitinfo($b) 0]
9225 if {$patcha eq "error"} {
9226 appendshortlink $a [mc "Error getting patch ID for "] \
9227 [mc " - stopping\n"]
9228 break
9229 }
9230 if {$patchb eq "error"} {
9231 appendshortlink $b [mc "Error getting patch ID for "] \
9232 [mc " - stopping\n"]
9233 break
9234 }
9235 if {$patcha eq $patchb} {
9236 if {$heada eq $headb} {
9237 appendshortlink $a [mc "Commit "]
9238 appendshortlink $b " == " " $heada\n"
9239 } else {
9240 appendshortlink $a [mc "Commit "] " $heada\n"
9241 appendshortlink $b [mc " is the same patch as\n "] \
9242 " $headb\n"
9243 }
9244 set skipa 1
9245 set skipb 1
9246 } else {
9247 $ctext insert end "\n"
9248 appendshortlink $a [mc "Commit "] " $heada\n"
9249 appendshortlink $b [mc " differs from\n "] \
9250 " $headb\n"
9251 $ctext insert end [mc "Diff of commits:\n\n"]
9252 $ctext conf -state disabled
9253 update
9254 diffcommits $a $b
9255 return
9256 }
9257 }
9258 if {$skipa} {
9259 set kids [real_children $curview,$a]
9260 if {[llength $kids] != 1} {
9261 $ctext insert end "\n"
9262 appendshortlink $a [mc "Commit "] \
9263 [mc " has %s children - stopping\n" [llength $kids]]
9264 break
9265 }
9266 set a [lindex $kids 0]
9267 }
9268 if {$skipb} {
9269 set kids [real_children $curview,$b]
9270 if {[llength $kids] != 1} {
9271 appendshortlink $b [mc "Commit "] \
9272 [mc " has %s children - stopping\n" [llength $kids]]
9273 break
9274 }
9275 set b [lindex $kids 0]
9276 }
9277 }
9278 $ctext conf -state disabled
9279}
9280
9281proc diffcommits {a b} {
9282 global diffcontext diffids blobdifffd diffinhdr currdiffsubmod
9283
9284 set tmpdir [gitknewtmpdir]
9285 set fna [file join $tmpdir "commit-[string range $a 0 7]"]
9286 set fnb [file join $tmpdir "commit-[string range $b 0 7]"]
9287 if {[catch {
9288 safe_exec_redirect [list git diff-tree -p --pretty $a] [list >$fna]
9289 safe_exec_redirect [list git diff-tree -p --pretty $b] [list >$fnb]
9290 } err]} {
9291 error_popup [mc "Error writing commit to file: %s" $err]
9292 return
9293 }
9294 if {[catch {
9295 set fd [safe_open_command "diff -U$diffcontext $fna $fnb"]
9296 } err]} {
9297 error_popup [mc "Error diffing commits: %s" $err]
9298 return
9299 }
9300 set diffids [list commits $a $b]
9301 set blobdifffd($diffids) $fd
9302 set diffinhdr 0
9303 set currdiffsubmod ""
9304 filerun $fd [list getblobdiffline $fd $diffids]
9305}
9306
9307proc diffvssel {dirn} {
9308 global rowmenuid selectedline
9309
9310 if {$selectedline eq {}} return
9311 if {$dirn} {
9312 set oldid [commitonrow $selectedline]
9313 set newid $rowmenuid
9314 } else {
9315 set oldid $rowmenuid
9316 set newid [commitonrow $selectedline]
9317 }
9318 addtohistory [list doseldiff $oldid $newid] savectextpos
9319 doseldiff $oldid $newid
9320}
9321
9322proc diffvsmark {dirn} {
9323 global rowmenuid markedid
9324
9325 if {![info exists markedid]} return
9326 if {$dirn} {
9327 set oldid $markedid
9328 set newid $rowmenuid
9329 } else {
9330 set oldid $rowmenuid
9331 set newid $markedid
9332 }
9333 addtohistory [list doseldiff $oldid $newid] savectextpos
9334 doseldiff $oldid $newid
9335}
9336
9337proc doseldiff {oldid newid} {
9338 global ctext
9339 global commitinfo
9340
9341 $ctext conf -state normal
9342 clear_ctext
9343 init_flist [mc "Top"]
9344 $ctext insert end "[mc "From"] "
9345 $ctext insert end $oldid link0
9346 setlink $oldid link0
9347 $ctext insert end "\n "
9348 $ctext insert end [lindex $commitinfo($oldid) 0]
9349 $ctext insert end "\n\n[mc "To"] "
9350 $ctext insert end $newid link1
9351 setlink $newid link1
9352 $ctext insert end "\n "
9353 $ctext insert end [lindex $commitinfo($newid) 0]
9354 $ctext insert end "\n"
9355 $ctext conf -state disabled
9356 $ctext tag remove found 1.0 end
9357 startdiff [list $oldid $newid]
9358}
9359
9360proc mkpatch {} {
9361 global rowmenuid currentid commitinfo patchtop patchnum
9362 global hashlength
9363
9364 if {![info exists currentid]} return
9365 set oldid $currentid
9366 set oldhead [lindex $commitinfo($oldid) 0]
9367 set newid $rowmenuid
9368 set newhead [lindex $commitinfo($newid) 0]
9369 set top .patch
9370 set patchtop $top
9371 catch {destroy $top}
9372 ttk_toplevel $top
9373 make_transient $top .
9374 ttk::label $top.title -text [mc "Generate patch"]
9375 grid $top.title - -pady 10
9376 ttk::label $top.from -text [mc "From:"]
9377 ttk::entry $top.fromsha1 -width $hashlength
9378 $top.fromsha1 insert 0 $oldid
9379 $top.fromsha1 conf -state readonly
9380 grid $top.from $top.fromsha1 -sticky w
9381 ttk::entry $top.fromhead -width 60
9382 $top.fromhead insert 0 $oldhead
9383 $top.fromhead conf -state readonly
9384 grid x $top.fromhead -sticky w
9385 ttk::label $top.to -text [mc "To:"]
9386 ttk::entry $top.tosha1 -width $hashlength
9387 $top.tosha1 insert 0 $newid
9388 $top.tosha1 conf -state readonly
9389 grid $top.to $top.tosha1 -sticky w
9390 ttk::entry $top.tohead -width 60
9391 $top.tohead insert 0 $newhead
9392 $top.tohead conf -state readonly
9393 grid x $top.tohead -sticky w
9394 ttk::button $top.rev -text [mc "Reverse"] -command mkpatchrev
9395 grid $top.rev x -pady 10 -padx 5
9396 ttk::label $top.flab -text [mc "Output file:"]
9397 ttk::entry $top.fname -width 60
9398 $top.fname insert 0 [file normalize "patch$patchnum.patch"]
9399 incr patchnum
9400 grid $top.flab $top.fname -sticky w
9401 ttk::frame $top.buts
9402 ttk::button $top.buts.gen -text [mc "Generate"] -command mkpatchgo
9403 ttk::button $top.buts.can -text [mc "Cancel"] -command mkpatchcan
9404 bind $top <Key-Return> mkpatchgo
9405 bind $top <Key-Escape> mkpatchcan
9406 grid $top.buts.gen $top.buts.can
9407 grid columnconfigure $top.buts 0 -weight 1 -uniform a
9408 grid columnconfigure $top.buts 1 -weight 1 -uniform a
9409 grid $top.buts - -pady 10 -sticky ew
9410 focus $top.fname
9411}
9412
9413proc mkpatchrev {} {
9414 global patchtop
9415
9416 set oldid [$patchtop.fromsha1 get]
9417 set oldhead [$patchtop.fromhead get]
9418 set newid [$patchtop.tosha1 get]
9419 set newhead [$patchtop.tohead get]
9420 foreach e [list fromsha1 fromhead tosha1 tohead] \
9421 v [list $newid $newhead $oldid $oldhead] {
9422 $patchtop.$e conf -state normal
9423 $patchtop.$e delete 0 end
9424 $patchtop.$e insert 0 $v
9425 $patchtop.$e conf -state readonly
9426 }
9427}
9428
9429proc mkpatchgo {} {
9430 global patchtop nullid nullid2
9431
9432 set oldid [$patchtop.fromsha1 get]
9433 set newid [$patchtop.tosha1 get]
9434 set fname [$patchtop.fname get]
9435 set cmd [diffcmd [list $oldid $newid] -p]
9436 if {[catch {safe_exec_redirect $cmd [list >$fname &]} err]} {
9437 error_popup "[mc "Error creating patch:"] $err" $patchtop
9438 }
9439 catch {destroy $patchtop}
9440 unset patchtop
9441}
9442
9443proc mkpatchcan {} {
9444 global patchtop
9445
9446 catch {destroy $patchtop}
9447 unset patchtop
9448}
9449
9450proc mktag {} {
9451 global rowmenuid mktagtop commitinfo
9452 global hashlength
9453
9454 set top .maketag
9455 set mktagtop $top
9456 catch {destroy $top}
9457 ttk_toplevel $top
9458 make_transient $top .
9459 ttk::label $top.title -text [mc "Create tag"]
9460 grid $top.title - -pady 10
9461 ttk::label $top.id -text [mc "ID:"]
9462 ttk::entry $top.sha1 -width $hashlength
9463 $top.sha1 insert 0 $rowmenuid
9464 $top.sha1 conf -state readonly
9465 grid $top.id $top.sha1 -sticky w
9466 ttk::entry $top.head -width 60
9467 $top.head insert 0 [lindex $commitinfo($rowmenuid) 0]
9468 $top.head conf -state readonly
9469 grid x $top.head -sticky w
9470 ttk::label $top.tlab -text [mc "Tag name:"]
9471 ttk::entry $top.tag -width 60
9472 grid $top.tlab $top.tag -sticky w
9473 ttk::label $top.op -text [mc "Tag message is optional"]
9474 grid $top.op -columnspan 2 -sticky we
9475 ttk::label $top.mlab -text [mc "Tag message:"]
9476 ttk::entry $top.msg -width 60
9477 grid $top.mlab $top.msg -sticky w
9478 ttk::frame $top.buts
9479 ttk::button $top.buts.gen -text [mc "Create"] -command mktaggo
9480 ttk::button $top.buts.can -text [mc "Cancel"] -command mktagcan
9481 bind $top <Key-Return> mktaggo
9482 bind $top <Key-Escape> mktagcan
9483 grid $top.buts.gen $top.buts.can
9484 grid columnconfigure $top.buts 0 -weight 1 -uniform a
9485 grid columnconfigure $top.buts 1 -weight 1 -uniform a
9486 grid $top.buts - -pady 10 -sticky ew
9487 focus $top.tag
9488}
9489
9490proc domktag {} {
9491 global mktagtop env tagids idtags
9492
9493 set id [$mktagtop.sha1 get]
9494 set tag [$mktagtop.tag get]
9495 set msg [$mktagtop.msg get]
9496 if {$tag == {}} {
9497 error_popup [mc "No tag name specified"] $mktagtop
9498 return 0
9499 }
9500 if {[info exists tagids($tag)]} {
9501 error_popup [mc "Tag \"%s\" already exists" $tag] $mktagtop
9502 return 0
9503 }
9504 if {[catch {
9505 if {$msg != {}} {
9506 safe_exec [list git tag -a -m $msg $tag $id]
9507 } else {
9508 safe_exec [list git tag $tag $id]
9509 }
9510 } err]} {
9511 error_popup "[mc "Error creating tag:"] $err" $mktagtop
9512 return 0
9513 }
9514
9515 set tagids($tag) $id
9516 lappend idtags($id) $tag
9517 redrawtags $id
9518 addedtag $id
9519 dispneartags 0
9520 run refill_reflist
9521 return 1
9522}
9523
9524proc redrawtags {id} {
9525 global canv linehtag idpos currentid curview cmitlisted markedid
9526 global canvxmax iddrawn circleitem mainheadid circlecolors
9527 global mainheadcirclecolor
9528
9529 if {![commitinview $id $curview]} return
9530 if {![info exists iddrawn($id)]} return
9531 set row [rowofcommit $id]
9532 if {$id eq $mainheadid} {
9533 set ofill $mainheadcirclecolor
9534 } else {
9535 set ofill [lindex $circlecolors $cmitlisted($curview,$id)]
9536 }
9537 $canv itemconf $circleitem($row) -fill $ofill
9538 $canv delete tag.$id
9539 set xt [eval drawtags $id $idpos($id)]
9540 $canv coords $linehtag($id) $xt [lindex $idpos($id) 2]
9541 set text [$canv itemcget $linehtag($id) -text]
9542 set font [$canv itemcget $linehtag($id) -font]
9543 set xr [expr {$xt + [font measure $font $text]}]
9544 if {$xr > $canvxmax} {
9545 set canvxmax $xr
9546 setcanvscroll
9547 }
9548 if {[info exists currentid] && $currentid == $id} {
9549 make_secsel $id
9550 }
9551 if {[info exists markedid] && $markedid eq $id} {
9552 make_idmark $id
9553 }
9554}
9555
9556proc mktagcan {} {
9557 global mktagtop
9558
9559 catch {destroy $mktagtop}
9560 unset mktagtop
9561}
9562
9563proc mktaggo {} {
9564 if {![domktag]} return
9565 mktagcan
9566}
9567
9568proc copyreference {} {
9569 global rowmenuid autosellen
9570 global hashlength
9571
9572 set format "%h (\"%s\", %ad)"
9573 set cmd [list git show -s --pretty=format:$format --date=short]
9574 if {$autosellen < $hashlength} {
9575 lappend cmd --abbrev=$autosellen
9576 }
9577 set reference [safe_exec [concat $cmd $rowmenuid]]
9578
9579 clipboard clear
9580 clipboard append $reference
9581}
9582
9583proc writecommit {} {
9584 global rowmenuid wrcomtop commitinfo wrcomcmd
9585 global hashlength
9586
9587 set top .writecommit
9588 set wrcomtop $top
9589 catch {destroy $top}
9590 ttk_toplevel $top
9591 make_transient $top .
9592 ttk::label $top.title -text [mc "Write commit to file"]
9593 grid $top.title - -pady 10
9594 ttk::label $top.id -text [mc "ID:"]
9595 ttk::entry $top.sha1 -width $hashlength
9596 $top.sha1 insert 0 $rowmenuid
9597 $top.sha1 conf -state readonly
9598 grid $top.id $top.sha1 -sticky w
9599 ttk::entry $top.head -width 60
9600 $top.head insert 0 [lindex $commitinfo($rowmenuid) 0]
9601 $top.head conf -state readonly
9602 grid x $top.head -sticky w
9603 ttk::label $top.clab -text [mc "Command:"]
9604 ttk::entry $top.cmd -width 60 -textvariable wrcomcmd
9605 grid $top.clab $top.cmd -sticky w -pady 10
9606 ttk::label $top.flab -text [mc "Output file:"]
9607 ttk::entry $top.fname -width 60
9608 $top.fname insert 0 [file normalize "commit-[string range $rowmenuid 0 6]"]
9609 grid $top.flab $top.fname -sticky w
9610 ttk::frame $top.buts
9611 ttk::button $top.buts.gen -text [mc "Write"] -command wrcomgo
9612 ttk::button $top.buts.can -text [mc "Cancel"] -command wrcomcan
9613 bind $top <Key-Return> wrcomgo
9614 bind $top <Key-Escape> wrcomcan
9615 grid $top.buts.gen $top.buts.can
9616 grid columnconfigure $top.buts 0 -weight 1 -uniform a
9617 grid columnconfigure $top.buts 1 -weight 1 -uniform a
9618 grid $top.buts - -pady 10 -sticky ew
9619 focus $top.fname
9620}
9621
9622proc wrcomgo {} {
9623 global wrcomtop
9624
9625 set id [$wrcomtop.sha1 get]
9626 set cmd "echo $id | [$wrcomtop.cmd get]"
9627 set fname [$wrcomtop.fname get]
9628 if {[catch {safe_exec_redirect [list sh -c $cmd] [list >$fname &]} err]} {
9629 error_popup "[mc "Error writing commit:"] $err" $wrcomtop
9630 }
9631 catch {destroy $wrcomtop}
9632 unset wrcomtop
9633}
9634
9635proc wrcomcan {} {
9636 global wrcomtop
9637
9638 catch {destroy $wrcomtop}
9639 unset wrcomtop
9640}
9641
9642proc mkbranch {} {
9643 global rowmenuid
9644
9645 set top .branchdialog
9646
9647 set val(name) ""
9648 set val(id) $rowmenuid
9649 set val(command) [list mkbrgo $top]
9650
9651 set ui(title) [mc "Create branch"]
9652 set ui(accept) [mc "Create"]
9653
9654 branchdia $top val ui
9655}
9656
9657proc mvbranch {} {
9658 global headmenuid headmenuhead
9659
9660 set top .branchdialog
9661
9662 set val(name) $headmenuhead
9663 set val(id) $headmenuid
9664 set val(command) [list mvbrgo $top $headmenuhead]
9665
9666 set ui(title) [mc "Rename branch %s" $headmenuhead]
9667 set ui(accept) [mc "Rename"]
9668
9669 branchdia $top val ui
9670}
9671
9672proc branchdia {top valvar uivar} {
9673 global commitinfo
9674 global hashlength
9675 upvar $valvar val $uivar ui
9676
9677 catch {destroy $top}
9678 ttk_toplevel $top
9679 make_transient $top .
9680 ttk::label $top.title -text $ui(title)
9681 grid $top.title - -pady 10
9682 ttk::label $top.id -text [mc "ID:"]
9683 ttk::entry $top.sha1 -width $hashlength
9684 $top.sha1 insert 0 $val(id)
9685 $top.sha1 conf -state readonly
9686 grid $top.id $top.sha1 -sticky w
9687 ttk::entry $top.head -width 60
9688 $top.head insert 0 [lindex $commitinfo($val(id)) 0]
9689 $top.head conf -state readonly
9690 grid x $top.head -sticky ew
9691 grid columnconfigure $top 1 -weight 1
9692 ttk::label $top.nlab -text [mc "Name:"]
9693 ttk::entry $top.name -width $hashlength
9694 $top.name insert 0 $val(name)
9695 grid $top.nlab $top.name -sticky w
9696 ttk::frame $top.buts
9697 ttk::button $top.buts.go -text $ui(accept) -command $val(command)
9698 ttk::button $top.buts.can -text [mc "Cancel"] -command "catch {destroy $top}"
9699 bind $top <Key-Return> $val(command)
9700 bind $top <Key-Escape> "catch {destroy $top}"
9701 grid $top.buts.go $top.buts.can
9702 grid columnconfigure $top.buts 0 -weight 1 -uniform a
9703 grid columnconfigure $top.buts 1 -weight 1 -uniform a
9704 grid $top.buts - -pady 10 -sticky ew
9705 focus $top.name
9706}
9707
9708proc mkbrgo {top} {
9709 global headids idheads
9710
9711 set name [$top.name get]
9712 set id [$top.sha1 get]
9713 set cmdargs {}
9714 set old_id {}
9715 if {$name eq {}} {
9716 error_popup [mc "Please specify a name for the new branch"] $top
9717 return
9718 }
9719 if {[info exists headids($name)]} {
9720 if {![confirm_popup [mc \
9721 "Branch '%s' already exists. Overwrite?" $name] $top]} {
9722 return
9723 }
9724 set old_id $headids($name)
9725 lappend cmdargs -f
9726 }
9727 catch {destroy $top}
9728 lappend cmdargs $name $id
9729 nowbusy newbranch
9730 update
9731 if {[catch {
9732 safe_exec [concat git branch $cmdargs]
9733 } err]} {
9734 notbusy newbranch
9735 error_popup $err
9736 } else {
9737 notbusy newbranch
9738 if {$old_id ne {}} {
9739 movehead $id $name
9740 movedhead $id $name
9741 redrawtags $old_id
9742 redrawtags $id
9743 } else {
9744 set headids($name) $id
9745 lappend idheads($id) $name
9746 addedhead $id $name
9747 redrawtags $id
9748 }
9749 dispneartags 0
9750 run refill_reflist
9751 }
9752}
9753
9754proc mvbrgo {top prevname} {
9755 global headids idheads mainhead mainheadid
9756
9757 set name [$top.name get]
9758 set id [$top.sha1 get]
9759 set cmdargs {}
9760 if {$name eq $prevname} {
9761 catch {destroy $top}
9762 return
9763 }
9764 if {$name eq {}} {
9765 error_popup [mc "Please specify a new name for the branch"] $top
9766 return
9767 }
9768 catch {destroy $top}
9769 lappend cmdargs -m $prevname $name
9770 nowbusy renamebranch
9771 update
9772 if {[catch {
9773 safe_exec [concat git branch $cmdargs]
9774 } err]} {
9775 notbusy renamebranch
9776 error_popup $err
9777 } else {
9778 notbusy renamebranch
9779 removehead $id $prevname
9780 removedhead $id $prevname
9781 set headids($name) $id
9782 lappend idheads($id) $name
9783 addedhead $id $name
9784 if {$prevname eq $mainhead} {
9785 set mainhead $name
9786 set mainheadid $id
9787 }
9788 redrawtags $id
9789 dispneartags 0
9790 run refill_reflist
9791 }
9792}
9793
9794proc exec_citool {tool_args {baseid {}}} {
9795 global commitinfo env
9796
9797 set save_env [array get env GIT_AUTHOR_*]
9798
9799 if {$baseid ne {}} {
9800 if {![info exists commitinfo($baseid)]} {
9801 getcommit $baseid
9802 }
9803 set author [lindex $commitinfo($baseid) 1]
9804 set date [lindex $commitinfo($baseid) 2]
9805 if {[regexp {^\s*(\S.*\S|\S)\s*<(.*)>\s*$} \
9806 $author author name email]
9807 && $date ne {}} {
9808 set env(GIT_AUTHOR_NAME) $name
9809 set env(GIT_AUTHOR_EMAIL) $email
9810 set env(GIT_AUTHOR_DATE) $date
9811 }
9812 }
9813
9814 safe_exec_redirect [concat git citool $tool_args] [list &]
9815
9816 array unset env GIT_AUTHOR_*
9817 array set env $save_env
9818}
9819
9820proc cherrypick {} {
9821 global rowmenuid curview
9822 global mainhead mainheadid
9823 global gitdir
9824
9825 set oldhead [exec git rev-parse HEAD]
9826 set dheads [descheads $rowmenuid]
9827 if {$dheads ne {} && [lsearch -exact $dheads $oldhead] >= 0} {
9828 set ok [confirm_popup [mc "Commit %s is already\
9829 included in branch %s -- really re-apply it?" \
9830 [string range $rowmenuid 0 7] $mainhead]]
9831 if {!$ok} return
9832 }
9833 nowbusy cherrypick [mc "Cherry-picking"]
9834 update
9835 # Unfortunately git-cherry-pick writes stuff to stderr even when
9836 # no error occurs, and exec takes that as an indication of error...
9837 if {[catch {safe_exec [list sh -c "git cherry-pick -r $rowmenuid 2>&1"]} err]} {
9838 notbusy cherrypick
9839 if {[regexp -line \
9840 {Entry '(.*)' (would be overwritten by merge|not uptodate)} \
9841 $err msg fname]} {
9842 error_popup [mc "Cherry-pick failed because of local changes\
9843 to file '%s'.\nPlease commit, reset or stash\
9844 your changes and try again." $fname]
9845 } elseif {[regexp -line \
9846 {^(CONFLICT \(.*\):|Automatic cherry-pick failed|error: could not apply)} \
9847 $err]} {
9848 if {[confirm_popup [mc "Cherry-pick failed because of merge\
9849 conflict.\nDo you wish to run git citool to\
9850 resolve it?"]]} {
9851 # Force citool to read MERGE_MSG
9852 file delete [file join $gitdir "GITGUI_MSG"]
9853 exec_citool {} $rowmenuid
9854 }
9855 } else {
9856 error_popup $err
9857 }
9858 run updatecommits
9859 return
9860 }
9861 set newhead [exec git rev-parse HEAD]
9862 if {$newhead eq $oldhead} {
9863 notbusy cherrypick
9864 error_popup [mc "No changes committed"]
9865 return
9866 }
9867 addnewchild $newhead $oldhead
9868 if {[commitinview $oldhead $curview]} {
9869 # XXX this isn't right if we have a path limit...
9870 insertrow $newhead $oldhead $curview
9871 if {$mainhead ne {}} {
9872 movehead $newhead $mainhead
9873 movedhead $newhead $mainhead
9874 }
9875 set mainheadid $newhead
9876 redrawtags $oldhead
9877 redrawtags $newhead
9878 selbyid $newhead
9879 }
9880 notbusy cherrypick
9881}
9882
9883proc revert {} {
9884 global rowmenuid curview
9885 global mainhead mainheadid
9886 global gitdir
9887
9888 set oldhead [exec git rev-parse HEAD]
9889 set dheads [descheads $rowmenuid]
9890 if { $dheads eq {} || [lsearch -exact $dheads $oldhead] == -1 } {
9891 set ok [confirm_popup [mc "Commit %s is not\
9892 included in branch %s -- really revert it?" \
9893 [string range $rowmenuid 0 7] $mainhead]]
9894 if {!$ok} return
9895 }
9896 nowbusy revert [mc "Reverting"]
9897 update
9898
9899 if [catch {safe_exec [list git revert --no-edit $rowmenuid]} err] {
9900 notbusy revert
9901 if [regexp {files would be overwritten by merge:(\n(( |\t)+[^\n]+\n)+)}\
9902 $err match files] {
9903 regsub {\n( |\t)+} $files "\n" files
9904 error_popup [mc "Revert failed because of local changes to\
9905 the following files:%s Please commit, reset or stash \
9906 your changes and try again." $files]
9907 } elseif [regexp {error: could not revert} $err] {
9908 if [confirm_popup [mc "Revert failed because of merge conflict.\n\
9909 Do you wish to run git citool to resolve it?"]] {
9910 # Force citool to read MERGE_MSG
9911 file delete [file join $gitdir "GITGUI_MSG"]
9912 exec_citool {} $rowmenuid
9913 }
9914 } else { error_popup $err }
9915 run updatecommits
9916 return
9917 }
9918
9919 set newhead [exec git rev-parse HEAD]
9920 if { $newhead eq $oldhead } {
9921 notbusy revert
9922 error_popup [mc "No changes committed"]
9923 return
9924 }
9925
9926 addnewchild $newhead $oldhead
9927
9928 if [commitinview $oldhead $curview] {
9929 # XXX this isn't right if we have a path limit...
9930 insertrow $newhead $oldhead $curview
9931 if {$mainhead ne {}} {
9932 movehead $newhead $mainhead
9933 movedhead $newhead $mainhead
9934 }
9935 set mainheadid $newhead
9936 redrawtags $oldhead
9937 redrawtags $newhead
9938 selbyid $newhead
9939 }
9940
9941 notbusy revert
9942}
9943
9944proc resethead {} {
9945 global mainhead rowmenuid confirm_ok resettype
9946
9947 set confirm_ok 0
9948 set w ".confirmreset"
9949 ttk_toplevel $w
9950 make_transient $w .
9951 wm title $w [mc "Confirm reset"]
9952 ttk::label $w.m -text \
9953 [mc "Reset branch %s to %s?" $mainhead [string range $rowmenuid 0 7]]
9954 pack $w.m -side top -fill x -padx 20 -pady 20
9955 ttk::labelframe $w.f -text [mc "Reset type:"]
9956 set resettype mixed
9957 ttk::radiobutton $w.f.soft -value soft -variable resettype \
9958 -text [mc "Soft: Leave working tree and index untouched"]
9959 grid $w.f.soft -sticky w
9960 ttk::radiobutton $w.f.mixed -value mixed -variable resettype \
9961 -text [mc "Mixed: Leave working tree untouched, reset index"]
9962 grid $w.f.mixed -sticky w
9963 ttk::radiobutton $w.f.hard -value hard -variable resettype \
9964 -text [mc "Hard: Reset working tree and index\n(discard ALL local changes)"]
9965 grid $w.f.hard -sticky w
9966 pack $w.f -side top -fill x -padx 4
9967 ttk::button $w.ok -text [mc OK] -command "set confirm_ok 1; destroy $w"
9968 pack $w.ok -side left -fill x -padx 20 -pady 20
9969 ttk::button $w.cancel -text [mc Cancel] -command "destroy $w"
9970 bind $w <Key-Escape> [list destroy $w]
9971 pack $w.cancel -side right -fill x -padx 20 -pady 20
9972 bind $w <Visibility> "grab $w; focus $w"
9973 tkwait window $w
9974 if {!$confirm_ok} return
9975 if {[catch {set fd [safe_open_command_redirect \
9976 [list git reset --$resettype $rowmenuid] [list 2>@1]]} err]} {
9977 error_popup $err
9978 } else {
9979 dohidelocalchanges
9980 filerun $fd [list readresetstat $fd]
9981 nowbusy reset [mc "Resetting"]
9982 selbyid $rowmenuid
9983 }
9984}
9985
9986proc readresetstat {fd} {
9987 global mainhead mainheadid showlocalchanges rprogcoord
9988
9989 if {[gets $fd line] >= 0} {
9990 if {[regexp {([0-9]+)% \(([0-9]+)/([0-9]+)\)} $line match p m n]} {
9991 set rprogcoord [expr {1.0 * $m / $n}]
9992 adjustprogress
9993 }
9994 return 1
9995 }
9996 set rprogcoord 0
9997 adjustprogress
9998 notbusy reset
9999 if {[catch {close $fd} err]} {
10000 error_popup $err
10001 }
10002 set oldhead $mainheadid
10003 set newhead [exec git rev-parse HEAD]
10004 if {$newhead ne $oldhead} {
10005 movehead $newhead $mainhead
10006 movedhead $newhead $mainhead
10007 set mainheadid $newhead
10008 redrawtags $oldhead
10009 redrawtags $newhead
10010 }
10011 if {$showlocalchanges} {
10012 doshowlocalchanges
10013 }
10014 return 0
10015}
10016
10017# context menu for a head
10018proc headmenu {x y id head} {
10019 global headmenuid headmenuhead headctxmenu mainhead headids
10020
10021 stopfinding
10022 set headmenuid $id
10023 set headmenuhead $head
10024 array set state {0 normal 1 normal 2 normal}
10025 if {[string match "remotes/*" $head]} {
10026 set localhead [string range $head [expr [string last / $head] + 1] end]
10027 if {[info exists headids($localhead)]} {
10028 set state(0) disabled
10029 }
10030 array set state {1 disabled 2 disabled}
10031 }
10032 if {$head eq $mainhead} {
10033 array set state {0 disabled 2 disabled}
10034 }
10035 foreach i {0 1 2} {
10036 $headctxmenu entryconfigure $i -state $state($i)
10037 }
10038 tk_popup $headctxmenu $x $y
10039}
10040
10041proc cobranch {} {
10042 global headmenuid headmenuhead headids
10043 global showlocalchanges
10044
10045 # check the tree is clean first??
10046 set newhead $headmenuhead
10047 set command [list git checkout]
10048 if {[string match "remotes/*" $newhead]} {
10049 set remote $newhead
10050 set newhead [string range $newhead [expr [string last / $newhead] + 1] end]
10051 # The following check is redundant - the menu option should
10052 # be disabled to begin with...
10053 if {[info exists headids($newhead)]} {
10054 error_popup [mc "A local branch named %s exists already" $newhead]
10055 return
10056 }
10057 lappend command -b $newhead --track $remote
10058 } else {
10059 lappend command $newhead
10060 }
10061 nowbusy checkout [mc "Checking out"]
10062 update
10063 dohidelocalchanges
10064 if {[catch {
10065 set fd [safe_open_command_redirect $command [list 2>@1]]
10066 } err]} {
10067 notbusy checkout
10068 error_popup $err
10069 if {$showlocalchanges} {
10070 dodiffindex
10071 }
10072 } else {
10073 filerun $fd [list readcheckoutstat $fd $newhead $headmenuid]
10074 }
10075}
10076
10077proc readcheckoutstat {fd newhead newheadid} {
10078 global mainhead mainheadid headids idheads showlocalchanges progresscoords
10079 global viewmainheadid curview
10080
10081 if {[gets $fd line] >= 0} {
10082 if {[regexp {([0-9]+)% \(([0-9]+)/([0-9]+)\)} $line match p m n]} {
10083 set progresscoords [list 0 [expr {1.0 * $m / $n}]]
10084 adjustprogress
10085 }
10086 return 1
10087 }
10088 set progresscoords {0 0}
10089 adjustprogress
10090 notbusy checkout
10091 if {[catch {close $fd} err]} {
10092 error_popup $err
10093 return
10094 }
10095 set oldmainid $mainheadid
10096 if {! [info exists headids($newhead)]} {
10097 set headids($newhead) $newheadid
10098 lappend idheads($newheadid) $newhead
10099 addedhead $newheadid $newhead
10100 }
10101 set mainhead $newhead
10102 set mainheadid $newheadid
10103 set viewmainheadid($curview) $newheadid
10104 redrawtags $oldmainid
10105 redrawtags $newheadid
10106 selbyid $newheadid
10107 if {$showlocalchanges} {
10108 dodiffindex
10109 }
10110}
10111
10112proc rmbranch {} {
10113 global headmenuid headmenuhead mainhead
10114 global idheads
10115
10116 set head $headmenuhead
10117 set id $headmenuid
10118 # this check shouldn't be needed any more...
10119 if {$head eq $mainhead} {
10120 error_popup [mc "Cannot delete the currently checked-out branch"]
10121 return
10122 }
10123 set dheads [descheads $id]
10124 if {[llength $dheads] == 1 && $idheads($dheads) eq $head} {
10125 # the stuff on this branch isn't on any other branch
10126 if {![confirm_popup [mc "The commits on branch %s aren't on any other\
10127 branch.\nReally delete branch %s?" $head $head]]} return
10128 }
10129 nowbusy rmbranch
10130 update
10131 if {[catch {safe_exec [list git branch -D $head]} err]} {
10132 notbusy rmbranch
10133 error_popup $err
10134 return
10135 }
10136 removehead $id $head
10137 removedhead $id $head
10138 redrawtags $id
10139 notbusy rmbranch
10140 dispneartags 0
10141 run refill_reflist
10142}
10143
10144# Display a list of tags and heads
10145proc showrefs {} {
10146 global showrefstop bgcolor fgcolor selectbgcolor
10147 global bglist fglist reflistfilter reflist maincursor
10148
10149 set top .showrefs
10150 set showrefstop $top
10151 if {[winfo exists $top]} {
10152 raise $top
10153 refill_reflist
10154 return
10155 }
10156 ttk_toplevel $top
10157 wm title $top [mc "Tags and heads: %s" [file tail [pwd]]]
10158 make_transient $top .
10159 text $top.list -background $bgcolor -foreground $fgcolor \
10160 -selectbackground $selectbgcolor -font mainfont \
10161 -xscrollcommand "$top.xsb set" -yscrollcommand "$top.ysb set" \
10162 -width 60 -height 20 -cursor $maincursor \
10163 -spacing1 1 -spacing3 1 -state disabled
10164 $top.list tag configure highlight -background $selectbgcolor
10165 if {![lsearch -exact $bglist $top.list]} {
10166 lappend bglist $top.list
10167 lappend fglist $top.list
10168 }
10169 ttk::scrollbar $top.ysb -command "$top.list yview" -orient vertical
10170 ttk::scrollbar $top.xsb -command "$top.list xview" -orient horizontal
10171 grid $top.list $top.ysb -sticky nsew
10172 grid $top.xsb x -sticky ew
10173 ttk::frame $top.f
10174 ttk::label $top.f.l -text "[mc "Filter"]: "
10175 ttk::entry $top.f.e -width 20 -textvariable reflistfilter
10176 set reflistfilter "*"
10177 trace add variable reflistfilter write reflistfilter_change
10178 pack $top.f.e -side right -fill x -expand 1
10179 pack $top.f.l -side left
10180 grid $top.f - -sticky ew -pady 2
10181 ttk::checkbutton $top.sort -text [mc "Sort refs by type"] \
10182 -variable sortrefsbytype -command {refill_reflist}
10183 grid $top.sort - -sticky w -pady 2
10184 ttk::button $top.close -command [list destroy $top] -text [mc "Close"]
10185 bind $top <Key-Escape> [list destroy $top]
10186 grid $top.close -
10187 grid columnconfigure $top 0 -weight 1
10188 grid rowconfigure $top 0 -weight 1
10189 bind $top.list <1> {break}
10190 bind $top.list <B1-Motion> {break}
10191 bind $top.list <ButtonRelease-1> {sel_reflist %W %x %y; break}
10192 set reflist {}
10193 refill_reflist
10194}
10195
10196proc sel_reflist {w x y} {
10197 global showrefstop reflist headids tagids otherrefids
10198
10199 if {![winfo exists $showrefstop]} return
10200 set l [lindex [split [$w index "@$x,$y"] "."] 0]
10201 set ref [lindex $reflist [expr {$l-1}]]
10202 set n [lindex $ref 0]
10203 switch -- [lindex $ref 1] {
10204 "H" {selbyid $headids($n)}
10205 "R" {selbyid $headids($n)}
10206 "T" {selbyid $tagids($n)}
10207 "o" {selbyid $otherrefids($n)}
10208 }
10209 $showrefstop.list tag add highlight $l.0 "$l.0 lineend"
10210}
10211
10212proc unsel_reflist {} {
10213 global showrefstop
10214
10215 if {![info exists showrefstop] || ![winfo exists $showrefstop]} return
10216 $showrefstop.list tag remove highlight 0.0 end
10217}
10218
10219proc reflistfilter_change {n1 n2 op} {
10220 global reflistfilter
10221
10222 after cancel refill_reflist
10223 after 200 refill_reflist
10224}
10225
10226proc refill_reflist {} {
10227 global reflist reflistfilter showrefstop headids tagids otherrefids sortrefsbytype
10228 global curview upstreamofref
10229
10230 if {![info exists showrefstop] || ![winfo exists $showrefstop]} return
10231 set localrefs {}
10232 set remoterefs {}
10233 set trackedremoterefs {}
10234 set tagrefs {}
10235 set otherrefs {}
10236
10237 foreach n [array names headids] {
10238 if {![string match "remotes/*" $n] && [string match $reflistfilter $n]} {
10239 if {[commitinview $headids($n) $curview]} {
10240 lappend localrefs [list $n H]
10241 if {[info exists upstreamofref($n)] && [commitinview $headids($upstreamofref($n)) $curview]} {
10242 lappend trackedremoterefs [list $upstreamofref($n) R]
10243 }
10244 } else {
10245 interestedin $headids($n) {run refill_reflist}
10246 }
10247 }
10248 }
10249 set trackedremoterefs [lsort -index 0 -unique $trackedremoterefs]
10250 set localrefs [lsort -index 0 $localrefs]
10251
10252 foreach n [array names headids] {
10253 if {[string match "remotes/*" $n] && [string match $reflistfilter $n]} {
10254 if {[commitinview $headids($n) $curview]} {
10255 if {[lsearch -exact $trackedremoterefs [list $n R]] < 0} {
10256 lappend remoterefs [list $n R]
10257 }
10258 } else {
10259 interestedin $headids($n) {run refill_reflist}
10260 }
10261 }
10262 }
10263 set remoterefs [lsort -index 0 $remoterefs]
10264
10265 foreach n [array names tagids] {
10266 if {[string match $reflistfilter $n]} {
10267 if {[commitinview $tagids($n) $curview]} {
10268 lappend tagrefs [list $n T]
10269 } else {
10270 interestedin $tagids($n) {run refill_reflist}
10271 }
10272 }
10273 }
10274 set tagrefs [lsort -index 0 $tagrefs]
10275
10276 foreach n [array names otherrefids] {
10277 if {[string match $reflistfilter $n]} {
10278 if {[commitinview $otherrefids($n) $curview]} {
10279 lappend otherrefs [list "$n" o]
10280 } else {
10281 interestedin $otherrefids($n) {run refill_reflist}
10282 }
10283 }
10284 }
10285 set otherrefs [lsort -index 0 $otherrefs]
10286
10287 set refs [concat $localrefs $trackedremoterefs $remoterefs $tagrefs $otherrefs]
10288 if {!$sortrefsbytype} {
10289 set refs [lsort -index 0 $refs]
10290 }
10291
10292 if {$refs eq $reflist} return
10293
10294 # Update the contents of $showrefstop.list according to the
10295 # differences between $reflist (old) and $refs (new)
10296 $showrefstop.list conf -state normal
10297 $showrefstop.list insert end "\n"
10298 set i 0
10299 set j 0
10300 while {$i < [llength $reflist] || $j < [llength $refs]} {
10301 if {$i < [llength $reflist]} {
10302 if {$j < [llength $refs]} {
10303 set cmp [string compare [lindex $reflist $i 0] \
10304 [lindex $refs $j 0]]
10305 if {$cmp == 0} {
10306 set cmp [string compare [lindex $reflist $i 1] \
10307 [lindex $refs $j 1]]
10308 }
10309 } else {
10310 set cmp -1
10311 }
10312 } else {
10313 set cmp 1
10314 }
10315 switch -- $cmp {
10316 -1 {
10317 $showrefstop.list delete "[expr {$j+1}].0" "[expr {$j+2}].0"
10318 incr i
10319 }
10320 0 {
10321 incr i
10322 incr j
10323 }
10324 1 {
10325 set l [expr {$j + 1}]
10326 $showrefstop.list image create $l.0 -align baseline \
10327 -image reficon-[lindex $refs $j 1] -padx 2
10328 $showrefstop.list insert $l.1 "[lindex $refs $j 0]\n"
10329 incr j
10330 }
10331 }
10332 }
10333 set reflist $refs
10334 # delete last newline
10335 $showrefstop.list delete end-2c end-1c
10336 $showrefstop.list conf -state disabled
10337}
10338
10339# Stuff for finding nearby tags
10340proc getallcommits {} {
10341 global allcommits nextarc seeds allccache allcwait cachedarcs allcupdate
10342 global idheads idtags idotherrefs allparents tagobjid
10343 global gitdir
10344
10345 if {![info exists allcommits]} {
10346 set nextarc 0
10347 set allcommits 0
10348 set seeds {}
10349 set allcwait 0
10350 set cachedarcs 0
10351 set allccache [file join $gitdir "gitk.cache"]
10352 if {![catch {
10353 set f [safe_open_file $allccache r]
10354 set allcwait 1
10355 getcache $f
10356 }]} return
10357 }
10358
10359 if {$allcwait} {
10360 return
10361 }
10362 set cmd [list git rev-list --parents]
10363 set allcupdate [expr {$seeds ne {}}]
10364 if {!$allcupdate} {
10365 set ids "--all"
10366 } else {
10367 set refs [concat [array names idheads] [array names idtags] \
10368 [array names idotherrefs]]
10369 set ids {}
10370 set tagobjs {}
10371 foreach name [array names tagobjid] {
10372 lappend tagobjs $tagobjid($name)
10373 }
10374 foreach id [lsort -unique $refs] {
10375 if {![info exists allparents($id)] &&
10376 [lsearch -exact $tagobjs $id] < 0} {
10377 lappend ids $id
10378 }
10379 }
10380 if {$ids ne {}} {
10381 foreach id $seeds {
10382 lappend ids "^$id"
10383 }
10384 lappend ids "--"
10385 }
10386 }
10387 if {$ids ne {}} {
10388 if {$ids eq "--all"} {
10389 set cmd [concat $cmd "--all"]
10390 set fd [safe_open_command $cmd]
10391 } else {
10392 set cmd [concat $cmd --stdin]
10393 set fd [safe_open_command_redirect $cmd [list "<<[join $ids "\n"]"]]
10394 }
10395 fconfigure $fd -blocking 0
10396 incr allcommits
10397 nowbusy allcommits
10398 filerun $fd [list getallclines $fd]
10399 } else {
10400 dispneartags 0
10401 }
10402}
10403
10404# Since most commits have 1 parent and 1 child, we group strings of
10405# such commits into "arcs" joining branch/merge points (BMPs), which
10406# are commits that either don't have 1 parent or don't have 1 child.
10407#
10408# arcnos(id) - incoming arcs for BMP, arc we're on for other nodes
10409# arcout(id) - outgoing arcs for BMP
10410# arcids(a) - list of IDs on arc including end but not start
10411# arcstart(a) - BMP ID at start of arc
10412# arcend(a) - BMP ID at end of arc
10413# growing(a) - arc a is still growing
10414# arctags(a) - IDs out of arcids (excluding end) that have tags
10415# archeads(a) - IDs out of arcids (excluding end) that have heads
10416# The start of an arc is at the descendent end, so "incoming" means
10417# coming from descendents, and "outgoing" means going towards ancestors.
10418
10419proc getallclines {fd} {
10420 global allparents allchildren idtags idheads nextarc
10421 global arcnos arcids arctags arcout arcend arcstart archeads growing
10422 global seeds allcommits cachedarcs allcupdate
10423
10424 set nid 0
10425 while {[incr nid] <= 1000 && [gets $fd line] >= 0} {
10426 set id [lindex $line 0]
10427 if {[info exists allparents($id)]} {
10428 # seen it already
10429 continue
10430 }
10431 set cachedarcs 0
10432 set olds [lrange $line 1 end]
10433 set allparents($id) $olds
10434 if {![info exists allchildren($id)]} {
10435 set allchildren($id) {}
10436 set arcnos($id) {}
10437 lappend seeds $id
10438 } else {
10439 set a $arcnos($id)
10440 if {[llength $olds] == 1 && [llength $a] == 1} {
10441 lappend arcids($a) $id
10442 if {[info exists idtags($id)]} {
10443 lappend arctags($a) $id
10444 }
10445 if {[info exists idheads($id)]} {
10446 lappend archeads($a) $id
10447 }
10448 if {[info exists allparents($olds)]} {
10449 # seen parent already
10450 if {![info exists arcout($olds)]} {
10451 splitarc $olds
10452 }
10453 lappend arcids($a) $olds
10454 set arcend($a) $olds
10455 unset growing($a)
10456 }
10457 lappend allchildren($olds) $id
10458 lappend arcnos($olds) $a
10459 continue
10460 }
10461 }
10462 foreach a $arcnos($id) {
10463 lappend arcids($a) $id
10464 set arcend($a) $id
10465 unset growing($a)
10466 }
10467
10468 set ao {}
10469 foreach p $olds {
10470 lappend allchildren($p) $id
10471 set a [incr nextarc]
10472 set arcstart($a) $id
10473 set archeads($a) {}
10474 set arctags($a) {}
10475 set archeads($a) {}
10476 set arcids($a) {}
10477 lappend ao $a
10478 set growing($a) 1
10479 if {[info exists allparents($p)]} {
10480 # seen it already, may need to make a new branch
10481 if {![info exists arcout($p)]} {
10482 splitarc $p
10483 }
10484 lappend arcids($a) $p
10485 set arcend($a) $p
10486 unset growing($a)
10487 }
10488 lappend arcnos($p) $a
10489 }
10490 set arcout($id) $ao
10491 }
10492 if {$nid > 0} {
10493 global cached_dheads cached_dtags cached_atags
10494 unset -nocomplain cached_dheads
10495 unset -nocomplain cached_dtags
10496 unset -nocomplain cached_atags
10497 }
10498 if {![eof $fd]} {
10499 return [expr {$nid >= 1000? 2: 1}]
10500 }
10501 set cacheok 1
10502 if {[catch {
10503 fconfigure $fd -blocking 1
10504 close $fd
10505 } err]} {
10506 # got an error reading the list of commits
10507 # if we were updating, try rereading the whole thing again
10508 if {$allcupdate} {
10509 incr allcommits -1
10510 dropcache $err
10511 return
10512 }
10513 error_popup "[mc "Error reading commit topology information;\
10514 branch and preceding/following tag information\
10515 will be incomplete."]\n($err)"
10516 set cacheok 0
10517 }
10518 if {[incr allcommits -1] == 0} {
10519 notbusy allcommits
10520 if {$cacheok} {
10521 run savecache
10522 }
10523 }
10524 dispneartags 0
10525 return 0
10526}
10527
10528proc recalcarc {a} {
10529 global arctags archeads arcids idtags idheads
10530
10531 set at {}
10532 set ah {}
10533 foreach id [lrange $arcids($a) 0 end-1] {
10534 if {[info exists idtags($id)]} {
10535 lappend at $id
10536 }
10537 if {[info exists idheads($id)]} {
10538 lappend ah $id
10539 }
10540 }
10541 set arctags($a) $at
10542 set archeads($a) $ah
10543}
10544
10545proc splitarc {p} {
10546 global arcnos arcids nextarc arctags archeads idtags idheads
10547 global arcstart arcend arcout allparents growing
10548
10549 set a $arcnos($p)
10550 if {[llength $a] != 1} {
10551 puts "oops splitarc called but [llength $a] arcs already"
10552 return
10553 }
10554 set a [lindex $a 0]
10555 set i [lsearch -exact $arcids($a) $p]
10556 if {$i < 0} {
10557 puts "oops splitarc $p not in arc $a"
10558 return
10559 }
10560 set na [incr nextarc]
10561 if {[info exists arcend($a)]} {
10562 set arcend($na) $arcend($a)
10563 } else {
10564 set l [lindex $allparents([lindex $arcids($a) end]) 0]
10565 set j [lsearch -exact $arcnos($l) $a]
10566 set arcnos($l) [lreplace $arcnos($l) $j $j $na]
10567 }
10568 set tail [lrange $arcids($a) [expr {$i+1}] end]
10569 set arcids($a) [lrange $arcids($a) 0 $i]
10570 set arcend($a) $p
10571 set arcstart($na) $p
10572 set arcout($p) $na
10573 set arcids($na) $tail
10574 if {[info exists growing($a)]} {
10575 set growing($na) 1
10576 unset growing($a)
10577 }
10578
10579 foreach id $tail {
10580 if {[llength $arcnos($id)] == 1} {
10581 set arcnos($id) $na
10582 } else {
10583 set j [lsearch -exact $arcnos($id) $a]
10584 set arcnos($id) [lreplace $arcnos($id) $j $j $na]
10585 }
10586 }
10587
10588 # reconstruct tags and heads lists
10589 if {$arctags($a) ne {} || $archeads($a) ne {}} {
10590 recalcarc $a
10591 recalcarc $na
10592 } else {
10593 set arctags($na) {}
10594 set archeads($na) {}
10595 }
10596}
10597
10598# Update things for a new commit added that is a child of one
10599# existing commit. Used when cherry-picking.
10600proc addnewchild {id p} {
10601 global allparents allchildren idtags nextarc
10602 global arcnos arcids arctags arcout arcend arcstart archeads growing
10603 global seeds allcommits
10604
10605 if {![info exists allcommits] || ![info exists arcnos($p)]} return
10606 set allparents($id) [list $p]
10607 set allchildren($id) {}
10608 set arcnos($id) {}
10609 lappend seeds $id
10610 lappend allchildren($p) $id
10611 set a [incr nextarc]
10612 set arcstart($a) $id
10613 set archeads($a) {}
10614 set arctags($a) {}
10615 set arcids($a) [list $p]
10616 set arcend($a) $p
10617 if {![info exists arcout($p)]} {
10618 splitarc $p
10619 }
10620 lappend arcnos($p) $a
10621 set arcout($id) [list $a]
10622}
10623
10624# This implements a cache for the topology information.
10625# The cache saves, for each arc, the start and end of the arc,
10626# the ids on the arc, and the outgoing arcs from the end.
10627proc readcache {f} {
10628 global arcnos arcids arcout arcstart arcend arctags archeads nextarc
10629 global idtags idheads allparents cachedarcs possible_seeds seeds growing
10630 global allcwait
10631
10632 set a $nextarc
10633 set lim $cachedarcs
10634 if {$lim - $a > 500} {
10635 set lim [expr {$a + 500}]
10636 }
10637 if {[catch {
10638 if {$a == $lim} {
10639 # finish reading the cache and setting up arctags, etc.
10640 set line [gets $f]
10641 if {$line ne "1"} {error "bad final version"}
10642 close $f
10643 foreach id [array names idtags] {
10644 if {[info exists arcnos($id)] && [llength $arcnos($id)] == 1 &&
10645 [llength $allparents($id)] == 1} {
10646 set a [lindex $arcnos($id) 0]
10647 if {$arctags($a) eq {}} {
10648 recalcarc $a
10649 }
10650 }
10651 }
10652 foreach id [array names idheads] {
10653 if {[info exists arcnos($id)] && [llength $arcnos($id)] == 1 &&
10654 [llength $allparents($id)] == 1} {
10655 set a [lindex $arcnos($id) 0]
10656 if {$archeads($a) eq {}} {
10657 recalcarc $a
10658 }
10659 }
10660 }
10661 foreach id [lsort -unique $possible_seeds] {
10662 if {$arcnos($id) eq {}} {
10663 lappend seeds $id
10664 }
10665 }
10666 set allcwait 0
10667 } else {
10668 while {[incr a] <= $lim} {
10669 set line [gets $f]
10670 if {[llength $line] != 3} {error "bad line"}
10671 set s [lindex $line 0]
10672 set arcstart($a) $s
10673 lappend arcout($s) $a
10674 if {![info exists arcnos($s)]} {
10675 lappend possible_seeds $s
10676 set arcnos($s) {}
10677 }
10678 set e [lindex $line 1]
10679 if {$e eq {}} {
10680 set growing($a) 1
10681 } else {
10682 set arcend($a) $e
10683 if {![info exists arcout($e)]} {
10684 set arcout($e) {}
10685 }
10686 }
10687 set arcids($a) [lindex $line 2]
10688 foreach id $arcids($a) {
10689 lappend allparents($s) $id
10690 set s $id
10691 lappend arcnos($id) $a
10692 }
10693 if {![info exists allparents($s)]} {
10694 set allparents($s) {}
10695 }
10696 set arctags($a) {}
10697 set archeads($a) {}
10698 }
10699 set nextarc [expr {$a - 1}]
10700 }
10701 } err]} {
10702 dropcache $err
10703 return 0
10704 }
10705 if {!$allcwait} {
10706 getallcommits
10707 }
10708 return $allcwait
10709}
10710
10711proc getcache {f} {
10712 global nextarc cachedarcs possible_seeds
10713
10714 if {[catch {
10715 set line [gets $f]
10716 if {[llength $line] != 2 || [lindex $line 0] ne "1"} {error "bad version"}
10717 # make sure it's an integer
10718 set cachedarcs [expr {int([lindex $line 1])}]
10719 if {$cachedarcs < 0} {error "bad number of arcs"}
10720 set nextarc 0
10721 set possible_seeds {}
10722 run readcache $f
10723 } err]} {
10724 dropcache $err
10725 }
10726 return 0
10727}
10728
10729proc dropcache {err} {
10730 global allcwait nextarc cachedarcs seeds
10731
10732 #puts "dropping cache ($err)"
10733 foreach v {arcnos arcout arcids arcstart arcend growing \
10734 arctags archeads allparents allchildren} {
10735 global $v
10736 unset -nocomplain $v
10737 }
10738 set allcwait 0
10739 set nextarc 0
10740 set cachedarcs 0
10741 set seeds {}
10742 getallcommits
10743}
10744
10745proc writecache {f} {
10746 global cachearc cachedarcs allccache
10747 global arcstart arcend arcnos arcids arcout
10748
10749 set a $cachearc
10750 set lim $cachedarcs
10751 if {$lim - $a > 1000} {
10752 set lim [expr {$a + 1000}]
10753 }
10754 if {[catch {
10755 while {[incr a] <= $lim} {
10756 if {[info exists arcend($a)]} {
10757 puts $f [list $arcstart($a) $arcend($a) $arcids($a)]
10758 } else {
10759 puts $f [list $arcstart($a) {} $arcids($a)]
10760 }
10761 }
10762 } err]} {
10763 catch {close $f}
10764 catch {file delete $allccache}
10765 #puts "writing cache failed ($err)"
10766 return 0
10767 }
10768 set cachearc [expr {$a - 1}]
10769 if {$a > $cachedarcs} {
10770 puts $f "1"
10771 close $f
10772 return 0
10773 }
10774 return 1
10775}
10776
10777proc savecache {} {
10778 global nextarc cachedarcs cachearc allccache
10779
10780 if {$nextarc == $cachedarcs} return
10781 set cachearc 0
10782 set cachedarcs $nextarc
10783 catch {
10784 set f [safe_open_file $allccache w]
10785 puts $f [list 1 $cachedarcs]
10786 run writecache $f
10787 }
10788}
10789
10790# Returns 1 if a is an ancestor of b, -1 if b is an ancestor of a,
10791# or 0 if neither is true.
10792proc anc_or_desc {a b} {
10793 global arcout arcstart arcend arcnos cached_isanc
10794
10795 if {$arcnos($a) eq $arcnos($b)} {
10796 # Both are on the same arc(s); either both are the same BMP,
10797 # or if one is not a BMP, the other is also not a BMP or is
10798 # the BMP at end of the arc (and it only has 1 incoming arc).
10799 # Or both can be BMPs with no incoming arcs.
10800 if {$a eq $b || $arcnos($a) eq {}} {
10801 return 0
10802 }
10803 # assert {[llength $arcnos($a)] == 1}
10804 set arc [lindex $arcnos($a) 0]
10805 set i [lsearch -exact $arcids($arc) $a]
10806 set j [lsearch -exact $arcids($arc) $b]
10807 if {$i < 0 || $i > $j} {
10808 return 1
10809 } else {
10810 return -1
10811 }
10812 }
10813
10814 if {![info exists arcout($a)]} {
10815 set arc [lindex $arcnos($a) 0]
10816 if {[info exists arcend($arc)]} {
10817 set aend $arcend($arc)
10818 } else {
10819 set aend {}
10820 }
10821 set a $arcstart($arc)
10822 } else {
10823 set aend $a
10824 }
10825 if {![info exists arcout($b)]} {
10826 set arc [lindex $arcnos($b) 0]
10827 if {[info exists arcend($arc)]} {
10828 set bend $arcend($arc)
10829 } else {
10830 set bend {}
10831 }
10832 set b $arcstart($arc)
10833 } else {
10834 set bend $b
10835 }
10836 if {$a eq $bend} {
10837 return 1
10838 }
10839 if {$b eq $aend} {
10840 return -1
10841 }
10842 if {[info exists cached_isanc($a,$bend)]} {
10843 if {$cached_isanc($a,$bend)} {
10844 return 1
10845 }
10846 }
10847 if {[info exists cached_isanc($b,$aend)]} {
10848 if {$cached_isanc($b,$aend)} {
10849 return -1
10850 }
10851 if {[info exists cached_isanc($a,$bend)]} {
10852 return 0
10853 }
10854 }
10855
10856 set todo [list $a $b]
10857 set anc($a) a
10858 set anc($b) b
10859 for {set i 0} {$i < [llength $todo]} {incr i} {
10860 set x [lindex $todo $i]
10861 if {$anc($x) eq {}} {
10862 continue
10863 }
10864 foreach arc $arcnos($x) {
10865 set xd $arcstart($arc)
10866 if {$xd eq $bend} {
10867 set cached_isanc($a,$bend) 1
10868 set cached_isanc($b,$aend) 0
10869 return 1
10870 } elseif {$xd eq $aend} {
10871 set cached_isanc($b,$aend) 1
10872 set cached_isanc($a,$bend) 0
10873 return -1
10874 }
10875 if {![info exists anc($xd)]} {
10876 set anc($xd) $anc($x)
10877 lappend todo $xd
10878 } elseif {$anc($xd) ne $anc($x)} {
10879 set anc($xd) {}
10880 }
10881 }
10882 }
10883 set cached_isanc($a,$bend) 0
10884 set cached_isanc($b,$aend) 0
10885 return 0
10886}
10887
10888# This identifies whether $desc has an ancestor that is
10889# a growing tip of the graph and which is not an ancestor of $anc
10890# and returns 0 if so and 1 if not.
10891# If we subsequently discover a tag on such a growing tip, and that
10892# turns out to be a descendent of $anc (which it could, since we
10893# don't necessarily see children before parents), then $desc
10894# isn't a good choice to display as a descendent tag of
10895# $anc (since it is the descendent of another tag which is
10896# a descendent of $anc). Similarly, $anc isn't a good choice to
10897# display as a ancestor tag of $desc.
10898#
10899proc is_certain {desc anc} {
10900 global arcnos arcout arcstart arcend growing problems
10901
10902 set certain {}
10903 if {[llength $arcnos($anc)] == 1} {
10904 # tags on the same arc are certain
10905 if {$arcnos($desc) eq $arcnos($anc)} {
10906 return 1
10907 }
10908 if {![info exists arcout($anc)]} {
10909 # if $anc is partway along an arc, use the start of the arc instead
10910 set a [lindex $arcnos($anc) 0]
10911 set anc $arcstart($a)
10912 }
10913 }
10914 if {[llength $arcnos($desc)] > 1 || [info exists arcout($desc)]} {
10915 set x $desc
10916 } else {
10917 set a [lindex $arcnos($desc) 0]
10918 set x $arcend($a)
10919 }
10920 if {$x == $anc} {
10921 return 1
10922 }
10923 set anclist [list $x]
10924 set dl($x) 1
10925 set nnh 1
10926 set ngrowanc 0
10927 for {set i 0} {$i < [llength $anclist] && ($nnh > 0 || $ngrowanc > 0)} {incr i} {
10928 set x [lindex $anclist $i]
10929 if {$dl($x)} {
10930 incr nnh -1
10931 }
10932 set done($x) 1
10933 foreach a $arcout($x) {
10934 if {[info exists growing($a)]} {
10935 if {![info exists growanc($x)] && $dl($x)} {
10936 set growanc($x) 1
10937 incr ngrowanc
10938 }
10939 } else {
10940 set y $arcend($a)
10941 if {[info exists dl($y)]} {
10942 if {$dl($y)} {
10943 if {!$dl($x)} {
10944 set dl($y) 0
10945 if {![info exists done($y)]} {
10946 incr nnh -1
10947 }
10948 if {[info exists growanc($x)]} {
10949 incr ngrowanc -1
10950 }
10951 set xl [list $y]
10952 for {set k 0} {$k < [llength $xl]} {incr k} {
10953 set z [lindex $xl $k]
10954 foreach c $arcout($z) {
10955 if {[info exists arcend($c)]} {
10956 set v $arcend($c)
10957 if {[info exists dl($v)] && $dl($v)} {
10958 set dl($v) 0
10959 if {![info exists done($v)]} {
10960 incr nnh -1
10961 }
10962 if {[info exists growanc($v)]} {
10963 incr ngrowanc -1
10964 }
10965 lappend xl $v
10966 }
10967 }
10968 }
10969 }
10970 }
10971 }
10972 } elseif {$y eq $anc || !$dl($x)} {
10973 set dl($y) 0
10974 lappend anclist $y
10975 } else {
10976 set dl($y) 1
10977 lappend anclist $y
10978 incr nnh
10979 }
10980 }
10981 }
10982 }
10983 foreach x [array names growanc] {
10984 if {$dl($x)} {
10985 return 0
10986 }
10987 return 0
10988 }
10989 return 1
10990}
10991
10992proc validate_arctags {a} {
10993 global arctags idtags
10994
10995 set i -1
10996 set na $arctags($a)
10997 foreach id $arctags($a) {
10998 incr i
10999 if {![info exists idtags($id)]} {
11000 set na [lreplace $na $i $i]
11001 incr i -1
11002 }
11003 }
11004 set arctags($a) $na
11005}
11006
11007proc validate_archeads {a} {
11008 global archeads idheads
11009
11010 set i -1
11011 set na $archeads($a)
11012 foreach id $archeads($a) {
11013 incr i
11014 if {![info exists idheads($id)]} {
11015 set na [lreplace $na $i $i]
11016 incr i -1
11017 }
11018 }
11019 set archeads($a) $na
11020}
11021
11022# Return the list of IDs that have tags that are descendents of id,
11023# ignoring IDs that are descendents of IDs already reported.
11024proc desctags {id} {
11025 global arcnos arcstart arcids arctags idtags allparents
11026 global growing cached_dtags
11027
11028 if {![info exists allparents($id)]} {
11029 return {}
11030 }
11031 set t1 [clock clicks -milliseconds]
11032 set argid $id
11033 if {[llength $arcnos($id)] == 1 && [llength $allparents($id)] == 1} {
11034 # part-way along an arc; check that arc first
11035 set a [lindex $arcnos($id) 0]
11036 if {$arctags($a) ne {}} {
11037 validate_arctags $a
11038 set i [lsearch -exact $arcids($a) $id]
11039 set tid {}
11040 foreach t $arctags($a) {
11041 set j [lsearch -exact $arcids($a) $t]
11042 if {$j >= $i} break
11043 set tid $t
11044 }
11045 if {$tid ne {}} {
11046 return $tid
11047 }
11048 }
11049 set id $arcstart($a)
11050 if {[info exists idtags($id)]} {
11051 return $id
11052 }
11053 }
11054 if {[info exists cached_dtags($id)]} {
11055 return $cached_dtags($id)
11056 }
11057
11058 set origid $id
11059 set todo [list $id]
11060 set queued($id) 1
11061 set nc 1
11062 for {set i 0} {$i < [llength $todo] && $nc > 0} {incr i} {
11063 set id [lindex $todo $i]
11064 set done($id) 1
11065 set ta [info exists hastaggedancestor($id)]
11066 if {!$ta} {
11067 incr nc -1
11068 }
11069 # ignore tags on starting node
11070 if {!$ta && $i > 0} {
11071 if {[info exists idtags($id)]} {
11072 set tagloc($id) $id
11073 set ta 1
11074 } elseif {[info exists cached_dtags($id)]} {
11075 set tagloc($id) $cached_dtags($id)
11076 set ta 1
11077 }
11078 }
11079 foreach a $arcnos($id) {
11080 set d $arcstart($a)
11081 if {!$ta && $arctags($a) ne {}} {
11082 validate_arctags $a
11083 if {$arctags($a) ne {}} {
11084 lappend tagloc($id) [lindex $arctags($a) end]
11085 }
11086 }
11087 if {$ta || $arctags($a) ne {}} {
11088 set tomark [list $d]
11089 for {set j 0} {$j < [llength $tomark]} {incr j} {
11090 set dd [lindex $tomark $j]
11091 if {![info exists hastaggedancestor($dd)]} {
11092 if {[info exists done($dd)]} {
11093 foreach b $arcnos($dd) {
11094 lappend tomark $arcstart($b)
11095 }
11096 if {[info exists tagloc($dd)]} {
11097 unset tagloc($dd)
11098 }
11099 } elseif {[info exists queued($dd)]} {
11100 incr nc -1
11101 }
11102 set hastaggedancestor($dd) 1
11103 }
11104 }
11105 }
11106 if {![info exists queued($d)]} {
11107 lappend todo $d
11108 set queued($d) 1
11109 if {![info exists hastaggedancestor($d)]} {
11110 incr nc
11111 }
11112 }
11113 }
11114 }
11115 set tags {}
11116 foreach id [array names tagloc] {
11117 if {![info exists hastaggedancestor($id)]} {
11118 foreach t $tagloc($id) {
11119 if {[lsearch -exact $tags $t] < 0} {
11120 lappend tags $t
11121 }
11122 }
11123 }
11124 }
11125 set t2 [clock clicks -milliseconds]
11126 set loopix $i
11127
11128 # remove tags that are descendents of other tags
11129 for {set i 0} {$i < [llength $tags]} {incr i} {
11130 set a [lindex $tags $i]
11131 for {set j 0} {$j < $i} {incr j} {
11132 set b [lindex $tags $j]
11133 set r [anc_or_desc $a $b]
11134 if {$r == 1} {
11135 set tags [lreplace $tags $j $j]
11136 incr j -1
11137 incr i -1
11138 } elseif {$r == -1} {
11139 set tags [lreplace $tags $i $i]
11140 incr i -1
11141 break
11142 }
11143 }
11144 }
11145
11146 if {[array names growing] ne {}} {
11147 # graph isn't finished, need to check if any tag could get
11148 # eclipsed by another tag coming later. Simply ignore any
11149 # tags that could later get eclipsed.
11150 set ctags {}
11151 foreach t $tags {
11152 if {[is_certain $t $origid]} {
11153 lappend ctags $t
11154 }
11155 }
11156 if {$tags eq $ctags} {
11157 set cached_dtags($origid) $tags
11158 } else {
11159 set tags $ctags
11160 }
11161 } else {
11162 set cached_dtags($origid) $tags
11163 }
11164 set t3 [clock clicks -milliseconds]
11165 if {0 && $t3 - $t1 >= 100} {
11166 puts "iterating descendents ($loopix/[llength $todo] nodes) took\
11167 [expr {$t2-$t1}]+[expr {$t3-$t2}]ms, $nc candidates left"
11168 }
11169 return $tags
11170}
11171
11172proc anctags {id} {
11173 global arcnos arcids arcout arcend arctags idtags allparents
11174 global growing cached_atags
11175
11176 if {![info exists allparents($id)]} {
11177 return {}
11178 }
11179 set t1 [clock clicks -milliseconds]
11180 set argid $id
11181 if {[llength $arcnos($id)] == 1 && [llength $allparents($id)] == 1} {
11182 # part-way along an arc; check that arc first
11183 set a [lindex $arcnos($id) 0]
11184 if {$arctags($a) ne {}} {
11185 validate_arctags $a
11186 set i [lsearch -exact $arcids($a) $id]
11187 foreach t $arctags($a) {
11188 set j [lsearch -exact $arcids($a) $t]
11189 if {$j > $i} {
11190 return $t
11191 }
11192 }
11193 }
11194 if {![info exists arcend($a)]} {
11195 return {}
11196 }
11197 set id $arcend($a)
11198 if {[info exists idtags($id)]} {
11199 return $id
11200 }
11201 }
11202 if {[info exists cached_atags($id)]} {
11203 return $cached_atags($id)
11204 }
11205
11206 set origid $id
11207 set todo [list $id]
11208 set queued($id) 1
11209 set taglist {}
11210 set nc 1
11211 for {set i 0} {$i < [llength $todo] && $nc > 0} {incr i} {
11212 set id [lindex $todo $i]
11213 set done($id) 1
11214 set td [info exists hastaggeddescendent($id)]
11215 if {!$td} {
11216 incr nc -1
11217 }
11218 # ignore tags on starting node
11219 if {!$td && $i > 0} {
11220 if {[info exists idtags($id)]} {
11221 set tagloc($id) $id
11222 set td 1
11223 } elseif {[info exists cached_atags($id)]} {
11224 set tagloc($id) $cached_atags($id)
11225 set td 1
11226 }
11227 }
11228 foreach a $arcout($id) {
11229 if {!$td && $arctags($a) ne {}} {
11230 validate_arctags $a
11231 if {$arctags($a) ne {}} {
11232 lappend tagloc($id) [lindex $arctags($a) 0]
11233 }
11234 }
11235 if {![info exists arcend($a)]} continue
11236 set d $arcend($a)
11237 if {$td || $arctags($a) ne {}} {
11238 set tomark [list $d]
11239 for {set j 0} {$j < [llength $tomark]} {incr j} {
11240 set dd [lindex $tomark $j]
11241 if {![info exists hastaggeddescendent($dd)]} {
11242 if {[info exists done($dd)]} {
11243 foreach b $arcout($dd) {
11244 if {[info exists arcend($b)]} {
11245 lappend tomark $arcend($b)
11246 }
11247 }
11248 if {[info exists tagloc($dd)]} {
11249 unset tagloc($dd)
11250 }
11251 } elseif {[info exists queued($dd)]} {
11252 incr nc -1
11253 }
11254 set hastaggeddescendent($dd) 1
11255 }
11256 }
11257 }
11258 if {![info exists queued($d)]} {
11259 lappend todo $d
11260 set queued($d) 1
11261 if {![info exists hastaggeddescendent($d)]} {
11262 incr nc
11263 }
11264 }
11265 }
11266 }
11267 set t2 [clock clicks -milliseconds]
11268 set loopix $i
11269 set tags {}
11270 foreach id [array names tagloc] {
11271 if {![info exists hastaggeddescendent($id)]} {
11272 foreach t $tagloc($id) {
11273 if {[lsearch -exact $tags $t] < 0} {
11274 lappend tags $t
11275 }
11276 }
11277 }
11278 }
11279
11280 # remove tags that are ancestors of other tags
11281 for {set i 0} {$i < [llength $tags]} {incr i} {
11282 set a [lindex $tags $i]
11283 for {set j 0} {$j < $i} {incr j} {
11284 set b [lindex $tags $j]
11285 set r [anc_or_desc $a $b]
11286 if {$r == -1} {
11287 set tags [lreplace $tags $j $j]
11288 incr j -1
11289 incr i -1
11290 } elseif {$r == 1} {
11291 set tags [lreplace $tags $i $i]
11292 incr i -1
11293 break
11294 }
11295 }
11296 }
11297
11298 if {[array names growing] ne {}} {
11299 # graph isn't finished, need to check if any tag could get
11300 # eclipsed by another tag coming later. Simply ignore any
11301 # tags that could later get eclipsed.
11302 set ctags {}
11303 foreach t $tags {
11304 if {[is_certain $origid $t]} {
11305 lappend ctags $t
11306 }
11307 }
11308 if {$tags eq $ctags} {
11309 set cached_atags($origid) $tags
11310 } else {
11311 set tags $ctags
11312 }
11313 } else {
11314 set cached_atags($origid) $tags
11315 }
11316 set t3 [clock clicks -milliseconds]
11317 if {0 && $t3 - $t1 >= 100} {
11318 puts "iterating ancestors ($loopix/[llength $todo] nodes) took\
11319 [expr {$t2-$t1}]+[expr {$t3-$t2}]ms, $nc candidates left"
11320 }
11321 return $tags
11322}
11323
11324# Return the list of IDs that have heads that are descendents of id,
11325# including id itself if it has a head.
11326proc descheads {id} {
11327 global arcnos arcstart arcids archeads idheads cached_dheads
11328 global allparents arcout
11329
11330 if {![info exists allparents($id)]} {
11331 return {}
11332 }
11333 set aret {}
11334 if {![info exists arcout($id)]} {
11335 # part-way along an arc; check it first
11336 set a [lindex $arcnos($id) 0]
11337 if {$archeads($a) ne {}} {
11338 validate_archeads $a
11339 set i [lsearch -exact $arcids($a) $id]
11340 foreach t $archeads($a) {
11341 set j [lsearch -exact $arcids($a) $t]
11342 if {$j > $i} break
11343 lappend aret $t
11344 }
11345 }
11346 set id $arcstart($a)
11347 }
11348 set origid $id
11349 set todo [list $id]
11350 set seen($id) 1
11351 set ret {}
11352 for {set i 0} {$i < [llength $todo]} {incr i} {
11353 set id [lindex $todo $i]
11354 if {[info exists cached_dheads($id)]} {
11355 set ret [concat $ret $cached_dheads($id)]
11356 } else {
11357 if {[info exists idheads($id)]} {
11358 lappend ret $id
11359 }
11360 foreach a $arcnos($id) {
11361 if {$archeads($a) ne {}} {
11362 validate_archeads $a
11363 if {$archeads($a) ne {}} {
11364 set ret [concat $ret $archeads($a)]
11365 }
11366 }
11367 set d $arcstart($a)
11368 if {![info exists seen($d)]} {
11369 lappend todo $d
11370 set seen($d) 1
11371 }
11372 }
11373 }
11374 }
11375 set ret [lsort -unique $ret]
11376 set cached_dheads($origid) $ret
11377 return [concat $ret $aret]
11378}
11379
11380proc addedtag {id} {
11381 global arcnos arcout cached_dtags cached_atags
11382
11383 if {![info exists arcnos($id)]} return
11384 if {![info exists arcout($id)]} {
11385 recalcarc [lindex $arcnos($id) 0]
11386 }
11387 unset -nocomplain cached_dtags
11388 unset -nocomplain cached_atags
11389}
11390
11391proc addedhead {hid head} {
11392 global arcnos arcout cached_dheads
11393
11394 if {![info exists arcnos($hid)]} return
11395 if {![info exists arcout($hid)]} {
11396 recalcarc [lindex $arcnos($hid) 0]
11397 }
11398 unset -nocomplain cached_dheads
11399}
11400
11401proc removedhead {hid head} {
11402 global cached_dheads
11403
11404 unset -nocomplain cached_dheads
11405}
11406
11407proc movedhead {hid head} {
11408 global arcnos arcout cached_dheads
11409
11410 if {![info exists arcnos($hid)]} return
11411 if {![info exists arcout($hid)]} {
11412 recalcarc [lindex $arcnos($hid) 0]
11413 }
11414 unset -nocomplain cached_dheads
11415}
11416
11417proc changedrefs {} {
11418 global cached_dheads cached_dtags cached_atags cached_tagcontent
11419 global arctags archeads arcnos arcout idheads idtags
11420
11421 foreach id [concat [array names idheads] [array names idtags]] {
11422 if {[info exists arcnos($id)] && ![info exists arcout($id)]} {
11423 set a [lindex $arcnos($id) 0]
11424 if {![info exists donearc($a)]} {
11425 recalcarc $a
11426 set donearc($a) 1
11427 }
11428 }
11429 }
11430 unset -nocomplain cached_tagcontent
11431 unset -nocomplain cached_dtags
11432 unset -nocomplain cached_atags
11433 unset -nocomplain cached_dheads
11434}
11435
11436proc rereadrefs {} {
11437 global idtags idheads idotherrefs mainheadid
11438
11439 set refids [concat [array names idtags] \
11440 [array names idheads] [array names idotherrefs]]
11441 foreach id $refids {
11442 if {![info exists ref($id)]} {
11443 set ref($id) [listrefs $id]
11444 }
11445 }
11446 set oldmainhead $mainheadid
11447 readrefs
11448 changedrefs
11449 set refids [lsort -unique [concat $refids [array names idtags] \
11450 [array names idheads] [array names idotherrefs]]]
11451 foreach id $refids {
11452 set v [listrefs $id]
11453 if {![info exists ref($id)] || $ref($id) != $v} {
11454 redrawtags $id
11455 }
11456 }
11457 if {$oldmainhead ne $mainheadid} {
11458 redrawtags $oldmainhead
11459 redrawtags $mainheadid
11460 }
11461 run refill_reflist
11462}
11463
11464proc listrefs {id} {
11465 global idtags idheads idotherrefs
11466
11467 set x {}
11468 if {[info exists idtags($id)]} {
11469 set x $idtags($id)
11470 }
11471 set y {}
11472 if {[info exists idheads($id)]} {
11473 set y $idheads($id)
11474 }
11475 set z {}
11476 if {[info exists idotherrefs($id)]} {
11477 set z $idotherrefs($id)
11478 }
11479 return [list $x $y $z]
11480}
11481
11482proc add_tag_ctext {tag} {
11483 global ctext cached_tagcontent tagids
11484
11485 if {![info exists cached_tagcontent($tag)]} {
11486 catch {
11487 set cached_tagcontent($tag) [safe_exec [list git cat-file -p $tag]]
11488 }
11489 }
11490 $ctext insert end "[mc "Tag"]: $tag\n" bold
11491 if {[info exists cached_tagcontent($tag)]} {
11492 set text $cached_tagcontent($tag)
11493 } else {
11494 set text "[mc "Id"]: $tagids($tag)"
11495 }
11496 appendwithlinks $text {}
11497}
11498
11499proc showtag {tag isnew} {
11500 global ctext cached_tagcontent tagids linknum tagobjid
11501
11502 if {$isnew} {
11503 addtohistory [list showtag $tag 0] savectextpos
11504 }
11505 $ctext conf -state normal
11506 clear_ctext
11507 settabs 0
11508 set linknum 0
11509 add_tag_ctext $tag
11510 maybe_scroll_ctext 1
11511 $ctext conf -state disabled
11512 init_flist {}
11513}
11514
11515proc showtags {id isnew} {
11516 global idtags ctext linknum
11517
11518 if {$isnew} {
11519 addtohistory [list showtags $id 0] savectextpos
11520 }
11521 $ctext conf -state normal
11522 clear_ctext
11523 settabs 0
11524 set linknum 0
11525 set sep {}
11526 foreach tag $idtags($id) {
11527 $ctext insert end $sep
11528 add_tag_ctext $tag
11529 set sep "\n\n"
11530 }
11531 maybe_scroll_ctext 1
11532 $ctext conf -state disabled
11533 init_flist {}
11534}
11535
11536proc doquit {} {
11537 global stopped
11538 global gitktmpdir
11539
11540 set stopped 100
11541 savestuff .
11542 destroy .
11543
11544 if {[info exists gitktmpdir]} {
11545 catch {file delete -force $gitktmpdir}
11546 }
11547}
11548
11549proc mkfontdisp {font top which} {
11550 global fontattr fontpref $font
11551
11552 set fontpref($font) [set $font]
11553 ttk::button $top.${font}but -text $which \
11554 -command [list choosefont $font $which]
11555 ttk::label $top.$font -relief flat -font $font \
11556 -text $fontattr($font,family) -justify left
11557 grid x $top.${font}but $top.$font -sticky w
11558}
11559
11560proc centertext {w} {
11561 $w coords text [expr {[winfo width $w] / 2}] [expr {[winfo height $w] / 2}]
11562}
11563
11564proc fontok {} {
11565 global fontparam fontpref prefstop
11566
11567 set f $fontparam(font)
11568 set fontpref($f) [list $fontparam(family) $fontparam(size)]
11569 if {$fontparam(weight) eq "bold"} {
11570 lappend fontpref($f) "bold"
11571 }
11572 if {$fontparam(slant) eq "italic"} {
11573 lappend fontpref($f) "italic"
11574 }
11575 set w $prefstop.notebook.fonts.$f
11576 $w conf -text $fontparam(family) -font $fontpref($f)
11577
11578 fontcan
11579}
11580
11581proc fontcan {} {
11582 global fonttop fontparam
11583
11584 if {[info exists fonttop]} {
11585 catch {destroy $fonttop}
11586 catch {font delete sample}
11587 unset fonttop
11588 unset fontparam
11589 }
11590}
11591
11592proc choosefont {font which} {
11593 tk fontchooser configure -title $which -font $font \
11594 -command [list on_choosefont $font $which]
11595 tk fontchooser show
11596}
11597proc on_choosefont {font which newfont} {
11598 global fontparam
11599 array set f [font actual $newfont]
11600 set fontparam(which) $which
11601 set fontparam(font) $font
11602 set fontparam(family) $f(-family)
11603 set fontparam(size) $f(-size)
11604 set fontparam(weight) $f(-weight)
11605 set fontparam(slant) $f(-slant)
11606 fontok
11607}
11608
11609proc selfontfam {} {
11610 global fonttop fontparam
11611
11612 set i [$fonttop.f.fam curselection]
11613 if {$i ne {}} {
11614 set fontparam(family) [$fonttop.f.fam get $i]
11615 }
11616}
11617
11618proc chg_fontparam {v sub op} {
11619 global fontparam
11620
11621 font config sample -$sub $fontparam($sub)
11622}
11623
11624# Create a property sheet tab page
11625proc create_prefs_page {w} {
11626 ttk::frame $w
11627}
11628
11629proc prefspage_general {notebook} {
11630 global {*}$::config_variables
11631 global hashlength
11632
11633 set page [create_prefs_page $notebook.general]
11634
11635 ttk::label $page.ldisp -text [mc "Commit list display options"] -font mainfontbold
11636 grid $page.ldisp - -sticky w -pady 10
11637 ttk::label $page.spacer -text " "
11638 ttk::label $page.maxwidthl -text [mc "Maximum graph width (lines)"]
11639 spinbox $page.maxwidth -from 0 -to 100 -width 4 -textvariable maxwidth
11640 grid $page.spacer $page.maxwidthl $page.maxwidth -sticky w
11641 #xgettext:no-tcl-format
11642 ttk::label $page.maxpctl -text [mc "Maximum graph width (% of pane)"]
11643 spinbox $page.maxpct -from 1 -to 100 -width 4 -textvariable maxgraphpct
11644 grid x $page.maxpctl $page.maxpct -sticky w
11645 ttk::checkbutton $page.showlocal -text [mc "Show local changes"] \
11646 -variable showlocalchanges
11647 grid x $page.showlocal -sticky w
11648 ttk::checkbutton $page.hideremotes -text [mc "Hide remote refs"] \
11649 -variable hideremotes
11650 grid x $page.hideremotes -sticky w
11651
11652 ttk::checkbutton $page.autocopy -text [mc "Copy commit ID to clipboard"] \
11653 -variable autocopy
11654 grid x $page.autocopy -sticky w
11655 if {[haveselectionclipboard]} {
11656 ttk::checkbutton $page.autoselect -text [mc "Copy commit ID to X11 selection"] \
11657 -variable autoselect
11658 grid x $page.autoselect -sticky w
11659 }
11660
11661 spinbox $page.autosellen -from 1 -to $hashlength -width 4 -textvariable autosellen
11662 ttk::label $page.autosellenl -text [mc "Length of commit ID to copy"]
11663 grid x $page.autosellenl $page.autosellen -sticky w
11664 ttk::label $page.kscroll1 -text [mc "Wheel scrolling multiplier"]
11665 spinbox $page.kscroll -from 1 -to 20 -width 4 -textvariable kscroll
11666 grid x $page.kscroll1 $page.kscroll -sticky w
11667
11668 ttk::label $page.ddisp -text [mc "Diff display options"] -font mainfontbold
11669 grid $page.ddisp - -sticky w -pady 10
11670 ttk::label $page.tabstopl -text [mc "Tab spacing"]
11671 spinbox $page.tabstop -from 1 -to 20 -width 4 -textvariable tabstop
11672 grid x $page.tabstopl $page.tabstop -sticky w
11673
11674 ttk::label $page.wrapcommentl -text [mc "Wrap comment text"]
11675 makedroplist $page.wrapcomment wrapcomment none char word
11676 grid x $page.wrapcommentl $page.wrapcomment -sticky w
11677
11678 ttk::label $page.wrapdefaultl -text [mc "Wrap other text"]
11679 makedroplist $page.wrapdefault wrapdefault none char word
11680 grid x $page.wrapdefaultl $page.wrapdefault -sticky w
11681
11682 ttk::checkbutton $page.ntag -text [mc "Display nearby tags/heads"] \
11683 -variable showneartags
11684 grid x $page.ntag -sticky w
11685 ttk::label $page.maxrefsl -text [mc "Maximum # tags/heads to show"]
11686 spinbox $page.maxrefs -from 1 -to 1000 -width 4 -textvariable maxrefs
11687 grid x $page.maxrefsl $page.maxrefs -sticky w
11688 ttk::checkbutton $page.ldiff -text [mc "Limit diffs to listed paths"] \
11689 -variable limitdiffs
11690 grid x $page.ldiff -sticky w
11691 ttk::checkbutton $page.lattr -text [mc "Support per-file encodings"] \
11692 -variable perfile_attrs
11693 grid x $page.lattr -sticky w
11694
11695 ttk::entry $page.extdifft -textvariable extdifftool
11696 ttk::frame $page.extdifff
11697 ttk::label $page.extdifff.l -text [mc "External diff tool" ]
11698 ttk::button $page.extdifff.b -text [mc "Choose..."] -command choose_extdiff
11699 pack $page.extdifff.l $page.extdifff.b -side left
11700 pack configure $page.extdifff.l -padx 10
11701 grid x $page.extdifff $page.extdifft -sticky ew
11702
11703 ttk::entry $page.webbrowser -textvariable web_browser
11704 ttk::frame $page.webbrowserf
11705 ttk::label $page.webbrowserf.l -text [mc "Web browser" ]
11706 pack $page.webbrowserf.l -side left
11707 pack configure $page.webbrowserf.l -padx 10
11708 grid x $page.webbrowserf $page.webbrowser -sticky ew
11709
11710 return $page
11711}
11712
11713proc prefspage_colors {notebook} {
11714 global uicolor bgcolor fgcolor ctext diffcolors selectbgcolor markbgcolor
11715 global diffbgcolors
11716
11717 set page [create_prefs_page $notebook.colors]
11718
11719 ttk::label $page.cdisp -text [mc "Colors: press to choose"] -font mainfontbold
11720 grid $page.cdisp - -sticky w -pady 10
11721 label $page.ui -padx 40 -relief sunk -background $uicolor
11722 ttk::button $page.uibut -text [mc "Interface"] \
11723 -command [list choosecolor uicolor {} $page [mc "interface"]]
11724 grid x $page.uibut $page.ui -sticky w
11725 label $page.bg -padx 40 -relief sunk -background $bgcolor
11726 ttk::button $page.bgbut -text [mc "Background"] \
11727 -command [list choosecolor bgcolor {} $page [mc "background"]]
11728 grid x $page.bgbut $page.bg -sticky w
11729 label $page.fg -padx 40 -relief sunk -background $fgcolor
11730 ttk::button $page.fgbut -text [mc "Foreground"] \
11731 -command [list choosecolor fgcolor {} $page [mc "foreground"]]
11732 grid x $page.fgbut $page.fg -sticky w
11733 label $page.diffold -padx 40 -relief sunk -background [lindex $diffcolors 0]
11734 ttk::button $page.diffoldbut -text [mc "Diff: old lines"] \
11735 -command [list choosecolor diffcolors 0 $page [mc "diff old lines"]]
11736 grid x $page.diffoldbut $page.diffold -sticky w
11737 label $page.diffoldbg -padx 40 -relief sunk -background [lindex $diffbgcolors 0]
11738 ttk::button $page.diffoldbgbut -text [mc "Diff: old lines bg"] \
11739 -command [list choosecolor diffbgcolors 0 $page [mc "diff old lines bg"]]
11740 grid x $page.diffoldbgbut $page.diffoldbg -sticky w
11741 label $page.diffnew -padx 40 -relief sunk -background [lindex $diffcolors 1]
11742 ttk::button $page.diffnewbut -text [mc "Diff: new lines"] \
11743 -command [list choosecolor diffcolors 1 $page [mc "diff new lines"]]
11744 grid x $page.diffnewbut $page.diffnew -sticky w
11745 label $page.diffnewbg -padx 40 -relief sunk -background [lindex $diffbgcolors 1]
11746 ttk::button $page.diffnewbgbut -text [mc "Diff: new lines bg"] \
11747 -command [list choosecolor diffbgcolors 1 $page [mc "diff new lines bg"]]
11748 grid x $page.diffnewbgbut $page.diffnewbg -sticky w
11749 label $page.hunksep -padx 40 -relief sunk -background [lindex $diffcolors 2]
11750 ttk::button $page.hunksepbut -text [mc "Diff: hunk header"] \
11751 -command [list choosecolor diffcolors 2 $page [mc "diff hunk header"]]
11752 grid x $page.hunksepbut $page.hunksep -sticky w
11753 label $page.markbgsep -padx 40 -relief sunk -background $markbgcolor
11754 ttk::button $page.markbgbut -text [mc "Marked line bg"] \
11755 -command [list choosecolor markbgcolor {} $page [mc "marked line background"]]
11756 grid x $page.markbgbut $page.markbgsep -sticky w
11757 label $page.selbgsep -padx 40 -relief sunk -background $selectbgcolor
11758 ttk::button $page.selbgbut -text [mc "Select bg"] \
11759 -command [list choosecolor selectbgcolor {} $page [mc "background"]]
11760 grid x $page.selbgbut $page.selbgsep -sticky w
11761 return $page
11762}
11763
11764proc prefspage_set_colorswatches {page} {
11765 global uicolor bgcolor fgcolor ctext diffcolors selectbgcolor markbgcolor
11766 global diffbgcolors
11767
11768 $page.ui configure -background $uicolor
11769 $page.bg configure -background $bgcolor
11770 $page.fg configure -background $fgcolor
11771 $page.diffold configure -background [lindex $diffcolors 0]
11772 $page.diffoldbg configure -background [lindex $diffbgcolors 0]
11773 $page.diffnew configure -background [lindex $diffcolors 1]
11774 $page.diffnewbg configure -background [lindex $diffbgcolors 1]
11775 $page.hunksep configure -background [lindex $diffcolors 2]
11776 $page.markbgsep configure -background $markbgcolor
11777 $page.selbgsep configure -background $selectbgcolor
11778}
11779
11780proc prefspage_fonts {notebook} {
11781 set page [create_prefs_page $notebook.fonts]
11782 ttk::label $page.cfont -text [mc "Fonts: press to choose"] -font mainfontbold
11783 grid $page.cfont - -sticky w -pady 10
11784 mkfontdisp mainfont $page [mc "Main font"]
11785 mkfontdisp textfont $page [mc "Diff display font"]
11786 mkfontdisp uifont $page [mc "User interface font"]
11787 return $page
11788}
11789
11790proc doprefs {} {
11791 global oldprefs prefstop
11792 global {*}$::config_variables
11793
11794 set top .gitkprefs
11795 set prefstop $top
11796 if {[winfo exists $top]} {
11797 raise $top
11798 return
11799 }
11800 foreach v $::config_variables {
11801 set oldprefs($v) [set $v]
11802 }
11803 ttk_toplevel $top
11804 wm title $top [mc "Gitk preferences"]
11805 make_transient $top .
11806
11807 set notebook [ttk::notebook $top.notebook]
11808
11809 lappend pages [prefspage_general $notebook] [mc "General"]
11810 lappend pages [prefspage_colors $notebook] [mc "Colors"]
11811 lappend pages [prefspage_fonts $notebook] [mc "Fonts"]
11812 set col 0
11813 foreach {page title} $pages {
11814 $notebook add $page -text $title
11815 }
11816
11817 grid columnconfigure $notebook 0 -weight 1
11818 grid rowconfigure $notebook 1 -weight 1
11819 raise [lindex $pages 0]
11820
11821 grid $notebook -sticky news -padx 2 -pady 2
11822 grid rowconfigure $top 0 -weight 1
11823 grid columnconfigure $top 0 -weight 1
11824
11825 ttk::frame $top.buts
11826 ttk::button $top.buts.ok -text [mc "OK"] -command prefsok -default active
11827 ttk::button $top.buts.can -text [mc "Cancel"] -command prefscan -default normal
11828 bind $top <Key-Return> prefsok
11829 bind $top <Key-Escape> prefscan
11830 grid $top.buts.ok $top.buts.can
11831 grid columnconfigure $top.buts 0 -weight 1 -uniform a
11832 grid columnconfigure $top.buts 1 -weight 1 -uniform a
11833 grid $top.buts - - -pady 10 -sticky ew
11834 grid columnconfigure $top 2 -weight 1
11835 bind $top <Visibility> [list focus $top.buts.ok]
11836}
11837
11838proc choose_extdiff {} {
11839 global extdifftool
11840
11841 set prog [tk_getOpenFile -title [mc "External diff tool"] -multiple false]
11842 if {$prog ne {}} {
11843 set extdifftool $prog
11844 }
11845}
11846
11847proc choosecolor {v vi prefspage x} {
11848 global $v
11849
11850 set c [tk_chooseColor -initialcolor [lindex [set $v] $vi] \
11851 -title [mc "Gitk: choose color for %s" $x]]
11852 if {$c eq {}} return
11853 lset $v $vi $c
11854 set_gui_colors
11855 prefspage_set_colorswatches $prefspage
11856}
11857
11858proc setselbg {c} {
11859 global bglist cflist
11860 foreach w $bglist {
11861 if {[winfo exists $w]} {
11862 $w configure -selectbackground $c
11863 }
11864 }
11865 $cflist tag configure highlight \
11866 -background [$cflist cget -selectbackground]
11867 allcanvs itemconf secsel -fill $c
11868}
11869
11870# This sets the background color and the color scheme for the whole UI.
11871# For some reason, tk_setPalette chooses a nasty dark red for selectColor
11872# if we don't specify one ourselves, which makes the checkbuttons and
11873# radiobuttons look bad. This chooses white for selectColor if the
11874# background color is light, or black if it is dark.
11875proc setui {c} {
11876 if {[tk windowingsystem] eq "win32"} { return }
11877 set bg [winfo rgb . $c]
11878 set selc black
11879 if {[lindex $bg 0] + 1.5 * [lindex $bg 1] + 0.5 * [lindex $bg 2] > 100000} {
11880 set selc white
11881 }
11882 tk_setPalette background $c selectColor $selc
11883}
11884
11885proc setbg {c} {
11886 global bglist
11887
11888 foreach w $bglist {
11889 if {[winfo exists $w]} {
11890 $w conf -background $c
11891 }
11892 }
11893}
11894
11895proc setfg {c} {
11896 global fglist canv
11897
11898 foreach w $fglist {
11899 if {[winfo exists $w]} {
11900 $w conf -foreground $c
11901 }
11902 }
11903 allcanvs itemconf text -fill $c
11904 $canv itemconf circle -outline $c
11905 $canv itemconf markid -outline $c
11906}
11907
11908proc set_gui_colors {} {
11909 global uicolor bgcolor fgcolor ctext diffcolors selectbgcolor markbgcolor
11910 global diffbgcolors
11911
11912 setui $uicolor
11913 setbg $bgcolor
11914 setfg $fgcolor
11915 $ctext tag conf d0 -foreground [lindex $diffcolors 0]
11916 $ctext tag conf d0 -background [lindex $diffbgcolors 0]
11917 $ctext tag conf dresult -foreground [lindex $diffcolors 1]
11918 $ctext tag conf dresult -background [lindex $diffbgcolors 1]
11919 $ctext tag conf hunksep -foreground [lindex $diffcolors 2]
11920 $ctext tag conf omark -background $markbgcolor
11921 setselbg $selectbgcolor
11922}
11923
11924proc prefscan {} {
11925 global oldprefs prefstop
11926 global {*}$::config_variables
11927
11928 foreach v $::config_variables {
11929 set $v $oldprefs($v)
11930 }
11931 catch {destroy $prefstop}
11932 unset prefstop
11933 fontcan
11934 set_gui_colors
11935}
11936
11937proc prefsok {} {
11938 global oldprefs prefstop fontpref treediffs
11939 global {*}$::config_variables
11940 global ctext
11941
11942 catch {destroy $prefstop}
11943 unset prefstop
11944 fontcan
11945 set fontchanged 0
11946 if {$mainfont ne $fontpref(mainfont)} {
11947 set mainfont $fontpref(mainfont)
11948 parsefont mainfont $mainfont
11949 eval font configure mainfont [fontflags mainfont]
11950 eval font configure mainfontbold [fontflags mainfont 1]
11951 setcoords
11952 set fontchanged 1
11953 }
11954 if {$textfont ne $fontpref(textfont)} {
11955 set textfont $fontpref(textfont)
11956 parsefont textfont $textfont
11957 eval font configure textfont [fontflags textfont]
11958 eval font configure textfontbold [fontflags textfont 1]
11959 }
11960 if {$uifont ne $fontpref(uifont)} {
11961 set uifont $fontpref(uifont)
11962 parsefont uifont $uifont
11963 eval font configure uifont [fontflags uifont]
11964 }
11965 settabs
11966 if {$showlocalchanges != $oldprefs(showlocalchanges)} {
11967 if {$showlocalchanges} {
11968 doshowlocalchanges
11969 } else {
11970 dohidelocalchanges
11971 }
11972 }
11973 if {$limitdiffs != $oldprefs(limitdiffs) ||
11974 ($perfile_attrs && !$oldprefs(perfile_attrs))} {
11975 # treediffs elements are limited by path;
11976 # won't have encodings cached if perfile_attrs was just turned on
11977 unset -nocomplain treediffs
11978 }
11979 if {$fontchanged || $maxwidth != $oldprefs(maxwidth)
11980 || $maxgraphpct != $oldprefs(maxgraphpct)} {
11981 redisplay
11982 } elseif {$showneartags != $oldprefs(showneartags) ||
11983 $limitdiffs != $oldprefs(limitdiffs)} {
11984 reselectline
11985 }
11986 if {$hideremotes != $oldprefs(hideremotes)} {
11987 rereadrefs
11988 }
11989 if {$wrapcomment != $oldprefs(wrapcomment)} {
11990 $ctext tag conf comment -wrap $wrapcomment
11991 }
11992 if {$wrapdefault != $oldprefs(wrapdefault)} {
11993 $ctext configure -wrap $wrapdefault
11994 }
11995}
11996
11997proc formatdate {d} {
11998 global datetimeformat
11999 if {$d ne {}} {
12000 # If $datetimeformat includes a timezone, display in the
12001 # timezone of the argument. Otherwise, display in local time.
12002 if {[string match {*%[zZ]*} $datetimeformat]} {
12003 if {[catch {set d [clock format [lindex $d 0] -timezone [lindex $d 1] -format $datetimeformat]}]} {
12004 # Tcl < 8.5 does not support -timezone. Emulate it by
12005 # setting TZ (e.g. TZ=<-0430>+04:30).
12006 global env
12007 if {[info exists env(TZ)]} {
12008 set savedTZ $env(TZ)
12009 }
12010 set zone [lindex $d 1]
12011 set sign [string map {+ - - +} [string index $zone 0]]
12012 set env(TZ) <$zone>$sign[string range $zone 1 2]:[string range $zone 3 4]
12013 set d [clock format [lindex $d 0] -format $datetimeformat]
12014 if {[info exists savedTZ]} {
12015 set env(TZ) $savedTZ
12016 } else {
12017 unset env(TZ)
12018 }
12019 }
12020 } else {
12021 set d [clock format [lindex $d 0] -format $datetimeformat]
12022 }
12023 }
12024 return $d
12025}
12026
12027# This list of encoding names and aliases is distilled from
12028# https://www.iana.org/assignments/character-sets.
12029# Not all of them are supported by Tcl.
12030set encoding_aliases {
12031 { ANSI_X3.4-1968 iso-ir-6 ANSI_X3.4-1986 ISO_646.irv:1991 ASCII
12032 ISO646-US US-ASCII us IBM367 cp367 csASCII }
12033 { ISO-10646-UTF-1 csISO10646UTF1 }
12034 { ISO_646.basic:1983 ref csISO646basic1983 }
12035 { INVARIANT csINVARIANT }
12036 { ISO_646.irv:1983 iso-ir-2 irv csISO2IntlRefVersion }
12037 { BS_4730 iso-ir-4 ISO646-GB gb uk csISO4UnitedKingdom }
12038 { NATS-SEFI iso-ir-8-1 csNATSSEFI }
12039 { NATS-SEFI-ADD iso-ir-8-2 csNATSSEFIADD }
12040 { NATS-DANO iso-ir-9-1 csNATSDANO }
12041 { NATS-DANO-ADD iso-ir-9-2 csNATSDANOADD }
12042 { SEN_850200_B iso-ir-10 FI ISO646-FI ISO646-SE se csISO10Swedish }
12043 { SEN_850200_C iso-ir-11 ISO646-SE2 se2 csISO11SwedishForNames }
12044 { KS_C_5601-1987 iso-ir-149 KS_C_5601-1989 KSC_5601 korean csKSC56011987 }
12045 { ISO-2022-KR csISO2022KR }
12046 { EUC-KR csEUCKR }
12047 { ISO-2022-JP csISO2022JP }
12048 { ISO-2022-JP-2 csISO2022JP2 }
12049 { JIS_C6220-1969-jp JIS_C6220-1969 iso-ir-13 katakana x0201-7
12050 csISO13JISC6220jp }
12051 { JIS_C6220-1969-ro iso-ir-14 jp ISO646-JP csISO14JISC6220ro }
12052 { IT iso-ir-15 ISO646-IT csISO15Italian }
12053 { PT iso-ir-16 ISO646-PT csISO16Portuguese }
12054 { ES iso-ir-17 ISO646-ES csISO17Spanish }
12055 { greek7-old iso-ir-18 csISO18Greek7Old }
12056 { latin-greek iso-ir-19 csISO19LatinGreek }
12057 { DIN_66003 iso-ir-21 de ISO646-DE csISO21German }
12058 { NF_Z_62-010_(1973) iso-ir-25 ISO646-FR1 csISO25French }
12059 { Latin-greek-1 iso-ir-27 csISO27LatinGreek1 }
12060 { ISO_5427 iso-ir-37 csISO5427Cyrillic }
12061 { JIS_C6226-1978 iso-ir-42 csISO42JISC62261978 }
12062 { BS_viewdata iso-ir-47 csISO47BSViewdata }
12063 { INIS iso-ir-49 csISO49INIS }
12064 { INIS-8 iso-ir-50 csISO50INIS8 }
12065 { INIS-cyrillic iso-ir-51 csISO51INISCyrillic }
12066 { ISO_5427:1981 iso-ir-54 ISO5427Cyrillic1981 }
12067 { ISO_5428:1980 iso-ir-55 csISO5428Greek }
12068 { GB_1988-80 iso-ir-57 cn ISO646-CN csISO57GB1988 }
12069 { GB_2312-80 iso-ir-58 chinese csISO58GB231280 }
12070 { NS_4551-1 iso-ir-60 ISO646-NO no csISO60DanishNorwegian
12071 csISO60Norwegian1 }
12072 { NS_4551-2 ISO646-NO2 iso-ir-61 no2 csISO61Norwegian2 }
12073 { NF_Z_62-010 iso-ir-69 ISO646-FR fr csISO69French }
12074 { videotex-suppl iso-ir-70 csISO70VideotexSupp1 }
12075 { PT2 iso-ir-84 ISO646-PT2 csISO84Portuguese2 }
12076 { ES2 iso-ir-85 ISO646-ES2 csISO85Spanish2 }
12077 { MSZ_7795.3 iso-ir-86 ISO646-HU hu csISO86Hungarian }
12078 { JIS_C6226-1983 iso-ir-87 x0208 JIS_X0208-1983 csISO87JISX0208 }
12079 { greek7 iso-ir-88 csISO88Greek7 }
12080 { ASMO_449 ISO_9036 arabic7 iso-ir-89 csISO89ASMO449 }
12081 { iso-ir-90 csISO90 }
12082 { JIS_C6229-1984-a iso-ir-91 jp-ocr-a csISO91JISC62291984a }
12083 { JIS_C6229-1984-b iso-ir-92 ISO646-JP-OCR-B jp-ocr-b
12084 csISO92JISC62991984b }
12085 { JIS_C6229-1984-b-add iso-ir-93 jp-ocr-b-add csISO93JIS62291984badd }
12086 { JIS_C6229-1984-hand iso-ir-94 jp-ocr-hand csISO94JIS62291984hand }
12087 { JIS_C6229-1984-hand-add iso-ir-95 jp-ocr-hand-add
12088 csISO95JIS62291984handadd }
12089 { JIS_C6229-1984-kana iso-ir-96 csISO96JISC62291984kana }
12090 { ISO_2033-1983 iso-ir-98 e13b csISO2033 }
12091 { ANSI_X3.110-1983 iso-ir-99 CSA_T500-1983 NAPLPS csISO99NAPLPS }
12092 { ISO_8859-1:1987 iso-ir-100 ISO_8859-1 ISO-8859-1 latin1 l1 IBM819
12093 CP819 csISOLatin1 }
12094 { ISO_8859-2:1987 iso-ir-101 ISO_8859-2 ISO-8859-2 latin2 l2 csISOLatin2 }
12095 { T.61-7bit iso-ir-102 csISO102T617bit }
12096 { T.61-8bit T.61 iso-ir-103 csISO103T618bit }
12097 { ISO_8859-3:1988 iso-ir-109 ISO_8859-3 ISO-8859-3 latin3 l3 csISOLatin3 }
12098 { ISO_8859-4:1988 iso-ir-110 ISO_8859-4 ISO-8859-4 latin4 l4 csISOLatin4 }
12099 { ECMA-cyrillic iso-ir-111 KOI8-E csISO111ECMACyrillic }
12100 { CSA_Z243.4-1985-1 iso-ir-121 ISO646-CA csa7-1 ca csISO121Canadian1 }
12101 { CSA_Z243.4-1985-2 iso-ir-122 ISO646-CA2 csa7-2 csISO122Canadian2 }
12102 { CSA_Z243.4-1985-gr iso-ir-123 csISO123CSAZ24341985gr }
12103 { ISO_8859-6:1987 iso-ir-127 ISO_8859-6 ISO-8859-6 ECMA-114 ASMO-708
12104 arabic csISOLatinArabic }
12105 { ISO_8859-6-E csISO88596E ISO-8859-6-E }
12106 { ISO_8859-6-I csISO88596I ISO-8859-6-I }
12107 { ISO_8859-7:1987 iso-ir-126 ISO_8859-7 ISO-8859-7 ELOT_928 ECMA-118
12108 greek greek8 csISOLatinGreek }
12109 { T.101-G2 iso-ir-128 csISO128T101G2 }
12110 { ISO_8859-8:1988 iso-ir-138 ISO_8859-8 ISO-8859-8 hebrew
12111 csISOLatinHebrew }
12112 { ISO_8859-8-E csISO88598E ISO-8859-8-E }
12113 { ISO_8859-8-I csISO88598I ISO-8859-8-I }
12114 { CSN_369103 iso-ir-139 csISO139CSN369103 }
12115 { JUS_I.B1.002 iso-ir-141 ISO646-YU js yu csISO141JUSIB1002 }
12116 { ISO_6937-2-add iso-ir-142 csISOTextComm }
12117 { IEC_P27-1 iso-ir-143 csISO143IECP271 }
12118 { ISO_8859-5:1988 iso-ir-144 ISO_8859-5 ISO-8859-5 cyrillic
12119 csISOLatinCyrillic }
12120 { JUS_I.B1.003-serb iso-ir-146 serbian csISO146Serbian }
12121 { JUS_I.B1.003-mac macedonian iso-ir-147 csISO147Macedonian }
12122 { ISO_8859-9:1989 iso-ir-148 ISO_8859-9 ISO-8859-9 latin5 l5 csISOLatin5 }
12123 { greek-ccitt iso-ir-150 csISO150 csISO150GreekCCITT }
12124 { NC_NC00-10:81 cuba iso-ir-151 ISO646-CU csISO151Cuba }
12125 { ISO_6937-2-25 iso-ir-152 csISO6937Add }
12126 { GOST_19768-74 ST_SEV_358-88 iso-ir-153 csISO153GOST1976874 }
12127 { ISO_8859-supp iso-ir-154 latin1-2-5 csISO8859Supp }
12128 { ISO_10367-box iso-ir-155 csISO10367Box }
12129 { ISO-8859-10 iso-ir-157 l6 ISO_8859-10:1992 csISOLatin6 latin6 }
12130 { latin-lap lap iso-ir-158 csISO158Lap }
12131 { JIS_X0212-1990 x0212 iso-ir-159 csISO159JISX02121990 }
12132 { DS_2089 DS2089 ISO646-DK dk csISO646Danish }
12133 { us-dk csUSDK }
12134 { dk-us csDKUS }
12135 { JIS_X0201 X0201 csHalfWidthKatakana }
12136 { KSC5636 ISO646-KR csKSC5636 }
12137 { ISO-10646-UCS-2 csUnicode }
12138 { ISO-10646-UCS-4 csUCS4 }
12139 { DEC-MCS dec csDECMCS }
12140 { hp-roman8 roman8 r8 csHPRoman8 }
12141 { macintosh mac csMacintosh }
12142 { IBM037 cp037 ebcdic-cp-us ebcdic-cp-ca ebcdic-cp-wt ebcdic-cp-nl
12143 csIBM037 }
12144 { IBM038 EBCDIC-INT cp038 csIBM038 }
12145 { IBM273 CP273 csIBM273 }
12146 { IBM274 EBCDIC-BE CP274 csIBM274 }
12147 { IBM275 EBCDIC-BR cp275 csIBM275 }
12148 { IBM277 EBCDIC-CP-DK EBCDIC-CP-NO csIBM277 }
12149 { IBM278 CP278 ebcdic-cp-fi ebcdic-cp-se csIBM278 }
12150 { IBM280 CP280 ebcdic-cp-it csIBM280 }
12151 { IBM281 EBCDIC-JP-E cp281 csIBM281 }
12152 { IBM284 CP284 ebcdic-cp-es csIBM284 }
12153 { IBM285 CP285 ebcdic-cp-gb csIBM285 }
12154 { IBM290 cp290 EBCDIC-JP-kana csIBM290 }
12155 { IBM297 cp297 ebcdic-cp-fr csIBM297 }
12156 { IBM420 cp420 ebcdic-cp-ar1 csIBM420 }
12157 { IBM423 cp423 ebcdic-cp-gr csIBM423 }
12158 { IBM424 cp424 ebcdic-cp-he csIBM424 }
12159 { IBM437 cp437 437 csPC8CodePage437 }
12160 { IBM500 CP500 ebcdic-cp-be ebcdic-cp-ch csIBM500 }
12161 { IBM775 cp775 csPC775Baltic }
12162 { IBM850 cp850 850 csPC850Multilingual }
12163 { IBM851 cp851 851 csIBM851 }
12164 { IBM852 cp852 852 csPCp852 }
12165 { IBM855 cp855 855 csIBM855 }
12166 { IBM857 cp857 857 csIBM857 }
12167 { IBM860 cp860 860 csIBM860 }
12168 { IBM861 cp861 861 cp-is csIBM861 }
12169 { IBM862 cp862 862 csPC862LatinHebrew }
12170 { IBM863 cp863 863 csIBM863 }
12171 { IBM864 cp864 csIBM864 }
12172 { IBM865 cp865 865 csIBM865 }
12173 { IBM866 cp866 866 csIBM866 }
12174 { IBM868 CP868 cp-ar csIBM868 }
12175 { IBM869 cp869 869 cp-gr csIBM869 }
12176 { IBM870 CP870 ebcdic-cp-roece ebcdic-cp-yu csIBM870 }
12177 { IBM871 CP871 ebcdic-cp-is csIBM871 }
12178 { IBM880 cp880 EBCDIC-Cyrillic csIBM880 }
12179 { IBM891 cp891 csIBM891 }
12180 { IBM903 cp903 csIBM903 }
12181 { IBM904 cp904 904 csIBBM904 }
12182 { IBM905 CP905 ebcdic-cp-tr csIBM905 }
12183 { IBM918 CP918 ebcdic-cp-ar2 csIBM918 }
12184 { IBM1026 CP1026 csIBM1026 }
12185 { EBCDIC-AT-DE csIBMEBCDICATDE }
12186 { EBCDIC-AT-DE-A csEBCDICATDEA }
12187 { EBCDIC-CA-FR csEBCDICCAFR }
12188 { EBCDIC-DK-NO csEBCDICDKNO }
12189 { EBCDIC-DK-NO-A csEBCDICDKNOA }
12190 { EBCDIC-FI-SE csEBCDICFISE }
12191 { EBCDIC-FI-SE-A csEBCDICFISEA }
12192 { EBCDIC-FR csEBCDICFR }
12193 { EBCDIC-IT csEBCDICIT }
12194 { EBCDIC-PT csEBCDICPT }
12195 { EBCDIC-ES csEBCDICES }
12196 { EBCDIC-ES-A csEBCDICESA }
12197 { EBCDIC-ES-S csEBCDICESS }
12198 { EBCDIC-UK csEBCDICUK }
12199 { EBCDIC-US csEBCDICUS }
12200 { UNKNOWN-8BIT csUnknown8BiT }
12201 { MNEMONIC csMnemonic }
12202 { MNEM csMnem }
12203 { VISCII csVISCII }
12204 { VIQR csVIQR }
12205 { KOI8-R csKOI8R }
12206 { IBM00858 CCSID00858 CP00858 PC-Multilingual-850+euro }
12207 { IBM00924 CCSID00924 CP00924 ebcdic-Latin9--euro }
12208 { IBM01140 CCSID01140 CP01140 ebcdic-us-37+euro }
12209 { IBM01141 CCSID01141 CP01141 ebcdic-de-273+euro }
12210 { IBM01142 CCSID01142 CP01142 ebcdic-dk-277+euro ebcdic-no-277+euro }
12211 { IBM01143 CCSID01143 CP01143 ebcdic-fi-278+euro ebcdic-se-278+euro }
12212 { IBM01144 CCSID01144 CP01144 ebcdic-it-280+euro }
12213 { IBM01145 CCSID01145 CP01145 ebcdic-es-284+euro }
12214 { IBM01146 CCSID01146 CP01146 ebcdic-gb-285+euro }
12215 { IBM01147 CCSID01147 CP01147 ebcdic-fr-297+euro }
12216 { IBM01148 CCSID01148 CP01148 ebcdic-international-500+euro }
12217 { IBM01149 CCSID01149 CP01149 ebcdic-is-871+euro }
12218 { IBM1047 IBM-1047 }
12219 { PTCP154 csPTCP154 PT154 CP154 Cyrillic-Asian }
12220 { Amiga-1251 Ami1251 Amiga1251 Ami-1251 }
12221 { UNICODE-1-1 csUnicode11 }
12222 { CESU-8 csCESU-8 }
12223 { BOCU-1 csBOCU-1 }
12224 { UNICODE-1-1-UTF-7 csUnicode11UTF7 }
12225 { ISO-8859-14 iso-ir-199 ISO_8859-14:1998 ISO_8859-14 latin8 iso-celtic
12226 l8 }
12227 { ISO-8859-15 ISO_8859-15 Latin-9 }
12228 { ISO-8859-16 iso-ir-226 ISO_8859-16:2001 ISO_8859-16 latin10 l10 }
12229 { GBK CP936 MS936 windows-936 }
12230 { JIS_Encoding csJISEncoding }
12231 { Shift_JIS MS_Kanji csShiftJIS ShiftJIS Shift-JIS }
12232 { Extended_UNIX_Code_Packed_Format_for_Japanese csEUCPkdFmtJapanese
12233 EUC-JP }
12234 { Extended_UNIX_Code_Fixed_Width_for_Japanese csEUCFixWidJapanese }
12235 { ISO-10646-UCS-Basic csUnicodeASCII }
12236 { ISO-10646-Unicode-Latin1 csUnicodeLatin1 ISO-10646 }
12237 { ISO-Unicode-IBM-1261 csUnicodeIBM1261 }
12238 { ISO-Unicode-IBM-1268 csUnicodeIBM1268 }
12239 { ISO-Unicode-IBM-1276 csUnicodeIBM1276 }
12240 { ISO-Unicode-IBM-1264 csUnicodeIBM1264 }
12241 { ISO-Unicode-IBM-1265 csUnicodeIBM1265 }
12242 { ISO-8859-1-Windows-3.0-Latin-1 csWindows30Latin1 }
12243 { ISO-8859-1-Windows-3.1-Latin-1 csWindows31Latin1 }
12244 { ISO-8859-2-Windows-Latin-2 csWindows31Latin2 }
12245 { ISO-8859-9-Windows-Latin-5 csWindows31Latin5 }
12246 { Adobe-Standard-Encoding csAdobeStandardEncoding }
12247 { Ventura-US csVenturaUS }
12248 { Ventura-International csVenturaInternational }
12249 { PC8-Danish-Norwegian csPC8DanishNorwegian }
12250 { PC8-Turkish csPC8Turkish }
12251 { IBM-Symbols csIBMSymbols }
12252 { IBM-Thai csIBMThai }
12253 { HP-Legal csHPLegal }
12254 { HP-Pi-font csHPPiFont }
12255 { HP-Math8 csHPMath8 }
12256 { Adobe-Symbol-Encoding csHPPSMath }
12257 { HP-DeskTop csHPDesktop }
12258 { Ventura-Math csVenturaMath }
12259 { Microsoft-Publishing csMicrosoftPublishing }
12260 { Windows-31J csWindows31J }
12261 { GB2312 csGB2312 }
12262 { Big5 csBig5 }
12263}
12264
12265proc tcl_encoding {enc} {
12266 global encoding_aliases tcl_encoding_cache
12267 if {[info exists tcl_encoding_cache($enc)]} {
12268 return $tcl_encoding_cache($enc)
12269 }
12270 set names [encoding names]
12271 set lcnames [string tolower $names]
12272 set enc [string tolower $enc]
12273 set i [lsearch -exact $lcnames $enc]
12274 if {$i < 0} {
12275 # look for "isonnn" instead of "iso-nnn" or "iso_nnn"
12276 if {[regsub {^(iso|cp|ibm|jis)[-_]} $enc {\1} encx]} {
12277 set i [lsearch -exact $lcnames $encx]
12278 }
12279 }
12280 if {$i < 0} {
12281 foreach l $encoding_aliases {
12282 set ll [string tolower $l]
12283 if {[lsearch -exact $ll $enc] < 0} continue
12284 # look through the aliases for one that tcl knows about
12285 foreach e $ll {
12286 set i [lsearch -exact $lcnames $e]
12287 if {$i < 0} {
12288 if {[regsub {^(iso|cp|ibm|jis)[-_]} $e {\1} ex]} {
12289 set i [lsearch -exact $lcnames $ex]
12290 }
12291 }
12292 if {$i >= 0} break
12293 }
12294 break
12295 }
12296 }
12297 set tclenc {}
12298 if {$i >= 0} {
12299 set tclenc [lindex $names $i]
12300 }
12301 set tcl_encoding_cache($enc) $tclenc
12302 return $tclenc
12303}
12304
12305proc gitattr {path attr default} {
12306 global path_attr_cache
12307 if {[info exists path_attr_cache($attr,$path)]} {
12308 set r $path_attr_cache($attr,$path)
12309 } else {
12310 set r "unspecified"
12311 if {![catch {set line [safe_exec [list git check-attr $attr -- $path]]}]} {
12312 regexp "(.*): $attr: (.*)" $line m f r
12313 }
12314 set path_attr_cache($attr,$path) $r
12315 }
12316 if {$r eq "unspecified"} {
12317 return $default
12318 }
12319 return $r
12320}
12321
12322proc cache_gitattr {attr pathlist} {
12323 global path_attr_cache
12324 set newlist {}
12325 foreach path $pathlist {
12326 if {![info exists path_attr_cache($attr,$path)]} {
12327 lappend newlist $path
12328 }
12329 }
12330 set lim 1000
12331 if {[tk windowingsystem] == "win32"} {
12332 # windows has a 32k limit on the arguments to a command...
12333 set lim 30
12334 }
12335 while {$newlist ne {}} {
12336 set head [lrange $newlist 0 [expr {$lim - 1}]]
12337 set newlist [lrange $newlist $lim end]
12338 if {![catch {set rlist [safe_exec [concat git check-attr $attr -- $head]]}]} {
12339 foreach row [split $rlist "\n"] {
12340 if {[regexp "(.*): $attr: (.*)" $row m path value]} {
12341 if {[string index $path 0] eq "\""} {
12342 set path [encoding convertfrom utf-8 [lindex $path 0]]
12343 }
12344 set path_attr_cache($attr,$path) $value
12345 }
12346 }
12347 }
12348 }
12349}
12350
12351proc get_path_encoding {path} {
12352 global gui_encoding perfile_attrs
12353 set tcl_enc $gui_encoding
12354 if {$path ne {} && $perfile_attrs} {
12355 set enc2 [tcl_encoding [gitattr $path encoding $tcl_enc]]
12356 if {$enc2 ne {}} {
12357 set tcl_enc $enc2
12358 }
12359 }
12360 return $tcl_enc
12361}
12362
12363## For msgcat loading, first locate the installation location.
12364if { [info exists ::env(GITK_MSGSDIR)] } {
12365 ## Msgsdir was manually set in the environment.
12366 set gitk_msgsdir $::env(GITK_MSGSDIR)
12367} else {
12368 ## Let's guess the prefix from argv0.
12369 set gitk_prefix [file dirname [file dirname [file normalize $argv0]]]
12370 set gitk_libdir [file join $gitk_prefix share gitk lib]
12371 set gitk_msgsdir [file join $gitk_libdir msgs]
12372}
12373
12374## Internationalization (i18n) through msgcat and gettext. See
12375## http://www.gnu.org/software/gettext/manual/html_node/Tcl.html
12376package require msgcat
12377namespace import ::msgcat::mc
12378## And eventually load the actual message catalog
12379::msgcat::mcload $gitk_msgsdir
12380
12381# on OSX bring the current Wish process window to front
12382if {[tk windowingsystem] eq "aqua"} {
12383 safe_exec [list osascript -e [format {
12384 tell application "System Events"
12385 set frontmost of processes whose unix id is %d to true
12386 end tell
12387 } [pid] ]]
12388}
12389
12390# Unset GIT_TRACE var if set
12391if { [info exists ::env(GIT_TRACE)] } {
12392 unset ::env(GIT_TRACE)
12393}
12394
12395# defaults...
12396set wrcomcmd "git diff-tree --stdin -p --pretty=email"
12397
12398set gitencoding {}
12399catch {
12400 set gitencoding [exec git config --get i18n.commitencoding]
12401}
12402catch {
12403 set gitencoding [exec git config --get i18n.logoutputencoding]
12404}
12405if {$gitencoding == ""} {
12406 set gitencoding "utf-8"
12407}
12408set tclencoding [tcl_encoding $gitencoding]
12409if {$tclencoding == {}} {
12410 puts stderr "Warning: encoding $gitencoding is not supported by Tcl/Tk"
12411}
12412
12413set gui_encoding [encoding system]
12414catch {
12415 set enc [exec git config --get gui.encoding]
12416 if {$enc ne {}} {
12417 set tclenc [tcl_encoding $enc]
12418 if {$tclenc ne {}} {
12419 set gui_encoding $tclenc
12420 } else {
12421 puts stderr "Warning: encoding $enc is not supported by Tcl/Tk"
12422 }
12423 }
12424}
12425
12426# Use object format as hash algorightm (either "sha1" or "sha256")
12427set hashalgorithm [exec git rev-parse --show-object-format]
12428if {$hashalgorithm eq "sha1"} {
12429 set hashlength 40
12430} elseif {$hashalgorithm eq "sha256"} {
12431 set hashlength 64
12432} else {
12433 puts stderr "Unknown hash algorithm: $hashalgorithm"
12434 exit 1
12435}
12436
12437set log_showroot true
12438catch {
12439 set log_showroot [exec git config --bool --get log.showroot]
12440}
12441
12442if {[tk windowingsystem] eq "aqua"} {
12443 set mainfont {{Lucida Grande} 9}
12444 set textfont {Monaco 9}
12445 set uifont {{Lucida Grande} 9 bold}
12446} elseif {![catch {::tk::pkgconfig get fontsystem} xft] && $xft eq "xft"} {
12447 # fontconfig!
12448 set mainfont {sans 9}
12449 set textfont {monospace 9}
12450 set uifont {sans 9 bold}
12451} else {
12452 set mainfont {Helvetica 9}
12453 set textfont {Courier 9}
12454 set uifont {Helvetica 9 bold}
12455}
12456set tabstop 8
12457set findmergefiles 0
12458set maxgraphpct 50
12459set maxwidth 16
12460set revlistorder 0
12461set fastdate 0
12462set uparrowlen 5
12463set downarrowlen 5
12464set mingaplen 100
12465set cmitmode "patch"
12466set wrapcomment "none"
12467set wrapdefault "none"
12468set showneartags 1
12469set hideremotes 0
12470set sortrefsbytype 1
12471set maxrefs 20
12472set visiblerefs {"master"}
12473set maxlinelen 200
12474set showlocalchanges 1
12475set limitdiffs 1
12476set kscroll 3
12477set datetimeformat "%Y-%m-%d %H:%M:%S"
12478set autocopy 0
12479set autoselect 1
12480set autosellen $hashlength
12481set perfile_attrs 0
12482
12483if {[tk windowingsystem] eq "aqua"} {
12484 set extdifftool "opendiff"
12485} else {
12486 set extdifftool "meld"
12487}
12488
12489set colors {"#00ff00" red blue magenta darkgrey brown orange}
12490if {[tk windowingsystem] eq "win32"} {
12491 set uicolor SystemButtonFace
12492 set uifgcolor SystemButtonText
12493 set uifgdisabledcolor SystemDisabledText
12494 set bgcolor SystemWindow
12495 set fgcolor SystemWindowText
12496 set selectbgcolor SystemHighlight
12497 set web_browser "cmd /c start"
12498} else {
12499 set uicolor grey85
12500 set uifgcolor black
12501 set uifgdisabledcolor "#999"
12502 set bgcolor white
12503 set fgcolor black
12504 set selectbgcolor gray85
12505 if {[tk windowingsystem] eq "aqua"} {
12506 set web_browser "open"
12507 } else {
12508 set web_browser "xdg-open"
12509 }
12510}
12511set diffcolors {"#c30000" "#009800" blue}
12512set diffbgcolors {"#fff3f3" "#f0fff0"}
12513set diffcontext 3
12514set mergecolors {red blue "#00ff00" purple brown "#009090" magenta "#808000" "#009000" "#ff0080" cyan "#b07070" "#70b0f0" "#70f0b0" "#f0b070" "#ff70b0"}
12515set ignorespace 0
12516set worddiff ""
12517set markbgcolor "#e0e0ff"
12518
12519set headbgcolor "#00ff00"
12520set headfgcolor black
12521set headoutlinecolor black
12522set remotebgcolor #ffddaa
12523set tagbgcolor yellow
12524set tagfgcolor black
12525set tagoutlinecolor black
12526set reflinecolor black
12527set filesepbgcolor #aaaaaa
12528set filesepfgcolor black
12529set linehoverbgcolor #ffff80
12530set linehoverfgcolor black
12531set linehoveroutlinecolor black
12532set mainheadcirclecolor yellow
12533set workingfilescirclecolor red
12534set indexcirclecolor "#00ff00"
12535set circlecolors {white blue gray blue blue}
12536set linkfgcolor blue
12537set circleoutlinecolor $fgcolor
12538set foundbgcolor yellow
12539set currentsearchhitbgcolor orange
12540
12541# button for popping up context menus
12542if {[tk windowingsystem] eq "aqua"} {
12543 set ctxbut <Button-2>
12544} else {
12545 set ctxbut <Button-3>
12546}
12547
12548catch {
12549 # follow the XDG base directory specification by default. See
12550 # https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
12551 if {[info exists env(XDG_CONFIG_HOME)] && $env(XDG_CONFIG_HOME) ne ""} {
12552 # XDG_CONFIG_HOME environment variable is set
12553 set config_file [file join $env(XDG_CONFIG_HOME) git gitk]
12554 set config_file_tmp [file join $env(XDG_CONFIG_HOME) git gitk-tmp]
12555 } else {
12556 # default XDG_CONFIG_HOME
12557 set config_file "~/.config/git/gitk"
12558 set config_file_tmp "~/.config/git/gitk-tmp"
12559 }
12560 if {![file exists $config_file]} {
12561 # for backward compatibility use the old config file if it exists
12562 if {[file exists "~/.gitk"]} {
12563 set config_file "~/.gitk"
12564 set config_file_tmp "~/.gitk-tmp"
12565 } elseif {![file exists [file dirname $config_file]]} {
12566 file mkdir [file dirname $config_file]
12567 }
12568 }
12569 source $config_file
12570}
12571config_check_tmp_exists 50
12572
12573set config_variables {
12574 autocopy
12575 autoselect
12576 autosellen
12577 bgcolor
12578 circlecolors
12579 circleoutlinecolor
12580 cmitmode
12581 colors
12582 currentsearchhitbgcolor
12583 datetimeformat
12584 diffbgcolors
12585 diffcolors
12586 diffcontext
12587 extdifftool
12588 fgcolor
12589 filesepbgcolor
12590 filesepfgcolor
12591 findmergefiles
12592 foundbgcolor
12593 headbgcolor
12594 headfgcolor
12595 headoutlinecolor
12596 hideremotes
12597 indexcirclecolor
12598 kscroll
12599 limitdiffs
12600 linehoverbgcolor
12601 linehoverfgcolor
12602 linehoveroutlinecolor
12603 linkfgcolor
12604 mainfont
12605 mainheadcirclecolor
12606 markbgcolor
12607 maxgraphpct
12608 maxrefs
12609 maxwidth
12610 mergecolors
12611 perfile_attrs
12612 reflinecolor
12613 remotebgcolor
12614 selectbgcolor
12615 showlocalchanges
12616 showneartags
12617 sortrefsbytype
12618 tabstop
12619 tagbgcolor
12620 tagfgcolor
12621 tagoutlinecolor
12622 textfont
12623 uicolor
12624 uifgcolor
12625 uifgdisabledcolor
12626 uifont
12627 visiblerefs
12628 web_browser
12629 workingfilescirclecolor
12630 wrapcomment
12631 wrapdefault
12632}
12633
12634foreach var $config_variables {
12635 config_init_trace $var
12636 trace add variable $var write config_variable_change_cb
12637}
12638
12639parsefont mainfont $mainfont
12640eval font create mainfont [fontflags mainfont]
12641eval font create mainfontbold [fontflags mainfont 1]
12642
12643parsefont textfont $textfont
12644eval font create textfont [fontflags textfont]
12645eval font create textfontbold [fontflags textfont 1]
12646
12647parsefont uifont $uifont
12648eval font create uifont [fontflags uifont]
12649
12650setoptions
12651
12652# check that we can find a .git directory somewhere...
12653if {[catch {set gitdir [exec git rev-parse --git-dir]}]} {
12654 show_error {} . [mc "Cannot find a git repository here."]
12655 exit 1
12656}
12657
12658set selecthead {}
12659set selectheadid {}
12660
12661set revtreeargs {}
12662set cmdline_files {}
12663set i 0
12664set revtreeargscmd {}
12665foreach arg $argv {
12666 switch -glob -- $arg {
12667 "" { }
12668 "--" {
12669 set cmdline_files [lrange $argv [expr {$i + 1}] end]
12670 break
12671 }
12672 "--select-commit=*" {
12673 set selecthead [string range $arg 16 end]
12674 }
12675 "--argscmd=*" {
12676 set revtreeargscmd [string range $arg 10 end]
12677 }
12678 default {
12679 lappend revtreeargs $arg
12680 }
12681 }
12682 incr i
12683}
12684
12685if {$selecthead eq "HEAD"} {
12686 set selecthead {}
12687}
12688
12689if {$i >= [llength $argv] && $revtreeargs ne {}} {
12690 # no -- on command line, but some arguments (other than --argscmd)
12691 if {[catch {
12692 set f [safe_exec [concat git rev-parse --no-revs --no-flags $revtreeargs]]
12693 set cmdline_files [split $f "\n"]
12694 set n [llength $cmdline_files]
12695 set revtreeargs [lrange $revtreeargs 0 end-$n]
12696 # Unfortunately git rev-parse doesn't produce an error when
12697 # something is both a revision and a filename. To be consistent
12698 # with git log and git rev-list, check revtreeargs for filenames.
12699 foreach arg $revtreeargs {
12700 if {[file exists $arg]} {
12701 show_error {} . [mc "Ambiguous argument '%s': both revision\
12702 and filename" $arg]
12703 exit 1
12704 }
12705 }
12706 } err]} {
12707 # unfortunately we get both stdout and stderr in $err,
12708 # so look for "fatal:".
12709 set i [string first "fatal:" $err]
12710 if {$i > 0} {
12711 set err [string range $err [expr {$i + 6}] end]
12712 }
12713 show_error {} . "[mc "Bad arguments to gitk:"]\n$err"
12714 exit 1
12715 }
12716}
12717
12718set nullid "0000000000000000000000000000000000000000"
12719set nullid2 "0000000000000000000000000000000000000001"
12720set nullfile "/dev/null"
12721
12722setttkstyle
12723set appname "gitk"
12724
12725set runq {}
12726set history {}
12727set historyindex 0
12728set fh_serial 0
12729set nhl_names {}
12730set highlight_paths {}
12731set findpattern {}
12732set searchdirn -forwards
12733set boldids {}
12734set boldnameids {}
12735set diffelide {0 0}
12736set markingmatches 0
12737set linkentercount 0
12738set need_redisplay 0
12739set nrows_drawn 0
12740set firsttabstop 0
12741
12742set nextviewnum 1
12743set curview 0
12744set selectedview 0
12745set selectedhlview [mc "None"]
12746set highlight_related [mc "None"]
12747set highlight_files {}
12748set viewfiles(0) {}
12749set viewperm(0) 0
12750set viewchanged(0) 0
12751set viewargs(0) {}
12752set viewargscmd(0) {}
12753
12754set selectedline {}
12755set numcommits 0
12756set loginstance 0
12757set cmdlineok 0
12758set stopped 0
12759set stuffsaved 0
12760set patchnum 0
12761set lserial 0
12762set hasworktree [hasworktree]
12763set cdup {}
12764if {[expr {[exec git rev-parse --is-inside-work-tree] == "true"}]} {
12765 set cdup [exec git rev-parse --show-cdup]
12766}
12767set worktree [gitworktree]
12768setcoords
12769makewindow
12770if {$::tcl_platform(platform) eq {windows} && [file exists $gitk_prefix/etc/git.ico]} {
12771 wm iconbitmap . -default $gitk_prefix/etc/git.ico
12772} else {
12773 catch {
12774 image create photo gitlogo -width 16 -height 16
12775
12776 image create photo gitlogominus -width 4 -height 2
12777 gitlogominus put #C00000 -to 0 0 4 2
12778 gitlogo copy gitlogominus -to 1 5
12779 gitlogo copy gitlogominus -to 6 5
12780 gitlogo copy gitlogominus -to 11 5
12781 image delete gitlogominus
12782
12783 image create photo gitlogoplus -width 4 -height 4
12784 gitlogoplus put #008000 -to 1 0 3 4
12785 gitlogoplus put #008000 -to 0 1 4 3
12786 gitlogo copy gitlogoplus -to 1 9
12787 gitlogo copy gitlogoplus -to 6 9
12788 gitlogo copy gitlogoplus -to 11 9
12789 image delete gitlogoplus
12790
12791 image create photo gitlogo32 -width 32 -height 32
12792 gitlogo32 copy gitlogo -zoom 2 2
12793
12794 wm iconphoto . -default gitlogo gitlogo32
12795 }
12796}
12797# wait for the window to become visible
12798if {![winfo viewable .]} {tkwait visibility .}
12799set_window_title
12800update
12801readrefs
12802
12803if {$cmdline_files ne {} || $revtreeargs ne {} || $revtreeargscmd ne {}} {
12804 # create a view for the files/dirs specified on the command line
12805 set curview 1
12806 set selectedview 1
12807 set nextviewnum 2
12808 set viewname(1) [mc "Command line"]
12809 set viewfiles(1) $cmdline_files
12810 set viewargs(1) $revtreeargs
12811 set viewargscmd(1) $revtreeargscmd
12812 set viewperm(1) 0
12813 set viewchanged(1) 0
12814 set vdatemode(1) 0
12815 addviewmenu 1
12816 .bar.view entryconf [mca "&Edit view..."] -state normal
12817 .bar.view entryconf [mca "&Delete view"] -state normal
12818}
12819
12820if {[info exists permviews]} {
12821 foreach v $permviews {
12822 set n $nextviewnum
12823 incr nextviewnum
12824 set viewname($n) [lindex $v 0]
12825 set viewfiles($n) [lindex $v 1]
12826 set viewargs($n) [lindex $v 2]
12827 set viewargscmd($n) [lindex $v 3]
12828 set viewperm($n) 1
12829 set viewchanged($n) 0
12830 addviewmenu $n
12831 }
12832}
12833
12834if {[tk windowingsystem] eq "win32"} {
12835 focus -force .
12836}
12837
12838set_gui_colors
12839
12840getcommits {}
12841
12842# Local variables:
12843# mode: tcl
12844# indent-tabs-mode: t
12845# tab-width: 8
12846# End: