]> asedeno.scripts.mit.edu Git - git.git/blob - git-gui
git-gui: Corrected behavior of deleted (but existing in HEAD) files.
[git.git] / git-gui
1 #!/bin/sh
2 # Tcl ignores the next line -*- tcl -*- \
3 exec wish "$0" -- "$@"
4
5 set copyright {
6 Copyright © 2006 Shawn Pearce, Paul Mackerras.
7
8 All rights reserved.
9
10 This program is free software; it may be used, copied, modified
11 and distributed under the terms of the GNU General Public Licence,
12 either version 2, or (at your option) any later version.}
13
14 set appname [lindex [file split $argv0] end]
15 set gitdir {}
16
17 ######################################################################
18 ##
19 ## config
20
21 proc is_many_config {name} {
22         switch -glob -- $name {
23         remote.*.fetch -
24         remote.*.push
25                 {return 1}
26         *
27                 {return 0}
28         }
29 }
30
31 proc load_config {include_global} {
32         global repo_config global_config default_config
33
34         array unset global_config
35         if {$include_global} {
36                 catch {
37                         set fd_rc [open "| git repo-config --global --list" r]
38                         while {[gets $fd_rc line] >= 0} {
39                                 if {[regexp {^([^=]+)=(.*)$} $line line name value]} {
40                                         if {[is_many_config $name]} {
41                                                 lappend global_config($name) $value
42                                         } else {
43                                                 set global_config($name) $value
44                                         }
45                                 }
46                         }
47                         close $fd_rc
48                 }
49         }
50
51         array unset repo_config
52         catch {
53                 set fd_rc [open "| git repo-config --list" r]
54                 while {[gets $fd_rc line] >= 0} {
55                         if {[regexp {^([^=]+)=(.*)$} $line line name value]} {
56                                 if {[is_many_config $name]} {
57                                         lappend repo_config($name) $value
58                                 } else {
59                                         set repo_config($name) $value
60                                 }
61                         }
62                 }
63                 close $fd_rc
64         }
65
66         foreach name [array names default_config] {
67                 if {[catch {set v $global_config($name)}]} {
68                         set global_config($name) $default_config($name)
69                 }
70                 if {[catch {set v $repo_config($name)}]} {
71                         set repo_config($name) $default_config($name)
72                 }
73         }
74 }
75
76 proc save_config {} {
77         global default_config font_descs
78         global repo_config global_config
79         global repo_config_new global_config_new
80
81         foreach option $font_descs {
82                 set name [lindex $option 0]
83                 set font [lindex $option 1]
84                 font configure $font \
85                         -family $global_config_new(gui.$font^^family) \
86                         -size $global_config_new(gui.$font^^size)
87                 font configure ${font}bold \
88                         -family $global_config_new(gui.$font^^family) \
89                         -size $global_config_new(gui.$font^^size)
90                 set global_config_new(gui.$name) [font configure $font]
91                 unset global_config_new(gui.$font^^family)
92                 unset global_config_new(gui.$font^^size)
93         }
94
95         foreach name [array names default_config] {
96                 set value $global_config_new($name)
97                 if {$value ne $global_config($name)} {
98                         if {$value eq $default_config($name)} {
99                                 catch {exec git repo-config --global --unset $name}
100                         } else {
101                                 regsub -all "\[{}\]" $value {"} value
102                                 exec git repo-config --global $name $value
103                         }
104                         set global_config($name) $value
105                         if {$value eq $repo_config($name)} {
106                                 catch {exec git repo-config --unset $name}
107                                 set repo_config($name) $value
108                         }
109                 }
110         }
111
112         foreach name [array names default_config] {
113                 set value $repo_config_new($name)
114                 if {$value ne $repo_config($name)} {
115                         if {$value eq $global_config($name)} {
116                                 catch {exec git repo-config --unset $name}
117                         } else {
118                                 regsub -all "\[{}\]" $value {"} value
119                                 exec git repo-config $name $value
120                         }
121                         set repo_config($name) $value
122                 }
123         }
124 }
125
126 proc error_popup {msg} {
127         global gitdir appname
128
129         set title $appname
130         if {$gitdir ne {}} {
131                 append title { (}
132                 append title [lindex \
133                         [file split [file normalize [file dirname $gitdir]]] \
134                         end]
135                 append title {)}
136         }
137         set cmd [list tk_messageBox \
138                 -icon error \
139                 -type ok \
140                 -title "$title: error" \
141                 -message $msg]
142         if {[winfo ismapped .]} {
143                 lappend cmd -parent .
144         }
145         eval $cmd
146 }
147
148 proc warn_popup {msg} {
149         global gitdir appname
150
151         set title $appname
152         if {$gitdir ne {}} {
153                 append title { (}
154                 append title [lindex \
155                         [file split [file normalize [file dirname $gitdir]]] \
156                         end]
157                 append title {)}
158         }
159         set cmd [list tk_messageBox \
160                 -icon warning \
161                 -type ok \
162                 -title "$title: warning" \
163                 -message $msg]
164         if {[winfo ismapped .]} {
165                 lappend cmd -parent .
166         }
167         eval $cmd
168 }
169
170 proc info_popup {msg} {
171         global gitdir appname
172
173         set title $appname
174         if {$gitdir ne {}} {
175                 append title { (}
176                 append title [lindex \
177                         [file split [file normalize [file dirname $gitdir]]] \
178                         end]
179                 append title {)}
180         }
181         tk_messageBox \
182                 -parent . \
183                 -icon info \
184                 -type ok \
185                 -title $title \
186                 -message $msg
187 }
188
189 ######################################################################
190 ##
191 ## repository setup
192
193 if {   [catch {set gitdir $env(GIT_DIR)}]
194         && [catch {set gitdir [exec git rev-parse --git-dir]} err]} {
195         catch {wm withdraw .}
196         error_popup "Cannot find the git directory:\n\n$err"
197         exit 1
198 }
199 if {![file isdirectory $gitdir]} {
200         catch {wm withdraw .}
201         error_popup "Git directory not found:\n\n$gitdir"
202         exit 1
203 }
204 if {[lindex [file split $gitdir] end] ne {.git}} {
205         catch {wm withdraw .}
206         error_popup "Cannot use funny .git directory:\n\n$gitdir"
207         exit 1
208 }
209 if {[catch {cd [file dirname $gitdir]} err]} {
210         catch {wm withdraw .}
211         error_popup "No working directory [file dirname $gitdir]:\n\n$err"
212         exit 1
213 }
214
215 set single_commit 0
216 if {$appname eq {git-citool}} {
217         set single_commit 1
218 }
219
220 ######################################################################
221 ##
222 ## task management
223
224 set rescan_active 0
225 set diff_active 0
226 set last_clicked {}
227
228 set disable_on_lock [list]
229 set index_lock_type none
230
231 proc lock_index {type} {
232         global index_lock_type disable_on_lock
233
234         if {$index_lock_type eq {none}} {
235                 set index_lock_type $type
236                 foreach w $disable_on_lock {
237                         uplevel #0 $w disabled
238                 }
239                 return 1
240         } elseif {$index_lock_type eq "begin-$type"} {
241                 set index_lock_type $type
242                 return 1
243         }
244         return 0
245 }
246
247 proc unlock_index {} {
248         global index_lock_type disable_on_lock
249
250         set index_lock_type none
251         foreach w $disable_on_lock {
252                 uplevel #0 $w normal
253         }
254 }
255
256 ######################################################################
257 ##
258 ## status
259
260 proc repository_state {ctvar hdvar mhvar} {
261         global gitdir current_branch
262         upvar $ctvar ct $hdvar hd $mhvar mh
263
264         set mh [list]
265
266         if {[catch {set current_branch [exec git symbolic-ref HEAD]}]} {
267                 set current_branch {}
268         } else {
269                 regsub ^refs/((heads|tags|remotes)/)? \
270                         $current_branch \
271                         {} \
272                         current_branch
273         }
274
275         if {[catch {set hd [exec git rev-parse --verify HEAD]}]} {
276                 set hd {}
277                 set ct initial
278                 return
279         }
280
281         set merge_head [file join $gitdir MERGE_HEAD]
282         if {[file exists $merge_head]} {
283                 set ct merge
284                 set fd_mh [open $merge_head r]
285                 while {[gets $fd_mh line] >= 0} {
286                         lappend mh $line
287                 }
288                 close $fd_mh
289                 return
290         }
291
292         set ct normal
293 }
294
295 proc PARENT {} {
296         global PARENT empty_tree
297
298         set p [lindex $PARENT 0]
299         if {$p ne {}} {
300                 return $p
301         }
302         if {$empty_tree eq {}} {
303                 set empty_tree [exec git mktree << {}]
304         }
305         return $empty_tree
306 }
307
308 proc rescan {after} {
309         global HEAD PARENT MERGE_HEAD commit_type
310         global ui_index ui_other ui_status_value ui_comm
311         global rescan_active file_states
312         global repo_config
313
314         if {$rescan_active > 0 || ![lock_index read]} return
315
316         repository_state newType newHEAD newMERGE_HEAD
317         if {[string match amend* $commit_type]
318                 && $newType eq {normal}
319                 && $newHEAD eq $HEAD} {
320         } else {
321                 set HEAD $newHEAD
322                 set PARENT $newHEAD
323                 set MERGE_HEAD $newMERGE_HEAD
324                 set commit_type $newType
325         }
326
327         array unset file_states
328
329         if {![$ui_comm edit modified]
330                 || [string trim [$ui_comm get 0.0 end]] eq {}} {
331                 if {[load_message GITGUI_MSG]} {
332                 } elseif {[load_message MERGE_MSG]} {
333                 } elseif {[load_message SQUASH_MSG]} {
334                 }
335                 $ui_comm edit reset
336                 $ui_comm edit modified false
337         }
338
339         if {$repo_config(gui.trustmtime) eq {true}} {
340                 rescan_stage2 {} $after
341         } else {
342                 set rescan_active 1
343                 set ui_status_value {Refreshing file status...}
344                 set cmd [list git update-index]
345                 lappend cmd -q
346                 lappend cmd --unmerged
347                 lappend cmd --ignore-missing
348                 lappend cmd --refresh
349                 set fd_rf [open "| $cmd" r]
350                 fconfigure $fd_rf -blocking 0 -translation binary
351                 fileevent $fd_rf readable \
352                         [list rescan_stage2 $fd_rf $after]
353         }
354 }
355
356 proc rescan_stage2 {fd after} {
357         global gitdir ui_status_value
358         global rescan_active buf_rdi buf_rdf buf_rlo
359
360         if {$fd ne {}} {
361                 read $fd
362                 if {![eof $fd]} return
363                 close $fd
364         }
365
366         set ls_others [list | git ls-files --others -z \
367                 --exclude-per-directory=.gitignore]
368         set info_exclude [file join $gitdir info exclude]
369         if {[file readable $info_exclude]} {
370                 lappend ls_others "--exclude-from=$info_exclude"
371         }
372
373         set buf_rdi {}
374         set buf_rdf {}
375         set buf_rlo {}
376
377         set rescan_active 3
378         set ui_status_value {Scanning for modified files ...}
379         set fd_di [open "| git diff-index --cached -z [PARENT]" r]
380         set fd_df [open "| git diff-files -z" r]
381         set fd_lo [open $ls_others r]
382
383         fconfigure $fd_di -blocking 0 -translation binary
384         fconfigure $fd_df -blocking 0 -translation binary
385         fconfigure $fd_lo -blocking 0 -translation binary
386         fileevent $fd_di readable [list read_diff_index $fd_di $after]
387         fileevent $fd_df readable [list read_diff_files $fd_df $after]
388         fileevent $fd_lo readable [list read_ls_others $fd_lo $after]
389 }
390
391 proc load_message {file} {
392         global gitdir ui_comm
393
394         set f [file join $gitdir $file]
395         if {[file isfile $f]} {
396                 if {[catch {set fd [open $f r]}]} {
397                         return 0
398                 }
399                 set content [string trim [read $fd]]
400                 close $fd
401                 $ui_comm delete 0.0 end
402                 $ui_comm insert end $content
403                 return 1
404         }
405         return 0
406 }
407
408 proc read_diff_index {fd after} {
409         global buf_rdi
410
411         append buf_rdi [read $fd]
412         set c 0
413         set n [string length $buf_rdi]
414         while {$c < $n} {
415                 set z1 [string first "\0" $buf_rdi $c]
416                 if {$z1 == -1} break
417                 incr z1
418                 set z2 [string first "\0" $buf_rdi $z1]
419                 if {$z2 == -1} break
420
421                 incr c
422                 set i [split [string range $buf_rdi $c [expr {$z1 - 2}]] { }]
423                 merge_state \
424                         [string range $buf_rdi $z1 [expr {$z2 - 1}]] \
425                         [lindex $i 4]? \
426                         [list [lindex $i 0] [lindex $i 2]] \
427                         [list]
428                 set c $z2
429                 incr c
430         }
431         if {$c < $n} {
432                 set buf_rdi [string range $buf_rdi $c end]
433         } else {
434                 set buf_rdi {}
435         }
436
437         rescan_done $fd buf_rdi $after
438 }
439
440 proc read_diff_files {fd after} {
441         global buf_rdf
442
443         append buf_rdf [read $fd]
444         set c 0
445         set n [string length $buf_rdf]
446         while {$c < $n} {
447                 set z1 [string first "\0" $buf_rdf $c]
448                 if {$z1 == -1} break
449                 incr z1
450                 set z2 [string first "\0" $buf_rdf $z1]
451                 if {$z2 == -1} break
452
453                 incr c
454                 set i [split [string range $buf_rdf $c [expr {$z1 - 2}]] { }]
455                 merge_state \
456                         [string range $buf_rdf $z1 [expr {$z2 - 1}]] \
457                         ?[lindex $i 4] \
458                         [list] \
459                         [list [lindex $i 0] [lindex $i 2]]
460                 set c $z2
461                 incr c
462         }
463         if {$c < $n} {
464                 set buf_rdf [string range $buf_rdf $c end]
465         } else {
466                 set buf_rdf {}
467         }
468
469         rescan_done $fd buf_rdf $after
470 }
471
472 proc read_ls_others {fd after} {
473         global buf_rlo
474
475         append buf_rlo [read $fd]
476         set pck [split $buf_rlo "\0"]
477         set buf_rlo [lindex $pck end]
478         foreach p [lrange $pck 0 end-1] {
479                 merge_state $p ?O
480         }
481         rescan_done $fd buf_rlo $after
482 }
483
484 proc rescan_done {fd buf after} {
485         global rescan_active
486         global file_states repo_config
487         upvar $buf to_clear
488
489         if {![eof $fd]} return
490         set to_clear {}
491         close $fd
492         if {[incr rescan_active -1] > 0} return
493
494         prune_selection
495         unlock_index
496         display_all_files
497
498         if {$repo_config(gui.partialinclude) ne {true}} {
499                 set pathList [list]
500                 foreach path [array names file_states] {
501                         switch -- [lindex $file_states($path) 0] {
502                         A? -
503                         M? {lappend pathList $path}
504                         }
505                 }
506                 if {$pathList ne {}} {
507                         update_index \
508                                 "Updating included files" \
509                                 $pathList \
510                                 [concat {reshow_diff;} $after]
511                         return
512                 }
513         }
514
515         reshow_diff
516         uplevel #0 $after
517 }
518
519 proc prune_selection {} {
520         global file_states selected_paths
521
522         foreach path [array names selected_paths] {
523                 if {[catch {set still_here $file_states($path)}]} {
524                         unset selected_paths($path)
525                 }
526         }
527 }
528
529 ######################################################################
530 ##
531 ## diff
532
533 proc clear_diff {} {
534         global ui_diff current_diff ui_index ui_other
535
536         $ui_diff conf -state normal
537         $ui_diff delete 0.0 end
538         $ui_diff conf -state disabled
539
540         set current_diff {}
541
542         $ui_index tag remove in_diff 0.0 end
543         $ui_other tag remove in_diff 0.0 end
544 }
545
546 proc reshow_diff {} {
547         global current_diff ui_status_value file_states
548
549         if {$current_diff eq {}
550                 || [catch {set s $file_states($current_diff)}]} {
551                 clear_diff
552         } else {
553                 show_diff $current_diff
554         }
555 }
556
557 proc handle_empty_diff {} {
558         global current_diff file_states file_lists
559
560         set path $current_diff
561         set s $file_states($path)
562         if {[lindex $s 0] ne {_M}} return
563
564         info_popup "No differences detected.
565
566 [short_path $path] has no changes.
567
568 The modification date of this file was updated
569 by another application and you currently have
570 the Trust File Modification Timestamps option
571 enabled, so Git did not automatically detect
572 that there are no content differences in this
573 file.
574
575 This file will now be removed from the modified
576 files list, to prevent possible confusion.
577 "
578         if {[catch {exec git update-index -- $path} err]} {
579                 error_popup "Failed to refresh index:\n\n$err"
580         }
581
582         clear_diff
583         set old_w [mapcol [lindex $file_states($path) 0] $path]
584         set lno [lsearch -sorted $file_lists($old_w) $path]
585         if {$lno >= 0} {
586                 set file_lists($old_w) \
587                         [lreplace $file_lists($old_w) $lno $lno]
588                 incr lno
589                 $old_w conf -state normal
590                 $old_w delete $lno.0 [expr {$lno + 1}].0
591                 $old_w conf -state disabled
592         }
593 }
594
595 proc show_diff {path {w {}} {lno {}}} {
596         global file_states file_lists
597         global is_3way_diff diff_active repo_config
598         global ui_diff current_diff ui_status_value
599
600         if {$diff_active || ![lock_index read]} return
601
602         clear_diff
603         if {$w eq {} || $lno == {}} {
604                 foreach w [array names file_lists] {
605                         set lno [lsearch -sorted $file_lists($w) $path]
606                         if {$lno >= 0} {
607                                 incr lno
608                                 break
609                         }
610                 }
611         }
612         if {$w ne {} && $lno >= 1} {
613                 $w tag add in_diff $lno.0 [expr {$lno + 1}].0
614         }
615
616         set s $file_states($path)
617         set m [lindex $s 0]
618         set is_3way_diff 0
619         set diff_active 1
620         set current_diff $path
621         set ui_status_value "Loading diff of [escape_path $path]..."
622
623         set cmd [list | git diff-index]
624         lappend cmd --no-color
625         if {$repo_config(gui.diffcontext) > 0} {
626                 lappend cmd "-U$repo_config(gui.diffcontext)"
627         }
628         lappend cmd -p
629
630         switch $m {
631         MM {
632                 lappend cmd -c
633         }
634         _O {
635                 if {[catch {
636                                 set fd [open $path r]
637                                 set content [read $fd]
638                                 close $fd
639                         } err ]} {
640                         set diff_active 0
641                         unlock_index
642                         set ui_status_value "Unable to display [escape_path $path]"
643                         error_popup "Error loading file:\n\n$err"
644                         return
645                 }
646                 $ui_diff conf -state normal
647                 $ui_diff insert end $content
648                 $ui_diff conf -state disabled
649                 set diff_active 0
650                 unlock_index
651                 set ui_status_value {Ready.}
652                 return
653         }
654         }
655
656         lappend cmd [PARENT]
657         lappend cmd --
658         lappend cmd $path
659
660         if {[catch {set fd [open $cmd r]} err]} {
661                 set diff_active 0
662                 unlock_index
663                 set ui_status_value "Unable to display [escape_path $path]"
664                 error_popup "Error loading diff:\n\n$err"
665                 return
666         }
667
668         fconfigure $fd -blocking 0 -translation auto
669         fileevent $fd readable [list read_diff $fd]
670 }
671
672 proc read_diff {fd} {
673         global ui_diff ui_status_value is_3way_diff diff_active
674         global repo_config
675
676         $ui_diff conf -state normal
677         while {[gets $fd line] >= 0} {
678                 # -- Cleanup uninteresting diff header lines.
679                 #
680                 if {[string match {diff --git *}      $line]} continue
681                 if {[string match {diff --combined *} $line]} continue
682                 if {[string match {--- *}             $line]} continue
683                 if {[string match {+++ *}             $line]} continue
684                 if {$line eq {deleted file mode 120000}} {
685                         set line "deleted symlink"
686                 }
687
688                 # -- Automatically detect if this is a 3 way diff.
689                 #
690                 if {[string match {@@@ *} $line]} {set is_3way_diff 1}
691
692                 # -- Reformat a 3 way diff, 'cause its too weird.
693                 #
694                 if {$is_3way_diff} {
695                         set op [string range $line 0 1]
696                         switch -- $op {
697                         {@@} {set tags d_@}
698                         {++} {set tags d_+ ; set op { +}}
699                         {--} {set tags d_- ; set op { -}}
700                         { +} {set tags d_++; set op {++}}
701                         { -} {set tags d_--; set op {--}}
702                         {+ } {set tags d_-+; set op {-+}}
703                         {- } {set tags d_+-; set op {+-}}
704                         default {set tags {}}
705                         }
706                         set line [string replace $line 0 1 $op]
707                 } else {
708                         switch -- [string index $line 0] {
709                         @ {set tags d_@}
710                         + {set tags d_+}
711                         - {set tags d_-}
712                         default {set tags {}}
713                         }
714                 }
715                 $ui_diff insert end $line $tags
716                 $ui_diff insert end "\n" $tags
717         }
718         $ui_diff conf -state disabled
719
720         if {[eof $fd]} {
721                 close $fd
722                 set diff_active 0
723                 unlock_index
724                 set ui_status_value {Ready.}
725
726                 if {$repo_config(gui.trustmtime) eq {true}
727                         && [$ui_diff index end] eq {2.0}} {
728                         handle_empty_diff
729                 }
730         }
731 }
732
733 ######################################################################
734 ##
735 ## commit
736
737 proc load_last_commit {} {
738         global HEAD PARENT MERGE_HEAD commit_type ui_comm
739
740         if {[llength $PARENT] == 0} {
741                 error_popup {There is nothing to amend.
742
743 You are about to create the initial commit.
744 There is no commit before this to amend.
745 }
746                 return
747         }
748
749         repository_state curType curHEAD curMERGE_HEAD
750         if {$curType eq {merge}} {
751                 error_popup {Cannot amend while merging.
752
753 You are currently in the middle of a merge that
754 has not been fully completed.  You cannot amend
755 the prior commit unless you first abort the
756 current merge activity.
757 }
758                 return
759         }
760
761         set msg {}
762         set parents [list]
763         if {[catch {
764                         set fd [open "| git cat-file commit $curHEAD" r]
765                         while {[gets $fd line] > 0} {
766                                 if {[string match {parent *} $line]} {
767                                         lappend parents [string range $line 7 end]
768                                 }
769                         }
770                         set msg [string trim [read $fd]]
771                         close $fd
772                 } err]} {
773                 error_popup "Error loading commit data for amend:\n\n$err"
774                 return
775         }
776
777         set HEAD $curHEAD
778         set PARENT $parents
779         set MERGE_HEAD [list]
780         switch -- [llength $parents] {
781         0       {set commit_type amend-initial}
782         1       {set commit_type amend}
783         default {set commit_type amend-merge}
784         }
785
786         $ui_comm delete 0.0 end
787         $ui_comm insert end $msg
788         $ui_comm edit reset
789         $ui_comm edit modified false
790         rescan {set ui_status_value {Ready.}}
791 }
792
793 proc create_new_commit {} {
794         global commit_type ui_comm
795
796         set commit_type normal
797         $ui_comm delete 0.0 end
798         $ui_comm edit reset
799         $ui_comm edit modified false
800         rescan {set ui_status_value {Ready.}}
801 }
802
803 set GIT_COMMITTER_IDENT {}
804
805 proc committer_ident {} {
806         global GIT_COMMITTER_IDENT
807
808         if {$GIT_COMMITTER_IDENT eq {}} {
809                 if {[catch {set me [exec git var GIT_COMMITTER_IDENT]} err]} {
810                         error_popup "Unable to obtain your identity:\n\n$err"
811                         return {}
812                 }
813                 if {![regexp {^(.*) [0-9]+ [-+0-9]+$} \
814                         $me me GIT_COMMITTER_IDENT]} {
815                         error_popup "Invalid GIT_COMMITTER_IDENT:\n\n$me"
816                         return {}
817                 }
818         }
819
820         return $GIT_COMMITTER_IDENT
821 }
822
823 proc commit_tree {} {
824         global HEAD commit_type file_states ui_comm repo_config
825
826         if {![lock_index update]} return
827         if {[committer_ident] eq {}} return
828
829         # -- Our in memory state should match the repository.
830         #
831         repository_state curType curHEAD curMERGE_HEAD
832         if {[string match amend* $commit_type]
833                 && $curType eq {normal}
834                 && $curHEAD eq $HEAD} {
835         } elseif {$commit_type ne $curType || $HEAD ne $curHEAD} {
836                 info_popup {Last scanned state does not match repository state.
837
838 Another Git program has modified this repository
839 since the last scan.  A rescan must be performed
840 before another commit can be created.
841
842 The rescan will be automatically started now.
843 }
844                 unlock_index
845                 rescan {set ui_status_value {Ready.}}
846                 return
847         }
848
849         # -- At least one file should differ in the index.
850         #
851         set files_ready 0
852         foreach path [array names file_states] {
853                 switch -glob -- [lindex $file_states($path) 0] {
854                 _? {continue}
855                 A? -
856                 D? -
857                 M? {set files_ready 1; break}
858                 U? {
859                         error_popup "Unmerged files cannot be committed.
860
861 File [short_path $path] has merge conflicts.
862 You must resolve them and include the file before committing.
863 "
864                         unlock_index
865                         return
866                 }
867                 default {
868                         error_popup "Unknown file state [lindex $s 0] detected.
869
870 File [short_path $path] cannot be committed by this program.
871 "
872                 }
873                 }
874         }
875         if {!$files_ready} {
876                 error_popup {No included files to commit.
877
878 You must include at least 1 file before you can commit.
879 }
880                 unlock_index
881                 return
882         }
883
884         # -- A message is required.
885         #
886         set msg [string trim [$ui_comm get 1.0 end]]
887         if {$msg eq {}} {
888                 error_popup {Please supply a commit message.
889
890 A good commit message has the following format:
891
892 - First line: Describe in one sentance what you did.
893 - Second line: Blank
894 - Remaining lines: Describe why this change is good.
895 }
896                 unlock_index
897                 return
898         }
899
900         # -- Update included files if partialincludes are off.
901         #
902         if {$repo_config(gui.partialinclude) ne {true}} {
903                 set pathList [list]
904                 foreach path [array names file_states] {
905                         switch -glob -- [lindex $file_states($path) 0] {
906                         A? -
907                         M? {lappend pathList $path}
908                         }
909                 }
910                 if {$pathList ne {}} {
911                         unlock_index
912                         update_index \
913                                 "Updating included files" \
914                                 $pathList \
915                                 [concat {lock_index update;} \
916                                         [list commit_prehook $curHEAD $msg]]
917                         return
918                 }
919         }
920
921         commit_prehook $curHEAD $msg
922 }
923
924 proc commit_prehook {curHEAD msg} {
925         global gitdir ui_status_value pch_error
926
927         set pchook [file join $gitdir hooks pre-commit]
928
929         # On Cygwin [file executable] might lie so we need to ask
930         # the shell if the hook is executable.  Yes that's annoying.
931         #
932         if {[is_Windows] && [file isfile $pchook]} {
933                 set pchook [list sh -c [concat \
934                         "if test -x \"$pchook\";" \
935                         "then exec \"$pchook\" 2>&1;" \
936                         "fi"]]
937         } elseif {[file executable $pchook]} {
938                 set pchook [list $pchook |& cat]
939         } else {
940                 commit_writetree $curHEAD $msg
941                 return
942         }
943
944         set ui_status_value {Calling pre-commit hook...}
945         set pch_error {}
946         set fd_ph [open "| $pchook" r]
947         fconfigure $fd_ph -blocking 0 -translation binary
948         fileevent $fd_ph readable \
949                 [list commit_prehook_wait $fd_ph $curHEAD $msg]
950 }
951
952 proc commit_prehook_wait {fd_ph curHEAD msg} {
953         global pch_error ui_status_value
954
955         append pch_error [read $fd_ph]
956         fconfigure $fd_ph -blocking 1
957         if {[eof $fd_ph]} {
958                 if {[catch {close $fd_ph}]} {
959                         set ui_status_value {Commit declined by pre-commit hook.}
960                         hook_failed_popup pre-commit $pch_error
961                         unlock_index
962                 } else {
963                         commit_writetree $curHEAD $msg
964                 }
965                 set pch_error {}
966                 return
967         }
968         fconfigure $fd_ph -blocking 0
969 }
970
971 proc commit_writetree {curHEAD msg} {
972         global ui_status_value
973
974         set ui_status_value {Committing changes...}
975         set fd_wt [open "| git write-tree" r]
976         fileevent $fd_wt readable \
977                 [list commit_committree $fd_wt $curHEAD $msg]
978 }
979
980 proc commit_committree {fd_wt curHEAD msg} {
981         global HEAD PARENT MERGE_HEAD commit_type
982         global single_commit gitdir
983         global ui_status_value ui_comm selected_commit_type
984         global file_states selected_paths rescan_active
985
986         gets $fd_wt tree_id
987         if {$tree_id eq {} || [catch {close $fd_wt} err]} {
988                 error_popup "write-tree failed:\n\n$err"
989                 set ui_status_value {Commit failed.}
990                 unlock_index
991                 return
992         }
993
994         # -- Create the commit.
995         #
996         set cmd [list git commit-tree $tree_id]
997         set parents [concat $PARENT $MERGE_HEAD]
998         if {[llength $parents] > 0} {
999                 foreach p $parents {
1000                         lappend cmd -p $p
1001                 }
1002         } else {
1003                 # git commit-tree writes to stderr during initial commit.
1004                 lappend cmd 2>/dev/null
1005         }
1006         lappend cmd << $msg
1007         if {[catch {set cmt_id [eval exec $cmd]} err]} {
1008                 error_popup "commit-tree failed:\n\n$err"
1009                 set ui_status_value {Commit failed.}
1010                 unlock_index
1011                 return
1012         }
1013
1014         # -- Update the HEAD ref.
1015         #
1016         set reflogm commit
1017         if {$commit_type ne {normal}} {
1018                 append reflogm " ($commit_type)"
1019         }
1020         set i [string first "\n" $msg]
1021         if {$i >= 0} {
1022                 append reflogm {: } [string range $msg 0 [expr {$i - 1}]]
1023         } else {
1024                 append reflogm {: } $msg
1025         }
1026         set cmd [list git update-ref -m $reflogm HEAD $cmt_id $curHEAD]
1027         if {[catch {eval exec $cmd} err]} {
1028                 error_popup "update-ref failed:\n\n$err"
1029                 set ui_status_value {Commit failed.}
1030                 unlock_index
1031                 return
1032         }
1033
1034         # -- Cleanup after ourselves.
1035         #
1036         catch {file delete [file join $gitdir MERGE_HEAD]}
1037         catch {file delete [file join $gitdir MERGE_MSG]}
1038         catch {file delete [file join $gitdir SQUASH_MSG]}
1039         catch {file delete [file join $gitdir GITGUI_MSG]}
1040
1041         # -- Let rerere do its thing.
1042         #
1043         if {[file isdirectory [file join $gitdir rr-cache]]} {
1044                 catch {exec git rerere}
1045         }
1046
1047         # -- Run the post-commit hook.
1048         #
1049         set pchook [file join $gitdir hooks post-commit]
1050         if {[is_Windows] && [file isfile $pchook]} {
1051                 set pchook [list sh -c [concat \
1052                         "if test -x \"$pchook\";" \
1053                         "then exec \"$pchook\";" \
1054                         "fi"]]
1055         } elseif {![file executable $pchook]} {
1056                 set pchook {}
1057         }
1058         if {$pchook ne {}} {
1059                 catch {exec $pchook &}
1060         }
1061
1062         $ui_comm delete 0.0 end
1063         $ui_comm edit reset
1064         $ui_comm edit modified false
1065
1066         if {$single_commit} do_quit
1067
1068         # -- Update in memory status
1069         #
1070         set selected_commit_type new
1071         set commit_type normal
1072         set HEAD $cmt_id
1073         set PARENT $cmt_id
1074         set MERGE_HEAD [list]
1075
1076         foreach path [array names file_states] {
1077                 set s $file_states($path)
1078                 set m [lindex $s 0]
1079                 switch -glob -- $m {
1080                 _O -
1081                 _M -
1082                 _D {continue}
1083                 __ -
1084                 A_ -
1085                 M_ -
1086                 DD {
1087                         unset file_states($path)
1088                         catch {unset selected_paths($path)}
1089                 }
1090                 DO {
1091                         set file_states($path) [list _O [lindex $s 1] {} {}]
1092                 }
1093                 AM -
1094                 AD -
1095                 MM -
1096                 MD -
1097                 DM {
1098                         set file_states($path) [list \
1099                                 _[string index $m 1] \
1100                                 [lindex $s 1] \
1101                                 [lindex $s 3] \
1102                                 {}]
1103                 }
1104                 }
1105         }
1106
1107         display_all_files
1108         unlock_index
1109         reshow_diff
1110         set ui_status_value \
1111                 "Changes committed as [string range $cmt_id 0 7]."
1112 }
1113
1114 ######################################################################
1115 ##
1116 ## fetch pull push
1117
1118 proc fetch_from {remote} {
1119         set w [new_console "fetch $remote" \
1120                 "Fetching new changes from $remote"]
1121         set cmd [list git fetch]
1122         lappend cmd $remote
1123         console_exec $w $cmd
1124 }
1125
1126 proc pull_remote {remote branch} {
1127         global HEAD commit_type file_states repo_config
1128
1129         if {![lock_index update]} return
1130
1131         # -- Our in memory state should match the repository.
1132         #
1133         repository_state curType curHEAD curMERGE_HEAD
1134         if {$commit_type ne $curType || $HEAD ne $curHEAD} {
1135                 info_popup {Last scanned state does not match repository state.
1136
1137 Another Git program has modified this repository
1138 since the last scan.  A rescan must be performed
1139 before a pull operation can be started.
1140
1141 The rescan will be automatically started now.
1142 }
1143                 unlock_index
1144                 rescan {set ui_status_value {Ready.}}
1145                 return
1146         }
1147
1148         # -- No differences should exist before a pull.
1149         #
1150         if {[array size file_states] != 0} {
1151                 error_popup {Uncommitted but modified files are present.
1152
1153 You should not perform a pull with unmodified
1154 files in your working directory as Git will be
1155 unable to recover from an incorrect merge.
1156
1157 You should commit or revert all changes before
1158 starting a pull operation.
1159 }
1160                 unlock_index
1161                 return
1162         }
1163
1164         set w [new_console "pull $remote $branch" \
1165                 "Pulling new changes from branch $branch in $remote"]
1166         set cmd [list git pull]
1167         if {$repo_config(gui.pullsummary) eq {false}} {
1168                 lappend cmd --no-summary
1169         }
1170         lappend cmd $remote
1171         lappend cmd $branch
1172         console_exec $w $cmd [list post_pull_remote $remote $branch]
1173 }
1174
1175 proc post_pull_remote {remote branch success} {
1176         global HEAD PARENT MERGE_HEAD commit_type selected_commit_type
1177         global ui_status_value
1178
1179         unlock_index
1180         if {$success} {
1181                 repository_state commit_type HEAD MERGE_HEAD
1182                 set PARENT $HEAD
1183                 set selected_commit_type new
1184                 set ui_status_value "Pulling $branch from $remote complete."
1185         } else {
1186                 rescan [list set ui_status_value \
1187                         "Conflicts detected while pulling $branch from $remote."]
1188         }
1189 }
1190
1191 proc push_to {remote} {
1192         set w [new_console "push $remote" \
1193                 "Pushing changes to $remote"]
1194         set cmd [list git push]
1195         lappend cmd $remote
1196         console_exec $w $cmd
1197 }
1198
1199 ######################################################################
1200 ##
1201 ## ui helpers
1202
1203 proc mapcol {state path} {
1204         global all_cols ui_other
1205
1206         if {[catch {set r $all_cols($state)}]} {
1207                 puts "error: no column for state={$state} $path"
1208                 return $ui_other
1209         }
1210         return $r
1211 }
1212
1213 proc mapicon {state path} {
1214         global all_icons
1215
1216         if {[catch {set r $all_icons($state)}]} {
1217                 puts "error: no icon for state={$state} $path"
1218                 return file_plain
1219         }
1220         return $r
1221 }
1222
1223 proc mapdesc {state path} {
1224         global all_descs
1225
1226         if {[catch {set r $all_descs($state)}]} {
1227                 puts "error: no desc for state={$state} $path"
1228                 return $state
1229         }
1230         return $r
1231 }
1232
1233 proc escape_path {path} {
1234         regsub -all "\n" $path "\\n" path
1235         return $path
1236 }
1237
1238 proc short_path {path} {
1239         return [escape_path [lindex [file split $path] end]]
1240 }
1241
1242 set next_icon_id 0
1243 set null_sha1 [string repeat 0 40]
1244
1245 proc merge_state {path new_state {head_info {}} {index_info {}}} {
1246         global file_states next_icon_id null_sha1
1247
1248         set s0 [string index $new_state 0]
1249         set s1 [string index $new_state 1]
1250
1251         if {[catch {set info $file_states($path)}]} {
1252                 set state __
1253                 set icon n[incr next_icon_id]
1254         } else {
1255                 set state [lindex $info 0]
1256                 set icon [lindex $info 1]
1257                 if {$head_info eq {}}  {set head_info  [lindex $info 2]}
1258                 if {$index_info eq {}} {set index_info [lindex $info 3]}
1259         }
1260
1261         if     {$s0 eq {?}} {set s0 [string index $state 0]} \
1262         elseif {$s0 eq {_}} {set s0 _}
1263
1264         if     {$s1 eq {?}} {set s1 [string index $state 1]} \
1265         elseif {$s1 eq {_}} {set s1 _}
1266
1267         if {$s0 eq {A} && $s1 eq {_} && $head_info eq {}} {
1268                 set head_info [list 0 $null_sha1]
1269         } elseif {$s0 ne {_} && [string index $state 0] eq {_}
1270                 && $head_info eq {}} {
1271                 set head_info $index_info
1272         }
1273
1274         set file_states($path) [list $s0$s1 $icon \
1275                 $head_info $index_info \
1276                 ]
1277         return $state
1278 }
1279
1280 proc display_file {path state} {
1281         global file_states file_lists selected_paths
1282
1283         set old_m [merge_state $path $state]
1284         set s $file_states($path)
1285         set new_m [lindex $s 0]
1286         set new_w [mapcol $new_m $path] 
1287         set old_w [mapcol $old_m $path]
1288         set new_icon [mapicon $new_m $path]
1289
1290         if {$new_m eq {__}} {
1291                 set lno [lsearch -sorted $file_lists($old_w) $path]
1292                 if {$lno >= 0} {
1293                         set file_lists($old_w) \
1294                                 [lreplace $file_lists($old_w) $lno $lno]
1295                         incr lno
1296                         $old_w conf -state normal
1297                         $old_w delete $lno.0 [expr {$lno + 1}].0
1298                         $old_w conf -state disabled
1299                 }
1300                 unset file_states($path)
1301                 catch {unset selected_paths($path)}
1302                 return
1303         }
1304
1305         if {$new_w ne $old_w} {
1306                 set lno [lsearch -sorted $file_lists($old_w) $path]
1307                 if {$lno >= 0} {
1308                         set file_lists($old_w) \
1309                                 [lreplace $file_lists($old_w) $lno $lno]
1310                         incr lno
1311                         $old_w conf -state normal
1312                         $old_w delete $lno.0 [expr {$lno + 1}].0
1313                         $old_w conf -state disabled
1314                 }
1315
1316                 lappend file_lists($new_w) $path
1317                 set file_lists($new_w) [lsort $file_lists($new_w)]
1318                 set lno [lsearch -sorted $file_lists($new_w) $path]
1319                 incr lno
1320                 $new_w conf -state normal
1321                 $new_w image create $lno.0 \
1322                         -align center -padx 5 -pady 1 \
1323                         -name [lindex $s 1] \
1324                         -image $new_icon
1325                 $new_w insert $lno.1 "[escape_path $path]\n"
1326                 if {[catch {set in_sel $selected_paths($path)}]} {
1327                         set in_sel 0
1328                 }
1329                 if {$in_sel} {
1330                         $new_w tag add in_sel $lno.0 [expr {$lno + 1}].0
1331                 }
1332                 $new_w conf -state disabled
1333         } elseif {$new_icon ne [mapicon $old_m $path]} {
1334                 $new_w conf -state normal
1335                 $new_w image conf [lindex $s 1] -image $new_icon
1336                 $new_w conf -state disabled
1337         }
1338 }
1339
1340 proc display_all_files {} {
1341         global ui_index ui_other
1342         global file_states file_lists
1343         global last_clicked selected_paths
1344
1345         $ui_index conf -state normal
1346         $ui_other conf -state normal
1347
1348         $ui_index delete 0.0 end
1349         $ui_other delete 0.0 end
1350         set last_clicked {}
1351
1352         set file_lists($ui_index) [list]
1353         set file_lists($ui_other) [list]
1354
1355         foreach path [lsort [array names file_states]] {
1356                 set s $file_states($path)
1357                 set m [lindex $s 0]
1358                 set w [mapcol $m $path]
1359                 lappend file_lists($w) $path
1360                 set lno [expr {[lindex [split [$w index end] .] 0] - 1}]
1361                 $w image create end \
1362                         -align center -padx 5 -pady 1 \
1363                         -name [lindex $s 1] \
1364                         -image [mapicon $m $path]
1365                 $w insert end "[escape_path $path]\n"
1366                 if {[catch {set in_sel $selected_paths($path)}]} {
1367                         set in_sel 0
1368                 }
1369                 if {$in_sel} {
1370                         $w tag add in_sel $lno.0 [expr {$lno + 1}].0
1371                 }
1372         }
1373
1374         $ui_index conf -state disabled
1375         $ui_other conf -state disabled
1376 }
1377
1378 proc update_indexinfo {msg pathList after} {
1379         global update_index_cp ui_status_value
1380
1381         if {![lock_index update]} return
1382
1383         set update_index_cp 0
1384         set pathList [lsort $pathList]
1385         set totalCnt [llength $pathList]
1386         set batch [expr {int($totalCnt * .01) + 1}]
1387         if {$batch > 25} {set batch 25}
1388
1389         set ui_status_value [format \
1390                 "$msg... %i/%i files (%.2f%%)" \
1391                 $update_index_cp \
1392                 $totalCnt \
1393                 0.0]
1394         set fd [open "| git update-index -z --index-info" w]
1395         fconfigure $fd \
1396                 -blocking 0 \
1397                 -buffering full \
1398                 -buffersize 512 \
1399                 -translation binary
1400         fileevent $fd writable [list \
1401                 write_update_indexinfo \
1402                 $fd \
1403                 $pathList \
1404                 $totalCnt \
1405                 $batch \
1406                 $msg \
1407                 $after \
1408                 ]
1409 }
1410
1411 proc write_update_indexinfo {fd pathList totalCnt batch msg after} {
1412         global update_index_cp ui_status_value
1413         global file_states current_diff
1414
1415         if {$update_index_cp >= $totalCnt} {
1416                 close $fd
1417                 unlock_index
1418                 uplevel #0 $after
1419                 return
1420         }
1421
1422         for {set i $batch} \
1423                 {$update_index_cp < $totalCnt && $i > 0} \
1424                 {incr i -1} {
1425                 set path [lindex $pathList $update_index_cp]
1426                 incr update_index_cp
1427
1428                 set s $file_states($path)
1429                 switch -glob -- [lindex $s 0] {
1430                 A? {set new _O}
1431                 M? {set new _M}
1432                 D_ {set new _D}
1433                 D? {set new _?}
1434                 ?? {continue}
1435                 }
1436                 set info [lindex $s 2]
1437                 if {$info eq {}} continue
1438
1439                 puts -nonewline $fd $info
1440                 puts -nonewline $fd "\t"
1441                 puts -nonewline $fd $path
1442                 puts -nonewline $fd "\0"
1443                 display_file $path $new
1444         }
1445
1446         set ui_status_value [format \
1447                 "$msg... %i/%i files (%.2f%%)" \
1448                 $update_index_cp \
1449                 $totalCnt \
1450                 [expr {100.0 * $update_index_cp / $totalCnt}]]
1451 }
1452
1453 proc update_index {msg pathList after} {
1454         global update_index_cp ui_status_value
1455
1456         if {![lock_index update]} return
1457
1458         set update_index_cp 0
1459         set pathList [lsort $pathList]
1460         set totalCnt [llength $pathList]
1461         set batch [expr {int($totalCnt * .01) + 1}]
1462         if {$batch > 25} {set batch 25}
1463
1464         set ui_status_value [format \
1465                 "$msg... %i/%i files (%.2f%%)" \
1466                 $update_index_cp \
1467                 $totalCnt \
1468                 0.0]
1469         set fd [open "| git update-index --add --remove -z --stdin" w]
1470         fconfigure $fd \
1471                 -blocking 0 \
1472                 -buffering full \
1473                 -buffersize 512 \
1474                 -translation binary
1475         fileevent $fd writable [list \
1476                 write_update_index \
1477                 $fd \
1478                 $pathList \
1479                 $totalCnt \
1480                 $batch \
1481                 $msg \
1482                 $after \
1483                 ]
1484 }
1485
1486 proc write_update_index {fd pathList totalCnt batch msg after} {
1487         global update_index_cp ui_status_value
1488         global file_states current_diff
1489
1490         if {$update_index_cp >= $totalCnt} {
1491                 close $fd
1492                 unlock_index
1493                 uplevel #0 $after
1494                 return
1495         }
1496
1497         for {set i $batch} \
1498                 {$update_index_cp < $totalCnt && $i > 0} \
1499                 {incr i -1} {
1500                 set path [lindex $pathList $update_index_cp]
1501                 incr update_index_cp
1502
1503                 switch -glob -- [lindex $file_states($path) 0] {
1504                 AD -
1505                 MD -
1506                 UD -
1507                 _D {set new DD}
1508
1509                 _M -
1510                 MM -
1511                 UM -
1512                 U_ -
1513                 M_ {set new M_}
1514
1515                 _O -
1516                 AM -
1517                 A_ {set new A_}
1518
1519                 ?? {continue}
1520                 }
1521
1522                 puts -nonewline $fd $path
1523                 puts -nonewline $fd "\0"
1524                 display_file $path $new
1525         }
1526
1527         set ui_status_value [format \
1528                 "$msg... %i/%i files (%.2f%%)" \
1529                 $update_index_cp \
1530                 $totalCnt \
1531                 [expr {100.0 * $update_index_cp / $totalCnt}]]
1532 }
1533
1534 proc checkout_index {msg pathList after} {
1535         global update_index_cp ui_status_value
1536
1537         if {![lock_index update]} return
1538
1539         set update_index_cp 0
1540         set pathList [lsort $pathList]
1541         set totalCnt [llength $pathList]
1542         set batch [expr {int($totalCnt * .01) + 1}]
1543         if {$batch > 25} {set batch 25}
1544
1545         set ui_status_value [format \
1546                 "$msg... %i/%i files (%.2f%%)" \
1547                 $update_index_cp \
1548                 $totalCnt \
1549                 0.0]
1550         set cmd [list git checkout-index]
1551         lappend cmd --index
1552         lappend cmd --quiet
1553         lappend cmd --force
1554         lappend cmd -z
1555         lappend cmd --stdin
1556         set fd [open "| $cmd " w]
1557         fconfigure $fd \
1558                 -blocking 0 \
1559                 -buffering full \
1560                 -buffersize 512 \
1561                 -translation binary
1562         fileevent $fd writable [list \
1563                 write_checkout_index \
1564                 $fd \
1565                 $pathList \
1566                 $totalCnt \
1567                 $batch \
1568                 $msg \
1569                 $after \
1570                 ]
1571 }
1572
1573 proc write_checkout_index {fd pathList totalCnt batch msg after} {
1574         global update_index_cp ui_status_value
1575         global file_states current_diff
1576
1577         if {$update_index_cp >= $totalCnt} {
1578                 close $fd
1579                 unlock_index
1580                 uplevel #0 $after
1581                 return
1582         }
1583
1584         for {set i $batch} \
1585                 {$update_index_cp < $totalCnt && $i > 0} \
1586                 {incr i -1} {
1587                 set path [lindex $pathList $update_index_cp]
1588                 incr update_index_cp
1589
1590                 switch -glob -- [lindex $file_states($path) 0] {
1591                 AM -
1592                 AD {set new A_}
1593                 MM -
1594                 MD {set new M_}
1595                 _M -
1596                 _D {set new __}
1597                 ?? {continue}
1598                 }
1599
1600                 puts -nonewline $fd $path
1601                 puts -nonewline $fd "\0"
1602                 display_file $path $new
1603         }
1604
1605         set ui_status_value [format \
1606                 "$msg... %i/%i files (%.2f%%)" \
1607                 $update_index_cp \
1608                 $totalCnt \
1609                 [expr {100.0 * $update_index_cp / $totalCnt}]]
1610 }
1611
1612 ######################################################################
1613 ##
1614 ## branch management
1615
1616 proc load_all_heads {} {
1617         global all_heads tracking_branches
1618
1619         set all_heads [list]
1620         set cmd [list git for-each-ref]
1621         lappend cmd --format=%(refname)
1622         lappend cmd refs/heads
1623         set fd [open "| $cmd" r]
1624         while {[gets $fd line] > 0} {
1625                 if {![catch {set info $tracking_branches($line)}]} continue
1626                 if {![regsub ^refs/heads/ $line {} name]} continue
1627                 lappend all_heads $name
1628         }
1629         close $fd
1630
1631         set all_heads [lsort $all_heads]
1632 }
1633
1634 proc populate_branch_menu {m} {
1635         global all_heads disable_on_lock
1636
1637         $m add separator
1638         foreach b $all_heads {
1639                 $m add radiobutton \
1640                         -label $b \
1641                         -command [list switch_branch $b] \
1642                         -variable current_branch \
1643                         -value $b \
1644                         -font font_ui
1645                 lappend disable_on_lock \
1646                         [list $m entryconf [$m index last] -state]
1647         }
1648 }
1649
1650 proc do_create_branch {} {
1651         error "NOT IMPLEMENTED"
1652 }
1653
1654 proc do_delete_branch {} {
1655         error "NOT IMPLEMENTED"
1656 }
1657
1658 proc switch_branch {b} {
1659         global HEAD commit_type file_states current_branch
1660         global selected_commit_type ui_comm
1661
1662         if {![lock_index switch]} return
1663
1664         # -- Backup the selected branch (repository_state resets it)
1665         #
1666         set new_branch $current_branch
1667
1668         # -- Our in memory state should match the repository.
1669         #
1670         repository_state curType curHEAD curMERGE_HEAD
1671         if {[string match amend* $commit_type]
1672                 && $curType eq {normal}
1673                 && $curHEAD eq $HEAD} {
1674         } elseif {$commit_type ne $curType || $HEAD ne $curHEAD} {
1675                 info_popup {Last scanned state does not match repository state.
1676
1677 Another Git program has modified this repository
1678 since the last scan.  A rescan must be performed
1679 before the current branch can be changed.
1680
1681 The rescan will be automatically started now.
1682 }
1683                 unlock_index
1684                 rescan {set ui_status_value {Ready.}}
1685                 return
1686         }
1687
1688         # -- Toss the message buffer if we are in amend mode.
1689         #
1690         if {[string match amend* $curType]} {
1691                 $ui_comm delete 0.0 end
1692                 $ui_comm edit reset
1693                 $ui_comm edit modified false
1694         }
1695
1696         set selected_commit_type new
1697         set current_branch $new_branch
1698
1699         unlock_index
1700         error "NOT FINISHED"
1701 }
1702
1703 ######################################################################
1704 ##
1705 ## remote management
1706
1707 proc load_all_remotes {} {
1708         global gitdir repo_config
1709         global all_remotes tracking_branches
1710
1711         set all_remotes [list]
1712         array unset tracking_branches
1713
1714         set rm_dir [file join $gitdir remotes]
1715         if {[file isdirectory $rm_dir]} {
1716                 set all_remotes [glob \
1717                         -types f \
1718                         -tails \
1719                         -nocomplain \
1720                         -directory $rm_dir *]
1721
1722                 foreach name $all_remotes {
1723                         catch {
1724                                 set fd [open [file join $rm_dir $name] r]
1725                                 while {[gets $fd line] >= 0} {
1726                                         if {![regexp {^Pull:[   ]*([^:]+):(.+)$} \
1727                                                 $line line src dst]} continue
1728                                         if {![regexp ^refs/ $dst]} {
1729                                                 set dst "refs/heads/$dst"
1730                                         }
1731                                         set tracking_branches($dst) [list $name $src]
1732                                 }
1733                                 close $fd
1734                         }
1735                 }
1736         }
1737
1738         foreach line [array names repo_config remote.*.url] {
1739                 if {![regexp ^remote\.(.*)\.url\$ $line line name]} continue
1740                 lappend all_remotes $name
1741
1742                 if {[catch {set fl $repo_config(remote.$name.fetch)}]} {
1743                         set fl {}
1744                 }
1745                 foreach line $fl {
1746                         if {![regexp {^([^:]+):(.+)$} $line line src dst]} continue
1747                         if {![regexp ^refs/ $dst]} {
1748                                 set dst "refs/heads/$dst"
1749                         }
1750                         set tracking_branches($dst) [list $name $src]
1751                 }
1752         }
1753
1754         set all_remotes [lsort -unique $all_remotes]
1755 }
1756
1757 proc populate_fetch_menu {m} {
1758         global gitdir all_remotes repo_config
1759
1760         foreach r $all_remotes {
1761                 set enable 0
1762                 if {![catch {set a $repo_config(remote.$r.url)}]} {
1763                         if {![catch {set a $repo_config(remote.$r.fetch)}]} {
1764                                 set enable 1
1765                         }
1766                 } else {
1767                         catch {
1768                                 set fd [open [file join $gitdir remotes $r] r]
1769                                 while {[gets $fd n] >= 0} {
1770                                         if {[regexp {^Pull:[ \t]*([^:]+):} $n]} {
1771                                                 set enable 1
1772                                                 break
1773                                         }
1774                                 }
1775                                 close $fd
1776                         }
1777                 }
1778
1779                 if {$enable} {
1780                         $m add command \
1781                                 -label "Fetch from $r..." \
1782                                 -command [list fetch_from $r] \
1783                                 -font font_ui
1784                 }
1785         }
1786 }
1787
1788 proc populate_push_menu {m} {
1789         global gitdir all_remotes repo_config
1790
1791         foreach r $all_remotes {
1792                 set enable 0
1793                 if {![catch {set a $repo_config(remote.$r.url)}]} {
1794                         if {![catch {set a $repo_config(remote.$r.push)}]} {
1795                                 set enable 1
1796                         }
1797                 } else {
1798                         catch {
1799                                 set fd [open [file join $gitdir remotes $r] r]
1800                                 while {[gets $fd n] >= 0} {
1801                                         if {[regexp {^Push:[ \t]*([^:]+):} $n]} {
1802                                                 set enable 1
1803                                                 break
1804                                         }
1805                                 }
1806                                 close $fd
1807                         }
1808                 }
1809
1810                 if {$enable} {
1811                         $m add command \
1812                                 -label "Push to $r..." \
1813                                 -command [list push_to $r] \
1814                                 -font font_ui
1815                 }
1816         }
1817 }
1818
1819 proc populate_pull_menu {m} {
1820         global gitdir repo_config all_remotes disable_on_lock
1821
1822         foreach remote $all_remotes {
1823                 set rb_list [list]
1824                 if {[array get repo_config remote.$remote.url] ne {}} {
1825                         if {[array get repo_config remote.$remote.fetch] ne {}} {
1826                                 foreach line $repo_config(remote.$remote.fetch) {
1827                                         if {[regexp {^([^:]+):} $line line rb]} {
1828                                                 lappend rb_list $rb
1829                                         }
1830                                 }
1831                         }
1832                 } else {
1833                         catch {
1834                                 set fd [open [file join $gitdir remotes $remote] r]
1835                                 while {[gets $fd line] >= 0} {
1836                                         if {[regexp {^Pull:[ \t]*([^:]+):} $line line rb]} {
1837                                                 lappend rb_list $rb
1838                                         }
1839                                 }
1840                                 close $fd
1841                         }
1842                 }
1843
1844                 foreach rb $rb_list {
1845                         regsub ^refs/heads/ $rb {} rb_short
1846                         $m add command \
1847                                 -label "Branch $rb_short from $remote..." \
1848                                 -command [list pull_remote $remote $rb] \
1849                                 -font font_ui
1850                         lappend disable_on_lock \
1851                                 [list $m entryconf [$m index last] -state]
1852                 }
1853         }
1854 }
1855
1856 ######################################################################
1857 ##
1858 ## icons
1859
1860 set filemask {
1861 #define mask_width 14
1862 #define mask_height 15
1863 static unsigned char mask_bits[] = {
1864    0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f,
1865    0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f,
1866    0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f};
1867 }
1868
1869 image create bitmap file_plain -background white -foreground black -data {
1870 #define plain_width 14
1871 #define plain_height 15
1872 static unsigned char plain_bits[] = {
1873    0xfe, 0x01, 0x02, 0x03, 0x02, 0x05, 0x02, 0x09, 0x02, 0x1f, 0x02, 0x10,
1874    0x02, 0x10, 0x02, 0x10, 0x02, 0x10, 0x02, 0x10, 0x02, 0x10, 0x02, 0x10,
1875    0x02, 0x10, 0x02, 0x10, 0xfe, 0x1f};
1876 } -maskdata $filemask
1877
1878 image create bitmap file_mod -background white -foreground blue -data {
1879 #define mod_width 14
1880 #define mod_height 15
1881 static unsigned char mod_bits[] = {
1882    0xfe, 0x01, 0x02, 0x03, 0x7a, 0x05, 0x02, 0x09, 0x7a, 0x1f, 0x02, 0x10,
1883    0xfa, 0x17, 0x02, 0x10, 0xfa, 0x17, 0x02, 0x10, 0xfa, 0x17, 0x02, 0x10,
1884    0xfa, 0x17, 0x02, 0x10, 0xfe, 0x1f};
1885 } -maskdata $filemask
1886
1887 image create bitmap file_fulltick -background white -foreground "#007000" -data {
1888 #define file_fulltick_width 14
1889 #define file_fulltick_height 15
1890 static unsigned char file_fulltick_bits[] = {
1891    0xfe, 0x01, 0x02, 0x1a, 0x02, 0x0c, 0x02, 0x0c, 0x02, 0x16, 0x02, 0x16,
1892    0x02, 0x13, 0x00, 0x13, 0x86, 0x11, 0x8c, 0x11, 0xd8, 0x10, 0xf2, 0x10,
1893    0x62, 0x10, 0x02, 0x10, 0xfe, 0x1f};
1894 } -maskdata $filemask
1895
1896 image create bitmap file_parttick -background white -foreground "#005050" -data {
1897 #define parttick_width 14
1898 #define parttick_height 15
1899 static unsigned char parttick_bits[] = {
1900    0xfe, 0x01, 0x02, 0x03, 0x7a, 0x05, 0x02, 0x09, 0x7a, 0x1f, 0x02, 0x10,
1901    0x7a, 0x14, 0x02, 0x16, 0x02, 0x13, 0x8a, 0x11, 0xda, 0x10, 0x72, 0x10,
1902    0x22, 0x10, 0x02, 0x10, 0xfe, 0x1f};
1903 } -maskdata $filemask
1904
1905 image create bitmap file_question -background white -foreground black -data {
1906 #define file_question_width 14
1907 #define file_question_height 15
1908 static unsigned char file_question_bits[] = {
1909    0xfe, 0x01, 0x02, 0x02, 0xe2, 0x04, 0xf2, 0x09, 0x1a, 0x1b, 0x0a, 0x13,
1910    0x82, 0x11, 0xc2, 0x10, 0x62, 0x10, 0x62, 0x10, 0x02, 0x10, 0x62, 0x10,
1911    0x62, 0x10, 0x02, 0x10, 0xfe, 0x1f};
1912 } -maskdata $filemask
1913
1914 image create bitmap file_removed -background white -foreground red -data {
1915 #define file_removed_width 14
1916 #define file_removed_height 15
1917 static unsigned char file_removed_bits[] = {
1918    0xfe, 0x01, 0x02, 0x03, 0x02, 0x05, 0x02, 0x09, 0x02, 0x1f, 0x02, 0x10,
1919    0x1a, 0x16, 0x32, 0x13, 0xe2, 0x11, 0xc2, 0x10, 0xe2, 0x11, 0x32, 0x13,
1920    0x1a, 0x16, 0x02, 0x10, 0xfe, 0x1f};
1921 } -maskdata $filemask
1922
1923 image create bitmap file_merge -background white -foreground blue -data {
1924 #define file_merge_width 14
1925 #define file_merge_height 15
1926 static unsigned char file_merge_bits[] = {
1927    0xfe, 0x01, 0x02, 0x03, 0x62, 0x05, 0x62, 0x09, 0x62, 0x1f, 0x62, 0x10,
1928    0xfa, 0x11, 0xf2, 0x10, 0x62, 0x10, 0x02, 0x10, 0xfa, 0x17, 0x02, 0x10,
1929    0xfa, 0x17, 0x02, 0x10, 0xfe, 0x1f};
1930 } -maskdata $filemask
1931
1932 set ui_index .vpane.files.index.list
1933 set ui_other .vpane.files.other.list
1934 set max_status_desc 0
1935 foreach i {
1936                 {__ i plain    "Unmodified"}
1937                 {_M i mod      "Modified"}
1938                 {M_ i fulltick "Included in commit"}
1939                 {MM i parttick "Partially included"}
1940                 {MD i question "Included (but gone)"}
1941
1942                 {_O o plain    "Untracked"}
1943                 {A_ o fulltick "Added by commit"}
1944                 {AM o parttick "Partially added"}
1945                 {AD o question "Added (but gone)"}
1946
1947                 {_D i question "Missing"}
1948                 {DD i removed  "Removed by commit"}
1949                 {D_ i removed  "Removed by commit"}
1950                 {DO i removed  "Removed (still exists)"}
1951                 {DM i removed  "Removed (but modified)"}
1952
1953                 {UD i merge    "Merge conflicts"}
1954                 {UM i merge    "Merge conflicts"}
1955                 {U_ i merge    "Merge conflicts"}
1956         } {
1957         if {$max_status_desc < [string length [lindex $i 3]]} {
1958                 set max_status_desc [string length [lindex $i 3]]
1959         }
1960         if {[lindex $i 1] eq {i}} {
1961                 set all_cols([lindex $i 0]) $ui_index
1962         } else {
1963                 set all_cols([lindex $i 0]) $ui_other
1964         }
1965         set all_icons([lindex $i 0]) file_[lindex $i 2]
1966         set all_descs([lindex $i 0]) [lindex $i 3]
1967 }
1968 unset filemask i
1969
1970 ######################################################################
1971 ##
1972 ## util
1973
1974 proc is_MacOSX {} {
1975         global tcl_platform tk_library
1976         if {[tk windowingsystem] eq {aqua}} {
1977                 return 1
1978         }
1979         return 0
1980 }
1981
1982 proc is_Windows {} {
1983         global tcl_platform
1984         if {$tcl_platform(platform) eq {windows}} {
1985                 return 1
1986         }
1987         return 0
1988 }
1989
1990 proc bind_button3 {w cmd} {
1991         bind $w <Any-Button-3> $cmd
1992         if {[is_MacOSX]} {
1993                 bind $w <Control-Button-1> $cmd
1994         }
1995 }
1996
1997 proc incr_font_size {font {amt 1}} {
1998         set sz [font configure $font -size]
1999         incr sz $amt
2000         font configure $font -size $sz
2001         font configure ${font}bold -size $sz
2002 }
2003
2004 proc hook_failed_popup {hook msg} {
2005         global gitdir appname
2006
2007         set w .hookfail
2008         toplevel $w
2009
2010         frame $w.m
2011         label $w.m.l1 -text "$hook hook failed:" \
2012                 -anchor w \
2013                 -justify left \
2014                 -font font_uibold
2015         text $w.m.t \
2016                 -background white -borderwidth 1 \
2017                 -relief sunken \
2018                 -width 80 -height 10 \
2019                 -font font_diff \
2020                 -yscrollcommand [list $w.m.sby set]
2021         label $w.m.l2 \
2022                 -text {You must correct the above errors before committing.} \
2023                 -anchor w \
2024                 -justify left \
2025                 -font font_uibold
2026         scrollbar $w.m.sby -command [list $w.m.t yview]
2027         pack $w.m.l1 -side top -fill x
2028         pack $w.m.l2 -side bottom -fill x
2029         pack $w.m.sby -side right -fill y
2030         pack $w.m.t -side left -fill both -expand 1
2031         pack $w.m -side top -fill both -expand 1 -padx 5 -pady 10
2032
2033         $w.m.t insert 1.0 $msg
2034         $w.m.t conf -state disabled
2035
2036         button $w.ok -text OK \
2037                 -width 15 \
2038                 -font font_ui \
2039                 -command "destroy $w"
2040         pack $w.ok -side bottom -anchor e -pady 10 -padx 10
2041
2042         bind $w <Visibility> "grab $w; focus $w"
2043         bind $w <Key-Return> "destroy $w"
2044         wm title $w "$appname ([lindex [file split \
2045                 [file normalize [file dirname $gitdir]]] \
2046                 end]): error"
2047         tkwait window $w
2048 }
2049
2050 set next_console_id 0
2051
2052 proc new_console {short_title long_title} {
2053         global next_console_id console_data
2054         set w .console[incr next_console_id]
2055         set console_data($w) [list $short_title $long_title]
2056         return [console_init $w]
2057 }
2058
2059 proc console_init {w} {
2060         global console_cr console_data
2061         global gitdir appname M1B
2062
2063         set console_cr($w) 1.0
2064         toplevel $w
2065         frame $w.m
2066         label $w.m.l1 -text "[lindex $console_data($w) 1]:" \
2067                 -anchor w \
2068                 -justify left \
2069                 -font font_uibold
2070         text $w.m.t \
2071                 -background white -borderwidth 1 \
2072                 -relief sunken \
2073                 -width 80 -height 10 \
2074                 -font font_diff \
2075                 -state disabled \
2076                 -yscrollcommand [list $w.m.sby set]
2077         label $w.m.s -text {Working... please wait...} \
2078                 -anchor w \
2079                 -justify left \
2080                 -font font_uibold
2081         scrollbar $w.m.sby -command [list $w.m.t yview]
2082         pack $w.m.l1 -side top -fill x
2083         pack $w.m.s -side bottom -fill x
2084         pack $w.m.sby -side right -fill y
2085         pack $w.m.t -side left -fill both -expand 1
2086         pack $w.m -side top -fill both -expand 1 -padx 5 -pady 10
2087
2088         menu $w.ctxm -tearoff 0
2089         $w.ctxm add command -label "Copy" \
2090                 -font font_ui \
2091                 -command "tk_textCopy $w.m.t"
2092         $w.ctxm add command -label "Select All" \
2093                 -font font_ui \
2094                 -command "$w.m.t tag add sel 0.0 end"
2095         $w.ctxm add command -label "Copy All" \
2096                 -font font_ui \
2097                 -command "
2098                         $w.m.t tag add sel 0.0 end
2099                         tk_textCopy $w.m.t
2100                         $w.m.t tag remove sel 0.0 end
2101                 "
2102
2103         button $w.ok -text {Close} \
2104                 -font font_ui \
2105                 -state disabled \
2106                 -command "destroy $w"
2107         pack $w.ok -side bottom -anchor e -pady 10 -padx 10
2108
2109         bind_button3 $w.m.t "tk_popup $w.ctxm %X %Y"
2110         bind $w.m.t <$M1B-Key-a> "$w.m.t tag add sel 0.0 end;break"
2111         bind $w.m.t <$M1B-Key-A> "$w.m.t tag add sel 0.0 end;break"
2112         bind $w <Visibility> "focus $w"
2113         wm title $w "$appname ([lindex [file split \
2114                 [file normalize [file dirname $gitdir]]] \
2115                 end]): [lindex $console_data($w) 0]"
2116         return $w
2117 }
2118
2119 proc console_exec {w cmd {after {}}} {
2120         # -- Windows tosses the enviroment when we exec our child.
2121         #    But most users need that so we have to relogin. :-(
2122         #
2123         if {[is_Windows]} {
2124                 set cmd [list sh --login -c "cd \"[pwd]\" && [join $cmd { }]"]
2125         }
2126
2127         # -- Tcl won't let us redirect both stdout and stderr to
2128         #    the same pipe.  So pass it through cat...
2129         #
2130         set cmd [concat | $cmd |& cat]
2131
2132         set fd_f [open $cmd r]
2133         fconfigure $fd_f -blocking 0 -translation binary
2134         fileevent $fd_f readable [list console_read $w $fd_f $after]
2135 }
2136
2137 proc console_read {w fd after} {
2138         global console_cr console_data
2139
2140         set buf [read $fd]
2141         if {$buf ne {}} {
2142                 if {![winfo exists $w]} {console_init $w}
2143                 $w.m.t conf -state normal
2144                 set c 0
2145                 set n [string length $buf]
2146                 while {$c < $n} {
2147                         set cr [string first "\r" $buf $c]
2148                         set lf [string first "\n" $buf $c]
2149                         if {$cr < 0} {set cr [expr {$n + 1}]}
2150                         if {$lf < 0} {set lf [expr {$n + 1}]}
2151
2152                         if {$lf < $cr} {
2153                                 $w.m.t insert end [string range $buf $c $lf]
2154                                 set console_cr($w) [$w.m.t index {end -1c}]
2155                                 set c $lf
2156                                 incr c
2157                         } else {
2158                                 $w.m.t delete $console_cr($w) end
2159                                 $w.m.t insert end "\n"
2160                                 $w.m.t insert end [string range $buf $c $cr]
2161                                 set c $cr
2162                                 incr c
2163                         }
2164                 }
2165                 $w.m.t conf -state disabled
2166                 $w.m.t see end
2167         }
2168
2169         fconfigure $fd -blocking 1
2170         if {[eof $fd]} {
2171                 if {[catch {close $fd}]} {
2172                         if {![winfo exists $w]} {console_init $w}
2173                         $w.m.s conf -background red -text {Error: Command Failed}
2174                         $w.ok conf -state normal
2175                         set ok 0
2176                 } elseif {[winfo exists $w]} {
2177                         $w.m.s conf -background green -text {Success}
2178                         $w.ok conf -state normal
2179                         set ok 1
2180                 }
2181                 array unset console_cr $w
2182                 array unset console_data $w
2183                 if {$after ne {}} {
2184                         uplevel #0 $after $ok
2185                 }
2186                 return
2187         }
2188         fconfigure $fd -blocking 0
2189 }
2190
2191 ######################################################################
2192 ##
2193 ## ui commands
2194
2195 set starting_gitk_msg {Please wait... Starting gitk...}
2196
2197 proc do_gitk {revs} {
2198         global ui_status_value starting_gitk_msg
2199
2200         set cmd gitk
2201         if {$revs ne {}} {
2202                 append cmd { }
2203                 append cmd $revs
2204         }
2205         if {[is_Windows]} {
2206                 set cmd "sh -c \"exec $cmd\""
2207         }
2208         append cmd { &}
2209
2210         if {[catch {eval exec $cmd} err]} {
2211                 error_popup "Failed to start gitk:\n\n$err"
2212         } else {
2213                 set ui_status_value $starting_gitk_msg
2214                 after 10000 {
2215                         if {$ui_status_value eq $starting_gitk_msg} {
2216                                 set ui_status_value {Ready.}
2217                         }
2218                 }
2219         }
2220 }
2221
2222 proc do_gc {} {
2223         set w [new_console {gc} {Compressing the object database}]
2224         console_exec $w {git gc}
2225 }
2226
2227 proc do_fsck_objects {} {
2228         set w [new_console {fsck-objects} \
2229                 {Verifying the object database with fsck-objects}]
2230         set cmd [list git fsck-objects]
2231         lappend cmd --full
2232         lappend cmd --cache
2233         lappend cmd --strict
2234         console_exec $w $cmd
2235 }
2236
2237 set is_quitting 0
2238
2239 proc do_quit {} {
2240         global gitdir ui_comm is_quitting repo_config commit_type
2241
2242         if {$is_quitting} return
2243         set is_quitting 1
2244
2245         # -- Stash our current commit buffer.
2246         #
2247         set save [file join $gitdir GITGUI_MSG]
2248         set msg [string trim [$ui_comm get 0.0 end]]
2249         if {![string match amend* $commit_type]
2250                 && [$ui_comm edit modified]
2251                 && $msg ne {}} {
2252                 catch {
2253                         set fd [open $save w]
2254                         puts $fd [string trim [$ui_comm get 0.0 end]]
2255                         close $fd
2256                 }
2257         } else {
2258                 catch {file delete $save}
2259         }
2260
2261         # -- Stash our current window geometry into this repository.
2262         #
2263         set cfg_geometry [list]
2264         lappend cfg_geometry [wm geometry .]
2265         lappend cfg_geometry [lindex [.vpane sash coord 0] 1]
2266         lappend cfg_geometry [lindex [.vpane.files sash coord 0] 0]
2267         if {[catch {set rc_geometry $repo_config(gui.geometry)}]} {
2268                 set rc_geometry {}
2269         }
2270         if {$cfg_geometry ne $rc_geometry} {
2271                 catch {exec git repo-config gui.geometry $cfg_geometry}
2272         }
2273
2274         destroy .
2275 }
2276
2277 proc do_rescan {} {
2278         rescan {set ui_status_value {Ready.}}
2279 }
2280
2281 proc remove_helper {txt paths} {
2282         global file_states current_diff
2283
2284         if {![lock_index begin-update]} return
2285
2286         set pathList [list]
2287         set after {}
2288         foreach path $paths {
2289                 switch -glob -- [lindex $file_states($path) 0] {
2290                 A? -
2291                 M? -
2292                 D? {
2293                         lappend pathList $path
2294                         if {$path eq $current_diff} {
2295                                 set after {reshow_diff;}
2296                         }
2297                 }
2298                 }
2299         }
2300         if {$pathList eq {}} {
2301                 unlock_index
2302         } else {
2303                 update_indexinfo \
2304                         $txt \
2305                         $pathList \
2306                         [concat $after {set ui_status_value {Ready.}}]
2307         }
2308 }
2309
2310 proc do_remove_selection {} {
2311         global current_diff selected_paths
2312
2313         if {[array size selected_paths] > 0} {
2314                 remove_helper \
2315                         {Removing selected files from commit} \
2316                         [array names selected_paths]
2317         } elseif {$current_diff ne {}} {
2318                 remove_helper \
2319                         "Removing [short_path $current_diff] from commit" \
2320                         [list $current_diff]
2321         }
2322 }
2323
2324 proc include_helper {txt paths} {
2325         global file_states current_diff
2326
2327         if {![lock_index begin-update]} return
2328
2329         set pathList [list]
2330         set after {}
2331         foreach path $paths {
2332                 switch -glob -- [lindex $file_states($path) 0] {
2333                 AM -
2334                 AD -
2335                 MM -
2336                 MD -
2337                 U? -
2338                 _M -
2339                 _D -
2340                 _O {
2341                         lappend pathList $path
2342                         if {$path eq $current_diff} {
2343                                 set after {reshow_diff;}
2344                         }
2345                 }
2346                 }
2347         }
2348         if {$pathList eq {}} {
2349                 unlock_index
2350         } else {
2351                 update_index \
2352                         $txt \
2353                         $pathList \
2354                         [concat $after {set ui_status_value {Ready to commit.}}]
2355         }
2356 }
2357
2358 proc do_include_selection {} {
2359         global current_diff selected_paths
2360
2361         if {[array size selected_paths] > 0} {
2362                 include_helper \
2363                         {Including selected files} \
2364                         [array names selected_paths]
2365         } elseif {$current_diff ne {}} {
2366                 include_helper \
2367                         "Including [short_path $current_diff]" \
2368                         [list $current_diff]
2369         }
2370 }
2371
2372 proc do_include_all {} {
2373         global file_states
2374
2375         set paths [list]
2376         foreach path [array names file_states] {
2377                 switch -- [lindex $file_states($path) 0] {
2378                 AM -
2379                 AD -
2380                 MM -
2381                 MD -
2382                 _M -
2383                 _D {lappend paths $path}
2384                 }
2385         }
2386         include_helper \
2387                 {Including all modified files} \
2388                 $paths
2389 }
2390
2391 proc revert_helper {txt paths} {
2392         global gitdir appname
2393         global file_states current_diff
2394
2395         if {![lock_index begin-update]} return
2396
2397         set pathList [list]
2398         set after {}
2399         foreach path $paths {
2400                 switch -glob -- [lindex $file_states($path) 0] {
2401                 AM -
2402                 AD -
2403                 MM -
2404                 MD -
2405                 _M -
2406                 _D {
2407                         lappend pathList $path
2408                         if {$path eq $current_diff} {
2409                                 set after {reshow_diff;}
2410                         }
2411                 }
2412                 }
2413         }
2414
2415         set n [llength $pathList]
2416         if {$n == 0} {
2417                 unlock_index
2418                 return
2419         } elseif {$n == 1} {
2420                 set s "[short_path [lindex $pathList]]"
2421         } else {
2422                 set s "these $n files"
2423         }
2424
2425         set reponame [lindex [file split \
2426                 [file normalize [file dirname $gitdir]]] \
2427                 end]
2428
2429         set reply [tk_dialog \
2430                 .confirm_revert \
2431                 "$appname ($reponame)" \
2432                 "Revert unincluded changes in $s?
2433
2434 Any unincluded changes will be permanently lost by the revert." \
2435                 question \
2436                 1 \
2437                 {Do Nothing} \
2438                 {Revert Changes} \
2439                 ]
2440         if {$reply == 1} {
2441                 checkout_index \
2442                         $txt \
2443                         $pathList \
2444                         [concat $after {set ui_status_value {Ready.}}]
2445         } else {
2446                 unlock_index
2447         }
2448 }
2449
2450 proc do_revert_selection {} {
2451         global current_diff selected_paths
2452
2453         if {[array size selected_paths] > 0} {
2454                 revert_helper \
2455                         {Reverting selected files} \
2456                         [array names selected_paths]
2457         } elseif {$current_diff ne {}} {
2458                 revert_helper \
2459                         "Reverting [short_path $current_diff]" \
2460                         [list $current_diff]
2461         }
2462 }
2463
2464 proc do_signoff {} {
2465         global ui_comm
2466
2467         set me [committer_ident]
2468         if {$me eq {}} return
2469
2470         set sob "Signed-off-by: $me"
2471         set last [$ui_comm get {end -1c linestart} {end -1c}]
2472         if {$last ne $sob} {
2473                 $ui_comm edit separator
2474                 if {$last ne {}
2475                         && ![regexp {^[A-Z][A-Za-z]*-[A-Za-z-]+: *} $last]} {
2476                         $ui_comm insert end "\n"
2477                 }
2478                 $ui_comm insert end "\n$sob"
2479                 $ui_comm edit separator
2480                 $ui_comm see end
2481         }
2482 }
2483
2484 proc do_select_commit_type {} {
2485         global commit_type selected_commit_type
2486
2487         if {$selected_commit_type eq {new}
2488                 && [string match amend* $commit_type]} {
2489                 create_new_commit
2490         } elseif {$selected_commit_type eq {amend}
2491                 && ![string match amend* $commit_type]} {
2492                 load_last_commit
2493
2494                 # The amend request was rejected...
2495                 #
2496                 if {![string match amend* $commit_type]} {
2497                         set selected_commit_type new
2498                 }
2499         }
2500 }
2501
2502 proc do_commit {} {
2503         commit_tree
2504 }
2505
2506 proc do_about {} {
2507         global appname copyright
2508         global tcl_patchLevel tk_patchLevel
2509
2510         set w .about_dialog
2511         toplevel $w
2512         wm geometry $w "+[winfo rootx .]+[winfo rooty .]"
2513
2514         label $w.header -text "About $appname" \
2515                 -font font_uibold
2516         pack $w.header -side top -fill x
2517
2518         frame $w.buttons
2519         button $w.buttons.close -text {Close} \
2520                 -font font_ui \
2521                 -command [list destroy $w]
2522         pack $w.buttons.close -side right
2523         pack $w.buttons -side bottom -fill x -pady 10 -padx 10
2524
2525         label $w.desc \
2526                 -text "$appname - a commit creation tool for Git.
2527 $copyright" \
2528                 -padx 5 -pady 5 \
2529                 -justify left \
2530                 -anchor w \
2531                 -borderwidth 1 \
2532                 -relief solid \
2533                 -font font_ui
2534         pack $w.desc -side top -fill x -padx 5 -pady 5
2535
2536         set v [exec git --version]
2537         append v "\n\n"
2538         if {$tcl_patchLevel eq $tk_patchLevel} {
2539                 append v "Tcl/Tk version $tcl_patchLevel"
2540         } else {
2541                 append v "Tcl version $tcl_patchLevel"
2542                 append v ", Tk version $tk_patchLevel"
2543         }
2544
2545         label $w.vers \
2546                 -text $v \
2547                 -padx 5 -pady 5 \
2548                 -justify left \
2549                 -anchor w \
2550                 -borderwidth 1 \
2551                 -relief solid \
2552                 -font font_ui
2553         pack $w.vers -side top -fill x -padx 5 -pady 5
2554
2555         bind $w <Visibility> "grab $w; focus $w"
2556         bind $w <Key-Escape> "destroy $w"
2557         wm title $w "About $appname"
2558         tkwait window $w
2559 }
2560
2561 proc do_options {} {
2562         global appname gitdir font_descs
2563         global repo_config global_config
2564         global repo_config_new global_config_new
2565
2566         array unset repo_config_new
2567         array unset global_config_new
2568         foreach name [array names repo_config] {
2569                 set repo_config_new($name) $repo_config($name)
2570         }
2571         load_config 1
2572         foreach name [array names repo_config] {
2573                 switch -- $name {
2574                 gui.diffcontext {continue}
2575                 }
2576                 set repo_config_new($name) $repo_config($name)
2577         }
2578         foreach name [array names global_config] {
2579                 set global_config_new($name) $global_config($name)
2580         }
2581         set reponame [lindex [file split \
2582                 [file normalize [file dirname $gitdir]]] \
2583                 end]
2584
2585         set w .options_editor
2586         toplevel $w
2587         wm geometry $w "+[winfo rootx .]+[winfo rooty .]"
2588
2589         label $w.header -text "$appname Options" \
2590                 -font font_uibold
2591         pack $w.header -side top -fill x
2592
2593         frame $w.buttons
2594         button $w.buttons.restore -text {Restore Defaults} \
2595                 -font font_ui \
2596                 -command do_restore_defaults
2597         pack $w.buttons.restore -side left
2598         button $w.buttons.save -text Save \
2599                 -font font_ui \
2600                 -command [list do_save_config $w]
2601         pack $w.buttons.save -side right
2602         button $w.buttons.cancel -text {Cancel} \
2603                 -font font_ui \
2604                 -command [list destroy $w]
2605         pack $w.buttons.cancel -side right
2606         pack $w.buttons -side bottom -fill x -pady 10 -padx 10
2607
2608         labelframe $w.repo -text "$reponame Repository" \
2609                 -font font_ui \
2610                 -relief raised -borderwidth 2
2611         labelframe $w.global -text {Global (All Repositories)} \
2612                 -font font_ui \
2613                 -relief raised -borderwidth 2
2614         pack $w.repo -side left -fill both -expand 1 -pady 5 -padx 5
2615         pack $w.global -side right -fill both -expand 1 -pady 5 -padx 5
2616
2617         foreach option {
2618                 {b partialinclude {Allow Partially Included Files}}
2619                 {b pullsummary {Show Pull Summary}}
2620                 {b trustmtime  {Trust File Modification Timestamps}}
2621                 {i diffcontext {Number of Diff Context Lines}}
2622                 } {
2623                 set type [lindex $option 0]
2624                 set name [lindex $option 1]
2625                 set text [lindex $option 2]
2626                 foreach f {repo global} {
2627                         switch $type {
2628                         b {
2629                                 checkbutton $w.$f.$name -text $text \
2630                                         -variable ${f}_config_new(gui.$name) \
2631                                         -onvalue true \
2632                                         -offvalue false \
2633                                         -font font_ui
2634                                 pack $w.$f.$name -side top -anchor w
2635                         }
2636                         i {
2637                                 frame $w.$f.$name
2638                                 label $w.$f.$name.l -text "$text:" -font font_ui
2639                                 pack $w.$f.$name.l -side left -anchor w -fill x
2640                                 spinbox $w.$f.$name.v \
2641                                         -textvariable ${f}_config_new(gui.$name) \
2642                                         -from 1 -to 99 -increment 1 \
2643                                         -width 3 \
2644                                         -font font_ui
2645                                 pack $w.$f.$name.v -side right -anchor e
2646                                 pack $w.$f.$name -side top -anchor w -fill x
2647                         }
2648                         }
2649                 }
2650         }
2651
2652         set all_fonts [lsort [font families]]
2653         foreach option $font_descs {
2654                 set name [lindex $option 0]
2655                 set font [lindex $option 1]
2656                 set text [lindex $option 2]
2657
2658                 set global_config_new(gui.$font^^family) \
2659                         [font configure $font -family]
2660                 set global_config_new(gui.$font^^size) \
2661                         [font configure $font -size]
2662
2663                 frame $w.global.$name
2664                 label $w.global.$name.l -text "$text:" -font font_ui
2665                 pack $w.global.$name.l -side left -anchor w -fill x
2666                 eval tk_optionMenu $w.global.$name.family \
2667                         global_config_new(gui.$font^^family) \
2668                         $all_fonts
2669                 spinbox $w.global.$name.size \
2670                         -textvariable global_config_new(gui.$font^^size) \
2671                         -from 2 -to 80 -increment 1 \
2672                         -width 3 \
2673                         -font font_ui
2674                 pack $w.global.$name.size -side right -anchor e
2675                 pack $w.global.$name.family -side right -anchor e
2676                 pack $w.global.$name -side top -anchor w -fill x
2677         }
2678
2679         bind $w <Visibility> "grab $w; focus $w"
2680         bind $w <Key-Escape> "destroy $w"
2681         wm title $w "$appname ($reponame): Options"
2682         tkwait window $w
2683 }
2684
2685 proc do_restore_defaults {} {
2686         global font_descs default_config repo_config
2687         global repo_config_new global_config_new
2688
2689         foreach name [array names default_config] {
2690                 set repo_config_new($name) $default_config($name)
2691                 set global_config_new($name) $default_config($name)
2692         }
2693
2694         foreach option $font_descs {
2695                 set name [lindex $option 0]
2696                 set repo_config(gui.$name) $default_config(gui.$name)
2697         }
2698         apply_config
2699
2700         foreach option $font_descs {
2701                 set name [lindex $option 0]
2702                 set font [lindex $option 1]
2703                 set global_config_new(gui.$font^^family) \
2704                         [font configure $font -family]
2705                 set global_config_new(gui.$font^^size) \
2706                         [font configure $font -size]
2707         }
2708 }
2709
2710 proc do_save_config {w} {
2711         if {[catch {save_config} err]} {
2712                 error_popup "Failed to completely save options:\n\n$err"
2713         }
2714         reshow_diff
2715         destroy $w
2716 }
2717
2718 proc do_windows_shortcut {} {
2719         global gitdir appname argv0
2720
2721         set reponame [lindex [file split \
2722                 [file normalize [file dirname $gitdir]]] \
2723                 end]
2724
2725         if {[catch {
2726                 set desktop [exec cygpath \
2727                         --windows \
2728                         --absolute \
2729                         --long-name \
2730                         --desktop]
2731                 }]} {
2732                         set desktop .
2733         }
2734         set fn [tk_getSaveFile \
2735                 -parent . \
2736                 -title "$appname ($reponame): Create Desktop Icon" \
2737                 -initialdir $desktop \
2738                 -initialfile "Git $reponame.bat"]
2739         if {$fn != {}} {
2740                 if {[catch {
2741                                 set fd [open $fn w]
2742                                 set sh [exec cygpath \
2743                                         --windows \
2744                                         --absolute \
2745                                         --long-name \
2746                                         /bin/sh]
2747                                 set me [exec cygpath \
2748                                         --unix \
2749                                         --absolute \
2750                                         $argv0]
2751                                 set gd [exec cygpath \
2752                                         --unix \
2753                                         --absolute \
2754                                         $gitdir]
2755                                 regsub -all ' $me "'\\''" me
2756                                 regsub -all ' $gd "'\\''" gd
2757                                 puts -nonewline $fd "\"$sh\" --login -c \""
2758                                 puts -nonewline $fd "GIT_DIR='$gd'"
2759                                 puts -nonewline $fd " '$me'"
2760                                 puts $fd "&\""
2761                                 close $fd
2762                         } err]} {
2763                         error_popup "Cannot write script:\n\n$err"
2764                 }
2765         }
2766 }
2767
2768 proc do_macosx_app {} {
2769         global gitdir appname argv0 env
2770
2771         set reponame [lindex [file split \
2772                 [file normalize [file dirname $gitdir]]] \
2773                 end]
2774
2775         set fn [tk_getSaveFile \
2776                 -parent . \
2777                 -title "$appname ($reponame): Create Desktop Icon" \
2778                 -initialdir [file join $env(HOME) Desktop] \
2779                 -initialfile "Git $reponame.app"]
2780         if {$fn != {}} {
2781                 if {[catch {
2782                                 set Contents [file join $fn Contents]
2783                                 set MacOS [file join $Contents MacOS]
2784                                 set exe [file join $MacOS git-gui]
2785
2786                                 file mkdir $MacOS
2787
2788                                 set fd [open [file join $Contents Info.plist] w]
2789                                 puts $fd {<?xml version="1.0" encoding="UTF-8"?>
2790 <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
2791 <plist version="1.0">
2792 <dict>
2793         <key>CFBundleDevelopmentRegion</key>
2794         <string>English</string>
2795         <key>CFBundleExecutable</key>
2796         <string>git-gui</string>
2797         <key>CFBundleIdentifier</key>
2798         <string>org.spearce.git-gui</string>
2799         <key>CFBundleInfoDictionaryVersion</key>
2800         <string>6.0</string>
2801         <key>CFBundlePackageType</key>
2802         <string>APPL</string>
2803         <key>CFBundleSignature</key>
2804         <string>????</string>
2805         <key>CFBundleVersion</key>
2806         <string>1.0</string>
2807         <key>NSPrincipalClass</key>
2808         <string>NSApplication</string>
2809 </dict>
2810 </plist>}
2811                                 close $fd
2812
2813                                 set fd [open $exe w]
2814                                 set gd [file normalize $gitdir]
2815                                 set ep [file normalize [exec git --exec-path]]
2816                                 regsub -all ' $gd "'\\''" gd
2817                                 regsub -all ' $ep "'\\''" ep
2818                                 puts $fd "#!/bin/sh"
2819                                 foreach name [array names env] {
2820                                         if {[string match GIT_* $name]} {
2821                                                 regsub -all ' $env($name) "'\\''" v
2822                                                 puts $fd "export $name='$v'"
2823                                         }
2824                                 }
2825                                 puts $fd "export PATH='$ep':\$PATH"
2826                                 puts $fd "export GIT_DIR='$gd'"
2827                                 puts $fd "exec [file normalize $argv0]"
2828                                 close $fd
2829
2830                                 file attributes $exe -permissions u+x,g+x,o+x
2831                         } err]} {
2832                         error_popup "Cannot write icon:\n\n$err"
2833                 }
2834         }
2835 }
2836
2837 proc toggle_or_diff {w x y} {
2838         global file_states file_lists current_diff ui_index ui_other
2839         global last_clicked selected_paths
2840
2841         set pos [split [$w index @$x,$y] .]
2842         set lno [lindex $pos 0]
2843         set col [lindex $pos 1]
2844         set path [lindex $file_lists($w) [expr {$lno - 1}]]
2845         if {$path eq {}} {
2846                 set last_clicked {}
2847                 return
2848         }
2849
2850         set last_clicked [list $w $lno]
2851         array unset selected_paths
2852         $ui_index tag remove in_sel 0.0 end
2853         $ui_other tag remove in_sel 0.0 end
2854
2855         if {$col == 0} {
2856                 if {$current_diff eq $path} {
2857                         set after {reshow_diff;}
2858                 } else {
2859                         set after {}
2860                 }
2861                 switch -glob -- [lindex $file_states($path) 0] {
2862                 A_ -
2863                 M_ -
2864                 DD -
2865                 DO -
2866                 DM {
2867                         update_indexinfo \
2868                                 "Removing [short_path $path] from commit" \
2869                                 [list $path] \
2870                                 [concat $after {set ui_status_value {Ready.}}]
2871                 }
2872                 ?? {
2873                         update_index \
2874                                 "Including [short_path $path]" \
2875                                 [list $path] \
2876                                 [concat $after {set ui_status_value {Ready.}}]
2877                 }
2878                 }
2879         } else {
2880                 show_diff $path $w $lno
2881         }
2882 }
2883
2884 proc add_one_to_selection {w x y} {
2885         global file_lists
2886         global last_clicked selected_paths
2887
2888         set pos [split [$w index @$x,$y] .]
2889         set lno [lindex $pos 0]
2890         set col [lindex $pos 1]
2891         set path [lindex $file_lists($w) [expr {$lno - 1}]]
2892         if {$path eq {}} {
2893                 set last_clicked {}
2894                 return
2895         }
2896
2897         set last_clicked [list $w $lno]
2898         if {[catch {set in_sel $selected_paths($path)}]} {
2899                 set in_sel 0
2900         }
2901         if {$in_sel} {
2902                 unset selected_paths($path)
2903                 $w tag remove in_sel $lno.0 [expr {$lno + 1}].0
2904         } else {
2905                 set selected_paths($path) 1
2906                 $w tag add in_sel $lno.0 [expr {$lno + 1}].0
2907         }
2908 }
2909
2910 proc add_range_to_selection {w x y} {
2911         global file_lists
2912         global last_clicked selected_paths
2913
2914         if {[lindex $last_clicked 0] ne $w} {
2915                 toggle_or_diff $w $x $y
2916                 return
2917         }
2918
2919         set pos [split [$w index @$x,$y] .]
2920         set lno [lindex $pos 0]
2921         set lc [lindex $last_clicked 1]
2922         if {$lc < $lno} {
2923                 set begin $lc
2924                 set end $lno
2925         } else {
2926                 set begin $lno
2927                 set end $lc
2928         }
2929
2930         foreach path [lrange $file_lists($w) \
2931                 [expr {$begin - 1}] \
2932                 [expr {$end - 1}]] {
2933                 set selected_paths($path) 1
2934         }
2935         $w tag add in_sel $begin.0 [expr {$end + 1}].0
2936 }
2937
2938 ######################################################################
2939 ##
2940 ## config defaults
2941
2942 set cursor_ptr arrow
2943 font create font_diff -family Courier -size 10
2944 font create font_ui
2945 catch {
2946         label .dummy
2947         eval font configure font_ui [font actual [.dummy cget -font]]
2948         destroy .dummy
2949 }
2950
2951 font create font_uibold
2952 font create font_diffbold
2953
2954 if {[is_Windows]} {
2955         set M1B Control
2956         set M1T Ctrl
2957 } elseif {[is_MacOSX]} {
2958         set M1B M1
2959         set M1T Cmd
2960 } else {
2961         set M1B M1
2962         set M1T M1
2963 }
2964
2965 proc apply_config {} {
2966         global repo_config font_descs
2967
2968         foreach option $font_descs {
2969                 set name [lindex $option 0]
2970                 set font [lindex $option 1]
2971                 if {[catch {
2972                         foreach {cn cv} $repo_config(gui.$name) {
2973                                 font configure $font $cn $cv
2974                         }
2975                         } err]} {
2976                         error_popup "Invalid font specified in gui.$name:\n\n$err"
2977                 }
2978                 foreach {cn cv} [font configure $font] {
2979                         font configure ${font}bold $cn $cv
2980                 }
2981                 font configure ${font}bold -weight bold
2982         }
2983 }
2984
2985 set default_config(gui.trustmtime) false
2986 set default_config(gui.pullsummary) true
2987 set default_config(gui.partialinclude) false
2988 set default_config(gui.diffcontext) 5
2989 set default_config(gui.fontui) [font configure font_ui]
2990 set default_config(gui.fontdiff) [font configure font_diff]
2991 set font_descs {
2992         {fontui   font_ui   {Main Font}}
2993         {fontdiff font_diff {Diff/Console Font}}
2994 }
2995 load_config 0
2996 apply_config
2997
2998 ######################################################################
2999 ##
3000 ## ui construction
3001
3002 # -- Menu Bar
3003 #
3004 menu .mbar -tearoff 0
3005 .mbar add cascade -label Repository -menu .mbar.repository
3006 .mbar add cascade -label Edit -menu .mbar.edit
3007 if {!$single_commit} {
3008         .mbar add cascade -label Branch -menu .mbar.branch
3009 }
3010 .mbar add cascade -label Commit -menu .mbar.commit
3011 if {!$single_commit} {
3012         .mbar add cascade -label Fetch -menu .mbar.fetch
3013         .mbar add cascade -label Pull -menu .mbar.pull
3014         .mbar add cascade -label Push -menu .mbar.push
3015 }
3016 . configure -menu .mbar
3017
3018 # -- Repository Menu
3019 #
3020 menu .mbar.repository
3021 .mbar.repository add command \
3022         -label {Visualize Current Branch} \
3023         -command {do_gitk {}} \
3024         -font font_ui
3025 if {![is_MacOSX]} {
3026         .mbar.repository add command \
3027                 -label {Visualize All Branches} \
3028                 -command {do_gitk {--all}} \
3029                 -font font_ui
3030 }
3031 .mbar.repository add separator
3032
3033 if {!$single_commit} {
3034         .mbar.repository add command -label {Compress Database} \
3035                 -command do_gc \
3036                 -font font_ui
3037
3038         .mbar.repository add command -label {Verify Database} \
3039                 -command do_fsck_objects \
3040                 -font font_ui
3041
3042         .mbar.repository add separator
3043
3044         if {[is_Windows]} {
3045                 .mbar.repository add command \
3046                         -label {Create Desktop Icon} \
3047                         -command do_windows_shortcut \
3048                         -font font_ui
3049         } elseif {[is_MacOSX]} {
3050                 .mbar.repository add command \
3051                         -label {Create Desktop Icon} \
3052                         -command do_macosx_app \
3053                         -font font_ui
3054         }
3055 }
3056
3057 .mbar.repository add command -label Quit \
3058         -command do_quit \
3059         -accelerator $M1T-Q \
3060         -font font_ui
3061
3062 # -- Edit Menu
3063 #
3064 menu .mbar.edit
3065 .mbar.edit add command -label Undo \
3066         -command {catch {[focus] edit undo}} \
3067         -accelerator $M1T-Z \
3068         -font font_ui
3069 .mbar.edit add command -label Redo \
3070         -command {catch {[focus] edit redo}} \
3071         -accelerator $M1T-Y \
3072         -font font_ui
3073 .mbar.edit add separator
3074 .mbar.edit add command -label Cut \
3075         -command {catch {tk_textCut [focus]}} \
3076         -accelerator $M1T-X \
3077         -font font_ui
3078 .mbar.edit add command -label Copy \
3079         -command {catch {tk_textCopy [focus]}} \
3080         -accelerator $M1T-C \
3081         -font font_ui
3082 .mbar.edit add command -label Paste \
3083         -command {catch {tk_textPaste [focus]; [focus] see insert}} \
3084         -accelerator $M1T-V \
3085         -font font_ui
3086 .mbar.edit add command -label Delete \
3087         -command {catch {[focus] delete sel.first sel.last}} \
3088         -accelerator Del \
3089         -font font_ui
3090 .mbar.edit add separator
3091 .mbar.edit add command -label {Select All} \
3092         -command {catch {[focus] tag add sel 0.0 end}} \
3093         -accelerator $M1T-A \
3094         -font font_ui
3095
3096 # -- Branch Menu
3097 #
3098 if {!$single_commit} {
3099         menu .mbar.branch
3100
3101         .mbar.branch add command -label {Create...} \
3102                 -command do_create_branch \
3103                 -font font_ui
3104         lappend disable_on_lock [list .mbar.branch entryconf \
3105                 [.mbar.branch index last] -state]
3106
3107         .mbar.branch add command -label {Delete...} \
3108                 -command do_delete_branch \
3109                 -font font_ui
3110         lappend disable_on_lock [list .mbar.branch entryconf \
3111                 [.mbar.branch index last] -state]
3112 }
3113
3114 # -- Commit Menu
3115 #
3116 menu .mbar.commit
3117
3118 .mbar.commit add radiobutton \
3119         -label {New Commit} \
3120         -command do_select_commit_type \
3121         -variable selected_commit_type \
3122         -value new \
3123         -font font_ui
3124 lappend disable_on_lock \
3125         [list .mbar.commit entryconf [.mbar.commit index last] -state]
3126
3127 .mbar.commit add radiobutton \
3128         -label {Amend Last Commit} \
3129         -command do_select_commit_type \
3130         -variable selected_commit_type \
3131         -value amend \
3132         -font font_ui
3133 lappend disable_on_lock \
3134         [list .mbar.commit entryconf [.mbar.commit index last] -state]
3135
3136 .mbar.commit add separator
3137
3138 .mbar.commit add command -label Rescan \
3139         -command do_rescan \
3140         -accelerator F5 \
3141         -font font_ui
3142 lappend disable_on_lock \
3143         [list .mbar.commit entryconf [.mbar.commit index last] -state]
3144
3145 .mbar.commit add command -label {Add To Commit} \
3146         -command do_include_selection \
3147         -font font_ui
3148 lappend disable_on_lock \
3149         [list .mbar.commit entryconf [.mbar.commit index last] -state]
3150
3151 .mbar.commit add command -label {Add All To Commit} \
3152         -command do_include_all \
3153         -accelerator $M1T-I \
3154         -font font_ui
3155 lappend disable_on_lock \
3156         [list .mbar.commit entryconf [.mbar.commit index last] -state]
3157
3158 .mbar.commit add command -label {Remove From Commit} \
3159         -command do_remove_selection \
3160         -font font_ui
3161 lappend disable_on_lock \
3162         [list .mbar.commit entryconf [.mbar.commit index last] -state]
3163
3164 .mbar.commit add command -label {Revert Changes} \
3165         -command do_revert_selection \
3166         -font font_ui
3167 lappend disable_on_lock \
3168         [list .mbar.commit entryconf [.mbar.commit index last] -state]
3169
3170 .mbar.commit add separator
3171
3172 .mbar.commit add command -label {Sign Off} \
3173         -command do_signoff \
3174         -accelerator $M1T-S \
3175         -font font_ui
3176
3177 .mbar.commit add command -label Commit \
3178         -command do_commit \
3179         -accelerator $M1T-Return \
3180         -font font_ui
3181 lappend disable_on_lock \
3182         [list .mbar.commit entryconf [.mbar.commit index last] -state]
3183
3184 # -- Transport menus
3185 #
3186 if {!$single_commit} {
3187         menu .mbar.fetch
3188         menu .mbar.pull
3189         menu .mbar.push
3190 }
3191
3192 if {[is_MacOSX]} {
3193         # -- Apple Menu (Mac OS X only)
3194         #
3195         .mbar add cascade -label Apple -menu .mbar.apple
3196         menu .mbar.apple
3197
3198         .mbar.apple add command -label "About $appname" \
3199                 -command do_about \
3200                 -font font_ui
3201         .mbar.apple add command -label "$appname Options..." \
3202                 -command do_options \
3203                 -font font_ui
3204 } else {
3205         # -- Edit Menu
3206         #
3207         .mbar.edit add separator
3208         .mbar.edit add command -label {Options...} \
3209                 -command do_options \
3210                 -font font_ui
3211
3212         # -- Tools Menu
3213         #
3214         if {[file exists /usr/local/miga/lib/gui-miga]} {
3215         proc do_miga {} {
3216                 global gitdir ui_status_value
3217                 if {![lock_index update]} return
3218                 set cmd [list sh --login -c "/usr/local/miga/lib/gui-miga \"[pwd]\""]
3219                 set miga_fd [open "|$cmd" r]
3220                 fconfigure $miga_fd -blocking 0
3221                 fileevent $miga_fd readable [list miga_done $miga_fd]
3222                 set ui_status_value {Running miga...}
3223         }
3224         proc miga_done {fd} {
3225                 read $fd 512
3226                 if {[eof $fd]} {
3227                         close $fd
3228                         unlock_index
3229                         rescan [list set ui_status_value {Ready.}]
3230                 }
3231         }
3232         .mbar add cascade -label Tools -menu .mbar.tools
3233         menu .mbar.tools
3234         .mbar.tools add command -label "Migrate" \
3235                 -command do_miga \
3236                 -font font_ui
3237         lappend disable_on_lock \
3238                 [list .mbar.tools entryconf [.mbar.tools index last] -state]
3239         }
3240
3241         # -- Help Menu
3242         #
3243         .mbar add cascade -label Help -menu .mbar.help
3244         menu .mbar.help
3245
3246         .mbar.help add command -label "About $appname" \
3247                 -command do_about \
3248                 -font font_ui
3249 }
3250
3251
3252 # -- Branch Control
3253 #
3254 frame .branch \
3255         -borderwidth 1 \
3256         -relief sunken
3257 label .branch.l1 \
3258         -text {Current Branch:} \
3259         -anchor w \
3260         -justify left \
3261         -font font_ui
3262 label .branch.cb \
3263         -textvariable current_branch \
3264         -anchor w \
3265         -justify left \
3266         -font font_ui
3267 pack .branch.l1 -side left
3268 pack .branch.cb -side left -fill x
3269 pack .branch -side top -fill x
3270
3271 # -- Main Window Layout
3272 #
3273 panedwindow .vpane -orient vertical
3274 panedwindow .vpane.files -orient horizontal
3275 .vpane add .vpane.files -sticky nsew -height 100 -width 400
3276 pack .vpane -anchor n -side top -fill both -expand 1
3277
3278 # -- Index File List
3279 #
3280 frame .vpane.files.index -height 100 -width 400
3281 label .vpane.files.index.title -text {Modified Files} \
3282         -background green \
3283         -font font_ui
3284 text $ui_index -background white -borderwidth 0 \
3285         -width 40 -height 10 \
3286         -font font_ui \
3287         -cursor $cursor_ptr \
3288         -yscrollcommand {.vpane.files.index.sb set} \
3289         -state disabled
3290 scrollbar .vpane.files.index.sb -command [list $ui_index yview]
3291 pack .vpane.files.index.title -side top -fill x
3292 pack .vpane.files.index.sb -side right -fill y
3293 pack $ui_index -side left -fill both -expand 1
3294 .vpane.files add .vpane.files.index -sticky nsew
3295
3296 # -- Other (Add) File List
3297 #
3298 frame .vpane.files.other -height 100 -width 100
3299 label .vpane.files.other.title -text {Untracked Files} \
3300         -background red \
3301         -font font_ui
3302 text $ui_other -background white -borderwidth 0 \
3303         -width 40 -height 10 \
3304         -font font_ui \
3305         -cursor $cursor_ptr \
3306         -yscrollcommand {.vpane.files.other.sb set} \
3307         -state disabled
3308 scrollbar .vpane.files.other.sb -command [list $ui_other yview]
3309 pack .vpane.files.other.title -side top -fill x
3310 pack .vpane.files.other.sb -side right -fill y
3311 pack $ui_other -side left -fill both -expand 1
3312 .vpane.files add .vpane.files.other -sticky nsew
3313
3314 foreach i [list $ui_index $ui_other] {
3315         $i tag conf in_diff -font font_uibold
3316         $i tag conf in_sel \
3317                 -background [$i cget -foreground] \
3318                 -foreground [$i cget -background]
3319 }
3320 unset i
3321
3322 # -- Diff and Commit Area
3323 #
3324 frame .vpane.lower -height 300 -width 400
3325 frame .vpane.lower.commarea
3326 frame .vpane.lower.diff -relief sunken -borderwidth 1
3327 pack .vpane.lower.commarea -side top -fill x
3328 pack .vpane.lower.diff -side bottom -fill both -expand 1
3329 .vpane add .vpane.lower -stick nsew
3330
3331 # -- Commit Area Buttons
3332 #
3333 frame .vpane.lower.commarea.buttons
3334 label .vpane.lower.commarea.buttons.l -text {} \
3335         -anchor w \
3336         -justify left \
3337         -font font_ui
3338 pack .vpane.lower.commarea.buttons.l -side top -fill x
3339 pack .vpane.lower.commarea.buttons -side left -fill y
3340
3341 button .vpane.lower.commarea.buttons.rescan -text {Rescan} \
3342         -command do_rescan \
3343         -font font_ui
3344 pack .vpane.lower.commarea.buttons.rescan -side top -fill x
3345 lappend disable_on_lock \
3346         {.vpane.lower.commarea.buttons.rescan conf -state}
3347
3348 button .vpane.lower.commarea.buttons.incall -text {Add All} \
3349         -command do_include_all \
3350         -font font_ui
3351 pack .vpane.lower.commarea.buttons.incall -side top -fill x
3352 lappend disable_on_lock \
3353         {.vpane.lower.commarea.buttons.incall conf -state}
3354
3355 button .vpane.lower.commarea.buttons.signoff -text {Sign Off} \
3356         -command do_signoff \
3357         -font font_ui
3358 pack .vpane.lower.commarea.buttons.signoff -side top -fill x
3359
3360 button .vpane.lower.commarea.buttons.commit -text {Commit} \
3361         -command do_commit \
3362         -font font_ui
3363 pack .vpane.lower.commarea.buttons.commit -side top -fill x
3364 lappend disable_on_lock \
3365         {.vpane.lower.commarea.buttons.commit conf -state}
3366
3367 # -- Commit Message Buffer
3368 #
3369 frame .vpane.lower.commarea.buffer
3370 frame .vpane.lower.commarea.buffer.header
3371 set ui_comm .vpane.lower.commarea.buffer.t
3372 set ui_coml .vpane.lower.commarea.buffer.header.l
3373 radiobutton .vpane.lower.commarea.buffer.header.new \
3374         -text {New Commit} \
3375         -command do_select_commit_type \
3376         -variable selected_commit_type \
3377         -value new \
3378         -font font_ui
3379 lappend disable_on_lock \
3380         [list .vpane.lower.commarea.buffer.header.new conf -state]
3381 radiobutton .vpane.lower.commarea.buffer.header.amend \
3382         -text {Amend Last Commit} \
3383         -command do_select_commit_type \
3384         -variable selected_commit_type \
3385         -value amend \
3386         -font font_ui
3387 lappend disable_on_lock \
3388         [list .vpane.lower.commarea.buffer.header.amend conf -state]
3389 label $ui_coml \
3390         -anchor w \
3391         -justify left \
3392         -font font_ui
3393 proc trace_commit_type {varname args} {
3394         global ui_coml commit_type
3395         switch -glob -- $commit_type {
3396         initial       {set txt {Initial Commit Message:}}
3397         amend         {set txt {Amended Commit Message:}}
3398         amend-initial {set txt {Amended Initial Commit Message:}}
3399         amend-merge   {set txt {Amended Merge Commit Message:}}
3400         merge         {set txt {Merge Commit Message:}}
3401         *             {set txt {Commit Message:}}
3402         }
3403         $ui_coml conf -text $txt
3404 }
3405 trace add variable commit_type write trace_commit_type
3406 pack $ui_coml -side left -fill x
3407 pack .vpane.lower.commarea.buffer.header.amend -side right
3408 pack .vpane.lower.commarea.buffer.header.new -side right
3409
3410 text $ui_comm -background white -borderwidth 1 \
3411         -undo true \
3412         -maxundo 20 \
3413         -autoseparators true \
3414         -relief sunken \
3415         -width 75 -height 9 -wrap none \
3416         -font font_diff \
3417         -yscrollcommand {.vpane.lower.commarea.buffer.sby set}
3418 scrollbar .vpane.lower.commarea.buffer.sby \
3419         -command [list $ui_comm yview]
3420 pack .vpane.lower.commarea.buffer.header -side top -fill x
3421 pack .vpane.lower.commarea.buffer.sby -side right -fill y
3422 pack $ui_comm -side left -fill y
3423 pack .vpane.lower.commarea.buffer -side left -fill y
3424
3425 # -- Commit Message Buffer Context Menu
3426 #
3427 set ctxm .vpane.lower.commarea.buffer.ctxm
3428 menu $ctxm -tearoff 0
3429 $ctxm add command \
3430         -label {Cut} \
3431         -font font_ui \
3432         -command {tk_textCut $ui_comm}
3433 $ctxm add command \
3434         -label {Copy} \
3435         -font font_ui \
3436         -command {tk_textCopy $ui_comm}
3437 $ctxm add command \
3438         -label {Paste} \
3439         -font font_ui \
3440         -command {tk_textPaste $ui_comm}
3441 $ctxm add command \
3442         -label {Delete} \
3443         -font font_ui \
3444         -command {$ui_comm delete sel.first sel.last}
3445 $ctxm add separator
3446 $ctxm add command \
3447         -label {Select All} \
3448         -font font_ui \
3449         -command {$ui_comm tag add sel 0.0 end}
3450 $ctxm add command \
3451         -label {Copy All} \
3452         -font font_ui \
3453         -command {
3454                 $ui_comm tag add sel 0.0 end
3455                 tk_textCopy $ui_comm
3456                 $ui_comm tag remove sel 0.0 end
3457         }
3458 $ctxm add separator
3459 $ctxm add command \
3460         -label {Sign Off} \
3461         -font font_ui \
3462         -command do_signoff
3463 bind_button3 $ui_comm "tk_popup $ctxm %X %Y"
3464
3465 # -- Diff Header
3466 #
3467 set current_diff {}
3468 set diff_actions [list]
3469 proc trace_current_diff {varname args} {
3470         global current_diff diff_actions file_states
3471         if {$current_diff eq {}} {
3472                 set s {}
3473                 set f {}
3474                 set p {}
3475                 set o disabled
3476         } else {
3477                 set p $current_diff
3478                 set s [mapdesc [lindex $file_states($p) 0] $p]
3479                 set f {File:}
3480                 set p [escape_path $p]
3481                 set o normal
3482         }
3483
3484         .vpane.lower.diff.header.status configure -text $s
3485         .vpane.lower.diff.header.file configure -text $f
3486         .vpane.lower.diff.header.path configure -text $p
3487         foreach w $diff_actions {
3488                 uplevel #0 $w $o
3489         }
3490 }
3491 trace add variable current_diff write trace_current_diff
3492
3493 frame .vpane.lower.diff.header -background orange
3494 label .vpane.lower.diff.header.status \
3495         -background orange \
3496         -width $max_status_desc \
3497         -anchor w \
3498         -justify left \
3499         -font font_ui
3500 label .vpane.lower.diff.header.file \
3501         -background orange \
3502         -anchor w \
3503         -justify left \
3504         -font font_ui
3505 label .vpane.lower.diff.header.path \
3506         -background orange \
3507         -anchor w \
3508         -justify left \
3509         -font font_ui
3510 pack .vpane.lower.diff.header.status -side left
3511 pack .vpane.lower.diff.header.file -side left
3512 pack .vpane.lower.diff.header.path -fill x
3513 set ctxm .vpane.lower.diff.header.ctxm
3514 menu $ctxm -tearoff 0
3515 $ctxm add command \
3516         -label {Copy} \
3517         -font font_ui \
3518         -command {
3519                 clipboard clear
3520                 clipboard append \
3521                         -format STRING \
3522                         -type STRING \
3523                         -- $current_diff
3524         }
3525 lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
3526 bind_button3 .vpane.lower.diff.header.path "tk_popup $ctxm %X %Y"
3527
3528 # -- Diff Body
3529 #
3530 frame .vpane.lower.diff.body
3531 set ui_diff .vpane.lower.diff.body.t
3532 text $ui_diff -background white -borderwidth 0 \
3533         -width 80 -height 15 -wrap none \
3534         -font font_diff \
3535         -xscrollcommand {.vpane.lower.diff.body.sbx set} \
3536         -yscrollcommand {.vpane.lower.diff.body.sby set} \
3537         -state disabled
3538 scrollbar .vpane.lower.diff.body.sbx -orient horizontal \
3539         -command [list $ui_diff xview]
3540 scrollbar .vpane.lower.diff.body.sby -orient vertical \
3541         -command [list $ui_diff yview]
3542 pack .vpane.lower.diff.body.sbx -side bottom -fill x
3543 pack .vpane.lower.diff.body.sby -side right -fill y
3544 pack $ui_diff -side left -fill both -expand 1
3545 pack .vpane.lower.diff.header -side top -fill x
3546 pack .vpane.lower.diff.body -side bottom -fill both -expand 1
3547
3548 $ui_diff tag conf d_@ -font font_diffbold
3549 $ui_diff tag conf d_+  -foreground blue
3550 $ui_diff tag conf d_-  -foreground red
3551 $ui_diff tag conf d_++ -foreground {#00a000}
3552 $ui_diff tag conf d_-- -foreground {#a000a0}
3553 $ui_diff tag conf d_+- \
3554         -foreground red \
3555         -background {light goldenrod yellow}
3556 $ui_diff tag conf d_-+ \
3557         -foreground blue \
3558         -background azure2
3559
3560 # -- Diff Body Context Menu
3561 #
3562 set ctxm .vpane.lower.diff.body.ctxm
3563 menu $ctxm -tearoff 0
3564 $ctxm add command \
3565         -label {Copy} \
3566         -font font_ui \
3567         -command {tk_textCopy $ui_diff}
3568 lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
3569 $ctxm add command \
3570         -label {Select All} \
3571         -font font_ui \
3572         -command {$ui_diff tag add sel 0.0 end}
3573 lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
3574 $ctxm add command \
3575         -label {Copy All} \
3576         -font font_ui \
3577         -command {
3578                 $ui_diff tag add sel 0.0 end
3579                 tk_textCopy $ui_diff
3580                 $ui_diff tag remove sel 0.0 end
3581         }
3582 lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
3583 $ctxm add separator
3584 $ctxm add command \
3585         -label {Decrease Font Size} \
3586         -font font_ui \
3587         -command {incr_font_size font_diff -1}
3588 lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
3589 $ctxm add command \
3590         -label {Increase Font Size} \
3591         -font font_ui \
3592         -command {incr_font_size font_diff 1}
3593 lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
3594 $ctxm add separator
3595 $ctxm add command \
3596         -label {Show Less Context} \
3597         -font font_ui \
3598         -command {if {$repo_config(gui.diffcontext) >= 2} {
3599                 incr repo_config(gui.diffcontext) -1
3600                 reshow_diff
3601         }}
3602 lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
3603 $ctxm add command \
3604         -label {Show More Context} \
3605         -font font_ui \
3606         -command {
3607                 incr repo_config(gui.diffcontext)
3608                 reshow_diff
3609         }
3610 lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
3611 $ctxm add separator
3612 $ctxm add command -label {Options...} \
3613         -font font_ui \
3614         -command do_options
3615 bind_button3 $ui_diff "tk_popup $ctxm %X %Y"
3616
3617 # -- Status Bar
3618 #
3619 set ui_status_value {Initializing...}
3620 label .status -textvariable ui_status_value \
3621         -anchor w \
3622         -justify left \
3623         -borderwidth 1 \
3624         -relief sunken \
3625         -font font_ui
3626 pack .status -anchor w -side bottom -fill x
3627
3628 # -- Load geometry
3629 #
3630 catch {
3631 set gm $repo_config(gui.geometry)
3632 wm geometry . [lindex $gm 0]
3633 .vpane sash place 0 \
3634         [lindex [.vpane sash coord 0] 0] \
3635         [lindex $gm 1]
3636 .vpane.files sash place 0 \
3637         [lindex $gm 2] \
3638         [lindex [.vpane.files sash coord 0] 1]
3639 unset gm
3640 }
3641
3642 # -- Key Bindings
3643 #
3644 bind $ui_comm <$M1B-Key-Return> {do_commit;break}
3645 bind $ui_comm <$M1B-Key-i> {do_include_all;break}
3646 bind $ui_comm <$M1B-Key-I> {do_include_all;break}
3647 bind $ui_comm <$M1B-Key-x> {tk_textCut %W;break}
3648 bind $ui_comm <$M1B-Key-X> {tk_textCut %W;break}
3649 bind $ui_comm <$M1B-Key-c> {tk_textCopy %W;break}
3650 bind $ui_comm <$M1B-Key-C> {tk_textCopy %W;break}
3651 bind $ui_comm <$M1B-Key-v> {tk_textPaste %W; %W see insert; break}
3652 bind $ui_comm <$M1B-Key-V> {tk_textPaste %W; %W see insert; break}
3653 bind $ui_comm <$M1B-Key-a> {%W tag add sel 0.0 end;break}
3654 bind $ui_comm <$M1B-Key-A> {%W tag add sel 0.0 end;break}
3655
3656 bind $ui_diff <$M1B-Key-x> {tk_textCopy %W;break}
3657 bind $ui_diff <$M1B-Key-X> {tk_textCopy %W;break}
3658 bind $ui_diff <$M1B-Key-c> {tk_textCopy %W;break}
3659 bind $ui_diff <$M1B-Key-C> {tk_textCopy %W;break}
3660 bind $ui_diff <$M1B-Key-v> {break}
3661 bind $ui_diff <$M1B-Key-V> {break}
3662 bind $ui_diff <$M1B-Key-a> {%W tag add sel 0.0 end;break}
3663 bind $ui_diff <$M1B-Key-A> {%W tag add sel 0.0 end;break}
3664 bind $ui_diff <Key-Up>     {catch {%W yview scroll -1 units};break}
3665 bind $ui_diff <Key-Down>   {catch {%W yview scroll  1 units};break}
3666 bind $ui_diff <Key-Left>   {catch {%W xview scroll -1 units};break}
3667 bind $ui_diff <Key-Right>  {catch {%W xview scroll  1 units};break}
3668
3669 bind .   <Destroy> do_quit
3670 bind all <Key-F5> do_rescan
3671 bind all <$M1B-Key-r> do_rescan
3672 bind all <$M1B-Key-R> do_rescan
3673 bind .   <$M1B-Key-s> do_signoff
3674 bind .   <$M1B-Key-S> do_signoff
3675 bind .   <$M1B-Key-i> do_include_all
3676 bind .   <$M1B-Key-I> do_include_all
3677 bind .   <$M1B-Key-Return> do_commit
3678 bind all <$M1B-Key-q> do_quit
3679 bind all <$M1B-Key-Q> do_quit
3680 bind all <$M1B-Key-w> {destroy [winfo toplevel %W]}
3681 bind all <$M1B-Key-W> {destroy [winfo toplevel %W]}
3682 foreach i [list $ui_index $ui_other] {
3683         bind $i <Button-1>       "toggle_or_diff         $i %x %y; break"
3684         bind $i <$M1B-Button-1>  "add_one_to_selection   $i %x %y; break"
3685         bind $i <Shift-Button-1> "add_range_to_selection $i %x %y; break"
3686 }
3687 unset i
3688
3689 set file_lists($ui_index) [list]
3690 set file_lists($ui_other) [list]
3691
3692 set HEAD {}
3693 set PARENT {}
3694 set MERGE_HEAD [list]
3695 set commit_type {}
3696 set empty_tree {}
3697 set current_branch {}
3698 set current_diff {}
3699 set selected_commit_type new
3700
3701 wm title . "$appname ([file normalize [file dirname $gitdir]])"
3702 focus -force $ui_comm
3703
3704 # -- Warn the user about environmental problems.  Cygwin's Tcl
3705 #    does *not* pass its env array onto any processes it spawns.
3706 #    This means that git processes get none of our environment.
3707 #
3708 if {[is_Windows]} {
3709         set ignored_env 0
3710         set suggest_user {}
3711         set msg "Possible environment issues exist.
3712
3713 The following environment variables are probably
3714 going to be ignored by any Git subprocess run
3715 by $appname:
3716
3717 "
3718         foreach name [array names env] {
3719                 switch -regexp -- $name {
3720                 {^GIT_INDEX_FILE$} -
3721                 {^GIT_OBJECT_DIRECTORY$} -
3722                 {^GIT_ALTERNATE_OBJECT_DIRECTORIES$} -
3723                 {^GIT_DIFF_OPTS$} -
3724                 {^GIT_EXTERNAL_DIFF$} -
3725                 {^GIT_PAGER$} -
3726                 {^GIT_TRACE$} -
3727                 {^GIT_CONFIG$} -
3728                 {^GIT_CONFIG_LOCAL$} -
3729                 {^GIT_(AUTHOR|COMMITTER)_DATE$} {
3730                         append msg " - $name\n"
3731                         incr ignored_env
3732                 }
3733                 {^GIT_(AUTHOR|COMMITTER)_(NAME|EMAIL)$} {
3734                         append msg " - $name\n"
3735                         incr ignored_env
3736                         set suggest_user $name
3737                 }
3738                 }
3739         }
3740         if {$ignored_env > 0} {
3741                 append msg "
3742 This is due to a known issue with the
3743 Tcl binary distributed by Cygwin."
3744
3745                 if {$suggest_user ne {}} {
3746                         append msg "
3747
3748 A good replacement for $suggest_user
3749 is placing values for the user.name and
3750 user.email settings into your personal
3751 ~/.gitconfig file.
3752 "
3753                 }
3754                 warn_popup $msg
3755         }
3756         unset ignored_env msg suggest_user name
3757 }
3758
3759 # -- Only initialize complex UI if we are going to stay running.
3760 #
3761 if {!$single_commit} {
3762         load_all_remotes
3763         load_all_heads
3764
3765         populate_branch_menu .mbar.branch
3766         populate_fetch_menu .mbar.fetch
3767         populate_pull_menu .mbar.pull
3768         populate_push_menu .mbar.push
3769 }
3770
3771 lock_index begin-read
3772 after 1 do_rescan