]> asedeno.scripts.mit.edu Git - git.git/blob - git-gui.sh
Merge branch 'maint'
[git.git] / git-gui.sh
1 #!/bin/sh
2 # Tcl ignores the next line -*- tcl -*- \
3 exec wish "$0" -- "$@"
4
5 set appvers {@@GITGUI_VERSION@@}
6 set copyright {
7 Copyright © 2006, 2007 Shawn Pearce, et. al.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA}
22
23 ######################################################################
24 ##
25 ## enable verbose loading?
26
27 if {![catch {set _verbose $env(GITGUI_VERBOSE)}]} {
28         unset _verbose
29         rename auto_load real__auto_load
30         proc auto_load {name args} {
31                 puts stderr "auto_load $name"
32                 return [uplevel 1 real__auto_load $name $args]
33         }
34         rename source real__source
35         proc source {name} {
36                 puts stderr "source    $name"
37                 uplevel 1 real__source $name
38         }
39 }
40
41 ######################################################################
42 ##
43 ## configure our library
44
45 set oguilib {@@GITGUI_LIBDIR@@}
46 set oguirel {@@GITGUI_RELATIVE@@}
47 if {$oguirel eq {1}} {
48         set oguilib [file dirname [file dirname [file normalize $argv0]]]
49         set oguilib [file join $oguilib share git-gui lib]
50 } elseif {[string match @@* $oguirel]} {
51         set oguilib [file join [file dirname [file normalize $argv0]] lib]
52 }
53
54 set idx [file join $oguilib tclIndex]
55 if {[catch {set fd [open $idx r]} err]} {
56         catch {wm withdraw .}
57         tk_messageBox \
58                 -icon error \
59                 -type ok \
60                 -title "git-gui: fatal error" \
61                 -message $err
62         exit 1
63 }
64 if {[gets $fd] eq {# Autogenerated by git-gui Makefile}} {
65         set idx [list]
66         while {[gets $fd n] >= 0} {
67                 if {$n ne {} && ![string match #* $n]} {
68                         lappend idx $n
69                 }
70         }
71 } else {
72         set idx {}
73 }
74 close $fd
75
76 if {$idx ne {}} {
77         set loaded [list]
78         foreach p $idx {
79                 if {[lsearch -exact $loaded $p] >= 0} continue
80                 source [file join $oguilib $p]
81                 lappend loaded $p
82         }
83         unset loaded p
84 } else {
85         set auto_path [concat [list $oguilib] $auto_path]
86 }
87 unset -nocomplain oguirel idx fd
88
89 ######################################################################
90 ##
91 ## read only globals
92
93 set _appname [lindex [file split $argv0] end]
94 set _gitdir {}
95 set _gitexec {}
96 set _reponame {}
97 set _iscygwin {}
98
99 proc appname {} {
100         global _appname
101         return $_appname
102 }
103
104 proc gitdir {args} {
105         global _gitdir
106         if {$args eq {}} {
107                 return $_gitdir
108         }
109         return [eval [concat [list file join $_gitdir] $args]]
110 }
111
112 proc gitexec {args} {
113         global _gitexec
114         if {$_gitexec eq {}} {
115                 if {[catch {set _gitexec [git --exec-path]} err]} {
116                         error "Git not installed?\n\n$err"
117                 }
118         }
119         if {$args eq {}} {
120                 return $_gitexec
121         }
122         return [eval [concat [list file join $_gitexec] $args]]
123 }
124
125 proc reponame {} {
126         global _reponame
127         return $_reponame
128 }
129
130 proc is_MacOSX {} {
131         global tcl_platform tk_library
132         if {[tk windowingsystem] eq {aqua}} {
133                 return 1
134         }
135         return 0
136 }
137
138 proc is_Windows {} {
139         global tcl_platform
140         if {$tcl_platform(platform) eq {windows}} {
141                 return 1
142         }
143         return 0
144 }
145
146 proc is_Cygwin {} {
147         global tcl_platform _iscygwin
148         if {$_iscygwin eq {}} {
149                 if {$tcl_platform(platform) eq {windows}} {
150                         if {[catch {set p [exec cygpath --windir]} err]} {
151                                 set _iscygwin 0
152                         } else {
153                                 set _iscygwin 1
154                         }
155                 } else {
156                         set _iscygwin 0
157                 }
158         }
159         return $_iscygwin
160 }
161
162 proc is_enabled {option} {
163         global enabled_options
164         if {[catch {set on $enabled_options($option)}]} {return 0}
165         return $on
166 }
167
168 proc enable_option {option} {
169         global enabled_options
170         set enabled_options($option) 1
171 }
172
173 proc disable_option {option} {
174         global enabled_options
175         set enabled_options($option) 0
176 }
177
178 ######################################################################
179 ##
180 ## config
181
182 proc is_many_config {name} {
183         switch -glob -- $name {
184         remote.*.fetch -
185         remote.*.push
186                 {return 1}
187         *
188                 {return 0}
189         }
190 }
191
192 proc is_config_true {name} {
193         global repo_config
194         if {[catch {set v $repo_config($name)}]} {
195                 return 0
196         } elseif {$v eq {true} || $v eq {1} || $v eq {yes}} {
197                 return 1
198         } else {
199                 return 0
200         }
201 }
202
203 proc get_config {name} {
204         global repo_config
205         if {[catch {set v $repo_config($name)}]} {
206                 return {}
207         } else {
208                 return $v
209         }
210 }
211
212 proc load_config {include_global} {
213         global repo_config global_config default_config
214
215         array unset global_config
216         if {$include_global} {
217                 catch {
218                         set fd_rc [open "| git config --global --list" r]
219                         while {[gets $fd_rc line] >= 0} {
220                                 if {[regexp {^([^=]+)=(.*)$} $line line name value]} {
221                                         if {[is_many_config $name]} {
222                                                 lappend global_config($name) $value
223                                         } else {
224                                                 set global_config($name) $value
225                                         }
226                                 }
227                         }
228                         close $fd_rc
229                 }
230         }
231
232         array unset repo_config
233         catch {
234                 set fd_rc [open "| git config --list" r]
235                 while {[gets $fd_rc line] >= 0} {
236                         if {[regexp {^([^=]+)=(.*)$} $line line name value]} {
237                                 if {[is_many_config $name]} {
238                                         lappend repo_config($name) $value
239                                 } else {
240                                         set repo_config($name) $value
241                                 }
242                         }
243                 }
244                 close $fd_rc
245         }
246
247         foreach name [array names default_config] {
248                 if {[catch {set v $global_config($name)}]} {
249                         set global_config($name) $default_config($name)
250                 }
251                 if {[catch {set v $repo_config($name)}]} {
252                         set repo_config($name) $default_config($name)
253                 }
254         }
255 }
256
257 ######################################################################
258 ##
259 ## handy utils
260
261 proc git {args} {
262         return [eval exec git $args]
263 }
264
265 auto_load tk_optionMenu
266 rename tk_optionMenu real__tkOptionMenu
267 proc tk_optionMenu {w varName args} {
268         set m [eval real__tkOptionMenu $w $varName $args]
269         $m configure -font font_ui
270         $w configure -font font_ui
271         return $m
272 }
273
274 ######################################################################
275 ##
276 ## version check
277
278 if {{--version} eq $argv || {version} eq $argv} {
279         puts "git-gui version $appvers"
280         exit
281 }
282
283 set req_maj 1
284 set req_min 5
285
286 if {[catch {set v [git --version]} err]} {
287         catch {wm withdraw .}
288         error_popup "Cannot determine Git version:
289
290 $err
291
292 [appname] requires Git $req_maj.$req_min or later."
293         exit 1
294 }
295 if {[regexp {^git version (\d+)\.(\d+)} $v _junk act_maj act_min]} {
296         if {$act_maj < $req_maj
297                 || ($act_maj == $req_maj && $act_min < $req_min)} {
298                 catch {wm withdraw .}
299                 error_popup "[appname] requires Git $req_maj.$req_min or later.
300
301 You are using $v."
302                 exit 1
303         }
304 } else {
305         catch {wm withdraw .}
306         error_popup "Cannot parse Git version string:\n\n$v"
307         exit 1
308 }
309 unset -nocomplain v _junk act_maj act_min req_maj req_min
310
311 ######################################################################
312 ##
313 ## repository setup
314
315 if {[catch {
316                 set _gitdir $env(GIT_DIR)
317                 set _prefix {}
318                 }]
319         && [catch {
320                 set _gitdir [git rev-parse --git-dir]
321                 set _prefix [git rev-parse --show-prefix]
322         } err]} {
323         catch {wm withdraw .}
324         error_popup "Cannot find the git directory:\n\n$err"
325         exit 1
326 }
327 if {![file isdirectory $_gitdir] && [is_Cygwin]} {
328         catch {set _gitdir [exec cygpath --unix $_gitdir]}
329 }
330 if {![file isdirectory $_gitdir]} {
331         catch {wm withdraw .}
332         error_popup "Git directory not found:\n\n$_gitdir"
333         exit 1
334 }
335 if {[lindex [file split $_gitdir] end] ne {.git}} {
336         catch {wm withdraw .}
337         error_popup "Cannot use funny .git directory:\n\n$_gitdir"
338         exit 1
339 }
340 if {[catch {cd [file dirname $_gitdir]} err]} {
341         catch {wm withdraw .}
342         error_popup "No working directory [file dirname $_gitdir]:\n\n$err"
343         exit 1
344 }
345 set _reponame [lindex [file split \
346         [file normalize [file dirname $_gitdir]]] \
347         end]
348
349 ######################################################################
350 ##
351 ## global init
352
353 set current_diff_path {}
354 set current_diff_side {}
355 set diff_actions [list]
356 set ui_status_value {Initializing...}
357
358 set HEAD {}
359 set PARENT {}
360 set MERGE_HEAD [list]
361 set commit_type {}
362 set empty_tree {}
363 set current_branch {}
364 set current_diff_path {}
365 set selected_commit_type new
366
367 ######################################################################
368 ##
369 ## task management
370
371 set rescan_active 0
372 set diff_active 0
373 set last_clicked {}
374
375 set disable_on_lock [list]
376 set index_lock_type none
377
378 proc lock_index {type} {
379         global index_lock_type disable_on_lock
380
381         if {$index_lock_type eq {none}} {
382                 set index_lock_type $type
383                 foreach w $disable_on_lock {
384                         uplevel #0 $w disabled
385                 }
386                 return 1
387         } elseif {$index_lock_type eq "begin-$type"} {
388                 set index_lock_type $type
389                 return 1
390         }
391         return 0
392 }
393
394 proc unlock_index {} {
395         global index_lock_type disable_on_lock
396
397         set index_lock_type none
398         foreach w $disable_on_lock {
399                 uplevel #0 $w normal
400         }
401 }
402
403 ######################################################################
404 ##
405 ## status
406
407 proc repository_state {ctvar hdvar mhvar} {
408         global current_branch
409         upvar $ctvar ct $hdvar hd $mhvar mh
410
411         set mh [list]
412
413         if {[catch {set current_branch [git symbolic-ref HEAD]}]} {
414                 set current_branch {}
415         } else {
416                 regsub ^refs/((heads|tags|remotes)/)? \
417                         $current_branch \
418                         {} \
419                         current_branch
420         }
421
422         if {[catch {set hd [git rev-parse --verify HEAD]}]} {
423                 set hd {}
424                 set ct initial
425                 return
426         }
427
428         set merge_head [gitdir MERGE_HEAD]
429         if {[file exists $merge_head]} {
430                 set ct merge
431                 set fd_mh [open $merge_head r]
432                 while {[gets $fd_mh line] >= 0} {
433                         lappend mh $line
434                 }
435                 close $fd_mh
436                 return
437         }
438
439         set ct normal
440 }
441
442 proc PARENT {} {
443         global PARENT empty_tree
444
445         set p [lindex $PARENT 0]
446         if {$p ne {}} {
447                 return $p
448         }
449         if {$empty_tree eq {}} {
450                 set empty_tree [git mktree << {}]
451         }
452         return $empty_tree
453 }
454
455 proc rescan {after {honor_trustmtime 1}} {
456         global HEAD PARENT MERGE_HEAD commit_type
457         global ui_index ui_workdir ui_status_value ui_comm
458         global rescan_active file_states
459         global repo_config
460
461         if {$rescan_active > 0 || ![lock_index read]} return
462
463         repository_state newType newHEAD newMERGE_HEAD
464         if {[string match amend* $commit_type]
465                 && $newType eq {normal}
466                 && $newHEAD eq $HEAD} {
467         } else {
468                 set HEAD $newHEAD
469                 set PARENT $newHEAD
470                 set MERGE_HEAD $newMERGE_HEAD
471                 set commit_type $newType
472         }
473
474         array unset file_states
475
476         if {![$ui_comm edit modified]
477                 || [string trim [$ui_comm get 0.0 end]] eq {}} {
478                 if {[load_message GITGUI_MSG]} {
479                 } elseif {[load_message MERGE_MSG]} {
480                 } elseif {[load_message SQUASH_MSG]} {
481                 }
482                 $ui_comm edit reset
483                 $ui_comm edit modified false
484         }
485
486         if {[is_enabled branch]} {
487                 load_all_heads
488                 populate_branch_menu
489         }
490
491         if {$honor_trustmtime && $repo_config(gui.trustmtime) eq {true}} {
492                 rescan_stage2 {} $after
493         } else {
494                 set rescan_active 1
495                 set ui_status_value {Refreshing file status...}
496                 set cmd [list git update-index]
497                 lappend cmd -q
498                 lappend cmd --unmerged
499                 lappend cmd --ignore-missing
500                 lappend cmd --refresh
501                 set fd_rf [open "| $cmd" r]
502                 fconfigure $fd_rf -blocking 0 -translation binary
503                 fileevent $fd_rf readable \
504                         [list rescan_stage2 $fd_rf $after]
505         }
506 }
507
508 proc rescan_stage2 {fd after} {
509         global ui_status_value
510         global rescan_active buf_rdi buf_rdf buf_rlo
511
512         if {$fd ne {}} {
513                 read $fd
514                 if {![eof $fd]} return
515                 close $fd
516         }
517
518         set ls_others [list | git ls-files --others -z \
519                 --exclude-per-directory=.gitignore]
520         set info_exclude [gitdir info exclude]
521         if {[file readable $info_exclude]} {
522                 lappend ls_others "--exclude-from=$info_exclude"
523         }
524
525         set buf_rdi {}
526         set buf_rdf {}
527         set buf_rlo {}
528
529         set rescan_active 3
530         set ui_status_value {Scanning for modified files ...}
531         set fd_di [open "| git diff-index --cached -z [PARENT]" r]
532         set fd_df [open "| git diff-files -z" r]
533         set fd_lo [open $ls_others r]
534
535         fconfigure $fd_di -blocking 0 -translation binary -encoding binary
536         fconfigure $fd_df -blocking 0 -translation binary -encoding binary
537         fconfigure $fd_lo -blocking 0 -translation binary -encoding binary
538         fileevent $fd_di readable [list read_diff_index $fd_di $after]
539         fileevent $fd_df readable [list read_diff_files $fd_df $after]
540         fileevent $fd_lo readable [list read_ls_others $fd_lo $after]
541 }
542
543 proc load_message {file} {
544         global ui_comm
545
546         set f [gitdir $file]
547         if {[file isfile $f]} {
548                 if {[catch {set fd [open $f r]}]} {
549                         return 0
550                 }
551                 set content [string trim [read $fd]]
552                 close $fd
553                 regsub -all -line {[ \r\t]+$} $content {} content
554                 $ui_comm delete 0.0 end
555                 $ui_comm insert end $content
556                 return 1
557         }
558         return 0
559 }
560
561 proc read_diff_index {fd after} {
562         global buf_rdi
563
564         append buf_rdi [read $fd]
565         set c 0
566         set n [string length $buf_rdi]
567         while {$c < $n} {
568                 set z1 [string first "\0" $buf_rdi $c]
569                 if {$z1 == -1} break
570                 incr z1
571                 set z2 [string first "\0" $buf_rdi $z1]
572                 if {$z2 == -1} break
573
574                 incr c
575                 set i [split [string range $buf_rdi $c [expr {$z1 - 2}]] { }]
576                 set p [string range $buf_rdi $z1 [expr {$z2 - 1}]]
577                 merge_state \
578                         [encoding convertfrom $p] \
579                         [lindex $i 4]? \
580                         [list [lindex $i 0] [lindex $i 2]] \
581                         [list]
582                 set c $z2
583                 incr c
584         }
585         if {$c < $n} {
586                 set buf_rdi [string range $buf_rdi $c end]
587         } else {
588                 set buf_rdi {}
589         }
590
591         rescan_done $fd buf_rdi $after
592 }
593
594 proc read_diff_files {fd after} {
595         global buf_rdf
596
597         append buf_rdf [read $fd]
598         set c 0
599         set n [string length $buf_rdf]
600         while {$c < $n} {
601                 set z1 [string first "\0" $buf_rdf $c]
602                 if {$z1 == -1} break
603                 incr z1
604                 set z2 [string first "\0" $buf_rdf $z1]
605                 if {$z2 == -1} break
606
607                 incr c
608                 set i [split [string range $buf_rdf $c [expr {$z1 - 2}]] { }]
609                 set p [string range $buf_rdf $z1 [expr {$z2 - 1}]]
610                 merge_state \
611                         [encoding convertfrom $p] \
612                         ?[lindex $i 4] \
613                         [list] \
614                         [list [lindex $i 0] [lindex $i 2]]
615                 set c $z2
616                 incr c
617         }
618         if {$c < $n} {
619                 set buf_rdf [string range $buf_rdf $c end]
620         } else {
621                 set buf_rdf {}
622         }
623
624         rescan_done $fd buf_rdf $after
625 }
626
627 proc read_ls_others {fd after} {
628         global buf_rlo
629
630         append buf_rlo [read $fd]
631         set pck [split $buf_rlo "\0"]
632         set buf_rlo [lindex $pck end]
633         foreach p [lrange $pck 0 end-1] {
634                 merge_state [encoding convertfrom $p] ?O
635         }
636         rescan_done $fd buf_rlo $after
637 }
638
639 proc rescan_done {fd buf after} {
640         global rescan_active current_diff_path
641         global file_states repo_config
642         upvar $buf to_clear
643
644         if {![eof $fd]} return
645         set to_clear {}
646         close $fd
647         if {[incr rescan_active -1] > 0} return
648
649         prune_selection
650         unlock_index
651         display_all_files
652         if {$current_diff_path ne {}} reshow_diff
653         uplevel #0 $after
654 }
655
656 proc prune_selection {} {
657         global file_states selected_paths
658
659         foreach path [array names selected_paths] {
660                 if {[catch {set still_here $file_states($path)}]} {
661                         unset selected_paths($path)
662                 }
663         }
664 }
665
666 ######################################################################
667 ##
668 ## ui helpers
669
670 proc mapicon {w state path} {
671         global all_icons
672
673         if {[catch {set r $all_icons($state$w)}]} {
674                 puts "error: no icon for $w state={$state} $path"
675                 return file_plain
676         }
677         return $r
678 }
679
680 proc mapdesc {state path} {
681         global all_descs
682
683         if {[catch {set r $all_descs($state)}]} {
684                 puts "error: no desc for state={$state} $path"
685                 return $state
686         }
687         return $r
688 }
689
690 proc escape_path {path} {
691         regsub -all {\\} $path "\\\\" path
692         regsub -all "\n" $path "\\n" path
693         return $path
694 }
695
696 proc short_path {path} {
697         return [escape_path [lindex [file split $path] end]]
698 }
699
700 set next_icon_id 0
701 set null_sha1 [string repeat 0 40]
702
703 proc merge_state {path new_state {head_info {}} {index_info {}}} {
704         global file_states next_icon_id null_sha1
705
706         set s0 [string index $new_state 0]
707         set s1 [string index $new_state 1]
708
709         if {[catch {set info $file_states($path)}]} {
710                 set state __
711                 set icon n[incr next_icon_id]
712         } else {
713                 set state [lindex $info 0]
714                 set icon [lindex $info 1]
715                 if {$head_info eq {}}  {set head_info  [lindex $info 2]}
716                 if {$index_info eq {}} {set index_info [lindex $info 3]}
717         }
718
719         if     {$s0 eq {?}} {set s0 [string index $state 0]} \
720         elseif {$s0 eq {_}} {set s0 _}
721
722         if     {$s1 eq {?}} {set s1 [string index $state 1]} \
723         elseif {$s1 eq {_}} {set s1 _}
724
725         if {$s0 eq {A} && $s1 eq {_} && $head_info eq {}} {
726                 set head_info [list 0 $null_sha1]
727         } elseif {$s0 ne {_} && [string index $state 0] eq {_}
728                 && $head_info eq {}} {
729                 set head_info $index_info
730         }
731
732         set file_states($path) [list $s0$s1 $icon \
733                 $head_info $index_info \
734                 ]
735         return $state
736 }
737
738 proc display_file_helper {w path icon_name old_m new_m} {
739         global file_lists
740
741         if {$new_m eq {_}} {
742                 set lno [lsearch -sorted -exact $file_lists($w) $path]
743                 if {$lno >= 0} {
744                         set file_lists($w) [lreplace $file_lists($w) $lno $lno]
745                         incr lno
746                         $w conf -state normal
747                         $w delete $lno.0 [expr {$lno + 1}].0
748                         $w conf -state disabled
749                 }
750         } elseif {$old_m eq {_} && $new_m ne {_}} {
751                 lappend file_lists($w) $path
752                 set file_lists($w) [lsort -unique $file_lists($w)]
753                 set lno [lsearch -sorted -exact $file_lists($w) $path]
754                 incr lno
755                 $w conf -state normal
756                 $w image create $lno.0 \
757                         -align center -padx 5 -pady 1 \
758                         -name $icon_name \
759                         -image [mapicon $w $new_m $path]
760                 $w insert $lno.1 "[escape_path $path]\n"
761                 $w conf -state disabled
762         } elseif {$old_m ne $new_m} {
763                 $w conf -state normal
764                 $w image conf $icon_name -image [mapicon $w $new_m $path]
765                 $w conf -state disabled
766         }
767 }
768
769 proc display_file {path state} {
770         global file_states selected_paths
771         global ui_index ui_workdir
772
773         set old_m [merge_state $path $state]
774         set s $file_states($path)
775         set new_m [lindex $s 0]
776         set icon_name [lindex $s 1]
777
778         set o [string index $old_m 0]
779         set n [string index $new_m 0]
780         if {$o eq {U}} {
781                 set o _
782         }
783         if {$n eq {U}} {
784                 set n _
785         }
786         display_file_helper     $ui_index $path $icon_name $o $n
787
788         if {[string index $old_m 0] eq {U}} {
789                 set o U
790         } else {
791                 set o [string index $old_m 1]
792         }
793         if {[string index $new_m 0] eq {U}} {
794                 set n U
795         } else {
796                 set n [string index $new_m 1]
797         }
798         display_file_helper     $ui_workdir $path $icon_name $o $n
799
800         if {$new_m eq {__}} {
801                 unset file_states($path)
802                 catch {unset selected_paths($path)}
803         }
804 }
805
806 proc display_all_files_helper {w path icon_name m} {
807         global file_lists
808
809         lappend file_lists($w) $path
810         set lno [expr {[lindex [split [$w index end] .] 0] - 1}]
811         $w image create end \
812                 -align center -padx 5 -pady 1 \
813                 -name $icon_name \
814                 -image [mapicon $w $m $path]
815         $w insert end "[escape_path $path]\n"
816 }
817
818 proc display_all_files {} {
819         global ui_index ui_workdir
820         global file_states file_lists
821         global last_clicked
822
823         $ui_index conf -state normal
824         $ui_workdir conf -state normal
825
826         $ui_index delete 0.0 end
827         $ui_workdir delete 0.0 end
828         set last_clicked {}
829
830         set file_lists($ui_index) [list]
831         set file_lists($ui_workdir) [list]
832
833         foreach path [lsort [array names file_states]] {
834                 set s $file_states($path)
835                 set m [lindex $s 0]
836                 set icon_name [lindex $s 1]
837
838                 set s [string index $m 0]
839                 if {$s ne {U} && $s ne {_}} {
840                         display_all_files_helper $ui_index $path \
841                                 $icon_name $s
842                 }
843
844                 if {[string index $m 0] eq {U}} {
845                         set s U
846                 } else {
847                         set s [string index $m 1]
848                 }
849                 if {$s ne {_}} {
850                         display_all_files_helper $ui_workdir $path \
851                                 $icon_name $s
852                 }
853         }
854
855         $ui_index conf -state disabled
856         $ui_workdir conf -state disabled
857 }
858
859 ######################################################################
860 ##
861 ## icons
862
863 set filemask {
864 #define mask_width 14
865 #define mask_height 15
866 static unsigned char mask_bits[] = {
867    0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f,
868    0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f,
869    0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f};
870 }
871
872 image create bitmap file_plain -background white -foreground black -data {
873 #define plain_width 14
874 #define plain_height 15
875 static unsigned char plain_bits[] = {
876    0xfe, 0x01, 0x02, 0x03, 0x02, 0x05, 0x02, 0x09, 0x02, 0x1f, 0x02, 0x10,
877    0x02, 0x10, 0x02, 0x10, 0x02, 0x10, 0x02, 0x10, 0x02, 0x10, 0x02, 0x10,
878    0x02, 0x10, 0x02, 0x10, 0xfe, 0x1f};
879 } -maskdata $filemask
880
881 image create bitmap file_mod -background white -foreground blue -data {
882 #define mod_width 14
883 #define mod_height 15
884 static unsigned char mod_bits[] = {
885    0xfe, 0x01, 0x02, 0x03, 0x7a, 0x05, 0x02, 0x09, 0x7a, 0x1f, 0x02, 0x10,
886    0xfa, 0x17, 0x02, 0x10, 0xfa, 0x17, 0x02, 0x10, 0xfa, 0x17, 0x02, 0x10,
887    0xfa, 0x17, 0x02, 0x10, 0xfe, 0x1f};
888 } -maskdata $filemask
889
890 image create bitmap file_fulltick -background white -foreground "#007000" -data {
891 #define file_fulltick_width 14
892 #define file_fulltick_height 15
893 static unsigned char file_fulltick_bits[] = {
894    0xfe, 0x01, 0x02, 0x1a, 0x02, 0x0c, 0x02, 0x0c, 0x02, 0x16, 0x02, 0x16,
895    0x02, 0x13, 0x00, 0x13, 0x86, 0x11, 0x8c, 0x11, 0xd8, 0x10, 0xf2, 0x10,
896    0x62, 0x10, 0x02, 0x10, 0xfe, 0x1f};
897 } -maskdata $filemask
898
899 image create bitmap file_parttick -background white -foreground "#005050" -data {
900 #define parttick_width 14
901 #define parttick_height 15
902 static unsigned char parttick_bits[] = {
903    0xfe, 0x01, 0x02, 0x03, 0x7a, 0x05, 0x02, 0x09, 0x7a, 0x1f, 0x02, 0x10,
904    0x7a, 0x14, 0x02, 0x16, 0x02, 0x13, 0x8a, 0x11, 0xda, 0x10, 0x72, 0x10,
905    0x22, 0x10, 0x02, 0x10, 0xfe, 0x1f};
906 } -maskdata $filemask
907
908 image create bitmap file_question -background white -foreground black -data {
909 #define file_question_width 14
910 #define file_question_height 15
911 static unsigned char file_question_bits[] = {
912    0xfe, 0x01, 0x02, 0x02, 0xe2, 0x04, 0xf2, 0x09, 0x1a, 0x1b, 0x0a, 0x13,
913    0x82, 0x11, 0xc2, 0x10, 0x62, 0x10, 0x62, 0x10, 0x02, 0x10, 0x62, 0x10,
914    0x62, 0x10, 0x02, 0x10, 0xfe, 0x1f};
915 } -maskdata $filemask
916
917 image create bitmap file_removed -background white -foreground red -data {
918 #define file_removed_width 14
919 #define file_removed_height 15
920 static unsigned char file_removed_bits[] = {
921    0xfe, 0x01, 0x02, 0x03, 0x02, 0x05, 0x02, 0x09, 0x02, 0x1f, 0x02, 0x10,
922    0x1a, 0x16, 0x32, 0x13, 0xe2, 0x11, 0xc2, 0x10, 0xe2, 0x11, 0x32, 0x13,
923    0x1a, 0x16, 0x02, 0x10, 0xfe, 0x1f};
924 } -maskdata $filemask
925
926 image create bitmap file_merge -background white -foreground blue -data {
927 #define file_merge_width 14
928 #define file_merge_height 15
929 static unsigned char file_merge_bits[] = {
930    0xfe, 0x01, 0x02, 0x03, 0x62, 0x05, 0x62, 0x09, 0x62, 0x1f, 0x62, 0x10,
931    0xfa, 0x11, 0xf2, 0x10, 0x62, 0x10, 0x02, 0x10, 0xfa, 0x17, 0x02, 0x10,
932    0xfa, 0x17, 0x02, 0x10, 0xfe, 0x1f};
933 } -maskdata $filemask
934
935 set file_dir_data {
936 #define file_width 18
937 #define file_height 18
938 static unsigned char file_bits[] = {
939   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0x03, 0x00,
940   0x0c, 0x03, 0x00, 0x04, 0xfe, 0x00, 0x06, 0x80, 0x00, 0xff, 0x9f, 0x00,
941   0x03, 0x98, 0x00, 0x02, 0x90, 0x00, 0x06, 0xb0, 0x00, 0x04, 0xa0, 0x00,
942   0x0c, 0xe0, 0x00, 0x08, 0xc0, 0x00, 0xf8, 0xff, 0x00, 0x00, 0x00, 0x00,
943   0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
944 }
945 image create bitmap file_dir -background white -foreground blue \
946         -data $file_dir_data -maskdata $file_dir_data
947 unset file_dir_data
948
949 set file_uplevel_data {
950 #define up_width 15
951 #define up_height 15
952 static unsigned char up_bits[] = {
953   0x80, 0x00, 0xc0, 0x01, 0xe0, 0x03, 0xf0, 0x07, 0xf8, 0x0f, 0xfc, 0x1f,
954   0xfe, 0x3f, 0xc0, 0x01, 0xc0, 0x01, 0xc0, 0x01, 0xc0, 0x01, 0xc0, 0x01,
955   0xc0, 0x01, 0xc0, 0x01, 0x00, 0x00};
956 }
957 image create bitmap file_uplevel -background white -foreground red \
958         -data $file_uplevel_data -maskdata $file_uplevel_data
959 unset file_uplevel_data
960
961 set ui_index .vpane.files.index.list
962 set ui_workdir .vpane.files.workdir.list
963
964 set all_icons(_$ui_index)   file_plain
965 set all_icons(A$ui_index)   file_fulltick
966 set all_icons(M$ui_index)   file_fulltick
967 set all_icons(D$ui_index)   file_removed
968 set all_icons(U$ui_index)   file_merge
969
970 set all_icons(_$ui_workdir) file_plain
971 set all_icons(M$ui_workdir) file_mod
972 set all_icons(D$ui_workdir) file_question
973 set all_icons(U$ui_workdir) file_merge
974 set all_icons(O$ui_workdir) file_plain
975
976 set max_status_desc 0
977 foreach i {
978                 {__ "Unmodified"}
979
980                 {_M "Modified, not staged"}
981                 {M_ "Staged for commit"}
982                 {MM "Portions staged for commit"}
983                 {MD "Staged for commit, missing"}
984
985                 {_O "Untracked, not staged"}
986                 {A_ "Staged for commit"}
987                 {AM "Portions staged for commit"}
988                 {AD "Staged for commit, missing"}
989
990                 {_D "Missing"}
991                 {D_ "Staged for removal"}
992                 {DO "Staged for removal, still present"}
993
994                 {U_ "Requires merge resolution"}
995                 {UU "Requires merge resolution"}
996                 {UM "Requires merge resolution"}
997                 {UD "Requires merge resolution"}
998         } {
999         if {$max_status_desc < [string length [lindex $i 1]]} {
1000                 set max_status_desc [string length [lindex $i 1]]
1001         }
1002         set all_descs([lindex $i 0]) [lindex $i 1]
1003 }
1004 unset i
1005
1006 ######################################################################
1007 ##
1008 ## util
1009
1010 proc bind_button3 {w cmd} {
1011         bind $w <Any-Button-3> $cmd
1012         if {[is_MacOSX]} {
1013                 bind $w <Control-Button-1> $cmd
1014         }
1015 }
1016
1017 proc scrollbar2many {list mode args} {
1018         foreach w $list {eval $w $mode $args}
1019 }
1020
1021 proc many2scrollbar {list mode sb top bottom} {
1022         $sb set $top $bottom
1023         foreach w $list {$w $mode moveto $top}
1024 }
1025
1026 proc incr_font_size {font {amt 1}} {
1027         set sz [font configure $font -size]
1028         incr sz $amt
1029         font configure $font -size $sz
1030         font configure ${font}bold -size $sz
1031 }
1032
1033 ######################################################################
1034 ##
1035 ## ui commands
1036
1037 set starting_gitk_msg {Starting gitk... please wait...}
1038
1039 proc do_gitk {revs} {
1040         global env ui_status_value starting_gitk_msg
1041
1042         # -- Always start gitk through whatever we were loaded with.  This
1043         #    lets us bypass using shell process on Windows systems.
1044         #
1045         set cmd [list [info nameofexecutable]]
1046         lappend cmd [gitexec gitk]
1047         if {$revs ne {}} {
1048                 append cmd { }
1049                 append cmd $revs
1050         }
1051
1052         if {[catch {eval exec $cmd &} err]} {
1053                 error_popup "Failed to start gitk:\n\n$err"
1054         } else {
1055                 set ui_status_value $starting_gitk_msg
1056                 after 10000 {
1057                         if {$ui_status_value eq $starting_gitk_msg} {
1058                                 set ui_status_value {Ready.}
1059                         }
1060                 }
1061         }
1062 }
1063
1064 set is_quitting 0
1065
1066 proc do_quit {} {
1067         global ui_comm is_quitting repo_config commit_type
1068
1069         if {$is_quitting} return
1070         set is_quitting 1
1071
1072         if {[winfo exists $ui_comm]} {
1073                 # -- Stash our current commit buffer.
1074                 #
1075                 set save [gitdir GITGUI_MSG]
1076                 set msg [string trim [$ui_comm get 0.0 end]]
1077                 regsub -all -line {[ \r\t]+$} $msg {} msg
1078                 if {(![string match amend* $commit_type]
1079                         || [$ui_comm edit modified])
1080                         && $msg ne {}} {
1081                         catch {
1082                                 set fd [open $save w]
1083                                 puts -nonewline $fd $msg
1084                                 close $fd
1085                         }
1086                 } else {
1087                         catch {file delete $save}
1088                 }
1089
1090                 # -- Stash our current window geometry into this repository.
1091                 #
1092                 set cfg_geometry [list]
1093                 lappend cfg_geometry [wm geometry .]
1094                 lappend cfg_geometry [lindex [.vpane sash coord 0] 1]
1095                 lappend cfg_geometry [lindex [.vpane.files sash coord 0] 0]
1096                 if {[catch {set rc_geometry $repo_config(gui.geometry)}]} {
1097                         set rc_geometry {}
1098                 }
1099                 if {$cfg_geometry ne $rc_geometry} {
1100                         catch {git config gui.geometry $cfg_geometry}
1101                 }
1102         }
1103
1104         destroy .
1105 }
1106
1107 proc do_rescan {} {
1108         rescan {set ui_status_value {Ready.}}
1109 }
1110
1111 proc do_commit {} {
1112         commit_tree
1113 }
1114
1115 proc toggle_or_diff {w x y} {
1116         global file_states file_lists current_diff_path ui_index ui_workdir
1117         global last_clicked selected_paths
1118
1119         set pos [split [$w index @$x,$y] .]
1120         set lno [lindex $pos 0]
1121         set col [lindex $pos 1]
1122         set path [lindex $file_lists($w) [expr {$lno - 1}]]
1123         if {$path eq {}} {
1124                 set last_clicked {}
1125                 return
1126         }
1127
1128         set last_clicked [list $w $lno]
1129         array unset selected_paths
1130         $ui_index tag remove in_sel 0.0 end
1131         $ui_workdir tag remove in_sel 0.0 end
1132
1133         if {$col == 0} {
1134                 if {$current_diff_path eq $path} {
1135                         set after {reshow_diff;}
1136                 } else {
1137                         set after {}
1138                 }
1139                 if {$w eq $ui_index} {
1140                         update_indexinfo \
1141                                 "Unstaging [short_path $path] from commit" \
1142                                 [list $path] \
1143                                 [concat $after {set ui_status_value {Ready.}}]
1144                 } elseif {$w eq $ui_workdir} {
1145                         update_index \
1146                                 "Adding [short_path $path]" \
1147                                 [list $path] \
1148                                 [concat $after {set ui_status_value {Ready.}}]
1149                 }
1150         } else {
1151                 show_diff $path $w $lno
1152         }
1153 }
1154
1155 proc add_one_to_selection {w x y} {
1156         global file_lists last_clicked selected_paths
1157
1158         set lno [lindex [split [$w index @$x,$y] .] 0]
1159         set path [lindex $file_lists($w) [expr {$lno - 1}]]
1160         if {$path eq {}} {
1161                 set last_clicked {}
1162                 return
1163         }
1164
1165         if {$last_clicked ne {}
1166                 && [lindex $last_clicked 0] ne $w} {
1167                 array unset selected_paths
1168                 [lindex $last_clicked 0] tag remove in_sel 0.0 end
1169         }
1170
1171         set last_clicked [list $w $lno]
1172         if {[catch {set in_sel $selected_paths($path)}]} {
1173                 set in_sel 0
1174         }
1175         if {$in_sel} {
1176                 unset selected_paths($path)
1177                 $w tag remove in_sel $lno.0 [expr {$lno + 1}].0
1178         } else {
1179                 set selected_paths($path) 1
1180                 $w tag add in_sel $lno.0 [expr {$lno + 1}].0
1181         }
1182 }
1183
1184 proc add_range_to_selection {w x y} {
1185         global file_lists last_clicked selected_paths
1186
1187         if {[lindex $last_clicked 0] ne $w} {
1188                 toggle_or_diff $w $x $y
1189                 return
1190         }
1191
1192         set lno [lindex [split [$w index @$x,$y] .] 0]
1193         set lc [lindex $last_clicked 1]
1194         if {$lc < $lno} {
1195                 set begin $lc
1196                 set end $lno
1197         } else {
1198                 set begin $lno
1199                 set end $lc
1200         }
1201
1202         foreach path [lrange $file_lists($w) \
1203                 [expr {$begin - 1}] \
1204                 [expr {$end - 1}]] {
1205                 set selected_paths($path) 1
1206         }
1207         $w tag add in_sel $begin.0 [expr {$end + 1}].0
1208 }
1209
1210 ######################################################################
1211 ##
1212 ## config defaults
1213
1214 set cursor_ptr arrow
1215 font create font_diff -family Courier -size 10
1216 font create font_ui
1217 catch {
1218         label .dummy
1219         eval font configure font_ui [font actual [.dummy cget -font]]
1220         destroy .dummy
1221 }
1222
1223 font create font_uibold
1224 font create font_diffbold
1225
1226 foreach class {Button Checkbutton Entry Label
1227                 Labelframe Listbox Menu Message
1228                 Radiobutton Text} {
1229         option add *$class.font font_ui
1230 }
1231 unset class
1232
1233 if {[is_Windows] || [is_MacOSX]} {
1234         option add *Menu.tearOff 0
1235 }
1236
1237 if {[is_MacOSX]} {
1238         set M1B M1
1239         set M1T Cmd
1240 } else {
1241         set M1B Control
1242         set M1T Ctrl
1243 }
1244
1245 proc apply_config {} {
1246         global repo_config font_descs
1247
1248         foreach option $font_descs {
1249                 set name [lindex $option 0]
1250                 set font [lindex $option 1]
1251                 if {[catch {
1252                         foreach {cn cv} $repo_config(gui.$name) {
1253                                 font configure $font $cn $cv
1254                         }
1255                         } err]} {
1256                         error_popup "Invalid font specified in gui.$name:\n\n$err"
1257                 }
1258                 foreach {cn cv} [font configure $font] {
1259                         font configure ${font}bold $cn $cv
1260                 }
1261                 font configure ${font}bold -weight bold
1262         }
1263 }
1264
1265 set default_config(merge.diffstat) true
1266 set default_config(merge.summary) false
1267 set default_config(merge.verbosity) 2
1268 set default_config(user.name) {}
1269 set default_config(user.email) {}
1270
1271 set default_config(gui.pruneduringfetch) false
1272 set default_config(gui.trustmtime) false
1273 set default_config(gui.diffcontext) 5
1274 set default_config(gui.newbranchtemplate) {}
1275 set default_config(gui.fontui) [font configure font_ui]
1276 set default_config(gui.fontdiff) [font configure font_diff]
1277 set font_descs {
1278         {fontui   font_ui   {Main Font}}
1279         {fontdiff font_diff {Diff/Console Font}}
1280 }
1281 load_config 0
1282 apply_config
1283
1284 ######################################################################
1285 ##
1286 ## feature option selection
1287
1288 if {[regexp {^git-(.+)$} [appname] _junk subcommand]} {
1289         unset _junk
1290 } else {
1291         set subcommand gui
1292 }
1293 if {$subcommand eq {gui.sh}} {
1294         set subcommand gui
1295 }
1296 if {$subcommand eq {gui} && [llength $argv] > 0} {
1297         set subcommand [lindex $argv 0]
1298         set argv [lrange $argv 1 end]
1299 }
1300
1301 enable_option multicommit
1302 enable_option branch
1303 enable_option transport
1304
1305 switch -- $subcommand {
1306 browser -
1307 blame {
1308         disable_option multicommit
1309         disable_option branch
1310         disable_option transport
1311 }
1312 citool {
1313         enable_option singlecommit
1314
1315         disable_option multicommit
1316         disable_option branch
1317         disable_option transport
1318 }
1319 }
1320
1321 ######################################################################
1322 ##
1323 ## ui construction
1324
1325 set ui_comm {}
1326
1327 # -- Menu Bar
1328 #
1329 menu .mbar -tearoff 0
1330 .mbar add cascade -label Repository -menu .mbar.repository
1331 .mbar add cascade -label Edit -menu .mbar.edit
1332 if {[is_enabled branch]} {
1333         .mbar add cascade -label Branch -menu .mbar.branch
1334 }
1335 if {[is_enabled multicommit] || [is_enabled singlecommit]} {
1336         .mbar add cascade -label Commit -menu .mbar.commit
1337 }
1338 if {[is_enabled transport]} {
1339         .mbar add cascade -label Merge -menu .mbar.merge
1340         .mbar add cascade -label Fetch -menu .mbar.fetch
1341         .mbar add cascade -label Push -menu .mbar.push
1342 }
1343 . configure -menu .mbar
1344
1345 # -- Repository Menu
1346 #
1347 menu .mbar.repository
1348
1349 .mbar.repository add command \
1350         -label {Browse Current Branch} \
1351         -command {browser::new $current_branch}
1352 trace add variable current_branch write ".mbar.repository entryconf [.mbar.repository index last] -label \"Browse \$current_branch\" ;#"
1353 .mbar.repository add separator
1354
1355 .mbar.repository add command \
1356         -label {Visualize Current Branch} \
1357         -command {do_gitk $current_branch}
1358 trace add variable current_branch write ".mbar.repository entryconf [.mbar.repository index last] -label \"Visualize \$current_branch\" ;#"
1359 .mbar.repository add command \
1360         -label {Visualize All Branches} \
1361         -command {do_gitk --all}
1362 .mbar.repository add separator
1363
1364 if {[is_enabled multicommit]} {
1365         .mbar.repository add command -label {Database Statistics} \
1366                 -command do_stats
1367
1368         .mbar.repository add command -label {Compress Database} \
1369                 -command do_gc
1370
1371         .mbar.repository add command -label {Verify Database} \
1372                 -command do_fsck_objects
1373
1374         .mbar.repository add separator
1375
1376         if {[is_Cygwin]} {
1377                 .mbar.repository add command \
1378                         -label {Create Desktop Icon} \
1379                         -command do_cygwin_shortcut
1380         } elseif {[is_Windows]} {
1381                 .mbar.repository add command \
1382                         -label {Create Desktop Icon} \
1383                         -command do_windows_shortcut
1384         } elseif {[is_MacOSX]} {
1385                 .mbar.repository add command \
1386                         -label {Create Desktop Icon} \
1387                         -command do_macosx_app
1388         }
1389 }
1390
1391 .mbar.repository add command -label Quit \
1392         -command do_quit \
1393         -accelerator $M1T-Q
1394
1395 # -- Edit Menu
1396 #
1397 menu .mbar.edit
1398 .mbar.edit add command -label Undo \
1399         -command {catch {[focus] edit undo}} \
1400         -accelerator $M1T-Z
1401 .mbar.edit add command -label Redo \
1402         -command {catch {[focus] edit redo}} \
1403         -accelerator $M1T-Y
1404 .mbar.edit add separator
1405 .mbar.edit add command -label Cut \
1406         -command {catch {tk_textCut [focus]}} \
1407         -accelerator $M1T-X
1408 .mbar.edit add command -label Copy \
1409         -command {catch {tk_textCopy [focus]}} \
1410         -accelerator $M1T-C
1411 .mbar.edit add command -label Paste \
1412         -command {catch {tk_textPaste [focus]; [focus] see insert}} \
1413         -accelerator $M1T-V
1414 .mbar.edit add command -label Delete \
1415         -command {catch {[focus] delete sel.first sel.last}} \
1416         -accelerator Del
1417 .mbar.edit add separator
1418 .mbar.edit add command -label {Select All} \
1419         -command {catch {[focus] tag add sel 0.0 end}} \
1420         -accelerator $M1T-A
1421
1422 # -- Branch Menu
1423 #
1424 if {[is_enabled branch]} {
1425         menu .mbar.branch
1426
1427         .mbar.branch add command -label {Create...} \
1428                 -command do_create_branch \
1429                 -accelerator $M1T-N
1430         lappend disable_on_lock [list .mbar.branch entryconf \
1431                 [.mbar.branch index last] -state]
1432
1433         .mbar.branch add command -label {Rename...} \
1434                 -command branch_rename::dialog
1435         lappend disable_on_lock [list .mbar.branch entryconf \
1436                 [.mbar.branch index last] -state]
1437
1438         .mbar.branch add command -label {Delete...} \
1439                 -command do_delete_branch
1440         lappend disable_on_lock [list .mbar.branch entryconf \
1441                 [.mbar.branch index last] -state]
1442
1443         .mbar.branch add command -label {Reset...} \
1444                 -command merge::reset_hard
1445         lappend disable_on_lock [list .mbar.branch entryconf \
1446                 [.mbar.branch index last] -state]
1447 }
1448
1449 # -- Commit Menu
1450 #
1451 if {[is_enabled multicommit] || [is_enabled singlecommit]} {
1452         menu .mbar.commit
1453
1454         .mbar.commit add radiobutton \
1455                 -label {New Commit} \
1456                 -command do_select_commit_type \
1457                 -variable selected_commit_type \
1458                 -value new
1459         lappend disable_on_lock \
1460                 [list .mbar.commit entryconf [.mbar.commit index last] -state]
1461
1462         .mbar.commit add radiobutton \
1463                 -label {Amend Last Commit} \
1464                 -command do_select_commit_type \
1465                 -variable selected_commit_type \
1466                 -value amend
1467         lappend disable_on_lock \
1468                 [list .mbar.commit entryconf [.mbar.commit index last] -state]
1469
1470         .mbar.commit add separator
1471
1472         .mbar.commit add command -label Rescan \
1473                 -command do_rescan \
1474                 -accelerator F5
1475         lappend disable_on_lock \
1476                 [list .mbar.commit entryconf [.mbar.commit index last] -state]
1477
1478         .mbar.commit add command -label {Add To Commit} \
1479                 -command do_add_selection
1480         lappend disable_on_lock \
1481                 [list .mbar.commit entryconf [.mbar.commit index last] -state]
1482
1483         .mbar.commit add command -label {Add Existing To Commit} \
1484                 -command do_add_all \
1485                 -accelerator $M1T-I
1486         lappend disable_on_lock \
1487                 [list .mbar.commit entryconf [.mbar.commit index last] -state]
1488
1489         .mbar.commit add command -label {Unstage From Commit} \
1490                 -command do_unstage_selection
1491         lappend disable_on_lock \
1492                 [list .mbar.commit entryconf [.mbar.commit index last] -state]
1493
1494         .mbar.commit add command -label {Revert Changes} \
1495                 -command do_revert_selection
1496         lappend disable_on_lock \
1497                 [list .mbar.commit entryconf [.mbar.commit index last] -state]
1498
1499         .mbar.commit add separator
1500
1501         .mbar.commit add command -label {Sign Off} \
1502                 -command do_signoff \
1503                 -accelerator $M1T-S
1504
1505         .mbar.commit add command -label Commit \
1506                 -command do_commit \
1507                 -accelerator $M1T-Return
1508         lappend disable_on_lock \
1509                 [list .mbar.commit entryconf [.mbar.commit index last] -state]
1510 }
1511
1512 # -- Merge Menu
1513 #
1514 if {[is_enabled branch]} {
1515         menu .mbar.merge
1516         .mbar.merge add command -label {Local Merge...} \
1517                 -command merge::dialog
1518         lappend disable_on_lock \
1519                 [list .mbar.merge entryconf [.mbar.merge index last] -state]
1520         .mbar.merge add command -label {Abort Merge...} \
1521                 -command merge::reset_hard
1522         lappend disable_on_lock \
1523                 [list .mbar.merge entryconf [.mbar.merge index last] -state]
1524
1525 }
1526
1527 # -- Transport Menu
1528 #
1529 if {[is_enabled transport]} {
1530         menu .mbar.fetch
1531
1532         menu .mbar.push
1533         .mbar.push add command -label {Push...} \
1534                 -command do_push_anywhere
1535         .mbar.push add command -label {Delete...} \
1536                 -command remote_branch_delete::dialog
1537 }
1538
1539 if {[is_MacOSX]} {
1540         # -- Apple Menu (Mac OS X only)
1541         #
1542         .mbar add cascade -label Apple -menu .mbar.apple
1543         menu .mbar.apple
1544
1545         .mbar.apple add command -label "About [appname]" \
1546                 -command do_about
1547         .mbar.apple add command -label "Options..." \
1548                 -command do_options
1549 } else {
1550         # -- Edit Menu
1551         #
1552         .mbar.edit add separator
1553         .mbar.edit add command -label {Options...} \
1554                 -command do_options
1555
1556         # -- Tools Menu
1557         #
1558         if {[file exists /usr/local/miga/lib/gui-miga]
1559                 && [file exists .pvcsrc]} {
1560         proc do_miga {} {
1561                 global ui_status_value
1562                 if {![lock_index update]} return
1563                 set cmd [list sh --login -c "/usr/local/miga/lib/gui-miga \"[pwd]\""]
1564                 set miga_fd [open "|$cmd" r]
1565                 fconfigure $miga_fd -blocking 0
1566                 fileevent $miga_fd readable [list miga_done $miga_fd]
1567                 set ui_status_value {Running miga...}
1568         }
1569         proc miga_done {fd} {
1570                 read $fd 512
1571                 if {[eof $fd]} {
1572                         close $fd
1573                         unlock_index
1574                         rescan [list set ui_status_value {Ready.}]
1575                 }
1576         }
1577         .mbar add cascade -label Tools -menu .mbar.tools
1578         menu .mbar.tools
1579         .mbar.tools add command -label "Migrate" \
1580                 -command do_miga
1581         lappend disable_on_lock \
1582                 [list .mbar.tools entryconf [.mbar.tools index last] -state]
1583         }
1584 }
1585
1586 # -- Help Menu
1587 #
1588 .mbar add cascade -label Help -menu .mbar.help
1589 menu .mbar.help
1590
1591 if {![is_MacOSX]} {
1592         .mbar.help add command -label "About [appname]" \
1593                 -command do_about
1594 }
1595
1596 set browser {}
1597 catch {set browser $repo_config(instaweb.browser)}
1598 set doc_path [file dirname [gitexec]]
1599 set doc_path [file join $doc_path Documentation index.html]
1600
1601 if {[is_Cygwin]} {
1602         set doc_path [exec cygpath --mixed $doc_path]
1603 }
1604
1605 if {$browser eq {}} {
1606         if {[is_MacOSX]} {
1607                 set browser open
1608         } elseif {[is_Cygwin]} {
1609                 set program_files [file dirname [exec cygpath --windir]]
1610                 set program_files [file join $program_files {Program Files}]
1611                 set firefox [file join $program_files {Mozilla Firefox} firefox.exe]
1612                 set ie [file join $program_files {Internet Explorer} IEXPLORE.EXE]
1613                 if {[file exists $firefox]} {
1614                         set browser $firefox
1615                 } elseif {[file exists $ie]} {
1616                         set browser $ie
1617                 }
1618                 unset program_files firefox ie
1619         }
1620 }
1621
1622 if {[file isfile $doc_path]} {
1623         set doc_url "file:$doc_path"
1624 } else {
1625         set doc_url {http://www.kernel.org/pub/software/scm/git/docs/}
1626 }
1627
1628 if {$browser ne {}} {
1629         .mbar.help add command -label {Online Documentation} \
1630                 -command [list exec $browser $doc_url &]
1631 }
1632 unset browser doc_path doc_url
1633
1634 # -- Standard bindings
1635 #
1636 bind .   <Destroy> do_quit
1637 bind all <$M1B-Key-q> do_quit
1638 bind all <$M1B-Key-Q> do_quit
1639 bind all <$M1B-Key-w> {destroy [winfo toplevel %W]}
1640 bind all <$M1B-Key-W> {destroy [winfo toplevel %W]}
1641
1642 set subcommand_args {}
1643 proc usage {} {
1644         puts stderr "usage: $::argv0 $::subcommand $::subcommand_args"
1645         exit 1
1646 }
1647
1648 # -- Not a normal commit type invocation?  Do that instead!
1649 #
1650 switch -- $subcommand {
1651 browser {
1652         set subcommand_args {rev?}
1653         switch [llength $argv] {
1654         0 {
1655                 set current_branch [git symbolic-ref HEAD]
1656                 regsub ^refs/((heads|tags|remotes)/)? \
1657                         $current_branch {} current_branch
1658         }
1659         1 {
1660                 set current_branch [lindex $argv 0]
1661         }
1662         default usage
1663         }
1664         browser::new $current_branch
1665         return
1666 }
1667 blame {
1668         set subcommand_args {rev? path?}
1669         set head {}
1670         set path {}
1671         set is_path 0
1672         foreach a $argv {
1673                 if {$is_path || [file exists $_prefix$a]} {
1674                         if {$path ne {}} usage
1675                         set path $_prefix$a
1676                         break
1677                 } elseif {$a eq {--}} {
1678                         if {$path ne {}} {
1679                                 if {$head ne {}} usage
1680                                 set head $path
1681                                 set path {}
1682                         }
1683                         set is_path 1
1684                 } elseif {$head eq {}} {
1685                         if {$head ne {}} usage
1686                         set head $a
1687                 } else {
1688                         usage
1689                 }
1690         }
1691         unset is_path
1692
1693         if {$head eq {}} {
1694                 set current_branch [git symbolic-ref HEAD]
1695                 regsub ^refs/((heads|tags|remotes)/)? \
1696                         $current_branch {} current_branch
1697         } else {
1698                 set current_branch $head
1699         }
1700
1701         if {$path eq {}} usage
1702         blame::new $head $path
1703         return
1704 }
1705 citool -
1706 gui {
1707         if {[llength $argv] != 0} {
1708                 puts -nonewline stderr "usage: $argv0"
1709                 if {$subcommand ne {gui} && [appname] ne "git-$subcommand"} {
1710                         puts -nonewline stderr " $subcommand"
1711                 }
1712                 puts stderr {}
1713                 exit 1
1714         }
1715         # fall through to setup UI for commits
1716 }
1717 default {
1718         puts stderr "usage: $argv0 \[{blame|browser|citool}\]"
1719         exit 1
1720 }
1721 }
1722
1723 # -- Branch Control
1724 #
1725 frame .branch \
1726         -borderwidth 1 \
1727         -relief sunken
1728 label .branch.l1 \
1729         -text {Current Branch:} \
1730         -anchor w \
1731         -justify left
1732 label .branch.cb \
1733         -textvariable current_branch \
1734         -anchor w \
1735         -justify left
1736 pack .branch.l1 -side left
1737 pack .branch.cb -side left -fill x
1738 pack .branch -side top -fill x
1739
1740 # -- Main Window Layout
1741 #
1742 panedwindow .vpane -orient vertical
1743 panedwindow .vpane.files -orient horizontal
1744 .vpane add .vpane.files -sticky nsew -height 100 -width 200
1745 pack .vpane -anchor n -side top -fill both -expand 1
1746
1747 # -- Index File List
1748 #
1749 frame .vpane.files.index -height 100 -width 200
1750 label .vpane.files.index.title -text {Staged Changes (Will Be Committed)} \
1751         -background green
1752 text $ui_index -background white -borderwidth 0 \
1753         -width 20 -height 10 \
1754         -wrap none \
1755         -cursor $cursor_ptr \
1756         -xscrollcommand {.vpane.files.index.sx set} \
1757         -yscrollcommand {.vpane.files.index.sy set} \
1758         -state disabled
1759 scrollbar .vpane.files.index.sx -orient h -command [list $ui_index xview]
1760 scrollbar .vpane.files.index.sy -orient v -command [list $ui_index yview]
1761 pack .vpane.files.index.title -side top -fill x
1762 pack .vpane.files.index.sx -side bottom -fill x
1763 pack .vpane.files.index.sy -side right -fill y
1764 pack $ui_index -side left -fill both -expand 1
1765 .vpane.files add .vpane.files.index -sticky nsew
1766
1767 # -- Working Directory File List
1768 #
1769 frame .vpane.files.workdir -height 100 -width 200
1770 label .vpane.files.workdir.title -text {Unstaged Changes (Will Not Be Committed)} \
1771         -background red
1772 text $ui_workdir -background white -borderwidth 0 \
1773         -width 20 -height 10 \
1774         -wrap none \
1775         -cursor $cursor_ptr \
1776         -xscrollcommand {.vpane.files.workdir.sx set} \
1777         -yscrollcommand {.vpane.files.workdir.sy set} \
1778         -state disabled
1779 scrollbar .vpane.files.workdir.sx -orient h -command [list $ui_workdir xview]
1780 scrollbar .vpane.files.workdir.sy -orient v -command [list $ui_workdir yview]
1781 pack .vpane.files.workdir.title -side top -fill x
1782 pack .vpane.files.workdir.sx -side bottom -fill x
1783 pack .vpane.files.workdir.sy -side right -fill y
1784 pack $ui_workdir -side left -fill both -expand 1
1785 .vpane.files add .vpane.files.workdir -sticky nsew
1786
1787 foreach i [list $ui_index $ui_workdir] {
1788         $i tag conf in_diff -font font_uibold
1789         $i tag conf in_sel \
1790                 -background [$i cget -foreground] \
1791                 -foreground [$i cget -background]
1792 }
1793 unset i
1794
1795 # -- Diff and Commit Area
1796 #
1797 frame .vpane.lower -height 300 -width 400
1798 frame .vpane.lower.commarea
1799 frame .vpane.lower.diff -relief sunken -borderwidth 1
1800 pack .vpane.lower.commarea -side top -fill x
1801 pack .vpane.lower.diff -side bottom -fill both -expand 1
1802 .vpane add .vpane.lower -sticky nsew
1803
1804 # -- Commit Area Buttons
1805 #
1806 frame .vpane.lower.commarea.buttons
1807 label .vpane.lower.commarea.buttons.l -text {} \
1808         -anchor w \
1809         -justify left
1810 pack .vpane.lower.commarea.buttons.l -side top -fill x
1811 pack .vpane.lower.commarea.buttons -side left -fill y
1812
1813 button .vpane.lower.commarea.buttons.rescan -text {Rescan} \
1814         -command do_rescan
1815 pack .vpane.lower.commarea.buttons.rescan -side top -fill x
1816 lappend disable_on_lock \
1817         {.vpane.lower.commarea.buttons.rescan conf -state}
1818
1819 button .vpane.lower.commarea.buttons.incall -text {Add Existing} \
1820         -command do_add_all
1821 pack .vpane.lower.commarea.buttons.incall -side top -fill x
1822 lappend disable_on_lock \
1823         {.vpane.lower.commarea.buttons.incall conf -state}
1824
1825 button .vpane.lower.commarea.buttons.signoff -text {Sign Off} \
1826         -command do_signoff
1827 pack .vpane.lower.commarea.buttons.signoff -side top -fill x
1828
1829 button .vpane.lower.commarea.buttons.commit -text {Commit} \
1830         -command do_commit
1831 pack .vpane.lower.commarea.buttons.commit -side top -fill x
1832 lappend disable_on_lock \
1833         {.vpane.lower.commarea.buttons.commit conf -state}
1834
1835 # -- Commit Message Buffer
1836 #
1837 frame .vpane.lower.commarea.buffer
1838 frame .vpane.lower.commarea.buffer.header
1839 set ui_comm .vpane.lower.commarea.buffer.t
1840 set ui_coml .vpane.lower.commarea.buffer.header.l
1841 radiobutton .vpane.lower.commarea.buffer.header.new \
1842         -text {New Commit} \
1843         -command do_select_commit_type \
1844         -variable selected_commit_type \
1845         -value new
1846 lappend disable_on_lock \
1847         [list .vpane.lower.commarea.buffer.header.new conf -state]
1848 radiobutton .vpane.lower.commarea.buffer.header.amend \
1849         -text {Amend Last Commit} \
1850         -command do_select_commit_type \
1851         -variable selected_commit_type \
1852         -value amend
1853 lappend disable_on_lock \
1854         [list .vpane.lower.commarea.buffer.header.amend conf -state]
1855 label $ui_coml \
1856         -anchor w \
1857         -justify left
1858 proc trace_commit_type {varname args} {
1859         global ui_coml commit_type
1860         switch -glob -- $commit_type {
1861         initial       {set txt {Initial Commit Message:}}
1862         amend         {set txt {Amended Commit Message:}}
1863         amend-initial {set txt {Amended Initial Commit Message:}}
1864         amend-merge   {set txt {Amended Merge Commit Message:}}
1865         merge         {set txt {Merge Commit Message:}}
1866         *             {set txt {Commit Message:}}
1867         }
1868         $ui_coml conf -text $txt
1869 }
1870 trace add variable commit_type write trace_commit_type
1871 pack $ui_coml -side left -fill x
1872 pack .vpane.lower.commarea.buffer.header.amend -side right
1873 pack .vpane.lower.commarea.buffer.header.new -side right
1874
1875 text $ui_comm -background white -borderwidth 1 \
1876         -undo true \
1877         -maxundo 20 \
1878         -autoseparators true \
1879         -relief sunken \
1880         -width 75 -height 9 -wrap none \
1881         -font font_diff \
1882         -yscrollcommand {.vpane.lower.commarea.buffer.sby set}
1883 scrollbar .vpane.lower.commarea.buffer.sby \
1884         -command [list $ui_comm yview]
1885 pack .vpane.lower.commarea.buffer.header -side top -fill x
1886 pack .vpane.lower.commarea.buffer.sby -side right -fill y
1887 pack $ui_comm -side left -fill y
1888 pack .vpane.lower.commarea.buffer -side left -fill y
1889
1890 # -- Commit Message Buffer Context Menu
1891 #
1892 set ctxm .vpane.lower.commarea.buffer.ctxm
1893 menu $ctxm -tearoff 0
1894 $ctxm add command \
1895         -label {Cut} \
1896         -command {tk_textCut $ui_comm}
1897 $ctxm add command \
1898         -label {Copy} \
1899         -command {tk_textCopy $ui_comm}
1900 $ctxm add command \
1901         -label {Paste} \
1902         -command {tk_textPaste $ui_comm}
1903 $ctxm add command \
1904         -label {Delete} \
1905         -command {$ui_comm delete sel.first sel.last}
1906 $ctxm add separator
1907 $ctxm add command \
1908         -label {Select All} \
1909         -command {focus $ui_comm;$ui_comm tag add sel 0.0 end}
1910 $ctxm add command \
1911         -label {Copy All} \
1912         -command {
1913                 $ui_comm tag add sel 0.0 end
1914                 tk_textCopy $ui_comm
1915                 $ui_comm tag remove sel 0.0 end
1916         }
1917 $ctxm add separator
1918 $ctxm add command \
1919         -label {Sign Off} \
1920         -command do_signoff
1921 bind_button3 $ui_comm "tk_popup $ctxm %X %Y"
1922
1923 # -- Diff Header
1924 #
1925 proc trace_current_diff_path {varname args} {
1926         global current_diff_path diff_actions file_states
1927         if {$current_diff_path eq {}} {
1928                 set s {}
1929                 set f {}
1930                 set p {}
1931                 set o disabled
1932         } else {
1933                 set p $current_diff_path
1934                 set s [mapdesc [lindex $file_states($p) 0] $p]
1935                 set f {File:}
1936                 set p [escape_path $p]
1937                 set o normal
1938         }
1939
1940         .vpane.lower.diff.header.status configure -text $s
1941         .vpane.lower.diff.header.file configure -text $f
1942         .vpane.lower.diff.header.path configure -text $p
1943         foreach w $diff_actions {
1944                 uplevel #0 $w $o
1945         }
1946 }
1947 trace add variable current_diff_path write trace_current_diff_path
1948
1949 frame .vpane.lower.diff.header -background orange
1950 label .vpane.lower.diff.header.status \
1951         -background orange \
1952         -width $max_status_desc \
1953         -anchor w \
1954         -justify left
1955 label .vpane.lower.diff.header.file \
1956         -background orange \
1957         -anchor w \
1958         -justify left
1959 label .vpane.lower.diff.header.path \
1960         -background orange \
1961         -anchor w \
1962         -justify left
1963 pack .vpane.lower.diff.header.status -side left
1964 pack .vpane.lower.diff.header.file -side left
1965 pack .vpane.lower.diff.header.path -fill x
1966 set ctxm .vpane.lower.diff.header.ctxm
1967 menu $ctxm -tearoff 0
1968 $ctxm add command \
1969         -label {Copy} \
1970         -command {
1971                 clipboard clear
1972                 clipboard append \
1973                         -format STRING \
1974                         -type STRING \
1975                         -- $current_diff_path
1976         }
1977 lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
1978 bind_button3 .vpane.lower.diff.header.path "tk_popup $ctxm %X %Y"
1979
1980 # -- Diff Body
1981 #
1982 frame .vpane.lower.diff.body
1983 set ui_diff .vpane.lower.diff.body.t
1984 text $ui_diff -background white -borderwidth 0 \
1985         -width 80 -height 15 -wrap none \
1986         -font font_diff \
1987         -xscrollcommand {.vpane.lower.diff.body.sbx set} \
1988         -yscrollcommand {.vpane.lower.diff.body.sby set} \
1989         -state disabled
1990 scrollbar .vpane.lower.diff.body.sbx -orient horizontal \
1991         -command [list $ui_diff xview]
1992 scrollbar .vpane.lower.diff.body.sby -orient vertical \
1993         -command [list $ui_diff yview]
1994 pack .vpane.lower.diff.body.sbx -side bottom -fill x
1995 pack .vpane.lower.diff.body.sby -side right -fill y
1996 pack $ui_diff -side left -fill both -expand 1
1997 pack .vpane.lower.diff.header -side top -fill x
1998 pack .vpane.lower.diff.body -side bottom -fill both -expand 1
1999
2000 $ui_diff tag conf d_cr -elide true
2001 $ui_diff tag conf d_@ -foreground blue -font font_diffbold
2002 $ui_diff tag conf d_+ -foreground {#00a000}
2003 $ui_diff tag conf d_- -foreground red
2004
2005 $ui_diff tag conf d_++ -foreground {#00a000}
2006 $ui_diff tag conf d_-- -foreground red
2007 $ui_diff tag conf d_+s \
2008         -foreground {#00a000} \
2009         -background {#e2effa}
2010 $ui_diff tag conf d_-s \
2011         -foreground red \
2012         -background {#e2effa}
2013 $ui_diff tag conf d_s+ \
2014         -foreground {#00a000} \
2015         -background ivory1
2016 $ui_diff tag conf d_s- \
2017         -foreground red \
2018         -background ivory1
2019
2020 $ui_diff tag conf d<<<<<<< \
2021         -foreground orange \
2022         -font font_diffbold
2023 $ui_diff tag conf d======= \
2024         -foreground orange \
2025         -font font_diffbold
2026 $ui_diff tag conf d>>>>>>> \
2027         -foreground orange \
2028         -font font_diffbold
2029
2030 $ui_diff tag raise sel
2031
2032 # -- Diff Body Context Menu
2033 #
2034 set ctxm .vpane.lower.diff.body.ctxm
2035 menu $ctxm -tearoff 0
2036 $ctxm add command \
2037         -label {Refresh} \
2038         -command reshow_diff
2039 lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
2040 $ctxm add command \
2041         -label {Copy} \
2042         -command {tk_textCopy $ui_diff}
2043 lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
2044 $ctxm add command \
2045         -label {Select All} \
2046         -command {focus $ui_diff;$ui_diff tag add sel 0.0 end}
2047 lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
2048 $ctxm add command \
2049         -label {Copy All} \
2050         -command {
2051                 $ui_diff tag add sel 0.0 end
2052                 tk_textCopy $ui_diff
2053                 $ui_diff tag remove sel 0.0 end
2054         }
2055 lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
2056 $ctxm add separator
2057 $ctxm add command \
2058         -label {Apply/Reverse Hunk} \
2059         -command {apply_hunk $cursorX $cursorY}
2060 set ui_diff_applyhunk [$ctxm index last]
2061 lappend diff_actions [list $ctxm entryconf $ui_diff_applyhunk -state]
2062 $ctxm add separator
2063 $ctxm add command \
2064         -label {Decrease Font Size} \
2065         -command {incr_font_size font_diff -1}
2066 lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
2067 $ctxm add command \
2068         -label {Increase Font Size} \
2069         -command {incr_font_size font_diff 1}
2070 lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
2071 $ctxm add separator
2072 $ctxm add command \
2073         -label {Show Less Context} \
2074         -command {if {$repo_config(gui.diffcontext) >= 2} {
2075                 incr repo_config(gui.diffcontext) -1
2076                 reshow_diff
2077         }}
2078 lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
2079 $ctxm add command \
2080         -label {Show More Context} \
2081         -command {
2082                 incr repo_config(gui.diffcontext)
2083                 reshow_diff
2084         }
2085 lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
2086 $ctxm add separator
2087 $ctxm add command -label {Options...} \
2088         -command do_options
2089 bind_button3 $ui_diff "
2090         set cursorX %x
2091         set cursorY %y
2092         if {\$ui_index eq \$current_diff_side} {
2093                 $ctxm entryconf $ui_diff_applyhunk -label {Unstage Hunk From Commit}
2094         } else {
2095                 $ctxm entryconf $ui_diff_applyhunk -label {Stage Hunk For Commit}
2096         }
2097         tk_popup $ctxm %X %Y
2098 "
2099 unset ui_diff_applyhunk
2100
2101 # -- Status Bar
2102 #
2103 label .status -textvariable ui_status_value \
2104         -anchor w \
2105         -justify left \
2106         -borderwidth 1 \
2107         -relief sunken
2108 pack .status -anchor w -side bottom -fill x
2109
2110 # -- Load geometry
2111 #
2112 catch {
2113 set gm $repo_config(gui.geometry)
2114 wm geometry . [lindex $gm 0]
2115 .vpane sash place 0 \
2116         [lindex [.vpane sash coord 0] 0] \
2117         [lindex $gm 1]
2118 .vpane.files sash place 0 \
2119         [lindex $gm 2] \
2120         [lindex [.vpane.files sash coord 0] 1]
2121 unset gm
2122 }
2123
2124 # -- Key Bindings
2125 #
2126 bind $ui_comm <$M1B-Key-Return> {do_commit;break}
2127 bind $ui_comm <$M1B-Key-i> {do_add_all;break}
2128 bind $ui_comm <$M1B-Key-I> {do_add_all;break}
2129 bind $ui_comm <$M1B-Key-x> {tk_textCut %W;break}
2130 bind $ui_comm <$M1B-Key-X> {tk_textCut %W;break}
2131 bind $ui_comm <$M1B-Key-c> {tk_textCopy %W;break}
2132 bind $ui_comm <$M1B-Key-C> {tk_textCopy %W;break}
2133 bind $ui_comm <$M1B-Key-v> {tk_textPaste %W; %W see insert; break}
2134 bind $ui_comm <$M1B-Key-V> {tk_textPaste %W; %W see insert; break}
2135 bind $ui_comm <$M1B-Key-a> {%W tag add sel 0.0 end;break}
2136 bind $ui_comm <$M1B-Key-A> {%W tag add sel 0.0 end;break}
2137
2138 bind $ui_diff <$M1B-Key-x> {tk_textCopy %W;break}
2139 bind $ui_diff <$M1B-Key-X> {tk_textCopy %W;break}
2140 bind $ui_diff <$M1B-Key-c> {tk_textCopy %W;break}
2141 bind $ui_diff <$M1B-Key-C> {tk_textCopy %W;break}
2142 bind $ui_diff <$M1B-Key-v> {break}
2143 bind $ui_diff <$M1B-Key-V> {break}
2144 bind $ui_diff <$M1B-Key-a> {%W tag add sel 0.0 end;break}
2145 bind $ui_diff <$M1B-Key-A> {%W tag add sel 0.0 end;break}
2146 bind $ui_diff <Key-Up>     {catch {%W yview scroll -1 units};break}
2147 bind $ui_diff <Key-Down>   {catch {%W yview scroll  1 units};break}
2148 bind $ui_diff <Key-Left>   {catch {%W xview scroll -1 units};break}
2149 bind $ui_diff <Key-Right>  {catch {%W xview scroll  1 units};break}
2150 bind $ui_diff <Key-k>         {catch {%W yview scroll -1 units};break}
2151 bind $ui_diff <Key-j>         {catch {%W yview scroll  1 units};break}
2152 bind $ui_diff <Key-h>         {catch {%W xview scroll -1 units};break}
2153 bind $ui_diff <Key-l>         {catch {%W xview scroll  1 units};break}
2154 bind $ui_diff <Control-Key-b> {catch {%W yview scroll -1 pages};break}
2155 bind $ui_diff <Control-Key-f> {catch {%W yview scroll  1 pages};break}
2156 bind $ui_diff <Button-1>   {focus %W}
2157
2158 if {[is_enabled branch]} {
2159         bind . <$M1B-Key-n> do_create_branch
2160         bind . <$M1B-Key-N> do_create_branch
2161 }
2162
2163 bind all <Key-F5> do_rescan
2164 bind all <$M1B-Key-r> do_rescan
2165 bind all <$M1B-Key-R> do_rescan
2166 bind .   <$M1B-Key-s> do_signoff
2167 bind .   <$M1B-Key-S> do_signoff
2168 bind .   <$M1B-Key-i> do_add_all
2169 bind .   <$M1B-Key-I> do_add_all
2170 bind .   <$M1B-Key-Return> do_commit
2171 foreach i [list $ui_index $ui_workdir] {
2172         bind $i <Button-1>       "toggle_or_diff         $i %x %y; break"
2173         bind $i <$M1B-Button-1>  "add_one_to_selection   $i %x %y; break"
2174         bind $i <Shift-Button-1> "add_range_to_selection $i %x %y; break"
2175 }
2176 unset i
2177
2178 set file_lists($ui_index) [list]
2179 set file_lists($ui_workdir) [list]
2180
2181 wm title . "[appname] ([reponame]) [file normalize [file dirname [gitdir]]]"
2182 focus -force $ui_comm
2183
2184 # -- Warn the user about environmental problems.  Cygwin's Tcl
2185 #    does *not* pass its env array onto any processes it spawns.
2186 #    This means that git processes get none of our environment.
2187 #
2188 if {[is_Cygwin]} {
2189         set ignored_env 0
2190         set suggest_user {}
2191         set msg "Possible environment issues exist.
2192
2193 The following environment variables are probably
2194 going to be ignored by any Git subprocess run
2195 by [appname]:
2196
2197 "
2198         foreach name [array names env] {
2199                 switch -regexp -- $name {
2200                 {^GIT_INDEX_FILE$} -
2201                 {^GIT_OBJECT_DIRECTORY$} -
2202                 {^GIT_ALTERNATE_OBJECT_DIRECTORIES$} -
2203                 {^GIT_DIFF_OPTS$} -
2204                 {^GIT_EXTERNAL_DIFF$} -
2205                 {^GIT_PAGER$} -
2206                 {^GIT_TRACE$} -
2207                 {^GIT_CONFIG$} -
2208                 {^GIT_CONFIG_LOCAL$} -
2209                 {^GIT_(AUTHOR|COMMITTER)_DATE$} {
2210                         append msg " - $name\n"
2211                         incr ignored_env
2212                 }
2213                 {^GIT_(AUTHOR|COMMITTER)_(NAME|EMAIL)$} {
2214                         append msg " - $name\n"
2215                         incr ignored_env
2216                         set suggest_user $name
2217                 }
2218                 }
2219         }
2220         if {$ignored_env > 0} {
2221                 append msg "
2222 This is due to a known issue with the
2223 Tcl binary distributed by Cygwin."
2224
2225                 if {$suggest_user ne {}} {
2226                         append msg "
2227
2228 A good replacement for $suggest_user
2229 is placing values for the user.name and
2230 user.email settings into your personal
2231 ~/.gitconfig file.
2232 "
2233                 }
2234                 warn_popup $msg
2235         }
2236         unset ignored_env msg suggest_user name
2237 }
2238
2239 # -- Only initialize complex UI if we are going to stay running.
2240 #
2241 if {[is_enabled transport]} {
2242         load_all_remotes
2243         load_all_heads
2244
2245         populate_branch_menu
2246         populate_fetch_menu
2247         populate_push_menu
2248 }
2249
2250 # -- Only suggest a gc run if we are going to stay running.
2251 #
2252 if {[is_enabled multicommit]} {
2253         set object_limit 2000
2254         if {[is_Windows]} {set object_limit 200}
2255         regexp {^([0-9]+) objects,} [git count-objects] _junk objects_current
2256         if {$objects_current >= $object_limit} {
2257                 if {[ask_popup \
2258                         "This repository currently has $objects_current loose objects.
2259
2260 To maintain optimal performance it is strongly recommended that you compress the database when more than $object_limit loose objects exist.
2261
2262 Compress the database now?"] eq yes} {
2263                         do_gc
2264                 }
2265         }
2266         unset object_limit _junk objects_current
2267 }
2268
2269 lock_index begin-read
2270 after 1 do_rescan