git-gui: update Italian translation
[git/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@@}
bdc9ea20 13set copyright {
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
28Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA}
cb07fc2a 29
f522c9b5 30######################################################################
cfb07cca
SP
31##
32## Tcl/Tk sanity check
33
34if {[catch {package require Tcl 8.4} err]
35 || [catch {package require Tk 8.4} err]
36} {
37 catch {wm withdraw .}
38 tk_messageBox \
39 -icon error \
40 -type ok \
c8c4854b 41 -title [mc "git-gui: fatal error"] \
cfb07cca
SP
42 -message $err
43 exit 1
44}
45
63c4024f 46catch {rename send {}} ; # What an evil concept...
cff93397 47
fc703c20
SP
48######################################################################
49##
50## locate our library
51
52set oguilib {@@GITGUI_LIBDIR@@}
53set oguirel {@@GITGUI_RELATIVE@@}
54if {$oguirel eq {1}} {
55 set oguilib [file dirname [file dirname [file normalize $argv0]]]
56 set oguilib [file join $oguilib share git-gui lib]
d4b0ccd9 57 set oguimsg [file join $oguilib msgs]
fc703c20
SP
58} elseif {[string match @@* $oguirel]} {
59 set oguilib [file join [file dirname [file normalize $argv0]] lib]
d4b0ccd9
SP
60 set oguimsg [file join [file dirname [file normalize $argv0]] po]
61} else {
62 set oguimsg [file join $oguilib msgs]
fc703c20
SP
63}
64unset oguirel
65
cd12901b
SP
66######################################################################
67##
68## enable verbose loading?
69
70if {![catch {set _verbose $env(GITGUI_VERBOSE)}]} {
71 unset _verbose
72 rename auto_load real__auto_load
73 proc auto_load {name args} {
74 puts stderr "auto_load $name"
75 return [uplevel 1 real__auto_load $name $args]
76 }
77 rename source real__source
78 proc source {name} {
79 puts stderr "source $name"
80 uplevel 1 real__source $name
81 }
82}
83
c950c66e 84######################################################################
d4b0ccd9
SP
85##
86## Internationalization (i18n) through msgcat and gettext. See
87## http://www.gnu.org/software/gettext/manual/html_node/Tcl.html
88
89package require msgcat
146d73a3
SP
90
91proc mc {fmt args} {
92 set fmt [::msgcat::mc $fmt]
93 set cmk [string first @@ $fmt]
94 if {$cmk > 0} {
95 set fmt [string range $fmt 0 [expr {$cmk - 1}]]
96 }
97 return [eval [list format $fmt] $args]
98}
99
31bb1d1b
SP
100proc strcat {args} {
101 return [join $args {}]
102}
103
d4b0ccd9
SP
104::msgcat::mcload $oguimsg
105unset oguimsg
106
107######################################################################
c950c66e
SP
108##
109## read only globals
110
0b2bc460 111set _appname {Git Gui}
c950c66e 112set _gitdir {}
20ddfcaa 113set _gitexec {}
c950c66e 114set _reponame {}
20ddfcaa 115set _iscygwin {}
0b812616 116set _search_path {}
c950c66e
SP
117
118proc appname {} {
119 global _appname
120 return $_appname
121}
122
c2758a17 123proc gitdir {args} {
c950c66e 124 global _gitdir
c2758a17
SP
125 if {$args eq {}} {
126 return $_gitdir
127 }
0b812616 128 return [eval [list file join $_gitdir] $args]
c950c66e
SP
129}
130
20ddfcaa
SP
131proc gitexec {args} {
132 global _gitexec
133 if {$_gitexec eq {}} {
81347223 134 if {[catch {set _gitexec [git --exec-path]} err]} {
20ddfcaa
SP
135 error "Git not installed?\n\n$err"
136 }
0b812616
SP
137 if {[is_Cygwin]} {
138 set _gitexec [exec cygpath \
139 --windows \
140 --absolute \
141 $_gitexec]
142 } else {
143 set _gitexec [file normalize $_gitexec]
144 }
20ddfcaa
SP
145 }
146 if {$args eq {}} {
147 return $_gitexec
148 }
0b812616 149 return [eval [list file join $_gitexec] $args]
20ddfcaa
SP
150}
151
c950c66e 152proc reponame {} {
d36cd968 153 return $::_reponame
c950c66e 154}
da5239dc 155
20ddfcaa 156proc is_MacOSX {} {
20ddfcaa
SP
157 if {[tk windowingsystem] eq {aqua}} {
158 return 1
159 }
160 return 0
161}
162
163proc is_Windows {} {
d36cd968 164 if {$::tcl_platform(platform) eq {windows}} {
20ddfcaa
SP
165 return 1
166 }
167 return 0
168}
169
170proc is_Cygwin {} {
d36cd968 171 global _iscygwin
20ddfcaa 172 if {$_iscygwin eq {}} {
d36cd968 173 if {$::tcl_platform(platform) eq {windows}} {
20ddfcaa
SP
174 if {[catch {set p [exec cygpath --windir]} err]} {
175 set _iscygwin 0
176 } else {
177 set _iscygwin 1
178 }
179 } else {
180 set _iscygwin 0
181 }
182 }
183 return $_iscygwin
184}
185
cf25ddc8
SP
186proc is_enabled {option} {
187 global enabled_options
188 if {[catch {set on $enabled_options($option)}]} {return 0}
189 return $on
190}
191
192proc enable_option {option} {
193 global enabled_options
194 set enabled_options($option) 1
195}
196
197proc disable_option {option} {
198 global enabled_options
199 set enabled_options($option) 0
200}
201
2d19516d
SP
202######################################################################
203##
204## config
205
51f4d16b
SP
206proc is_many_config {name} {
207 switch -glob -- $name {
208 remote.*.fetch -
209 remote.*.push
210 {return 1}
211 *
212 {return 0}
213 }
214}
2d19516d 215
c539449b
SP
216proc is_config_true {name} {
217 global repo_config
218 if {[catch {set v $repo_config($name)}]} {
219 return 0
220 } elseif {$v eq {true} || $v eq {1} || $v eq {yes}} {
221 return 1
222 } else {
223 return 0
224 }
225}
226
61f82ce7
SP
227proc get_config {name} {
228 global repo_config
229 if {[catch {set v $repo_config($name)}]} {
230 return {}
231 } else {
232 return $v
233 }
234}
235
6bbd1cb9 236proc load_config {include_global} {
51f4d16b
SP
237 global repo_config global_config default_config
238
239 array unset global_config
6bbd1cb9
SP
240 if {$include_global} {
241 catch {
0b812616 242 set fd_rc [git_read config --global --list]
6bbd1cb9
SP
243 while {[gets $fd_rc line] >= 0} {
244 if {[regexp {^([^=]+)=(.*)$} $line line name value]} {
245 if {[is_many_config $name]} {
246 lappend global_config($name) $value
247 } else {
248 set global_config($name) $value
249 }
51f4d16b
SP
250 }
251 }
6bbd1cb9 252 close $fd_rc
51f4d16b 253 }
51f4d16b 254 }
6bbd1cb9
SP
255
256 array unset repo_config
2d19516d 257 catch {
0b812616 258 set fd_rc [git_read config --list]
2d19516d
SP
259 while {[gets $fd_rc line] >= 0} {
260 if {[regexp {^([^=]+)=(.*)$} $line line name value]} {
51f4d16b
SP
261 if {[is_many_config $name]} {
262 lappend repo_config($name) $value
263 } else {
264 set repo_config($name) $value
265 }
2d19516d
SP
266 }
267 }
268 close $fd_rc
269 }
270
51f4d16b
SP
271 foreach name [array names default_config] {
272 if {[catch {set v $global_config($name)}]} {
273 set global_config($name) $default_config($name)
274 }
275 if {[catch {set v $repo_config($name)}]} {
276 set repo_config($name) $default_config($name)
277 }
2d19516d
SP
278 }
279}
280
81347223
SP
281######################################################################
282##
283## handy utils
284
0b812616
SP
285proc _git_cmd {name} {
286 global _git_cmd_path
287
288 if {[catch {set v $_git_cmd_path($name)}]} {
289 switch -- $name {
70a7595c 290 version -
0b812616
SP
291 --version -
292 --exec-path { return [list $::_git $name] }
293 }
294
295 set p [gitexec git-$name$::_search_exe]
296 if {[file exists $p]} {
297 set v [list $p]
c136f2b8
SP
298 } elseif {[is_Windows] && [file exists [gitexec git-$name]]} {
299 # Try to determine what sort of magic will make
300 # git-$name go and do its thing, because native
301 # Tcl on Windows doesn't know it.
0b812616 302 #
c136f2b8
SP
303 set p [gitexec git-$name]
304 set f [open $p r]
305 set s [gets $f]
306 close $f
307
6e4ba05c 308 switch -glob -- [lindex $s 0] {
c136f2b8
SP
309 #!*sh { set i sh }
310 #!*perl { set i perl }
311 #!*python { set i python }
312 default { error "git-$name is not supported: $s" }
313 }
314
315 upvar #0 _$i interp
316 if {![info exists interp]} {
317 set interp [_which $i]
318 }
319 if {$interp eq {}} {
320 error "git-$name requires $i (not in PATH)"
321 }
6e4ba05c 322 set v [concat [list $interp] [lrange $s 1 end] [list $p]]
0b812616 323 } else {
c6729890
SP
324 # Assume it is builtin to git somehow and we
325 # aren't actually able to see a file for it.
326 #
327 set v [list $::_git $name]
0b812616
SP
328 }
329 set _git_cmd_path($name) $v
330 }
331 return $v
332}
333
334proc _which {what} {
335 global env _search_exe _search_path
336
337 if {$_search_path eq {}} {
299077fb 338 if {[is_Cygwin] && [regexp {^(/|\.:)} $env(PATH)]} {
0b812616
SP
339 set _search_path [split [exec cygpath \
340 --windows \
341 --path \
342 --absolute \
343 $env(PATH)] {;}]
344 set _search_exe .exe
345 } elseif {[is_Windows]} {
be700fe3
SP
346 set gitguidir [file dirname [info script]]
347 regsub -all ";" $gitguidir "\\;" gitguidir
348 set env(PATH) "$gitguidir;$env(PATH)"
0b812616
SP
349 set _search_path [split $env(PATH) {;}]
350 set _search_exe .exe
351 } else {
352 set _search_path [split $env(PATH) :]
353 set _search_exe {}
354 }
355 }
356
357 foreach p $_search_path {
358 set p [file join $p $what$_search_exe]
359 if {[file exists $p]} {
360 return [file normalize $p]
361 }
362 }
363 return {}
364}
365
6f62b4f7
SP
366proc _lappend_nice {cmd_var} {
367 global _nice
368 upvar $cmd_var cmd
369
370 if {![info exists _nice]} {
371 set _nice [_which nice]
372 }
373 if {$_nice ne {}} {
374 lappend cmd $_nice
375 }
376}
377
81347223 378proc git {args} {
0b812616
SP
379 set opt [list exec]
380
381 while {1} {
382 switch -- [lindex $args 0] {
383 --nice {
6f62b4f7 384 _lappend_nice opt
0b812616
SP
385 }
386
387 default {
388 break
389 }
390
391 }
392
393 set args [lrange $args 1 end]
394 }
395
396 set cmdp [_git_cmd [lindex $args 0]]
397 set args [lrange $args 1 end]
398
399 return [eval $opt $cmdp $args]
400}
401
74c4763c
SP
402proc _open_stdout_stderr {cmd} {
403 if {[catch {
404 set fd [open $cmd r]
405 } err]} {
406 if { [lindex $cmd end] eq {2>@1}
407 && $err eq {can not find channel named "1"}
408 } {
409 # Older versions of Tcl 8.4 don't have this 2>@1 IO
410 # redirect operator. Fallback to |& cat for those.
411 # The command was not actually started, so its safe
412 # to try to start it a second time.
413 #
414 set fd [open [concat \
415 [lrange $cmd 0 end-1] \
416 [list |& cat] \
417 ] r]
418 } else {
419 error $err
420 }
421 }
6eb420ef 422 fconfigure $fd -eofchar {}
74c4763c
SP
423 return $fd
424}
425
0b812616
SP
426proc git_read {args} {
427 set opt [list |]
428
429 while {1} {
430 switch -- [lindex $args 0] {
431 --nice {
6f62b4f7 432 _lappend_nice opt
0b812616
SP
433 }
434
435 --stderr {
436 lappend args 2>@1
437 }
438
439 default {
440 break
441 }
442
443 }
444
445 set args [lrange $args 1 end]
446 }
447
448 set cmdp [_git_cmd [lindex $args 0]]
449 set args [lrange $args 1 end]
450
74c4763c 451 return [_open_stdout_stderr [concat $opt $cmdp $args]]
0b812616
SP
452}
453
454proc git_write {args} {
455 set opt [list |]
456
457 while {1} {
458 switch -- [lindex $args 0] {
459 --nice {
6f62b4f7 460 _lappend_nice opt
0b812616
SP
461 }
462
463 default {
464 break
465 }
466
467 }
468
469 set args [lrange $args 1 end]
470 }
471
472 set cmdp [_git_cmd [lindex $args 0]]
473 set args [lrange $args 1 end]
474
475 return [open [concat $opt $cmdp $args] w]
81347223
SP
476}
477
7eafa2f1
SP
478proc sq {value} {
479 regsub -all ' $value "'\\''" value
480 return "'$value'"
481}
482
d41b43eb
SP
483proc load_current_branch {} {
484 global current_branch is_detached
485
fc4e8da7 486 set fd [open [gitdir HEAD] r]
311e02a4 487 if {[gets $fd ref] < 1} {
fc4e8da7
SP
488 set ref {}
489 }
490 close $fd
311e02a4
SP
491
492 set pfx {ref: refs/heads/}
493 set len [string length $pfx]
494 if {[string equal -length $len $pfx $ref]} {
495 # We're on a branch. It might not exist. But
496 # HEAD looks good enough to be a branch.
497 #
d41b43eb
SP
498 set current_branch [string range $ref $len end]
499 set is_detached 0
311e02a4
SP
500 } else {
501 # Assume this is a detached head.
502 #
d41b43eb
SP
503 set current_branch HEAD
504 set is_detached 1
311e02a4 505 }
fc4e8da7
SP
506}
507
2739291b
SP
508auto_load tk_optionMenu
509rename tk_optionMenu real__tkOptionMenu
510proc tk_optionMenu {w varName args} {
511 set m [eval real__tkOptionMenu $w $varName $args]
512 $m configure -font font_ui
513 $w configure -font font_ui
514 return $m
515}
516
3849bfba
SP
517proc rmsel_tag {text} {
518 $text tag conf sel \
519 -background [$text cget -background] \
520 -foreground [$text cget -foreground] \
521 -borderwidth 0
522 $text tag conf in_sel -background lightgray
523 bind $text <Motion> break
524 return $text
525}
526
a4bee597
SP
527set root_exists 0
528bind . <Visibility> {
529 bind . <Visibility> {}
530 set root_exists 1
531}
532
1bdd8a15
SP
533if {[is_Windows]} {
534 wm iconbitmap . -default $oguilib/git-gui.ico
535}
536
a4bee597
SP
537######################################################################
538##
539## config defaults
540
541set cursor_ptr arrow
542font create font_diff -family Courier -size 10
543font create font_ui
544catch {
545 label .dummy
546 eval font configure font_ui [font actual [.dummy cget -font]]
547 destroy .dummy
548}
549
550font create font_uiitalic
551font create font_uibold
552font create font_diffbold
553font create font_diffitalic
554
555foreach class {Button Checkbutton Entry Label
556 Labelframe Listbox Menu Message
557 Radiobutton Spinbox Text} {
558 option add *$class.font font_ui
559}
560unset class
561
562if {[is_Windows] || [is_MacOSX]} {
563 option add *Menu.tearOff 0
564}
565
566if {[is_MacOSX]} {
567 set M1B M1
568 set M1T Cmd
569} else {
570 set M1B Control
571 set M1T Ctrl
572}
573
574proc bind_button3 {w cmd} {
575 bind $w <Any-Button-3> $cmd
576 if {[is_MacOSX]} {
577 # Mac OS X sends Button-2 on right click through three-button mouse,
578 # or through trackpad right-clicking (two-finger touch + click).
579 bind $w <Any-Button-2> $cmd
580 bind $w <Control-Button-1> $cmd
581 }
582}
583
584proc apply_config {} {
585 global repo_config font_descs
586
587 foreach option $font_descs {
588 set name [lindex $option 0]
589 set font [lindex $option 1]
590 if {[catch {
591 foreach {cn cv} $repo_config(gui.$name) {
592 font configure $font $cn $cv -weight normal
593 }
594 } err]} {
595 error_popup [strcat [mc "Invalid font specified in %s:" "gui.$name"] "\n\n$err"]
596 }
597 foreach {cn cv} [font configure $font] {
598 font configure ${font}bold $cn $cv
599 font configure ${font}italic $cn $cv
600 }
601 font configure ${font}bold -weight bold
602 font configure ${font}italic -slant italic
603 }
604}
605
606set default_config(merge.diffstat) true
607set default_config(merge.summary) false
608set default_config(merge.verbosity) 2
609set default_config(user.name) {}
610set default_config(user.email) {}
611
612set default_config(gui.matchtrackingbranch) false
613set default_config(gui.pruneduringfetch) false
614set default_config(gui.trustmtime) false
615set default_config(gui.diffcontext) 5
616set default_config(gui.newbranchtemplate) {}
617set default_config(gui.fontui) [font configure font_ui]
618set default_config(gui.fontdiff) [font configure font_diff]
619set font_descs {
620 {fontui font_ui {mc "Main Font"}}
621 {fontdiff font_diff {mc "Diff/Console Font"}}
622}
623
0b812616
SP
624######################################################################
625##
626## find git
627
628set _git [_which git]
629if {$_git eq {}} {
630 catch {wm withdraw .}
183a1d14
SP
631 tk_messageBox \
632 -icon error \
633 -type ok \
634 -title [mc "git-gui: fatal error"] \
635 -message [mc "Cannot find git in PATH."]
0b812616
SP
636 exit 1
637}
0b812616 638
54acdd95
SP
639######################################################################
640##
641## version check
642
d6967022 643if {[catch {set _git_version [git --version]} err]} {
54acdd95 644 catch {wm withdraw .}
875b7c93
SP
645 tk_messageBox \
646 -icon error \
647 -type ok \
c8c4854b 648 -title [mc "git-gui: fatal error"] \
875b7c93 649 -message "Cannot determine Git version:
54acdd95
SP
650
651$err
652
d6967022
SP
653[appname] requires Git 1.5.0 or later."
654 exit 1
655}
656if {![regsub {^git version } $_git_version {} _git_version]} {
657 catch {wm withdraw .}
875b7c93
SP
658 tk_messageBox \
659 -icon error \
660 -type ok \
c8c4854b 661 -title [mc "git-gui: fatal error"] \
31bb1d1b 662 -message [strcat [mc "Cannot parse Git version string:"] "\n\n$_git_version"]
54acdd95
SP
663 exit 1
664}
301dfaa9
SP
665
666set _real_git_version $_git_version
ec4fceec 667regsub -- {-dirty$} $_git_version {} _git_version
d6967022
SP
668regsub {\.[0-9]+\.g[0-9a-f]+$} $_git_version {} _git_version
669regsub {\.rc[0-9]+$} $_git_version {} _git_version
91464dfb 670regsub {\.GIT$} $_git_version {} _git_version
96f11953 671regsub {\.[a-zA-Z]+\.[0-9]+$} $_git_version {} _git_version
d6967022 672
301dfaa9
SP
673if {![regexp {^[1-9]+(\.[0-9]+)+$} $_git_version]} {
674 catch {wm withdraw .}
675 if {[tk_messageBox \
676 -icon warning \
677 -type yesno \
678 -default no \
679 -title "[appname]: warning" \
1ac17950 680 -message [mc "Git version cannot be determined.
301dfaa9 681
1ac17950 682%s claims it is version '%s'.
301dfaa9 683
1ac17950 684%s requires at least Git 1.5.0 or later.
301dfaa9 685
1ac17950
CS
686Assume '%s' is version 1.5.0?
687" $_git $_real_git_version [appname] $_real_git_version]] eq {yes}} {
301dfaa9
SP
688 set _git_version 1.5.0
689 } else {
690 exit 1
691 }
692}
693unset _real_git_version
694
d6967022
SP
695proc git-version {args} {
696 global _git_version
697
698 switch [llength $args] {
699 0 {
700 return $_git_version
54acdd95 701 }
d6967022
SP
702
703 2 {
704 set op [lindex $args 0]
705 set vr [lindex $args 1]
706 set cm [package vcompare $_git_version $vr]
707 return [expr $cm $op 0]
708 }
709
710 4 {
711 set type [lindex $args 0]
712 set name [lindex $args 1]
713 set parm [lindex $args 2]
714 set body [lindex $args 3]
715
716 if {($type ne {proc} && $type ne {method})} {
717 error "Invalid arguments to git-version"
718 }
719 if {[llength $body] < 2 || [lindex $body end-1] ne {default}} {
720 error "Last arm of $type $name must be default"
721 }
722
723 foreach {op vr cb} [lrange $body 0 end-2] {
724 if {[git-version $op $vr]} {
725 return [uplevel [list $type $name $parm $cb]]
726 }
727 }
728
729 return [uplevel [list $type $name $parm [lindex $body end]]]
730 }
731
732 default {
733 error "git-version >= x"
734 }
735
736 }
737}
738
739if {[git-version < 1.5]} {
54acdd95 740 catch {wm withdraw .}
875b7c93
SP
741 tk_messageBox \
742 -icon error \
743 -type ok \
c8c4854b 744 -title [mc "git-gui: fatal error"] \
875b7c93 745 -message "[appname] requires Git 1.5.0 or later.
d6967022
SP
746
747You are using [git-version]:
748
749[git --version]"
54acdd95
SP
750 exit 1
751}
54acdd95 752
875b7c93
SP
753######################################################################
754##
755## configure our library
756
875b7c93
SP
757set idx [file join $oguilib tclIndex]
758if {[catch {set fd [open $idx r]} err]} {
759 catch {wm withdraw .}
760 tk_messageBox \
761 -icon error \
762 -type ok \
c8c4854b 763 -title [mc "git-gui: fatal error"] \
875b7c93
SP
764 -message $err
765 exit 1
766}
767if {[gets $fd] eq {# Autogenerated by git-gui Makefile}} {
768 set idx [list]
769 while {[gets $fd n] >= 0} {
770 if {$n ne {} && ![string match #* $n]} {
771 lappend idx $n
772 }
773 }
774} else {
775 set idx {}
776}
777close $fd
778
779if {$idx ne {}} {
780 set loaded [list]
781 foreach p $idx {
782 if {[lsearch -exact $loaded $p] >= 0} continue
783 source [file join $oguilib $p]
784 lappend loaded $p
785 }
786 unset loaded p
787} else {
788 set auto_path [concat [list $oguilib] $auto_path]
789}
fc703c20 790unset -nocomplain idx fd
875b7c93 791
ba7cc660
SP
792######################################################################
793##
794## feature option selection
795
0b2bc460 796if {[regexp {^git-(.+)$} [file tail $argv0] _junk subcommand]} {
ba7cc660
SP
797 unset _junk
798} else {
799 set subcommand gui
800}
801if {$subcommand eq {gui.sh}} {
802 set subcommand gui
803}
804if {$subcommand eq {gui} && [llength $argv] > 0} {
805 set subcommand [lindex $argv 0]
806 set argv [lrange $argv 1 end]
807}
808
809enable_option multicommit
810enable_option branch
811enable_option transport
c52c9452 812disable_option bare
ba7cc660
SP
813
814switch -- $subcommand {
815browser -
816blame {
c52c9452
SP
817 enable_option bare
818
ba7cc660
SP
819 disable_option multicommit
820 disable_option branch
821 disable_option transport
822}
823citool {
824 enable_option singlecommit
825
826 disable_option multicommit
827 disable_option branch
828 disable_option transport
829}
830}
831
2d19516d
SP
832######################################################################
833##
834## repository setup
835
c6127856
SP
836if {[catch {
837 set _gitdir $env(GIT_DIR)
838 set _prefix {}
839 }]
840 && [catch {
841 set _gitdir [git rev-parse --git-dir]
842 set _prefix [git rev-parse --show-prefix]
843 } err]} {
ab08b363
SP
844 load_config 1
845 apply_config
846 choose_repository::pick
2d19516d 847}
20ddfcaa 848if {![file isdirectory $_gitdir] && [is_Cygwin]} {
2f7c9a7f 849 catch {set _gitdir [exec cygpath --windows $_gitdir]}
20ddfcaa 850}
c950c66e 851if {![file isdirectory $_gitdir]} {
dbccbbda 852 catch {wm withdraw .}
31bb1d1b 853 error_popup [strcat [mc "Git directory not found:"] "\n\n$_gitdir"]
dbccbbda
SP
854 exit 1
855}
c80d25db
SP
856if {$_prefix ne {}} {
857 regsub -all {[^/]+/} $_prefix ../ cdup
858 if {[catch {cd $cdup} err]} {
859 catch {wm withdraw .}
31bb1d1b 860 error_popup [strcat [mc "Cannot move to top of working directory:"] "\n\n$err"]
c80d25db
SP
861 exit 1
862 }
863 unset cdup
864} elseif {![is_enabled bare]} {
c52c9452
SP
865 if {[lindex [file split $_gitdir] end] ne {.git}} {
866 catch {wm withdraw .}
31bb1d1b 867 error_popup [strcat [mc "Cannot use funny .git directory:"] "\n\n$_gitdir"]
c52c9452
SP
868 exit 1
869 }
870 if {[catch {cd [file dirname $_gitdir]} err]} {
871 catch {wm withdraw .}
31bb1d1b 872 error_popup [strcat [mc "No working directory"] " [file dirname $_gitdir]:\n\n$err"]
c52c9452
SP
873 exit 1
874 }
dbccbbda 875}
c52c9452
SP
876set _reponame [file split [file normalize $_gitdir]]
877if {[lindex $_reponame end] eq {.git}} {
878 set _reponame [lindex $_reponame end-1]
879} else {
880 set _reponame [lindex $_reponame end]
2d19516d 881}
2d19516d 882
372ef954
SP
883######################################################################
884##
885## global init
886
887set current_diff_path {}
888set current_diff_side {}
889set diff_actions [list]
372ef954
SP
890
891set HEAD {}
892set PARENT {}
893set MERGE_HEAD [list]
894set commit_type {}
895set empty_tree {}
896set current_branch {}
d41b43eb 897set is_detached 0
372ef954 898set current_diff_path {}
9c9f5fa9 899set is_3way_diff 0
372ef954
SP
900set selected_commit_type new
901
cb07fc2a
SP
902######################################################################
903##
e210e674 904## task management
cb07fc2a 905
8f52548a 906set rescan_active 0
131f503b 907set diff_active 0
24263b77 908set last_clicked {}
131f503b 909
e210e674
SP
910set disable_on_lock [list]
911set index_lock_type none
912
913proc lock_index {type} {
914 global index_lock_type disable_on_lock
131f503b 915
043f7011 916 if {$index_lock_type eq {none}} {
e210e674
SP
917 set index_lock_type $type
918 foreach w $disable_on_lock {
919 uplevel #0 $w disabled
920 }
921 return 1
53716a7b 922 } elseif {$index_lock_type eq "begin-$type"} {
e210e674 923 set index_lock_type $type
131f503b
SP
924 return 1
925 }
926 return 0
927}
cb07fc2a 928
e210e674
SP
929proc unlock_index {} {
930 global index_lock_type disable_on_lock
931
932 set index_lock_type none
933 foreach w $disable_on_lock {
934 uplevel #0 $w normal
935 }
936}
937
938######################################################################
939##
940## status
941
f18e40a1 942proc repository_state {ctvar hdvar mhvar} {
c950c66e 943 global current_branch
f18e40a1
SP
944 upvar $ctvar ct $hdvar hd $mhvar mh
945
946 set mh [list]
ec6b424a 947
d41b43eb 948 load_current_branch
81347223 949 if {[catch {set hd [git rev-parse --verify HEAD]}]} {
4539eacd 950 set hd {}
ec6b424a 951 set ct initial
f18e40a1
SP
952 return
953 }
954
c2758a17 955 set merge_head [gitdir MERGE_HEAD]
f18e40a1 956 if {[file exists $merge_head]} {
ec6b424a 957 set ct merge
f18e40a1
SP
958 set fd_mh [open $merge_head r]
959 while {[gets $fd_mh line] >= 0} {
960 lappend mh $line
961 }
962 close $fd_mh
963 return
ec6b424a 964 }
f18e40a1
SP
965
966 set ct normal
ec6b424a
SP
967}
968
4539eacd
SP
969proc PARENT {} {
970 global PARENT empty_tree
971
f18e40a1
SP
972 set p [lindex $PARENT 0]
973 if {$p ne {}} {
974 return $p
4539eacd
SP
975 }
976 if {$empty_tree eq {}} {
81347223 977 set empty_tree [git mktree << {}]
4539eacd
SP
978 }
979 return $empty_tree
980}
981
46aaf90b 982proc rescan {after {honor_trustmtime 1}} {
f18e40a1 983 global HEAD PARENT MERGE_HEAD commit_type
699d5601 984 global ui_index ui_workdir ui_comm
8f52548a 985 global rescan_active file_states
cf25ddc8 986 global repo_config
cb07fc2a 987
8f52548a 988 if {$rescan_active > 0 || ![lock_index read]} return
cb07fc2a 989
f18e40a1 990 repository_state newType newHEAD newMERGE_HEAD
4539eacd 991 if {[string match amend* $commit_type]
f18e40a1
SP
992 && $newType eq {normal}
993 && $newHEAD eq $HEAD} {
e57ca85e 994 } else {
f18e40a1
SP
995 set HEAD $newHEAD
996 set PARENT $newHEAD
997 set MERGE_HEAD $newMERGE_HEAD
998 set commit_type $newType
e57ca85e
SP
999 }
1000
cb07fc2a 1001 array unset file_states
cb07fc2a 1002
1e0a92fd
SP
1003 if {!$::GITGUI_BCK_exists &&
1004 (![$ui_comm edit modified]
1005 || [string trim [$ui_comm get 0.0 end]] eq {})} {
b2f3bb1b
SP
1006 if {[string match amend* $commit_type]} {
1007 } elseif {[load_message GITGUI_MSG]} {
131f503b
SP
1008 } elseif {[load_message MERGE_MSG]} {
1009 } elseif {[load_message SQUASH_MSG]} {
1010 }
b2c6fcf1 1011 $ui_comm edit reset
21d7744f 1012 $ui_comm edit modified false
131f503b
SP
1013 }
1014
46aaf90b 1015 if {$honor_trustmtime && $repo_config(gui.trustmtime) eq {true}} {
8f52548a 1016 rescan_stage2 {} $after
e534f3a8 1017 } else {
8f52548a 1018 set rescan_active 1
1ac17950 1019 ui_status [mc "Refreshing file status..."]
0b812616
SP
1020 set fd_rf [git_read update-index \
1021 -q \
1022 --unmerged \
1023 --ignore-missing \
1024 --refresh \
1025 ]
e534f3a8 1026 fconfigure $fd_rf -blocking 0 -translation binary
390adaea 1027 fileevent $fd_rf readable \
8f52548a 1028 [list rescan_stage2 $fd_rf $after]
e534f3a8 1029 }
131f503b
SP
1030}
1031
2fe167b6
SP
1032if {[is_Cygwin]} {
1033 set is_git_info_link {}
1034 set is_git_info_exclude {}
1035 proc have_info_exclude {} {
1036 global is_git_info_link is_git_info_exclude
1037
1038 if {$is_git_info_link eq {}} {
1039 set is_git_info_link [file isfile [gitdir info.lnk]]
1040 }
1041
1042 if {$is_git_info_link} {
1043 if {$is_git_info_exclude eq {}} {
1044 if {[catch {exec test -f [gitdir info exclude]}]} {
1045 set is_git_info_exclude 0
1046 } else {
1047 set is_git_info_exclude 1
1048 }
1049 }
1050 return $is_git_info_exclude
1051 } else {
1052 return [file readable [gitdir info exclude]]
1053 }
1054 }
1055} else {
1056 proc have_info_exclude {} {
1057 return [file readable [gitdir info exclude]]
1058 }
1059}
1060
8f52548a 1061proc rescan_stage2 {fd after} {
4539eacd 1062 global rescan_active buf_rdi buf_rdf buf_rlo
131f503b 1063
043f7011 1064 if {$fd ne {}} {
e534f3a8
SP
1065 read $fd
1066 if {![eof $fd]} return
1067 close $fd
1068 }
131f503b 1069
0b812616 1070 set ls_others [list --exclude-per-directory=.gitignore]
2fe167b6
SP
1071 if {[have_info_exclude]} {
1072 lappend ls_others "--exclude-from=[gitdir info exclude]"
cb07fc2a 1073 }
94a4dd9b
SP
1074 set user_exclude [get_config core.excludesfile]
1075 if {$user_exclude ne {} && [file readable $user_exclude]} {
1076 lappend ls_others "--exclude-from=$user_exclude"
1077 }
cb07fc2a 1078
868c8752
SP
1079 set buf_rdi {}
1080 set buf_rdf {}
1081 set buf_rlo {}
1082
8f52548a 1083 set rescan_active 3
1ac17950 1084 ui_status [mc "Scanning for modified files ..."]
0b812616
SP
1085 set fd_di [git_read diff-index --cached -z [PARENT]]
1086 set fd_df [git_read diff-files -z]
1087 set fd_lo [eval git_read ls-files --others -z $ls_others]
cb07fc2a 1088
51a989ba
SP
1089 fconfigure $fd_di -blocking 0 -translation binary -encoding binary
1090 fconfigure $fd_df -blocking 0 -translation binary -encoding binary
1091 fconfigure $fd_lo -blocking 0 -translation binary -encoding binary
8f52548a
SP
1092 fileevent $fd_di readable [list read_diff_index $fd_di $after]
1093 fileevent $fd_df readable [list read_diff_files $fd_df $after]
1094 fileevent $fd_lo readable [list read_ls_others $fd_lo $after]
cb07fc2a
SP
1095}
1096
131f503b 1097proc load_message {file} {
c950c66e 1098 global ui_comm
131f503b 1099
c2758a17 1100 set f [gitdir $file]
e57ca85e 1101 if {[file isfile $f]} {
131f503b
SP
1102 if {[catch {set fd [open $f r]}]} {
1103 return 0
1104 }
6eb420ef 1105 fconfigure $fd -eofchar {}
e57ca85e 1106 set content [string trim [read $fd]]
131f503b 1107 close $fd
4e55d19a 1108 regsub -all -line {[ \r\t]+$} $content {} content
131f503b
SP
1109 $ui_comm delete 0.0 end
1110 $ui_comm insert end $content
1111 return 1
1112 }
1113 return 0
1114}
1115
8f52548a 1116proc read_diff_index {fd after} {
cb07fc2a
SP
1117 global buf_rdi
1118
1119 append buf_rdi [read $fd]
868c8752
SP
1120 set c 0
1121 set n [string length $buf_rdi]
1122 while {$c < $n} {
1123 set z1 [string first "\0" $buf_rdi $c]
1124 if {$z1 == -1} break
1125 incr z1
1126 set z2 [string first "\0" $buf_rdi $z1]
1127 if {$z2 == -1} break
1128
868c8752 1129 incr c
86291555 1130 set i [split [string range $buf_rdi $c [expr {$z1 - 2}]] { }]
51a989ba 1131 set p [string range $buf_rdi $z1 [expr {$z2 - 1}]]
1461c5f3 1132 merge_state \
51a989ba 1133 [encoding convertfrom $p] \
86291555
SP
1134 [lindex $i 4]? \
1135 [list [lindex $i 0] [lindex $i 2]] \
1461c5f3
SP
1136 [list]
1137 set c $z2
86291555 1138 incr c
cb07fc2a 1139 }
868c8752
SP
1140 if {$c < $n} {
1141 set buf_rdi [string range $buf_rdi $c end]
1142 } else {
1143 set buf_rdi {}
1144 }
1145
8f52548a 1146 rescan_done $fd buf_rdi $after
cb07fc2a
SP
1147}
1148
8f52548a 1149proc read_diff_files {fd after} {
cb07fc2a
SP
1150 global buf_rdf
1151
1152 append buf_rdf [read $fd]
868c8752
SP
1153 set c 0
1154 set n [string length $buf_rdf]
1155 while {$c < $n} {
1156 set z1 [string first "\0" $buf_rdf $c]
1157 if {$z1 == -1} break
1158 incr z1
1159 set z2 [string first "\0" $buf_rdf $z1]
1160 if {$z2 == -1} break
1161
868c8752 1162 incr c
86291555 1163 set i [split [string range $buf_rdf $c [expr {$z1 - 2}]] { }]
51a989ba 1164 set p [string range $buf_rdf $z1 [expr {$z2 - 1}]]
1461c5f3 1165 merge_state \
51a989ba 1166 [encoding convertfrom $p] \
86291555 1167 ?[lindex $i 4] \
1461c5f3 1168 [list] \
86291555 1169 [list [lindex $i 0] [lindex $i 2]]
1461c5f3 1170 set c $z2
86291555 1171 incr c
868c8752
SP
1172 }
1173 if {$c < $n} {
1174 set buf_rdf [string range $buf_rdf $c end]
1175 } else {
1176 set buf_rdf {}
cb07fc2a 1177 }
868c8752 1178
8f52548a 1179 rescan_done $fd buf_rdf $after
cb07fc2a
SP
1180}
1181
8f52548a 1182proc read_ls_others {fd after} {
cb07fc2a
SP
1183 global buf_rlo
1184
1185 append buf_rlo [read $fd]
1186 set pck [split $buf_rlo "\0"]
1187 set buf_rlo [lindex $pck end]
1188 foreach p [lrange $pck 0 end-1] {
89384101
SP
1189 set p [encoding convertfrom $p]
1190 if {[string index $p end] eq {/}} {
1191 set p [string range $p 0 end-1]
1192 }
1193 merge_state $p ?O
cb07fc2a 1194 }
8f52548a 1195 rescan_done $fd buf_rlo $after
cb07fc2a
SP
1196}
1197
8f52548a 1198proc rescan_done {fd buf after} {
f522c9b5 1199 global rescan_active current_diff_path
f7f8d322 1200 global file_states repo_config
7f1df79b 1201 upvar $buf to_clear
cb07fc2a 1202
f7f8d322
SP
1203 if {![eof $fd]} return
1204 set to_clear {}
1205 close $fd
8f52548a 1206 if {[incr rescan_active -1] > 0} return
93f654df 1207
24263b77 1208 prune_selection
f7f8d322
SP
1209 unlock_index
1210 display_all_files
f522c9b5 1211 if {$current_diff_path ne {}} reshow_diff
8f52548a 1212 uplevel #0 $after
cb07fc2a
SP
1213}
1214
24263b77
SP
1215proc prune_selection {} {
1216 global file_states selected_paths
1217
1218 foreach path [array names selected_paths] {
1219 if {[catch {set still_here $file_states($path)}]} {
1220 unset selected_paths($path)
1221 }
1222 }
1223}
1224
cb07fc2a
SP
1225######################################################################
1226##
f522c9b5 1227## ui helpers
cb07fc2a 1228
f522c9b5
SP
1229proc mapicon {w state path} {
1230 global all_icons
1231
1232 if {[catch {set r $all_icons($state$w)}]} {
1233 puts "error: no icon for $w state={$state} $path"
1234 return file_plain
1235 }
1236 return $r
1237}
cb07fc2a 1238
f522c9b5
SP
1239proc mapdesc {state path} {
1240 global all_descs
03e4ec53 1241
f522c9b5
SP
1242 if {[catch {set r $all_descs($state)}]} {
1243 puts "error: no desc for state={$state} $path"
1244 return $state
1245 }
1246 return $r
1247}
03e4ec53 1248
699d5601 1249proc ui_status {msg} {
906ab7f6
SP
1250 global main_status
1251 if {[info exists main_status]} {
1252 $main_status show $msg
1253 }
699d5601
SP
1254}
1255
1256proc ui_ready {{test {}}} {
906ab7f6
SP
1257 global main_status
1258 if {[info exists main_status]} {
1259 $main_status show [mc "Ready."] $test
1260 }
699d5601
SP
1261}
1262
f522c9b5
SP
1263proc escape_path {path} {
1264 regsub -all {\\} $path "\\\\" path
1265 regsub -all "\n" $path "\\n" path
1266 return $path
cb07fc2a
SP
1267}
1268
f522c9b5
SP
1269proc short_path {path} {
1270 return [escape_path [lindex [file split $path] end]]
7f1df79b
SP
1271}
1272
f522c9b5
SP
1273set next_icon_id 0
1274set null_sha1 [string repeat 0 40]
16403d0b 1275
f522c9b5
SP
1276proc merge_state {path new_state {head_info {}} {index_info {}}} {
1277 global file_states next_icon_id null_sha1
16403d0b 1278
f522c9b5
SP
1279 set s0 [string index $new_state 0]
1280 set s1 [string index $new_state 1]
16403d0b 1281
f522c9b5
SP
1282 if {[catch {set info $file_states($path)}]} {
1283 set state __
1284 set icon n[incr next_icon_id]
1285 } else {
1286 set state [lindex $info 0]
1287 set icon [lindex $info 1]
1288 if {$head_info eq {}} {set head_info [lindex $info 2]}
1289 if {$index_info eq {}} {set index_info [lindex $info 3]}
1290 }
16403d0b 1291
f522c9b5
SP
1292 if {$s0 eq {?}} {set s0 [string index $state 0]} \
1293 elseif {$s0 eq {_}} {set s0 _}
124355d3 1294
f522c9b5
SP
1295 if {$s1 eq {?}} {set s1 [string index $state 1]} \
1296 elseif {$s1 eq {_}} {set s1 _}
16403d0b 1297
f522c9b5
SP
1298 if {$s0 eq {A} && $s1 eq {_} && $head_info eq {}} {
1299 set head_info [list 0 $null_sha1]
1300 } elseif {$s0 ne {_} && [string index $state 0] eq {_}
1301 && $head_info eq {}} {
1302 set head_info $index_info
1303 }
16403d0b 1304
f522c9b5
SP
1305 set file_states($path) [list $s0$s1 $icon \
1306 $head_info $index_info \
1307 ]
1308 return $state
1309}
cb07fc2a 1310
f522c9b5
SP
1311proc display_file_helper {w path icon_name old_m new_m} {
1312 global file_lists
cb07fc2a 1313
f522c9b5 1314 if {$new_m eq {_}} {
156b2921 1315 set lno [lsearch -sorted -exact $file_lists($w) $path]
5f8b70b1 1316 if {$lno >= 0} {
f522c9b5 1317 set file_lists($w) [lreplace $file_lists($w) $lno $lno]
5f8b70b1 1318 incr lno
f522c9b5
SP
1319 $w conf -state normal
1320 $w delete $lno.0 [expr {$lno + 1}].0
1321 $w conf -state disabled
03e4ec53 1322 }
f522c9b5
SP
1323 } elseif {$old_m eq {_} && $new_m ne {_}} {
1324 lappend file_lists($w) $path
1325 set file_lists($w) [lsort -unique $file_lists($w)]
1326 set lno [lsearch -sorted -exact $file_lists($w) $path]
1327 incr lno
1328 $w conf -state normal
1329 $w image create $lno.0 \
1330 -align center -padx 5 -pady 1 \
1331 -name $icon_name \
1332 -image [mapicon $w $new_m $path]
1333 $w insert $lno.1 "[escape_path $path]\n"
1334 $w conf -state disabled
1335 } elseif {$old_m ne $new_m} {
1336 $w conf -state normal
1337 $w image conf $icon_name -image [mapicon $w $new_m $path]
1338 $w conf -state disabled
03e4ec53 1339 }
f522c9b5
SP
1340}
1341
1342proc display_file {path state} {
1343 global file_states selected_paths
1344 global ui_index ui_workdir
03e4ec53 1345
f522c9b5 1346 set old_m [merge_state $path $state]
cb07fc2a 1347 set s $file_states($path)
f522c9b5
SP
1348 set new_m [lindex $s 0]
1349 set icon_name [lindex $s 1]
82cb8706 1350
f522c9b5
SP
1351 set o [string index $old_m 0]
1352 set n [string index $new_m 0]
1353 if {$o eq {U}} {
1354 set o _
1355 }
1356 if {$n eq {U}} {
1357 set n _
cb07fc2a 1358 }
f522c9b5 1359 display_file_helper $ui_index $path $icon_name $o $n
cb07fc2a 1360
f522c9b5
SP
1361 if {[string index $old_m 0] eq {U}} {
1362 set o U
1363 } else {
1364 set o [string index $old_m 1]
82cb8706 1365 }
f522c9b5
SP
1366 if {[string index $new_m 0] eq {U}} {
1367 set n U
1368 } else {
1369 set n [string index $new_m 1]
82cb8706 1370 }
f522c9b5
SP
1371 display_file_helper $ui_workdir $path $icon_name $o $n
1372
1373 if {$new_m eq {__}} {
1374 unset file_states($path)
1375 catch {unset selected_paths($path)}
cb07fc2a 1376 }
f522c9b5 1377}
cb07fc2a 1378
f522c9b5
SP
1379proc display_all_files_helper {w path icon_name m} {
1380 global file_lists
1381
1382 lappend file_lists($w) $path
1383 set lno [expr {[lindex [split [$w index end] .] 0] - 1}]
1384 $w image create end \
1385 -align center -padx 5 -pady 1 \
1386 -name $icon_name \
1387 -image [mapicon $w $m $path]
1388 $w insert end "[escape_path $path]\n"
cb07fc2a
SP
1389}
1390
f522c9b5
SP
1391proc display_all_files {} {
1392 global ui_index ui_workdir
1393 global file_states file_lists
1394 global last_clicked
cb07fc2a 1395
f522c9b5
SP
1396 $ui_index conf -state normal
1397 $ui_workdir conf -state normal
cb07fc2a 1398
f522c9b5
SP
1399 $ui_index delete 0.0 end
1400 $ui_workdir delete 0.0 end
1401 set last_clicked {}
cb07fc2a 1402
f522c9b5
SP
1403 set file_lists($ui_index) [list]
1404 set file_lists($ui_workdir) [list]
16403d0b 1405
f522c9b5
SP
1406 foreach path [lsort [array names file_states]] {
1407 set s $file_states($path)
1408 set m [lindex $s 0]
1409 set icon_name [lindex $s 1]
1410
1411 set s [string index $m 0]
1412 if {$s ne {U} && $s ne {_}} {
1413 display_all_files_helper $ui_index $path \
1414 $icon_name $s
16403d0b 1415 }
cb07fc2a 1416
f522c9b5
SP
1417 if {[string index $m 0] eq {U}} {
1418 set s U
1419 } else {
1420 set s [string index $m 1]
a25c5189 1421 }
f522c9b5
SP
1422 if {$s ne {_}} {
1423 display_all_files_helper $ui_workdir $path \
1424 $icon_name $s
a25c5189
SP
1425 }
1426 }
1427
f522c9b5
SP
1428 $ui_index conf -state disabled
1429 $ui_workdir conf -state disabled
a25c5189
SP
1430}
1431
ec6b424a
SP
1432######################################################################
1433##
f522c9b5 1434## icons
ec6b424a 1435
f522c9b5
SP
1436set filemask {
1437#define mask_width 14
1438#define mask_height 15
1439static unsigned char mask_bits[] = {
1440 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f,
1441 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f,
1442 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f};
1443}
cb07fc2a
SP
1444
1445image create bitmap file_plain -background white -foreground black -data {
1446#define plain_width 14
1447#define plain_height 15
1448static unsigned char plain_bits[] = {
1449 0xfe, 0x01, 0x02, 0x03, 0x02, 0x05, 0x02, 0x09, 0x02, 0x1f, 0x02, 0x10,
1450 0x02, 0x10, 0x02, 0x10, 0x02, 0x10, 0x02, 0x10, 0x02, 0x10, 0x02, 0x10,
1451 0x02, 0x10, 0x02, 0x10, 0xfe, 0x1f};
1452} -maskdata $filemask
1453
1454image create bitmap file_mod -background white -foreground blue -data {
1455#define mod_width 14
1456#define mod_height 15
1457static unsigned char mod_bits[] = {
1458 0xfe, 0x01, 0x02, 0x03, 0x7a, 0x05, 0x02, 0x09, 0x7a, 0x1f, 0x02, 0x10,
1459 0xfa, 0x17, 0x02, 0x10, 0xfa, 0x17, 0x02, 0x10, 0xfa, 0x17, 0x02, 0x10,
1460 0xfa, 0x17, 0x02, 0x10, 0xfe, 0x1f};
1461} -maskdata $filemask
1462
131f503b
SP
1463image create bitmap file_fulltick -background white -foreground "#007000" -data {
1464#define file_fulltick_width 14
1465#define file_fulltick_height 15
1466static unsigned char file_fulltick_bits[] = {
cb07fc2a
SP
1467 0xfe, 0x01, 0x02, 0x1a, 0x02, 0x0c, 0x02, 0x0c, 0x02, 0x16, 0x02, 0x16,
1468 0x02, 0x13, 0x00, 0x13, 0x86, 0x11, 0x8c, 0x11, 0xd8, 0x10, 0xf2, 0x10,
1469 0x62, 0x10, 0x02, 0x10, 0xfe, 0x1f};
1470} -maskdata $filemask
1471
1472image create bitmap file_parttick -background white -foreground "#005050" -data {
1473#define parttick_width 14
1474#define parttick_height 15
1475static unsigned char parttick_bits[] = {
1476 0xfe, 0x01, 0x02, 0x03, 0x7a, 0x05, 0x02, 0x09, 0x7a, 0x1f, 0x02, 0x10,
1477 0x7a, 0x14, 0x02, 0x16, 0x02, 0x13, 0x8a, 0x11, 0xda, 0x10, 0x72, 0x10,
1478 0x22, 0x10, 0x02, 0x10, 0xfe, 0x1f};
1479} -maskdata $filemask
1480
1481image create bitmap file_question -background white -foreground black -data {
1482#define file_question_width 14
1483#define file_question_height 15
1484static unsigned char file_question_bits[] = {
1485 0xfe, 0x01, 0x02, 0x02, 0xe2, 0x04, 0xf2, 0x09, 0x1a, 0x1b, 0x0a, 0x13,
1486 0x82, 0x11, 0xc2, 0x10, 0x62, 0x10, 0x62, 0x10, 0x02, 0x10, 0x62, 0x10,
1487 0x62, 0x10, 0x02, 0x10, 0xfe, 0x1f};
1488} -maskdata $filemask
1489
1490image create bitmap file_removed -background white -foreground red -data {
1491#define file_removed_width 14
1492#define file_removed_height 15
1493static unsigned char file_removed_bits[] = {
1494 0xfe, 0x01, 0x02, 0x03, 0x02, 0x05, 0x02, 0x09, 0x02, 0x1f, 0x02, 0x10,
1495 0x1a, 0x16, 0x32, 0x13, 0xe2, 0x11, 0xc2, 0x10, 0xe2, 0x11, 0x32, 0x13,
1496 0x1a, 0x16, 0x02, 0x10, 0xfe, 0x1f};
1497} -maskdata $filemask
1498
1499image create bitmap file_merge -background white -foreground blue -data {
1500#define file_merge_width 14
1501#define file_merge_height 15
1502static unsigned char file_merge_bits[] = {
1503 0xfe, 0x01, 0x02, 0x03, 0x62, 0x05, 0x62, 0x09, 0x62, 0x1f, 0x62, 0x10,
1504 0xfa, 0x11, 0xf2, 0x10, 0x62, 0x10, 0x02, 0x10, 0xfa, 0x17, 0x02, 0x10,
1505 0xfa, 0x17, 0x02, 0x10, 0xfe, 0x1f};
1506} -maskdata $filemask
1507
6b292675 1508set ui_index .vpane.files.index.list
0812665e 1509set ui_workdir .vpane.files.workdir.list
21e409ad
SP
1510
1511set all_icons(_$ui_index) file_plain
1512set all_icons(A$ui_index) file_fulltick
1513set all_icons(M$ui_index) file_fulltick
1514set all_icons(D$ui_index) file_removed
1515set all_icons(U$ui_index) file_merge
1516
1517set all_icons(_$ui_workdir) file_plain
1518set all_icons(M$ui_workdir) file_mod
1519set all_icons(D$ui_workdir) file_question
3b4db3c1 1520set all_icons(U$ui_workdir) file_merge
21e409ad
SP
1521set all_icons(O$ui_workdir) file_plain
1522
131f503b 1523set max_status_desc 0
cb07fc2a 1524foreach i {
1ac17950
CS
1525 {__ {mc "Unmodified"}}
1526
1527 {_M {mc "Modified, not staged"}}
1528 {M_ {mc "Staged for commit"}}
1529 {MM {mc "Portions staged for commit"}}
1530 {MD {mc "Staged for commit, missing"}}
1531
1532 {_O {mc "Untracked, not staged"}}
1533 {A_ {mc "Staged for commit"}}
1534 {AM {mc "Portions staged for commit"}}
1535 {AD {mc "Staged for commit, missing"}}
1536
1537 {_D {mc "Missing"}}
1538 {D_ {mc "Staged for removal"}}
1539 {DO {mc "Staged for removal, still present"}}
1540
1541 {U_ {mc "Requires merge resolution"}}
1542 {UU {mc "Requires merge resolution"}}
1543 {UM {mc "Requires merge resolution"}}
1544 {UD {mc "Requires merge resolution"}}
cb07fc2a 1545 } {
1ac17950
CS
1546 set text [eval [lindex $i 1]]
1547 if {$max_status_desc < [string length $text]} {
1548 set max_status_desc [string length $text]
131f503b 1549 }
1ac17950 1550 set all_descs([lindex $i 0]) $text
cb07fc2a 1551}
21e409ad 1552unset i
cb07fc2a
SP
1553
1554######################################################################
1555##
1556## util
1557
35874c16
SP
1558proc scrollbar2many {list mode args} {
1559 foreach w $list {eval $w $mode $args}
1560}
1561
1562proc many2scrollbar {list mode sb top bottom} {
1563 $sb set $top $bottom
1564 foreach w $list {$w $mode moveto $top}
1565}
1566
b4946930
SP
1567proc incr_font_size {font {amt 1}} {
1568 set sz [font configure $font -size]
1569 incr sz $amt
1570 font configure $font -size $sz
1571 font configure ${font}bold -size $sz
debcd0fd 1572 font configure ${font}italic -size $sz
b4946930
SP
1573}
1574
cb07fc2a
SP
1575######################################################################
1576##
1577## ui commands
1578
1ac17950 1579set starting_gitk_msg [mc "Starting gitk... please wait..."]
cc4b1c02 1580
d0752429 1581proc do_gitk {revs} {
20ddfcaa
SP
1582 # -- Always start gitk through whatever we were loaded with. This
1583 # lets us bypass using shell process on Windows systems.
1584 #
02efd48f
SP
1585 set exe [file join [file dirname $::_git] gitk]
1586 set cmd [list [info nameofexecutable] $exe]
7aecb128 1587 if {! [file exists $exe]} {
1ac17950 1588 error_popup [mc "Unable to start gitk:\n\n%s does not exist" $exe]
cb07fc2a 1589 } else {
501e4c6f
SP
1590 global env
1591
1592 if {[info exists env(GIT_DIR)]} {
1593 set old_GIT_DIR $env(GIT_DIR)
1594 } else {
1595 set old_GIT_DIR {}
1596 }
1597
1598 set pwd [pwd]
1599 cd [file dirname [gitdir]]
1600 set env(GIT_DIR) [file tail [gitdir]]
1601
02efd48f 1602 eval exec $cmd $revs &
501e4c6f
SP
1603
1604 if {$old_GIT_DIR eq {}} {
1605 unset env(GIT_DIR)
1606 } else {
1607 set env(GIT_DIR) $old_GIT_DIR
1608 }
1609 cd $pwd
1610
02efd48f 1611 ui_status $::starting_gitk_msg
d0752429 1612 after 10000 {
699d5601 1613 ui_ready $starting_gitk_msg
d0752429 1614 }
cb07fc2a
SP
1615 }
1616}
1617
b5834d70 1618set is_quitting 0
c4fe7728 1619
cb07fc2a 1620proc do_quit {} {
c950c66e 1621 global ui_comm is_quitting repo_config commit_type
4578c5cb 1622 global GITGUI_BCK_exists GITGUI_BCK_i
c4fe7728 1623
b5834d70
SP
1624 if {$is_quitting} return
1625 set is_quitting 1
131f503b 1626
db7f34d4
SP
1627 if {[winfo exists $ui_comm]} {
1628 # -- Stash our current commit buffer.
1629 #
1630 set save [gitdir GITGUI_MSG]
4578c5cb
SP
1631 if {$GITGUI_BCK_exists && ![$ui_comm edit modified]} {
1632 file rename -force [gitdir GITGUI_BCK] $save
1633 set GITGUI_BCK_exists 0
db7f34d4 1634 } else {
4578c5cb
SP
1635 set msg [string trim [$ui_comm get 0.0 end]]
1636 regsub -all -line {[ \r\t]+$} $msg {} msg
1637 if {(![string match amend* $commit_type]
1638 || [$ui_comm edit modified])
1639 && $msg ne {}} {
1640 catch {
1641 set fd [open $save w]
1642 puts -nonewline $fd $msg
1643 close $fd
1644 }
1645 } else {
1646 catch {file delete $save}
1647 }
1648 }
1649
1650 # -- Remove our editor backup, its not needed.
1651 #
1652 after cancel $GITGUI_BCK_i
1653 if {$GITGUI_BCK_exists} {
1654 catch {file delete [gitdir GITGUI_BCK]}
131f503b 1655 }
131f503b 1656
db7f34d4
SP
1657 # -- Stash our current window geometry into this repository.
1658 #
1659 set cfg_geometry [list]
1660 lappend cfg_geometry [wm geometry .]
1661 lappend cfg_geometry [lindex [.vpane sash coord 0] 1]
1662 lappend cfg_geometry [lindex [.vpane.files sash coord 0] 0]
1663 if {[catch {set rc_geometry $repo_config(gui.geometry)}]} {
1664 set rc_geometry {}
1665 }
1666 if {$cfg_geometry ne $rc_geometry} {
81347223 1667 catch {git config gui.geometry $cfg_geometry}
db7f34d4 1668 }
51f4d16b
SP
1669 }
1670
cb07fc2a
SP
1671 destroy .
1672}
1673
1674proc do_rescan {} {
699d5601 1675 rescan ui_ready
cb07fc2a
SP
1676}
1677
6e27d826 1678proc do_commit {} {
ec6b424a 1679 commit_tree
6e27d826
SP
1680}
1681
24263b77 1682proc toggle_or_diff {w x y} {
20a53c02 1683 global file_states file_lists current_diff_path ui_index ui_workdir
24263b77 1684 global last_clicked selected_paths
131f503b 1685
cb07fc2a
SP
1686 set pos [split [$w index @$x,$y] .]
1687 set lno [lindex $pos 0]
1688 set col [lindex $pos 1]
24263b77
SP
1689 set path [lindex $file_lists($w) [expr {$lno - 1}]]
1690 if {$path eq {}} {
1691 set last_clicked {}
1692 return
1693 }
1694
1695 set last_clicked [list $w $lno]
1696 array unset selected_paths
1697 $ui_index tag remove in_sel 0.0 end
0812665e 1698 $ui_workdir tag remove in_sel 0.0 end
cb07fc2a 1699
24263b77 1700 if {$col == 0} {
20a53c02 1701 if {$current_diff_path eq $path} {
32e0bcab
SP
1702 set after {reshow_diff;}
1703 } else {
1704 set after {}
1705 }
de5f6d5d 1706 if {$w eq $ui_index} {
74d18d2e 1707 update_indexinfo \
93e912c5 1708 "Unstaging [short_path $path] from commit" \
74d18d2e 1709 [list $path] \
699d5601 1710 [concat $after [list ui_ready]]
de5f6d5d 1711 } elseif {$w eq $ui_workdir} {
74d18d2e 1712 update_index \
4d583c86 1713 "Adding [short_path $path]" \
74d18d2e 1714 [list $path] \
699d5601 1715 [concat $after [list ui_ready]]
74d18d2e 1716 }
24263b77 1717 } else {
03e4ec53 1718 show_diff $path $w $lno
cb07fc2a
SP
1719 }
1720}
1721
24263b77 1722proc add_one_to_selection {w x y} {
833eda73 1723 global file_lists last_clicked selected_paths
7f1df79b 1724
833eda73 1725 set lno [lindex [split [$w index @$x,$y] .] 0]
24263b77
SP
1726 set path [lindex $file_lists($w) [expr {$lno - 1}]]
1727 if {$path eq {}} {
1728 set last_clicked {}
1729 return
1730 }
cb07fc2a 1731
833eda73
SP
1732 if {$last_clicked ne {}
1733 && [lindex $last_clicked 0] ne $w} {
1734 array unset selected_paths
1735 [lindex $last_clicked 0] tag remove in_sel 0.0 end
1736 }
1737
24263b77
SP
1738 set last_clicked [list $w $lno]
1739 if {[catch {set in_sel $selected_paths($path)}]} {
1740 set in_sel 0
1741 }
1742 if {$in_sel} {
1743 unset selected_paths($path)
1744 $w tag remove in_sel $lno.0 [expr {$lno + 1}].0
1745 } else {
1746 set selected_paths($path) 1
1747 $w tag add in_sel $lno.0 [expr {$lno + 1}].0
1748 }
1749}
1750
1751proc add_range_to_selection {w x y} {
833eda73 1752 global file_lists last_clicked selected_paths
24263b77
SP
1753
1754 if {[lindex $last_clicked 0] ne $w} {
1755 toggle_or_diff $w $x $y
1756 return
cb07fc2a 1757 }
24263b77 1758
833eda73 1759 set lno [lindex [split [$w index @$x,$y] .] 0]
24263b77
SP
1760 set lc [lindex $last_clicked 1]
1761 if {$lc < $lno} {
1762 set begin $lc
1763 set end $lno
1764 } else {
1765 set begin $lno
1766 set end $lc
1767 }
1768
1769 foreach path [lrange $file_lists($w) \
1770 [expr {$begin - 1}] \
1771 [expr {$end - 1}]] {
1772 set selected_paths($path) 1
1773 }
1774 $w tag add in_sel $begin.0 [expr {$end + 1}].0
cb07fc2a
SP
1775}
1776
1777######################################################################
1778##
a4bee597 1779## ui construction
db453781 1780
6bbd1cb9 1781load_config 0
92148d80 1782apply_config
2ebba528
SP
1783set ui_comm {}
1784
cb07fc2a 1785# -- Menu Bar
a49c67d1 1786#
b4946930 1787menu .mbar -tearoff 0
1ac17950
CS
1788.mbar add cascade -label [mc Repository] -menu .mbar.repository
1789.mbar add cascade -label [mc Edit] -menu .mbar.edit
64a906f8 1790if {[is_enabled branch]} {
1ac17950 1791 .mbar add cascade -label [mc Branch] -menu .mbar.branch
700a65ce 1792}
2ebba528 1793if {[is_enabled multicommit] || [is_enabled singlecommit]} {
a9813cb5 1794 .mbar add cascade -label [mc Commit@@noun] -menu .mbar.commit
2ebba528 1795}
64a906f8 1796if {[is_enabled transport]} {
1ac17950 1797 .mbar add cascade -label [mc Merge] -menu .mbar.merge
6bdf5e5f 1798 .mbar add cascade -label [mc Remote] -menu .mbar.remote
4ccdab02 1799}
cb07fc2a
SP
1800. configure -menu .mbar
1801
a4abfa62 1802# -- Repository Menu
a49c67d1 1803#
a4abfa62 1804menu .mbar.repository
35874c16
SP
1805
1806.mbar.repository add command \
1ac17950 1807 -label [mc "Browse Current Branch's Files"] \
c74b6c66 1808 -command {browser::new $current_branch}
a8139888 1809set ui_browse_current [.mbar.repository index last]
8e891fac 1810.mbar.repository add command \
1ac17950 1811 -label [mc "Browse Branch Files..."] \
8e891fac 1812 -command browser_open::dialog
35874c16
SP
1813.mbar.repository add separator
1814
d0752429 1815.mbar.repository add command \
1ac17950 1816 -label [mc "Visualize Current Branch's History"] \
7416bbc6 1817 -command {do_gitk $current_branch}
a8139888 1818set ui_visualize_current [.mbar.repository index last]
5753ef1a 1819.mbar.repository add command \
1ac17950 1820 -label [mc "Visualize All Branch History"] \
7416bbc6 1821 -command {do_gitk --all}
d0752429 1822.mbar.repository add separator
75e355d6 1823
a8139888
SP
1824proc current_branch_write {args} {
1825 global current_branch
1826 .mbar.repository entryconf $::ui_browse_current \
1ac17950 1827 -label [mc "Browse %s's Files" $current_branch]
a8139888 1828 .mbar.repository entryconf $::ui_visualize_current \
1ac17950 1829 -label [mc "Visualize %s's History" $current_branch]
a8139888
SP
1830}
1831trace add variable current_branch write current_branch_write
1832
cf25ddc8 1833if {[is_enabled multicommit]} {
1ac17950 1834 .mbar.repository add command -label [mc "Database Statistics"] \
7416bbc6 1835 -command do_stats
0fd49d0a 1836
1ac17950 1837 .mbar.repository add command -label [mc "Compress Database"] \
7416bbc6 1838 -command do_gc
4aca740b 1839
1ac17950 1840 .mbar.repository add command -label [mc "Verify Database"] \
7416bbc6 1841 -command do_fsck_objects
444f92d0 1842
a4abfa62 1843 .mbar.repository add separator
75e355d6 1844
20ddfcaa
SP
1845 if {[is_Cygwin]} {
1846 .mbar.repository add command \
1ac17950 1847 -label [mc "Create Desktop Icon"] \
7416bbc6 1848 -command do_cygwin_shortcut
20ddfcaa 1849 } elseif {[is_Windows]} {
a4abfa62 1850 .mbar.repository add command \
1ac17950 1851 -label [mc "Create Desktop Icon"] \
7416bbc6 1852 -command do_windows_shortcut
06c31115 1853 } elseif {[is_MacOSX]} {
a4abfa62 1854 .mbar.repository add command \
1ac17950 1855 -label [mc "Create Desktop Icon"] \
7416bbc6 1856 -command do_macosx_app
4aca740b 1857 }
4ccdab02 1858}
85ab313e 1859
1ac17950 1860.mbar.repository add command -label [mc Quit] \
cb07fc2a 1861 -command do_quit \
7416bbc6 1862 -accelerator $M1T-Q
cb07fc2a 1863
9861671d
SP
1864# -- Edit Menu
1865#
1866menu .mbar.edit
1ac17950 1867.mbar.edit add command -label [mc Undo] \
9861671d 1868 -command {catch {[focus] edit undo}} \
7416bbc6 1869 -accelerator $M1T-Z
1ac17950 1870.mbar.edit add command -label [mc Redo] \
9861671d 1871 -command {catch {[focus] edit redo}} \
7416bbc6 1872 -accelerator $M1T-Y
9861671d 1873.mbar.edit add separator
1ac17950 1874.mbar.edit add command -label [mc Cut] \
9861671d 1875 -command {catch {tk_textCut [focus]}} \
7416bbc6 1876 -accelerator $M1T-X
1ac17950 1877.mbar.edit add command -label [mc Copy] \
9861671d 1878 -command {catch {tk_textCopy [focus]}} \
7416bbc6 1879 -accelerator $M1T-C
1ac17950 1880.mbar.edit add command -label [mc Paste] \
9861671d 1881 -command {catch {tk_textPaste [focus]; [focus] see insert}} \
7416bbc6 1882 -accelerator $M1T-V
1ac17950 1883.mbar.edit add command -label [mc Delete] \
9861671d 1884 -command {catch {[focus] delete sel.first sel.last}} \
7416bbc6 1885 -accelerator Del
9861671d 1886.mbar.edit add separator
1ac17950 1887.mbar.edit add command -label [mc "Select All"] \
9861671d 1888 -command {catch {[focus] tag add sel 0.0 end}} \
7416bbc6 1889 -accelerator $M1T-A
9861671d 1890
85ab313e
SP
1891# -- Branch Menu
1892#
64a906f8 1893if {[is_enabled branch]} {
700a65ce
SP
1894 menu .mbar.branch
1895
1ac17950 1896 .mbar.branch add command -label [mc "Create..."] \
b1fa2bff 1897 -command branch_create::dialog \
7416bbc6 1898 -accelerator $M1T-N
700a65ce
SP
1899 lappend disable_on_lock [list .mbar.branch entryconf \
1900 [.mbar.branch index last] -state]
1901
1ac17950 1902 .mbar.branch add command -label [mc "Checkout..."] \
d41b43eb
SP
1903 -command branch_checkout::dialog \
1904 -accelerator $M1T-O
1905 lappend disable_on_lock [list .mbar.branch entryconf \
1906 [.mbar.branch index last] -state]
1907
1ac17950 1908 .mbar.branch add command -label [mc "Rename..."] \
61f82ce7
SP
1909 -command branch_rename::dialog
1910 lappend disable_on_lock [list .mbar.branch entryconf \
1911 [.mbar.branch index last] -state]
1912
1ac17950 1913 .mbar.branch add command -label [mc "Delete..."] \
3206c63d 1914 -command branch_delete::dialog
700a65ce
SP
1915 lappend disable_on_lock [list .mbar.branch entryconf \
1916 [.mbar.branch index last] -state]
fd234dfd 1917
1ac17950 1918 .mbar.branch add command -label [mc "Reset..."] \
a6c9b081 1919 -command merge::reset_hard
fd234dfd
SP
1920 lappend disable_on_lock [list .mbar.branch entryconf \
1921 [.mbar.branch index last] -state]
700a65ce
SP
1922}
1923
cb07fc2a 1924# -- Commit Menu
a49c67d1 1925#
2ebba528
SP
1926if {[is_enabled multicommit] || [is_enabled singlecommit]} {
1927 menu .mbar.commit
1928
1929 .mbar.commit add radiobutton \
1ac17950 1930 -label [mc "New Commit"] \
2ebba528
SP
1931 -command do_select_commit_type \
1932 -variable selected_commit_type \
7416bbc6 1933 -value new
2ebba528
SP
1934 lappend disable_on_lock \
1935 [list .mbar.commit entryconf [.mbar.commit index last] -state]
24ac9b75 1936
2ebba528 1937 .mbar.commit add radiobutton \
1ac17950 1938 -label [mc "Amend Last Commit"] \
2ebba528
SP
1939 -command do_select_commit_type \
1940 -variable selected_commit_type \
7416bbc6 1941 -value amend
2ebba528
SP
1942 lappend disable_on_lock \
1943 [list .mbar.commit entryconf [.mbar.commit index last] -state]
24ac9b75 1944
2ebba528 1945 .mbar.commit add separator
24ac9b75 1946
1ac17950 1947 .mbar.commit add command -label [mc Rescan] \
2ebba528 1948 -command do_rescan \
7416bbc6 1949 -accelerator F5
2ebba528
SP
1950 lappend disable_on_lock \
1951 [list .mbar.commit entryconf [.mbar.commit index last] -state]
24ac9b75 1952
1ac17950 1953 .mbar.commit add command -label [mc "Stage To Commit"] \
7416bbc6 1954 -command do_add_selection
2ebba528
SP
1955 lappend disable_on_lock \
1956 [list .mbar.commit entryconf [.mbar.commit index last] -state]
24ac9b75 1957
1ac17950 1958 .mbar.commit add command -label [mc "Stage Changed Files To Commit"] \
2ebba528 1959 -command do_add_all \
7416bbc6 1960 -accelerator $M1T-I
2ebba528
SP
1961 lappend disable_on_lock \
1962 [list .mbar.commit entryconf [.mbar.commit index last] -state]
24ac9b75 1963
1ac17950 1964 .mbar.commit add command -label [mc "Unstage From Commit"] \
7416bbc6 1965 -command do_unstage_selection
2ebba528
SP
1966 lappend disable_on_lock \
1967 [list .mbar.commit entryconf [.mbar.commit index last] -state]
e734817d 1968
1ac17950 1969 .mbar.commit add command -label [mc "Revert Changes"] \
7416bbc6 1970 -command do_revert_selection
2ebba528
SP
1971 lappend disable_on_lock \
1972 [list .mbar.commit entryconf [.mbar.commit index last] -state]
e734817d 1973
2ebba528 1974 .mbar.commit add separator
1461c5f3 1975
1ac17950 1976 .mbar.commit add command -label [mc "Sign Off"] \
2ebba528 1977 -command do_signoff \
7416bbc6 1978 -accelerator $M1T-S
24ac9b75 1979
a9813cb5 1980 .mbar.commit add command -label [mc Commit@@verb] \
2ebba528 1981 -command do_commit \
7416bbc6 1982 -accelerator $M1T-Return
2ebba528
SP
1983 lappend disable_on_lock \
1984 [list .mbar.commit entryconf [.mbar.commit index last] -state]
1985}
cb07fc2a 1986
9b28a8b9
SP
1987# -- Merge Menu
1988#
1989if {[is_enabled branch]} {
1990 menu .mbar.merge
1ac17950 1991 .mbar.merge add command -label [mc "Local Merge..."] \
a870ddc0
SP
1992 -command merge::dialog \
1993 -accelerator $M1T-M
9b28a8b9
SP
1994 lappend disable_on_lock \
1995 [list .mbar.merge entryconf [.mbar.merge index last] -state]
1ac17950 1996 .mbar.merge add command -label [mc "Abort Merge..."] \
a6c9b081 1997 -command merge::reset_hard
9b28a8b9
SP
1998 lappend disable_on_lock \
1999 [list .mbar.merge entryconf [.mbar.merge index last] -state]
9b28a8b9
SP
2000}
2001
2002# -- Transport Menu
2003#
2004if {[is_enabled transport]} {
6bdf5e5f 2005 menu .mbar.remote
9b28a8b9 2006
6bdf5e5f
SP
2007 .mbar.remote add command \
2008 -label [mc "Push..."] \
840bcfa7
SP
2009 -command do_push_anywhere \
2010 -accelerator $M1T-P
6bdf5e5f
SP
2011 .mbar.remote add command \
2012 -label [mc "Delete..."] \
aa252f19 2013 -command remote_branch_delete::dialog
9b28a8b9
SP
2014}
2015
0c8d7839
SP
2016if {[is_MacOSX]} {
2017 # -- Apple Menu (Mac OS X only)
2018 #
1ac17950 2019 .mbar add cascade -label [mc Apple] -menu .mbar.apple
0c8d7839
SP
2020 menu .mbar.apple
2021
1ac17950 2022 .mbar.apple add command -label [mc "About %s" [appname]] \
7416bbc6 2023 -command do_about
13824e2d
SP
2024 .mbar.apple add separator
2025 .mbar.apple add command \
2026 -label [mc "Preferences..."] \
2027 -command do_options \
2028 -accelerator $M1T-,
2029 bind . <$M1B-,> do_options
0c8d7839
SP
2030} else {
2031 # -- Edit Menu
2032 #
2033 .mbar.edit add separator
1ac17950 2034 .mbar.edit add command -label [mc "Options..."] \
7416bbc6 2035 -command do_options
273984fc 2036}
557afe82 2037
273984fc
SP
2038# -- Help Menu
2039#
1ac17950 2040.mbar add cascade -label [mc Help] -menu .mbar.help
273984fc 2041menu .mbar.help
0c8d7839 2042
273984fc 2043if {![is_MacOSX]} {
1ac17950 2044 .mbar.help add command -label [mc "About %s" [appname]] \
7416bbc6 2045 -command do_about
0c8d7839 2046}
82aa2354 2047
273984fc
SP
2048set browser {}
2049catch {set browser $repo_config(instaweb.browser)}
20ddfcaa 2050set doc_path [file dirname [gitexec]]
273984fc
SP
2051set doc_path [file join $doc_path Documentation index.html]
2052
20ddfcaa 2053if {[is_Cygwin]} {
ee405993 2054 set doc_path [exec cygpath --mixed $doc_path]
273984fc
SP
2055}
2056
2057if {$browser eq {}} {
2058 if {[is_MacOSX]} {
2059 set browser open
20ddfcaa 2060 } elseif {[is_Cygwin]} {
273984fc
SP
2061 set program_files [file dirname [exec cygpath --windir]]
2062 set program_files [file join $program_files {Program Files}]
2063 set firefox [file join $program_files {Mozilla Firefox} firefox.exe]
2064 set ie [file join $program_files {Internet Explorer} IEXPLORE.EXE]
2065 if {[file exists $firefox]} {
2066 set browser $firefox
2067 } elseif {[file exists $ie]} {
2068 set browser $ie
2069 }
2070 unset program_files firefox ie
2071 }
2072}
2073
2074if {[file isfile $doc_path]} {
2075 set doc_url "file:$doc_path"
2076} else {
2077 set doc_url {http://www.kernel.org/pub/software/scm/git/docs/}
2078}
2079
2080if {$browser ne {}} {
1ac17950 2081 .mbar.help add command -label [mc "Online Documentation"] \
7416bbc6 2082 -command [list exec $browser $doc_url &]
273984fc
SP
2083}
2084unset browser doc_path doc_url
82aa2354 2085
2ebba528
SP
2086# -- Standard bindings
2087#
39fa2a98 2088wm protocol . WM_DELETE_WINDOW do_quit
2ebba528
SP
2089bind all <$M1B-Key-q> do_quit
2090bind all <$M1B-Key-Q> do_quit
2091bind all <$M1B-Key-w> {destroy [winfo toplevel %W]}
2092bind all <$M1B-Key-W> {destroy [winfo toplevel %W]}
2093
3e45ee1e
SP
2094set subcommand_args {}
2095proc usage {} {
2096 puts stderr "usage: $::argv0 $::subcommand $::subcommand_args"
2097 exit 1
2098}
2099
2ebba528
SP
2100# -- Not a normal commit type invocation? Do that instead!
2101#
258871d3 2102switch -- $subcommand {
85d2d597 2103browser -
2ebba528 2104blame {
c52c9452
SP
2105 set subcommand_args {rev? path}
2106 if {$argv eq {}} usage
a0db0d61 2107 set head {}
3e45ee1e
SP
2108 set path {}
2109 set is_path 0
2110 foreach a $argv {
2111 if {$is_path || [file exists $_prefix$a]} {
2112 if {$path ne {}} usage
6b3d8b97 2113 set path $_prefix$a
3e45ee1e
SP
2114 break
2115 } elseif {$a eq {--}} {
2116 if {$path ne {}} {
a0db0d61
SP
2117 if {$head ne {}} usage
2118 set head $path
3e45ee1e
SP
2119 set path {}
2120 }
2121 set is_path 1
a0db0d61
SP
2122 } elseif {$head eq {}} {
2123 if {$head ne {}} usage
2124 set head $a
c52c9452 2125 set is_path 1
3e45ee1e
SP
2126 } else {
2127 usage
2128 }
2129 }
2130 unset is_path
2131
c52c9452
SP
2132 if {$head ne {} && $path eq {}} {
2133 set path $_prefix$head
2134 set head {}
2135 }
2136
a0db0d61 2137 if {$head eq {}} {
d41b43eb 2138 load_current_branch
a0db0d61 2139 } else {
02087abc
SP
2140 if {[regexp {^[0-9a-f]{1,39}$} $head]} {
2141 if {[catch {
2142 set head [git rev-parse --verify $head]
2143 } err]} {
2144 puts stderr $err
2145 exit 1
2146 }
2147 }
a0db0d61 2148 set current_branch $head
2ebba528 2149 }
a0db0d61 2150
85d2d597
SP
2151 switch -- $subcommand {
2152 browser {
2153 if {$head eq {}} {
2154 if {$path ne {} && [file isdirectory $path]} {
2155 set head $current_branch
2156 } else {
2157 set head $path
2158 set path {}
2159 }
2160 }
2161 browser::new $head $path
2162 }
2163 blame {
2164 if {$head eq {} && ![file exists $path]} {
c8c4854b 2165 puts stderr [mc "fatal: cannot stat path %s: No such file or directory" $path]
85d2d597
SP
2166 exit 1
2167 }
2168 blame::new $head $path
2169 }
c52c9452 2170 }
258871d3
SP
2171 return
2172}
2173citool -
2174gui {
2175 if {[llength $argv] != 0} {
2176 puts -nonewline stderr "usage: $argv0"
0b2bc460
SP
2177 if {$subcommand ne {gui}
2178 && [file tail $argv0] ne "git-$subcommand"} {
258871d3
SP
2179 puts -nonewline stderr " $subcommand"
2180 }
2181 puts stderr {}
2182 exit 1
2183 }
2184 # fall through to setup UI for commits
2ebba528 2185}
2ebba528 2186default {
c0f7a6c3 2187 puts stderr "usage: $argv0 \[{blame|browser|citool}\]"
2ebba528
SP
2188 exit 1
2189}
2190}
2191
8553b772
SP
2192# -- Branch Control
2193#
2194frame .branch \
2195 -borderwidth 1 \
2196 -relief sunken
2197label .branch.l1 \
1ac17950 2198 -text [mc "Current Branch:"] \
8553b772 2199 -anchor w \
7416bbc6 2200 -justify left
8553b772
SP
2201label .branch.cb \
2202 -textvariable current_branch \
2203 -anchor w \
7416bbc6 2204 -justify left
8553b772
SP
2205pack .branch.l1 -side left
2206pack .branch.cb -side left -fill x
2207pack .branch -side top -fill x
2208
cb07fc2a 2209# -- Main Window Layout
a49c67d1 2210#
cb07fc2a
SP
2211panedwindow .vpane -orient vertical
2212panedwindow .vpane.files -orient horizontal
c5a1eb88 2213.vpane add .vpane.files -sticky nsew -height 100 -width 200
cb07fc2a
SP
2214pack .vpane -anchor n -side top -fill both -expand 1
2215
2216# -- Index File List
a49c67d1 2217#
c5a1eb88 2218frame .vpane.files.index -height 100 -width 200
c73ce762 2219label .vpane.files.index.title -text [mc "Staged Changes (Will Commit)"] \
9adccb05 2220 -background lightgreen
cb07fc2a 2221text $ui_index -background white -borderwidth 0 \
c5a1eb88 2222 -width 20 -height 10 \
3c236977 2223 -wrap none \
6c6dd01a 2224 -cursor $cursor_ptr \
3c236977
SP
2225 -xscrollcommand {.vpane.files.index.sx set} \
2226 -yscrollcommand {.vpane.files.index.sy set} \
cb07fc2a 2227 -state disabled
3c236977
SP
2228scrollbar .vpane.files.index.sx -orient h -command [list $ui_index xview]
2229scrollbar .vpane.files.index.sy -orient v -command [list $ui_index yview]
cb07fc2a 2230pack .vpane.files.index.title -side top -fill x
3c236977
SP
2231pack .vpane.files.index.sx -side bottom -fill x
2232pack .vpane.files.index.sy -side right -fill y
cb07fc2a
SP
2233pack $ui_index -side left -fill both -expand 1
2234.vpane.files add .vpane.files.index -sticky nsew
2235
0812665e 2236# -- Working Directory File List
a49c67d1 2237#
c5a1eb88 2238frame .vpane.files.workdir -height 100 -width 200
c73ce762 2239label .vpane.files.workdir.title -text [mc "Unstaged Changes"] \
9adccb05 2240 -background lightsalmon
0812665e 2241text $ui_workdir -background white -borderwidth 0 \
c5a1eb88 2242 -width 20 -height 10 \
3c236977 2243 -wrap none \
6c6dd01a 2244 -cursor $cursor_ptr \
3c236977
SP
2245 -xscrollcommand {.vpane.files.workdir.sx set} \
2246 -yscrollcommand {.vpane.files.workdir.sy set} \
cb07fc2a 2247 -state disabled
3c236977
SP
2248scrollbar .vpane.files.workdir.sx -orient h -command [list $ui_workdir xview]
2249scrollbar .vpane.files.workdir.sy -orient v -command [list $ui_workdir yview]
0812665e 2250pack .vpane.files.workdir.title -side top -fill x
3c236977
SP
2251pack .vpane.files.workdir.sx -side bottom -fill x
2252pack .vpane.files.workdir.sy -side right -fill y
0812665e
SP
2253pack $ui_workdir -side left -fill both -expand 1
2254.vpane.files add .vpane.files.workdir -sticky nsew
cb07fc2a 2255
0812665e 2256foreach i [list $ui_index $ui_workdir] {
3849bfba
SP
2257 rmsel_tag $i
2258 $i tag conf in_diff -background [$i tag cget in_sel -background]
24263b77
SP
2259}
2260unset i
131f503b 2261
0fb8f9ce 2262# -- Diff and Commit Area
a49c67d1 2263#
8009dcdc 2264frame .vpane.lower -height 300 -width 400
0fb8f9ce
SP
2265frame .vpane.lower.commarea
2266frame .vpane.lower.diff -relief sunken -borderwidth 1
2267pack .vpane.lower.commarea -side top -fill x
2268pack .vpane.lower.diff -side bottom -fill both -expand 1
0fd49d0a 2269.vpane add .vpane.lower -sticky nsew
cb07fc2a
SP
2270
2271# -- Commit Area Buttons
a49c67d1 2272#
0fb8f9ce
SP
2273frame .vpane.lower.commarea.buttons
2274label .vpane.lower.commarea.buttons.l -text {} \
cb07fc2a 2275 -anchor w \
7416bbc6 2276 -justify left
0fb8f9ce
SP
2277pack .vpane.lower.commarea.buttons.l -side top -fill x
2278pack .vpane.lower.commarea.buttons -side left -fill y
131f503b 2279
1ac17950 2280button .vpane.lower.commarea.buttons.rescan -text [mc Rescan] \
7416bbc6 2281 -command do_rescan
0fb8f9ce 2282pack .vpane.lower.commarea.buttons.rescan -side top -fill x
390adaea
SP
2283lappend disable_on_lock \
2284 {.vpane.lower.commarea.buttons.rescan conf -state}
131f503b 2285
1ac17950 2286button .vpane.lower.commarea.buttons.incall -text [mc "Stage Changed"] \
7416bbc6 2287 -command do_add_all
7fe7e733 2288pack .vpane.lower.commarea.buttons.incall -side top -fill x
390adaea
SP
2289lappend disable_on_lock \
2290 {.vpane.lower.commarea.buttons.incall conf -state}
131f503b 2291
1ac17950 2292button .vpane.lower.commarea.buttons.signoff -text [mc "Sign Off"] \
7416bbc6 2293 -command do_signoff
0fb8f9ce 2294pack .vpane.lower.commarea.buttons.signoff -side top -fill x
131f503b 2295
a9813cb5 2296button .vpane.lower.commarea.buttons.commit -text [mc Commit@@verb] \
7416bbc6 2297 -command do_commit
0fb8f9ce 2298pack .vpane.lower.commarea.buttons.commit -side top -fill x
390adaea
SP
2299lappend disable_on_lock \
2300 {.vpane.lower.commarea.buttons.commit conf -state}
cb07fc2a 2301
1ac17950 2302button .vpane.lower.commarea.buttons.push -text [mc Push] \
87b49a53
SP
2303 -command do_push_anywhere
2304pack .vpane.lower.commarea.buttons.push -side top -fill x
2305
cb07fc2a 2306# -- Commit Message Buffer
a49c67d1 2307#
0fb8f9ce 2308frame .vpane.lower.commarea.buffer
24ac9b75 2309frame .vpane.lower.commarea.buffer.header
0fb8f9ce 2310set ui_comm .vpane.lower.commarea.buffer.t
24ac9b75
SP
2311set ui_coml .vpane.lower.commarea.buffer.header.l
2312radiobutton .vpane.lower.commarea.buffer.header.new \
1ac17950 2313 -text [mc "New Commit"] \
24ac9b75
SP
2314 -command do_select_commit_type \
2315 -variable selected_commit_type \
7416bbc6 2316 -value new
24ac9b75
SP
2317lappend disable_on_lock \
2318 [list .vpane.lower.commarea.buffer.header.new conf -state]
2319radiobutton .vpane.lower.commarea.buffer.header.amend \
1ac17950 2320 -text [mc "Amend Last Commit"] \
24ac9b75
SP
2321 -command do_select_commit_type \
2322 -variable selected_commit_type \
7416bbc6 2323 -value amend
24ac9b75
SP
2324lappend disable_on_lock \
2325 [list .vpane.lower.commarea.buffer.header.amend conf -state]
a49c67d1 2326label $ui_coml \
cb07fc2a 2327 -anchor w \
7416bbc6 2328 -justify left
4539eacd
SP
2329proc trace_commit_type {varname args} {
2330 global ui_coml commit_type
2331 switch -glob -- $commit_type {
1ac17950
CS
2332 initial {set txt [mc "Initial Commit Message:"]}
2333 amend {set txt [mc "Amended Commit Message:"]}
2334 amend-initial {set txt [mc "Amended Initial Commit Message:"]}
2335 amend-merge {set txt [mc "Amended Merge Commit Message:"]}
2336 merge {set txt [mc "Merge Commit Message:"]}
2337 * {set txt [mc "Commit Message:"]}
4539eacd
SP
2338 }
2339 $ui_coml conf -text $txt
2340}
2341trace add variable commit_type write trace_commit_type
24ac9b75
SP
2342pack $ui_coml -side left -fill x
2343pack .vpane.lower.commarea.buffer.header.amend -side right
2344pack .vpane.lower.commarea.buffer.header.new -side right
2345
cb07fc2a 2346text $ui_comm -background white -borderwidth 1 \
9861671d 2347 -undo true \
b2c6fcf1 2348 -maxundo 20 \
9861671d 2349 -autoseparators true \
cb07fc2a 2350 -relief sunken \
0fb8f9ce 2351 -width 75 -height 9 -wrap none \
b4946930 2352 -font font_diff \
6c6dd01a 2353 -yscrollcommand {.vpane.lower.commarea.buffer.sby set}
390adaea
SP
2354scrollbar .vpane.lower.commarea.buffer.sby \
2355 -command [list $ui_comm yview]
24ac9b75 2356pack .vpane.lower.commarea.buffer.header -side top -fill x
0fb8f9ce 2357pack .vpane.lower.commarea.buffer.sby -side right -fill y
cb07fc2a 2358pack $ui_comm -side left -fill y
0fb8f9ce
SP
2359pack .vpane.lower.commarea.buffer -side left -fill y
2360
0e794311
SP
2361# -- Commit Message Buffer Context Menu
2362#
e8ab6446
SP
2363set ctxm .vpane.lower.commarea.buffer.ctxm
2364menu $ctxm -tearoff 0
2365$ctxm add command \
1ac17950 2366 -label [mc Cut] \
e8ab6446
SP
2367 -command {tk_textCut $ui_comm}
2368$ctxm add command \
1ac17950 2369 -label [mc Copy] \
e8ab6446
SP
2370 -command {tk_textCopy $ui_comm}
2371$ctxm add command \
1ac17950 2372 -label [mc Paste] \
e8ab6446
SP
2373 -command {tk_textPaste $ui_comm}
2374$ctxm add command \
1ac17950 2375 -label [mc Delete] \
e8ab6446
SP
2376 -command {$ui_comm delete sel.first sel.last}
2377$ctxm add separator
2378$ctxm add command \
1ac17950 2379 -label [mc "Select All"] \
75e78c8a 2380 -command {focus $ui_comm;$ui_comm tag add sel 0.0 end}
e8ab6446 2381$ctxm add command \
1ac17950 2382 -label [mc "Copy All"] \
e8ab6446 2383 -command {
0e794311
SP
2384 $ui_comm tag add sel 0.0 end
2385 tk_textCopy $ui_comm
2386 $ui_comm tag remove sel 0.0 end
e8ab6446
SP
2387 }
2388$ctxm add separator
2389$ctxm add command \
1ac17950 2390 -label [mc "Sign Off"] \
0e794311 2391 -command do_signoff
e8ab6446 2392bind_button3 $ui_comm "tk_popup $ctxm %X %Y"
0e794311 2393
0fb8f9ce 2394# -- Diff Header
a49c67d1 2395#
20a53c02
SP
2396proc trace_current_diff_path {varname args} {
2397 global current_diff_path diff_actions file_states
2398 if {$current_diff_path eq {}} {
e8ab6446
SP
2399 set s {}
2400 set f {}
2401 set p {}
2402 set o disabled
2403 } else {
20a53c02 2404 set p $current_diff_path
e8ab6446 2405 set s [mapdesc [lindex $file_states($p) 0] $p]
1ac17950 2406 set f [mc "File:"]
e8ab6446
SP
2407 set p [escape_path $p]
2408 set o normal
2409 }
2410
2411 .vpane.lower.diff.header.status configure -text $s
2412 .vpane.lower.diff.header.file configure -text $f
2413 .vpane.lower.diff.header.path configure -text $p
2414 foreach w $diff_actions {
2415 uplevel #0 $w $o
2416 }
2417}
20a53c02 2418trace add variable current_diff_path write trace_current_diff_path
e8ab6446 2419
9adccb05 2420frame .vpane.lower.diff.header -background gold
e8ab6446 2421label .vpane.lower.diff.header.status \
9adccb05 2422 -background gold \
3e7b0e1d
SP
2423 -width $max_status_desc \
2424 -anchor w \
7416bbc6 2425 -justify left
e8ab6446 2426label .vpane.lower.diff.header.file \
9adccb05 2427 -background gold \
e8ab6446 2428 -anchor w \
7416bbc6 2429 -justify left
e8ab6446 2430label .vpane.lower.diff.header.path \
9adccb05 2431 -background gold \
fce89e46 2432 -anchor w \
7416bbc6 2433 -justify left
e8ab6446
SP
2434pack .vpane.lower.diff.header.status -side left
2435pack .vpane.lower.diff.header.file -side left
2436pack .vpane.lower.diff.header.path -fill x
2437set ctxm .vpane.lower.diff.header.ctxm
2438menu $ctxm -tearoff 0
2439$ctxm add command \
1ac17950 2440 -label [mc Copy] \
fce89e46
SP
2441 -command {
2442 clipboard clear
2443 clipboard append \
2444 -format STRING \
2445 -type STRING \
20a53c02 2446 -- $current_diff_path
fce89e46 2447 }
e8ab6446
SP
2448lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
2449bind_button3 .vpane.lower.diff.header.path "tk_popup $ctxm %X %Y"
0fb8f9ce
SP
2450
2451# -- Diff Body
a49c67d1 2452#
0fb8f9ce
SP
2453frame .vpane.lower.diff.body
2454set ui_diff .vpane.lower.diff.body.t
2455text $ui_diff -background white -borderwidth 0 \
2456 -width 80 -height 15 -wrap none \
b4946930 2457 -font font_diff \
0fb8f9ce
SP
2458 -xscrollcommand {.vpane.lower.diff.body.sbx set} \
2459 -yscrollcommand {.vpane.lower.diff.body.sby set} \
0fb8f9ce
SP
2460 -state disabled
2461scrollbar .vpane.lower.diff.body.sbx -orient horizontal \
2462 -command [list $ui_diff xview]
2463scrollbar .vpane.lower.diff.body.sby -orient vertical \
2464 -command [list $ui_diff yview]
2465pack .vpane.lower.diff.body.sbx -side bottom -fill x
2466pack .vpane.lower.diff.body.sby -side right -fill y
2467pack $ui_diff -side left -fill both -expand 1
2468pack .vpane.lower.diff.header -side top -fill x
2469pack .vpane.lower.diff.body -side bottom -fill both -expand 1
2470
30b14ed3 2471$ui_diff tag conf d_cr -elide true
ca521566
SP
2472$ui_diff tag conf d_@ -foreground blue -font font_diffbold
2473$ui_diff tag conf d_+ -foreground {#00a000}
fec4a785
SP
2474$ui_diff tag conf d_- -foreground red
2475
ca521566 2476$ui_diff tag conf d_++ -foreground {#00a000}
fec4a785
SP
2477$ui_diff tag conf d_-- -foreground red
2478$ui_diff tag conf d_+s \
ca521566
SP
2479 -foreground {#00a000} \
2480 -background {#e2effa}
fec4a785
SP
2481$ui_diff tag conf d_-s \
2482 -foreground red \
ca521566 2483 -background {#e2effa}
fec4a785 2484$ui_diff tag conf d_s+ \
ca521566
SP
2485 -foreground {#00a000} \
2486 -background ivory1
fec4a785
SP
2487$ui_diff tag conf d_s- \
2488 -foreground red \
ca521566 2489 -background ivory1
fec4a785
SP
2490
2491$ui_diff tag conf d<<<<<<< \
2492 -foreground orange \
2493 -font font_diffbold
2494$ui_diff tag conf d======= \
2495 -foreground orange \
2496 -font font_diffbold
2497$ui_diff tag conf d>>>>>>> \
2498 -foreground orange \
2499 -font font_diffbold
cb07fc2a 2500
ca521566
SP
2501$ui_diff tag raise sel
2502
0e794311
SP
2503# -- Diff Body Context Menu
2504#
e8ab6446
SP
2505set ctxm .vpane.lower.diff.body.ctxm
2506menu $ctxm -tearoff 0
68c30b4a 2507$ctxm add command \
1ac17950 2508 -label [mc Refresh] \
68c30b4a 2509 -command reshow_diff
86773d9b 2510lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
e8ab6446 2511$ctxm add command \
1ac17950 2512 -label [mc Copy] \
e8ab6446
SP
2513 -command {tk_textCopy $ui_diff}
2514lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
2515$ctxm add command \
1ac17950 2516 -label [mc "Select All"] \
75e78c8a 2517 -command {focus $ui_diff;$ui_diff tag add sel 0.0 end}
e8ab6446
SP
2518lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
2519$ctxm add command \
1ac17950 2520 -label [mc "Copy All"] \
e8ab6446 2521 -command {
0e794311
SP
2522 $ui_diff tag add sel 0.0 end
2523 tk_textCopy $ui_diff
2524 $ui_diff tag remove sel 0.0 end
e8ab6446
SP
2525 }
2526lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
2527$ctxm add separator
a25c5189 2528$ctxm add command \
1ac17950 2529 -label [mc "Apply/Reverse Hunk"] \
a25c5189
SP
2530 -command {apply_hunk $cursorX $cursorY}
2531set ui_diff_applyhunk [$ctxm index last]
2532lappend diff_actions [list $ctxm entryconf $ui_diff_applyhunk -state]
2533$ctxm add separator
e8ab6446 2534$ctxm add command \
1ac17950 2535 -label [mc "Decrease Font Size"] \
b4946930 2536 -command {incr_font_size font_diff -1}
e8ab6446
SP
2537lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
2538$ctxm add command \
1ac17950 2539 -label [mc "Increase Font Size"] \
b4946930 2540 -command {incr_font_size font_diff 1}
e8ab6446
SP
2541lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
2542$ctxm add separator
2543$ctxm add command \
1ac17950 2544 -label [mc "Show Less Context"] \
b8848f77 2545 -command {if {$repo_config(gui.diffcontext) >= 1} {
358d8de8
SP
2546 incr repo_config(gui.diffcontext) -1
2547 reshow_diff
2548 }}
e8ab6446
SP
2549lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
2550$ctxm add command \
1ac17950 2551 -label [mc "Show More Context"] \
b8848f77 2552 -command {if {$repo_config(gui.diffcontext) < 99} {
358d8de8
SP
2553 incr repo_config(gui.diffcontext)
2554 reshow_diff
b8848f77 2555 }}
e8ab6446
SP
2556lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
2557$ctxm add separator
1ac17950 2558$ctxm add command -label [mc "Options..."] \
8009dcdc 2559 -command do_options
83751fc1 2560proc popup_diff_menu {ctxm x y X Y} {
ce015c21 2561 global current_diff_path file_states
83751fc1
SP
2562 set ::cursorX $x
2563 set ::cursorY $y
2564 if {$::ui_index eq $::current_diff_side} {
1ac17950 2565 set l [mc "Unstage Hunk From Commit"]
a25c5189 2566 } else {
1ac17950 2567 set l [mc "Stage Hunk For Commit"]
a25c5189 2568 }
047d94d5
SP
2569 if {$::is_3way_diff
2570 || $current_diff_path eq {}
2571 || ![info exists file_states($current_diff_path)]
2572 || {_O} eq [lindex $file_states($current_diff_path) 0]} {
9c9f5fa9 2573 set s disabled
047d94d5
SP
2574 } else {
2575 set s normal
9c9f5fa9 2576 }
9f4119eb 2577 $ctxm entryconf $::ui_diff_applyhunk -state $s -label $l
83751fc1
SP
2578 tk_popup $ctxm $X $Y
2579}
2580bind_button3 $ui_diff [list popup_diff_menu $ctxm %x %y %X %Y]
0e794311 2581
cb07fc2a 2582# -- Status Bar
e8ab6446 2583#
51530d17 2584set main_status [::status_bar::new .status]
cb07fc2a 2585pack .status -anchor w -side bottom -fill x
1ac17950 2586$main_status show [mc "Initializing..."]
cb07fc2a 2587
2d19516d 2588# -- Load geometry
e8ab6446 2589#
2d19516d 2590catch {
51f4d16b 2591set gm $repo_config(gui.geometry)
c4fe7728
SP
2592wm geometry . [lindex $gm 0]
2593.vpane sash place 0 \
2594 [lindex [.vpane sash coord 0] 0] \
2595 [lindex $gm 1]
2596.vpane.files sash place 0 \
2597 [lindex $gm 2] \
2598 [lindex [.vpane.files sash coord 0] 1]
c4fe7728 2599unset gm
390adaea 2600}
2d19516d 2601
cb07fc2a 2602# -- Key Bindings
e8ab6446 2603#
ec6b424a 2604bind $ui_comm <$M1B-Key-Return> {do_commit;break}
93e912c5
SP
2605bind $ui_comm <$M1B-Key-i> {do_add_all;break}
2606bind $ui_comm <$M1B-Key-I> {do_add_all;break}
9861671d
SP
2607bind $ui_comm <$M1B-Key-x> {tk_textCut %W;break}
2608bind $ui_comm <$M1B-Key-X> {tk_textCut %W;break}
2609bind $ui_comm <$M1B-Key-c> {tk_textCopy %W;break}
2610bind $ui_comm <$M1B-Key-C> {tk_textCopy %W;break}
2611bind $ui_comm <$M1B-Key-v> {tk_textPaste %W; %W see insert; break}
2612bind $ui_comm <$M1B-Key-V> {tk_textPaste %W; %W see insert; break}
2613bind $ui_comm <$M1B-Key-a> {%W tag add sel 0.0 end;break}
2614bind $ui_comm <$M1B-Key-A> {%W tag add sel 0.0 end;break}
2615
2616bind $ui_diff <$M1B-Key-x> {tk_textCopy %W;break}
2617bind $ui_diff <$M1B-Key-X> {tk_textCopy %W;break}
2618bind $ui_diff <$M1B-Key-c> {tk_textCopy %W;break}
2619bind $ui_diff <$M1B-Key-C> {tk_textCopy %W;break}
2620bind $ui_diff <$M1B-Key-v> {break}
2621bind $ui_diff <$M1B-Key-V> {break}
2622bind $ui_diff <$M1B-Key-a> {%W tag add sel 0.0 end;break}
2623bind $ui_diff <$M1B-Key-A> {%W tag add sel 0.0 end;break}
b2c6fcf1
SP
2624bind $ui_diff <Key-Up> {catch {%W yview scroll -1 units};break}
2625bind $ui_diff <Key-Down> {catch {%W yview scroll 1 units};break}
2626bind $ui_diff <Key-Left> {catch {%W xview scroll -1 units};break}
2627bind $ui_diff <Key-Right> {catch {%W xview scroll 1 units};break}
60aa065f
SP
2628bind $ui_diff <Key-k> {catch {%W yview scroll -1 units};break}
2629bind $ui_diff <Key-j> {catch {%W yview scroll 1 units};break}
2630bind $ui_diff <Key-h> {catch {%W xview scroll -1 units};break}
2631bind $ui_diff <Key-l> {catch {%W xview scroll 1 units};break}
2632bind $ui_diff <Control-Key-b> {catch {%W yview scroll -1 pages};break}
2633bind $ui_diff <Control-Key-f> {catch {%W yview scroll 1 pages};break}
23effa79 2634bind $ui_diff <Button-1> {focus %W}
49b86f01 2635
64a906f8 2636if {[is_enabled branch]} {
b1fa2bff
SP
2637 bind . <$M1B-Key-n> branch_create::dialog
2638 bind . <$M1B-Key-N> branch_create::dialog
d41b43eb
SP
2639 bind . <$M1B-Key-o> branch_checkout::dialog
2640 bind . <$M1B-Key-O> branch_checkout::dialog
a870ddc0
SP
2641 bind . <$M1B-Key-m> merge::dialog
2642 bind . <$M1B-Key-M> merge::dialog
bd29ebc3 2643}
840bcfa7
SP
2644if {[is_enabled transport]} {
2645 bind . <$M1B-Key-p> do_push_anywhere
2646 bind . <$M1B-Key-P> do_push_anywhere
2647}
bd29ebc3 2648
f1e031bb
SP
2649bind . <Key-F5> do_rescan
2650bind . <$M1B-Key-r> do_rescan
2651bind . <$M1B-Key-R> do_rescan
07123f40
SP
2652bind . <$M1B-Key-s> do_signoff
2653bind . <$M1B-Key-S> do_signoff
93e912c5
SP
2654bind . <$M1B-Key-i> do_add_all
2655bind . <$M1B-Key-I> do_add_all
07123f40 2656bind . <$M1B-Key-Return> do_commit
0812665e 2657foreach i [list $ui_index $ui_workdir] {
24263b77
SP
2658 bind $i <Button-1> "toggle_or_diff $i %x %y; break"
2659 bind $i <$M1B-Button-1> "add_one_to_selection $i %x %y; break"
2660 bind $i <Shift-Button-1> "add_range_to_selection $i %x %y; break"
cb07fc2a 2661}
62aac80b
SP
2662unset i
2663
2664set file_lists($ui_index) [list]
0812665e 2665set file_lists($ui_workdir) [list]
a49c67d1 2666
19c82148 2667wm title . "[appname] ([reponame]) [file normalize [file dirname [gitdir]]]"
cb07fc2a 2668focus -force $ui_comm
1d8b3cbf 2669
85ab313e
SP
2670# -- Warn the user about environmental problems. Cygwin's Tcl
2671# does *not* pass its env array onto any processes it spawns.
2672# This means that git processes get none of our environment.
1d8b3cbf 2673#
20ddfcaa 2674if {[is_Cygwin]} {
1d8b3cbf
SP
2675 set ignored_env 0
2676 set suggest_user {}
c8c4854b 2677 set msg [mc "Possible environment issues exist.
1d8b3cbf
SP
2678
2679The following environment variables are probably
2680going to be ignored by any Git subprocess run
c8c4854b 2681by %s:
1d8b3cbf 2682
c8c4854b 2683" [appname]]
1d8b3cbf
SP
2684 foreach name [array names env] {
2685 switch -regexp -- $name {
2686 {^GIT_INDEX_FILE$} -
2687 {^GIT_OBJECT_DIRECTORY$} -
2688 {^GIT_ALTERNATE_OBJECT_DIRECTORIES$} -
2689 {^GIT_DIFF_OPTS$} -
2690 {^GIT_EXTERNAL_DIFF$} -
2691 {^GIT_PAGER$} -
2692 {^GIT_TRACE$} -
2693 {^GIT_CONFIG$} -
2694 {^GIT_CONFIG_LOCAL$} -
2695 {^GIT_(AUTHOR|COMMITTER)_DATE$} {
2696 append msg " - $name\n"
2697 incr ignored_env
2698 }
2699 {^GIT_(AUTHOR|COMMITTER)_(NAME|EMAIL)$} {
2700 append msg " - $name\n"
2701 incr ignored_env
2702 set suggest_user $name
2703 }
2704 }
2705 }
2706 if {$ignored_env > 0} {
c8c4854b 2707 append msg [mc "
1d8b3cbf 2708This is due to a known issue with the
c8c4854b 2709Tcl binary distributed by Cygwin."]
1d8b3cbf
SP
2710
2711 if {$suggest_user ne {}} {
c8c4854b 2712 append msg [mc "
1d8b3cbf 2713
c8c4854b 2714A good replacement for %s
1d8b3cbf
SP
2715is placing values for the user.name and
2716user.email settings into your personal
2717~/.gitconfig file.
c8c4854b 2718" $suggest_user]
1d8b3cbf
SP
2719 }
2720 warn_popup $msg
2721 }
2722 unset ignored_env msg suggest_user name
2723}
2724
85ab313e
SP
2725# -- Only initialize complex UI if we are going to stay running.
2726#
64a906f8 2727if {[is_enabled transport]} {
4ccdab02 2728 load_all_remotes
85ab313e 2729
6bdf5e5f 2730 set n [.mbar.remote index end]
3f7fd924 2731 populate_push_menu
6bdf5e5f
SP
2732 populate_fetch_menu
2733 set n [expr {[.mbar.remote index end] - $n}]
2734 if {$n > 0} {
2735 .mbar.remote insert $n separator
2736 }
2737 unset n
4ccdab02 2738}
85ab313e 2739
4578c5cb
SP
2740if {[winfo exists $ui_comm]} {
2741 set GITGUI_BCK_exists [load_message GITGUI_BCK]
2742
2743 # -- If both our backup and message files exist use the
2744 # newer of the two files to initialize the buffer.
2745 #
2746 if {$GITGUI_BCK_exists} {
2747 set m [gitdir GITGUI_MSG]
2748 if {[file isfile $m]} {
2749 if {[file mtime [gitdir GITGUI_BCK]] > [file mtime $m]} {
2750 catch {file delete [gitdir GITGUI_MSG]}
2751 } else {
2752 $ui_comm delete 0.0 end
2753 $ui_comm edit reset
2754 $ui_comm edit modified false
2755 catch {file delete [gitdir GITGUI_BCK]}
2756 set GITGUI_BCK_exists 0
2757 }
2758 }
2759 unset m
2760 }
2761
2762 proc backup_commit_buffer {} {
2763 global ui_comm GITGUI_BCK_exists
2764
2765 set m [$ui_comm edit modified]
2766 if {$m || $GITGUI_BCK_exists} {
2767 set msg [string trim [$ui_comm get 0.0 end]]
2768 regsub -all -line {[ \r\t]+$} $msg {} msg
2769
2770 if {$msg eq {}} {
2771 if {$GITGUI_BCK_exists} {
2772 catch {file delete [gitdir GITGUI_BCK]}
2773 set GITGUI_BCK_exists 0
2774 }
2775 } elseif {$m} {
2776 catch {
2777 set fd [open [gitdir GITGUI_BCK] w]
2778 puts -nonewline $fd $msg
2779 close $fd
2780 set GITGUI_BCK_exists 1
2781 }
2782 }
2783
2784 $ui_comm edit modified false
2785 }
2786
2787 set ::GITGUI_BCK_i [after 2000 backup_commit_buffer]
2788 }
2789
2790 backup_commit_buffer
2791}
2792
53716a7b 2793lock_index begin-read
301dfaa9
SP
2794if {![winfo ismapped .]} {
2795 wm deiconify .
2796}
8f52548a 2797after 1 do_rescan
3972b987
SP
2798if {[is_enabled multicommit]} {
2799 after 1000 hint_gc
2800}