]> asedeno.scripts.mit.edu Git - git.git/blob - git-gui.sh
git-gui: Allow users to rename branches through 'branch -m'
[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 }
1535
1536 if {[is_MacOSX]} {
1537         # -- Apple Menu (Mac OS X only)
1538         #
1539         .mbar add cascade -label Apple -menu .mbar.apple
1540         menu .mbar.apple
1541
1542         .mbar.apple add command -label "About [appname]" \
1543                 -command do_about
1544         .mbar.apple add command -label "Options..." \
1545                 -command do_options
1546 } else {
1547         # -- Edit Menu
1548         #
1549         .mbar.edit add separator
1550         .mbar.edit add command -label {Options...} \
1551                 -command do_options
1552
1553         # -- Tools Menu
1554         #
1555         if {[file exists /usr/local/miga/lib/gui-miga]
1556                 && [file exists .pvcsrc]} {
1557         proc do_miga {} {
1558                 global ui_status_value
1559                 if {![lock_index update]} return
1560                 set cmd [list sh --login -c "/usr/local/miga/lib/gui-miga \"[pwd]\""]
1561                 set miga_fd [open "|$cmd" r]
1562                 fconfigure $miga_fd -blocking 0
1563                 fileevent $miga_fd readable [list miga_done $miga_fd]
1564                 set ui_status_value {Running miga...}
1565         }
1566         proc miga_done {fd} {
1567                 read $fd 512
1568                 if {[eof $fd]} {
1569                         close $fd
1570                         unlock_index
1571                         rescan [list set ui_status_value {Ready.}]
1572                 }
1573         }
1574         .mbar add cascade -label Tools -menu .mbar.tools
1575         menu .mbar.tools
1576         .mbar.tools add command -label "Migrate" \
1577                 -command do_miga
1578         lappend disable_on_lock \
1579                 [list .mbar.tools entryconf [.mbar.tools index last] -state]
1580         }
1581 }
1582
1583 # -- Help Menu
1584 #
1585 .mbar add cascade -label Help -menu .mbar.help
1586 menu .mbar.help
1587
1588 if {![is_MacOSX]} {
1589         .mbar.help add command -label "About [appname]" \
1590                 -command do_about
1591 }
1592
1593 set browser {}
1594 catch {set browser $repo_config(instaweb.browser)}
1595 set doc_path [file dirname [gitexec]]
1596 set doc_path [file join $doc_path Documentation index.html]
1597
1598 if {[is_Cygwin]} {
1599         set doc_path [exec cygpath --mixed $doc_path]
1600 }
1601
1602 if {$browser eq {}} {
1603         if {[is_MacOSX]} {
1604                 set browser open
1605         } elseif {[is_Cygwin]} {
1606                 set program_files [file dirname [exec cygpath --windir]]
1607                 set program_files [file join $program_files {Program Files}]
1608                 set firefox [file join $program_files {Mozilla Firefox} firefox.exe]
1609                 set ie [file join $program_files {Internet Explorer} IEXPLORE.EXE]
1610                 if {[file exists $firefox]} {
1611                         set browser $firefox
1612                 } elseif {[file exists $ie]} {
1613                         set browser $ie
1614                 }
1615                 unset program_files firefox ie
1616         }
1617 }
1618
1619 if {[file isfile $doc_path]} {
1620         set doc_url "file:$doc_path"
1621 } else {
1622         set doc_url {http://www.kernel.org/pub/software/scm/git/docs/}
1623 }
1624
1625 if {$browser ne {}} {
1626         .mbar.help add command -label {Online Documentation} \
1627                 -command [list exec $browser $doc_url &]
1628 }
1629 unset browser doc_path doc_url
1630
1631 # -- Standard bindings
1632 #
1633 bind .   <Destroy> do_quit
1634 bind all <$M1B-Key-q> do_quit
1635 bind all <$M1B-Key-Q> do_quit
1636 bind all <$M1B-Key-w> {destroy [winfo toplevel %W]}
1637 bind all <$M1B-Key-W> {destroy [winfo toplevel %W]}
1638
1639 set subcommand_args {}
1640 proc usage {} {
1641         puts stderr "usage: $::argv0 $::subcommand $::subcommand_args"
1642         exit 1
1643 }
1644
1645 # -- Not a normal commit type invocation?  Do that instead!
1646 #
1647 switch -- $subcommand {
1648 browser {
1649         set subcommand_args {rev?}
1650         switch [llength $argv] {
1651         0 {
1652                 set current_branch [git symbolic-ref HEAD]
1653                 regsub ^refs/((heads|tags|remotes)/)? \
1654                         $current_branch {} current_branch
1655         }
1656         1 {
1657                 set current_branch [lindex $argv 0]
1658         }
1659         default usage
1660         }
1661         browser::new $current_branch
1662         return
1663 }
1664 blame {
1665         set subcommand_args {rev? path?}
1666         set head {}
1667         set path {}
1668         set is_path 0
1669         foreach a $argv {
1670                 if {$is_path || [file exists $_prefix$a]} {
1671                         if {$path ne {}} usage
1672                         set path $_prefix$a
1673                         break
1674                 } elseif {$a eq {--}} {
1675                         if {$path ne {}} {
1676                                 if {$head ne {}} usage
1677                                 set head $path
1678                                 set path {}
1679                         }
1680                         set is_path 1
1681                 } elseif {$head eq {}} {
1682                         if {$head ne {}} usage
1683                         set head $a
1684                 } else {
1685                         usage
1686                 }
1687         }
1688         unset is_path
1689
1690         if {$head eq {}} {
1691                 set current_branch [git symbolic-ref HEAD]
1692                 regsub ^refs/((heads|tags|remotes)/)? \
1693                         $current_branch {} current_branch
1694         } else {
1695                 set current_branch $head
1696         }
1697
1698         if {$path eq {}} usage
1699         blame::new $head $path
1700         return
1701 }
1702 citool -
1703 gui {
1704         if {[llength $argv] != 0} {
1705                 puts -nonewline stderr "usage: $argv0"
1706                 if {$subcommand ne {gui} && [appname] ne "git-$subcommand"} {
1707                         puts -nonewline stderr " $subcommand"
1708                 }
1709                 puts stderr {}
1710                 exit 1
1711         }
1712         # fall through to setup UI for commits
1713 }
1714 default {
1715         puts stderr "usage: $argv0 \[{blame|browser|citool}\]"
1716         exit 1
1717 }
1718 }
1719
1720 # -- Branch Control
1721 #
1722 frame .branch \
1723         -borderwidth 1 \
1724         -relief sunken
1725 label .branch.l1 \
1726         -text {Current Branch:} \
1727         -anchor w \
1728         -justify left
1729 label .branch.cb \
1730         -textvariable current_branch \
1731         -anchor w \
1732         -justify left
1733 pack .branch.l1 -side left
1734 pack .branch.cb -side left -fill x
1735 pack .branch -side top -fill x
1736
1737 # -- Main Window Layout
1738 #
1739 panedwindow .vpane -orient vertical
1740 panedwindow .vpane.files -orient horizontal
1741 .vpane add .vpane.files -sticky nsew -height 100 -width 200
1742 pack .vpane -anchor n -side top -fill both -expand 1
1743
1744 # -- Index File List
1745 #
1746 frame .vpane.files.index -height 100 -width 200
1747 label .vpane.files.index.title -text {Staged Changes (Will Be Committed)} \
1748         -background green
1749 text $ui_index -background white -borderwidth 0 \
1750         -width 20 -height 10 \
1751         -wrap none \
1752         -cursor $cursor_ptr \
1753         -xscrollcommand {.vpane.files.index.sx set} \
1754         -yscrollcommand {.vpane.files.index.sy set} \
1755         -state disabled
1756 scrollbar .vpane.files.index.sx -orient h -command [list $ui_index xview]
1757 scrollbar .vpane.files.index.sy -orient v -command [list $ui_index yview]
1758 pack .vpane.files.index.title -side top -fill x
1759 pack .vpane.files.index.sx -side bottom -fill x
1760 pack .vpane.files.index.sy -side right -fill y
1761 pack $ui_index -side left -fill both -expand 1
1762 .vpane.files add .vpane.files.index -sticky nsew
1763
1764 # -- Working Directory File List
1765 #
1766 frame .vpane.files.workdir -height 100 -width 200
1767 label .vpane.files.workdir.title -text {Unstaged Changes (Will Not Be Committed)} \
1768         -background red
1769 text $ui_workdir -background white -borderwidth 0 \
1770         -width 20 -height 10 \
1771         -wrap none \
1772         -cursor $cursor_ptr \
1773         -xscrollcommand {.vpane.files.workdir.sx set} \
1774         -yscrollcommand {.vpane.files.workdir.sy set} \
1775         -state disabled
1776 scrollbar .vpane.files.workdir.sx -orient h -command [list $ui_workdir xview]
1777 scrollbar .vpane.files.workdir.sy -orient v -command [list $ui_workdir yview]
1778 pack .vpane.files.workdir.title -side top -fill x
1779 pack .vpane.files.workdir.sx -side bottom -fill x
1780 pack .vpane.files.workdir.sy -side right -fill y
1781 pack $ui_workdir -side left -fill both -expand 1
1782 .vpane.files add .vpane.files.workdir -sticky nsew
1783
1784 foreach i [list $ui_index $ui_workdir] {
1785         $i tag conf in_diff -font font_uibold
1786         $i tag conf in_sel \
1787                 -background [$i cget -foreground] \
1788                 -foreground [$i cget -background]
1789 }
1790 unset i
1791
1792 # -- Diff and Commit Area
1793 #
1794 frame .vpane.lower -height 300 -width 400
1795 frame .vpane.lower.commarea
1796 frame .vpane.lower.diff -relief sunken -borderwidth 1
1797 pack .vpane.lower.commarea -side top -fill x
1798 pack .vpane.lower.diff -side bottom -fill both -expand 1
1799 .vpane add .vpane.lower -sticky nsew
1800
1801 # -- Commit Area Buttons
1802 #
1803 frame .vpane.lower.commarea.buttons
1804 label .vpane.lower.commarea.buttons.l -text {} \
1805         -anchor w \
1806         -justify left
1807 pack .vpane.lower.commarea.buttons.l -side top -fill x
1808 pack .vpane.lower.commarea.buttons -side left -fill y
1809
1810 button .vpane.lower.commarea.buttons.rescan -text {Rescan} \
1811         -command do_rescan
1812 pack .vpane.lower.commarea.buttons.rescan -side top -fill x
1813 lappend disable_on_lock \
1814         {.vpane.lower.commarea.buttons.rescan conf -state}
1815
1816 button .vpane.lower.commarea.buttons.incall -text {Add Existing} \
1817         -command do_add_all
1818 pack .vpane.lower.commarea.buttons.incall -side top -fill x
1819 lappend disable_on_lock \
1820         {.vpane.lower.commarea.buttons.incall conf -state}
1821
1822 button .vpane.lower.commarea.buttons.signoff -text {Sign Off} \
1823         -command do_signoff
1824 pack .vpane.lower.commarea.buttons.signoff -side top -fill x
1825
1826 button .vpane.lower.commarea.buttons.commit -text {Commit} \
1827         -command do_commit
1828 pack .vpane.lower.commarea.buttons.commit -side top -fill x
1829 lappend disable_on_lock \
1830         {.vpane.lower.commarea.buttons.commit conf -state}
1831
1832 # -- Commit Message Buffer
1833 #
1834 frame .vpane.lower.commarea.buffer
1835 frame .vpane.lower.commarea.buffer.header
1836 set ui_comm .vpane.lower.commarea.buffer.t
1837 set ui_coml .vpane.lower.commarea.buffer.header.l
1838 radiobutton .vpane.lower.commarea.buffer.header.new \
1839         -text {New Commit} \
1840         -command do_select_commit_type \
1841         -variable selected_commit_type \
1842         -value new
1843 lappend disable_on_lock \
1844         [list .vpane.lower.commarea.buffer.header.new conf -state]
1845 radiobutton .vpane.lower.commarea.buffer.header.amend \
1846         -text {Amend Last Commit} \
1847         -command do_select_commit_type \
1848         -variable selected_commit_type \
1849         -value amend
1850 lappend disable_on_lock \
1851         [list .vpane.lower.commarea.buffer.header.amend conf -state]
1852 label $ui_coml \
1853         -anchor w \
1854         -justify left
1855 proc trace_commit_type {varname args} {
1856         global ui_coml commit_type
1857         switch -glob -- $commit_type {
1858         initial       {set txt {Initial Commit Message:}}
1859         amend         {set txt {Amended Commit Message:}}
1860         amend-initial {set txt {Amended Initial Commit Message:}}
1861         amend-merge   {set txt {Amended Merge Commit Message:}}
1862         merge         {set txt {Merge Commit Message:}}
1863         *             {set txt {Commit Message:}}
1864         }
1865         $ui_coml conf -text $txt
1866 }
1867 trace add variable commit_type write trace_commit_type
1868 pack $ui_coml -side left -fill x
1869 pack .vpane.lower.commarea.buffer.header.amend -side right
1870 pack .vpane.lower.commarea.buffer.header.new -side right
1871
1872 text $ui_comm -background white -borderwidth 1 \
1873         -undo true \
1874         -maxundo 20 \
1875         -autoseparators true \
1876         -relief sunken \
1877         -width 75 -height 9 -wrap none \
1878         -font font_diff \
1879         -yscrollcommand {.vpane.lower.commarea.buffer.sby set}
1880 scrollbar .vpane.lower.commarea.buffer.sby \
1881         -command [list $ui_comm yview]
1882 pack .vpane.lower.commarea.buffer.header -side top -fill x
1883 pack .vpane.lower.commarea.buffer.sby -side right -fill y
1884 pack $ui_comm -side left -fill y
1885 pack .vpane.lower.commarea.buffer -side left -fill y
1886
1887 # -- Commit Message Buffer Context Menu
1888 #
1889 set ctxm .vpane.lower.commarea.buffer.ctxm
1890 menu $ctxm -tearoff 0
1891 $ctxm add command \
1892         -label {Cut} \
1893         -command {tk_textCut $ui_comm}
1894 $ctxm add command \
1895         -label {Copy} \
1896         -command {tk_textCopy $ui_comm}
1897 $ctxm add command \
1898         -label {Paste} \
1899         -command {tk_textPaste $ui_comm}
1900 $ctxm add command \
1901         -label {Delete} \
1902         -command {$ui_comm delete sel.first sel.last}
1903 $ctxm add separator
1904 $ctxm add command \
1905         -label {Select All} \
1906         -command {focus $ui_comm;$ui_comm tag add sel 0.0 end}
1907 $ctxm add command \
1908         -label {Copy All} \
1909         -command {
1910                 $ui_comm tag add sel 0.0 end
1911                 tk_textCopy $ui_comm
1912                 $ui_comm tag remove sel 0.0 end
1913         }
1914 $ctxm add separator
1915 $ctxm add command \
1916         -label {Sign Off} \
1917         -command do_signoff
1918 bind_button3 $ui_comm "tk_popup $ctxm %X %Y"
1919
1920 # -- Diff Header
1921 #
1922 proc trace_current_diff_path {varname args} {
1923         global current_diff_path diff_actions file_states
1924         if {$current_diff_path eq {}} {
1925                 set s {}
1926                 set f {}
1927                 set p {}
1928                 set o disabled
1929         } else {
1930                 set p $current_diff_path
1931                 set s [mapdesc [lindex $file_states($p) 0] $p]
1932                 set f {File:}
1933                 set p [escape_path $p]
1934                 set o normal
1935         }
1936
1937         .vpane.lower.diff.header.status configure -text $s
1938         .vpane.lower.diff.header.file configure -text $f
1939         .vpane.lower.diff.header.path configure -text $p
1940         foreach w $diff_actions {
1941                 uplevel #0 $w $o
1942         }
1943 }
1944 trace add variable current_diff_path write trace_current_diff_path
1945
1946 frame .vpane.lower.diff.header -background orange
1947 label .vpane.lower.diff.header.status \
1948         -background orange \
1949         -width $max_status_desc \
1950         -anchor w \
1951         -justify left
1952 label .vpane.lower.diff.header.file \
1953         -background orange \
1954         -anchor w \
1955         -justify left
1956 label .vpane.lower.diff.header.path \
1957         -background orange \
1958         -anchor w \
1959         -justify left
1960 pack .vpane.lower.diff.header.status -side left
1961 pack .vpane.lower.diff.header.file -side left
1962 pack .vpane.lower.diff.header.path -fill x
1963 set ctxm .vpane.lower.diff.header.ctxm
1964 menu $ctxm -tearoff 0
1965 $ctxm add command \
1966         -label {Copy} \
1967         -command {
1968                 clipboard clear
1969                 clipboard append \
1970                         -format STRING \
1971                         -type STRING \
1972                         -- $current_diff_path
1973         }
1974 lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
1975 bind_button3 .vpane.lower.diff.header.path "tk_popup $ctxm %X %Y"
1976
1977 # -- Diff Body
1978 #
1979 frame .vpane.lower.diff.body
1980 set ui_diff .vpane.lower.diff.body.t
1981 text $ui_diff -background white -borderwidth 0 \
1982         -width 80 -height 15 -wrap none \
1983         -font font_diff \
1984         -xscrollcommand {.vpane.lower.diff.body.sbx set} \
1985         -yscrollcommand {.vpane.lower.diff.body.sby set} \
1986         -state disabled
1987 scrollbar .vpane.lower.diff.body.sbx -orient horizontal \
1988         -command [list $ui_diff xview]
1989 scrollbar .vpane.lower.diff.body.sby -orient vertical \
1990         -command [list $ui_diff yview]
1991 pack .vpane.lower.diff.body.sbx -side bottom -fill x
1992 pack .vpane.lower.diff.body.sby -side right -fill y
1993 pack $ui_diff -side left -fill both -expand 1
1994 pack .vpane.lower.diff.header -side top -fill x
1995 pack .vpane.lower.diff.body -side bottom -fill both -expand 1
1996
1997 $ui_diff tag conf d_cr -elide true
1998 $ui_diff tag conf d_@ -foreground blue -font font_diffbold
1999 $ui_diff tag conf d_+ -foreground {#00a000}
2000 $ui_diff tag conf d_- -foreground red
2001
2002 $ui_diff tag conf d_++ -foreground {#00a000}
2003 $ui_diff tag conf d_-- -foreground red
2004 $ui_diff tag conf d_+s \
2005         -foreground {#00a000} \
2006         -background {#e2effa}
2007 $ui_diff tag conf d_-s \
2008         -foreground red \
2009         -background {#e2effa}
2010 $ui_diff tag conf d_s+ \
2011         -foreground {#00a000} \
2012         -background ivory1
2013 $ui_diff tag conf d_s- \
2014         -foreground red \
2015         -background ivory1
2016
2017 $ui_diff tag conf d<<<<<<< \
2018         -foreground orange \
2019         -font font_diffbold
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
2027 $ui_diff tag raise sel
2028
2029 # -- Diff Body Context Menu
2030 #
2031 set ctxm .vpane.lower.diff.body.ctxm
2032 menu $ctxm -tearoff 0
2033 $ctxm add command \
2034         -label {Refresh} \
2035         -command reshow_diff
2036 lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
2037 $ctxm add command \
2038         -label {Copy} \
2039         -command {tk_textCopy $ui_diff}
2040 lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
2041 $ctxm add command \
2042         -label {Select All} \
2043         -command {focus $ui_diff;$ui_diff tag add sel 0.0 end}
2044 lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
2045 $ctxm add command \
2046         -label {Copy All} \
2047         -command {
2048                 $ui_diff tag add sel 0.0 end
2049                 tk_textCopy $ui_diff
2050                 $ui_diff tag remove sel 0.0 end
2051         }
2052 lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
2053 $ctxm add separator
2054 $ctxm add command \
2055         -label {Apply/Reverse Hunk} \
2056         -command {apply_hunk $cursorX $cursorY}
2057 set ui_diff_applyhunk [$ctxm index last]
2058 lappend diff_actions [list $ctxm entryconf $ui_diff_applyhunk -state]
2059 $ctxm add separator
2060 $ctxm add command \
2061         -label {Decrease Font Size} \
2062         -command {incr_font_size font_diff -1}
2063 lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
2064 $ctxm add command \
2065         -label {Increase Font Size} \
2066         -command {incr_font_size font_diff 1}
2067 lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
2068 $ctxm add separator
2069 $ctxm add command \
2070         -label {Show Less Context} \
2071         -command {if {$repo_config(gui.diffcontext) >= 2} {
2072                 incr repo_config(gui.diffcontext) -1
2073                 reshow_diff
2074         }}
2075 lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
2076 $ctxm add command \
2077         -label {Show More Context} \
2078         -command {
2079                 incr repo_config(gui.diffcontext)
2080                 reshow_diff
2081         }
2082 lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
2083 $ctxm add separator
2084 $ctxm add command -label {Options...} \
2085         -command do_options
2086 bind_button3 $ui_diff "
2087         set cursorX %x
2088         set cursorY %y
2089         if {\$ui_index eq \$current_diff_side} {
2090                 $ctxm entryconf $ui_diff_applyhunk -label {Unstage Hunk From Commit}
2091         } else {
2092                 $ctxm entryconf $ui_diff_applyhunk -label {Stage Hunk For Commit}
2093         }
2094         tk_popup $ctxm %X %Y
2095 "
2096 unset ui_diff_applyhunk
2097
2098 # -- Status Bar
2099 #
2100 label .status -textvariable ui_status_value \
2101         -anchor w \
2102         -justify left \
2103         -borderwidth 1 \
2104         -relief sunken
2105 pack .status -anchor w -side bottom -fill x
2106
2107 # -- Load geometry
2108 #
2109 catch {
2110 set gm $repo_config(gui.geometry)
2111 wm geometry . [lindex $gm 0]
2112 .vpane sash place 0 \
2113         [lindex [.vpane sash coord 0] 0] \
2114         [lindex $gm 1]
2115 .vpane.files sash place 0 \
2116         [lindex $gm 2] \
2117         [lindex [.vpane.files sash coord 0] 1]
2118 unset gm
2119 }
2120
2121 # -- Key Bindings
2122 #
2123 bind $ui_comm <$M1B-Key-Return> {do_commit;break}
2124 bind $ui_comm <$M1B-Key-i> {do_add_all;break}
2125 bind $ui_comm <$M1B-Key-I> {do_add_all;break}
2126 bind $ui_comm <$M1B-Key-x> {tk_textCut %W;break}
2127 bind $ui_comm <$M1B-Key-X> {tk_textCut %W;break}
2128 bind $ui_comm <$M1B-Key-c> {tk_textCopy %W;break}
2129 bind $ui_comm <$M1B-Key-C> {tk_textCopy %W;break}
2130 bind $ui_comm <$M1B-Key-v> {tk_textPaste %W; %W see insert; break}
2131 bind $ui_comm <$M1B-Key-V> {tk_textPaste %W; %W see insert; break}
2132 bind $ui_comm <$M1B-Key-a> {%W tag add sel 0.0 end;break}
2133 bind $ui_comm <$M1B-Key-A> {%W tag add sel 0.0 end;break}
2134
2135 bind $ui_diff <$M1B-Key-x> {tk_textCopy %W;break}
2136 bind $ui_diff <$M1B-Key-X> {tk_textCopy %W;break}
2137 bind $ui_diff <$M1B-Key-c> {tk_textCopy %W;break}
2138 bind $ui_diff <$M1B-Key-C> {tk_textCopy %W;break}
2139 bind $ui_diff <$M1B-Key-v> {break}
2140 bind $ui_diff <$M1B-Key-V> {break}
2141 bind $ui_diff <$M1B-Key-a> {%W tag add sel 0.0 end;break}
2142 bind $ui_diff <$M1B-Key-A> {%W tag add sel 0.0 end;break}
2143 bind $ui_diff <Key-Up>     {catch {%W yview scroll -1 units};break}
2144 bind $ui_diff <Key-Down>   {catch {%W yview scroll  1 units};break}
2145 bind $ui_diff <Key-Left>   {catch {%W xview scroll -1 units};break}
2146 bind $ui_diff <Key-Right>  {catch {%W xview scroll  1 units};break}
2147 bind $ui_diff <Key-k>         {catch {%W yview scroll -1 units};break}
2148 bind $ui_diff <Key-j>         {catch {%W yview scroll  1 units};break}
2149 bind $ui_diff <Key-h>         {catch {%W xview scroll -1 units};break}
2150 bind $ui_diff <Key-l>         {catch {%W xview scroll  1 units};break}
2151 bind $ui_diff <Control-Key-b> {catch {%W yview scroll -1 pages};break}
2152 bind $ui_diff <Control-Key-f> {catch {%W yview scroll  1 pages};break}
2153 bind $ui_diff <Button-1>   {focus %W}
2154
2155 if {[is_enabled branch]} {
2156         bind . <$M1B-Key-n> do_create_branch
2157         bind . <$M1B-Key-N> do_create_branch
2158 }
2159
2160 bind all <Key-F5> do_rescan
2161 bind all <$M1B-Key-r> do_rescan
2162 bind all <$M1B-Key-R> do_rescan
2163 bind .   <$M1B-Key-s> do_signoff
2164 bind .   <$M1B-Key-S> do_signoff
2165 bind .   <$M1B-Key-i> do_add_all
2166 bind .   <$M1B-Key-I> do_add_all
2167 bind .   <$M1B-Key-Return> do_commit
2168 foreach i [list $ui_index $ui_workdir] {
2169         bind $i <Button-1>       "toggle_or_diff         $i %x %y; break"
2170         bind $i <$M1B-Button-1>  "add_one_to_selection   $i %x %y; break"
2171         bind $i <Shift-Button-1> "add_range_to_selection $i %x %y; break"
2172 }
2173 unset i
2174
2175 set file_lists($ui_index) [list]
2176 set file_lists($ui_workdir) [list]
2177
2178 wm title . "[appname] ([reponame]) [file normalize [file dirname [gitdir]]]"
2179 focus -force $ui_comm
2180
2181 # -- Warn the user about environmental problems.  Cygwin's Tcl
2182 #    does *not* pass its env array onto any processes it spawns.
2183 #    This means that git processes get none of our environment.
2184 #
2185 if {[is_Cygwin]} {
2186         set ignored_env 0
2187         set suggest_user {}
2188         set msg "Possible environment issues exist.
2189
2190 The following environment variables are probably
2191 going to be ignored by any Git subprocess run
2192 by [appname]:
2193
2194 "
2195         foreach name [array names env] {
2196                 switch -regexp -- $name {
2197                 {^GIT_INDEX_FILE$} -
2198                 {^GIT_OBJECT_DIRECTORY$} -
2199                 {^GIT_ALTERNATE_OBJECT_DIRECTORIES$} -
2200                 {^GIT_DIFF_OPTS$} -
2201                 {^GIT_EXTERNAL_DIFF$} -
2202                 {^GIT_PAGER$} -
2203                 {^GIT_TRACE$} -
2204                 {^GIT_CONFIG$} -
2205                 {^GIT_CONFIG_LOCAL$} -
2206                 {^GIT_(AUTHOR|COMMITTER)_DATE$} {
2207                         append msg " - $name\n"
2208                         incr ignored_env
2209                 }
2210                 {^GIT_(AUTHOR|COMMITTER)_(NAME|EMAIL)$} {
2211                         append msg " - $name\n"
2212                         incr ignored_env
2213                         set suggest_user $name
2214                 }
2215                 }
2216         }
2217         if {$ignored_env > 0} {
2218                 append msg "
2219 This is due to a known issue with the
2220 Tcl binary distributed by Cygwin."
2221
2222                 if {$suggest_user ne {}} {
2223                         append msg "
2224
2225 A good replacement for $suggest_user
2226 is placing values for the user.name and
2227 user.email settings into your personal
2228 ~/.gitconfig file.
2229 "
2230                 }
2231                 warn_popup $msg
2232         }
2233         unset ignored_env msg suggest_user name
2234 }
2235
2236 # -- Only initialize complex UI if we are going to stay running.
2237 #
2238 if {[is_enabled transport]} {
2239         load_all_remotes
2240         load_all_heads
2241
2242         populate_branch_menu
2243         populate_fetch_menu
2244         populate_push_menu
2245 }
2246
2247 # -- Only suggest a gc run if we are going to stay running.
2248 #
2249 if {[is_enabled multicommit]} {
2250         set object_limit 2000
2251         if {[is_Windows]} {set object_limit 200}
2252         regexp {^([0-9]+) objects,} [git count-objects] _junk objects_current
2253         if {$objects_current >= $object_limit} {
2254                 if {[ask_popup \
2255                         "This repository currently has $objects_current loose objects.
2256
2257 To maintain optimal performance it is strongly recommended that you compress the database when more than $object_limit loose objects exist.
2258
2259 Compress the database now?"] eq yes} {
2260                         do_gc
2261                 }
2262         }
2263         unset object_limit _junk objects_current
2264 }
2265
2266 lock_index begin-read
2267 after 1 do_rescan