git-gui: open the staged content of a file in the editor
[git-gui/bertw.git] / git-gui.sh
blobc6717070f4eff8b198e4a9221e31b071564db816
1 #!/bin/sh
2 # Tcl ignores the next line -*- tcl -*- \
3 if test "z$*" = zversion \
4 || test "z$*" = z--version; \
5 then \
6 echo 'git-gui version @@GITGUI_VERSION@@'; \
7 exit; \
8 fi; \
9 argv0=$0; \
10 exec wish "$argv0" -- "$@"
12 set appvers {@@GITGUI_VERSION@@}
13 set copyright [string map [list (c) \u00a9] {
14 Copyright (c) 2006-2010 Shawn Pearce, et. al.
16 This program is free software; you can redistribute it and/or modify
17 it under the terms of the GNU General Public License as published by
18 the Free Software Foundation; either version 2 of the License, or
19 (at your option) any later version.
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public License
27 along with this program; if not, write to the Free Software
28 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA}]
30 ######################################################################
32 ## Tcl/Tk sanity check
34 if {[catch {package require Tcl 8.4} err]
35 || [catch {package require Tk 8.4} err]
36 } {
37 catch {wm withdraw .}
38 tk_messageBox \
39 -icon error \
40 -type ok \
41 -title "git-gui: fatal error" \
42 -message $err
43 exit 1
46 catch {rename send {}} ; # What an evil concept...
48 ######################################################################
50 ## locate our library
52 set oguilib {@@GITGUI_LIBDIR@@}
53 set oguirel {@@GITGUI_RELATIVE@@}
54 if {$oguirel eq {1}} {
55 set oguilib [file dirname [file normalize $argv0]]
56 if {[file tail $oguilib] eq {git-core}} {
57 set oguilib [file dirname $oguilib]
59 set oguilib [file dirname $oguilib]
60 set oguilib [file join $oguilib share git-gui lib]
61 set oguimsg [file join $oguilib msgs]
62 } elseif {[string match @@* $oguirel]} {
63 set oguilib [file join [file dirname [file normalize $argv0]] lib]
64 set oguimsg [file join [file dirname [file normalize $argv0]] po]
65 } else {
66 set oguimsg [file join $oguilib msgs]
68 unset oguirel
70 ######################################################################
72 ## enable verbose loading?
74 if {![catch {set _verbose $env(GITGUI_VERBOSE)}]} {
75 unset _verbose
76 rename auto_load real__auto_load
77 proc auto_load {name args} {
78 puts stderr "auto_load $name"
79 return [uplevel 1 real__auto_load $name $args]
81 rename source real__source
82 proc source {name} {
83 puts stderr "source $name"
84 uplevel 1 real__source $name
86 if {[tk windowingsystem] eq "win32"} { console show }
89 ######################################################################
91 ## Internationalization (i18n) through msgcat and gettext. See
92 ## http://www.gnu.org/software/gettext/manual/html_node/Tcl.html
94 package require msgcat
96 # Check for Windows 7 MUI language pack (missed by msgcat < 1.4.4)
97 if {[tk windowingsystem] eq "win32"
98 && [package vcompare [package provide msgcat] 1.4.4] < 0
99 } then {
100 proc _mc_update_locale {} {
101 set key {HKEY_CURRENT_USER\Control Panel\Desktop}
102 if {![catch {
103 package require registry
104 set uilocale [registry get $key "PreferredUILanguages"]
105 msgcat::ConvertLocale [string map {- _} [lindex $uilocale 0]]
106 } uilocale]} {
107 if {[string length $uilocale] > 0} {
108 msgcat::mclocale $uilocale
112 _mc_update_locale
115 proc _mc_trim {fmt} {
116 set cmk [string first @@ $fmt]
117 if {$cmk > 0} {
118 return [string range $fmt 0 [expr {$cmk - 1}]]
120 return $fmt
123 proc mc {en_fmt args} {
124 set fmt [_mc_trim [::msgcat::mc $en_fmt]]
125 if {[catch {set msg [eval [list format $fmt] $args]} err]} {
126 set msg [eval [list format [_mc_trim $en_fmt]] $args]
128 return $msg
131 proc strcat {args} {
132 return [join $args {}]
135 ::msgcat::mcload $oguimsg
136 unset oguimsg
138 ######################################################################
140 ## On Mac, bring the current Wish process window to front
142 if {[tk windowingsystem] eq "aqua"} {
143 catch {
144 exec osascript -e [format {
145 tell application "System Events"
146 set frontmost of processes whose unix id is %d to true
147 end tell
148 } [pid]]
152 ######################################################################
154 ## read only globals
156 set _appname {Git Gui}
157 set _gitdir {}
158 set _gitworktree {}
159 set _isbare {}
160 set _gitexec {}
161 set _githtmldir {}
162 set _reponame {}
163 set _iscygwin {}
164 set _search_path {}
165 set _shellpath {@@SHELL_PATH@@}
167 set _trace [lsearch -exact $argv --trace]
168 if {$_trace >= 0} {
169 set argv [lreplace $argv $_trace $_trace]
170 set _trace 1
171 if {[tk windowingsystem] eq "win32"} { console show }
172 } else {
173 set _trace 0
176 # variable for the last merged branch (useful for a default when deleting
177 # branches).
178 set _last_merged_branch {}
180 proc shellpath {} {
181 global _shellpath env
182 if {[string match @@* $_shellpath]} {
183 if {[info exists env(SHELL)]} {
184 return $env(SHELL)
185 } else {
186 return /bin/sh
189 return $_shellpath
192 proc appname {} {
193 global _appname
194 return $_appname
197 proc gitdir {args} {
198 global _gitdir
199 if {$args eq {}} {
200 return $_gitdir
202 return [eval [list file join $_gitdir] $args]
205 proc gitexec {args} {
206 global _gitexec
207 if {$_gitexec eq {}} {
208 if {[catch {set _gitexec [git --exec-path]} err]} {
209 error "Git not installed?\n\n$err"
211 if {[is_Cygwin]} {
212 set _gitexec [exec cygpath \
213 --windows \
214 --absolute \
215 $_gitexec]
216 } else {
217 set _gitexec [file normalize $_gitexec]
220 if {$args eq {}} {
221 return $_gitexec
223 return [eval [list file join $_gitexec] $args]
226 proc githtmldir {args} {
227 global _githtmldir
228 if {$_githtmldir eq {}} {
229 if {[catch {set _githtmldir [git --html-path]}]} {
230 # Git not installed or option not yet supported
231 return {}
233 if {[is_Cygwin]} {
234 set _githtmldir [exec cygpath \
235 --windows \
236 --absolute \
237 $_githtmldir]
238 } else {
239 set _githtmldir [file normalize $_githtmldir]
242 if {$args eq {}} {
243 return $_githtmldir
245 return [eval [list file join $_githtmldir] $args]
248 proc reponame {} {
249 return $::_reponame
252 proc is_MacOSX {} {
253 if {[tk windowingsystem] eq {aqua}} {
254 return 1
256 return 0
259 proc is_Windows {} {
260 if {$::tcl_platform(platform) eq {windows}} {
261 return 1
263 return 0
266 proc is_Cygwin {} {
267 global _iscygwin
268 if {$_iscygwin eq {}} {
269 if {$::tcl_platform(platform) eq {windows}} {
270 if {[catch {set p [exec cygpath --windir]} err]} {
271 set _iscygwin 0
272 } else {
273 set _iscygwin 1
275 } else {
276 set _iscygwin 0
279 return $_iscygwin
282 proc is_enabled {option} {
283 global enabled_options
284 if {[catch {set on $enabled_options($option)}]} {return 0}
285 return $on
288 proc enable_option {option} {
289 global enabled_options
290 set enabled_options($option) 1
293 proc disable_option {option} {
294 global enabled_options
295 set enabled_options($option) 0
298 ######################################################################
300 ## config
302 proc is_many_config {name} {
303 switch -glob -- $name {
304 gui.recentrepo -
305 remote.*.fetch -
306 remote.*.push
307 {return 1}
309 {return 0}
313 proc is_config_true {name} {
314 global repo_config
315 if {[catch {set v $repo_config($name)}]} {
316 return 0
318 set v [string tolower $v]
319 if {$v eq {} || $v eq {true} || $v eq {1} || $v eq {yes} || $v eq {on}} {
320 return 1
321 } else {
322 return 0
326 proc is_config_false {name} {
327 global repo_config
328 if {[catch {set v $repo_config($name)}]} {
329 return 0
331 set v [string tolower $v]
332 if {$v eq {false} || $v eq {0} || $v eq {no} || $v eq {off}} {
333 return 1
334 } else {
335 return 0
339 proc get_config {name} {
340 global repo_config
341 if {[catch {set v $repo_config($name)}]} {
342 return {}
343 } else {
344 return $v
348 proc is_bare {} {
349 global _isbare
350 global _gitdir
351 global _gitworktree
353 if {$_isbare eq {}} {
354 if {[catch {
355 set _bare [git rev-parse --is-bare-repository]
356 switch -- $_bare {
357 true { set _isbare 1 }
358 false { set _isbare 0}
359 default { throw }
361 }]} {
362 if {[is_config_true core.bare]
363 || ($_gitworktree eq {}
364 && [lindex [file split $_gitdir] end] ne {.git})} {
365 set _isbare 1
366 } else {
367 set _isbare 0
371 return $_isbare
374 ######################################################################
376 ## handy utils
378 proc _trace_exec {cmd} {
379 if {!$::_trace} return
380 set d {}
381 foreach v $cmd {
382 if {$d ne {}} {
383 append d { }
385 if {[regexp {[ \t\r\n'"$?*]} $v]} {
386 set v [sq $v]
388 append d $v
390 puts stderr $d
393 #'" fix poor old emacs font-lock mode
395 proc _git_cmd {name} {
396 global _git_cmd_path
398 if {[catch {set v $_git_cmd_path($name)}]} {
399 switch -- $name {
400 version -
401 --version -
402 --exec-path { return [list $::_git $name] }
405 set p [gitexec git-$name$::_search_exe]
406 if {[file exists $p]} {
407 set v [list $p]
408 } elseif {[is_Windows] && [file exists [gitexec git-$name]]} {
409 # Try to determine what sort of magic will make
410 # git-$name go and do its thing, because native
411 # Tcl on Windows doesn't know it.
413 set p [gitexec git-$name]
414 set f [open $p r]
415 set s [gets $f]
416 close $f
418 switch -glob -- [lindex $s 0] {
419 #!*sh { set i sh }
420 #!*perl { set i perl }
421 #!*python { set i python }
422 default { error "git-$name is not supported: $s" }
425 upvar #0 _$i interp
426 if {![info exists interp]} {
427 set interp [_which $i]
429 if {$interp eq {}} {
430 error "git-$name requires $i (not in PATH)"
432 set v [concat [list $interp] [lrange $s 1 end] [list $p]]
433 } else {
434 # Assume it is builtin to git somehow and we
435 # aren't actually able to see a file for it.
437 set v [list $::_git $name]
439 set _git_cmd_path($name) $v
441 return $v
444 proc _which {what args} {
445 global env _search_exe _search_path
447 if {$_search_path eq {}} {
448 if {[is_Cygwin] && [regexp {^(/|\.:)} $env(PATH)]} {
449 set _search_path [split [exec cygpath \
450 --windows \
451 --path \
452 --absolute \
453 $env(PATH)] {;}]
454 set _search_exe .exe
455 } elseif {[is_Windows]} {
456 set gitguidir [file dirname [info script]]
457 regsub -all ";" $gitguidir "\\;" gitguidir
458 set env(PATH) "$gitguidir;$env(PATH)"
459 set _search_path [split $env(PATH) {;}]
460 set _search_exe .exe
461 } else {
462 set _search_path [split $env(PATH) :]
463 set _search_exe {}
467 if {[is_Windows] && [lsearch -exact $args -script] >= 0} {
468 set suffix {}
469 } else {
470 set suffix $_search_exe
473 foreach p $_search_path {
474 set p [file join $p $what$suffix]
475 if {[file exists $p]} {
476 return [file normalize $p]
479 return {}
482 # Test a file for a hashbang to identify executable scripts on Windows.
483 proc is_shellscript {filename} {
484 if {![file exists $filename]} {return 0}
485 set f [open $filename r]
486 fconfigure $f -encoding binary
487 set magic [read $f 2]
488 close $f
489 return [expr {$magic eq "#!"}]
492 # Run a command connected via pipes on stdout.
493 # This is for use with textconv filters and uses sh -c "..." to allow it to
494 # contain a command with arguments. On windows we must check for shell
495 # scripts specifically otherwise just call the filter command.
496 proc open_cmd_pipe {cmd path} {
497 global env
498 if {![file executable [shellpath]]} {
499 set exe [auto_execok [lindex $cmd 0]]
500 if {[is_shellscript [lindex $exe 0]]} {
501 set run [linsert [auto_execok sh] end -c "$cmd \"\$0\"" $path]
502 } else {
503 set run [concat $exe [lrange $cmd 1 end] $path]
505 } else {
506 set run [list [shellpath] -c "$cmd \"\$0\"" $path]
508 return [open |$run r]
511 proc _lappend_nice {cmd_var} {
512 global _nice
513 upvar $cmd_var cmd
515 if {![info exists _nice]} {
516 set _nice [_which nice]
517 if {[catch {exec $_nice git version}]} {
518 set _nice {}
519 } elseif {[is_Windows] && [file dirname $_nice] ne [file dirname $::_git]} {
520 set _nice {}
523 if {$_nice ne {}} {
524 lappend cmd $_nice
528 proc git {args} {
529 set opt [list]
531 while {1} {
532 switch -- [lindex $args 0] {
533 --nice {
534 _lappend_nice opt
537 default {
538 break
543 set args [lrange $args 1 end]
546 set cmdp [_git_cmd [lindex $args 0]]
547 set args [lrange $args 1 end]
549 _trace_exec [concat $opt $cmdp $args]
550 set result [eval exec $opt $cmdp $args]
551 if {$::_trace} {
552 puts stderr "< $result"
554 return $result
557 proc _open_stdout_stderr {cmd} {
558 _trace_exec $cmd
559 if {[catch {
560 set fd [open [concat [list | ] $cmd] r]
561 } err]} {
562 if { [lindex $cmd end] eq {2>@1}
563 && $err eq {can not find channel named "1"}
565 # Older versions of Tcl 8.4 don't have this 2>@1 IO
566 # redirect operator. Fallback to |& cat for those.
567 # The command was not actually started, so its safe
568 # to try to start it a second time.
570 set fd [open [concat \
571 [list | ] \
572 [lrange $cmd 0 end-1] \
573 [list |& cat] \
574 ] r]
575 } else {
576 error $err
579 fconfigure $fd -eofchar {}
580 return $fd
583 proc git_read {args} {
584 set opt [list]
586 while {1} {
587 switch -- [lindex $args 0] {
588 --nice {
589 _lappend_nice opt
592 --stderr {
593 lappend args 2>@1
596 default {
597 break
602 set args [lrange $args 1 end]
605 set cmdp [_git_cmd [lindex $args 0]]
606 set args [lrange $args 1 end]
608 return [_open_stdout_stderr [concat $opt $cmdp $args]]
611 proc git_write {args} {
612 set opt [list]
614 while {1} {
615 switch -- [lindex $args 0] {
616 --nice {
617 _lappend_nice opt
620 default {
621 break
626 set args [lrange $args 1 end]
629 set cmdp [_git_cmd [lindex $args 0]]
630 set args [lrange $args 1 end]
632 _trace_exec [concat $opt $cmdp $args]
633 return [open [concat [list | ] $opt $cmdp $args] w]
636 proc githook_read {hook_name args} {
637 set pchook [gitdir hooks $hook_name]
638 lappend args 2>@1
640 # On Windows [file executable] might lie so we need to ask
641 # the shell if the hook is executable. Yes that's annoying.
643 if {[is_Windows]} {
644 upvar #0 _sh interp
645 if {![info exists interp]} {
646 set interp [_which sh]
648 if {$interp eq {}} {
649 error "hook execution requires sh (not in PATH)"
652 set scr {if test -x "$1";then exec "$@";fi}
653 set sh_c [list $interp -c $scr $interp $pchook]
654 return [_open_stdout_stderr [concat $sh_c $args]]
657 if {[file executable $pchook]} {
658 return [_open_stdout_stderr [concat [list $pchook] $args]]
661 return {}
664 proc kill_file_process {fd} {
665 set process [pid $fd]
667 catch {
668 if {[is_Windows]} {
669 # Use a Cygwin-specific flag to allow killing
670 # native Windows processes
671 exec kill -f $process
672 } else {
673 exec kill $process
678 proc gitattr {path attr default} {
679 if {[catch {set r [git check-attr $attr -- $path]}]} {
680 set r unspecified
681 } else {
682 set r [join [lrange [split $r :] 2 end] :]
683 regsub {^ } $r {} r
685 if {$r eq {unspecified}} {
686 return $default
688 return $r
691 proc sq {value} {
692 regsub -all ' $value "'\\''" value
693 return "'$value'"
696 proc load_current_branch {} {
697 global current_branch is_detached
699 set fd [open [gitdir HEAD] r]
700 if {[gets $fd ref] < 1} {
701 set ref {}
703 close $fd
705 set pfx {ref: refs/heads/}
706 set len [string length $pfx]
707 if {[string equal -length $len $pfx $ref]} {
708 # We're on a branch. It might not exist. But
709 # HEAD looks good enough to be a branch.
711 set current_branch [string range $ref $len end]
712 set is_detached 0
713 } else {
714 # Assume this is a detached head.
716 set current_branch HEAD
717 set is_detached 1
721 auto_load tk_optionMenu
722 rename tk_optionMenu real__tkOptionMenu
723 proc tk_optionMenu {w varName args} {
724 set m [eval real__tkOptionMenu $w $varName $args]
725 $m configure -font font_ui
726 $w configure -font font_ui
727 return $m
730 proc rmsel_tag {text} {
731 $text tag conf sel \
732 -background [$text cget -background] \
733 -foreground [$text cget -foreground] \
734 -borderwidth 0
735 $text tag conf in_sel -background lightgray
736 bind $text <Motion> break
737 return $text
740 wm withdraw .
741 set root_exists 0
742 bind . <Visibility> {
743 bind . <Visibility> {}
744 set root_exists 1
747 if {[is_Windows]} {
748 wm iconbitmap . -default $oguilib/git-gui.ico
749 set ::tk::AlwaysShowSelection 1
750 bind . <Control-F2> {console show}
752 # Spoof an X11 display for SSH
753 if {![info exists env(DISPLAY)]} {
754 set env(DISPLAY) :9999
756 } else {
757 catch {
758 image create photo gitlogo -width 16 -height 16
760 gitlogo put #33CC33 -to 7 0 9 2
761 gitlogo put #33CC33 -to 4 2 12 4
762 gitlogo put #33CC33 -to 7 4 9 6
763 gitlogo put #CC3333 -to 4 6 12 8
764 gitlogo put gray26 -to 4 9 6 10
765 gitlogo put gray26 -to 3 10 6 12
766 gitlogo put gray26 -to 8 9 13 11
767 gitlogo put gray26 -to 8 11 10 12
768 gitlogo put gray26 -to 11 11 13 14
769 gitlogo put gray26 -to 3 12 5 14
770 gitlogo put gray26 -to 5 13
771 gitlogo put gray26 -to 10 13
772 gitlogo put gray26 -to 4 14 12 15
773 gitlogo put gray26 -to 5 15 11 16
774 gitlogo redither
776 image create photo gitlogo32 -width 32 -height 32
777 gitlogo32 copy gitlogo -zoom 2 2
779 wm iconphoto . -default gitlogo gitlogo32
783 ######################################################################
785 ## config defaults
787 set cursor_ptr arrow
788 font create font_ui
789 if {[lsearch -exact [font names] TkDefaultFont] != -1} {
790 eval [linsert [font actual TkDefaultFont] 0 font configure font_ui]
791 eval [linsert [font actual TkFixedFont] 0 font create font_diff]
792 } else {
793 font create font_diff -family Courier -size 10
794 catch {
795 label .dummy
796 eval font configure font_ui [font actual [.dummy cget -font]]
797 destroy .dummy
801 font create font_uiitalic
802 font create font_uibold
803 font create font_diffbold
804 font create font_diffitalic
806 foreach class {Button Checkbutton Entry Label
807 Labelframe Listbox Message
808 Radiobutton Spinbox Text} {
809 option add *$class.font font_ui
811 if {![is_MacOSX]} {
812 option add *Menu.font font_ui
813 option add *Entry.borderWidth 1 startupFile
814 option add *Entry.relief sunken startupFile
815 option add *RadioButton.anchor w startupFile
817 unset class
819 if {[is_Windows] || [is_MacOSX]} {
820 option add *Menu.tearOff 0
823 if {[is_MacOSX]} {
824 set M1B M1
825 set M1T Cmd
826 } else {
827 set M1B Control
828 set M1T Ctrl
831 proc bind_button3 {w cmd} {
832 bind $w <Any-Button-3> $cmd
833 if {[is_MacOSX]} {
834 # Mac OS X sends Button-2 on right click through three-button mouse,
835 # or through trackpad right-clicking (two-finger touch + click).
836 bind $w <Any-Button-2> $cmd
837 bind $w <Control-Button-1> $cmd
841 proc apply_config {} {
842 global repo_config font_descs
844 foreach option $font_descs {
845 set name [lindex $option 0]
846 set font [lindex $option 1]
847 if {[catch {
848 set need_weight 1
849 foreach {cn cv} $repo_config(gui.$name) {
850 if {$cn eq {-weight}} {
851 set need_weight 0
853 font configure $font $cn $cv
855 if {$need_weight} {
856 font configure $font -weight normal
858 } err]} {
859 error_popup [strcat [mc "Invalid font specified in %s:" "gui.$name"] "\n\n$err"]
861 foreach {cn cv} [font configure $font] {
862 font configure ${font}bold $cn $cv
863 font configure ${font}italic $cn $cv
865 font configure ${font}bold -weight bold
866 font configure ${font}italic -slant italic
869 global use_ttk NS
870 set use_ttk 0
871 set NS {}
872 if {$repo_config(gui.usettk)} {
873 set use_ttk [package vsatisfies [package provide Tk] 8.5]
874 if {$use_ttk} {
875 set NS ttk
876 bind [winfo class .] <<ThemeChanged>> [list InitTheme]
877 pave_toplevel .
882 set default_config(branch.autosetupmerge) true
883 set default_config(merge.tool) {}
884 set default_config(mergetool.keepbackup) true
885 set default_config(merge.diffstat) true
886 set default_config(merge.summary) false
887 set default_config(merge.verbosity) 2
888 set default_config(user.name) {}
889 set default_config(user.email) {}
891 set default_config(gui.encoding) [encoding system]
892 set default_config(gui.matchtrackingbranch) false
893 set default_config(gui.textconv) true
894 set default_config(gui.pruneduringfetch) false
895 set default_config(gui.trustmtime) false
896 set default_config(gui.fastcopyblame) false
897 set default_config(gui.maxrecentrepo) 10
898 set default_config(gui.copyblamethreshold) 40
899 set default_config(gui.blamehistoryctx) 7
900 set default_config(gui.diffcontext) 5
901 set default_config(gui.diffopts) {}
902 set default_config(gui.commitmsgwidth) 75
903 set default_config(gui.newbranchtemplate) {}
904 set default_config(gui.spellingdictionary) {}
905 set default_config(gui.fontui) [font configure font_ui]
906 set default_config(gui.fontdiff) [font configure font_diff]
907 # TODO: this option should be added to the git-config documentation
908 set default_config(gui.maxfilesdisplayed) 5000
909 set default_config(gui.usettk) 1
910 set default_config(gui.warndetachedcommit) 1
911 set font_descs {
912 {fontui font_ui {mc "Main Font"}}
913 {fontdiff font_diff {mc "Diff/Console Font"}}
915 set default_config(gui.stageuntracked) ask
916 set default_config(gui.displayuntracked) true
918 ######################################################################
920 ## find git
922 set _git [_which git]
923 if {$_git eq {}} {
924 catch {wm withdraw .}
925 tk_messageBox \
926 -icon error \
927 -type ok \
928 -title [mc "git-gui: fatal error"] \
929 -message [mc "Cannot find git in PATH."]
930 exit 1
933 ######################################################################
935 ## version check
937 if {[catch {set _git_version [git --version]} err]} {
938 catch {wm withdraw .}
939 tk_messageBox \
940 -icon error \
941 -type ok \
942 -title [mc "git-gui: fatal error"] \
943 -message "Cannot determine Git version:
945 $err
947 [appname] requires Git 1.5.0 or later."
948 exit 1
950 if {![regsub {^git version } $_git_version {} _git_version]} {
951 catch {wm withdraw .}
952 tk_messageBox \
953 -icon error \
954 -type ok \
955 -title [mc "git-gui: fatal error"] \
956 -message [strcat [mc "Cannot parse Git version string:"] "\n\n$_git_version"]
957 exit 1
960 proc get_trimmed_version {s} {
961 set r {}
962 foreach x [split $s -._] {
963 if {[string is integer -strict $x]} {
964 lappend r $x
965 } else {
966 break
969 return [join $r .]
971 set _real_git_version $_git_version
972 set _git_version [get_trimmed_version $_git_version]
974 if {![regexp {^[1-9]+(\.[0-9]+)+$} $_git_version]} {
975 catch {wm withdraw .}
976 if {[tk_messageBox \
977 -icon warning \
978 -type yesno \
979 -default no \
980 -title "[appname]: warning" \
981 -message [mc "Git version cannot be determined.
983 %s claims it is version '%s'.
985 %s requires at least Git 1.5.0 or later.
987 Assume '%s' is version 1.5.0?
988 " $_git $_real_git_version [appname] $_real_git_version]] eq {yes}} {
989 set _git_version 1.5.0
990 } else {
991 exit 1
994 unset _real_git_version
996 proc git-version {args} {
997 global _git_version
999 switch [llength $args] {
1001 return $_git_version
1005 set op [lindex $args 0]
1006 set vr [lindex $args 1]
1007 set cm [package vcompare $_git_version $vr]
1008 return [expr $cm $op 0]
1012 set type [lindex $args 0]
1013 set name [lindex $args 1]
1014 set parm [lindex $args 2]
1015 set body [lindex $args 3]
1017 if {($type ne {proc} && $type ne {method})} {
1018 error "Invalid arguments to git-version"
1020 if {[llength $body] < 2 || [lindex $body end-1] ne {default}} {
1021 error "Last arm of $type $name must be default"
1024 foreach {op vr cb} [lrange $body 0 end-2] {
1025 if {[git-version $op $vr]} {
1026 return [uplevel [list $type $name $parm $cb]]
1030 return [uplevel [list $type $name $parm [lindex $body end]]]
1033 default {
1034 error "git-version >= x"
1040 if {[git-version < 1.5]} {
1041 catch {wm withdraw .}
1042 tk_messageBox \
1043 -icon error \
1044 -type ok \
1045 -title [mc "git-gui: fatal error"] \
1046 -message "[appname] requires Git 1.5.0 or later.
1048 You are using [git-version]:
1050 [git --version]"
1051 exit 1
1054 ######################################################################
1056 ## configure our library
1058 set idx [file join $oguilib tclIndex]
1059 if {[catch {set fd [open $idx r]} err]} {
1060 catch {wm withdraw .}
1061 tk_messageBox \
1062 -icon error \
1063 -type ok \
1064 -title [mc "git-gui: fatal error"] \
1065 -message $err
1066 exit 1
1068 if {[gets $fd] eq {# Autogenerated by git-gui Makefile}} {
1069 set idx [list]
1070 while {[gets $fd n] >= 0} {
1071 if {$n ne {} && ![string match #* $n]} {
1072 lappend idx $n
1075 } else {
1076 set idx {}
1078 close $fd
1080 if {$idx ne {}} {
1081 set loaded [list]
1082 foreach p $idx {
1083 if {[lsearch -exact $loaded $p] >= 0} continue
1084 source [file join $oguilib $p]
1085 lappend loaded $p
1087 unset loaded p
1088 } else {
1089 set auto_path [concat [list $oguilib] $auto_path]
1091 unset -nocomplain idx fd
1093 ######################################################################
1095 ## config file parsing
1097 git-version proc _parse_config {arr_name args} {
1098 >= 1.5.3 {
1099 upvar $arr_name arr
1100 array unset arr
1101 set buf {}
1102 catch {
1103 set fd_rc [eval \
1104 [list git_read config] \
1105 $args \
1106 [list --null --list]]
1107 fconfigure $fd_rc -translation binary
1108 set buf [read $fd_rc]
1109 close $fd_rc
1111 foreach line [split $buf "\0"] {
1112 if {[regexp {^([^\n]+)\n(.*)$} $line line name value]} {
1113 if {[is_many_config $name]} {
1114 lappend arr($name) $value
1115 } else {
1116 set arr($name) $value
1118 } elseif {[regexp {^([^\n]+)$} $line line name]} {
1119 # no value given, but interpreting them as
1120 # boolean will be handled as true
1121 set arr($name) {}
1125 default {
1126 upvar $arr_name arr
1127 array unset arr
1128 catch {
1129 set fd_rc [eval [list git_read config --list] $args]
1130 while {[gets $fd_rc line] >= 0} {
1131 if {[regexp {^([^=]+)=(.*)$} $line line name value]} {
1132 if {[is_many_config $name]} {
1133 lappend arr($name) $value
1134 } else {
1135 set arr($name) $value
1137 } elseif {[regexp {^([^=]+)$} $line line name]} {
1138 # no value given, but interpreting them as
1139 # boolean will be handled as true
1140 set arr($name) {}
1143 close $fd_rc
1148 proc load_config {include_global} {
1149 global repo_config global_config system_config default_config
1151 if {$include_global} {
1152 _parse_config system_config --system
1153 _parse_config global_config --global
1155 _parse_config repo_config
1157 foreach name [array names default_config] {
1158 if {[catch {set v $system_config($name)}]} {
1159 set system_config($name) $default_config($name)
1162 foreach name [array names system_config] {
1163 if {[catch {set v $global_config($name)}]} {
1164 set global_config($name) $system_config($name)
1166 if {[catch {set v $repo_config($name)}]} {
1167 set repo_config($name) $system_config($name)
1172 ######################################################################
1174 ## feature option selection
1176 if {[regexp {^git-(.+)$} [file tail $argv0] _junk subcommand]} {
1177 unset _junk
1178 } else {
1179 set subcommand gui
1181 if {$subcommand eq {gui.sh}} {
1182 set subcommand gui
1184 if {$subcommand eq {gui} && [llength $argv] > 0} {
1185 set subcommand [lindex $argv 0]
1186 set argv [lrange $argv 1 end]
1189 enable_option multicommit
1190 enable_option branch
1191 enable_option transport
1192 disable_option bare
1194 switch -- $subcommand {
1195 browser -
1196 blame {
1197 enable_option bare
1199 disable_option multicommit
1200 disable_option branch
1201 disable_option transport
1203 citool {
1204 enable_option singlecommit
1205 enable_option retcode
1207 disable_option multicommit
1208 disable_option branch
1209 disable_option transport
1211 while {[llength $argv] > 0} {
1212 set a [lindex $argv 0]
1213 switch -- $a {
1214 --amend {
1215 enable_option initialamend
1217 --nocommit {
1218 enable_option nocommit
1219 enable_option nocommitmsg
1221 --commitmsg {
1222 disable_option nocommitmsg
1224 default {
1225 break
1229 set argv [lrange $argv 1 end]
1234 ######################################################################
1236 ## execution environment
1238 set have_tk85 [expr {[package vcompare $tk_version "8.5"] >= 0}]
1240 # Suggest our implementation of askpass, if none is set
1241 if {![info exists env(SSH_ASKPASS)]} {
1242 set env(SSH_ASKPASS) [gitexec git-gui--askpass]
1245 ######################################################################
1247 ## repository setup
1249 set picked 0
1250 if {[catch {
1251 set _gitdir $env(GIT_DIR)
1252 set _prefix {}
1254 && [catch {
1255 # beware that from the .git dir this sets _gitdir to .
1256 # and _prefix to the empty string
1257 set _gitdir [git rev-parse --git-dir]
1258 set _prefix [git rev-parse --show-prefix]
1259 } err]} {
1260 load_config 1
1261 apply_config
1262 choose_repository::pick
1263 set picked 1
1266 # we expand the _gitdir when it's just a single dot (i.e. when we're being
1267 # run from the .git dir itself) lest the routines to find the worktree
1268 # get confused
1269 if {$_gitdir eq "."} {
1270 set _gitdir [pwd]
1273 if {![file isdirectory $_gitdir] && [is_Cygwin]} {
1274 catch {set _gitdir [exec cygpath --windows $_gitdir]}
1276 if {![file isdirectory $_gitdir]} {
1277 catch {wm withdraw .}
1278 error_popup [strcat [mc "Git directory not found:"] "\n\n$_gitdir"]
1279 exit 1
1281 # _gitdir exists, so try loading the config
1282 load_config 0
1283 apply_config
1285 # v1.7.0 introduced --show-toplevel to return the canonical work-tree
1286 if {[package vsatisfies $_git_version 1.7.0]} {
1287 if { [is_Cygwin] } {
1288 catch {set _gitworktree [exec cygpath --windows [git rev-parse --show-toplevel]]}
1289 } else {
1290 set _gitworktree [git rev-parse --show-toplevel]
1292 } else {
1293 # try to set work tree from environment, core.worktree or use
1294 # cdup to obtain a relative path to the top of the worktree. If
1295 # run from the top, the ./ prefix ensures normalize expands pwd.
1296 if {[catch { set _gitworktree $env(GIT_WORK_TREE) }]} {
1297 set _gitworktree [get_config core.worktree]
1298 if {$_gitworktree eq ""} {
1299 set _gitworktree [file normalize ./[git rev-parse --show-cdup]]
1304 if {$_prefix ne {}} {
1305 if {$_gitworktree eq {}} {
1306 regsub -all {[^/]+/} $_prefix ../ cdup
1307 } else {
1308 set cdup $_gitworktree
1310 if {[catch {cd $cdup} err]} {
1311 catch {wm withdraw .}
1312 error_popup [strcat [mc "Cannot move to top of working directory:"] "\n\n$err"]
1313 exit 1
1315 set _gitworktree [pwd]
1316 unset cdup
1317 } elseif {![is_enabled bare]} {
1318 if {[is_bare]} {
1319 catch {wm withdraw .}
1320 error_popup [strcat [mc "Cannot use bare repository:"] "\n\n$_gitdir"]
1321 exit 1
1323 if {$_gitworktree eq {}} {
1324 set _gitworktree [file dirname $_gitdir]
1326 if {[catch {cd $_gitworktree} err]} {
1327 catch {wm withdraw .}
1328 error_popup [strcat [mc "No working directory"] " $_gitworktree:\n\n$err"]
1329 exit 1
1331 set _gitworktree [pwd]
1333 set _reponame [file split [file normalize $_gitdir]]
1334 if {[lindex $_reponame end] eq {.git}} {
1335 set _reponame [lindex $_reponame end-1]
1336 } else {
1337 set _reponame [lindex $_reponame end]
1340 set env(GIT_DIR) $_gitdir
1341 set env(GIT_WORK_TREE) $_gitworktree
1343 catch { unset env(GIT_EDITOR_F_NBLOCK) }
1344 catch { unset env(GIT_EDITOR_F_POSITION) }
1346 ######################################################################
1348 ## global init
1350 set current_diff_path {}
1351 set current_diff_side {}
1352 set diff_actions [list]
1354 set HEAD {}
1355 set PARENT {}
1356 set MERGE_HEAD [list]
1357 set commit_type {}
1358 set empty_tree {}
1359 set current_branch {}
1360 set is_detached 0
1361 set current_diff_path {}
1362 set is_3way_diff 0
1363 set is_submodule_diff 0
1364 set is_conflict_diff 0
1365 set selected_commit_type new
1366 set diff_empty_count 0
1367 set diff_show_line_numbers 1
1368 set diff_lno_column_width 0
1369 set is_other_diff 0
1371 set nullid "0000000000000000000000000000000000000000"
1372 set nullid2 "0000000000000000000000000000000000000001"
1375 ######################################################################
1377 ## task management
1379 set rescan_active 0
1380 set diff_active 0
1381 set last_clicked {}
1383 set disable_on_lock [list]
1384 set index_lock_type none
1386 proc lock_index {type} {
1387 global index_lock_type disable_on_lock
1389 if {$index_lock_type eq {none}} {
1390 set index_lock_type $type
1391 foreach w $disable_on_lock {
1392 uplevel #0 $w disabled
1394 return 1
1395 } elseif {$index_lock_type eq "begin-$type"} {
1396 set index_lock_type $type
1397 return 1
1399 return 0
1402 proc unlock_index {} {
1403 global index_lock_type disable_on_lock
1405 set index_lock_type none
1406 foreach w $disable_on_lock {
1407 uplevel #0 $w normal
1411 ######################################################################
1413 ## status
1415 proc repository_state {ctvar hdvar mhvar} {
1416 global current_branch
1417 upvar $ctvar ct $hdvar hd $mhvar mh
1419 set mh [list]
1421 load_current_branch
1422 if {[catch {set hd [git rev-parse --verify HEAD]}]} {
1423 set hd {}
1424 set ct initial
1425 return
1428 set merge_head [gitdir MERGE_HEAD]
1429 if {[file exists $merge_head]} {
1430 set ct merge
1431 set fd_mh [open $merge_head r]
1432 while {[gets $fd_mh line] >= 0} {
1433 lappend mh $line
1435 close $fd_mh
1436 return
1439 set ct normal
1442 proc PARENT {} {
1443 global PARENT empty_tree
1445 set p [lindex $PARENT 0]
1446 if {$p ne {}} {
1447 return $p
1449 if {$empty_tree eq {}} {
1450 set empty_tree [git mktree << {}]
1452 return $empty_tree
1455 proc force_amend {} {
1456 global selected_commit_type
1457 global HEAD PARENT MERGE_HEAD commit_type
1459 repository_state newType newHEAD newMERGE_HEAD
1460 set HEAD $newHEAD
1461 set PARENT $newHEAD
1462 set MERGE_HEAD $newMERGE_HEAD
1463 set commit_type $newType
1465 set selected_commit_type amend
1466 do_select_commit_type
1469 proc rescan {after {honor_trustmtime 1}} {
1470 global HEAD PARENT MERGE_HEAD commit_type
1471 global ui_index ui_workdir ui_comm
1472 global rescan_active file_states
1473 global repo_config
1475 if {$rescan_active > 0 || ![lock_index read]} return
1477 repository_state newType newHEAD newMERGE_HEAD
1478 if {[string match amend* $commit_type]
1479 && $newType eq {normal}
1480 && $newHEAD eq $HEAD} {
1481 } else {
1482 set HEAD $newHEAD
1483 set PARENT $newHEAD
1484 set MERGE_HEAD $newMERGE_HEAD
1485 set commit_type $newType
1488 array unset file_states
1490 if {!$::GITGUI_BCK_exists &&
1491 (![$ui_comm edit modified]
1492 || [string trim [$ui_comm get 0.0 end]] eq {})} {
1493 if {[string match amend* $commit_type]} {
1494 } elseif {[load_message GITGUI_MSG utf-8]} {
1495 } elseif {[run_prepare_commit_msg_hook]} {
1496 } elseif {[load_message MERGE_MSG]} {
1497 } elseif {[load_message SQUASH_MSG]} {
1499 $ui_comm edit reset
1500 $ui_comm edit modified false
1503 if {$honor_trustmtime && $repo_config(gui.trustmtime) eq {true}} {
1504 rescan_stage2 {} $after
1505 } else {
1506 set rescan_active 1
1507 ui_status [mc "Refreshing file status..."]
1508 set fd_rf [git_read update-index \
1509 -q \
1510 --unmerged \
1511 --ignore-missing \
1512 --refresh \
1514 fconfigure $fd_rf -blocking 0 -translation binary
1515 fileevent $fd_rf readable \
1516 [list rescan_stage2 $fd_rf $after]
1520 if {[is_Cygwin]} {
1521 set is_git_info_exclude {}
1522 proc have_info_exclude {} {
1523 global is_git_info_exclude
1525 if {$is_git_info_exclude eq {}} {
1526 if {[catch {exec test -f [gitdir info exclude]}]} {
1527 set is_git_info_exclude 0
1528 } else {
1529 set is_git_info_exclude 1
1532 return $is_git_info_exclude
1534 } else {
1535 proc have_info_exclude {} {
1536 return [file readable [gitdir info exclude]]
1540 proc rescan_stage2 {fd after} {
1541 global rescan_active buf_rdi buf_rdf buf_rlo
1543 if {$fd ne {}} {
1544 read $fd
1545 if {![eof $fd]} return
1546 close $fd
1549 if {[package vsatisfies $::_git_version 1.6.3]} {
1550 set ls_others [list --exclude-standard]
1551 } else {
1552 set ls_others [list --exclude-per-directory=.gitignore]
1553 if {[have_info_exclude]} {
1554 lappend ls_others "--exclude-from=[gitdir info exclude]"
1556 set user_exclude [get_config core.excludesfile]
1557 if {$user_exclude ne {} && [file readable $user_exclude]} {
1558 lappend ls_others "--exclude-from=[file normalize $user_exclude]"
1562 set buf_rdi {}
1563 set buf_rdf {}
1564 set buf_rlo {}
1566 set rescan_active 2
1567 ui_status [mc "Scanning for modified files ..."]
1568 set fd_di [git_read diff-index --cached -z [PARENT]]
1569 set fd_df [git_read diff-files -z]
1571 fconfigure $fd_di -blocking 0 -translation binary -encoding binary
1572 fconfigure $fd_df -blocking 0 -translation binary -encoding binary
1574 fileevent $fd_di readable [list read_diff_index $fd_di $after]
1575 fileevent $fd_df readable [list read_diff_files $fd_df $after]
1577 if {[is_config_true gui.displayuntracked]} {
1578 set fd_lo [eval git_read ls-files --others -z $ls_others]
1579 fconfigure $fd_lo -blocking 0 -translation binary -encoding binary
1580 fileevent $fd_lo readable [list read_ls_others $fd_lo $after]
1581 incr rescan_active
1585 proc load_message {file {encoding {}}} {
1586 global ui_comm
1588 set f [gitdir $file]
1589 if {[file isfile $f]} {
1590 if {[catch {set fd [open $f r]}]} {
1591 return 0
1593 fconfigure $fd -eofchar {}
1594 if {$encoding ne {}} {
1595 fconfigure $fd -encoding $encoding
1597 set content [string trim [read $fd]]
1598 close $fd
1599 regsub -all -line {[ \r\t]+$} $content {} content
1600 $ui_comm delete 0.0 end
1601 $ui_comm insert end $content
1602 return 1
1604 return 0
1607 proc run_prepare_commit_msg_hook {} {
1608 global pch_error
1610 # prepare-commit-msg requires PREPARE_COMMIT_MSG exist. From git-gui
1611 # it will be .git/MERGE_MSG (merge), .git/SQUASH_MSG (squash), or an
1612 # empty file but existent file.
1614 set fd_pcm [open [gitdir PREPARE_COMMIT_MSG] a]
1616 if {[file isfile [gitdir MERGE_MSG]]} {
1617 set pcm_source "merge"
1618 set fd_mm [open [gitdir MERGE_MSG] r]
1619 puts -nonewline $fd_pcm [read $fd_mm]
1620 close $fd_mm
1621 } elseif {[file isfile [gitdir SQUASH_MSG]]} {
1622 set pcm_source "squash"
1623 set fd_sm [open [gitdir SQUASH_MSG] r]
1624 puts -nonewline $fd_pcm [read $fd_sm]
1625 close $fd_sm
1626 } else {
1627 set pcm_source ""
1630 close $fd_pcm
1632 set fd_ph [githook_read prepare-commit-msg \
1633 [gitdir PREPARE_COMMIT_MSG] $pcm_source]
1634 if {$fd_ph eq {}} {
1635 catch {file delete [gitdir PREPARE_COMMIT_MSG]}
1636 return 0;
1639 ui_status [mc "Calling prepare-commit-msg hook..."]
1640 set pch_error {}
1642 fconfigure $fd_ph -blocking 0 -translation binary -eofchar {}
1643 fileevent $fd_ph readable \
1644 [list prepare_commit_msg_hook_wait $fd_ph]
1646 return 1;
1649 proc prepare_commit_msg_hook_wait {fd_ph} {
1650 global pch_error
1652 append pch_error [read $fd_ph]
1653 fconfigure $fd_ph -blocking 1
1654 if {[eof $fd_ph]} {
1655 if {[catch {close $fd_ph}]} {
1656 ui_status [mc "Commit declined by prepare-commit-msg hook."]
1657 hook_failed_popup prepare-commit-msg $pch_error
1658 catch {file delete [gitdir PREPARE_COMMIT_MSG]}
1659 exit 1
1660 } else {
1661 load_message PREPARE_COMMIT_MSG
1663 set pch_error {}
1664 catch {file delete [gitdir PREPARE_COMMIT_MSG]}
1665 return
1667 fconfigure $fd_ph -blocking 0
1668 catch {file delete [gitdir PREPARE_COMMIT_MSG]}
1671 proc read_diff_index {fd after} {
1672 global buf_rdi
1674 append buf_rdi [read $fd]
1675 set c 0
1676 set n [string length $buf_rdi]
1677 while {$c < $n} {
1678 set z1 [string first "\0" $buf_rdi $c]
1679 if {$z1 == -1} break
1680 incr z1
1681 set z2 [string first "\0" $buf_rdi $z1]
1682 if {$z2 == -1} break
1684 incr c
1685 set i [split [string range $buf_rdi $c [expr {$z1 - 2}]] { }]
1686 set p [string range $buf_rdi $z1 [expr {$z2 - 1}]]
1687 merge_state \
1688 [encoding convertfrom $p] \
1689 [lindex $i 4]? \
1690 [list [lindex $i 0] [lindex $i 2]] \
1691 [list]
1692 set c $z2
1693 incr c
1695 if {$c < $n} {
1696 set buf_rdi [string range $buf_rdi $c end]
1697 } else {
1698 set buf_rdi {}
1701 rescan_done $fd buf_rdi $after
1704 proc read_diff_files {fd after} {
1705 global buf_rdf
1707 append buf_rdf [read $fd]
1708 set c 0
1709 set n [string length $buf_rdf]
1710 while {$c < $n} {
1711 set z1 [string first "\0" $buf_rdf $c]
1712 if {$z1 == -1} break
1713 incr z1
1714 set z2 [string first "\0" $buf_rdf $z1]
1715 if {$z2 == -1} break
1717 incr c
1718 set i [split [string range $buf_rdf $c [expr {$z1 - 2}]] { }]
1719 set p [string range $buf_rdf $z1 [expr {$z2 - 1}]]
1720 merge_state \
1721 [encoding convertfrom $p] \
1722 ?[lindex $i 4] \
1723 [list] \
1724 [list [lindex $i 0] [lindex $i 2]]
1725 set c $z2
1726 incr c
1728 if {$c < $n} {
1729 set buf_rdf [string range $buf_rdf $c end]
1730 } else {
1731 set buf_rdf {}
1734 rescan_done $fd buf_rdf $after
1737 proc read_ls_others {fd after} {
1738 global buf_rlo
1740 append buf_rlo [read $fd]
1741 set pck [split $buf_rlo "\0"]
1742 set buf_rlo [lindex $pck end]
1743 foreach p [lrange $pck 0 end-1] {
1744 set p [encoding convertfrom $p]
1745 if {[string index $p end] eq {/}} {
1746 set p [string range $p 0 end-1]
1748 merge_state $p ?O
1750 rescan_done $fd buf_rlo $after
1753 proc rescan_done {fd buf after} {
1754 global rescan_active current_diff_path
1755 global file_states repo_config
1756 upvar $buf to_clear
1758 if {![eof $fd]} return
1759 set to_clear {}
1760 close $fd
1761 if {[incr rescan_active -1] > 0} return
1763 prune_selection
1764 unlock_index
1765 display_all_files
1766 if {$current_diff_path ne {}} { reshow_diff $after }
1767 if {$current_diff_path eq {}} { select_first_diff $after }
1770 proc prune_selection {} {
1771 global file_states selected_paths
1773 foreach path [array names selected_paths] {
1774 if {[catch {set still_here $file_states($path)}]} {
1775 unset selected_paths($path)
1780 ######################################################################
1782 ## ui helpers
1784 proc mapicon {w state path} {
1785 global all_icons
1787 if {[catch {set r $all_icons($state$w)}]} {
1788 puts "error: no icon for $w state={$state} $path"
1789 return file_plain
1791 return $r
1794 proc mapdesc {state path} {
1795 global all_descs
1797 if {[catch {set r $all_descs($state)}]} {
1798 puts "error: no desc for state={$state} $path"
1799 return $state
1801 return $r
1804 proc ui_status {msg} {
1805 global main_status
1806 if {[info exists main_status]} {
1807 $main_status show $msg
1811 proc ui_ready {{test {}}} {
1812 global main_status
1813 if {[info exists main_status]} {
1814 $main_status show [mc "Ready."] $test
1818 proc escape_path {path} {
1819 regsub -all {\\} $path "\\\\" path
1820 regsub -all "\n" $path "\\n" path
1821 return $path
1824 proc short_path {path} {
1825 return [escape_path [lindex [file split $path] end]]
1828 set next_icon_id 0
1829 set null_sha1 [string repeat 0 40]
1831 proc merge_state {path new_state {head_info {}} {index_info {}}} {
1832 global file_states next_icon_id null_sha1
1834 set s0 [string index $new_state 0]
1835 set s1 [string index $new_state 1]
1837 if {[catch {set info $file_states($path)}]} {
1838 set state __
1839 set icon n[incr next_icon_id]
1840 } else {
1841 set state [lindex $info 0]
1842 set icon [lindex $info 1]
1843 if {$head_info eq {}} {set head_info [lindex $info 2]}
1844 if {$index_info eq {}} {set index_info [lindex $info 3]}
1847 if {$s0 eq {?}} {set s0 [string index $state 0]} \
1848 elseif {$s0 eq {_}} {set s0 _}
1850 if {$s1 eq {?}} {set s1 [string index $state 1]} \
1851 elseif {$s1 eq {_}} {set s1 _}
1853 if {$s0 eq {A} && $s1 eq {_} && $head_info eq {}} {
1854 set head_info [list 0 $null_sha1]
1855 } elseif {$s0 ne {_} && [string index $state 0] eq {_}
1856 && $head_info eq {}} {
1857 set head_info $index_info
1858 } elseif {$s0 eq {_} && [string index $state 0] ne {_}} {
1859 set index_info $head_info
1860 set head_info {}
1863 set file_states($path) [list $s0$s1 $icon \
1864 $head_info $index_info \
1866 return $state
1869 proc display_file_helper {w path icon_name old_m new_m} {
1870 global file_lists
1872 if {$new_m eq {_}} {
1873 set lno [lsearch -sorted -exact $file_lists($w) $path]
1874 if {$lno >= 0} {
1875 set file_lists($w) [lreplace $file_lists($w) $lno $lno]
1876 incr lno
1877 $w conf -state normal
1878 $w delete $lno.0 [expr {$lno + 1}].0
1879 $w conf -state disabled
1881 } elseif {$old_m eq {_} && $new_m ne {_}} {
1882 lappend file_lists($w) $path
1883 set file_lists($w) [lsort -unique $file_lists($w)]
1884 set lno [lsearch -sorted -exact $file_lists($w) $path]
1885 incr lno
1886 $w conf -state normal
1887 $w image create $lno.0 \
1888 -align center -padx 5 -pady 1 \
1889 -name $icon_name \
1890 -image [mapicon $w $new_m $path]
1891 $w insert $lno.1 "[escape_path $path]\n"
1892 $w conf -state disabled
1893 } elseif {$old_m ne $new_m} {
1894 $w conf -state normal
1895 $w image conf $icon_name -image [mapicon $w $new_m $path]
1896 $w conf -state disabled
1900 proc display_file {path state} {
1901 global file_states selected_paths
1902 global ui_index ui_workdir
1904 set old_m [merge_state $path $state]
1905 set s $file_states($path)
1906 set new_m [lindex $s 0]
1907 set icon_name [lindex $s 1]
1909 set o [string index $old_m 0]
1910 set n [string index $new_m 0]
1911 if {$o eq {U}} {
1912 set o _
1914 if {$n eq {U}} {
1915 set n _
1917 display_file_helper $ui_index $path $icon_name $o $n
1919 if {[string index $old_m 0] eq {U}} {
1920 set o U
1921 } else {
1922 set o [string index $old_m 1]
1924 if {[string index $new_m 0] eq {U}} {
1925 set n U
1926 } else {
1927 set n [string index $new_m 1]
1929 display_file_helper $ui_workdir $path $icon_name $o $n
1931 if {$new_m eq {__}} {
1932 unset file_states($path)
1933 catch {unset selected_paths($path)}
1937 proc display_all_files_helper {w path icon_name m} {
1938 global file_lists
1940 lappend file_lists($w) $path
1941 set lno [expr {[lindex [split [$w index end] .] 0] - 1}]
1942 $w image create end \
1943 -align center -padx 5 -pady 1 \
1944 -name $icon_name \
1945 -image [mapicon $w $m $path]
1946 $w insert end "[escape_path $path]\n"
1949 set files_warning 0
1950 proc display_all_files {} {
1951 global ui_index ui_workdir
1952 global file_states file_lists
1953 global last_clicked
1954 global files_warning
1956 $ui_index conf -state normal
1957 $ui_workdir conf -state normal
1959 $ui_index delete 0.0 end
1960 $ui_workdir delete 0.0 end
1961 set last_clicked {}
1963 set file_lists($ui_index) [list]
1964 set file_lists($ui_workdir) [list]
1966 set to_display [lsort [array names file_states]]
1967 set display_limit [get_config gui.maxfilesdisplayed]
1968 if {[llength $to_display] > $display_limit} {
1969 if {!$files_warning} {
1970 # do not repeatedly warn:
1971 set files_warning 1
1972 info_popup [mc "Displaying only %s of %s files." \
1973 $display_limit [llength $to_display]]
1975 set to_display [lrange $to_display 0 [expr {$display_limit-1}]]
1977 foreach path $to_display {
1978 set s $file_states($path)
1979 set m [lindex $s 0]
1980 set icon_name [lindex $s 1]
1982 set s [string index $m 0]
1983 if {$s ne {U} && $s ne {_}} {
1984 display_all_files_helper $ui_index $path \
1985 $icon_name $s
1988 if {[string index $m 0] eq {U}} {
1989 set s U
1990 } else {
1991 set s [string index $m 1]
1993 if {$s ne {_}} {
1994 display_all_files_helper $ui_workdir $path \
1995 $icon_name $s
1999 $ui_index conf -state disabled
2000 $ui_workdir conf -state disabled
2003 ######################################################################
2005 ## icons
2007 set filemask {
2008 #define mask_width 14
2009 #define mask_height 15
2010 static unsigned char mask_bits[] = {
2011 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f,
2012 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f,
2013 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f};
2016 image create bitmap file_plain -background white -foreground black -data {
2017 #define plain_width 14
2018 #define plain_height 15
2019 static unsigned char plain_bits[] = {
2020 0xfe, 0x01, 0x02, 0x03, 0x02, 0x05, 0x02, 0x09, 0x02, 0x1f, 0x02, 0x10,
2021 0x02, 0x10, 0x02, 0x10, 0x02, 0x10, 0x02, 0x10, 0x02, 0x10, 0x02, 0x10,
2022 0x02, 0x10, 0x02, 0x10, 0xfe, 0x1f};
2023 } -maskdata $filemask
2025 image create bitmap file_mod -background white -foreground blue -data {
2026 #define mod_width 14
2027 #define mod_height 15
2028 static unsigned char mod_bits[] = {
2029 0xfe, 0x01, 0x02, 0x03, 0x7a, 0x05, 0x02, 0x09, 0x7a, 0x1f, 0x02, 0x10,
2030 0xfa, 0x17, 0x02, 0x10, 0xfa, 0x17, 0x02, 0x10, 0xfa, 0x17, 0x02, 0x10,
2031 0xfa, 0x17, 0x02, 0x10, 0xfe, 0x1f};
2032 } -maskdata $filemask
2034 image create bitmap file_fulltick -background white -foreground "#007000" -data {
2035 #define file_fulltick_width 14
2036 #define file_fulltick_height 15
2037 static unsigned char file_fulltick_bits[] = {
2038 0xfe, 0x01, 0x02, 0x1a, 0x02, 0x0c, 0x02, 0x0c, 0x02, 0x16, 0x02, 0x16,
2039 0x02, 0x13, 0x00, 0x13, 0x86, 0x11, 0x8c, 0x11, 0xd8, 0x10, 0xf2, 0x10,
2040 0x62, 0x10, 0x02, 0x10, 0xfe, 0x1f};
2041 } -maskdata $filemask
2043 image create bitmap file_question -background white -foreground black -data {
2044 #define file_question_width 14
2045 #define file_question_height 15
2046 static unsigned char file_question_bits[] = {
2047 0xfe, 0x01, 0x02, 0x02, 0xe2, 0x04, 0xf2, 0x09, 0x1a, 0x1b, 0x0a, 0x13,
2048 0x82, 0x11, 0xc2, 0x10, 0x62, 0x10, 0x62, 0x10, 0x02, 0x10, 0x62, 0x10,
2049 0x62, 0x10, 0x02, 0x10, 0xfe, 0x1f};
2050 } -maskdata $filemask
2052 image create bitmap file_removed -background white -foreground red -data {
2053 #define file_removed_width 14
2054 #define file_removed_height 15
2055 static unsigned char file_removed_bits[] = {
2056 0xfe, 0x01, 0x02, 0x03, 0x02, 0x05, 0x02, 0x09, 0x02, 0x1f, 0x02, 0x10,
2057 0x1a, 0x16, 0x32, 0x13, 0xe2, 0x11, 0xc2, 0x10, 0xe2, 0x11, 0x32, 0x13,
2058 0x1a, 0x16, 0x02, 0x10, 0xfe, 0x1f};
2059 } -maskdata $filemask
2061 image create bitmap file_merge -background white -foreground blue -data {
2062 #define file_merge_width 14
2063 #define file_merge_height 15
2064 static unsigned char file_merge_bits[] = {
2065 0xfe, 0x01, 0x02, 0x03, 0x62, 0x05, 0x62, 0x09, 0x62, 0x1f, 0x62, 0x10,
2066 0xfa, 0x11, 0xf2, 0x10, 0x62, 0x10, 0x02, 0x10, 0xfa, 0x17, 0x02, 0x10,
2067 0xfa, 0x17, 0x02, 0x10, 0xfe, 0x1f};
2068 } -maskdata $filemask
2070 image create bitmap file_statechange -background white -foreground green -data {
2071 #define file_statechange_width 14
2072 #define file_statechange_height 15
2073 static unsigned char file_statechange_bits[] = {
2074 0xfe, 0x01, 0x02, 0x03, 0x02, 0x05, 0x02, 0x09, 0x02, 0x1f, 0x62, 0x10,
2075 0x62, 0x10, 0xba, 0x11, 0xba, 0x11, 0x62, 0x10, 0x62, 0x10, 0x02, 0x10,
2076 0x02, 0x10, 0x02, 0x10, 0xfe, 0x1f};
2077 } -maskdata $filemask
2079 set ui_index .vpane.files.index.list
2080 set ui_workdir .vpane.files.workdir.list
2082 set all_icons(_$ui_index) file_plain
2083 set all_icons(A$ui_index) file_plain
2084 set all_icons(M$ui_index) file_fulltick
2085 set all_icons(D$ui_index) file_removed
2086 set all_icons(U$ui_index) file_merge
2087 set all_icons(T$ui_index) file_statechange
2089 set all_icons(_$ui_workdir) file_plain
2090 set all_icons(M$ui_workdir) file_mod
2091 set all_icons(D$ui_workdir) file_question
2092 set all_icons(U$ui_workdir) file_merge
2093 set all_icons(O$ui_workdir) file_plain
2094 set all_icons(T$ui_workdir) file_statechange
2096 foreach i {
2097 {__ {mc "Unmodified"}}
2099 {_M {mc "Modified, not staged"}}
2100 {M_ {mc "Staged for commit"}}
2101 {MM {mc "Portions staged for commit"}}
2102 {MD {mc "Staged for commit, missing"}}
2104 {_T {mc "File type changed, not staged"}}
2105 {MT {mc "File type changed, old type staged for commit"}}
2106 {AT {mc "File type changed, old type staged for commit"}}
2107 {T_ {mc "File type changed, staged"}}
2108 {TM {mc "File type change staged, modification not staged"}}
2109 {TD {mc "File type change staged, file missing"}}
2111 {_O {mc "Untracked, not staged"}}
2112 {A_ {mc "Staged for commit"}}
2113 {AM {mc "Portions staged for commit"}}
2114 {AD {mc "Staged for commit, missing"}}
2116 {_D {mc "Missing"}}
2117 {D_ {mc "Staged for removal"}}
2118 {DO {mc "Staged for removal, still present"}}
2120 {_U {mc "Requires merge resolution"}}
2121 {U_ {mc "Requires merge resolution"}}
2122 {UU {mc "Requires merge resolution"}}
2123 {UM {mc "Requires merge resolution"}}
2124 {UD {mc "Requires merge resolution"}}
2125 {UT {mc "Requires merge resolution"}}
2127 set all_descs([lindex $i 0]) [eval [lindex $i 1]]
2129 unset i
2131 ######################################################################
2133 ## util
2135 proc scrollbar2many {list mode args} {
2136 foreach w $list {eval $w $mode $args}
2139 proc many2scrollbar {list mode sb top bottom} {
2140 $sb set $top $bottom
2141 foreach w $list {$w $mode moveto $top}
2144 # delegates mouse selection actions from gutter columns to the main text
2145 # widget
2146 # use delegator_bind, if you need to bind more actions
2147 proc delegate_sel_to {w from} {
2148 set bind_list [list \
2149 <Button-1> \
2150 <B1-Motion> \
2151 <Double-Button-1> \
2152 <Triple-Button-1> \
2153 <Shift-Button-1> \
2154 <Double-Shift-Button-1> \
2155 <Triple-Shift-Button-1> \
2158 foreach seq $bind_list {
2159 set script [bind Text $seq]
2160 set new_script [string map [list %W $w %x 0 word line] $script]
2161 foreach f $from {
2162 bind $f $seq "$new_script; focus $w; break"
2167 # use this for binding any of the mouse actions from a delegator
2168 # see bind_list in delegate_sel_to
2169 proc delegator_bind {tag seq script} {
2170 bind $tag $seq "$script; [bind $tag $seq]"
2173 proc incr_font_size {font {amt 1}} {
2174 set sz [font configure $font -size]
2175 incr sz $amt
2176 font configure $font -size $sz
2177 font configure ${font}bold -size $sz
2178 font configure ${font}italic -size $sz
2181 ######################################################################
2183 ## ui commands
2185 set starting_gitk_msg [mc "Starting gitk... please wait..."]
2187 proc do_gitk {revs {is_submodule false}} {
2188 global current_diff_path file_states current_diff_side ui_index
2189 global _gitdir _gitworktree
2191 # -- Always start gitk through whatever we were loaded with. This
2192 # lets us bypass using shell process on Windows systems.
2194 set exe [_which gitk -script]
2195 set cmd [list [info nameofexecutable] $exe]
2196 if {$exe eq {}} {
2197 error_popup [mc "Couldn't find gitk in PATH"]
2198 } else {
2199 global env
2201 set pwd [pwd]
2203 if {!$is_submodule} {
2204 if {![is_bare]} {
2205 cd $_gitworktree
2207 } else {
2208 cd $current_diff_path
2209 if {$revs eq {--}} {
2210 set s $file_states($current_diff_path)
2211 set old_sha1 {}
2212 set new_sha1 {}
2213 switch -glob -- [lindex $s 0] {
2214 M_ { set old_sha1 [lindex [lindex $s 2] 1] }
2215 _M { set old_sha1 [lindex [lindex $s 3] 1] }
2216 MM {
2217 if {$current_diff_side eq $ui_index} {
2218 set old_sha1 [lindex [lindex $s 2] 1]
2219 set new_sha1 [lindex [lindex $s 3] 1]
2220 } else {
2221 set old_sha1 [lindex [lindex $s 3] 1]
2225 set revs $old_sha1...$new_sha1
2227 # GIT_DIR and GIT_WORK_TREE for the submodule are not the ones
2228 # we've been using for the main repository, so unset them.
2229 # TODO we could make life easier (start up faster?) for gitk
2230 # by setting these to the appropriate values to allow gitk
2231 # to skip the heuristics to find their proper value
2232 unset env(GIT_DIR)
2233 unset env(GIT_WORK_TREE)
2235 eval exec $cmd $revs "--" "--" &
2237 set env(GIT_DIR) $_gitdir
2238 set env(GIT_WORK_TREE) $_gitworktree
2239 cd $pwd
2241 ui_status $::starting_gitk_msg
2242 after 10000 {
2243 ui_ready $starting_gitk_msg
2248 proc do_git_gui {} {
2249 global current_diff_path
2251 # -- Always start git gui through whatever we were loaded with. This
2252 # lets us bypass using shell process on Windows systems.
2254 set exe [list [_which git]]
2255 if {$exe eq {}} {
2256 error_popup [mc "Couldn't find git gui in PATH"]
2257 } else {
2258 global env
2259 global _gitdir _gitworktree
2261 # see note in do_gitk about unsetting these vars when
2262 # running tools in a submodule
2263 unset env(GIT_DIR)
2264 unset env(GIT_WORK_TREE)
2266 set pwd [pwd]
2267 cd $current_diff_path
2269 eval exec $exe gui &
2271 set env(GIT_DIR) $_gitdir
2272 set env(GIT_WORK_TREE) $_gitworktree
2273 cd $pwd
2275 ui_status $::starting_gitk_msg
2276 after 10000 {
2277 ui_ready $starting_gitk_msg
2282 proc do_explore {} {
2283 global _gitworktree
2284 set explorer {}
2285 if {[is_Cygwin] || [is_Windows]} {
2286 set explorer "explorer.exe"
2287 } elseif {[is_MacOSX]} {
2288 set explorer "open"
2289 } else {
2290 # freedesktop.org-conforming system is our best shot
2291 set explorer "xdg-open"
2293 eval exec $explorer [list [file nativename $_gitworktree]] &
2296 set is_quitting 0
2297 set ret_code 1
2299 proc terminate_me {win} {
2300 global ret_code
2301 if {$win ne {.}} return
2302 exit $ret_code
2305 proc do_quit {{rc {1}}} {
2306 global ui_comm is_quitting repo_config commit_type
2307 global GITGUI_BCK_exists GITGUI_BCK_i
2308 global ui_comm_spell
2309 global ret_code use_ttk
2311 if {$is_quitting} return
2312 set is_quitting 1
2314 if {[winfo exists $ui_comm]} {
2315 # -- Stash our current commit buffer.
2317 set save [gitdir GITGUI_MSG]
2318 if {$GITGUI_BCK_exists && ![$ui_comm edit modified]} {
2319 file rename -force [gitdir GITGUI_BCK] $save
2320 set GITGUI_BCK_exists 0
2321 } else {
2322 set msg [string trim [$ui_comm get 0.0 end]]
2323 regsub -all -line {[ \r\t]+$} $msg {} msg
2324 if {(![string match amend* $commit_type]
2325 || [$ui_comm edit modified])
2326 && $msg ne {}} {
2327 catch {
2328 set fd [open $save w]
2329 fconfigure $fd -encoding utf-8
2330 puts -nonewline $fd $msg
2331 close $fd
2333 } else {
2334 catch {file delete $save}
2338 # -- Cancel our spellchecker if its running.
2340 if {[info exists ui_comm_spell]} {
2341 $ui_comm_spell stop
2344 # -- Remove our editor backup, its not needed.
2346 after cancel $GITGUI_BCK_i
2347 if {$GITGUI_BCK_exists} {
2348 catch {file delete [gitdir GITGUI_BCK]}
2351 # -- Stash our current window geometry into this repository.
2353 set cfg_wmstate [wm state .]
2354 if {[catch {set rc_wmstate $repo_config(gui.wmstate)}]} {
2355 set rc_wmstate {}
2357 if {$cfg_wmstate ne $rc_wmstate} {
2358 catch {git config gui.wmstate $cfg_wmstate}
2360 if {$cfg_wmstate eq {zoomed}} {
2361 # on Windows wm geometry will lie about window
2362 # position (but not size) when window is zoomed
2363 # restore the window before querying wm geometry
2364 wm state . normal
2366 set cfg_geometry [list]
2367 lappend cfg_geometry [wm geometry .]
2368 if {$use_ttk} {
2369 lappend cfg_geometry [.vpane sashpos 0]
2370 lappend cfg_geometry [.vpane.files sashpos 0]
2371 } else {
2372 lappend cfg_geometry [lindex [.vpane sash coord 0] 0]
2373 lappend cfg_geometry [lindex [.vpane.files sash coord 0] 1]
2375 if {[catch {set rc_geometry $repo_config(gui.geometry)}]} {
2376 set rc_geometry {}
2378 if {$cfg_geometry ne $rc_geometry} {
2379 catch {git config gui.geometry $cfg_geometry}
2383 set ret_code $rc
2385 # Briefly enable send again, working around Tk bug
2386 # http://sourceforge.net/tracker/?func=detail&atid=112997&aid=1821174&group_id=12997
2387 tk appname [appname]
2389 destroy .
2392 proc do_rescan {} {
2393 rescan ui_ready
2396 proc ui_do_rescan {} {
2397 rescan {force_first_diff ui_ready}
2400 proc do_commit {} {
2401 commit_tree
2404 proc next_diff {{after {}}} {
2405 global next_diff_p next_diff_w next_diff_i
2406 show_diff $next_diff_p $next_diff_w {} {} $after
2409 proc find_anchor_pos {lst name} {
2410 set lid [lsearch -sorted -exact $lst $name]
2412 if {$lid == -1} {
2413 set lid 0
2414 foreach lname $lst {
2415 if {$lname >= $name} break
2416 incr lid
2420 return $lid
2423 proc find_file_from {flist idx delta path mmask} {
2424 global file_states
2426 set len [llength $flist]
2427 while {$idx >= 0 && $idx < $len} {
2428 set name [lindex $flist $idx]
2430 if {$name ne $path && [info exists file_states($name)]} {
2431 set state [lindex $file_states($name) 0]
2433 if {$mmask eq {} || [regexp $mmask $state]} {
2434 return $idx
2438 incr idx $delta
2441 return {}
2444 proc find_next_diff {w path {lno {}} {mmask {}}} {
2445 global next_diff_p next_diff_w next_diff_i
2446 global file_lists ui_index ui_workdir
2448 set flist $file_lists($w)
2449 if {$lno eq {}} {
2450 set lno [find_anchor_pos $flist $path]
2451 } else {
2452 incr lno -1
2455 if {$mmask ne {} && ![regexp {(^\^)|(\$$)} $mmask]} {
2456 if {$w eq $ui_index} {
2457 set mmask "^$mmask"
2458 } else {
2459 set mmask "$mmask\$"
2463 set idx [find_file_from $flist $lno 1 $path $mmask]
2464 if {$idx eq {}} {
2465 incr lno -1
2466 set idx [find_file_from $flist $lno -1 $path $mmask]
2469 if {$idx ne {}} {
2470 set next_diff_w $w
2471 set next_diff_p [lindex $flist $idx]
2472 set next_diff_i [expr {$idx+1}]
2473 return 1
2474 } else {
2475 return 0
2479 proc next_diff_after_action {w path {lno {}} {mmask {}}} {
2480 global current_diff_path
2482 if {$path ne $current_diff_path} {
2483 return {}
2484 } elseif {[find_next_diff $w $path $lno $mmask]} {
2485 return {next_diff;}
2486 } else {
2487 return {reshow_diff;}
2491 proc select_first_diff {after} {
2492 global ui_workdir
2494 if {[find_next_diff $ui_workdir {} 1 {^_?U}] ||
2495 [find_next_diff $ui_workdir {} 1 {[^O]$}]} {
2496 next_diff $after
2497 } else {
2498 uplevel #0 $after
2502 proc force_first_diff {after} {
2503 global ui_workdir current_diff_path file_states
2505 if {[info exists file_states($current_diff_path)]} {
2506 set state [lindex $file_states($current_diff_path) 0]
2507 } else {
2508 set state {OO}
2511 set reselect 0
2512 if {[string first {U} $state] >= 0} {
2513 # Already a conflict, do nothing
2514 } elseif {[find_next_diff $ui_workdir $current_diff_path {} {^_?U}]} {
2515 set reselect 1
2516 } elseif {[string index $state 1] ne {O}} {
2517 # Already a diff & no conflicts, do nothing
2518 } elseif {[find_next_diff $ui_workdir $current_diff_path {} {[^O]$}]} {
2519 set reselect 1
2522 if {$reselect} {
2523 next_diff $after
2524 } else {
2525 uplevel #0 $after
2529 # opens file in editor
2530 proc open_in_git_editor {path {lno 0} {edit_index 0}} {
2531 global env
2533 if {$edit_index} {
2534 if {[catch {
2535 set fd [git_read ls-files -s --error-unmatch -- $path]
2536 fconfigure $fd -translation binary -encoding binary
2537 set info [split [gets $fd] " \t"]
2538 close $fd
2539 set mode [lindex $info 0]
2540 set sha1 [lindex $info 1]
2541 set stage [lindex $info 2]
2542 if {$mode != 100644 && $mode != 100755} {
2543 return -code error "Path is not a regular file: $mode"
2545 if {$stage != 0} {
2546 return -code error "Path is in unmerged state."
2548 set tmp_path "[file rootname $path].[string range $sha1 0 11][file extension $path]"
2549 if {[file exists $tmp_path]} {
2550 return -code error "Path is currently edited."
2552 set fd [git_read checkout-index --temp -- $path]
2553 fconfigure $fd -translation binary -encoding binary
2554 set info [split [gets $fd] " \t"]
2555 close $fd
2556 set git_tmp_path [lindex $info 0]
2557 file rename $git_tmp_path $tmp_path
2558 unset git_tmp_path
2559 if {$mode == 100644} {
2560 file attributes $tmp_path -permissions 0600
2561 } else {
2562 file attributes $tmp_path -permissions 0700
2564 set orig_path $path
2565 set path $tmp_path
2566 unset tmp_path
2567 } err]} {
2568 if {[info exists tmp_path]} {
2569 file delete $tmp_path
2571 if {[info exists git_tmp_path]} {
2572 file delete $git_tmp_path
2574 tk_messageBox \
2575 -icon error \
2576 -type ok \
2577 -title {git-gui: Can't edit path from index} \
2578 -message $err
2579 return
2583 catch { unset env(ARGS) }
2584 catch { unset env(REVISION) }
2586 if {!$edit_index} {
2587 set env(GIT_EDITOR_F_NBLOCK) 1
2589 if {$lno != 0} {
2590 set env(GIT_EDITOR_F_POSITION) $lno
2593 if {[catch {exec [shellpath] -c "[git var GIT_EDITOR] [sq $path]"} err]} {
2594 tk_messageBox \
2595 -icon error \
2596 -type ok \
2597 -title {git-gui: Can't start GIT_EDITOR} \
2598 -message $err
2601 catch { unset env(GIT_EDITOR_F_NBLOCK) }
2602 catch { unset env(GIT_EDITOR_F_POSITION) }
2604 if {$edit_index} {
2605 if {[catch {
2606 set new_mode [file attributes $path -permissions]
2607 switch -glob $new_mode {
2608 ??6?? {
2609 set new_mode 100644
2611 ??7?? {
2612 set new_mode 100755
2615 return -code error "Invalid mode of edited file."
2618 set fd [git_read hash-object -w --path=[encoding convertto $orig_path] -- [encoding convertto $path]]
2619 fconfigure $fd -translation binary -encoding binary
2620 set new_sha1 [gets $fd]
2621 close $fd
2622 file delete $path
2623 set fd [git_write update-index -z --index-info]
2624 puts -nonewline $fd "$new_mode $new_sha1\t[encoding convertto $orig_path]\0"
2625 close $fd
2626 } err]} {
2627 file delete $path
2628 tk_messageBox \
2629 -icon error \
2630 -type ok \
2631 -title {git-gui: Can't edit path from index} \
2632 -message $err
2633 return
2635 do_rescan
2639 proc get_best_diff_lno {w lno} {
2640 set fwi [$w search -elide -regexp {^\d+$} $lno.0 end]
2641 if {$fwi eq {}} {
2642 set fwi end
2644 set bwi [$w search -elide -backwards -regexp {^\d+$} $lno.0 1.0]
2645 if {$bwi eq {}} {
2646 set bwi 1.0
2648 set fwl [$w count -lines $lno.0 $fwi]
2649 set bwl [$w count -lines $bwi $lno.0]
2650 if {$fwl <= $bwl} {
2651 set lno [$w get "$fwi linestart" "$fwi lineend"]
2652 } else {
2653 set lno [$w get "$bwi linestart" "$bwi lineend"]
2656 if {$lno eq {}} {
2657 set lno 0
2660 return $lno
2663 proc open_from_file_list {w x y {edit_index 0}} {
2664 global ui_diff ui_diff_blnos
2665 global file_lists current_diff_path
2667 set pos [split [$w index @$x,$y] .]
2668 set lno [lindex $pos 0]
2669 set col [lindex $pos 1]
2670 set path [lindex $file_lists($w) [expr {$lno - 1}]]
2671 if {$path eq {}} {
2672 return
2675 set lno 0
2676 if {$path eq $current_diff_path} {
2677 # calculate the line number which is visible in the middle
2678 set height [$ui_diff count -ypixels 1.0 end]
2679 set ypos [$ui_diff yview]
2680 set yposm [expr {([lindex $ypos 1] + [lindex $ypos 0]) / 2}]
2681 set mpixel [expr {$height * $yposm}]
2682 set ytop [expr {[lindex $ypos 0] * $height}]
2683 set rmpixel [expr {round($mpixel - $ytop)}]
2684 set lno [$ui_diff index "@0,$rmpixel linestart"]
2685 set lno [lindex [split $lno .] 0]
2687 set lno [get_best_diff_lno $ui_diff_blnos $lno]
2690 open_in_git_editor $path $lno $edit_index
2693 proc open_from_diff_view {x y {edit_index 0}} {
2694 global ui_diff ui_diff_blnos
2695 global file_lists current_diff_path
2697 if {$current_diff_path eq {}} {
2698 return
2701 set lno [$ui_diff index "@0,$y linestart"]
2702 set lno [lindex [split $lno .] 0]
2703 set lno [get_best_diff_lno $ui_diff_blnos $lno]
2705 open_in_git_editor $current_diff_path $lno $edit_index
2708 proc popup_files_ctxm {m w x y X Y} {
2709 global file_lists popup_path
2711 set ::cursorX $x
2712 set ::cursorY $y
2714 set pos [split [$w index @$x,$y] .]
2715 set lno [lindex $pos 0]
2716 set col [lindex $pos 1]
2717 set popup_path [lindex $file_lists($w) [expr {$lno - 1}]]
2718 if {$popup_path eq {}} {
2719 return
2721 tk_popup $m $X $Y
2724 proc toggle_or_diff {w x y} {
2725 global file_states file_lists current_diff_path ui_index ui_workdir
2726 global last_clicked selected_paths
2728 set pos [split [$w index @$x,$y] .]
2729 set lno [lindex $pos 0]
2730 set col [lindex $pos 1]
2731 set path [lindex $file_lists($w) [expr {$lno - 1}]]
2732 if {$path eq {}} {
2733 set last_clicked {}
2734 return
2737 set last_clicked [list $w $lno]
2738 array unset selected_paths
2739 $ui_index tag remove in_sel 0.0 end
2740 $ui_workdir tag remove in_sel 0.0 end
2742 # Determine the state of the file
2743 if {[info exists file_states($path)]} {
2744 set state [lindex $file_states($path) 0]
2745 } else {
2746 set state {__}
2749 # Restage the file, or simply show the diff
2750 if {$col == 0 && $y > 1} {
2751 # Conflicts need special handling
2752 if {[string first {U} $state] >= 0} {
2753 # $w must always be $ui_workdir, but...
2754 if {$w ne $ui_workdir} { set lno {} }
2755 merge_stage_workdir $path $lno
2756 return
2759 if {[string index $state 1] eq {O}} {
2760 set mmask {}
2761 } else {
2762 set mmask {[^O]}
2765 set after [next_diff_after_action $w $path $lno $mmask]
2767 if {$w eq $ui_index} {
2768 update_indexinfo \
2769 "Unstaging [short_path $path] from commit" \
2770 [list $path] \
2771 [concat $after [list ui_ready]]
2772 } elseif {$w eq $ui_workdir} {
2773 update_index \
2774 "Adding [short_path $path]" \
2775 [list $path] \
2776 [concat $after [list ui_ready]]
2778 } else {
2779 show_diff $path $w $lno
2783 proc add_one_to_selection {w x y} {
2784 global file_lists last_clicked selected_paths
2786 set lno [lindex [split [$w index @$x,$y] .] 0]
2787 set path [lindex $file_lists($w) [expr {$lno - 1}]]
2788 if {$path eq {}} {
2789 set last_clicked {}
2790 return
2793 if {$last_clicked ne {}
2794 && [lindex $last_clicked 0] ne $w} {
2795 array unset selected_paths
2796 [lindex $last_clicked 0] tag remove in_sel 0.0 end
2799 set last_clicked [list $w $lno]
2800 if {[catch {set in_sel $selected_paths($path)}]} {
2801 set in_sel 0
2803 if {$in_sel} {
2804 unset selected_paths($path)
2805 $w tag remove in_sel $lno.0 [expr {$lno + 1}].0
2806 } else {
2807 set selected_paths($path) 1
2808 $w tag add in_sel $lno.0 [expr {$lno + 1}].0
2812 proc add_range_to_selection {w x y} {
2813 global file_lists last_clicked selected_paths
2815 if {[lindex $last_clicked 0] ne $w} {
2816 toggle_or_diff $w $x $y
2817 return
2820 set lno [lindex [split [$w index @$x,$y] .] 0]
2821 set lc [lindex $last_clicked 1]
2822 if {$lc < $lno} {
2823 set begin $lc
2824 set end $lno
2825 } else {
2826 set begin $lno
2827 set end $lc
2830 foreach path [lrange $file_lists($w) \
2831 [expr {$begin - 1}] \
2832 [expr {$end - 1}]] {
2833 set selected_paths($path) 1
2835 $w tag add in_sel $begin.0 [expr {$end + 1}].0
2838 proc show_more_context {} {
2839 global repo_config
2840 if {$repo_config(gui.diffcontext) < 99} {
2841 incr repo_config(gui.diffcontext)
2842 reshow_diff
2846 proc show_less_context {} {
2847 global repo_config
2848 if {$repo_config(gui.diffcontext) > 1} {
2849 incr repo_config(gui.diffcontext) -1
2850 reshow_diff
2854 ######################################################################
2856 ## ui construction
2858 set ui_comm {}
2860 # -- Menu Bar
2862 menu .mbar -tearoff 0
2863 if {[is_MacOSX]} {
2864 # -- Apple Menu (Mac OS X only)
2866 .mbar add cascade -label Apple -menu .mbar.apple
2867 menu .mbar.apple
2869 .mbar add cascade -label [mc Repository] -menu .mbar.repository
2870 .mbar add cascade -label [mc Edit] -menu .mbar.edit
2871 if {[is_enabled branch]} {
2872 .mbar add cascade -label [mc Branch] -menu .mbar.branch
2874 if {[is_enabled multicommit] || [is_enabled singlecommit]} {
2875 .mbar add cascade -label [mc Commit@@noun] -menu .mbar.commit
2877 if {[is_enabled transport]} {
2878 .mbar add cascade -label [mc Merge] -menu .mbar.merge
2879 .mbar add cascade -label [mc Remote] -menu .mbar.remote
2881 if {[is_enabled multicommit] || [is_enabled singlecommit]} {
2882 .mbar add cascade -label [mc Tools] -menu .mbar.tools
2885 # -- Repository Menu
2887 menu .mbar.repository
2889 if {![is_bare]} {
2890 .mbar.repository add command \
2891 -label [mc "Explore Working Copy"] \
2892 -command {do_explore}
2895 if {[is_Windows]} {
2896 .mbar.repository add command \
2897 -label [mc "Git Bash"] \
2898 -command {eval exec [auto_execok start] \
2899 [list "Git Bash" bash --login -l &]}
2902 if {[is_Windows] || ![is_bare]} {
2903 .mbar.repository add separator
2906 .mbar.repository add command \
2907 -label [mc "Browse Current Branch's Files"] \
2908 -command {browser::new $current_branch}
2909 set ui_browse_current [.mbar.repository index last]
2910 .mbar.repository add command \
2911 -label [mc "Browse Branch Files..."] \
2912 -command browser_open::dialog
2913 .mbar.repository add separator
2915 .mbar.repository add command \
2916 -label [mc "Visualize Current Branch's History"] \
2917 -command {do_gitk $current_branch}
2918 set ui_visualize_current [.mbar.repository index last]
2919 .mbar.repository add command \
2920 -label [mc "Visualize All Branch History"] \
2921 -command {do_gitk --all}
2922 .mbar.repository add separator
2924 proc current_branch_write {args} {
2925 global current_branch
2926 .mbar.repository entryconf $::ui_browse_current \
2927 -label [mc "Browse %s's Files" $current_branch]
2928 .mbar.repository entryconf $::ui_visualize_current \
2929 -label [mc "Visualize %s's History" $current_branch]
2931 trace add variable current_branch write current_branch_write
2933 if {[is_enabled multicommit]} {
2934 .mbar.repository add command -label [mc "Database Statistics"] \
2935 -command do_stats
2937 .mbar.repository add command -label [mc "Compress Database"] \
2938 -command do_gc
2940 .mbar.repository add command -label [mc "Verify Database"] \
2941 -command do_fsck_objects
2943 .mbar.repository add separator
2945 if {[is_Cygwin]} {
2946 .mbar.repository add command \
2947 -label [mc "Create Desktop Icon"] \
2948 -command do_cygwin_shortcut
2949 } elseif {[is_Windows]} {
2950 .mbar.repository add command \
2951 -label [mc "Create Desktop Icon"] \
2952 -command do_windows_shortcut
2953 } elseif {[is_MacOSX]} {
2954 .mbar.repository add command \
2955 -label [mc "Create Desktop Icon"] \
2956 -command do_macosx_app
2960 if {[is_MacOSX]} {
2961 proc ::tk::mac::Quit {args} { do_quit }
2962 } else {
2963 .mbar.repository add command -label [mc Quit] \
2964 -command do_quit \
2965 -accelerator $M1T-Q
2968 # -- Edit Menu
2970 menu .mbar.edit
2971 .mbar.edit add command -label [mc Undo] \
2972 -command {catch {[focus] edit undo}} \
2973 -accelerator $M1T-Z
2974 .mbar.edit add command -label [mc Redo] \
2975 -command {catch {[focus] edit redo}} \
2976 -accelerator $M1T-Y
2977 .mbar.edit add separator
2978 .mbar.edit add command -label [mc Cut] \
2979 -command {catch {tk_textCut [focus]}} \
2980 -accelerator $M1T-X
2981 .mbar.edit add command -label [mc Copy] \
2982 -command {catch {tk_textCopy [focus]}} \
2983 -accelerator $M1T-C
2984 .mbar.edit add command -label [mc Paste] \
2985 -command {catch {tk_textPaste [focus]; [focus] see insert}} \
2986 -accelerator $M1T-V
2987 .mbar.edit add command -label [mc Delete] \
2988 -command {catch {[focus] delete sel.first sel.last}} \
2989 -accelerator Del
2990 .mbar.edit add separator
2991 .mbar.edit add command -label [mc "Select All"] \
2992 -command {catch {[focus] tag add sel 0.0 end}} \
2993 -accelerator $M1T-A
2995 # -- Branch Menu
2997 if {[is_enabled branch]} {
2998 menu .mbar.branch
3000 .mbar.branch add command -label [mc "Create..."] \
3001 -command branch_create::dialog \
3002 -accelerator $M1T-N
3003 lappend disable_on_lock [list .mbar.branch entryconf \
3004 [.mbar.branch index last] -state]
3006 .mbar.branch add command -label [mc "Checkout..."] \
3007 -command branch_checkout::dialog \
3008 -accelerator $M1T-O
3009 lappend disable_on_lock [list .mbar.branch entryconf \
3010 [.mbar.branch index last] -state]
3012 .mbar.branch add command -label [mc "Rename..."] \
3013 -command branch_rename::dialog
3014 lappend disable_on_lock [list .mbar.branch entryconf \
3015 [.mbar.branch index last] -state]
3017 .mbar.branch add command -label [mc "Delete..."] \
3018 -command branch_delete::dialog
3019 lappend disable_on_lock [list .mbar.branch entryconf \
3020 [.mbar.branch index last] -state]
3022 .mbar.branch add command -label [mc "Reset..."] \
3023 -command merge::reset_hard
3024 lappend disable_on_lock [list .mbar.branch entryconf \
3025 [.mbar.branch index last] -state]
3028 # -- Commit Menu
3030 proc commit_btn_caption {} {
3031 if {[is_enabled nocommit]} {
3032 return [mc "Done"]
3033 } else {
3034 return [mc Commit@@verb]
3038 if {[is_enabled multicommit] || [is_enabled singlecommit]} {
3039 menu .mbar.commit
3041 if {![is_enabled nocommit]} {
3042 .mbar.commit add radiobutton \
3043 -label [mc "New Commit"] \
3044 -command do_select_commit_type \
3045 -variable selected_commit_type \
3046 -value new
3047 lappend disable_on_lock \
3048 [list .mbar.commit entryconf [.mbar.commit index last] -state]
3050 .mbar.commit add radiobutton \
3051 -label [mc "Amend Last Commit"] \
3052 -command do_select_commit_type \
3053 -variable selected_commit_type \
3054 -value amend
3055 lappend disable_on_lock \
3056 [list .mbar.commit entryconf [.mbar.commit index last] -state]
3058 .mbar.commit add separator
3061 .mbar.commit add command -label [mc Rescan] \
3062 -command ui_do_rescan \
3063 -accelerator $M1T-R
3064 lappend disable_on_lock \
3065 [list .mbar.commit entryconf [.mbar.commit index last] -state]
3067 .mbar.commit add command -label [mc "Stage To Commit"] \
3068 -command do_add_selection \
3069 -accelerator $M1T-T
3070 lappend disable_on_lock \
3071 [list .mbar.commit entryconf [.mbar.commit index last] -state]
3073 .mbar.commit add command -label [mc "Stage Changed Files To Commit"] \
3074 -command do_add_all \
3075 -accelerator $M1T-I
3076 lappend disable_on_lock \
3077 [list .mbar.commit entryconf [.mbar.commit index last] -state]
3079 .mbar.commit add command -label [mc "Unstage From Commit"] \
3080 -command do_unstage_selection \
3081 -accelerator $M1T-U
3082 lappend disable_on_lock \
3083 [list .mbar.commit entryconf [.mbar.commit index last] -state]
3085 .mbar.commit add command -label [mc "Revert Changes"] \
3086 -command do_revert_selection \
3087 -accelerator $M1T-J
3088 lappend disable_on_lock \
3089 [list .mbar.commit entryconf [.mbar.commit index last] -state]
3091 .mbar.commit add separator
3093 .mbar.commit add command -label [mc "Show Less Context"] \
3094 -command show_less_context \
3095 -accelerator $M1T-\-
3097 .mbar.commit add command -label [mc "Show More Context"] \
3098 -command show_more_context \
3099 -accelerator $M1T-=
3101 .mbar.commit add checkbutton -label [mc "Show Line Numbers"] \
3102 -command update_show_line_numbers \
3103 -variable diff_show_line_numbers
3105 .mbar.commit add separator
3107 if {![is_enabled nocommitmsg]} {
3108 .mbar.commit add command -label [mc "Sign Off"] \
3109 -command do_signoff \
3110 -accelerator $M1T-S
3113 .mbar.commit add command -label [commit_btn_caption] \
3114 -command do_commit \
3115 -accelerator $M1T-Return
3116 lappend disable_on_lock \
3117 [list .mbar.commit entryconf [.mbar.commit index last] -state]
3120 # -- Merge Menu
3122 if {[is_enabled branch]} {
3123 menu .mbar.merge
3124 .mbar.merge add command -label [mc "Local Merge..."] \
3125 -command merge::dialog \
3126 -accelerator $M1T-M
3127 lappend disable_on_lock \
3128 [list .mbar.merge entryconf [.mbar.merge index last] -state]
3129 .mbar.merge add command -label [mc "Abort Merge..."] \
3130 -command merge::reset_hard
3131 lappend disable_on_lock \
3132 [list .mbar.merge entryconf [.mbar.merge index last] -state]
3135 # -- Transport Menu
3137 if {[is_enabled transport]} {
3138 menu .mbar.remote
3140 .mbar.remote add command \
3141 -label [mc "Add..."] \
3142 -command remote_add::dialog \
3143 -accelerator $M1T-A
3144 .mbar.remote add command \
3145 -label [mc "Push..."] \
3146 -command do_push_anywhere \
3147 -accelerator $M1T-P
3148 .mbar.remote add command \
3149 -label [mc "Delete Branch..."] \
3150 -command remote_branch_delete::dialog
3153 if {[is_MacOSX]} {
3154 proc ::tk::mac::ShowPreferences {} {do_options}
3155 } else {
3156 # -- Edit Menu
3158 .mbar.edit add separator
3159 .mbar.edit add command -label [mc "Options..."] \
3160 -command do_options
3163 # -- Tools Menu
3165 if {[is_enabled multicommit] || [is_enabled singlecommit]} {
3166 set tools_menubar .mbar.tools
3167 menu $tools_menubar
3168 $tools_menubar add separator
3169 $tools_menubar add command -label [mc "Add..."] -command tools_add::dialog
3170 $tools_menubar add command -label [mc "Remove..."] -command tools_remove::dialog
3171 if {[array names repo_config guitool.*.cmd] ne {}} {
3172 tools_populate_all 3
3176 # -- Help Menu
3178 .mbar add cascade -label [mc Help] -menu .mbar.help
3179 menu .mbar.help
3181 if {[is_MacOSX]} {
3182 .mbar.apple add command -label [mc "About %s" [appname]] \
3183 -command do_about
3184 .mbar.apple add separator
3185 } else {
3186 .mbar.help add command -label [mc "About %s" [appname]] \
3187 -command do_about
3189 . configure -menu .mbar
3191 set doc_path [githtmldir]
3192 if {$doc_path ne {}} {
3193 set doc_path [file join $doc_path index.html]
3195 if {[is_Cygwin]} {
3196 set doc_path [exec cygpath --mixed $doc_path]
3200 if {[file isfile $doc_path]} {
3201 set doc_url "file:$doc_path"
3202 } else {
3203 set doc_url {http://www.kernel.org/pub/software/scm/git/docs/}
3206 proc start_browser {url} {
3207 git "web--browse" $url
3210 .mbar.help add command -label [mc "Online Documentation"] \
3211 -command [list start_browser $doc_url]
3213 .mbar.help add command -label [mc "Show SSH Key"] \
3214 -command do_ssh_key
3216 unset doc_path doc_url
3218 # -- Standard bindings
3220 wm protocol . WM_DELETE_WINDOW do_quit
3221 bind all <$M1B-Key-q> do_quit
3222 bind all <$M1B-Key-Q> do_quit
3223 bind all <$M1B-Key-w> {destroy [winfo toplevel %W]}
3224 bind all <$M1B-Key-W> {destroy [winfo toplevel %W]}
3226 set subcommand_args {}
3227 proc usage {} {
3228 set s "usage: $::argv0 $::subcommand $::subcommand_args"
3229 if {[tk windowingsystem] eq "win32"} {
3230 wm withdraw .
3231 tk_messageBox -icon info -message $s \
3232 -title [mc "Usage"]
3233 } else {
3234 puts stderr $s
3236 exit 1
3239 proc normalize_relpath {path} {
3240 set elements {}
3241 foreach item [file split $path] {
3242 if {$item eq {.}} continue
3243 if {$item eq {..} && [llength $elements] > 0
3244 && [lindex $elements end] ne {..}} {
3245 set elements [lrange $elements 0 end-1]
3246 continue
3248 lappend elements $item
3250 return [eval file join $elements]
3253 # -- Not a normal commit type invocation? Do that instead!
3255 switch -- $subcommand {
3256 browser -
3257 blame {
3258 if {$subcommand eq "blame"} {
3259 set subcommand_args {[--line=<num>] rev? path}
3260 } else {
3261 set subcommand_args {rev? path}
3263 if {$argv eq {}} usage
3264 set head {}
3265 set path {}
3266 set jump_spec {}
3267 set is_path 0
3268 foreach a $argv {
3269 set p [file join $_prefix $a]
3271 if {$is_path || [file exists $p]} {
3272 if {$path ne {}} usage
3273 set path [normalize_relpath $p]
3274 break
3275 } elseif {$a eq {--}} {
3276 if {$path ne {}} {
3277 if {$head ne {}} usage
3278 set head $path
3279 set path {}
3281 set is_path 1
3282 } elseif {[regexp {^--line=(\d+)$} $a a lnum]} {
3283 if {$jump_spec ne {} || $head ne {}} usage
3284 set jump_spec [list $lnum]
3285 } elseif {$head eq {}} {
3286 if {$head ne {}} usage
3287 set head $a
3288 set is_path 1
3289 } else {
3290 usage
3293 unset is_path
3295 if {$head ne {} && $path eq {}} {
3296 if {[string index $head 0] eq {/}} {
3297 set path [normalize_relpath $head]
3298 set head {}
3299 } else {
3300 set path [normalize_relpath $_prefix$head]
3301 set head {}
3305 if {$head eq {}} {
3306 load_current_branch
3307 } else {
3308 if {[regexp {^[0-9a-f]{1,39}$} $head]} {
3309 if {[catch {
3310 set head [git rev-parse --verify $head]
3311 } err]} {
3312 if {[tk windowingsystem] eq "win32"} {
3313 tk_messageBox -icon error -title [mc Error] -message $err
3314 } else {
3315 puts stderr $err
3317 exit 1
3320 set current_branch $head
3323 wm deiconify .
3324 switch -- $subcommand {
3325 browser {
3326 if {$jump_spec ne {}} usage
3327 if {$head eq {}} {
3328 if {$path ne {} && [file isdirectory $path]} {
3329 set head $current_branch
3330 } else {
3331 set head $path
3332 set path {}
3335 browser::new $head $path
3337 blame {
3338 if {$head eq {} && ![file exists $path]} {
3339 catch {wm withdraw .}
3340 tk_messageBox \
3341 -icon error \
3342 -type ok \
3343 -title [mc "git-gui: fatal error"] \
3344 -message [mc "fatal: cannot stat path %s: No such file or directory" $path]
3345 exit 1
3347 blame::new $head $path $jump_spec
3350 return
3352 citool -
3353 gui {
3354 if {[llength $argv] != 0} {
3355 usage
3357 # fall through to setup UI for commits
3359 default {
3360 set err "usage: $argv0 \[{blame|browser|citool}\]"
3361 if {[tk windowingsystem] eq "win32"} {
3362 wm withdraw .
3363 tk_messageBox -icon error -message $err \
3364 -title [mc "Usage"]
3365 } else {
3366 puts stderr $err
3368 exit 1
3372 # -- Branch Control
3374 ${NS}::frame .branch
3375 if {!$use_ttk} {.branch configure -borderwidth 1 -relief sunken}
3376 ${NS}::label .branch.l1 \
3377 -text [mc "Current Branch:"] \
3378 -anchor w \
3379 -justify left
3380 ${NS}::label .branch.cb \
3381 -textvariable current_branch \
3382 -anchor w \
3383 -justify left
3384 pack .branch.l1 -side left
3385 pack .branch.cb -side left -fill x
3386 pack .branch -side top -fill x
3388 # -- Main Window Layout
3390 ${NS}::panedwindow .vpane -orient horizontal
3391 ${NS}::panedwindow .vpane.files -orient vertical
3392 if {$use_ttk} {
3393 .vpane add .vpane.files -weight 0
3394 } else {
3395 .vpane add .vpane.files -sticky nsew -height 100 -width 200
3397 pack .vpane -anchor n -side top -fill both -expand 1
3399 # -- Index File List
3401 ${NS}::frame .vpane.files.index -height 100 -width 200
3402 tlabel .vpane.files.index.title \
3403 -text [mc "Staged Changes (Will Commit)"] \
3404 -background lightgreen -foreground black
3405 text $ui_index -background white -foreground black \
3406 -borderwidth 0 \
3407 -width 20 -height 10 \
3408 -wrap none \
3409 -cursor $cursor_ptr \
3410 -xscrollcommand {.vpane.files.index.sx set} \
3411 -yscrollcommand {.vpane.files.index.sy set} \
3412 -state disabled
3413 ${NS}::scrollbar .vpane.files.index.sx -orient h -command [list $ui_index xview]
3414 ${NS}::scrollbar .vpane.files.index.sy -orient v -command [list $ui_index yview]
3415 pack .vpane.files.index.title -side top -fill x
3416 pack .vpane.files.index.sx -side bottom -fill x
3417 pack .vpane.files.index.sy -side right -fill y
3418 pack $ui_index -side left -fill both -expand 1
3420 # -- Working Directory File List
3422 ${NS}::frame .vpane.files.workdir -height 100 -width 200
3423 tlabel .vpane.files.workdir.title -text [mc "Unstaged Changes"] \
3424 -background lightsalmon -foreground black
3425 text $ui_workdir -background white -foreground black \
3426 -borderwidth 0 \
3427 -width 20 -height 10 \
3428 -wrap none \
3429 -cursor $cursor_ptr \
3430 -xscrollcommand {.vpane.files.workdir.sx set} \
3431 -yscrollcommand {.vpane.files.workdir.sy set} \
3432 -state disabled
3433 ${NS}::scrollbar .vpane.files.workdir.sx -orient h -command [list $ui_workdir xview]
3434 ${NS}::scrollbar .vpane.files.workdir.sy -orient v -command [list $ui_workdir yview]
3435 pack .vpane.files.workdir.title -side top -fill x
3436 pack .vpane.files.workdir.sx -side bottom -fill x
3437 pack .vpane.files.workdir.sy -side right -fill y
3438 pack $ui_workdir -side left -fill both -expand 1
3440 if {$use_ttk} {
3441 .vpane.files add .vpane.files.workdir -weight 1
3442 .vpane.files add .vpane.files.index -weight 1
3443 } else {
3444 .vpane.files add .vpane.files.workdir -sticky news
3445 .vpane.files add .vpane.files.index -sticky news
3448 foreach i [list $ui_index $ui_workdir] {
3449 rmsel_tag $i
3450 $i tag conf in_diff -background [$i tag cget in_sel -background]
3451 #$i tag conf in_diff -underline 1
3452 #$i tag conf in_sel -background {#c6e2ff} -font font_diffitalic
3453 $i tag conf in_sel -background [$i cget -background] -underline 1
3455 unset i
3457 set files_ctxm .vpane.files.ctxm
3458 menu $files_ctxm -tearoff 0
3459 $files_ctxm add separator
3460 $files_ctxm add command -label [mc "Open in Editor"] -command {open_from_file_list $current_diff_side $cursorX $cursorY}
3461 $files_ctxm add command -label [mc "Stage All Changed"] -command do_add_all
3462 $files_ctxm add command -label [mc "Stage Selected"] -command do_add_selection
3463 if {[array names repo_config guitool.*.cmd] ne {}} {
3464 files_tools_populate_all 4 popup_path
3468 # -- Diff and Commit Area
3470 if {$have_tk85} {
3471 ${NS}::panedwindow .vpane.lower -orient vertical
3472 ${NS}::frame .vpane.lower.commarea
3473 ${NS}::frame .vpane.lower.diff -relief sunken -borderwidth 1 -height 500
3474 .vpane.lower add .vpane.lower.diff
3475 .vpane.lower add .vpane.lower.commarea
3476 .vpane add .vpane.lower
3477 if {$use_ttk} {
3478 .vpane.lower pane .vpane.lower.diff -weight 1
3479 .vpane.lower pane .vpane.lower.commarea -weight 0
3480 } else {
3481 .vpane.lower paneconfigure .vpane.lower.diff -stretch always
3482 .vpane.lower paneconfigure .vpane.lower.commarea -stretch never
3484 } else {
3485 frame .vpane.lower -height 300 -width 400
3486 frame .vpane.lower.commarea
3487 frame .vpane.lower.diff -relief sunken -borderwidth 1
3488 pack .vpane.lower.diff -fill both -expand 1
3489 pack .vpane.lower.commarea -side bottom -fill x
3490 .vpane add .vpane.lower
3491 .vpane paneconfigure .vpane.lower -sticky nsew
3494 # -- Commit Area Buttons
3496 ${NS}::frame .vpane.lower.commarea.buttons
3497 ${NS}::label .vpane.lower.commarea.buttons.l -text {} \
3498 -anchor w \
3499 -justify left
3500 pack .vpane.lower.commarea.buttons.l -side top -fill x
3501 pack .vpane.lower.commarea.buttons -side left -fill y
3503 ${NS}::button .vpane.lower.commarea.buttons.rescan -text [mc Rescan] \
3504 -command ui_do_rescan
3505 pack .vpane.lower.commarea.buttons.rescan -side top -fill x
3506 lappend disable_on_lock \
3507 {.vpane.lower.commarea.buttons.rescan conf -state}
3509 ${NS}::button .vpane.lower.commarea.buttons.incall -text [mc "Stage Changed"] \
3510 -command do_add_all
3511 pack .vpane.lower.commarea.buttons.incall -side top -fill x
3512 lappend disable_on_lock \
3513 {.vpane.lower.commarea.buttons.incall conf -state}
3515 if {![is_enabled nocommitmsg]} {
3516 ${NS}::button .vpane.lower.commarea.buttons.signoff -text [mc "Sign Off"] \
3517 -command do_signoff
3518 pack .vpane.lower.commarea.buttons.signoff -side top -fill x
3521 ${NS}::button .vpane.lower.commarea.buttons.commit -text [commit_btn_caption] \
3522 -command do_commit
3523 pack .vpane.lower.commarea.buttons.commit -side top -fill x
3524 lappend disable_on_lock \
3525 {.vpane.lower.commarea.buttons.commit conf -state}
3527 if {![is_enabled nocommit]} {
3528 ${NS}::button .vpane.lower.commarea.buttons.push -text [mc Push] \
3529 -command do_push_anywhere
3530 pack .vpane.lower.commarea.buttons.push -side top -fill x
3533 # -- Commit Message Buffer
3535 ${NS}::frame .vpane.lower.commarea.buffer
3536 ${NS}::frame .vpane.lower.commarea.buffer.header
3537 set ui_comm .vpane.lower.commarea.buffer.t
3538 set ui_coml .vpane.lower.commarea.buffer.header.l
3540 proc ignore_yscrollcommand {top bottom} {}
3542 proc update_show_line_numbers {} {
3543 global ui_diff_alnos ui_diff_blnos ui_diff_clnos
3544 global diff_show_line_numbers is_3way_diff is_other_diff
3545 global ui_diff ui_diff_columns ui_diff_columns_to_scroll
3547 # remember current sscroll position
3548 set current_pos [.vpane.lower.diff.body.sby get]
3550 # remove all line number columns
3551 grid remove $ui_diff_alnos $ui_diff_blnos $ui_diff_clnos
3552 set ui_diff_columns_to_scroll [list $ui_diff]
3553 foreach i $ui_diff_columns {
3554 $i conf -yscrollcommand {}
3557 if {$diff_show_line_numbers} {
3558 if {!$is_other_diff} {
3559 grid configure $ui_diff_alnos
3560 lappend ui_diff_columns_to_scroll $ui_diff_alnos
3562 grid configure $ui_diff_blnos
3563 lappend ui_diff_columns_to_scroll $ui_diff_blnos
3564 if {$is_3way_diff} {
3565 grid configure $ui_diff_clnos
3566 lappend ui_diff_columns_to_scroll $ui_diff_clnos
3570 foreach i $ui_diff_columns_to_scroll {
3571 $i conf -yscrollcommand \
3572 "[list many2scrollbar $ui_diff_columns_to_scroll yview .vpane.lower.diff.body.sby]"
3574 .vpane.lower.diff.body.sby conf \
3575 -command [list scrollbar2many $ui_diff_columns_to_scroll yview]
3577 # restore current scroll position
3578 many2scrollbar $ui_diff_columns_to_scroll yview .vpane.lower.diff.body.sby [lindex $current_pos 0] [lindex $current_pos 1]
3581 if {![is_enabled nocommit]} {
3582 ${NS}::radiobutton .vpane.lower.commarea.buffer.header.new \
3583 -text [mc "New Commit"] \
3584 -command do_select_commit_type \
3585 -variable selected_commit_type \
3586 -value new
3587 lappend disable_on_lock \
3588 [list .vpane.lower.commarea.buffer.header.new conf -state]
3589 ${NS}::radiobutton .vpane.lower.commarea.buffer.header.amend \
3590 -text [mc "Amend Last Commit"] \
3591 -command do_select_commit_type \
3592 -variable selected_commit_type \
3593 -value amend
3594 lappend disable_on_lock \
3595 [list .vpane.lower.commarea.buffer.header.amend conf -state]
3598 ${NS}::label $ui_coml \
3599 -anchor w \
3600 -justify left
3601 proc trace_commit_type {varname args} {
3602 global ui_coml commit_type
3603 switch -glob -- $commit_type {
3604 initial {set txt [mc "Initial Commit Message:"]}
3605 amend {set txt [mc "Amended Commit Message:"]}
3606 amend-initial {set txt [mc "Amended Initial Commit Message:"]}
3607 amend-merge {set txt [mc "Amended Merge Commit Message:"]}
3608 merge {set txt [mc "Merge Commit Message:"]}
3609 * {set txt [mc "Commit Message:"]}
3611 $ui_coml conf -text $txt
3613 trace add variable commit_type write trace_commit_type
3614 pack $ui_coml -side left -fill x
3616 if {![is_enabled nocommit]} {
3617 pack .vpane.lower.commarea.buffer.header.amend -side right
3618 pack .vpane.lower.commarea.buffer.header.new -side right
3621 text $ui_comm -background white -foreground black \
3622 -borderwidth 1 \
3623 -undo true \
3624 -maxundo 20 \
3625 -autoseparators true \
3626 -relief sunken \
3627 -width $repo_config(gui.commitmsgwidth) -height 9 -wrap none \
3628 -font font_diff \
3629 -yscrollcommand {.vpane.lower.commarea.buffer.sby set}
3630 ${NS}::scrollbar .vpane.lower.commarea.buffer.sby \
3631 -command [list $ui_comm yview]
3632 pack .vpane.lower.commarea.buffer.header -side top -fill x
3633 pack .vpane.lower.commarea.buffer.sby -side right -fill y
3634 pack $ui_comm -side left -fill y
3635 pack .vpane.lower.commarea.buffer -side left -fill y
3637 # -- Commit Message Buffer Context Menu
3639 set ui_comm_ctxm .vpane.lower.commarea.buffer.ctxm
3640 menu $ui_comm_ctxm -tearoff 0
3641 $ui_comm_ctxm add command \
3642 -label [mc Cut] \
3643 -command {tk_textCut $ui_comm}
3644 $ui_comm_ctxm add command \
3645 -label [mc Copy] \
3646 -command {tk_textCopy $ui_comm}
3647 $ui_comm_ctxm add command \
3648 -label [mc Paste] \
3649 -command {tk_textPaste $ui_comm}
3650 $ui_comm_ctxm add command \
3651 -label [mc Delete] \
3652 -command {catch {$ui_comm delete sel.first sel.last}}
3653 $ui_comm_ctxm add separator
3654 $ui_comm_ctxm add command \
3655 -label [mc "Select All"] \
3656 -command {focus $ui_comm;$ui_comm tag add sel 0.0 end}
3657 $ui_comm_ctxm add command \
3658 -label [mc "Copy All"] \
3659 -command {
3660 $ui_comm tag add sel 0.0 end
3661 tk_textCopy $ui_comm
3662 $ui_comm tag remove sel 0.0 end
3664 $ui_comm_ctxm add separator
3665 $ui_comm_ctxm add command \
3666 -label [mc "Sign Off"] \
3667 -command do_signoff
3669 # -- Diff Header
3671 proc trace_current_diff_path {varname args} {
3672 global current_diff_path diff_actions file_states
3673 if {$current_diff_path eq {}} {
3674 set s {}
3675 set f {}
3676 set p {}
3677 set o disabled
3678 } else {
3679 set p $current_diff_path
3680 set s [mapdesc [lindex $file_states($p) 0] $p]
3681 set f [mc "File:"]
3682 set p [escape_path $p]
3683 set o normal
3686 .vpane.lower.diff.header.status configure -text $s
3687 .vpane.lower.diff.header.file configure -text $f
3688 .vpane.lower.diff.header.path configure -text $p
3689 foreach w $diff_actions {
3690 uplevel #0 $w $o
3693 trace add variable current_diff_path write trace_current_diff_path
3695 gold_frame .vpane.lower.diff.header
3696 tlabel .vpane.lower.diff.header.status \
3697 -background gold \
3698 -foreground black \
3699 -anchor w \
3700 -justify left
3701 tlabel .vpane.lower.diff.header.file \
3702 -background gold \
3703 -foreground black \
3704 -anchor w \
3705 -justify left
3706 tlabel .vpane.lower.diff.header.path \
3707 -background gold \
3708 -foreground black \
3709 -anchor w \
3710 -justify left
3711 pack .vpane.lower.diff.header.status -side left
3712 pack .vpane.lower.diff.header.path -side right -fill x
3713 pack .vpane.lower.diff.header.file -side right
3714 set hctxm .vpane.lower.diff.header.ctxm
3715 menu $hctxm -tearoff 0
3716 $hctxm add command \
3717 -label [mc Copy] \
3718 -command {
3719 clipboard clear
3720 clipboard append \
3721 -format STRING \
3722 -type STRING \
3723 -- $current_diff_path
3725 lappend diff_actions [list $hctxm entryconf [$hctxm index last] -state]
3726 bind_button3 .vpane.lower.diff.header.path "tk_popup $hctxm %X %Y"
3728 # -- Diff Body
3730 ${NS}::frame .vpane.lower.diff.body
3731 set ui_diff .vpane.lower.diff.body.t
3732 # for 3way merges
3733 set ui_diff_clnos .vpane.lower.diff.body.c
3734 # - lines
3735 set ui_diff_alnos .vpane.lower.diff.body.a
3736 # + lines
3737 set ui_diff_blnos .vpane.lower.diff.body.b
3739 set ui_diff_lno_col_width 2
3741 text $ui_diff_alnos \
3742 -takefocus 0 \
3743 -highlightthickness 0 \
3744 -padx 0 -pady 0 \
3745 -background grey90 \
3746 -foreground black \
3747 -borderwidth 0 \
3748 -width [expr $ui_diff_lno_col_width + 2] \
3749 -height 5 \
3750 -wrap none \
3751 -font font_diff \
3752 -state disabled
3753 $ui_diff_alnos tag conf linenumber -justify right -rmargin 5
3754 $ui_diff_alnos tag conf red -foreground red
3755 $ui_diff_alnos tag conf green -foreground green4
3757 text $ui_diff_blnos \
3758 -takefocus 0 \
3759 -highlightthickness 0 \
3760 -padx 0 -pady 0 \
3761 -background grey95 \
3762 -foreground black \
3763 -borderwidth 0 \
3764 -width [expr $ui_diff_lno_col_width + 2] \
3765 -height 5 \
3766 -wrap none \
3767 -font font_diff \
3768 -state disabled
3769 $ui_diff_blnos tag conf linenumber -justify right -rmargin 5
3770 $ui_diff_blnos tag conf red -foreground red
3771 $ui_diff_blnos tag conf green -foreground green4
3772 $ui_diff_blnos tag conf hide -elide 1
3774 text $ui_diff_clnos \
3775 -takefocus 0 \
3776 -highlightthickness 0 \
3777 -padx 0 -pady 0 \
3778 -background grey90 \
3779 -foreground black \
3780 -borderwidth 0 \
3781 -width [expr $ui_diff_lno_col_width + 2] \
3782 -height 5 \
3783 -wrap none \
3784 -font font_diff \
3785 -state disabled
3786 $ui_diff_clnos tag conf linenumber -justify right -rmargin 5
3787 $ui_diff_clnos tag conf red -foreground red
3788 $ui_diff_clnos tag conf green -foreground green4
3790 delegate_sel_to $ui_diff [list $ui_diff_alnos $ui_diff_blnos $ui_diff_clnos]
3792 text $ui_diff \
3793 -highlightthickness 0 \
3794 -padx 0 -pady 0 \
3795 -background white \
3796 -foreground black \
3797 -borderwidth 0 \
3798 -width 80 -height 5 \
3799 -wrap none \
3800 -font font_diff \
3801 -xscrollcommand {.vpane.lower.diff.body.sbx set} \
3802 -state disabled
3803 catch {$ui_diff configure -tabstyle wordprocessor}
3805 set ui_diff_columns [list $ui_diff_alnos $ui_diff_blnos $ui_diff $ui_diff_clnos]
3806 set ui_diff_line_columns [list $ui_diff_alnos $ui_diff_blnos $ui_diff_clnos]
3807 set ui_diff_columns_to_scroll $ui_diff_columns
3809 ${NS}::scrollbar .vpane.lower.diff.body.sbx -orient horizontal \
3810 -command [list $ui_diff xview]
3811 ${NS}::scrollbar .vpane.lower.diff.body.sby -orient vertical \
3812 -command [list scrollbar2many $ui_diff_columns_to_scroll yview]
3814 grid $ui_diff_alnos $ui_diff_blnos $ui_diff_clnos $ui_diff .vpane.lower.diff.body.sby -sticky nsew
3815 grid conf .vpane.lower.diff.body.sbx \
3816 -column 0 \
3817 -sticky we \
3818 -columnspan 5
3819 grid columnconfigure .vpane.lower.diff.body \
3821 -weight 1
3822 grid rowconfigure .vpane.lower.diff.body \
3824 -weight 1
3826 update_show_line_numbers
3828 pack .vpane.lower.diff.header -side top -fill x
3829 pack .vpane.lower.diff.body -side bottom -fill both -expand 1
3832 foreach {n c} {0 black 1 red 2 green4 3 yellow4 4 blue4 5 magenta4 6 cyan4 7 grey60} {
3833 $ui_diff tag configure clr4$n -background $c
3834 $ui_diff tag configure clri4$n -foreground $c
3835 $ui_diff tag configure clr3$n -foreground $c
3836 $ui_diff tag configure clri3$n -background $c
3838 $ui_diff tag configure clr1 -font font_diffbold
3839 $ui_diff tag configure clr4 -underline 1
3841 $ui_diff tag conf d_info -foreground blue -font font_diffbold
3843 $ui_diff tag conf d_cr -elide true
3844 $ui_diff tag conf d_@ -font font_diffbold
3845 $ui_diff tag conf d_+ -foreground green4
3846 $ui_diff tag conf d_- -foreground red
3848 $ui_diff tag conf d_++ -foreground green4
3849 $ui_diff tag conf d_-- -foreground red
3850 $ui_diff tag conf d_+s \
3851 -foreground green4 \
3852 -background {#e2effa}
3853 $ui_diff tag conf d_-s \
3854 -foreground red \
3855 -background {#e2effa}
3856 $ui_diff tag conf d_s+ \
3857 -foreground green4 \
3858 -background ivory1
3859 $ui_diff tag conf d_s- \
3860 -foreground red \
3861 -background ivory1
3863 $ui_diff tag conf d< \
3864 -foreground orange \
3865 -font font_diffbold
3866 $ui_diff tag conf d| \
3867 -foreground orange \
3868 -font font_diffbold
3869 $ui_diff tag conf d= \
3870 -foreground orange \
3871 -font font_diffbold
3872 $ui_diff tag conf d> \
3873 -foreground orange \
3874 -font font_diffbold
3876 foreach i $ui_diff_columns {
3877 $i tag raise sel
3880 # -- Diff Body Context Menu
3883 proc create_common_diff_popup {ctxm} {
3884 $ctxm add command \
3885 -label [mc Refresh] \
3886 -command reshow_diff
3887 lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
3888 $ctxm add command \
3889 -label [mc Copy] \
3890 -command {tk_textCopy $ui_diff}
3891 lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
3892 $ctxm add command \
3893 -label [mc "Select All"] \
3894 -command {focus $ui_diff;$ui_diff tag add sel 0.0 end}
3895 lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
3896 $ctxm add command \
3897 -label [mc "Copy All"] \
3898 -command {
3899 $ui_diff tag add sel 0.0 end
3900 tk_textCopy $ui_diff
3901 $ui_diff tag remove sel 0.0 end
3903 lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
3904 $ctxm add separator
3905 $ctxm add command \
3906 -label [mc "Decrease Font Size"] \
3907 -command {incr_font_size font_diff -1}
3908 lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
3909 $ctxm add command \
3910 -label [mc "Increase Font Size"] \
3911 -command {incr_font_size font_diff 1}
3912 lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
3913 $ctxm add separator
3914 set emenu $ctxm.enc
3915 menu $emenu
3916 build_encoding_menu $emenu [list force_diff_encoding]
3917 $ctxm add cascade \
3918 -label [mc "Encoding"] \
3919 -menu $emenu
3920 lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
3921 $ctxm add separator
3922 $ctxm add command -label [mc "Options..."] \
3923 -command do_options
3926 set ctxmw .vpane.lower.diff.body.ctxmw
3927 menu $ctxmw -tearoff 0
3928 $ctxmw add command \
3929 -label [mc "Apply/Reverse Hunk"] \
3930 -command {apply_hunk $cursorX $cursorY}
3931 set ui_diff_applyhunk [$ctxmw index last]
3932 lappend diff_actions [list $ctxmw entryconf $ui_diff_applyhunk -state]
3933 $ctxmw add command \
3934 -label [mc "Apply/Reverse Line"] \
3935 -command {apply_range_or_line $cursorX $cursorY; do_rescan}
3936 set ui_diff_applyline [$ctxmw index last]
3937 lappend diff_actions [list $ctxmw entryconf $ui_diff_applyline -state]
3938 $ctxmw add separator
3939 $ctxmw add command \
3940 -label [mc "Revert Hunk"] \
3941 -command {apply_hunk $cursorX $cursorY 1}
3942 lappend diff_actions [list $ctxmw entryconf $ui_diff_applyhunk -state]
3943 $ctxmw add command \
3944 -label [mc "Revert Line"] \
3945 -command {apply_range_or_line $cursorX $cursorY 1; do_rescan}
3946 set ui_diff_revertline [$ctxmw index last]
3947 lappend diff_actions [list $ctxmw entryconf $ui_diff_applyline -state]
3948 $ctxmw add separator
3949 $ctxmw add command \
3950 -label [mc "Open in Editor"] \
3951 -command {open_from_diff_view $cursorX $cursorY}
3952 lappend diff_actions [list $ctxmw entryconf [$ctxmw index last] -state]
3953 $ctxmw add command \
3954 -label [mc "Show Less Context"] \
3955 -command show_less_context
3956 lappend diff_actions [list $ctxmw entryconf [$ctxmw index last] -state]
3957 $ctxmw add command \
3958 -label [mc "Show More Context"] \
3959 -command show_more_context
3960 lappend diff_actions [list $ctxmw entryconf [$ctxmw index last] -state]
3961 $ctxmw add checkbutton \
3962 -label [mc "Show Line Numbers"] \
3963 -command update_show_line_numbers \
3964 -variable diff_show_line_numbers
3965 $ctxmw add separator
3966 create_common_diff_popup $ctxmw
3968 set ctxmi .vpane.lower.diff.body.ctxmi
3969 menu $ctxmi -tearoff 0
3970 $ctxmi add command \
3971 -label [mc "Apply/Reverse Hunk"] \
3972 -command {apply_hunk $cursorX $cursorY}
3973 lappend diff_actions [list $ctxmi entryconf $ui_diff_applyhunk -state]
3974 $ctxmi add command \
3975 -label [mc "Apply/Reverse Line"] \
3976 -command {apply_range_or_line $cursorX $cursorY; do_rescan}
3977 lappend diff_actions [list $ctxmi entryconf $ui_diff_applyline -state]
3978 $ctxmi add separator
3979 $ctxmi add command \
3980 -label [mc "Open in Editor"] \
3981 -command {open_from_diff_view $cursorX $cursorY}
3982 lappend diff_actions [list $ctxmi entryconf [$ctxmi index last] -state]
3983 $ctxmi add command \
3984 -label [mc "Open Staged Content in Editor"] \
3985 -command {open_from_diff_view $cursorX $cursorY 1}
3986 lappend diff_actions [list $ctxmi entryconf [$ctxmi index last] -state]
3987 $ctxmi add command \
3988 -label [mc "Show Less Context"] \
3989 -command show_less_context
3990 lappend diff_actions [list $ctxmi entryconf [$ctxmi index last] -state]
3991 $ctxmi add command \
3992 -label [mc "Show More Context"] \
3993 -command show_more_context
3994 lappend diff_actions [list $ctxmi entryconf [$ctxmi index last] -state]
3995 $ctxmi add checkbutton \
3996 -label [mc "Show Line Numbers"] \
3997 -command update_show_line_numbers \
3998 -variable diff_show_line_numbers
3999 $ctxmi add separator
4000 create_common_diff_popup $ctxmi
4002 set ctxmmg .vpane.lower.diff.body.ctxmmg
4003 menu $ctxmmg -tearoff 0
4004 $ctxmmg add command \
4005 -label [mc "Run Merge Tool"] \
4006 -command {merge_resolve_tool}
4007 lappend diff_actions [list $ctxmmg entryconf [$ctxmmg index last] -state]
4008 $ctxmmg add separator
4009 $ctxmmg add command \
4010 -label [mc "Use Remote Version"] \
4011 -command {merge_resolve_one 3}
4012 lappend diff_actions [list $ctxmmg entryconf [$ctxmmg index last] -state]
4013 $ctxmmg add command \
4014 -label [mc "Use Local Version"] \
4015 -command {merge_resolve_one 2}
4016 lappend diff_actions [list $ctxmmg entryconf [$ctxmmg index last] -state]
4017 $ctxmmg add command \
4018 -label [mc "Revert To Base"] \
4019 -command {merge_resolve_one 1}
4020 lappend diff_actions [list $ctxmmg entryconf [$ctxmmg index last] -state]
4021 $ctxmmg add separator
4022 $ctxmmg add command \
4023 -label [mc "Open in Editor"] \
4024 -command {open_from_diff_view $cursorX $cursorY}
4025 lappend diff_actions [list $ctxmmg entryconf [$ctxmmg index last] -state]
4026 $ctxmmg add command \
4027 -label [mc "Show Less Context"] \
4028 -command show_less_context
4029 lappend diff_actions [list $ctxmmg entryconf [$ctxmmg index last] -state]
4030 $ctxmmg add command \
4031 -label [mc "Show More Context"] \
4032 -command show_more_context
4033 lappend diff_actions [list $ctxmmg entryconf [$ctxmmg index last] -state]
4034 $ctxmmg add checkbutton \
4035 -label [mc "Show Line Numbers"] \
4036 -command update_show_line_numbers \
4037 -variable diff_show_line_numbers
4038 $ctxmmg add separator
4039 create_common_diff_popup $ctxmmg
4041 set ctxmsm .vpane.lower.diff.body.ctxmsm
4042 menu $ctxmsm -tearoff 0
4043 $ctxmsm add command \
4044 -label [mc "Visualize These Changes In The Submodule"] \
4045 -command {do_gitk -- true}
4046 lappend diff_actions [list $ctxmsm entryconf [$ctxmsm index last] -state]
4047 $ctxmsm add command \
4048 -label [mc "Visualize Current Branch History In The Submodule"] \
4049 -command {do_gitk {} true}
4050 lappend diff_actions [list $ctxmsm entryconf [$ctxmsm index last] -state]
4051 $ctxmsm add command \
4052 -label [mc "Visualize All Branch History In The Submodule"] \
4053 -command {do_gitk --all true}
4054 lappend diff_actions [list $ctxmsm entryconf [$ctxmsm index last] -state]
4055 $ctxmsm add separator
4056 $ctxmsm add command \
4057 -label [mc "Start git gui In The Submodule"] \
4058 -command {do_git_gui}
4059 lappend diff_actions [list $ctxmsm entryconf [$ctxmsm index last] -state]
4060 $ctxmsm add separator
4061 create_common_diff_popup $ctxmsm
4063 proc has_textconv {path} {
4064 if {[is_config_false gui.textconv]} {
4065 return 0
4067 set filter [gitattr $path diff set]
4068 set textconv [get_config [join [list diff $filter textconv] .]]
4069 if {$filter ne {set} && $textconv ne {}} {
4070 return 1
4071 } else {
4072 return 0
4076 proc popup_diff_menu {ctxmw ctxmi ctxmmg ctxmsm x y X Y} {
4077 global current_diff_path file_states
4078 set ::cursorX $x
4079 set ::cursorY $y
4080 if {[info exists file_states($current_diff_path)]} {
4081 set state [lindex $file_states($current_diff_path) 0]
4082 } else {
4083 set state {__}
4085 if {[string first {U} $state] >= 0} {
4086 tk_popup $ctxmmg $X $Y
4087 } elseif {$::is_submodule_diff} {
4088 tk_popup $ctxmsm $X $Y
4089 } else {
4090 set has_range [expr {[$::ui_diff tag nextrange sel 0.0] != {}}]
4091 if {$::ui_index eq $::current_diff_side} {
4092 set ctxm $ctxmi
4093 set l [mc "Unstage Hunk From Commit"]
4094 if {$has_range} {
4095 set t [mc "Unstage Lines From Commit"]
4096 } else {
4097 set t [mc "Unstage Line From Commit"]
4099 } else {
4100 set ctxm $ctxmw
4101 set l [mc "Stage Hunk For Commit"]
4102 if {$has_range} {
4103 set t [mc "Stage Lines For Commit"]
4104 set r [mc "Revert Lines"]
4105 } else {
4106 set t [mc "Stage Line For Commit"]
4107 set r [mc "Revert Line"]
4110 if {$::is_3way_diff
4111 || $current_diff_path eq {}
4112 || {__} eq $state
4113 || {_O} eq $state
4114 || [string match {?T} $state]
4115 || [string match {T?} $state]
4116 || [has_textconv $current_diff_path]} {
4117 set s disabled
4118 } else {
4119 set s normal
4121 $ctxm entryconf $::ui_diff_applyhunk -state $s -label $l
4122 $ctxm entryconf $::ui_diff_applyline -state $s -label $t
4123 if {$::ui_workdir eq $::current_diff_side} {
4124 $ctxm entryconf $::ui_diff_revertline -state $s -label $r
4126 tk_popup $ctxm $X $Y
4129 bind_button3 $ui_diff [list popup_diff_menu $ctxmw $ctxmi $ctxmmg $ctxmsm %x %y %X %Y]
4131 foreach i $ui_diff_columns {
4132 bind $i <ButtonRelease-2> {open_from_diff_view %x %y}
4133 bind $i <Shift-ButtonRelease-2> {open_from_diff_view %x %y 1}
4136 # -- Status Bar
4138 set main_status [::status_bar::new .status]
4139 pack .status -anchor w -side bottom -fill x
4140 $main_status show [mc "Initializing..."]
4142 # -- Load geometry
4144 proc on_ttk_pane_mapped {w pane pos} {
4145 bind $w <Map> {}
4146 after 0 [list after idle [list $w sashpos $pane $pos]]
4148 proc on_tk_pane_mapped {w pane x y} {
4149 bind $w <Map> {}
4150 after 0 [list after idle [list $w sash place $pane $x $y]]
4152 proc on_application_mapped {} {
4153 global repo_config use_ttk
4154 bind . <Map> {}
4155 set gm $repo_config(gui.geometry)
4156 if {$use_ttk} {
4157 bind .vpane <Map> \
4158 [list on_ttk_pane_mapped %W 0 [lindex $gm 1]]
4159 bind .vpane.files <Map> \
4160 [list on_ttk_pane_mapped %W 0 [lindex $gm 2]]
4161 } else {
4162 bind .vpane <Map> \
4163 [list on_tk_pane_mapped %W 0 \
4164 [lindex $gm 1] \
4165 [lindex [.vpane sash coord 0] 1]]
4166 bind .vpane.files <Map> \
4167 [list on_tk_pane_mapped %W 0 \
4168 [lindex [.vpane.files sash coord 0] 0] \
4169 [lindex $gm 2]]
4171 wm geometry . [lindex $gm 0]
4173 if {[info exists repo_config(gui.geometry)]} {
4174 bind . <Map> [list on_application_mapped]
4175 wm geometry . [lindex $repo_config(gui.geometry) 0]
4178 # -- Load window state
4180 if {[info exists repo_config(gui.wmstate)]} {
4181 catch {wm state . $repo_config(gui.wmstate)}
4184 # -- Key Bindings
4186 bind $ui_comm <$M1B-Key-Return> {do_commit;break}
4187 bind $ui_comm <$M1B-Key-t> {do_add_selection;break}
4188 bind $ui_comm <$M1B-Key-T> {do_add_selection;break}
4189 bind $ui_comm <$M1B-Key-u> {do_unstage_selection;break}
4190 bind $ui_comm <$M1B-Key-U> {do_unstage_selection;break}
4191 bind $ui_comm <$M1B-Key-j> {do_revert_selection;break}
4192 bind $ui_comm <$M1B-Key-J> {do_revert_selection;break}
4193 bind $ui_comm <$M1B-Key-i> {do_add_all;break}
4194 bind $ui_comm <$M1B-Key-I> {do_add_all;break}
4195 bind $ui_comm <$M1B-Key-x> {tk_textCut %W;break}
4196 bind $ui_comm <$M1B-Key-X> {tk_textCut %W;break}
4197 bind $ui_comm <$M1B-Key-c> {tk_textCopy %W;break}
4198 bind $ui_comm <$M1B-Key-C> {tk_textCopy %W;break}
4199 bind $ui_comm <$M1B-Key-v> {tk_textPaste %W; %W see insert; break}
4200 bind $ui_comm <$M1B-Key-V> {tk_textPaste %W; %W see insert; break}
4201 bind $ui_comm <$M1B-Key-a> {%W tag add sel 0.0 end;break}
4202 bind $ui_comm <$M1B-Key-A> {%W tag add sel 0.0 end;break}
4203 bind $ui_comm <$M1B-Key-minus> {show_less_context;break}
4204 bind $ui_comm <$M1B-Key-KP_Subtract> {show_less_context;break}
4205 bind $ui_comm <$M1B-Key-equal> {show_more_context;break}
4206 bind $ui_comm <$M1B-Key-plus> {show_more_context;break}
4207 bind $ui_comm <$M1B-Key-KP_Add> {show_more_context;break}
4209 bind $ui_diff <$M1B-Key-x> {tk_textCopy %W;break}
4210 bind $ui_diff <$M1B-Key-X> {tk_textCopy %W;break}
4211 bind $ui_diff <$M1B-Key-c> {tk_textCopy %W;break}
4212 bind $ui_diff <$M1B-Key-C> {tk_textCopy %W;break}
4213 bind $ui_diff <$M1B-Key-v> {break}
4214 bind $ui_diff <$M1B-Key-V> {break}
4215 bind $ui_diff <$M1B-Key-a> {%W tag add sel 0.0 end;break}
4216 bind $ui_diff <$M1B-Key-A> {%W tag add sel 0.0 end;break}
4217 bind $ui_diff <$M1B-Key-j> {do_revert_selection;break}
4218 bind $ui_diff <$M1B-Key-J> {do_revert_selection;break}
4219 bind $ui_diff <Key-Up> {catch {%W yview scroll -1 units};break}
4220 bind $ui_diff <Key-Down> {catch {%W yview scroll 1 units};break}
4221 bind $ui_diff <Key-Left> {catch {%W xview scroll -1 units};break}
4222 bind $ui_diff <Key-Right> {catch {%W xview scroll 1 units};break}
4223 bind $ui_diff <Key-k> {catch {%W yview scroll -1 units};break}
4224 bind $ui_diff <Key-j> {catch {%W yview scroll 1 units};break}
4225 bind $ui_diff <Key-h> {catch {%W xview scroll -1 units};break}
4226 bind $ui_diff <Key-l> {catch {%W xview scroll 1 units};break}
4227 bind $ui_diff <Control-Key-b> {catch {%W yview scroll -1 pages};break}
4228 bind $ui_diff <Control-Key-f> {catch {%W yview scroll 1 pages};break}
4229 bind $ui_diff <Button-1> {focus %W}
4231 if {[is_enabled branch]} {
4232 bind . <$M1B-Key-n> branch_create::dialog
4233 bind . <$M1B-Key-N> branch_create::dialog
4234 bind . <$M1B-Key-o> branch_checkout::dialog
4235 bind . <$M1B-Key-O> branch_checkout::dialog
4236 bind . <$M1B-Key-m> merge::dialog
4237 bind . <$M1B-Key-M> merge::dialog
4239 if {[is_enabled transport]} {
4240 bind . <$M1B-Key-p> do_push_anywhere
4241 bind . <$M1B-Key-P> do_push_anywhere
4244 bind . <Key-F5> ui_do_rescan
4245 bind . <$M1B-Key-r> ui_do_rescan
4246 bind . <$M1B-Key-R> ui_do_rescan
4247 bind . <$M1B-Key-s> do_signoff
4248 bind . <$M1B-Key-S> do_signoff
4249 bind . <$M1B-Key-t> do_add_selection
4250 bind . <$M1B-Key-T> do_add_selection
4251 bind . <$M1B-Key-u> do_unstage_selection
4252 bind . <$M1B-Key-U> do_unstage_selection
4253 bind . <$M1B-Key-j> do_revert_selection
4254 bind . <$M1B-Key-J> do_revert_selection
4255 bind . <$M1B-Key-i> do_add_all
4256 bind . <$M1B-Key-I> do_add_all
4257 bind . <$M1B-Key-minus> {show_less_context;break}
4258 bind . <$M1B-Key-KP_Subtract> {show_less_context;break}
4259 bind . <$M1B-Key-equal> {show_more_context;break}
4260 bind . <$M1B-Key-plus> {show_more_context;break}
4261 bind . <$M1B-Key-KP_Add> {show_more_context;break}
4262 bind . <$M1B-Key-Return> do_commit
4263 foreach i [list $ui_index $ui_workdir] {
4264 bind $i <Button-1> "toggle_or_diff $i %x %y; break"
4265 bind $i <$M1B-Button-1> "add_one_to_selection $i %x %y; break"
4266 bind $i <Shift-Button-1> "add_range_to_selection $i %x %y; break"
4268 bind $i <ButtonRelease-2> "open_from_file_list $i %x %y; break"
4269 bind $i <Shift-ButtonRelease-2> "open_from_file_list $i %x %y 1; break"
4272 if {[$files_ctxm index end] == 1} {
4273 # remove the separator, when we don't have user specified file tools
4274 $files_ctxm delete 0
4276 bind_button3 $ui_index "popup_files_ctxm $files_ctxm $ui_index %x %y %X %Y; break"
4277 bind_button3 $ui_workdir "popup_files_ctxm $files_ctxm $ui_workdir %x %y %X %Y; break"
4279 unset i
4281 set file_lists($ui_index) [list]
4282 set file_lists($ui_workdir) [list]
4284 wm title . "[appname] ([reponame]) [file normalize $_gitworktree]"
4285 focus -force $ui_comm
4287 # -- Warn the user about environmental problems. Cygwin's Tcl
4288 # does *not* pass its env array onto any processes it spawns.
4289 # This means that git processes get none of our environment.
4291 if {[is_Cygwin]} {
4292 set ignored_env 0
4293 set suggest_user {}
4294 set msg [mc "Possible environment issues exist.
4296 The following environment variables are probably
4297 going to be ignored by any Git subprocess run
4298 by %s:
4300 " [appname]]
4301 foreach name [array names env] {
4302 switch -regexp -- $name {
4303 {^GIT_INDEX_FILE$} -
4304 {^GIT_OBJECT_DIRECTORY$} -
4305 {^GIT_ALTERNATE_OBJECT_DIRECTORIES$} -
4306 {^GIT_DIFF_OPTS$} -
4307 {^GIT_EXTERNAL_DIFF$} -
4308 {^GIT_PAGER$} -
4309 {^GIT_TRACE$} -
4310 {^GIT_CONFIG$} -
4311 {^GIT_(AUTHOR|COMMITTER)_DATE$} {
4312 append msg " - $name\n"
4313 incr ignored_env
4315 {^GIT_(AUTHOR|COMMITTER)_(NAME|EMAIL)$} {
4316 append msg " - $name\n"
4317 incr ignored_env
4318 set suggest_user $name
4322 if {$ignored_env > 0} {
4323 append msg [mc "
4324 This is due to a known issue with the
4325 Tcl binary distributed by Cygwin."]
4327 if {$suggest_user ne {}} {
4328 append msg [mc "
4330 A good replacement for %s
4331 is placing values for the user.name and
4332 user.email settings into your personal
4333 ~/.gitconfig file.
4334 " $suggest_user]
4336 warn_popup $msg
4338 unset ignored_env msg suggest_user name
4341 # -- Only initialize complex UI if we are going to stay running.
4343 if {[is_enabled transport]} {
4344 load_all_remotes
4346 set n [.mbar.remote index end]
4347 populate_remotes_menu
4348 set n [expr {[.mbar.remote index end] - $n}]
4349 if {$n > 0} {
4350 if {[.mbar.remote type 0] eq "tearoff"} { incr n }
4351 .mbar.remote insert $n separator
4353 unset n
4356 if {[winfo exists $ui_comm]} {
4357 set GITGUI_BCK_exists [load_message GITGUI_BCK utf-8]
4359 # -- If both our backup and message files exist use the
4360 # newer of the two files to initialize the buffer.
4362 if {$GITGUI_BCK_exists} {
4363 set m [gitdir GITGUI_MSG]
4364 if {[file isfile $m]} {
4365 if {[file mtime [gitdir GITGUI_BCK]] > [file mtime $m]} {
4366 catch {file delete [gitdir GITGUI_MSG]}
4367 } else {
4368 $ui_comm delete 0.0 end
4369 $ui_comm edit reset
4370 $ui_comm edit modified false
4371 catch {file delete [gitdir GITGUI_BCK]}
4372 set GITGUI_BCK_exists 0
4375 unset m
4378 proc backup_commit_buffer {} {
4379 global ui_comm GITGUI_BCK_exists
4381 set m [$ui_comm edit modified]
4382 if {$m || $GITGUI_BCK_exists} {
4383 set msg [string trim [$ui_comm get 0.0 end]]
4384 regsub -all -line {[ \r\t]+$} $msg {} msg
4386 if {$msg eq {}} {
4387 if {$GITGUI_BCK_exists} {
4388 catch {file delete [gitdir GITGUI_BCK]}
4389 set GITGUI_BCK_exists 0
4391 } elseif {$m} {
4392 catch {
4393 set fd [open [gitdir GITGUI_BCK] w]
4394 fconfigure $fd -encoding utf-8
4395 puts -nonewline $fd $msg
4396 close $fd
4397 set GITGUI_BCK_exists 1
4401 $ui_comm edit modified false
4404 set ::GITGUI_BCK_i [after 2000 backup_commit_buffer]
4407 backup_commit_buffer
4409 # -- If the user has aspell available we can drive it
4410 # in pipe mode to spellcheck the commit message.
4412 set spell_cmd [list |]
4413 set spell_dict [get_config gui.spellingdictionary]
4414 lappend spell_cmd aspell
4415 if {$spell_dict ne {}} {
4416 lappend spell_cmd --master=$spell_dict
4418 lappend spell_cmd --mode=none
4419 lappend spell_cmd --encoding=utf-8
4420 lappend spell_cmd pipe
4421 if {$spell_dict eq {none}
4422 || [catch {set spell_fd [open $spell_cmd r+]} spell_err]} {
4423 bind_button3 $ui_comm [list tk_popup $ui_comm_ctxm %X %Y]
4424 } else {
4425 set ui_comm_spell [spellcheck::init \
4426 $spell_fd \
4427 $ui_comm \
4428 $ui_comm_ctxm \
4431 unset -nocomplain spell_cmd spell_fd spell_err spell_dict
4434 lock_index begin-read
4435 if {![winfo ismapped .]} {
4436 wm deiconify .
4438 after 1 {
4439 if {[is_enabled initialamend]} {
4440 force_amend
4441 } else {
4442 do_rescan
4445 if {[is_enabled nocommitmsg]} {
4446 $ui_comm configure -state disabled -background gray
4449 if {[is_enabled multicommit] && ![is_config_false gui.gcwarning]} {
4450 after 1000 hint_gc
4452 if {[is_enabled retcode]} {
4453 bind . <Destroy> {+terminate_me %W}
4455 if {$picked && [is_config_true gui.autoexplore]} {
4456 do_explore
4459 # Local variables:
4460 # mode: tcl
4461 # indent-tabs-mode: t
4462 # tab-width: 4
4463 # End: