]> git.ipfire.org Git - thirdparty/git.git/blame - git-gui/git-gui.sh
Merge branch 'maint-1.6.2' into maint-1.6.3
[thirdparty/git.git] / git-gui / git-gui.sh
CommitLineData
bd11b82d 1#!/bin/sh
cb07fc2a 2# Tcl ignores the next line -*- tcl -*- \
4e817d1a
SP
3 if test "z$*" = zversion \
4 || test "z$*" = z--version; \
5 then \
6 echo 'git-gui version @@GITGUI_VERSION@@'; \
7 exit; \
8 fi; \
2f7c9a7f
SP
9 argv0=$0; \
10 exec wish "$argv0" -- "$@"
cb07fc2a 11
7e81d4ee 12set appvers {@@GITGUI_VERSION@@}
d6db1ad5 13set copyright [encoding convertfrom utf-8 {
a9813cb5 14Copyright © 2006, 2007 Shawn Pearce, et. al.
bdc9ea20 15
0499b24a
SP
16This program is free software; you can redistribute it and/or modify
17it under the terms of the GNU General Public License as published by
18the Free Software Foundation; either version 2 of the License, or
19(at your option) any later version.
20
21This program is distributed in the hope that it will be useful,
22but WITHOUT ANY WARRANTY; without even the implied warranty of
23MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24GNU General Public License for more details.
25
26You should have received a copy of the GNU General Public License
27along with this program; if not, write to the Free Software
d6db1ad5 28Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA}]
cb07fc2a 29
f522c9b5 30######################################################################
cfb07cca
SP
31##
32## Tcl/Tk sanity check
33
34if {[catch {package require Tcl 8.4} err]
35 || [catch {package require Tk 8.4} err]
36} {
37 catch {wm withdraw .}
38 tk_messageBox \
39 -icon error \
40 -type ok \
c8c4854b 41 -title [mc "git-gui: fatal error"] \
cfb07cca
SP
42 -message $err
43 exit 1
44}
45
63c4024f 46catch {rename send {}} ; # What an evil concept...
cff93397 47
fc703c20
SP
48######################################################################
49##
50## locate our library
51
52set oguilib {@@GITGUI_LIBDIR@@}
53set oguirel {@@GITGUI_RELATIVE@@}
54if {$oguirel eq {1}} {
9534c9fb
JS
55 set oguilib [file dirname [file normalize $argv0]]
56 if {[file tail $oguilib] eq {git-core}} {
57 set oguilib [file dirname $oguilib]
58 }
59 set oguilib [file dirname $oguilib]
fc703c20 60 set oguilib [file join $oguilib share git-gui lib]
d4b0ccd9 61 set oguimsg [file join $oguilib msgs]
fc703c20
SP
62} elseif {[string match @@* $oguirel]} {
63 set oguilib [file join [file dirname [file normalize $argv0]] lib]
d4b0ccd9
SP
64 set oguimsg [file join [file dirname [file normalize $argv0]] po]
65} else {
66 set oguimsg [file join $oguilib msgs]
fc703c20
SP
67}
68unset oguirel
69
cd12901b
SP
70######################################################################
71##
72## enable verbose loading?
73
74if {![catch {set _verbose $env(GITGUI_VERBOSE)}]} {
75 unset _verbose
76 rename auto_load real__auto_load
77 proc auto_load {name args} {
78 puts stderr "auto_load $name"
79 return [uplevel 1 real__auto_load $name $args]
80 }
81 rename source real__source
82 proc source {name} {
83 puts stderr "source $name"
84 uplevel 1 real__source $name
85 }
86}
87
c950c66e 88######################################################################
d4b0ccd9
SP
89##
90## Internationalization (i18n) through msgcat and gettext. See
91## http://www.gnu.org/software/gettext/manual/html_node/Tcl.html
92
93package require msgcat
146d73a3 94
ab0d33c4 95proc _mc_trim {fmt} {
146d73a3
SP
96 set cmk [string first @@ $fmt]
97 if {$cmk > 0} {
ab0d33c4 98 return [string range $fmt 0 [expr {$cmk - 1}]]
146d73a3 99 }
ab0d33c4
SP
100 return $fmt
101}
102
103proc mc {en_fmt args} {
104 set fmt [_mc_trim [::msgcat::mc $en_fmt]]
105 if {[catch {set msg [eval [list format $fmt] $args]} err]} {
106 set msg [eval [list format [_mc_trim $en_fmt]] $args]
107 }
108 return $msg
146d73a3
SP
109}
110
31bb1d1b
SP
111proc strcat {args} {
112 return [join $args {}]
113}
114
d4b0ccd9
SP
115::msgcat::mcload $oguimsg
116unset oguimsg
117
118######################################################################
c950c66e
SP
119##
120## read only globals
121
0b2bc460 122set _appname {Git Gui}
c950c66e 123set _gitdir {}
20ddfcaa 124set _gitexec {}
3eb5682b 125set _githtmldir {}
c950c66e 126set _reponame {}
20ddfcaa 127set _iscygwin {}
0b812616 128set _search_path {}
c950c66e 129
16dd62ac
SP
130set _trace [lsearch -exact $argv --trace]
131if {$_trace >= 0} {
132 set argv [lreplace $argv $_trace $_trace]
133 set _trace 1
134} else {
135 set _trace 0
136}
137
c950c66e
SP
138proc appname {} {
139 global _appname
140 return $_appname
141}
142
c2758a17 143proc gitdir {args} {
c950c66e 144 global _gitdir
c2758a17
SP
145 if {$args eq {}} {
146 return $_gitdir
147 }
0b812616 148 return [eval [list file join $_gitdir] $args]
c950c66e
SP
149}
150
20ddfcaa
SP
151proc gitexec {args} {
152 global _gitexec
153 if {$_gitexec eq {}} {
81347223 154 if {[catch {set _gitexec [git --exec-path]} err]} {
20ddfcaa
SP
155 error "Git not installed?\n\n$err"
156 }
0b812616
SP
157 if {[is_Cygwin]} {
158 set _gitexec [exec cygpath \
159 --windows \
160 --absolute \
161 $_gitexec]
162 } else {
163 set _gitexec [file normalize $_gitexec]
164 }
20ddfcaa
SP
165 }
166 if {$args eq {}} {
167 return $_gitexec
168 }
0b812616 169 return [eval [list file join $_gitexec] $args]
20ddfcaa
SP
170}
171
3eb5682b
MH
172proc githtmldir {args} {
173 global _githtmldir
174 if {$_githtmldir eq {}} {
175 if {[catch {set _githtmldir [git --html-path]}]} {
176 # Git not installed or option not yet supported
177 return {}
178 }
179 if {[is_Cygwin]} {
180 set _githtmldir [exec cygpath \
181 --windows \
182 --absolute \
183 $_githtmldir]
184 } else {
185 set _githtmldir [file normalize $_githtmldir]
186 }
187 }
188 if {$args eq {}} {
189 return $_githtmldir
190 }
191 return [eval [list file join $_githtmldir] $args]
192}
193
c950c66e 194proc reponame {} {
d36cd968 195 return $::_reponame
c950c66e 196}
da5239dc 197
20ddfcaa 198proc is_MacOSX {} {
20ddfcaa
SP
199 if {[tk windowingsystem] eq {aqua}} {
200 return 1
201 }
202 return 0
203}
204
205proc is_Windows {} {
d36cd968 206 if {$::tcl_platform(platform) eq {windows}} {
20ddfcaa
SP
207 return 1
208 }
209 return 0
210}
211
212proc is_Cygwin {} {
d36cd968 213 global _iscygwin
20ddfcaa 214 if {$_iscygwin eq {}} {
d36cd968 215 if {$::tcl_platform(platform) eq {windows}} {
20ddfcaa
SP
216 if {[catch {set p [exec cygpath --windir]} err]} {
217 set _iscygwin 0
218 } else {
219 set _iscygwin 1
220 }
221 } else {
222 set _iscygwin 0
223 }
224 }
225 return $_iscygwin
226}
227
cf25ddc8
SP
228proc is_enabled {option} {
229 global enabled_options
230 if {[catch {set on $enabled_options($option)}]} {return 0}
231 return $on
232}
233
234proc enable_option {option} {
235 global enabled_options
236 set enabled_options($option) 1
237}
238
239proc disable_option {option} {
240 global enabled_options
241 set enabled_options($option) 0
242}
243
2d19516d
SP
244######################################################################
245##
246## config
247
51f4d16b
SP
248proc is_many_config {name} {
249 switch -glob -- $name {
24f7c64b 250 gui.recentrepo -
51f4d16b
SP
251 remote.*.fetch -
252 remote.*.push
253 {return 1}
254 *
255 {return 0}
256 }
257}
2d19516d 258
c539449b
SP
259proc is_config_true {name} {
260 global repo_config
261 if {[catch {set v $repo_config($name)}]} {
262 return 0
263 } elseif {$v eq {true} || $v eq {1} || $v eq {yes}} {
264 return 1
265 } else {
266 return 0
267 }
268}
269
61f82ce7
SP
270proc get_config {name} {
271 global repo_config
272 if {[catch {set v $repo_config($name)}]} {
273 return {}
274 } else {
275 return $v
276 }
277}
278
81347223
SP
279######################################################################
280##
281## handy utils
282
16dd62ac
SP
283proc _trace_exec {cmd} {
284 if {!$::_trace} return
285 set d {}
286 foreach v $cmd {
287 if {$d ne {}} {
288 append d { }
289 }
290 if {[regexp {[ \t\r\n'"$?*]} $v]} {
291 set v [sq $v]
292 }
293 append d $v
294 }
295 puts stderr $d
296}
297
0b812616
SP
298proc _git_cmd {name} {
299 global _git_cmd_path
300
301 if {[catch {set v $_git_cmd_path($name)}]} {
302 switch -- $name {
70a7595c 303 version -
0b812616
SP
304 --version -
305 --exec-path { return [list $::_git $name] }
306 }
307
308 set p [gitexec git-$name$::_search_exe]
309 if {[file exists $p]} {
310 set v [list $p]
c136f2b8
SP
311 } elseif {[is_Windows] && [file exists [gitexec git-$name]]} {
312 # Try to determine what sort of magic will make
313 # git-$name go and do its thing, because native
314 # Tcl on Windows doesn't know it.
0b812616 315 #
c136f2b8
SP
316 set p [gitexec git-$name]
317 set f [open $p r]
318 set s [gets $f]
319 close $f
320
6e4ba05c 321 switch -glob -- [lindex $s 0] {
c136f2b8
SP
322 #!*sh { set i sh }
323 #!*perl { set i perl }
324 #!*python { set i python }
325 default { error "git-$name is not supported: $s" }
326 }
327
328 upvar #0 _$i interp
329 if {![info exists interp]} {
330 set interp [_which $i]
331 }
332 if {$interp eq {}} {
333 error "git-$name requires $i (not in PATH)"
334 }
6e4ba05c 335 set v [concat [list $interp] [lrange $s 1 end] [list $p]]
0b812616 336 } else {
c6729890
SP
337 # Assume it is builtin to git somehow and we
338 # aren't actually able to see a file for it.
339 #
340 set v [list $::_git $name]
0b812616
SP
341 }
342 set _git_cmd_path($name) $v
343 }
344 return $v
345}
346
79317e5d 347proc _which {what args} {
0b812616
SP
348 global env _search_exe _search_path
349
350 if {$_search_path eq {}} {
299077fb 351 if {[is_Cygwin] && [regexp {^(/|\.:)} $env(PATH)]} {
0b812616
SP
352 set _search_path [split [exec cygpath \
353 --windows \
354 --path \
355 --absolute \
356 $env(PATH)] {;}]
357 set _search_exe .exe
358 } elseif {[is_Windows]} {
be700fe3
SP
359 set gitguidir [file dirname [info script]]
360 regsub -all ";" $gitguidir "\\;" gitguidir
361 set env(PATH) "$gitguidir;$env(PATH)"
0b812616
SP
362 set _search_path [split $env(PATH) {;}]
363 set _search_exe .exe
364 } else {
365 set _search_path [split $env(PATH) :]
366 set _search_exe {}
367 }
368 }
369
79317e5d
SP
370 if {[is_Windows] && [lsearch -exact $args -script] >= 0} {
371 set suffix {}
372 } else {
373 set suffix $_search_exe
374 }
375
0b812616 376 foreach p $_search_path {
79317e5d 377 set p [file join $p $what$suffix]
0b812616
SP
378 if {[file exists $p]} {
379 return [file normalize $p]
380 }
381 }
382 return {}
383}
384
6f62b4f7
SP
385proc _lappend_nice {cmd_var} {
386 global _nice
387 upvar $cmd_var cmd
388
389 if {![info exists _nice]} {
390 set _nice [_which nice]
391 }
392 if {$_nice ne {}} {
393 lappend cmd $_nice
394 }
395}
396
81347223 397proc git {args} {
16dd62ac 398 set opt [list]
0b812616
SP
399
400 while {1} {
401 switch -- [lindex $args 0] {
402 --nice {
6f62b4f7 403 _lappend_nice opt
0b812616
SP
404 }
405
406 default {
407 break
408 }
409
410 }
411
412 set args [lrange $args 1 end]
413 }
414
415 set cmdp [_git_cmd [lindex $args 0]]
416 set args [lrange $args 1 end]
417
16dd62ac
SP
418 _trace_exec [concat $opt $cmdp $args]
419 set result [eval exec $opt $cmdp $args]
420 if {$::_trace} {
421 puts stderr "< $result"
422 }
423 return $result
0b812616
SP
424}
425
74c4763c 426proc _open_stdout_stderr {cmd} {
16dd62ac 427 _trace_exec $cmd
74c4763c 428 if {[catch {
16dd62ac 429 set fd [open [concat [list | ] $cmd] r]
74c4763c
SP
430 } err]} {
431 if { [lindex $cmd end] eq {2>@1}
432 && $err eq {can not find channel named "1"}
433 } {
434 # Older versions of Tcl 8.4 don't have this 2>@1 IO
435 # redirect operator. Fallback to |& cat for those.
436 # The command was not actually started, so its safe
437 # to try to start it a second time.
438 #
439 set fd [open [concat \
16dd62ac 440 [list | ] \
74c4763c
SP
441 [lrange $cmd 0 end-1] \
442 [list |& cat] \
443 ] r]
444 } else {
445 error $err
446 }
447 }
6eb420ef 448 fconfigure $fd -eofchar {}
74c4763c
SP
449 return $fd
450}
451
0b812616 452proc git_read {args} {
16dd62ac 453 set opt [list]
0b812616
SP
454
455 while {1} {
456 switch -- [lindex $args 0] {
457 --nice {
6f62b4f7 458 _lappend_nice opt
0b812616
SP
459 }
460
461 --stderr {
462 lappend args 2>@1
463 }
464
465 default {
466 break
467 }
468
469 }
470
471 set args [lrange $args 1 end]
472 }
473
474 set cmdp [_git_cmd [lindex $args 0]]
475 set args [lrange $args 1 end]
476
74c4763c 477 return [_open_stdout_stderr [concat $opt $cmdp $args]]
0b812616
SP
478}
479
480proc git_write {args} {
16dd62ac 481 set opt [list]
0b812616
SP
482
483 while {1} {
484 switch -- [lindex $args 0] {
485 --nice {
6f62b4f7 486 _lappend_nice opt
0b812616
SP
487 }
488
489 default {
490 break
491 }
492
493 }
494
495 set args [lrange $args 1 end]
496 }
497
498 set cmdp [_git_cmd [lindex $args 0]]
499 set args [lrange $args 1 end]
500
16dd62ac
SP
501 _trace_exec [concat $opt $cmdp $args]
502 return [open [concat [list | ] $opt $cmdp $args] w]
81347223
SP
503}
504
ed76cb70
SP
505proc githook_read {hook_name args} {
506 set pchook [gitdir hooks $hook_name]
507 lappend args 2>@1
508
fbc0e7ac 509 # On Windows [file executable] might lie so we need to ask
ed76cb70
SP
510 # the shell if the hook is executable. Yes that's annoying.
511 #
fbc0e7ac 512 if {[is_Windows]} {
ed76cb70
SP
513 upvar #0 _sh interp
514 if {![info exists interp]} {
515 set interp [_which sh]
516 }
517 if {$interp eq {}} {
518 error "hook execution requires sh (not in PATH)"
519 }
520
521 set scr {if test -x "$1";then exec "$@";fi}
16dd62ac 522 set sh_c [list $interp -c $scr $interp $pchook]
ed76cb70
SP
523 return [_open_stdout_stderr [concat $sh_c $args]]
524 }
525
526 if {[file executable $pchook]} {
16dd62ac 527 return [_open_stdout_stderr [concat [list $pchook] $args]]
ed76cb70
SP
528 }
529
530 return {}
531}
532
e6131d30
AG
533proc kill_file_process {fd} {
534 set process [pid $fd]
535
536 catch {
537 if {[is_Windows]} {
538 # Use a Cygwin-specific flag to allow killing
539 # native Windows processes
540 exec kill -f $process
541 } else {
542 exec kill $process
543 }
544 }
545}
546
1ffca60f
SP
547proc gitattr {path attr default} {
548 if {[catch {set r [git check-attr $attr -- $path]}]} {
549 set r unspecified
550 } else {
551 set r [join [lrange [split $r :] 2 end] :]
552 regsub {^ } $r {} r
553 }
554 if {$r eq {unspecified}} {
555 return $default
556 }
557 return $r
558}
559
7eafa2f1
SP
560proc sq {value} {
561 regsub -all ' $value "'\\''" value
562 return "'$value'"
563}
564
d41b43eb
SP
565proc load_current_branch {} {
566 global current_branch is_detached
567
fc4e8da7 568 set fd [open [gitdir HEAD] r]
311e02a4 569 if {[gets $fd ref] < 1} {
fc4e8da7
SP
570 set ref {}
571 }
572 close $fd
311e02a4
SP
573
574 set pfx {ref: refs/heads/}
575 set len [string length $pfx]
576 if {[string equal -length $len $pfx $ref]} {
577 # We're on a branch. It might not exist. But
578 # HEAD looks good enough to be a branch.
579 #
d41b43eb
SP
580 set current_branch [string range $ref $len end]
581 set is_detached 0
311e02a4
SP
582 } else {
583 # Assume this is a detached head.
584 #
d41b43eb
SP
585 set current_branch HEAD
586 set is_detached 1
311e02a4 587 }
fc4e8da7
SP
588}
589
2739291b
SP
590auto_load tk_optionMenu
591rename tk_optionMenu real__tkOptionMenu
592proc tk_optionMenu {w varName args} {
593 set m [eval real__tkOptionMenu $w $varName $args]
594 $m configure -font font_ui
595 $w configure -font font_ui
596 return $m
597}
598
3849bfba
SP
599proc rmsel_tag {text} {
600 $text tag conf sel \
601 -background [$text cget -background] \
602 -foreground [$text cget -foreground] \
603 -borderwidth 0
604 $text tag conf in_sel -background lightgray
605 bind $text <Motion> break
606 return $text
607}
608
a4bee597
SP
609set root_exists 0
610bind . <Visibility> {
611 bind . <Visibility> {}
612 set root_exists 1
613}
614
1bdd8a15
SP
615if {[is_Windows]} {
616 wm iconbitmap . -default $oguilib/git-gui.ico
f10d5b06 617 set ::tk::AlwaysShowSelection 1
8c762125
AG
618
619 # Spoof an X11 display for SSH
620 if {![info exists env(DISPLAY)]} {
621 set env(DISPLAY) :9999
622 }
d1f2b362
GB
623} else {
624 catch {
625 image create photo gitlogo -width 16 -height 16
626
627 gitlogo put #33CC33 -to 7 0 9 2
628 gitlogo put #33CC33 -to 4 2 12 4
629 gitlogo put #33CC33 -to 7 4 9 6
630 gitlogo put #CC3333 -to 4 6 12 8
631 gitlogo put gray26 -to 4 9 6 10
632 gitlogo put gray26 -to 3 10 6 12
633 gitlogo put gray26 -to 8 9 13 11
634 gitlogo put gray26 -to 8 11 10 12
635 gitlogo put gray26 -to 11 11 13 14
636 gitlogo put gray26 -to 3 12 5 14
637 gitlogo put gray26 -to 5 13
638 gitlogo put gray26 -to 10 13
639 gitlogo put gray26 -to 4 14 12 15
640 gitlogo put gray26 -to 5 15 11 16
641 gitlogo redither
642
643 wm iconphoto . -default gitlogo
644 }
1bdd8a15
SP
645}
646
a4bee597
SP
647######################################################################
648##
649## config defaults
650
651set cursor_ptr arrow
652font create font_diff -family Courier -size 10
653font create font_ui
654catch {
655 label .dummy
656 eval font configure font_ui [font actual [.dummy cget -font]]
657 destroy .dummy
658}
659
660font create font_uiitalic
661font create font_uibold
662font create font_diffbold
663font create font_diffitalic
664
665foreach class {Button Checkbutton Entry Label
a91be3fc 666 Labelframe Listbox Message
a4bee597
SP
667 Radiobutton Spinbox Text} {
668 option add *$class.font font_ui
669}
a91be3fc
DS
670if {![is_MacOSX]} {
671 option add *Menu.font font_ui
672}
a4bee597
SP
673unset class
674
675if {[is_Windows] || [is_MacOSX]} {
676 option add *Menu.tearOff 0
677}
678
679if {[is_MacOSX]} {
680 set M1B M1
681 set M1T Cmd
682} else {
683 set M1B Control
684 set M1T Ctrl
685}
686
687proc bind_button3 {w cmd} {
688 bind $w <Any-Button-3> $cmd
689 if {[is_MacOSX]} {
690 # Mac OS X sends Button-2 on right click through three-button mouse,
691 # or through trackpad right-clicking (two-finger touch + click).
692 bind $w <Any-Button-2> $cmd
693 bind $w <Control-Button-1> $cmd
694 }
695}
696
697proc apply_config {} {
698 global repo_config font_descs
699
700 foreach option $font_descs {
701 set name [lindex $option 0]
702 set font [lindex $option 1]
703 if {[catch {
48b8d2b3 704 set need_weight 1
a4bee597 705 foreach {cn cv} $repo_config(gui.$name) {
48b8d2b3
SP
706 if {$cn eq {-weight}} {
707 set need_weight 0
708 }
709 font configure $font $cn $cv
710 }
711 if {$need_weight} {
712 font configure $font -weight normal
a4bee597
SP
713 }
714 } err]} {
715 error_popup [strcat [mc "Invalid font specified in %s:" "gui.$name"] "\n\n$err"]
716 }
717 foreach {cn cv} [font configure $font] {
718 font configure ${font}bold $cn $cv
719 font configure ${font}italic $cn $cv
720 }
721 font configure ${font}bold -weight bold
722 font configure ${font}italic -slant italic
723 }
724}
725
fe70225d 726set default_config(branch.autosetupmerge) true
7e30682c 727set default_config(merge.tool) {}
fb25092a 728set default_config(mergetool.keepbackup) true
a4bee597
SP
729set default_config(merge.diffstat) true
730set default_config(merge.summary) false
731set default_config(merge.verbosity) 2
732set default_config(user.name) {}
733set default_config(user.email) {}
734
72e6b002 735set default_config(gui.encoding) [encoding system]
a4bee597
SP
736set default_config(gui.matchtrackingbranch) false
737set default_config(gui.pruneduringfetch) false
738set default_config(gui.trustmtime) false
57cae87b
AG
739set default_config(gui.fastcopyblame) false
740set default_config(gui.copyblamethreshold) 40
a9c80b83 741set default_config(gui.blamehistoryctx) 7
a4bee597 742set default_config(gui.diffcontext) 5
11027d54 743set default_config(gui.commitmsgwidth) 75
a4bee597 744set default_config(gui.newbranchtemplate) {}
95b002ee 745set default_config(gui.spellingdictionary) {}
a4bee597
SP
746set default_config(gui.fontui) [font configure font_ui]
747set default_config(gui.fontdiff) [font configure font_diff]
748set font_descs {
749 {fontui font_ui {mc "Main Font"}}
750 {fontdiff font_diff {mc "Diff/Console Font"}}
751}
752
0b812616
SP
753######################################################################
754##
755## find git
756
757set _git [_which git]
758if {$_git eq {}} {
759 catch {wm withdraw .}
183a1d14
SP
760 tk_messageBox \
761 -icon error \
762 -type ok \
763 -title [mc "git-gui: fatal error"] \
764 -message [mc "Cannot find git in PATH."]
0b812616
SP
765 exit 1
766}
0b812616 767
54acdd95
SP
768######################################################################
769##
770## version check
771
d6967022 772if {[catch {set _git_version [git --version]} err]} {
54acdd95 773 catch {wm withdraw .}
875b7c93
SP
774 tk_messageBox \
775 -icon error \
776 -type ok \
c8c4854b 777 -title [mc "git-gui: fatal error"] \
875b7c93 778 -message "Cannot determine Git version:
54acdd95
SP
779
780$err
781
d6967022
SP
782[appname] requires Git 1.5.0 or later."
783 exit 1
784}
785if {![regsub {^git version } $_git_version {} _git_version]} {
786 catch {wm withdraw .}
875b7c93
SP
787 tk_messageBox \
788 -icon error \
789 -type ok \
c8c4854b 790 -title [mc "git-gui: fatal error"] \
31bb1d1b 791 -message [strcat [mc "Cannot parse Git version string:"] "\n\n$_git_version"]
54acdd95
SP
792 exit 1
793}
301dfaa9
SP
794
795set _real_git_version $_git_version
2c2a3782 796regsub -- {[\-\.]dirty$} $_git_version {} _git_version
d6967022 797regsub {\.[0-9]+\.g[0-9a-f]+$} $_git_version {} _git_version
764369c5 798regsub {\.[a-zA-Z]+\.?[0-9]+$} $_git_version {} _git_version
91464dfb 799regsub {\.GIT$} $_git_version {} _git_version
764369c5 800regsub {\.[a-zA-Z]+\.?[0-9]+$} $_git_version {} _git_version
d6967022 801
301dfaa9
SP
802if {![regexp {^[1-9]+(\.[0-9]+)+$} $_git_version]} {
803 catch {wm withdraw .}
804 if {[tk_messageBox \
805 -icon warning \
806 -type yesno \
807 -default no \
808 -title "[appname]: warning" \
1ac17950 809 -message [mc "Git version cannot be determined.
301dfaa9 810
1ac17950 811%s claims it is version '%s'.
301dfaa9 812
1ac17950 813%s requires at least Git 1.5.0 or later.
301dfaa9 814
1ac17950
CS
815Assume '%s' is version 1.5.0?
816" $_git $_real_git_version [appname] $_real_git_version]] eq {yes}} {
301dfaa9
SP
817 set _git_version 1.5.0
818 } else {
819 exit 1
820 }
821}
822unset _real_git_version
823
d6967022
SP
824proc git-version {args} {
825 global _git_version
826
827 switch [llength $args] {
828 0 {
829 return $_git_version
54acdd95 830 }
d6967022
SP
831
832 2 {
833 set op [lindex $args 0]
834 set vr [lindex $args 1]
835 set cm [package vcompare $_git_version $vr]
836 return [expr $cm $op 0]
837 }
838
839 4 {
840 set type [lindex $args 0]
841 set name [lindex $args 1]
842 set parm [lindex $args 2]
843 set body [lindex $args 3]
844
845 if {($type ne {proc} && $type ne {method})} {
846 error "Invalid arguments to git-version"
847 }
848 if {[llength $body] < 2 || [lindex $body end-1] ne {default}} {
849 error "Last arm of $type $name must be default"
850 }
851
852 foreach {op vr cb} [lrange $body 0 end-2] {
853 if {[git-version $op $vr]} {
854 return [uplevel [list $type $name $parm $cb]]
855 }
856 }
857
858 return [uplevel [list $type $name $parm [lindex $body end]]]
859 }
860
861 default {
862 error "git-version >= x"
863 }
864
865 }
866}
867
868if {[git-version < 1.5]} {
54acdd95 869 catch {wm withdraw .}
875b7c93
SP
870 tk_messageBox \
871 -icon error \
872 -type ok \
c8c4854b 873 -title [mc "git-gui: fatal error"] \
875b7c93 874 -message "[appname] requires Git 1.5.0 or later.
d6967022
SP
875
876You are using [git-version]:
877
878[git --version]"
54acdd95
SP
879 exit 1
880}
54acdd95 881
875b7c93
SP
882######################################################################
883##
884## configure our library
885
875b7c93
SP
886set idx [file join $oguilib tclIndex]
887if {[catch {set fd [open $idx r]} err]} {
888 catch {wm withdraw .}
889 tk_messageBox \
890 -icon error \
891 -type ok \
c8c4854b 892 -title [mc "git-gui: fatal error"] \
875b7c93
SP
893 -message $err
894 exit 1
895}
896if {[gets $fd] eq {# Autogenerated by git-gui Makefile}} {
897 set idx [list]
898 while {[gets $fd n] >= 0} {
899 if {$n ne {} && ![string match #* $n]} {
900 lappend idx $n
901 }
902 }
903} else {
904 set idx {}
905}
906close $fd
907
908if {$idx ne {}} {
909 set loaded [list]
910 foreach p $idx {
911 if {[lsearch -exact $loaded $p] >= 0} continue
912 source [file join $oguilib $p]
913 lappend loaded $p
914 }
915 unset loaded p
916} else {
917 set auto_path [concat [list $oguilib] $auto_path]
918}
fc703c20 919unset -nocomplain idx fd
875b7c93 920
ba7cc660 921######################################################################
69f85ffa
SP
922##
923## config file parsing
924
f00d504a 925git-version proc _parse_config {arr_name args} {
85f7a94b
SP
926 >= 1.5.3 {
927 upvar $arr_name arr
928 array unset arr
929 set buf {}
930 catch {
a5bb31fb
SP
931 set fd_rc [eval \
932 [list git_read config] \
933 $args \
934 [list --null --list]]
85f7a94b
SP
935 fconfigure $fd_rc -translation binary
936 set buf [read $fd_rc]
937 close $fd_rc
938 }
939 foreach line [split $buf "\0"] {
940 if {[regexp {^([^\n]+)\n(.*)$} $line line name value]} {
941 if {[is_many_config $name]} {
942 lappend arr($name) $value
943 } else {
944 set arr($name) $value
945 }
946 }
947 }
948 }
f00d504a
SP
949 default {
950 upvar $arr_name arr
951 array unset arr
69f85ffa 952 catch {
f00d504a 953 set fd_rc [eval [list git_read config --list] $args]
69f85ffa
SP
954 while {[gets $fd_rc line] >= 0} {
955 if {[regexp {^([^=]+)=(.*)$} $line line name value]} {
956 if {[is_many_config $name]} {
f00d504a 957 lappend arr($name) $value
69f85ffa 958 } else {
f00d504a 959 set arr($name) $value
69f85ffa
SP
960 }
961 }
962 }
963 close $fd_rc
964 }
965 }
f00d504a 966}
69f85ffa 967
f00d504a 968proc load_config {include_global} {
153ad78b 969 global repo_config global_config system_config default_config
f00d504a
SP
970
971 if {$include_global} {
153ad78b 972 _parse_config system_config --system
f00d504a 973 _parse_config global_config --global
69f85ffa 974 }
f00d504a 975 _parse_config repo_config
69f85ffa
SP
976
977 foreach name [array names default_config] {
153ad78b
AG
978 if {[catch {set v $system_config($name)}]} {
979 set system_config($name) $default_config($name)
980 }
981 }
982 foreach name [array names system_config] {
69f85ffa 983 if {[catch {set v $global_config($name)}]} {
153ad78b 984 set global_config($name) $system_config($name)
69f85ffa
SP
985 }
986 if {[catch {set v $repo_config($name)}]} {
153ad78b 987 set repo_config($name) $system_config($name)
69f85ffa
SP
988 }
989 }
990}
991
992######################################################################
ba7cc660
SP
993##
994## feature option selection
995
0b2bc460 996if {[regexp {^git-(.+)$} [file tail $argv0] _junk subcommand]} {
ba7cc660
SP
997 unset _junk
998} else {
999 set subcommand gui
1000}
1001if {$subcommand eq {gui.sh}} {
1002 set subcommand gui
1003}
1004if {$subcommand eq {gui} && [llength $argv] > 0} {
1005 set subcommand [lindex $argv 0]
1006 set argv [lrange $argv 1 end]
1007}
1008
1009enable_option multicommit
1010enable_option branch
1011enable_option transport
c52c9452 1012disable_option bare
ba7cc660
SP
1013
1014switch -- $subcommand {
1015browser -
1016blame {
c52c9452
SP
1017 enable_option bare
1018
ba7cc660
SP
1019 disable_option multicommit
1020 disable_option branch
1021 disable_option transport
1022}
1023citool {
1024 enable_option singlecommit
1e65c622 1025 enable_option retcode
ba7cc660
SP
1026
1027 disable_option multicommit
1028 disable_option branch
1029 disable_option transport
1e65c622
AG
1030
1031 while {[llength $argv] > 0} {
1032 set a [lindex $argv 0]
1033 switch -- $a {
1034 --amend {
1035 enable_option initialamend
1036 }
1037 --nocommit {
1038 enable_option nocommit
1039 enable_option nocommitmsg
1040 }
1041 --commitmsg {
1042 disable_option nocommitmsg
1043 }
1044 default {
1045 break
1046 }
1047 }
1048
1049 set argv [lrange $argv 1 end]
1050 }
ba7cc660
SP
1051}
1052}
1053
e29c0d10
AG
1054######################################################################
1055##
1056## execution environment
1057
1058set have_tk85 [expr {[package vcompare $tk_version "8.5"] >= 0}]
1059
1060# Suggest our implementation of askpass, if none is set
1061if {![info exists env(SSH_ASKPASS)]} {
1062 set env(SSH_ASKPASS) [gitexec git-gui--askpass]
1063}
1064
2d19516d
SP
1065######################################################################
1066##
1067## repository setup
1068
bb4812bc 1069set picked 0
c6127856
SP
1070if {[catch {
1071 set _gitdir $env(GIT_DIR)
1072 set _prefix {}
1073 }]
1074 && [catch {
1075 set _gitdir [git rev-parse --git-dir]
1076 set _prefix [git rev-parse --show-prefix]
1077 } err]} {
ab08b363
SP
1078 load_config 1
1079 apply_config
1080 choose_repository::pick
bb4812bc 1081 set picked 1
2d19516d 1082}
20ddfcaa 1083if {![file isdirectory $_gitdir] && [is_Cygwin]} {
2f7c9a7f 1084 catch {set _gitdir [exec cygpath --windows $_gitdir]}
20ddfcaa 1085}
c950c66e 1086if {![file isdirectory $_gitdir]} {
dbccbbda 1087 catch {wm withdraw .}
31bb1d1b 1088 error_popup [strcat [mc "Git directory not found:"] "\n\n$_gitdir"]
dbccbbda
SP
1089 exit 1
1090}
c80d25db
SP
1091if {$_prefix ne {}} {
1092 regsub -all {[^/]+/} $_prefix ../ cdup
1093 if {[catch {cd $cdup} err]} {
1094 catch {wm withdraw .}
31bb1d1b 1095 error_popup [strcat [mc "Cannot move to top of working directory:"] "\n\n$err"]
c80d25db
SP
1096 exit 1
1097 }
1098 unset cdup
1099} elseif {![is_enabled bare]} {
c52c9452
SP
1100 if {[lindex [file split $_gitdir] end] ne {.git}} {
1101 catch {wm withdraw .}
31bb1d1b 1102 error_popup [strcat [mc "Cannot use funny .git directory:"] "\n\n$_gitdir"]
c52c9452
SP
1103 exit 1
1104 }
1105 if {[catch {cd [file dirname $_gitdir]} err]} {
1106 catch {wm withdraw .}
31bb1d1b 1107 error_popup [strcat [mc "No working directory"] " [file dirname $_gitdir]:\n\n$err"]
c52c9452
SP
1108 exit 1
1109 }
dbccbbda 1110}
c52c9452
SP
1111set _reponame [file split [file normalize $_gitdir]]
1112if {[lindex $_reponame end] eq {.git}} {
1113 set _reponame [lindex $_reponame end-1]
1114} else {
1115 set _reponame [lindex $_reponame end]
2d19516d 1116}
2d19516d 1117
372ef954
SP
1118######################################################################
1119##
1120## global init
1121
1122set current_diff_path {}
1123set current_diff_side {}
1124set diff_actions [list]
372ef954
SP
1125
1126set HEAD {}
1127set PARENT {}
1128set MERGE_HEAD [list]
1129set commit_type {}
1130set empty_tree {}
1131set current_branch {}
d41b43eb 1132set is_detached 0
372ef954 1133set current_diff_path {}
9c9f5fa9 1134set is_3way_diff 0
3e34838c 1135set is_conflict_diff 0
372ef954 1136set selected_commit_type new
8052e788 1137set diff_empty_count 0
372ef954 1138
a9786bb4
AG
1139set nullid "0000000000000000000000000000000000000000"
1140set nullid2 "0000000000000000000000000000000000000001"
1141
cb07fc2a
SP
1142######################################################################
1143##
e210e674 1144## task management
cb07fc2a 1145
8f52548a 1146set rescan_active 0
131f503b 1147set diff_active 0
24263b77 1148set last_clicked {}
131f503b 1149
e210e674
SP
1150set disable_on_lock [list]
1151set index_lock_type none
1152
1153proc lock_index {type} {
1154 global index_lock_type disable_on_lock
131f503b 1155
043f7011 1156 if {$index_lock_type eq {none}} {
e210e674
SP
1157 set index_lock_type $type
1158 foreach w $disable_on_lock {
1159 uplevel #0 $w disabled
1160 }
1161 return 1
53716a7b 1162 } elseif {$index_lock_type eq "begin-$type"} {
e210e674 1163 set index_lock_type $type
131f503b
SP
1164 return 1
1165 }
1166 return 0
1167}
cb07fc2a 1168
e210e674
SP
1169proc unlock_index {} {
1170 global index_lock_type disable_on_lock
1171
1172 set index_lock_type none
1173 foreach w $disable_on_lock {
1174 uplevel #0 $w normal
1175 }
1176}
1177
1178######################################################################
1179##
1180## status
1181
f18e40a1 1182proc repository_state {ctvar hdvar mhvar} {
c950c66e 1183 global current_branch
f18e40a1
SP
1184 upvar $ctvar ct $hdvar hd $mhvar mh
1185
1186 set mh [list]
ec6b424a 1187
d41b43eb 1188 load_current_branch
81347223 1189 if {[catch {set hd [git rev-parse --verify HEAD]}]} {
4539eacd 1190 set hd {}
ec6b424a 1191 set ct initial
f18e40a1
SP
1192 return
1193 }
1194
c2758a17 1195 set merge_head [gitdir MERGE_HEAD]
f18e40a1 1196 if {[file exists $merge_head]} {
ec6b424a 1197 set ct merge
f18e40a1
SP
1198 set fd_mh [open $merge_head r]
1199 while {[gets $fd_mh line] >= 0} {
1200 lappend mh $line
1201 }
1202 close $fd_mh
1203 return
ec6b424a 1204 }
f18e40a1
SP
1205
1206 set ct normal
ec6b424a
SP
1207}
1208
4539eacd
SP
1209proc PARENT {} {
1210 global PARENT empty_tree
1211
f18e40a1
SP
1212 set p [lindex $PARENT 0]
1213 if {$p ne {}} {
1214 return $p
4539eacd
SP
1215 }
1216 if {$empty_tree eq {}} {
81347223 1217 set empty_tree [git mktree << {}]
4539eacd
SP
1218 }
1219 return $empty_tree
1220}
1221
1e65c622
AG
1222proc force_amend {} {
1223 global selected_commit_type
1224 global HEAD PARENT MERGE_HEAD commit_type
1225
1226 repository_state newType newHEAD newMERGE_HEAD
1227 set HEAD $newHEAD
1228 set PARENT $newHEAD
1229 set MERGE_HEAD $newMERGE_HEAD
1230 set commit_type $newType
1231
1232 set selected_commit_type amend
1233 do_select_commit_type
1234}
1235
46aaf90b 1236proc rescan {after {honor_trustmtime 1}} {
f18e40a1 1237 global HEAD PARENT MERGE_HEAD commit_type
699d5601 1238 global ui_index ui_workdir ui_comm
8f52548a 1239 global rescan_active file_states
cf25ddc8 1240 global repo_config
cb07fc2a 1241
8f52548a 1242 if {$rescan_active > 0 || ![lock_index read]} return
cb07fc2a 1243
f18e40a1 1244 repository_state newType newHEAD newMERGE_HEAD
4539eacd 1245 if {[string match amend* $commit_type]
f18e40a1
SP
1246 && $newType eq {normal}
1247 && $newHEAD eq $HEAD} {
e57ca85e 1248 } else {
f18e40a1
SP
1249 set HEAD $newHEAD
1250 set PARENT $newHEAD
1251 set MERGE_HEAD $newMERGE_HEAD
1252 set commit_type $newType
e57ca85e
SP
1253 }
1254
cb07fc2a 1255 array unset file_states
cb07fc2a 1256
1e0a92fd
SP
1257 if {!$::GITGUI_BCK_exists &&
1258 (![$ui_comm edit modified]
1259 || [string trim [$ui_comm get 0.0 end]] eq {})} {
b2f3bb1b
SP
1260 if {[string match amend* $commit_type]} {
1261 } elseif {[load_message GITGUI_MSG]} {
2cd1fd1f 1262 } elseif {[run_prepare_commit_msg_hook]} {
131f503b
SP
1263 } elseif {[load_message MERGE_MSG]} {
1264 } elseif {[load_message SQUASH_MSG]} {
1265 }
b2c6fcf1 1266 $ui_comm edit reset
21d7744f 1267 $ui_comm edit modified false
131f503b
SP
1268 }
1269
46aaf90b 1270 if {$honor_trustmtime && $repo_config(gui.trustmtime) eq {true}} {
8f52548a 1271 rescan_stage2 {} $after
e534f3a8 1272 } else {
8f52548a 1273 set rescan_active 1
1ac17950 1274 ui_status [mc "Refreshing file status..."]
0b812616
SP
1275 set fd_rf [git_read update-index \
1276 -q \
1277 --unmerged \
1278 --ignore-missing \
1279 --refresh \
1280 ]
e534f3a8 1281 fconfigure $fd_rf -blocking 0 -translation binary
390adaea 1282 fileevent $fd_rf readable \
8f52548a 1283 [list rescan_stage2 $fd_rf $after]
e534f3a8 1284 }
131f503b
SP
1285}
1286
2fe167b6 1287if {[is_Cygwin]} {
2fe167b6
SP
1288 set is_git_info_exclude {}
1289 proc have_info_exclude {} {
7f83aa2d 1290 global is_git_info_exclude
2fe167b6 1291
7f83aa2d
SP
1292 if {$is_git_info_exclude eq {}} {
1293 if {[catch {exec test -f [gitdir info exclude]}]} {
1294 set is_git_info_exclude 0
1295 } else {
1296 set is_git_info_exclude 1
2fe167b6 1297 }
2fe167b6 1298 }
7f83aa2d 1299 return $is_git_info_exclude
2fe167b6
SP
1300 }
1301} else {
1302 proc have_info_exclude {} {
1303 return [file readable [gitdir info exclude]]
1304 }
1305}
1306
8f52548a 1307proc rescan_stage2 {fd after} {
4539eacd 1308 global rescan_active buf_rdi buf_rdf buf_rlo
131f503b 1309
043f7011 1310 if {$fd ne {}} {
e534f3a8
SP
1311 read $fd
1312 if {![eof $fd]} return
1313 close $fd
1314 }
131f503b 1315
0b812616 1316 set ls_others [list --exclude-per-directory=.gitignore]
2fe167b6
SP
1317 if {[have_info_exclude]} {
1318 lappend ls_others "--exclude-from=[gitdir info exclude]"
cb07fc2a 1319 }
94a4dd9b
SP
1320 set user_exclude [get_config core.excludesfile]
1321 if {$user_exclude ne {} && [file readable $user_exclude]} {
1322 lappend ls_others "--exclude-from=$user_exclude"
1323 }
cb07fc2a 1324
868c8752
SP
1325 set buf_rdi {}
1326 set buf_rdf {}
1327 set buf_rlo {}
1328
8f52548a 1329 set rescan_active 3
1ac17950 1330 ui_status [mc "Scanning for modified files ..."]
0b812616
SP
1331 set fd_di [git_read diff-index --cached -z [PARENT]]
1332 set fd_df [git_read diff-files -z]
1333 set fd_lo [eval git_read ls-files --others -z $ls_others]
cb07fc2a 1334
51a989ba
SP
1335 fconfigure $fd_di -blocking 0 -translation binary -encoding binary
1336 fconfigure $fd_df -blocking 0 -translation binary -encoding binary
1337 fconfigure $fd_lo -blocking 0 -translation binary -encoding binary
8f52548a
SP
1338 fileevent $fd_di readable [list read_diff_index $fd_di $after]
1339 fileevent $fd_df readable [list read_diff_files $fd_df $after]
1340 fileevent $fd_lo readable [list read_ls_others $fd_lo $after]
cb07fc2a
SP
1341}
1342
131f503b 1343proc load_message {file} {
c950c66e 1344 global ui_comm
131f503b 1345
c2758a17 1346 set f [gitdir $file]
e57ca85e 1347 if {[file isfile $f]} {
131f503b
SP
1348 if {[catch {set fd [open $f r]}]} {
1349 return 0
1350 }
6eb420ef 1351 fconfigure $fd -eofchar {}
e57ca85e 1352 set content [string trim [read $fd]]
131f503b 1353 close $fd
4e55d19a 1354 regsub -all -line {[ \r\t]+$} $content {} content
131f503b
SP
1355 $ui_comm delete 0.0 end
1356 $ui_comm insert end $content
1357 return 1
1358 }
1359 return 0
1360}
1361
2cd1fd1f
JW
1362proc run_prepare_commit_msg_hook {} {
1363 global pch_error
1364
1365 # prepare-commit-msg requires PREPARE_COMMIT_MSG exist. From git-gui
1366 # it will be .git/MERGE_MSG (merge), .git/SQUASH_MSG (squash), or an
1367 # empty file but existant file.
1368
1369 set fd_pcm [open [gitdir PREPARE_COMMIT_MSG] a]
1370
1371 if {[file isfile [gitdir MERGE_MSG]]} {
1372 set pcm_source "merge"
1373 set fd_mm [open [gitdir MERGE_MSG] r]
1374 puts -nonewline $fd_pcm [read $fd_mm]
1375 close $fd_mm
1376 } elseif {[file isfile [gitdir SQUASH_MSG]]} {
1377 set pcm_source "squash"
1378 set fd_sm [open [gitdir SQUASH_MSG] r]
1379 puts -nonewline $fd_pcm [read $fd_sm]
1380 close $fd_sm
1381 } else {
1382 set pcm_source ""
1383 }
1384
1385 close $fd_pcm
1386
1387 set fd_ph [githook_read prepare-commit-msg \
1388 [gitdir PREPARE_COMMIT_MSG] $pcm_source]
1389 if {$fd_ph eq {}} {
1390 catch {file delete [gitdir PREPARE_COMMIT_MSG]}
1391 return 0;
1392 }
1393
1394 ui_status [mc "Calling prepare-commit-msg hook..."]
1395 set pch_error {}
1396
1397 fconfigure $fd_ph -blocking 0 -translation binary -eofchar {}
1398 fileevent $fd_ph readable \
1399 [list prepare_commit_msg_hook_wait $fd_ph]
1400
1401 return 1;
1402}
1403
1404proc prepare_commit_msg_hook_wait {fd_ph} {
1405 global pch_error
1406
1407 append pch_error [read $fd_ph]
1408 fconfigure $fd_ph -blocking 1
1409 if {[eof $fd_ph]} {
1410 if {[catch {close $fd_ph}]} {
1411 ui_status [mc "Commit declined by prepare-commit-msg hook."]
1412 hook_failed_popup prepare-commit-msg $pch_error
1413 catch {file delete [gitdir PREPARE_COMMIT_MSG]}
1414 exit 1
1415 } else {
1416 load_message PREPARE_COMMIT_MSG
1417 }
1418 set pch_error {}
1419 catch {file delete [gitdir PREPARE_COMMIT_MSG]}
1420 return
1421 }
1422 fconfigure $fd_ph -blocking 0
1423 catch {file delete [gitdir PREPARE_COMMIT_MSG]}
1424}
1425
8f52548a 1426proc read_diff_index {fd after} {
cb07fc2a
SP
1427 global buf_rdi
1428
1429 append buf_rdi [read $fd]
868c8752
SP
1430 set c 0
1431 set n [string length $buf_rdi]
1432 while {$c < $n} {
1433 set z1 [string first "\0" $buf_rdi $c]
1434 if {$z1 == -1} break
1435 incr z1
1436 set z2 [string first "\0" $buf_rdi $z1]
1437 if {$z2 == -1} break
1438
868c8752 1439 incr c
86291555 1440 set i [split [string range $buf_rdi $c [expr {$z1 - 2}]] { }]
51a989ba 1441 set p [string range $buf_rdi $z1 [expr {$z2 - 1}]]
1461c5f3 1442 merge_state \
51a989ba 1443 [encoding convertfrom $p] \
86291555
SP
1444 [lindex $i 4]? \
1445 [list [lindex $i 0] [lindex $i 2]] \
1461c5f3
SP
1446 [list]
1447 set c $z2
86291555 1448 incr c
cb07fc2a 1449 }
868c8752
SP
1450 if {$c < $n} {
1451 set buf_rdi [string range $buf_rdi $c end]
1452 } else {
1453 set buf_rdi {}
1454 }
1455
8f52548a 1456 rescan_done $fd buf_rdi $after
cb07fc2a
SP
1457}
1458
8f52548a 1459proc read_diff_files {fd after} {
cb07fc2a
SP
1460 global buf_rdf
1461
1462 append buf_rdf [read $fd]
868c8752
SP
1463 set c 0
1464 set n [string length $buf_rdf]
1465 while {$c < $n} {
1466 set z1 [string first "\0" $buf_rdf $c]
1467 if {$z1 == -1} break
1468 incr z1
1469 set z2 [string first "\0" $buf_rdf $z1]
1470 if {$z2 == -1} break
1471
868c8752 1472 incr c
86291555 1473 set i [split [string range $buf_rdf $c [expr {$z1 - 2}]] { }]
51a989ba 1474 set p [string range $buf_rdf $z1 [expr {$z2 - 1}]]
1461c5f3 1475 merge_state \
51a989ba 1476 [encoding convertfrom $p] \
86291555 1477 ?[lindex $i 4] \
1461c5f3 1478 [list] \
86291555 1479 [list [lindex $i 0] [lindex $i 2]]
1461c5f3 1480 set c $z2
86291555 1481 incr c
868c8752
SP
1482 }
1483 if {$c < $n} {
1484 set buf_rdf [string range $buf_rdf $c end]
1485 } else {
1486 set buf_rdf {}
cb07fc2a 1487 }
868c8752 1488
8f52548a 1489 rescan_done $fd buf_rdf $after
cb07fc2a
SP
1490}
1491
8f52548a 1492proc read_ls_others {fd after} {
cb07fc2a
SP
1493 global buf_rlo
1494
1495 append buf_rlo [read $fd]
1496 set pck [split $buf_rlo "\0"]
1497 set buf_rlo [lindex $pck end]
1498 foreach p [lrange $pck 0 end-1] {
89384101
SP
1499 set p [encoding convertfrom $p]
1500 if {[string index $p end] eq {/}} {
1501 set p [string range $p 0 end-1]
1502 }
1503 merge_state $p ?O
cb07fc2a 1504 }
8f52548a 1505 rescan_done $fd buf_rlo $after
cb07fc2a
SP
1506}
1507
8f52548a 1508proc rescan_done {fd buf after} {
f522c9b5 1509 global rescan_active current_diff_path
f7f8d322 1510 global file_states repo_config
7f1df79b 1511 upvar $buf to_clear
cb07fc2a 1512
f7f8d322
SP
1513 if {![eof $fd]} return
1514 set to_clear {}
1515 close $fd
8f52548a 1516 if {[incr rescan_active -1] > 0} return
93f654df 1517
24263b77 1518 prune_selection
f7f8d322
SP
1519 unlock_index
1520 display_all_files
7cf4566f
AG
1521 if {$current_diff_path ne {}} { reshow_diff $after }
1522 if {$current_diff_path eq {}} { select_first_diff $after }
cb07fc2a
SP
1523}
1524
24263b77
SP
1525proc prune_selection {} {
1526 global file_states selected_paths
1527
1528 foreach path [array names selected_paths] {
1529 if {[catch {set still_here $file_states($path)}]} {
1530 unset selected_paths($path)
1531 }
1532 }
1533}
1534
cb07fc2a
SP
1535######################################################################
1536##
f522c9b5 1537## ui helpers
cb07fc2a 1538
f522c9b5
SP
1539proc mapicon {w state path} {
1540 global all_icons
1541
1542 if {[catch {set r $all_icons($state$w)}]} {
1543 puts "error: no icon for $w state={$state} $path"
1544 return file_plain
1545 }
1546 return $r
1547}
cb07fc2a 1548
f522c9b5
SP
1549proc mapdesc {state path} {
1550 global all_descs
03e4ec53 1551
f522c9b5
SP
1552 if {[catch {set r $all_descs($state)}]} {
1553 puts "error: no desc for state={$state} $path"
1554 return $state
1555 }
1556 return $r
1557}
03e4ec53 1558
699d5601 1559proc ui_status {msg} {
906ab7f6
SP
1560 global main_status
1561 if {[info exists main_status]} {
1562 $main_status show $msg
1563 }
699d5601
SP
1564}
1565
1566proc ui_ready {{test {}}} {
906ab7f6
SP
1567 global main_status
1568 if {[info exists main_status]} {
1569 $main_status show [mc "Ready."] $test
1570 }
699d5601
SP
1571}
1572
f522c9b5
SP
1573proc escape_path {path} {
1574 regsub -all {\\} $path "\\\\" path
1575 regsub -all "\n" $path "\\n" path
1576 return $path
cb07fc2a
SP
1577}
1578
f522c9b5
SP
1579proc short_path {path} {
1580 return [escape_path [lindex [file split $path] end]]
7f1df79b
SP
1581}
1582
f522c9b5
SP
1583set next_icon_id 0
1584set null_sha1 [string repeat 0 40]
16403d0b 1585
f522c9b5
SP
1586proc merge_state {path new_state {head_info {}} {index_info {}}} {
1587 global file_states next_icon_id null_sha1
16403d0b 1588
f522c9b5
SP
1589 set s0 [string index $new_state 0]
1590 set s1 [string index $new_state 1]
16403d0b 1591
f522c9b5
SP
1592 if {[catch {set info $file_states($path)}]} {
1593 set state __
1594 set icon n[incr next_icon_id]
1595 } else {
1596 set state [lindex $info 0]
1597 set icon [lindex $info 1]
1598 if {$head_info eq {}} {set head_info [lindex $info 2]}
1599 if {$index_info eq {}} {set index_info [lindex $info 3]}
1600 }
16403d0b 1601
f522c9b5
SP
1602 if {$s0 eq {?}} {set s0 [string index $state 0]} \
1603 elseif {$s0 eq {_}} {set s0 _}
124355d3 1604
f522c9b5
SP
1605 if {$s1 eq {?}} {set s1 [string index $state 1]} \
1606 elseif {$s1 eq {_}} {set s1 _}
16403d0b 1607
f522c9b5
SP
1608 if {$s0 eq {A} && $s1 eq {_} && $head_info eq {}} {
1609 set head_info [list 0 $null_sha1]
1610 } elseif {$s0 ne {_} && [string index $state 0] eq {_}
1611 && $head_info eq {}} {
1612 set head_info $index_info
1613 }
16403d0b 1614
f522c9b5
SP
1615 set file_states($path) [list $s0$s1 $icon \
1616 $head_info $index_info \
1617 ]
1618 return $state
1619}
cb07fc2a 1620
f522c9b5
SP
1621proc display_file_helper {w path icon_name old_m new_m} {
1622 global file_lists
cb07fc2a 1623
f522c9b5 1624 if {$new_m eq {_}} {
156b2921 1625 set lno [lsearch -sorted -exact $file_lists($w) $path]
5f8b70b1 1626 if {$lno >= 0} {
f522c9b5 1627 set file_lists($w) [lreplace $file_lists($w) $lno $lno]
5f8b70b1 1628 incr lno
f522c9b5
SP
1629 $w conf -state normal
1630 $w delete $lno.0 [expr {$lno + 1}].0
1631 $w conf -state disabled
03e4ec53 1632 }
f522c9b5
SP
1633 } elseif {$old_m eq {_} && $new_m ne {_}} {
1634 lappend file_lists($w) $path
1635 set file_lists($w) [lsort -unique $file_lists($w)]
1636 set lno [lsearch -sorted -exact $file_lists($w) $path]
1637 incr lno
1638 $w conf -state normal
1639 $w image create $lno.0 \
1640 -align center -padx 5 -pady 1 \
1641 -name $icon_name \
1642 -image [mapicon $w $new_m $path]
1643 $w insert $lno.1 "[escape_path $path]\n"
1644 $w conf -state disabled
1645 } elseif {$old_m ne $new_m} {
1646 $w conf -state normal
1647 $w image conf $icon_name -image [mapicon $w $new_m $path]
1648 $w conf -state disabled
03e4ec53 1649 }
f522c9b5
SP
1650}
1651
1652proc display_file {path state} {
1653 global file_states selected_paths
1654 global ui_index ui_workdir
03e4ec53 1655
f522c9b5 1656 set old_m [merge_state $path $state]
cb07fc2a 1657 set s $file_states($path)
f522c9b5
SP
1658 set new_m [lindex $s 0]
1659 set icon_name [lindex $s 1]
82cb8706 1660
f522c9b5
SP
1661 set o [string index $old_m 0]
1662 set n [string index $new_m 0]
1663 if {$o eq {U}} {
1664 set o _
1665 }
1666 if {$n eq {U}} {
1667 set n _
cb07fc2a 1668 }
f522c9b5 1669 display_file_helper $ui_index $path $icon_name $o $n
cb07fc2a 1670
f522c9b5
SP
1671 if {[string index $old_m 0] eq {U}} {
1672 set o U
1673 } else {
1674 set o [string index $old_m 1]
82cb8706 1675 }
f522c9b5
SP
1676 if {[string index $new_m 0] eq {U}} {
1677 set n U
1678 } else {
1679 set n [string index $new_m 1]
82cb8706 1680 }
f522c9b5
SP
1681 display_file_helper $ui_workdir $path $icon_name $o $n
1682
1683 if {$new_m eq {__}} {
1684 unset file_states($path)
1685 catch {unset selected_paths($path)}
cb07fc2a 1686 }
f522c9b5 1687}
cb07fc2a 1688
f522c9b5
SP
1689proc display_all_files_helper {w path icon_name m} {
1690 global file_lists
1691
1692 lappend file_lists($w) $path
1693 set lno [expr {[lindex [split [$w index end] .] 0] - 1}]
1694 $w image create end \
1695 -align center -padx 5 -pady 1 \
1696 -name $icon_name \
1697 -image [mapicon $w $m $path]
1698 $w insert end "[escape_path $path]\n"
cb07fc2a
SP
1699}
1700
f522c9b5
SP
1701proc display_all_files {} {
1702 global ui_index ui_workdir
1703 global file_states file_lists
1704 global last_clicked
cb07fc2a 1705
f522c9b5
SP
1706 $ui_index conf -state normal
1707 $ui_workdir conf -state normal
cb07fc2a 1708
f522c9b5
SP
1709 $ui_index delete 0.0 end
1710 $ui_workdir delete 0.0 end
1711 set last_clicked {}
cb07fc2a 1712
f522c9b5
SP
1713 set file_lists($ui_index) [list]
1714 set file_lists($ui_workdir) [list]
16403d0b 1715
f522c9b5
SP
1716 foreach path [lsort [array names file_states]] {
1717 set s $file_states($path)
1718 set m [lindex $s 0]
1719 set icon_name [lindex $s 1]
1720
1721 set s [string index $m 0]
1722 if {$s ne {U} && $s ne {_}} {
1723 display_all_files_helper $ui_index $path \
1724 $icon_name $s
16403d0b 1725 }
cb07fc2a 1726
f522c9b5
SP
1727 if {[string index $m 0] eq {U}} {
1728 set s U
1729 } else {
1730 set s [string index $m 1]
a25c5189 1731 }
f522c9b5
SP
1732 if {$s ne {_}} {
1733 display_all_files_helper $ui_workdir $path \
1734 $icon_name $s
a25c5189
SP
1735 }
1736 }
1737
f522c9b5
SP
1738 $ui_index conf -state disabled
1739 $ui_workdir conf -state disabled
a25c5189
SP
1740}
1741
ec6b424a
SP
1742######################################################################
1743##
f522c9b5 1744## icons
ec6b424a 1745
f522c9b5
SP
1746set filemask {
1747#define mask_width 14
1748#define mask_height 15
1749static unsigned char mask_bits[] = {
1750 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f,
1751 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f,
1752 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f};
1753}
cb07fc2a
SP
1754
1755image create bitmap file_plain -background white -foreground black -data {
1756#define plain_width 14
1757#define plain_height 15
1758static unsigned char plain_bits[] = {
1759 0xfe, 0x01, 0x02, 0x03, 0x02, 0x05, 0x02, 0x09, 0x02, 0x1f, 0x02, 0x10,
1760 0x02, 0x10, 0x02, 0x10, 0x02, 0x10, 0x02, 0x10, 0x02, 0x10, 0x02, 0x10,
1761 0x02, 0x10, 0x02, 0x10, 0xfe, 0x1f};
1762} -maskdata $filemask
1763
1764image create bitmap file_mod -background white -foreground blue -data {
1765#define mod_width 14
1766#define mod_height 15
1767static unsigned char mod_bits[] = {
1768 0xfe, 0x01, 0x02, 0x03, 0x7a, 0x05, 0x02, 0x09, 0x7a, 0x1f, 0x02, 0x10,
1769 0xfa, 0x17, 0x02, 0x10, 0xfa, 0x17, 0x02, 0x10, 0xfa, 0x17, 0x02, 0x10,
1770 0xfa, 0x17, 0x02, 0x10, 0xfe, 0x1f};
1771} -maskdata $filemask
1772
131f503b
SP
1773image create bitmap file_fulltick -background white -foreground "#007000" -data {
1774#define file_fulltick_width 14
1775#define file_fulltick_height 15
1776static unsigned char file_fulltick_bits[] = {
cb07fc2a
SP
1777 0xfe, 0x01, 0x02, 0x1a, 0x02, 0x0c, 0x02, 0x0c, 0x02, 0x16, 0x02, 0x16,
1778 0x02, 0x13, 0x00, 0x13, 0x86, 0x11, 0x8c, 0x11, 0xd8, 0x10, 0xf2, 0x10,
1779 0x62, 0x10, 0x02, 0x10, 0xfe, 0x1f};
1780} -maskdata $filemask
1781
1782image create bitmap file_parttick -background white -foreground "#005050" -data {
1783#define parttick_width 14
1784#define parttick_height 15
1785static unsigned char parttick_bits[] = {
1786 0xfe, 0x01, 0x02, 0x03, 0x7a, 0x05, 0x02, 0x09, 0x7a, 0x1f, 0x02, 0x10,
1787 0x7a, 0x14, 0x02, 0x16, 0x02, 0x13, 0x8a, 0x11, 0xda, 0x10, 0x72, 0x10,
1788 0x22, 0x10, 0x02, 0x10, 0xfe, 0x1f};
1789} -maskdata $filemask
1790
1791image create bitmap file_question -background white -foreground black -data {
1792#define file_question_width 14
1793#define file_question_height 15
1794static unsigned char file_question_bits[] = {
1795 0xfe, 0x01, 0x02, 0x02, 0xe2, 0x04, 0xf2, 0x09, 0x1a, 0x1b, 0x0a, 0x13,
1796 0x82, 0x11, 0xc2, 0x10, 0x62, 0x10, 0x62, 0x10, 0x02, 0x10, 0x62, 0x10,
1797 0x62, 0x10, 0x02, 0x10, 0xfe, 0x1f};
1798} -maskdata $filemask
1799
1800image create bitmap file_removed -background white -foreground red -data {
1801#define file_removed_width 14
1802#define file_removed_height 15
1803static unsigned char file_removed_bits[] = {
1804 0xfe, 0x01, 0x02, 0x03, 0x02, 0x05, 0x02, 0x09, 0x02, 0x1f, 0x02, 0x10,
1805 0x1a, 0x16, 0x32, 0x13, 0xe2, 0x11, 0xc2, 0x10, 0xe2, 0x11, 0x32, 0x13,
1806 0x1a, 0x16, 0x02, 0x10, 0xfe, 0x1f};
1807} -maskdata $filemask
1808
1809image create bitmap file_merge -background white -foreground blue -data {
1810#define file_merge_width 14
1811#define file_merge_height 15
1812static unsigned char file_merge_bits[] = {
1813 0xfe, 0x01, 0x02, 0x03, 0x62, 0x05, 0x62, 0x09, 0x62, 0x1f, 0x62, 0x10,
1814 0xfa, 0x11, 0xf2, 0x10, 0x62, 0x10, 0x02, 0x10, 0xfa, 0x17, 0x02, 0x10,
1815 0xfa, 0x17, 0x02, 0x10, 0xfe, 0x1f};
1816} -maskdata $filemask
1817
e681cb7d
GH
1818image create bitmap file_statechange -background white -foreground green -data {
1819#define file_merge_width 14
1820#define file_merge_height 15
1821static unsigned char file_statechange_bits[] = {
1822 0xfe, 0x01, 0x02, 0x03, 0x02, 0x05, 0x02, 0x09, 0x02, 0x1f, 0x62, 0x10,
1823 0x62, 0x10, 0xba, 0x11, 0xba, 0x11, 0x62, 0x10, 0x62, 0x10, 0x02, 0x10,
1824 0x02, 0x10, 0x02, 0x10, 0xfe, 0x1f};
1825} -maskdata $filemask
1826
6b292675 1827set ui_index .vpane.files.index.list
0812665e 1828set ui_workdir .vpane.files.workdir.list
21e409ad
SP
1829
1830set all_icons(_$ui_index) file_plain
1831set all_icons(A$ui_index) file_fulltick
1832set all_icons(M$ui_index) file_fulltick
1833set all_icons(D$ui_index) file_removed
1834set all_icons(U$ui_index) file_merge
e681cb7d 1835set all_icons(T$ui_index) file_statechange
21e409ad
SP
1836
1837set all_icons(_$ui_workdir) file_plain
1838set all_icons(M$ui_workdir) file_mod
1839set all_icons(D$ui_workdir) file_question
3b4db3c1 1840set all_icons(U$ui_workdir) file_merge
21e409ad 1841set all_icons(O$ui_workdir) file_plain
e681cb7d 1842set all_icons(T$ui_workdir) file_statechange
21e409ad 1843
131f503b 1844set max_status_desc 0
cb07fc2a 1845foreach i {
1ac17950
CS
1846 {__ {mc "Unmodified"}}
1847
1848 {_M {mc "Modified, not staged"}}
1849 {M_ {mc "Staged for commit"}}
1850 {MM {mc "Portions staged for commit"}}
1851 {MD {mc "Staged for commit, missing"}}
1852
e681cb7d
GH
1853 {_T {mc "File type changed, not staged"}}
1854 {T_ {mc "File type changed, staged"}}
1855
1ac17950
CS
1856 {_O {mc "Untracked, not staged"}}
1857 {A_ {mc "Staged for commit"}}
1858 {AM {mc "Portions staged for commit"}}
1859 {AD {mc "Staged for commit, missing"}}
1860
1861 {_D {mc "Missing"}}
1862 {D_ {mc "Staged for removal"}}
1863 {DO {mc "Staged for removal, still present"}}
1864
ff515d81 1865 {_U {mc "Requires merge resolution"}}
1ac17950
CS
1866 {U_ {mc "Requires merge resolution"}}
1867 {UU {mc "Requires merge resolution"}}
1868 {UM {mc "Requires merge resolution"}}
1869 {UD {mc "Requires merge resolution"}}
ff515d81 1870 {UT {mc "Requires merge resolution"}}
cb07fc2a 1871 } {
1ac17950
CS
1872 set text [eval [lindex $i 1]]
1873 if {$max_status_desc < [string length $text]} {
1874 set max_status_desc [string length $text]
131f503b 1875 }
1ac17950 1876 set all_descs([lindex $i 0]) $text
cb07fc2a 1877}
21e409ad 1878unset i
cb07fc2a
SP
1879
1880######################################################################
1881##
1882## util
1883
35874c16
SP
1884proc scrollbar2many {list mode args} {
1885 foreach w $list {eval $w $mode $args}
1886}
1887
1888proc many2scrollbar {list mode sb top bottom} {
1889 $sb set $top $bottom
1890 foreach w $list {$w $mode moveto $top}
1891}
1892
b4946930
SP
1893proc incr_font_size {font {amt 1}} {
1894 set sz [font configure $font -size]
1895 incr sz $amt
1896 font configure $font -size $sz
1897 font configure ${font}bold -size $sz
debcd0fd 1898 font configure ${font}italic -size $sz
b4946930
SP
1899}
1900
cb07fc2a
SP
1901######################################################################
1902##
1903## ui commands
1904
1ac17950 1905set starting_gitk_msg [mc "Starting gitk... please wait..."]
cc4b1c02 1906
d0752429 1907proc do_gitk {revs} {
20ddfcaa
SP
1908 # -- Always start gitk through whatever we were loaded with. This
1909 # lets us bypass using shell process on Windows systems.
1910 #
79317e5d 1911 set exe [_which gitk -script]
02efd48f 1912 set cmd [list [info nameofexecutable] $exe]
15430be5
AMS
1913 if {$exe eq {}} {
1914 error_popup [mc "Couldn't find gitk in PATH"]
cb07fc2a 1915 } else {
501e4c6f
SP
1916 global env
1917
1918 if {[info exists env(GIT_DIR)]} {
1919 set old_GIT_DIR $env(GIT_DIR)
1920 } else {
1921 set old_GIT_DIR {}
1922 }
1923
1924 set pwd [pwd]
1925 cd [file dirname [gitdir]]
1926 set env(GIT_DIR) [file tail [gitdir]]
1927
02efd48f 1928 eval exec $cmd $revs &
501e4c6f
SP
1929
1930 if {$old_GIT_DIR eq {}} {
1931 unset env(GIT_DIR)
1932 } else {
1933 set env(GIT_DIR) $old_GIT_DIR
1934 }
1935 cd $pwd
1936
02efd48f 1937 ui_status $::starting_gitk_msg
d0752429 1938 after 10000 {
699d5601 1939 ui_ready $starting_gitk_msg
d0752429 1940 }
cb07fc2a
SP
1941 }
1942}
1943
afd54240
PB
1944proc do_explore {} {
1945 set explorer {}
1946 if {[is_Cygwin] || [is_Windows]} {
1947 set explorer "explorer.exe"
1948 } elseif {[is_MacOSX]} {
1949 set explorer "open"
1950 } else {
1951 # freedesktop.org-conforming system is our best shot
1952 set explorer "xdg-open"
1953 }
454efb47 1954 eval exec $explorer [list [file nativename [file dirname [gitdir]]]] &
afd54240
PB
1955}
1956
b5834d70 1957set is_quitting 0
1e65c622 1958set ret_code 1
c4fe7728 1959
1e65c622
AG
1960proc terminate_me {win} {
1961 global ret_code
1962 if {$win ne {.}} return
1963 exit $ret_code
1964}
1965
1966proc do_quit {{rc {1}}} {
c950c66e 1967 global ui_comm is_quitting repo_config commit_type
4578c5cb 1968 global GITGUI_BCK_exists GITGUI_BCK_i
95b002ee 1969 global ui_comm_spell
1e65c622 1970 global ret_code
c4fe7728 1971
b5834d70
SP
1972 if {$is_quitting} return
1973 set is_quitting 1
131f503b 1974
db7f34d4
SP
1975 if {[winfo exists $ui_comm]} {
1976 # -- Stash our current commit buffer.
1977 #
1978 set save [gitdir GITGUI_MSG]
4578c5cb
SP
1979 if {$GITGUI_BCK_exists && ![$ui_comm edit modified]} {
1980 file rename -force [gitdir GITGUI_BCK] $save
1981 set GITGUI_BCK_exists 0
db7f34d4 1982 } else {
4578c5cb
SP
1983 set msg [string trim [$ui_comm get 0.0 end]]
1984 regsub -all -line {[ \r\t]+$} $msg {} msg
1985 if {(![string match amend* $commit_type]
1986 || [$ui_comm edit modified])
1987 && $msg ne {}} {
1988 catch {
1989 set fd [open $save w]
1990 puts -nonewline $fd $msg
1991 close $fd
1992 }
1993 } else {
1994 catch {file delete $save}
1995 }
1996 }
1997
95b002ee
SP
1998 # -- Cancel our spellchecker if its running.
1999 #
2000 if {[info exists ui_comm_spell]} {
2001 $ui_comm_spell stop
2002 }
2003
4578c5cb
SP
2004 # -- Remove our editor backup, its not needed.
2005 #
2006 after cancel $GITGUI_BCK_i
2007 if {$GITGUI_BCK_exists} {
2008 catch {file delete [gitdir GITGUI_BCK]}
131f503b 2009 }
131f503b 2010
db7f34d4
SP
2011 # -- Stash our current window geometry into this repository.
2012 #
2013 set cfg_geometry [list]
2014 lappend cfg_geometry [wm geometry .]
a0592d3f
JS
2015 lappend cfg_geometry [lindex [.vpane sash coord 0] 0]
2016 lappend cfg_geometry [lindex [.vpane.files sash coord 0] 1]
db7f34d4
SP
2017 if {[catch {set rc_geometry $repo_config(gui.geometry)}]} {
2018 set rc_geometry {}
2019 }
2020 if {$cfg_geometry ne $rc_geometry} {
81347223 2021 catch {git config gui.geometry $cfg_geometry}
db7f34d4 2022 }
51f4d16b
SP
2023 }
2024
1e65c622 2025 set ret_code $rc
cb07fc2a
SP
2026 destroy .
2027}
2028
2029proc do_rescan {} {
699d5601 2030 rescan ui_ready
cb07fc2a
SP
2031}
2032
8056cc4f 2033proc ui_do_rescan {} {
7cf4566f 2034 rescan {force_first_diff ui_ready}
8056cc4f
AG
2035}
2036
6e27d826 2037proc do_commit {} {
ec6b424a 2038 commit_tree
6e27d826
SP
2039}
2040
7cf4566f 2041proc next_diff {{after {}}} {
8a965b8e 2042 global next_diff_p next_diff_w next_diff_i
7cf4566f 2043 show_diff $next_diff_p $next_diff_w {} {} $after
29853b90
AG
2044}
2045
2046proc find_anchor_pos {lst name} {
2047 set lid [lsearch -sorted -exact $lst $name]
2048
2049 if {$lid == -1} {
2050 set lid 0
2051 foreach lname $lst {
2052 if {$lname >= $name} break
2053 incr lid
2054 }
2055 }
2056
2057 return $lid
2058}
2059
2060proc find_file_from {flist idx delta path mmask} {
2061 global file_states
2062
2063 set len [llength $flist]
2064 while {$idx >= 0 && $idx < $len} {
2065 set name [lindex $flist $idx]
2066
2067 if {$name ne $path && [info exists file_states($name)]} {
2068 set state [lindex $file_states($name) 0]
2069
2070 if {$mmask eq {} || [regexp $mmask $state]} {
2071 return $idx
2072 }
2073 }
2074
2075 incr idx $delta
2076 }
2077
2078 return {}
2079}
2080
2081proc find_next_diff {w path {lno {}} {mmask {}}} {
2082 global next_diff_p next_diff_w next_diff_i
2083 global file_lists ui_index ui_workdir
2084
2085 set flist $file_lists($w)
2086 if {$lno eq {}} {
2087 set lno [find_anchor_pos $flist $path]
2088 } else {
2089 incr lno -1
2090 }
2091
2092 if {$mmask ne {} && ![regexp {(^\^)|(\$$)} $mmask]} {
2093 if {$w eq $ui_index} {
2094 set mmask "^$mmask"
2095 } else {
2096 set mmask "$mmask\$"
2097 }
2098 }
2099
2100 set idx [find_file_from $flist $lno 1 $path $mmask]
2101 if {$idx eq {}} {
2102 incr lno -1
2103 set idx [find_file_from $flist $lno -1 $path $mmask]
2104 }
2105
2106 if {$idx ne {}} {
2107 set next_diff_w $w
2108 set next_diff_p [lindex $flist $idx]
2109 set next_diff_i [expr {$idx+1}]
2110 return 1
2111 } else {
2112 return 0
2113 }
2114}
2115
2116proc next_diff_after_action {w path {lno {}} {mmask {}}} {
2117 global current_diff_path
2118
2119 if {$path ne $current_diff_path} {
2120 return {}
2121 } elseif {[find_next_diff $w $path $lno $mmask]} {
2122 return {next_diff;}
2123 } else {
2124 return {reshow_diff;}
2125 }
2126}
2127
7cf4566f 2128proc select_first_diff {after} {
29853b90
AG
2129 global ui_workdir
2130
2131 if {[find_next_diff $ui_workdir {} 1 {^_?U}] ||
2132 [find_next_diff $ui_workdir {} 1 {[^O]$}]} {
7cf4566f
AG
2133 next_diff $after
2134 } else {
2135 uplevel #0 $after
29853b90 2136 }
8a965b8e
AMS
2137}
2138
7cf4566f
AG
2139proc force_first_diff {after} {
2140 global ui_workdir current_diff_path file_states
8056cc4f
AG
2141
2142 if {[info exists file_states($current_diff_path)]} {
2143 set state [lindex $file_states($current_diff_path) 0]
7cf4566f
AG
2144 } else {
2145 set state {OO}
2146 }
8056cc4f 2147
7cf4566f
AG
2148 set reselect 0
2149 if {[string first {U} $state] >= 0} {
2150 # Already a conflict, do nothing
2151 } elseif {[find_next_diff $ui_workdir $current_diff_path {} {^_?U}]} {
2152 set reselect 1
2153 } elseif {[string index $state 1] ne {O}} {
2154 # Already a diff & no conflicts, do nothing
2155 } elseif {[find_next_diff $ui_workdir $current_diff_path {} {[^O]$}]} {
2156 set reselect 1
8056cc4f
AG
2157 }
2158
7cf4566f
AG
2159 if {$reselect} {
2160 next_diff $after
2161 } else {
2162 uplevel #0 $after
2163 }
8056cc4f
AG
2164}
2165
24263b77 2166proc toggle_or_diff {w x y} {
20a53c02 2167 global file_states file_lists current_diff_path ui_index ui_workdir
24263b77 2168 global last_clicked selected_paths
131f503b 2169
cb07fc2a
SP
2170 set pos [split [$w index @$x,$y] .]
2171 set lno [lindex $pos 0]
2172 set col [lindex $pos 1]
24263b77
SP
2173 set path [lindex $file_lists($w) [expr {$lno - 1}]]
2174 if {$path eq {}} {
2175 set last_clicked {}
2176 return
2177 }
2178
2179 set last_clicked [list $w $lno]
2180 array unset selected_paths
2181 $ui_index tag remove in_sel 0.0 end
0812665e 2182 $ui_workdir tag remove in_sel 0.0 end
cb07fc2a 2183
3e34838c 2184 # Determine the state of the file
617ceee6
AG
2185 if {[info exists file_states($path)]} {
2186 set state [lindex $file_states($path) 0]
8056cc4f
AG
2187 } else {
2188 set state {__}
2189 }
2190
8056cc4f 2191 # Restage the file, or simply show the diff
cead78ed 2192 if {$col == 0 && $y > 1} {
3e34838c
AG
2193 # Conflicts need special handling
2194 if {[string first {U} $state] >= 0} {
0aea2842
AG
2195 # $w must always be $ui_workdir, but...
2196 if {$w ne $ui_workdir} { set lno {} }
2197 merge_stage_workdir $path $lno
3e34838c
AG
2198 return
2199 }
2200
8056cc4f
AG
2201 if {[string index $state 1] eq {O}} {
2202 set mmask {}
2203 } else {
2204 set mmask {[^O]}
2205 }
2206
2207 set after [next_diff_after_action $w $path $lno $mmask]
8a965b8e 2208
de5f6d5d 2209 if {$w eq $ui_index} {
74d18d2e 2210 update_indexinfo \
93e912c5 2211 "Unstaging [short_path $path] from commit" \
74d18d2e 2212 [list $path] \
699d5601 2213 [concat $after [list ui_ready]]
de5f6d5d 2214 } elseif {$w eq $ui_workdir} {
74d18d2e 2215 update_index \
4d583c86 2216 "Adding [short_path $path]" \
74d18d2e 2217 [list $path] \
699d5601 2218 [concat $after [list ui_ready]]
74d18d2e 2219 }
24263b77 2220 } else {
03e4ec53 2221 show_diff $path $w $lno
cb07fc2a
SP
2222 }
2223}
2224
24263b77 2225proc add_one_to_selection {w x y} {
833eda73 2226 global file_lists last_clicked selected_paths
7f1df79b 2227
833eda73 2228 set lno [lindex [split [$w index @$x,$y] .] 0]
24263b77
SP
2229 set path [lindex $file_lists($w) [expr {$lno - 1}]]
2230 if {$path eq {}} {
2231 set last_clicked {}
2232 return
2233 }
cb07fc2a 2234
833eda73
SP
2235 if {$last_clicked ne {}
2236 && [lindex $last_clicked 0] ne $w} {
2237 array unset selected_paths
2238 [lindex $last_clicked 0] tag remove in_sel 0.0 end
2239 }
2240
24263b77
SP
2241 set last_clicked [list $w $lno]
2242 if {[catch {set in_sel $selected_paths($path)}]} {
2243 set in_sel 0
2244 }
2245 if {$in_sel} {
2246 unset selected_paths($path)
2247 $w tag remove in_sel $lno.0 [expr {$lno + 1}].0
2248 } else {
2249 set selected_paths($path) 1
2250 $w tag add in_sel $lno.0 [expr {$lno + 1}].0
2251 }
2252}
2253
2254proc add_range_to_selection {w x y} {
833eda73 2255 global file_lists last_clicked selected_paths
24263b77
SP
2256
2257 if {[lindex $last_clicked 0] ne $w} {
2258 toggle_or_diff $w $x $y
2259 return
cb07fc2a 2260 }
24263b77 2261
833eda73 2262 set lno [lindex [split [$w index @$x,$y] .] 0]
24263b77
SP
2263 set lc [lindex $last_clicked 1]
2264 if {$lc < $lno} {
2265 set begin $lc
2266 set end $lno
2267 } else {
2268 set begin $lno
2269 set end $lc
2270 }
2271
2272 foreach path [lrange $file_lists($w) \
2273 [expr {$begin - 1}] \
2274 [expr {$end - 1}]] {
2275 set selected_paths($path) 1
2276 }
2277 $w tag add in_sel $begin.0 [expr {$end + 1}].0
cb07fc2a
SP
2278}
2279
c91ee2bd
JS
2280proc show_more_context {} {
2281 global repo_config
2282 if {$repo_config(gui.diffcontext) < 99} {
2283 incr repo_config(gui.diffcontext)
2284 reshow_diff
2285 }
2286}
2287
2288proc show_less_context {} {
2289 global repo_config
55ba8a34 2290 if {$repo_config(gui.diffcontext) > 1} {
c91ee2bd
JS
2291 incr repo_config(gui.diffcontext) -1
2292 reshow_diff
2293 }
2294}
2295
cb07fc2a
SP
2296######################################################################
2297##
a4bee597 2298## ui construction
db453781 2299
6bbd1cb9 2300load_config 0
92148d80 2301apply_config
2ebba528
SP
2302set ui_comm {}
2303
cb07fc2a 2304# -- Menu Bar
a49c67d1 2305#
b4946930 2306menu .mbar -tearoff 0
a91be3fc
DS
2307if {[is_MacOSX]} {
2308 # -- Apple Menu (Mac OS X only)
2309 #
2310 .mbar add cascade -label Apple -menu .mbar.apple
2311 menu .mbar.apple
2312}
1ac17950
CS
2313.mbar add cascade -label [mc Repository] -menu .mbar.repository
2314.mbar add cascade -label [mc Edit] -menu .mbar.edit
64a906f8 2315if {[is_enabled branch]} {
1ac17950 2316 .mbar add cascade -label [mc Branch] -menu .mbar.branch
700a65ce 2317}
2ebba528 2318if {[is_enabled multicommit] || [is_enabled singlecommit]} {
a9813cb5 2319 .mbar add cascade -label [mc Commit@@noun] -menu .mbar.commit
2ebba528 2320}
64a906f8 2321if {[is_enabled transport]} {
1ac17950 2322 .mbar add cascade -label [mc Merge] -menu .mbar.merge
6bdf5e5f 2323 .mbar add cascade -label [mc Remote] -menu .mbar.remote
4ccdab02 2324}
0ce76ded
AG
2325if {[is_enabled multicommit] || [is_enabled singlecommit]} {
2326 .mbar add cascade -label [mc Tools] -menu .mbar.tools
2327}
cb07fc2a 2328
a4abfa62 2329# -- Repository Menu
a49c67d1 2330#
a4abfa62 2331menu .mbar.repository
35874c16 2332
afd54240
PB
2333.mbar.repository add command \
2334 -label [mc "Explore Working Copy"] \
2335 -command {do_explore}
2336.mbar.repository add separator
2337
35874c16 2338.mbar.repository add command \
1ac17950 2339 -label [mc "Browse Current Branch's Files"] \
c74b6c66 2340 -command {browser::new $current_branch}
a8139888 2341set ui_browse_current [.mbar.repository index last]
8e891fac 2342.mbar.repository add command \
1ac17950 2343 -label [mc "Browse Branch Files..."] \
8e891fac 2344 -command browser_open::dialog
35874c16
SP
2345.mbar.repository add separator
2346
d0752429 2347.mbar.repository add command \
1ac17950 2348 -label [mc "Visualize Current Branch's History"] \
7416bbc6 2349 -command {do_gitk $current_branch}
a8139888 2350set ui_visualize_current [.mbar.repository index last]
5753ef1a 2351.mbar.repository add command \
1ac17950 2352 -label [mc "Visualize All Branch History"] \
7416bbc6 2353 -command {do_gitk --all}
d0752429 2354.mbar.repository add separator
75e355d6 2355
a8139888
SP
2356proc current_branch_write {args} {
2357 global current_branch
2358 .mbar.repository entryconf $::ui_browse_current \
1ac17950 2359 -label [mc "Browse %s's Files" $current_branch]
a8139888 2360 .mbar.repository entryconf $::ui_visualize_current \
1ac17950 2361 -label [mc "Visualize %s's History" $current_branch]
a8139888
SP
2362}
2363trace add variable current_branch write current_branch_write
2364
cf25ddc8 2365if {[is_enabled multicommit]} {
1ac17950 2366 .mbar.repository add command -label [mc "Database Statistics"] \
7416bbc6 2367 -command do_stats
0fd49d0a 2368
1ac17950 2369 .mbar.repository add command -label [mc "Compress Database"] \
7416bbc6 2370 -command do_gc
4aca740b 2371
1ac17950 2372 .mbar.repository add command -label [mc "Verify Database"] \
7416bbc6 2373 -command do_fsck_objects
444f92d0 2374
a4abfa62 2375 .mbar.repository add separator
75e355d6 2376
20ddfcaa
SP
2377 if {[is_Cygwin]} {
2378 .mbar.repository add command \
1ac17950 2379 -label [mc "Create Desktop Icon"] \
7416bbc6 2380 -command do_cygwin_shortcut
20ddfcaa 2381 } elseif {[is_Windows]} {
a4abfa62 2382 .mbar.repository add command \
1ac17950 2383 -label [mc "Create Desktop Icon"] \
7416bbc6 2384 -command do_windows_shortcut
06c31115 2385 } elseif {[is_MacOSX]} {
a4abfa62 2386 .mbar.repository add command \
1ac17950 2387 -label [mc "Create Desktop Icon"] \
7416bbc6 2388 -command do_macosx_app
4aca740b 2389 }
4ccdab02 2390}
85ab313e 2391
af894943
SF
2392if {[is_MacOSX]} {
2393 proc ::tk::mac::Quit {args} { do_quit }
2394} else {
2395 .mbar.repository add command -label [mc Quit] \
2396 -command do_quit \
2397 -accelerator $M1T-Q
2398}
cb07fc2a 2399
9861671d
SP
2400# -- Edit Menu
2401#
2402menu .mbar.edit
1ac17950 2403.mbar.edit add command -label [mc Undo] \
9861671d 2404 -command {catch {[focus] edit undo}} \
7416bbc6 2405 -accelerator $M1T-Z
1ac17950 2406.mbar.edit add command -label [mc Redo] \
9861671d 2407 -command {catch {[focus] edit redo}} \
7416bbc6 2408 -accelerator $M1T-Y
9861671d 2409.mbar.edit add separator
1ac17950 2410.mbar.edit add command -label [mc Cut] \
9861671d 2411 -command {catch {tk_textCut [focus]}} \
7416bbc6 2412 -accelerator $M1T-X
1ac17950 2413.mbar.edit add command -label [mc Copy] \
9861671d 2414 -command {catch {tk_textCopy [focus]}} \
7416bbc6 2415 -accelerator $M1T-C
1ac17950 2416.mbar.edit add command -label [mc Paste] \
9861671d 2417 -command {catch {tk_textPaste [focus]; [focus] see insert}} \
7416bbc6 2418 -accelerator $M1T-V
1ac17950 2419.mbar.edit add command -label [mc Delete] \
9861671d 2420 -command {catch {[focus] delete sel.first sel.last}} \
7416bbc6 2421 -accelerator Del
9861671d 2422.mbar.edit add separator
1ac17950 2423.mbar.edit add command -label [mc "Select All"] \
9861671d 2424 -command {catch {[focus] tag add sel 0.0 end}} \
7416bbc6 2425 -accelerator $M1T-A
9861671d 2426
85ab313e
SP
2427# -- Branch Menu
2428#
64a906f8 2429if {[is_enabled branch]} {
700a65ce
SP
2430 menu .mbar.branch
2431
1ac17950 2432 .mbar.branch add command -label [mc "Create..."] \
b1fa2bff 2433 -command branch_create::dialog \
7416bbc6 2434 -accelerator $M1T-N
700a65ce
SP
2435 lappend disable_on_lock [list .mbar.branch entryconf \
2436 [.mbar.branch index last] -state]
2437
1ac17950 2438 .mbar.branch add command -label [mc "Checkout..."] \
d41b43eb
SP
2439 -command branch_checkout::dialog \
2440 -accelerator $M1T-O
2441 lappend disable_on_lock [list .mbar.branch entryconf \
2442 [.mbar.branch index last] -state]
2443
1ac17950 2444 .mbar.branch add command -label [mc "Rename..."] \
61f82ce7
SP
2445 -command branch_rename::dialog
2446 lappend disable_on_lock [list .mbar.branch entryconf \
2447 [.mbar.branch index last] -state]
2448
1ac17950 2449 .mbar.branch add command -label [mc "Delete..."] \
3206c63d 2450 -command branch_delete::dialog
700a65ce
SP
2451 lappend disable_on_lock [list .mbar.branch entryconf \
2452 [.mbar.branch index last] -state]
fd234dfd 2453
1ac17950 2454 .mbar.branch add command -label [mc "Reset..."] \
a6c9b081 2455 -command merge::reset_hard
fd234dfd
SP
2456 lappend disable_on_lock [list .mbar.branch entryconf \
2457 [.mbar.branch index last] -state]
700a65ce
SP
2458}
2459
cb07fc2a 2460# -- Commit Menu
a49c67d1 2461#
1e65c622
AG
2462proc commit_btn_caption {} {
2463 if {[is_enabled nocommit]} {
2464 return [mc "Done"]
2465 } else {
2466 return [mc Commit@@verb]
2467 }
2468}
2469
2ebba528
SP
2470if {[is_enabled multicommit] || [is_enabled singlecommit]} {
2471 menu .mbar.commit
2472
1e02b32e
SP
2473 if {![is_enabled nocommit]} {
2474 .mbar.commit add radiobutton \
2475 -label [mc "New Commit"] \
2476 -command do_select_commit_type \
2477 -variable selected_commit_type \
2478 -value new
2479 lappend disable_on_lock \
2480 [list .mbar.commit entryconf [.mbar.commit index last] -state]
2481
2482 .mbar.commit add radiobutton \
2483 -label [mc "Amend Last Commit"] \
2484 -command do_select_commit_type \
2485 -variable selected_commit_type \
2486 -value amend
2487 lappend disable_on_lock \
2488 [list .mbar.commit entryconf [.mbar.commit index last] -state]
2489
2490 .mbar.commit add separator
2491 }
24ac9b75 2492
1ac17950 2493 .mbar.commit add command -label [mc Rescan] \
8056cc4f 2494 -command ui_do_rescan \
7416bbc6 2495 -accelerator F5
2ebba528
SP
2496 lappend disable_on_lock \
2497 [list .mbar.commit entryconf [.mbar.commit index last] -state]
24ac9b75 2498
1ac17950 2499 .mbar.commit add command -label [mc "Stage To Commit"] \
cd16a6c9
SP
2500 -command do_add_selection \
2501 -accelerator $M1T-T
2ebba528
SP
2502 lappend disable_on_lock \
2503 [list .mbar.commit entryconf [.mbar.commit index last] -state]
24ac9b75 2504
1ac17950 2505 .mbar.commit add command -label [mc "Stage Changed Files To Commit"] \
2ebba528 2506 -command do_add_all \
7416bbc6 2507 -accelerator $M1T-I
2ebba528
SP
2508 lappend disable_on_lock \
2509 [list .mbar.commit entryconf [.mbar.commit index last] -state]
24ac9b75 2510
1ac17950 2511 .mbar.commit add command -label [mc "Unstage From Commit"] \
7416bbc6 2512 -command do_unstage_selection
2ebba528
SP
2513 lappend disable_on_lock \
2514 [list .mbar.commit entryconf [.mbar.commit index last] -state]
e734817d 2515
1ac17950 2516 .mbar.commit add command -label [mc "Revert Changes"] \
7416bbc6 2517 -command do_revert_selection
2ebba528
SP
2518 lappend disable_on_lock \
2519 [list .mbar.commit entryconf [.mbar.commit index last] -state]
e734817d 2520
2ebba528 2521 .mbar.commit add separator
1461c5f3 2522
c91ee2bd
JS
2523 .mbar.commit add command -label [mc "Show Less Context"] \
2524 -command show_less_context \
729ffa50 2525 -accelerator $M1T-\-
c91ee2bd
JS
2526
2527 .mbar.commit add command -label [mc "Show More Context"] \
2528 -command show_more_context \
729ffa50 2529 -accelerator $M1T-=
c91ee2bd
JS
2530
2531 .mbar.commit add separator
2532
ed70e4d7 2533 if {![is_enabled nocommitmsg]} {
1e02b32e
SP
2534 .mbar.commit add command -label [mc "Sign Off"] \
2535 -command do_signoff \
2536 -accelerator $M1T-S
2537 }
24ac9b75 2538
1e65c622 2539 .mbar.commit add command -label [commit_btn_caption] \
2ebba528 2540 -command do_commit \
7416bbc6 2541 -accelerator $M1T-Return
2ebba528
SP
2542 lappend disable_on_lock \
2543 [list .mbar.commit entryconf [.mbar.commit index last] -state]
2544}
cb07fc2a 2545
9b28a8b9
SP
2546# -- Merge Menu
2547#
2548if {[is_enabled branch]} {
2549 menu .mbar.merge
1ac17950 2550 .mbar.merge add command -label [mc "Local Merge..."] \
a870ddc0
SP
2551 -command merge::dialog \
2552 -accelerator $M1T-M
9b28a8b9
SP
2553 lappend disable_on_lock \
2554 [list .mbar.merge entryconf [.mbar.merge index last] -state]
1ac17950 2555 .mbar.merge add command -label [mc "Abort Merge..."] \
a6c9b081 2556 -command merge::reset_hard
9b28a8b9
SP
2557 lappend disable_on_lock \
2558 [list .mbar.merge entryconf [.mbar.merge index last] -state]
9b28a8b9
SP
2559}
2560
2561# -- Transport Menu
2562#
2563if {[is_enabled transport]} {
6bdf5e5f 2564 menu .mbar.remote
9b28a8b9 2565
ba6485e0
PB
2566 .mbar.remote add command \
2567 -label [mc "Add..."] \
2568 -command remote_add::dialog \
2569 -accelerator $M1T-A
6bdf5e5f
SP
2570 .mbar.remote add command \
2571 -label [mc "Push..."] \
840bcfa7
SP
2572 -command do_push_anywhere \
2573 -accelerator $M1T-P
6bdf5e5f 2574 .mbar.remote add command \
3c1c2a00 2575 -label [mc "Delete Branch..."] \
aa252f19 2576 -command remote_branch_delete::dialog
9b28a8b9
SP
2577}
2578
0c8d7839 2579if {[is_MacOSX]} {
a91be3fc 2580 proc ::tk::mac::ShowPreferences {} {do_options}
0c8d7839
SP
2581} else {
2582 # -- Edit Menu
2583 #
2584 .mbar.edit add separator
1ac17950 2585 .mbar.edit add command -label [mc "Options..."] \
7416bbc6 2586 -command do_options
273984fc 2587}
557afe82 2588
0ce76ded
AG
2589# -- Tools Menu
2590#
2591if {[is_enabled multicommit] || [is_enabled singlecommit]} {
2592 set tools_menubar .mbar.tools
2593 menu $tools_menubar
2594 $tools_menubar add separator
2595 $tools_menubar add command -label [mc "Add..."] -command tools_add::dialog
2596 $tools_menubar add command -label [mc "Remove..."] -command tools_remove::dialog
2597 set tools_tailcnt 3
2598 if {[array names repo_config guitool.*.cmd] ne {}} {
2599 tools_populate_all
2600 }
2601}
2602
273984fc
SP
2603# -- Help Menu
2604#
1ac17950 2605.mbar add cascade -label [mc Help] -menu .mbar.help
273984fc 2606menu .mbar.help
0c8d7839 2607
a91be3fc
DS
2608if {[is_MacOSX]} {
2609 .mbar.apple add command -label [mc "About %s" [appname]] \
2610 -command do_about
2611 .mbar.apple add separator
2612} else {
1ac17950 2613 .mbar.help add command -label [mc "About %s" [appname]] \
7416bbc6 2614 -command do_about
0c8d7839 2615}
a91be3fc 2616. configure -menu .mbar
2db21e70 2617
3eb5682b
MH
2618set doc_path [githtmldir]
2619if {$doc_path ne {}} {
2620 set doc_path [file join $doc_path index.html]
273984fc 2621
3eb5682b
MH
2622 if {[is_Cygwin]} {
2623 set doc_path [exec cygpath --mixed $doc_path]
2624 }
273984fc
SP
2625}
2626
273984fc
SP
2627if {[file isfile $doc_path]} {
2628 set doc_url "file:$doc_path"
2629} else {
2630 set doc_url {http://www.kernel.org/pub/software/scm/git/docs/}
2631}
2632
2db21e70
PB
2633proc start_browser {url} {
2634 git "web--browse" $url
273984fc 2635}
2db21e70
PB
2636
2637.mbar.help add command -label [mc "Online Documentation"] \
2638 -command [list start_browser $doc_url]
98a6846b
AG
2639
2640.mbar.help add command -label [mc "Show SSH Key"] \
2641 -command do_ssh_key
2642
2db21e70 2643unset doc_path doc_url
82aa2354 2644
2ebba528
SP
2645# -- Standard bindings
2646#
39fa2a98 2647wm protocol . WM_DELETE_WINDOW do_quit
2ebba528
SP
2648bind all <$M1B-Key-q> do_quit
2649bind all <$M1B-Key-Q> do_quit
2650bind all <$M1B-Key-w> {destroy [winfo toplevel %W]}
2651bind all <$M1B-Key-W> {destroy [winfo toplevel %W]}
2652
3e45ee1e
SP
2653set subcommand_args {}
2654proc usage {} {
2655 puts stderr "usage: $::argv0 $::subcommand $::subcommand_args"
2656 exit 1
2657}
2658
95e706b5
AG
2659proc normalize_relpath {path} {
2660 set elements {}
2661 foreach item [file split $path] {
2662 if {$item eq {.}} continue
2663 if {$item eq {..} && [llength $elements] > 0
2664 && [lindex $elements end] ne {..}} {
2665 set elements [lrange $elements 0 end-1]
2666 continue
2667 }
2668 lappend elements $item
2669 }
2670 return [eval file join $elements]
2671}
2672
2ebba528
SP
2673# -- Not a normal commit type invocation? Do that instead!
2674#
258871d3 2675switch -- $subcommand {
85d2d597 2676browser -
2ebba528 2677blame {
f7078b40
AG
2678 if {$subcommand eq "blame"} {
2679 set subcommand_args {[--line=<num>] rev? path}
2680 } else {
2681 set subcommand_args {rev? path}
2682 }
c52c9452 2683 if {$argv eq {}} usage
a0db0d61 2684 set head {}
3e45ee1e 2685 set path {}
f7078b40 2686 set jump_spec {}
3e45ee1e
SP
2687 set is_path 0
2688 foreach a $argv {
2689 if {$is_path || [file exists $_prefix$a]} {
2690 if {$path ne {}} usage
95e706b5 2691 set path [normalize_relpath $_prefix$a]
3e45ee1e
SP
2692 break
2693 } elseif {$a eq {--}} {
2694 if {$path ne {}} {
a0db0d61
SP
2695 if {$head ne {}} usage
2696 set head $path
3e45ee1e
SP
2697 set path {}
2698 }
2699 set is_path 1
f7078b40
AG
2700 } elseif {[regexp {^--line=(\d+)$} $a a lnum]} {
2701 if {$jump_spec ne {} || $head ne {}} usage
2702 set jump_spec [list $lnum]
a0db0d61
SP
2703 } elseif {$head eq {}} {
2704 if {$head ne {}} usage
2705 set head $a
c52c9452 2706 set is_path 1
3e45ee1e
SP
2707 } else {
2708 usage
2709 }
2710 }
2711 unset is_path
2712
c52c9452 2713 if {$head ne {} && $path eq {}} {
95e706b5 2714 set path [normalize_relpath $_prefix$head]
c52c9452
SP
2715 set head {}
2716 }
2717
a0db0d61 2718 if {$head eq {}} {
d41b43eb 2719 load_current_branch
a0db0d61 2720 } else {
02087abc
SP
2721 if {[regexp {^[0-9a-f]{1,39}$} $head]} {
2722 if {[catch {
2723 set head [git rev-parse --verify $head]
2724 } err]} {
2725 puts stderr $err
2726 exit 1
2727 }
2728 }
a0db0d61 2729 set current_branch $head
2ebba528 2730 }
a0db0d61 2731
85d2d597
SP
2732 switch -- $subcommand {
2733 browser {
f7078b40 2734 if {$jump_spec ne {}} usage
85d2d597
SP
2735 if {$head eq {}} {
2736 if {$path ne {} && [file isdirectory $path]} {
2737 set head $current_branch
2738 } else {
2739 set head $path
2740 set path {}
2741 }
2742 }
2743 browser::new $head $path
2744 }
2745 blame {
2746 if {$head eq {} && ![file exists $path]} {
c8c4854b 2747 puts stderr [mc "fatal: cannot stat path %s: No such file or directory" $path]
85d2d597
SP
2748 exit 1
2749 }
f7078b40 2750 blame::new $head $path $jump_spec
85d2d597 2751 }
c52c9452 2752 }
258871d3
SP
2753 return
2754}
2755citool -
2756gui {
2757 if {[llength $argv] != 0} {
2758 puts -nonewline stderr "usage: $argv0"
0b2bc460
SP
2759 if {$subcommand ne {gui}
2760 && [file tail $argv0] ne "git-$subcommand"} {
258871d3
SP
2761 puts -nonewline stderr " $subcommand"
2762 }
2763 puts stderr {}
2764 exit 1
2765 }
2766 # fall through to setup UI for commits
2ebba528 2767}
2ebba528 2768default {
c0f7a6c3 2769 puts stderr "usage: $argv0 \[{blame|browser|citool}\]"
2ebba528
SP
2770 exit 1
2771}
2772}
2773
8553b772
SP
2774# -- Branch Control
2775#
2776frame .branch \
2777 -borderwidth 1 \
2778 -relief sunken
2779label .branch.l1 \
1ac17950 2780 -text [mc "Current Branch:"] \
8553b772 2781 -anchor w \
7416bbc6 2782 -justify left
8553b772
SP
2783label .branch.cb \
2784 -textvariable current_branch \
2785 -anchor w \
7416bbc6 2786 -justify left
8553b772
SP
2787pack .branch.l1 -side left
2788pack .branch.cb -side left -fill x
2789pack .branch -side top -fill x
2790
cb07fc2a 2791# -- Main Window Layout
a49c67d1 2792#
a0592d3f
JS
2793panedwindow .vpane -orient horizontal
2794panedwindow .vpane.files -orient vertical
c5a1eb88 2795.vpane add .vpane.files -sticky nsew -height 100 -width 200
cb07fc2a
SP
2796pack .vpane -anchor n -side top -fill both -expand 1
2797
2798# -- Index File List
a49c67d1 2799#
c5a1eb88 2800frame .vpane.files.index -height 100 -width 200
c73ce762 2801label .vpane.files.index.title -text [mc "Staged Changes (Will Commit)"] \
c382fdd7
PH
2802 -background lightgreen -foreground black
2803text $ui_index -background white -foreground black \
2804 -borderwidth 0 \
c5a1eb88 2805 -width 20 -height 10 \
3c236977 2806 -wrap none \
6c6dd01a 2807 -cursor $cursor_ptr \
3c236977
SP
2808 -xscrollcommand {.vpane.files.index.sx set} \
2809 -yscrollcommand {.vpane.files.index.sy set} \
cb07fc2a 2810 -state disabled
3c236977
SP
2811scrollbar .vpane.files.index.sx -orient h -command [list $ui_index xview]
2812scrollbar .vpane.files.index.sy -orient v -command [list $ui_index yview]
cb07fc2a 2813pack .vpane.files.index.title -side top -fill x
3c236977
SP
2814pack .vpane.files.index.sx -side bottom -fill x
2815pack .vpane.files.index.sy -side right -fill y
cb07fc2a 2816pack $ui_index -side left -fill both -expand 1
cb07fc2a 2817
0812665e 2818# -- Working Directory File List
a49c67d1 2819#
c5a1eb88 2820frame .vpane.files.workdir -height 100 -width 200
c73ce762 2821label .vpane.files.workdir.title -text [mc "Unstaged Changes"] \
c382fdd7
PH
2822 -background lightsalmon -foreground black
2823text $ui_workdir -background white -foreground black \
2824 -borderwidth 0 \
c5a1eb88 2825 -width 20 -height 10 \
3c236977 2826 -wrap none \
6c6dd01a 2827 -cursor $cursor_ptr \
3c236977
SP
2828 -xscrollcommand {.vpane.files.workdir.sx set} \
2829 -yscrollcommand {.vpane.files.workdir.sy set} \
cb07fc2a 2830 -state disabled
3c236977
SP
2831scrollbar .vpane.files.workdir.sx -orient h -command [list $ui_workdir xview]
2832scrollbar .vpane.files.workdir.sy -orient v -command [list $ui_workdir yview]
0812665e 2833pack .vpane.files.workdir.title -side top -fill x
3c236977
SP
2834pack .vpane.files.workdir.sx -side bottom -fill x
2835pack .vpane.files.workdir.sy -side right -fill y
0812665e 2836pack $ui_workdir -side left -fill both -expand 1
a0592d3f 2837
0812665e 2838.vpane.files add .vpane.files.workdir -sticky nsew
a0592d3f 2839.vpane.files add .vpane.files.index -sticky nsew
cb07fc2a 2840
0812665e 2841foreach i [list $ui_index $ui_workdir] {
3849bfba
SP
2842 rmsel_tag $i
2843 $i tag conf in_diff -background [$i tag cget in_sel -background]
24263b77
SP
2844}
2845unset i
131f503b 2846
0fb8f9ce 2847# -- Diff and Commit Area
a49c67d1 2848#
8009dcdc 2849frame .vpane.lower -height 300 -width 400
0fb8f9ce
SP
2850frame .vpane.lower.commarea
2851frame .vpane.lower.diff -relief sunken -borderwidth 1
a0592d3f
JS
2852pack .vpane.lower.diff -fill both -expand 1
2853pack .vpane.lower.commarea -side bottom -fill x
0fd49d0a 2854.vpane add .vpane.lower -sticky nsew
cb07fc2a
SP
2855
2856# -- Commit Area Buttons
a49c67d1 2857#
0fb8f9ce
SP
2858frame .vpane.lower.commarea.buttons
2859label .vpane.lower.commarea.buttons.l -text {} \
cb07fc2a 2860 -anchor w \
7416bbc6 2861 -justify left
0fb8f9ce
SP
2862pack .vpane.lower.commarea.buttons.l -side top -fill x
2863pack .vpane.lower.commarea.buttons -side left -fill y
131f503b 2864
1ac17950 2865button .vpane.lower.commarea.buttons.rescan -text [mc Rescan] \
8056cc4f 2866 -command ui_do_rescan
0fb8f9ce 2867pack .vpane.lower.commarea.buttons.rescan -side top -fill x
390adaea
SP
2868lappend disable_on_lock \
2869 {.vpane.lower.commarea.buttons.rescan conf -state}
131f503b 2870
1ac17950 2871button .vpane.lower.commarea.buttons.incall -text [mc "Stage Changed"] \
7416bbc6 2872 -command do_add_all
7fe7e733 2873pack .vpane.lower.commarea.buttons.incall -side top -fill x
390adaea
SP
2874lappend disable_on_lock \
2875 {.vpane.lower.commarea.buttons.incall conf -state}
131f503b 2876
ed70e4d7 2877if {![is_enabled nocommitmsg]} {
1e02b32e
SP
2878 button .vpane.lower.commarea.buttons.signoff -text [mc "Sign Off"] \
2879 -command do_signoff
2880 pack .vpane.lower.commarea.buttons.signoff -side top -fill x
2881}
131f503b 2882
1e65c622 2883button .vpane.lower.commarea.buttons.commit -text [commit_btn_caption] \
7416bbc6 2884 -command do_commit
0fb8f9ce 2885pack .vpane.lower.commarea.buttons.commit -side top -fill x
390adaea
SP
2886lappend disable_on_lock \
2887 {.vpane.lower.commarea.buttons.commit conf -state}
cb07fc2a 2888
1e02b32e
SP
2889if {![is_enabled nocommit]} {
2890 button .vpane.lower.commarea.buttons.push -text [mc Push] \
2891 -command do_push_anywhere
2892 pack .vpane.lower.commarea.buttons.push -side top -fill x
1e65c622
AG
2893}
2894
cb07fc2a 2895# -- Commit Message Buffer
a49c67d1 2896#
0fb8f9ce 2897frame .vpane.lower.commarea.buffer
24ac9b75 2898frame .vpane.lower.commarea.buffer.header
0fb8f9ce 2899set ui_comm .vpane.lower.commarea.buffer.t
24ac9b75 2900set ui_coml .vpane.lower.commarea.buffer.header.l
1e02b32e
SP
2901
2902if {![is_enabled nocommit]} {
2903 radiobutton .vpane.lower.commarea.buffer.header.new \
2904 -text [mc "New Commit"] \
2905 -command do_select_commit_type \
2906 -variable selected_commit_type \
2907 -value new
2908 lappend disable_on_lock \
2909 [list .vpane.lower.commarea.buffer.header.new conf -state]
2910 radiobutton .vpane.lower.commarea.buffer.header.amend \
2911 -text [mc "Amend Last Commit"] \
2912 -command do_select_commit_type \
2913 -variable selected_commit_type \
2914 -value amend
2915 lappend disable_on_lock \
2916 [list .vpane.lower.commarea.buffer.header.amend conf -state]
2917}
2918
a49c67d1 2919label $ui_coml \
cb07fc2a 2920 -anchor w \
7416bbc6 2921 -justify left
4539eacd
SP
2922proc trace_commit_type {varname args} {
2923 global ui_coml commit_type
2924 switch -glob -- $commit_type {
1ac17950
CS
2925 initial {set txt [mc "Initial Commit Message:"]}
2926 amend {set txt [mc "Amended Commit Message:"]}
2927 amend-initial {set txt [mc "Amended Initial Commit Message:"]}
2928 amend-merge {set txt [mc "Amended Merge Commit Message:"]}
2929 merge {set txt [mc "Merge Commit Message:"]}
2930 * {set txt [mc "Commit Message:"]}
4539eacd
SP
2931 }
2932 $ui_coml conf -text $txt
2933}
2934trace add variable commit_type write trace_commit_type
24ac9b75 2935pack $ui_coml -side left -fill x
1e02b32e
SP
2936
2937if {![is_enabled nocommit]} {
2938 pack .vpane.lower.commarea.buffer.header.amend -side right
2939 pack .vpane.lower.commarea.buffer.header.new -side right
2940}
24ac9b75 2941
c382fdd7
PH
2942text $ui_comm -background white -foreground black \
2943 -borderwidth 1 \
9861671d 2944 -undo true \
b2c6fcf1 2945 -maxundo 20 \
9861671d 2946 -autoseparators true \
cb07fc2a 2947 -relief sunken \
11027d54 2948 -width $repo_config(gui.commitmsgwidth) -height 9 -wrap none \
b4946930 2949 -font font_diff \
6c6dd01a 2950 -yscrollcommand {.vpane.lower.commarea.buffer.sby set}
390adaea
SP
2951scrollbar .vpane.lower.commarea.buffer.sby \
2952 -command [list $ui_comm yview]
24ac9b75 2953pack .vpane.lower.commarea.buffer.header -side top -fill x
0fb8f9ce 2954pack .vpane.lower.commarea.buffer.sby -side right -fill y
cb07fc2a 2955pack $ui_comm -side left -fill y
0fb8f9ce
SP
2956pack .vpane.lower.commarea.buffer -side left -fill y
2957
0e794311
SP
2958# -- Commit Message Buffer Context Menu
2959#
e8ab6446
SP
2960set ctxm .vpane.lower.commarea.buffer.ctxm
2961menu $ctxm -tearoff 0
2962$ctxm add command \
1ac17950 2963 -label [mc Cut] \
e8ab6446
SP
2964 -command {tk_textCut $ui_comm}
2965$ctxm add command \
1ac17950 2966 -label [mc Copy] \
e8ab6446
SP
2967 -command {tk_textCopy $ui_comm}
2968$ctxm add command \
1ac17950 2969 -label [mc Paste] \
e8ab6446
SP
2970 -command {tk_textPaste $ui_comm}
2971$ctxm add command \
1ac17950 2972 -label [mc Delete] \
bf516eca 2973 -command {catch {$ui_comm delete sel.first sel.last}}
e8ab6446
SP
2974$ctxm add separator
2975$ctxm add command \
1ac17950 2976 -label [mc "Select All"] \
75e78c8a 2977 -command {focus $ui_comm;$ui_comm tag add sel 0.0 end}
e8ab6446 2978$ctxm add command \
1ac17950 2979 -label [mc "Copy All"] \
e8ab6446 2980 -command {
0e794311
SP
2981 $ui_comm tag add sel 0.0 end
2982 tk_textCopy $ui_comm
2983 $ui_comm tag remove sel 0.0 end
e8ab6446
SP
2984 }
2985$ctxm add separator
2986$ctxm add command \
1ac17950 2987 -label [mc "Sign Off"] \
0e794311 2988 -command do_signoff
95b002ee 2989set ui_comm_ctxm $ctxm
0e794311 2990
0fb8f9ce 2991# -- Diff Header
a49c67d1 2992#
20a53c02
SP
2993proc trace_current_diff_path {varname args} {
2994 global current_diff_path diff_actions file_states
2995 if {$current_diff_path eq {}} {
e8ab6446
SP
2996 set s {}
2997 set f {}
2998 set p {}
2999 set o disabled
3000 } else {
20a53c02 3001 set p $current_diff_path
e8ab6446 3002 set s [mapdesc [lindex $file_states($p) 0] $p]
1ac17950 3003 set f [mc "File:"]
e8ab6446
SP
3004 set p [escape_path $p]
3005 set o normal
3006 }
3007
3008 .vpane.lower.diff.header.status configure -text $s
3009 .vpane.lower.diff.header.file configure -text $f
3010 .vpane.lower.diff.header.path configure -text $p
3011 foreach w $diff_actions {
3012 uplevel #0 $w $o
3013 }
3014}
20a53c02 3015trace add variable current_diff_path write trace_current_diff_path
e8ab6446 3016
9adccb05 3017frame .vpane.lower.diff.header -background gold
e8ab6446 3018label .vpane.lower.diff.header.status \
9adccb05 3019 -background gold \
c382fdd7 3020 -foreground black \
3e7b0e1d
SP
3021 -width $max_status_desc \
3022 -anchor w \
7416bbc6 3023 -justify left
e8ab6446 3024label .vpane.lower.diff.header.file \
9adccb05 3025 -background gold \
c382fdd7 3026 -foreground black \
e8ab6446 3027 -anchor w \
7416bbc6 3028 -justify left
e8ab6446 3029label .vpane.lower.diff.header.path \
9adccb05 3030 -background gold \
c382fdd7 3031 -foreground black \
fce89e46 3032 -anchor w \
7416bbc6 3033 -justify left
e8ab6446
SP
3034pack .vpane.lower.diff.header.status -side left
3035pack .vpane.lower.diff.header.file -side left
3036pack .vpane.lower.diff.header.path -fill x
3037set ctxm .vpane.lower.diff.header.ctxm
3038menu $ctxm -tearoff 0
3039$ctxm add command \
1ac17950 3040 -label [mc Copy] \
fce89e46
SP
3041 -command {
3042 clipboard clear
3043 clipboard append \
3044 -format STRING \
3045 -type STRING \
20a53c02 3046 -- $current_diff_path
fce89e46 3047 }
e8ab6446
SP
3048lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
3049bind_button3 .vpane.lower.diff.header.path "tk_popup $ctxm %X %Y"
0fb8f9ce
SP
3050
3051# -- Diff Body
a49c67d1 3052#
0fb8f9ce
SP
3053frame .vpane.lower.diff.body
3054set ui_diff .vpane.lower.diff.body.t
c382fdd7
PH
3055text $ui_diff -background white -foreground black \
3056 -borderwidth 0 \
0fb8f9ce 3057 -width 80 -height 15 -wrap none \
b4946930 3058 -font font_diff \
0fb8f9ce
SP
3059 -xscrollcommand {.vpane.lower.diff.body.sbx set} \
3060 -yscrollcommand {.vpane.lower.diff.body.sby set} \
0fb8f9ce
SP
3061 -state disabled
3062scrollbar .vpane.lower.diff.body.sbx -orient horizontal \
3063 -command [list $ui_diff xview]
3064scrollbar .vpane.lower.diff.body.sby -orient vertical \
3065 -command [list $ui_diff yview]
3066pack .vpane.lower.diff.body.sbx -side bottom -fill x
3067pack .vpane.lower.diff.body.sby -side right -fill y
3068pack $ui_diff -side left -fill both -expand 1
3069pack .vpane.lower.diff.header -side top -fill x
3070pack .vpane.lower.diff.body -side bottom -fill both -expand 1
3071
30b14ed3 3072$ui_diff tag conf d_cr -elide true
ca521566
SP
3073$ui_diff tag conf d_@ -foreground blue -font font_diffbold
3074$ui_diff tag conf d_+ -foreground {#00a000}
fec4a785
SP
3075$ui_diff tag conf d_- -foreground red
3076
ca521566 3077$ui_diff tag conf d_++ -foreground {#00a000}
fec4a785
SP
3078$ui_diff tag conf d_-- -foreground red
3079$ui_diff tag conf d_+s \
ca521566
SP
3080 -foreground {#00a000} \
3081 -background {#e2effa}
fec4a785
SP
3082$ui_diff tag conf d_-s \
3083 -foreground red \
ca521566 3084 -background {#e2effa}
fec4a785 3085$ui_diff tag conf d_s+ \
ca521566
SP
3086 -foreground {#00a000} \
3087 -background ivory1
fec4a785
SP
3088$ui_diff tag conf d_s- \
3089 -foreground red \
ca521566 3090 -background ivory1
fec4a785
SP
3091
3092$ui_diff tag conf d<<<<<<< \
3093 -foreground orange \
3094 -font font_diffbold
3095$ui_diff tag conf d======= \
3096 -foreground orange \
3097 -font font_diffbold
3098$ui_diff tag conf d>>>>>>> \
3099 -foreground orange \
3100 -font font_diffbold
cb07fc2a 3101
ca521566
SP
3102$ui_diff tag raise sel
3103
0e794311
SP
3104# -- Diff Body Context Menu
3105#
042c2325
AG
3106
3107proc create_common_diff_popup {ctxm} {
3108 $ctxm add command \
3109 -label [mc "Show Less Context"] \
3110 -command show_less_context
3111 lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
3112 $ctxm add command \
3113 -label [mc "Show More Context"] \
3114 -command show_more_context
3115 lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
3116 $ctxm add separator
3117 $ctxm add command \
3118 -label [mc Refresh] \
3119 -command reshow_diff
3120 lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
3121 $ctxm add command \
3122 -label [mc Copy] \
3123 -command {tk_textCopy $ui_diff}
3124 lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
3125 $ctxm add command \
3126 -label [mc "Select All"] \
3127 -command {focus $ui_diff;$ui_diff tag add sel 0.0 end}
3128 lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
3129 $ctxm add command \
3130 -label [mc "Copy All"] \
3131 -command {
3132 $ui_diff tag add sel 0.0 end
3133 tk_textCopy $ui_diff
3134 $ui_diff tag remove sel 0.0 end
3135 }
3136 lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
3137 $ctxm add separator
3138 $ctxm add command \
3139 -label [mc "Decrease Font Size"] \
3140 -command {incr_font_size font_diff -1}
3141 lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
3142 $ctxm add command \
3143 -label [mc "Increase Font Size"] \
3144 -command {incr_font_size font_diff 1}
3145 lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
3146 $ctxm add separator
3fe01623
AG
3147 set emenu $ctxm.enc
3148 menu $emenu
3149 build_encoding_menu $emenu [list force_diff_encoding]
3150 $ctxm add cascade \
3151 -label [mc "Encoding"] \
3152 -menu $emenu
3153 lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
3154 $ctxm add separator
042c2325
AG
3155 $ctxm add command -label [mc "Options..."] \
3156 -command do_options
3157}
3158
e8ab6446
SP
3159set ctxm .vpane.lower.diff.body.ctxm
3160menu $ctxm -tearoff 0
fba6072e
JS
3161$ctxm add command \
3162 -label [mc "Apply/Reverse Hunk"] \
3163 -command {apply_hunk $cursorX $cursorY}
3164set ui_diff_applyhunk [$ctxm index last]
3165lappend diff_actions [list $ctxm entryconf $ui_diff_applyhunk -state]
5821988f
JS
3166$ctxm add command \
3167 -label [mc "Apply/Reverse Line"] \
3168 -command {apply_line $cursorX $cursorY; do_rescan}
3169set ui_diff_applyline [$ctxm index last]
3170lappend diff_actions [list $ctxm entryconf $ui_diff_applyline -state]
fba6072e 3171$ctxm add separator
042c2325
AG
3172create_common_diff_popup $ctxm
3173
3174set ctxmmg .vpane.lower.diff.body.ctxmmg
3175menu $ctxmmg -tearoff 0
7e30682c
AG
3176$ctxmmg add command \
3177 -label [mc "Run Merge Tool"] \
3178 -command {merge_resolve_tool}
3179lappend diff_actions [list $ctxmmg entryconf [$ctxmmg index last] -state]
3180$ctxmmg add separator
042c2325
AG
3181$ctxmmg add command \
3182 -label [mc "Use Remote Version"] \
3183 -command {merge_resolve_one 3}
3184lappend diff_actions [list $ctxmmg entryconf [$ctxmmg index last] -state]
3185$ctxmmg add command \
3186 -label [mc "Use Local Version"] \
3187 -command {merge_resolve_one 2}
3188lappend diff_actions [list $ctxmmg entryconf [$ctxmmg index last] -state]
3189$ctxmmg add command \
3190 -label [mc "Revert To Base"] \
3191 -command {merge_resolve_one 1}
3192lappend diff_actions [list $ctxmmg entryconf [$ctxmmg index last] -state]
3193$ctxmmg add separator
3194create_common_diff_popup $ctxmmg
3195
3196proc popup_diff_menu {ctxm ctxmmg x y X Y} {
ce015c21 3197 global current_diff_path file_states
83751fc1
SP
3198 set ::cursorX $x
3199 set ::cursorY $y
042c2325
AG
3200 if {[info exists file_states($current_diff_path)]} {
3201 set state [lindex $file_states($current_diff_path) 0]
a25c5189 3202 } else {
042c2325
AG
3203 set state {__}
3204 }
ff515d81 3205 if {[string first {U} $state] >= 0} {
042c2325 3206 tk_popup $ctxmmg $X $Y
047d94d5 3207 } else {
042c2325
AG
3208 if {$::ui_index eq $::current_diff_side} {
3209 set l [mc "Unstage Hunk From Commit"]
3210 set t [mc "Unstage Line From Commit"]
3211 } else {
3212 set l [mc "Stage Hunk For Commit"]
3213 set t [mc "Stage Line For Commit"]
3214 }
3215 if {$::is_3way_diff
3216 || $current_diff_path eq {}
3217 || {__} eq $state
3218 || {_O} eq $state
3219 || {_T} eq $state
3220 || {T_} eq $state} {
3221 set s disabled
3222 } else {
3223 set s normal
3224 }
3225 $ctxm entryconf $::ui_diff_applyhunk -state $s -label $l
3226 $ctxm entryconf $::ui_diff_applyline -state $s -label $t
3227 tk_popup $ctxm $X $Y
9c9f5fa9 3228 }
83751fc1 3229}
042c2325 3230bind_button3 $ui_diff [list popup_diff_menu $ctxm $ctxmmg %x %y %X %Y]
0e794311 3231
cb07fc2a 3232# -- Status Bar
e8ab6446 3233#
51530d17 3234set main_status [::status_bar::new .status]
cb07fc2a 3235pack .status -anchor w -side bottom -fill x
1ac17950 3236$main_status show [mc "Initializing..."]
cb07fc2a 3237
2d19516d 3238# -- Load geometry
e8ab6446 3239#
2d19516d 3240catch {
51f4d16b 3241set gm $repo_config(gui.geometry)
c4fe7728
SP
3242wm geometry . [lindex $gm 0]
3243.vpane sash place 0 \
a0592d3f
JS
3244 [lindex $gm 1] \
3245 [lindex [.vpane sash coord 0] 1]
c4fe7728 3246.vpane.files sash place 0 \
a0592d3f
JS
3247 [lindex [.vpane.files sash coord 0] 0] \
3248 [lindex $gm 2]
c4fe7728 3249unset gm
390adaea 3250}
2d19516d 3251
cb07fc2a 3252# -- Key Bindings
e8ab6446 3253#
ec6b424a 3254bind $ui_comm <$M1B-Key-Return> {do_commit;break}
cd16a6c9
SP
3255bind $ui_comm <$M1B-Key-t> {do_add_selection;break}
3256bind $ui_comm <$M1B-Key-T> {do_add_selection;break}
93e912c5
SP
3257bind $ui_comm <$M1B-Key-i> {do_add_all;break}
3258bind $ui_comm <$M1B-Key-I> {do_add_all;break}
9861671d
SP
3259bind $ui_comm <$M1B-Key-x> {tk_textCut %W;break}
3260bind $ui_comm <$M1B-Key-X> {tk_textCut %W;break}
3261bind $ui_comm <$M1B-Key-c> {tk_textCopy %W;break}
3262bind $ui_comm <$M1B-Key-C> {tk_textCopy %W;break}
3263bind $ui_comm <$M1B-Key-v> {tk_textPaste %W; %W see insert; break}
3264bind $ui_comm <$M1B-Key-V> {tk_textPaste %W; %W see insert; break}
3265bind $ui_comm <$M1B-Key-a> {%W tag add sel 0.0 end;break}
3266bind $ui_comm <$M1B-Key-A> {%W tag add sel 0.0 end;break}
729ffa50
MB
3267bind $ui_comm <$M1B-Key-minus> {show_less_context;break}
3268bind $ui_comm <$M1B-Key-KP_Subtract> {show_less_context;break}
3269bind $ui_comm <$M1B-Key-equal> {show_more_context;break}
3270bind $ui_comm <$M1B-Key-plus> {show_more_context;break}
3271bind $ui_comm <$M1B-Key-KP_Add> {show_more_context;break}
9861671d
SP
3272
3273bind $ui_diff <$M1B-Key-x> {tk_textCopy %W;break}
3274bind $ui_diff <$M1B-Key-X> {tk_textCopy %W;break}
3275bind $ui_diff <$M1B-Key-c> {tk_textCopy %W;break}
3276bind $ui_diff <$M1B-Key-C> {tk_textCopy %W;break}
3277bind $ui_diff <$M1B-Key-v> {break}
3278bind $ui_diff <$M1B-Key-V> {break}
3279bind $ui_diff <$M1B-Key-a> {%W tag add sel 0.0 end;break}
3280bind $ui_diff <$M1B-Key-A> {%W tag add sel 0.0 end;break}
b2c6fcf1
SP
3281bind $ui_diff <Key-Up> {catch {%W yview scroll -1 units};break}
3282bind $ui_diff <Key-Down> {catch {%W yview scroll 1 units};break}
3283bind $ui_diff <Key-Left> {catch {%W xview scroll -1 units};break}
3284bind $ui_diff <Key-Right> {catch {%W xview scroll 1 units};break}
60aa065f
SP
3285bind $ui_diff <Key-k> {catch {%W yview scroll -1 units};break}
3286bind $ui_diff <Key-j> {catch {%W yview scroll 1 units};break}
3287bind $ui_diff <Key-h> {catch {%W xview scroll -1 units};break}
3288bind $ui_diff <Key-l> {catch {%W xview scroll 1 units};break}
3289bind $ui_diff <Control-Key-b> {catch {%W yview scroll -1 pages};break}
3290bind $ui_diff <Control-Key-f> {catch {%W yview scroll 1 pages};break}
23effa79 3291bind $ui_diff <Button-1> {focus %W}
49b86f01 3292
64a906f8 3293if {[is_enabled branch]} {
b1fa2bff
SP
3294 bind . <$M1B-Key-n> branch_create::dialog
3295 bind . <$M1B-Key-N> branch_create::dialog
d41b43eb
SP
3296 bind . <$M1B-Key-o> branch_checkout::dialog
3297 bind . <$M1B-Key-O> branch_checkout::dialog
a870ddc0
SP
3298 bind . <$M1B-Key-m> merge::dialog
3299 bind . <$M1B-Key-M> merge::dialog
bd29ebc3 3300}
840bcfa7
SP
3301if {[is_enabled transport]} {
3302 bind . <$M1B-Key-p> do_push_anywhere
3303 bind . <$M1B-Key-P> do_push_anywhere
3304}
bd29ebc3 3305
8056cc4f
AG
3306bind . <Key-F5> ui_do_rescan
3307bind . <$M1B-Key-r> ui_do_rescan
3308bind . <$M1B-Key-R> ui_do_rescan
07123f40
SP
3309bind . <$M1B-Key-s> do_signoff
3310bind . <$M1B-Key-S> do_signoff
cd16a6c9
SP
3311bind . <$M1B-Key-t> do_add_selection
3312bind . <$M1B-Key-T> do_add_selection
93e912c5
SP
3313bind . <$M1B-Key-i> do_add_all
3314bind . <$M1B-Key-I> do_add_all
729ffa50
MB
3315bind . <$M1B-Key-minus> {show_less_context;break}
3316bind . <$M1B-Key-KP_Subtract> {show_less_context;break}
3317bind . <$M1B-Key-equal> {show_more_context;break}
3318bind . <$M1B-Key-plus> {show_more_context;break}
3319bind . <$M1B-Key-KP_Add> {show_more_context;break}
07123f40 3320bind . <$M1B-Key-Return> do_commit
0812665e 3321foreach i [list $ui_index $ui_workdir] {
24263b77
SP
3322 bind $i <Button-1> "toggle_or_diff $i %x %y; break"
3323 bind $i <$M1B-Button-1> "add_one_to_selection $i %x %y; break"
3324 bind $i <Shift-Button-1> "add_range_to_selection $i %x %y; break"
cb07fc2a 3325}
62aac80b
SP
3326unset i
3327
3328set file_lists($ui_index) [list]
0812665e 3329set file_lists($ui_workdir) [list]
a49c67d1 3330
19c82148 3331wm title . "[appname] ([reponame]) [file normalize [file dirname [gitdir]]]"
cb07fc2a 3332focus -force $ui_comm
1d8b3cbf 3333
85ab313e
SP
3334# -- Warn the user about environmental problems. Cygwin's Tcl
3335# does *not* pass its env array onto any processes it spawns.
3336# This means that git processes get none of our environment.
1d8b3cbf 3337#
20ddfcaa 3338if {[is_Cygwin]} {
1d8b3cbf
SP
3339 set ignored_env 0
3340 set suggest_user {}
c8c4854b 3341 set msg [mc "Possible environment issues exist.
1d8b3cbf
SP
3342
3343The following environment variables are probably
3344going to be ignored by any Git subprocess run
c8c4854b 3345by %s:
1d8b3cbf 3346
c8c4854b 3347" [appname]]
1d8b3cbf
SP
3348 foreach name [array names env] {
3349 switch -regexp -- $name {
3350 {^GIT_INDEX_FILE$} -
3351 {^GIT_OBJECT_DIRECTORY$} -
3352 {^GIT_ALTERNATE_OBJECT_DIRECTORIES$} -
3353 {^GIT_DIFF_OPTS$} -
3354 {^GIT_EXTERNAL_DIFF$} -
3355 {^GIT_PAGER$} -
3356 {^GIT_TRACE$} -
3357 {^GIT_CONFIG$} -
1d8b3cbf
SP
3358 {^GIT_(AUTHOR|COMMITTER)_DATE$} {
3359 append msg " - $name\n"
3360 incr ignored_env
3361 }
3362 {^GIT_(AUTHOR|COMMITTER)_(NAME|EMAIL)$} {
3363 append msg " - $name\n"
3364 incr ignored_env
3365 set suggest_user $name
3366 }
3367 }
3368 }
3369 if {$ignored_env > 0} {
c8c4854b 3370 append msg [mc "
1d8b3cbf 3371This is due to a known issue with the
c8c4854b 3372Tcl binary distributed by Cygwin."]
1d8b3cbf
SP
3373
3374 if {$suggest_user ne {}} {
c8c4854b 3375 append msg [mc "
1d8b3cbf 3376
c8c4854b 3377A good replacement for %s
1d8b3cbf
SP
3378is placing values for the user.name and
3379user.email settings into your personal
3380~/.gitconfig file.
c8c4854b 3381" $suggest_user]
1d8b3cbf
SP
3382 }
3383 warn_popup $msg
3384 }
3385 unset ignored_env msg suggest_user name
3386}
3387
85ab313e
SP
3388# -- Only initialize complex UI if we are going to stay running.
3389#
64a906f8 3390if {[is_enabled transport]} {
4ccdab02 3391 load_all_remotes
85ab313e 3392
6bdf5e5f 3393 set n [.mbar.remote index end]
8329bd07 3394 populate_remotes_menu
6bdf5e5f
SP
3395 set n [expr {[.mbar.remote index end] - $n}]
3396 if {$n > 0} {
7e09b153 3397 if {[.mbar.remote type 0] eq "tearoff"} { incr n }
6bdf5e5f
SP
3398 .mbar.remote insert $n separator
3399 }
3400 unset n
4ccdab02 3401}
85ab313e 3402
4578c5cb
SP
3403if {[winfo exists $ui_comm]} {
3404 set GITGUI_BCK_exists [load_message GITGUI_BCK]
3405
3406 # -- If both our backup and message files exist use the
3407 # newer of the two files to initialize the buffer.
3408 #
3409 if {$GITGUI_BCK_exists} {
3410 set m [gitdir GITGUI_MSG]
3411 if {[file isfile $m]} {
3412 if {[file mtime [gitdir GITGUI_BCK]] > [file mtime $m]} {
3413 catch {file delete [gitdir GITGUI_MSG]}
3414 } else {
3415 $ui_comm delete 0.0 end
3416 $ui_comm edit reset
3417 $ui_comm edit modified false
3418 catch {file delete [gitdir GITGUI_BCK]}
3419 set GITGUI_BCK_exists 0
3420 }
3421 }
3422 unset m
3423 }
3424
3425 proc backup_commit_buffer {} {
3426 global ui_comm GITGUI_BCK_exists
3427
3428 set m [$ui_comm edit modified]
3429 if {$m || $GITGUI_BCK_exists} {
3430 set msg [string trim [$ui_comm get 0.0 end]]
3431 regsub -all -line {[ \r\t]+$} $msg {} msg
3432
3433 if {$msg eq {}} {
3434 if {$GITGUI_BCK_exists} {
3435 catch {file delete [gitdir GITGUI_BCK]}
3436 set GITGUI_BCK_exists 0
3437 }
3438 } elseif {$m} {
3439 catch {
3440 set fd [open [gitdir GITGUI_BCK] w]
3441 puts -nonewline $fd $msg
3442 close $fd
3443 set GITGUI_BCK_exists 1
3444 }
3445 }
3446
3447 $ui_comm edit modified false
3448 }
3449
3450 set ::GITGUI_BCK_i [after 2000 backup_commit_buffer]
3451 }
3452
3453 backup_commit_buffer
95b002ee
SP
3454
3455 # -- If the user has aspell available we can drive it
3456 # in pipe mode to spellcheck the commit message.
3457 #
3458 set spell_cmd [list |]
3459 set spell_dict [get_config gui.spellingdictionary]
3460 lappend spell_cmd aspell
3461 if {$spell_dict ne {}} {
3462 lappend spell_cmd --master=$spell_dict
3463 }
3464 lappend spell_cmd --mode=none
3465 lappend spell_cmd --encoding=utf-8
3466 lappend spell_cmd pipe
3467 if {$spell_dict eq {none}
3468 || [catch {set spell_fd [open $spell_cmd r+]} spell_err]} {
3469 bind_button3 $ui_comm [list tk_popup $ui_comm_ctxm %X %Y]
3470 } else {
3471 set ui_comm_spell [spellcheck::init \
3472 $spell_fd \
3473 $ui_comm \
3474 $ui_comm_ctxm \
3475 ]
3476 }
3477 unset -nocomplain spell_cmd spell_fd spell_err spell_dict
4578c5cb
SP
3478}
3479
53716a7b 3480lock_index begin-read
301dfaa9
SP
3481if {![winfo ismapped .]} {
3482 wm deiconify .
3483}
1e65c622
AG
3484after 1 {
3485 if {[is_enabled initialamend]} {
3486 force_amend
3487 } else {
3488 do_rescan
3489 }
3490
3491 if {[is_enabled nocommitmsg]} {
3492 $ui_comm configure -state disabled -background gray
3493 }
3494}
3972b987
SP
3495if {[is_enabled multicommit]} {
3496 after 1000 hint_gc
3497}
1e65c622
AG
3498if {[is_enabled retcode]} {
3499 bind . <Destroy> {+terminate_me %W}
3500}
bb4812bc
PB
3501if {$picked && [is_config_true gui.autoexplore]} {
3502 do_explore
3503}