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