]> asedeno.scripts.mit.edu Git - git.git/blob - git-gui.sh
git-gui: Allow users to delete remote branches
[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.summary) false
1266 set default_config(merge.verbosity) 2
1267 set default_config(user.name) {}
1268 set default_config(user.email) {}
1269
1270 set default_config(gui.pruneduringfetch) false
1271 set default_config(gui.trustmtime) false
1272 set default_config(gui.diffcontext) 5
1273 set default_config(gui.newbranchtemplate) {}
1274 set default_config(gui.fontui) [font configure font_ui]
1275 set default_config(gui.fontdiff) [font configure font_diff]
1276 set font_descs {
1277         {fontui   font_ui   {Main Font}}
1278         {fontdiff font_diff {Diff/Console Font}}
1279 }
1280 load_config 0
1281 apply_config
1282
1283 ######################################################################
1284 ##
1285 ## feature option selection
1286
1287 if {[regexp {^git-(.+)$} [appname] _junk subcommand]} {
1288         unset _junk
1289 } else {
1290         set subcommand gui
1291 }
1292 if {$subcommand eq {gui.sh}} {
1293         set subcommand gui
1294 }
1295 if {$subcommand eq {gui} && [llength $argv] > 0} {
1296         set subcommand [lindex $argv 0]
1297         set argv [lrange $argv 1 end]
1298 }
1299
1300 enable_option multicommit
1301 enable_option branch
1302 enable_option transport
1303
1304 switch -- $subcommand {
1305 browser -
1306 blame {
1307         disable_option multicommit
1308         disable_option branch
1309         disable_option transport
1310 }
1311 citool {
1312         enable_option singlecommit
1313
1314         disable_option multicommit
1315         disable_option branch
1316         disable_option transport
1317 }
1318 }
1319
1320 ######################################################################
1321 ##
1322 ## ui construction
1323
1324 set ui_comm {}
1325
1326 # -- Menu Bar
1327 #
1328 menu .mbar -tearoff 0
1329 .mbar add cascade -label Repository -menu .mbar.repository
1330 .mbar add cascade -label Edit -menu .mbar.edit
1331 if {[is_enabled branch]} {
1332         .mbar add cascade -label Branch -menu .mbar.branch
1333 }
1334 if {[is_enabled multicommit] || [is_enabled singlecommit]} {
1335         .mbar add cascade -label Commit -menu .mbar.commit
1336 }
1337 if {[is_enabled transport]} {
1338         .mbar add cascade -label Merge -menu .mbar.merge
1339         .mbar add cascade -label Fetch -menu .mbar.fetch
1340         .mbar add cascade -label Push -menu .mbar.push
1341 }
1342 . configure -menu .mbar
1343
1344 # -- Repository Menu
1345 #
1346 menu .mbar.repository
1347
1348 .mbar.repository add command \
1349         -label {Browse Current Branch} \
1350         -command {browser::new $current_branch}
1351 trace add variable current_branch write ".mbar.repository entryconf [.mbar.repository index last] -label \"Browse \$current_branch\" ;#"
1352 .mbar.repository add separator
1353
1354 .mbar.repository add command \
1355         -label {Visualize Current Branch} \
1356         -command {do_gitk $current_branch}
1357 trace add variable current_branch write ".mbar.repository entryconf [.mbar.repository index last] -label \"Visualize \$current_branch\" ;#"
1358 .mbar.repository add command \
1359         -label {Visualize All Branches} \
1360         -command {do_gitk --all}
1361 .mbar.repository add separator
1362
1363 if {[is_enabled multicommit]} {
1364         .mbar.repository add command -label {Database Statistics} \
1365                 -command do_stats
1366
1367         .mbar.repository add command -label {Compress Database} \
1368                 -command do_gc
1369
1370         .mbar.repository add command -label {Verify Database} \
1371                 -command do_fsck_objects
1372
1373         .mbar.repository add separator
1374
1375         if {[is_Cygwin]} {
1376                 .mbar.repository add command \
1377                         -label {Create Desktop Icon} \
1378                         -command do_cygwin_shortcut
1379         } elseif {[is_Windows]} {
1380                 .mbar.repository add command \
1381                         -label {Create Desktop Icon} \
1382                         -command do_windows_shortcut
1383         } elseif {[is_MacOSX]} {
1384                 .mbar.repository add command \
1385                         -label {Create Desktop Icon} \
1386                         -command do_macosx_app
1387         }
1388 }
1389
1390 .mbar.repository add command -label Quit \
1391         -command do_quit \
1392         -accelerator $M1T-Q
1393
1394 # -- Edit Menu
1395 #
1396 menu .mbar.edit
1397 .mbar.edit add command -label Undo \
1398         -command {catch {[focus] edit undo}} \
1399         -accelerator $M1T-Z
1400 .mbar.edit add command -label Redo \
1401         -command {catch {[focus] edit redo}} \
1402         -accelerator $M1T-Y
1403 .mbar.edit add separator
1404 .mbar.edit add command -label Cut \
1405         -command {catch {tk_textCut [focus]}} \
1406         -accelerator $M1T-X
1407 .mbar.edit add command -label Copy \
1408         -command {catch {tk_textCopy [focus]}} \
1409         -accelerator $M1T-C
1410 .mbar.edit add command -label Paste \
1411         -command {catch {tk_textPaste [focus]; [focus] see insert}} \
1412         -accelerator $M1T-V
1413 .mbar.edit add command -label Delete \
1414         -command {catch {[focus] delete sel.first sel.last}} \
1415         -accelerator Del
1416 .mbar.edit add separator
1417 .mbar.edit add command -label {Select All} \
1418         -command {catch {[focus] tag add sel 0.0 end}} \
1419         -accelerator $M1T-A
1420
1421 # -- Branch Menu
1422 #
1423 if {[is_enabled branch]} {
1424         menu .mbar.branch
1425
1426         .mbar.branch add command -label {Create...} \
1427                 -command do_create_branch \
1428                 -accelerator $M1T-N
1429         lappend disable_on_lock [list .mbar.branch entryconf \
1430                 [.mbar.branch index last] -state]
1431
1432         .mbar.branch add command -label {Rename...} \
1433                 -command branch_rename::dialog
1434         lappend disable_on_lock [list .mbar.branch entryconf \
1435                 [.mbar.branch index last] -state]
1436
1437         .mbar.branch add command -label {Delete...} \
1438                 -command do_delete_branch
1439         lappend disable_on_lock [list .mbar.branch entryconf \
1440                 [.mbar.branch index last] -state]
1441
1442         .mbar.branch add command -label {Reset...} \
1443                 -command merge::reset_hard
1444         lappend disable_on_lock [list .mbar.branch entryconf \
1445                 [.mbar.branch index last] -state]
1446 }
1447
1448 # -- Commit Menu
1449 #
1450 if {[is_enabled multicommit] || [is_enabled singlecommit]} {
1451         menu .mbar.commit
1452
1453         .mbar.commit add radiobutton \
1454                 -label {New Commit} \
1455                 -command do_select_commit_type \
1456                 -variable selected_commit_type \
1457                 -value new
1458         lappend disable_on_lock \
1459                 [list .mbar.commit entryconf [.mbar.commit index last] -state]
1460
1461         .mbar.commit add radiobutton \
1462                 -label {Amend Last Commit} \
1463                 -command do_select_commit_type \
1464                 -variable selected_commit_type \
1465                 -value amend
1466         lappend disable_on_lock \
1467                 [list .mbar.commit entryconf [.mbar.commit index last] -state]
1468
1469         .mbar.commit add separator
1470
1471         .mbar.commit add command -label Rescan \
1472                 -command do_rescan \
1473                 -accelerator F5
1474         lappend disable_on_lock \
1475                 [list .mbar.commit entryconf [.mbar.commit index last] -state]
1476
1477         .mbar.commit add command -label {Add To Commit} \
1478                 -command do_add_selection
1479         lappend disable_on_lock \
1480                 [list .mbar.commit entryconf [.mbar.commit index last] -state]
1481
1482         .mbar.commit add command -label {Add Existing To Commit} \
1483                 -command do_add_all \
1484                 -accelerator $M1T-I
1485         lappend disable_on_lock \
1486                 [list .mbar.commit entryconf [.mbar.commit index last] -state]
1487
1488         .mbar.commit add command -label {Unstage From Commit} \
1489                 -command do_unstage_selection
1490         lappend disable_on_lock \
1491                 [list .mbar.commit entryconf [.mbar.commit index last] -state]
1492
1493         .mbar.commit add command -label {Revert Changes} \
1494                 -command do_revert_selection
1495         lappend disable_on_lock \
1496                 [list .mbar.commit entryconf [.mbar.commit index last] -state]
1497
1498         .mbar.commit add separator
1499
1500         .mbar.commit add command -label {Sign Off} \
1501                 -command do_signoff \
1502                 -accelerator $M1T-S
1503
1504         .mbar.commit add command -label Commit \
1505                 -command do_commit \
1506                 -accelerator $M1T-Return
1507         lappend disable_on_lock \
1508                 [list .mbar.commit entryconf [.mbar.commit index last] -state]
1509 }
1510
1511 # -- Merge Menu
1512 #
1513 if {[is_enabled branch]} {
1514         menu .mbar.merge
1515         .mbar.merge add command -label {Local Merge...} \
1516                 -command merge::dialog
1517         lappend disable_on_lock \
1518                 [list .mbar.merge entryconf [.mbar.merge index last] -state]
1519         .mbar.merge add command -label {Abort Merge...} \
1520                 -command merge::reset_hard
1521         lappend disable_on_lock \
1522                 [list .mbar.merge entryconf [.mbar.merge index last] -state]
1523
1524 }
1525
1526 # -- Transport Menu
1527 #
1528 if {[is_enabled transport]} {
1529         menu .mbar.fetch
1530
1531         menu .mbar.push
1532         .mbar.push add command -label {Push...} \
1533                 -command do_push_anywhere
1534         .mbar.push add command -label {Delete...} \
1535                 -command remote_branch_delete::dialog
1536 }
1537
1538 if {[is_MacOSX]} {
1539         # -- Apple Menu (Mac OS X only)
1540         #
1541         .mbar add cascade -label Apple -menu .mbar.apple
1542         menu .mbar.apple
1543
1544         .mbar.apple add command -label "About [appname]" \
1545                 -command do_about
1546         .mbar.apple add command -label "Options..." \
1547                 -command do_options
1548 } else {
1549         # -- Edit Menu
1550         #
1551         .mbar.edit add separator
1552         .mbar.edit add command -label {Options...} \
1553                 -command do_options
1554
1555         # -- Tools Menu
1556         #
1557         if {[file exists /usr/local/miga/lib/gui-miga]
1558                 && [file exists .pvcsrc]} {
1559         proc do_miga {} {
1560                 global ui_status_value
1561                 if {![lock_index update]} return
1562                 set cmd [list sh --login -c "/usr/local/miga/lib/gui-miga \"[pwd]\""]
1563                 set miga_fd [open "|$cmd" r]
1564                 fconfigure $miga_fd -blocking 0
1565                 fileevent $miga_fd readable [list miga_done $miga_fd]
1566                 set ui_status_value {Running miga...}
1567         }
1568         proc miga_done {fd} {
1569                 read $fd 512
1570                 if {[eof $fd]} {
1571                         close $fd
1572                         unlock_index
1573                         rescan [list set ui_status_value {Ready.}]
1574                 }
1575         }
1576         .mbar add cascade -label Tools -menu .mbar.tools
1577         menu .mbar.tools
1578         .mbar.tools add command -label "Migrate" \
1579                 -command do_miga
1580         lappend disable_on_lock \
1581                 [list .mbar.tools entryconf [.mbar.tools index last] -state]
1582         }
1583 }
1584
1585 # -- Help Menu
1586 #
1587 .mbar add cascade -label Help -menu .mbar.help
1588 menu .mbar.help
1589
1590 if {![is_MacOSX]} {
1591         .mbar.help add command -label "About [appname]" \
1592                 -command do_about
1593 }
1594
1595 set browser {}
1596 catch {set browser $repo_config(instaweb.browser)}
1597 set doc_path [file dirname [gitexec]]
1598 set doc_path [file join $doc_path Documentation index.html]
1599
1600 if {[is_Cygwin]} {
1601         set doc_path [exec cygpath --mixed $doc_path]
1602 }
1603
1604 if {$browser eq {}} {
1605         if {[is_MacOSX]} {
1606                 set browser open
1607         } elseif {[is_Cygwin]} {
1608                 set program_files [file dirname [exec cygpath --windir]]
1609                 set program_files [file join $program_files {Program Files}]
1610                 set firefox [file join $program_files {Mozilla Firefox} firefox.exe]
1611                 set ie [file join $program_files {Internet Explorer} IEXPLORE.EXE]
1612                 if {[file exists $firefox]} {
1613                         set browser $firefox
1614                 } elseif {[file exists $ie]} {
1615                         set browser $ie
1616                 }
1617                 unset program_files firefox ie
1618         }
1619 }
1620
1621 if {[file isfile $doc_path]} {
1622         set doc_url "file:$doc_path"
1623 } else {
1624         set doc_url {http://www.kernel.org/pub/software/scm/git/docs/}
1625 }
1626
1627 if {$browser ne {}} {
1628         .mbar.help add command -label {Online Documentation} \
1629                 -command [list exec $browser $doc_url &]
1630 }
1631 unset browser doc_path doc_url
1632
1633 # -- Standard bindings
1634 #
1635 bind .   <Destroy> do_quit
1636 bind all <$M1B-Key-q> do_quit
1637 bind all <$M1B-Key-Q> do_quit
1638 bind all <$M1B-Key-w> {destroy [winfo toplevel %W]}
1639 bind all <$M1B-Key-W> {destroy [winfo toplevel %W]}
1640
1641 set subcommand_args {}
1642 proc usage {} {
1643         puts stderr "usage: $::argv0 $::subcommand $::subcommand_args"
1644         exit 1
1645 }
1646
1647 # -- Not a normal commit type invocation?  Do that instead!
1648 #
1649 switch -- $subcommand {
1650 browser {
1651         set subcommand_args {rev?}
1652         switch [llength $argv] {
1653         0 {
1654                 set current_branch [git symbolic-ref HEAD]
1655                 regsub ^refs/((heads|tags|remotes)/)? \
1656                         $current_branch {} current_branch
1657         }
1658         1 {
1659                 set current_branch [lindex $argv 0]
1660         }
1661         default usage
1662         }
1663         browser::new $current_branch
1664         return
1665 }
1666 blame {
1667         set subcommand_args {rev? path?}
1668         set head {}
1669         set path {}
1670         set is_path 0
1671         foreach a $argv {
1672                 if {$is_path || [file exists $_prefix$a]} {
1673                         if {$path ne {}} usage
1674                         set path $_prefix$a
1675                         break
1676                 } elseif {$a eq {--}} {
1677                         if {$path ne {}} {
1678                                 if {$head ne {}} usage
1679                                 set head $path
1680                                 set path {}
1681                         }
1682                         set is_path 1
1683                 } elseif {$head eq {}} {
1684                         if {$head ne {}} usage
1685                         set head $a
1686                 } else {
1687                         usage
1688                 }
1689         }
1690         unset is_path
1691
1692         if {$head eq {}} {
1693                 set current_branch [git symbolic-ref HEAD]
1694                 regsub ^refs/((heads|tags|remotes)/)? \
1695                         $current_branch {} current_branch
1696         } else {
1697                 set current_branch $head
1698         }
1699
1700         if {$path eq {}} usage
1701         blame::new $head $path
1702         return
1703 }
1704 citool -
1705 gui {
1706         if {[llength $argv] != 0} {
1707                 puts -nonewline stderr "usage: $argv0"
1708                 if {$subcommand ne {gui} && [appname] ne "git-$subcommand"} {
1709                         puts -nonewline stderr " $subcommand"
1710                 }
1711                 puts stderr {}
1712                 exit 1
1713         }
1714         # fall through to setup UI for commits
1715 }
1716 default {
1717         puts stderr "usage: $argv0 \[{blame|browser|citool}\]"
1718         exit 1
1719 }
1720 }
1721
1722 # -- Branch Control
1723 #
1724 frame .branch \
1725         -borderwidth 1 \
1726         -relief sunken
1727 label .branch.l1 \
1728         -text {Current Branch:} \
1729         -anchor w \
1730         -justify left
1731 label .branch.cb \
1732         -textvariable current_branch \
1733         -anchor w \
1734         -justify left
1735 pack .branch.l1 -side left
1736 pack .branch.cb -side left -fill x
1737 pack .branch -side top -fill x
1738
1739 # -- Main Window Layout
1740 #
1741 panedwindow .vpane -orient vertical
1742 panedwindow .vpane.files -orient horizontal
1743 .vpane add .vpane.files -sticky nsew -height 100 -width 200
1744 pack .vpane -anchor n -side top -fill both -expand 1
1745
1746 # -- Index File List
1747 #
1748 frame .vpane.files.index -height 100 -width 200
1749 label .vpane.files.index.title -text {Staged Changes (Will Be Committed)} \
1750         -background green
1751 text $ui_index -background white -borderwidth 0 \
1752         -width 20 -height 10 \
1753         -wrap none \
1754         -cursor $cursor_ptr \
1755         -xscrollcommand {.vpane.files.index.sx set} \
1756         -yscrollcommand {.vpane.files.index.sy set} \
1757         -state disabled
1758 scrollbar .vpane.files.index.sx -orient h -command [list $ui_index xview]
1759 scrollbar .vpane.files.index.sy -orient v -command [list $ui_index yview]
1760 pack .vpane.files.index.title -side top -fill x
1761 pack .vpane.files.index.sx -side bottom -fill x
1762 pack .vpane.files.index.sy -side right -fill y
1763 pack $ui_index -side left -fill both -expand 1
1764 .vpane.files add .vpane.files.index -sticky nsew
1765
1766 # -- Working Directory File List
1767 #
1768 frame .vpane.files.workdir -height 100 -width 200
1769 label .vpane.files.workdir.title -text {Unstaged Changes (Will Not Be Committed)} \
1770         -background red
1771 text $ui_workdir -background white -borderwidth 0 \
1772         -width 20 -height 10 \
1773         -wrap none \
1774         -cursor $cursor_ptr \
1775         -xscrollcommand {.vpane.files.workdir.sx set} \
1776         -yscrollcommand {.vpane.files.workdir.sy set} \
1777         -state disabled
1778 scrollbar .vpane.files.workdir.sx -orient h -command [list $ui_workdir xview]
1779 scrollbar .vpane.files.workdir.sy -orient v -command [list $ui_workdir yview]
1780 pack .vpane.files.workdir.title -side top -fill x
1781 pack .vpane.files.workdir.sx -side bottom -fill x
1782 pack .vpane.files.workdir.sy -side right -fill y
1783 pack $ui_workdir -side left -fill both -expand 1
1784 .vpane.files add .vpane.files.workdir -sticky nsew
1785
1786 foreach i [list $ui_index $ui_workdir] {
1787         $i tag conf in_diff -font font_uibold
1788         $i tag conf in_sel \
1789                 -background [$i cget -foreground] \
1790                 -foreground [$i cget -background]
1791 }
1792 unset i
1793
1794 # -- Diff and Commit Area
1795 #
1796 frame .vpane.lower -height 300 -width 400
1797 frame .vpane.lower.commarea
1798 frame .vpane.lower.diff -relief sunken -borderwidth 1
1799 pack .vpane.lower.commarea -side top -fill x
1800 pack .vpane.lower.diff -side bottom -fill both -expand 1
1801 .vpane add .vpane.lower -sticky nsew
1802
1803 # -- Commit Area Buttons
1804 #
1805 frame .vpane.lower.commarea.buttons
1806 label .vpane.lower.commarea.buttons.l -text {} \
1807         -anchor w \
1808         -justify left
1809 pack .vpane.lower.commarea.buttons.l -side top -fill x
1810 pack .vpane.lower.commarea.buttons -side left -fill y
1811
1812 button .vpane.lower.commarea.buttons.rescan -text {Rescan} \
1813         -command do_rescan
1814 pack .vpane.lower.commarea.buttons.rescan -side top -fill x
1815 lappend disable_on_lock \
1816         {.vpane.lower.commarea.buttons.rescan conf -state}
1817
1818 button .vpane.lower.commarea.buttons.incall -text {Add Existing} \
1819         -command do_add_all
1820 pack .vpane.lower.commarea.buttons.incall -side top -fill x
1821 lappend disable_on_lock \
1822         {.vpane.lower.commarea.buttons.incall conf -state}
1823
1824 button .vpane.lower.commarea.buttons.signoff -text {Sign Off} \
1825         -command do_signoff
1826 pack .vpane.lower.commarea.buttons.signoff -side top -fill x
1827
1828 button .vpane.lower.commarea.buttons.commit -text {Commit} \
1829         -command do_commit
1830 pack .vpane.lower.commarea.buttons.commit -side top -fill x
1831 lappend disable_on_lock \
1832         {.vpane.lower.commarea.buttons.commit conf -state}
1833
1834 # -- Commit Message Buffer
1835 #
1836 frame .vpane.lower.commarea.buffer
1837 frame .vpane.lower.commarea.buffer.header
1838 set ui_comm .vpane.lower.commarea.buffer.t
1839 set ui_coml .vpane.lower.commarea.buffer.header.l
1840 radiobutton .vpane.lower.commarea.buffer.header.new \
1841         -text {New Commit} \
1842         -command do_select_commit_type \
1843         -variable selected_commit_type \
1844         -value new
1845 lappend disable_on_lock \
1846         [list .vpane.lower.commarea.buffer.header.new conf -state]
1847 radiobutton .vpane.lower.commarea.buffer.header.amend \
1848         -text {Amend Last Commit} \
1849         -command do_select_commit_type \
1850         -variable selected_commit_type \
1851         -value amend
1852 lappend disable_on_lock \
1853         [list .vpane.lower.commarea.buffer.header.amend conf -state]
1854 label $ui_coml \
1855         -anchor w \
1856         -justify left
1857 proc trace_commit_type {varname args} {
1858         global ui_coml commit_type
1859         switch -glob -- $commit_type {
1860         initial       {set txt {Initial Commit Message:}}
1861         amend         {set txt {Amended Commit Message:}}
1862         amend-initial {set txt {Amended Initial Commit Message:}}
1863         amend-merge   {set txt {Amended Merge Commit Message:}}
1864         merge         {set txt {Merge Commit Message:}}
1865         *             {set txt {Commit Message:}}
1866         }
1867         $ui_coml conf -text $txt
1868 }
1869 trace add variable commit_type write trace_commit_type
1870 pack $ui_coml -side left -fill x
1871 pack .vpane.lower.commarea.buffer.header.amend -side right
1872 pack .vpane.lower.commarea.buffer.header.new -side right
1873
1874 text $ui_comm -background white -borderwidth 1 \
1875         -undo true \
1876         -maxundo 20 \
1877         -autoseparators true \
1878         -relief sunken \
1879         -width 75 -height 9 -wrap none \
1880         -font font_diff \
1881         -yscrollcommand {.vpane.lower.commarea.buffer.sby set}
1882 scrollbar .vpane.lower.commarea.buffer.sby \
1883         -command [list $ui_comm yview]
1884 pack .vpane.lower.commarea.buffer.header -side top -fill x
1885 pack .vpane.lower.commarea.buffer.sby -side right -fill y
1886 pack $ui_comm -side left -fill y
1887 pack .vpane.lower.commarea.buffer -side left -fill y
1888
1889 # -- Commit Message Buffer Context Menu
1890 #
1891 set ctxm .vpane.lower.commarea.buffer.ctxm
1892 menu $ctxm -tearoff 0
1893 $ctxm add command \
1894         -label {Cut} \
1895         -command {tk_textCut $ui_comm}
1896 $ctxm add command \
1897         -label {Copy} \
1898         -command {tk_textCopy $ui_comm}
1899 $ctxm add command \
1900         -label {Paste} \
1901         -command {tk_textPaste $ui_comm}
1902 $ctxm add command \
1903         -label {Delete} \
1904         -command {$ui_comm delete sel.first sel.last}
1905 $ctxm add separator
1906 $ctxm add command \
1907         -label {Select All} \
1908         -command {focus $ui_comm;$ui_comm tag add sel 0.0 end}
1909 $ctxm add command \
1910         -label {Copy All} \
1911         -command {
1912                 $ui_comm tag add sel 0.0 end
1913                 tk_textCopy $ui_comm
1914                 $ui_comm tag remove sel 0.0 end
1915         }
1916 $ctxm add separator
1917 $ctxm add command \
1918         -label {Sign Off} \
1919         -command do_signoff
1920 bind_button3 $ui_comm "tk_popup $ctxm %X %Y"
1921
1922 # -- Diff Header
1923 #
1924 proc trace_current_diff_path {varname args} {
1925         global current_diff_path diff_actions file_states
1926         if {$current_diff_path eq {}} {
1927                 set s {}
1928                 set f {}
1929                 set p {}
1930                 set o disabled
1931         } else {
1932                 set p $current_diff_path
1933                 set s [mapdesc [lindex $file_states($p) 0] $p]
1934                 set f {File:}
1935                 set p [escape_path $p]
1936                 set o normal
1937         }
1938
1939         .vpane.lower.diff.header.status configure -text $s
1940         .vpane.lower.diff.header.file configure -text $f
1941         .vpane.lower.diff.header.path configure -text $p
1942         foreach w $diff_actions {
1943                 uplevel #0 $w $o
1944         }
1945 }
1946 trace add variable current_diff_path write trace_current_diff_path
1947
1948 frame .vpane.lower.diff.header -background orange
1949 label .vpane.lower.diff.header.status \
1950         -background orange \
1951         -width $max_status_desc \
1952         -anchor w \
1953         -justify left
1954 label .vpane.lower.diff.header.file \
1955         -background orange \
1956         -anchor w \
1957         -justify left
1958 label .vpane.lower.diff.header.path \
1959         -background orange \
1960         -anchor w \
1961         -justify left
1962 pack .vpane.lower.diff.header.status -side left
1963 pack .vpane.lower.diff.header.file -side left
1964 pack .vpane.lower.diff.header.path -fill x
1965 set ctxm .vpane.lower.diff.header.ctxm
1966 menu $ctxm -tearoff 0
1967 $ctxm add command \
1968         -label {Copy} \
1969         -command {
1970                 clipboard clear
1971                 clipboard append \
1972                         -format STRING \
1973                         -type STRING \
1974                         -- $current_diff_path
1975         }
1976 lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
1977 bind_button3 .vpane.lower.diff.header.path "tk_popup $ctxm %X %Y"
1978
1979 # -- Diff Body
1980 #
1981 frame .vpane.lower.diff.body
1982 set ui_diff .vpane.lower.diff.body.t
1983 text $ui_diff -background white -borderwidth 0 \
1984         -width 80 -height 15 -wrap none \
1985         -font font_diff \
1986         -xscrollcommand {.vpane.lower.diff.body.sbx set} \
1987         -yscrollcommand {.vpane.lower.diff.body.sby set} \
1988         -state disabled
1989 scrollbar .vpane.lower.diff.body.sbx -orient horizontal \
1990         -command [list $ui_diff xview]
1991 scrollbar .vpane.lower.diff.body.sby -orient vertical \
1992         -command [list $ui_diff yview]
1993 pack .vpane.lower.diff.body.sbx -side bottom -fill x
1994 pack .vpane.lower.diff.body.sby -side right -fill y
1995 pack $ui_diff -side left -fill both -expand 1
1996 pack .vpane.lower.diff.header -side top -fill x
1997 pack .vpane.lower.diff.body -side bottom -fill both -expand 1
1998
1999 $ui_diff tag conf d_cr -elide true
2000 $ui_diff tag conf d_@ -foreground blue -font font_diffbold
2001 $ui_diff tag conf d_+ -foreground {#00a000}
2002 $ui_diff tag conf d_- -foreground red
2003
2004 $ui_diff tag conf d_++ -foreground {#00a000}
2005 $ui_diff tag conf d_-- -foreground red
2006 $ui_diff tag conf d_+s \
2007         -foreground {#00a000} \
2008         -background {#e2effa}
2009 $ui_diff tag conf d_-s \
2010         -foreground red \
2011         -background {#e2effa}
2012 $ui_diff tag conf d_s+ \
2013         -foreground {#00a000} \
2014         -background ivory1
2015 $ui_diff tag conf d_s- \
2016         -foreground red \
2017         -background ivory1
2018
2019 $ui_diff tag conf d<<<<<<< \
2020         -foreground orange \
2021         -font font_diffbold
2022 $ui_diff tag conf d======= \
2023         -foreground orange \
2024         -font font_diffbold
2025 $ui_diff tag conf d>>>>>>> \
2026         -foreground orange \
2027         -font font_diffbold
2028
2029 $ui_diff tag raise sel
2030
2031 # -- Diff Body Context Menu
2032 #
2033 set ctxm .vpane.lower.diff.body.ctxm
2034 menu $ctxm -tearoff 0
2035 $ctxm add command \
2036         -label {Refresh} \
2037         -command reshow_diff
2038 lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
2039 $ctxm add command \
2040         -label {Copy} \
2041         -command {tk_textCopy $ui_diff}
2042 lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
2043 $ctxm add command \
2044         -label {Select All} \
2045         -command {focus $ui_diff;$ui_diff tag add sel 0.0 end}
2046 lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
2047 $ctxm add command \
2048         -label {Copy All} \
2049         -command {
2050                 $ui_diff tag add sel 0.0 end
2051                 tk_textCopy $ui_diff
2052                 $ui_diff tag remove sel 0.0 end
2053         }
2054 lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
2055 $ctxm add separator
2056 $ctxm add command \
2057         -label {Apply/Reverse Hunk} \
2058         -command {apply_hunk $cursorX $cursorY}
2059 set ui_diff_applyhunk [$ctxm index last]
2060 lappend diff_actions [list $ctxm entryconf $ui_diff_applyhunk -state]
2061 $ctxm add separator
2062 $ctxm add command \
2063         -label {Decrease Font Size} \
2064         -command {incr_font_size font_diff -1}
2065 lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
2066 $ctxm add command \
2067         -label {Increase Font Size} \
2068         -command {incr_font_size font_diff 1}
2069 lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
2070 $ctxm add separator
2071 $ctxm add command \
2072         -label {Show Less Context} \
2073         -command {if {$repo_config(gui.diffcontext) >= 2} {
2074                 incr repo_config(gui.diffcontext) -1
2075                 reshow_diff
2076         }}
2077 lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
2078 $ctxm add command \
2079         -label {Show More Context} \
2080         -command {
2081                 incr repo_config(gui.diffcontext)
2082                 reshow_diff
2083         }
2084 lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
2085 $ctxm add separator
2086 $ctxm add command -label {Options...} \
2087         -command do_options
2088 bind_button3 $ui_diff "
2089         set cursorX %x
2090         set cursorY %y
2091         if {\$ui_index eq \$current_diff_side} {
2092                 $ctxm entryconf $ui_diff_applyhunk -label {Unstage Hunk From Commit}
2093         } else {
2094                 $ctxm entryconf $ui_diff_applyhunk -label {Stage Hunk For Commit}
2095         }
2096         tk_popup $ctxm %X %Y
2097 "
2098 unset ui_diff_applyhunk
2099
2100 # -- Status Bar
2101 #
2102 label .status -textvariable ui_status_value \
2103         -anchor w \
2104         -justify left \
2105         -borderwidth 1 \
2106         -relief sunken
2107 pack .status -anchor w -side bottom -fill x
2108
2109 # -- Load geometry
2110 #
2111 catch {
2112 set gm $repo_config(gui.geometry)
2113 wm geometry . [lindex $gm 0]
2114 .vpane sash place 0 \
2115         [lindex [.vpane sash coord 0] 0] \
2116         [lindex $gm 1]
2117 .vpane.files sash place 0 \
2118         [lindex $gm 2] \
2119         [lindex [.vpane.files sash coord 0] 1]
2120 unset gm
2121 }
2122
2123 # -- Key Bindings
2124 #
2125 bind $ui_comm <$M1B-Key-Return> {do_commit;break}
2126 bind $ui_comm <$M1B-Key-i> {do_add_all;break}
2127 bind $ui_comm <$M1B-Key-I> {do_add_all;break}
2128 bind $ui_comm <$M1B-Key-x> {tk_textCut %W;break}
2129 bind $ui_comm <$M1B-Key-X> {tk_textCut %W;break}
2130 bind $ui_comm <$M1B-Key-c> {tk_textCopy %W;break}
2131 bind $ui_comm <$M1B-Key-C> {tk_textCopy %W;break}
2132 bind $ui_comm <$M1B-Key-v> {tk_textPaste %W; %W see insert; break}
2133 bind $ui_comm <$M1B-Key-V> {tk_textPaste %W; %W see insert; break}
2134 bind $ui_comm <$M1B-Key-a> {%W tag add sel 0.0 end;break}
2135 bind $ui_comm <$M1B-Key-A> {%W tag add sel 0.0 end;break}
2136
2137 bind $ui_diff <$M1B-Key-x> {tk_textCopy %W;break}
2138 bind $ui_diff <$M1B-Key-X> {tk_textCopy %W;break}
2139 bind $ui_diff <$M1B-Key-c> {tk_textCopy %W;break}
2140 bind $ui_diff <$M1B-Key-C> {tk_textCopy %W;break}
2141 bind $ui_diff <$M1B-Key-v> {break}
2142 bind $ui_diff <$M1B-Key-V> {break}
2143 bind $ui_diff <$M1B-Key-a> {%W tag add sel 0.0 end;break}
2144 bind $ui_diff <$M1B-Key-A> {%W tag add sel 0.0 end;break}
2145 bind $ui_diff <Key-Up>     {catch {%W yview scroll -1 units};break}
2146 bind $ui_diff <Key-Down>   {catch {%W yview scroll  1 units};break}
2147 bind $ui_diff <Key-Left>   {catch {%W xview scroll -1 units};break}
2148 bind $ui_diff <Key-Right>  {catch {%W xview scroll  1 units};break}
2149 bind $ui_diff <Key-k>         {catch {%W yview scroll -1 units};break}
2150 bind $ui_diff <Key-j>         {catch {%W yview scroll  1 units};break}
2151 bind $ui_diff <Key-h>         {catch {%W xview scroll -1 units};break}
2152 bind $ui_diff <Key-l>         {catch {%W xview scroll  1 units};break}
2153 bind $ui_diff <Control-Key-b> {catch {%W yview scroll -1 pages};break}
2154 bind $ui_diff <Control-Key-f> {catch {%W yview scroll  1 pages};break}
2155 bind $ui_diff <Button-1>   {focus %W}
2156
2157 if {[is_enabled branch]} {
2158         bind . <$M1B-Key-n> do_create_branch
2159         bind . <$M1B-Key-N> do_create_branch
2160 }
2161
2162 bind all <Key-F5> do_rescan
2163 bind all <$M1B-Key-r> do_rescan
2164 bind all <$M1B-Key-R> do_rescan
2165 bind .   <$M1B-Key-s> do_signoff
2166 bind .   <$M1B-Key-S> do_signoff
2167 bind .   <$M1B-Key-i> do_add_all
2168 bind .   <$M1B-Key-I> do_add_all
2169 bind .   <$M1B-Key-Return> do_commit
2170 foreach i [list $ui_index $ui_workdir] {
2171         bind $i <Button-1>       "toggle_or_diff         $i %x %y; break"
2172         bind $i <$M1B-Button-1>  "add_one_to_selection   $i %x %y; break"
2173         bind $i <Shift-Button-1> "add_range_to_selection $i %x %y; break"
2174 }
2175 unset i
2176
2177 set file_lists($ui_index) [list]
2178 set file_lists($ui_workdir) [list]
2179
2180 wm title . "[appname] ([reponame]) [file normalize [file dirname [gitdir]]]"
2181 focus -force $ui_comm
2182
2183 # -- Warn the user about environmental problems.  Cygwin's Tcl
2184 #    does *not* pass its env array onto any processes it spawns.
2185 #    This means that git processes get none of our environment.
2186 #
2187 if {[is_Cygwin]} {
2188         set ignored_env 0
2189         set suggest_user {}
2190         set msg "Possible environment issues exist.
2191
2192 The following environment variables are probably
2193 going to be ignored by any Git subprocess run
2194 by [appname]:
2195
2196 "
2197         foreach name [array names env] {
2198                 switch -regexp -- $name {
2199                 {^GIT_INDEX_FILE$} -
2200                 {^GIT_OBJECT_DIRECTORY$} -
2201                 {^GIT_ALTERNATE_OBJECT_DIRECTORIES$} -
2202                 {^GIT_DIFF_OPTS$} -
2203                 {^GIT_EXTERNAL_DIFF$} -
2204                 {^GIT_PAGER$} -
2205                 {^GIT_TRACE$} -
2206                 {^GIT_CONFIG$} -
2207                 {^GIT_CONFIG_LOCAL$} -
2208                 {^GIT_(AUTHOR|COMMITTER)_DATE$} {
2209                         append msg " - $name\n"
2210                         incr ignored_env
2211                 }
2212                 {^GIT_(AUTHOR|COMMITTER)_(NAME|EMAIL)$} {
2213                         append msg " - $name\n"
2214                         incr ignored_env
2215                         set suggest_user $name
2216                 }
2217                 }
2218         }
2219         if {$ignored_env > 0} {
2220                 append msg "
2221 This is due to a known issue with the
2222 Tcl binary distributed by Cygwin."
2223
2224                 if {$suggest_user ne {}} {
2225                         append msg "
2226
2227 A good replacement for $suggest_user
2228 is placing values for the user.name and
2229 user.email settings into your personal
2230 ~/.gitconfig file.
2231 "
2232                 }
2233                 warn_popup $msg
2234         }
2235         unset ignored_env msg suggest_user name
2236 }
2237
2238 # -- Only initialize complex UI if we are going to stay running.
2239 #
2240 if {[is_enabled transport]} {
2241         load_all_remotes
2242         load_all_heads
2243
2244         populate_branch_menu
2245         populate_fetch_menu
2246         populate_push_menu
2247 }
2248
2249 # -- Only suggest a gc run if we are going to stay running.
2250 #
2251 if {[is_enabled multicommit]} {
2252         set object_limit 2000
2253         if {[is_Windows]} {set object_limit 200}
2254         regexp {^([0-9]+) objects,} [git count-objects] _junk objects_current
2255         if {$objects_current >= $object_limit} {
2256                 if {[ask_popup \
2257                         "This repository currently has $objects_current loose objects.
2258
2259 To maintain optimal performance it is strongly recommended that you compress the database when more than $object_limit loose objects exist.
2260
2261 Compress the database now?"] eq yes} {
2262                         do_gc
2263                 }
2264         }
2265         unset object_limit _junk objects_current
2266 }
2267
2268 lock_index begin-read
2269 after 1 do_rescan