@matheusbsbs / matheus-bagliano / commits / f522c9b5ed
Fork 0

git-gui: Refactor into multiple files to save my sanity

I'm finding it difficult to work with a 6,000+ line Tcl script and not go insane while looking for a particular block of code. Since most of the program is organized into different units of functionality and not all users will need all units immediately on startup we can improve things by splitting procs out into multiple files and let auto_load handle things for us. This should help not only to better organize the source, but it may also improve startup times for some users as the Tcl parser does not need to read as much script before it can show the UI. In many cases the user can avoid reading at least half of git-gui now. Unfortunately we now need a library directory in our runtime location. This is currently assumed to be $(sharedir)/git-gui/lib and its expected that the Makefile invoker will setup some sort of reasonable sharedir value for us, or let us assume its going to be $(gitexecdir)/../share. We now also require a tclsh (in TCL_PATH) to just run the Makefile, as we use tclsh to generate the tclIndex for our lib directory. I'm hoping this is not an unncessary burden on end-users who are building from source. I haven't really made any functionality changes here, this is just a huge migration of code from one file to many smaller files. All of the new changes are to setup the library path and install the library files. Signed-off-by: Shawn O. Pearce <spearce@spearce.org>

Shawn O. Pearce committed May 7, 2007 at 23:35 UTC f522c9b5ed367172f969397589ae3d686b867ac0
17 files changed +3982 -3938
.gitignore
+1
@@ -2,3 +2,4 @@ GIT-VERSION-FILE
2 GIT-GUI-VARS
3 git-citool
4 git-gui
5 +lib/tclIndex
Makefile
+21 -2
@@ -10,6 +10,7 @@ GIT-VERSION-FILE: .FORCE-GIT-VERSION-FILE
10 SCRIPT_SH = git-gui.sh
11 GITGUI_BUILT_INS = git-citool
12 ALL_PROGRAMS = $(GITGUI_BUILT_INS) $(patsubst %.sh,%,$(SCRIPT_SH))
13 +ALL_LIBFILES = $(wildcard lib/*.tcl)
14
15 ifndef SHELL_PATH
16 SHELL_PATH = /bin/sh
@@ -19,6 +20,10 @@ ifndef gitexecdir
20 gitexecdir := $(shell git --exec-path)
21 endif
22
23 +ifndef sharedir
24 + sharedir := $(dir $(gitexecdir))/share
25 +endif
26 +
27 ifndef INSTALL
28 INSTALL = install
29 endif
@@ -26,8 +31,10 @@ endif
31 ifndef V
32 QUIET_GEN = @echo ' ' GEN $@;
33 QUIET_BUILT_IN = @echo ' ' BUILTIN $@;
34 + QUIET_INDEX = @echo ' ' INDEX $(dir $@);
35 endif
36
37 +TCL_PATH ?= tclsh
38 TCLTK_PATH ?= wish
39
40 ifeq ($(findstring $(MAKEFLAGS),s),s)
@@ -40,11 +47,15 @@ gitexecdir_SQ = $(subst ','\'',$(gitexecdir))
47 SHELL_PATH_SQ = $(subst ','\'',$(SHELL_PATH))
48 TCLTK_PATH_SQ = $(subst ','\'',$(TCLTK_PATH))
49
50 +libdir ?= $(sharedir)/git-gui/lib
51 +libdir_SQ = $(subst ','\'',$(libdir))
52 +
53 $(patsubst %.sh,%,$(SCRIPT_SH)) : % : %.sh
54 $(QUIET_GEN)rm -f $@ $@+ && \
55 sed -e '1s|#!.*/sh|#!$(SHELL_PATH_SQ)|' \
56 -e 's|^exec wish "$$0"|exec $(subst |,'\|',$(TCLTK_PATH_SQ)) "$$0"|' \
57 -e 's/@@GITGUI_VERSION@@/$(GITGUI_VERSION)/g' \
58 + -e 's|@@GITGUI_LIBDIR@@|$(libdir_SQ)|' \
59 $@.sh >$@+ && \
60 chmod +x $@+ && \
61 mv $@+ $@
@@ -52,12 +63,16 @@ $(patsubst %.sh,%,$(SCRIPT_SH)) : % : %.sh
63 $(GITGUI_BUILT_INS): git-gui
64 $(QUIET_BUILT_IN)rm -f $@ && ln git-gui $@
65
66 +lib/tclIndex: $(ALL_LIBFILES)
67 + $(QUIET_INDEX)echo auto_mkindex lib '*.tcl' | $(TCL_PATH)
68 +
69 # These can record GITGUI_VERSION
70 $(patsubst %.sh,%,$(SCRIPT_SH)): GIT-VERSION-FILE GIT-GUI-VARS
71
72 TRACK_VARS = \
73 $(subst ','\'',SHELL_PATH='$(SHELL_PATH_SQ)') \
74 $(subst ','\'',TCLTK_PATH='$(TCLTK_PATH_SQ)') \
75 + $(subst ','\'',libdir='$(libdir_SQ)') \
76 #end TRACK_VARS
77
78 GIT-GUI-VARS: .FORCE-GIT-GUI-VARS
@@ -67,19 +82,23 @@ GIT-GUI-VARS: .FORCE-GIT-GUI-VARS
82 echo 1>$@ "$$VARS"; \
83 fi
84
70 -all:: $(ALL_PROGRAMS)
85 +all:: $(ALL_PROGRAMS) lib/tclIndex
86
87 install: all
88 $(INSTALL) -d -m755 '$(DESTDIR_SQ)$(gitexecdir_SQ)'
89 $(INSTALL) git-gui '$(DESTDIR_SQ)$(gitexecdir_SQ)'
90 $(foreach p,$(GITGUI_BUILT_INS), rm -f '$(DESTDIR_SQ)$(gitexecdir_SQ)/$p' && ln '$(DESTDIR_SQ)$(gitexecdir_SQ)/git-gui' '$(DESTDIR_SQ)$(gitexecdir_SQ)/$p' ;)
91 + $(INSTALL) -d -m755 '$(DESTDIR_SQ)$(libdir_SQ)'
92 + $(INSTALL) -m644 lib/tclIndex '$(DESTDIR_SQ)$(libdir_SQ)'
93 + $(foreach p,$(ALL_LIBFILES), $(INSTALL) -m644 $p '$(DESTDIR_SQ)$(libdir_SQ)' ;)
94
95 dist-version:
96 @mkdir -p $(TARDIR)
97 @echo $(GITGUI_VERSION) > $(TARDIR)/version
98
99 clean::
82 - rm -f $(ALL_PROGRAMS) GIT-VERSION-FILE GIT-GUI-VARS
100 + rm -f $(ALL_PROGRAMS) lib/tclIndex
101 + rm -f GIT-VERSION-FILE GIT-GUI-VARS
102
103 .PHONY: all install dist-version clean
104 .PHONY: .FORCE-GIT-VERSION-FILE
git-gui.sh
+172 -3936
@@ -20,6 +20,30 @@ You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA}
22
23 +######################################################################
24 +##
25 +## configure our library
26 +
27 +set oguilib {@@GITGUI_LIBDIR@@}
28 +if {[string match @@* $oguilib]} {
29 + set oguilib [file join [file dirname [file normalize $argv0]] lib]
30 +}
31 +set auto_path [concat [list $oguilib] $auto_path]
32 +
33 +if {![catch {set _verbose $env(GITGUI_VERBOSE)}]} {
34 + unset _verbose
35 + rename auto_load real__auto_load
36 + proc auto_load {name args} {
37 + puts stderr "auto_load $name"
38 + return [uplevel 1 real__auto_load $name $args]
39 + }
40 + rename source real__source
41 + proc source {name} {
42 + puts stderr "source $name"
43 + uplevel 1 real__source $name
44 + }
45 +}
46 +
47 ######################################################################
48 ##
49 ## read only globals
@@ -179,56 +203,6 @@ proc load_config {include_global} {
203 }
204 }
205
182 -proc save_config {} {
183 - global default_config font_descs
184 - global repo_config global_config
185 - global repo_config_new global_config_new
186 -
187 - foreach option $font_descs {
188 - set name [lindex $option 0]
189 - set font [lindex $option 1]
190 - font configure $font \
191 - -family $global_config_new(gui.$font^^family) \
192 - -size $global_config_new(gui.$font^^size)
193 - font configure ${font}bold \
194 - -family $global_config_new(gui.$font^^family) \
195 - -size $global_config_new(gui.$font^^size)
196 - set global_config_new(gui.$name) [font configure $font]
197 - unset global_config_new(gui.$font^^family)
198 - unset global_config_new(gui.$font^^size)
199 - }
200 -
201 - foreach name [array names default_config] {
202 - set value $global_config_new($name)
203 - if {$value ne $global_config($name)} {
204 - if {$value eq $default_config($name)} {
205 - catch {git config --global --unset $name}
206 - } else {
207 - regsub -all "\[{}\]" $value {"} value
208 - git config --global $name $value
209 - }
210 - set global_config($name) $value
211 - if {$value eq $repo_config($name)} {
212 - catch {git config --unset $name}
213 - set repo_config($name) $value
214 - }
215 - }
216 - }
217 -
218 - foreach name [array names default_config] {
219 - set value $repo_config_new($name)
220 - if {$value ne $repo_config($name)} {
221 - if {$value eq $global_config($name)} {
222 - catch {git config --unset $name}
223 - } else {
224 - regsub -all "\[{}\]" $value {"} value
225 - git config $name $value
226 - }
227 - set repo_config($name) $value
228 - }
229 - }
230 -}
231 -
206 ######################################################################
207 ##
208 ## handy utils
@@ -237,64 +211,6 @@ proc git {args} {
211 return [eval exec git $args]
212 }
213
240 -proc error_popup {msg} {
241 - set title [appname]
242 - if {[reponame] ne {}} {
243 - append title " ([reponame])"
244 - }
245 - set cmd [list tk_messageBox \
246 - -icon error \
247 - -type ok \
248 - -title "$title: error" \
249 - -message $msg]
250 - if {[winfo ismapped .]} {
251 - lappend cmd -parent .
252 - }
253 - eval $cmd
254 -}
255 -
256 -proc warn_popup {msg} {
257 - set title [appname]
258 - if {[reponame] ne {}} {
259 - append title " ([reponame])"
260 - }
261 - set cmd [list tk_messageBox \
262 - -icon warning \
263 - -type ok \
264 - -title "$title: warning" \
265 - -message $msg]
266 - if {[winfo ismapped .]} {
267 - lappend cmd -parent .
268 - }
269 - eval $cmd
270 -}
271 -
272 -proc info_popup {msg {parent .}} {
273 - set title [appname]
274 - if {[reponame] ne {}} {
275 - append title " ([reponame])"
276 - }
277 - tk_messageBox \
278 - -parent $parent \
279 - -icon info \
280 - -type ok \
281 - -title $title \
282 - -message $msg
283 -}
284 -
285 -proc ask_popup {msg} {
286 - set title [appname]
287 - if {[reponame] ne {}} {
288 - append title " ([reponame])"
289 - }
290 - return [tk_messageBox \
291 - -parent . \
292 - -icon question \
293 - -type yesno \
294 - -title $title \
295 - -message $msg]
296 -}
297 -
214 auto_load tk_optionMenu
215 rename tk_optionMenu real__tkOptionMenu
216 proc tk_optionMenu {w varName args} {
@@ -664,7 +580,7 @@ proc read_ls_others {fd after} {
580 }
581
582 proc rescan_done {fd buf after} {
667 - global rescan_active
583 + global rescan_active current_diff_path
584 global file_states repo_config
585 upvar $buf to_clear
586
@@ -676,7 +592,7 @@ proc rescan_done {fd buf after} {
592 prune_selection
593 unlock_index
594 display_all_files
679 - reshow_diff
595 + if {$current_diff_path ne {}} reshow_diff
596 uplevel #0 $after
597 }
598
@@ -692,2983 +608,195 @@ proc prune_selection {} {
608
609 ######################################################################
610 ##
695 -## diff
611 +## ui helpers
612
697 -proc clear_diff {} {
698 - global ui_diff current_diff_path current_diff_header
699 - global ui_index ui_workdir
613 +proc mapicon {w state path} {
614 + global all_icons
615
701 - $ui_diff conf -state normal
702 - $ui_diff delete 0.0 end
703 - $ui_diff conf -state disabled
616 + if {[catch {set r $all_icons($state$w)}]} {
617 + puts "error: no icon for $w state={$state} $path"
618 + return file_plain
619 + }
620 + return $r
621 +}
622
705 - set current_diff_path {}
706 - set current_diff_header {}
623 +proc mapdesc {state path} {
624 + global all_descs
625
708 - $ui_index tag remove in_diff 0.0 end
709 - $ui_workdir tag remove in_diff 0.0 end
626 + if {[catch {set r $all_descs($state)}]} {
627 + puts "error: no desc for state={$state} $path"
628 + return $state
629 + }
630 + return $r
631 }
632
712 -proc reshow_diff {} {
713 - global ui_status_value file_states file_lists
714 - global current_diff_path current_diff_side
715 -
716 - set p $current_diff_path
717 - if {$p eq {}} {
718 - # No diff is being shown.
719 - } elseif {$current_diff_side eq {}
720 - || [catch {set s $file_states($p)}]
721 - || [lsearch -sorted -exact $file_lists($current_diff_side) $p] == -1} {
722 - clear_diff
723 - } else {
724 - show_diff $p $current_diff_side
725 - }
633 +proc escape_path {path} {
634 + regsub -all {\\} $path "\\\\" path
635 + regsub -all "\n" $path "\\n" path
636 + return $path
637 }
638
728 -proc handle_empty_diff {} {
729 - global current_diff_path file_states file_lists
639 +proc short_path {path} {
640 + return [escape_path [lindex [file split $path] end]]
641 +}
642
731 - set path $current_diff_path
732 - set s $file_states($path)
733 - if {[lindex $s 0] ne {_M}} return
643 +set next_icon_id 0
644 +set null_sha1 [string repeat 0 40]
645
735 - info_popup "No differences detected.
646 +proc merge_state {path new_state {head_info {}} {index_info {}}} {
647 + global file_states next_icon_id null_sha1
648
737 -[short_path $path] has no changes.
649 + set s0 [string index $new_state 0]
650 + set s1 [string index $new_state 1]
651
739 -The modification date of this file was updated by another application, but the content within the file was not changed.
652 + if {[catch {set info $file_states($path)}]} {
653 + set state __
654 + set icon n[incr next_icon_id]
655 + } else {
656 + set state [lindex $info 0]
657 + set icon [lindex $info 1]
658 + if {$head_info eq {}} {set head_info [lindex $info 2]}
659 + if {$index_info eq {}} {set index_info [lindex $info 3]}
660 + }
661
741 -A rescan will be automatically started to find other files which may have the same state."
662 + if {$s0 eq {?}} {set s0 [string index $state 0]} \
663 + elseif {$s0 eq {_}} {set s0 _}
664
743 - clear_diff
744 - display_file $path __
745 - rescan {set ui_status_value {Ready.}} 0
746 -}
665 + if {$s1 eq {?}} {set s1 [string index $state 1]} \
666 + elseif {$s1 eq {_}} {set s1 _}
667
748 -proc show_diff {path w {lno {}}} {
749 - global file_states file_lists
750 - global is_3way_diff diff_active repo_config
751 - global ui_diff ui_status_value ui_index ui_workdir
752 - global current_diff_path current_diff_side current_diff_header
668 + if {$s0 eq {A} && $s1 eq {_} && $head_info eq {}} {
669 + set head_info [list 0 $null_sha1]
670 + } elseif {$s0 ne {_} && [string index $state 0] eq {_}
671 + && $head_info eq {}} {
672 + set head_info $index_info
673 + }
674 +
675 + set file_states($path) [list $s0$s1 $icon \
676 + $head_info $index_info \
677 + ]
678 + return $state
679 +}
680
754 - if {$diff_active || ![lock_index read]} return
681 +proc display_file_helper {w path icon_name old_m new_m} {
682 + global file_lists
683
756 - clear_diff
757 - if {$lno == {}} {
684 + if {$new_m eq {_}} {
685 set lno [lsearch -sorted -exact $file_lists($w) $path]
686 if {$lno >= 0} {
687 + set file_lists($w) [lreplace $file_lists($w) $lno $lno]
688 incr lno
689 + $w conf -state normal
690 + $w delete $lno.0 [expr {$lno + 1}].0
691 + $w conf -state disabled
692 }
693 + } elseif {$old_m eq {_} && $new_m ne {_}} {
694 + lappend file_lists($w) $path
695 + set file_lists($w) [lsort -unique $file_lists($w)]
696 + set lno [lsearch -sorted -exact $file_lists($w) $path]
697 + incr lno
698 + $w conf -state normal
699 + $w image create $lno.0 \
700 + -align center -padx 5 -pady 1 \
701 + -name $icon_name \
702 + -image [mapicon $w $new_m $path]
703 + $w insert $lno.1 "[escape_path $path]\n"
704 + $w conf -state disabled
705 + } elseif {$old_m ne $new_m} {
706 + $w conf -state normal
707 + $w image conf $icon_name -image [mapicon $w $new_m $path]
708 + $w conf -state disabled
709 }
763 - if {$lno >= 1} {
764 - $w tag add in_diff $lno.0 [expr {$lno + 1}].0
765 - }
710 +}
711
712 +proc display_file {path state} {
713 + global file_states selected_paths
714 + global ui_index ui_workdir
715 +
716 + set old_m [merge_state $path $state]
717 set s $file_states($path)
768 - set m [lindex $s 0]
769 - set is_3way_diff 0
770 - set diff_active 1
771 - set current_diff_path $path
772 - set current_diff_side $w
773 - set current_diff_header {}
774 - set ui_status_value "Loading diff of [escape_path $path]..."
775 -
776 - # - Git won't give us the diff, there's nothing to compare to!
777 - #
778 - if {$m eq {_O}} {
779 - set max_sz [expr {128 * 1024}]
780 - if {[catch {
781 - set fd [open $path r]
782 - set content [read $fd $max_sz]
783 - close $fd
784 - set sz [file size $path]
785 - } err ]} {
786 - set diff_active 0
787 - unlock_index
788 - set ui_status_value "Unable to display [escape_path $path]"
789 - error_popup "Error loading file:\n\n$err"
790 - return
791 - }
792 - $ui_diff conf -state normal
793 - if {![catch {set type [exec file $path]}]} {
794 - set n [string length $path]
795 - if {[string equal -length $n $path $type]} {
796 - set type [string range $type $n end]
797 - regsub {^:?\s*} $type {} type
798 - }
799 - $ui_diff insert end "* $type\n" d_@
800 - }
801 - if {[string first "\0" $content] != -1} {
802 - $ui_diff insert end \
803 - "* Binary file (not showing content)." \
804 - d_@
805 - } else {
806 - if {$sz > $max_sz} {
807 - $ui_diff insert end \
808 -"* Untracked file is $sz bytes.
809 -* Showing only first $max_sz bytes.
810 -" d_@
811 - }
812 - $ui_diff insert end $content
813 - if {$sz > $max_sz} {
814 - $ui_diff insert end "
815 -* Untracked file clipped here by [appname].
816 -* To see the entire file, use an external editor.
817 -" d_@
818 - }
819 - }
820 - $ui_diff conf -state disabled
821 - set diff_active 0
822 - unlock_index
823 - set ui_status_value {Ready.}
824 - return
825 - }
718 + set new_m [lindex $s 0]
719 + set icon_name [lindex $s 1]
720
827 - set cmd [list | git]
828 - if {$w eq $ui_index} {
829 - lappend cmd diff-index
830 - lappend cmd --cached
831 - } elseif {$w eq $ui_workdir} {
832 - if {[string index $m 0] eq {U}} {
833 - lappend cmd diff
834 - } else {
835 - lappend cmd diff-files
836 - }
721 + set o [string index $old_m 0]
722 + set n [string index $new_m 0]
723 + if {$o eq {U}} {
724 + set o _
725 }
838 -
839 - lappend cmd -p
840 - lappend cmd --no-color
841 - if {$repo_config(gui.diffcontext) > 0} {
842 - lappend cmd "-U$repo_config(gui.diffcontext)"
726 + if {$n eq {U}} {
727 + set n _
728 }
844 - if {$w eq $ui_index} {
845 - lappend cmd [PARENT]
729 + display_file_helper $ui_index $path $icon_name $o $n
730 +
731 + if {[string index $old_m 0] eq {U}} {
732 + set o U
733 + } else {
734 + set o [string index $old_m 1]
735 }
847 - lappend cmd --
848 - lappend cmd $path
849 -
850 - if {[catch {set fd [open $cmd r]} err]} {
851 - set diff_active 0
852 - unlock_index
853 - set ui_status_value "Unable to display [escape_path $path]"
854 - error_popup "Error loading diff:\n\n$err"
855 - return
736 + if {[string index $new_m 0] eq {U}} {
737 + set n U
738 + } else {
739 + set n [string index $new_m 1]
740 }
741 + display_file_helper $ui_workdir $path $icon_name $o $n
742
858 - fconfigure $fd \
859 - -blocking 0 \
860 - -encoding binary \
861 - -translation binary
862 - fileevent $fd readable [list read_diff $fd]
863 -}
864 -
865 -proc read_diff {fd} {
866 - global ui_diff ui_status_value diff_active
867 - global is_3way_diff current_diff_header
868 -
869 - $ui_diff conf -state normal
870 - while {[gets $fd line] >= 0} {
871 - # -- Cleanup uninteresting diff header lines.
872 - #
873 - if { [string match {diff --git *} $line]
874 - || [string match {diff --cc *} $line]
875 - || [string match {diff --combined *} $line]
876 - || [string match {--- *} $line]
877 - || [string match {+++ *} $line]} {
878 - append current_diff_header $line "\n"
879 - continue
880 - }
881 - if {[string match {index *} $line]} continue
882 - if {$line eq {deleted file mode 120000}} {
883 - set line "deleted symlink"
884 - }
885 -
886 - # -- Automatically detect if this is a 3 way diff.
887 - #
888 - if {[string match {@@@ *} $line]} {set is_3way_diff 1}
889 -
890 - if {[string match {mode *} $line]
891 - || [string match {new file *} $line]
892 - || [string match {deleted file *} $line]
893 - || [string match {Binary files * and * differ} $line]
894 - || $line eq {\ No newline at end of file}
895 - || [regexp {^\* Unmerged path } $line]} {
896 - set tags {}
897 - } elseif {$is_3way_diff} {
898 - set op [string range $line 0 1]
899 - switch -- $op {
900 - { } {set tags {}}
901 - {@@} {set tags d_@}
902 - { +} {set tags d_s+}
903 - { -} {set tags d_s-}
904 - {+ } {set tags d_+s}
905 - {- } {set tags d_-s}
906 - {--} {set tags d_--}
907 - {++} {
908 - if {[regexp {^\+\+([<>]{7} |={7})} $line _g op]} {
909 - set line [string replace $line 0 1 { }]
910 - set tags d$op
911 - } else {
912 - set tags d_++
913 - }
914 - }
915 - default {
916 - puts "error: Unhandled 3 way diff marker: {$op}"
917 - set tags {}
918 - }
919 - }
920 - } else {
921 - set op [string index $line 0]
922 - switch -- $op {
923 - { } {set tags {}}
924 - {@} {set tags d_@}
925 - {-} {set tags d_-}
926 - {+} {
927 - if {[regexp {^\+([<>]{7} |={7})} $line _g op]} {
928 - set line [string replace $line 0 0 { }]
929 - set tags d$op
930 - } else {
931 - set tags d_+
932 - }
933 - }
934 - default {
935 - puts "error: Unhandled 2 way diff marker: {$op}"
936 - set tags {}
937 - }
938 - }
939 - }
940 - $ui_diff insert end $line $tags
941 - if {[string index $line end] eq "\r"} {
942 - $ui_diff tag add d_cr {end - 2c}
943 - }
944 - $ui_diff insert end "\n" $tags
743 + if {$new_m eq {__}} {
744 + unset file_states($path)
745 + catch {unset selected_paths($path)}
746 }
946 - $ui_diff conf -state disabled
747 +}
748
948 - if {[eof $fd]} {
949 - close $fd
950 - set diff_active 0
951 - unlock_index
952 - set ui_status_value {Ready.}
749 +proc display_all_files_helper {w path icon_name m} {
750 + global file_lists
751
954 - if {[$ui_diff index end] eq {2.0}} {
955 - handle_empty_diff
956 - }
957 - }
752 + lappend file_lists($w) $path
753 + set lno [expr {[lindex [split [$w index end] .] 0] - 1}]
754 + $w image create end \
755 + -align center -padx 5 -pady 1 \
756 + -name $icon_name \
757 + -image [mapicon $w $m $path]
758 + $w insert end "[escape_path $path]\n"
759 }
760
960 -proc apply_hunk {x y} {
961 - global current_diff_path current_diff_header current_diff_side
962 - global ui_diff ui_index file_states
963 -
964 - if {$current_diff_path eq {} || $current_diff_header eq {}} return
965 - if {![lock_index apply_hunk]} return
761 +proc display_all_files {} {
762 + global ui_index ui_workdir
763 + global file_states file_lists
764 + global last_clicked
765
967 - set apply_cmd {git apply --cached --whitespace=nowarn}
968 - set mi [lindex $file_states($current_diff_path) 0]
969 - if {$current_diff_side eq $ui_index} {
970 - set mode unstage
971 - lappend apply_cmd --reverse
972 - if {[string index $mi 0] ne {M}} {
973 - unlock_index
974 - return
975 - }
976 - } else {
977 - set mode stage
978 - if {[string index $mi 1] ne {M}} {
979 - unlock_index
980 - return
981 - }
982 - }
766 + $ui_index conf -state normal
767 + $ui_workdir conf -state normal
768
984 - set s_lno [lindex [split [$ui_diff index @$x,$y] .] 0]
985 - set s_lno [$ui_diff search -backwards -regexp ^@@ $s_lno.0 0.0]
986 - if {$s_lno eq {}} {
987 - unlock_index
988 - return
989 - }
769 + $ui_index delete 0.0 end
770 + $ui_workdir delete 0.0 end
771 + set last_clicked {}
772
991 - set e_lno [$ui_diff search -forwards -regexp ^@@ "$s_lno + 1 lines" end]
992 - if {$e_lno eq {}} {
993 - set e_lno end
994 - }
773 + set file_lists($ui_index) [list]
774 + set file_lists($ui_workdir) [list]
775
996 - if {[catch {
997 - set p [open "| $apply_cmd" w]
998 - fconfigure $p -translation binary -encoding binary
999 - puts -nonewline $p $current_diff_header
1000 - puts -nonewline $p [$ui_diff get $s_lno $e_lno]
1001 - close $p} err]} {
1002 - error_popup "Failed to $mode selected hunk.\n\n$err"
1003 - unlock_index
1004 - return
1005 - }
776 + foreach path [lsort [array names file_states]] {
777 + set s $file_states($path)
778 + set m [lindex $s 0]
779 + set icon_name [lindex $s 1]
780
1007 - $ui_diff conf -state normal
1008 - $ui_diff delete $s_lno $e_lno
1009 - $ui_diff conf -state disabled
781 + set s [string index $m 0]
782 + if {$s ne {U} && $s ne {_}} {
783 + display_all_files_helper $ui_index $path \
784 + $icon_name $s
785 + }
786
1011 - if {[$ui_diff get 1.0 end] eq "\n"} {
1012 - set o _
1013 - } else {
1014 - set o ?
787 + if {[string index $m 0] eq {U}} {
788 + set s U
789 + } else {
790 + set s [string index $m 1]
791 + }
792 + if {$s ne {_}} {
793 + display_all_files_helper $ui_workdir $path \
794 + $icon_name $s
795 + }
796 }
797
1017 - if {$current_diff_side eq $ui_index} {
1018 - set mi ${o}M
1019 - } elseif {[string index $mi 0] eq {_}} {
1020 - set mi M$o
1021 - } else {
1022 - set mi ?$o
1023 - }
1024 - unlock_index
1025 - display_file $current_diff_path $mi
1026 - if {$o eq {_}} {
1027 - clear_diff
1028 - }
1029 -}
1030 -
1031 -######################################################################
1032 -##
1033 -## commit
1034 -
1035 -proc load_last_commit {} {
1036 - global HEAD PARENT MERGE_HEAD commit_type ui_comm
1037 - global repo_config
1038 -
1039 - if {[llength $PARENT] == 0} {
1040 - error_popup {There is nothing to amend.
1041 -
1042 -You are about to create the initial commit. There is no commit before this to amend.
1043 -}
1044 - return
1045 - }
1046 -
1047 - repository_state curType curHEAD curMERGE_HEAD
1048 - if {$curType eq {merge}} {
1049 - error_popup {Cannot amend while merging.
1050 -
1051 -You are currently in the middle of a merge that has not been fully completed. You cannot amend the prior commit unless you first abort the current merge activity.
1052 -}
1053 - return
1054 - }
1055 -
1056 - set msg {}
1057 - set parents [list]
1058 - if {[catch {
1059 - set fd [open "| git cat-file commit $curHEAD" r]
1060 - fconfigure $fd -encoding binary -translation lf
1061 - if {[catch {set enc $repo_config(i18n.commitencoding)}]} {
1062 - set enc utf-8
1063 - }
1064 - while {[gets $fd line] > 0} {
1065 - if {[string match {parent *} $line]} {
1066 - lappend parents [string range $line 7 end]
1067 - } elseif {[string match {encoding *} $line]} {
1068 - set enc [string tolower [string range $line 9 end]]
1069 - }
1070 - }
1071 - set msg [encoding convertfrom $enc [read $fd]]
1072 - set msg [string trim $msg]
1073 - close $fd
1074 - } err]} {
1075 - error_popup "Error loading commit data for amend:\n\n$err"
1076 - return
1077 - }
1078 -
1079 - set HEAD $curHEAD
1080 - set PARENT $parents
1081 - set MERGE_HEAD [list]
1082 - switch -- [llength $parents] {
1083 - 0 {set commit_type amend-initial}
1084 - 1 {set commit_type amend}
1085 - default {set commit_type amend-merge}
1086 - }
1087 -
1088 - $ui_comm delete 0.0 end
1089 - $ui_comm insert end $msg
1090 - $ui_comm edit reset
1091 - $ui_comm edit modified false
1092 - rescan {set ui_status_value {Ready.}}
1093 -}
1094 -
1095 -proc create_new_commit {} {
1096 - global commit_type ui_comm
1097 -
1098 - set commit_type normal
1099 - $ui_comm delete 0.0 end
1100 - $ui_comm edit reset
1101 - $ui_comm edit modified false
1102 - rescan {set ui_status_value {Ready.}}
1103 -}
1104 -
1105 -set GIT_COMMITTER_IDENT {}
1106 -
1107 -proc committer_ident {} {
1108 - global GIT_COMMITTER_IDENT
1109 -
1110 - if {$GIT_COMMITTER_IDENT eq {}} {
1111 - if {[catch {set me [git var GIT_COMMITTER_IDENT]} err]} {
1112 - error_popup "Unable to obtain your identity:\n\n$err"
1113 - return {}
1114 - }
1115 - if {![regexp {^(.*) [0-9]+ [-+0-9]+$} \
1116 - $me me GIT_COMMITTER_IDENT]} {
1117 - error_popup "Invalid GIT_COMMITTER_IDENT:\n\n$me"
1118 - return {}
1119 - }
1120 - }
1121 -
1122 - return $GIT_COMMITTER_IDENT
1123 -}
1124 -
1125 -proc commit_tree {} {
1126 - global HEAD commit_type file_states ui_comm repo_config
1127 - global ui_status_value pch_error
1128 -
1129 - if {[committer_ident] eq {}} return
1130 - if {![lock_index update]} return
1131 -
1132 - # -- Our in memory state should match the repository.
1133 - #
1134 - repository_state curType curHEAD curMERGE_HEAD
1135 - if {[string match amend* $commit_type]
1136 - && $curType eq {normal}
1137 - && $curHEAD eq $HEAD} {
1138 - } elseif {$commit_type ne $curType || $HEAD ne $curHEAD} {
1139 - info_popup {Last scanned state does not match repository state.
1140 -
1141 -Another Git program has modified this repository since the last scan. A rescan must be performed before another commit can be created.
1142 -
1143 -The rescan will be automatically started now.
1144 -}
1145 - unlock_index
1146 - rescan {set ui_status_value {Ready.}}
1147 - return
1148 - }
1149 -
1150 - # -- At least one file should differ in the index.
1151 - #
1152 - set files_ready 0
1153 - foreach path [array names file_states] {
1154 - switch -glob -- [lindex $file_states($path) 0] {
1155 - _? {continue}
1156 - A? -
1157 - D? -
1158 - M? {set files_ready 1}
1159 - U? {
1160 - error_popup "Unmerged files cannot be committed.
1161 -
1162 -File [short_path $path] has merge conflicts. You must resolve them and add the file before committing.
1163 -"
1164 - unlock_index
1165 - return
1166 - }
1167 - default {
1168 - error_popup "Unknown file state [lindex $s 0] detected.
1169 -
1170 -File [short_path $path] cannot be committed by this program.
1171 -"
1172 - }
1173 - }
1174 - }
1175 - if {!$files_ready && ![string match *merge $curType]} {
1176 - info_popup {No changes to commit.
1177 -
1178 -You must add at least 1 file before you can commit.
1179 -}
1180 - unlock_index
1181 - return
1182 - }
1183 -
1184 - # -- A message is required.
1185 - #
1186 - set msg [string trim [$ui_comm get 1.0 end]]
1187 - regsub -all -line {[ \t\r]+$} $msg {} msg
1188 - if {$msg eq {}} {
1189 - error_popup {Please supply a commit message.
1190 -
1191 -A good commit message has the following format:
1192 -
1193 -- First line: Describe in one sentance what you did.
1194 -- Second line: Blank
1195 -- Remaining lines: Describe why this change is good.
1196 -}
1197 - unlock_index
1198 - return
1199 - }
1200 -
1201 - # -- Run the pre-commit hook.
1202 - #
1203 - set pchook [gitdir hooks pre-commit]
1204 -
1205 - # On Cygwin [file executable] might lie so we need to ask
1206 - # the shell if the hook is executable. Yes that's annoying.
1207 - #
1208 - if {[is_Cygwin] && [file isfile $pchook]} {
1209 - set pchook [list sh -c [concat \
1210 - "if test -x \"$pchook\";" \
1211 - "then exec \"$pchook\" 2>&1;" \
1212 - "fi"]]
1213 - } elseif {[file executable $pchook]} {
1214 - set pchook [list $pchook |& cat]
1215 - } else {
1216 - commit_writetree $curHEAD $msg
1217 - return
1218 - }
1219 -
1220 - set ui_status_value {Calling pre-commit hook...}
1221 - set pch_error {}
1222 - set fd_ph [open "| $pchook" r]
1223 - fconfigure $fd_ph -blocking 0 -translation binary
1224 - fileevent $fd_ph readable \
1225 - [list commit_prehook_wait $fd_ph $curHEAD $msg]
1226 -}
1227 -
1228 -proc commit_prehook_wait {fd_ph curHEAD msg} {
1229 - global pch_error ui_status_value
1230 -
1231 - append pch_error [read $fd_ph]
1232 - fconfigure $fd_ph -blocking 1
1233 - if {[eof $fd_ph]} {
1234 - if {[catch {close $fd_ph}]} {
1235 - set ui_status_value {Commit declined by pre-commit hook.}
1236 - hook_failed_popup pre-commit $pch_error
1237 - unlock_index
1238 - } else {
1239 - commit_writetree $curHEAD $msg
1240 - }
1241 - set pch_error {}
1242 - return
1243 - }
1244 - fconfigure $fd_ph -blocking 0
1245 -}
1246 -
1247 -proc commit_writetree {curHEAD msg} {
1248 - global ui_status_value
1249 -
1250 - set ui_status_value {Committing changes...}
1251 - set fd_wt [open "| git write-tree" r]
1252 - fileevent $fd_wt readable \
1253 - [list commit_committree $fd_wt $curHEAD $msg]
1254 -}
1255 -
1256 -proc commit_committree {fd_wt curHEAD msg} {
1257 - global HEAD PARENT MERGE_HEAD commit_type
1258 - global all_heads current_branch
1259 - global ui_status_value ui_comm selected_commit_type
1260 - global file_states selected_paths rescan_active
1261 - global repo_config
1262 -
1263 - gets $fd_wt tree_id
1264 - if {$tree_id eq {} || [catch {close $fd_wt} err]} {
1265 - error_popup "write-tree failed:\n\n$err"
1266 - set ui_status_value {Commit failed.}
1267 - unlock_index
1268 - return
1269 - }
1270 -
1271 - # -- Verify this wasn't an empty change.
1272 - #
1273 - if {$commit_type eq {normal}} {
1274 - set old_tree [git rev-parse "$PARENT^{tree}"]
1275 - if {$tree_id eq $old_tree} {
1276 - info_popup {No changes to commit.
1277 -
1278 -No files were modified by this commit and it was not a merge commit.
1279 -
1280 -A rescan will be automatically started now.
1281 -}
1282 - unlock_index
1283 - rescan {set ui_status_value {No changes to commit.}}
1284 - return
1285 - }
1286 - }
1287 -
1288 - # -- Build the message.
1289 - #
1290 - set msg_p [gitdir COMMIT_EDITMSG]
1291 - set msg_wt [open $msg_p w]
1292 - if {[catch {set enc $repo_config(i18n.commitencoding)}]} {
1293 - set enc utf-8
1294 - }
1295 - fconfigure $msg_wt -encoding binary -translation binary
1296 - puts -nonewline $msg_wt [encoding convertto $enc $msg]
1297 - close $msg_wt
1298 -
1299 - # -- Create the commit.
1300 - #
1301 - set cmd [list commit-tree $tree_id]
1302 - foreach p [concat $PARENT $MERGE_HEAD] {
1303 - lappend cmd -p $p
1304 - }
1305 - lappend cmd <$msg_p
1306 - if {[catch {set cmt_id [eval git $cmd]} err]} {
1307 - error_popup "commit-tree failed:\n\n$err"
1308 - set ui_status_value {Commit failed.}
1309 - unlock_index
1310 - return
1311 - }
1312 -
1313 - # -- Update the HEAD ref.
1314 - #
1315 - set reflogm commit
1316 - if {$commit_type ne {normal}} {
1317 - append reflogm " ($commit_type)"
1318 - }
1319 - set i [string first "\n" $msg]
1320 - if {$i >= 0} {
1321 - set subject [string range $msg 0 [expr {$i - 1}]]
1322 - } else {
1323 - set subject $msg
1324 - }
1325 - append reflogm {: } $subject
1326 - if {[catch {
1327 - git update-ref -m $reflogm HEAD $cmt_id $curHEAD
1328 - } err]} {
1329 - error_popup "update-ref failed:\n\n$err"
1330 - set ui_status_value {Commit failed.}
1331 - unlock_index
1332 - return
1333 - }
1334 -
1335 - # -- Cleanup after ourselves.
1336 - #
1337 - catch {file delete $msg_p}
1338 - catch {file delete [gitdir MERGE_HEAD]}
1339 - catch {file delete [gitdir MERGE_MSG]}
1340 - catch {file delete [gitdir SQUASH_MSG]}
1341 - catch {file delete [gitdir GITGUI_MSG]}
1342 -
1343 - # -- Let rerere do its thing.
1344 - #
1345 - if {[file isdirectory [gitdir rr-cache]]} {
1346 - catch {git rerere}
1347 - }
1348 -
1349 - # -- Run the post-commit hook.
1350 - #
1351 - set pchook [gitdir hooks post-commit]
1352 - if {[is_Cygwin] && [file isfile $pchook]} {
1353 - set pchook [list sh -c [concat \
1354 - "if test -x \"$pchook\";" \
1355 - "then exec \"$pchook\";" \
1356 - "fi"]]
1357 - } elseif {![file executable $pchook]} {
1358 - set pchook {}
1359 - }
1360 - if {$pchook ne {}} {
1361 - catch {exec $pchook &}
1362 - }
1363 -
1364 - $ui_comm delete 0.0 end
1365 - $ui_comm edit reset
1366 - $ui_comm edit modified false
1367 -
1368 - if {[is_enabled singlecommit]} do_quit
1369 -
1370 - # -- Make sure our current branch exists.
1371 - #
1372 - if {$commit_type eq {initial}} {
1373 - lappend all_heads $current_branch
1374 - set all_heads [lsort -unique $all_heads]
1375 - populate_branch_menu
1376 - }
1377 -
1378 - # -- Update in memory status
1379 - #
1380 - set selected_commit_type new
1381 - set commit_type normal
1382 - set HEAD $cmt_id
1383 - set PARENT $cmt_id
1384 - set MERGE_HEAD [list]
1385 -
1386 - foreach path [array names file_states] {
1387 - set s $file_states($path)
1388 - set m [lindex $s 0]
1389 - switch -glob -- $m {
1390 - _O -
1391 - _M -
1392 - _D {continue}
1393 - __ -
1394 - A_ -
1395 - M_ -
1396 - D_ {
1397 - unset file_states($path)
1398 - catch {unset selected_paths($path)}
1399 - }
1400 - DO {
1401 - set file_states($path) [list _O [lindex $s 1] {} {}]
1402 - }
1403 - AM -
1404 - AD -
1405 - MM -
1406 - MD {
1407 - set file_states($path) [list \
1408 - _[string index $m 1] \
1409 - [lindex $s 1] \
1410 - [lindex $s 3] \
1411 - {}]
1412 - }
1413 - }
1414 - }
1415 -
1416 - display_all_files
1417 - unlock_index
1418 - reshow_diff
1419 - set ui_status_value \
1420 - "Created commit [string range $cmt_id 0 7]: $subject"
1421 -}
1422 -
1423 -######################################################################
1424 -##
1425 -## fetch push
1426 -
1427 -proc fetch_from {remote} {
1428 - set w [new_console \
1429 - "fetch $remote" \
1430 - "Fetching new changes from $remote"]
1431 - set cmd [list git fetch]
1432 - lappend cmd $remote
1433 - console_exec $w $cmd console_done
1434 -}
1435 -
1436 -proc push_to {remote} {
1437 - set w [new_console \
1438 - "push $remote" \
1439 - "Pushing changes to $remote"]
1440 - set cmd [list git push]
1441 - lappend cmd -v
1442 - lappend cmd $remote
1443 - console_exec $w $cmd console_done
1444 -}
1445 -
1446 -######################################################################
1447 -##
1448 -## ui helpers
1449 -
1450 -proc mapicon {w state path} {
1451 - global all_icons
1452 -
1453 - if {[catch {set r $all_icons($state$w)}]} {
1454 - puts "error: no icon for $w state={$state} $path"
1455 - return file_plain
1456 - }
1457 - return $r
1458 -}
1459 -
1460 -proc mapdesc {state path} {
1461 - global all_descs
1462 -
1463 - if {[catch {set r $all_descs($state)}]} {
1464 - puts "error: no desc for state={$state} $path"
1465 - return $state
1466 - }
1467 - return $r
1468 -}
1469 -
1470 -proc escape_path {path} {
1471 - regsub -all {\\} $path "\\\\" path
1472 - regsub -all "\n" $path "\\n" path
1473 - return $path
1474 -}
1475 -
1476 -proc short_path {path} {
1477 - return [escape_path [lindex [file split $path] end]]
1478 -}
1479 -
1480 -set next_icon_id 0
1481 -set null_sha1 [string repeat 0 40]
1482 -
1483 -proc merge_state {path new_state {head_info {}} {index_info {}}} {
1484 - global file_states next_icon_id null_sha1
1485 -
1486 - set s0 [string index $new_state 0]
1487 - set s1 [string index $new_state 1]
1488 -
1489 - if {[catch {set info $file_states($path)}]} {
1490 - set state __
1491 - set icon n[incr next_icon_id]
1492 - } else {
1493 - set state [lindex $info 0]
1494 - set icon [lindex $info 1]
1495 - if {$head_info eq {}} {set head_info [lindex $info 2]}
1496 - if {$index_info eq {}} {set index_info [lindex $info 3]}
1497 - }
1498 -
1499 - if {$s0 eq {?}} {set s0 [string index $state 0]} \
1500 - elseif {$s0 eq {_}} {set s0 _}
1501 -
1502 - if {$s1 eq {?}} {set s1 [string index $state 1]} \
1503 - elseif {$s1 eq {_}} {set s1 _}
1504 -
1505 - if {$s0 eq {A} && $s1 eq {_} && $head_info eq {}} {
1506 - set head_info [list 0 $null_sha1]
1507 - } elseif {$s0 ne {_} && [string index $state 0] eq {_}
1508 - && $head_info eq {}} {
1509 - set head_info $index_info
1510 - }
1511 -
1512 - set file_states($path) [list $s0$s1 $icon \
1513 - $head_info $index_info \
1514 - ]
1515 - return $state
1516 -}
1517 -
1518 -proc display_file_helper {w path icon_name old_m new_m} {
1519 - global file_lists
1520 -
1521 - if {$new_m eq {_}} {
1522 - set lno [lsearch -sorted -exact $file_lists($w) $path]
1523 - if {$lno >= 0} {
1524 - set file_lists($w) [lreplace $file_lists($w) $lno $lno]
1525 - incr lno
1526 - $w conf -state normal
1527 - $w delete $lno.0 [expr {$lno + 1}].0
1528 - $w conf -state disabled
1529 - }
1530 - } elseif {$old_m eq {_} && $new_m ne {_}} {
1531 - lappend file_lists($w) $path
1532 - set file_lists($w) [lsort -unique $file_lists($w)]
1533 - set lno [lsearch -sorted -exact $file_lists($w) $path]
1534 - incr lno
1535 - $w conf -state normal
1536 - $w image create $lno.0 \
1537 - -align center -padx 5 -pady 1 \
1538 - -name $icon_name \
1539 - -image [mapicon $w $new_m $path]
1540 - $w insert $lno.1 "[escape_path $path]\n"
1541 - $w conf -state disabled
1542 - } elseif {$old_m ne $new_m} {
1543 - $w conf -state normal
1544 - $w image conf $icon_name -image [mapicon $w $new_m $path]
1545 - $w conf -state disabled
1546 - }
1547 -}
1548 -
1549 -proc display_file {path state} {
1550 - global file_states selected_paths
1551 - global ui_index ui_workdir
1552 -
1553 - set old_m [merge_state $path $state]
1554 - set s $file_states($path)
1555 - set new_m [lindex $s 0]
1556 - set icon_name [lindex $s 1]
1557 -
1558 - set o [string index $old_m 0]
1559 - set n [string index $new_m 0]
1560 - if {$o eq {U}} {
1561 - set o _
1562 - }
1563 - if {$n eq {U}} {
1564 - set n _
1565 - }
1566 - display_file_helper $ui_index $path $icon_name $o $n
1567 -
1568 - if {[string index $old_m 0] eq {U}} {
1569 - set o U
1570 - } else {
1571 - set o [string index $old_m 1]
1572 - }
1573 - if {[string index $new_m 0] eq {U}} {
1574 - set n U
1575 - } else {
1576 - set n [string index $new_m 1]
1577 - }
1578 - display_file_helper $ui_workdir $path $icon_name $o $n
1579 -
1580 - if {$new_m eq {__}} {
1581 - unset file_states($path)
1582 - catch {unset selected_paths($path)}
1583 - }
1584 -}
1585 -
1586 -proc display_all_files_helper {w path icon_name m} {
1587 - global file_lists
1588 -
1589 - lappend file_lists($w) $path
1590 - set lno [expr {[lindex [split [$w index end] .] 0] - 1}]
1591 - $w image create end \
1592 - -align center -padx 5 -pady 1 \
1593 - -name $icon_name \
1594 - -image [mapicon $w $m $path]
1595 - $w insert end "[escape_path $path]\n"
1596 -}
1597 -
1598 -proc display_all_files {} {
1599 - global ui_index ui_workdir
1600 - global file_states file_lists
1601 - global last_clicked
1602 -
1603 - $ui_index conf -state normal
1604 - $ui_workdir conf -state normal
1605 -
1606 - $ui_index delete 0.0 end
1607 - $ui_workdir delete 0.0 end
1608 - set last_clicked {}
1609 -
1610 - set file_lists($ui_index) [list]
1611 - set file_lists($ui_workdir) [list]
1612 -
1613 - foreach path [lsort [array names file_states]] {
1614 - set s $file_states($path)
1615 - set m [lindex $s 0]
1616 - set icon_name [lindex $s 1]
1617 -
1618 - set s [string index $m 0]
1619 - if {$s ne {U} && $s ne {_}} {
1620 - display_all_files_helper $ui_index $path \
1621 - $icon_name $s
1622 - }
1623 -
1624 - if {[string index $m 0] eq {U}} {
1625 - set s U
1626 - } else {
1627 - set s [string index $m 1]
1628 - }
1629 - if {$s ne {_}} {
1630 - display_all_files_helper $ui_workdir $path \
1631 - $icon_name $s
1632 - }
1633 - }
1634 -
1635 - $ui_index conf -state disabled
1636 - $ui_workdir conf -state disabled
1637 -}
1638 -
1639 -proc update_indexinfo {msg pathList after} {
1640 - global update_index_cp ui_status_value
1641 -
1642 - if {![lock_index update]} return
1643 -
1644 - set update_index_cp 0
1645 - set pathList [lsort $pathList]
1646 - set totalCnt [llength $pathList]
1647 - set batch [expr {int($totalCnt * .01) + 1}]
1648 - if {$batch > 25} {set batch 25}
1649 -
1650 - set ui_status_value [format \
1651 - "$msg... %i/%i files (%.2f%%)" \
1652 - $update_index_cp \
1653 - $totalCnt \
1654 - 0.0]
1655 - set fd [open "| git update-index -z --index-info" w]
1656 - fconfigure $fd \
1657 - -blocking 0 \
1658 - -buffering full \
1659 - -buffersize 512 \
1660 - -encoding binary \
1661 - -translation binary
1662 - fileevent $fd writable [list \
1663 - write_update_indexinfo \
1664 - $fd \
1665 - $pathList \
1666 - $totalCnt \
1667 - $batch \
1668 - $msg \
1669 - $after \
1670 - ]
1671 -}
1672 -
1673 -proc write_update_indexinfo {fd pathList totalCnt batch msg after} {
1674 - global update_index_cp ui_status_value
1675 - global file_states current_diff_path
1676 -
1677 - if {$update_index_cp >= $totalCnt} {
1678 - close $fd
1679 - unlock_index
1680 - uplevel #0 $after
1681 - return
1682 - }
1683 -
1684 - for {set i $batch} \
1685 - {$update_index_cp < $totalCnt && $i > 0} \
1686 - {incr i -1} {
1687 - set path [lindex $pathList $update_index_cp]
1688 - incr update_index_cp
1689 -
1690 - set s $file_states($path)
1691 - switch -glob -- [lindex $s 0] {
1692 - A? {set new _O}
1693 - M? {set new _M}
1694 - D_ {set new _D}
1695 - D? {set new _?}
1696 - ?? {continue}
1697 - }
1698 - set info [lindex $s 2]
1699 - if {$info eq {}} continue
1700 -
1701 - puts -nonewline $fd "$info\t[encoding convertto $path]\0"
1702 - display_file $path $new
1703 - }
1704 -
1705 - set ui_status_value [format \
1706 - "$msg... %i/%i files (%.2f%%)" \
1707 - $update_index_cp \
1708 - $totalCnt \
1709 - [expr {100.0 * $update_index_cp / $totalCnt}]]
1710 -}
1711 -
1712 -proc update_index {msg pathList after} {
1713 - global update_index_cp ui_status_value
1714 -
1715 - if {![lock_index update]} return
1716 -
1717 - set update_index_cp 0
1718 - set pathList [lsort $pathList]
1719 - set totalCnt [llength $pathList]
1720 - set batch [expr {int($totalCnt * .01) + 1}]
1721 - if {$batch > 25} {set batch 25}
1722 -
1723 - set ui_status_value [format \
1724 - "$msg... %i/%i files (%.2f%%)" \
1725 - $update_index_cp \
1726 - $totalCnt \
1727 - 0.0]
1728 - set fd [open "| git update-index --add --remove -z --stdin" w]
1729 - fconfigure $fd \
1730 - -blocking 0 \
1731 - -buffering full \
1732 - -buffersize 512 \
1733 - -encoding binary \
1734 - -translation binary
1735 - fileevent $fd writable [list \
1736 - write_update_index \
1737 - $fd \
1738 - $pathList \
1739 - $totalCnt \
1740 - $batch \
1741 - $msg \
1742 - $after \
1743 - ]
1744 -}
1745 -
1746 -proc write_update_index {fd pathList totalCnt batch msg after} {
1747 - global update_index_cp ui_status_value
1748 - global file_states current_diff_path
1749 -
1750 - if {$update_index_cp >= $totalCnt} {
1751 - close $fd
1752 - unlock_index
1753 - uplevel #0 $after
1754 - return
1755 - }
1756 -
1757 - for {set i $batch} \
1758 - {$update_index_cp < $totalCnt && $i > 0} \
1759 - {incr i -1} {
1760 - set path [lindex $pathList $update_index_cp]
1761 - incr update_index_cp
1762 -
1763 - switch -glob -- [lindex $file_states($path) 0] {
1764 - AD {set new __}
1765 - ?D {set new D_}
1766 - _O -
1767 - AM {set new A_}
1768 - U? {
1769 - if {[file exists $path]} {
1770 - set new M_
1771 - } else {
1772 - set new D_
1773 - }
1774 - }
1775 - ?M {set new M_}
1776 - ?? {continue}
1777 - }
1778 - puts -nonewline $fd "[encoding convertto $path]\0"
1779 - display_file $path $new
1780 - }
1781 -
1782 - set ui_status_value [format \
1783 - "$msg... %i/%i files (%.2f%%)" \
1784 - $update_index_cp \
1785 - $totalCnt \
1786 - [expr {100.0 * $update_index_cp / $totalCnt}]]
1787 -}
1788 -
1789 -proc checkout_index {msg pathList after} {
1790 - global update_index_cp ui_status_value
1791 -
1792 - if {![lock_index update]} return
1793 -
1794 - set update_index_cp 0
1795 - set pathList [lsort $pathList]
1796 - set totalCnt [llength $pathList]
1797 - set batch [expr {int($totalCnt * .01) + 1}]
1798 - if {$batch > 25} {set batch 25}
1799 -
1800 - set ui_status_value [format \
1801 - "$msg... %i/%i files (%.2f%%)" \
1802 - $update_index_cp \
1803 - $totalCnt \
1804 - 0.0]
1805 - set cmd [list git checkout-index]
1806 - lappend cmd --index
1807 - lappend cmd --quiet
1808 - lappend cmd --force
1809 - lappend cmd -z
1810 - lappend cmd --stdin
1811 - set fd [open "| $cmd " w]
1812 - fconfigure $fd \
1813 - -blocking 0 \
1814 - -buffering full \
1815 - -buffersize 512 \
1816 - -encoding binary \
1817 - -translation binary
1818 - fileevent $fd writable [list \
1819 - write_checkout_index \
1820 - $fd \
1821 - $pathList \
1822 - $totalCnt \
1823 - $batch \
1824 - $msg \
1825 - $after \
1826 - ]
1827 -}
1828 -
1829 -proc write_checkout_index {fd pathList totalCnt batch msg after} {
1830 - global update_index_cp ui_status_value
1831 - global file_states current_diff_path
1832 -
1833 - if {$update_index_cp >= $totalCnt} {
1834 - close $fd
1835 - unlock_index
1836 - uplevel #0 $after
1837 - return
1838 - }
1839 -
1840 - for {set i $batch} \
1841 - {$update_index_cp < $totalCnt && $i > 0} \
1842 - {incr i -1} {
1843 - set path [lindex $pathList $update_index_cp]
1844 - incr update_index_cp
1845 - switch -glob -- [lindex $file_states($path) 0] {
1846 - U? {continue}
1847 - ?M -
1848 - ?D {
1849 - puts -nonewline $fd "[encoding convertto $path]\0"
1850 - display_file $path ?_
1851 - }
1852 - }
1853 - }
1854 -
1855 - set ui_status_value [format \
1856 - "$msg... %i/%i files (%.2f%%)" \
1857 - $update_index_cp \
1858 - $totalCnt \
1859 - [expr {100.0 * $update_index_cp / $totalCnt}]]
1860 -}
1861 -
1862 -######################################################################
1863 -##
1864 -## branch management
1865 -
1866 -proc is_tracking_branch {name} {
1867 - global tracking_branches
1868 -
1869 - if {![catch {set info $tracking_branches($name)}]} {
1870 - return 1
1871 - }
1872 - foreach t [array names tracking_branches] {
1873 - if {[string match {*/\*} $t] && [string match $t $name]} {
1874 - return 1
1875 - }
1876 - }
1877 - return 0
1878 -}
1879 -
1880 -proc load_all_heads {} {
1881 - global all_heads
1882 -
1883 - set all_heads [list]
1884 - set fd [open "| git for-each-ref --format=%(refname) refs/heads" r]
1885 - while {[gets $fd line] > 0} {
1886 - if {[is_tracking_branch $line]} continue
1887 - if {![regsub ^refs/heads/ $line {} name]} continue
1888 - lappend all_heads $name
1889 - }
1890 - close $fd
1891 -
1892 - set all_heads [lsort $all_heads]
1893 -}
1894 -
1895 -proc populate_branch_menu {} {
1896 - global all_heads disable_on_lock
1897 -
1898 - set m .mbar.branch
1899 - set last [$m index last]
1900 - for {set i 0} {$i <= $last} {incr i} {
1901 - if {[$m type $i] eq {separator}} {
1902 - $m delete $i last
1903 - set new_dol [list]
1904 - foreach a $disable_on_lock {
1905 - if {[lindex $a 0] ne $m || [lindex $a 2] < $i} {
1906 - lappend new_dol $a
1907 - }
1908 - }
1909 - set disable_on_lock $new_dol
1910 - break
1911 - }
1912 - }
1913 -
1914 - if {$all_heads ne {}} {
1915 - $m add separator
1916 - }
1917 - foreach b $all_heads {
1918 - $m add radiobutton \
1919 - -label $b \
1920 - -command [list switch_branch $b] \
1921 - -variable current_branch \
1922 - -value $b
1923 - lappend disable_on_lock \
1924 - [list $m entryconf [$m index last] -state]
1925 - }
1926 -}
1927 -
1928 -proc all_tracking_branches {} {
1929 - global tracking_branches
1930 -
1931 - set all_trackings {}
1932 - set cmd {}
1933 - foreach name [array names tracking_branches] {
1934 - if {[regsub {/\*$} $name {} name]} {
1935 - lappend cmd $name
1936 - } else {
1937 - regsub ^refs/(heads|remotes)/ $name {} name
1938 - lappend all_trackings $name
1939 - }
1940 - }
1941 -
1942 - if {$cmd ne {}} {
1943 - set fd [open "| git for-each-ref --format=%(refname) $cmd" r]
1944 - while {[gets $fd name] > 0} {
1945 - regsub ^refs/(heads|remotes)/ $name {} name
1946 - lappend all_trackings $name
1947 - }
1948 - close $fd
1949 - }
1950 -
1951 - return [lsort -unique $all_trackings]
1952 -}
1953 -
1954 -proc load_all_tags {} {
1955 - set all_tags [list]
1956 - set fd [open "| git for-each-ref --format=%(refname) refs/tags" r]
1957 - while {[gets $fd line] > 0} {
1958 - if {![regsub ^refs/tags/ $line {} name]} continue
1959 - lappend all_tags $name
1960 - }
1961 - close $fd
1962 -
1963 - return [lsort $all_tags]
1964 -}
1965 -
1966 -proc do_create_branch_action {w} {
1967 - global all_heads null_sha1 repo_config
1968 - global create_branch_checkout create_branch_revtype
1969 - global create_branch_head create_branch_trackinghead
1970 - global create_branch_name create_branch_revexp
1971 - global create_branch_tag
1972 -
1973 - set newbranch $create_branch_name
1974 - if {$newbranch eq {}
1975 - || $newbranch eq $repo_config(gui.newbranchtemplate)} {
1976 - tk_messageBox \
1977 - -icon error \
1978 - -type ok \
1979 - -title [wm title $w] \
1980 - -parent $w \
1981 - -message "Please supply a branch name."
1982 - focus $w.desc.name_t
1983 - return
1984 - }
1985 - if {![catch {git show-ref --verify -- "refs/heads/$newbranch"}]} {
1986 - tk_messageBox \
1987 - -icon error \
1988 - -type ok \
1989 - -title [wm title $w] \
1990 - -parent $w \
1991 - -message "Branch '$newbranch' already exists."
1992 - focus $w.desc.name_t
1993 - return
1994 - }
1995 - if {[catch {git check-ref-format "heads/$newbranch"}]} {
1996 - tk_messageBox \
1997 - -icon error \
1998 - -type ok \
1999 - -title [wm title $w] \
2000 - -parent $w \
2001 - -message "We do not like '$newbranch' as a branch name."
2002 - focus $w.desc.name_t
2003 - return
2004 - }
2005 -
2006 - set rev {}
2007 - switch -- $create_branch_revtype {
2008 - head {set rev $create_branch_head}
2009 - tracking {set rev $create_branch_trackinghead}
2010 - tag {set rev $create_branch_tag}
2011 - expression {set rev $create_branch_revexp}
2012 - }
2013 - if {[catch {set cmt [git rev-parse --verify "${rev}^0"]}]} {
2014 - tk_messageBox \
2015 - -icon error \
2016 - -type ok \
2017 - -title [wm title $w] \
2018 - -parent $w \
2019 - -message "Invalid starting revision: $rev"
2020 - return
2021 - }
2022 - if {[catch {
2023 - git update-ref \
2024 - -m "branch: Created from $rev" \
2025 - "refs/heads/$newbranch" \
2026 - $cmt \
2027 - $null_sha1
2028 - } err]} {
2029 - tk_messageBox \
2030 - -icon error \
2031 - -type ok \
2032 - -title [wm title $w] \
2033 - -parent $w \
2034 - -message "Failed to create '$newbranch'.\n\n$err"
2035 - return
2036 - }
2037 -
2038 - lappend all_heads $newbranch
2039 - set all_heads [lsort $all_heads]
2040 - populate_branch_menu
2041 - destroy $w
2042 - if {$create_branch_checkout} {
2043 - switch_branch $newbranch
2044 - }
2045 -}
2046 -
2047 -proc radio_selector {varname value args} {
2048 - upvar #0 $varname var
2049 - set var $value
2050 -}
2051 -
2052 -trace add variable create_branch_head write \
2053 - [list radio_selector create_branch_revtype head]
2054 -trace add variable create_branch_trackinghead write \
2055 - [list radio_selector create_branch_revtype tracking]
2056 -trace add variable create_branch_tag write \
2057 - [list radio_selector create_branch_revtype tag]
2058 -
2059 -trace add variable delete_branch_head write \
2060 - [list radio_selector delete_branch_checktype head]
2061 -trace add variable delete_branch_trackinghead write \
2062 - [list radio_selector delete_branch_checktype tracking]
2063 -
2064 -proc do_create_branch {} {
2065 - global all_heads current_branch repo_config
2066 - global create_branch_checkout create_branch_revtype
2067 - global create_branch_head create_branch_trackinghead
2068 - global create_branch_name create_branch_revexp
2069 - global create_branch_tag
2070 -
2071 - set w .branch_editor
2072 - toplevel $w
2073 - wm geometry $w "+[winfo rootx .]+[winfo rooty .]"
2074 -
2075 - label $w.header -text {Create New Branch} \
2076 - -font font_uibold
2077 - pack $w.header -side top -fill x
2078 -
2079 - frame $w.buttons
2080 - button $w.buttons.create -text Create \
2081 - -default active \
2082 - -command [list do_create_branch_action $w]
2083 - pack $w.buttons.create -side right
2084 - button $w.buttons.cancel -text {Cancel} \
2085 - -command [list destroy $w]
2086 - pack $w.buttons.cancel -side right -padx 5
2087 - pack $w.buttons -side bottom -fill x -pady 10 -padx 10
2088 -
2089 - labelframe $w.desc -text {Branch Description}
2090 - label $w.desc.name_l -text {Name:}
2091 - entry $w.desc.name_t \
2092 - -borderwidth 1 \
2093 - -relief sunken \
2094 - -width 40 \
2095 - -textvariable create_branch_name \
2096 - -validate key \
2097 - -validatecommand {
2098 - if {%d == 1 && [regexp {[~^:?*\[\0- ]} %S]} {return 0}
2099 - return 1
2100 - }
2101 - grid $w.desc.name_l $w.desc.name_t -sticky we -padx {0 5}
2102 - grid columnconfigure $w.desc 1 -weight 1
2103 - pack $w.desc -anchor nw -fill x -pady 5 -padx 5
2104 -
2105 - labelframe $w.from -text {Starting Revision}
2106 - radiobutton $w.from.head_r \
2107 - -text {Local Branch:} \
2108 - -value head \
2109 - -variable create_branch_revtype
2110 - eval tk_optionMenu $w.from.head_m create_branch_head $all_heads
2111 - grid $w.from.head_r $w.from.head_m -sticky w
2112 - set all_trackings [all_tracking_branches]
2113 - if {$all_trackings ne {}} {
2114 - set create_branch_trackinghead [lindex $all_trackings 0]
2115 - radiobutton $w.from.tracking_r \
2116 - -text {Tracking Branch:} \
2117 - -value tracking \
2118 - -variable create_branch_revtype
2119 - eval tk_optionMenu $w.from.tracking_m \
2120 - create_branch_trackinghead \
2121 - $all_trackings
2122 - grid $w.from.tracking_r $w.from.tracking_m -sticky w
2123 - }
2124 - set all_tags [load_all_tags]
2125 - if {$all_tags ne {}} {
2126 - set create_branch_tag [lindex $all_tags 0]
2127 - radiobutton $w.from.tag_r \
2128 - -text {Tag:} \
2129 - -value tag \
2130 - -variable create_branch_revtype
2131 - eval tk_optionMenu $w.from.tag_m create_branch_tag $all_tags
2132 - grid $w.from.tag_r $w.from.tag_m -sticky w
2133 - }
2134 - radiobutton $w.from.exp_r \
2135 - -text {Revision Expression:} \
2136 - -value expression \
2137 - -variable create_branch_revtype
2138 - entry $w.from.exp_t \
2139 - -borderwidth 1 \
2140 - -relief sunken \
2141 - -width 50 \
2142 - -textvariable create_branch_revexp \
2143 - -validate key \
2144 - -validatecommand {
2145 - if {%d == 1 && [regexp {\s} %S]} {return 0}
2146 - if {%d == 1 && [string length %S] > 0} {
2147 - set create_branch_revtype expression
2148 - }
2149 - return 1
2150 - }
2151 - grid $w.from.exp_r $w.from.exp_t -sticky we -padx {0 5}
2152 - grid columnconfigure $w.from 1 -weight 1
2153 - pack $w.from -anchor nw -fill x -pady 5 -padx 5
2154 -
2155 - labelframe $w.postActions -text {Post Creation Actions}
2156 - checkbutton $w.postActions.checkout \
2157 - -text {Checkout after creation} \
2158 - -variable create_branch_checkout
2159 - pack $w.postActions.checkout -anchor nw
2160 - pack $w.postActions -anchor nw -fill x -pady 5 -padx 5
2161 -
2162 - set create_branch_checkout 1
2163 - set create_branch_head $current_branch
2164 - set create_branch_revtype head
2165 - set create_branch_name $repo_config(gui.newbranchtemplate)
2166 - set create_branch_revexp {}
2167 -
2168 - bind $w <Visibility> "
2169 - grab $w
2170 - $w.desc.name_t icursor end
2171 - focus $w.desc.name_t
2172 - "
2173 - bind $w <Key-Escape> "destroy $w"
2174 - bind $w <Key-Return> "do_create_branch_action $w;break"
2175 - wm title $w "[appname] ([reponame]): Create Branch"
2176 - tkwait window $w
2177 -}
2178 -
2179 -proc do_delete_branch_action {w} {
2180 - global all_heads
2181 - global delete_branch_checktype delete_branch_head delete_branch_trackinghead
2182 -
2183 - set check_rev {}
2184 - switch -- $delete_branch_checktype {
2185 - head {set check_rev $delete_branch_head}
2186 - tracking {set check_rev $delete_branch_trackinghead}
2187 - always {set check_rev {:none}}
2188 - }
2189 - if {$check_rev eq {:none}} {
2190 - set check_cmt {}
2191 - } elseif {[catch {set check_cmt [git rev-parse --verify "${check_rev}^0"]}]} {
2192 - tk_messageBox \
2193 - -icon error \
2194 - -type ok \
2195 - -title [wm title $w] \
2196 - -parent $w \
2197 - -message "Invalid check revision: $check_rev"
2198 - return
2199 - }
2200 -
2201 - set to_delete [list]
2202 - set not_merged [list]
2203 - foreach i [$w.list.l curselection] {
2204 - set b [$w.list.l get $i]
2205 - if {[catch {set o [git rev-parse --verify $b]}]} continue
2206 - if {$check_cmt ne {}} {
2207 - if {$b eq $check_rev} continue
2208 - if {[catch {set m [git merge-base $o $check_cmt]}]} continue
2209 - if {$o ne $m} {
2210 - lappend not_merged $b
2211 - continue
2212 - }
2213 - }
2214 - lappend to_delete [list $b $o]
2215 - }
2216 - if {$not_merged ne {}} {
2217 - set msg "The following branches are not completely merged into $check_rev:
2218 -
2219 - - [join $not_merged "\n - "]"
2220 - tk_messageBox \
2221 - -icon info \
2222 - -type ok \
2223 - -title [wm title $w] \
2224 - -parent $w \
2225 - -message $msg
2226 - }
2227 - if {$to_delete eq {}} return
2228 - if {$delete_branch_checktype eq {always}} {
2229 - set msg {Recovering deleted branches is difficult.
2230 -
2231 -Delete the selected branches?}
2232 - if {[tk_messageBox \
2233 - -icon warning \
2234 - -type yesno \
2235 - -title [wm title $w] \
2236 - -parent $w \
2237 - -message $msg] ne yes} {
2238 - return
2239 - }
2240 - }
2241 -
2242 - set failed {}
2243 - foreach i $to_delete {
2244 - set b [lindex $i 0]
2245 - set o [lindex $i 1]
2246 - if {[catch {git update-ref -d "refs/heads/$b" $o} err]} {
2247 - append failed " - $b: $err\n"
2248 - } else {
2249 - set x [lsearch -sorted -exact $all_heads $b]
2250 - if {$x >= 0} {
2251 - set all_heads [lreplace $all_heads $x $x]
2252 - }
2253 - }
2254 - }
2255 -
2256 - if {$failed ne {}} {
2257 - tk_messageBox \
2258 - -icon error \
2259 - -type ok \
2260 - -title [wm title $w] \
2261 - -parent $w \
2262 - -message "Failed to delete branches:\n$failed"
2263 - }
2264 -
2265 - set all_heads [lsort $all_heads]
2266 - populate_branch_menu
2267 - destroy $w
2268 -}
2269 -
2270 -proc do_delete_branch {} {
2271 - global all_heads tracking_branches current_branch
2272 - global delete_branch_checktype delete_branch_head delete_branch_trackinghead
2273 -
2274 - set w .branch_editor
2275 - toplevel $w
2276 - wm geometry $w "+[winfo rootx .]+[winfo rooty .]"
2277 -
2278 - label $w.header -text {Delete Local Branch} \
2279 - -font font_uibold
2280 - pack $w.header -side top -fill x
2281 -
2282 - frame $w.buttons
2283 - button $w.buttons.create -text Delete \
2284 - -command [list do_delete_branch_action $w]
2285 - pack $w.buttons.create -side right
2286 - button $w.buttons.cancel -text {Cancel} \
2287 - -command [list destroy $w]
2288 - pack $w.buttons.cancel -side right -padx 5
2289 - pack $w.buttons -side bottom -fill x -pady 10 -padx 10
2290 -
2291 - labelframe $w.list -text {Local Branches}
2292 - listbox $w.list.l \
2293 - -height 10 \
2294 - -width 70 \
2295 - -selectmode extended \
2296 - -yscrollcommand [list $w.list.sby set]
2297 - foreach h $all_heads {
2298 - if {$h ne $current_branch} {
2299 - $w.list.l insert end $h
2300 - }
2301 - }
2302 - scrollbar $w.list.sby -command [list $w.list.l yview]
2303 - pack $w.list.sby -side right -fill y
2304 - pack $w.list.l -side left -fill both -expand 1
2305 - pack $w.list -fill both -expand 1 -pady 5 -padx 5
2306 -
2307 - labelframe $w.validate -text {Delete Only If}
2308 - radiobutton $w.validate.head_r \
2309 - -text {Merged Into Local Branch:} \
2310 - -value head \
2311 - -variable delete_branch_checktype
2312 - eval tk_optionMenu $w.validate.head_m delete_branch_head $all_heads
2313 - grid $w.validate.head_r $w.validate.head_m -sticky w
2314 - set all_trackings [all_tracking_branches]
2315 - if {$all_trackings ne {}} {
2316 - set delete_branch_trackinghead [lindex $all_trackings 0]
2317 - radiobutton $w.validate.tracking_r \
2318 - -text {Merged Into Tracking Branch:} \
2319 - -value tracking \
2320 - -variable delete_branch_checktype
2321 - eval tk_optionMenu $w.validate.tracking_m \
2322 - delete_branch_trackinghead \
2323 - $all_trackings
2324 - grid $w.validate.tracking_r $w.validate.tracking_m -sticky w
2325 - }
2326 - radiobutton $w.validate.always_r \
2327 - -text {Always (Do not perform merge checks)} \
2328 - -value always \
2329 - -variable delete_branch_checktype
2330 - grid $w.validate.always_r -columnspan 2 -sticky w
2331 - grid columnconfigure $w.validate 1 -weight 1
2332 - pack $w.validate -anchor nw -fill x -pady 5 -padx 5
2333 -
2334 - set delete_branch_head $current_branch
2335 - set delete_branch_checktype head
2336 -
2337 - bind $w <Visibility> "grab $w; focus $w"
2338 - bind $w <Key-Escape> "destroy $w"
2339 - wm title $w "[appname] ([reponame]): Delete Branch"
2340 - tkwait window $w
2341 -}
2342 -
2343 -proc switch_branch {new_branch} {
2344 - global HEAD commit_type current_branch repo_config
2345 -
2346 - if {![lock_index switch]} return
2347 -
2348 - # -- Our in memory state should match the repository.
2349 - #
2350 - repository_state curType curHEAD curMERGE_HEAD
2351 - if {[string match amend* $commit_type]
2352 - && $curType eq {normal}
2353 - && $curHEAD eq $HEAD} {
2354 - } elseif {$commit_type ne $curType || $HEAD ne $curHEAD} {
2355 - info_popup {Last scanned state does not match repository state.
2356 -
2357 -Another Git program has modified this repository since the last scan. A rescan must be performed before the current branch can be changed.
2358 -
2359 -The rescan will be automatically started now.
2360 -}
2361 - unlock_index
2362 - rescan {set ui_status_value {Ready.}}
2363 - return
2364 - }
2365 -
2366 - # -- Don't do a pointless switch.
2367 - #
2368 - if {$current_branch eq $new_branch} {
2369 - unlock_index
2370 - return
2371 - }
2372 -
2373 - if {$repo_config(gui.trustmtime) eq {true}} {
2374 - switch_branch_stage2 {} $new_branch
2375 - } else {
2376 - set ui_status_value {Refreshing file status...}
2377 - set cmd [list git update-index]
2378 - lappend cmd -q
2379 - lappend cmd --unmerged
2380 - lappend cmd --ignore-missing
2381 - lappend cmd --refresh
2382 - set fd_rf [open "| $cmd" r]
2383 - fconfigure $fd_rf -blocking 0 -translation binary
2384 - fileevent $fd_rf readable \
2385 - [list switch_branch_stage2 $fd_rf $new_branch]
2386 - }
2387 -}
2388 -
2389 -proc switch_branch_stage2 {fd_rf new_branch} {
2390 - global ui_status_value HEAD
2391 -
2392 - if {$fd_rf ne {}} {
2393 - read $fd_rf
2394 - if {![eof $fd_rf]} return
2395 - close $fd_rf
2396 - }
2397 -
2398 - set ui_status_value "Updating working directory to '$new_branch'..."
2399 - set cmd [list git read-tree]
2400 - lappend cmd -m
2401 - lappend cmd -u
2402 - lappend cmd --exclude-per-directory=.gitignore
2403 - lappend cmd $HEAD
2404 - lappend cmd $new_branch
2405 - set fd_rt [open "| $cmd" r]
2406 - fconfigure $fd_rt -blocking 0 -translation binary
2407 - fileevent $fd_rt readable \
2408 - [list switch_branch_readtree_wait $fd_rt $new_branch]
2409 -}
2410 -
2411 -proc switch_branch_readtree_wait {fd_rt new_branch} {
2412 - global selected_commit_type commit_type HEAD MERGE_HEAD PARENT
2413 - global current_branch
2414 - global ui_comm ui_status_value
2415 -
2416 - # -- We never get interesting output on stdout; only stderr.
2417 - #
2418 - read $fd_rt
2419 - fconfigure $fd_rt -blocking 1
2420 - if {![eof $fd_rt]} {
2421 - fconfigure $fd_rt -blocking 0
2422 - return
2423 - }
2424 -
2425 - # -- The working directory wasn't in sync with the index and
2426 - # we'd have to overwrite something to make the switch. A
2427 - # merge is required.
2428 - #
2429 - if {[catch {close $fd_rt} err]} {
2430 - regsub {^fatal: } $err {} err
2431 - warn_popup "File level merge required.
2432 -
2433 -$err
2434 -
2435 -Staying on branch '$current_branch'."
2436 - set ui_status_value "Aborted checkout of '$new_branch' (file level merging is required)."
2437 - unlock_index
2438 - return
2439 - }
2440 -
2441 - # -- Update the symbolic ref. Core git doesn't even check for failure
2442 - # here, it Just Works(tm). If it doesn't we are in some really ugly
2443 - # state that is difficult to recover from within git-gui.
2444 - #
2445 - if {[catch {git symbolic-ref HEAD "refs/heads/$new_branch"} err]} {
2446 - error_popup "Failed to set current branch.
2447 -
2448 -This working directory is only partially switched. We successfully updated your files, but failed to update an internal Git file.
2449 -
2450 -This should not have occurred. [appname] will now close and give up.
2451 -
2452 -$err"
2453 - do_quit
2454 - return
2455 - }
2456 -
2457 - # -- Update our repository state. If we were previously in amend mode
2458 - # we need to toss the current buffer and do a full rescan to update
2459 - # our file lists. If we weren't in amend mode our file lists are
2460 - # accurate and we can avoid the rescan.
2461 - #
2462 - unlock_index
2463 - set selected_commit_type new
2464 - if {[string match amend* $commit_type]} {
2465 - $ui_comm delete 0.0 end
2466 - $ui_comm edit reset
2467 - $ui_comm edit modified false
2468 - rescan {set ui_status_value "Checked out branch '$current_branch'."}
2469 - } else {
2470 - repository_state commit_type HEAD MERGE_HEAD
2471 - set PARENT $HEAD
2472 - set ui_status_value "Checked out branch '$current_branch'."
2473 - }
2474 -}
2475 -
2476 -######################################################################
2477 -##
2478 -## remote management
2479 -
2480 -proc load_all_remotes {} {
2481 - global repo_config
2482 - global all_remotes tracking_branches
2483 -
2484 - set all_remotes [list]
2485 - array unset tracking_branches
2486 -
2487 - set rm_dir [gitdir remotes]
2488 - if {[file isdirectory $rm_dir]} {
2489 - set all_remotes [glob \
2490 - -types f \
2491 - -tails \
2492 - -nocomplain \
2493 - -directory $rm_dir *]
2494 -
2495 - foreach name $all_remotes {
2496 - catch {
2497 - set fd [open [file join $rm_dir $name] r]
2498 - while {[gets $fd line] >= 0} {
2499 - if {![regexp {^Pull:[ ]*([^:]+):(.+)$} \
2500 - $line line src dst]} continue
2501 - if {![regexp ^refs/ $dst]} {
2502 - set dst "refs/heads/$dst"
2503 - }
2504 - set tracking_branches($dst) [list $name $src]
2505 - }
2506 - close $fd
2507 - }
2508 - }
2509 - }
2510 -
2511 - foreach line [array names repo_config remote.*.url] {
2512 - if {![regexp ^remote\.(.*)\.url\$ $line line name]} continue
2513 - lappend all_remotes $name
2514 -
2515 - if {[catch {set fl $repo_config(remote.$name.fetch)}]} {
2516 - set fl {}
2517 - }
2518 - foreach line $fl {
2519 - if {![regexp {^([^:]+):(.+)$} $line line src dst]} continue
2520 - if {![regexp ^refs/ $dst]} {
2521 - set dst "refs/heads/$dst"
2522 - }
2523 - set tracking_branches($dst) [list $name $src]
2524 - }
2525 - }
2526 -
2527 - set all_remotes [lsort -unique $all_remotes]
2528 -}
2529 -
2530 -proc populate_fetch_menu {} {
2531 - global all_remotes repo_config
2532 -
2533 - set m .mbar.fetch
2534 - foreach r $all_remotes {
2535 - set enable 0
2536 - if {![catch {set a $repo_config(remote.$r.url)}]} {
2537 - if {![catch {set a $repo_config(remote.$r.fetch)}]} {
2538 - set enable 1
2539 - }
2540 - } else {
2541 - catch {
2542 - set fd [open [gitdir remotes $r] r]
2543 - while {[gets $fd n] >= 0} {
2544 - if {[regexp {^Pull:[ \t]*([^:]+):} $n]} {
2545 - set enable 1
2546 - break
2547 - }
2548 - }
2549 - close $fd
2550 - }
2551 - }
2552 -
2553 - if {$enable} {
2554 - $m add command \
2555 - -label "Fetch from $r..." \
2556 - -command [list fetch_from $r]
2557 - }
2558 - }
2559 -}
2560 -
2561 -proc populate_push_menu {} {
2562 - global all_remotes repo_config
2563 -
2564 - set m .mbar.push
2565 - set fast_count 0
2566 - foreach r $all_remotes {
2567 - set enable 0
2568 - if {![catch {set a $repo_config(remote.$r.url)}]} {
2569 - if {![catch {set a $repo_config(remote.$r.push)}]} {
2570 - set enable 1
2571 - }
2572 - } else {
2573 - catch {
2574 - set fd [open [gitdir remotes $r] r]
2575 - while {[gets $fd n] >= 0} {
2576 - if {[regexp {^Push:[ \t]*([^:]+):} $n]} {
2577 - set enable 1
2578 - break
2579 - }
2580 - }
2581 - close $fd
2582 - }
2583 - }
2584 -
2585 - if {$enable} {
2586 - if {!$fast_count} {
2587 - $m add separator
2588 - }
2589 - $m add command \
2590 - -label "Push to $r..." \
2591 - -command [list push_to $r]
2592 - incr fast_count
2593 - }
2594 - }
2595 -}
2596 -
2597 -proc start_push_anywhere_action {w} {
2598 - global push_urltype push_remote push_url push_thin push_tags
2599 -
2600 - set r_url {}
2601 - switch -- $push_urltype {
2602 - remote {set r_url $push_remote}
2603 - url {set r_url $push_url}
2604 - }
2605 - if {$r_url eq {}} return
2606 -
2607 - set cmd [list git push]
2608 - lappend cmd -v
2609 - if {$push_thin} {
2610 - lappend cmd --thin
2611 - }
2612 - if {$push_tags} {
2613 - lappend cmd --tags
2614 - }
2615 - lappend cmd $r_url
2616 - set cnt 0
2617 - foreach i [$w.source.l curselection] {
2618 - set b [$w.source.l get $i]
2619 - lappend cmd "refs/heads/$b:refs/heads/$b"
2620 - incr cnt
2621 - }
2622 - if {$cnt == 0} {
2623 - return
2624 - } elseif {$cnt == 1} {
2625 - set unit branch
2626 - } else {
2627 - set unit branches
2628 - }
2629 -
2630 - set cons [new_console "push $r_url" "Pushing $cnt $unit to $r_url"]
2631 - console_exec $cons $cmd console_done
2632 - destroy $w
2633 -}
2634 -
2635 -trace add variable push_remote write \
2636 - [list radio_selector push_urltype remote]
2637 -
2638 -proc do_push_anywhere {} {
2639 - global all_heads all_remotes current_branch
2640 - global push_urltype push_remote push_url push_thin push_tags
2641 -
2642 - set w .push_setup
2643 - toplevel $w
2644 - wm geometry $w "+[winfo rootx .]+[winfo rooty .]"
2645 -
2646 - label $w.header -text {Push Branches} -font font_uibold
2647 - pack $w.header -side top -fill x
2648 -
2649 - frame $w.buttons
2650 - button $w.buttons.create -text Push \
2651 - -default active \
2652 - -command [list start_push_anywhere_action $w]
2653 - pack $w.buttons.create -side right
2654 - button $w.buttons.cancel -text {Cancel} \
2655 - -default normal \
2656 - -command [list destroy $w]
2657 - pack $w.buttons.cancel -side right -padx 5
2658 - pack $w.buttons -side bottom -fill x -pady 10 -padx 10
2659 -
2660 - labelframe $w.source -text {Source Branches}
2661 - listbox $w.source.l \
2662 - -height 10 \
2663 - -width 70 \
2664 - -selectmode extended \
2665 - -yscrollcommand [list $w.source.sby set]
2666 - foreach h $all_heads {
2667 - $w.source.l insert end $h
2668 - if {$h eq $current_branch} {
2669 - $w.source.l select set end
2670 - }
2671 - }
2672 - scrollbar $w.source.sby -command [list $w.source.l yview]
2673 - pack $w.source.sby -side right -fill y
2674 - pack $w.source.l -side left -fill both -expand 1
2675 - pack $w.source -fill both -expand 1 -pady 5 -padx 5
2676 -
2677 - labelframe $w.dest -text {Destination Repository}
2678 - if {$all_remotes ne {}} {
2679 - radiobutton $w.dest.remote_r \
2680 - -text {Remote:} \
2681 - -value remote \
2682 - -variable push_urltype
2683 - eval tk_optionMenu $w.dest.remote_m push_remote $all_remotes
2684 - grid $w.dest.remote_r $w.dest.remote_m -sticky w
2685 - if {[lsearch -sorted -exact $all_remotes origin] != -1} {
2686 - set push_remote origin
2687 - } else {
2688 - set push_remote [lindex $all_remotes 0]
2689 - }
2690 - set push_urltype remote
2691 - } else {
2692 - set push_urltype url
2693 - }
2694 - radiobutton $w.dest.url_r \
2695 - -text {Arbitrary URL:} \
2696 - -value url \
2697 - -variable push_urltype
2698 - entry $w.dest.url_t \
2699 - -borderwidth 1 \
2700 - -relief sunken \
2701 - -width 50 \
2702 - -textvariable push_url \
2703 - -validate key \
2704 - -validatecommand {
2705 - if {%d == 1 && [regexp {\s} %S]} {return 0}
2706 - if {%d == 1 && [string length %S] > 0} {
2707 - set push_urltype url
2708 - }
2709 - return 1
2710 - }
2711 - grid $w.dest.url_r $w.dest.url_t -sticky we -padx {0 5}
2712 - grid columnconfigure $w.dest 1 -weight 1
2713 - pack $w.dest -anchor nw -fill x -pady 5 -padx 5
2714 -
2715 - labelframe $w.options -text {Transfer Options}
2716 - checkbutton $w.options.thin \
2717 - -text {Use thin pack (for slow network connections)} \
2718 - -variable push_thin
2719 - grid $w.options.thin -columnspan 2 -sticky w
2720 - checkbutton $w.options.tags \
2721 - -text {Include tags} \
2722 - -variable push_tags
2723 - grid $w.options.tags -columnspan 2 -sticky w
2724 - grid columnconfigure $w.options 1 -weight 1
2725 - pack $w.options -anchor nw -fill x -pady 5 -padx 5
2726 -
2727 - set push_url {}
2728 - set push_thin 0
2729 - set push_tags 0
2730 -
2731 - bind $w <Visibility> "grab $w; focus $w.buttons.create"
2732 - bind $w <Key-Escape> "destroy $w"
2733 - bind $w <Key-Return> [list start_push_anywhere_action $w]
2734 - wm title $w "[appname] ([reponame]): Push"
2735 - tkwait window $w
2736 -}
2737 -
2738 -######################################################################
2739 -##
2740 -## merge
2741 -
2742 -proc can_merge {} {
2743 - global HEAD commit_type file_states
2744 -
2745 - if {[string match amend* $commit_type]} {
2746 - info_popup {Cannot merge while amending.
2747 -
2748 -You must finish amending this commit before starting any type of merge.
2749 -}
2750 - return 0
2751 - }
2752 -
2753 - if {[committer_ident] eq {}} {return 0}
2754 - if {![lock_index merge]} {return 0}
2755 -
2756 - # -- Our in memory state should match the repository.
2757 - #
2758 - repository_state curType curHEAD curMERGE_HEAD
2759 - if {$commit_type ne $curType || $HEAD ne $curHEAD} {
2760 - info_popup {Last scanned state does not match repository state.
2761 -
2762 -Another Git program has modified this repository since the last scan. A rescan must be performed before a merge can be performed.
2763 -
2764 -The rescan will be automatically started now.
2765 -}
2766 - unlock_index
2767 - rescan {set ui_status_value {Ready.}}
2768 - return 0
2769 - }
2770 -
2771 - foreach path [array names file_states] {
2772 - switch -glob -- [lindex $file_states($path) 0] {
2773 - _O {
2774 - continue; # and pray it works!
2775 - }
2776 - U? {
2777 - error_popup "You are in the middle of a conflicted merge.
2778 -
2779 -File [short_path $path] has merge conflicts.
2780 -
2781 -You must resolve them, add the file, and commit to complete the current merge. Only then can you begin another merge.
2782 -"
2783 - unlock_index
2784 - return 0
2785 - }
2786 - ?? {
2787 - error_popup "You are in the middle of a change.
2788 -
2789 -File [short_path $path] is modified.
2790 -
2791 -You should complete the current commit before starting a merge. Doing so will help you abort a failed merge, should the need arise.
2792 -"
2793 - unlock_index
2794 - return 0
2795 - }
2796 - }
2797 - }
2798 -
2799 - return 1
2800 -}
2801 -
2802 -proc visualize_local_merge {w} {
2803 - set revs {}
2804 - foreach i [$w.source.l curselection] {
2805 - lappend revs [$w.source.l get $i]
2806 - }
2807 - if {$revs eq {}} return
2808 - lappend revs --not HEAD
2809 - do_gitk $revs
2810 -}
2811 -
2812 -proc start_local_merge_action {w} {
2813 - global HEAD ui_status_value current_branch
2814 -
2815 - set cmd [list git merge]
2816 - set names {}
2817 - set revcnt 0
2818 - foreach i [$w.source.l curselection] {
2819 - set b [$w.source.l get $i]
2820 - lappend cmd $b
2821 - lappend names $b
2822 - incr revcnt
2823 - }
2824 -
2825 - if {$revcnt == 0} {
2826 - return
2827 - } elseif {$revcnt == 1} {
2828 - set unit branch
2829 - } elseif {$revcnt <= 15} {
2830 - set unit branches
2831 -
2832 - if {[tk_dialog \
2833 - $w.confirm_octopus \
2834 - [wm title $w] \
2835 - "Use octopus merge strategy?
2836 -
2837 -You are merging $revcnt branches at once. This requires using the octopus merge driver, which may not succeed if there are file-level conflicts.
2838 -" \
2839 - question \
2840 - 0 \
2841 - {Cancel} \
2842 - {Use octopus} \
2843 - ] != 1} return
2844 - } else {
2845 - tk_messageBox \
2846 - -icon error \
2847 - -type ok \
2848 - -title [wm title $w] \
2849 - -parent $w \
2850 - -message "Too many branches selected.
2851 -
2852 -You have requested to merge $revcnt branches in an octopus merge. This exceeds Git's internal limit of 15 branches per merge.
2853 -
2854 -Please select fewer branches. To merge more than 15 branches, merge the branches in batches.
2855 -"
2856 - return
2857 - }
2858 -
2859 - set msg "Merging $current_branch, [join $names {, }]"
2860 - set ui_status_value "$msg..."
2861 - set cons [new_console "Merge" $msg]
2862 - console_exec $cons $cmd [list finish_merge $revcnt]
2863 - bind $w <Destroy> {}
2864 - destroy $w
2865 -}
2866 -
2867 -proc finish_merge {revcnt w ok} {
2868 - console_done $w $ok
2869 - if {$ok} {
2870 - set msg {Merge completed successfully.}
2871 - } else {
2872 - if {$revcnt != 1} {
2873 - info_popup "Octopus merge failed.
2874 -
2875 -Your merge of $revcnt branches has failed.
2876 -
2877 -There are file-level conflicts between the branches which must be resolved manually.
2878 -
2879 -The working directory will now be reset.
2880 -
2881 -You can attempt this merge again by merging only one branch at a time." $w
2882 -
2883 - set fd [open "| git read-tree --reset -u HEAD" r]
2884 - fconfigure $fd -blocking 0 -translation binary
2885 - fileevent $fd readable [list reset_hard_wait $fd]
2886 - set ui_status_value {Aborting... please wait...}
2887 - return
2888 - }
2889 -
2890 - set msg {Merge failed. Conflict resolution is required.}
2891 - }
2892 - unlock_index
2893 - rescan [list set ui_status_value $msg]
2894 -}
2895 -
2896 -proc do_local_merge {} {
2897 - global current_branch
2898 -
2899 - if {![can_merge]} return
2900 -
2901 - set w .merge_setup
2902 - toplevel $w
2903 - wm geometry $w "+[winfo rootx .]+[winfo rooty .]"
2904 -
2905 - label $w.header \
2906 - -text "Merge Into $current_branch" \
2907 - -font font_uibold
2908 - pack $w.header -side top -fill x
2909 -
2910 - frame $w.buttons
2911 - button $w.buttons.visualize -text Visualize \
2912 - -command [list visualize_local_merge $w]
2913 - pack $w.buttons.visualize -side left
2914 - button $w.buttons.create -text Merge \
2915 - -command [list start_local_merge_action $w]
2916 - pack $w.buttons.create -side right
2917 - button $w.buttons.cancel -text {Cancel} \
2918 - -command [list destroy $w]
2919 - pack $w.buttons.cancel -side right -padx 5
2920 - pack $w.buttons -side bottom -fill x -pady 10 -padx 10
2921 -
2922 - labelframe $w.source -text {Source Branches}
2923 - listbox $w.source.l \
2924 - -height 10 \
2925 - -width 70 \
2926 - -selectmode extended \
2927 - -yscrollcommand [list $w.source.sby set]
2928 - scrollbar $w.source.sby -command [list $w.source.l yview]
2929 - pack $w.source.sby -side right -fill y
2930 - pack $w.source.l -side left -fill both -expand 1
2931 - pack $w.source -fill both -expand 1 -pady 5 -padx 5
2932 -
2933 - set cmd [list git for-each-ref]
2934 - lappend cmd {--format=%(objectname) %(*objectname) %(refname)}
2935 - lappend cmd refs/heads
2936 - lappend cmd refs/remotes
2937 - lappend cmd refs/tags
2938 - set fr_fd [open "| $cmd" r]
2939 - fconfigure $fr_fd -translation binary
2940 - while {[gets $fr_fd line] > 0} {
2941 - set line [split $line { }]
2942 - set sha1([lindex $line 0]) [lindex $line 2]
2943 - set sha1([lindex $line 1]) [lindex $line 2]
2944 - }
2945 - close $fr_fd
2946 -
2947 - set to_show {}
2948 - set fr_fd [open "| git rev-list --all --not HEAD"]
2949 - while {[gets $fr_fd line] > 0} {
2950 - if {[catch {set ref $sha1($line)}]} continue
2951 - regsub ^refs/(heads|remotes|tags)/ $ref {} ref
2952 - lappend to_show $ref
2953 - }
2954 - close $fr_fd
2955 -
2956 - foreach ref [lsort -unique $to_show] {
2957 - $w.source.l insert end $ref
2958 - }
2959 -
2960 - bind $w <Visibility> "grab $w"
2961 - bind $w <Key-Escape> "unlock_index;destroy $w"
2962 - bind $w <Destroy> unlock_index
2963 - wm title $w "[appname] ([reponame]): Merge"
2964 - tkwait window $w
2965 -}
2966 -
2967 -proc do_reset_hard {} {
2968 - global HEAD commit_type file_states
2969 -
2970 - if {[string match amend* $commit_type]} {
2971 - info_popup {Cannot abort while amending.
2972 -
2973 -You must finish amending this commit.
2974 -}
2975 - return
2976 - }
2977 -
2978 - if {![lock_index abort]} return
2979 -
2980 - if {[string match *merge* $commit_type]} {
2981 - set op merge
2982 - } else {
2983 - set op commit
2984 - }
2985 -
2986 - if {[ask_popup "Abort $op?
2987 -
2988 -Aborting the current $op will cause *ALL* uncommitted changes to be lost.
2989 -
2990 -Continue with aborting the current $op?"] eq {yes}} {
2991 - set fd [open "| git read-tree --reset -u HEAD" r]
2992 - fconfigure $fd -blocking 0 -translation binary
2993 - fileevent $fd readable [list reset_hard_wait $fd]
2994 - set ui_status_value {Aborting... please wait...}
2995 - } else {
2996 - unlock_index
2997 - }
2998 -}
2999 -
3000 -proc reset_hard_wait {fd} {
3001 - global ui_comm
3002 -
3003 - read $fd
3004 - if {[eof $fd]} {
3005 - close $fd
3006 - unlock_index
3007 -
3008 - $ui_comm delete 0.0 end
3009 - $ui_comm edit modified false
3010 -
3011 - catch {file delete [gitdir MERGE_HEAD]}
3012 - catch {file delete [gitdir rr-cache MERGE_RR]}
3013 - catch {file delete [gitdir SQUASH_MSG]}
3014 - catch {file delete [gitdir MERGE_MSG]}
3015 - catch {file delete [gitdir GITGUI_MSG]}
3016 -
3017 - rescan {set ui_status_value {Abort completed. Ready.}}
3018 - }
3019 -}
3020 -
3021 -######################################################################
3022 -##
3023 -## browser
3024 -
3025 -set next_browser_id 0
3026 -
3027 -proc new_browser {commit} {
3028 - global next_browser_id cursor_ptr M1B
3029 - global browser_commit browser_status browser_stack browser_path browser_busy
3030 -
3031 - if {[winfo ismapped .]} {
3032 - set w .browser[incr next_browser_id]
3033 - set tl $w
3034 - toplevel $w
3035 - } else {
3036 - set w {}
3037 - set tl .
3038 - }
3039 - set w_list $w.list.l
3040 - set browser_commit($w_list) $commit
3041 - set browser_status($w_list) {Starting...}
3042 - set browser_stack($w_list) {}
3043 - set browser_path($w_list) $browser_commit($w_list):
3044 - set browser_busy($w_list) 1
3045 -
3046 - label $w.path -textvariable browser_path($w_list) \
3047 - -anchor w \
3048 - -justify left \
3049 - -borderwidth 1 \
3050 - -relief sunken \
3051 - -font font_uibold
3052 - pack $w.path -anchor w -side top -fill x
3053 -
3054 - frame $w.list
3055 - text $w_list -background white -borderwidth 0 \
3056 - -cursor $cursor_ptr \
3057 - -state disabled \
3058 - -wrap none \
3059 - -height 20 \
3060 - -width 70 \
3061 - -xscrollcommand [list $w.list.sbx set] \
3062 - -yscrollcommand [list $w.list.sby set]
3063 - $w_list tag conf in_sel \
3064 - -background [$w_list cget -foreground] \
3065 - -foreground [$w_list cget -background]
3066 - scrollbar $w.list.sbx -orient h -command [list $w_list xview]
3067 - scrollbar $w.list.sby -orient v -command [list $w_list yview]
3068 - pack $w.list.sbx -side bottom -fill x
3069 - pack $w.list.sby -side right -fill y
3070 - pack $w_list -side left -fill both -expand 1
3071 - pack $w.list -side top -fill both -expand 1
3072 -
3073 - label $w.status -textvariable browser_status($w_list) \
3074 - -anchor w \
3075 - -justify left \
3076 - -borderwidth 1 \
3077 - -relief sunken
3078 - pack $w.status -anchor w -side bottom -fill x
3079 -
3080 - bind $w_list <Button-1> "browser_click 0 $w_list @%x,%y;break"
3081 - bind $w_list <Double-Button-1> "browser_click 1 $w_list @%x,%y;break"
3082 - bind $w_list <$M1B-Up> "browser_parent $w_list;break"
3083 - bind $w_list <$M1B-Left> "browser_parent $w_list;break"
3084 - bind $w_list <Up> "browser_move -1 $w_list;break"
3085 - bind $w_list <Down> "browser_move 1 $w_list;break"
3086 - bind $w_list <$M1B-Right> "browser_enter $w_list;break"
3087 - bind $w_list <Return> "browser_enter $w_list;break"
3088 - bind $w_list <Prior> "browser_page -1 $w_list;break"
3089 - bind $w_list <Next> "browser_page 1 $w_list;break"
3090 - bind $w_list <Left> break
3091 - bind $w_list <Right> break
3092 -
3093 - bind $tl <Visibility> "focus $w"
3094 - bind $tl <Destroy> "
3095 - array unset browser_buffer $w_list
3096 - array unset browser_files $w_list
3097 - array unset browser_status $w_list
3098 - array unset browser_stack $w_list
3099 - array unset browser_path $w_list
3100 - array unset browser_commit $w_list
3101 - array unset browser_busy $w_list
3102 - "
3103 - wm title $tl "[appname] ([reponame]): File Browser"
3104 - ls_tree $w_list $browser_commit($w_list) {}
3105 -}
3106 -
3107 -proc browser_move {dir w} {
3108 - global browser_files browser_busy
3109 -
3110 - if {$browser_busy($w)} return
3111 - set lno [lindex [split [$w index in_sel.first] .] 0]
3112 - incr lno $dir
3113 - if {[lindex $browser_files($w) [expr {$lno - 1}]] ne {}} {
3114 - $w tag remove in_sel 0.0 end
3115 - $w tag add in_sel $lno.0 [expr {$lno + 1}].0
3116 - $w see $lno.0
3117 - }
3118 -}
3119 -
3120 -proc browser_page {dir w} {
3121 - global browser_files browser_busy
3122 -
3123 - if {$browser_busy($w)} return
3124 - $w yview scroll $dir pages
3125 - set lno [expr {int(
3126 - [lindex [$w yview] 0]
3127 - * [llength $browser_files($w)]
3128 - + 1)}]
3129 - if {[lindex $browser_files($w) [expr {$lno - 1}]] ne {}} {
3130 - $w tag remove in_sel 0.0 end
3131 - $w tag add in_sel $lno.0 [expr {$lno + 1}].0
3132 - $w see $lno.0
3133 - }
3134 -}
3135 -
3136 -proc browser_parent {w} {
3137 - global browser_files browser_status browser_path
3138 - global browser_stack browser_busy
3139 -
3140 - if {$browser_busy($w)} return
3141 - set info [lindex $browser_files($w) 0]
3142 - if {[lindex $info 0] eq {parent}} {
3143 - set parent [lindex $browser_stack($w) end-1]
3144 - set browser_stack($w) [lrange $browser_stack($w) 0 end-2]
3145 - if {$browser_stack($w) eq {}} {
3146 - regsub {:.*$} $browser_path($w) {:} browser_path($w)
3147 - } else {
3148 - regsub {/[^/]+$} $browser_path($w) {} browser_path($w)
3149 - }
3150 - set browser_status($w) "Loading $browser_path($w)..."
3151 - ls_tree $w [lindex $parent 0] [lindex $parent 1]
3152 - }
3153 -}
3154 -
3155 -proc browser_enter {w} {
3156 - global browser_files browser_status browser_path
3157 - global browser_commit browser_stack browser_busy
3158 -
3159 - if {$browser_busy($w)} return
3160 - set lno [lindex [split [$w index in_sel.first] .] 0]
3161 - set info [lindex $browser_files($w) [expr {$lno - 1}]]
3162 - if {$info ne {}} {
3163 - switch -- [lindex $info 0] {
3164 - parent {
3165 - browser_parent $w
3166 - }
3167 - tree {
3168 - set name [lindex $info 2]
3169 - set escn [escape_path $name]
3170 - set browser_status($w) "Loading $escn..."
3171 - append browser_path($w) $escn
3172 - ls_tree $w [lindex $info 1] $name
3173 - }
3174 - blob {
3175 - set name [lindex $info 2]
3176 - set p {}
3177 - foreach n $browser_stack($w) {
3178 - append p [lindex $n 1]
3179 - }
3180 - append p $name
3181 - show_blame $browser_commit($w) $p
3182 - }
3183 - }
3184 - }
3185 -}
3186 -
3187 -proc browser_click {was_double_click w pos} {
3188 - global browser_files browser_busy
3189 -
3190 - if {$browser_busy($w)} return
3191 - set lno [lindex [split [$w index $pos] .] 0]
3192 - focus $w
3193 -
3194 - if {[lindex $browser_files($w) [expr {$lno - 1}]] ne {}} {
3195 - $w tag remove in_sel 0.0 end
3196 - $w tag add in_sel $lno.0 [expr {$lno + 1}].0
3197 - if {$was_double_click} {
3198 - browser_enter $w
3199 - }
3200 - }
3201 -}
3202 -
3203 -proc ls_tree {w tree_id name} {
3204 - global browser_buffer browser_files browser_stack browser_busy
3205 -
3206 - set browser_buffer($w) {}
3207 - set browser_files($w) {}
3208 - set browser_busy($w) 1
3209 -
3210 - $w conf -state normal
3211 - $w tag remove in_sel 0.0 end
3212 - $w delete 0.0 end
3213 - if {$browser_stack($w) ne {}} {
3214 - $w image create end \
3215 - -align center -padx 5 -pady 1 \
3216 - -name icon0 \
3217 - -image file_uplevel
3218 - $w insert end {[Up To Parent]}
3219 - lappend browser_files($w) parent
3220 - }
3221 - lappend browser_stack($w) [list $tree_id $name]
3222 - $w conf -state disabled
3223 -
3224 - set cmd [list git ls-tree -z $tree_id]
3225 - set fd [open "| $cmd" r]
3226 - fconfigure $fd -blocking 0 -translation binary -encoding binary
3227 - fileevent $fd readable [list read_ls_tree $fd $w]
3228 -}
3229 -
3230 -proc read_ls_tree {fd w} {
3231 - global browser_buffer browser_files browser_status browser_busy
3232 -
3233 - if {![winfo exists $w]} {
3234 - catch {close $fd}
3235 - return
3236 - }
3237 -
3238 - append browser_buffer($w) [read $fd]
3239 - set pck [split $browser_buffer($w) "\0"]
3240 - set browser_buffer($w) [lindex $pck end]
3241 -
3242 - set n [llength $browser_files($w)]
3243 - $w conf -state normal
3244 - foreach p [lrange $pck 0 end-1] {
3245 - set info [split $p "\t"]
3246 - set path [lindex $info 1]
3247 - set info [split [lindex $info 0] { }]
3248 - set type [lindex $info 1]
3249 - set object [lindex $info 2]
3250 -
3251 - switch -- $type {
3252 - blob {
3253 - set image file_mod
3254 - }
3255 - tree {
3256 - set image file_dir
3257 - append path /
3258 - }
3259 - default {
3260 - set image file_question
3261 - }
3262 - }
3263 -
3264 - if {$n > 0} {$w insert end "\n"}
3265 - $w image create end \
3266 - -align center -padx 5 -pady 1 \
3267 - -name icon[incr n] \
3268 - -image $image
3269 - $w insert end [escape_path $path]
3270 - lappend browser_files($w) [list $type $object $path]
3271 - }
3272 - $w conf -state disabled
3273 -
3274 - if {[eof $fd]} {
3275 - close $fd
3276 - set browser_status($w) Ready.
3277 - set browser_busy($w) 0
3278 - array unset browser_buffer $w
3279 - if {$n > 0} {
3280 - $w tag add in_sel 1.0 2.0
3281 - focus -force $w
3282 - }
3283 - }
3284 -}
3285 -
3286 -proc show_blame {commit path} {
3287 - global next_browser_id blame_status blame_data
3288 -
3289 - if {[winfo ismapped .]} {
3290 - set w .browser[incr next_browser_id]
3291 - set tl $w
3292 - toplevel $w
3293 - } else {
3294 - set w {}
3295 - set tl .
3296 - }
3297 - set blame_status($w) {Loading current file content...}
3298 -
3299 - label $w.path -text "$commit:$path" \
3300 - -anchor w \
3301 - -justify left \
3302 - -borderwidth 1 \
3303 - -relief sunken \
3304 - -font font_uibold
3305 - pack $w.path -side top -fill x
3306 -
3307 - frame $w.out
3308 - text $w.out.loaded_t \
3309 - -background white -borderwidth 0 \
3310 - -state disabled \
3311 - -wrap none \
3312 - -height 40 \
3313 - -width 1 \
3314 - -font font_diff
3315 - $w.out.loaded_t tag conf annotated -background grey
3316 -
3317 - text $w.out.linenumber_t \
3318 - -background white -borderwidth 0 \
3319 - -state disabled \
3320 - -wrap none \
3321 - -height 40 \
3322 - -width 5 \
3323 - -font font_diff
3324 - $w.out.linenumber_t tag conf linenumber -justify right
3325 -
3326 - text $w.out.file_t \
3327 - -background white -borderwidth 0 \
3328 - -state disabled \
3329 - -wrap none \
3330 - -height 40 \
3331 - -width 80 \
3332 - -xscrollcommand [list $w.out.sbx set] \
3333 - -font font_diff
3334 -
3335 - scrollbar $w.out.sbx -orient h -command [list $w.out.file_t xview]
3336 - scrollbar $w.out.sby -orient v \
3337 - -command [list scrollbar2many [list \
3338 - $w.out.loaded_t \
3339 - $w.out.linenumber_t \
3340 - $w.out.file_t \
3341 - ] yview]
3342 - grid \
3343 - $w.out.linenumber_t \
3344 - $w.out.loaded_t \
3345 - $w.out.file_t \
3346 - $w.out.sby \
3347 - -sticky nsew
3348 - grid conf $w.out.sbx -column 2 -sticky we
3349 - grid columnconfigure $w.out 2 -weight 1
3350 - grid rowconfigure $w.out 0 -weight 1
3351 - pack $w.out -fill both -expand 1
3352 -
3353 - label $w.status -textvariable blame_status($w) \
3354 - -anchor w \
3355 - -justify left \
3356 - -borderwidth 1 \
3357 - -relief sunken
3358 - pack $w.status -side bottom -fill x
3359 -
3360 - frame $w.cm
3361 - text $w.cm.t \
3362 - -background white -borderwidth 0 \
3363 - -state disabled \
3364 - -wrap none \
3365 - -height 10 \
3366 - -width 80 \
3367 - -xscrollcommand [list $w.cm.sbx set] \
3368 - -yscrollcommand [list $w.cm.sby set] \
3369 - -font font_diff
3370 - scrollbar $w.cm.sbx -orient h -command [list $w.cm.t xview]
3371 - scrollbar $w.cm.sby -orient v -command [list $w.cm.t yview]
3372 - pack $w.cm.sby -side right -fill y
3373 - pack $w.cm.sbx -side bottom -fill x
3374 - pack $w.cm.t -expand 1 -fill both
3375 - pack $w.cm -side bottom -fill x
3376 -
3377 - menu $w.ctxm -tearoff 0
3378 - $w.ctxm add command -label "Copy Commit" \
3379 - -command "blame_copycommit $w \$cursorW @\$cursorX,\$cursorY"
3380 -
3381 - foreach i [list \
3382 - $w.out.loaded_t \
3383 - $w.out.linenumber_t \
3384 - $w.out.file_t] {
3385 - $i tag conf in_sel \
3386 - -background [$i cget -foreground] \
3387 - -foreground [$i cget -background]
3388 - $i conf -yscrollcommand \
3389 - [list many2scrollbar [list \
3390 - $w.out.loaded_t \
3391 - $w.out.linenumber_t \
3392 - $w.out.file_t \
3393 - ] yview $w.out.sby]
3394 - bind $i <Button-1> "
3395 - blame_click {$w} \\
3396 - $w.cm.t \\
3397 - $w.out.linenumber_t \\
3398 - $w.out.file_t \\
3399 - $i @%x,%y
3400 - focus $i
3401 - "
3402 - bind_button3 $i "
3403 - set cursorX %x
3404 - set cursorY %y
3405 - set cursorW %W
3406 - tk_popup $w.ctxm %X %Y
3407 - "
3408 - }
3409 -
3410 - bind $w.cm.t <Button-1> "focus $w.cm.t"
3411 - bind $tl <Visibility> "focus $tl"
3412 - bind $tl <Destroy> "
3413 - array unset blame_status {$w}
3414 - array unset blame_data $w,*
3415 - "
3416 - wm title $tl "[appname] ([reponame]): File Viewer"
3417 -
3418 - set blame_data($w,commit_count) 0
3419 - set blame_data($w,commit_list) {}
3420 - set blame_data($w,total_lines) 0
3421 - set blame_data($w,blame_lines) 0
3422 - set blame_data($w,highlight_commit) {}
3423 - set blame_data($w,highlight_line) -1
3424 -
3425 - set cmd [list git cat-file blob "$commit:$path"]
3426 - set fd [open "| $cmd" r]
3427 - fconfigure $fd -blocking 0 -translation lf -encoding binary
3428 - fileevent $fd readable [list read_blame_catfile \
3429 - $fd $w $commit $path \
3430 - $w.cm.t $w.out.loaded_t $w.out.linenumber_t $w.out.file_t]
3431 -}
3432 -
3433 -proc read_blame_catfile {fd w commit path w_cmit w_load w_line w_file} {
3434 - global blame_status blame_data
3435 -
3436 - if {![winfo exists $w_file]} {
3437 - catch {close $fd}
3438 - return
3439 - }
3440 -
3441 - set n $blame_data($w,total_lines)
3442 - $w_load conf -state normal
3443 - $w_line conf -state normal
3444 - $w_file conf -state normal
3445 - while {[gets $fd line] >= 0} {
3446 - regsub "\r\$" $line {} line
3447 - incr n
3448 - $w_load insert end "\n"
3449 - $w_line insert end "$n\n" linenumber
3450 - $w_file insert end "$line\n"
3451 - }
3452 - $w_load conf -state disabled
3453 - $w_line conf -state disabled
3454 - $w_file conf -state disabled
3455 - set blame_data($w,total_lines) $n
3456 -
3457 - if {[eof $fd]} {
3458 - close $fd
3459 - blame_incremental_status $w
3460 - set cmd [list git blame -M -C --incremental]
3461 - lappend cmd $commit -- $path
3462 - set fd [open "| $cmd" r]
3463 - fconfigure $fd -blocking 0 -translation lf -encoding binary
3464 - fileevent $fd readable [list read_blame_incremental $fd $w \
3465 - $w_load $w_cmit $w_line $w_file]
3466 - }
3467 -}
3468 -
3469 -proc read_blame_incremental {fd w w_load w_cmit w_line w_file} {
3470 - global blame_status blame_data
3471 -
3472 - if {![winfo exists $w_file]} {
3473 - catch {close $fd}
3474 - return
3475 - }
3476 -
3477 - while {[gets $fd line] >= 0} {
3478 - if {[regexp {^([a-z0-9]{40}) (\d+) (\d+) (\d+)$} $line line \
3479 - cmit original_line final_line line_count]} {
3480 - set blame_data($w,commit) $cmit
3481 - set blame_data($w,original_line) $original_line
3482 - set blame_data($w,final_line) $final_line
3483 - set blame_data($w,line_count) $line_count
3484 -
3485 - if {[catch {set g $blame_data($w,$cmit,order)}]} {
3486 - $w_line tag conf g$cmit
3487 - $w_file tag conf g$cmit
3488 - $w_line tag raise in_sel
3489 - $w_file tag raise in_sel
3490 - $w_file tag raise sel
3491 - set blame_data($w,$cmit,order) $blame_data($w,commit_count)
3492 - incr blame_data($w,commit_count)
3493 - lappend blame_data($w,commit_list) $cmit
3494 - }
3495 - } elseif {[string match {filename *} $line]} {
3496 - set file [string range $line 9 end]
3497 - set n $blame_data($w,line_count)
3498 - set lno $blame_data($w,final_line)
3499 - set cmit $blame_data($w,commit)
3500 -
3501 - while {$n > 0} {
3502 - if {[catch {set g g$blame_data($w,line$lno,commit)}]} {
3503 - $w_load tag add annotated $lno.0 "$lno.0 lineend + 1c"
3504 - } else {
3505 - $w_line tag remove g$g $lno.0 "$lno.0 lineend + 1c"
3506 - $w_file tag remove g$g $lno.0 "$lno.0 lineend + 1c"
3507 - }
3508 -
3509 - set blame_data($w,line$lno,commit) $cmit
3510 - set blame_data($w,line$lno,file) $file
3511 - $w_line tag add g$cmit $lno.0 "$lno.0 lineend + 1c"
3512 - $w_file tag add g$cmit $lno.0 "$lno.0 lineend + 1c"
3513 -
3514 - if {$blame_data($w,highlight_line) == -1} {
3515 - if {[lindex [$w_file yview] 0] == 0} {
3516 - $w_file see $lno.0
3517 - blame_showcommit $w $w_cmit $w_line $w_file $lno
3518 - }
3519 - } elseif {$blame_data($w,highlight_line) == $lno} {
3520 - blame_showcommit $w $w_cmit $w_line $w_file $lno
3521 - }
3522 -
3523 - incr n -1
3524 - incr lno
3525 - incr blame_data($w,blame_lines)
3526 - }
3527 -
3528 - set hc $blame_data($w,highlight_commit)
3529 - if {$hc ne {}
3530 - && [expr {$blame_data($w,$hc,order) + 1}]
3531 - == $blame_data($w,$cmit,order)} {
3532 - blame_showcommit $w $w_cmit $w_line $w_file \
3533 - $blame_data($w,highlight_line)
3534 - }
3535 - } elseif {[regexp {^([a-z-]+) (.*)$} $line line header data]} {
3536 - set blame_data($w,$blame_data($w,commit),$header) $data
3537 - }
3538 - }
3539 -
3540 - if {[eof $fd]} {
3541 - close $fd
3542 - set blame_status($w) {Annotation complete.}
3543 - } else {
3544 - blame_incremental_status $w
3545 - }
3546 -}
3547 -
3548 -proc blame_incremental_status {w} {
3549 - global blame_status blame_data
3550 -
3551 - set have $blame_data($w,blame_lines)
3552 - set total $blame_data($w,total_lines)
3553 - set pdone 0
3554 - if {$total} {set pdone [expr {100 * $have / $total}]}
3555 -
3556 - set blame_status($w) [format \
3557 - "Loading annotations... %i of %i lines annotated (%2i%%)" \
3558 - $have $total $pdone]
3559 -}
3560 -
3561 -proc blame_click {w w_cmit w_line w_file cur_w pos} {
3562 - set lno [lindex [split [$cur_w index $pos] .] 0]
3563 - if {$lno eq {}} return
3564 -
3565 - $w_line tag remove in_sel 0.0 end
3566 - $w_file tag remove in_sel 0.0 end
3567 - $w_line tag add in_sel $lno.0 "$lno.0 + 1 line"
3568 - $w_file tag add in_sel $lno.0 "$lno.0 + 1 line"
3569 -
3570 - blame_showcommit $w $w_cmit $w_line $w_file $lno
3571 -}
3572 -
3573 -set blame_colors {
3574 - #ff4040
3575 - #ff40ff
3576 - #4040ff
3577 -}
3578 -
3579 -proc blame_showcommit {w w_cmit w_line w_file lno} {
3580 - global blame_colors blame_data repo_config
3581 -
3582 - set cmit $blame_data($w,highlight_commit)
3583 - if {$cmit ne {}} {
3584 - set idx $blame_data($w,$cmit,order)
3585 - set i 0
3586 - foreach c $blame_colors {
3587 - set h [lindex $blame_data($w,commit_list) [expr {$idx - 1 + $i}]]
3588 - $w_line tag conf g$h -background white
3589 - $w_file tag conf g$h -background white
3590 - incr i
3591 - }
3592 - }
3593 -
3594 - $w_cmit conf -state normal
3595 - $w_cmit delete 0.0 end
3596 - if {[catch {set cmit $blame_data($w,line$lno,commit)}]} {
3597 - set cmit {}
3598 - $w_cmit insert end "Loading annotation..."
3599 - } else {
3600 - set idx $blame_data($w,$cmit,order)
3601 - set i 0
3602 - foreach c $blame_colors {
3603 - set h [lindex $blame_data($w,commit_list) [expr {$idx - 1 + $i}]]
3604 - $w_line tag conf g$h -background $c
3605 - $w_file tag conf g$h -background $c
3606 - incr i
3607 - }
3608 -
3609 - set author_name {}
3610 - set author_email {}
3611 - set author_time {}
3612 - catch {set author_name $blame_data($w,$cmit,author)}
3613 - catch {set author_email $blame_data($w,$cmit,author-mail)}
3614 - catch {set author_time [clock format $blame_data($w,$cmit,author-time)]}
3615 -
3616 - set committer_name {}
3617 - set committer_email {}
3618 - set committer_time {}
3619 - catch {set committer_name $blame_data($w,$cmit,committer)}
3620 - catch {set committer_email $blame_data($w,$cmit,committer-mail)}
3621 - catch {set committer_time [clock format $blame_data($w,$cmit,committer-time)]}
3622 -
3623 - if {[catch {set msg $blame_data($w,$cmit,message)}]} {
3624 - set msg {}
3625 - catch {
3626 - set fd [open "| git cat-file commit $cmit" r]
3627 - fconfigure $fd -encoding binary -translation lf
3628 - if {[catch {set enc $repo_config(i18n.commitencoding)}]} {
3629 - set enc utf-8
3630 - }
3631 - while {[gets $fd line] > 0} {
3632 - if {[string match {encoding *} $line]} {
3633 - set enc [string tolower [string range $line 9 end]]
3634 - }
3635 - }
3636 - set msg [encoding convertfrom $enc [read $fd]]
3637 - set msg [string trim $msg]
3638 - close $fd
3639 -
3640 - set author_name [encoding convertfrom $enc $author_name]
3641 - set committer_name [encoding convertfrom $enc $committer_name]
3642 -
3643 - set blame_data($w,$cmit,author) $author_name
3644 - set blame_data($w,$cmit,committer) $committer_name
3645 - }
3646 - set blame_data($w,$cmit,message) $msg
3647 - }
3648 -
3649 - $w_cmit insert end "commit $cmit\n"
3650 - $w_cmit insert end "Author: $author_name $author_email $author_time\n"
3651 - $w_cmit insert end "Committer: $committer_name $committer_email $committer_time\n"
3652 - $w_cmit insert end "Original File: [escape_path $blame_data($w,line$lno,file)]\n"
3653 - $w_cmit insert end "\n"
3654 - $w_cmit insert end $msg
3655 - }
3656 - $w_cmit conf -state disabled
3657 -
3658 - set blame_data($w,highlight_line) $lno
3659 - set blame_data($w,highlight_commit) $cmit
3660 -}
3661 -
3662 -proc blame_copycommit {w i pos} {
3663 - global blame_data
3664 - set lno [lindex [split [$i index $pos] .] 0]
3665 - if {![catch {set commit $blame_data($w,line$lno,commit)}]} {
3666 - clipboard clear
3667 - clipboard append \
3668 - -format STRING \
3669 - -type STRING \
3670 - -- $commit
3671 - }
798 + $ui_index conf -state disabled
799 + $ui_workdir conf -state disabled
800 }
801
802 ######################################################################
@@ -3845,230 +973,6 @@ proc incr_font_size {font {amt 1}} {
973 font configure ${font}bold -size $sz
974 }
975
3848 -proc hook_failed_popup {hook msg} {
3849 - set w .hookfail
3850 - toplevel $w
3851 -
3852 - frame $w.m
3853 - label $w.m.l1 -text "$hook hook failed:" \
3854 - -anchor w \
3855 - -justify left \
3856 - -font font_uibold
3857 - text $w.m.t \
3858 - -background white -borderwidth 1 \
3859 - -relief sunken \
3860 - -width 80 -height 10 \
3861 - -font font_diff \
3862 - -yscrollcommand [list $w.m.sby set]
3863 - label $w.m.l2 \
3864 - -text {You must correct the above errors before committing.} \
3865 - -anchor w \
3866 - -justify left \
3867 - -font font_uibold
3868 - scrollbar $w.m.sby -command [list $w.m.t yview]
3869 - pack $w.m.l1 -side top -fill x
3870 - pack $w.m.l2 -side bottom -fill x
3871 - pack $w.m.sby -side right -fill y
3872 - pack $w.m.t -side left -fill both -expand 1
3873 - pack $w.m -side top -fill both -expand 1 -padx 5 -pady 10
3874 -
3875 - $w.m.t insert 1.0 $msg
3876 - $w.m.t conf -state disabled
3877 -
3878 - button $w.ok -text OK \
3879 - -width 15 \
3880 - -command "destroy $w"
3881 - pack $w.ok -side bottom -anchor e -pady 10 -padx 10
3882 -
3883 - bind $w <Visibility> "grab $w; focus $w"
3884 - bind $w <Key-Return> "destroy $w"
3885 - wm title $w "[appname] ([reponame]): error"
3886 - tkwait window $w
3887 -}
3888 -
3889 -set next_console_id 0
3890 -
3891 -proc new_console {short_title long_title} {
3892 - global next_console_id console_data
3893 - set w .console[incr next_console_id]
3894 - set console_data($w) [list $short_title $long_title]
3895 - return [console_init $w]
3896 -}
3897 -
3898 -proc console_init {w} {
3899 - global console_cr console_data M1B
3900 -
3901 - set console_cr($w) 1.0
3902 - toplevel $w
3903 - frame $w.m
3904 - label $w.m.l1 -text "[lindex $console_data($w) 1]:" \
3905 - -anchor w \
3906 - -justify left \
3907 - -font font_uibold
3908 - text $w.m.t \
3909 - -background white -borderwidth 1 \
3910 - -relief sunken \
3911 - -width 80 -height 10 \
3912 - -font font_diff \
3913 - -state disabled \
3914 - -yscrollcommand [list $w.m.sby set]
3915 - label $w.m.s -text {Working... please wait...} \
3916 - -anchor w \
3917 - -justify left \
3918 - -font font_uibold
3919 - scrollbar $w.m.sby -command [list $w.m.t yview]
3920 - pack $w.m.l1 -side top -fill x
3921 - pack $w.m.s -side bottom -fill x
3922 - pack $w.m.sby -side right -fill y
3923 - pack $w.m.t -side left -fill both -expand 1
3924 - pack $w.m -side top -fill both -expand 1 -padx 5 -pady 10
3925 -
3926 - menu $w.ctxm -tearoff 0
3927 - $w.ctxm add command -label "Copy" \
3928 - -command "tk_textCopy $w.m.t"
3929 - $w.ctxm add command -label "Select All" \
3930 - -command "focus $w.m.t;$w.m.t tag add sel 0.0 end"
3931 - $w.ctxm add command -label "Copy All" \
3932 - -command "
3933 - $w.m.t tag add sel 0.0 end
3934 - tk_textCopy $w.m.t
3935 - $w.m.t tag remove sel 0.0 end
3936 - "
3937 -
3938 - button $w.ok -text {Close} \
3939 - -state disabled \
3940 - -command "destroy $w"
3941 - pack $w.ok -side bottom -anchor e -pady 10 -padx 10
3942 -
3943 - bind_button3 $w.m.t "tk_popup $w.ctxm %X %Y"
3944 - bind $w.m.t <$M1B-Key-a> "$w.m.t tag add sel 0.0 end;break"
3945 - bind $w.m.t <$M1B-Key-A> "$w.m.t tag add sel 0.0 end;break"
3946 - bind $w <Visibility> "focus $w"
3947 - wm title $w "[appname] ([reponame]): [lindex $console_data($w) 0]"
3948 - return $w
3949 -}
3950 -
3951 -proc console_exec {w cmd after} {
3952 - # -- Cygwin's Tcl tosses the enviroment when we exec our child.
3953 - # But most users need that so we have to relogin. :-(
3954 - #
3955 - if {[is_Cygwin]} {
3956 - set cmd [list sh --login -c "cd \"[pwd]\" && [join $cmd { }]"]
3957 - }
3958 -
3959 - # -- Tcl won't let us redirect both stdout and stderr to
3960 - # the same pipe. So pass it through cat...
3961 - #
3962 - set cmd [concat | $cmd |& cat]
3963 -
3964 - set fd_f [open $cmd r]
3965 - fconfigure $fd_f -blocking 0 -translation binary
3966 - fileevent $fd_f readable [list console_read $w $fd_f $after]
3967 -}
3968 -
3969 -proc console_read {w fd after} {
3970 - global console_cr
3971 -
3972 - set buf [read $fd]
3973 - if {$buf ne {}} {
3974 - if {![winfo exists $w]} {console_init $w}
3975 - $w.m.t conf -state normal
3976 - set c 0
3977 - set n [string length $buf]
3978 - while {$c < $n} {
3979 - set cr [string first "\r" $buf $c]
3980 - set lf [string first "\n" $buf $c]
3981 - if {$cr < 0} {set cr [expr {$n + 1}]}
3982 - if {$lf < 0} {set lf [expr {$n + 1}]}
3983 -
3984 - if {$lf < $cr} {
3985 - $w.m.t insert end [string range $buf $c $lf]
3986 - set console_cr($w) [$w.m.t index {end -1c}]
3987 - set c $lf
3988 - incr c
3989 - } else {
3990 - $w.m.t delete $console_cr($w) end
3991 - $w.m.t insert end "\n"
3992 - $w.m.t insert end [string range $buf $c $cr]
3993 - set c $cr
3994 - incr c
3995 - }
3996 - }
3997 - $w.m.t conf -state disabled
3998 - $w.m.t see end
3999 - }
4000 -
4001 - fconfigure $fd -blocking 1
4002 - if {[eof $fd]} {
4003 - if {[catch {close $fd}]} {
4004 - set ok 0
4005 - } else {
4006 - set ok 1
4007 - }
4008 - uplevel #0 $after $w $ok
4009 - return
4010 - }
4011 - fconfigure $fd -blocking 0
4012 -}
4013 -
4014 -proc console_chain {cmdlist w {ok 1}} {
4015 - if {$ok} {
4016 - if {[llength $cmdlist] == 0} {
4017 - console_done $w $ok
4018 - return
4019 - }
4020 -
4021 - set cmd [lindex $cmdlist 0]
4022 - set cmdlist [lrange $cmdlist 1 end]
4023 -
4024 - if {[lindex $cmd 0] eq {console_exec}} {
4025 - console_exec $w \
4026 - [lindex $cmd 1] \
4027 - [list console_chain $cmdlist]
4028 - } else {
4029 - uplevel #0 $cmd $cmdlist $w $ok
4030 - }
4031 - } else {
4032 - console_done $w $ok
4033 - }
4034 -}
4035 -
4036 -proc console_done {args} {
4037 - global console_cr console_data
4038 -
4039 - switch -- [llength $args] {
4040 - 2 {
4041 - set w [lindex $args 0]
4042 - set ok [lindex $args 1]
4043 - }
4044 - 3 {
4045 - set w [lindex $args 1]
4046 - set ok [lindex $args 2]
4047 - }
4048 - default {
4049 - error "wrong number of args: console_done ?ignored? w ok"
4050 - }
4051 - }
4052 -
4053 - if {$ok} {
4054 - if {[winfo exists $w]} {
4055 - $w.m.s conf -background green -text {Success}
4056 - $w.ok conf -state normal
4057 - focus $w.ok
4058 - }
4059 - } else {
4060 - if {![winfo exists $w]} {
4061 - console_init $w
4062 - }
4063 - $w.m.s conf -background red -text {Error: Command Failed}
4064 - $w.ok conf -state normal
4065 - focus $w.ok
4066 - }
4067 -
4068 - array unset console_cr $w
4069 - array unset console_data $w
4070 -}
4071 -
976 ######################################################################
977 ##
978 ## ui commands
@@ -4100,93 +1004,6 @@ proc do_gitk {revs} {
1004 }
1005 }
1006
4103 -proc do_stats {} {
4104 - set fd [open "| git count-objects -v" r]
4105 - while {[gets $fd line] > 0} {
4106 - if {[regexp {^([^:]+): (\d+)$} $line _ name value]} {
4107 - set stats($name) $value
4108 - }
4109 - }
4110 - close $fd
4111 -
4112 - set packed_sz 0
4113 - foreach p [glob -directory [gitdir objects pack] \
4114 - -type f \
4115 - -nocomplain -- *] {
4116 - incr packed_sz [file size $p]
4117 - }
4118 - if {$packed_sz > 0} {
4119 - set stats(size-pack) [expr {$packed_sz / 1024}]
4120 - }
4121 -
4122 - set w .stats_view
4123 - toplevel $w
4124 - wm geometry $w "+[winfo rootx .]+[winfo rooty .]"
4125 -
4126 - label $w.header -text {Database Statistics}
4127 - pack $w.header -side top -fill x
4128 -
4129 - frame $w.buttons -border 1
4130 - button $w.buttons.close -text Close \
4131 - -default active \
4132 - -command [list destroy $w]
4133 - button $w.buttons.gc -text {Compress Database} \
4134 - -default normal \
4135 - -command "destroy $w;do_gc"
4136 - pack $w.buttons.close -side right
4137 - pack $w.buttons.gc -side left
4138 - pack $w.buttons -side bottom -fill x -pady 10 -padx 10
4139 -
4140 - frame $w.stat -borderwidth 1 -relief solid
4141 - foreach s {
4142 - {count {Number of loose objects}}
4143 - {size {Disk space used by loose objects} { KiB}}
4144 - {in-pack {Number of packed objects}}
4145 - {packs {Number of packs}}
4146 - {size-pack {Disk space used by packed objects} { KiB}}
4147 - {prune-packable {Packed objects waiting for pruning}}
4148 - {garbage {Garbage files}}
4149 - } {
4150 - set name [lindex $s 0]
4151 - set label [lindex $s 1]
4152 - if {[catch {set value $stats($name)}]} continue
4153 - if {[llength $s] > 2} {
4154 - set value "$value[lindex $s 2]"
4155 - }
4156 -
4157 - label $w.stat.l_$name -text "$label:" -anchor w
4158 - label $w.stat.v_$name -text $value -anchor w
4159 - grid $w.stat.l_$name $w.stat.v_$name -sticky we -padx {0 5}
4160 - }
4161 - pack $w.stat -pady 10 -padx 10
4162 -
4163 - bind $w <Visibility> "grab $w; focus $w.buttons.close"
4164 - bind $w <Key-Escape> [list destroy $w]
4165 - bind $w <Key-Return> [list destroy $w]
4166 - wm title $w "[appname] ([reponame]): Database Statistics"
4167 - tkwait window $w
4168 -}
4169 -
4170 -proc do_gc {} {
4171 - set w [new_console {gc} {Compressing the object database}]
4172 - console_chain {
4173 - {console_exec {git pack-refs --prune}}
4174 - {console_exec {git reflog expire --all}}
4175 - {console_exec {git repack -a -d -l}}
4176 - {console_exec {git rerere gc}}
4177 - } $w
4178 -}
4179 -
4180 -proc do_fsck_objects {} {
4181 - set w [new_console {fsck-objects} \
4182 - {Verifying the object database with fsck-objects}]
4183 - set cmd [list git fsck-objects]
4184 - lappend cmd --full
4185 - lappend cmd --cache
4186 - lappend cmd --strict
4187 - console_exec $w $cmd console_done
4188 -}
4189 -
1007 set is_quitting 0
1008
1009 proc do_quit {} {
@@ -4234,591 +1051,10 @@ proc do_rescan {} {
1051 rescan {set ui_status_value {Ready.}}
1052 }
1053
4237 -proc unstage_helper {txt paths} {
4238 - global file_states current_diff_path
4239 -
4240 - if {![lock_index begin-update]} return
4241 -
4242 - set pathList [list]
4243 - set after {}
4244 - foreach path $paths {
4245 - switch -glob -- [lindex $file_states($path) 0] {
4246 - A? -
4247 - M? -
4248 - D? {
4249 - lappend pathList $path
4250 - if {$path eq $current_diff_path} {
4251 - set after {reshow_diff;}
4252 - }
4253 - }
4254 - }
4255 - }
4256 - if {$pathList eq {}} {
4257 - unlock_index
4258 - } else {
4259 - update_indexinfo \
4260 - $txt \
4261 - $pathList \
4262 - [concat $after {set ui_status_value {Ready.}}]
4263 - }
4264 -}
4265 -
4266 -proc do_unstage_selection {} {
4267 - global current_diff_path selected_paths
4268 -
4269 - if {[array size selected_paths] > 0} {
4270 - unstage_helper \
4271 - {Unstaging selected files from commit} \
4272 - [array names selected_paths]
4273 - } elseif {$current_diff_path ne {}} {
4274 - unstage_helper \
4275 - "Unstaging [short_path $current_diff_path] from commit" \
4276 - [list $current_diff_path]
4277 - }
4278 -}
4279 -
4280 -proc add_helper {txt paths} {
4281 - global file_states current_diff_path
4282 -
4283 - if {![lock_index begin-update]} return
4284 -
4285 - set pathList [list]
4286 - set after {}
4287 - foreach path $paths {
4288 - switch -glob -- [lindex $file_states($path) 0] {
4289 - _O -
4290 - ?M -
4291 - ?D -
4292 - U? {
4293 - lappend pathList $path
4294 - if {$path eq $current_diff_path} {
4295 - set after {reshow_diff;}
4296 - }
4297 - }
4298 - }
4299 - }
4300 - if {$pathList eq {}} {
4301 - unlock_index
4302 - } else {
4303 - update_index \
4304 - $txt \
4305 - $pathList \
4306 - [concat $after {set ui_status_value {Ready to commit.}}]
4307 - }
4308 -}
4309 -
4310 -proc do_add_selection {} {
4311 - global current_diff_path selected_paths
4312 -
4313 - if {[array size selected_paths] > 0} {
4314 - add_helper \
4315 - {Adding selected files} \
4316 - [array names selected_paths]
4317 - } elseif {$current_diff_path ne {}} {
4318 - add_helper \
4319 - "Adding [short_path $current_diff_path]" \
4320 - [list $current_diff_path]
4321 - }
4322 -}
4323 -
4324 -proc do_add_all {} {
4325 - global file_states
4326 -
4327 - set paths [list]
4328 - foreach path [array names file_states] {
4329 - switch -glob -- [lindex $file_states($path) 0] {
4330 - U? {continue}
4331 - ?M -
4332 - ?D {lappend paths $path}
4333 - }
4334 - }
4335 - add_helper {Adding all changed files} $paths
4336 -}
4337 -
4338 -proc revert_helper {txt paths} {
4339 - global file_states current_diff_path
4340 -
4341 - if {![lock_index begin-update]} return
4342 -
4343 - set pathList [list]
4344 - set after {}
4345 - foreach path $paths {
4346 - switch -glob -- [lindex $file_states($path) 0] {
4347 - U? {continue}
4348 - ?M -
4349 - ?D {
4350 - lappend pathList $path
4351 - if {$path eq $current_diff_path} {
4352 - set after {reshow_diff;}
4353 - }
4354 - }
4355 - }
4356 - }
4357 -
4358 - set n [llength $pathList]
4359 - if {$n == 0} {
4360 - unlock_index
4361 - return
4362 - } elseif {$n == 1} {
4363 - set s "[short_path [lindex $pathList]]"
4364 - } else {
4365 - set s "these $n files"
4366 - }
4367 -
4368 - set reply [tk_dialog \
4369 - .confirm_revert \
4370 - "[appname] ([reponame])" \
4371 - "Revert changes in $s?
4372 -
4373 -Any unadded changes will be permanently lost by the revert." \
4374 - question \
4375 - 1 \
4376 - {Do Nothing} \
4377 - {Revert Changes} \
4378 - ]
4379 - if {$reply == 1} {
4380 - checkout_index \
4381 - $txt \
4382 - $pathList \
4383 - [concat $after {set ui_status_value {Ready.}}]
4384 - } else {
4385 - unlock_index
4386 - }
4387 -}
4388 -
4389 -proc do_revert_selection {} {
4390 - global current_diff_path selected_paths
4391 -
4392 - if {[array size selected_paths] > 0} {
4393 - revert_helper \
4394 - {Reverting selected files} \
4395 - [array names selected_paths]
4396 - } elseif {$current_diff_path ne {}} {
4397 - revert_helper \
4398 - "Reverting [short_path $current_diff_path]" \
4399 - [list $current_diff_path]
4400 - }
4401 -}
4402 -
4403 -proc do_signoff {} {
4404 - global ui_comm
4405 -
4406 - set me [committer_ident]
4407 - if {$me eq {}} return
4408 -
4409 - set sob "Signed-off-by: $me"
4410 - set last [$ui_comm get {end -1c linestart} {end -1c}]
4411 - if {$last ne $sob} {
4412 - $ui_comm edit separator
4413 - if {$last ne {}
4414 - && ![regexp {^[A-Z][A-Za-z]*-[A-Za-z-]+: *} $last]} {
4415 - $ui_comm insert end "\n"
4416 - }
4417 - $ui_comm insert end "\n$sob"
4418 - $ui_comm edit separator
4419 - $ui_comm see end
4420 - }
4421 -}
4422 -
4423 -proc do_select_commit_type {} {
4424 - global commit_type selected_commit_type
4425 -
4426 - if {$selected_commit_type eq {new}
4427 - && [string match amend* $commit_type]} {
4428 - create_new_commit
4429 - } elseif {$selected_commit_type eq {amend}
4430 - && ![string match amend* $commit_type]} {
4431 - load_last_commit
4432 -
4433 - # The amend request was rejected...
4434 - #
4435 - if {![string match amend* $commit_type]} {
4436 - set selected_commit_type new
4437 - }
4438 - }
4439 -}
4440 -
1054 proc do_commit {} {
1055 commit_tree
1056 }
1057
4445 -proc do_about {} {
4446 - global appvers copyright
4447 - global tcl_patchLevel tk_patchLevel
4448 -
4449 - set w .about_dialog
4450 - toplevel $w
4451 - wm geometry $w "+[winfo rootx .]+[winfo rooty .]"
4452 -
4453 - label $w.header -text "About [appname]" \
4454 - -font font_uibold
4455 - pack $w.header -side top -fill x
4456 -
4457 - frame $w.buttons
4458 - button $w.buttons.close -text {Close} \
4459 - -default active \
4460 - -command [list destroy $w]
4461 - pack $w.buttons.close -side right
4462 - pack $w.buttons -side bottom -fill x -pady 10 -padx 10
4463 -
4464 - label $w.desc \
4465 - -text "git-gui - a graphical user interface for Git.
4466 -$copyright" \
4467 - -padx 5 -pady 5 \
4468 - -justify left \
4469 - -anchor w \
4470 - -borderwidth 1 \
4471 - -relief solid
4472 - pack $w.desc -side top -fill x -padx 5 -pady 5
4473 -
4474 - set v {}
4475 - append v "git-gui version $appvers\n"
4476 - append v "[git version]\n"
4477 - append v "\n"
4478 - if {$tcl_patchLevel eq $tk_patchLevel} {
4479 - append v "Tcl/Tk version $tcl_patchLevel"
4480 - } else {
4481 - append v "Tcl version $tcl_patchLevel"
4482 - append v ", Tk version $tk_patchLevel"
4483 - }
4484 -
4485 - label $w.vers \
4486 - -text $v \
4487 - -padx 5 -pady 5 \
4488 - -justify left \
4489 - -anchor w \
4490 - -borderwidth 1 \
4491 - -relief solid
4492 - pack $w.vers -side top -fill x -padx 5 -pady 5
4493 -
4494 - menu $w.ctxm -tearoff 0
4495 - $w.ctxm add command \
4496 - -label {Copy} \
4497 - -command "
4498 - clipboard clear
4499 - clipboard append -format STRING -type STRING -- \[$w.vers cget -text\]
4500 - "
4501 -
4502 - bind $w <Visibility> "grab $w; focus $w.buttons.close"
4503 - bind $w <Key-Escape> "destroy $w"
4504 - bind $w <Key-Return> "destroy $w"
4505 - bind_button3 $w.vers "tk_popup $w.ctxm %X %Y; grab $w; focus $w"
4506 - wm title $w "About [appname]"
4507 - tkwait window $w
4508 -}
4509 -
4510 -proc do_options {} {
4511 - global repo_config global_config font_descs
4512 - global repo_config_new global_config_new
4513 -
4514 - array unset repo_config_new
4515 - array unset global_config_new
4516 - foreach name [array names repo_config] {
4517 - set repo_config_new($name) $repo_config($name)
4518 - }
4519 - load_config 1
4520 - foreach name [array names repo_config] {
4521 - switch -- $name {
4522 - gui.diffcontext {continue}
4523 - }
4524 - set repo_config_new($name) $repo_config($name)
4525 - }
4526 - foreach name [array names global_config] {
4527 - set global_config_new($name) $global_config($name)
4528 - }
4529 -
4530 - set w .options_editor
4531 - toplevel $w
4532 - wm geometry $w "+[winfo rootx .]+[winfo rooty .]"
4533 -
4534 - label $w.header -text "Options" \
4535 - -font font_uibold
4536 - pack $w.header -side top -fill x
4537 -
4538 - frame $w.buttons
4539 - button $w.buttons.restore -text {Restore Defaults} \
4540 - -default normal \
4541 - -command do_restore_defaults
4542 - pack $w.buttons.restore -side left
4543 - button $w.buttons.save -text Save \
4544 - -default active \
4545 - -command [list do_save_config $w]
4546 - pack $w.buttons.save -side right
4547 - button $w.buttons.cancel -text {Cancel} \
4548 - -default normal \
4549 - -command [list destroy $w]
4550 - pack $w.buttons.cancel -side right -padx 5
4551 - pack $w.buttons -side bottom -fill x -pady 10 -padx 10
4552 -
4553 - labelframe $w.repo -text "[reponame] Repository"
4554 - labelframe $w.global -text {Global (All Repositories)}
4555 - pack $w.repo -side left -fill both -expand 1 -pady 5 -padx 5
4556 - pack $w.global -side right -fill both -expand 1 -pady 5 -padx 5
4557 -
4558 - set optid 0
4559 - foreach option {
4560 - {t user.name {User Name}}
4561 - {t user.email {Email Address}}
4562 -
4563 - {b merge.summary {Summarize Merge Commits}}
4564 - {i-1..5 merge.verbosity {Merge Verbosity}}
4565 -
4566 - {b gui.trustmtime {Trust File Modification Timestamps}}
4567 - {i-1..99 gui.diffcontext {Number of Diff Context Lines}}
4568 - {t gui.newbranchtemplate {New Branch Name Template}}
4569 - } {
4570 - set type [lindex $option 0]
4571 - set name [lindex $option 1]
4572 - set text [lindex $option 2]
4573 - incr optid
4574 - foreach f {repo global} {
4575 - switch -glob -- $type {
4576 - b {
4577 - checkbutton $w.$f.$optid -text $text \
4578 - -variable ${f}_config_new($name) \
4579 - -onvalue true \
4580 - -offvalue false
4581 - pack $w.$f.$optid -side top -anchor w
4582 - }
4583 - i-* {
4584 - regexp -- {-(\d+)\.\.(\d+)$} $type _junk min max
4585 - frame $w.$f.$optid
4586 - label $w.$f.$optid.l -text "$text:"
4587 - pack $w.$f.$optid.l -side left -anchor w -fill x
4588 - spinbox $w.$f.$optid.v \
4589 - -textvariable ${f}_config_new($name) \
4590 - -from $min \
4591 - -to $max \
4592 - -increment 1 \
4593 - -width [expr {1 + [string length $max]}]
4594 - bind $w.$f.$optid.v <FocusIn> {%W selection range 0 end}
4595 - pack $w.$f.$optid.v -side right -anchor e -padx 5
4596 - pack $w.$f.$optid -side top -anchor w -fill x
4597 - }
4598 - t {
4599 - frame $w.$f.$optid
4600 - label $w.$f.$optid.l -text "$text:"
4601 - entry $w.$f.$optid.v \
4602 - -borderwidth 1 \
4603 - -relief sunken \
4604 - -width 20 \
4605 - -textvariable ${f}_config_new($name)
4606 - pack $w.$f.$optid.l -side left -anchor w
4607 - pack $w.$f.$optid.v -side left -anchor w \
4608 - -fill x -expand 1 \
4609 - -padx 5
4610 - pack $w.$f.$optid -side top -anchor w -fill x
4611 - }
4612 - }
4613 - }
4614 - }
4615 -
4616 - set all_fonts [lsort [font families]]
4617 - foreach option $font_descs {
4618 - set name [lindex $option 0]
4619 - set font [lindex $option 1]
4620 - set text [lindex $option 2]
4621 -
4622 - set global_config_new(gui.$font^^family) \
4623 - [font configure $font -family]
4624 - set global_config_new(gui.$font^^size) \
4625 - [font configure $font -size]
4626 -
4627 - frame $w.global.$name
4628 - label $w.global.$name.l -text "$text:"
4629 - pack $w.global.$name.l -side left -anchor w -fill x
4630 - eval tk_optionMenu $w.global.$name.family \
4631 - global_config_new(gui.$font^^family) \
4632 - $all_fonts
4633 - spinbox $w.global.$name.size \
4634 - -textvariable global_config_new(gui.$font^^size) \
4635 - -from 2 -to 80 -increment 1 \
4636 - -width 3
4637 - bind $w.global.$name.size <FocusIn> {%W selection range 0 end}
4638 - pack $w.global.$name.size -side right -anchor e
4639 - pack $w.global.$name.family -side right -anchor e
4640 - pack $w.global.$name -side top -anchor w -fill x
4641 - }
4642 -
4643 - bind $w <Visibility> "grab $w; focus $w.buttons.save"
4644 - bind $w <Key-Escape> "destroy $w"
4645 - bind $w <Key-Return> [list do_save_config $w]
4646 - wm title $w "[appname] ([reponame]): Options"
4647 - tkwait window $w
4648 -}
4649 -
4650 -proc do_restore_defaults {} {
4651 - global font_descs default_config repo_config
4652 - global repo_config_new global_config_new
4653 -
4654 - foreach name [array names default_config] {
4655 - set repo_config_new($name) $default_config($name)
4656 - set global_config_new($name) $default_config($name)
4657 - }
4658 -
4659 - foreach option $font_descs {
4660 - set name [lindex $option 0]
4661 - set repo_config(gui.$name) $default_config(gui.$name)
4662 - }
4663 - apply_config
4664 -
4665 - foreach option $font_descs {
4666 - set name [lindex $option 0]
4667 - set font [lindex $option 1]
4668 - set global_config_new(gui.$font^^family) \
4669 - [font configure $font -family]
4670 - set global_config_new(gui.$font^^size) \
4671 - [font configure $font -size]
4672 - }
4673 -}
4674 -
4675 -proc do_save_config {w} {
4676 - if {[catch {save_config} err]} {
4677 - error_popup "Failed to completely save options:\n\n$err"
4678 - }
4679 - reshow_diff
4680 - destroy $w
4681 -}
4682 -
4683 -proc do_windows_shortcut {} {
4684 - global argv0
4685 -
4686 - set fn [tk_getSaveFile \
4687 - -parent . \
4688 - -title "[appname] ([reponame]): Create Desktop Icon" \
4689 - -initialfile "Git [reponame].bat"]
4690 - if {$fn != {}} {
4691 - if {[catch {
4692 - set fd [open $fn w]
4693 - puts $fd "@ECHO Entering [reponame]"
4694 - puts $fd "@ECHO Starting git-gui... please wait..."
4695 - puts $fd "@SET PATH=[file normalize [gitexec]];%PATH%"
4696 - puts $fd "@SET GIT_DIR=[file normalize [gitdir]]"
4697 - puts -nonewline $fd "@\"[info nameofexecutable]\""
4698 - puts $fd " \"[file normalize $argv0]\""
4699 - close $fd
4700 - } err]} {
4701 - error_popup "Cannot write script:\n\n$err"
4702 - }
4703 - }
4704 -}
4705 -
4706 -proc do_cygwin_shortcut {} {
4707 - global argv0
4708 -
4709 - if {[catch {
4710 - set desktop [exec cygpath \
4711 - --windows \
4712 - --absolute \
4713 - --long-name \
4714 - --desktop]
4715 - }]} {
4716 - set desktop .
4717 - }
4718 - set fn [tk_getSaveFile \
4719 - -parent . \
4720 - -title "[appname] ([reponame]): Create Desktop Icon" \
4721 - -initialdir $desktop \
4722 - -initialfile "Git [reponame].bat"]
4723 - if {$fn != {}} {
4724 - if {[catch {
4725 - set fd [open $fn w]
4726 - set sh [exec cygpath \
4727 - --windows \
4728 - --absolute \
4729 - /bin/sh]
4730 - set me [exec cygpath \
4731 - --unix \
4732 - --absolute \
4733 - $argv0]
4734 - set gd [exec cygpath \
4735 - --unix \
4736 - --absolute \
4737 - [gitdir]]
4738 - set gw [exec cygpath \
4739 - --windows \
4740 - --absolute \
4741 - [file dirname [gitdir]]]
4742 - regsub -all ' $me "'\\''" me
4743 - regsub -all ' $gd "'\\''" gd
4744 - puts $fd "@ECHO Entering $gw"
4745 - puts $fd "@ECHO Starting git-gui... please wait..."
4746 - puts -nonewline $fd "@\"$sh\" --login -c \""
4747 - puts -nonewline $fd "GIT_DIR='$gd'"
4748 - puts -nonewline $fd " '$me'"
4749 - puts $fd "&\""
4750 - close $fd
4751 - } err]} {
4752 - error_popup "Cannot write script:\n\n$err"
4753 - }
4754 - }
4755 -}
4756 -
4757 -proc do_macosx_app {} {
4758 - global argv0 env
4759 -
4760 - set fn [tk_getSaveFile \
4761 - -parent . \
4762 - -title "[appname] ([reponame]): Create Desktop Icon" \
4763 - -initialdir [file join $env(HOME) Desktop] \
4764 - -initialfile "Git [reponame].app"]
4765 - if {$fn != {}} {
4766 - if {[catch {
4767 - set Contents [file join $fn Contents]
4768 - set MacOS [file join $Contents MacOS]
4769 - set exe [file join $MacOS git-gui]
4770 -
4771 - file mkdir $MacOS
4772 -
4773 - set fd [open [file join $Contents Info.plist] w]
4774 - puts $fd {<?xml version="1.0" encoding="UTF-8"?>
4775 -<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
4776 -<plist version="1.0">
4777 -<dict>
4778 - <key>CFBundleDevelopmentRegion</key>
4779 - <string>English</string>
4780 - <key>CFBundleExecutable</key>
4781 - <string>git-gui</string>
4782 - <key>CFBundleIdentifier</key>
4783 - <string>org.spearce.git-gui</string>
4784 - <key>CFBundleInfoDictionaryVersion</key>
4785 - <string>6.0</string>
4786 - <key>CFBundlePackageType</key>
4787 - <string>APPL</string>
4788 - <key>CFBundleSignature</key>
4789 - <string>????</string>
4790 - <key>CFBundleVersion</key>
4791 - <string>1.0</string>
4792 - <key>NSPrincipalClass</key>
4793 - <string>NSApplication</string>
4794 -</dict>
4795 -</plist>}
4796 - close $fd
4797 -
4798 - set fd [open $exe w]
4799 - set gd [file normalize [gitdir]]
4800 - set ep [file normalize [gitexec]]
4801 - regsub -all ' $gd "'\\''" gd
4802 - regsub -all ' $ep "'\\''" ep
4803 - puts $fd "#!/bin/sh"
4804 - foreach name [array names env] {
4805 - if {[string match GIT_* $name]} {
4806 - regsub -all ' $env($name) "'\\''" v
4807 - puts $fd "export $name='$v'"
4808 - }
4809 - }
4810 - puts $fd "export PATH='$ep':\$PATH"
4811 - puts $fd "export GIT_DIR='$gd'"
4812 - puts $fd "exec [file normalize $argv0]"
4813 - close $fd
4814 -
4815 - file attributes $exe -permissions u+x,g+x,o+x
4816 - } err]} {
4817 - error_popup "Cannot write icon:\n\n$err"
4818 - }
4819 - }
4820 -}
4821 -
1058 proc toggle_or_diff {w x y} {
1059 global file_states file_lists current_diff_path ui_index ui_workdir
1060 global last_clicked selected_paths
lib/blame.tcl new
+390
@@ -0,0 +1,390 @@
1 +# git-gui blame viewer
2 +# Copyright (C) 2006, 2007 Shawn Pearce
3 +
4 +proc show_blame {commit path} {
5 + global next_browser_id blame_status blame_data
6 +
7 + if {[winfo ismapped .]} {
8 + set w .browser[incr next_browser_id]
9 + set tl $w
10 + toplevel $w
11 + } else {
12 + set w {}
13 + set tl .
14 + }
15 + set blame_status($w) {Loading current file content...}
16 +
17 + label $w.path -text "$commit:$path" \
18 + -anchor w \
19 + -justify left \
20 + -borderwidth 1 \
21 + -relief sunken \
22 + -font font_uibold
23 + pack $w.path -side top -fill x
24 +
25 + frame $w.out
26 + text $w.out.loaded_t \
27 + -background white -borderwidth 0 \
28 + -state disabled \
29 + -wrap none \
30 + -height 40 \
31 + -width 1 \
32 + -font font_diff
33 + $w.out.loaded_t tag conf annotated -background grey
34 +
35 + text $w.out.linenumber_t \
36 + -background white -borderwidth 0 \
37 + -state disabled \
38 + -wrap none \
39 + -height 40 \
40 + -width 5 \
41 + -font font_diff
42 + $w.out.linenumber_t tag conf linenumber -justify right
43 +
44 + text $w.out.file_t \
45 + -background white -borderwidth 0 \
46 + -state disabled \
47 + -wrap none \
48 + -height 40 \
49 + -width 80 \
50 + -xscrollcommand [list $w.out.sbx set] \
51 + -font font_diff
52 +
53 + scrollbar $w.out.sbx -orient h -command [list $w.out.file_t xview]
54 + scrollbar $w.out.sby -orient v \
55 + -command [list scrollbar2many [list \
56 + $w.out.loaded_t \
57 + $w.out.linenumber_t \
58 + $w.out.file_t \
59 + ] yview]
60 + grid \
61 + $w.out.linenumber_t \
62 + $w.out.loaded_t \
63 + $w.out.file_t \
64 + $w.out.sby \
65 + -sticky nsew
66 + grid conf $w.out.sbx -column 2 -sticky we
67 + grid columnconfigure $w.out 2 -weight 1
68 + grid rowconfigure $w.out 0 -weight 1
69 + pack $w.out -fill both -expand 1
70 +
71 + label $w.status -textvariable blame_status($w) \
72 + -anchor w \
73 + -justify left \
74 + -borderwidth 1 \
75 + -relief sunken
76 + pack $w.status -side bottom -fill x
77 +
78 + frame $w.cm
79 + text $w.cm.t \
80 + -background white -borderwidth 0 \
81 + -state disabled \
82 + -wrap none \
83 + -height 10 \
84 + -width 80 \
85 + -xscrollcommand [list $w.cm.sbx set] \
86 + -yscrollcommand [list $w.cm.sby set] \
87 + -font font_diff
88 + scrollbar $w.cm.sbx -orient h -command [list $w.cm.t xview]
89 + scrollbar $w.cm.sby -orient v -command [list $w.cm.t yview]
90 + pack $w.cm.sby -side right -fill y
91 + pack $w.cm.sbx -side bottom -fill x
92 + pack $w.cm.t -expand 1 -fill both
93 + pack $w.cm -side bottom -fill x
94 +
95 + menu $w.ctxm -tearoff 0
96 + $w.ctxm add command -label "Copy Commit" \
97 + -command "blame_copycommit $w \$cursorW @\$cursorX,\$cursorY"
98 +
99 + foreach i [list \
100 + $w.out.loaded_t \
101 + $w.out.linenumber_t \
102 + $w.out.file_t] {
103 + $i tag conf in_sel \
104 + -background [$i cget -foreground] \
105 + -foreground [$i cget -background]
106 + $i conf -yscrollcommand \
107 + [list many2scrollbar [list \
108 + $w.out.loaded_t \
109 + $w.out.linenumber_t \
110 + $w.out.file_t \
111 + ] yview $w.out.sby]
112 + bind $i <Button-1> "
113 + blame_click {$w} \\
114 + $w.cm.t \\
115 + $w.out.linenumber_t \\
116 + $w.out.file_t \\
117 + $i @%x,%y
118 + focus $i
119 + "
120 + bind_button3 $i "
121 + set cursorX %x
122 + set cursorY %y
123 + set cursorW %W
124 + tk_popup $w.ctxm %X %Y
125 + "
126 + }
127 +
128 + bind $w.cm.t <Button-1> "focus $w.cm.t"
129 + bind $tl <Visibility> "focus $tl"
130 + bind $tl <Destroy> "
131 + array unset blame_status {$w}
132 + array unset blame_data $w,*
133 + "
134 + wm title $tl "[appname] ([reponame]): File Viewer"
135 +
136 + set blame_data($w,commit_count) 0
137 + set blame_data($w,commit_list) {}
138 + set blame_data($w,total_lines) 0
139 + set blame_data($w,blame_lines) 0
140 + set blame_data($w,highlight_commit) {}
141 + set blame_data($w,highlight_line) -1
142 +
143 + set cmd [list git cat-file blob "$commit:$path"]
144 + set fd [open "| $cmd" r]
145 + fconfigure $fd -blocking 0 -translation lf -encoding binary
146 + fileevent $fd readable [list read_blame_catfile \
147 + $fd $w $commit $path \
148 + $w.cm.t $w.out.loaded_t $w.out.linenumber_t $w.out.file_t]
149 +}
150 +
151 +proc read_blame_catfile {fd w commit path w_cmit w_load w_line w_file} {
152 + global blame_status blame_data
153 +
154 + if {![winfo exists $w_file]} {
155 + catch {close $fd}
156 + return
157 + }
158 +
159 + set n $blame_data($w,total_lines)
160 + $w_load conf -state normal
161 + $w_line conf -state normal
162 + $w_file conf -state normal
163 + while {[gets $fd line] >= 0} {
164 + regsub "\r\$" $line {} line
165 + incr n
166 + $w_load insert end "\n"
167 + $w_line insert end "$n\n" linenumber
168 + $w_file insert end "$line\n"
169 + }
170 + $w_load conf -state disabled
171 + $w_line conf -state disabled
172 + $w_file conf -state disabled
173 + set blame_data($w,total_lines) $n
174 +
175 + if {[eof $fd]} {
176 + close $fd
177 + blame_incremental_status $w
178 + set cmd [list git blame -M -C --incremental]
179 + lappend cmd $commit -- $path
180 + set fd [open "| $cmd" r]
181 + fconfigure $fd -blocking 0 -translation lf -encoding binary
182 + fileevent $fd readable [list read_blame_incremental $fd $w \
183 + $w_load $w_cmit $w_line $w_file]
184 + }
185 +}
186 +
187 +proc read_blame_incremental {fd w w_load w_cmit w_line w_file} {
188 + global blame_status blame_data
189 +
190 + if {![winfo exists $w_file]} {
191 + catch {close $fd}
192 + return
193 + }
194 +
195 + while {[gets $fd line] >= 0} {
196 + if {[regexp {^([a-z0-9]{40}) (\d+) (\d+) (\d+)$} $line line \
197 + cmit original_line final_line line_count]} {
198 + set blame_data($w,commit) $cmit
199 + set blame_data($w,original_line) $original_line
200 + set blame_data($w,final_line) $final_line
201 + set blame_data($w,line_count) $line_count
202 +
203 + if {[catch {set g $blame_data($w,$cmit,order)}]} {
204 + $w_line tag conf g$cmit
205 + $w_file tag conf g$cmit
206 + $w_line tag raise in_sel
207 + $w_file tag raise in_sel
208 + $w_file tag raise sel
209 + set blame_data($w,$cmit,order) $blame_data($w,commit_count)
210 + incr blame_data($w,commit_count)
211 + lappend blame_data($w,commit_list) $cmit
212 + }
213 + } elseif {[string match {filename *} $line]} {
214 + set file [string range $line 9 end]
215 + set n $blame_data($w,line_count)
216 + set lno $blame_data($w,final_line)
217 + set cmit $blame_data($w,commit)
218 +
219 + while {$n > 0} {
220 + if {[catch {set g g$blame_data($w,line$lno,commit)}]} {
221 + $w_load tag add annotated $lno.0 "$lno.0 lineend + 1c"
222 + } else {
223 + $w_line tag remove g$g $lno.0 "$lno.0 lineend + 1c"
224 + $w_file tag remove g$g $lno.0 "$lno.0 lineend + 1c"
225 + }
226 +
227 + set blame_data($w,line$lno,commit) $cmit
228 + set blame_data($w,line$lno,file) $file
229 + $w_line tag add g$cmit $lno.0 "$lno.0 lineend + 1c"
230 + $w_file tag add g$cmit $lno.0 "$lno.0 lineend + 1c"
231 +
232 + if {$blame_data($w,highlight_line) == -1} {
233 + if {[lindex [$w_file yview] 0] == 0} {
234 + $w_file see $lno.0
235 + blame_showcommit $w $w_cmit $w_line $w_file $lno
236 + }
237 + } elseif {$blame_data($w,highlight_line) == $lno} {
238 + blame_showcommit $w $w_cmit $w_line $w_file $lno
239 + }
240 +
241 + incr n -1
242 + incr lno
243 + incr blame_data($w,blame_lines)
244 + }
245 +
246 + set hc $blame_data($w,highlight_commit)
247 + if {$hc ne {}
248 + && [expr {$blame_data($w,$hc,order) + 1}]
249 + == $blame_data($w,$cmit,order)} {
250 + blame_showcommit $w $w_cmit $w_line $w_file \
251 + $blame_data($w,highlight_line)
252 + }
253 + } elseif {[regexp {^([a-z-]+) (.*)$} $line line header data]} {
254 + set blame_data($w,$blame_data($w,commit),$header) $data
255 + }
256 + }
257 +
258 + if {[eof $fd]} {
259 + close $fd
260 + set blame_status($w) {Annotation complete.}
261 + } else {
262 + blame_incremental_status $w
263 + }
264 +}
265 +
266 +proc blame_incremental_status {w} {
267 + global blame_status blame_data
268 +
269 + set have $blame_data($w,blame_lines)
270 + set total $blame_data($w,total_lines)
271 + set pdone 0
272 + if {$total} {set pdone [expr {100 * $have / $total}]}
273 +
274 + set blame_status($w) [format \
275 + "Loading annotations... %i of %i lines annotated (%2i%%)" \
276 + $have $total $pdone]
277 +}
278 +
279 +proc blame_click {w w_cmit w_line w_file cur_w pos} {
280 + set lno [lindex [split [$cur_w index $pos] .] 0]
281 + if {$lno eq {}} return
282 +
283 + $w_line tag remove in_sel 0.0 end
284 + $w_file tag remove in_sel 0.0 end
285 + $w_line tag add in_sel $lno.0 "$lno.0 + 1 line"
286 + $w_file tag add in_sel $lno.0 "$lno.0 + 1 line"
287 +
288 + blame_showcommit $w $w_cmit $w_line $w_file $lno
289 +}
290 +
291 +set blame_colors {
292 + #ff4040
293 + #ff40ff
294 + #4040ff
295 +}
296 +
297 +proc blame_showcommit {w w_cmit w_line w_file lno} {
298 + global blame_colors blame_data repo_config
299 +
300 + set cmit $blame_data($w,highlight_commit)
301 + if {$cmit ne {}} {
302 + set idx $blame_data($w,$cmit,order)
303 + set i 0
304 + foreach c $blame_colors {
305 + set h [lindex $blame_data($w,commit_list) [expr {$idx - 1 + $i}]]
306 + $w_line tag conf g$h -background white
307 + $w_file tag conf g$h -background white
308 + incr i
309 + }
310 + }
311 +
312 + $w_cmit conf -state normal
313 + $w_cmit delete 0.0 end
314 + if {[catch {set cmit $blame_data($w,line$lno,commit)}]} {
315 + set cmit {}
316 + $w_cmit insert end "Loading annotation..."
317 + } else {
318 + set idx $blame_data($w,$cmit,order)
319 + set i 0
320 + foreach c $blame_colors {
321 + set h [lindex $blame_data($w,commit_list) [expr {$idx - 1 + $i}]]
322 + $w_line tag conf g$h -background $c
323 + $w_file tag conf g$h -background $c
324 + incr i
325 + }
326 +
327 + set author_name {}
328 + set author_email {}
329 + set author_time {}
330 + catch {set author_name $blame_data($w,$cmit,author)}
331 + catch {set author_email $blame_data($w,$cmit,author-mail)}
332 + catch {set author_time [clock format $blame_data($w,$cmit,author-time)]}
333 +
334 + set committer_name {}
335 + set committer_email {}
336 + set committer_time {}
337 + catch {set committer_name $blame_data($w,$cmit,committer)}
338 + catch {set committer_email $blame_data($w,$cmit,committer-mail)}
339 + catch {set committer_time [clock format $blame_data($w,$cmit,committer-time)]}
340 +
341 + if {[catch {set msg $blame_data($w,$cmit,message)}]} {
342 + set msg {}
343 + catch {
344 + set fd [open "| git cat-file commit $cmit" r]
345 + fconfigure $fd -encoding binary -translation lf
346 + if {[catch {set enc $repo_config(i18n.commitencoding)}]} {
347 + set enc utf-8
348 + }
349 + while {[gets $fd line] > 0} {
350 + if {[string match {encoding *} $line]} {
351 + set enc [string tolower [string range $line 9 end]]
352 + }
353 + }
354 + set msg [encoding convertfrom $enc [read $fd]]
355 + set msg [string trim $msg]
356 + close $fd
357 +
358 + set author_name [encoding convertfrom $enc $author_name]
359 + set committer_name [encoding convertfrom $enc $committer_name]
360 +
361 + set blame_data($w,$cmit,author) $author_name
362 + set blame_data($w,$cmit,committer) $committer_name
363 + }
364 + set blame_data($w,$cmit,message) $msg
365 + }
366 +
367 + $w_cmit insert end "commit $cmit\n"
368 + $w_cmit insert end "Author: $author_name $author_email $author_time\n"
369 + $w_cmit insert end "Committer: $committer_name $committer_email $committer_time\n"
370 + $w_cmit insert end "Original File: [escape_path $blame_data($w,line$lno,file)]\n"
371 + $w_cmit insert end "\n"
372 + $w_cmit insert end $msg
373 + }
374 + $w_cmit conf -state disabled
375 +
376 + set blame_data($w,highlight_line) $lno
377 + set blame_data($w,highlight_commit) $cmit
378 +}
379 +
380 +proc blame_copycommit {w i pos} {
381 + global blame_data
382 + set lno [lindex [split [$i index $pos] .] 0]
383 + if {![catch {set commit $blame_data($w,line$lno,commit)}]} {
384 + clipboard clear
385 + clipboard append \
386 + -format STRING \
387 + -type STRING \
388 + -- $commit
389 + }
390 +}
lib/branch.tcl new
+572
@@ -0,0 +1,572 @@
1 +# git-gui branch (create/delete) support
2 +# Copyright (C) 2006, 2007 Shawn Pearce
3 +
4 +proc load_all_heads {} {
5 + global all_heads
6 +
7 + set all_heads [list]
8 + set fd [open "| git for-each-ref --format=%(refname) refs/heads" r]
9 + while {[gets $fd line] > 0} {
10 + if {[is_tracking_branch $line]} continue
11 + if {![regsub ^refs/heads/ $line {} name]} continue
12 + lappend all_heads $name
13 + }
14 + close $fd
15 +
16 + set all_heads [lsort $all_heads]
17 +}
18 +
19 +proc load_all_tags {} {
20 + set all_tags [list]
21 + set fd [open "| git for-each-ref --format=%(refname) refs/tags" r]
22 + while {[gets $fd line] > 0} {
23 + if {![regsub ^refs/tags/ $line {} name]} continue
24 + lappend all_tags $name
25 + }
26 + close $fd
27 +
28 + return [lsort $all_tags]
29 +}
30 +
31 +proc populate_branch_menu {} {
32 + global all_heads disable_on_lock
33 +
34 + set m .mbar.branch
35 + set last [$m index last]
36 + for {set i 0} {$i <= $last} {incr i} {
37 + if {[$m type $i] eq {separator}} {
38 + $m delete $i last
39 + set new_dol [list]
40 + foreach a $disable_on_lock {
41 + if {[lindex $a 0] ne $m || [lindex $a 2] < $i} {
42 + lappend new_dol $a
43 + }
44 + }
45 + set disable_on_lock $new_dol
46 + break
47 + }
48 + }
49 +
50 + if {$all_heads ne {}} {
51 + $m add separator
52 + }
53 + foreach b $all_heads {
54 + $m add radiobutton \
55 + -label $b \
56 + -command [list switch_branch $b] \
57 + -variable current_branch \
58 + -value $b
59 + lappend disable_on_lock \
60 + [list $m entryconf [$m index last] -state]
61 + }
62 +}
63 +
64 +proc do_create_branch_action {w} {
65 + global all_heads null_sha1 repo_config
66 + global create_branch_checkout create_branch_revtype
67 + global create_branch_head create_branch_trackinghead
68 + global create_branch_name create_branch_revexp
69 + global create_branch_tag
70 +
71 + set newbranch $create_branch_name
72 + if {$newbranch eq {}
73 + || $newbranch eq $repo_config(gui.newbranchtemplate)} {
74 + tk_messageBox \
75 + -icon error \
76 + -type ok \
77 + -title [wm title $w] \
78 + -parent $w \
79 + -message "Please supply a branch name."
80 + focus $w.desc.name_t
81 + return
82 + }
83 + if {![catch {git show-ref --verify -- "refs/heads/$newbranch"}]} {
84 + tk_messageBox \
85 + -icon error \
86 + -type ok \
87 + -title [wm title $w] \
88 + -parent $w \
89 + -message "Branch '$newbranch' already exists."
90 + focus $w.desc.name_t
91 + return
92 + }
93 + if {[catch {git check-ref-format "heads/$newbranch"}]} {
94 + tk_messageBox \
95 + -icon error \
96 + -type ok \
97 + -title [wm title $w] \
98 + -parent $w \
99 + -message "We do not like '$newbranch' as a branch name."
100 + focus $w.desc.name_t
101 + return
102 + }
103 +
104 + set rev {}
105 + switch -- $create_branch_revtype {
106 + head {set rev $create_branch_head}
107 + tracking {set rev $create_branch_trackinghead}
108 + tag {set rev $create_branch_tag}
109 + expression {set rev $create_branch_revexp}
110 + }
111 + if {[catch {set cmt [git rev-parse --verify "${rev}^0"]}]} {
112 + tk_messageBox \
113 + -icon error \
114 + -type ok \
115 + -title [wm title $w] \
116 + -parent $w \
117 + -message "Invalid starting revision: $rev"
118 + return
119 + }
120 + if {[catch {
121 + git update-ref \
122 + -m "branch: Created from $rev" \
123 + "refs/heads/$newbranch" \
124 + $cmt \
125 + $null_sha1
126 + } err]} {
127 + tk_messageBox \
128 + -icon error \
129 + -type ok \
130 + -title [wm title $w] \
131 + -parent $w \
132 + -message "Failed to create '$newbranch'.\n\n$err"
133 + return
134 + }
135 +
136 + lappend all_heads $newbranch
137 + set all_heads [lsort $all_heads]
138 + populate_branch_menu
139 + destroy $w
140 + if {$create_branch_checkout} {
141 + switch_branch $newbranch
142 + }
143 +}
144 +
145 +proc radio_selector {varname value args} {
146 + upvar #0 $varname var
147 + set var $value
148 +}
149 +
150 +trace add variable create_branch_head write \
151 + [list radio_selector create_branch_revtype head]
152 +trace add variable create_branch_trackinghead write \
153 + [list radio_selector create_branch_revtype tracking]
154 +trace add variable create_branch_tag write \
155 + [list radio_selector create_branch_revtype tag]
156 +
157 +trace add variable delete_branch_head write \
158 + [list radio_selector delete_branch_checktype head]
159 +trace add variable delete_branch_trackinghead write \
160 + [list radio_selector delete_branch_checktype tracking]
161 +
162 +proc do_create_branch {} {
163 + global all_heads current_branch repo_config
164 + global create_branch_checkout create_branch_revtype
165 + global create_branch_head create_branch_trackinghead
166 + global create_branch_name create_branch_revexp
167 + global create_branch_tag
168 +
169 + set w .branch_editor
170 + toplevel $w
171 + wm geometry $w "+[winfo rootx .]+[winfo rooty .]"
172 +
173 + label $w.header -text {Create New Branch} \
174 + -font font_uibold
175 + pack $w.header -side top -fill x
176 +
177 + frame $w.buttons
178 + button $w.buttons.create -text Create \
179 + -default active \
180 + -command [list do_create_branch_action $w]
181 + pack $w.buttons.create -side right
182 + button $w.buttons.cancel -text {Cancel} \
183 + -command [list destroy $w]
184 + pack $w.buttons.cancel -side right -padx 5
185 + pack $w.buttons -side bottom -fill x -pady 10 -padx 10
186 +
187 + labelframe $w.desc -text {Branch Description}
188 + label $w.desc.name_l -text {Name:}
189 + entry $w.desc.name_t \
190 + -borderwidth 1 \
191 + -relief sunken \
192 + -width 40 \
193 + -textvariable create_branch_name \
194 + -validate key \
195 + -validatecommand {
196 + if {%d == 1 && [regexp {[~^:?*\[\0- ]} %S]} {return 0}
197 + return 1
198 + }
199 + grid $w.desc.name_l $w.desc.name_t -sticky we -padx {0 5}
200 + grid columnconfigure $w.desc 1 -weight 1
201 + pack $w.desc -anchor nw -fill x -pady 5 -padx 5
202 +
203 + labelframe $w.from -text {Starting Revision}
204 + radiobutton $w.from.head_r \
205 + -text {Local Branch:} \
206 + -value head \
207 + -variable create_branch_revtype
208 + eval tk_optionMenu $w.from.head_m create_branch_head $all_heads
209 + grid $w.from.head_r $w.from.head_m -sticky w
210 + set all_trackings [all_tracking_branches]
211 + if {$all_trackings ne {}} {
212 + set create_branch_trackinghead [lindex $all_trackings 0]
213 + radiobutton $w.from.tracking_r \
214 + -text {Tracking Branch:} \
215 + -value tracking \
216 + -variable create_branch_revtype
217 + eval tk_optionMenu $w.from.tracking_m \
218 + create_branch_trackinghead \
219 + $all_trackings
220 + grid $w.from.tracking_r $w.from.tracking_m -sticky w
221 + }
222 + set all_tags [load_all_tags]
223 + if {$all_tags ne {}} {
224 + set create_branch_tag [lindex $all_tags 0]
225 + radiobutton $w.from.tag_r \
226 + -text {Tag:} \
227 + -value tag \
228 + -variable create_branch_revtype
229 + eval tk_optionMenu $w.from.tag_m create_branch_tag $all_tags
230 + grid $w.from.tag_r $w.from.tag_m -sticky w
231 + }
232 + radiobutton $w.from.exp_r \
233 + -text {Revision Expression:} \
234 + -value expression \
235 + -variable create_branch_revtype
236 + entry $w.from.exp_t \
237 + -borderwidth 1 \
238 + -relief sunken \
239 + -width 50 \
240 + -textvariable create_branch_revexp \
241 + -validate key \
242 + -validatecommand {
243 + if {%d == 1 && [regexp {\s} %S]} {return 0}
244 + if {%d == 1 && [string length %S] > 0} {
245 + set create_branch_revtype expression
246 + }
247 + return 1
248 + }
249 + grid $w.from.exp_r $w.from.exp_t -sticky we -padx {0 5}
250 + grid columnconfigure $w.from 1 -weight 1
251 + pack $w.from -anchor nw -fill x -pady 5 -padx 5
252 +
253 + labelframe $w.postActions -text {Post Creation Actions}
254 + checkbutton $w.postActions.checkout \
255 + -text {Checkout after creation} \
256 + -variable create_branch_checkout
257 + pack $w.postActions.checkout -anchor nw
258 + pack $w.postActions -anchor nw -fill x -pady 5 -padx 5
259 +
260 + set create_branch_checkout 1
261 + set create_branch_head $current_branch
262 + set create_branch_revtype head
263 + set create_branch_name $repo_config(gui.newbranchtemplate)
264 + set create_branch_revexp {}
265 +
266 + bind $w <Visibility> "
267 + grab $w
268 + $w.desc.name_t icursor end
269 + focus $w.desc.name_t
270 + "
271 + bind $w <Key-Escape> "destroy $w"
272 + bind $w <Key-Return> "do_create_branch_action $w;break"
273 + wm title $w "[appname] ([reponame]): Create Branch"
274 + tkwait window $w
275 +}
276 +
277 +proc do_delete_branch_action {w} {
278 + global all_heads
279 + global delete_branch_checktype delete_branch_head delete_branch_trackinghead
280 +
281 + set check_rev {}
282 + switch -- $delete_branch_checktype {
283 + head {set check_rev $delete_branch_head}
284 + tracking {set check_rev $delete_branch_trackinghead}
285 + always {set check_rev {:none}}
286 + }
287 + if {$check_rev eq {:none}} {
288 + set check_cmt {}
289 + } elseif {[catch {set check_cmt [git rev-parse --verify "${check_rev}^0"]}]} {
290 + tk_messageBox \
291 + -icon error \
292 + -type ok \
293 + -title [wm title $w] \
294 + -parent $w \
295 + -message "Invalid check revision: $check_rev"
296 + return
297 + }
298 +
299 + set to_delete [list]
300 + set not_merged [list]
301 + foreach i [$w.list.l curselection] {
302 + set b [$w.list.l get $i]
303 + if {[catch {set o [git rev-parse --verify $b]}]} continue
304 + if {$check_cmt ne {}} {
305 + if {$b eq $check_rev} continue
306 + if {[catch {set m [git merge-base $o $check_cmt]}]} continue
307 + if {$o ne $m} {
308 + lappend not_merged $b
309 + continue
310 + }
311 + }
312 + lappend to_delete [list $b $o]
313 + }
314 + if {$not_merged ne {}} {
315 + set msg "The following branches are not completely merged into $check_rev:
316 +
317 + - [join $not_merged "\n - "]"
318 + tk_messageBox \
319 + -icon info \
320 + -type ok \
321 + -title [wm title $w] \
322 + -parent $w \
323 + -message $msg
324 + }
325 + if {$to_delete eq {}} return
326 + if {$delete_branch_checktype eq {always}} {
327 + set msg {Recovering deleted branches is difficult.
328 +
329 +Delete the selected branches?}
330 + if {[tk_messageBox \
331 + -icon warning \
332 + -type yesno \
333 + -title [wm title $w] \
334 + -parent $w \
335 + -message $msg] ne yes} {
336 + return
337 + }
338 + }
339 +
340 + set failed {}
341 + foreach i $to_delete {
342 + set b [lindex $i 0]
343 + set o [lindex $i 1]
344 + if {[catch {git update-ref -d "refs/heads/$b" $o} err]} {
345 + append failed " - $b: $err\n"
346 + } else {
347 + set x [lsearch -sorted -exact $all_heads $b]
348 + if {$x >= 0} {
349 + set all_heads [lreplace $all_heads $x $x]
350 + }
351 + }
352 + }
353 +
354 + if {$failed ne {}} {
355 + tk_messageBox \
356 + -icon error \
357 + -type ok \
358 + -title [wm title $w] \
359 + -parent $w \
360 + -message "Failed to delete branches:\n$failed"
361 + }
362 +
363 + set all_heads [lsort $all_heads]
364 + populate_branch_menu
365 + destroy $w
366 +}
367 +
368 +proc do_delete_branch {} {
369 + global all_heads tracking_branches current_branch
370 + global delete_branch_checktype delete_branch_head delete_branch_trackinghead
371 +
372 + set w .branch_editor
373 + toplevel $w
374 + wm geometry $w "+[winfo rootx .]+[winfo rooty .]"
375 +
376 + label $w.header -text {Delete Local Branch} \
377 + -font font_uibold
378 + pack $w.header -side top -fill x
379 +
380 + frame $w.buttons
381 + button $w.buttons.create -text Delete \
382 + -command [list do_delete_branch_action $w]
383 + pack $w.buttons.create -side right
384 + button $w.buttons.cancel -text {Cancel} \
385 + -command [list destroy $w]
386 + pack $w.buttons.cancel -side right -padx 5
387 + pack $w.buttons -side bottom -fill x -pady 10 -padx 10
388 +
389 + labelframe $w.list -text {Local Branches}
390 + listbox $w.list.l \
391 + -height 10 \
392 + -width 70 \
393 + -selectmode extended \
394 + -yscrollcommand [list $w.list.sby set]
395 + foreach h $all_heads {
396 + if {$h ne $current_branch} {
397 + $w.list.l insert end $h
398 + }
399 + }
400 + scrollbar $w.list.sby -command [list $w.list.l yview]
401 + pack $w.list.sby -side right -fill y
402 + pack $w.list.l -side left -fill both -expand 1
403 + pack $w.list -fill both -expand 1 -pady 5 -padx 5
404 +
405 + labelframe $w.validate -text {Delete Only If}
406 + radiobutton $w.validate.head_r \
407 + -text {Merged Into Local Branch:} \
408 + -value head \
409 + -variable delete_branch_checktype
410 + eval tk_optionMenu $w.validate.head_m delete_branch_head $all_heads
411 + grid $w.validate.head_r $w.validate.head_m -sticky w
412 + set all_trackings [all_tracking_branches]
413 + if {$all_trackings ne {}} {
414 + set delete_branch_trackinghead [lindex $all_trackings 0]
415 + radiobutton $w.validate.tracking_r \
416 + -text {Merged Into Tracking Branch:} \
417 + -value tracking \
418 + -variable delete_branch_checktype
419 + eval tk_optionMenu $w.validate.tracking_m \
420 + delete_branch_trackinghead \
421 + $all_trackings
422 + grid $w.validate.tracking_r $w.validate.tracking_m -sticky w
423 + }
424 + radiobutton $w.validate.always_r \
425 + -text {Always (Do not perform merge checks)} \
426 + -value always \
427 + -variable delete_branch_checktype
428 + grid $w.validate.always_r -columnspan 2 -sticky w
429 + grid columnconfigure $w.validate 1 -weight 1
430 + pack $w.validate -anchor nw -fill x -pady 5 -padx 5
431 +
432 + set delete_branch_head $current_branch
433 + set delete_branch_checktype head
434 +
435 + bind $w <Visibility> "grab $w; focus $w"
436 + bind $w <Key-Escape> "destroy $w"
437 + wm title $w "[appname] ([reponame]): Delete Branch"
438 + tkwait window $w
439 +}
440 +
441 +proc switch_branch {new_branch} {
442 + global HEAD commit_type current_branch repo_config
443 +
444 + if {![lock_index switch]} return
445 +
446 + # -- Our in memory state should match the repository.
447 + #
448 + repository_state curType curHEAD curMERGE_HEAD
449 + if {[string match amend* $commit_type]
450 + && $curType eq {normal}
451 + && $curHEAD eq $HEAD} {
452 + } elseif {$commit_type ne $curType || $HEAD ne $curHEAD} {
453 + info_popup {Last scanned state does not match repository state.
454 +
455 +Another Git program has modified this repository since the last scan. A rescan must be performed before the current branch can be changed.
456 +
457 +The rescan will be automatically started now.
458 +}
459 + unlock_index
460 + rescan {set ui_status_value {Ready.}}
461 + return
462 + }
463 +
464 + # -- Don't do a pointless switch.
465 + #
466 + if {$current_branch eq $new_branch} {
467 + unlock_index
468 + return
469 + }
470 +
471 + if {$repo_config(gui.trustmtime) eq {true}} {
472 + switch_branch_stage2 {} $new_branch
473 + } else {
474 + set ui_status_value {Refreshing file status...}
475 + set cmd [list git update-index]
476 + lappend cmd -q
477 + lappend cmd --unmerged
478 + lappend cmd --ignore-missing
479 + lappend cmd --refresh
480 + set fd_rf [open "| $cmd" r]
481 + fconfigure $fd_rf -blocking 0 -translation binary
482 + fileevent $fd_rf readable \
483 + [list switch_branch_stage2 $fd_rf $new_branch]
484 + }
485 +}
486 +
487 +proc switch_branch_stage2 {fd_rf new_branch} {
488 + global ui_status_value HEAD
489 +
490 + if {$fd_rf ne {}} {
491 + read $fd_rf
492 + if {![eof $fd_rf]} return
493 + close $fd_rf
494 + }
495 +
496 + set ui_status_value "Updating working directory to '$new_branch'..."
497 + set cmd [list git read-tree]
498 + lappend cmd -m
499 + lappend cmd -u
500 + lappend cmd --exclude-per-directory=.gitignore
501 + lappend cmd $HEAD
502 + lappend cmd $new_branch
503 + set fd_rt [open "| $cmd" r]
504 + fconfigure $fd_rt -blocking 0 -translation binary
505 + fileevent $fd_rt readable \
506 + [list switch_branch_readtree_wait $fd_rt $new_branch]
507 +}
508 +
509 +proc switch_branch_readtree_wait {fd_rt new_branch} {
510 + global selected_commit_type commit_type HEAD MERGE_HEAD PARENT
511 + global current_branch
512 + global ui_comm ui_status_value
513 +
514 + # -- We never get interesting output on stdout; only stderr.
515 + #
516 + read $fd_rt
517 + fconfigure $fd_rt -blocking 1
518 + if {![eof $fd_rt]} {
519 + fconfigure $fd_rt -blocking 0
520 + return
521 + }
522 +
523 + # -- The working directory wasn't in sync with the index and
524 + # we'd have to overwrite something to make the switch. A
525 + # merge is required.
526 + #
527 + if {[catch {close $fd_rt} err]} {
528 + regsub {^fatal: } $err {} err
529 + warn_popup "File level merge required.
530 +
531 +$err
532 +
533 +Staying on branch '$current_branch'."
534 + set ui_status_value "Aborted checkout of '$new_branch' (file level merging is required)."
535 + unlock_index
536 + return
537 + }
538 +
539 + # -- Update the symbolic ref. Core git doesn't even check for failure
540 + # here, it Just Works(tm). If it doesn't we are in some really ugly
541 + # state that is difficult to recover from within git-gui.
542 + #
543 + if {[catch {git symbolic-ref HEAD "refs/heads/$new_branch"} err]} {
544 + error_popup "Failed to set current branch.
545 +
546 +This working directory is only partially switched. We successfully updated your files, but failed to update an internal Git file.
547 +
548 +This should not have occurred. [appname] will now close and give up.
549 +
550 +$err"
551 + do_quit
552 + return
553 + }
554 +
555 + # -- Update our repository state. If we were previously in amend mode
556 + # we need to toss the current buffer and do a full rescan to update
557 + # our file lists. If we weren't in amend mode our file lists are
558 + # accurate and we can avoid the rescan.
559 + #
560 + unlock_index
561 + set selected_commit_type new
562 + if {[string match amend* $commit_type]} {
563 + $ui_comm delete 0.0 end
564 + $ui_comm edit reset
565 + $ui_comm edit modified false
566 + rescan {set ui_status_value "Checked out branch '$current_branch'."}
567 + } else {
568 + repository_state commit_type HEAD MERGE_HEAD
569 + set PARENT $HEAD
570 + set ui_status_value "Checked out branch '$current_branch'."
571 + }
572 +}
lib/browser.tcl new
+263
@@ -0,0 +1,263 @@
1 +# git-gui tree browser
2 +# Copyright (C) 2006, 2007 Shawn Pearce
3 +
4 +set next_browser_id 0
5 +
6 +proc new_browser {commit} {
7 + global next_browser_id cursor_ptr M1B
8 + global browser_commit browser_status browser_stack browser_path browser_busy
9 +
10 + if {[winfo ismapped .]} {
11 + set w .browser[incr next_browser_id]
12 + set tl $w
13 + toplevel $w
14 + } else {
15 + set w {}
16 + set tl .
17 + }
18 + set w_list $w.list.l
19 + set browser_commit($w_list) $commit
20 + set browser_status($w_list) {Starting...}
21 + set browser_stack($w_list) {}
22 + set browser_path($w_list) $browser_commit($w_list):
23 + set browser_busy($w_list) 1
24 +
25 + label $w.path -textvariable browser_path($w_list) \
26 + -anchor w \
27 + -justify left \
28 + -borderwidth 1 \
29 + -relief sunken \
30 + -font font_uibold
31 + pack $w.path -anchor w -side top -fill x
32 +
33 + frame $w.list
34 + text $w_list -background white -borderwidth 0 \
35 + -cursor $cursor_ptr \
36 + -state disabled \
37 + -wrap none \
38 + -height 20 \
39 + -width 70 \
40 + -xscrollcommand [list $w.list.sbx set] \
41 + -yscrollcommand [list $w.list.sby set]
42 + $w_list tag conf in_sel \
43 + -background [$w_list cget -foreground] \
44 + -foreground [$w_list cget -background]
45 + scrollbar $w.list.sbx -orient h -command [list $w_list xview]
46 + scrollbar $w.list.sby -orient v -command [list $w_list yview]
47 + pack $w.list.sbx -side bottom -fill x
48 + pack $w.list.sby -side right -fill y
49 + pack $w_list -side left -fill both -expand 1
50 + pack $w.list -side top -fill both -expand 1
51 +
52 + label $w.status -textvariable browser_status($w_list) \
53 + -anchor w \
54 + -justify left \
55 + -borderwidth 1 \
56 + -relief sunken
57 + pack $w.status -anchor w -side bottom -fill x
58 +
59 + bind $w_list <Button-1> "browser_click 0 $w_list @%x,%y;break"
60 + bind $w_list <Double-Button-1> "browser_click 1 $w_list @%x,%y;break"
61 + bind $w_list <$M1B-Up> "browser_parent $w_list;break"
62 + bind $w_list <$M1B-Left> "browser_parent $w_list;break"
63 + bind $w_list <Up> "browser_move -1 $w_list;break"
64 + bind $w_list <Down> "browser_move 1 $w_list;break"
65 + bind $w_list <$M1B-Right> "browser_enter $w_list;break"
66 + bind $w_list <Return> "browser_enter $w_list;break"
67 + bind $w_list <Prior> "browser_page -1 $w_list;break"
68 + bind $w_list <Next> "browser_page 1 $w_list;break"
69 + bind $w_list <Left> break
70 + bind $w_list <Right> break
71 +
72 + bind $tl <Visibility> "focus $w"
73 + bind $tl <Destroy> "
74 + array unset browser_buffer $w_list
75 + array unset browser_files $w_list
76 + array unset browser_status $w_list
77 + array unset browser_stack $w_list
78 + array unset browser_path $w_list
79 + array unset browser_commit $w_list
80 + array unset browser_busy $w_list
81 + "
82 + wm title $tl "[appname] ([reponame]): File Browser"
83 + ls_tree $w_list $browser_commit($w_list) {}
84 +}
85 +
86 +proc browser_move {dir w} {
87 + global browser_files browser_busy
88 +
89 + if {$browser_busy($w)} return
90 + set lno [lindex [split [$w index in_sel.first] .] 0]
91 + incr lno $dir
92 + if {[lindex $browser_files($w) [expr {$lno - 1}]] ne {}} {
93 + $w tag remove in_sel 0.0 end
94 + $w tag add in_sel $lno.0 [expr {$lno + 1}].0
95 + $w see $lno.0
96 + }
97 +}
98 +
99 +proc browser_page {dir w} {
100 + global browser_files browser_busy
101 +
102 + if {$browser_busy($w)} return
103 + $w yview scroll $dir pages
104 + set lno [expr {int(
105 + [lindex [$w yview] 0]
106 + * [llength $browser_files($w)]
107 + + 1)}]
108 + if {[lindex $browser_files($w) [expr {$lno - 1}]] ne {}} {
109 + $w tag remove in_sel 0.0 end
110 + $w tag add in_sel $lno.0 [expr {$lno + 1}].0
111 + $w see $lno.0
112 + }
113 +}
114 +
115 +proc browser_parent {w} {
116 + global browser_files browser_status browser_path
117 + global browser_stack browser_busy
118 +
119 + if {$browser_busy($w)} return
120 + set info [lindex $browser_files($w) 0]
121 + if {[lindex $info 0] eq {parent}} {
122 + set parent [lindex $browser_stack($w) end-1]
123 + set browser_stack($w) [lrange $browser_stack($w) 0 end-2]
124 + if {$browser_stack($w) eq {}} {
125 + regsub {:.*$} $browser_path($w) {:} browser_path($w)
126 + } else {
127 + regsub {/[^/]+$} $browser_path($w) {} browser_path($w)
128 + }
129 + set browser_status($w) "Loading $browser_path($w)..."
130 + ls_tree $w [lindex $parent 0] [lindex $parent 1]
131 + }
132 +}
133 +
134 +proc browser_enter {w} {
135 + global browser_files browser_status browser_path
136 + global browser_commit browser_stack browser_busy
137 +
138 + if {$browser_busy($w)} return
139 + set lno [lindex [split [$w index in_sel.first] .] 0]
140 + set info [lindex $browser_files($w) [expr {$lno - 1}]]
141 + if {$info ne {}} {
142 + switch -- [lindex $info 0] {
143 + parent {
144 + browser_parent $w
145 + }
146 + tree {
147 + set name [lindex $info 2]
148 + set escn [escape_path $name]
149 + set browser_status($w) "Loading $escn..."
150 + append browser_path($w) $escn
151 + ls_tree $w [lindex $info 1] $name
152 + }
153 + blob {
154 + set name [lindex $info 2]
155 + set p {}
156 + foreach n $browser_stack($w) {
157 + append p [lindex $n 1]
158 + }
159 + append p $name
160 + show_blame $browser_commit($w) $p
161 + }
162 + }
163 + }
164 +}
165 +
166 +proc browser_click {was_double_click w pos} {
167 + global browser_files browser_busy
168 +
169 + if {$browser_busy($w)} return
170 + set lno [lindex [split [$w index $pos] .] 0]
171 + focus $w
172 +
173 + if {[lindex $browser_files($w) [expr {$lno - 1}]] ne {}} {
174 + $w tag remove in_sel 0.0 end
175 + $w tag add in_sel $lno.0 [expr {$lno + 1}].0
176 + if {$was_double_click} {
177 + browser_enter $w
178 + }
179 + }
180 +}
181 +
182 +proc ls_tree {w tree_id name} {
183 + global browser_buffer browser_files browser_stack browser_busy
184 +
185 + set browser_buffer($w) {}
186 + set browser_files($w) {}
187 + set browser_busy($w) 1
188 +
189 + $w conf -state normal
190 + $w tag remove in_sel 0.0 end
191 + $w delete 0.0 end
192 + if {$browser_stack($w) ne {}} {
193 + $w image create end \
194 + -align center -padx 5 -pady 1 \
195 + -name icon0 \
196 + -image file_uplevel
197 + $w insert end {[Up To Parent]}
198 + lappend browser_files($w) parent
199 + }
200 + lappend browser_stack($w) [list $tree_id $name]
201 + $w conf -state disabled
202 +
203 + set cmd [list git ls-tree -z $tree_id]
204 + set fd [open "| $cmd" r]
205 + fconfigure $fd -blocking 0 -translation binary -encoding binary
206 + fileevent $fd readable [list read_ls_tree $fd $w]
207 +}
208 +
209 +proc read_ls_tree {fd w} {
210 + global browser_buffer browser_files browser_status browser_busy
211 +
212 + if {![winfo exists $w]} {
213 + catch {close $fd}
214 + return
215 + }
216 +
217 + append browser_buffer($w) [read $fd]
218 + set pck [split $browser_buffer($w) "\0"]
219 + set browser_buffer($w) [lindex $pck end]
220 +
221 + set n [llength $browser_files($w)]
222 + $w conf -state normal
223 + foreach p [lrange $pck 0 end-1] {
224 + set info [split $p "\t"]
225 + set path [lindex $info 1]
226 + set info [split [lindex $info 0] { }]
227 + set type [lindex $info 1]
228 + set object [lindex $info 2]
229 +
230 + switch -- $type {
231 + blob {
232 + set image file_mod
233 + }
234 + tree {
235 + set image file_dir
236 + append path /
237 + }
238 + default {
239 + set image file_question
240 + }
241 + }
242 +
243 + if {$n > 0} {$w insert end "\n"}
244 + $w image create end \
245 + -align center -padx 5 -pady 1 \
246 + -name icon[incr n] \
247 + -image $image
248 + $w insert end [escape_path $path]
249 + lappend browser_files($w) [list $type $object $path]
250 + }
251 + $w conf -state disabled
252 +
253 + if {[eof $fd]} {
254 + close $fd
255 + set browser_status($w) Ready.
256 + set browser_busy($w) 0
257 + array unset browser_buffer $w
258 + if {$n > 0} {
259 + $w tag add in_sel 1.0 2.0
260 + focus -force $w
261 + }
262 + }
263 +}
lib/commit.tcl new
+410
@@ -0,0 +1,410 @@
1 +# git-gui misc. commit reading/writing support
2 +# Copyright (C) 2006, 2007 Shawn Pearce
3 +
4 +proc load_last_commit {} {
5 + global HEAD PARENT MERGE_HEAD commit_type ui_comm
6 + global repo_config
7 +
8 + if {[llength $PARENT] == 0} {
9 + error_popup {There is nothing to amend.
10 +
11 +You are about to create the initial commit. There is no commit before this to amend.
12 +}
13 + return
14 + }
15 +
16 + repository_state curType curHEAD curMERGE_HEAD
17 + if {$curType eq {merge}} {
18 + error_popup {Cannot amend while merging.
19 +
20 +You are currently in the middle of a merge that has not been fully completed. You cannot amend the prior commit unless you first abort the current merge activity.
21 +}
22 + return
23 + }
24 +
25 + set msg {}
26 + set parents [list]
27 + if {[catch {
28 + set fd [open "| git cat-file commit $curHEAD" r]
29 + fconfigure $fd -encoding binary -translation lf
30 + if {[catch {set enc $repo_config(i18n.commitencoding)}]} {
31 + set enc utf-8
32 + }
33 + while {[gets $fd line] > 0} {
34 + if {[string match {parent *} $line]} {
35 + lappend parents [string range $line 7 end]
36 + } elseif {[string match {encoding *} $line]} {
37 + set enc [string tolower [string range $line 9 end]]
38 + }
39 + }
40 + set msg [encoding convertfrom $enc [read $fd]]
41 + set msg [string trim $msg]
42 + close $fd
43 + } err]} {
44 + error_popup "Error loading commit data for amend:\n\n$err"
45 + return
46 + }
47 +
48 + set HEAD $curHEAD
49 + set PARENT $parents
50 + set MERGE_HEAD [list]
51 + switch -- [llength $parents] {
52 + 0 {set commit_type amend-initial}
53 + 1 {set commit_type amend}
54 + default {set commit_type amend-merge}
55 + }
56 +
57 + $ui_comm delete 0.0 end
58 + $ui_comm insert end $msg
59 + $ui_comm edit reset
60 + $ui_comm edit modified false
61 + rescan {set ui_status_value {Ready.}}
62 +}
63 +
64 +set GIT_COMMITTER_IDENT {}
65 +
66 +proc committer_ident {} {
67 + global GIT_COMMITTER_IDENT
68 +
69 + if {$GIT_COMMITTER_IDENT eq {}} {
70 + if {[catch {set me [git var GIT_COMMITTER_IDENT]} err]} {
71 + error_popup "Unable to obtain your identity:\n\n$err"
72 + return {}
73 + }
74 + if {![regexp {^(.*) [0-9]+ [-+0-9]+$} \
75 + $me me GIT_COMMITTER_IDENT]} {
76 + error_popup "Invalid GIT_COMMITTER_IDENT:\n\n$me"
77 + return {}
78 + }
79 + }
80 +
81 + return $GIT_COMMITTER_IDENT
82 +}
83 +
84 +proc do_signoff {} {
85 + global ui_comm
86 +
87 + set me [committer_ident]
88 + if {$me eq {}} return
89 +
90 + set sob "Signed-off-by: $me"
91 + set last [$ui_comm get {end -1c linestart} {end -1c}]
92 + if {$last ne $sob} {
93 + $ui_comm edit separator
94 + if {$last ne {}
95 + && ![regexp {^[A-Z][A-Za-z]*-[A-Za-z-]+: *} $last]} {
96 + $ui_comm insert end "\n"
97 + }
98 + $ui_comm insert end "\n$sob"
99 + $ui_comm edit separator
100 + $ui_comm see end
101 + }
102 +}
103 +
104 +proc create_new_commit {} {
105 + global commit_type ui_comm
106 +
107 + set commit_type normal
108 + $ui_comm delete 0.0 end
109 + $ui_comm edit reset
110 + $ui_comm edit modified false
111 + rescan {set ui_status_value {Ready.}}
112 +}
113 +
114 +proc commit_tree {} {
115 + global HEAD commit_type file_states ui_comm repo_config
116 + global ui_status_value pch_error
117 +
118 + if {[committer_ident] eq {}} return
119 + if {![lock_index update]} return
120 +
121 + # -- Our in memory state should match the repository.
122 + #
123 + repository_state curType curHEAD curMERGE_HEAD
124 + if {[string match amend* $commit_type]
125 + && $curType eq {normal}
126 + && $curHEAD eq $HEAD} {
127 + } elseif {$commit_type ne $curType || $HEAD ne $curHEAD} {
128 + info_popup {Last scanned state does not match repository state.
129 +
130 +Another Git program has modified this repository since the last scan. A rescan must be performed before another commit can be created.
131 +
132 +The rescan will be automatically started now.
133 +}
134 + unlock_index
135 + rescan {set ui_status_value {Ready.}}
136 + return
137 + }
138 +
139 + # -- At least one file should differ in the index.
140 + #
141 + set files_ready 0
142 + foreach path [array names file_states] {
143 + switch -glob -- [lindex $file_states($path) 0] {
144 + _? {continue}
145 + A? -
146 + D? -
147 + M? {set files_ready 1}
148 + U? {
149 + error_popup "Unmerged files cannot be committed.
150 +
151 +File [short_path $path] has merge conflicts. You must resolve them and add the file before committing.
152 +"
153 + unlock_index
154 + return
155 + }
156 + default {
157 + error_popup "Unknown file state [lindex $s 0] detected.
158 +
159 +File [short_path $path] cannot be committed by this program.
160 +"
161 + }
162 + }
163 + }
164 + if {!$files_ready && ![string match *merge $curType]} {
165 + info_popup {No changes to commit.
166 +
167 +You must add at least 1 file before you can commit.
168 +}
169 + unlock_index
170 + return
171 + }
172 +
173 + # -- A message is required.
174 + #
175 + set msg [string trim [$ui_comm get 1.0 end]]
176 + regsub -all -line {[ \t\r]+$} $msg {} msg
177 + if {$msg eq {}} {
178 + error_popup {Please supply a commit message.
179 +
180 +A good commit message has the following format:
181 +
182 +- First line: Describe in one sentance what you did.
183 +- Second line: Blank
184 +- Remaining lines: Describe why this change is good.
185 +}
186 + unlock_index
187 + return
188 + }
189 +
190 + # -- Run the pre-commit hook.
191 + #
192 + set pchook [gitdir hooks pre-commit]
193 +
194 + # On Cygwin [file executable] might lie so we need to ask
195 + # the shell if the hook is executable. Yes that's annoying.
196 + #
197 + if {[is_Cygwin] && [file isfile $pchook]} {
198 + set pchook [list sh -c [concat \
199 + "if test -x \"$pchook\";" \
200 + "then exec \"$pchook\" 2>&1;" \
201 + "fi"]]
202 + } elseif {[file executable $pchook]} {
203 + set pchook [list $pchook |& cat]
204 + } else {
205 + commit_writetree $curHEAD $msg
206 + return
207 + }
208 +
209 + set ui_status_value {Calling pre-commit hook...}
210 + set pch_error {}
211 + set fd_ph [open "| $pchook" r]
212 + fconfigure $fd_ph -blocking 0 -translation binary
213 + fileevent $fd_ph readable \
214 + [list commit_prehook_wait $fd_ph $curHEAD $msg]
215 +}
216 +
217 +proc commit_prehook_wait {fd_ph curHEAD msg} {
218 + global pch_error ui_status_value
219 +
220 + append pch_error [read $fd_ph]
221 + fconfigure $fd_ph -blocking 1
222 + if {[eof $fd_ph]} {
223 + if {[catch {close $fd_ph}]} {
224 + set ui_status_value {Commit declined by pre-commit hook.}
225 + hook_failed_popup pre-commit $pch_error
226 + unlock_index
227 + } else {
228 + commit_writetree $curHEAD $msg
229 + }
230 + set pch_error {}
231 + return
232 + }
233 + fconfigure $fd_ph -blocking 0
234 +}
235 +
236 +proc commit_writetree {curHEAD msg} {
237 + global ui_status_value
238 +
239 + set ui_status_value {Committing changes...}
240 + set fd_wt [open "| git write-tree" r]
241 + fileevent $fd_wt readable \
242 + [list commit_committree $fd_wt $curHEAD $msg]
243 +}
244 +
245 +proc commit_committree {fd_wt curHEAD msg} {
246 + global HEAD PARENT MERGE_HEAD commit_type
247 + global all_heads current_branch
248 + global ui_status_value ui_comm selected_commit_type
249 + global file_states selected_paths rescan_active
250 + global repo_config
251 +
252 + gets $fd_wt tree_id
253 + if {$tree_id eq {} || [catch {close $fd_wt} err]} {
254 + error_popup "write-tree failed:\n\n$err"
255 + set ui_status_value {Commit failed.}
256 + unlock_index
257 + return
258 + }
259 +
260 + # -- Verify this wasn't an empty change.
261 + #
262 + if {$commit_type eq {normal}} {
263 + set old_tree [git rev-parse "$PARENT^{tree}"]
264 + if {$tree_id eq $old_tree} {
265 + info_popup {No changes to commit.
266 +
267 +No files were modified by this commit and it was not a merge commit.
268 +
269 +A rescan will be automatically started now.
270 +}
271 + unlock_index
272 + rescan {set ui_status_value {No changes to commit.}}
273 + return
274 + }
275 + }
276 +
277 + # -- Build the message.
278 + #
279 + set msg_p [gitdir COMMIT_EDITMSG]
280 + set msg_wt [open $msg_p w]
281 + if {[catch {set enc $repo_config(i18n.commitencoding)}]} {
282 + set enc utf-8
283 + }
284 + fconfigure $msg_wt -encoding binary -translation binary
285 + puts -nonewline $msg_wt [encoding convertto $enc $msg]
286 + close $msg_wt
287 +
288 + # -- Create the commit.
289 + #
290 + set cmd [list commit-tree $tree_id]
291 + foreach p [concat $PARENT $MERGE_HEAD] {
292 + lappend cmd -p $p
293 + }
294 + lappend cmd <$msg_p
295 + if {[catch {set cmt_id [eval git $cmd]} err]} {
296 + error_popup "commit-tree failed:\n\n$err"
297 + set ui_status_value {Commit failed.}
298 + unlock_index
299 + return
300 + }
301 +
302 + # -- Update the HEAD ref.
303 + #
304 + set reflogm commit
305 + if {$commit_type ne {normal}} {
306 + append reflogm " ($commit_type)"
307 + }
308 + set i [string first "\n" $msg]
309 + if {$i >= 0} {
310 + set subject [string range $msg 0 [expr {$i - 1}]]
311 + } else {
312 + set subject $msg
313 + }
314 + append reflogm {: } $subject
315 + if {[catch {
316 + git update-ref -m $reflogm HEAD $cmt_id $curHEAD
317 + } err]} {
318 + error_popup "update-ref failed:\n\n$err"
319 + set ui_status_value {Commit failed.}
320 + unlock_index
321 + return
322 + }
323 +
324 + # -- Cleanup after ourselves.
325 + #
326 + catch {file delete $msg_p}
327 + catch {file delete [gitdir MERGE_HEAD]}
328 + catch {file delete [gitdir MERGE_MSG]}
329 + catch {file delete [gitdir SQUASH_MSG]}
330 + catch {file delete [gitdir GITGUI_MSG]}
331 +
332 + # -- Let rerere do its thing.
333 + #
334 + if {[file isdirectory [gitdir rr-cache]]} {
335 + catch {git rerere}
336 + }
337 +
338 + # -- Run the post-commit hook.
339 + #
340 + set pchook [gitdir hooks post-commit]
341 + if {[is_Cygwin] && [file isfile $pchook]} {
342 + set pchook [list sh -c [concat \
343 + "if test -x \"$pchook\";" \
344 + "then exec \"$pchook\";" \
345 + "fi"]]
346 + } elseif {![file executable $pchook]} {
347 + set pchook {}
348 + }
349 + if {$pchook ne {}} {
350 + catch {exec $pchook &}
351 + }
352 +
353 + $ui_comm delete 0.0 end
354 + $ui_comm edit reset
355 + $ui_comm edit modified false
356 +
357 + if {[is_enabled singlecommit]} do_quit
358 +
359 + # -- Make sure our current branch exists.
360 + #
361 + if {$commit_type eq {initial}} {
362 + lappend all_heads $current_branch
363 + set all_heads [lsort -unique $all_heads]
364 + populate_branch_menu
365 + }
366 +
367 + # -- Update in memory status
368 + #
369 + set selected_commit_type new
370 + set commit_type normal
371 + set HEAD $cmt_id
372 + set PARENT $cmt_id
373 + set MERGE_HEAD [list]
374 +
375 + foreach path [array names file_states] {
376 + set s $file_states($path)
377 + set m [lindex $s 0]
378 + switch -glob -- $m {
379 + _O -
380 + _M -
381 + _D {continue}
382 + __ -
383 + A_ -
384 + M_ -
385 + D_ {
386 + unset file_states($path)
387 + catch {unset selected_paths($path)}
388 + }
389 + DO {
390 + set file_states($path) [list _O [lindex $s 1] {} {}]
391 + }
392 + AM -
393 + AD -
394 + MM -
395 + MD {
396 + set file_states($path) [list \
397 + _[string index $m 1] \
398 + [lindex $s 1] \
399 + [lindex $s 3] \
400 + {}]
401 + }
402 + }
403 + }
404 +
405 + display_all_files
406 + unlock_index
407 + reshow_diff
408 + set ui_status_value \
409 + "Created commit [string range $cmt_id 0 7]: $subject"
410 +}
lib/console.tcl new
+185
@@ -0,0 +1,185 @@
1 +# git-gui console support
2 +# Copyright (C) 2006, 2007 Shawn Pearce
3 +
4 +set next_console_id 0
5 +
6 +proc new_console {short_title long_title} {
7 + global next_console_id console_data
8 + set w .console[incr next_console_id]
9 + set console_data($w) [list $short_title $long_title]
10 + return [console_init $w]
11 +}
12 +
13 +proc console_init {w} {
14 + global console_cr console_data M1B
15 +
16 + set console_cr($w) 1.0
17 + toplevel $w
18 + frame $w.m
19 + label $w.m.l1 -text "[lindex $console_data($w) 1]:" \
20 + -anchor w \
21 + -justify left \
22 + -font font_uibold
23 + text $w.m.t \
24 + -background white -borderwidth 1 \
25 + -relief sunken \
26 + -width 80 -height 10 \
27 + -font font_diff \
28 + -state disabled \
29 + -yscrollcommand [list $w.m.sby set]
30 + label $w.m.s -text {Working... please wait...} \
31 + -anchor w \
32 + -justify left \
33 + -font font_uibold
34 + scrollbar $w.m.sby -command [list $w.m.t yview]
35 + pack $w.m.l1 -side top -fill x
36 + pack $w.m.s -side bottom -fill x
37 + pack $w.m.sby -side right -fill y
38 + pack $w.m.t -side left -fill both -expand 1
39 + pack $w.m -side top -fill both -expand 1 -padx 5 -pady 10
40 +
41 + menu $w.ctxm -tearoff 0
42 + $w.ctxm add command -label "Copy" \
43 + -command "tk_textCopy $w.m.t"
44 + $w.ctxm add command -label "Select All" \
45 + -command "focus $w.m.t;$w.m.t tag add sel 0.0 end"
46 + $w.ctxm add command -label "Copy All" \
47 + -command "
48 + $w.m.t tag add sel 0.0 end
49 + tk_textCopy $w.m.t
50 + $w.m.t tag remove sel 0.0 end
51 + "
52 +
53 + button $w.ok -text {Close} \
54 + -state disabled \
55 + -command "destroy $w"
56 + pack $w.ok -side bottom -anchor e -pady 10 -padx 10
57 +
58 + bind_button3 $w.m.t "tk_popup $w.ctxm %X %Y"
59 + bind $w.m.t <$M1B-Key-a> "$w.m.t tag add sel 0.0 end;break"
60 + bind $w.m.t <$M1B-Key-A> "$w.m.t tag add sel 0.0 end;break"
61 + bind $w <Visibility> "focus $w"
62 + wm title $w "[appname] ([reponame]): [lindex $console_data($w) 0]"
63 + return $w
64 +}
65 +
66 +proc console_exec {w cmd after} {
67 + # -- Cygwin's Tcl tosses the enviroment when we exec our child.
68 + # But most users need that so we have to relogin. :-(
69 + #
70 + if {[is_Cygwin]} {
71 + set cmd [list sh --login -c "cd \"[pwd]\" && [join $cmd { }]"]
72 + }
73 +
74 + # -- Tcl won't let us redirect both stdout and stderr to
75 + # the same pipe. So pass it through cat...
76 + #
77 + set cmd [concat | $cmd |& cat]
78 +
79 + set fd_f [open $cmd r]
80 + fconfigure $fd_f -blocking 0 -translation binary
81 + fileevent $fd_f readable [list console_read $w $fd_f $after]
82 +}
83 +
84 +proc console_read {w fd after} {
85 + global console_cr
86 +
87 + set buf [read $fd]
88 + if {$buf ne {}} {
89 + if {![winfo exists $w]} {console_init $w}
90 + $w.m.t conf -state normal
91 + set c 0
92 + set n [string length $buf]
93 + while {$c < $n} {
94 + set cr [string first "\r" $buf $c]
95 + set lf [string first "\n" $buf $c]
96 + if {$cr < 0} {set cr [expr {$n + 1}]}
97 + if {$lf < 0} {set lf [expr {$n + 1}]}
98 +
99 + if {$lf < $cr} {
100 + $w.m.t insert end [string range $buf $c $lf]
101 + set console_cr($w) [$w.m.t index {end -1c}]
102 + set c $lf
103 + incr c
104 + } else {
105 + $w.m.t delete $console_cr($w) end
106 + $w.m.t insert end "\n"
107 + $w.m.t insert end [string range $buf $c $cr]
108 + set c $cr
109 + incr c
110 + }
111 + }
112 + $w.m.t conf -state disabled
113 + $w.m.t see end
114 + }
115 +
116 + fconfigure $fd -blocking 1
117 + if {[eof $fd]} {
118 + if {[catch {close $fd}]} {
119 + set ok 0
120 + } else {
121 + set ok 1
122 + }
123 + uplevel #0 $after $w $ok
124 + return
125 + }
126 + fconfigure $fd -blocking 0
127 +}
128 +
129 +proc console_chain {cmdlist w {ok 1}} {
130 + if {$ok} {
131 + if {[llength $cmdlist] == 0} {
132 + console_done $w $ok
133 + return
134 + }
135 +
136 + set cmd [lindex $cmdlist 0]
137 + set cmdlist [lrange $cmdlist 1 end]
138 +
139 + if {[lindex $cmd 0] eq {console_exec}} {
140 + console_exec $w \
141 + [lindex $cmd 1] \
142 + [list console_chain $cmdlist]
143 + } else {
144 + uplevel #0 $cmd $cmdlist $w $ok
145 + }
146 + } else {
147 + console_done $w $ok
148 + }
149 +}
150 +
151 +proc console_done {args} {
152 + global console_cr console_data
153 +
154 + switch -- [llength $args] {
155 + 2 {
156 + set w [lindex $args 0]
157 + set ok [lindex $args 1]
158 + }
159 + 3 {
160 + set w [lindex $args 1]
161 + set ok [lindex $args 2]
162 + }
163 + default {
164 + error "wrong number of args: console_done ?ignored? w ok"
165 + }
166 + }
167 +
168 + if {$ok} {
169 + if {[winfo exists $w]} {
170 + $w.m.s conf -background green -text {Success}
171 + $w.ok conf -state normal
172 + focus $w.ok
173 + }
174 + } else {
175 + if {![winfo exists $w]} {
176 + console_init $w
177 + }
178 + $w.m.s conf -background red -text {Error: Command Failed}
179 + $w.ok conf -state normal
180 + focus $w.ok
181 + }
182 +
183 + array unset console_cr $w
184 + array unset console_data $w
185 +}
lib/database.tcl new
+89
@@ -0,0 +1,89 @@
1 +# git-gui object database management support
2 +# Copyright (C) 2006, 2007 Shawn Pearce
3 +
4 +proc do_stats {} {
5 + set fd [open "| git count-objects -v" r]
6 + while {[gets $fd line] > 0} {
7 + if {[regexp {^([^:]+): (\d+)$} $line _ name value]} {
8 + set stats($name) $value
9 + }
10 + }
11 + close $fd
12 +
13 + set packed_sz 0
14 + foreach p [glob -directory [gitdir objects pack] \
15 + -type f \
16 + -nocomplain -- *] {
17 + incr packed_sz [file size $p]
18 + }
19 + if {$packed_sz > 0} {
20 + set stats(size-pack) [expr {$packed_sz / 1024}]
21 + }
22 +
23 + set w .stats_view
24 + toplevel $w
25 + wm geometry $w "+[winfo rootx .]+[winfo rooty .]"
26 +
27 + label $w.header -text {Database Statistics}
28 + pack $w.header -side top -fill x
29 +
30 + frame $w.buttons -border 1
31 + button $w.buttons.close -text Close \
32 + -default active \
33 + -command [list destroy $w]
34 + button $w.buttons.gc -text {Compress Database} \
35 + -default normal \
36 + -command "destroy $w;do_gc"
37 + pack $w.buttons.close -side right
38 + pack $w.buttons.gc -side left
39 + pack $w.buttons -side bottom -fill x -pady 10 -padx 10
40 +
41 + frame $w.stat -borderwidth 1 -relief solid
42 + foreach s {
43 + {count {Number of loose objects}}
44 + {size {Disk space used by loose objects} { KiB}}
45 + {in-pack {Number of packed objects}}
46 + {packs {Number of packs}}
47 + {size-pack {Disk space used by packed objects} { KiB}}
48 + {prune-packable {Packed objects waiting for pruning}}
49 + {garbage {Garbage files}}
50 + } {
51 + set name [lindex $s 0]
52 + set label [lindex $s 1]
53 + if {[catch {set value $stats($name)}]} continue
54 + if {[llength $s] > 2} {
55 + set value "$value[lindex $s 2]"
56 + }
57 +
58 + label $w.stat.l_$name -text "$label:" -anchor w
59 + label $w.stat.v_$name -text $value -anchor w
60 + grid $w.stat.l_$name $w.stat.v_$name -sticky we -padx {0 5}
61 + }
62 + pack $w.stat -pady 10 -padx 10
63 +
64 + bind $w <Visibility> "grab $w; focus $w.buttons.close"
65 + bind $w <Key-Escape> [list destroy $w]
66 + bind $w <Key-Return> [list destroy $w]
67 + wm title $w "[appname] ([reponame]): Database Statistics"
68 + tkwait window $w
69 +}
70 +
71 +proc do_gc {} {
72 + set w [new_console {gc} {Compressing the object database}]
73 + console_chain {
74 + {console_exec {git pack-refs --prune}}
75 + {console_exec {git reflog expire --all}}
76 + {console_exec {git repack -a -d -l}}
77 + {console_exec {git rerere gc}}
78 + } $w
79 +}
80 +
81 +proc do_fsck_objects {} {
82 + set w [new_console {fsck-objects} \
83 + {Verifying the object database with fsck-objects}]
84 + set cmd [list git fsck-objects]
85 + lappend cmd --full
86 + lappend cmd --cache
87 + lappend cmd --strict
88 + console_exec $w $cmd console_done
89 +}
lib/diff.tcl new
+336
@@ -0,0 +1,336 @@
1 +# git-gui diff viewer
2 +# Copyright (C) 2006, 2007 Shawn Pearce
3 +
4 +proc clear_diff {} {
5 + global ui_diff current_diff_path current_diff_header
6 + global ui_index ui_workdir
7 +
8 + $ui_diff conf -state normal
9 + $ui_diff delete 0.0 end
10 + $ui_diff conf -state disabled
11 +
12 + set current_diff_path {}
13 + set current_diff_header {}
14 +
15 + $ui_index tag remove in_diff 0.0 end
16 + $ui_workdir tag remove in_diff 0.0 end
17 +}
18 +
19 +proc reshow_diff {} {
20 + global ui_status_value file_states file_lists
21 + global current_diff_path current_diff_side
22 +
23 + set p $current_diff_path
24 + if {$p eq {}} {
25 + # No diff is being shown.
26 + } elseif {$current_diff_side eq {}
27 + || [catch {set s $file_states($p)}]
28 + || [lsearch -sorted -exact $file_lists($current_diff_side) $p] == -1} {
29 + clear_diff
30 + } else {
31 + show_diff $p $current_diff_side
32 + }
33 +}
34 +
35 +proc handle_empty_diff {} {
36 + global current_diff_path file_states file_lists
37 +
38 + set path $current_diff_path
39 + set s $file_states($path)
40 + if {[lindex $s 0] ne {_M}} return
41 +
42 + info_popup "No differences detected.
43 +
44 +[short_path $path] has no changes.
45 +
46 +The modification date of this file was updated by another application, but the content within the file was not changed.
47 +
48 +A rescan will be automatically started to find other files which may have the same state."
49 +
50 + clear_diff
51 + display_file $path __
52 + rescan {set ui_status_value {Ready.}} 0
53 +}
54 +
55 +proc show_diff {path w {lno {}}} {
56 + global file_states file_lists
57 + global is_3way_diff diff_active repo_config
58 + global ui_diff ui_status_value ui_index ui_workdir
59 + global current_diff_path current_diff_side current_diff_header
60 +
61 + if {$diff_active || ![lock_index read]} return
62 +
63 + clear_diff
64 + if {$lno == {}} {
65 + set lno [lsearch -sorted -exact $file_lists($w) $path]
66 + if {$lno >= 0} {
67 + incr lno
68 + }
69 + }
70 + if {$lno >= 1} {
71 + $w tag add in_diff $lno.0 [expr {$lno + 1}].0
72 + }
73 +
74 + set s $file_states($path)
75 + set m [lindex $s 0]
76 + set is_3way_diff 0
77 + set diff_active 1
78 + set current_diff_path $path
79 + set current_diff_side $w
80 + set current_diff_header {}
81 + set ui_status_value "Loading diff of [escape_path $path]..."
82 +
83 + # - Git won't give us the diff, there's nothing to compare to!
84 + #
85 + if {$m eq {_O}} {
86 + set max_sz [expr {128 * 1024}]
87 + if {[catch {
88 + set fd [open $path r]
89 + set content [read $fd $max_sz]
90 + close $fd
91 + set sz [file size $path]
92 + } err ]} {
93 + set diff_active 0
94 + unlock_index
95 + set ui_status_value "Unable to display [escape_path $path]"
96 + error_popup "Error loading file:\n\n$err"
97 + return
98 + }
99 + $ui_diff conf -state normal
100 + if {![catch {set type [exec file $path]}]} {
101 + set n [string length $path]
102 + if {[string equal -length $n $path $type]} {
103 + set type [string range $type $n end]
104 + regsub {^:?\s*} $type {} type
105 + }
106 + $ui_diff insert end "* $type\n" d_@
107 + }
108 + if {[string first "\0" $content] != -1} {
109 + $ui_diff insert end \
110 + "* Binary file (not showing content)." \
111 + d_@
112 + } else {
113 + if {$sz > $max_sz} {
114 + $ui_diff insert end \
115 +"* Untracked file is $sz bytes.
116 +* Showing only first $max_sz bytes.
117 +" d_@
118 + }
119 + $ui_diff insert end $content
120 + if {$sz > $max_sz} {
121 + $ui_diff insert end "
122 +* Untracked file clipped here by [appname].
123 +* To see the entire file, use an external editor.
124 +" d_@
125 + }
126 + }
127 + $ui_diff conf -state disabled
128 + set diff_active 0
129 + unlock_index
130 + set ui_status_value {Ready.}
131 + return
132 + }
133 +
134 + set cmd [list | git]
135 + if {$w eq $ui_index} {
136 + lappend cmd diff-index
137 + lappend cmd --cached
138 + } elseif {$w eq $ui_workdir} {
139 + if {[string index $m 0] eq {U}} {
140 + lappend cmd diff
141 + } else {
142 + lappend cmd diff-files
143 + }
144 + }
145 +
146 + lappend cmd -p
147 + lappend cmd --no-color
148 + if {$repo_config(gui.diffcontext) > 0} {
149 + lappend cmd "-U$repo_config(gui.diffcontext)"
150 + }
151 + if {$w eq $ui_index} {
152 + lappend cmd [PARENT]
153 + }
154 + lappend cmd --
155 + lappend cmd $path
156 +
157 + if {[catch {set fd [open $cmd r]} err]} {
158 + set diff_active 0
159 + unlock_index
160 + set ui_status_value "Unable to display [escape_path $path]"
161 + error_popup "Error loading diff:\n\n$err"
162 + return
163 + }
164 +
165 + fconfigure $fd \
166 + -blocking 0 \
167 + -encoding binary \
168 + -translation binary
169 + fileevent $fd readable [list read_diff $fd]
170 +}
171 +
172 +proc read_diff {fd} {
173 + global ui_diff ui_status_value diff_active
174 + global is_3way_diff current_diff_header
175 +
176 + $ui_diff conf -state normal
177 + while {[gets $fd line] >= 0} {
178 + # -- Cleanup uninteresting diff header lines.
179 + #
180 + if { [string match {diff --git *} $line]
181 + || [string match {diff --cc *} $line]
182 + || [string match {diff --combined *} $line]
183 + || [string match {--- *} $line]
184 + || [string match {+++ *} $line]} {
185 + append current_diff_header $line "\n"
186 + continue
187 + }
188 + if {[string match {index *} $line]} continue
189 + if {$line eq {deleted file mode 120000}} {
190 + set line "deleted symlink"
191 + }
192 +
193 + # -- Automatically detect if this is a 3 way diff.
194 + #
195 + if {[string match {@@@ *} $line]} {set is_3way_diff 1}
196 +
197 + if {[string match {mode *} $line]
198 + || [string match {new file *} $line]
199 + || [string match {deleted file *} $line]
200 + || [string match {Binary files * and * differ} $line]
201 + || $line eq {\ No newline at end of file}
202 + || [regexp {^\* Unmerged path } $line]} {
203 + set tags {}
204 + } elseif {$is_3way_diff} {
205 + set op [string range $line 0 1]
206 + switch -- $op {
207 + { } {set tags {}}
208 + {@@} {set tags d_@}
209 + { +} {set tags d_s+}
210 + { -} {set tags d_s-}
211 + {+ } {set tags d_+s}
212 + {- } {set tags d_-s}
213 + {--} {set tags d_--}
214 + {++} {
215 + if {[regexp {^\+\+([<>]{7} |={7})} $line _g op]} {
216 + set line [string replace $line 0 1 { }]
217 + set tags d$op
218 + } else {
219 + set tags d_++
220 + }
221 + }
222 + default {
223 + puts "error: Unhandled 3 way diff marker: {$op}"
224 + set tags {}
225 + }
226 + }
227 + } else {
228 + set op [string index $line 0]
229 + switch -- $op {
230 + { } {set tags {}}
231 + {@} {set tags d_@}
232 + {-} {set tags d_-}
233 + {+} {
234 + if {[regexp {^\+([<>]{7} |={7})} $line _g op]} {
235 + set line [string replace $line 0 0 { }]
236 + set tags d$op
237 + } else {
238 + set tags d_+
239 + }
240 + }
241 + default {
242 + puts "error: Unhandled 2 way diff marker: {$op}"
243 + set tags {}
244 + }
245 + }
246 + }
247 + $ui_diff insert end $line $tags
248 + if {[string index $line end] eq "\r"} {
249 + $ui_diff tag add d_cr {end - 2c}
250 + }
251 + $ui_diff insert end "\n" $tags
252 + }
253 + $ui_diff conf -state disabled
254 +
255 + if {[eof $fd]} {
256 + close $fd
257 + set diff_active 0
258 + unlock_index
259 + set ui_status_value {Ready.}
260 +
261 + if {[$ui_diff index end] eq {2.0}} {
262 + handle_empty_diff
263 + }
264 + }
265 +}
266 +
267 +proc apply_hunk {x y} {
268 + global current_diff_path current_diff_header current_diff_side
269 + global ui_diff ui_index file_states
270 +
271 + if {$current_diff_path eq {} || $current_diff_header eq {}} return
272 + if {![lock_index apply_hunk]} return
273 +
274 + set apply_cmd {git apply --cached --whitespace=nowarn}
275 + set mi [lindex $file_states($current_diff_path) 0]
276 + if {$current_diff_side eq $ui_index} {
277 + set mode unstage
278 + lappend apply_cmd --reverse
279 + if {[string index $mi 0] ne {M}} {
280 + unlock_index
281 + return
282 + }
283 + } else {
284 + set mode stage
285 + if {[string index $mi 1] ne {M}} {
286 + unlock_index
287 + return
288 + }
289 + }
290 +
291 + set s_lno [lindex [split [$ui_diff index @$x,$y] .] 0]
292 + set s_lno [$ui_diff search -backwards -regexp ^@@ $s_lno.0 0.0]
293 + if {$s_lno eq {}} {
294 + unlock_index
295 + return
296 + }
297 +
298 + set e_lno [$ui_diff search -forwards -regexp ^@@ "$s_lno + 1 lines" end]
299 + if {$e_lno eq {}} {
300 + set e_lno end
301 + }
302 +
303 + if {[catch {
304 + set p [open "| $apply_cmd" w]
305 + fconfigure $p -translation binary -encoding binary
306 + puts -nonewline $p $current_diff_header
307 + puts -nonewline $p [$ui_diff get $s_lno $e_lno]
308 + close $p} err]} {
309 + error_popup "Failed to $mode selected hunk.\n\n$err"
310 + unlock_index
311 + return
312 + }
313 +
314 + $ui_diff conf -state normal
315 + $ui_diff delete $s_lno $e_lno
316 + $ui_diff conf -state disabled
317 +
318 + if {[$ui_diff get 1.0 end] eq "\n"} {
319 + set o _
320 + } else {
321 + set o ?
322 + }
323 +
324 + if {$current_diff_side eq $ui_index} {
325 + set mi ${o}M
326 + } elseif {[string index $mi 0] eq {_}} {
327 + set mi M$o
328 + } else {
329 + set mi ?$o
330 + }
331 + unlock_index
332 + display_file $current_diff_path $mi
333 + if {$o eq {_}} {
334 + clear_diff
335 + }
336 +}
lib/error.tcl new
+101
@@ -0,0 +1,101 @@
1 +# git-gui branch (create/delete) support
2 +# Copyright (C) 2006, 2007 Shawn Pearce
3 +
4 +proc error_popup {msg} {
5 + set title [appname]
6 + if {[reponame] ne {}} {
7 + append title " ([reponame])"
8 + }
9 + set cmd [list tk_messageBox \
10 + -icon error \
11 + -type ok \
12 + -title "$title: error" \
13 + -message $msg]
14 + if {[winfo ismapped .]} {
15 + lappend cmd -parent .
16 + }
17 + eval $cmd
18 +}
19 +
20 +proc warn_popup {msg} {
21 + set title [appname]
22 + if {[reponame] ne {}} {
23 + append title " ([reponame])"
24 + }
25 + set cmd [list tk_messageBox \
26 + -icon warning \
27 + -type ok \
28 + -title "$title: warning" \
29 + -message $msg]
30 + if {[winfo ismapped .]} {
31 + lappend cmd -parent .
32 + }
33 + eval $cmd
34 +}
35 +
36 +proc info_popup {msg {parent .}} {
37 + set title [appname]
38 + if {[reponame] ne {}} {
39 + append title " ([reponame])"
40 + }
41 + tk_messageBox \
42 + -parent $parent \
43 + -icon info \
44 + -type ok \
45 + -title $title \
46 + -message $msg
47 +}
48 +
49 +proc ask_popup {msg} {
50 + set title [appname]
51 + if {[reponame] ne {}} {
52 + append title " ([reponame])"
53 + }
54 + return [tk_messageBox \
55 + -parent . \
56 + -icon question \
57 + -type yesno \
58 + -title $title \
59 + -message $msg]
60 +}
61 +
62 +proc hook_failed_popup {hook msg} {
63 + set w .hookfail
64 + toplevel $w
65 +
66 + frame $w.m
67 + label $w.m.l1 -text "$hook hook failed:" \
68 + -anchor w \
69 + -justify left \
70 + -font font_uibold
71 + text $w.m.t \
72 + -background white -borderwidth 1 \
73 + -relief sunken \
74 + -width 80 -height 10 \
75 + -font font_diff \
76 + -yscrollcommand [list $w.m.sby set]
77 + label $w.m.l2 \
78 + -text {You must correct the above errors before committing.} \
79 + -anchor w \
80 + -justify left \
81 + -font font_uibold
82 + scrollbar $w.m.sby -command [list $w.m.t yview]
83 + pack $w.m.l1 -side top -fill x
84 + pack $w.m.l2 -side bottom -fill x
85 + pack $w.m.sby -side right -fill y
86 + pack $w.m.t -side left -fill both -expand 1
87 + pack $w.m -side top -fill both -expand 1 -padx 5 -pady 10
88 +
89 + $w.m.t insert 1.0 $msg
90 + $w.m.t conf -state disabled
91 +
92 + button $w.ok -text OK \
93 + -width 15 \
94 + -command "destroy $w"
95 + pack $w.ok -side bottom -anchor e -pady 10 -padx 10
96 +
97 + bind $w <Visibility> "grab $w; focus $w"
98 + bind $w <Key-Return> "destroy $w"
99 + wm title $w "[appname] ([reponame]): error"
100 + tkwait window $w
101 +}
lib/index.tcl new
+409
@@ -0,0 +1,409 @@
1 +# git-gui index (add/remove) support
2 +# Copyright (C) 2006, 2007 Shawn Pearce
3 +
4 +proc update_indexinfo {msg pathList after} {
5 + global update_index_cp ui_status_value
6 +
7 + if {![lock_index update]} return
8 +
9 + set update_index_cp 0
10 + set pathList [lsort $pathList]
11 + set totalCnt [llength $pathList]
12 + set batch [expr {int($totalCnt * .01) + 1}]
13 + if {$batch > 25} {set batch 25}
14 +
15 + set ui_status_value [format \
16 + "$msg... %i/%i files (%.2f%%)" \
17 + $update_index_cp \
18 + $totalCnt \
19 + 0.0]
20 + set fd [open "| git update-index -z --index-info" w]
21 + fconfigure $fd \
22 + -blocking 0 \
23 + -buffering full \
24 + -buffersize 512 \
25 + -encoding binary \
26 + -translation binary
27 + fileevent $fd writable [list \
28 + write_update_indexinfo \
29 + $fd \
30 + $pathList \
31 + $totalCnt \
32 + $batch \
33 + $msg \
34 + $after \
35 + ]
36 +}
37 +
38 +proc write_update_indexinfo {fd pathList totalCnt batch msg after} {
39 + global update_index_cp ui_status_value
40 + global file_states current_diff_path
41 +
42 + if {$update_index_cp >= $totalCnt} {
43 + close $fd
44 + unlock_index
45 + uplevel #0 $after
46 + return
47 + }
48 +
49 + for {set i $batch} \
50 + {$update_index_cp < $totalCnt && $i > 0} \
51 + {incr i -1} {
52 + set path [lindex $pathList $update_index_cp]
53 + incr update_index_cp
54 +
55 + set s $file_states($path)
56 + switch -glob -- [lindex $s 0] {
57 + A? {set new _O}
58 + M? {set new _M}
59 + D_ {set new _D}
60 + D? {set new _?}
61 + ?? {continue}
62 + }
63 + set info [lindex $s 2]
64 + if {$info eq {}} continue
65 +
66 + puts -nonewline $fd "$info\t[encoding convertto $path]\0"
67 + display_file $path $new
68 + }
69 +
70 + set ui_status_value [format \
71 + "$msg... %i/%i files (%.2f%%)" \
72 + $update_index_cp \
73 + $totalCnt \
74 + [expr {100.0 * $update_index_cp / $totalCnt}]]
75 +}
76 +
77 +proc update_index {msg pathList after} {
78 + global update_index_cp ui_status_value
79 +
80 + if {![lock_index update]} return
81 +
82 + set update_index_cp 0
83 + set pathList [lsort $pathList]
84 + set totalCnt [llength $pathList]
85 + set batch [expr {int($totalCnt * .01) + 1}]
86 + if {$batch > 25} {set batch 25}
87 +
88 + set ui_status_value [format \
89 + "$msg... %i/%i files (%.2f%%)" \
90 + $update_index_cp \
91 + $totalCnt \
92 + 0.0]
93 + set fd [open "| git update-index --add --remove -z --stdin" w]
94 + fconfigure $fd \
95 + -blocking 0 \
96 + -buffering full \
97 + -buffersize 512 \
98 + -encoding binary \
99 + -translation binary
100 + fileevent $fd writable [list \
101 + write_update_index \
102 + $fd \
103 + $pathList \
104 + $totalCnt \
105 + $batch \
106 + $msg \
107 + $after \
108 + ]
109 +}
110 +
111 +proc write_update_index {fd pathList totalCnt batch msg after} {
112 + global update_index_cp ui_status_value
113 + global file_states current_diff_path
114 +
115 + if {$update_index_cp >= $totalCnt} {
116 + close $fd
117 + unlock_index
118 + uplevel #0 $after
119 + return
120 + }
121 +
122 + for {set i $batch} \
123 + {$update_index_cp < $totalCnt && $i > 0} \
124 + {incr i -1} {
125 + set path [lindex $pathList $update_index_cp]
126 + incr update_index_cp
127 +
128 + switch -glob -- [lindex $file_states($path) 0] {
129 + AD {set new __}
130 + ?D {set new D_}
131 + _O -
132 + AM {set new A_}
133 + U? {
134 + if {[file exists $path]} {
135 + set new M_
136 + } else {
137 + set new D_
138 + }
139 + }
140 + ?M {set new M_}
141 + ?? {continue}
142 + }
143 + puts -nonewline $fd "[encoding convertto $path]\0"
144 + display_file $path $new
145 + }
146 +
147 + set ui_status_value [format \
148 + "$msg... %i/%i files (%.2f%%)" \
149 + $update_index_cp \
150 + $totalCnt \
151 + [expr {100.0 * $update_index_cp / $totalCnt}]]
152 +}
153 +
154 +proc checkout_index {msg pathList after} {
155 + global update_index_cp ui_status_value
156 +
157 + if {![lock_index update]} return
158 +
159 + set update_index_cp 0
160 + set pathList [lsort $pathList]
161 + set totalCnt [llength $pathList]
162 + set batch [expr {int($totalCnt * .01) + 1}]
163 + if {$batch > 25} {set batch 25}
164 +
165 + set ui_status_value [format \
166 + "$msg... %i/%i files (%.2f%%)" \
167 + $update_index_cp \
168 + $totalCnt \
169 + 0.0]
170 + set cmd [list git checkout-index]
171 + lappend cmd --index
172 + lappend cmd --quiet
173 + lappend cmd --force
174 + lappend cmd -z
175 + lappend cmd --stdin
176 + set fd [open "| $cmd " w]
177 + fconfigure $fd \
178 + -blocking 0 \
179 + -buffering full \
180 + -buffersize 512 \
181 + -encoding binary \
182 + -translation binary
183 + fileevent $fd writable [list \
184 + write_checkout_index \
185 + $fd \
186 + $pathList \
187 + $totalCnt \
188 + $batch \
189 + $msg \
190 + $after \
191 + ]
192 +}
193 +
194 +proc write_checkout_index {fd pathList totalCnt batch msg after} {
195 + global update_index_cp ui_status_value
196 + global file_states current_diff_path
197 +
198 + if {$update_index_cp >= $totalCnt} {
199 + close $fd
200 + unlock_index
201 + uplevel #0 $after
202 + return
203 + }
204 +
205 + for {set i $batch} \
206 + {$update_index_cp < $totalCnt && $i > 0} \
207 + {incr i -1} {
208 + set path [lindex $pathList $update_index_cp]
209 + incr update_index_cp
210 + switch -glob -- [lindex $file_states($path) 0] {
211 + U? {continue}
212 + ?M -
213 + ?D {
214 + puts -nonewline $fd "[encoding convertto $path]\0"
215 + display_file $path ?_
216 + }
217 + }
218 + }
219 +
220 + set ui_status_value [format \
221 + "$msg... %i/%i files (%.2f%%)" \
222 + $update_index_cp \
223 + $totalCnt \
224 + [expr {100.0 * $update_index_cp / $totalCnt}]]
225 +}
226 +
227 +proc unstage_helper {txt paths} {
228 + global file_states current_diff_path
229 +
230 + if {![lock_index begin-update]} return
231 +
232 + set pathList [list]
233 + set after {}
234 + foreach path $paths {
235 + switch -glob -- [lindex $file_states($path) 0] {
236 + A? -
237 + M? -
238 + D? {
239 + lappend pathList $path
240 + if {$path eq $current_diff_path} {
241 + set after {reshow_diff;}
242 + }
243 + }
244 + }
245 + }
246 + if {$pathList eq {}} {
247 + unlock_index
248 + } else {
249 + update_indexinfo \
250 + $txt \
251 + $pathList \
252 + [concat $after {set ui_status_value {Ready.}}]
253 + }
254 +}
255 +
256 +proc do_unstage_selection {} {
257 + global current_diff_path selected_paths
258 +
259 + if {[array size selected_paths] > 0} {
260 + unstage_helper \
261 + {Unstaging selected files from commit} \
262 + [array names selected_paths]
263 + } elseif {$current_diff_path ne {}} {
264 + unstage_helper \
265 + "Unstaging [short_path $current_diff_path] from commit" \
266 + [list $current_diff_path]
267 + }
268 +}
269 +
270 +proc add_helper {txt paths} {
271 + global file_states current_diff_path
272 +
273 + if {![lock_index begin-update]} return
274 +
275 + set pathList [list]
276 + set after {}
277 + foreach path $paths {
278 + switch -glob -- [lindex $file_states($path) 0] {
279 + _O -
280 + ?M -
281 + ?D -
282 + U? {
283 + lappend pathList $path
284 + if {$path eq $current_diff_path} {
285 + set after {reshow_diff;}
286 + }
287 + }
288 + }
289 + }
290 + if {$pathList eq {}} {
291 + unlock_index
292 + } else {
293 + update_index \
294 + $txt \
295 + $pathList \
296 + [concat $after {set ui_status_value {Ready to commit.}}]
297 + }
298 +}
299 +
300 +proc do_add_selection {} {
301 + global current_diff_path selected_paths
302 +
303 + if {[array size selected_paths] > 0} {
304 + add_helper \
305 + {Adding selected files} \
306 + [array names selected_paths]
307 + } elseif {$current_diff_path ne {}} {
308 + add_helper \
309 + "Adding [short_path $current_diff_path]" \
310 + [list $current_diff_path]
311 + }
312 +}
313 +
314 +proc do_add_all {} {
315 + global file_states
316 +
317 + set paths [list]
318 + foreach path [array names file_states] {
319 + switch -glob -- [lindex $file_states($path) 0] {
320 + U? {continue}
321 + ?M -
322 + ?D {lappend paths $path}
323 + }
324 + }
325 + add_helper {Adding all changed files} $paths
326 +}
327 +
328 +proc revert_helper {txt paths} {
329 + global file_states current_diff_path
330 +
331 + if {![lock_index begin-update]} return
332 +
333 + set pathList [list]
334 + set after {}
335 + foreach path $paths {
336 + switch -glob -- [lindex $file_states($path) 0] {
337 + U? {continue}
338 + ?M -
339 + ?D {
340 + lappend pathList $path
341 + if {$path eq $current_diff_path} {
342 + set after {reshow_diff;}
343 + }
344 + }
345 + }
346 + }
347 +
348 + set n [llength $pathList]
349 + if {$n == 0} {
350 + unlock_index
351 + return
352 + } elseif {$n == 1} {
353 + set s "[short_path [lindex $pathList]]"
354 + } else {
355 + set s "these $n files"
356 + }
357 +
358 + set reply [tk_dialog \
359 + .confirm_revert \
360 + "[appname] ([reponame])" \
361 + "Revert changes in $s?
362 +
363 +Any unadded changes will be permanently lost by the revert." \
364 + question \
365 + 1 \
366 + {Do Nothing} \
367 + {Revert Changes} \
368 + ]
369 + if {$reply == 1} {
370 + checkout_index \
371 + $txt \
372 + $pathList \
373 + [concat $after {set ui_status_value {Ready.}}]
374 + } else {
375 + unlock_index
376 + }
377 +}
378 +
379 +proc do_revert_selection {} {
380 + global current_diff_path selected_paths
381 +
382 + if {[array size selected_paths] > 0} {
383 + revert_helper \
384 + {Reverting selected files} \
385 + [array names selected_paths]
386 + } elseif {$current_diff_path ne {}} {
387 + revert_helper \
388 + "Reverting [short_path $current_diff_path]" \
389 + [list $current_diff_path]
390 + }
391 +}
392 +
393 +proc do_select_commit_type {} {
394 + global commit_type selected_commit_type
395 +
396 + if {$selected_commit_type eq {new}
397 + && [string match amend* $commit_type]} {
398 + create_new_commit
399 + } elseif {$selected_commit_type eq {amend}
400 + && ![string match amend* $commit_type]} {
401 + load_last_commit
402 +
403 + # The amend request was rejected...
404 + #
405 + if {![string match amend* $commit_type]} {
406 + set selected_commit_type new
407 + }
408 + }
409 +}
lib/merge.tcl new
+281
@@ -0,0 +1,281 @@
1 +# git-gui branch merge support
2 +# Copyright (C) 2006, 2007 Shawn Pearce
3 +
4 +proc can_merge {} {
5 + global HEAD commit_type file_states
6 +
7 + if {[string match amend* $commit_type]} {
8 + info_popup {Cannot merge while amending.
9 +
10 +You must finish amending this commit before starting any type of merge.
11 +}
12 + return 0
13 + }
14 +
15 + if {[committer_ident] eq {}} {return 0}
16 + if {![lock_index merge]} {return 0}
17 +
18 + # -- Our in memory state should match the repository.
19 + #
20 + repository_state curType curHEAD curMERGE_HEAD
21 + if {$commit_type ne $curType || $HEAD ne $curHEAD} {
22 + info_popup {Last scanned state does not match repository state.
23 +
24 +Another Git program has modified this repository since the last scan. A rescan must be performed before a merge can be performed.
25 +
26 +The rescan will be automatically started now.
27 +}
28 + unlock_index
29 + rescan {set ui_status_value {Ready.}}
30 + return 0
31 + }
32 +
33 + foreach path [array names file_states] {
34 + switch -glob -- [lindex $file_states($path) 0] {
35 + _O {
36 + continue; # and pray it works!
37 + }
38 + U? {
39 + error_popup "You are in the middle of a conflicted merge.
40 +
41 +File [short_path $path] has merge conflicts.
42 +
43 +You must resolve them, add the file, and commit to complete the current merge. Only then can you begin another merge.
44 +"
45 + unlock_index
46 + return 0
47 + }
48 + ?? {
49 + error_popup "You are in the middle of a change.
50 +
51 +File [short_path $path] is modified.
52 +
53 +You should complete the current commit before starting a merge. Doing so will help you abort a failed merge, should the need arise.
54 +"
55 + unlock_index
56 + return 0
57 + }
58 + }
59 + }
60 +
61 + return 1
62 +}
63 +
64 +proc visualize_local_merge {w} {
65 + set revs {}
66 + foreach i [$w.source.l curselection] {
67 + lappend revs [$w.source.l get $i]
68 + }
69 + if {$revs eq {}} return
70 + lappend revs --not HEAD
71 + do_gitk $revs
72 +}
73 +
74 +proc start_local_merge_action {w} {
75 + global HEAD ui_status_value current_branch
76 +
77 + set cmd [list git merge]
78 + set names {}
79 + set revcnt 0
80 + foreach i [$w.source.l curselection] {
81 + set b [$w.source.l get $i]
82 + lappend cmd $b
83 + lappend names $b
84 + incr revcnt
85 + }
86 +
87 + if {$revcnt == 0} {
88 + return
89 + } elseif {$revcnt == 1} {
90 + set unit branch
91 + } elseif {$revcnt <= 15} {
92 + set unit branches
93 +
94 + if {[tk_dialog \
95 + $w.confirm_octopus \
96 + [wm title $w] \
97 + "Use octopus merge strategy?
98 +
99 +You are merging $revcnt branches at once. This requires using the octopus merge driver, which may not succeed if there are file-level conflicts.
100 +" \
101 + question \
102 + 0 \
103 + {Cancel} \
104 + {Use octopus} \
105 + ] != 1} return
106 + } else {
107 + tk_messageBox \
108 + -icon error \
109 + -type ok \
110 + -title [wm title $w] \
111 + -parent $w \
112 + -message "Too many branches selected.
113 +
114 +You have requested to merge $revcnt branches in an octopus merge. This exceeds Git's internal limit of 15 branches per merge.
115 +
116 +Please select fewer branches. To merge more than 15 branches, merge the branches in batches.
117 +"
118 + return
119 + }
120 +
121 + set msg "Merging $current_branch, [join $names {, }]"
122 + set ui_status_value "$msg..."
123 + set cons [new_console "Merge" $msg]
124 + console_exec $cons $cmd [list finish_merge $revcnt]
125 + bind $w <Destroy> {}
126 + destroy $w
127 +}
128 +
129 +proc finish_merge {revcnt w ok} {
130 + console_done $w $ok
131 + if {$ok} {
132 + set msg {Merge completed successfully.}
133 + } else {
134 + if {$revcnt != 1} {
135 + info_popup "Octopus merge failed.
136 +
137 +Your merge of $revcnt branches has failed.
138 +
139 +There are file-level conflicts between the branches which must be resolved manually.
140 +
141 +The working directory will now be reset.
142 +
143 +You can attempt this merge again by merging only one branch at a time." $w
144 +
145 + set fd [open "| git read-tree --reset -u HEAD" r]
146 + fconfigure $fd -blocking 0 -translation binary
147 + fileevent $fd readable [list reset_hard_wait $fd]
148 + set ui_status_value {Aborting... please wait...}
149 + return
150 + }
151 +
152 + set msg {Merge failed. Conflict resolution is required.}
153 + }
154 + unlock_index
155 + rescan [list set ui_status_value $msg]
156 +}
157 +
158 +proc do_local_merge {} {
159 + global current_branch
160 +
161 + if {![can_merge]} return
162 +
163 + set w .merge_setup
164 + toplevel $w
165 + wm geometry $w "+[winfo rootx .]+[winfo rooty .]"
166 +
167 + label $w.header \
168 + -text "Merge Into $current_branch" \
169 + -font font_uibold
170 + pack $w.header -side top -fill x
171 +
172 + frame $w.buttons
173 + button $w.buttons.visualize -text Visualize \
174 + -command [list visualize_local_merge $w]
175 + pack $w.buttons.visualize -side left
176 + button $w.buttons.create -text Merge \
177 + -command [list start_local_merge_action $w]
178 + pack $w.buttons.create -side right
179 + button $w.buttons.cancel -text {Cancel} \
180 + -command [list destroy $w]
181 + pack $w.buttons.cancel -side right -padx 5
182 + pack $w.buttons -side bottom -fill x -pady 10 -padx 10
183 +
184 + labelframe $w.source -text {Source Branches}
185 + listbox $w.source.l \
186 + -height 10 \
187 + -width 70 \
188 + -selectmode extended \
189 + -yscrollcommand [list $w.source.sby set]
190 + scrollbar $w.source.sby -command [list $w.source.l yview]
191 + pack $w.source.sby -side right -fill y
192 + pack $w.source.l -side left -fill both -expand 1
193 + pack $w.source -fill both -expand 1 -pady 5 -padx 5
194 +
195 + set cmd [list git for-each-ref]
196 + lappend cmd {--format=%(objectname) %(*objectname) %(refname)}
197 + lappend cmd refs/heads
198 + lappend cmd refs/remotes
199 + lappend cmd refs/tags
200 + set fr_fd [open "| $cmd" r]
201 + fconfigure $fr_fd -translation binary
202 + while {[gets $fr_fd line] > 0} {
203 + set line [split $line { }]
204 + set sha1([lindex $line 0]) [lindex $line 2]
205 + set sha1([lindex $line 1]) [lindex $line 2]
206 + }
207 + close $fr_fd
208 +
209 + set to_show {}
210 + set fr_fd [open "| git rev-list --all --not HEAD"]
211 + while {[gets $fr_fd line] > 0} {
212 + if {[catch {set ref $sha1($line)}]} continue
213 + regsub ^refs/(heads|remotes|tags)/ $ref {} ref
214 + lappend to_show $ref
215 + }
216 + close $fr_fd
217 +
218 + foreach ref [lsort -unique $to_show] {
219 + $w.source.l insert end $ref
220 + }
221 +
222 + bind $w <Visibility> "grab $w"
223 + bind $w <Key-Escape> "unlock_index;destroy $w"
224 + bind $w <Destroy> unlock_index
225 + wm title $w "[appname] ([reponame]): Merge"
226 + tkwait window $w
227 +}
228 +
229 +proc do_reset_hard {} {
230 + global HEAD commit_type file_states
231 +
232 + if {[string match amend* $commit_type]} {
233 + info_popup {Cannot abort while amending.
234 +
235 +You must finish amending this commit.
236 +}
237 + return
238 + }
239 +
240 + if {![lock_index abort]} return
241 +
242 + if {[string match *merge* $commit_type]} {
243 + set op merge
244 + } else {
245 + set op commit
246 + }
247 +
248 + if {[ask_popup "Abort $op?
249 +
250 +Aborting the current $op will cause *ALL* uncommitted changes to be lost.
251 +
252 +Continue with aborting the current $op?"] eq {yes}} {
253 + set fd [open "| git read-tree --reset -u HEAD" r]
254 + fconfigure $fd -blocking 0 -translation binary
255 + fileevent $fd readable [list reset_hard_wait $fd]
256 + set ui_status_value {Aborting... please wait...}
257 + } else {
258 + unlock_index
259 + }
260 +}
261 +
262 +proc reset_hard_wait {fd} {
263 + global ui_comm
264 +
265 + read $fd
266 + if {[eof $fd]} {
267 + close $fd
268 + unlock_index
269 +
270 + $ui_comm delete 0.0 end
271 + $ui_comm edit modified false
272 +
273 + catch {file delete [gitdir MERGE_HEAD]}
274 + catch {file delete [gitdir rr-cache MERGE_RR]}
275 + catch {file delete [gitdir SQUASH_MSG]}
276 + catch {file delete [gitdir MERGE_MSG]}
277 + catch {file delete [gitdir GITGUI_MSG]}
278 +
279 + rescan {set ui_status_value {Abort completed. Ready.}}
280 + }
281 +}
lib/option.tcl new
+290
@@ -0,0 +1,290 @@
1 +# git-gui options editor
2 +# Copyright (C) 2006, 2007 Shawn Pearce
3 +
4 +proc save_config {} {
5 + global default_config font_descs
6 + global repo_config global_config
7 + global repo_config_new global_config_new
8 +
9 + foreach option $font_descs {
10 + set name [lindex $option 0]
11 + set font [lindex $option 1]
12 + font configure $font \
13 + -family $global_config_new(gui.$font^^family) \
14 + -size $global_config_new(gui.$font^^size)
15 + font configure ${font}bold \
16 + -family $global_config_new(gui.$font^^family) \
17 + -size $global_config_new(gui.$font^^size)
18 + set global_config_new(gui.$name) [font configure $font]
19 + unset global_config_new(gui.$font^^family)
20 + unset global_config_new(gui.$font^^size)
21 + }
22 +
23 + foreach name [array names default_config] {
24 + set value $global_config_new($name)
25 + if {$value ne $global_config($name)} {
26 + if {$value eq $default_config($name)} {
27 + catch {git config --global --unset $name}
28 + } else {
29 + regsub -all "\[{}\]" $value {"} value
30 + git config --global $name $value
31 + }
32 + set global_config($name) $value
33 + if {$value eq $repo_config($name)} {
34 + catch {git config --unset $name}
35 + set repo_config($name) $value
36 + }
37 + }
38 + }
39 +
40 + foreach name [array names default_config] {
41 + set value $repo_config_new($name)
42 + if {$value ne $repo_config($name)} {
43 + if {$value eq $global_config($name)} {
44 + catch {git config --unset $name}
45 + } else {
46 + regsub -all "\[{}\]" $value {"} value
47 + git config $name $value
48 + }
49 + set repo_config($name) $value
50 + }
51 + }
52 +}
53 +
54 +proc do_about {} {
55 + global appvers copyright
56 + global tcl_patchLevel tk_patchLevel
57 +
58 + set w .about_dialog
59 + toplevel $w
60 + wm geometry $w "+[winfo rootx .]+[winfo rooty .]"
61 +
62 + label $w.header -text "About [appname]" \
63 + -font font_uibold
64 + pack $w.header -side top -fill x
65 +
66 + frame $w.buttons
67 + button $w.buttons.close -text {Close} \
68 + -default active \
69 + -command [list destroy $w]
70 + pack $w.buttons.close -side right
71 + pack $w.buttons -side bottom -fill x -pady 10 -padx 10
72 +
73 + label $w.desc \
74 + -text "git-gui - a graphical user interface for Git.
75 +$copyright" \
76 + -padx 5 -pady 5 \
77 + -justify left \
78 + -anchor w \
79 + -borderwidth 1 \
80 + -relief solid
81 + pack $w.desc -side top -fill x -padx 5 -pady 5
82 +
83 + set v {}
84 + append v "git-gui version $appvers\n"
85 + append v "[git version]\n"
86 + append v "\n"
87 + if {$tcl_patchLevel eq $tk_patchLevel} {
88 + append v "Tcl/Tk version $tcl_patchLevel"
89 + } else {
90 + append v "Tcl version $tcl_patchLevel"
91 + append v ", Tk version $tk_patchLevel"
92 + }
93 +
94 + label $w.vers \
95 + -text $v \
96 + -padx 5 -pady 5 \
97 + -justify left \
98 + -anchor w \
99 + -borderwidth 1 \
100 + -relief solid
101 + pack $w.vers -side top -fill x -padx 5 -pady 5
102 +
103 + menu $w.ctxm -tearoff 0
104 + $w.ctxm add command \
105 + -label {Copy} \
106 + -command "
107 + clipboard clear
108 + clipboard append -format STRING -type STRING -- \[$w.vers cget -text\]
109 + "
110 +
111 + bind $w <Visibility> "grab $w; focus $w.buttons.close"
112 + bind $w <Key-Escape> "destroy $w"
113 + bind $w <Key-Return> "destroy $w"
114 + bind_button3 $w.vers "tk_popup $w.ctxm %X %Y; grab $w; focus $w"
115 + wm title $w "About [appname]"
116 + tkwait window $w
117 +}
118 +
119 +proc do_options {} {
120 + global repo_config global_config font_descs
121 + global repo_config_new global_config_new
122 +
123 + array unset repo_config_new
124 + array unset global_config_new
125 + foreach name [array names repo_config] {
126 + set repo_config_new($name) $repo_config($name)
127 + }
128 + load_config 1
129 + foreach name [array names repo_config] {
130 + switch -- $name {
131 + gui.diffcontext {continue}
132 + }
133 + set repo_config_new($name) $repo_config($name)
134 + }
135 + foreach name [array names global_config] {
136 + set global_config_new($name) $global_config($name)
137 + }
138 +
139 + set w .options_editor
140 + toplevel $w
141 + wm geometry $w "+[winfo rootx .]+[winfo rooty .]"
142 +
143 + label $w.header -text "Options" \
144 + -font font_uibold
145 + pack $w.header -side top -fill x
146 +
147 + frame $w.buttons
148 + button $w.buttons.restore -text {Restore Defaults} \
149 + -default normal \
150 + -command do_restore_defaults
151 + pack $w.buttons.restore -side left
152 + button $w.buttons.save -text Save \
153 + -default active \
154 + -command [list do_save_config $w]
155 + pack $w.buttons.save -side right
156 + button $w.buttons.cancel -text {Cancel} \
157 + -default normal \
158 + -command [list destroy $w]
159 + pack $w.buttons.cancel -side right -padx 5
160 + pack $w.buttons -side bottom -fill x -pady 10 -padx 10
161 +
162 + labelframe $w.repo -text "[reponame] Repository"
163 + labelframe $w.global -text {Global (All Repositories)}
164 + pack $w.repo -side left -fill both -expand 1 -pady 5 -padx 5
165 + pack $w.global -side right -fill both -expand 1 -pady 5 -padx 5
166 +
167 + set optid 0
168 + foreach option {
169 + {t user.name {User Name}}
170 + {t user.email {Email Address}}
171 +
172 + {b merge.summary {Summarize Merge Commits}}
173 + {i-1..5 merge.verbosity {Merge Verbosity}}
174 +
175 + {b gui.trustmtime {Trust File Modification Timestamps}}
176 + {i-1..99 gui.diffcontext {Number of Diff Context Lines}}
177 + {t gui.newbranchtemplate {New Branch Name Template}}
178 + } {
179 + set type [lindex $option 0]
180 + set name [lindex $option 1]
181 + set text [lindex $option 2]
182 + incr optid
183 + foreach f {repo global} {
184 + switch -glob -- $type {
185 + b {
186 + checkbutton $w.$f.$optid -text $text \
187 + -variable ${f}_config_new($name) \
188 + -onvalue true \
189 + -offvalue false
190 + pack $w.$f.$optid -side top -anchor w
191 + }
192 + i-* {
193 + regexp -- {-(\d+)\.\.(\d+)$} $type _junk min max
194 + frame $w.$f.$optid
195 + label $w.$f.$optid.l -text "$text:"
196 + pack $w.$f.$optid.l -side left -anchor w -fill x
197 + spinbox $w.$f.$optid.v \
198 + -textvariable ${f}_config_new($name) \
199 + -from $min \
200 + -to $max \
201 + -increment 1 \
202 + -width [expr {1 + [string length $max]}]
203 + bind $w.$f.$optid.v <FocusIn> {%W selection range 0 end}
204 + pack $w.$f.$optid.v -side right -anchor e -padx 5
205 + pack $w.$f.$optid -side top -anchor w -fill x
206 + }
207 + t {
208 + frame $w.$f.$optid
209 + label $w.$f.$optid.l -text "$text:"
210 + entry $w.$f.$optid.v \
211 + -borderwidth 1 \
212 + -relief sunken \
213 + -width 20 \
214 + -textvariable ${f}_config_new($name)
215 + pack $w.$f.$optid.l -side left -anchor w
216 + pack $w.$f.$optid.v -side left -anchor w \
217 + -fill x -expand 1 \
218 + -padx 5
219 + pack $w.$f.$optid -side top -anchor w -fill x
220 + }
221 + }
222 + }
223 + }
224 +
225 + set all_fonts [lsort [font families]]
226 + foreach option $font_descs {
227 + set name [lindex $option 0]
228 + set font [lindex $option 1]
229 + set text [lindex $option 2]
230 +
231 + set global_config_new(gui.$font^^family) \
232 + [font configure $font -family]
233 + set global_config_new(gui.$font^^size) \
234 + [font configure $font -size]
235 +
236 + frame $w.global.$name
237 + label $w.global.$name.l -text "$text:"
238 + pack $w.global.$name.l -side left -anchor w -fill x
239 + eval tk_optionMenu $w.global.$name.family \
240 + global_config_new(gui.$font^^family) \
241 + $all_fonts
242 + spinbox $w.global.$name.size \
243 + -textvariable global_config_new(gui.$font^^size) \
244 + -from 2 -to 80 -increment 1 \
245 + -width 3
246 + bind $w.global.$name.size <FocusIn> {%W selection range 0 end}
247 + pack $w.global.$name.size -side right -anchor e
248 + pack $w.global.$name.family -side right -anchor e
249 + pack $w.global.$name -side top -anchor w -fill x
250 + }
251 +
252 + bind $w <Visibility> "grab $w; focus $w.buttons.save"
253 + bind $w <Key-Escape> "destroy $w"
254 + bind $w <Key-Return> [list do_save_config $w]
255 + wm title $w "[appname] ([reponame]): Options"
256 + tkwait window $w
257 +}
258 +
259 +proc do_restore_defaults {} {
260 + global font_descs default_config repo_config
261 + global repo_config_new global_config_new
262 +
263 + foreach name [array names default_config] {
264 + set repo_config_new($name) $default_config($name)
265 + set global_config_new($name) $default_config($name)
266 + }
267 +
268 + foreach option $font_descs {
269 + set name [lindex $option 0]
270 + set repo_config(gui.$name) $default_config(gui.$name)
271 + }
272 + apply_config
273 +
274 + foreach option $font_descs {
275 + set name [lindex $option 0]
276 + set font [lindex $option 1]
277 + set global_config_new(gui.$font^^family) \
278 + [font configure $font -family]
279 + set global_config_new(gui.$font^^size) \
280 + [font configure $font -size]
281 + }
282 +}
283 +
284 +proc do_save_config {w} {
285 + if {[catch {save_config} err]} {
286 + error_popup "Failed to completely save options:\n\n$err"
287 + }
288 + reshow_diff
289 + destroy $w
290 +}
lib/remote.tcl new
+159
@@ -0,0 +1,159 @@
1 +# git-gui remote management
2 +# Copyright (C) 2006, 2007 Shawn Pearce
3 +
4 +proc is_tracking_branch {name} {
5 + global tracking_branches
6 +
7 + if {![catch {set info $tracking_branches($name)}]} {
8 + return 1
9 + }
10 + foreach t [array names tracking_branches] {
11 + if {[string match {*/\*} $t] && [string match $t $name]} {
12 + return 1
13 + }
14 + }
15 + return 0
16 +}
17 +
18 +proc all_tracking_branches {} {
19 + global tracking_branches
20 +
21 + set all_trackings {}
22 + set cmd {}
23 + foreach name [array names tracking_branches] {
24 + if {[regsub {/\*$} $name {} name]} {
25 + lappend cmd $name
26 + } else {
27 + regsub ^refs/(heads|remotes)/ $name {} name
28 + lappend all_trackings $name
29 + }
30 + }
31 +
32 + if {$cmd ne {}} {
33 + set fd [open "| git for-each-ref --format=%(refname) $cmd" r]
34 + while {[gets $fd name] > 0} {
35 + regsub ^refs/(heads|remotes)/ $name {} name
36 + lappend all_trackings $name
37 + }
38 + close $fd
39 + }
40 +
41 + return [lsort -unique $all_trackings]
42 +}
43 +
44 +proc load_all_remotes {} {
45 + global repo_config
46 + global all_remotes tracking_branches
47 +
48 + set all_remotes [list]
49 + array unset tracking_branches
50 +
51 + set rm_dir [gitdir remotes]
52 + if {[file isdirectory $rm_dir]} {
53 + set all_remotes [glob \
54 + -types f \
55 + -tails \
56 + -nocomplain \
57 + -directory $rm_dir *]
58 +
59 + foreach name $all_remotes {
60 + catch {
61 + set fd [open [file join $rm_dir $name] r]
62 + while {[gets $fd line] >= 0} {
63 + if {![regexp {^Pull:[ ]*([^:]+):(.+)$} \
64 + $line line src dst]} continue
65 + if {![regexp ^refs/ $dst]} {
66 + set dst "refs/heads/$dst"
67 + }
68 + set tracking_branches($dst) [list $name $src]
69 + }
70 + close $fd
71 + }
72 + }
73 + }
74 +
75 + foreach line [array names repo_config remote.*.url] {
76 + if {![regexp ^remote\.(.*)\.url\$ $line line name]} continue
77 + lappend all_remotes $name
78 +
79 + if {[catch {set fl $repo_config(remote.$name.fetch)}]} {
80 + set fl {}
81 + }
82 + foreach line $fl {
83 + if {![regexp {^([^:]+):(.+)$} $line line src dst]} continue
84 + if {![regexp ^refs/ $dst]} {
85 + set dst "refs/heads/$dst"
86 + }
87 + set tracking_branches($dst) [list $name $src]
88 + }
89 + }
90 +
91 + set all_remotes [lsort -unique $all_remotes]
92 +}
93 +
94 +proc populate_fetch_menu {} {
95 + global all_remotes repo_config
96 +
97 + set m .mbar.fetch
98 + foreach r $all_remotes {
99 + set enable 0
100 + if {![catch {set a $repo_config(remote.$r.url)}]} {
101 + if {![catch {set a $repo_config(remote.$r.fetch)}]} {
102 + set enable 1
103 + }
104 + } else {
105 + catch {
106 + set fd [open [gitdir remotes $r] r]
107 + while {[gets $fd n] >= 0} {
108 + if {[regexp {^Pull:[ \t]*([^:]+):} $n]} {
109 + set enable 1
110 + break
111 + }
112 + }
113 + close $fd
114 + }
115 + }
116 +
117 + if {$enable} {
118 + $m add command \
119 + -label "Fetch from $r..." \
120 + -command [list fetch_from $r]
121 + }
122 + }
123 +}
124 +
125 +proc populate_push_menu {} {
126 + global all_remotes repo_config
127 +
128 + set m .mbar.push
129 + set fast_count 0
130 + foreach r $all_remotes {
131 + set enable 0
132 + if {![catch {set a $repo_config(remote.$r.url)}]} {
133 + if {![catch {set a $repo_config(remote.$r.push)}]} {
134 + set enable 1
135 + }
136 + } else {
137 + catch {
138 + set fd [open [gitdir remotes $r] r]
139 + while {[gets $fd n] >= 0} {
140 + if {[regexp {^Push:[ \t]*([^:]+):} $n]} {
141 + set enable 1
142 + break
143 + }
144 + }
145 + close $fd
146 + }
147 + }
148 +
149 + if {$enable} {
150 + if {!$fast_count} {
151 + $m add separator
152 + }
153 + $m add command \
154 + -label "Push to $r..." \
155 + -command [list push_to $r]
156 + incr fast_count
157 + }
158 + }
159 +}
lib/shortcut.tcl new
+141
@@ -0,0 +1,141 @@
1 +# git-gui desktop icon creators
2 +# Copyright (C) 2006, 2007 Shawn Pearce
3 +
4 +proc do_windows_shortcut {} {
5 + global argv0
6 +
7 + set fn [tk_getSaveFile \
8 + -parent . \
9 + -title "[appname] ([reponame]): Create Desktop Icon" \
10 + -initialfile "Git [reponame].bat"]
11 + if {$fn != {}} {
12 + if {[catch {
13 + set fd [open $fn w]
14 + puts $fd "@ECHO Entering [reponame]"
15 + puts $fd "@ECHO Starting git-gui... please wait..."
16 + puts $fd "@SET PATH=[file normalize [gitexec]];%PATH%"
17 + puts $fd "@SET GIT_DIR=[file normalize [gitdir]]"
18 + puts -nonewline $fd "@\"[info nameofexecutable]\""
19 + puts $fd " \"[file normalize $argv0]\""
20 + close $fd
21 + } err]} {
22 + error_popup "Cannot write script:\n\n$err"
23 + }
24 + }
25 +}
26 +
27 +proc do_cygwin_shortcut {} {
28 + global argv0
29 +
30 + if {[catch {
31 + set desktop [exec cygpath \
32 + --windows \
33 + --absolute \
34 + --long-name \
35 + --desktop]
36 + }]} {
37 + set desktop .
38 + }
39 + set fn [tk_getSaveFile \
40 + -parent . \
41 + -title "[appname] ([reponame]): Create Desktop Icon" \
42 + -initialdir $desktop \
43 + -initialfile "Git [reponame].bat"]
44 + if {$fn != {}} {
45 + if {[catch {
46 + set fd [open $fn w]
47 + set sh [exec cygpath \
48 + --windows \
49 + --absolute \
50 + /bin/sh]
51 + set me [exec cygpath \
52 + --unix \
53 + --absolute \
54 + $argv0]
55 + set gd [exec cygpath \
56 + --unix \
57 + --absolute \
58 + [gitdir]]
59 + set gw [exec cygpath \
60 + --windows \
61 + --absolute \
62 + [file dirname [gitdir]]]
63 + regsub -all ' $me "'\\''" me
64 + regsub -all ' $gd "'\\''" gd
65 + puts $fd "@ECHO Entering $gw"
66 + puts $fd "@ECHO Starting git-gui... please wait..."
67 + puts -nonewline $fd "@\"$sh\" --login -c \""
68 + puts -nonewline $fd "GIT_DIR='$gd'"
69 + puts -nonewline $fd " '$me'"
70 + puts $fd "&\""
71 + close $fd
72 + } err]} {
73 + error_popup "Cannot write script:\n\n$err"
74 + }
75 + }
76 +}
77 +
78 +proc do_macosx_app {} {
79 + global argv0 env
80 +
81 + set fn [tk_getSaveFile \
82 + -parent . \
83 + -title "[appname] ([reponame]): Create Desktop Icon" \
84 + -initialdir [file join $env(HOME) Desktop] \
85 + -initialfile "Git [reponame].app"]
86 + if {$fn != {}} {
87 + if {[catch {
88 + set Contents [file join $fn Contents]
89 + set MacOS [file join $Contents MacOS]
90 + set exe [file join $MacOS git-gui]
91 +
92 + file mkdir $MacOS
93 +
94 + set fd [open [file join $Contents Info.plist] w]
95 + puts $fd {<?xml version="1.0" encoding="UTF-8"?>
96 +<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
97 +<plist version="1.0">
98 +<dict>
99 + <key>CFBundleDevelopmentRegion</key>
100 + <string>English</string>
101 + <key>CFBundleExecutable</key>
102 + <string>git-gui</string>
103 + <key>CFBundleIdentifier</key>
104 + <string>org.spearce.git-gui</string>
105 + <key>CFBundleInfoDictionaryVersion</key>
106 + <string>6.0</string>
107 + <key>CFBundlePackageType</key>
108 + <string>APPL</string>
109 + <key>CFBundleSignature</key>
110 + <string>????</string>
111 + <key>CFBundleVersion</key>
112 + <string>1.0</string>
113 + <key>NSPrincipalClass</key>
114 + <string>NSApplication</string>
115 +</dict>
116 +</plist>}
117 + close $fd
118 +
119 + set fd [open $exe w]
120 + set gd [file normalize [gitdir]]
121 + set ep [file normalize [gitexec]]
122 + regsub -all ' $gd "'\\''" gd
123 + regsub -all ' $ep "'\\''" ep
124 + puts $fd "#!/bin/sh"
125 + foreach name [array names env] {
126 + if {[string match GIT_* $name]} {
127 + regsub -all ' $env($name) "'\\''" v
128 + puts $fd "export $name='$v'"
129 + }
130 + }
131 + puts $fd "export PATH='$ep':\$PATH"
132 + puts $fd "export GIT_DIR='$gd'"
133 + puts $fd "exec [file normalize $argv0]"
134 + close $fd
135 +
136 + file attributes $exe -permissions u+x,g+x,o+x
137 + } err]} {
138 + error_popup "Cannot write icon:\n\n$err"
139 + }
140 + }
141 +}
lib/transport.tcl new
+162
@@ -0,0 +1,162 @@
1 +# git-gui transport (fetch/push) support
2 +# Copyright (C) 2006, 2007 Shawn Pearce
3 +
4 +proc fetch_from {remote} {
5 + set w [new_console \
6 + "fetch $remote" \
7 + "Fetching new changes from $remote"]
8 + set cmd [list git fetch]
9 + lappend cmd $remote
10 + console_exec $w $cmd console_done
11 +}
12 +
13 +proc push_to {remote} {
14 + set w [new_console \
15 + "push $remote" \
16 + "Pushing changes to $remote"]
17 + set cmd [list git push]
18 + lappend cmd -v
19 + lappend cmd $remote
20 + console_exec $w $cmd console_done
21 +}
22 +
23 +proc start_push_anywhere_action {w} {
24 + global push_urltype push_remote push_url push_thin push_tags
25 +
26 + set r_url {}
27 + switch -- $push_urltype {
28 + remote {set r_url $push_remote}
29 + url {set r_url $push_url}
30 + }
31 + if {$r_url eq {}} return
32 +
33 + set cmd [list git push]
34 + lappend cmd -v
35 + if {$push_thin} {
36 + lappend cmd --thin
37 + }
38 + if {$push_tags} {
39 + lappend cmd --tags
40 + }
41 + lappend cmd $r_url
42 + set cnt 0
43 + foreach i [$w.source.l curselection] {
44 + set b [$w.source.l get $i]
45 + lappend cmd "refs/heads/$b:refs/heads/$b"
46 + incr cnt
47 + }
48 + if {$cnt == 0} {
49 + return
50 + } elseif {$cnt == 1} {
51 + set unit branch
52 + } else {
53 + set unit branches
54 + }
55 +
56 + set cons [new_console "push $r_url" "Pushing $cnt $unit to $r_url"]
57 + console_exec $cons $cmd console_done
58 + destroy $w
59 +}
60 +
61 +trace add variable push_remote write \
62 + [list radio_selector push_urltype remote]
63 +
64 +proc do_push_anywhere {} {
65 + global all_heads all_remotes current_branch
66 + global push_urltype push_remote push_url push_thin push_tags
67 +
68 + set w .push_setup
69 + toplevel $w
70 + wm geometry $w "+[winfo rootx .]+[winfo rooty .]"
71 +
72 + label $w.header -text {Push Branches} -font font_uibold
73 + pack $w.header -side top -fill x
74 +
75 + frame $w.buttons
76 + button $w.buttons.create -text Push \
77 + -default active \
78 + -command [list start_push_anywhere_action $w]
79 + pack $w.buttons.create -side right
80 + button $w.buttons.cancel -text {Cancel} \
81 + -default normal \
82 + -command [list destroy $w]
83 + pack $w.buttons.cancel -side right -padx 5
84 + pack $w.buttons -side bottom -fill x -pady 10 -padx 10
85 +
86 + labelframe $w.source -text {Source Branches}
87 + listbox $w.source.l \
88 + -height 10 \
89 + -width 70 \
90 + -selectmode extended \
91 + -yscrollcommand [list $w.source.sby set]
92 + foreach h $all_heads {
93 + $w.source.l insert end $h
94 + if {$h eq $current_branch} {
95 + $w.source.l select set end
96 + }
97 + }
98 + scrollbar $w.source.sby -command [list $w.source.l yview]
99 + pack $w.source.sby -side right -fill y
100 + pack $w.source.l -side left -fill both -expand 1
101 + pack $w.source -fill both -expand 1 -pady 5 -padx 5
102 +
103 + labelframe $w.dest -text {Destination Repository}
104 + if {$all_remotes ne {}} {
105 + radiobutton $w.dest.remote_r \
106 + -text {Remote:} \
107 + -value remote \
108 + -variable push_urltype
109 + eval tk_optionMenu $w.dest.remote_m push_remote $all_remotes
110 + grid $w.dest.remote_r $w.dest.remote_m -sticky w
111 + if {[lsearch -sorted -exact $all_remotes origin] != -1} {
112 + set push_remote origin
113 + } else {
114 + set push_remote [lindex $all_remotes 0]
115 + }
116 + set push_urltype remote
117 + } else {
118 + set push_urltype url
119 + }
120 + radiobutton $w.dest.url_r \
121 + -text {Arbitrary URL:} \
122 + -value url \
123 + -variable push_urltype
124 + entry $w.dest.url_t \
125 + -borderwidth 1 \
126 + -relief sunken \
127 + -width 50 \
128 + -textvariable push_url \
129 + -validate key \
130 + -validatecommand {
131 + if {%d == 1 && [regexp {\s} %S]} {return 0}
132 + if {%d == 1 && [string length %S] > 0} {
133 + set push_urltype url
134 + }
135 + return 1
136 + }
137 + grid $w.dest.url_r $w.dest.url_t -sticky we -padx {0 5}
138 + grid columnconfigure $w.dest 1 -weight 1
139 + pack $w.dest -anchor nw -fill x -pady 5 -padx 5
140 +
141 + labelframe $w.options -text {Transfer Options}
142 + checkbutton $w.options.thin \
143 + -text {Use thin pack (for slow network connections)} \
144 + -variable push_thin
145 + grid $w.options.thin -columnspan 2 -sticky w
146 + checkbutton $w.options.tags \
147 + -text {Include tags} \
148 + -variable push_tags
149 + grid $w.options.tags -columnspan 2 -sticky w
150 + grid columnconfigure $w.options 1 -weight 1
151 + pack $w.options -anchor nw -fill x -pady 5 -padx 5
152 +
153 + set push_url {}
154 + set push_thin 0
155 + set push_tags 0
156 +
157 + bind $w <Visibility> "grab $w; focus $w.buttons.create"
158 + bind $w <Key-Escape> "destroy $w"
159 + bind $w <Key-Return> [list start_push_anywhere_action $w]
160 + wm title $w "[appname] ([reponame]): Push"
161 + tkwait window $w
162 +}