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