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