Code

0982da569045afae4ec1059816cefa71c30fcbb8
[git.git] / git-gui.sh
1 #!/bin/sh
2 # Tcl ignores the next line -*- tcl -*- \
3 exec wish "$0" -- "$@"
5 set appvers {@@GIT_VERSION@@}
6 set copyright {
7 Copyright © 2006, 2007 Shawn Pearce, Paul Mackerras.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA}
23 ######################################################################
24 ##
25 ## read only globals
27 set _appname [lindex [file split $argv0] end]
28 set _gitdir {}
29 set _reponame {}
31 proc appname {} {
32         global _appname
33         return $_appname
34 }
36 proc gitdir {args} {
37         global _gitdir
38         if {$args eq {}} {
39                 return $_gitdir
40         }
41         return [eval [concat [list file join $_gitdir] $args]]
42 }
44 proc reponame {} {
45         global _reponame
46         return $_reponame
47 }
49 ######################################################################
50 ##
51 ## config
53 proc is_many_config {name} {
54         switch -glob -- $name {
55         remote.*.fetch -
56         remote.*.push
57                 {return 1}
58         *
59                 {return 0}
60         }
61 }
63 proc load_config {include_global} {
64         global repo_config global_config default_config
66         array unset global_config
67         if {$include_global} {
68                 catch {
69                         set fd_rc [open "| git repo-config --global --list" r]
70                         while {[gets $fd_rc line] >= 0} {
71                                 if {[regexp {^([^=]+)=(.*)$} $line line name value]} {
72                                         if {[is_many_config $name]} {
73                                                 lappend global_config($name) $value
74                                         } else {
75                                                 set global_config($name) $value
76                                         }
77                                 }
78                         }
79                         close $fd_rc
80                 }
81         }
83         array unset repo_config
84         catch {
85                 set fd_rc [open "| git repo-config --list" r]
86                 while {[gets $fd_rc line] >= 0} {
87                         if {[regexp {^([^=]+)=(.*)$} $line line name value]} {
88                                 if {[is_many_config $name]} {
89                                         lappend repo_config($name) $value
90                                 } else {
91                                         set repo_config($name) $value
92                                 }
93                         }
94                 }
95                 close $fd_rc
96         }
98         foreach name [array names default_config] {
99                 if {[catch {set v $global_config($name)}]} {
100                         set global_config($name) $default_config($name)
101                 }
102                 if {[catch {set v $repo_config($name)}]} {
103                         set repo_config($name) $default_config($name)
104                 }
105         }
108 proc save_config {} {
109         global default_config font_descs
110         global repo_config global_config
111         global repo_config_new global_config_new
113         foreach option $font_descs {
114                 set name [lindex $option 0]
115                 set font [lindex $option 1]
116                 font configure $font \
117                         -family $global_config_new(gui.$font^^family) \
118                         -size $global_config_new(gui.$font^^size)
119                 font configure ${font}bold \
120                         -family $global_config_new(gui.$font^^family) \
121                         -size $global_config_new(gui.$font^^size)
122                 set global_config_new(gui.$name) [font configure $font]
123                 unset global_config_new(gui.$font^^family)
124                 unset global_config_new(gui.$font^^size)
125         }
127         foreach name [array names default_config] {
128                 set value $global_config_new($name)
129                 if {$value ne $global_config($name)} {
130                         if {$value eq $default_config($name)} {
131                                 catch {exec git repo-config --global --unset $name}
132                         } else {
133                                 regsub -all "\[{}\]" $value {"} value
134                                 exec git repo-config --global $name $value
135                         }
136                         set global_config($name) $value
137                         if {$value eq $repo_config($name)} {
138                                 catch {exec git repo-config --unset $name}
139                                 set repo_config($name) $value
140                         }
141                 }
142         }
144         foreach name [array names default_config] {
145                 set value $repo_config_new($name)
146                 if {$value ne $repo_config($name)} {
147                         if {$value eq $global_config($name)} {
148                                 catch {exec git repo-config --unset $name}
149                         } else {
150                                 regsub -all "\[{}\]" $value {"} value
151                                 exec git repo-config $name $value
152                         }
153                         set repo_config($name) $value
154                 }
155         }
158 proc error_popup {msg} {
159         set title [appname]
160         if {[reponame] ne {}} {
161                 append title " ([reponame])"
162         }
163         set cmd [list tk_messageBox \
164                 -icon error \
165                 -type ok \
166                 -title "$title: error" \
167                 -message $msg]
168         if {[winfo ismapped .]} {
169                 lappend cmd -parent .
170         }
171         eval $cmd
174 proc warn_popup {msg} {
175         set title [appname]
176         if {[reponame] ne {}} {
177                 append title " ([reponame])"
178         }
179         set cmd [list tk_messageBox \
180                 -icon warning \
181                 -type ok \
182                 -title "$title: warning" \
183                 -message $msg]
184         if {[winfo ismapped .]} {
185                 lappend cmd -parent .
186         }
187         eval $cmd
190 proc info_popup {msg} {
191         set title [appname]
192         if {[reponame] ne {}} {
193                 append title " ([reponame])"
194         }
195         tk_messageBox \
196                 -parent . \
197                 -icon info \
198                 -type ok \
199                 -title $title \
200                 -message $msg
203 proc ask_popup {msg} {
204         set title [appname]
205         if {[reponame] ne {}} {
206                 append title " ([reponame])"
207         }
208         return [tk_messageBox \
209                 -parent . \
210                 -icon question \
211                 -type yesno \
212                 -title $title \
213                 -message $msg]
216 ######################################################################
217 ##
218 ## repository setup
220 if {   [catch {set _gitdir $env(GIT_DIR)}]
221         && [catch {set _gitdir [exec git rev-parse --git-dir]} err]} {
222         catch {wm withdraw .}
223         error_popup "Cannot find the git directory:\n\n$err"
224         exit 1
226 if {![file isdirectory $_gitdir]} {
227         catch {wm withdraw .}
228         error_popup "Git directory not found:\n\n$_gitdir"
229         exit 1
231 if {[lindex [file split $_gitdir] end] ne {.git}} {
232         catch {wm withdraw .}
233         error_popup "Cannot use funny .git directory:\n\n$gitdir"
234         exit 1
236 if {[catch {cd [file dirname $_gitdir]} err]} {
237         catch {wm withdraw .}
238         error_popup "No working directory [file dirname $_gitdir]:\n\n$err"
239         exit 1
241 set _reponame [lindex [file split \
242         [file normalize [file dirname $_gitdir]]] \
243         end]
245 set single_commit 0
246 if {[appname] eq {git-citool}} {
247         set single_commit 1
250 ######################################################################
251 ##
252 ## task management
254 set rescan_active 0
255 set diff_active 0
256 set last_clicked {}
258 set disable_on_lock [list]
259 set index_lock_type none
261 proc lock_index {type} {
262         global index_lock_type disable_on_lock
264         if {$index_lock_type eq {none}} {
265                 set index_lock_type $type
266                 foreach w $disable_on_lock {
267                         uplevel #0 $w disabled
268                 }
269                 return 1
270         } elseif {$index_lock_type eq "begin-$type"} {
271                 set index_lock_type $type
272                 return 1
273         }
274         return 0
277 proc unlock_index {} {
278         global index_lock_type disable_on_lock
280         set index_lock_type none
281         foreach w $disable_on_lock {
282                 uplevel #0 $w normal
283         }
286 ######################################################################
287 ##
288 ## status
290 proc repository_state {ctvar hdvar mhvar} {
291         global current_branch
292         upvar $ctvar ct $hdvar hd $mhvar mh
294         set mh [list]
296         if {[catch {set current_branch [exec git symbolic-ref HEAD]}]} {
297                 set current_branch {}
298         } else {
299                 regsub ^refs/((heads|tags|remotes)/)? \
300                         $current_branch \
301                         {} \
302                         current_branch
303         }
305         if {[catch {set hd [exec git rev-parse --verify HEAD]}]} {
306                 set hd {}
307                 set ct initial
308                 return
309         }
311         set merge_head [gitdir MERGE_HEAD]
312         if {[file exists $merge_head]} {
313                 set ct merge
314                 set fd_mh [open $merge_head r]
315                 while {[gets $fd_mh line] >= 0} {
316                         lappend mh $line
317                 }
318                 close $fd_mh
319                 return
320         }
322         set ct normal
325 proc PARENT {} {
326         global PARENT empty_tree
328         set p [lindex $PARENT 0]
329         if {$p ne {}} {
330                 return $p
331         }
332         if {$empty_tree eq {}} {
333                 set empty_tree [exec git mktree << {}]
334         }
335         return $empty_tree
338 proc rescan {after {honor_trustmtime 1}} {
339         global HEAD PARENT MERGE_HEAD commit_type
340         global ui_index ui_workdir ui_status_value ui_comm
341         global rescan_active file_states
342         global repo_config
344         if {$rescan_active > 0 || ![lock_index read]} return
346         repository_state newType newHEAD newMERGE_HEAD
347         if {[string match amend* $commit_type]
348                 && $newType eq {normal}
349                 && $newHEAD eq $HEAD} {
350         } else {
351                 set HEAD $newHEAD
352                 set PARENT $newHEAD
353                 set MERGE_HEAD $newMERGE_HEAD
354                 set commit_type $newType
355         }
357         array unset file_states
359         if {![$ui_comm edit modified]
360                 || [string trim [$ui_comm get 0.0 end]] eq {}} {
361                 if {[load_message GITGUI_MSG]} {
362                 } elseif {[load_message MERGE_MSG]} {
363                 } elseif {[load_message SQUASH_MSG]} {
364                 }
365                 $ui_comm edit reset
366                 $ui_comm edit modified false
367         }
369         if {$honor_trustmtime && $repo_config(gui.trustmtime) eq {true}} {
370                 rescan_stage2 {} $after
371         } else {
372                 set rescan_active 1
373                 set ui_status_value {Refreshing file status...}
374                 set cmd [list git update-index]
375                 lappend cmd -q
376                 lappend cmd --unmerged
377                 lappend cmd --ignore-missing
378                 lappend cmd --refresh
379                 set fd_rf [open "| $cmd" r]
380                 fconfigure $fd_rf -blocking 0 -translation binary
381                 fileevent $fd_rf readable \
382                         [list rescan_stage2 $fd_rf $after]
383         }
386 proc rescan_stage2 {fd after} {
387         global ui_status_value
388         global rescan_active buf_rdi buf_rdf buf_rlo
390         if {$fd ne {}} {
391                 read $fd
392                 if {![eof $fd]} return
393                 close $fd
394         }
396         set ls_others [list | git ls-files --others -z \
397                 --exclude-per-directory=.gitignore]
398         set info_exclude [gitdir info exclude]
399         if {[file readable $info_exclude]} {
400                 lappend ls_others "--exclude-from=$info_exclude"
401         }
403         set buf_rdi {}
404         set buf_rdf {}
405         set buf_rlo {}
407         set rescan_active 3
408         set ui_status_value {Scanning for modified files ...}
409         set fd_di [open "| git diff-index --cached -z [PARENT]" r]
410         set fd_df [open "| git diff-files -z" r]
411         set fd_lo [open $ls_others r]
413         fconfigure $fd_di -blocking 0 -translation binary -encoding binary
414         fconfigure $fd_df -blocking 0 -translation binary -encoding binary
415         fconfigure $fd_lo -blocking 0 -translation binary -encoding binary
416         fileevent $fd_di readable [list read_diff_index $fd_di $after]
417         fileevent $fd_df readable [list read_diff_files $fd_df $after]
418         fileevent $fd_lo readable [list read_ls_others $fd_lo $after]
421 proc load_message {file} {
422         global ui_comm
424         set f [gitdir $file]
425         if {[file isfile $f]} {
426                 if {[catch {set fd [open $f r]}]} {
427                         return 0
428                 }
429                 set content [string trim [read $fd]]
430                 close $fd
431                 regsub -all -line {[ \r\t]+$} $content {} content
432                 $ui_comm delete 0.0 end
433                 $ui_comm insert end $content
434                 return 1
435         }
436         return 0
439 proc read_diff_index {fd after} {
440         global buf_rdi
442         append buf_rdi [read $fd]
443         set c 0
444         set n [string length $buf_rdi]
445         while {$c < $n} {
446                 set z1 [string first "\0" $buf_rdi $c]
447                 if {$z1 == -1} break
448                 incr z1
449                 set z2 [string first "\0" $buf_rdi $z1]
450                 if {$z2 == -1} break
452                 incr c
453                 set i [split [string range $buf_rdi $c [expr {$z1 - 2}]] { }]
454                 set p [string range $buf_rdi $z1 [expr {$z2 - 1}]]
455                 merge_state \
456                         [encoding convertfrom $p] \
457                         [lindex $i 4]? \
458                         [list [lindex $i 0] [lindex $i 2]] \
459                         [list]
460                 set c $z2
461                 incr c
462         }
463         if {$c < $n} {
464                 set buf_rdi [string range $buf_rdi $c end]
465         } else {
466                 set buf_rdi {}
467         }
469         rescan_done $fd buf_rdi $after
472 proc read_diff_files {fd after} {
473         global buf_rdf
475         append buf_rdf [read $fd]
476         set c 0
477         set n [string length $buf_rdf]
478         while {$c < $n} {
479                 set z1 [string first "\0" $buf_rdf $c]
480                 if {$z1 == -1} break
481                 incr z1
482                 set z2 [string first "\0" $buf_rdf $z1]
483                 if {$z2 == -1} break
485                 incr c
486                 set i [split [string range $buf_rdf $c [expr {$z1 - 2}]] { }]
487                 set p [string range $buf_rdf $z1 [expr {$z2 - 1}]]
488                 merge_state \
489                         [encoding convertfrom $p] \
490                         ?[lindex $i 4] \
491                         [list] \
492                         [list [lindex $i 0] [lindex $i 2]]
493                 set c $z2
494                 incr c
495         }
496         if {$c < $n} {
497                 set buf_rdf [string range $buf_rdf $c end]
498         } else {
499                 set buf_rdf {}
500         }
502         rescan_done $fd buf_rdf $after
505 proc read_ls_others {fd after} {
506         global buf_rlo
508         append buf_rlo [read $fd]
509         set pck [split $buf_rlo "\0"]
510         set buf_rlo [lindex $pck end]
511         foreach p [lrange $pck 0 end-1] {
512                 merge_state [encoding convertfrom $p] ?O
513         }
514         rescan_done $fd buf_rlo $after
517 proc rescan_done {fd buf after} {
518         global rescan_active
519         global file_states repo_config
520         upvar $buf to_clear
522         if {![eof $fd]} return
523         set to_clear {}
524         close $fd
525         if {[incr rescan_active -1] > 0} return
527         prune_selection
528         unlock_index
529         display_all_files
530         reshow_diff
531         uplevel #0 $after
534 proc prune_selection {} {
535         global file_states selected_paths
537         foreach path [array names selected_paths] {
538                 if {[catch {set still_here $file_states($path)}]} {
539                         unset selected_paths($path)
540                 }
541         }
544 ######################################################################
545 ##
546 ## diff
548 proc clear_diff {} {
549         global ui_diff current_diff_path current_diff_header
550         global ui_index ui_workdir
552         $ui_diff conf -state normal
553         $ui_diff delete 0.0 end
554         $ui_diff conf -state disabled
556         set current_diff_path {}
557         set current_diff_header {}
559         $ui_index tag remove in_diff 0.0 end
560         $ui_workdir tag remove in_diff 0.0 end
563 proc reshow_diff {} {
564         global ui_status_value file_states file_lists
565         global current_diff_path current_diff_side
567         set p $current_diff_path
568         if {$p eq {}
569                 || $current_diff_side eq {}
570                 || [catch {set s $file_states($p)}]
571                 || [lsearch -sorted -exact $file_lists($current_diff_side) $p] == -1} {
572                 clear_diff
573         } else {
574                 show_diff $p $current_diff_side
575         }
578 proc handle_empty_diff {} {
579         global current_diff_path file_states file_lists
581         set path $current_diff_path
582         set s $file_states($path)
583         if {[lindex $s 0] ne {_M}} return
585         info_popup "No differences detected.
587 [short_path $path] has no changes.
589 The modification date of this file was updated
590 by another application, but the content within
591 the file was not changed.
593 A rescan will be automatically started to find
594 other files which may have the same state."
596         clear_diff
597         display_file $path __
598         rescan {set ui_status_value {Ready.}} 0
601 proc show_diff {path w {lno {}}} {
602         global file_states file_lists
603         global is_3way_diff diff_active repo_config
604         global ui_diff ui_status_value ui_index ui_workdir
605         global current_diff_path current_diff_side current_diff_header
607         if {$diff_active || ![lock_index read]} return
609         clear_diff
610         if {$lno == {}} {
611                 set lno [lsearch -sorted -exact $file_lists($w) $path]
612                 if {$lno >= 0} {
613                         incr lno
614                 }
615         }
616         if {$lno >= 1} {
617                 $w tag add in_diff $lno.0 [expr {$lno + 1}].0
618         }
620         set s $file_states($path)
621         set m [lindex $s 0]
622         set is_3way_diff 0
623         set diff_active 1
624         set current_diff_path $path
625         set current_diff_side $w
626         set current_diff_header {}
627         set ui_status_value "Loading diff of [escape_path $path]..."
629         # - Git won't give us the diff, there's nothing to compare to!
630         #
631         if {$m eq {_O}} {
632                 set max_sz [expr {128 * 1024}]
633                 if {[catch {
634                                 set fd [open $path r]
635                                 set content [read $fd $max_sz]
636                                 close $fd
637                                 set sz [file size $path]
638                         } err ]} {
639                         set diff_active 0
640                         unlock_index
641                         set ui_status_value "Unable to display [escape_path $path]"
642                         error_popup "Error loading file:\n\n$err"
643                         return
644                 }
645                 $ui_diff conf -state normal
646                 if {![catch {set type [exec file $path]}]} {
647                         set n [string length $path]
648                         if {[string equal -length $n $path $type]} {
649                                 set type [string range $type $n end]
650                                 regsub {^:?\s*} $type {} type
651                         }
652                         $ui_diff insert end "* $type\n" d_@
653                 }
654                 if {[string first "\0" $content] != -1} {
655                         $ui_diff insert end \
656                                 "* Binary file (not showing content)." \
657                                 d_@
658                 } else {
659                         if {$sz > $max_sz} {
660                                 $ui_diff insert end \
661 "* Untracked file is $sz bytes.
662 * Showing only first $max_sz bytes.
663 " d_@
664                         }
665                         $ui_diff insert end $content
666                         if {$sz > $max_sz} {
667                                 $ui_diff insert end "
668 * Untracked file clipped here by [appname].
669 * To see the entire file, use an external editor.
670 " d_@
671                         }
672                 }
673                 $ui_diff conf -state disabled
674                 set diff_active 0
675                 unlock_index
676                 set ui_status_value {Ready.}
677                 return
678         }
680         set cmd [list | git]
681         if {$w eq $ui_index} {
682                 lappend cmd diff-index
683                 lappend cmd --cached
684         } elseif {$w eq $ui_workdir} {
685                 if {[string index $m 0] eq {U}} {
686                         lappend cmd diff
687                 } else {
688                         lappend cmd diff-files
689                 }
690         }
692         lappend cmd -p
693         lappend cmd --no-color
694         if {$repo_config(gui.diffcontext) > 0} {
695                 lappend cmd "-U$repo_config(gui.diffcontext)"
696         }
697         if {$w eq $ui_index} {
698                 lappend cmd [PARENT]
699         }
700         lappend cmd --
701         lappend cmd $path
703         if {[catch {set fd [open $cmd r]} err]} {
704                 set diff_active 0
705                 unlock_index
706                 set ui_status_value "Unable to display [escape_path $path]"
707                 error_popup "Error loading diff:\n\n$err"
708                 return
709         }
711         fconfigure $fd \
712                 -blocking 0 \
713                 -encoding binary \
714                 -translation binary
715         fileevent $fd readable [list read_diff $fd]
718 proc read_diff {fd} {
719         global ui_diff ui_status_value diff_active
720         global is_3way_diff current_diff_header
722         $ui_diff conf -state normal
723         while {[gets $fd line] >= 0} {
724                 # -- Cleanup uninteresting diff header lines.
725                 #
726                 if {   [string match {diff --git *}      $line]
727                         || [string match {diff --cc *}       $line]
728                         || [string match {diff --combined *} $line]
729                         || [string match {--- *}             $line]
730                         || [string match {+++ *}             $line]} {
731                         append current_diff_header $line "\n"
732                         continue
733                 }
734                 if {[string match {index *} $line]} continue
735                 if {$line eq {deleted file mode 120000}} {
736                         set line "deleted symlink"
737                 }
739                 # -- Automatically detect if this is a 3 way diff.
740                 #
741                 if {[string match {@@@ *} $line]} {set is_3way_diff 1}
743                 if {[string match {mode *} $line]
744                         || [string match {new file *} $line]
745                         || [string match {deleted file *} $line]
746                         || [string match {Binary files * and * differ} $line]
747                         || $line eq {\ No newline at end of file}
748                         || [regexp {^\* Unmerged path } $line]} {
749                         set tags {}
750                 } elseif {$is_3way_diff} {
751                         set op [string range $line 0 1]
752                         switch -- $op {
753                         {  } {set tags {}}
754                         {@@} {set tags d_@}
755                         { +} {set tags d_s+}
756                         { -} {set tags d_s-}
757                         {+ } {set tags d_+s}
758                         {- } {set tags d_-s}
759                         {--} {set tags d_--}
760                         {++} {
761                                 if {[regexp {^\+\+([<>]{7} |={7})} $line _g op]} {
762                                         set line [string replace $line 0 1 {  }]
763                                         set tags d$op
764                                 } else {
765                                         set tags d_++
766                                 }
767                         }
768                         default {
769                                 puts "error: Unhandled 3 way diff marker: {$op}"
770                                 set tags {}
771                         }
772                         }
773                 } else {
774                         set op [string index $line 0]
775                         switch -- $op {
776                         { } {set tags {}}
777                         {@} {set tags d_@}
778                         {-} {set tags d_-}
779                         {+} {
780                                 if {[regexp {^\+([<>]{7} |={7})} $line _g op]} {
781                                         set line [string replace $line 0 0 { }]
782                                         set tags d$op
783                                 } else {
784                                         set tags d_+
785                                 }
786                         }
787                         default {
788                                 puts "error: Unhandled 2 way diff marker: {$op}"
789                                 set tags {}
790                         }
791                         }
792                 }
793                 $ui_diff insert end $line $tags
794                 if {[string index $line end] eq "\r"} {
795                         $ui_diff tag add d_cr {end - 2c}
796                 }
797                 $ui_diff insert end "\n" $tags
798         }
799         $ui_diff conf -state disabled
801         if {[eof $fd]} {
802                 close $fd
803                 set diff_active 0
804                 unlock_index
805                 set ui_status_value {Ready.}
807                 if {[$ui_diff index end] eq {2.0}} {
808                         handle_empty_diff
809                 }
810         }
813 proc apply_hunk {x y} {
814         global current_diff_path current_diff_header current_diff_side
815         global ui_diff ui_index file_states
817         if {$current_diff_path eq {} || $current_diff_header eq {}} return
818         if {![lock_index apply_hunk]} return
820         set apply_cmd {git apply --cached --whitespace=nowarn}
821         set mi [lindex $file_states($current_diff_path) 0]
822         if {$current_diff_side eq $ui_index} {
823                 set mode unstage
824                 lappend apply_cmd --reverse
825                 if {[string index $mi 0] ne {M}} {
826                         unlock_index
827                         return
828                 }
829         } else {
830                 set mode stage
831                 if {[string index $mi 1] ne {M}} {
832                         unlock_index
833                         return
834                 }
835         }
837         set s_lno [lindex [split [$ui_diff index @$x,$y] .] 0]
838         set s_lno [$ui_diff search -backwards -regexp ^@@ $s_lno.0 0.0]
839         if {$s_lno eq {}} {
840                 unlock_index
841                 return
842         }
844         set e_lno [$ui_diff search -forwards -regexp ^@@ "$s_lno + 1 lines" end]
845         if {$e_lno eq {}} {
846                 set e_lno end
847         }
849         if {[catch {
850                 set p [open "| $apply_cmd" w]
851                 fconfigure $p -translation binary -encoding binary
852                 puts -nonewline $p $current_diff_header
853                 puts -nonewline $p [$ui_diff get $s_lno $e_lno]
854                 close $p} err]} {
855                 error_popup "Failed to $mode selected hunk.\n\n$err"
856                 unlock_index
857                 return
858         }
860         $ui_diff conf -state normal
861         $ui_diff delete $s_lno $e_lno
862         $ui_diff conf -state disabled
864         if {[$ui_diff get 1.0 end] eq "\n"} {
865                 set o _
866         } else {
867                 set o ?
868         }
870         if {$current_diff_side eq $ui_index} {
871                 set mi ${o}M
872         } elseif {[string index $mi 0] eq {_}} {
873                 set mi M$o
874         } else {
875                 set mi ?$o
876         }
877         unlock_index
878         display_file $current_diff_path $mi
879         if {$o eq {_}} {
880                 clear_diff
881         }
884 ######################################################################
885 ##
886 ## commit
888 proc load_last_commit {} {
889         global HEAD PARENT MERGE_HEAD commit_type ui_comm
890         global repo_config
892         if {[llength $PARENT] == 0} {
893                 error_popup {There is nothing to amend.
895 You are about to create the initial commit.
896 There is no commit before this to amend.
898                 return
899         }
901         repository_state curType curHEAD curMERGE_HEAD
902         if {$curType eq {merge}} {
903                 error_popup {Cannot amend while merging.
905 You are currently in the middle of a merge that
906 has not been fully completed.  You cannot amend
907 the prior commit unless you first abort the
908 current merge activity.
910                 return
911         }
913         set msg {}
914         set parents [list]
915         if {[catch {
916                         set fd [open "| git cat-file commit $curHEAD" r]
917                         fconfigure $fd -encoding binary -translation lf
918                         if {[catch {set enc $repo_config(i18n.commitencoding)}]} {
919                                 set enc utf-8
920                         }
921                         while {[gets $fd line] > 0} {
922                                 if {[string match {parent *} $line]} {
923                                         lappend parents [string range $line 7 end]
924                                 } elseif {[string match {encoding *} $line]} {
925                                         set enc [string tolower [string range $line 9 end]]
926                                 }
927                         }
928                         fconfigure $fd -encoding $enc
929                         set msg [string trim [read $fd]]
930                         close $fd
931                 } err]} {
932                 error_popup "Error loading commit data for amend:\n\n$err"
933                 return
934         }
936         set HEAD $curHEAD
937         set PARENT $parents
938         set MERGE_HEAD [list]
939         switch -- [llength $parents] {
940         0       {set commit_type amend-initial}
941         1       {set commit_type amend}
942         default {set commit_type amend-merge}
943         }
945         $ui_comm delete 0.0 end
946         $ui_comm insert end $msg
947         $ui_comm edit reset
948         $ui_comm edit modified false
949         rescan {set ui_status_value {Ready.}}
952 proc create_new_commit {} {
953         global commit_type ui_comm
955         set commit_type normal
956         $ui_comm delete 0.0 end
957         $ui_comm edit reset
958         $ui_comm edit modified false
959         rescan {set ui_status_value {Ready.}}
962 set GIT_COMMITTER_IDENT {}
964 proc committer_ident {} {
965         global GIT_COMMITTER_IDENT
967         if {$GIT_COMMITTER_IDENT eq {}} {
968                 if {[catch {set me [exec git var GIT_COMMITTER_IDENT]} err]} {
969                         error_popup "Unable to obtain your identity:\n\n$err"
970                         return {}
971                 }
972                 if {![regexp {^(.*) [0-9]+ [-+0-9]+$} \
973                         $me me GIT_COMMITTER_IDENT]} {
974                         error_popup "Invalid GIT_COMMITTER_IDENT:\n\n$me"
975                         return {}
976                 }
977         }
979         return $GIT_COMMITTER_IDENT
982 proc commit_tree {} {
983         global HEAD commit_type file_states ui_comm repo_config
984         global ui_status_value pch_error
986         if {![lock_index update]} return
987         if {[committer_ident] eq {}} return
989         # -- Our in memory state should match the repository.
990         #
991         repository_state curType curHEAD curMERGE_HEAD
992         if {[string match amend* $commit_type]
993                 && $curType eq {normal}
994                 && $curHEAD eq $HEAD} {
995         } elseif {$commit_type ne $curType || $HEAD ne $curHEAD} {
996                 info_popup {Last scanned state does not match repository state.
998 Another Git program has modified this repository
999 since the last scan.  A rescan must be performed
1000 before another commit can be created.
1002 The rescan will be automatically started now.
1004                 unlock_index
1005                 rescan {set ui_status_value {Ready.}}
1006                 return
1007         }
1009         # -- At least one file should differ in the index.
1010         #
1011         set files_ready 0
1012         foreach path [array names file_states] {
1013                 switch -glob -- [lindex $file_states($path) 0] {
1014                 _? {continue}
1015                 A? -
1016                 D? -
1017                 M? {set files_ready 1}
1018                 U? {
1019                         error_popup "Unmerged files cannot be committed.
1021 File [short_path $path] has merge conflicts.
1022 You must resolve them and add the file before committing.
1024                         unlock_index
1025                         return
1026                 }
1027                 default {
1028                         error_popup "Unknown file state [lindex $s 0] detected.
1030 File [short_path $path] cannot be committed by this program.
1032                 }
1033                 }
1034         }
1035         if {!$files_ready} {
1036                 info_popup {No changes to commit.
1038 You must add at least 1 file before you can commit.
1040                 unlock_index
1041                 return
1042         }
1044         # -- A message is required.
1045         #
1046         set msg [string trim [$ui_comm get 1.0 end]]
1047         regsub -all -line {[ \t\r]+$} $msg {} msg
1048         if {$msg eq {}} {
1049                 error_popup {Please supply a commit message.
1051 A good commit message has the following format:
1053 - First line: Describe in one sentance what you did.
1054 - Second line: Blank
1055 - Remaining lines: Describe why this change is good.
1057                 unlock_index
1058                 return
1059         }
1061         # -- Run the pre-commit hook.
1062         #
1063         set pchook [gitdir hooks pre-commit]
1065         # On Cygwin [file executable] might lie so we need to ask
1066         # the shell if the hook is executable.  Yes that's annoying.
1067         #
1068         if {[is_Windows] && [file isfile $pchook]} {
1069                 set pchook [list sh -c [concat \
1070                         "if test -x \"$pchook\";" \
1071                         "then exec \"$pchook\" 2>&1;" \
1072                         "fi"]]
1073         } elseif {[file executable $pchook]} {
1074                 set pchook [list $pchook |& cat]
1075         } else {
1076                 commit_writetree $curHEAD $msg
1077                 return
1078         }
1080         set ui_status_value {Calling pre-commit hook...}
1081         set pch_error {}
1082         set fd_ph [open "| $pchook" r]
1083         fconfigure $fd_ph -blocking 0 -translation binary
1084         fileevent $fd_ph readable \
1085                 [list commit_prehook_wait $fd_ph $curHEAD $msg]
1088 proc commit_prehook_wait {fd_ph curHEAD msg} {
1089         global pch_error ui_status_value
1091         append pch_error [read $fd_ph]
1092         fconfigure $fd_ph -blocking 1
1093         if {[eof $fd_ph]} {
1094                 if {[catch {close $fd_ph}]} {
1095                         set ui_status_value {Commit declined by pre-commit hook.}
1096                         hook_failed_popup pre-commit $pch_error
1097                         unlock_index
1098                 } else {
1099                         commit_writetree $curHEAD $msg
1100                 }
1101                 set pch_error {}
1102                 return
1103         }
1104         fconfigure $fd_ph -blocking 0
1107 proc commit_writetree {curHEAD msg} {
1108         global ui_status_value
1110         set ui_status_value {Committing changes...}
1111         set fd_wt [open "| git write-tree" r]
1112         fileevent $fd_wt readable \
1113                 [list commit_committree $fd_wt $curHEAD $msg]
1116 proc commit_committree {fd_wt curHEAD msg} {
1117         global HEAD PARENT MERGE_HEAD commit_type
1118         global single_commit all_heads current_branch
1119         global ui_status_value ui_comm selected_commit_type
1120         global file_states selected_paths rescan_active
1121         global repo_config
1123         gets $fd_wt tree_id
1124         if {$tree_id eq {} || [catch {close $fd_wt} err]} {
1125                 error_popup "write-tree failed:\n\n$err"
1126                 set ui_status_value {Commit failed.}
1127                 unlock_index
1128                 return
1129         }
1131         # -- Build the message.
1132         #
1133         set msg_p [gitdir COMMIT_EDITMSG]
1134         set msg_wt [open $msg_p w]
1135         if {[catch {set enc $repo_config(i18n.commitencoding)}]} {
1136                 set enc utf-8
1137         }
1138         fconfigure $msg_wt -encoding $enc -translation binary
1139         puts -nonewline $msg_wt $msg
1140         close $msg_wt
1142         # -- Create the commit.
1143         #
1144         set cmd [list git commit-tree $tree_id]
1145         set parents [concat $PARENT $MERGE_HEAD]
1146         if {[llength $parents] > 0} {
1147                 foreach p $parents {
1148                         lappend cmd -p $p
1149                 }
1150         } else {
1151                 # git commit-tree writes to stderr during initial commit.
1152                 lappend cmd 2>/dev/null
1153         }
1154         lappend cmd <$msg_p
1155         if {[catch {set cmt_id [eval exec $cmd]} err]} {
1156                 error_popup "commit-tree failed:\n\n$err"
1157                 set ui_status_value {Commit failed.}
1158                 unlock_index
1159                 return
1160         }
1162         # -- Update the HEAD ref.
1163         #
1164         set reflogm commit
1165         if {$commit_type ne {normal}} {
1166                 append reflogm " ($commit_type)"
1167         }
1168         set i [string first "\n" $msg]
1169         if {$i >= 0} {
1170                 append reflogm {: } [string range $msg 0 [expr {$i - 1}]]
1171         } else {
1172                 append reflogm {: } $msg
1173         }
1174         set cmd [list git update-ref -m $reflogm HEAD $cmt_id $curHEAD]
1175         if {[catch {eval exec $cmd} err]} {
1176                 error_popup "update-ref failed:\n\n$err"
1177                 set ui_status_value {Commit failed.}
1178                 unlock_index
1179                 return
1180         }
1182         # -- Make sure our current branch exists.
1183         #
1184         if {$commit_type eq {initial}} {
1185                 lappend all_heads $current_branch
1186                 set all_heads [lsort -unique $all_heads]
1187                 populate_branch_menu
1188         }
1190         # -- Cleanup after ourselves.
1191         #
1192         catch {file delete $msg_p}
1193         catch {file delete [gitdir MERGE_HEAD]}
1194         catch {file delete [gitdir MERGE_MSG]}
1195         catch {file delete [gitdir SQUASH_MSG]}
1196         catch {file delete [gitdir GITGUI_MSG]}
1198         # -- Let rerere do its thing.
1199         #
1200         if {[file isdirectory [gitdir rr-cache]]} {
1201                 catch {exec git rerere}
1202         }
1204         # -- Run the post-commit hook.
1205         #
1206         set pchook [gitdir hooks post-commit]
1207         if {[is_Windows] && [file isfile $pchook]} {
1208                 set pchook [list sh -c [concat \
1209                         "if test -x \"$pchook\";" \
1210                         "then exec \"$pchook\";" \
1211                         "fi"]]
1212         } elseif {![file executable $pchook]} {
1213                 set pchook {}
1214         }
1215         if {$pchook ne {}} {
1216                 catch {exec $pchook &}
1217         }
1219         $ui_comm delete 0.0 end
1220         $ui_comm edit reset
1221         $ui_comm edit modified false
1223         if {$single_commit} do_quit
1225         # -- Update in memory status
1226         #
1227         set selected_commit_type new
1228         set commit_type normal
1229         set HEAD $cmt_id
1230         set PARENT $cmt_id
1231         set MERGE_HEAD [list]
1233         foreach path [array names file_states] {
1234                 set s $file_states($path)
1235                 set m [lindex $s 0]
1236                 switch -glob -- $m {
1237                 _O -
1238                 _M -
1239                 _D {continue}
1240                 __ -
1241                 A_ -
1242                 M_ -
1243                 D_ {
1244                         unset file_states($path)
1245                         catch {unset selected_paths($path)}
1246                 }
1247                 DO {
1248                         set file_states($path) [list _O [lindex $s 1] {} {}]
1249                 }
1250                 AM -
1251                 AD -
1252                 MM -
1253                 MD {
1254                         set file_states($path) [list \
1255                                 _[string index $m 1] \
1256                                 [lindex $s 1] \
1257                                 [lindex $s 3] \
1258                                 {}]
1259                 }
1260                 }
1261         }
1263         display_all_files
1264         unlock_index
1265         reshow_diff
1266         set ui_status_value \
1267                 "Changes committed as [string range $cmt_id 0 7]."
1270 ######################################################################
1271 ##
1272 ## fetch push
1274 proc fetch_from {remote} {
1275         set w [new_console \
1276                 "fetch $remote" \
1277                 "Fetching new changes from $remote"]
1278         set cmd [list git fetch]
1279         lappend cmd $remote
1280         console_exec $w $cmd console_done
1283 proc push_to {remote} {
1284         set w [new_console \
1285                 "push $remote" \
1286                 "Pushing changes to $remote"]
1287         set cmd [list git push]
1288         lappend cmd -v
1289         lappend cmd $remote
1290         console_exec $w $cmd console_done
1293 ######################################################################
1294 ##
1295 ## ui helpers
1297 proc mapicon {w state path} {
1298         global all_icons
1300         if {[catch {set r $all_icons($state$w)}]} {
1301                 puts "error: no icon for $w state={$state} $path"
1302                 return file_plain
1303         }
1304         return $r
1307 proc mapdesc {state path} {
1308         global all_descs
1310         if {[catch {set r $all_descs($state)}]} {
1311                 puts "error: no desc for state={$state} $path"
1312                 return $state
1313         }
1314         return $r
1317 proc escape_path {path} {
1318         regsub -all "\n" $path "\\n" path
1319         return $path
1322 proc short_path {path} {
1323         return [escape_path [lindex [file split $path] end]]
1326 set next_icon_id 0
1327 set null_sha1 [string repeat 0 40]
1329 proc merge_state {path new_state {head_info {}} {index_info {}}} {
1330         global file_states next_icon_id null_sha1
1332         set s0 [string index $new_state 0]
1333         set s1 [string index $new_state 1]
1335         if {[catch {set info $file_states($path)}]} {
1336                 set state __
1337                 set icon n[incr next_icon_id]
1338         } else {
1339                 set state [lindex $info 0]
1340                 set icon [lindex $info 1]
1341                 if {$head_info eq {}}  {set head_info  [lindex $info 2]}
1342                 if {$index_info eq {}} {set index_info [lindex $info 3]}
1343         }
1345         if     {$s0 eq {?}} {set s0 [string index $state 0]} \
1346         elseif {$s0 eq {_}} {set s0 _}
1348         if     {$s1 eq {?}} {set s1 [string index $state 1]} \
1349         elseif {$s1 eq {_}} {set s1 _}
1351         if {$s0 eq {A} && $s1 eq {_} && $head_info eq {}} {
1352                 set head_info [list 0 $null_sha1]
1353         } elseif {$s0 ne {_} && [string index $state 0] eq {_}
1354                 && $head_info eq {}} {
1355                 set head_info $index_info
1356         }
1358         set file_states($path) [list $s0$s1 $icon \
1359                 $head_info $index_info \
1360                 ]
1361         return $state
1364 proc display_file_helper {w path icon_name old_m new_m} {
1365         global file_lists
1367         if {$new_m eq {_}} {
1368                 set lno [lsearch -sorted -exact $file_lists($w) $path]
1369                 if {$lno >= 0} {
1370                         set file_lists($w) [lreplace $file_lists($w) $lno $lno]
1371                         incr lno
1372                         $w conf -state normal
1373                         $w delete $lno.0 [expr {$lno + 1}].0
1374                         $w conf -state disabled
1375                 }
1376         } elseif {$old_m eq {_} && $new_m ne {_}} {
1377                 lappend file_lists($w) $path
1378                 set file_lists($w) [lsort -unique $file_lists($w)]
1379                 set lno [lsearch -sorted -exact $file_lists($w) $path]
1380                 incr lno
1381                 $w conf -state normal
1382                 $w image create $lno.0 \
1383                         -align center -padx 5 -pady 1 \
1384                         -name $icon_name \
1385                         -image [mapicon $w $new_m $path]
1386                 $w insert $lno.1 "[escape_path $path]\n"
1387                 $w conf -state disabled
1388         } elseif {$old_m ne $new_m} {
1389                 $w conf -state normal
1390                 $w image conf $icon_name -image [mapicon $w $new_m $path]
1391                 $w conf -state disabled
1392         }
1395 proc display_file {path state} {
1396         global file_states selected_paths
1397         global ui_index ui_workdir
1399         set old_m [merge_state $path $state]
1400         set s $file_states($path)
1401         set new_m [lindex $s 0]
1402         set icon_name [lindex $s 1]
1404         set o [string index $old_m 0]
1405         set n [string index $new_m 0]
1406         if {$o eq {U}} {
1407                 set o _
1408         }
1409         if {$n eq {U}} {
1410                 set n _
1411         }
1412         display_file_helper     $ui_index $path $icon_name $o $n
1414         if {[string index $old_m 0] eq {U}} {
1415                 set o U
1416         } else {
1417                 set o [string index $old_m 1]
1418         }
1419         if {[string index $new_m 0] eq {U}} {
1420                 set n U
1421         } else {
1422                 set n [string index $new_m 1]
1423         }
1424         display_file_helper     $ui_workdir $path $icon_name $o $n
1426         if {$new_m eq {__}} {
1427                 unset file_states($path)
1428                 catch {unset selected_paths($path)}
1429         }
1432 proc display_all_files_helper {w path icon_name m} {
1433         global file_lists
1435         lappend file_lists($w) $path
1436         set lno [expr {[lindex [split [$w index end] .] 0] - 1}]
1437         $w image create end \
1438                 -align center -padx 5 -pady 1 \
1439                 -name $icon_name \
1440                 -image [mapicon $w $m $path]
1441         $w insert end "[escape_path $path]\n"
1444 proc display_all_files {} {
1445         global ui_index ui_workdir
1446         global file_states file_lists
1447         global last_clicked
1449         $ui_index conf -state normal
1450         $ui_workdir conf -state normal
1452         $ui_index delete 0.0 end
1453         $ui_workdir delete 0.0 end
1454         set last_clicked {}
1456         set file_lists($ui_index) [list]
1457         set file_lists($ui_workdir) [list]
1459         foreach path [lsort [array names file_states]] {
1460                 set s $file_states($path)
1461                 set m [lindex $s 0]
1462                 set icon_name [lindex $s 1]
1464                 set s [string index $m 0]
1465                 if {$s ne {U} && $s ne {_}} {
1466                         display_all_files_helper $ui_index $path \
1467                                 $icon_name $s
1468                 }
1470                 if {[string index $m 0] eq {U}} {
1471                         set s U
1472                 } else {
1473                         set s [string index $m 1]
1474                 }
1475                 if {$s ne {_}} {
1476                         display_all_files_helper $ui_workdir $path \
1477                                 $icon_name $s
1478                 }
1479         }
1481         $ui_index conf -state disabled
1482         $ui_workdir conf -state disabled
1485 proc update_indexinfo {msg pathList after} {
1486         global update_index_cp ui_status_value
1488         if {![lock_index update]} return
1490         set update_index_cp 0
1491         set pathList [lsort $pathList]
1492         set totalCnt [llength $pathList]
1493         set batch [expr {int($totalCnt * .01) + 1}]
1494         if {$batch > 25} {set batch 25}
1496         set ui_status_value [format \
1497                 "$msg... %i/%i files (%.2f%%)" \
1498                 $update_index_cp \
1499                 $totalCnt \
1500                 0.0]
1501         set fd [open "| git update-index -z --index-info" w]
1502         fconfigure $fd \
1503                 -blocking 0 \
1504                 -buffering full \
1505                 -buffersize 512 \
1506                 -encoding binary \
1507                 -translation binary
1508         fileevent $fd writable [list \
1509                 write_update_indexinfo \
1510                 $fd \
1511                 $pathList \
1512                 $totalCnt \
1513                 $batch \
1514                 $msg \
1515                 $after \
1516                 ]
1519 proc write_update_indexinfo {fd pathList totalCnt batch msg after} {
1520         global update_index_cp ui_status_value
1521         global file_states current_diff_path
1523         if {$update_index_cp >= $totalCnt} {
1524                 close $fd
1525                 unlock_index
1526                 uplevel #0 $after
1527                 return
1528         }
1530         for {set i $batch} \
1531                 {$update_index_cp < $totalCnt && $i > 0} \
1532                 {incr i -1} {
1533                 set path [lindex $pathList $update_index_cp]
1534                 incr update_index_cp
1536                 set s $file_states($path)
1537                 switch -glob -- [lindex $s 0] {
1538                 A? {set new _O}
1539                 M? {set new _M}
1540                 D_ {set new _D}
1541                 D? {set new _?}
1542                 ?? {continue}
1543                 }
1544                 set info [lindex $s 2]
1545                 if {$info eq {}} continue
1547                 puts -nonewline $fd "$info\t[encoding convertto $path]\0"
1548                 display_file $path $new
1549         }
1551         set ui_status_value [format \
1552                 "$msg... %i/%i files (%.2f%%)" \
1553                 $update_index_cp \
1554                 $totalCnt \
1555                 [expr {100.0 * $update_index_cp / $totalCnt}]]
1558 proc update_index {msg pathList after} {
1559         global update_index_cp ui_status_value
1561         if {![lock_index update]} return
1563         set update_index_cp 0
1564         set pathList [lsort $pathList]
1565         set totalCnt [llength $pathList]
1566         set batch [expr {int($totalCnt * .01) + 1}]
1567         if {$batch > 25} {set batch 25}
1569         set ui_status_value [format \
1570                 "$msg... %i/%i files (%.2f%%)" \
1571                 $update_index_cp \
1572                 $totalCnt \
1573                 0.0]
1574         set fd [open "| git update-index --add --remove -z --stdin" w]
1575         fconfigure $fd \
1576                 -blocking 0 \
1577                 -buffering full \
1578                 -buffersize 512 \
1579                 -encoding binary \
1580                 -translation binary
1581         fileevent $fd writable [list \
1582                 write_update_index \
1583                 $fd \
1584                 $pathList \
1585                 $totalCnt \
1586                 $batch \
1587                 $msg \
1588                 $after \
1589                 ]
1592 proc write_update_index {fd pathList totalCnt batch msg after} {
1593         global update_index_cp ui_status_value
1594         global file_states current_diff_path
1596         if {$update_index_cp >= $totalCnt} {
1597                 close $fd
1598                 unlock_index
1599                 uplevel #0 $after
1600                 return
1601         }
1603         for {set i $batch} \
1604                 {$update_index_cp < $totalCnt && $i > 0} \
1605                 {incr i -1} {
1606                 set path [lindex $pathList $update_index_cp]
1607                 incr update_index_cp
1609                 switch -glob -- [lindex $file_states($path) 0] {
1610                 AD {set new __}
1611                 ?D {set new D_}
1612                 _O -
1613                 AM {set new A_}
1614                 U? {
1615                         if {[file exists $path]} {
1616                                 set new M_
1617                         } else {
1618                                 set new D_
1619                         }
1620                 }
1621                 ?M {set new M_}
1622                 ?? {continue}
1623                 }
1624                 puts -nonewline $fd "[encoding convertto $path]\0"
1625                 display_file $path $new
1626         }
1628         set ui_status_value [format \
1629                 "$msg... %i/%i files (%.2f%%)" \
1630                 $update_index_cp \
1631                 $totalCnt \
1632                 [expr {100.0 * $update_index_cp / $totalCnt}]]
1635 proc checkout_index {msg pathList after} {
1636         global update_index_cp ui_status_value
1638         if {![lock_index update]} return
1640         set update_index_cp 0
1641         set pathList [lsort $pathList]
1642         set totalCnt [llength $pathList]
1643         set batch [expr {int($totalCnt * .01) + 1}]
1644         if {$batch > 25} {set batch 25}
1646         set ui_status_value [format \
1647                 "$msg... %i/%i files (%.2f%%)" \
1648                 $update_index_cp \
1649                 $totalCnt \
1650                 0.0]
1651         set cmd [list git checkout-index]
1652         lappend cmd --index
1653         lappend cmd --quiet
1654         lappend cmd --force
1655         lappend cmd -z
1656         lappend cmd --stdin
1657         set fd [open "| $cmd " w]
1658         fconfigure $fd \
1659                 -blocking 0 \
1660                 -buffering full \
1661                 -buffersize 512 \
1662                 -encoding binary \
1663                 -translation binary
1664         fileevent $fd writable [list \
1665                 write_checkout_index \
1666                 $fd \
1667                 $pathList \
1668                 $totalCnt \
1669                 $batch \
1670                 $msg \
1671                 $after \
1672                 ]
1675 proc write_checkout_index {fd pathList totalCnt batch msg after} {
1676         global update_index_cp ui_status_value
1677         global file_states current_diff_path
1679         if {$update_index_cp >= $totalCnt} {
1680                 close $fd
1681                 unlock_index
1682                 uplevel #0 $after
1683                 return
1684         }
1686         for {set i $batch} \
1687                 {$update_index_cp < $totalCnt && $i > 0} \
1688                 {incr i -1} {
1689                 set path [lindex $pathList $update_index_cp]
1690                 incr update_index_cp
1691                 switch -glob -- [lindex $file_states($path) 0] {
1692                 U? {continue}
1693                 ?M -
1694                 ?D {
1695                         puts -nonewline $fd "[encoding convertto $path]\0"
1696                         display_file $path ?_
1697                 }
1698                 }
1699         }
1701         set ui_status_value [format \
1702                 "$msg... %i/%i files (%.2f%%)" \
1703                 $update_index_cp \
1704                 $totalCnt \
1705                 [expr {100.0 * $update_index_cp / $totalCnt}]]
1708 ######################################################################
1709 ##
1710 ## branch management
1712 proc is_tracking_branch {name} {
1713         global tracking_branches
1715         if {![catch {set info $tracking_branches($name)}]} {
1716                 return 1
1717         }
1718         foreach t [array names tracking_branches] {
1719                 if {[string match {*/\*} $t] && [string match $t $name]} {
1720                         return 1
1721                 }
1722         }
1723         return 0
1726 proc load_all_heads {} {
1727         global all_heads
1729         set all_heads [list]
1730         set fd [open "| git for-each-ref --format=%(refname) refs/heads" r]
1731         while {[gets $fd line] > 0} {
1732                 if {[is_tracking_branch $line]} continue
1733                 if {![regsub ^refs/heads/ $line {} name]} continue
1734                 lappend all_heads $name
1735         }
1736         close $fd
1738         set all_heads [lsort $all_heads]
1741 proc populate_branch_menu {} {
1742         global all_heads disable_on_lock
1744         set m .mbar.branch
1745         set last [$m index last]
1746         for {set i 0} {$i <= $last} {incr i} {
1747                 if {[$m type $i] eq {separator}} {
1748                         $m delete $i last
1749                         set new_dol [list]
1750                         foreach a $disable_on_lock {
1751                                 if {[lindex $a 0] ne $m || [lindex $a 2] < $i} {
1752                                         lappend new_dol $a
1753                                 }
1754                         }
1755                         set disable_on_lock $new_dol
1756                         break
1757                 }
1758         }
1760         if {$all_heads ne {}} {
1761                 $m add separator
1762         }
1763         foreach b $all_heads {
1764                 $m add radiobutton \
1765                         -label $b \
1766                         -command [list switch_branch $b] \
1767                         -variable current_branch \
1768                         -value $b \
1769                         -font font_ui
1770                 lappend disable_on_lock \
1771                         [list $m entryconf [$m index last] -state]
1772         }
1775 proc all_tracking_branches {} {
1776         global tracking_branches
1778         set all_trackings {}
1779         set cmd {}
1780         foreach name [array names tracking_branches] {
1781                 if {[regsub {/\*$} $name {} name]} {
1782                         lappend cmd $name
1783                 } else {
1784                         regsub ^refs/(heads|remotes)/ $name {} name
1785                         lappend all_trackings $name
1786                 }
1787         }
1789         if {$cmd ne {}} {
1790                 set fd [open "| git for-each-ref --format=%(refname) $cmd" r]
1791                 while {[gets $fd name] > 0} {
1792                         regsub ^refs/(heads|remotes)/ $name {} name
1793                         lappend all_trackings $name
1794                 }
1795                 close $fd
1796         }
1798         return [lsort -unique $all_trackings]
1801 proc do_create_branch_action {w} {
1802         global all_heads null_sha1 repo_config
1803         global create_branch_checkout create_branch_revtype
1804         global create_branch_head create_branch_trackinghead
1805         global create_branch_name create_branch_revexp
1807         set newbranch $create_branch_name
1808         if {$newbranch eq {}
1809                 || $newbranch eq $repo_config(gui.newbranchtemplate)} {
1810                 tk_messageBox \
1811                         -icon error \
1812                         -type ok \
1813                         -title [wm title $w] \
1814                         -parent $w \
1815                         -message "Please supply a branch name."
1816                 focus $w.desc.name_t
1817                 return
1818         }
1819         if {![catch {exec git show-ref --verify -- "refs/heads/$newbranch"}]} {
1820                 tk_messageBox \
1821                         -icon error \
1822                         -type ok \
1823                         -title [wm title $w] \
1824                         -parent $w \
1825                         -message "Branch '$newbranch' already exists."
1826                 focus $w.desc.name_t
1827                 return
1828         }
1829         if {[catch {exec git check-ref-format "heads/$newbranch"}]} {
1830                 tk_messageBox \
1831                         -icon error \
1832                         -type ok \
1833                         -title [wm title $w] \
1834                         -parent $w \
1835                         -message "We do not like '$newbranch' as a branch name."
1836                 focus $w.desc.name_t
1837                 return
1838         }
1840         set rev {}
1841         switch -- $create_branch_revtype {
1842         head {set rev $create_branch_head}
1843         tracking {set rev $create_branch_trackinghead}
1844         expression {set rev $create_branch_revexp}
1845         }
1846         if {[catch {set cmt [exec git rev-parse --verify "${rev}^0"]}]} {
1847                 tk_messageBox \
1848                         -icon error \
1849                         -type ok \
1850                         -title [wm title $w] \
1851                         -parent $w \
1852                         -message "Invalid starting revision: $rev"
1853                 return
1854         }
1855         set cmd [list git update-ref]
1856         lappend cmd -m
1857         lappend cmd "branch: Created from $rev"
1858         lappend cmd "refs/heads/$newbranch"
1859         lappend cmd $cmt
1860         lappend cmd $null_sha1
1861         if {[catch {eval exec $cmd} err]} {
1862                 tk_messageBox \
1863                         -icon error \
1864                         -type ok \
1865                         -title [wm title $w] \
1866                         -parent $w \
1867                         -message "Failed to create '$newbranch'.\n\n$err"
1868                 return
1869         }
1871         lappend all_heads $newbranch
1872         set all_heads [lsort $all_heads]
1873         populate_branch_menu
1874         destroy $w
1875         if {$create_branch_checkout} {
1876                 switch_branch $newbranch
1877         }
1880 proc radio_selector {varname value args} {
1881         upvar #0 $varname var
1882         set var $value
1885 trace add variable create_branch_head write \
1886         [list radio_selector create_branch_revtype head]
1887 trace add variable create_branch_trackinghead write \
1888         [list radio_selector create_branch_revtype tracking]
1890 trace add variable delete_branch_head write \
1891         [list radio_selector delete_branch_checktype head]
1892 trace add variable delete_branch_trackinghead write \
1893         [list radio_selector delete_branch_checktype tracking]
1895 proc do_create_branch {} {
1896         global all_heads current_branch repo_config
1897         global create_branch_checkout create_branch_revtype
1898         global create_branch_head create_branch_trackinghead
1899         global create_branch_name create_branch_revexp
1901         set w .branch_editor
1902         toplevel $w
1903         wm geometry $w "+[winfo rootx .]+[winfo rooty .]"
1905         label $w.header -text {Create New Branch} \
1906                 -font font_uibold
1907         pack $w.header -side top -fill x
1909         frame $w.buttons
1910         button $w.buttons.create -text Create \
1911                 -font font_ui \
1912                 -default active \
1913                 -command [list do_create_branch_action $w]
1914         pack $w.buttons.create -side right
1915         button $w.buttons.cancel -text {Cancel} \
1916                 -font font_ui \
1917                 -command [list destroy $w]
1918         pack $w.buttons.cancel -side right -padx 5
1919         pack $w.buttons -side bottom -fill x -pady 10 -padx 10
1921         labelframe $w.desc \
1922                 -text {Branch Description} \
1923                 -font font_ui
1924         label $w.desc.name_l -text {Name:} -font font_ui
1925         entry $w.desc.name_t \
1926                 -borderwidth 1 \
1927                 -relief sunken \
1928                 -width 40 \
1929                 -textvariable create_branch_name \
1930                 -font font_ui \
1931                 -validate key \
1932                 -validatecommand {
1933                         if {%d == 1 && [regexp {[~^:?*\[\0- ]} %S]} {return 0}
1934                         return 1
1935                 }
1936         grid $w.desc.name_l $w.desc.name_t -sticky we -padx {0 5}
1937         grid columnconfigure $w.desc 1 -weight 1
1938         pack $w.desc -anchor nw -fill x -pady 5 -padx 5
1940         labelframe $w.from \
1941                 -text {Starting Revision} \
1942                 -font font_ui
1943         radiobutton $w.from.head_r \
1944                 -text {Local Branch:} \
1945                 -value head \
1946                 -variable create_branch_revtype \
1947                 -font font_ui
1948         eval tk_optionMenu $w.from.head_m create_branch_head $all_heads
1949         grid $w.from.head_r $w.from.head_m -sticky w
1950         set all_trackings [all_tracking_branches]
1951         if {$all_trackings ne {}} {
1952                 set create_branch_trackinghead [lindex $all_trackings 0]
1953                 radiobutton $w.from.tracking_r \
1954                         -text {Tracking Branch:} \
1955                         -value tracking \
1956                         -variable create_branch_revtype \
1957                         -font font_ui
1958                 eval tk_optionMenu $w.from.tracking_m \
1959                         create_branch_trackinghead \
1960                         $all_trackings
1961                 grid $w.from.tracking_r $w.from.tracking_m -sticky w
1962         }
1963         radiobutton $w.from.exp_r \
1964                 -text {Revision Expression:} \
1965                 -value expression \
1966                 -variable create_branch_revtype \
1967                 -font font_ui
1968         entry $w.from.exp_t \
1969                 -borderwidth 1 \
1970                 -relief sunken \
1971                 -width 50 \
1972                 -textvariable create_branch_revexp \
1973                 -font font_ui \
1974                 -validate key \
1975                 -validatecommand {
1976                         if {%d == 1 && [regexp {\s} %S]} {return 0}
1977                         if {%d == 1 && [string length %S] > 0} {
1978                                 set create_branch_revtype expression
1979                         }
1980                         return 1
1981                 }
1982         grid $w.from.exp_r $w.from.exp_t -sticky we -padx {0 5}
1983         grid columnconfigure $w.from 1 -weight 1
1984         pack $w.from -anchor nw -fill x -pady 5 -padx 5
1986         labelframe $w.postActions \
1987                 -text {Post Creation Actions} \
1988                 -font font_ui
1989         checkbutton $w.postActions.checkout \
1990                 -text {Checkout after creation} \
1991                 -variable create_branch_checkout \
1992                 -font font_ui
1993         pack $w.postActions.checkout -anchor nw
1994         pack $w.postActions -anchor nw -fill x -pady 5 -padx 5
1996         set create_branch_checkout 1
1997         set create_branch_head $current_branch
1998         set create_branch_revtype head
1999         set create_branch_name $repo_config(gui.newbranchtemplate)
2000         set create_branch_revexp {}
2002         bind $w <Visibility> "
2003                 grab $w
2004                 $w.desc.name_t icursor end
2005                 focus $w.desc.name_t
2006         "
2007         bind $w <Key-Escape> "destroy $w"
2008         bind $w <Key-Return> "do_create_branch_action $w;break"
2009         wm title $w "[appname] ([reponame]): Create Branch"
2010         tkwait window $w
2013 proc do_delete_branch_action {w} {
2014         global all_heads
2015         global delete_branch_checktype delete_branch_head delete_branch_trackinghead
2017         set check_rev {}
2018         switch -- $delete_branch_checktype {
2019         head {set check_rev $delete_branch_head}
2020         tracking {set check_rev $delete_branch_trackinghead}
2021         always {set check_rev {:none}}
2022         }
2023         if {$check_rev eq {:none}} {
2024                 set check_cmt {}
2025         } elseif {[catch {set check_cmt [exec git rev-parse --verify "${check_rev}^0"]}]} {
2026                 tk_messageBox \
2027                         -icon error \
2028                         -type ok \
2029                         -title [wm title $w] \
2030                         -parent $w \
2031                         -message "Invalid check revision: $check_rev"
2032                 return
2033         }
2035         set to_delete [list]
2036         set not_merged [list]
2037         foreach i [$w.list.l curselection] {
2038                 set b [$w.list.l get $i]
2039                 if {[catch {set o [exec git rev-parse --verify $b]}]} continue
2040                 if {$check_cmt ne {}} {
2041                         if {$b eq $check_rev} continue
2042                         if {[catch {set m [exec git merge-base $o $check_cmt]}]} continue
2043                         if {$o ne $m} {
2044                                 lappend not_merged $b
2045                                 continue
2046                         }
2047                 }
2048                 lappend to_delete [list $b $o]
2049         }
2050         if {$not_merged ne {}} {
2051                 set msg "The following branches are not completely merged into $check_rev:
2053  - [join $not_merged "\n - "]"
2054                 tk_messageBox \
2055                         -icon info \
2056                         -type ok \
2057                         -title [wm title $w] \
2058                         -parent $w \
2059                         -message $msg
2060         }
2061         if {$to_delete eq {}} return
2062         if {$delete_branch_checktype eq {always}} {
2063                 set msg {Recovering deleted branches is difficult.
2065 Delete the selected branches?}
2066                 if {[tk_messageBox \
2067                         -icon warning \
2068                         -type yesno \
2069                         -title [wm title $w] \
2070                         -parent $w \
2071                         -message $msg] ne yes} {
2072                         return
2073                 }
2074         }
2076         set failed {}
2077         foreach i $to_delete {
2078                 set b [lindex $i 0]
2079                 set o [lindex $i 1]
2080                 if {[catch {exec git update-ref -d "refs/heads/$b" $o} err]} {
2081                         append failed " - $b: $err\n"
2082                 } else {
2083                         set x [lsearch -sorted -exact $all_heads $b]
2084                         if {$x >= 0} {
2085                                 set all_heads [lreplace $all_heads $x $x]
2086                         }
2087                 }
2088         }
2090         if {$failed ne {}} {
2091                 tk_messageBox \
2092                         -icon error \
2093                         -type ok \
2094                         -title [wm title $w] \
2095                         -parent $w \
2096                         -message "Failed to delete branches:\n$failed"
2097         }
2099         set all_heads [lsort $all_heads]
2100         populate_branch_menu
2101         destroy $w
2104 proc do_delete_branch {} {
2105         global all_heads tracking_branches current_branch
2106         global delete_branch_checktype delete_branch_head delete_branch_trackinghead
2108         set w .branch_editor
2109         toplevel $w
2110         wm geometry $w "+[winfo rootx .]+[winfo rooty .]"
2112         label $w.header -text {Delete Local Branch} \
2113                 -font font_uibold
2114         pack $w.header -side top -fill x
2116         frame $w.buttons
2117         button $w.buttons.create -text Delete \
2118                 -font font_ui \
2119                 -command [list do_delete_branch_action $w]
2120         pack $w.buttons.create -side right
2121         button $w.buttons.cancel -text {Cancel} \
2122                 -font font_ui \
2123                 -command [list destroy $w]
2124         pack $w.buttons.cancel -side right -padx 5
2125         pack $w.buttons -side bottom -fill x -pady 10 -padx 10
2127         labelframe $w.list \
2128                 -text {Local Branches} \
2129                 -font font_ui
2130         listbox $w.list.l \
2131                 -height 10 \
2132                 -width 50 \
2133                 -selectmode extended \
2134                 -font font_ui
2135         foreach h $all_heads {
2136                 if {$h ne $current_branch} {
2137                         $w.list.l insert end $h
2138                 }
2139         }
2140         pack $w.list.l -fill both -pady 5 -padx 5
2141         pack $w.list -fill both -pady 5 -padx 5
2143         labelframe $w.validate \
2144                 -text {Delete Only If} \
2145                 -font font_ui
2146         radiobutton $w.validate.head_r \
2147                 -text {Merged Into Local Branch:} \
2148                 -value head \
2149                 -variable delete_branch_checktype \
2150                 -font font_ui
2151         eval tk_optionMenu $w.validate.head_m delete_branch_head $all_heads
2152         grid $w.validate.head_r $w.validate.head_m -sticky w
2153         set all_trackings [all_tracking_branches]
2154         if {$all_trackings ne {}} {
2155                 set delete_branch_trackinghead [lindex $all_trackings 0]
2156                 radiobutton $w.validate.tracking_r \
2157                         -text {Merged Into Tracking Branch:} \
2158                         -value tracking \
2159                         -variable delete_branch_checktype \
2160                         -font font_ui
2161                 eval tk_optionMenu $w.validate.tracking_m \
2162                         delete_branch_trackinghead \
2163                         $all_trackings
2164                 grid $w.validate.tracking_r $w.validate.tracking_m -sticky w
2165         }
2166         radiobutton $w.validate.always_r \
2167                 -text {Always (Do not perform merge checks)} \
2168                 -value always \
2169                 -variable delete_branch_checktype \
2170                 -font font_ui
2171         grid $w.validate.always_r -columnspan 2 -sticky w
2172         grid columnconfigure $w.validate 1 -weight 1
2173         pack $w.validate -anchor nw -fill x -pady 5 -padx 5
2175         set delete_branch_head $current_branch
2176         set delete_branch_checktype head
2178         bind $w <Visibility> "grab $w; focus $w"
2179         bind $w <Key-Escape> "destroy $w"
2180         wm title $w "[appname] ([reponame]): Delete Branch"
2181         tkwait window $w
2184 proc switch_branch {new_branch} {
2185         global HEAD commit_type current_branch repo_config
2187         if {![lock_index switch]} return
2189         # -- Our in memory state should match the repository.
2190         #
2191         repository_state curType curHEAD curMERGE_HEAD
2192         if {[string match amend* $commit_type]
2193                 && $curType eq {normal}
2194                 && $curHEAD eq $HEAD} {
2195         } elseif {$commit_type ne $curType || $HEAD ne $curHEAD} {
2196                 info_popup {Last scanned state does not match repository state.
2198 Another Git program has modified this repository
2199 since the last scan.  A rescan must be performed
2200 before the current branch can be changed.
2202 The rescan will be automatically started now.
2204                 unlock_index
2205                 rescan {set ui_status_value {Ready.}}
2206                 return
2207         }
2209         # -- Don't do a pointless switch.
2210         #
2211         if {$current_branch eq $new_branch} {
2212                 unlock_index
2213                 return
2214         }
2216         if {$repo_config(gui.trustmtime) eq {true}} {
2217                 switch_branch_stage2 {} $new_branch
2218         } else {
2219                 set ui_status_value {Refreshing file status...}
2220                 set cmd [list git update-index]
2221                 lappend cmd -q
2222                 lappend cmd --unmerged
2223                 lappend cmd --ignore-missing
2224                 lappend cmd --refresh
2225                 set fd_rf [open "| $cmd" r]
2226                 fconfigure $fd_rf -blocking 0 -translation binary
2227                 fileevent $fd_rf readable \
2228                         [list switch_branch_stage2 $fd_rf $new_branch]
2229         }
2232 proc switch_branch_stage2 {fd_rf new_branch} {
2233         global ui_status_value HEAD
2235         if {$fd_rf ne {}} {
2236                 read $fd_rf
2237                 if {![eof $fd_rf]} return
2238                 close $fd_rf
2239         }
2241         set ui_status_value "Updating working directory to '$new_branch'..."
2242         set cmd [list git read-tree]
2243         lappend cmd -m
2244         lappend cmd -u
2245         lappend cmd --exclude-per-directory=.gitignore
2246         lappend cmd $HEAD
2247         lappend cmd $new_branch
2248         set fd_rt [open "| $cmd" r]
2249         fconfigure $fd_rt -blocking 0 -translation binary
2250         fileevent $fd_rt readable \
2251                 [list switch_branch_readtree_wait $fd_rt $new_branch]
2254 proc switch_branch_readtree_wait {fd_rt new_branch} {
2255         global selected_commit_type commit_type HEAD MERGE_HEAD PARENT
2256         global current_branch
2257         global ui_comm ui_status_value
2259         # -- We never get interesting output on stdout; only stderr.
2260         #
2261         read $fd_rt
2262         fconfigure $fd_rt -blocking 1
2263         if {![eof $fd_rt]} {
2264                 fconfigure $fd_rt -blocking 0
2265                 return
2266         }
2268         # -- The working directory wasn't in sync with the index and
2269         #    we'd have to overwrite something to make the switch. A
2270         #    merge is required.
2271         #
2272         if {[catch {close $fd_rt} err]} {
2273                 regsub {^fatal: } $err {} err
2274                 warn_popup "File level merge required.
2276 $err
2278 Staying on branch '$current_branch'."
2279                 set ui_status_value "Aborted checkout of '$new_branch' (file level merging is required)."
2280                 unlock_index
2281                 return
2282         }
2284         # -- Update the symbolic ref.  Core git doesn't even check for failure
2285         #    here, it Just Works(tm).  If it doesn't we are in some really ugly
2286         #    state that is difficult to recover from within git-gui.
2287         #
2288         if {[catch {exec git symbolic-ref HEAD "refs/heads/$new_branch"} err]} {
2289                 error_popup "Failed to set current branch.
2291 This working directory is only partially switched.
2292 We successfully updated your files, but failed to
2293 update an internal Git file.
2295 This should not have occurred.  [appname] will now
2296 close and give up.
2298 $err"
2299                 do_quit
2300                 return
2301         }
2303         # -- Update our repository state.  If we were previously in amend mode
2304         #    we need to toss the current buffer and do a full rescan to update
2305         #    our file lists.  If we weren't in amend mode our file lists are
2306         #    accurate and we can avoid the rescan.
2307         #
2308         unlock_index
2309         set selected_commit_type new
2310         if {[string match amend* $commit_type]} {
2311                 $ui_comm delete 0.0 end
2312                 $ui_comm edit reset
2313                 $ui_comm edit modified false
2314                 rescan {set ui_status_value "Checked out branch '$current_branch'."}
2315         } else {
2316                 repository_state commit_type HEAD MERGE_HEAD
2317                 set PARENT $HEAD
2318                 set ui_status_value "Checked out branch '$current_branch'."
2319         }
2322 ######################################################################
2323 ##
2324 ## remote management
2326 proc load_all_remotes {} {
2327         global repo_config
2328         global all_remotes tracking_branches
2330         set all_remotes [list]
2331         array unset tracking_branches
2333         set rm_dir [gitdir remotes]
2334         if {[file isdirectory $rm_dir]} {
2335                 set all_remotes [glob \
2336                         -types f \
2337                         -tails \
2338                         -nocomplain \
2339                         -directory $rm_dir *]
2341                 foreach name $all_remotes {
2342                         catch {
2343                                 set fd [open [file join $rm_dir $name] r]
2344                                 while {[gets $fd line] >= 0} {
2345                                         if {![regexp {^Pull:[   ]*([^:]+):(.+)$} \
2346                                                 $line line src dst]} continue
2347                                         if {![regexp ^refs/ $dst]} {
2348                                                 set dst "refs/heads/$dst"
2349                                         }
2350                                         set tracking_branches($dst) [list $name $src]
2351                                 }
2352                                 close $fd
2353                         }
2354                 }
2355         }
2357         foreach line [array names repo_config remote.*.url] {
2358                 if {![regexp ^remote\.(.*)\.url\$ $line line name]} continue
2359                 lappend all_remotes $name
2361                 if {[catch {set fl $repo_config(remote.$name.fetch)}]} {
2362                         set fl {}
2363                 }
2364                 foreach line $fl {
2365                         if {![regexp {^([^:]+):(.+)$} $line line src dst]} continue
2366                         if {![regexp ^refs/ $dst]} {
2367                                 set dst "refs/heads/$dst"
2368                         }
2369                         set tracking_branches($dst) [list $name $src]
2370                 }
2371         }
2373         set all_remotes [lsort -unique $all_remotes]
2376 proc populate_fetch_menu {} {
2377         global all_remotes repo_config
2379         set m .mbar.fetch
2380         foreach r $all_remotes {
2381                 set enable 0
2382                 if {![catch {set a $repo_config(remote.$r.url)}]} {
2383                         if {![catch {set a $repo_config(remote.$r.fetch)}]} {
2384                                 set enable 1
2385                         }
2386                 } else {
2387                         catch {
2388                                 set fd [open [gitdir remotes $r] r]
2389                                 while {[gets $fd n] >= 0} {
2390                                         if {[regexp {^Pull:[ \t]*([^:]+):} $n]} {
2391                                                 set enable 1
2392                                                 break
2393                                         }
2394                                 }
2395                                 close $fd
2396                         }
2397                 }
2399                 if {$enable} {
2400                         $m add command \
2401                                 -label "Fetch from $r..." \
2402                                 -command [list fetch_from $r] \
2403                                 -font font_ui
2404                 }
2405         }
2408 proc populate_push_menu {} {
2409         global all_remotes repo_config
2411         set m .mbar.push
2412         set fast_count 0
2413         foreach r $all_remotes {
2414                 set enable 0
2415                 if {![catch {set a $repo_config(remote.$r.url)}]} {
2416                         if {![catch {set a $repo_config(remote.$r.push)}]} {
2417                                 set enable 1
2418                         }
2419                 } else {
2420                         catch {
2421                                 set fd [open [gitdir remotes $r] r]
2422                                 while {[gets $fd n] >= 0} {
2423                                         if {[regexp {^Push:[ \t]*([^:]+):} $n]} {
2424                                                 set enable 1
2425                                                 break
2426                                         }
2427                                 }
2428                                 close $fd
2429                         }
2430                 }
2432                 if {$enable} {
2433                         if {!$fast_count} {
2434                                 $m add separator
2435                         }
2436                         $m add command \
2437                                 -label "Push to $r..." \
2438                                 -command [list push_to $r] \
2439                                 -font font_ui
2440                         incr fast_count
2441                 }
2442         }
2445 proc start_push_anywhere_action {w} {
2446         global push_urltype push_remote push_url push_thin push_tags
2448         set r_url {}
2449         switch -- $push_urltype {
2450         remote {set r_url $push_remote}
2451         url {set r_url $push_url}
2452         }
2453         if {$r_url eq {}} return
2455         set cmd [list git push]
2456         lappend cmd -v
2457         if {$push_thin} {
2458                 lappend cmd --thin
2459         }
2460         if {$push_tags} {
2461                 lappend cmd --tags
2462         }
2463         lappend cmd $r_url
2464         set cnt 0
2465         foreach i [$w.source.l curselection] {
2466                 set b [$w.source.l get $i]
2467                 lappend cmd "refs/heads/$b:refs/heads/$b"
2468                 incr cnt
2469         }
2470         if {$cnt == 0} {
2471                 return
2472         } elseif {$cnt == 1} {
2473                 set unit branch
2474         } else {
2475                 set unit branches
2476         }
2478         set cons [new_console "push $r_url" "Pushing $cnt $unit to $r_url"]
2479         console_exec $cons $cmd console_done
2480         destroy $w
2483 trace add variable push_remote write \
2484         [list radio_selector push_urltype remote]
2486 proc do_push_anywhere {} {
2487         global all_heads all_remotes current_branch
2488         global push_urltype push_remote push_url push_thin push_tags
2490         set w .push_setup
2491         toplevel $w
2492         wm geometry $w "+[winfo rootx .]+[winfo rooty .]"
2494         label $w.header -text {Push Branches} -font font_uibold
2495         pack $w.header -side top -fill x
2497         frame $w.buttons
2498         button $w.buttons.create -text Push \
2499                 -font font_ui \
2500                 -command [list start_push_anywhere_action $w]
2501         pack $w.buttons.create -side right
2502         button $w.buttons.cancel -text {Cancel} \
2503                 -font font_ui \
2504                 -command [list destroy $w]
2505         pack $w.buttons.cancel -side right -padx 5
2506         pack $w.buttons -side bottom -fill x -pady 10 -padx 10
2508         labelframe $w.source \
2509                 -text {Source Branches} \
2510                 -font font_ui
2511         listbox $w.source.l \
2512                 -height 10 \
2513                 -width 50 \
2514                 -selectmode extended \
2515                 -font font_ui
2516         foreach h $all_heads {
2517                 $w.source.l insert end $h
2518                 if {$h eq $current_branch} {
2519                         $w.source.l select set end
2520                 }
2521         }
2522         pack $w.source.l -fill both -pady 5 -padx 5
2523         pack $w.source -fill both -pady 5 -padx 5
2525         labelframe $w.dest \
2526                 -text {Destination Repository} \
2527                 -font font_ui
2528         if {$all_remotes ne {}} {
2529                 radiobutton $w.dest.remote_r \
2530                         -text {Remote:} \
2531                         -value remote \
2532                         -variable push_urltype \
2533                         -font font_ui
2534                 eval tk_optionMenu $w.dest.remote_m push_remote $all_remotes
2535                 grid $w.dest.remote_r $w.dest.remote_m -sticky w
2536                 if {[lsearch -sorted -exact $all_remotes origin] != -1} {
2537                         set push_remote origin
2538                 } else {
2539                         set push_remote [lindex $all_remotes 0]
2540                 }
2541                 set push_urltype remote
2542         } else {
2543                 set push_urltype url
2544         }
2545         radiobutton $w.dest.url_r \
2546                 -text {Arbitrary URL:} \
2547                 -value url \
2548                 -variable push_urltype \
2549                 -font font_ui
2550         entry $w.dest.url_t \
2551                 -borderwidth 1 \
2552                 -relief sunken \
2553                 -width 50 \
2554                 -textvariable push_url \
2555                 -font font_ui \
2556                 -validate key \
2557                 -validatecommand {
2558                         if {%d == 1 && [regexp {\s} %S]} {return 0}
2559                         if {%d == 1 && [string length %S] > 0} {
2560                                 set push_urltype url
2561                         }
2562                         return 1
2563                 }
2564         grid $w.dest.url_r $w.dest.url_t -sticky we -padx {0 5}
2565         grid columnconfigure $w.dest 1 -weight 1
2566         pack $w.dest -anchor nw -fill x -pady 5 -padx 5
2568         labelframe $w.options \
2569                 -text {Transfer Options} \
2570                 -font font_ui
2571         checkbutton $w.options.thin \
2572                 -text {Use thin pack (for slow network connections)} \
2573                 -variable push_thin \
2574                 -font font_ui
2575         grid $w.options.thin -columnspan 2 -sticky w
2576         checkbutton $w.options.tags \
2577                 -text {Include tags} \
2578                 -variable push_tags \
2579                 -font font_ui
2580         grid $w.options.tags -columnspan 2 -sticky w
2581         grid columnconfigure $w.options 1 -weight 1
2582         pack $w.options -anchor nw -fill x -pady 5 -padx 5
2584         set push_url {}
2585         set push_thin 0
2586         set push_tags 0
2588         bind $w <Visibility> "grab $w"
2589         bind $w <Key-Escape> "destroy $w"
2590         wm title $w "[appname] ([reponame]): Push"
2591         tkwait window $w
2594 ######################################################################
2595 ##
2596 ## icons
2598 set filemask {
2599 #define mask_width 14
2600 #define mask_height 15
2601 static unsigned char mask_bits[] = {
2602    0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f,
2603    0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f,
2604    0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f};
2607 image create bitmap file_plain -background white -foreground black -data {
2608 #define plain_width 14
2609 #define plain_height 15
2610 static unsigned char plain_bits[] = {
2611    0xfe, 0x01, 0x02, 0x03, 0x02, 0x05, 0x02, 0x09, 0x02, 0x1f, 0x02, 0x10,
2612    0x02, 0x10, 0x02, 0x10, 0x02, 0x10, 0x02, 0x10, 0x02, 0x10, 0x02, 0x10,
2613    0x02, 0x10, 0x02, 0x10, 0xfe, 0x1f};
2614 } -maskdata $filemask
2616 image create bitmap file_mod -background white -foreground blue -data {
2617 #define mod_width 14
2618 #define mod_height 15
2619 static unsigned char mod_bits[] = {
2620    0xfe, 0x01, 0x02, 0x03, 0x7a, 0x05, 0x02, 0x09, 0x7a, 0x1f, 0x02, 0x10,
2621    0xfa, 0x17, 0x02, 0x10, 0xfa, 0x17, 0x02, 0x10, 0xfa, 0x17, 0x02, 0x10,
2622    0xfa, 0x17, 0x02, 0x10, 0xfe, 0x1f};
2623 } -maskdata $filemask
2625 image create bitmap file_fulltick -background white -foreground "#007000" -data {
2626 #define file_fulltick_width 14
2627 #define file_fulltick_height 15
2628 static unsigned char file_fulltick_bits[] = {
2629    0xfe, 0x01, 0x02, 0x1a, 0x02, 0x0c, 0x02, 0x0c, 0x02, 0x16, 0x02, 0x16,
2630    0x02, 0x13, 0x00, 0x13, 0x86, 0x11, 0x8c, 0x11, 0xd8, 0x10, 0xf2, 0x10,
2631    0x62, 0x10, 0x02, 0x10, 0xfe, 0x1f};
2632 } -maskdata $filemask
2634 image create bitmap file_parttick -background white -foreground "#005050" -data {
2635 #define parttick_width 14
2636 #define parttick_height 15
2637 static unsigned char parttick_bits[] = {
2638    0xfe, 0x01, 0x02, 0x03, 0x7a, 0x05, 0x02, 0x09, 0x7a, 0x1f, 0x02, 0x10,
2639    0x7a, 0x14, 0x02, 0x16, 0x02, 0x13, 0x8a, 0x11, 0xda, 0x10, 0x72, 0x10,
2640    0x22, 0x10, 0x02, 0x10, 0xfe, 0x1f};
2641 } -maskdata $filemask
2643 image create bitmap file_question -background white -foreground black -data {
2644 #define file_question_width 14
2645 #define file_question_height 15
2646 static unsigned char file_question_bits[] = {
2647    0xfe, 0x01, 0x02, 0x02, 0xe2, 0x04, 0xf2, 0x09, 0x1a, 0x1b, 0x0a, 0x13,
2648    0x82, 0x11, 0xc2, 0x10, 0x62, 0x10, 0x62, 0x10, 0x02, 0x10, 0x62, 0x10,
2649    0x62, 0x10, 0x02, 0x10, 0xfe, 0x1f};
2650 } -maskdata $filemask
2652 image create bitmap file_removed -background white -foreground red -data {
2653 #define file_removed_width 14
2654 #define file_removed_height 15
2655 static unsigned char file_removed_bits[] = {
2656    0xfe, 0x01, 0x02, 0x03, 0x02, 0x05, 0x02, 0x09, 0x02, 0x1f, 0x02, 0x10,
2657    0x1a, 0x16, 0x32, 0x13, 0xe2, 0x11, 0xc2, 0x10, 0xe2, 0x11, 0x32, 0x13,
2658    0x1a, 0x16, 0x02, 0x10, 0xfe, 0x1f};
2659 } -maskdata $filemask
2661 image create bitmap file_merge -background white -foreground blue -data {
2662 #define file_merge_width 14
2663 #define file_merge_height 15
2664 static unsigned char file_merge_bits[] = {
2665    0xfe, 0x01, 0x02, 0x03, 0x62, 0x05, 0x62, 0x09, 0x62, 0x1f, 0x62, 0x10,
2666    0xfa, 0x11, 0xf2, 0x10, 0x62, 0x10, 0x02, 0x10, 0xfa, 0x17, 0x02, 0x10,
2667    0xfa, 0x17, 0x02, 0x10, 0xfe, 0x1f};
2668 } -maskdata $filemask
2670 set ui_index .vpane.files.index.list
2671 set ui_workdir .vpane.files.workdir.list
2673 set all_icons(_$ui_index)   file_plain
2674 set all_icons(A$ui_index)   file_fulltick
2675 set all_icons(M$ui_index)   file_fulltick
2676 set all_icons(D$ui_index)   file_removed
2677 set all_icons(U$ui_index)   file_merge
2679 set all_icons(_$ui_workdir) file_plain
2680 set all_icons(M$ui_workdir) file_mod
2681 set all_icons(D$ui_workdir) file_question
2682 set all_icons(U$ui_workdir) file_merge
2683 set all_icons(O$ui_workdir) file_plain
2685 set max_status_desc 0
2686 foreach i {
2687                 {__ "Unmodified"}
2689                 {_M "Modified, not staged"}
2690                 {M_ "Staged for commit"}
2691                 {MM "Portions staged for commit"}
2692                 {MD "Staged for commit, missing"}
2694                 {_O "Untracked, not staged"}
2695                 {A_ "Staged for commit"}
2696                 {AM "Portions staged for commit"}
2697                 {AD "Staged for commit, missing"}
2699                 {_D "Missing"}
2700                 {D_ "Staged for removal"}
2701                 {DO "Staged for removal, still present"}
2703                 {U_ "Requires merge resolution"}
2704                 {UU "Requires merge resolution"}
2705                 {UM "Requires merge resolution"}
2706                 {UD "Requires merge resolution"}
2707         } {
2708         if {$max_status_desc < [string length [lindex $i 1]]} {
2709                 set max_status_desc [string length [lindex $i 1]]
2710         }
2711         set all_descs([lindex $i 0]) [lindex $i 1]
2713 unset i
2715 ######################################################################
2716 ##
2717 ## util
2719 proc is_MacOSX {} {
2720         global tcl_platform tk_library
2721         if {[tk windowingsystem] eq {aqua}} {
2722                 return 1
2723         }
2724         return 0
2727 proc is_Windows {} {
2728         global tcl_platform
2729         if {$tcl_platform(platform) eq {windows}} {
2730                 return 1
2731         }
2732         return 0
2735 proc bind_button3 {w cmd} {
2736         bind $w <Any-Button-3> $cmd
2737         if {[is_MacOSX]} {
2738                 bind $w <Control-Button-1> $cmd
2739         }
2742 proc incr_font_size {font {amt 1}} {
2743         set sz [font configure $font -size]
2744         incr sz $amt
2745         font configure $font -size $sz
2746         font configure ${font}bold -size $sz
2749 proc hook_failed_popup {hook msg} {
2750         set w .hookfail
2751         toplevel $w
2753         frame $w.m
2754         label $w.m.l1 -text "$hook hook failed:" \
2755                 -anchor w \
2756                 -justify left \
2757                 -font font_uibold
2758         text $w.m.t \
2759                 -background white -borderwidth 1 \
2760                 -relief sunken \
2761                 -width 80 -height 10 \
2762                 -font font_diff \
2763                 -yscrollcommand [list $w.m.sby set]
2764         label $w.m.l2 \
2765                 -text {You must correct the above errors before committing.} \
2766                 -anchor w \
2767                 -justify left \
2768                 -font font_uibold
2769         scrollbar $w.m.sby -command [list $w.m.t yview]
2770         pack $w.m.l1 -side top -fill x
2771         pack $w.m.l2 -side bottom -fill x
2772         pack $w.m.sby -side right -fill y
2773         pack $w.m.t -side left -fill both -expand 1
2774         pack $w.m -side top -fill both -expand 1 -padx 5 -pady 10
2776         $w.m.t insert 1.0 $msg
2777         $w.m.t conf -state disabled
2779         button $w.ok -text OK \
2780                 -width 15 \
2781                 -font font_ui \
2782                 -command "destroy $w"
2783         pack $w.ok -side bottom -anchor e -pady 10 -padx 10
2785         bind $w <Visibility> "grab $w; focus $w"
2786         bind $w <Key-Return> "destroy $w"
2787         wm title $w "[appname] ([reponame]): error"
2788         tkwait window $w
2791 set next_console_id 0
2793 proc new_console {short_title long_title} {
2794         global next_console_id console_data
2795         set w .console[incr next_console_id]
2796         set console_data($w) [list $short_title $long_title]
2797         return [console_init $w]
2800 proc console_init {w} {
2801         global console_cr console_data M1B
2803         set console_cr($w) 1.0
2804         toplevel $w
2805         frame $w.m
2806         label $w.m.l1 -text "[lindex $console_data($w) 1]:" \
2807                 -anchor w \
2808                 -justify left \
2809                 -font font_uibold
2810         text $w.m.t \
2811                 -background white -borderwidth 1 \
2812                 -relief sunken \
2813                 -width 80 -height 10 \
2814                 -font font_diff \
2815                 -state disabled \
2816                 -yscrollcommand [list $w.m.sby set]
2817         label $w.m.s -text {Working... please wait...} \
2818                 -anchor w \
2819                 -justify left \
2820                 -font font_uibold
2821         scrollbar $w.m.sby -command [list $w.m.t yview]
2822         pack $w.m.l1 -side top -fill x
2823         pack $w.m.s -side bottom -fill x
2824         pack $w.m.sby -side right -fill y
2825         pack $w.m.t -side left -fill both -expand 1
2826         pack $w.m -side top -fill both -expand 1 -padx 5 -pady 10
2828         menu $w.ctxm -tearoff 0
2829         $w.ctxm add command -label "Copy" \
2830                 -font font_ui \
2831                 -command "tk_textCopy $w.m.t"
2832         $w.ctxm add command -label "Select All" \
2833                 -font font_ui \
2834                 -command "focus $w.m.t;$w.m.t tag add sel 0.0 end"
2835         $w.ctxm add command -label "Copy All" \
2836                 -font font_ui \
2837                 -command "
2838                         $w.m.t tag add sel 0.0 end
2839                         tk_textCopy $w.m.t
2840                         $w.m.t tag remove sel 0.0 end
2841                 "
2843         button $w.ok -text {Close} \
2844                 -font font_ui \
2845                 -state disabled \
2846                 -command "destroy $w"
2847         pack $w.ok -side bottom -anchor e -pady 10 -padx 10
2849         bind_button3 $w.m.t "tk_popup $w.ctxm %X %Y"
2850         bind $w.m.t <$M1B-Key-a> "$w.m.t tag add sel 0.0 end;break"
2851         bind $w.m.t <$M1B-Key-A> "$w.m.t tag add sel 0.0 end;break"
2852         bind $w <Visibility> "focus $w"
2853         wm title $w "[appname] ([reponame]): [lindex $console_data($w) 0]"
2854         return $w
2857 proc console_exec {w cmd after} {
2858         # -- Windows tosses the enviroment when we exec our child.
2859         #    But most users need that so we have to relogin. :-(
2860         #
2861         if {[is_Windows]} {
2862                 set cmd [list sh --login -c "cd \"[pwd]\" && [join $cmd { }]"]
2863         }
2865         # -- Tcl won't let us redirect both stdout and stderr to
2866         #    the same pipe.  So pass it through cat...
2867         #
2868         set cmd [concat | $cmd |& cat]
2870         set fd_f [open $cmd r]
2871         fconfigure $fd_f -blocking 0 -translation binary
2872         fileevent $fd_f readable [list console_read $w $fd_f $after]
2875 proc console_read {w fd after} {
2876         global console_cr
2878         set buf [read $fd]
2879         if {$buf ne {}} {
2880                 if {![winfo exists $w]} {console_init $w}
2881                 $w.m.t conf -state normal
2882                 set c 0
2883                 set n [string length $buf]
2884                 while {$c < $n} {
2885                         set cr [string first "\r" $buf $c]
2886                         set lf [string first "\n" $buf $c]
2887                         if {$cr < 0} {set cr [expr {$n + 1}]}
2888                         if {$lf < 0} {set lf [expr {$n + 1}]}
2890                         if {$lf < $cr} {
2891                                 $w.m.t insert end [string range $buf $c $lf]
2892                                 set console_cr($w) [$w.m.t index {end -1c}]
2893                                 set c $lf
2894                                 incr c
2895                         } else {
2896                                 $w.m.t delete $console_cr($w) end
2897                                 $w.m.t insert end "\n"
2898                                 $w.m.t insert end [string range $buf $c $cr]
2899                                 set c $cr
2900                                 incr c
2901                         }
2902                 }
2903                 $w.m.t conf -state disabled
2904                 $w.m.t see end
2905         }
2907         fconfigure $fd -blocking 1
2908         if {[eof $fd]} {
2909                 if {[catch {close $fd}]} {
2910                         set ok 0
2911                 } else {
2912                         set ok 1
2913                 }
2914                 uplevel #0 $after $w $ok
2915                 return
2916         }
2917         fconfigure $fd -blocking 0
2920 proc console_chain {cmdlist w {ok 1}} {
2921         if {$ok} {
2922                 if {[llength $cmdlist] == 0} {
2923                         console_done $w $ok
2924                         return
2925                 }
2927                 set cmd [lindex $cmdlist 0]
2928                 set cmdlist [lrange $cmdlist 1 end]
2930                 if {[lindex $cmd 0] eq {console_exec}} {
2931                         console_exec $w \
2932                                 [lindex $cmd 1] \
2933                                 [list console_chain $cmdlist]
2934                 } else {
2935                         uplevel #0 $cmd $cmdlist $w $ok
2936                 }
2937         } else {
2938                 console_done $w $ok
2939         }
2942 proc console_done {args} {
2943         global console_cr console_data
2945         switch -- [llength $args] {
2946         2 {
2947                 set w [lindex $args 0]
2948                 set ok [lindex $args 1]
2949         }
2950         3 {
2951                 set w [lindex $args 1]
2952                 set ok [lindex $args 2]
2953         }
2954         default {
2955                 error "wrong number of args: console_done ?ignored? w ok"
2956         }
2957         }
2959         if {$ok} {
2960                 if {[winfo exists $w]} {
2961                         $w.m.s conf -background green -text {Success}
2962                         $w.ok conf -state normal
2963                 }
2964         } else {
2965                 if {![winfo exists $w]} {
2966                         console_init $w
2967                 }
2968                 $w.m.s conf -background red -text {Error: Command Failed}
2969                 $w.ok conf -state normal
2970         }
2972         array unset console_cr $w
2973         array unset console_data $w
2976 ######################################################################
2977 ##
2978 ## ui commands
2980 set starting_gitk_msg {Starting gitk... please wait...}
2982 proc do_gitk {revs} {
2983         global ui_status_value starting_gitk_msg
2985         set cmd gitk
2986         if {$revs ne {}} {
2987                 append cmd { }
2988                 append cmd $revs
2989         }
2990         if {[is_Windows]} {
2991                 set cmd "sh -c \"exec $cmd\""
2992         }
2993         append cmd { &}
2995         if {[catch {eval exec $cmd} err]} {
2996                 error_popup "Failed to start gitk:\n\n$err"
2997         } else {
2998                 set ui_status_value $starting_gitk_msg
2999                 after 10000 {
3000                         if {$ui_status_value eq $starting_gitk_msg} {
3001                                 set ui_status_value {Ready.}
3002                         }
3003                 }
3004         }
3007 proc do_stats {} {
3008         set fd [open "| git count-objects -v" r]
3009         while {[gets $fd line] > 0} {
3010                 if {[regexp {^([^:]+): (\d+)$} $line _ name value]} {
3011                         set stats($name) $value
3012                 }
3013         }
3014         close $fd
3016         set packed_sz 0
3017         foreach p [glob -directory [gitdir objects pack] \
3018                 -type f \
3019                 -nocomplain -- *] {
3020                 incr packed_sz [file size $p]
3021         }
3022         if {$packed_sz > 0} {
3023                 set stats(size-pack) [expr {$packed_sz / 1024}]
3024         }
3026         set w .stats_view
3027         toplevel $w
3028         wm geometry $w "+[winfo rootx .]+[winfo rooty .]"
3030         label $w.header -text {Database Statistics} \
3031                 -font font_uibold
3032         pack $w.header -side top -fill x
3034         frame $w.buttons -border 1
3035         button $w.buttons.close -text Close \
3036                 -font font_ui \
3037                 -command [list destroy $w]
3038         button $w.buttons.gc -text {Compress Database} \
3039                 -font font_ui \
3040                 -command "destroy $w;do_gc"
3041         pack $w.buttons.close -side right
3042         pack $w.buttons.gc -side left
3043         pack $w.buttons -side bottom -fill x -pady 10 -padx 10
3045         frame $w.stat -borderwidth 1 -relief solid
3046         foreach s {
3047                 {count           {Number of loose objects}}
3048                 {size            {Disk space used by loose objects} { KiB}}
3049                 {in-pack         {Number of packed objects}}
3050                 {packs           {Number of packs}}
3051                 {size-pack       {Disk space used by packed objects} { KiB}}
3052                 {prune-packable  {Packed objects waiting for pruning}}
3053                 {garbage         {Garbage files}}
3054                 } {
3055                 set name [lindex $s 0]
3056                 set label [lindex $s 1]
3057                 if {[catch {set value $stats($name)}]} continue
3058                 if {[llength $s] > 2} {
3059                         set value "$value[lindex $s 2]"
3060                 }
3062                 label $w.stat.l_$name -text "$label:" -anchor w -font font_ui
3063                 label $w.stat.v_$name -text $value -anchor w -font font_ui
3064                 grid $w.stat.l_$name $w.stat.v_$name -sticky we -padx {0 5}
3065         }
3066         pack $w.stat -pady 10 -padx 10
3068         bind $w <Visibility> "grab $w; focus $w"
3069         bind $w <Key-Escape> [list destroy $w]
3070         bind $w <Key-Return> [list destroy $w]
3071         wm title $w "[appname] ([reponame]): Database Statistics"
3072         tkwait window $w
3075 proc do_gc {} {
3076         set w [new_console {gc} {Compressing the object database}]
3077         console_chain {
3078                 {console_exec {git pack-refs --prune}}
3079                 {console_exec {git reflog expire --all}}
3080                 {console_exec {git repack -a -d -l}}
3081                 {console_exec {git rerere gc}}
3082         } $w
3085 proc do_fsck_objects {} {
3086         set w [new_console {fsck-objects} \
3087                 {Verifying the object database with fsck-objects}]
3088         set cmd [list git fsck-objects]
3089         lappend cmd --full
3090         lappend cmd --cache
3091         lappend cmd --strict
3092         console_exec $w $cmd console_done
3095 set is_quitting 0
3097 proc do_quit {} {
3098         global ui_comm is_quitting repo_config commit_type
3100         if {$is_quitting} return
3101         set is_quitting 1
3103         # -- Stash our current commit buffer.
3104         #
3105         set save [gitdir GITGUI_MSG]
3106         set msg [string trim [$ui_comm get 0.0 end]]
3107         regsub -all -line {[ \r\t]+$} $msg {} msg
3108         if {(![string match amend* $commit_type]
3109                 || [$ui_comm edit modified])
3110                 && $msg ne {}} {
3111                 catch {
3112                         set fd [open $save w]
3113                         puts -nonewline $fd $msg
3114                         close $fd
3115                 }
3116         } else {
3117                 catch {file delete $save}
3118         }
3120         # -- Stash our current window geometry into this repository.
3121         #
3122         set cfg_geometry [list]
3123         lappend cfg_geometry [wm geometry .]
3124         lappend cfg_geometry [lindex [.vpane sash coord 0] 1]
3125         lappend cfg_geometry [lindex [.vpane.files sash coord 0] 0]
3126         if {[catch {set rc_geometry $repo_config(gui.geometry)}]} {
3127                 set rc_geometry {}
3128         }
3129         if {$cfg_geometry ne $rc_geometry} {
3130                 catch {exec git repo-config gui.geometry $cfg_geometry}
3131         }
3133         destroy .
3136 proc do_rescan {} {
3137         rescan {set ui_status_value {Ready.}}
3140 proc unstage_helper {txt paths} {
3141         global file_states current_diff_path
3143         if {![lock_index begin-update]} return
3145         set pathList [list]
3146         set after {}
3147         foreach path $paths {
3148                 switch -glob -- [lindex $file_states($path) 0] {
3149                 A? -
3150                 M? -
3151                 D? {
3152                         lappend pathList $path
3153                         if {$path eq $current_diff_path} {
3154                                 set after {reshow_diff;}
3155                         }
3156                 }
3157                 }
3158         }
3159         if {$pathList eq {}} {
3160                 unlock_index
3161         } else {
3162                 update_indexinfo \
3163                         $txt \
3164                         $pathList \
3165                         [concat $after {set ui_status_value {Ready.}}]
3166         }
3169 proc do_unstage_selection {} {
3170         global current_diff_path selected_paths
3172         if {[array size selected_paths] > 0} {
3173                 unstage_helper \
3174                         {Unstaging selected files from commit} \
3175                         [array names selected_paths]
3176         } elseif {$current_diff_path ne {}} {
3177                 unstage_helper \
3178                         "Unstaging [short_path $current_diff_path] from commit" \
3179                         [list $current_diff_path]
3180         }
3183 proc add_helper {txt paths} {
3184         global file_states current_diff_path
3186         if {![lock_index begin-update]} return
3188         set pathList [list]
3189         set after {}
3190         foreach path $paths {
3191                 switch -glob -- [lindex $file_states($path) 0] {
3192                 _O -
3193                 ?M -
3194                 ?D -
3195                 U? {
3196                         lappend pathList $path
3197                         if {$path eq $current_diff_path} {
3198                                 set after {reshow_diff;}
3199                         }
3200                 }
3201                 }
3202         }
3203         if {$pathList eq {}} {
3204                 unlock_index
3205         } else {
3206                 update_index \
3207                         $txt \
3208                         $pathList \
3209                         [concat $after {set ui_status_value {Ready to commit.}}]
3210         }
3213 proc do_add_selection {} {
3214         global current_diff_path selected_paths
3216         if {[array size selected_paths] > 0} {
3217                 add_helper \
3218                         {Adding selected files} \
3219                         [array names selected_paths]
3220         } elseif {$current_diff_path ne {}} {
3221                 add_helper \
3222                         "Adding [short_path $current_diff_path]" \
3223                         [list $current_diff_path]
3224         }
3227 proc do_add_all {} {
3228         global file_states
3230         set paths [list]
3231         foreach path [array names file_states] {
3232                 switch -glob -- [lindex $file_states($path) 0] {
3233                 U? {continue}
3234                 ?M -
3235                 ?D {lappend paths $path}
3236                 }
3237         }
3238         add_helper {Adding all changed files} $paths
3241 proc revert_helper {txt paths} {
3242         global file_states current_diff_path
3244         if {![lock_index begin-update]} return
3246         set pathList [list]
3247         set after {}
3248         foreach path $paths {
3249                 switch -glob -- [lindex $file_states($path) 0] {
3250                 U? {continue}
3251                 ?M -
3252                 ?D {
3253                         lappend pathList $path
3254                         if {$path eq $current_diff_path} {
3255                                 set after {reshow_diff;}
3256                         }
3257                 }
3258                 }
3259         }
3261         set n [llength $pathList]
3262         if {$n == 0} {
3263                 unlock_index
3264                 return
3265         } elseif {$n == 1} {
3266                 set s "[short_path [lindex $pathList]]"
3267         } else {
3268                 set s "these $n files"
3269         }
3271         set reply [tk_dialog \
3272                 .confirm_revert \
3273                 "[appname] ([reponame])" \
3274                 "Revert changes in $s?
3276 Any unadded changes will be permanently lost by the revert." \
3277                 question \
3278                 1 \
3279                 {Do Nothing} \
3280                 {Revert Changes} \
3281                 ]
3282         if {$reply == 1} {
3283                 checkout_index \
3284                         $txt \
3285                         $pathList \
3286                         [concat $after {set ui_status_value {Ready.}}]
3287         } else {
3288                 unlock_index
3289         }
3292 proc do_revert_selection {} {
3293         global current_diff_path selected_paths
3295         if {[array size selected_paths] > 0} {
3296                 revert_helper \
3297                         {Reverting selected files} \
3298                         [array names selected_paths]
3299         } elseif {$current_diff_path ne {}} {
3300                 revert_helper \
3301                         "Reverting [short_path $current_diff_path]" \
3302                         [list $current_diff_path]
3303         }
3306 proc do_signoff {} {
3307         global ui_comm
3309         set me [committer_ident]
3310         if {$me eq {}} return
3312         set sob "Signed-off-by: $me"
3313         set last [$ui_comm get {end -1c linestart} {end -1c}]
3314         if {$last ne $sob} {
3315                 $ui_comm edit separator
3316                 if {$last ne {}
3317                         && ![regexp {^[A-Z][A-Za-z]*-[A-Za-z-]+: *} $last]} {
3318                         $ui_comm insert end "\n"
3319                 }
3320                 $ui_comm insert end "\n$sob"
3321                 $ui_comm edit separator
3322                 $ui_comm see end
3323         }
3326 proc do_select_commit_type {} {
3327         global commit_type selected_commit_type
3329         if {$selected_commit_type eq {new}
3330                 && [string match amend* $commit_type]} {
3331                 create_new_commit
3332         } elseif {$selected_commit_type eq {amend}
3333                 && ![string match amend* $commit_type]} {
3334                 load_last_commit
3336                 # The amend request was rejected...
3337                 #
3338                 if {![string match amend* $commit_type]} {
3339                         set selected_commit_type new
3340                 }
3341         }
3344 proc do_commit {} {
3345         commit_tree
3348 proc do_about {} {
3349         global appvers copyright
3350         global tcl_patchLevel tk_patchLevel
3352         set w .about_dialog
3353         toplevel $w
3354         wm geometry $w "+[winfo rootx .]+[winfo rooty .]"
3356         label $w.header -text "About [appname]" \
3357                 -font font_uibold
3358         pack $w.header -side top -fill x
3360         frame $w.buttons
3361         button $w.buttons.close -text {Close} \
3362                 -font font_ui \
3363                 -command [list destroy $w]
3364         pack $w.buttons.close -side right
3365         pack $w.buttons -side bottom -fill x -pady 10 -padx 10
3367         label $w.desc \
3368                 -text "[appname] - a commit creation tool for Git.
3369 $copyright" \
3370                 -padx 5 -pady 5 \
3371                 -justify left \
3372                 -anchor w \
3373                 -borderwidth 1 \
3374                 -relief solid \
3375                 -font font_ui
3376         pack $w.desc -side top -fill x -padx 5 -pady 5
3378         set v {}
3379         append v "[appname] version $appvers\n"
3380         append v "[exec git version]\n"
3381         append v "\n"
3382         if {$tcl_patchLevel eq $tk_patchLevel} {
3383                 append v "Tcl/Tk version $tcl_patchLevel"
3384         } else {
3385                 append v "Tcl version $tcl_patchLevel"
3386                 append v ", Tk version $tk_patchLevel"
3387         }
3389         label $w.vers \
3390                 -text $v \
3391                 -padx 5 -pady 5 \
3392                 -justify left \
3393                 -anchor w \
3394                 -borderwidth 1 \
3395                 -relief solid \
3396                 -font font_ui
3397         pack $w.vers -side top -fill x -padx 5 -pady 5
3399         menu $w.ctxm -tearoff 0
3400         $w.ctxm add command \
3401                 -label {Copy} \
3402                 -font font_ui \
3403                 -command "
3404                 clipboard clear
3405                 clipboard append -format STRING -type STRING -- \[$w.vers cget -text\]
3406         "
3408         bind $w <Visibility> "grab $w; focus $w"
3409         bind $w <Key-Escape> "destroy $w"
3410         bind_button3 $w.vers "tk_popup $w.ctxm %X %Y; grab $w; focus $w"
3411         wm title $w "About [appname]"
3412         tkwait window $w
3415 proc do_options {} {
3416         global repo_config global_config font_descs
3417         global repo_config_new global_config_new
3419         array unset repo_config_new
3420         array unset global_config_new
3421         foreach name [array names repo_config] {
3422                 set repo_config_new($name) $repo_config($name)
3423         }
3424         load_config 1
3425         foreach name [array names repo_config] {
3426                 switch -- $name {
3427                 gui.diffcontext {continue}
3428                 }
3429                 set repo_config_new($name) $repo_config($name)
3430         }
3431         foreach name [array names global_config] {
3432                 set global_config_new($name) $global_config($name)
3433         }
3435         set w .options_editor
3436         toplevel $w
3437         wm geometry $w "+[winfo rootx .]+[winfo rooty .]"
3439         label $w.header -text "[appname] Options" \
3440                 -font font_uibold
3441         pack $w.header -side top -fill x
3443         frame $w.buttons
3444         button $w.buttons.restore -text {Restore Defaults} \
3445                 -font font_ui \
3446                 -command do_restore_defaults
3447         pack $w.buttons.restore -side left
3448         button $w.buttons.save -text Save \
3449                 -font font_ui \
3450                 -command [list do_save_config $w]
3451         pack $w.buttons.save -side right
3452         button $w.buttons.cancel -text {Cancel} \
3453                 -font font_ui \
3454                 -command [list destroy $w]
3455         pack $w.buttons.cancel -side right -padx 5
3456         pack $w.buttons -side bottom -fill x -pady 10 -padx 10
3458         labelframe $w.repo -text "[reponame] Repository" \
3459                 -font font_ui
3460         labelframe $w.global -text {Global (All Repositories)} \
3461                 -font font_ui
3462         pack $w.repo -side left -fill both -expand 1 -pady 5 -padx 5
3463         pack $w.global -side right -fill both -expand 1 -pady 5 -padx 5
3465         foreach option {
3466                 {b pullsummary {Show Pull Summary}}
3467                 {b trustmtime  {Trust File Modification Timestamps}}
3468                 {i diffcontext {Number of Diff Context Lines}}
3469                 {t newbranchtemplate {New Branch Name Template}}
3470                 } {
3471                 set type [lindex $option 0]
3472                 set name [lindex $option 1]
3473                 set text [lindex $option 2]
3474                 foreach f {repo global} {
3475                         switch $type {
3476                         b {
3477                                 checkbutton $w.$f.$name -text $text \
3478                                         -variable ${f}_config_new(gui.$name) \
3479                                         -onvalue true \
3480                                         -offvalue false \
3481                                         -font font_ui
3482                                 pack $w.$f.$name -side top -anchor w
3483                         }
3484                         i {
3485                                 frame $w.$f.$name
3486                                 label $w.$f.$name.l -text "$text:" -font font_ui
3487                                 pack $w.$f.$name.l -side left -anchor w -fill x
3488                                 spinbox $w.$f.$name.v \
3489                                         -textvariable ${f}_config_new(gui.$name) \
3490                                         -from 1 -to 99 -increment 1 \
3491                                         -width 3 \
3492                                         -font font_ui
3493                                 bind $w.$f.$name.v <FocusIn> {%W selection range 0 end}
3494                                 pack $w.$f.$name.v -side right -anchor e -padx 5
3495                                 pack $w.$f.$name -side top -anchor w -fill x
3496                         }
3497                         t {
3498                                 frame $w.$f.$name
3499                                 label $w.$f.$name.l -text "$text:" -font font_ui
3500                                 entry $w.$f.$name.v \
3501                                         -borderwidth 1 \
3502                                         -relief sunken \
3503                                         -width 20 \
3504                                         -textvariable ${f}_config_new(gui.$name) \
3505                                         -font font_ui
3506                                 pack $w.$f.$name.l -side left -anchor w
3507                                 pack $w.$f.$name.v -side left -anchor w \
3508                                         -fill x -expand 1 \
3509                                         -padx 5
3510                                 pack $w.$f.$name -side top -anchor w -fill x
3511                         }
3512                         }
3513                 }
3514         }
3516         set all_fonts [lsort [font families]]
3517         foreach option $font_descs {
3518                 set name [lindex $option 0]
3519                 set font [lindex $option 1]
3520                 set text [lindex $option 2]
3522                 set global_config_new(gui.$font^^family) \
3523                         [font configure $font -family]
3524                 set global_config_new(gui.$font^^size) \
3525                         [font configure $font -size]
3527                 frame $w.global.$name
3528                 label $w.global.$name.l -text "$text:" -font font_ui
3529                 pack $w.global.$name.l -side left -anchor w -fill x
3530                 eval tk_optionMenu $w.global.$name.family \
3531                         global_config_new(gui.$font^^family) \
3532                         $all_fonts
3533                 spinbox $w.global.$name.size \
3534                         -textvariable global_config_new(gui.$font^^size) \
3535                         -from 2 -to 80 -increment 1 \
3536                         -width 3 \
3537                         -font font_ui
3538                 bind $w.global.$name.size <FocusIn> {%W selection range 0 end}
3539                 pack $w.global.$name.size -side right -anchor e
3540                 pack $w.global.$name.family -side right -anchor e
3541                 pack $w.global.$name -side top -anchor w -fill x
3542         }
3544         bind $w <Visibility> "grab $w; focus $w"
3545         bind $w <Key-Escape> "destroy $w"
3546         wm title $w "[appname] ([reponame]): Options"
3547         tkwait window $w
3550 proc do_restore_defaults {} {
3551         global font_descs default_config repo_config
3552         global repo_config_new global_config_new
3554         foreach name [array names default_config] {
3555                 set repo_config_new($name) $default_config($name)
3556                 set global_config_new($name) $default_config($name)
3557         }
3559         foreach option $font_descs {
3560                 set name [lindex $option 0]
3561                 set repo_config(gui.$name) $default_config(gui.$name)
3562         }
3563         apply_config
3565         foreach option $font_descs {
3566                 set name [lindex $option 0]
3567                 set font [lindex $option 1]
3568                 set global_config_new(gui.$font^^family) \
3569                         [font configure $font -family]
3570                 set global_config_new(gui.$font^^size) \
3571                         [font configure $font -size]
3572         }
3575 proc do_save_config {w} {
3576         if {[catch {save_config} err]} {
3577                 error_popup "Failed to completely save options:\n\n$err"
3578         }
3579         reshow_diff
3580         destroy $w
3583 proc do_windows_shortcut {} {
3584         global argv0
3586         if {[catch {
3587                 set desktop [exec cygpath \
3588                         --windows \
3589                         --absolute \
3590                         --long-name \
3591                         --desktop]
3592                 }]} {
3593                         set desktop .
3594         }
3595         set fn [tk_getSaveFile \
3596                 -parent . \
3597                 -title "[appname] ([reponame]): Create Desktop Icon" \
3598                 -initialdir $desktop \
3599                 -initialfile "Git [reponame].bat"]
3600         if {$fn != {}} {
3601                 if {[catch {
3602                                 set fd [open $fn w]
3603                                 set sh [exec cygpath \
3604                                         --windows \
3605                                         --absolute \
3606                                         /bin/sh]
3607                                 set me [exec cygpath \
3608                                         --unix \
3609                                         --absolute \
3610                                         $argv0]
3611                                 set gd [exec cygpath \
3612                                         --unix \
3613                                         --absolute \
3614                                         [gitdir]]
3615                                 set gw [exec cygpath \
3616                                         --windows \
3617                                         --absolute \
3618                                         [file dirname [gitdir]]]
3619                                 regsub -all ' $me "'\\''" me
3620                                 regsub -all ' $gd "'\\''" gd
3621                                 puts $fd "@ECHO Entering $gw"
3622                                 puts $fd "@ECHO Starting git-gui... please wait..."
3623                                 puts -nonewline $fd "@\"$sh\" --login -c \""
3624                                 puts -nonewline $fd "GIT_DIR='$gd'"
3625                                 puts -nonewline $fd " '$me'"
3626                                 puts $fd "&\""
3627                                 close $fd
3628                         } err]} {
3629                         error_popup "Cannot write script:\n\n$err"
3630                 }
3631         }
3634 proc do_macosx_app {} {
3635         global argv0 env
3637         set fn [tk_getSaveFile \
3638                 -parent . \
3639                 -title "[appname] ([reponame]): Create Desktop Icon" \
3640                 -initialdir [file join $env(HOME) Desktop] \
3641                 -initialfile "Git [reponame].app"]
3642         if {$fn != {}} {
3643                 if {[catch {
3644                                 set Contents [file join $fn Contents]
3645                                 set MacOS [file join $Contents MacOS]
3646                                 set exe [file join $MacOS git-gui]
3648                                 file mkdir $MacOS
3650                                 set fd [open [file join $Contents Info.plist] w]
3651                                 puts $fd {<?xml version="1.0" encoding="UTF-8"?>
3652 <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3653 <plist version="1.0">
3654 <dict>
3655         <key>CFBundleDevelopmentRegion</key>
3656         <string>English</string>
3657         <key>CFBundleExecutable</key>
3658         <string>git-gui</string>
3659         <key>CFBundleIdentifier</key>
3660         <string>org.spearce.git-gui</string>
3661         <key>CFBundleInfoDictionaryVersion</key>
3662         <string>6.0</string>
3663         <key>CFBundlePackageType</key>
3664         <string>APPL</string>
3665         <key>CFBundleSignature</key>
3666         <string>????</string>
3667         <key>CFBundleVersion</key>
3668         <string>1.0</string>
3669         <key>NSPrincipalClass</key>
3670         <string>NSApplication</string>
3671 </dict>
3672 </plist>}
3673                                 close $fd
3675                                 set fd [open $exe w]
3676                                 set gd [file normalize [gitdir]]
3677                                 set ep [file normalize [exec git --exec-path]]
3678                                 regsub -all ' $gd "'\\''" gd
3679                                 regsub -all ' $ep "'\\''" ep
3680                                 puts $fd "#!/bin/sh"
3681                                 foreach name [array names env] {
3682                                         if {[string match GIT_* $name]} {
3683                                                 regsub -all ' $env($name) "'\\''" v
3684                                                 puts $fd "export $name='$v'"
3685                                         }
3686                                 }
3687                                 puts $fd "export PATH='$ep':\$PATH"
3688                                 puts $fd "export GIT_DIR='$gd'"
3689                                 puts $fd "exec [file normalize $argv0]"
3690                                 close $fd
3692                                 file attributes $exe -permissions u+x,g+x,o+x
3693                         } err]} {
3694                         error_popup "Cannot write icon:\n\n$err"
3695                 }
3696         }
3699 proc toggle_or_diff {w x y} {
3700         global file_states file_lists current_diff_path ui_index ui_workdir
3701         global last_clicked selected_paths
3703         set pos [split [$w index @$x,$y] .]
3704         set lno [lindex $pos 0]
3705         set col [lindex $pos 1]
3706         set path [lindex $file_lists($w) [expr {$lno - 1}]]
3707         if {$path eq {}} {
3708                 set last_clicked {}
3709                 return
3710         }
3712         set last_clicked [list $w $lno]
3713         array unset selected_paths
3714         $ui_index tag remove in_sel 0.0 end
3715         $ui_workdir tag remove in_sel 0.0 end
3717         if {$col == 0} {
3718                 if {$current_diff_path eq $path} {
3719                         set after {reshow_diff;}
3720                 } else {
3721                         set after {}
3722                 }
3723                 if {$w eq $ui_index} {
3724                         update_indexinfo \
3725                                 "Unstaging [short_path $path] from commit" \
3726                                 [list $path] \
3727                                 [concat $after {set ui_status_value {Ready.}}]
3728                 } elseif {$w eq $ui_workdir} {
3729                         update_index \
3730                                 "Adding [short_path $path]" \
3731                                 [list $path] \
3732                                 [concat $after {set ui_status_value {Ready.}}]
3733                 }
3734         } else {
3735                 show_diff $path $w $lno
3736         }
3739 proc add_one_to_selection {w x y} {
3740         global file_lists last_clicked selected_paths
3742         set lno [lindex [split [$w index @$x,$y] .] 0]
3743         set path [lindex $file_lists($w) [expr {$lno - 1}]]
3744         if {$path eq {}} {
3745                 set last_clicked {}
3746                 return
3747         }
3749         if {$last_clicked ne {}
3750                 && [lindex $last_clicked 0] ne $w} {
3751                 array unset selected_paths
3752                 [lindex $last_clicked 0] tag remove in_sel 0.0 end
3753         }
3755         set last_clicked [list $w $lno]
3756         if {[catch {set in_sel $selected_paths($path)}]} {
3757                 set in_sel 0
3758         }
3759         if {$in_sel} {
3760                 unset selected_paths($path)
3761                 $w tag remove in_sel $lno.0 [expr {$lno + 1}].0
3762         } else {
3763                 set selected_paths($path) 1
3764                 $w tag add in_sel $lno.0 [expr {$lno + 1}].0
3765         }
3768 proc add_range_to_selection {w x y} {
3769         global file_lists last_clicked selected_paths
3771         if {[lindex $last_clicked 0] ne $w} {
3772                 toggle_or_diff $w $x $y
3773                 return
3774         }
3776         set lno [lindex [split [$w index @$x,$y] .] 0]
3777         set lc [lindex $last_clicked 1]
3778         if {$lc < $lno} {
3779                 set begin $lc
3780                 set end $lno
3781         } else {
3782                 set begin $lno
3783                 set end $lc
3784         }
3786         foreach path [lrange $file_lists($w) \
3787                 [expr {$begin - 1}] \
3788                 [expr {$end - 1}]] {
3789                 set selected_paths($path) 1
3790         }
3791         $w tag add in_sel $begin.0 [expr {$end + 1}].0
3794 ######################################################################
3795 ##
3796 ## config defaults
3798 set cursor_ptr arrow
3799 font create font_diff -family Courier -size 10
3800 font create font_ui
3801 catch {
3802         label .dummy
3803         eval font configure font_ui [font actual [.dummy cget -font]]
3804         destroy .dummy
3807 font create font_uibold
3808 font create font_diffbold
3810 if {[is_Windows]} {
3811         set M1B Control
3812         set M1T Ctrl
3813 } elseif {[is_MacOSX]} {
3814         set M1B M1
3815         set M1T Cmd
3816 } else {
3817         set M1B M1
3818         set M1T M1
3821 proc apply_config {} {
3822         global repo_config font_descs
3824         foreach option $font_descs {
3825                 set name [lindex $option 0]
3826                 set font [lindex $option 1]
3827                 if {[catch {
3828                         foreach {cn cv} $repo_config(gui.$name) {
3829                                 font configure $font $cn $cv
3830                         }
3831                         } err]} {
3832                         error_popup "Invalid font specified in gui.$name:\n\n$err"
3833                 }
3834                 foreach {cn cv} [font configure $font] {
3835                         font configure ${font}bold $cn $cv
3836                 }
3837                 font configure ${font}bold -weight bold
3838         }
3841 set default_config(gui.trustmtime) false
3842 set default_config(gui.pullsummary) true
3843 set default_config(gui.diffcontext) 5
3844 set default_config(gui.newbranchtemplate) {}
3845 set default_config(gui.fontui) [font configure font_ui]
3846 set default_config(gui.fontdiff) [font configure font_diff]
3847 set font_descs {
3848         {fontui   font_ui   {Main Font}}
3849         {fontdiff font_diff {Diff/Console Font}}
3851 load_config 0
3852 apply_config
3854 ######################################################################
3855 ##
3856 ## ui construction
3858 # -- Menu Bar
3860 menu .mbar -tearoff 0
3861 .mbar add cascade -label Repository -menu .mbar.repository
3862 .mbar add cascade -label Edit -menu .mbar.edit
3863 if {!$single_commit} {
3864         .mbar add cascade -label Branch -menu .mbar.branch
3866 .mbar add cascade -label Commit -menu .mbar.commit
3867 if {!$single_commit} {
3868         .mbar add cascade -label Fetch -menu .mbar.fetch
3869         .mbar add cascade -label Push -menu .mbar.push
3871 . configure -menu .mbar
3873 # -- Repository Menu
3875 menu .mbar.repository
3876 .mbar.repository add command \
3877         -label {Visualize Current Branch} \
3878         -command {do_gitk {}} \
3879         -font font_ui
3880 .mbar.repository add command \
3881         -label {Visualize All Branches} \
3882         -command {do_gitk {--all}} \
3883         -font font_ui
3884 .mbar.repository add separator
3886 if {!$single_commit} {
3887         .mbar.repository add command -label {Database Statistics} \
3888                 -command do_stats \
3889                 -font font_ui
3891         .mbar.repository add command -label {Compress Database} \
3892                 -command do_gc \
3893                 -font font_ui
3895         .mbar.repository add command -label {Verify Database} \
3896                 -command do_fsck_objects \
3897                 -font font_ui
3899         .mbar.repository add separator
3901         if {[is_Windows]} {
3902                 .mbar.repository add command \
3903                         -label {Create Desktop Icon} \
3904                         -command do_windows_shortcut \
3905                         -font font_ui
3906         } elseif {[is_MacOSX]} {
3907                 .mbar.repository add command \
3908                         -label {Create Desktop Icon} \
3909                         -command do_macosx_app \
3910                         -font font_ui
3911         }
3914 .mbar.repository add command -label Quit \
3915         -command do_quit \
3916         -accelerator $M1T-Q \
3917         -font font_ui
3919 # -- Edit Menu
3921 menu .mbar.edit
3922 .mbar.edit add command -label Undo \
3923         -command {catch {[focus] edit undo}} \
3924         -accelerator $M1T-Z \
3925         -font font_ui
3926 .mbar.edit add command -label Redo \
3927         -command {catch {[focus] edit redo}} \
3928         -accelerator $M1T-Y \
3929         -font font_ui
3930 .mbar.edit add separator
3931 .mbar.edit add command -label Cut \
3932         -command {catch {tk_textCut [focus]}} \
3933         -accelerator $M1T-X \
3934         -font font_ui
3935 .mbar.edit add command -label Copy \
3936         -command {catch {tk_textCopy [focus]}} \
3937         -accelerator $M1T-C \
3938         -font font_ui
3939 .mbar.edit add command -label Paste \
3940         -command {catch {tk_textPaste [focus]; [focus] see insert}} \
3941         -accelerator $M1T-V \
3942         -font font_ui
3943 .mbar.edit add command -label Delete \
3944         -command {catch {[focus] delete sel.first sel.last}} \
3945         -accelerator Del \
3946         -font font_ui
3947 .mbar.edit add separator
3948 .mbar.edit add command -label {Select All} \
3949         -command {catch {[focus] tag add sel 0.0 end}} \
3950         -accelerator $M1T-A \
3951         -font font_ui
3953 # -- Branch Menu
3955 if {!$single_commit} {
3956         menu .mbar.branch
3958         .mbar.branch add command -label {Create...} \
3959                 -command do_create_branch \
3960                 -accelerator $M1T-N \
3961                 -font font_ui
3962         lappend disable_on_lock [list .mbar.branch entryconf \
3963                 [.mbar.branch index last] -state]
3965         .mbar.branch add command -label {Delete...} \
3966                 -command do_delete_branch \
3967                 -font font_ui
3968         lappend disable_on_lock [list .mbar.branch entryconf \
3969                 [.mbar.branch index last] -state]
3972 # -- Commit Menu
3974 menu .mbar.commit
3976 .mbar.commit add radiobutton \
3977         -label {New Commit} \
3978         -command do_select_commit_type \
3979         -variable selected_commit_type \
3980         -value new \
3981         -font font_ui
3982 lappend disable_on_lock \
3983         [list .mbar.commit entryconf [.mbar.commit index last] -state]
3985 .mbar.commit add radiobutton \
3986         -label {Amend Last Commit} \
3987         -command do_select_commit_type \
3988         -variable selected_commit_type \
3989         -value amend \
3990         -font font_ui
3991 lappend disable_on_lock \
3992         [list .mbar.commit entryconf [.mbar.commit index last] -state]
3994 .mbar.commit add separator
3996 .mbar.commit add command -label Rescan \
3997         -command do_rescan \
3998         -accelerator F5 \
3999         -font font_ui
4000 lappend disable_on_lock \
4001         [list .mbar.commit entryconf [.mbar.commit index last] -state]
4003 .mbar.commit add command -label {Add To Commit} \
4004         -command do_add_selection \
4005         -font font_ui
4006 lappend disable_on_lock \
4007         [list .mbar.commit entryconf [.mbar.commit index last] -state]
4009 .mbar.commit add command -label {Add All To Commit} \
4010         -command do_add_all \
4011         -accelerator $M1T-I \
4012         -font font_ui
4013 lappend disable_on_lock \
4014         [list .mbar.commit entryconf [.mbar.commit index last] -state]
4016 .mbar.commit add command -label {Unstage From Commit} \
4017         -command do_unstage_selection \
4018         -font font_ui
4019 lappend disable_on_lock \
4020         [list .mbar.commit entryconf [.mbar.commit index last] -state]
4022 .mbar.commit add command -label {Revert Changes} \
4023         -command do_revert_selection \
4024         -font font_ui
4025 lappend disable_on_lock \
4026         [list .mbar.commit entryconf [.mbar.commit index last] -state]
4028 .mbar.commit add separator
4030 .mbar.commit add command -label {Sign Off} \
4031         -command do_signoff \
4032         -accelerator $M1T-S \
4033         -font font_ui
4035 .mbar.commit add command -label Commit \
4036         -command do_commit \
4037         -accelerator $M1T-Return \
4038         -font font_ui
4039 lappend disable_on_lock \
4040         [list .mbar.commit entryconf [.mbar.commit index last] -state]
4042 # -- Transport menus
4044 if {!$single_commit} {
4045         menu .mbar.fetch
4046         menu .mbar.push
4048         .mbar.push add command -label {Push...} \
4049                 -command do_push_anywhere \
4050                 -font font_ui
4053 if {[is_MacOSX]} {
4054         # -- Apple Menu (Mac OS X only)
4055         #
4056         .mbar add cascade -label Apple -menu .mbar.apple
4057         menu .mbar.apple
4059         .mbar.apple add command -label "About [appname]" \
4060                 -command do_about \
4061                 -font font_ui
4062         .mbar.apple add command -label "[appname] Options..." \
4063                 -command do_options \
4064                 -font font_ui
4065 } else {
4066         # -- Edit Menu
4067         #
4068         .mbar.edit add separator
4069         .mbar.edit add command -label {Options...} \
4070                 -command do_options \
4071                 -font font_ui
4073         # -- Tools Menu
4074         #
4075         if {[file exists /usr/local/miga/lib/gui-miga]
4076                 && [file exists .pvcsrc]} {
4077         proc do_miga {} {
4078                 global ui_status_value
4079                 if {![lock_index update]} return
4080                 set cmd [list sh --login -c "/usr/local/miga/lib/gui-miga \"[pwd]\""]
4081                 set miga_fd [open "|$cmd" r]
4082                 fconfigure $miga_fd -blocking 0
4083                 fileevent $miga_fd readable [list miga_done $miga_fd]
4084                 set ui_status_value {Running miga...}
4085         }
4086         proc miga_done {fd} {
4087                 read $fd 512
4088                 if {[eof $fd]} {
4089                         close $fd
4090                         unlock_index
4091                         rescan [list set ui_status_value {Ready.}]
4092                 }
4093         }
4094         .mbar add cascade -label Tools -menu .mbar.tools
4095         menu .mbar.tools
4096         .mbar.tools add command -label "Migrate" \
4097                 -command do_miga \
4098                 -font font_ui
4099         lappend disable_on_lock \
4100                 [list .mbar.tools entryconf [.mbar.tools index last] -state]
4101         }
4103         # -- Help Menu
4104         #
4105         .mbar add cascade -label Help -menu .mbar.help
4106         menu .mbar.help
4108         .mbar.help add command -label "About [appname]" \
4109                 -command do_about \
4110                 -font font_ui
4114 # -- Branch Control
4116 frame .branch \
4117         -borderwidth 1 \
4118         -relief sunken
4119 label .branch.l1 \
4120         -text {Current Branch:} \
4121         -anchor w \
4122         -justify left \
4123         -font font_ui
4124 label .branch.cb \
4125         -textvariable current_branch \
4126         -anchor w \
4127         -justify left \
4128         -font font_ui
4129 pack .branch.l1 -side left
4130 pack .branch.cb -side left -fill x
4131 pack .branch -side top -fill x
4133 # -- Main Window Layout
4135 panedwindow .vpane -orient vertical
4136 panedwindow .vpane.files -orient horizontal
4137 .vpane add .vpane.files -sticky nsew -height 100 -width 200
4138 pack .vpane -anchor n -side top -fill both -expand 1
4140 # -- Index File List
4142 frame .vpane.files.index -height 100 -width 200
4143 label .vpane.files.index.title -text {Changes To Be Committed} \
4144         -background green \
4145         -font font_ui
4146 text $ui_index -background white -borderwidth 0 \
4147         -width 20 -height 10 \
4148         -wrap none \
4149         -font font_ui \
4150         -cursor $cursor_ptr \
4151         -xscrollcommand {.vpane.files.index.sx set} \
4152         -yscrollcommand {.vpane.files.index.sy set} \
4153         -state disabled
4154 scrollbar .vpane.files.index.sx -orient h -command [list $ui_index xview]
4155 scrollbar .vpane.files.index.sy -orient v -command [list $ui_index yview]
4156 pack .vpane.files.index.title -side top -fill x
4157 pack .vpane.files.index.sx -side bottom -fill x
4158 pack .vpane.files.index.sy -side right -fill y
4159 pack $ui_index -side left -fill both -expand 1
4160 .vpane.files add .vpane.files.index -sticky nsew
4162 # -- Working Directory File List
4164 frame .vpane.files.workdir -height 100 -width 200
4165 label .vpane.files.workdir.title -text {Changed But Not Updated} \
4166         -background red \
4167         -font font_ui
4168 text $ui_workdir -background white -borderwidth 0 \
4169         -width 20 -height 10 \
4170         -wrap none \
4171         -font font_ui \
4172         -cursor $cursor_ptr \
4173         -xscrollcommand {.vpane.files.workdir.sx set} \
4174         -yscrollcommand {.vpane.files.workdir.sy set} \
4175         -state disabled
4176 scrollbar .vpane.files.workdir.sx -orient h -command [list $ui_workdir xview]
4177 scrollbar .vpane.files.workdir.sy -orient v -command [list $ui_workdir yview]
4178 pack .vpane.files.workdir.title -side top -fill x
4179 pack .vpane.files.workdir.sx -side bottom -fill x
4180 pack .vpane.files.workdir.sy -side right -fill y
4181 pack $ui_workdir -side left -fill both -expand 1
4182 .vpane.files add .vpane.files.workdir -sticky nsew
4184 foreach i [list $ui_index $ui_workdir] {
4185         $i tag conf in_diff -font font_uibold
4186         $i tag conf in_sel \
4187                 -background [$i cget -foreground] \
4188                 -foreground [$i cget -background]
4190 unset i
4192 # -- Diff and Commit Area
4194 frame .vpane.lower -height 300 -width 400
4195 frame .vpane.lower.commarea
4196 frame .vpane.lower.diff -relief sunken -borderwidth 1
4197 pack .vpane.lower.commarea -side top -fill x
4198 pack .vpane.lower.diff -side bottom -fill both -expand 1
4199 .vpane add .vpane.lower -sticky nsew
4201 # -- Commit Area Buttons
4203 frame .vpane.lower.commarea.buttons
4204 label .vpane.lower.commarea.buttons.l -text {} \
4205         -anchor w \
4206         -justify left \
4207         -font font_ui
4208 pack .vpane.lower.commarea.buttons.l -side top -fill x
4209 pack .vpane.lower.commarea.buttons -side left -fill y
4211 button .vpane.lower.commarea.buttons.rescan -text {Rescan} \
4212         -command do_rescan \
4213         -font font_ui
4214 pack .vpane.lower.commarea.buttons.rescan -side top -fill x
4215 lappend disable_on_lock \
4216         {.vpane.lower.commarea.buttons.rescan conf -state}
4218 button .vpane.lower.commarea.buttons.incall -text {Add All} \
4219         -command do_add_all \
4220         -font font_ui
4221 pack .vpane.lower.commarea.buttons.incall -side top -fill x
4222 lappend disable_on_lock \
4223         {.vpane.lower.commarea.buttons.incall conf -state}
4225 button .vpane.lower.commarea.buttons.signoff -text {Sign Off} \
4226         -command do_signoff \
4227         -font font_ui
4228 pack .vpane.lower.commarea.buttons.signoff -side top -fill x
4230 button .vpane.lower.commarea.buttons.commit -text {Commit} \
4231         -command do_commit \
4232         -font font_ui
4233 pack .vpane.lower.commarea.buttons.commit -side top -fill x
4234 lappend disable_on_lock \
4235         {.vpane.lower.commarea.buttons.commit conf -state}
4237 # -- Commit Message Buffer
4239 frame .vpane.lower.commarea.buffer
4240 frame .vpane.lower.commarea.buffer.header
4241 set ui_comm .vpane.lower.commarea.buffer.t
4242 set ui_coml .vpane.lower.commarea.buffer.header.l
4243 radiobutton .vpane.lower.commarea.buffer.header.new \
4244         -text {New Commit} \
4245         -command do_select_commit_type \
4246         -variable selected_commit_type \
4247         -value new \
4248         -font font_ui
4249 lappend disable_on_lock \
4250         [list .vpane.lower.commarea.buffer.header.new conf -state]
4251 radiobutton .vpane.lower.commarea.buffer.header.amend \
4252         -text {Amend Last Commit} \
4253         -command do_select_commit_type \
4254         -variable selected_commit_type \
4255         -value amend \
4256         -font font_ui
4257 lappend disable_on_lock \
4258         [list .vpane.lower.commarea.buffer.header.amend conf -state]
4259 label $ui_coml \
4260         -anchor w \
4261         -justify left \
4262         -font font_ui
4263 proc trace_commit_type {varname args} {
4264         global ui_coml commit_type
4265         switch -glob -- $commit_type {
4266         initial       {set txt {Initial Commit Message:}}
4267         amend         {set txt {Amended Commit Message:}}
4268         amend-initial {set txt {Amended Initial Commit Message:}}
4269         amend-merge   {set txt {Amended Merge Commit Message:}}
4270         merge         {set txt {Merge Commit Message:}}
4271         *             {set txt {Commit Message:}}
4272         }
4273         $ui_coml conf -text $txt
4275 trace add variable commit_type write trace_commit_type
4276 pack $ui_coml -side left -fill x
4277 pack .vpane.lower.commarea.buffer.header.amend -side right
4278 pack .vpane.lower.commarea.buffer.header.new -side right
4280 text $ui_comm -background white -borderwidth 1 \
4281         -undo true \
4282         -maxundo 20 \
4283         -autoseparators true \
4284         -relief sunken \
4285         -width 75 -height 9 -wrap none \
4286         -font font_diff \
4287         -yscrollcommand {.vpane.lower.commarea.buffer.sby set}
4288 scrollbar .vpane.lower.commarea.buffer.sby \
4289         -command [list $ui_comm yview]
4290 pack .vpane.lower.commarea.buffer.header -side top -fill x
4291 pack .vpane.lower.commarea.buffer.sby -side right -fill y
4292 pack $ui_comm -side left -fill y
4293 pack .vpane.lower.commarea.buffer -side left -fill y
4295 # -- Commit Message Buffer Context Menu
4297 set ctxm .vpane.lower.commarea.buffer.ctxm
4298 menu $ctxm -tearoff 0
4299 $ctxm add command \
4300         -label {Cut} \
4301         -font font_ui \
4302         -command {tk_textCut $ui_comm}
4303 $ctxm add command \
4304         -label {Copy} \
4305         -font font_ui \
4306         -command {tk_textCopy $ui_comm}
4307 $ctxm add command \
4308         -label {Paste} \
4309         -font font_ui \
4310         -command {tk_textPaste $ui_comm}
4311 $ctxm add command \
4312         -label {Delete} \
4313         -font font_ui \
4314         -command {$ui_comm delete sel.first sel.last}
4315 $ctxm add separator
4316 $ctxm add command \
4317         -label {Select All} \
4318         -font font_ui \
4319         -command {focus $ui_comm;$ui_comm tag add sel 0.0 end}
4320 $ctxm add command \
4321         -label {Copy All} \
4322         -font font_ui \
4323         -command {
4324                 $ui_comm tag add sel 0.0 end
4325                 tk_textCopy $ui_comm
4326                 $ui_comm tag remove sel 0.0 end
4327         }
4328 $ctxm add separator
4329 $ctxm add command \
4330         -label {Sign Off} \
4331         -font font_ui \
4332         -command do_signoff
4333 bind_button3 $ui_comm "tk_popup $ctxm %X %Y"
4335 # -- Diff Header
4337 set current_diff_path {}
4338 set current_diff_side {}
4339 set diff_actions [list]
4340 proc trace_current_diff_path {varname args} {
4341         global current_diff_path diff_actions file_states
4342         if {$current_diff_path eq {}} {
4343                 set s {}
4344                 set f {}
4345                 set p {}
4346                 set o disabled
4347         } else {
4348                 set p $current_diff_path
4349                 set s [mapdesc [lindex $file_states($p) 0] $p]
4350                 set f {File:}
4351                 set p [escape_path $p]
4352                 set o normal
4353         }
4355         .vpane.lower.diff.header.status configure -text $s
4356         .vpane.lower.diff.header.file configure -text $f
4357         .vpane.lower.diff.header.path configure -text $p
4358         foreach w $diff_actions {
4359                 uplevel #0 $w $o
4360         }
4362 trace add variable current_diff_path write trace_current_diff_path
4364 frame .vpane.lower.diff.header -background orange
4365 label .vpane.lower.diff.header.status \
4366         -background orange \
4367         -width $max_status_desc \
4368         -anchor w \
4369         -justify left \
4370         -font font_ui
4371 label .vpane.lower.diff.header.file \
4372         -background orange \
4373         -anchor w \
4374         -justify left \
4375         -font font_ui
4376 label .vpane.lower.diff.header.path \
4377         -background orange \
4378         -anchor w \
4379         -justify left \
4380         -font font_ui
4381 pack .vpane.lower.diff.header.status -side left
4382 pack .vpane.lower.diff.header.file -side left
4383 pack .vpane.lower.diff.header.path -fill x
4384 set ctxm .vpane.lower.diff.header.ctxm
4385 menu $ctxm -tearoff 0
4386 $ctxm add command \
4387         -label {Copy} \
4388         -font font_ui \
4389         -command {
4390                 clipboard clear
4391                 clipboard append \
4392                         -format STRING \
4393                         -type STRING \
4394                         -- $current_diff_path
4395         }
4396 lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
4397 bind_button3 .vpane.lower.diff.header.path "tk_popup $ctxm %X %Y"
4399 # -- Diff Body
4401 frame .vpane.lower.diff.body
4402 set ui_diff .vpane.lower.diff.body.t
4403 text $ui_diff -background white -borderwidth 0 \
4404         -width 80 -height 15 -wrap none \
4405         -font font_diff \
4406         -xscrollcommand {.vpane.lower.diff.body.sbx set} \
4407         -yscrollcommand {.vpane.lower.diff.body.sby set} \
4408         -state disabled
4409 scrollbar .vpane.lower.diff.body.sbx -orient horizontal \
4410         -command [list $ui_diff xview]
4411 scrollbar .vpane.lower.diff.body.sby -orient vertical \
4412         -command [list $ui_diff yview]
4413 pack .vpane.lower.diff.body.sbx -side bottom -fill x
4414 pack .vpane.lower.diff.body.sby -side right -fill y
4415 pack $ui_diff -side left -fill both -expand 1
4416 pack .vpane.lower.diff.header -side top -fill x
4417 pack .vpane.lower.diff.body -side bottom -fill both -expand 1
4419 $ui_diff tag conf d_cr -elide true
4420 $ui_diff tag conf d_@ -foreground blue -font font_diffbold
4421 $ui_diff tag conf d_+ -foreground {#00a000}
4422 $ui_diff tag conf d_- -foreground red
4424 $ui_diff tag conf d_++ -foreground {#00a000}
4425 $ui_diff tag conf d_-- -foreground red
4426 $ui_diff tag conf d_+s \
4427         -foreground {#00a000} \
4428         -background {#e2effa}
4429 $ui_diff tag conf d_-s \
4430         -foreground red \
4431         -background {#e2effa}
4432 $ui_diff tag conf d_s+ \
4433         -foreground {#00a000} \
4434         -background ivory1
4435 $ui_diff tag conf d_s- \
4436         -foreground red \
4437         -background ivory1
4439 $ui_diff tag conf d<<<<<<< \
4440         -foreground orange \
4441         -font font_diffbold
4442 $ui_diff tag conf d======= \
4443         -foreground orange \
4444         -font font_diffbold
4445 $ui_diff tag conf d>>>>>>> \
4446         -foreground orange \
4447         -font font_diffbold
4449 $ui_diff tag raise sel
4451 # -- Diff Body Context Menu
4453 set ctxm .vpane.lower.diff.body.ctxm
4454 menu $ctxm -tearoff 0
4455 $ctxm add command \
4456         -label {Refresh} \
4457         -font font_ui \
4458         -command reshow_diff
4459 lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
4460 $ctxm add command \
4461         -label {Copy} \
4462         -font font_ui \
4463         -command {tk_textCopy $ui_diff}
4464 lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
4465 $ctxm add command \
4466         -label {Select All} \
4467         -font font_ui \
4468         -command {focus $ui_diff;$ui_diff tag add sel 0.0 end}
4469 lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
4470 $ctxm add command \
4471         -label {Copy All} \
4472         -font font_ui \
4473         -command {
4474                 $ui_diff tag add sel 0.0 end
4475                 tk_textCopy $ui_diff
4476                 $ui_diff tag remove sel 0.0 end
4477         }
4478 lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
4479 $ctxm add separator
4480 $ctxm add command \
4481         -label {Apply/Reverse Hunk} \
4482         -font font_ui \
4483         -command {apply_hunk $cursorX $cursorY}
4484 set ui_diff_applyhunk [$ctxm index last]
4485 lappend diff_actions [list $ctxm entryconf $ui_diff_applyhunk -state]
4486 $ctxm add separator
4487 $ctxm add command \
4488         -label {Decrease Font Size} \
4489         -font font_ui \
4490         -command {incr_font_size font_diff -1}
4491 lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
4492 $ctxm add command \
4493         -label {Increase Font Size} \
4494         -font font_ui \
4495         -command {incr_font_size font_diff 1}
4496 lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
4497 $ctxm add separator
4498 $ctxm add command \
4499         -label {Show Less Context} \
4500         -font font_ui \
4501         -command {if {$repo_config(gui.diffcontext) >= 2} {
4502                 incr repo_config(gui.diffcontext) -1
4503                 reshow_diff
4504         }}
4505 lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
4506 $ctxm add command \
4507         -label {Show More Context} \
4508         -font font_ui \
4509         -command {
4510                 incr repo_config(gui.diffcontext)
4511                 reshow_diff
4512         }
4513 lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
4514 $ctxm add separator
4515 $ctxm add command -label {Options...} \
4516         -font font_ui \
4517         -command do_options
4518 bind_button3 $ui_diff "
4519         set cursorX %x
4520         set cursorY %y
4521         if {\$ui_index eq \$current_diff_side} {
4522                 $ctxm entryconf $ui_diff_applyhunk -label {Unstage Hunk From Commit}
4523         } else {
4524                 $ctxm entryconf $ui_diff_applyhunk -label {Stage Hunk For Commit}
4525         }
4526         tk_popup $ctxm %X %Y
4528 unset ui_diff_applyhunk
4530 # -- Status Bar
4532 set ui_status_value {Initializing...}
4533 label .status -textvariable ui_status_value \
4534         -anchor w \
4535         -justify left \
4536         -borderwidth 1 \
4537         -relief sunken \
4538         -font font_ui
4539 pack .status -anchor w -side bottom -fill x
4541 # -- Load geometry
4543 catch {
4544 set gm $repo_config(gui.geometry)
4545 wm geometry . [lindex $gm 0]
4546 .vpane sash place 0 \
4547         [lindex [.vpane sash coord 0] 0] \
4548         [lindex $gm 1]
4549 .vpane.files sash place 0 \
4550         [lindex $gm 2] \
4551         [lindex [.vpane.files sash coord 0] 1]
4552 unset gm
4555 # -- Key Bindings
4557 bind $ui_comm <$M1B-Key-Return> {do_commit;break}
4558 bind $ui_comm <$M1B-Key-i> {do_add_all;break}
4559 bind $ui_comm <$M1B-Key-I> {do_add_all;break}
4560 bind $ui_comm <$M1B-Key-x> {tk_textCut %W;break}
4561 bind $ui_comm <$M1B-Key-X> {tk_textCut %W;break}
4562 bind $ui_comm <$M1B-Key-c> {tk_textCopy %W;break}
4563 bind $ui_comm <$M1B-Key-C> {tk_textCopy %W;break}
4564 bind $ui_comm <$M1B-Key-v> {tk_textPaste %W; %W see insert; break}
4565 bind $ui_comm <$M1B-Key-V> {tk_textPaste %W; %W see insert; break}
4566 bind $ui_comm <$M1B-Key-a> {%W tag add sel 0.0 end;break}
4567 bind $ui_comm <$M1B-Key-A> {%W tag add sel 0.0 end;break}
4569 bind $ui_diff <$M1B-Key-x> {tk_textCopy %W;break}
4570 bind $ui_diff <$M1B-Key-X> {tk_textCopy %W;break}
4571 bind $ui_diff <$M1B-Key-c> {tk_textCopy %W;break}
4572 bind $ui_diff <$M1B-Key-C> {tk_textCopy %W;break}
4573 bind $ui_diff <$M1B-Key-v> {break}
4574 bind $ui_diff <$M1B-Key-V> {break}
4575 bind $ui_diff <$M1B-Key-a> {%W tag add sel 0.0 end;break}
4576 bind $ui_diff <$M1B-Key-A> {%W tag add sel 0.0 end;break}
4577 bind $ui_diff <Key-Up>     {catch {%W yview scroll -1 units};break}
4578 bind $ui_diff <Key-Down>   {catch {%W yview scroll  1 units};break}
4579 bind $ui_diff <Key-Left>   {catch {%W xview scroll -1 units};break}
4580 bind $ui_diff <Key-Right>  {catch {%W xview scroll  1 units};break}
4581 bind $ui_diff <Button-1>   {focus %W}
4583 if {!$single_commit} {
4584         bind . <$M1B-Key-n> do_create_branch
4585         bind . <$M1B-Key-N> do_create_branch
4588 bind .   <Destroy> do_quit
4589 bind all <Key-F5> do_rescan
4590 bind all <$M1B-Key-r> do_rescan
4591 bind all <$M1B-Key-R> do_rescan
4592 bind .   <$M1B-Key-s> do_signoff
4593 bind .   <$M1B-Key-S> do_signoff
4594 bind .   <$M1B-Key-i> do_add_all
4595 bind .   <$M1B-Key-I> do_add_all
4596 bind .   <$M1B-Key-Return> do_commit
4597 bind all <$M1B-Key-q> do_quit
4598 bind all <$M1B-Key-Q> do_quit
4599 bind all <$M1B-Key-w> {destroy [winfo toplevel %W]}
4600 bind all <$M1B-Key-W> {destroy [winfo toplevel %W]}
4601 foreach i [list $ui_index $ui_workdir] {
4602         bind $i <Button-1>       "toggle_or_diff         $i %x %y; break"
4603         bind $i <$M1B-Button-1>  "add_one_to_selection   $i %x %y; break"
4604         bind $i <Shift-Button-1> "add_range_to_selection $i %x %y; break"
4606 unset i
4608 set file_lists($ui_index) [list]
4609 set file_lists($ui_workdir) [list]
4611 set HEAD {}
4612 set PARENT {}
4613 set MERGE_HEAD [list]
4614 set commit_type {}
4615 set empty_tree {}
4616 set current_branch {}
4617 set current_diff_path {}
4618 set selected_commit_type new
4620 wm title . "[appname] ([file normalize [file dirname [gitdir]]])"
4621 focus -force $ui_comm
4623 # -- Warn the user about environmental problems.  Cygwin's Tcl
4624 #    does *not* pass its env array onto any processes it spawns.
4625 #    This means that git processes get none of our environment.
4627 if {[is_Windows]} {
4628         set ignored_env 0
4629         set suggest_user {}
4630         set msg "Possible environment issues exist.
4632 The following environment variables are probably
4633 going to be ignored by any Git subprocess run
4634 by [appname]:
4637         foreach name [array names env] {
4638                 switch -regexp -- $name {
4639                 {^GIT_INDEX_FILE$} -
4640                 {^GIT_OBJECT_DIRECTORY$} -
4641                 {^GIT_ALTERNATE_OBJECT_DIRECTORIES$} -
4642                 {^GIT_DIFF_OPTS$} -
4643                 {^GIT_EXTERNAL_DIFF$} -
4644                 {^GIT_PAGER$} -
4645                 {^GIT_TRACE$} -
4646                 {^GIT_CONFIG$} -
4647                 {^GIT_CONFIG_LOCAL$} -
4648                 {^GIT_(AUTHOR|COMMITTER)_DATE$} {
4649                         append msg " - $name\n"
4650                         incr ignored_env
4651                 }
4652                 {^GIT_(AUTHOR|COMMITTER)_(NAME|EMAIL)$} {
4653                         append msg " - $name\n"
4654                         incr ignored_env
4655                         set suggest_user $name
4656                 }
4657                 }
4658         }
4659         if {$ignored_env > 0} {
4660                 append msg "
4661 This is due to a known issue with the
4662 Tcl binary distributed by Cygwin."
4664                 if {$suggest_user ne {}} {
4665                         append msg "
4667 A good replacement for $suggest_user
4668 is placing values for the user.name and
4669 user.email settings into your personal
4670 ~/.gitconfig file.
4672                 }
4673                 warn_popup $msg
4674         }
4675         unset ignored_env msg suggest_user name
4678 # -- Only initialize complex UI if we are going to stay running.
4680 if {!$single_commit} {
4681         load_all_remotes
4682         load_all_heads
4684         populate_branch_menu
4685         populate_fetch_menu
4686         populate_push_menu
4689 # -- Only suggest a gc run if we are going to stay running.
4691 if {!$single_commit} {
4692         set object_limit 2000
4693         if {[is_Windows]} {set object_limit 200}
4694         regexp {^([0-9]+) objects,} [exec git count-objects] _junk objects_current
4695         if {$objects_current >= $object_limit} {
4696                 if {[ask_popup \
4697                         "This repository currently has $objects_current loose objects.
4699 To maintain optimal performance it is strongly
4700 recommended that you compress the database
4701 when more than $object_limit loose objects exist.
4703 Compress the database now?"] eq yes} {
4704                         do_gc
4705                 }
4706         }
4707         unset object_limit _junk objects_current
4710 lock_index begin-read
4711 after 1 do_rescan