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