gitk: Teach "Reread references" to reload tags
[git/git.git] / gitk
CommitLineData
1db95b00
PM
1#!/bin/sh
2# Tcl ignores the next line -*- tcl -*- \
9e026d39 3exec wish "$0" -- "$@"
1db95b00 4
bb3e86a1 5# Copyright © 2005-2011 Paul Mackerras. All rights reserved.
1db95b00
PM
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
d93f1713
PT
10package require Tk
11
74cb884f
MZ
12proc hasworktree {} {
13 return [expr {[exec git rev-parse --is-bare-repository] == "false" &&
14 [exec git rev-parse --is-inside-git-dir] == "false"}]
15}
16
3878e636
ZJS
17proc reponame {} {
18 global gitdir
19 set n [file normalize $gitdir]
20 if {[string match "*/.git" $n]} {
21 set n [string range $n 0 end-5]
22 }
23 return [file tail $n]
24}
25
65bb0bda
PT
26proc gitworktree {} {
27 variable _gitworktree
28 if {[info exists _gitworktree]} {
29 return $_gitworktree
30 }
31 # v1.7.0 introduced --show-toplevel to return the canonical work-tree
32 if {[catch {set _gitworktree [exec git rev-parse --show-toplevel]}]} {
33 # try to set work tree from environment, core.worktree or use
34 # cdup to obtain a relative path to the top of the worktree. If
35 # run from the top, the ./ prefix ensures normalize expands pwd.
36 if {[catch { set _gitworktree $env(GIT_WORK_TREE) }]} {
37 catch {set _gitworktree [exec git config --get core.worktree]}
38 if {$_gitworktree eq ""} {
39 set _gitworktree [file normalize ./[exec git rev-parse --show-cdup]]
40 }
41 }
42 }
43 return $_gitworktree
44}
45
7eb3cb9c
PM
46# A simple scheduler for compute-intensive stuff.
47# The aim is to make sure that event handlers for GUI actions can
48# run at least every 50-100 ms. Unfortunately fileevent handlers are
49# run before X event handlers, so reading from a fast source can
50# make the GUI completely unresponsive.
51proc run args {
df75e86d 52 global isonrunq runq currunq
7eb3cb9c
PM
53
54 set script $args
55 if {[info exists isonrunq($script)]} return
df75e86d 56 if {$runq eq {} && ![info exists currunq]} {
7eb3cb9c
PM
57 after idle dorunq
58 }
59 lappend runq [list {} $script]
60 set isonrunq($script) 1
61}
62
63proc filerun {fd script} {
64 fileevent $fd readable [list filereadable $fd $script]
65}
66
67proc filereadable {fd script} {
df75e86d 68 global runq currunq
7eb3cb9c
PM
69
70 fileevent $fd readable {}
df75e86d 71 if {$runq eq {} && ![info exists currunq]} {
7eb3cb9c
PM
72 after idle dorunq
73 }
74 lappend runq [list $fd $script]
75}
76
7fcc92bf
PM
77proc nukefile {fd} {
78 global runq
79
80 for {set i 0} {$i < [llength $runq]} {} {
81 if {[lindex $runq $i 0] eq $fd} {
82 set runq [lreplace $runq $i $i]
83 } else {
84 incr i
85 }
86 }
87}
88
7eb3cb9c 89proc dorunq {} {
df75e86d 90 global isonrunq runq currunq
7eb3cb9c
PM
91
92 set tstart [clock clicks -milliseconds]
93 set t0 $tstart
7fcc92bf 94 while {[llength $runq] > 0} {
7eb3cb9c
PM
95 set fd [lindex $runq 0 0]
96 set script [lindex $runq 0 1]
df75e86d
AG
97 set currunq [lindex $runq 0]
98 set runq [lrange $runq 1 end]
7eb3cb9c 99 set repeat [eval $script]
df75e86d 100 unset currunq
7eb3cb9c
PM
101 set t1 [clock clicks -milliseconds]
102 set t [expr {$t1 - $t0}]
7eb3cb9c
PM
103 if {$repeat ne {} && $repeat} {
104 if {$fd eq {} || $repeat == 2} {
105 # script returns 1 if it wants to be readded
106 # file readers return 2 if they could do more straight away
107 lappend runq [list $fd $script]
108 } else {
109 fileevent $fd readable [list filereadable $fd $script]
110 }
111 } elseif {$fd eq {}} {
112 unset isonrunq($script)
113 }
114 set t0 $t1
115 if {$t1 - $tstart >= 80} break
116 }
117 if {$runq ne {}} {
118 after idle dorunq
119 }
120}
121
e439e092
AG
122proc reg_instance {fd} {
123 global commfd leftover loginstance
124
125 set i [incr loginstance]
126 set commfd($i) $fd
127 set leftover($i) {}
128 return $i
129}
130
3ed31a81
PM
131proc unmerged_files {files} {
132 global nr_unmerged
133
134 # find the list of unmerged files
135 set mlist {}
136 set nr_unmerged 0
137 if {[catch {
138 set fd [open "| git ls-files -u" r]
139 } err]} {
140 show_error {} . "[mc "Couldn't get list of unmerged files:"] $err"
141 exit 1
142 }
143 while {[gets $fd line] >= 0} {
144 set i [string first "\t" $line]
145 if {$i < 0} continue
146 set fname [string range $line [expr {$i+1}] end]
147 if {[lsearch -exact $mlist $fname] >= 0} continue
148 incr nr_unmerged
149 if {$files eq {} || [path_filter $files $fname]} {
150 lappend mlist $fname
151 }
152 }
153 catch {close $fd}
154 return $mlist
155}
156
157proc parseviewargs {n arglist} {
c2f2dab9 158 global vdatemode vmergeonly vflags vdflags vrevs vfiltered vorigargs env
ae4e3ff9 159 global worddiff git_version
3ed31a81
PM
160
161 set vdatemode($n) 0
162 set vmergeonly($n) 0
ee66e089
PM
163 set glflags {}
164 set diffargs {}
165 set nextisval 0
166 set revargs {}
167 set origargs $arglist
168 set allknown 1
169 set filtered 0
170 set i -1
171 foreach arg $arglist {
172 incr i
173 if {$nextisval} {
174 lappend glflags $arg
175 set nextisval 0
176 continue
177 }
3ed31a81
PM
178 switch -glob -- $arg {
179 "-d" -
180 "--date-order" {
181 set vdatemode($n) 1
ee66e089
PM
182 # remove from origargs in case we hit an unknown option
183 set origargs [lreplace $origargs $i $i]
184 incr i -1
185 }
ee66e089
PM
186 "-[puabwcrRBMC]" -
187 "--no-renames" - "--full-index" - "--binary" - "--abbrev=*" -
188 "--find-copies-harder" - "-l*" - "--ext-diff" - "--no-ext-diff" -
189 "--src-prefix=*" - "--dst-prefix=*" - "--no-prefix" -
190 "-O*" - "--text" - "--full-diff" - "--ignore-space-at-eol" -
191 "--ignore-space-change" - "-U*" - "--unified=*" {
29582284
PM
192 # These request or affect diff output, which we don't want.
193 # Some could be used to set our defaults for diff display.
ee66e089
PM
194 lappend diffargs $arg
195 }
ee66e089 196 "--raw" - "--patch-with-raw" - "--patch-with-stat" -
ae4e3ff9 197 "--name-only" - "--name-status" - "--color" -
ee66e089
PM
198 "--log-size" - "--pretty=*" - "--decorate" - "--abbrev-commit" -
199 "--cc" - "-z" - "--header" - "--parents" - "--boundary" -
200 "--no-color" - "-g" - "--walk-reflogs" - "--no-walk" -
201 "--timestamp" - "relative-date" - "--date=*" - "--stdin" -
202 "--objects" - "--objects-edge" - "--reverse" {
29582284
PM
203 # These cause our parsing of git log's output to fail, or else
204 # they're options we want to set ourselves, so ignore them.
ee66e089 205 }
ae4e3ff9
TR
206 "--color-words*" - "--word-diff=color" {
207 # These trigger a word diff in the console interface,
208 # so help the user by enabling our own support
209 if {[package vcompare $git_version "1.7.2"] >= 0} {
210 set worddiff [mc "Color words"]
211 }
212 }
213 "--word-diff*" {
214 if {[package vcompare $git_version "1.7.2"] >= 0} {
215 set worddiff [mc "Markup words"]
216 }
217 }
ee66e089
PM
218 "--stat=*" - "--numstat" - "--shortstat" - "--summary" -
219 "--check" - "--exit-code" - "--quiet" - "--topo-order" -
220 "--full-history" - "--dense" - "--sparse" -
221 "--follow" - "--left-right" - "--encoding=*" {
29582284 222 # These are harmless, and some are even useful
ee66e089
PM
223 lappend glflags $arg
224 }
ee66e089
PM
225 "--diff-filter=*" - "--no-merges" - "--unpacked" -
226 "--max-count=*" - "--skip=*" - "--since=*" - "--after=*" -
227 "--until=*" - "--before=*" - "--max-age=*" - "--min-age=*" -
228 "--author=*" - "--committer=*" - "--grep=*" - "-[iE]" -
229 "--remove-empty" - "--first-parent" - "--cherry-pick" -
f687aaa8
DS
230 "-S*" - "--pickaxe-all" - "--pickaxe-regex" -
231 "--simplify-by-decoration" {
29582284 232 # These mean that we get a subset of the commits
ee66e089
PM
233 set filtered 1
234 lappend glflags $arg
235 }
ee66e089 236 "-n" {
29582284
PM
237 # This appears to be the only one that has a value as a
238 # separate word following it
ee66e089
PM
239 set filtered 1
240 set nextisval 1
241 lappend glflags $arg
242 }
6e7e87c7 243 "--not" - "--all" {
ee66e089 244 lappend revargs $arg
3ed31a81
PM
245 }
246 "--merge" {
247 set vmergeonly($n) 1
ee66e089
PM
248 # git rev-parse doesn't understand --merge
249 lappend revargs --gitk-symmetric-diff-marker MERGE_HEAD...HEAD
250 }
c2f2dab9
CC
251 "--no-replace-objects" {
252 set env(GIT_NO_REPLACE_OBJECTS) "1"
253 }
ee66e089 254 "-*" {
29582284 255 # Other flag arguments including -<n>
ee66e089
PM
256 if {[string is digit -strict [string range $arg 1 end]]} {
257 set filtered 1
258 } else {
259 # a flag argument that we don't recognize;
260 # that means we can't optimize
261 set allknown 0
262 }
263 lappend glflags $arg
3ed31a81
PM
264 }
265 default {
29582284 266 # Non-flag arguments specify commits or ranges of commits
ee66e089
PM
267 if {[string match "*...*" $arg]} {
268 lappend revargs --gitk-symmetric-diff-marker
269 }
270 lappend revargs $arg
271 }
272 }
273 }
274 set vdflags($n) $diffargs
275 set vflags($n) $glflags
276 set vrevs($n) $revargs
277 set vfiltered($n) $filtered
278 set vorigargs($n) $origargs
279 return $allknown
280}
281
282proc parseviewrevs {view revs} {
283 global vposids vnegids
284
285 if {$revs eq {}} {
286 set revs HEAD
287 }
288 if {[catch {set ids [eval exec git rev-parse $revs]} err]} {
289 # we get stdout followed by stderr in $err
290 # for an unknown rev, git rev-parse echoes it and then errors out
291 set errlines [split $err "\n"]
292 set badrev {}
293 for {set l 0} {$l < [llength $errlines]} {incr l} {
294 set line [lindex $errlines $l]
295 if {!([string length $line] == 40 && [string is xdigit $line])} {
296 if {[string match "fatal:*" $line]} {
297 if {[string match "fatal: ambiguous argument*" $line]
298 && $badrev ne {}} {
299 if {[llength $badrev] == 1} {
300 set err "unknown revision $badrev"
301 } else {
302 set err "unknown revisions: [join $badrev ", "]"
303 }
304 } else {
305 set err [join [lrange $errlines $l end] "\n"]
306 }
307 break
308 }
309 lappend badrev $line
310 }
d93f1713 311 }
3945d2c0 312 error_popup "[mc "Error parsing revisions:"] $err"
ee66e089
PM
313 return {}
314 }
315 set ret {}
316 set pos {}
317 set neg {}
318 set sdm 0
319 foreach id [split $ids "\n"] {
320 if {$id eq "--gitk-symmetric-diff-marker"} {
321 set sdm 4
322 } elseif {[string match "^*" $id]} {
323 if {$sdm != 1} {
324 lappend ret $id
325 if {$sdm == 3} {
326 set sdm 0
327 }
328 }
329 lappend neg [string range $id 1 end]
330 } else {
331 if {$sdm != 2} {
332 lappend ret $id
333 } else {
2b1fbf90 334 lset ret end $id...[lindex $ret end]
3ed31a81 335 }
ee66e089 336 lappend pos $id
3ed31a81 337 }
ee66e089 338 incr sdm -1
3ed31a81 339 }
ee66e089
PM
340 set vposids($view) $pos
341 set vnegids($view) $neg
342 return $ret
3ed31a81
PM
343}
344
f9e0b6fb 345# Start off a git log process and arrange to read its output
da7c24dd 346proc start_rev_list {view} {
6df7403a 347 global startmsecs commitidx viewcomplete curview
e439e092 348 global tclencoding
ee66e089 349 global viewargs viewargscmd viewfiles vfilelimit
d375ef9b 350 global showlocalchanges
e439e092 351 global viewactive viewinstances vmergeonly
cdc8429c 352 global mainheadid viewmainheadid viewmainheadid_orig
ee66e089 353 global vcanopt vflags vrevs vorigargs
7defefb1 354 global show_notes
9ccbdfbf 355
9ccbdfbf 356 set startmsecs [clock clicks -milliseconds]
da7c24dd 357 set commitidx($view) 0
3ed31a81
PM
358 # these are set this way for the error exits
359 set viewcomplete($view) 1
360 set viewactive($view) 0
7fcc92bf
PM
361 varcinit $view
362
2d480856
YD
363 set args $viewargs($view)
364 if {$viewargscmd($view) ne {}} {
365 if {[catch {
366 set str [exec sh -c $viewargscmd($view)]
367 } err]} {
3945d2c0 368 error_popup "[mc "Error executing --argscmd command:"] $err"
3ed31a81 369 return 0
2d480856
YD
370 }
371 set args [concat $args [split $str "\n"]]
372 }
ee66e089 373 set vcanopt($view) [parseviewargs $view $args]
3ed31a81
PM
374
375 set files $viewfiles($view)
376 if {$vmergeonly($view)} {
377 set files [unmerged_files $files]
378 if {$files eq {}} {
379 global nr_unmerged
380 if {$nr_unmerged == 0} {
381 error_popup [mc "No files selected: --merge specified but\
382 no files are unmerged."]
383 } else {
384 error_popup [mc "No files selected: --merge specified but\
385 no unmerged files are within file limit."]
386 }
387 return 0
388 }
389 }
390 set vfilelimit($view) $files
391
ee66e089
PM
392 if {$vcanopt($view)} {
393 set revs [parseviewrevs $view $vrevs($view)]
394 if {$revs eq {}} {
395 return 0
396 }
397 set args [concat $vflags($view) $revs]
398 } else {
399 set args $vorigargs($view)
400 }
401
418c4c7b 402 if {[catch {
7defefb1
KS
403 set fd [open [concat | git log --no-color -z --pretty=raw $show_notes \
404 --parents --boundary $args "--" $files] r]
418c4c7b 405 } err]} {
00abadb9 406 error_popup "[mc "Error executing git log:"] $err"
3ed31a81 407 return 0
1d10f36d 408 }
e439e092 409 set i [reg_instance $fd]
7fcc92bf 410 set viewinstances($view) [list $i]
cdc8429c
PM
411 set viewmainheadid($view) $mainheadid
412 set viewmainheadid_orig($view) $mainheadid
413 if {$files ne {} && $mainheadid ne {}} {
414 get_viewmainhead $view
415 }
416 if {$showlocalchanges && $viewmainheadid($view) ne {}} {
417 interestedin $viewmainheadid($view) dodiffindex
3e6b893f 418 }
86da5b6c 419 fconfigure $fd -blocking 0 -translation lf -eofchar {}
fd8ccbec 420 if {$tclencoding != {}} {
da7c24dd 421 fconfigure $fd -encoding $tclencoding
fd8ccbec 422 }
f806f0fb 423 filerun $fd [list getcommitlines $fd $i $view 0]
d990cedf 424 nowbusy $view [mc "Reading"]
3ed31a81
PM
425 set viewcomplete($view) 0
426 set viewactive($view) 1
427 return 1
38ad0910
PM
428}
429
e2f90ee4
AG
430proc stop_instance {inst} {
431 global commfd leftover
432
433 set fd $commfd($inst)
434 catch {
435 set pid [pid $fd]
b6326e92
AG
436
437 if {$::tcl_platform(platform) eq {windows}} {
438 exec kill -f $pid
439 } else {
440 exec kill $pid
441 }
e2f90ee4
AG
442 }
443 catch {close $fd}
444 nukefile $fd
445 unset commfd($inst)
446 unset leftover($inst)
447}
448
449proc stop_backends {} {
450 global commfd
451
452 foreach inst [array names commfd] {
453 stop_instance $inst
454 }
455}
456
7fcc92bf 457proc stop_rev_list {view} {
e2f90ee4 458 global viewinstances
22626ef4 459
7fcc92bf 460 foreach inst $viewinstances($view) {
e2f90ee4 461 stop_instance $inst
22626ef4 462 }
7fcc92bf 463 set viewinstances($view) {}
22626ef4
PM
464}
465
567c34e0 466proc reset_pending_select {selid} {
39816d60 467 global pending_select mainheadid selectheadid
567c34e0
AG
468
469 if {$selid ne {}} {
470 set pending_select $selid
39816d60
AG
471 } elseif {$selectheadid ne {}} {
472 set pending_select $selectheadid
567c34e0
AG
473 } else {
474 set pending_select $mainheadid
475 }
476}
477
478proc getcommits {selid} {
3ed31a81 479 global canv curview need_redisplay viewactive
38ad0910 480
da7c24dd 481 initlayout
3ed31a81 482 if {[start_rev_list $curview]} {
567c34e0 483 reset_pending_select $selid
3ed31a81
PM
484 show_status [mc "Reading commits..."]
485 set need_redisplay 1
486 } else {
487 show_status [mc "No commits selected"]
488 }
1d10f36d
PM
489}
490
7fcc92bf 491proc updatecommits {} {
ee66e089 492 global curview vcanopt vorigargs vfilelimit viewinstances
e439e092
AG
493 global viewactive viewcomplete tclencoding
494 global startmsecs showneartags showlocalchanges
cdc8429c 495 global mainheadid viewmainheadid viewmainheadid_orig pending_select
74cb884f 496 global hasworktree
ee66e089 497 global varcid vposids vnegids vflags vrevs
7defefb1 498 global show_notes
7fcc92bf 499
74cb884f 500 set hasworktree [hasworktree]
fc2a256f 501 rereadrefs
cdc8429c
PM
502 set view $curview
503 if {$mainheadid ne $viewmainheadid_orig($view)} {
504 if {$showlocalchanges} {
eb5f8c9c
PM
505 dohidelocalchanges
506 }
cdc8429c
PM
507 set viewmainheadid($view) $mainheadid
508 set viewmainheadid_orig($view) $mainheadid
509 if {$vfilelimit($view) ne {}} {
510 get_viewmainhead $view
eb5f8c9c
PM
511 }
512 }
cdc8429c
PM
513 if {$showlocalchanges} {
514 doshowlocalchanges
515 }
ee66e089
PM
516 if {$vcanopt($view)} {
517 set oldpos $vposids($view)
518 set oldneg $vnegids($view)
519 set revs [parseviewrevs $view $vrevs($view)]
520 if {$revs eq {}} {
521 return
522 }
523 # note: getting the delta when negative refs change is hard,
524 # and could require multiple git log invocations, so in that
525 # case we ask git log for all the commits (not just the delta)
526 if {$oldneg eq $vnegids($view)} {
527 set newrevs {}
528 set npos 0
529 # take out positive refs that we asked for before or
530 # that we have already seen
531 foreach rev $revs {
532 if {[string length $rev] == 40} {
533 if {[lsearch -exact $oldpos $rev] < 0
534 && ![info exists varcid($view,$rev)]} {
535 lappend newrevs $rev
536 incr npos
537 }
538 } else {
539 lappend $newrevs $rev
540 }
541 }
542 if {$npos == 0} return
543 set revs $newrevs
544 set vposids($view) [lsort -unique [concat $oldpos $vposids($view)]]
545 }
546 set args [concat $vflags($view) $revs --not $oldpos]
547 } else {
548 set args $vorigargs($view)
549 }
7fcc92bf 550 if {[catch {
7defefb1
KS
551 set fd [open [concat | git log --no-color -z --pretty=raw $show_notes \
552 --parents --boundary $args "--" $vfilelimit($view)] r]
7fcc92bf 553 } err]} {
3945d2c0 554 error_popup "[mc "Error executing git log:"] $err"
ee66e089 555 return
7fcc92bf
PM
556 }
557 if {$viewactive($view) == 0} {
558 set startmsecs [clock clicks -milliseconds]
559 }
e439e092 560 set i [reg_instance $fd]
7fcc92bf 561 lappend viewinstances($view) $i
7fcc92bf
PM
562 fconfigure $fd -blocking 0 -translation lf -eofchar {}
563 if {$tclencoding != {}} {
564 fconfigure $fd -encoding $tclencoding
565 }
f806f0fb 566 filerun $fd [list getcommitlines $fd $i $view 1]
7fcc92bf
PM
567 incr viewactive($view)
568 set viewcomplete($view) 0
567c34e0 569 reset_pending_select {}
b56e0a9a 570 nowbusy $view [mc "Reading"]
7fcc92bf
PM
571 if {$showneartags} {
572 getallcommits
573 }
574}
575
576proc reloadcommits {} {
577 global curview viewcomplete selectedline currentid thickerline
578 global showneartags treediffs commitinterest cached_commitrow
6df7403a 579 global targetid
7fcc92bf 580
567c34e0
AG
581 set selid {}
582 if {$selectedline ne {}} {
583 set selid $currentid
584 }
585
7fcc92bf
PM
586 if {!$viewcomplete($curview)} {
587 stop_rev_list $curview
7fcc92bf
PM
588 }
589 resetvarcs $curview
94b4a69f 590 set selectedline {}
7fcc92bf
PM
591 catch {unset currentid}
592 catch {unset thickerline}
593 catch {unset treediffs}
594 readrefs
595 changedrefs
596 if {$showneartags} {
597 getallcommits
598 }
599 clear_display
600 catch {unset commitinterest}
601 catch {unset cached_commitrow}
42a671fc 602 catch {unset targetid}
7fcc92bf 603 setcanvscroll
567c34e0 604 getcommits $selid
e7297a1c 605 return 0
7fcc92bf
PM
606}
607
6e8c8707
PM
608# This makes a string representation of a positive integer which
609# sorts as a string in numerical order
610proc strrep {n} {
611 if {$n < 16} {
612 return [format "%x" $n]
613 } elseif {$n < 256} {
614 return [format "x%.2x" $n]
615 } elseif {$n < 65536} {
616 return [format "y%.4x" $n]
617 }
618 return [format "z%.8x" $n]
619}
620
7fcc92bf
PM
621# Procedures used in reordering commits from git log (without
622# --topo-order) into the order for display.
623
624proc varcinit {view} {
f3ea5ede
PM
625 global varcstart vupptr vdownptr vleftptr vbackptr varctok varcrow
626 global vtokmod varcmod vrowmod varcix vlastins
7fcc92bf 627
7fcc92bf
PM
628 set varcstart($view) {{}}
629 set vupptr($view) {0}
630 set vdownptr($view) {0}
631 set vleftptr($view) {0}
f3ea5ede 632 set vbackptr($view) {0}
7fcc92bf
PM
633 set varctok($view) {{}}
634 set varcrow($view) {{}}
635 set vtokmod($view) {}
636 set varcmod($view) 0
e5b37ac1 637 set vrowmod($view) 0
7fcc92bf 638 set varcix($view) {{}}
f3ea5ede 639 set vlastins($view) {0}
7fcc92bf
PM
640}
641
642proc resetvarcs {view} {
643 global varcid varccommits parents children vseedcount ordertok
22387f23 644 global vshortids
7fcc92bf
PM
645
646 foreach vid [array names varcid $view,*] {
647 unset varcid($vid)
648 unset children($vid)
649 unset parents($vid)
650 }
22387f23
PM
651 foreach vid [array names vshortids $view,*] {
652 unset vshortids($vid)
653 }
7fcc92bf
PM
654 # some commits might have children but haven't been seen yet
655 foreach vid [array names children $view,*] {
656 unset children($vid)
657 }
658 foreach va [array names varccommits $view,*] {
659 unset varccommits($va)
660 }
661 foreach vd [array names vseedcount $view,*] {
662 unset vseedcount($vd)
663 }
9257d8f7 664 catch {unset ordertok}
7fcc92bf
PM
665}
666
468bcaed
PM
667# returns a list of the commits with no children
668proc seeds {v} {
669 global vdownptr vleftptr varcstart
670
671 set ret {}
672 set a [lindex $vdownptr($v) 0]
673 while {$a != 0} {
674 lappend ret [lindex $varcstart($v) $a]
675 set a [lindex $vleftptr($v) $a]
676 }
677 return $ret
678}
679
7fcc92bf 680proc newvarc {view id} {
3ed31a81 681 global varcid varctok parents children vdatemode
f3ea5ede
PM
682 global vupptr vdownptr vleftptr vbackptr varcrow varcix varcstart
683 global commitdata commitinfo vseedcount varccommits vlastins
7fcc92bf
PM
684
685 set a [llength $varctok($view)]
686 set vid $view,$id
3ed31a81 687 if {[llength $children($vid)] == 0 || $vdatemode($view)} {
7fcc92bf
PM
688 if {![info exists commitinfo($id)]} {
689 parsecommit $id $commitdata($id) 1
690 }
f5974d97 691 set cdate [lindex [lindex $commitinfo($id) 4] 0]
7fcc92bf
PM
692 if {![string is integer -strict $cdate]} {
693 set cdate 0
694 }
695 if {![info exists vseedcount($view,$cdate)]} {
696 set vseedcount($view,$cdate) -1
697 }
698 set c [incr vseedcount($view,$cdate)]
699 set cdate [expr {$cdate ^ 0xffffffff}]
700 set tok "s[strrep $cdate][strrep $c]"
7fcc92bf
PM
701 } else {
702 set tok {}
f3ea5ede
PM
703 }
704 set ka 0
705 if {[llength $children($vid)] > 0} {
706 set kid [lindex $children($vid) end]
707 set k $varcid($view,$kid)
708 if {[string compare [lindex $varctok($view) $k] $tok] > 0} {
709 set ki $kid
710 set ka $k
711 set tok [lindex $varctok($view) $k]
7fcc92bf 712 }
f3ea5ede
PM
713 }
714 if {$ka != 0} {
7fcc92bf
PM
715 set i [lsearch -exact $parents($view,$ki) $id]
716 set j [expr {[llength $parents($view,$ki)] - 1 - $i}]
7fcc92bf
PM
717 append tok [strrep $j]
718 }
f3ea5ede
PM
719 set c [lindex $vlastins($view) $ka]
720 if {$c == 0 || [string compare $tok [lindex $varctok($view) $c]] < 0} {
721 set c $ka
722 set b [lindex $vdownptr($view) $ka]
723 } else {
724 set b [lindex $vleftptr($view) $c]
725 }
726 while {$b != 0 && [string compare $tok [lindex $varctok($view) $b]] >= 0} {
727 set c $b
728 set b [lindex $vleftptr($view) $c]
729 }
730 if {$c == $ka} {
731 lset vdownptr($view) $ka $a
732 lappend vbackptr($view) 0
733 } else {
734 lset vleftptr($view) $c $a
735 lappend vbackptr($view) $c
736 }
737 lset vlastins($view) $ka $a
738 lappend vupptr($view) $ka
739 lappend vleftptr($view) $b
740 if {$b != 0} {
741 lset vbackptr($view) $b $a
742 }
7fcc92bf
PM
743 lappend varctok($view) $tok
744 lappend varcstart($view) $id
745 lappend vdownptr($view) 0
746 lappend varcrow($view) {}
747 lappend varcix($view) {}
e5b37ac1 748 set varccommits($view,$a) {}
f3ea5ede 749 lappend vlastins($view) 0
7fcc92bf
PM
750 return $a
751}
752
753proc splitvarc {p v} {
52b8ea93 754 global varcid varcstart varccommits varctok vtokmod
f3ea5ede 755 global vupptr vdownptr vleftptr vbackptr varcix varcrow vlastins
7fcc92bf
PM
756
757 set oa $varcid($v,$p)
52b8ea93 758 set otok [lindex $varctok($v) $oa]
7fcc92bf
PM
759 set ac $varccommits($v,$oa)
760 set i [lsearch -exact $varccommits($v,$oa) $p]
761 if {$i <= 0} return
762 set na [llength $varctok($v)]
763 # "%" sorts before "0"...
52b8ea93 764 set tok "$otok%[strrep $i]"
7fcc92bf
PM
765 lappend varctok($v) $tok
766 lappend varcrow($v) {}
767 lappend varcix($v) {}
768 set varccommits($v,$oa) [lrange $ac 0 [expr {$i - 1}]]
769 set varccommits($v,$na) [lrange $ac $i end]
770 lappend varcstart($v) $p
771 foreach id $varccommits($v,$na) {
772 set varcid($v,$id) $na
773 }
774 lappend vdownptr($v) [lindex $vdownptr($v) $oa]
841ea824 775 lappend vlastins($v) [lindex $vlastins($v) $oa]
7fcc92bf 776 lset vdownptr($v) $oa $na
841ea824 777 lset vlastins($v) $oa 0
7fcc92bf
PM
778 lappend vupptr($v) $oa
779 lappend vleftptr($v) 0
f3ea5ede 780 lappend vbackptr($v) 0
7fcc92bf
PM
781 for {set b [lindex $vdownptr($v) $na]} {$b != 0} {set b [lindex $vleftptr($v) $b]} {
782 lset vupptr($v) $b $na
783 }
52b8ea93
PM
784 if {[string compare $otok $vtokmod($v)] <= 0} {
785 modify_arc $v $oa
786 }
7fcc92bf
PM
787}
788
789proc renumbervarc {a v} {
790 global parents children varctok varcstart varccommits
3ed31a81 791 global vupptr vdownptr vleftptr vbackptr vlastins varcid vtokmod vdatemode
7fcc92bf
PM
792
793 set t1 [clock clicks -milliseconds]
794 set todo {}
795 set isrelated($a) 1
f3ea5ede 796 set kidchanged($a) 1
7fcc92bf
PM
797 set ntot 0
798 while {$a != 0} {
799 if {[info exists isrelated($a)]} {
800 lappend todo $a
801 set id [lindex $varccommits($v,$a) end]
802 foreach p $parents($v,$id) {
803 if {[info exists varcid($v,$p)]} {
804 set isrelated($varcid($v,$p)) 1
805 }
806 }
807 }
808 incr ntot
809 set b [lindex $vdownptr($v) $a]
810 if {$b == 0} {
811 while {$a != 0} {
812 set b [lindex $vleftptr($v) $a]
813 if {$b != 0} break
814 set a [lindex $vupptr($v) $a]
815 }
816 }
817 set a $b
818 }
819 foreach a $todo {
f3ea5ede 820 if {![info exists kidchanged($a)]} continue
7fcc92bf 821 set id [lindex $varcstart($v) $a]
f3ea5ede
PM
822 if {[llength $children($v,$id)] > 1} {
823 set children($v,$id) [lsort -command [list vtokcmp $v] \
824 $children($v,$id)]
825 }
826 set oldtok [lindex $varctok($v) $a]
3ed31a81 827 if {!$vdatemode($v)} {
f3ea5ede
PM
828 set tok {}
829 } else {
830 set tok $oldtok
831 }
832 set ka 0
c8c9f3d9
PM
833 set kid [last_real_child $v,$id]
834 if {$kid ne {}} {
f3ea5ede
PM
835 set k $varcid($v,$kid)
836 if {[string compare [lindex $varctok($v) $k] $tok] > 0} {
837 set ki $kid
838 set ka $k
839 set tok [lindex $varctok($v) $k]
7fcc92bf
PM
840 }
841 }
f3ea5ede 842 if {$ka != 0} {
7fcc92bf
PM
843 set i [lsearch -exact $parents($v,$ki) $id]
844 set j [expr {[llength $parents($v,$ki)] - 1 - $i}]
845 append tok [strrep $j]
7fcc92bf 846 }
f3ea5ede
PM
847 if {$tok eq $oldtok} {
848 continue
849 }
850 set id [lindex $varccommits($v,$a) end]
851 foreach p $parents($v,$id) {
852 if {[info exists varcid($v,$p)]} {
853 set kidchanged($varcid($v,$p)) 1
854 } else {
855 set sortkids($p) 1
856 }
857 }
858 lset varctok($v) $a $tok
7fcc92bf
PM
859 set b [lindex $vupptr($v) $a]
860 if {$b != $ka} {
9257d8f7
PM
861 if {[string compare [lindex $varctok($v) $ka] $vtokmod($v)] < 0} {
862 modify_arc $v $ka
38dfe939 863 }
9257d8f7
PM
864 if {[string compare [lindex $varctok($v) $b] $vtokmod($v)] < 0} {
865 modify_arc $v $b
38dfe939 866 }
f3ea5ede
PM
867 set c [lindex $vbackptr($v) $a]
868 set d [lindex $vleftptr($v) $a]
869 if {$c == 0} {
870 lset vdownptr($v) $b $d
7fcc92bf 871 } else {
f3ea5ede
PM
872 lset vleftptr($v) $c $d
873 }
874 if {$d != 0} {
875 lset vbackptr($v) $d $c
7fcc92bf 876 }
841ea824
PM
877 if {[lindex $vlastins($v) $b] == $a} {
878 lset vlastins($v) $b $c
879 }
7fcc92bf 880 lset vupptr($v) $a $ka
f3ea5ede
PM
881 set c [lindex $vlastins($v) $ka]
882 if {$c == 0 || \
883 [string compare $tok [lindex $varctok($v) $c]] < 0} {
884 set c $ka
885 set b [lindex $vdownptr($v) $ka]
886 } else {
887 set b [lindex $vleftptr($v) $c]
888 }
889 while {$b != 0 && \
890 [string compare $tok [lindex $varctok($v) $b]] >= 0} {
891 set c $b
892 set b [lindex $vleftptr($v) $c]
7fcc92bf 893 }
f3ea5ede
PM
894 if {$c == $ka} {
895 lset vdownptr($v) $ka $a
896 lset vbackptr($v) $a 0
897 } else {
898 lset vleftptr($v) $c $a
899 lset vbackptr($v) $a $c
7fcc92bf 900 }
f3ea5ede
PM
901 lset vleftptr($v) $a $b
902 if {$b != 0} {
903 lset vbackptr($v) $b $a
904 }
905 lset vlastins($v) $ka $a
906 }
907 }
908 foreach id [array names sortkids] {
909 if {[llength $children($v,$id)] > 1} {
910 set children($v,$id) [lsort -command [list vtokcmp $v] \
911 $children($v,$id)]
7fcc92bf
PM
912 }
913 }
914 set t2 [clock clicks -milliseconds]
915 #puts "renumbervarc did [llength $todo] of $ntot arcs in [expr {$t2-$t1}]ms"
916}
917
f806f0fb
PM
918# Fix up the graph after we have found out that in view $v,
919# $p (a commit that we have already seen) is actually the parent
920# of the last commit in arc $a.
7fcc92bf 921proc fix_reversal {p a v} {
24f7a667 922 global varcid varcstart varctok vupptr
7fcc92bf
PM
923
924 set pa $varcid($v,$p)
925 if {$p ne [lindex $varcstart($v) $pa]} {
926 splitvarc $p $v
927 set pa $varcid($v,$p)
928 }
24f7a667
PM
929 # seeds always need to be renumbered
930 if {[lindex $vupptr($v) $pa] == 0 ||
931 [string compare [lindex $varctok($v) $a] \
932 [lindex $varctok($v) $pa]] > 0} {
7fcc92bf
PM
933 renumbervarc $pa $v
934 }
935}
936
937proc insertrow {id p v} {
b8a938cf
PM
938 global cmitlisted children parents varcid varctok vtokmod
939 global varccommits ordertok commitidx numcommits curview
22387f23 940 global targetid targetrow vshortids
b8a938cf
PM
941
942 readcommit $id
943 set vid $v,$id
944 set cmitlisted($vid) 1
945 set children($vid) {}
946 set parents($vid) [list $p]
947 set a [newvarc $v $id]
948 set varcid($vid) $a
22387f23 949 lappend vshortids($v,[string range $id 0 3]) $id
b8a938cf
PM
950 if {[string compare [lindex $varctok($v) $a] $vtokmod($v)] < 0} {
951 modify_arc $v $a
952 }
953 lappend varccommits($v,$a) $id
954 set vp $v,$p
955 if {[llength [lappend children($vp) $id]] > 1} {
956 set children($vp) [lsort -command [list vtokcmp $v] $children($vp)]
957 catch {unset ordertok}
958 }
959 fix_reversal $p $a $v
960 incr commitidx($v)
961 if {$v == $curview} {
962 set numcommits $commitidx($v)
963 setcanvscroll
964 if {[info exists targetid]} {
965 if {![comes_before $targetid $p]} {
966 incr targetrow
967 }
968 }
969 }
970}
971
972proc insertfakerow {id p} {
9257d8f7 973 global varcid varccommits parents children cmitlisted
b8a938cf 974 global commitidx varctok vtokmod targetid targetrow curview numcommits
7fcc92bf 975
b8a938cf 976 set v $curview
7fcc92bf
PM
977 set a $varcid($v,$p)
978 set i [lsearch -exact $varccommits($v,$a) $p]
979 if {$i < 0} {
b8a938cf 980 puts "oops: insertfakerow can't find [shortids $p] on arc $a"
7fcc92bf
PM
981 return
982 }
983 set children($v,$id) {}
984 set parents($v,$id) [list $p]
985 set varcid($v,$id) $a
9257d8f7 986 lappend children($v,$p) $id
7fcc92bf 987 set cmitlisted($v,$id) 1
b8a938cf 988 set numcommits [incr commitidx($v)]
7fcc92bf
PM
989 # note we deliberately don't update varcstart($v) even if $i == 0
990 set varccommits($v,$a) [linsert $varccommits($v,$a) $i $id]
c9cfdc96 991 modify_arc $v $a $i
42a671fc
PM
992 if {[info exists targetid]} {
993 if {![comes_before $targetid $p]} {
994 incr targetrow
995 }
996 }
b8a938cf 997 setcanvscroll
9257d8f7 998 drawvisible
7fcc92bf
PM
999}
1000
b8a938cf 1001proc removefakerow {id} {
9257d8f7 1002 global varcid varccommits parents children commitidx
fc2a256f 1003 global varctok vtokmod cmitlisted currentid selectedline
b8a938cf 1004 global targetid curview numcommits
7fcc92bf 1005
b8a938cf 1006 set v $curview
7fcc92bf 1007 if {[llength $parents($v,$id)] != 1} {
b8a938cf 1008 puts "oops: removefakerow [shortids $id] has [llength $parents($v,$id)] parents"
7fcc92bf
PM
1009 return
1010 }
1011 set p [lindex $parents($v,$id) 0]
1012 set a $varcid($v,$id)
1013 set i [lsearch -exact $varccommits($v,$a) $id]
1014 if {$i < 0} {
b8a938cf 1015 puts "oops: removefakerow can't find [shortids $id] on arc $a"
7fcc92bf
PM
1016 return
1017 }
1018 unset varcid($v,$id)
1019 set varccommits($v,$a) [lreplace $varccommits($v,$a) $i $i]
1020 unset parents($v,$id)
1021 unset children($v,$id)
1022 unset cmitlisted($v,$id)
b8a938cf 1023 set numcommits [incr commitidx($v) -1]
7fcc92bf
PM
1024 set j [lsearch -exact $children($v,$p) $id]
1025 if {$j >= 0} {
1026 set children($v,$p) [lreplace $children($v,$p) $j $j]
1027 }
c9cfdc96 1028 modify_arc $v $a $i
fc2a256f
PM
1029 if {[info exist currentid] && $id eq $currentid} {
1030 unset currentid
94b4a69f 1031 set selectedline {}
fc2a256f 1032 }
42a671fc
PM
1033 if {[info exists targetid] && $targetid eq $id} {
1034 set targetid $p
1035 }
b8a938cf 1036 setcanvscroll
9257d8f7 1037 drawvisible
7fcc92bf
PM
1038}
1039
aa43561a
PM
1040proc real_children {vp} {
1041 global children nullid nullid2
1042
1043 set kids {}
1044 foreach id $children($vp) {
1045 if {$id ne $nullid && $id ne $nullid2} {
1046 lappend kids $id
1047 }
1048 }
1049 return $kids
1050}
1051
c8c9f3d9
PM
1052proc first_real_child {vp} {
1053 global children nullid nullid2
1054
1055 foreach id $children($vp) {
1056 if {$id ne $nullid && $id ne $nullid2} {
1057 return $id
1058 }
1059 }
1060 return {}
1061}
1062
1063proc last_real_child {vp} {
1064 global children nullid nullid2
1065
1066 set kids $children($vp)
1067 for {set i [llength $kids]} {[incr i -1] >= 0} {} {
1068 set id [lindex $kids $i]
1069 if {$id ne $nullid && $id ne $nullid2} {
1070 return $id
1071 }
1072 }
1073 return {}
1074}
1075
7fcc92bf
PM
1076proc vtokcmp {v a b} {
1077 global varctok varcid
1078
1079 return [string compare [lindex $varctok($v) $varcid($v,$a)] \
1080 [lindex $varctok($v) $varcid($v,$b)]]
1081}
1082
c9cfdc96
PM
1083# This assumes that if lim is not given, the caller has checked that
1084# arc a's token is less than $vtokmod($v)
e5b37ac1
PM
1085proc modify_arc {v a {lim {}}} {
1086 global varctok vtokmod varcmod varcrow vupptr curview vrowmod varccommits
9257d8f7 1087
c9cfdc96
PM
1088 if {$lim ne {}} {
1089 set c [string compare [lindex $varctok($v) $a] $vtokmod($v)]
1090 if {$c > 0} return
1091 if {$c == 0} {
1092 set r [lindex $varcrow($v) $a]
1093 if {$r ne {} && $vrowmod($v) <= $r + $lim} return
1094 }
1095 }
9257d8f7
PM
1096 set vtokmod($v) [lindex $varctok($v) $a]
1097 set varcmod($v) $a
1098 if {$v == $curview} {
1099 while {$a != 0 && [lindex $varcrow($v) $a] eq {}} {
1100 set a [lindex $vupptr($v) $a]
e5b37ac1 1101 set lim {}
9257d8f7 1102 }
e5b37ac1
PM
1103 set r 0
1104 if {$a != 0} {
1105 if {$lim eq {}} {
1106 set lim [llength $varccommits($v,$a)]
1107 }
1108 set r [expr {[lindex $varcrow($v) $a] + $lim}]
1109 }
1110 set vrowmod($v) $r
0c27886e 1111 undolayout $r
9257d8f7
PM
1112 }
1113}
1114
7fcc92bf 1115proc update_arcrows {v} {
e5b37ac1 1116 global vtokmod varcmod vrowmod varcrow commitidx currentid selectedline
24f7a667 1117 global varcid vrownum varcorder varcix varccommits
7fcc92bf 1118 global vupptr vdownptr vleftptr varctok
24f7a667 1119 global displayorder parentlist curview cached_commitrow
7fcc92bf 1120
c9cfdc96
PM
1121 if {$vrowmod($v) == $commitidx($v)} return
1122 if {$v == $curview} {
1123 if {[llength $displayorder] > $vrowmod($v)} {
1124 set displayorder [lrange $displayorder 0 [expr {$vrowmod($v) - 1}]]
1125 set parentlist [lrange $parentlist 0 [expr {$vrowmod($v) - 1}]]
1126 }
1127 catch {unset cached_commitrow}
1128 }
7fcc92bf
PM
1129 set narctot [expr {[llength $varctok($v)] - 1}]
1130 set a $varcmod($v)
1131 while {$a != 0 && [lindex $varcix($v) $a] eq {}} {
1132 # go up the tree until we find something that has a row number,
1133 # or we get to a seed
1134 set a [lindex $vupptr($v) $a]
1135 }
1136 if {$a == 0} {
1137 set a [lindex $vdownptr($v) 0]
1138 if {$a == 0} return
1139 set vrownum($v) {0}
1140 set varcorder($v) [list $a]
1141 lset varcix($v) $a 0
1142 lset varcrow($v) $a 0
1143 set arcn 0
1144 set row 0
1145 } else {
1146 set arcn [lindex $varcix($v) $a]
7fcc92bf
PM
1147 if {[llength $vrownum($v)] > $arcn + 1} {
1148 set vrownum($v) [lrange $vrownum($v) 0 $arcn]
1149 set varcorder($v) [lrange $varcorder($v) 0 $arcn]
1150 }
1151 set row [lindex $varcrow($v) $a]
1152 }
7fcc92bf
PM
1153 while {1} {
1154 set p $a
1155 incr row [llength $varccommits($v,$a)]
1156 # go down if possible
1157 set b [lindex $vdownptr($v) $a]
1158 if {$b == 0} {
1159 # if not, go left, or go up until we can go left
1160 while {$a != 0} {
1161 set b [lindex $vleftptr($v) $a]
1162 if {$b != 0} break
1163 set a [lindex $vupptr($v) $a]
1164 }
1165 if {$a == 0} break
1166 }
1167 set a $b
1168 incr arcn
1169 lappend vrownum($v) $row
1170 lappend varcorder($v) $a
1171 lset varcix($v) $a $arcn
1172 lset varcrow($v) $a $row
1173 }
e5b37ac1
PM
1174 set vtokmod($v) [lindex $varctok($v) $p]
1175 set varcmod($v) $p
1176 set vrowmod($v) $row
7fcc92bf
PM
1177 if {[info exists currentid]} {
1178 set selectedline [rowofcommit $currentid]
1179 }
7fcc92bf
PM
1180}
1181
1182# Test whether view $v contains commit $id
1183proc commitinview {id v} {
1184 global varcid
1185
1186 return [info exists varcid($v,$id)]
1187}
1188
1189# Return the row number for commit $id in the current view
1190proc rowofcommit {id} {
1191 global varcid varccommits varcrow curview cached_commitrow
9257d8f7 1192 global varctok vtokmod
7fcc92bf 1193
7fcc92bf
PM
1194 set v $curview
1195 if {![info exists varcid($v,$id)]} {
1196 puts "oops rowofcommit no arc for [shortids $id]"
1197 return {}
1198 }
1199 set a $varcid($v,$id)
fc2a256f 1200 if {[string compare [lindex $varctok($v) $a] $vtokmod($v)] >= 0} {
9257d8f7
PM
1201 update_arcrows $v
1202 }
31c0eaa8
PM
1203 if {[info exists cached_commitrow($id)]} {
1204 return $cached_commitrow($id)
1205 }
7fcc92bf
PM
1206 set i [lsearch -exact $varccommits($v,$a) $id]
1207 if {$i < 0} {
1208 puts "oops didn't find commit [shortids $id] in arc $a"
1209 return {}
1210 }
1211 incr i [lindex $varcrow($v) $a]
1212 set cached_commitrow($id) $i
1213 return $i
1214}
1215
42a671fc
PM
1216# Returns 1 if a is on an earlier row than b, otherwise 0
1217proc comes_before {a b} {
1218 global varcid varctok curview
1219
1220 set v $curview
1221 if {$a eq $b || ![info exists varcid($v,$a)] || \
1222 ![info exists varcid($v,$b)]} {
1223 return 0
1224 }
1225 if {$varcid($v,$a) != $varcid($v,$b)} {
1226 return [expr {[string compare [lindex $varctok($v) $varcid($v,$a)] \
1227 [lindex $varctok($v) $varcid($v,$b)]] < 0}]
1228 }
1229 return [expr {[rowofcommit $a] < [rowofcommit $b]}]
1230}
1231
7fcc92bf
PM
1232proc bsearch {l elt} {
1233 if {[llength $l] == 0 || $elt <= [lindex $l 0]} {
1234 return 0
1235 }
1236 set lo 0
1237 set hi [llength $l]
1238 while {$hi - $lo > 1} {
1239 set mid [expr {int(($lo + $hi) / 2)}]
1240 set t [lindex $l $mid]
1241 if {$elt < $t} {
1242 set hi $mid
1243 } elseif {$elt > $t} {
1244 set lo $mid
1245 } else {
1246 return $mid
1247 }
1248 }
1249 return $lo
1250}
1251
1252# Make sure rows $start..$end-1 are valid in displayorder and parentlist
1253proc make_disporder {start end} {
1254 global vrownum curview commitidx displayorder parentlist
e5b37ac1 1255 global varccommits varcorder parents vrowmod varcrow
7fcc92bf
PM
1256 global d_valid_start d_valid_end
1257
e5b37ac1 1258 if {$end > $vrowmod($curview)} {
9257d8f7
PM
1259 update_arcrows $curview
1260 }
7fcc92bf
PM
1261 set ai [bsearch $vrownum($curview) $start]
1262 set start [lindex $vrownum($curview) $ai]
1263 set narc [llength $vrownum($curview)]
1264 for {set r $start} {$ai < $narc && $r < $end} {incr ai} {
1265 set a [lindex $varcorder($curview) $ai]
1266 set l [llength $displayorder]
1267 set al [llength $varccommits($curview,$a)]
1268 if {$l < $r + $al} {
1269 if {$l < $r} {
1270 set pad [ntimes [expr {$r - $l}] {}]
1271 set displayorder [concat $displayorder $pad]
1272 set parentlist [concat $parentlist $pad]
1273 } elseif {$l > $r} {
1274 set displayorder [lrange $displayorder 0 [expr {$r - 1}]]
1275 set parentlist [lrange $parentlist 0 [expr {$r - 1}]]
1276 }
1277 foreach id $varccommits($curview,$a) {
1278 lappend displayorder $id
1279 lappend parentlist $parents($curview,$id)
1280 }
17529cf9 1281 } elseif {[lindex $displayorder [expr {$r + $al - 1}]] eq {}} {
7fcc92bf
PM
1282 set i $r
1283 foreach id $varccommits($curview,$a) {
1284 lset displayorder $i $id
1285 lset parentlist $i $parents($curview,$id)
1286 incr i
1287 }
1288 }
1289 incr r $al
1290 }
1291}
1292
1293proc commitonrow {row} {
1294 global displayorder
1295
1296 set id [lindex $displayorder $row]
1297 if {$id eq {}} {
1298 make_disporder $row [expr {$row + 1}]
1299 set id [lindex $displayorder $row]
1300 }
1301 return $id
1302}
1303
1304proc closevarcs {v} {
1305 global varctok varccommits varcid parents children
d375ef9b 1306 global cmitlisted commitidx vtokmod
7fcc92bf
PM
1307
1308 set missing_parents 0
1309 set scripts {}
1310 set narcs [llength $varctok($v)]
1311 for {set a 1} {$a < $narcs} {incr a} {
1312 set id [lindex $varccommits($v,$a) end]
1313 foreach p $parents($v,$id) {
1314 if {[info exists varcid($v,$p)]} continue
1315 # add p as a new commit
1316 incr missing_parents
1317 set cmitlisted($v,$p) 0
1318 set parents($v,$p) {}
1319 if {[llength $children($v,$p)] == 1 &&
1320 [llength $parents($v,$id)] == 1} {
1321 set b $a
1322 } else {
1323 set b [newvarc $v $p]
1324 }
1325 set varcid($v,$p) $b
9257d8f7
PM
1326 if {[string compare [lindex $varctok($v) $b] $vtokmod($v)] < 0} {
1327 modify_arc $v $b
7fcc92bf 1328 }
e5b37ac1 1329 lappend varccommits($v,$b) $p
7fcc92bf 1330 incr commitidx($v)
d375ef9b 1331 set scripts [check_interest $p $scripts]
7fcc92bf
PM
1332 }
1333 }
1334 if {$missing_parents > 0} {
7fcc92bf
PM
1335 foreach s $scripts {
1336 eval $s
1337 }
1338 }
1339}
1340
f806f0fb
PM
1341# Use $rwid as a substitute for $id, i.e. reparent $id's children to $rwid
1342# Assumes we already have an arc for $rwid.
1343proc rewrite_commit {v id rwid} {
1344 global children parents varcid varctok vtokmod varccommits
1345
1346 foreach ch $children($v,$id) {
1347 # make $rwid be $ch's parent in place of $id
1348 set i [lsearch -exact $parents($v,$ch) $id]
1349 if {$i < 0} {
1350 puts "oops rewrite_commit didn't find $id in parent list for $ch"
1351 }
1352 set parents($v,$ch) [lreplace $parents($v,$ch) $i $i $rwid]
1353 # add $ch to $rwid's children and sort the list if necessary
1354 if {[llength [lappend children($v,$rwid) $ch]] > 1} {
1355 set children($v,$rwid) [lsort -command [list vtokcmp $v] \
1356 $children($v,$rwid)]
1357 }
1358 # fix the graph after joining $id to $rwid
1359 set a $varcid($v,$ch)
1360 fix_reversal $rwid $a $v
c9cfdc96
PM
1361 # parentlist is wrong for the last element of arc $a
1362 # even if displayorder is right, hence the 3rd arg here
1363 modify_arc $v $a [expr {[llength $varccommits($v,$a)] - 1}]
f806f0fb
PM
1364 }
1365}
1366
d375ef9b
PM
1367# Mechanism for registering a command to be executed when we come
1368# across a particular commit. To handle the case when only the
1369# prefix of the commit is known, the commitinterest array is now
1370# indexed by the first 4 characters of the ID. Each element is a
1371# list of id, cmd pairs.
1372proc interestedin {id cmd} {
1373 global commitinterest
1374
1375 lappend commitinterest([string range $id 0 3]) $id $cmd
1376}
1377
1378proc check_interest {id scripts} {
1379 global commitinterest
1380
1381 set prefix [string range $id 0 3]
1382 if {[info exists commitinterest($prefix)]} {
1383 set newlist {}
1384 foreach {i script} $commitinterest($prefix) {
1385 if {[string match "$i*" $id]} {
1386 lappend scripts [string map [list "%I" $id "%P" $i] $script]
1387 } else {
1388 lappend newlist $i $script
1389 }
1390 }
1391 if {$newlist ne {}} {
1392 set commitinterest($prefix) $newlist
1393 } else {
1394 unset commitinterest($prefix)
1395 }
1396 }
1397 return $scripts
1398}
1399
f806f0fb 1400proc getcommitlines {fd inst view updating} {
d375ef9b 1401 global cmitlisted leftover
3ed31a81 1402 global commitidx commitdata vdatemode
7fcc92bf 1403 global parents children curview hlview
468bcaed 1404 global idpending ordertok
22387f23 1405 global varccommits varcid varctok vtokmod vfilelimit vshortids
9ccbdfbf 1406
d1e46756 1407 set stuff [read $fd 500000]
005a2f4e 1408 # git log doesn't terminate the last commit with a null...
7fcc92bf 1409 if {$stuff == {} && $leftover($inst) ne {} && [eof $fd]} {
005a2f4e
PM
1410 set stuff "\0"
1411 }
b490a991 1412 if {$stuff == {}} {
7eb3cb9c
PM
1413 if {![eof $fd]} {
1414 return 1
1415 }
6df7403a 1416 global commfd viewcomplete viewactive viewname
7fcc92bf
PM
1417 global viewinstances
1418 unset commfd($inst)
1419 set i [lsearch -exact $viewinstances($view) $inst]
1420 if {$i >= 0} {
1421 set viewinstances($view) [lreplace $viewinstances($view) $i $i]
b0cdca99 1422 }
f0654861 1423 # set it blocking so we wait for the process to terminate
da7c24dd 1424 fconfigure $fd -blocking 1
098dd8a3
PM
1425 if {[catch {close $fd} err]} {
1426 set fv {}
1427 if {$view != $curview} {
1428 set fv " for the \"$viewname($view)\" view"
da7c24dd 1429 }
098dd8a3
PM
1430 if {[string range $err 0 4] == "usage"} {
1431 set err "Gitk: error reading commits$fv:\
f9e0b6fb 1432 bad arguments to git log."
098dd8a3
PM
1433 if {$viewname($view) eq "Command line"} {
1434 append err \
f9e0b6fb 1435 " (Note: arguments to gitk are passed to git log\
098dd8a3
PM
1436 to allow selection of commits to be displayed.)"
1437 }
1438 } else {
1439 set err "Error reading commits$fv: $err"
1440 }
1441 error_popup $err
1d10f36d 1442 }
7fcc92bf
PM
1443 if {[incr viewactive($view) -1] <= 0} {
1444 set viewcomplete($view) 1
1445 # Check if we have seen any ids listed as parents that haven't
1446 # appeared in the list
1447 closevarcs $view
1448 notbusy $view
7fcc92bf 1449 }
098dd8a3 1450 if {$view == $curview} {
ac1276ab 1451 run chewcommits
9a40c50c 1452 }
7eb3cb9c 1453 return 0
9a40c50c 1454 }
b490a991 1455 set start 0
8f7d0cec 1456 set gotsome 0
7fcc92bf 1457 set scripts {}
b490a991
PM
1458 while 1 {
1459 set i [string first "\0" $stuff $start]
1460 if {$i < 0} {
7fcc92bf 1461 append leftover($inst) [string range $stuff $start end]
9f1afe05 1462 break
9ccbdfbf 1463 }
b490a991 1464 if {$start == 0} {
7fcc92bf 1465 set cmit $leftover($inst)
8f7d0cec 1466 append cmit [string range $stuff 0 [expr {$i - 1}]]
7fcc92bf 1467 set leftover($inst) {}
8f7d0cec
PM
1468 } else {
1469 set cmit [string range $stuff $start [expr {$i - 1}]]
b490a991
PM
1470 }
1471 set start [expr {$i + 1}]
e5ea701b
PM
1472 set j [string first "\n" $cmit]
1473 set ok 0
16c1ff96 1474 set listed 1
c961b228
PM
1475 if {$j >= 0 && [string match "commit *" $cmit]} {
1476 set ids [string range $cmit 7 [expr {$j - 1}]]
1407ade9 1477 if {[string match {[-^<>]*} $ids]} {
c961b228
PM
1478 switch -- [string index $ids 0] {
1479 "-" {set listed 0}
1407ade9
LT
1480 "^" {set listed 2}
1481 "<" {set listed 3}
1482 ">" {set listed 4}
c961b228 1483 }
16c1ff96
PM
1484 set ids [string range $ids 1 end]
1485 }
e5ea701b
PM
1486 set ok 1
1487 foreach id $ids {
8f7d0cec 1488 if {[string length $id] != 40} {
e5ea701b
PM
1489 set ok 0
1490 break
1491 }
1492 }
1493 }
1494 if {!$ok} {
7e952e79
PM
1495 set shortcmit $cmit
1496 if {[string length $shortcmit] > 80} {
1497 set shortcmit "[string range $shortcmit 0 80]..."
1498 }
d990cedf 1499 error_popup "[mc "Can't parse git log output:"] {$shortcmit}"
b490a991
PM
1500 exit 1
1501 }
e5ea701b 1502 set id [lindex $ids 0]
7fcc92bf 1503 set vid $view,$id
f806f0fb 1504
22387f23
PM
1505 lappend vshortids($view,[string range $id 0 3]) $id
1506
f806f0fb 1507 if {!$listed && $updating && ![info exists varcid($vid)] &&
3ed31a81 1508 $vfilelimit($view) ne {}} {
f806f0fb
PM
1509 # git log doesn't rewrite parents for unlisted commits
1510 # when doing path limiting, so work around that here
1511 # by working out the rewritten parent with git rev-list
1512 # and if we already know about it, using the rewritten
1513 # parent as a substitute parent for $id's children.
1514 if {![catch {
1515 set rwid [exec git rev-list --first-parent --max-count=1 \
3ed31a81 1516 $id -- $vfilelimit($view)]
f806f0fb
PM
1517 }]} {
1518 if {$rwid ne {} && [info exists varcid($view,$rwid)]} {
1519 # use $rwid in place of $id
1520 rewrite_commit $view $id $rwid
1521 continue
1522 }
1523 }
1524 }
1525
f1bf4ee6
PM
1526 set a 0
1527 if {[info exists varcid($vid)]} {
1528 if {$cmitlisted($vid) || !$listed} continue
1529 set a $varcid($vid)
1530 }
16c1ff96
PM
1531 if {$listed} {
1532 set olds [lrange $ids 1 end]
16c1ff96
PM
1533 } else {
1534 set olds {}
1535 }
f7a3e8d2 1536 set commitdata($id) [string range $cmit [expr {$j + 1}] end]
7fcc92bf
PM
1537 set cmitlisted($vid) $listed
1538 set parents($vid) $olds
7fcc92bf
PM
1539 if {![info exists children($vid)]} {
1540 set children($vid) {}
f1bf4ee6 1541 } elseif {$a == 0 && [llength $children($vid)] == 1} {
f3ea5ede
PM
1542 set k [lindex $children($vid) 0]
1543 if {[llength $parents($view,$k)] == 1 &&
3ed31a81 1544 (!$vdatemode($view) ||
f3ea5ede
PM
1545 $varcid($view,$k) == [llength $varctok($view)] - 1)} {
1546 set a $varcid($view,$k)
7fcc92bf 1547 }
da7c24dd 1548 }
7fcc92bf
PM
1549 if {$a == 0} {
1550 # new arc
1551 set a [newvarc $view $id]
1552 }
e5b37ac1
PM
1553 if {[string compare [lindex $varctok($view) $a] $vtokmod($view)] < 0} {
1554 modify_arc $view $a
1555 }
f1bf4ee6
PM
1556 if {![info exists varcid($vid)]} {
1557 set varcid($vid) $a
1558 lappend varccommits($view,$a) $id
1559 incr commitidx($view)
1560 }
e5b37ac1 1561
7fcc92bf
PM
1562 set i 0
1563 foreach p $olds {
1564 if {$i == 0 || [lsearch -exact $olds $p] >= $i} {
1565 set vp $view,$p
1566 if {[llength [lappend children($vp) $id]] > 1 &&
1567 [vtokcmp $view [lindex $children($vp) end-1] $id] > 0} {
1568 set children($vp) [lsort -command [list vtokcmp $view] \
1569 $children($vp)]
9257d8f7 1570 catch {unset ordertok}
7fcc92bf 1571 }
f3ea5ede
PM
1572 if {[info exists varcid($view,$p)]} {
1573 fix_reversal $p $a $view
1574 }
7fcc92bf
PM
1575 }
1576 incr i
1577 }
7fcc92bf 1578
d375ef9b 1579 set scripts [check_interest $id $scripts]
8f7d0cec
PM
1580 set gotsome 1
1581 }
1582 if {$gotsome} {
ac1276ab
PM
1583 global numcommits hlview
1584
1585 if {$view == $curview} {
1586 set numcommits $commitidx($view)
1587 run chewcommits
1588 }
1589 if {[info exists hlview] && $view == $hlview} {
1590 # we never actually get here...
1591 run vhighlightmore
1592 }
7fcc92bf
PM
1593 foreach s $scripts {
1594 eval $s
1595 }
9ccbdfbf 1596 }
7eb3cb9c 1597 return 2
9ccbdfbf
PM
1598}
1599
ac1276ab 1600proc chewcommits {} {
f5f3c2e2 1601 global curview hlview viewcomplete
7fcc92bf 1602 global pending_select
7eb3cb9c 1603
ac1276ab
PM
1604 layoutmore
1605 if {$viewcomplete($curview)} {
1606 global commitidx varctok
1607 global numcommits startmsecs
ac1276ab
PM
1608
1609 if {[info exists pending_select]} {
835e62ae
AG
1610 update
1611 reset_pending_select {}
1612
1613 if {[commitinview $pending_select $curview]} {
1614 selectline [rowofcommit $pending_select] 1
1615 } else {
1616 set row [first_real_row]
1617 selectline $row 1
1618 }
7eb3cb9c 1619 }
ac1276ab
PM
1620 if {$commitidx($curview) > 0} {
1621 #set ms [expr {[clock clicks -milliseconds] - $startmsecs}]
1622 #puts "overall $ms ms for $numcommits commits"
1623 #puts "[llength $varctok($view)] arcs, $commitidx($view) commits"
1624 } else {
1625 show_status [mc "No commits selected"]
1626 }
1627 notbusy layout
b664550c 1628 }
f5f3c2e2 1629 return 0
1db95b00
PM
1630}
1631
590915da
AG
1632proc do_readcommit {id} {
1633 global tclencoding
1634
1635 # Invoke git-log to handle automatic encoding conversion
1636 set fd [open [concat | git log --no-color --pretty=raw -1 $id] r]
1637 # Read the results using i18n.logoutputencoding
1638 fconfigure $fd -translation lf -eofchar {}
1639 if {$tclencoding != {}} {
1640 fconfigure $fd -encoding $tclencoding
1641 }
1642 set contents [read $fd]
1643 close $fd
1644 # Remove the heading line
1645 regsub {^commit [0-9a-f]+\n} $contents {} contents
1646
1647 return $contents
1648}
1649
1db95b00 1650proc readcommit {id} {
590915da
AG
1651 if {[catch {set contents [do_readcommit $id]}]} return
1652 parsecommit $id $contents 1
b490a991
PM
1653}
1654
8f7d0cec 1655proc parsecommit {id contents listed} {
ef73896b 1656 global commitinfo
b5c2f306
SV
1657
1658 set inhdr 1
1659 set comment {}
1660 set headline {}
1661 set auname {}
1662 set audate {}
1663 set comname {}
1664 set comdate {}
232475d3
PM
1665 set hdrend [string first "\n\n" $contents]
1666 if {$hdrend < 0} {
1667 # should never happen...
1668 set hdrend [string length $contents]
1669 }
1670 set header [string range $contents 0 [expr {$hdrend - 1}]]
1671 set comment [string range $contents [expr {$hdrend + 2}] end]
1672 foreach line [split $header "\n"] {
61f57cb0 1673 set line [split $line " "]
232475d3
PM
1674 set tag [lindex $line 0]
1675 if {$tag == "author"} {
f5974d97 1676 set audate [lrange $line end-1 end]
61f57cb0 1677 set auname [join [lrange $line 1 end-2] " "]
232475d3 1678 } elseif {$tag == "committer"} {
f5974d97 1679 set comdate [lrange $line end-1 end]
61f57cb0 1680 set comname [join [lrange $line 1 end-2] " "]
1db95b00
PM
1681 }
1682 }
232475d3 1683 set headline {}
43c25074
PM
1684 # take the first non-blank line of the comment as the headline
1685 set headline [string trimleft $comment]
1686 set i [string first "\n" $headline]
232475d3 1687 if {$i >= 0} {
43c25074
PM
1688 set headline [string range $headline 0 $i]
1689 }
1690 set headline [string trimright $headline]
1691 set i [string first "\r" $headline]
1692 if {$i >= 0} {
1693 set headline [string trimright [string range $headline 0 $i]]
232475d3
PM
1694 }
1695 if {!$listed} {
f9e0b6fb 1696 # git log indents the comment by 4 spaces;
8974c6f9 1697 # if we got this via git cat-file, add the indentation
232475d3
PM
1698 set newcomment {}
1699 foreach line [split $comment "\n"] {
1700 append newcomment " "
1701 append newcomment $line
f6e2869f 1702 append newcomment "\n"
232475d3
PM
1703 }
1704 set comment $newcomment
1db95b00 1705 }
36242490 1706 set hasnote [string first "\nNotes:\n" $contents]
e5c2d856 1707 set commitinfo($id) [list $headline $auname $audate \
36242490 1708 $comname $comdate $comment $hasnote]
1db95b00
PM
1709}
1710
f7a3e8d2 1711proc getcommit {id} {
79b2c75e 1712 global commitdata commitinfo
8ed16484 1713
f7a3e8d2
PM
1714 if {[info exists commitdata($id)]} {
1715 parsecommit $id $commitdata($id) 1
8ed16484
PM
1716 } else {
1717 readcommit $id
1718 if {![info exists commitinfo($id)]} {
d990cedf 1719 set commitinfo($id) [list [mc "No commit information available"]]
8ed16484
PM
1720 }
1721 }
1722 return 1
1723}
1724
d375ef9b
PM
1725# Expand an abbreviated commit ID to a list of full 40-char IDs that match
1726# and are present in the current view.
1727# This is fairly slow...
1728proc longid {prefix} {
22387f23 1729 global varcid curview vshortids
d375ef9b
PM
1730
1731 set ids {}
22387f23
PM
1732 if {[string length $prefix] >= 4} {
1733 set vshortid $curview,[string range $prefix 0 3]
1734 if {[info exists vshortids($vshortid)]} {
1735 foreach id $vshortids($vshortid) {
1736 if {[string match "$prefix*" $id]} {
1737 if {[lsearch -exact $ids $id] < 0} {
1738 lappend ids $id
1739 if {[llength $ids] >= 2} break
1740 }
1741 }
1742 }
1743 }
1744 } else {
1745 foreach match [array names varcid "$curview,$prefix*"] {
1746 lappend ids [lindex [split $match ","] 1]
1747 if {[llength $ids] >= 2} break
1748 }
d375ef9b
PM
1749 }
1750 return $ids
1751}
1752
887fe3c4 1753proc readrefs {} {
62d3ea65 1754 global tagids idtags headids idheads tagobjid
219ea3a9 1755 global otherrefids idotherrefs mainhead mainheadid
39816d60 1756 global selecthead selectheadid
ffe15297 1757 global hideremotes
106288cb 1758
b5c2f306
SV
1759 foreach v {tagids idtags headids idheads otherrefids idotherrefs} {
1760 catch {unset $v}
1761 }
62d3ea65
PM
1762 set refd [open [list | git show-ref -d] r]
1763 while {[gets $refd line] >= 0} {
1764 if {[string index $line 40] ne " "} continue
1765 set id [string range $line 0 39]
1766 set ref [string range $line 41 end]
1767 if {![string match "refs/*" $ref]} continue
1768 set name [string range $ref 5 end]
1769 if {[string match "remotes/*" $name]} {
ffe15297 1770 if {![string match "*/HEAD" $name] && !$hideremotes} {
62d3ea65
PM
1771 set headids($name) $id
1772 lappend idheads($id) $name
f1d83ba3 1773 }
62d3ea65
PM
1774 } elseif {[string match "heads/*" $name]} {
1775 set name [string range $name 6 end]
36a7cad6
JH
1776 set headids($name) $id
1777 lappend idheads($id) $name
62d3ea65
PM
1778 } elseif {[string match "tags/*" $name]} {
1779 # this lets refs/tags/foo^{} overwrite refs/tags/foo,
1780 # which is what we want since the former is the commit ID
1781 set name [string range $name 5 end]
1782 if {[string match "*^{}" $name]} {
1783 set name [string range $name 0 end-3]
1784 } else {
1785 set tagobjid($name) $id
1786 }
1787 set tagids($name) $id
1788 lappend idtags($id) $name
36a7cad6
JH
1789 } else {
1790 set otherrefids($name) $id
1791 lappend idotherrefs($id) $name
f1d83ba3
PM
1792 }
1793 }
062d671f 1794 catch {close $refd}
8a48571c 1795 set mainhead {}
219ea3a9 1796 set mainheadid {}
8a48571c 1797 catch {
c11ff120 1798 set mainheadid [exec git rev-parse HEAD]
8a48571c
PM
1799 set thehead [exec git symbolic-ref HEAD]
1800 if {[string match "refs/heads/*" $thehead]} {
1801 set mainhead [string range $thehead 11 end]
1802 }
1803 }
39816d60
AG
1804 set selectheadid {}
1805 if {$selecthead ne {}} {
1806 catch {
1807 set selectheadid [exec git rev-parse --verify $selecthead]
1808 }
1809 }
887fe3c4
PM
1810}
1811
8f489363
PM
1812# skip over fake commits
1813proc first_real_row {} {
7fcc92bf 1814 global nullid nullid2 numcommits
8f489363
PM
1815
1816 for {set row 0} {$row < $numcommits} {incr row} {
7fcc92bf 1817 set id [commitonrow $row]
8f489363
PM
1818 if {$id ne $nullid && $id ne $nullid2} {
1819 break
1820 }
1821 }
1822 return $row
1823}
1824
e11f1233
PM
1825# update things for a head moved to a child of its previous location
1826proc movehead {id name} {
1827 global headids idheads
1828
1829 removehead $headids($name) $name
1830 set headids($name) $id
1831 lappend idheads($id) $name
1832}
1833
1834# update things when a head has been removed
1835proc removehead {id name} {
1836 global headids idheads
1837
1838 if {$idheads($id) eq $name} {
1839 unset idheads($id)
1840 } else {
1841 set i [lsearch -exact $idheads($id) $name]
1842 if {$i >= 0} {
1843 set idheads($id) [lreplace $idheads($id) $i $i]
1844 }
1845 }
1846 unset headids($name)
1847}
1848
d93f1713
PT
1849proc ttk_toplevel {w args} {
1850 global use_ttk
1851 eval [linsert $args 0 ::toplevel $w]
1852 if {$use_ttk} {
1853 place [ttk::frame $w._toplevel_background] -x 0 -y 0 -relwidth 1 -relheight 1
1854 }
1855 return $w
1856}
1857
e7d64008
AG
1858proc make_transient {window origin} {
1859 global have_tk85
1860
1861 # In MacOS Tk 8.4 transient appears to work by setting
1862 # overrideredirect, which is utterly useless, since the
1863 # windows get no border, and are not even kept above
1864 # the parent.
1865 if {!$have_tk85 && [tk windowingsystem] eq {aqua}} return
1866
1867 wm transient $window $origin
1868
1869 # Windows fails to place transient windows normally, so
1870 # schedule a callback to center them on the parent.
1871 if {[tk windowingsystem] eq {win32}} {
1872 after idle [list tk::PlaceWindow $window widget $origin]
1873 }
1874}
1875
8d849957 1876proc show_error {w top msg {mc mc}} {
d93f1713 1877 global NS
3cb1f9c9 1878 if {![info exists NS]} {set NS ""}
d93f1713 1879 if {[wm state $top] eq "withdrawn"} { wm deiconify $top }
df3d83b1
PM
1880 message $w.m -text $msg -justify center -aspect 400
1881 pack $w.m -side top -fill x -padx 20 -pady 20
7a0ebbf8 1882 ${NS}::button $w.ok -default active -text [$mc OK] -command "destroy $top"
df3d83b1 1883 pack $w.ok -side bottom -fill x
e54be9e3
PM
1884 bind $top <Visibility> "grab $top; focus $top"
1885 bind $top <Key-Return> "destroy $top"
76f15947
AG
1886 bind $top <Key-space> "destroy $top"
1887 bind $top <Key-Escape> "destroy $top"
e54be9e3 1888 tkwait window $top
df3d83b1
PM
1889}
1890
84a76f18 1891proc error_popup {msg {owner .}} {
d93f1713
PT
1892 if {[tk windowingsystem] eq "win32"} {
1893 tk_messageBox -icon error -type ok -title [wm title .] \
1894 -parent $owner -message $msg
1895 } else {
1896 set w .error
1897 ttk_toplevel $w
1898 make_transient $w $owner
1899 show_error $w $w $msg
1900 }
098dd8a3
PM
1901}
1902
84a76f18 1903proc confirm_popup {msg {owner .}} {
d93f1713 1904 global confirm_ok NS
10299152
PM
1905 set confirm_ok 0
1906 set w .confirm
d93f1713 1907 ttk_toplevel $w
e7d64008 1908 make_transient $w $owner
10299152
PM
1909 message $w.m -text $msg -justify center -aspect 400
1910 pack $w.m -side top -fill x -padx 20 -pady 20
d93f1713 1911 ${NS}::button $w.ok -text [mc OK] -command "set confirm_ok 1; destroy $w"
10299152 1912 pack $w.ok -side left -fill x
d93f1713 1913 ${NS}::button $w.cancel -text [mc Cancel] -command "destroy $w"
10299152
PM
1914 pack $w.cancel -side right -fill x
1915 bind $w <Visibility> "grab $w; focus $w"
76f15947
AG
1916 bind $w <Key-Return> "set confirm_ok 1; destroy $w"
1917 bind $w <Key-space> "set confirm_ok 1; destroy $w"
1918 bind $w <Key-Escape> "destroy $w"
d93f1713 1919 tk::PlaceWindow $w widget $owner
10299152
PM
1920 tkwait window $w
1921 return $confirm_ok
1922}
1923
b039f0a6 1924proc setoptions {} {
d93f1713
PT
1925 if {[tk windowingsystem] ne "win32"} {
1926 option add *Panedwindow.showHandle 1 startupFile
1927 option add *Panedwindow.sashRelief raised startupFile
1928 if {[tk windowingsystem] ne "aqua"} {
1929 option add *Menu.font uifont startupFile
1930 }
1931 } else {
1932 option add *Menu.TearOff 0 startupFile
1933 }
b039f0a6
PM
1934 option add *Button.font uifont startupFile
1935 option add *Checkbutton.font uifont startupFile
1936 option add *Radiobutton.font uifont startupFile
b039f0a6
PM
1937 option add *Menubutton.font uifont startupFile
1938 option add *Label.font uifont startupFile
1939 option add *Message.font uifont startupFile
b9b142ff
MH
1940 option add *Entry.font textfont startupFile
1941 option add *Text.font textfont startupFile
d93f1713 1942 option add *Labelframe.font uifont startupFile
0933b04e 1943 option add *Spinbox.font textfont startupFile
207ad7b8 1944 option add *Listbox.font mainfont startupFile
b039f0a6
PM
1945}
1946
79056034
PM
1947# Make a menu and submenus.
1948# m is the window name for the menu, items is the list of menu items to add.
1949# Each item is a list {mc label type description options...}
1950# mc is ignored; it's so we can put mc there to alert xgettext
1951# label is the string that appears in the menu
1952# type is cascade, command or radiobutton (should add checkbutton)
1953# description depends on type; it's the sublist for cascade, the
1954# command to invoke for command, or {variable value} for radiobutton
f2d0bbbd
PM
1955proc makemenu {m items} {
1956 menu $m
cea07cf8
AG
1957 if {[tk windowingsystem] eq {aqua}} {
1958 set Meta1 Cmd
1959 } else {
1960 set Meta1 Ctrl
1961 }
f2d0bbbd 1962 foreach i $items {
79056034
PM
1963 set name [mc [lindex $i 1]]
1964 set type [lindex $i 2]
1965 set thing [lindex $i 3]
f2d0bbbd
PM
1966 set params [list $type]
1967 if {$name ne {}} {
1968 set u [string first "&" [string map {&& x} $name]]
1969 lappend params -label [string map {&& & & {}} $name]
1970 if {$u >= 0} {
1971 lappend params -underline $u
1972 }
1973 }
1974 switch -- $type {
1975 "cascade" {
79056034 1976 set submenu [string tolower [string map {& ""} [lindex $i 1]]]
f2d0bbbd
PM
1977 lappend params -menu $m.$submenu
1978 }
1979 "command" {
1980 lappend params -command $thing
1981 }
1982 "radiobutton" {
1983 lappend params -variable [lindex $thing 0] \
1984 -value [lindex $thing 1]
1985 }
1986 }
cea07cf8
AG
1987 set tail [lrange $i 4 end]
1988 regsub -all {\yMeta1\y} $tail $Meta1 tail
1989 eval $m add $params $tail
f2d0bbbd
PM
1990 if {$type eq "cascade"} {
1991 makemenu $m.$submenu $thing
1992 }
1993 }
1994}
1995
1996# translate string and remove ampersands
1997proc mca {str} {
1998 return [string map {&& & & {}} [mc $str]]
1999}
2000
d93f1713
PT
2001proc makedroplist {w varname args} {
2002 global use_ttk
2003 if {$use_ttk} {
3cb1f9c9
PT
2004 set width 0
2005 foreach label $args {
2006 set cx [string length $label]
2007 if {$cx > $width} {set width $cx}
2008 }
2009 set gm [ttk::combobox $w -width $width -state readonly\
d93f1713
PT
2010 -textvariable $varname -values $args]
2011 } else {
2012 set gm [eval [linsert $args 0 tk_optionMenu $w $varname]]
2013 }
2014 return $gm
2015}
2016
d94f8cd6 2017proc makewindow {} {
31c0eaa8 2018 global canv canv2 canv3 linespc charspc ctext cflist cscroll
9c311b32 2019 global tabstop
b74fd579 2020 global findtype findtypemenu findloc findstring fstring geometry
887fe3c4 2021 global entries sha1entry sha1string sha1but
890fae70 2022 global diffcontextstring diffcontext
b9b86007 2023 global ignorespace
94a2eede 2024 global maincursor textcursor curtextcursor
219ea3a9 2025 global rowctxmenu fakerowmenu mergemax wrapcomment
60f7a7dc 2026 global highlight_files gdttype
3ea06f9f 2027 global searchstring sstring
60378c0c 2028 global bgcolor fgcolor bglist fglist diffcolors selectbgcolor
bb3edc8b
PM
2029 global headctxmenu progresscanv progressitem progresscoords statusw
2030 global fprogitem fprogcoord lastprogupdate progupdatepending
6df7403a 2031 global rprogitem rprogcoord rownumsel numcommits
d93f1713 2032 global have_tk85 use_ttk NS
ae4e3ff9
TR
2033 global git_version
2034 global worddiff
9a40c50c 2035
79056034
PM
2036 # The "mc" arguments here are purely so that xgettext
2037 # sees the following string as needing to be translated
5fdcbb13
DS
2038 set file {
2039 mc "File" cascade {
79056034 2040 {mc "Update" command updatecommits -accelerator F5}
a135f214 2041 {mc "Reload" command reloadcommits -accelerator Shift-F5}
79056034 2042 {mc "Reread references" command rereadrefs}
cea07cf8 2043 {mc "List references" command showrefs -accelerator F2}
7fb0abb1
AG
2044 {xx "" separator}
2045 {mc "Start git gui" command {exec git gui &}}
2046 {xx "" separator}
cea07cf8 2047 {mc "Quit" command doquit -accelerator Meta1-Q}
f2d0bbbd 2048 }}
5fdcbb13
DS
2049 set edit {
2050 mc "Edit" cascade {
79056034 2051 {mc "Preferences" command doprefs}
f2d0bbbd 2052 }}
5fdcbb13
DS
2053 set view {
2054 mc "View" cascade {
cea07cf8
AG
2055 {mc "New view..." command {newview 0} -accelerator Shift-F4}
2056 {mc "Edit view..." command editview -state disabled -accelerator F4}
79056034
PM
2057 {mc "Delete view" command delview -state disabled}
2058 {xx "" separator}
2059 {mc "All files" radiobutton {selectedview 0} -command {showview 0}}
f2d0bbbd 2060 }}
5fdcbb13
DS
2061 if {[tk windowingsystem] ne "aqua"} {
2062 set help {
2063 mc "Help" cascade {
2064 {mc "About gitk" command about}
2065 {mc "Key bindings" command keys}
2066 }}
2067 set bar [list $file $edit $view $help]
2068 } else {
2069 proc ::tk::mac::ShowPreferences {} {doprefs}
2070 proc ::tk::mac::Quit {} {doquit}
2071 lset file end [lreplace [lindex $file end] end-1 end]
2072 set apple {
2073 xx "Apple" cascade {
79056034 2074 {mc "About gitk" command about}
5fdcbb13
DS
2075 {xx "" separator}
2076 }}
2077 set help {
2078 mc "Help" cascade {
79056034 2079 {mc "Key bindings" command keys}
f2d0bbbd 2080 }}
5fdcbb13 2081 set bar [list $apple $file $view $help]
f2d0bbbd 2082 }
5fdcbb13 2083 makemenu .bar $bar
9a40c50c
PM
2084 . configure -menu .bar
2085
d93f1713
PT
2086 if {$use_ttk} {
2087 # cover the non-themed toplevel with a themed frame.
2088 place [ttk::frame ._main_background] -x 0 -y 0 -relwidth 1 -relheight 1
2089 }
2090
e9937d2a 2091 # the gui has upper and lower half, parts of a paned window.
d93f1713 2092 ${NS}::panedwindow .ctop -orient vertical
e9937d2a
JH
2093
2094 # possibly use assumed geometry
9ca72f4f 2095 if {![info exists geometry(pwsash0)]} {
e9937d2a
JH
2096 set geometry(topheight) [expr {15 * $linespc}]
2097 set geometry(topwidth) [expr {80 * $charspc}]
2098 set geometry(botheight) [expr {15 * $linespc}]
2099 set geometry(botwidth) [expr {50 * $charspc}]
d93f1713
PT
2100 set geometry(pwsash0) [list [expr {40 * $charspc}] 2]
2101 set geometry(pwsash1) [list [expr {60 * $charspc}] 2]
e9937d2a
JH
2102 }
2103
2104 # the upper half will have a paned window, a scroll bar to the right, and some stuff below
d93f1713
PT
2105 ${NS}::frame .tf -height $geometry(topheight) -width $geometry(topwidth)
2106 ${NS}::frame .tf.histframe
2107 ${NS}::panedwindow .tf.histframe.pwclist -orient horizontal
2108 if {!$use_ttk} {
2109 .tf.histframe.pwclist configure -sashpad 0 -handlesize 4
2110 }
e9937d2a
JH
2111
2112 # create three canvases
2113 set cscroll .tf.histframe.csb
2114 set canv .tf.histframe.pwclist.canv
9ca72f4f 2115 canvas $canv \
60378c0c 2116 -selectbackground $selectbgcolor \
f8a2c0d1 2117 -background $bgcolor -bd 0 \
9f1afe05 2118 -yscrollincr $linespc -yscrollcommand "scrollcanv $cscroll"
e9937d2a
JH
2119 .tf.histframe.pwclist add $canv
2120 set canv2 .tf.histframe.pwclist.canv2
9ca72f4f 2121 canvas $canv2 \
60378c0c 2122 -selectbackground $selectbgcolor \
f8a2c0d1 2123 -background $bgcolor -bd 0 -yscrollincr $linespc
e9937d2a
JH
2124 .tf.histframe.pwclist add $canv2
2125 set canv3 .tf.histframe.pwclist.canv3
9ca72f4f 2126 canvas $canv3 \
60378c0c 2127 -selectbackground $selectbgcolor \
f8a2c0d1 2128 -background $bgcolor -bd 0 -yscrollincr $linespc
e9937d2a 2129 .tf.histframe.pwclist add $canv3
d93f1713
PT
2130 if {$use_ttk} {
2131 bind .tf.histframe.pwclist <Map> {
2132 bind %W <Map> {}
2133 .tf.histframe.pwclist sashpos 1 [lindex $::geometry(pwsash1) 0]
2134 .tf.histframe.pwclist sashpos 0 [lindex $::geometry(pwsash0) 0]
2135 }
2136 } else {
2137 eval .tf.histframe.pwclist sash place 0 $geometry(pwsash0)
2138 eval .tf.histframe.pwclist sash place 1 $geometry(pwsash1)
2139 }
e9937d2a
JH
2140
2141 # a scroll bar to rule them
d93f1713
PT
2142 ${NS}::scrollbar $cscroll -command {allcanvs yview}
2143 if {!$use_ttk} {$cscroll configure -highlightthickness 0}
e9937d2a
JH
2144 pack $cscroll -side right -fill y
2145 bind .tf.histframe.pwclist <Configure> {resizeclistpanes %W %w}
f8a2c0d1 2146 lappend bglist $canv $canv2 $canv3
e9937d2a 2147 pack .tf.histframe.pwclist -fill both -expand 1 -side left
98f350e5 2148
e9937d2a 2149 # we have two button bars at bottom of top frame. Bar 1
d93f1713
PT
2150 ${NS}::frame .tf.bar
2151 ${NS}::frame .tf.lbar -height 15
e9937d2a
JH
2152
2153 set sha1entry .tf.bar.sha1
887fe3c4 2154 set entries $sha1entry
e9937d2a 2155 set sha1but .tf.bar.sha1label
0359ba72 2156 button $sha1but -text "[mc "SHA1 ID:"] " -state disabled -relief flat \
b039f0a6 2157 -command gotocommit -width 8
887fe3c4 2158 $sha1but conf -disabledforeground [$sha1but cget -foreground]
e9937d2a 2159 pack .tf.bar.sha1label -side left
d93f1713 2160 ${NS}::entry $sha1entry -width 40 -font textfont -textvariable sha1string
887fe3c4 2161 trace add variable sha1string write sha1change
98f350e5 2162 pack $sha1entry -side left -pady 2
d698206c
PM
2163
2164 image create bitmap bm-left -data {
2165 #define left_width 16
2166 #define left_height 16
2167 static unsigned char left_bits[] = {
2168 0x00, 0x00, 0xc0, 0x01, 0xe0, 0x00, 0x70, 0x00, 0x38, 0x00, 0x1c, 0x00,
2169 0x0e, 0x00, 0xff, 0x7f, 0xff, 0x7f, 0xff, 0x7f, 0x0e, 0x00, 0x1c, 0x00,
2170 0x38, 0x00, 0x70, 0x00, 0xe0, 0x00, 0xc0, 0x01};
2171 }
2172 image create bitmap bm-right -data {
2173 #define right_width 16
2174 #define right_height 16
2175 static unsigned char right_bits[] = {
2176 0x00, 0x00, 0xc0, 0x01, 0x80, 0x03, 0x00, 0x07, 0x00, 0x0e, 0x00, 0x1c,
2177 0x00, 0x38, 0xff, 0x7f, 0xff, 0x7f, 0xff, 0x7f, 0x00, 0x38, 0x00, 0x1c,
2178 0x00, 0x0e, 0x00, 0x07, 0x80, 0x03, 0xc0, 0x01};
2179 }
d93f1713 2180 ${NS}::button .tf.bar.leftbut -image bm-left -command goback \
d698206c 2181 -state disabled -width 26
e9937d2a 2182 pack .tf.bar.leftbut -side left -fill y
d93f1713 2183 ${NS}::button .tf.bar.rightbut -image bm-right -command goforw \
d698206c 2184 -state disabled -width 26
e9937d2a 2185 pack .tf.bar.rightbut -side left -fill y
d698206c 2186
d93f1713 2187 ${NS}::label .tf.bar.rowlabel -text [mc "Row"]
6df7403a 2188 set rownumsel {}
d93f1713 2189 ${NS}::label .tf.bar.rownum -width 7 -textvariable rownumsel \
6df7403a 2190 -relief sunken -anchor e
d93f1713
PT
2191 ${NS}::label .tf.bar.rowlabel2 -text "/"
2192 ${NS}::label .tf.bar.numcommits -width 7 -textvariable numcommits \
6df7403a
PM
2193 -relief sunken -anchor e
2194 pack .tf.bar.rowlabel .tf.bar.rownum .tf.bar.rowlabel2 .tf.bar.numcommits \
2195 -side left
d93f1713
PT
2196 if {!$use_ttk} {
2197 foreach w {rownum numcommits} {.tf.bar.$w configure -font textfont}
2198 }
6df7403a 2199 global selectedline
94b4a69f 2200 trace add variable selectedline write selectedline_change
6df7403a 2201
bb3edc8b
PM
2202 # Status label and progress bar
2203 set statusw .tf.bar.status
d93f1713 2204 ${NS}::label $statusw -width 15 -relief sunken
bb3edc8b 2205 pack $statusw -side left -padx 5
d93f1713
PT
2206 if {$use_ttk} {
2207 set progresscanv [ttk::progressbar .tf.bar.progress]
2208 } else {
2209 set h [expr {[font metrics uifont -linespace] + 2}]
2210 set progresscanv .tf.bar.progress
2211 canvas $progresscanv -relief sunken -height $h -borderwidth 2
2212 set progressitem [$progresscanv create rect -1 0 0 $h -fill green]
2213 set fprogitem [$progresscanv create rect -1 0 0 $h -fill yellow]
2214 set rprogitem [$progresscanv create rect -1 0 0 $h -fill red]
2215 }
2216 pack $progresscanv -side right -expand 1 -fill x -padx {0 2}
bb3edc8b
PM
2217 set progresscoords {0 0}
2218 set fprogcoord 0
a137a90f 2219 set rprogcoord 0
bb3edc8b
PM
2220 bind $progresscanv <Configure> adjustprogress
2221 set lastprogupdate [clock clicks -milliseconds]
2222 set progupdatepending 0
2223
687c8765 2224 # build up the bottom bar of upper window
d93f1713
PT
2225 ${NS}::label .tf.lbar.flabel -text "[mc "Find"] "
2226 ${NS}::button .tf.lbar.fnext -text [mc "next"] -command {dofind 1 1}
2227 ${NS}::button .tf.lbar.fprev -text [mc "prev"] -command {dofind -1 1}
2228 ${NS}::label .tf.lbar.flab2 -text " [mc "commit"] "
687c8765
PM
2229 pack .tf.lbar.flabel .tf.lbar.fnext .tf.lbar.fprev .tf.lbar.flab2 \
2230 -side left -fill y
b007ee20 2231 set gdttype [mc "containing:"]
3cb1f9c9 2232 set gm [makedroplist .tf.lbar.gdttype gdttype \
b007ee20
CS
2233 [mc "containing:"] \
2234 [mc "touching paths:"] \
2235 [mc "adding/removing string:"]]
687c8765 2236 trace add variable gdttype write gdttype_change
687c8765
PM
2237 pack .tf.lbar.gdttype -side left -fill y
2238
98f350e5 2239 set findstring {}
687c8765 2240 set fstring .tf.lbar.findstring
887fe3c4 2241 lappend entries $fstring
b9b142ff 2242 ${NS}::entry $fstring -width 30 -textvariable findstring
60f7a7dc 2243 trace add variable findstring write find_change
b007ee20 2244 set findtype [mc "Exact"]
d93f1713
PT
2245 set findtypemenu [makedroplist .tf.lbar.findtype \
2246 findtype [mc "Exact"] [mc "IgnCase"] [mc "Regexp"]]
687c8765 2247 trace add variable findtype write findcom_change
b007ee20 2248 set findloc [mc "All fields"]
d93f1713 2249 makedroplist .tf.lbar.findloc findloc [mc "All fields"] [mc "Headline"] \
b007ee20 2250 [mc "Comments"] [mc "Author"] [mc "Committer"]
60f7a7dc 2251 trace add variable findloc write find_change
687c8765
PM
2252 pack .tf.lbar.findloc -side right
2253 pack .tf.lbar.findtype -side right
2254 pack $fstring -side left -expand 1 -fill x
e9937d2a
JH
2255
2256 # Finish putting the upper half of the viewer together
2257 pack .tf.lbar -in .tf -side bottom -fill x
2258 pack .tf.bar -in .tf -side bottom -fill x
2259 pack .tf.histframe -fill both -side top -expand 1
2260 .ctop add .tf
d93f1713
PT
2261 if {!$use_ttk} {
2262 .ctop paneconfigure .tf -height $geometry(topheight)
2263 .ctop paneconfigure .tf -width $geometry(topwidth)
2264 }
e9937d2a
JH
2265
2266 # now build up the bottom
d93f1713 2267 ${NS}::panedwindow .pwbottom -orient horizontal
e9937d2a
JH
2268
2269 # lower left, a text box over search bar, scroll bar to the right
2270 # if we know window height, then that will set the lower text height, otherwise
2271 # we set lower text height which will drive window height
2272 if {[info exists geometry(main)]} {
d93f1713 2273 ${NS}::frame .bleft -width $geometry(botwidth)
e9937d2a 2274 } else {
d93f1713 2275 ${NS}::frame .bleft -width $geometry(botwidth) -height $geometry(botheight)
e9937d2a 2276 }
d93f1713
PT
2277 ${NS}::frame .bleft.top
2278 ${NS}::frame .bleft.mid
2279 ${NS}::frame .bleft.bottom
e9937d2a 2280
d93f1713 2281 ${NS}::button .bleft.top.search -text [mc "Search"] -command dosearch
e9937d2a
JH
2282 pack .bleft.top.search -side left -padx 5
2283 set sstring .bleft.top.sstring
d93f1713 2284 set searchstring ""
b9b142ff 2285 ${NS}::entry $sstring -width 20 -textvariable searchstring
3ea06f9f
PM
2286 lappend entries $sstring
2287 trace add variable searchstring write incrsearch
2288 pack $sstring -side left -expand 1 -fill x
d93f1713 2289 ${NS}::radiobutton .bleft.mid.diff -text [mc "Diff"] \
a8d610a2 2290 -command changediffdisp -variable diffelide -value {0 0}
d93f1713 2291 ${NS}::radiobutton .bleft.mid.old -text [mc "Old version"] \
a8d610a2 2292 -command changediffdisp -variable diffelide -value {0 1}
d93f1713 2293 ${NS}::radiobutton .bleft.mid.new -text [mc "New version"] \
a8d610a2 2294 -command changediffdisp -variable diffelide -value {1 0}
d93f1713 2295 ${NS}::label .bleft.mid.labeldiffcontext -text " [mc "Lines of context"]: "
a8d610a2 2296 pack .bleft.mid.diff .bleft.mid.old .bleft.mid.new -side left
0933b04e 2297 spinbox .bleft.mid.diffcontext -width 5 \
a41ddbb6 2298 -from 0 -increment 1 -to 10000000 \
890fae70
SP
2299 -validate all -validatecommand "diffcontextvalidate %P" \
2300 -textvariable diffcontextstring
2301 .bleft.mid.diffcontext set $diffcontext
2302 trace add variable diffcontextstring write diffcontextchange
2303 lappend entries .bleft.mid.diffcontext
2304 pack .bleft.mid.labeldiffcontext .bleft.mid.diffcontext -side left
d93f1713 2305 ${NS}::checkbutton .bleft.mid.ignspace -text [mc "Ignore space change"] \
b9b86007
SP
2306 -command changeignorespace -variable ignorespace
2307 pack .bleft.mid.ignspace -side left -padx 5
ae4e3ff9
TR
2308
2309 set worddiff [mc "Line diff"]
2310 if {[package vcompare $git_version "1.7.2"] >= 0} {
2311 makedroplist .bleft.mid.worddiff worddiff [mc "Line diff"] \
2312 [mc "Markup words"] [mc "Color words"]
2313 trace add variable worddiff write changeworddiff
2314 pack .bleft.mid.worddiff -side left -padx 5
2315 }
2316
8809d691 2317 set ctext .bleft.bottom.ctext
f8a2c0d1 2318 text $ctext -background $bgcolor -foreground $fgcolor \
9c311b32 2319 -state disabled -font textfont \
8809d691
PK
2320 -yscrollcommand scrolltext -wrap none \
2321 -xscrollcommand ".bleft.bottom.sbhorizontal set"
32f1b3e4
PM
2322 if {$have_tk85} {
2323 $ctext conf -tabstyle wordprocessor
2324 }
d93f1713
PT
2325 ${NS}::scrollbar .bleft.bottom.sb -command "$ctext yview"
2326 ${NS}::scrollbar .bleft.bottom.sbhorizontal -command "$ctext xview" -orient h
e9937d2a 2327 pack .bleft.top -side top -fill x
a8d610a2 2328 pack .bleft.mid -side top -fill x
8809d691
PK
2329 grid $ctext .bleft.bottom.sb -sticky nsew
2330 grid .bleft.bottom.sbhorizontal -sticky ew
2331 grid columnconfigure .bleft.bottom 0 -weight 1
2332 grid rowconfigure .bleft.bottom 0 -weight 1
2333 grid rowconfigure .bleft.bottom 1 -weight 0
2334 pack .bleft.bottom -side top -fill both -expand 1
f8a2c0d1
PM
2335 lappend bglist $ctext
2336 lappend fglist $ctext
d2610d11 2337
f1b86294 2338 $ctext tag conf comment -wrap $wrapcomment
9c311b32 2339 $ctext tag conf filesep -font textfontbold -back "#aaaaaa"
f8a2c0d1
PM
2340 $ctext tag conf hunksep -fore [lindex $diffcolors 2]
2341 $ctext tag conf d0 -fore [lindex $diffcolors 0]
8b07dca1 2342 $ctext tag conf dresult -fore [lindex $diffcolors 1]
712fcc08
PM
2343 $ctext tag conf m0 -fore red
2344 $ctext tag conf m1 -fore blue
2345 $ctext tag conf m2 -fore green
2346 $ctext tag conf m3 -fore purple
2347 $ctext tag conf m4 -fore brown
b77b0278
PM
2348 $ctext tag conf m5 -fore "#009090"
2349 $ctext tag conf m6 -fore magenta
2350 $ctext tag conf m7 -fore "#808000"
2351 $ctext tag conf m8 -fore "#009000"
2352 $ctext tag conf m9 -fore "#ff0080"
2353 $ctext tag conf m10 -fore cyan
2354 $ctext tag conf m11 -fore "#b07070"
2355 $ctext tag conf m12 -fore "#70b0f0"
2356 $ctext tag conf m13 -fore "#70f0b0"
2357 $ctext tag conf m14 -fore "#f0b070"
2358 $ctext tag conf m15 -fore "#ff70b0"
712fcc08 2359 $ctext tag conf mmax -fore darkgrey
b77b0278 2360 set mergemax 16
9c311b32
PM
2361 $ctext tag conf mresult -font textfontbold
2362 $ctext tag conf msep -font textfontbold
712fcc08 2363 $ctext tag conf found -back yellow
e5c2d856 2364
e9937d2a 2365 .pwbottom add .bleft
d93f1713
PT
2366 if {!$use_ttk} {
2367 .pwbottom paneconfigure .bleft -width $geometry(botwidth)
2368 }
e9937d2a
JH
2369
2370 # lower right
d93f1713
PT
2371 ${NS}::frame .bright
2372 ${NS}::frame .bright.mode
2373 ${NS}::radiobutton .bright.mode.patch -text [mc "Patch"] \
f8b28a40 2374 -command reselectline -variable cmitmode -value "patch"
d93f1713 2375 ${NS}::radiobutton .bright.mode.tree -text [mc "Tree"] \
f8b28a40 2376 -command reselectline -variable cmitmode -value "tree"
e9937d2a
JH
2377 grid .bright.mode.patch .bright.mode.tree -sticky ew
2378 pack .bright.mode -side top -fill x
2379 set cflist .bright.cfiles
9c311b32 2380 set indent [font measure mainfont "nn"]
e9937d2a 2381 text $cflist \
60378c0c 2382 -selectbackground $selectbgcolor \
f8a2c0d1 2383 -background $bgcolor -foreground $fgcolor \
9c311b32 2384 -font mainfont \
7fcceed7 2385 -tabs [list $indent [expr {2 * $indent}]] \
e9937d2a 2386 -yscrollcommand ".bright.sb set" \
7fcceed7
PM
2387 -cursor [. cget -cursor] \
2388 -spacing1 1 -spacing3 1
f8a2c0d1
PM
2389 lappend bglist $cflist
2390 lappend fglist $cflist
d93f1713 2391 ${NS}::scrollbar .bright.sb -command "$cflist yview"
e9937d2a 2392 pack .bright.sb -side right -fill y
d2610d11 2393 pack $cflist -side left -fill both -expand 1
89b11d3b
PM
2394 $cflist tag configure highlight \
2395 -background [$cflist cget -selectbackground]
9c311b32 2396 $cflist tag configure bold -font mainfontbold
d2610d11 2397
e9937d2a
JH
2398 .pwbottom add .bright
2399 .ctop add .pwbottom
1db95b00 2400
b9bee115 2401 # restore window width & height if known
e9937d2a 2402 if {[info exists geometry(main)]} {
b9bee115
PM
2403 if {[scan $geometry(main) "%dx%d" w h] >= 2} {
2404 if {$w > [winfo screenwidth .]} {
2405 set w [winfo screenwidth .]
2406 }
2407 if {$h > [winfo screenheight .]} {
2408 set h [winfo screenheight .]
2409 }
2410 wm geometry . "${w}x$h"
2411 }
e9937d2a
JH
2412 }
2413
c876dbad
PT
2414 if {[info exists geometry(state)] && $geometry(state) eq "zoomed"} {
2415 wm state . $geometry(state)
2416 }
2417
d23d98d3
SP
2418 if {[tk windowingsystem] eq {aqua}} {
2419 set M1B M1
5fdcbb13 2420 set ::BM "3"
d23d98d3
SP
2421 } else {
2422 set M1B Control
5fdcbb13 2423 set ::BM "2"
d23d98d3
SP
2424 }
2425
d93f1713
PT
2426 if {$use_ttk} {
2427 bind .ctop <Map> {
2428 bind %W <Map> {}
2429 %W sashpos 0 $::geometry(topheight)
2430 }
2431 bind .pwbottom <Map> {
2432 bind %W <Map> {}
2433 %W sashpos 0 $::geometry(botwidth)
2434 }
2435 }
2436
e9937d2a
JH
2437 bind .pwbottom <Configure> {resizecdetpanes %W %w}
2438 pack .ctop -fill both -expand 1
c8dfbcf9
PM
2439 bindall <1> {selcanvline %W %x %y}
2440 #bindall <B1-Motion> {selcanvline %W %x %y}
314c3093
ML
2441 if {[tk windowingsystem] == "win32"} {
2442 bind . <MouseWheel> { windows_mousewheel_redirector %W %X %Y %D }
2443 bind $ctext <MouseWheel> { windows_mousewheel_redirector %W %X %Y %D ; break }
2444 } else {
2445 bindall <ButtonRelease-4> "allcanvs yview scroll -5 units"
2446 bindall <ButtonRelease-5> "allcanvs yview scroll 5 units"
5dd57d51
JS
2447 if {[tk windowingsystem] eq "aqua"} {
2448 bindall <MouseWheel> {
2449 set delta [expr {- (%D)}]
2450 allcanvs yview scroll $delta units
2451 }
5fdcbb13
DS
2452 bindall <Shift-MouseWheel> {
2453 set delta [expr {- (%D)}]
2454 $canv xview scroll $delta units
2455 }
5dd57d51 2456 }
314c3093 2457 }
5fdcbb13
DS
2458 bindall <$::BM> "canvscan mark %W %x %y"
2459 bindall <B$::BM-Motion> "canvscan dragto %W %x %y"
decd0a1e
JL
2460 bind all <$M1B-Key-w> {destroy [winfo toplevel %W]}
2461 bind . <$M1B-Key-w> doquit
6e5f7203
RN
2462 bindkey <Home> selfirstline
2463 bindkey <End> sellastline
17386066
PM
2464 bind . <Key-Up> "selnextline -1"
2465 bind . <Key-Down> "selnextline 1"
cca5d946
PM
2466 bind . <Shift-Key-Up> "dofind -1 0"
2467 bind . <Shift-Key-Down> "dofind 1 0"
6e5f7203
RN
2468 bindkey <Key-Right> "goforw"
2469 bindkey <Key-Left> "goback"
2470 bind . <Key-Prior> "selnextpage -1"
2471 bind . <Key-Next> "selnextpage 1"
d23d98d3
SP
2472 bind . <$M1B-Home> "allcanvs yview moveto 0.0"
2473 bind . <$M1B-End> "allcanvs yview moveto 1.0"
2474 bind . <$M1B-Key-Up> "allcanvs yview scroll -1 units"
2475 bind . <$M1B-Key-Down> "allcanvs yview scroll 1 units"
2476 bind . <$M1B-Key-Prior> "allcanvs yview scroll -1 pages"
2477 bind . <$M1B-Key-Next> "allcanvs yview scroll 1 pages"
cfb4563c
PM
2478 bindkey <Key-Delete> "$ctext yview scroll -1 pages"
2479 bindkey <Key-BackSpace> "$ctext yview scroll -1 pages"
2480 bindkey <Key-space> "$ctext yview scroll 1 pages"
df3d83b1
PM
2481 bindkey p "selnextline -1"
2482 bindkey n "selnextline 1"
6e2dda35
RS
2483 bindkey z "goback"
2484 bindkey x "goforw"
811c70fc
JN
2485 bindkey k "selnextline -1"
2486 bindkey j "selnextline 1"
2487 bindkey h "goback"
6e2dda35 2488 bindkey l "goforw"
f4c54b3c 2489 bindkey b prevfile
cfb4563c
PM
2490 bindkey d "$ctext yview scroll 18 units"
2491 bindkey u "$ctext yview scroll -18 units"
97bed034 2492 bindkey / {focus $fstring}
b6e192db 2493 bindkey <Key-KP_Divide> {focus $fstring}
cca5d946
PM
2494 bindkey <Key-Return> {dofind 1 1}
2495 bindkey ? {dofind -1 1}
39ad8570 2496 bindkey f nextfile
cea07cf8 2497 bind . <F5> updatecommits
a135f214 2498 bind . <Shift-F5> reloadcommits
cea07cf8
AG
2499 bind . <F2> showrefs
2500 bind . <Shift-F4> {newview 0}
2501 catch { bind . <Shift-Key-XF86_Switch_VT_4> {newview 0} }
2502 bind . <F4> edit_or_newview
d23d98d3 2503 bind . <$M1B-q> doquit
cca5d946
PM
2504 bind . <$M1B-f> {dofind 1 1}
2505 bind . <$M1B-g> {dofind 1 0}
d23d98d3
SP
2506 bind . <$M1B-r> dosearchback
2507 bind . <$M1B-s> dosearch
2508 bind . <$M1B-equal> {incrfont 1}
646f3a14 2509 bind . <$M1B-plus> {incrfont 1}
d23d98d3
SP
2510 bind . <$M1B-KP_Add> {incrfont 1}
2511 bind . <$M1B-minus> {incrfont -1}
2512 bind . <$M1B-KP_Subtract> {incrfont -1}
b6047c5a 2513 wm protocol . WM_DELETE_WINDOW doquit
e2f90ee4 2514 bind . <Destroy> {stop_backends}
df3d83b1 2515 bind . <Button-1> "click %W"
cca5d946 2516 bind $fstring <Key-Return> {dofind 1 1}
968ce45c 2517 bind $sha1entry <Key-Return> {gotocommit; break}
ee3dc72e 2518 bind $sha1entry <<PasteSelection>> clearsha1
7fcceed7
PM
2519 bind $cflist <1> {sel_flist %W %x %y; break}
2520 bind $cflist <B1-Motion> {sel_flist %W %x %y; break}
f8b28a40 2521 bind $cflist <ButtonRelease-1> {treeclick %W %x %y}
d277e89f
PM
2522 global ctxbut
2523 bind $cflist $ctxbut {pop_flist_menu %W %X %Y %x %y}
7cdc3556 2524 bind $ctext $ctxbut {pop_diff_menu %W %X %Y %x %y}
4adcbea0 2525 bind $ctext <Button-1> {focus %W}
ea13cba1
PM
2526
2527 set maincursor [. cget -cursor]
2528 set textcursor [$ctext cget -cursor]
94a2eede 2529 set curtextcursor $textcursor
84ba7345 2530
c8dfbcf9 2531 set rowctxmenu .rowctxmenu
f2d0bbbd 2532 makemenu $rowctxmenu {
79056034
PM
2533 {mc "Diff this -> selected" command {diffvssel 0}}
2534 {mc "Diff selected -> this" command {diffvssel 1}}
2535 {mc "Make patch" command mkpatch}
2536 {mc "Create tag" command mktag}
2537 {mc "Write commit to file" command writecommit}
2538 {mc "Create new branch" command mkbranch}
2539 {mc "Cherry-pick this commit" command cherrypick}
2540 {mc "Reset HEAD branch to here" command resethead}
b9fdba7f
PM
2541 {mc "Mark this commit" command markhere}
2542 {mc "Return to mark" command gotomark}
2543 {mc "Find descendant of this and mark" command find_common_desc}
010509f2 2544 {mc "Compare with marked commit" command compare_commits}
6febdede
PM
2545 {mc "Diff this -> marked commit" command {diffvsmark 0}}
2546 {mc "Diff marked commit -> this" command {diffvsmark 1}}
f2d0bbbd
PM
2547 }
2548 $rowctxmenu configure -tearoff 0
10299152 2549
219ea3a9 2550 set fakerowmenu .fakerowmenu
f2d0bbbd 2551 makemenu $fakerowmenu {
79056034
PM
2552 {mc "Diff this -> selected" command {diffvssel 0}}
2553 {mc "Diff selected -> this" command {diffvssel 1}}
2554 {mc "Make patch" command mkpatch}
6febdede
PM
2555 {mc "Diff this -> marked commit" command {diffvsmark 0}}
2556 {mc "Diff marked commit -> this" command {diffvsmark 1}}
f2d0bbbd
PM
2557 }
2558 $fakerowmenu configure -tearoff 0
219ea3a9 2559
10299152 2560 set headctxmenu .headctxmenu
f2d0bbbd 2561 makemenu $headctxmenu {
79056034
PM
2562 {mc "Check out this branch" command cobranch}
2563 {mc "Remove this branch" command rmbranch}
f2d0bbbd
PM
2564 }
2565 $headctxmenu configure -tearoff 0
3244729a
PM
2566
2567 global flist_menu
2568 set flist_menu .flistctxmenu
f2d0bbbd 2569 makemenu $flist_menu {
79056034
PM
2570 {mc "Highlight this too" command {flist_hl 0}}
2571 {mc "Highlight this only" command {flist_hl 1}}
2572 {mc "External diff" command {external_diff}}
2573 {mc "Blame parent commit" command {external_blame 1}}
f2d0bbbd
PM
2574 }
2575 $flist_menu configure -tearoff 0
7cdc3556
AG
2576
2577 global diff_menu
2578 set diff_menu .diffctxmenu
2579 makemenu $diff_menu {
8a897742 2580 {mc "Show origin of this line" command show_line_source}
7cdc3556
AG
2581 {mc "Run git gui blame on this line" command {external_blame_diff}}
2582 }
2583 $diff_menu configure -tearoff 0
df3d83b1
PM
2584}
2585
314c3093
ML
2586# Windows sends all mouse wheel events to the current focused window, not
2587# the one where the mouse hovers, so bind those events here and redirect
2588# to the correct window
2589proc windows_mousewheel_redirector {W X Y D} {
2590 global canv canv2 canv3
2591 set w [winfo containing -displayof $W $X $Y]
2592 if {$w ne ""} {
2593 set u [expr {$D < 0 ? 5 : -5}]
2594 if {$w == $canv || $w == $canv2 || $w == $canv3} {
2595 allcanvs yview scroll $u units
2596 } else {
2597 catch {
2598 $w yview scroll $u units
2599 }
2600 }
2601 }
2602}
2603
6df7403a
PM
2604# Update row number label when selectedline changes
2605proc selectedline_change {n1 n2 op} {
2606 global selectedline rownumsel
2607
94b4a69f 2608 if {$selectedline eq {}} {
6df7403a
PM
2609 set rownumsel {}
2610 } else {
2611 set rownumsel [expr {$selectedline + 1}]
2612 }
2613}
2614
be0cd098
PM
2615# mouse-2 makes all windows scan vertically, but only the one
2616# the cursor is in scans horizontally
2617proc canvscan {op w x y} {
2618 global canv canv2 canv3
2619 foreach c [list $canv $canv2 $canv3] {
2620 if {$c == $w} {
2621 $c scan $op $x $y
2622 } else {
2623 $c scan $op 0 $y
2624 }
2625 }
2626}
2627
9f1afe05
PM
2628proc scrollcanv {cscroll f0 f1} {
2629 $cscroll set $f0 $f1
31c0eaa8 2630 drawvisible
908c3585 2631 flushhighlights
9f1afe05
PM
2632}
2633
df3d83b1
PM
2634# when we make a key binding for the toplevel, make sure
2635# it doesn't get triggered when that key is pressed in the
2636# find string entry widget.
2637proc bindkey {ev script} {
887fe3c4 2638 global entries
df3d83b1
PM
2639 bind . $ev $script
2640 set escript [bind Entry $ev]
2641 if {$escript == {}} {
2642 set escript [bind Entry <Key>]
2643 }
887fe3c4
PM
2644 foreach e $entries {
2645 bind $e $ev "$escript; break"
2646 }
df3d83b1
PM
2647}
2648
2649# set the focus back to the toplevel for any click outside
887fe3c4 2650# the entry widgets
df3d83b1 2651proc click {w} {
bd441de4
ML
2652 global ctext entries
2653 foreach e [concat $entries $ctext] {
887fe3c4 2654 if {$w == $e} return
df3d83b1 2655 }
887fe3c4 2656 focus .
0fba86b3
PM
2657}
2658
bb3edc8b
PM
2659# Adjust the progress bar for a change in requested extent or canvas size
2660proc adjustprogress {} {
2661 global progresscanv progressitem progresscoords
2662 global fprogitem fprogcoord lastprogupdate progupdatepending
d93f1713
PT
2663 global rprogitem rprogcoord use_ttk
2664
2665 if {$use_ttk} {
2666 $progresscanv configure -value [expr {int($fprogcoord * 100)}]
2667 return
2668 }
bb3edc8b
PM
2669
2670 set w [expr {[winfo width $progresscanv] - 4}]
2671 set x0 [expr {$w * [lindex $progresscoords 0]}]
2672 set x1 [expr {$w * [lindex $progresscoords 1]}]
2673 set h [winfo height $progresscanv]
2674 $progresscanv coords $progressitem $x0 0 $x1 $h
2675 $progresscanv coords $fprogitem 0 0 [expr {$w * $fprogcoord}] $h
a137a90f 2676 $progresscanv coords $rprogitem 0 0 [expr {$w * $rprogcoord}] $h
bb3edc8b
PM
2677 set now [clock clicks -milliseconds]
2678 if {$now >= $lastprogupdate + 100} {
2679 set progupdatepending 0
2680 update
2681 } elseif {!$progupdatepending} {
2682 set progupdatepending 1
2683 after [expr {$lastprogupdate + 100 - $now}] doprogupdate
2684 }
2685}
2686
2687proc doprogupdate {} {
2688 global lastprogupdate progupdatepending
2689
2690 if {$progupdatepending} {
2691 set progupdatepending 0
2692 set lastprogupdate [clock clicks -milliseconds]
2693 update
2694 }
2695}
2696
0fba86b3 2697proc savestuff {w} {
32f1b3e4 2698 global canv canv2 canv3 mainfont textfont uifont tabstop
712fcc08 2699 global stuffsaved findmergefiles maxgraphpct
219ea3a9 2700 global maxwidth showneartags showlocalchanges
2d480856 2701 global viewname viewfiles viewargs viewargscmd viewperm nextviewnum
7a39a17a 2702 global cmitmode wrapcomment datetimeformat limitdiffs
5497f7a2 2703 global colors uicolor bgcolor fgcolor diffcolors diffcontext selectbgcolor
21ac8a8d 2704 global autoselect autosellen extdifftool perfile_attrs markbgcolor use_ttk
0cc08ff7 2705 global hideremotes want_ttk
4ef17537 2706
0fba86b3 2707 if {$stuffsaved} return
df3d83b1 2708 if {![winfo viewable .]} return
0fba86b3 2709 catch {
9bedb0e1 2710 if {[file exists ~/.gitk-new]} {file delete -force ~/.gitk-new}
0fba86b3 2711 set f [open "~/.gitk-new" w]
9832e4f2
PM
2712 if {$::tcl_platform(platform) eq {windows}} {
2713 file attributes "~/.gitk-new" -hidden true
2714 }
f0654861
PM
2715 puts $f [list set mainfont $mainfont]
2716 puts $f [list set textfont $textfont]
4840be66 2717 puts $f [list set uifont $uifont]
7e12f1a6 2718 puts $f [list set tabstop $tabstop]
f0654861 2719 puts $f [list set findmergefiles $findmergefiles]
8d858d1a 2720 puts $f [list set maxgraphpct $maxgraphpct]
04c13d38 2721 puts $f [list set maxwidth $maxwidth]
f8b28a40 2722 puts $f [list set cmitmode $cmitmode]
f1b86294 2723 puts $f [list set wrapcomment $wrapcomment]
95293b58 2724 puts $f [list set autoselect $autoselect]
21ac8a8d 2725 puts $f [list set autosellen $autosellen]
b8ab2e17 2726 puts $f [list set showneartags $showneartags]
ffe15297 2727 puts $f [list set hideremotes $hideremotes]
219ea3a9 2728 puts $f [list set showlocalchanges $showlocalchanges]
e8b5f4be 2729 puts $f [list set datetimeformat $datetimeformat]
7a39a17a 2730 puts $f [list set limitdiffs $limitdiffs]
5497f7a2 2731 puts $f [list set uicolor $uicolor]
0cc08ff7 2732 puts $f [list set want_ttk $want_ttk]
f8a2c0d1
PM
2733 puts $f [list set bgcolor $bgcolor]
2734 puts $f [list set fgcolor $fgcolor]
2735 puts $f [list set colors $colors]
2736 puts $f [list set diffcolors $diffcolors]
e3e901be 2737 puts $f [list set markbgcolor $markbgcolor]
890fae70 2738 puts $f [list set diffcontext $diffcontext]
60378c0c 2739 puts $f [list set selectbgcolor $selectbgcolor]
314f5de1 2740 puts $f [list set extdifftool $extdifftool]
39ee47ef 2741 puts $f [list set perfile_attrs $perfile_attrs]
e9937d2a 2742
b6047c5a 2743 puts $f "set geometry(main) [wm geometry .]"
c876dbad 2744 puts $f "set geometry(state) [wm state .]"
e9937d2a
JH
2745 puts $f "set geometry(topwidth) [winfo width .tf]"
2746 puts $f "set geometry(topheight) [winfo height .tf]"
d93f1713
PT
2747 if {$use_ttk} {
2748 puts $f "set geometry(pwsash0) \"[.tf.histframe.pwclist sashpos 0] 1\""
2749 puts $f "set geometry(pwsash1) \"[.tf.histframe.pwclist sashpos 1] 1\""
2750 } else {
2751 puts $f "set geometry(pwsash0) \"[.tf.histframe.pwclist sash coord 0]\""
2752 puts $f "set geometry(pwsash1) \"[.tf.histframe.pwclist sash coord 1]\""
2753 }
e9937d2a
JH
2754 puts $f "set geometry(botwidth) [winfo width .bleft]"
2755 puts $f "set geometry(botheight) [winfo height .bleft]"
2756
a90a6d24
PM
2757 puts -nonewline $f "set permviews {"
2758 for {set v 0} {$v < $nextviewnum} {incr v} {
2759 if {$viewperm($v)} {
2d480856 2760 puts $f "{[list $viewname($v) $viewfiles($v) $viewargs($v) $viewargscmd($v)]}"
a90a6d24
PM
2761 }
2762 }
2763 puts $f "}"
0fba86b3
PM
2764 close $f
2765 file rename -force "~/.gitk-new" "~/.gitk"
2766 }
2767 set stuffsaved 1
1db95b00
PM
2768}
2769
43bddeb4 2770proc resizeclistpanes {win w} {
d93f1713 2771 global oldwidth use_ttk
418c4c7b 2772 if {[info exists oldwidth($win)]} {
d93f1713
PT
2773 if {$use_ttk} {
2774 set s0 [$win sashpos 0]
2775 set s1 [$win sashpos 1]
2776 } else {
2777 set s0 [$win sash coord 0]
2778 set s1 [$win sash coord 1]
2779 }
43bddeb4
PM
2780 if {$w < 60} {
2781 set sash0 [expr {int($w/2 - 2)}]
2782 set sash1 [expr {int($w*5/6 - 2)}]
2783 } else {
2784 set factor [expr {1.0 * $w / $oldwidth($win)}]
2785 set sash0 [expr {int($factor * [lindex $s0 0])}]
2786 set sash1 [expr {int($factor * [lindex $s1 0])}]
2787 if {$sash0 < 30} {
2788 set sash0 30
2789 }
2790 if {$sash1 < $sash0 + 20} {
2ed49d54 2791 set sash1 [expr {$sash0 + 20}]
43bddeb4
PM
2792 }
2793 if {$sash1 > $w - 10} {
2ed49d54 2794 set sash1 [expr {$w - 10}]
43bddeb4 2795 if {$sash0 > $sash1 - 20} {
2ed49d54 2796 set sash0 [expr {$sash1 - 20}]
43bddeb4
PM
2797 }
2798 }
2799 }
d93f1713
PT
2800 if {$use_ttk} {
2801 $win sashpos 0 $sash0
2802 $win sashpos 1 $sash1
2803 } else {
2804 $win sash place 0 $sash0 [lindex $s0 1]
2805 $win sash place 1 $sash1 [lindex $s1 1]
2806 }
43bddeb4
PM
2807 }
2808 set oldwidth($win) $w
2809}
2810
2811proc resizecdetpanes {win w} {
d93f1713 2812 global oldwidth use_ttk
418c4c7b 2813 if {[info exists oldwidth($win)]} {
d93f1713
PT
2814 if {$use_ttk} {
2815 set s0 [$win sashpos 0]
2816 } else {
2817 set s0 [$win sash coord 0]
2818 }
43bddeb4
PM
2819 if {$w < 60} {
2820 set sash0 [expr {int($w*3/4 - 2)}]
2821 } else {
2822 set factor [expr {1.0 * $w / $oldwidth($win)}]
2823 set sash0 [expr {int($factor * [lindex $s0 0])}]
2824 if {$sash0 < 45} {
2825 set sash0 45
2826 }
2827 if {$sash0 > $w - 15} {
2ed49d54 2828 set sash0 [expr {$w - 15}]
43bddeb4
PM
2829 }
2830 }
d93f1713
PT
2831 if {$use_ttk} {
2832 $win sashpos 0 $sash0
2833 } else {
2834 $win sash place 0 $sash0 [lindex $s0 1]
2835 }
43bddeb4
PM
2836 }
2837 set oldwidth($win) $w
2838}
2839
b5721c72
PM
2840proc allcanvs args {
2841 global canv canv2 canv3
2842 eval $canv $args
2843 eval $canv2 $args
2844 eval $canv3 $args
2845}
2846
2847proc bindall {event action} {
2848 global canv canv2 canv3
2849 bind $canv $event $action
2850 bind $canv2 $event $action
2851 bind $canv3 $event $action
2852}
2853
9a40c50c 2854proc about {} {
d93f1713 2855 global uifont NS
9a40c50c
PM
2856 set w .about
2857 if {[winfo exists $w]} {
2858 raise $w
2859 return
2860 }
d93f1713 2861 ttk_toplevel $w
d990cedf 2862 wm title $w [mc "About gitk"]
e7d64008 2863 make_transient $w .
d990cedf 2864 message $w.m -text [mc "
9f1afe05 2865Gitk - a commit viewer for git
9a40c50c 2866
bb3e86a1 2867Copyright \u00a9 2005-2011 Paul Mackerras
9a40c50c 2868
d990cedf 2869Use and redistribute under the terms of the GNU General Public License"] \
3a950e9a
ER
2870 -justify center -aspect 400 -border 2 -bg white -relief groove
2871 pack $w.m -side top -fill x -padx 2 -pady 2
d93f1713 2872 ${NS}::button $w.ok -text [mc "Close"] -command "destroy $w" -default active
9a40c50c 2873 pack $w.ok -side bottom
3a950e9a
ER
2874 bind $w <Visibility> "focus $w.ok"
2875 bind $w <Key-Escape> "destroy $w"
2876 bind $w <Key-Return> "destroy $w"
d93f1713 2877 tk::PlaceWindow $w widget .
9a40c50c
PM
2878}
2879
4e95e1f7 2880proc keys {} {
d93f1713 2881 global NS
4e95e1f7
PM
2882 set w .keys
2883 if {[winfo exists $w]} {
2884 raise $w
2885 return
2886 }
d23d98d3
SP
2887 if {[tk windowingsystem] eq {aqua}} {
2888 set M1T Cmd
2889 } else {
2890 set M1T Ctrl
2891 }
d93f1713 2892 ttk_toplevel $w
d990cedf 2893 wm title $w [mc "Gitk key bindings"]
e7d64008 2894 make_transient $w .
3d2c998e
MB
2895 message $w.m -text "
2896[mc "Gitk key bindings:"]
2897
2898[mc "<%s-Q> Quit" $M1T]
decd0a1e 2899[mc "<%s-W> Close window" $M1T]
3d2c998e
MB
2900[mc "<Home> Move to first commit"]
2901[mc "<End> Move to last commit"]
811c70fc
JN
2902[mc "<Up>, p, k Move up one commit"]
2903[mc "<Down>, n, j Move down one commit"]
2904[mc "<Left>, z, h Go back in history list"]
3d2c998e
MB
2905[mc "<Right>, x, l Go forward in history list"]
2906[mc "<PageUp> Move up one page in commit list"]
2907[mc "<PageDown> Move down one page in commit list"]
2908[mc "<%s-Home> Scroll to top of commit list" $M1T]
2909[mc "<%s-End> Scroll to bottom of commit list" $M1T]
2910[mc "<%s-Up> Scroll commit list up one line" $M1T]
2911[mc "<%s-Down> Scroll commit list down one line" $M1T]
2912[mc "<%s-PageUp> Scroll commit list up one page" $M1T]
2913[mc "<%s-PageDown> Scroll commit list down one page" $M1T]
2914[mc "<Shift-Up> Find backwards (upwards, later commits)"]
2915[mc "<Shift-Down> Find forwards (downwards, earlier commits)"]
2916[mc "<Delete>, b Scroll diff view up one page"]
2917[mc "<Backspace> Scroll diff view up one page"]
2918[mc "<Space> Scroll diff view down one page"]
2919[mc "u Scroll diff view up 18 lines"]
2920[mc "d Scroll diff view down 18 lines"]
2921[mc "<%s-F> Find" $M1T]
2922[mc "<%s-G> Move to next find hit" $M1T]
2923[mc "<Return> Move to next find hit"]
97bed034 2924[mc "/ Focus the search box"]
3d2c998e
MB
2925[mc "? Move to previous find hit"]
2926[mc "f Scroll diff view to next file"]
2927[mc "<%s-S> Search for next hit in diff view" $M1T]
2928[mc "<%s-R> Search for previous hit in diff view" $M1T]
2929[mc "<%s-KP+> Increase font size" $M1T]
2930[mc "<%s-plus> Increase font size" $M1T]
2931[mc "<%s-KP-> Decrease font size" $M1T]
2932[mc "<%s-minus> Decrease font size" $M1T]
2933[mc "<F5> Update"]
2934" \
3a950e9a
ER
2935 -justify left -bg white -border 2 -relief groove
2936 pack $w.m -side top -fill both -padx 2 -pady 2
d93f1713 2937 ${NS}::button $w.ok -text [mc "Close"] -command "destroy $w" -default active
76f15947 2938 bind $w <Key-Escape> [list destroy $w]
4e95e1f7 2939 pack $w.ok -side bottom
3a950e9a
ER
2940 bind $w <Visibility> "focus $w.ok"
2941 bind $w <Key-Escape> "destroy $w"
2942 bind $w <Key-Return> "destroy $w"
4e95e1f7
PM
2943}
2944
7fcceed7
PM
2945# Procedures for manipulating the file list window at the
2946# bottom right of the overall window.
f8b28a40
PM
2947
2948proc treeview {w l openlevs} {
2949 global treecontents treediropen treeheight treeparent treeindex
2950
2951 set ix 0
2952 set treeindex() 0
2953 set lev 0
2954 set prefix {}
2955 set prefixend -1
2956 set prefendstack {}
2957 set htstack {}
2958 set ht 0
2959 set treecontents() {}
2960 $w conf -state normal
2961 foreach f $l {
2962 while {[string range $f 0 $prefixend] ne $prefix} {
2963 if {$lev <= $openlevs} {
2964 $w mark set e:$treeindex($prefix) "end -1c"
2965 $w mark gravity e:$treeindex($prefix) left
2966 }
2967 set treeheight($prefix) $ht
2968 incr ht [lindex $htstack end]
2969 set htstack [lreplace $htstack end end]
2970 set prefixend [lindex $prefendstack end]
2971 set prefendstack [lreplace $prefendstack end end]
2972 set prefix [string range $prefix 0 $prefixend]
2973 incr lev -1
2974 }
2975 set tail [string range $f [expr {$prefixend+1}] end]
2976 while {[set slash [string first "/" $tail]] >= 0} {
2977 lappend htstack $ht
2978 set ht 0
2979 lappend prefendstack $prefixend
2980 incr prefixend [expr {$slash + 1}]
2981 set d [string range $tail 0 $slash]
2982 lappend treecontents($prefix) $d
2983 set oldprefix $prefix
2984 append prefix $d
2985 set treecontents($prefix) {}
2986 set treeindex($prefix) [incr ix]
2987 set treeparent($prefix) $oldprefix
2988 set tail [string range $tail [expr {$slash+1}] end]
2989 if {$lev <= $openlevs} {
2990 set ht 1
2991 set treediropen($prefix) [expr {$lev < $openlevs}]
2992 set bm [expr {$lev == $openlevs? "tri-rt": "tri-dn"}]
2993 $w mark set d:$ix "end -1c"
2994 $w mark gravity d:$ix left
2995 set str "\n"
2996 for {set i 0} {$i < $lev} {incr i} {append str "\t"}
2997 $w insert end $str
2998 $w image create end -align center -image $bm -padx 1 \
2999 -name a:$ix
45a9d505 3000 $w insert end $d [highlight_tag $prefix]
f8b28a40
PM
3001 $w mark set s:$ix "end -1c"
3002 $w mark gravity s:$ix left
3003 }
3004 incr lev
3005 }
3006 if {$tail ne {}} {
3007 if {$lev <= $openlevs} {
3008 incr ht
3009 set str "\n"
3010 for {set i 0} {$i < $lev} {incr i} {append str "\t"}
3011 $w insert end $str
45a9d505 3012 $w insert end $tail [highlight_tag $f]
f8b28a40
PM
3013 }
3014 lappend treecontents($prefix) $tail
3015 }
3016 }
3017 while {$htstack ne {}} {
3018 set treeheight($prefix) $ht
3019 incr ht [lindex $htstack end]
3020 set htstack [lreplace $htstack end end]
096e96b4
BD
3021 set prefixend [lindex $prefendstack end]
3022 set prefendstack [lreplace $prefendstack end end]
3023 set prefix [string range $prefix 0 $prefixend]
f8b28a40
PM
3024 }
3025 $w conf -state disabled
3026}
3027
3028proc linetoelt {l} {
3029 global treeheight treecontents
3030
3031 set y 2
3032 set prefix {}
3033 while {1} {
3034 foreach e $treecontents($prefix) {
3035 if {$y == $l} {
3036 return "$prefix$e"
3037 }
3038 set n 1
3039 if {[string index $e end] eq "/"} {
3040 set n $treeheight($prefix$e)
3041 if {$y + $n > $l} {
3042 append prefix $e
3043 incr y
3044 break
3045 }
3046 }
3047 incr y $n
3048 }
3049 }
3050}
3051
45a9d505
PM
3052proc highlight_tree {y prefix} {
3053 global treeheight treecontents cflist
3054
3055 foreach e $treecontents($prefix) {
3056 set path $prefix$e
3057 if {[highlight_tag $path] ne {}} {
3058 $cflist tag add bold $y.0 "$y.0 lineend"
3059 }
3060 incr y
3061 if {[string index $e end] eq "/" && $treeheight($path) > 1} {
3062 set y [highlight_tree $y $path]
3063 }
3064 }
3065 return $y
3066}
3067
f8b28a40
PM
3068proc treeclosedir {w dir} {
3069 global treediropen treeheight treeparent treeindex
3070
3071 set ix $treeindex($dir)
3072 $w conf -state normal
3073 $w delete s:$ix e:$ix
3074 set treediropen($dir) 0
3075 $w image configure a:$ix -image tri-rt
3076 $w conf -state disabled
3077 set n [expr {1 - $treeheight($dir)}]
3078 while {$dir ne {}} {
3079 incr treeheight($dir) $n
3080 set dir $treeparent($dir)
3081 }
3082}
3083
3084proc treeopendir {w dir} {
3085 global treediropen treeheight treeparent treecontents treeindex
3086
3087 set ix $treeindex($dir)
3088 $w conf -state normal
3089 $w image configure a:$ix -image tri-dn
3090 $w mark set e:$ix s:$ix
3091 $w mark gravity e:$ix right
3092 set lev 0
3093 set str "\n"
3094 set n [llength $treecontents($dir)]
3095 for {set x $dir} {$x ne {}} {set x $treeparent($x)} {
3096 incr lev
3097 append str "\t"
3098 incr treeheight($x) $n
3099 }
3100 foreach e $treecontents($dir) {
45a9d505 3101 set de $dir$e
f8b28a40 3102 if {[string index $e end] eq "/"} {
f8b28a40
PM
3103 set iy $treeindex($de)
3104 $w mark set d:$iy e:$ix
3105 $w mark gravity d:$iy left
3106 $w insert e:$ix $str
3107 set treediropen($de) 0
3108 $w image create e:$ix -align center -image tri-rt -padx 1 \
3109 -name a:$iy
45a9d505 3110 $w insert e:$ix $e [highlight_tag $de]
f8b28a40
PM
3111 $w mark set s:$iy e:$ix
3112 $w mark gravity s:$iy left
3113 set treeheight($de) 1
3114 } else {
3115 $w insert e:$ix $str
45a9d505 3116 $w insert e:$ix $e [highlight_tag $de]
f8b28a40
PM
3117 }
3118 }
b8a640ee 3119 $w mark gravity e:$ix right
f8b28a40
PM
3120 $w conf -state disabled
3121 set treediropen($dir) 1
3122 set top [lindex [split [$w index @0,0] .] 0]
3123 set ht [$w cget -height]
3124 set l [lindex [split [$w index s:$ix] .] 0]
3125 if {$l < $top} {
3126 $w yview $l.0
3127 } elseif {$l + $n + 1 > $top + $ht} {
3128 set top [expr {$l + $n + 2 - $ht}]
3129 if {$l < $top} {
3130 set top $l
3131 }
3132 $w yview $top.0
3133 }
3134}
3135
3136proc treeclick {w x y} {
3137 global treediropen cmitmode ctext cflist cflist_top
3138
3139 if {$cmitmode ne "tree"} return
3140 if {![info exists cflist_top]} return
3141 set l [lindex [split [$w index "@$x,$y"] "."] 0]
3142 $cflist tag remove highlight $cflist_top.0 "$cflist_top.0 lineend"
3143 $cflist tag add highlight $l.0 "$l.0 lineend"
3144 set cflist_top $l
3145 if {$l == 1} {
3146 $ctext yview 1.0
3147 return
3148 }
3149 set e [linetoelt $l]
3150 if {[string index $e end] ne "/"} {
3151 showfile $e
3152 } elseif {$treediropen($e)} {
3153 treeclosedir $w $e
3154 } else {
3155 treeopendir $w $e
3156 }
3157}
3158
3159proc setfilelist {id} {
8a897742 3160 global treefilelist cflist jump_to_here
f8b28a40
PM
3161
3162 treeview $cflist $treefilelist($id) 0
8a897742
PM
3163 if {$jump_to_here ne {}} {
3164 set f [lindex $jump_to_here 0]
3165 if {[lsearch -exact $treefilelist($id) $f] >= 0} {
3166 showfile $f
3167 }
3168 }
f8b28a40
PM
3169}
3170
3171image create bitmap tri-rt -background black -foreground blue -data {
3172 #define tri-rt_width 13
3173 #define tri-rt_height 13
3174 static unsigned char tri-rt_bits[] = {
3175 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x30, 0x00, 0x70, 0x00, 0xf0, 0x00,
3176 0xf0, 0x01, 0xf0, 0x00, 0x70, 0x00, 0x30, 0x00, 0x10, 0x00, 0x00, 0x00,
3177 0x00, 0x00};
3178} -maskdata {
3179 #define tri-rt-mask_width 13
3180 #define tri-rt-mask_height 13
3181 static unsigned char tri-rt-mask_bits[] = {
3182 0x08, 0x00, 0x18, 0x00, 0x38, 0x00, 0x78, 0x00, 0xf8, 0x00, 0xf8, 0x01,
3183 0xf8, 0x03, 0xf8, 0x01, 0xf8, 0x00, 0x78, 0x00, 0x38, 0x00, 0x18, 0x00,
3184 0x08, 0x00};
3185}
3186image create bitmap tri-dn -background black -foreground blue -data {
3187 #define tri-dn_width 13
3188 #define tri-dn_height 13
3189 static unsigned char tri-dn_bits[] = {
3190 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0x07, 0xf8, 0x03,
3191 0xf0, 0x01, 0xe0, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3192 0x00, 0x00};
3193} -maskdata {
3194 #define tri-dn-mask_width 13
3195 #define tri-dn-mask_height 13
3196 static unsigned char tri-dn-mask_bits[] = {
3197 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x1f, 0xfe, 0x0f, 0xfc, 0x07,
3198 0xf8, 0x03, 0xf0, 0x01, 0xe0, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00,
3199 0x00, 0x00};
3200}
3201
887c996e
PM
3202image create bitmap reficon-T -background black -foreground yellow -data {
3203 #define tagicon_width 13
3204 #define tagicon_height 9
3205 static unsigned char tagicon_bits[] = {
3206 0x00, 0x00, 0x00, 0x00, 0xf0, 0x07, 0xf8, 0x07,
3207 0xfc, 0x07, 0xf8, 0x07, 0xf0, 0x07, 0x00, 0x00, 0x00, 0x00};
3208} -maskdata {
3209 #define tagicon-mask_width 13
3210 #define tagicon-mask_height 9
3211 static unsigned char tagicon-mask_bits[] = {
3212 0x00, 0x00, 0xf0, 0x0f, 0xf8, 0x0f, 0xfc, 0x0f,
3213 0xfe, 0x0f, 0xfc, 0x0f, 0xf8, 0x0f, 0xf0, 0x0f, 0x00, 0x00};
3214}
3215set rectdata {
3216 #define headicon_width 13
3217 #define headicon_height 9
3218 static unsigned char headicon_bits[] = {
3219 0x00, 0x00, 0x00, 0x00, 0xf8, 0x07, 0xf8, 0x07,
3220 0xf8, 0x07, 0xf8, 0x07, 0xf8, 0x07, 0x00, 0x00, 0x00, 0x00};
3221}
3222set rectmask {
3223 #define headicon-mask_width 13
3224 #define headicon-mask_height 9
3225 static unsigned char headicon-mask_bits[] = {
3226 0x00, 0x00, 0xfc, 0x0f, 0xfc, 0x0f, 0xfc, 0x0f,
3227 0xfc, 0x0f, 0xfc, 0x0f, 0xfc, 0x0f, 0xfc, 0x0f, 0x00, 0x00};
3228}
3229image create bitmap reficon-H -background black -foreground green \
3230 -data $rectdata -maskdata $rectmask
3231image create bitmap reficon-o -background black -foreground "#ddddff" \
3232 -data $rectdata -maskdata $rectmask
3233
7fcceed7 3234proc init_flist {first} {
7fcc92bf 3235 global cflist cflist_top difffilestart
7fcceed7
PM
3236
3237 $cflist conf -state normal
3238 $cflist delete 0.0 end
3239 if {$first ne {}} {
3240 $cflist insert end $first
3241 set cflist_top 1
7fcceed7
PM
3242 $cflist tag add highlight 1.0 "1.0 lineend"
3243 } else {
3244 catch {unset cflist_top}
3245 }
3246 $cflist conf -state disabled
3247 set difffilestart {}
3248}
3249
63b79191
PM
3250proc highlight_tag {f} {
3251 global highlight_paths
3252
3253 foreach p $highlight_paths {
3254 if {[string match $p $f]} {
3255 return "bold"
3256 }
3257 }
3258 return {}
3259}
3260
3261proc highlight_filelist {} {
45a9d505 3262 global cmitmode cflist
63b79191 3263
45a9d505
PM
3264 $cflist conf -state normal
3265 if {$cmitmode ne "tree"} {
63b79191
PM
3266 set end [lindex [split [$cflist index end] .] 0]
3267 for {set l 2} {$l < $end} {incr l} {
3268 set line [$cflist get $l.0 "$l.0 lineend"]
3269 if {[highlight_tag $line] ne {}} {
3270 $cflist tag add bold $l.0 "$l.0 lineend"
3271 }
3272 }
45a9d505
PM
3273 } else {
3274 highlight_tree 2 {}
63b79191 3275 }
45a9d505 3276 $cflist conf -state disabled
63b79191
PM
3277}
3278
3279proc unhighlight_filelist {} {
45a9d505 3280 global cflist
63b79191 3281
45a9d505
PM
3282 $cflist conf -state normal
3283 $cflist tag remove bold 1.0 end
3284 $cflist conf -state disabled
63b79191
PM
3285}
3286
f8b28a40 3287proc add_flist {fl} {
45a9d505 3288 global cflist
7fcceed7 3289
45a9d505
PM
3290 $cflist conf -state normal
3291 foreach f $fl {
3292 $cflist insert end "\n"
3293 $cflist insert end $f [highlight_tag $f]
7fcceed7 3294 }
45a9d505 3295 $cflist conf -state disabled
7fcceed7
PM
3296}
3297
3298proc sel_flist {w x y} {
45a9d505 3299 global ctext difffilestart cflist cflist_top cmitmode
7fcceed7 3300
f8b28a40 3301 if {$cmitmode eq "tree"} return
7fcceed7
PM
3302 if {![info exists cflist_top]} return
3303 set l [lindex [split [$w index "@$x,$y"] "."] 0]
89b11d3b
PM
3304 $cflist tag remove highlight $cflist_top.0 "$cflist_top.0 lineend"
3305 $cflist tag add highlight $l.0 "$l.0 lineend"
3306 set cflist_top $l
f8b28a40
PM
3307 if {$l == 1} {
3308 $ctext yview 1.0
3309 } else {
3310 catch {$ctext yview [lindex $difffilestart [expr {$l - 2}]]}
7fcceed7 3311 }
7fcceed7
PM
3312}
3313
3244729a
PM
3314proc pop_flist_menu {w X Y x y} {
3315 global ctext cflist cmitmode flist_menu flist_menu_file
3316 global treediffs diffids
3317
bb3edc8b 3318 stopfinding
3244729a
PM
3319 set l [lindex [split [$w index "@$x,$y"] "."] 0]
3320 if {$l <= 1} return
3321 if {$cmitmode eq "tree"} {
3322 set e [linetoelt $l]
3323 if {[string index $e end] eq "/"} return
3324 } else {
3325 set e [lindex $treediffs($diffids) [expr {$l-2}]]
3326 }
3327 set flist_menu_file $e
314f5de1
TA
3328 set xdiffstate "normal"
3329 if {$cmitmode eq "tree"} {
3330 set xdiffstate "disabled"
3331 }
3332 # Disable "External diff" item in tree mode
3333 $flist_menu entryconf 2 -state $xdiffstate
3244729a
PM
3334 tk_popup $flist_menu $X $Y
3335}
3336
7cdc3556
AG
3337proc find_ctext_fileinfo {line} {
3338 global ctext_file_names ctext_file_lines
3339
3340 set ok [bsearch $ctext_file_lines $line]
3341 set tline [lindex $ctext_file_lines $ok]
3342
3343 if {$ok >= [llength $ctext_file_lines] || $line < $tline} {
3344 return {}
3345 } else {
3346 return [list [lindex $ctext_file_names $ok] $tline]
3347 }
3348}
3349
3350proc pop_diff_menu {w X Y x y} {
3351 global ctext diff_menu flist_menu_file
3352 global diff_menu_txtpos diff_menu_line
3353 global diff_menu_filebase
3354
7cdc3556
AG
3355 set diff_menu_txtpos [split [$w index "@$x,$y"] "."]
3356 set diff_menu_line [lindex $diff_menu_txtpos 0]
190ec52c
PM
3357 # don't pop up the menu on hunk-separator or file-separator lines
3358 if {[lsearch -glob [$ctext tag names $diff_menu_line.0] "*sep"] >= 0} {
3359 return
3360 }
3361 stopfinding
7cdc3556
AG
3362 set f [find_ctext_fileinfo $diff_menu_line]
3363 if {$f eq {}} return
3364 set flist_menu_file [lindex $f 0]
3365 set diff_menu_filebase [lindex $f 1]
3366 tk_popup $diff_menu $X $Y
3367}
3368
3244729a 3369proc flist_hl {only} {
bb3edc8b 3370 global flist_menu_file findstring gdttype
3244729a
PM
3371
3372 set x [shellquote $flist_menu_file]
b007ee20 3373 if {$only || $findstring eq {} || $gdttype ne [mc "touching paths:"]} {
bb3edc8b 3374 set findstring $x
3244729a 3375 } else {
bb3edc8b 3376 append findstring " " $x
3244729a 3377 }
b007ee20 3378 set gdttype [mc "touching paths:"]
3244729a
PM
3379}
3380
c21398be
PM
3381proc gitknewtmpdir {} {
3382 global diffnum gitktmpdir gitdir
3383
3384 if {![info exists gitktmpdir]} {
929f577e 3385 set gitktmpdir [file join $gitdir [format ".gitk-tmp.%s" [pid]]]
c21398be
PM
3386 if {[catch {file mkdir $gitktmpdir} err]} {
3387 error_popup "[mc "Error creating temporary directory %s:" $gitktmpdir] $err"
3388 unset gitktmpdir
3389 return {}
3390 }
3391 set diffnum 0
3392 }
3393 incr diffnum
3394 set diffdir [file join $gitktmpdir $diffnum]
3395 if {[catch {file mkdir $diffdir} err]} {
3396 error_popup "[mc "Error creating temporary directory %s:" $diffdir] $err"
3397 return {}
3398 }
3399 return $diffdir
3400}
3401
314f5de1
TA
3402proc save_file_from_commit {filename output what} {
3403 global nullfile
3404
3405 if {[catch {exec git show $filename -- > $output} err]} {
3406 if {[string match "fatal: bad revision *" $err]} {
3407 return $nullfile
3408 }
3945d2c0 3409 error_popup "[mc "Error getting \"%s\" from %s:" $filename $what] $err"
314f5de1
TA
3410 return {}
3411 }
3412 return $output
3413}
3414
3415proc external_diff_get_one_file {diffid filename diffdir} {
3416 global nullid nullid2 nullfile
784b7e2f 3417 global worktree
314f5de1
TA
3418
3419 if {$diffid == $nullid} {
784b7e2f 3420 set difffile [file join $worktree $filename]
314f5de1
TA
3421 if {[file exists $difffile]} {
3422 return $difffile
3423 }
3424 return $nullfile
3425 }
3426 if {$diffid == $nullid2} {
3427 set difffile [file join $diffdir "\[index\] [file tail $filename]"]
3428 return [save_file_from_commit :$filename $difffile index]
3429 }
3430 set difffile [file join $diffdir "\[$diffid\] [file tail $filename]"]
3431 return [save_file_from_commit $diffid:$filename $difffile \
3432 "revision $diffid"]
3433}
3434
3435proc external_diff {} {
c21398be 3436 global nullid nullid2
314f5de1
TA
3437 global flist_menu_file
3438 global diffids
c21398be 3439 global extdifftool
314f5de1
TA
3440
3441 if {[llength $diffids] == 1} {
3442 # no reference commit given
3443 set diffidto [lindex $diffids 0]
3444 if {$diffidto eq $nullid} {
3445 # diffing working copy with index
3446 set diffidfrom $nullid2
3447 } elseif {$diffidto eq $nullid2} {
3448 # diffing index with HEAD
3449 set diffidfrom "HEAD"
3450 } else {
3451 # use first parent commit
3452 global parentlist selectedline
3453 set diffidfrom [lindex $parentlist $selectedline 0]
3454 }
3455 } else {
3456 set diffidfrom [lindex $diffids 0]
3457 set diffidto [lindex $diffids 1]
3458 }
3459
3460 # make sure that several diffs wont collide
c21398be
PM
3461 set diffdir [gitknewtmpdir]
3462 if {$diffdir eq {}} return
314f5de1
TA
3463
3464 # gather files to diff
3465 set difffromfile [external_diff_get_one_file $diffidfrom $flist_menu_file $diffdir]
3466 set difftofile [external_diff_get_one_file $diffidto $flist_menu_file $diffdir]
3467
3468 if {$difffromfile ne {} && $difftofile ne {}} {
b575b2f1
PT
3469 set cmd [list [shellsplit $extdifftool] $difffromfile $difftofile]
3470 if {[catch {set fl [open |$cmd r]} err]} {
314f5de1 3471 file delete -force $diffdir
3945d2c0 3472 error_popup "$extdifftool: [mc "command failed:"] $err"
314f5de1
TA
3473 } else {
3474 fconfigure $fl -blocking 0
3475 filerun $fl [list delete_at_eof $fl $diffdir]
3476 }
3477 }
3478}
3479
7cdc3556
AG
3480proc find_hunk_blamespec {base line} {
3481 global ctext
3482
3483 # Find and parse the hunk header
3484 set s_lix [$ctext search -backwards -regexp ^@@ "$line.0 lineend" $base.0]
3485 if {$s_lix eq {}} return
3486
3487 set s_line [$ctext get $s_lix "$s_lix + 1 lines"]
3488 if {![regexp {^@@@*(( -\d+(,\d+)?)+) \+(\d+)(,\d+)? @@} $s_line \
3489 s_line old_specs osz osz1 new_line nsz]} {
3490 return
3491 }
3492
3493 # base lines for the parents
3494 set base_lines [list $new_line]
3495 foreach old_spec [lrange [split $old_specs " "] 1 end] {
3496 if {![regexp -- {-(\d+)(,\d+)?} $old_spec \
3497 old_spec old_line osz]} {
3498 return
3499 }
3500 lappend base_lines $old_line
3501 }
3502
3503 # Now scan the lines to determine offset within the hunk
7cdc3556
AG
3504 set max_parent [expr {[llength $base_lines]-2}]
3505 set dline 0
3506 set s_lno [lindex [split $s_lix "."] 0]
3507
190ec52c
PM
3508 # Determine if the line is removed
3509 set chunk [$ctext get $line.0 "$line.1 + $max_parent chars"]
3510 if {[string match {[-+ ]*} $chunk]} {
7cdc3556
AG
3511 set removed_idx [string first "-" $chunk]
3512 # Choose a parent index
190ec52c
PM
3513 if {$removed_idx >= 0} {
3514 set parent $removed_idx
3515 } else {
3516 set unchanged_idx [string first " " $chunk]
3517 if {$unchanged_idx >= 0} {
3518 set parent $unchanged_idx
7cdc3556 3519 } else {
190ec52c
PM
3520 # blame the current commit
3521 set parent -1
7cdc3556
AG
3522 }
3523 }
3524 # then count other lines that belong to it
190ec52c
PM
3525 for {set i $line} {[incr i -1] > $s_lno} {} {
3526 set chunk [$ctext get $i.0 "$i.1 + $max_parent chars"]
3527 # Determine if the line is removed
3528 set removed_idx [string first "-" $chunk]
3529 if {$parent >= 0} {
3530 set code [string index $chunk $parent]
3531 if {$code eq "-" || ($removed_idx < 0 && $code ne "+")} {
3532 incr dline
3533 }
3534 } else {
3535 if {$removed_idx < 0} {
3536 incr dline
3537 }
7cdc3556
AG
3538 }
3539 }
190ec52c
PM
3540 incr parent
3541 } else {
3542 set parent 0
7cdc3556
AG
3543 }
3544
7cdc3556
AG
3545 incr dline [lindex $base_lines $parent]
3546 return [list $parent $dline]
3547}
3548
3549proc external_blame_diff {} {
8b07dca1 3550 global currentid cmitmode
7cdc3556
AG
3551 global diff_menu_txtpos diff_menu_line
3552 global diff_menu_filebase flist_menu_file
3553
3554 if {$cmitmode eq "tree"} {
3555 set parent_idx 0
190ec52c 3556 set line [expr {$diff_menu_line - $diff_menu_filebase}]
7cdc3556
AG
3557 } else {
3558 set hinfo [find_hunk_blamespec $diff_menu_filebase $diff_menu_line]
3559 if {$hinfo ne {}} {
3560 set parent_idx [lindex $hinfo 0]
3561 set line [lindex $hinfo 1]
3562 } else {
3563 set parent_idx 0
3564 set line 0
3565 }
3566 }
3567
3568 external_blame $parent_idx $line
3569}
3570
fc4977e1
PM
3571# Find the SHA1 ID of the blob for file $fname in the index
3572# at stage 0 or 2
3573proc index_sha1 {fname} {
3574 set f [open [list | git ls-files -s $fname] r]
3575 while {[gets $f line] >= 0} {
3576 set info [lindex [split $line "\t"] 0]
3577 set stage [lindex $info 2]
3578 if {$stage eq "0" || $stage eq "2"} {
3579 close $f
3580 return [lindex $info 1]
3581 }
3582 }
3583 close $f
3584 return {}
3585}
3586
9712b81a
PM
3587# Turn an absolute path into one relative to the current directory
3588proc make_relative {f} {
a4390ace
MH
3589 if {[file pathtype $f] eq "relative"} {
3590 return $f
3591 }
9712b81a
PM
3592 set elts [file split $f]
3593 set here [file split [pwd]]
3594 set ei 0
3595 set hi 0
3596 set res {}
3597 foreach d $here {
3598 if {$ei < $hi || $ei >= [llength $elts] || [lindex $elts $ei] ne $d} {
3599 lappend res ".."
3600 } else {
3601 incr ei
3602 }
3603 incr hi
3604 }
3605 set elts [concat $res [lrange $elts $ei end]]
3606 return [eval file join $elts]
3607}
3608
7cdc3556 3609proc external_blame {parent_idx {line {}}} {
0a2a9793 3610 global flist_menu_file cdup
77aa0ae8
AG
3611 global nullid nullid2
3612 global parentlist selectedline currentid
3613
3614 if {$parent_idx > 0} {
3615 set base_commit [lindex $parentlist $selectedline [expr {$parent_idx-1}]]
3616 } else {
3617 set base_commit $currentid
3618 }
3619
3620 if {$base_commit eq {} || $base_commit eq $nullid || $base_commit eq $nullid2} {
3621 error_popup [mc "No such commit"]
3622 return
3623 }
3624
7cdc3556
AG
3625 set cmdline [list git gui blame]
3626 if {$line ne {} && $line > 1} {
3627 lappend cmdline "--line=$line"
3628 }
0a2a9793 3629 set f [file join $cdup $flist_menu_file]
9712b81a
PM
3630 # Unfortunately it seems git gui blame doesn't like
3631 # being given an absolute path...
3632 set f [make_relative $f]
3633 lappend cmdline $base_commit $f
7cdc3556 3634 if {[catch {eval exec $cmdline &} err]} {
3945d2c0 3635 error_popup "[mc "git gui blame: command failed:"] $err"
77aa0ae8
AG
3636 }
3637}
3638
8a897742
PM
3639proc show_line_source {} {
3640 global cmitmode currentid parents curview blamestuff blameinst
3641 global diff_menu_line diff_menu_filebase flist_menu_file
9b6adf34 3642 global nullid nullid2 gitdir cdup
8a897742 3643
fc4977e1 3644 set from_index {}
8a897742
PM
3645 if {$cmitmode eq "tree"} {
3646 set id $currentid
3647 set line [expr {$diff_menu_line - $diff_menu_filebase}]
3648 } else {
3649 set h [find_hunk_blamespec $diff_menu_filebase $diff_menu_line]
3650 if {$h eq {}} return
3651 set pi [lindex $h 0]
3652 if {$pi == 0} {
3653 mark_ctext_line $diff_menu_line
3654 return
3655 }
fc4977e1
PM
3656 incr pi -1
3657 if {$currentid eq $nullid} {
3658 if {$pi > 0} {
3659 # must be a merge in progress...
3660 if {[catch {
3661 # get the last line from .git/MERGE_HEAD
3662 set f [open [file join $gitdir MERGE_HEAD] r]
3663 set id [lindex [split [read $f] "\n"] end-1]
3664 close $f
3665 } err]} {
3666 error_popup [mc "Couldn't read merge head: %s" $err]
3667 return
3668 }
3669 } elseif {$parents($curview,$currentid) eq $nullid2} {
3670 # need to do the blame from the index
3671 if {[catch {
3672 set from_index [index_sha1 $flist_menu_file]
3673 } err]} {
3674 error_popup [mc "Error reading index: %s" $err]
3675 return
3676 }
9712b81a
PM
3677 } else {
3678 set id $parents($curview,$currentid)
fc4977e1
PM
3679 }
3680 } else {
3681 set id [lindex $parents($curview,$currentid) $pi]
3682 }
8a897742
PM
3683 set line [lindex $h 1]
3684 }
fc4977e1
PM
3685 set blameargs {}
3686 if {$from_index ne {}} {
3687 lappend blameargs | git cat-file blob $from_index
3688 }
3689 lappend blameargs | git blame -p -L$line,+1
3690 if {$from_index ne {}} {
3691 lappend blameargs --contents -
3692 } else {
3693 lappend blameargs $id
3694 }
9b6adf34 3695 lappend blameargs -- [file join $cdup $flist_menu_file]
8a897742 3696 if {[catch {
fc4977e1 3697 set f [open $blameargs r]
8a897742
PM
3698 } err]} {
3699 error_popup [mc "Couldn't start git blame: %s" $err]
3700 return
3701 }
f3413079 3702 nowbusy blaming [mc "Searching"]
8a897742
PM
3703 fconfigure $f -blocking 0
3704 set i [reg_instance $f]
3705 set blamestuff($i) {}
3706 set blameinst $i
3707 filerun $f [list read_line_source $f $i]
3708}
3709
3710proc stopblaming {} {
3711 global blameinst
3712
3713 if {[info exists blameinst]} {
3714 stop_instance $blameinst
3715 unset blameinst
f3413079 3716 notbusy blaming
8a897742
PM
3717 }
3718}
3719
3720proc read_line_source {fd inst} {
fc4977e1 3721 global blamestuff curview commfd blameinst nullid nullid2
8a897742
PM
3722
3723 while {[gets $fd line] >= 0} {
3724 lappend blamestuff($inst) $line
3725 }
3726 if {![eof $fd]} {
3727 return 1
3728 }
3729 unset commfd($inst)
3730 unset blameinst
f3413079 3731 notbusy blaming
8a897742
PM
3732 fconfigure $fd -blocking 1
3733 if {[catch {close $fd} err]} {
3734 error_popup [mc "Error running git blame: %s" $err]
3735 return 0
3736 }
3737
3738 set fname {}
3739 set line [split [lindex $blamestuff($inst) 0] " "]
3740 set id [lindex $line 0]
3741 set lnum [lindex $line 1]
3742 if {[string length $id] == 40 && [string is xdigit $id] &&
3743 [string is digit -strict $lnum]} {
3744 # look for "filename" line
3745 foreach l $blamestuff($inst) {
3746 if {[string match "filename *" $l]} {
3747 set fname [string range $l 9 end]
3748 break
3749 }
3750 }
3751 }
3752 if {$fname ne {}} {
3753 # all looks good, select it
fc4977e1
PM
3754 if {$id eq $nullid} {
3755 # blame uses all-zeroes to mean not committed,
3756 # which would mean a change in the index
3757 set id $nullid2
3758 }
8a897742
PM
3759 if {[commitinview $id $curview]} {
3760 selectline [rowofcommit $id] 1 [list $fname $lnum]
3761 } else {
3762 error_popup [mc "That line comes from commit %s, \
3763 which is not in this view" [shortids $id]]
3764 }
3765 } else {
3766 puts "oops couldn't parse git blame output"
3767 }