]> git.ipfire.org Git - thirdparty/git.git/blame - git-gui.sh
git-gui: Support a native Mac OS X application bundle
[thirdparty/git.git] / git-gui.sh
CommitLineData
bd11b82d 1#!/bin/sh
cb07fc2a 2# Tcl ignores the next line -*- tcl -*- \
4e817d1a
SP
3 if test "z$*" = zversion \
4 || test "z$*" = z--version; \
5 then \
6 echo 'git-gui version @@GITGUI_VERSION@@'; \
7 exit; \
8 fi; \
2f7c9a7f
SP
9 argv0=$0; \
10 exec wish "$argv0" -- "$@"
cb07fc2a 11
7e81d4ee 12set appvers {@@GITGUI_VERSION@@}
bdc9ea20 13set copyright {
a9813cb5 14Copyright © 2006, 2007 Shawn Pearce, et. al.
bdc9ea20 15
0499b24a
SP
16This program is free software; you can redistribute it and/or modify
17it under the terms of the GNU General Public License as published by
18the Free Software Foundation; either version 2 of the License, or
19(at your option) any later version.
20
21This program is distributed in the hope that it will be useful,
22but WITHOUT ANY WARRANTY; without even the implied warranty of
23MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24GNU General Public License for more details.
25
26You should have received a copy of the GNU General Public License
27along with this program; if not, write to the Free Software
28Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA}
cb07fc2a 29
f522c9b5 30######################################################################
cfb07cca
SP
31##
32## Tcl/Tk sanity check
33
34if {[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 \
c8c4854b 41 -title [mc "git-gui: fatal error"] \
cfb07cca
SP
42 -message $err
43 exit 1
44}
45
63c4024f 46catch {rename send {}} ; # What an evil concept...
cff93397 47
fc703c20
SP
48######################################################################
49##
50## locate our library
51
52set oguilib {@@GITGUI_LIBDIR@@}
53set oguirel {@@GITGUI_RELATIVE@@}
54if {$oguirel eq {1}} {
55 set oguilib [file dirname [file dirname [file normalize $argv0]]]
56 set oguilib [file join $oguilib share git-gui lib]
d4b0ccd9 57 set oguimsg [file join $oguilib msgs]
fc703c20
SP
58} elseif {[string match @@* $oguirel]} {
59 set oguilib [file join [file dirname [file normalize $argv0]] lib]
d4b0ccd9
SP
60 set oguimsg [file join [file dirname [file normalize $argv0]] po]
61} else {
62 set oguimsg [file join $oguilib msgs]
fc703c20
SP
63}
64unset oguirel
65
cd12901b
SP
66######################################################################
67##
68## enable verbose loading?
69
70if {![catch {set _verbose $env(GITGUI_VERBOSE)}]} {
71 unset _verbose
72 rename auto_load real__auto_load
73 proc auto_load {name args} {
74 puts stderr "auto_load $name"
75 return [uplevel 1 real__auto_load $name $args]
76 }
77 rename source real__source
78 proc source {name} {
79 puts stderr "source $name"
80 uplevel 1 real__source $name
81 }
82}
83
c950c66e 84######################################################################
d4b0ccd9
SP
85##
86## Internationalization (i18n) through msgcat and gettext. See
87## http://www.gnu.org/software/gettext/manual/html_node/Tcl.html
88
89package require msgcat
146d73a3
SP
90
91proc mc {fmt args} {
92 set fmt [::msgcat::mc $fmt]
93 set cmk [string first @@ $fmt]
94 if {$cmk > 0} {
95 set fmt [string range $fmt 0 [expr {$cmk - 1}]]
96 }
97 return [eval [list format $fmt] $args]
98}
99
31bb1d1b
SP
100proc strcat {args} {
101 return [join $args {}]
102}
103
d4b0ccd9
SP
104::msgcat::mcload $oguimsg
105unset oguimsg
106
107######################################################################
c950c66e
SP
108##
109## read only globals
110
111set _appname [lindex [file split $argv0] end]
112set _gitdir {}
20ddfcaa 113set _gitexec {}
c950c66e 114set _reponame {}
20ddfcaa 115set _iscygwin {}
0b812616 116set _search_path {}
c950c66e
SP
117
118proc appname {} {
119 global _appname
120 return $_appname
121}
122
c2758a17 123proc gitdir {args} {
c950c66e 124 global _gitdir
c2758a17
SP
125 if {$args eq {}} {
126 return $_gitdir
127 }
0b812616 128 return [eval [list file join $_gitdir] $args]
c950c66e
SP
129}
130
20ddfcaa
SP
131proc gitexec {args} {
132 global _gitexec
133 if {$_gitexec eq {}} {
81347223 134 if {[catch {set _gitexec [git --exec-path]} err]} {
20ddfcaa
SP
135 error "Git not installed?\n\n$err"
136 }
0b812616
SP
137 if {[is_Cygwin]} {
138 set _gitexec [exec cygpath \
139 --windows \
140 --absolute \
141 $_gitexec]
142 } else {
143 set _gitexec [file normalize $_gitexec]
144 }
20ddfcaa
SP
145 }
146 if {$args eq {}} {
147 return $_gitexec
148 }
0b812616 149 return [eval [list file join $_gitexec] $args]
20ddfcaa
SP
150}
151
c950c66e 152proc reponame {} {
d36cd968 153 return $::_reponame
c950c66e 154}
da5239dc 155
20ddfcaa 156proc is_MacOSX {} {
20ddfcaa
SP
157 if {[tk windowingsystem] eq {aqua}} {
158 return 1
159 }
160 return 0
161}
162
163proc is_Windows {} {
d36cd968 164 if {$::tcl_platform(platform) eq {windows}} {
20ddfcaa
SP
165 return 1
166 }
167 return 0
168}
169
170proc is_Cygwin {} {
d36cd968 171 global _iscygwin
20ddfcaa 172 if {$_iscygwin eq {}} {
d36cd968 173 if {$::tcl_platform(platform) eq {windows}} {
20ddfcaa
SP
174 if {[catch {set p [exec cygpath --windir]} err]} {
175 set _iscygwin 0
176 } else {
177 set _iscygwin 1
178 }
179 } else {
180 set _iscygwin 0
181 }
182 }
183 return $_iscygwin
184}
185
cf25ddc8
SP
186proc is_enabled {option} {
187 global enabled_options
188 if {[catch {set on $enabled_options($option)}]} {return 0}
189 return $on
190}
191
192proc enable_option {option} {
193 global enabled_options
194 set enabled_options($option) 1
195}
196
197proc disable_option {option} {
198 global enabled_options
199 set enabled_options($option) 0
200}
201
2d19516d
SP
202######################################################################
203##
204## config
205
51f4d16b
SP
206proc is_many_config {name} {
207 switch -glob -- $name {
208 remote.*.fetch -
209 remote.*.push
210 {return 1}
211 *
212 {return 0}
213 }
214}
2d19516d 215
c539449b
SP
216proc is_config_true {name} {
217 global repo_config
218 if {[catch {set v $repo_config($name)}]} {
219 return 0
220 } elseif {$v eq {true} || $v eq {1} || $v eq {yes}} {
221 return 1
222 } else {
223 return 0
224 }
225}
226
61f82ce7
SP
227proc get_config {name} {
228 global repo_config
229 if {[catch {set v $repo_config($name)}]} {
230 return {}
231 } else {
232 return $v
233 }
234}
235
6bbd1cb9 236proc load_config {include_global} {
51f4d16b
SP
237 global repo_config global_config default_config
238
239 array unset global_config
6bbd1cb9
SP
240 if {$include_global} {
241 catch {
0b812616 242 set fd_rc [git_read config --global --list]
6bbd1cb9
SP
243 while {[gets $fd_rc line] >= 0} {
244 if {[regexp {^([^=]+)=(.*)$} $line line name value]} {
245 if {[is_many_config $name]} {
246 lappend global_config($name) $value
247 } else {
248 set global_config($name) $value
249 }
51f4d16b
SP
250 }
251 }
6bbd1cb9 252 close $fd_rc
51f4d16b 253 }
51f4d16b 254 }
6bbd1cb9
SP
255
256 array unset repo_config
2d19516d 257 catch {
0b812616 258 set fd_rc [git_read config --list]
2d19516d
SP
259 while {[gets $fd_rc line] >= 0} {
260 if {[regexp {^([^=]+)=(.*)$} $line line name value]} {
51f4d16b
SP
261 if {[is_many_config $name]} {
262 lappend repo_config($name) $value
263 } else {
264 set repo_config($name) $value
265 }
2d19516d
SP
266 }
267 }
268 close $fd_rc
269 }
270
51f4d16b
SP
271 foreach name [array names default_config] {
272 if {[catch {set v $global_config($name)}]} {
273 set global_config($name) $default_config($name)
274 }
275 if {[catch {set v $repo_config($name)}]} {
276 set repo_config($name) $default_config($name)
277 }
2d19516d
SP
278 }
279}
280
81347223
SP
281######################################################################
282##
283## handy utils
284
0b812616
SP
285proc _git_cmd {name} {
286 global _git_cmd_path
287
288 if {[catch {set v $_git_cmd_path($name)}]} {
289 switch -- $name {
70a7595c 290 version -
0b812616
SP
291 --version -
292 --exec-path { return [list $::_git $name] }
293 }
294
295 set p [gitexec git-$name$::_search_exe]
296 if {[file exists $p]} {
297 set v [list $p]
c136f2b8
SP
298 } elseif {[is_Windows] && [file exists [gitexec git-$name]]} {
299 # Try to determine what sort of magic will make
300 # git-$name go and do its thing, because native
301 # Tcl on Windows doesn't know it.
0b812616 302 #
c136f2b8
SP
303 set p [gitexec git-$name]
304 set f [open $p r]
305 set s [gets $f]
306 close $f
307
6e4ba05c 308 switch -glob -- [lindex $s 0] {
c136f2b8
SP
309 #!*sh { set i sh }
310 #!*perl { set i perl }
311 #!*python { set i python }
312 default { error "git-$name is not supported: $s" }
313 }
314
315 upvar #0 _$i interp
316 if {![info exists interp]} {
317 set interp [_which $i]
318 }
319 if {$interp eq {}} {
320 error "git-$name requires $i (not in PATH)"
321 }
6e4ba05c 322 set v [concat [list $interp] [lrange $s 1 end] [list $p]]
0b812616 323 } else {
c6729890
SP
324 # Assume it is builtin to git somehow and we
325 # aren't actually able to see a file for it.
326 #
327 set v [list $::_git $name]
0b812616
SP
328 }
329 set _git_cmd_path($name) $v
330 }
331 return $v
332}
333
334proc _which {what} {
335 global env _search_exe _search_path
336
337 if {$_search_path eq {}} {
299077fb 338 if {[is_Cygwin] && [regexp {^(/|\.:)} $env(PATH)]} {
0b812616
SP
339 set _search_path [split [exec cygpath \
340 --windows \
341 --path \
342 --absolute \
343 $env(PATH)] {;}]
344 set _search_exe .exe
345 } elseif {[is_Windows]} {
346 set _search_path [split $env(PATH) {;}]
347 set _search_exe .exe
348 } else {
349 set _search_path [split $env(PATH) :]
350 set _search_exe {}
351 }
352 }
353
354 foreach p $_search_path {
355 set p [file join $p $what$_search_exe]
356 if {[file exists $p]} {
357 return [file normalize $p]
358 }
359 }
360 return {}
361}
362
6f62b4f7
SP
363proc _lappend_nice {cmd_var} {
364 global _nice
365 upvar $cmd_var cmd
366
367 if {![info exists _nice]} {
368 set _nice [_which nice]
369 }
370 if {$_nice ne {}} {
371 lappend cmd $_nice
372 }
373}
374
81347223 375proc git {args} {
0b812616
SP
376 set opt [list exec]
377
378 while {1} {
379 switch -- [lindex $args 0] {
380 --nice {
6f62b4f7 381 _lappend_nice opt
0b812616
SP
382 }
383
384 default {
385 break
386 }
387
388 }
389
390 set args [lrange $args 1 end]
391 }
392
393 set cmdp [_git_cmd [lindex $args 0]]
394 set args [lrange $args 1 end]
395
396 return [eval $opt $cmdp $args]
397}
398
74c4763c
SP
399proc _open_stdout_stderr {cmd} {
400 if {[catch {
401 set fd [open $cmd r]
402 } err]} {
403 if { [lindex $cmd end] eq {2>@1}
404 && $err eq {can not find channel named "1"}
405 } {
406 # Older versions of Tcl 8.4 don't have this 2>@1 IO
407 # redirect operator. Fallback to |& cat for those.
408 # The command was not actually started, so its safe
409 # to try to start it a second time.
410 #
411 set fd [open [concat \
412 [lrange $cmd 0 end-1] \
413 [list |& cat] \
414 ] r]
415 } else {
416 error $err
417 }
418 }
6eb420ef 419 fconfigure $fd -eofchar {}
74c4763c
SP
420 return $fd
421}
422
0b812616
SP
423proc git_read {args} {
424 set opt [list |]
425
426 while {1} {
427 switch -- [lindex $args 0] {
428 --nice {
6f62b4f7 429 _lappend_nice opt
0b812616
SP
430 }
431
432 --stderr {
433 lappend args 2>@1
434 }
435
436 default {
437 break
438 }
439
440 }
441
442 set args [lrange $args 1 end]
443 }
444
445 set cmdp [_git_cmd [lindex $args 0]]
446 set args [lrange $args 1 end]
447
74c4763c 448 return [_open_stdout_stderr [concat $opt $cmdp $args]]
0b812616
SP
449}
450
451proc git_write {args} {
452 set opt [list |]
453
454 while {1} {
455 switch -- [lindex $args 0] {
456 --nice {
6f62b4f7 457 _lappend_nice opt
0b812616
SP
458 }
459
460 default {
461 break
462 }
463
464 }
465
466 set args [lrange $args 1 end]
467 }
468
469 set cmdp [_git_cmd [lindex $args 0]]
470 set args [lrange $args 1 end]
471
472 return [open [concat $opt $cmdp $args] w]
81347223
SP
473}
474
7eafa2f1
SP
475proc sq {value} {
476 regsub -all ' $value "'\\''" value
477 return "'$value'"
478}
479
d41b43eb
SP
480proc load_current_branch {} {
481 global current_branch is_detached
482
fc4e8da7 483 set fd [open [gitdir HEAD] r]
311e02a4 484 if {[gets $fd ref] < 1} {
fc4e8da7
SP
485 set ref {}
486 }
487 close $fd
311e02a4
SP
488
489 set pfx {ref: refs/heads/}
490 set len [string length $pfx]
491 if {[string equal -length $len $pfx $ref]} {
492 # We're on a branch. It might not exist. But
493 # HEAD looks good enough to be a branch.
494 #
d41b43eb
SP
495 set current_branch [string range $ref $len end]
496 set is_detached 0
311e02a4
SP
497 } else {
498 # Assume this is a detached head.
499 #
d41b43eb
SP
500 set current_branch HEAD
501 set is_detached 1
311e02a4 502 }
fc4e8da7
SP
503}
504
2739291b
SP
505auto_load tk_optionMenu
506rename tk_optionMenu real__tkOptionMenu
507proc tk_optionMenu {w varName args} {
508 set m [eval real__tkOptionMenu $w $varName $args]
509 $m configure -font font_ui
510 $w configure -font font_ui
511 return $m
512}
513
3849bfba
SP
514proc rmsel_tag {text} {
515 $text tag conf sel \
516 -background [$text cget -background] \
517 -foreground [$text cget -foreground] \
518 -borderwidth 0
519 $text tag conf in_sel -background lightgray
520 bind $text <Motion> break
521 return $text
522}
523
a4bee597
SP
524set root_exists 0
525bind . <Visibility> {
526 bind . <Visibility> {}
527 set root_exists 1
528}
529
1bdd8a15
SP
530if {[is_Windows]} {
531 wm iconbitmap . -default $oguilib/git-gui.ico
532}
533
a4bee597
SP
534######################################################################
535##
536## config defaults
537
538set cursor_ptr arrow
539font create font_diff -family Courier -size 10
540font create font_ui
541catch {
542 label .dummy
543 eval font configure font_ui [font actual [.dummy cget -font]]
544 destroy .dummy
545}
546
547font create font_uiitalic
548font create font_uibold
549font create font_diffbold
550font create font_diffitalic
551
552foreach class {Button Checkbutton Entry Label
553 Labelframe Listbox Menu Message
554 Radiobutton Spinbox Text} {
555 option add *$class.font font_ui
556}
557unset class
558
559if {[is_Windows] || [is_MacOSX]} {
560 option add *Menu.tearOff 0
561}
562
563if {[is_MacOSX]} {
564 set M1B M1
565 set M1T Cmd
566} else {
567 set M1B Control
568 set M1T Ctrl
569}
570
571proc bind_button3 {w cmd} {
572 bind $w <Any-Button-3> $cmd
573 if {[is_MacOSX]} {
574 # Mac OS X sends Button-2 on right click through three-button mouse,
575 # or through trackpad right-clicking (two-finger touch + click).
576 bind $w <Any-Button-2> $cmd
577 bind $w <Control-Button-1> $cmd
578 }
579}
580
581proc apply_config {} {
582 global repo_config font_descs
583
584 foreach option $font_descs {
585 set name [lindex $option 0]
586 set font [lindex $option 1]
587 if {[catch {
588 foreach {cn cv} $repo_config(gui.$name) {
589 font configure $font $cn $cv -weight normal
590 }
591 } err]} {
592 error_popup [strcat [mc "Invalid font specified in %s:" "gui.$name"] "\n\n$err"]
593 }
594 foreach {cn cv} [font configure $font] {
595 font configure ${font}bold $cn $cv
596 font configure ${font}italic $cn $cv
597 }
598 font configure ${font}bold -weight bold
599 font configure ${font}italic -slant italic
600 }
601}
602
603set default_config(merge.diffstat) true
604set default_config(merge.summary) false
605set default_config(merge.verbosity) 2
606set default_config(user.name) {}
607set default_config(user.email) {}
608
609set default_config(gui.matchtrackingbranch) false
610set default_config(gui.pruneduringfetch) false
611set default_config(gui.trustmtime) false
612set default_config(gui.diffcontext) 5
613set default_config(gui.newbranchtemplate) {}
614set default_config(gui.fontui) [font configure font_ui]
615set default_config(gui.fontdiff) [font configure font_diff]
616set font_descs {
617 {fontui font_ui {mc "Main Font"}}
618 {fontdiff font_diff {mc "Diff/Console Font"}}
619}
620
0b812616
SP
621######################################################################
622##
623## find git
624
625set _git [_which git]
626if {$_git eq {}} {
627 catch {wm withdraw .}
183a1d14
SP
628 tk_messageBox \
629 -icon error \
630 -type ok \
631 -title [mc "git-gui: fatal error"] \
632 -message [mc "Cannot find git in PATH."]
0b812616
SP
633 exit 1
634}
0b812616 635
54acdd95
SP
636######################################################################
637##
638## version check
639
d6967022 640if {[catch {set _git_version [git --version]} err]} {
54acdd95 641 catch {wm withdraw .}
875b7c93
SP
642 tk_messageBox \
643 -icon error \
644 -type ok \
c8c4854b 645 -title [mc "git-gui: fatal error"] \
875b7c93 646 -message "Cannot determine Git version:
54acdd95
SP
647
648$err
649
d6967022
SP
650[appname] requires Git 1.5.0 or later."
651 exit 1
652}
653if {![regsub {^git version } $_git_version {} _git_version]} {
654 catch {wm withdraw .}
875b7c93
SP
655 tk_messageBox \
656 -icon error \
657 -type ok \
c8c4854b 658 -title [mc "git-gui: fatal error"] \
31bb1d1b 659 -message [strcat [mc "Cannot parse Git version string:"] "\n\n$_git_version"]
54acdd95
SP
660 exit 1
661}
301dfaa9
SP
662
663set _real_git_version $_git_version
ec4fceec 664regsub -- {-dirty$} $_git_version {} _git_version
d6967022
SP
665regsub {\.[0-9]+\.g[0-9a-f]+$} $_git_version {} _git_version
666regsub {\.rc[0-9]+$} $_git_version {} _git_version
91464dfb 667regsub {\.GIT$} $_git_version {} _git_version
d6967022 668
301dfaa9
SP
669if {![regexp {^[1-9]+(\.[0-9]+)+$} $_git_version]} {
670 catch {wm withdraw .}
671 if {[tk_messageBox \
672 -icon warning \
673 -type yesno \
674 -default no \
675 -title "[appname]: warning" \
1ac17950 676 -message [mc "Git version cannot be determined.
301dfaa9 677
1ac17950 678%s claims it is version '%s'.
301dfaa9 679
1ac17950 680%s requires at least Git 1.5.0 or later.
301dfaa9 681
1ac17950
CS
682Assume '%s' is version 1.5.0?
683" $_git $_real_git_version [appname] $_real_git_version]] eq {yes}} {
301dfaa9
SP
684 set _git_version 1.5.0
685 } else {
686 exit 1
687 }
688}
689unset _real_git_version
690
d6967022
SP
691proc git-version {args} {
692 global _git_version
693
694 switch [llength $args] {
695 0 {
696 return $_git_version
54acdd95 697 }
d6967022
SP
698
699 2 {
700 set op [lindex $args 0]
701 set vr [lindex $args 1]
702 set cm [package vcompare $_git_version $vr]
703 return [expr $cm $op 0]
704 }
705
706 4 {
707 set type [lindex $args 0]
708 set name [lindex $args 1]
709 set parm [lindex $args 2]
710 set body [lindex $args 3]
711
712 if {($type ne {proc} && $type ne {method})} {
713 error "Invalid arguments to git-version"
714 }
715 if {[llength $body] < 2 || [lindex $body end-1] ne {default}} {
716 error "Last arm of $type $name must be default"
717 }
718
719 foreach {op vr cb} [lrange $body 0 end-2] {
720 if {[git-version $op $vr]} {
721 return [uplevel [list $type $name $parm $cb]]
722 }
723 }
724
725 return [uplevel [list $type $name $parm [lindex $body end]]]
726 }
727
728 default {
729 error "git-version >= x"
730 }
731
732 }
733}
734
735if {[git-version < 1.5]} {
54acdd95 736 catch {wm withdraw .}
875b7c93
SP
737 tk_messageBox \
738 -icon error \
739 -type ok \
c8c4854b 740 -title [mc "git-gui: fatal error"] \
875b7c93 741 -message "[appname] requires Git 1.5.0 or later.
d6967022
SP
742
743You are using [git-version]:
744
745[git --version]"
54acdd95
SP
746 exit 1
747}
54acdd95 748
875b7c93
SP
749######################################################################
750##
751## configure our library
752
875b7c93
SP
753set idx [file join $oguilib tclIndex]
754if {[catch {set fd [open $idx r]} err]} {
755 catch {wm withdraw .}
756 tk_messageBox \
757 -icon error \
758 -type ok \
c8c4854b 759 -title [mc "git-gui: fatal error"] \
875b7c93
SP
760 -message $err
761 exit 1
762}
763if {[gets $fd] eq {# Autogenerated by git-gui Makefile}} {
764 set idx [list]
765 while {[gets $fd n] >= 0} {
766 if {$n ne {} && ![string match #* $n]} {
767 lappend idx $n
768 }
769 }
770} else {
771 set idx {}
772}
773close $fd
774
775if {$idx ne {}} {
776 set loaded [list]
777 foreach p $idx {
778 if {[lsearch -exact $loaded $p] >= 0} continue
779 source [file join $oguilib $p]
780 lappend loaded $p
781 }
782 unset loaded p
783} else {
784 set auto_path [concat [list $oguilib] $auto_path]
785}
fc703c20 786unset -nocomplain idx fd
875b7c93 787
ba7cc660
SP
788######################################################################
789##
790## feature option selection
791
792if {[regexp {^git-(.+)$} [appname] _junk subcommand]} {
793 unset _junk
794} else {
795 set subcommand gui
796}
797if {$subcommand eq {gui.sh}} {
798 set subcommand gui
799}
800if {$subcommand eq {gui} && [llength $argv] > 0} {
801 set subcommand [lindex $argv 0]
802 set argv [lrange $argv 1 end]
803}
804
805enable_option multicommit
806enable_option branch
807enable_option transport
c52c9452 808disable_option bare
ba7cc660
SP
809
810switch -- $subcommand {
811browser -
812blame {
c52c9452
SP
813 enable_option bare
814
ba7cc660
SP
815 disable_option multicommit
816 disable_option branch
817 disable_option transport
818}
819citool {
820 enable_option singlecommit
821
822 disable_option multicommit
823 disable_option branch
824 disable_option transport
825}
826}
827
2d19516d
SP
828######################################################################
829##
830## repository setup
831
c6127856
SP
832if {[catch {
833 set _gitdir $env(GIT_DIR)
834 set _prefix {}
835 }]
836 && [catch {
837 set _gitdir [git rev-parse --git-dir]
838 set _prefix [git rev-parse --show-prefix]
839 } err]} {
ab08b363
SP
840 load_config 1
841 apply_config
842 choose_repository::pick
2d19516d 843}
20ddfcaa 844if {![file isdirectory $_gitdir] && [is_Cygwin]} {
2f7c9a7f 845 catch {set _gitdir [exec cygpath --windows $_gitdir]}
20ddfcaa 846}
c950c66e 847if {![file isdirectory $_gitdir]} {
dbccbbda 848 catch {wm withdraw .}
31bb1d1b 849 error_popup [strcat [mc "Git directory not found:"] "\n\n$_gitdir"]
dbccbbda
SP
850 exit 1
851}
c80d25db
SP
852if {$_prefix ne {}} {
853 regsub -all {[^/]+/} $_prefix ../ cdup
854 if {[catch {cd $cdup} err]} {
855 catch {wm withdraw .}
31bb1d1b 856 error_popup [strcat [mc "Cannot move to top of working directory:"] "\n\n$err"]
c80d25db
SP
857 exit 1
858 }
859 unset cdup
860} elseif {![is_enabled bare]} {
c52c9452
SP
861 if {[lindex [file split $_gitdir] end] ne {.git}} {
862 catch {wm withdraw .}
31bb1d1b 863 error_popup [strcat [mc "Cannot use funny .git directory:"] "\n\n$_gitdir"]
c52c9452
SP
864 exit 1
865 }
866 if {[catch {cd [file dirname $_gitdir]} err]} {
867 catch {wm withdraw .}
31bb1d1b 868 error_popup [strcat [mc "No working directory"] " [file dirname $_gitdir]:\n\n$err"]
c52c9452
SP
869 exit 1
870 }
dbccbbda 871}
c52c9452
SP
872set _reponame [file split [file normalize $_gitdir]]
873if {[lindex $_reponame end] eq {.git}} {
874 set _reponame [lindex $_reponame end-1]
875} else {
876 set _reponame [lindex $_reponame end]
2d19516d 877}
2d19516d 878
372ef954
SP
879######################################################################
880##
881## global init
882
883set current_diff_path {}
884set current_diff_side {}
885set diff_actions [list]
372ef954
SP
886
887set HEAD {}
888set PARENT {}
889set MERGE_HEAD [list]
890set commit_type {}
891set empty_tree {}
892set current_branch {}
d41b43eb 893set is_detached 0
372ef954 894set current_diff_path {}
9c9f5fa9 895set is_3way_diff 0
372ef954
SP
896set selected_commit_type new
897
cb07fc2a
SP
898######################################################################
899##
e210e674 900## task management
cb07fc2a 901
8f52548a 902set rescan_active 0
131f503b 903set diff_active 0
24263b77 904set last_clicked {}
131f503b 905
e210e674
SP
906set disable_on_lock [list]
907set index_lock_type none
908
909proc lock_index {type} {
910 global index_lock_type disable_on_lock
131f503b 911
043f7011 912 if {$index_lock_type eq {none}} {
e210e674
SP
913 set index_lock_type $type
914 foreach w $disable_on_lock {
915 uplevel #0 $w disabled
916 }
917 return 1
53716a7b 918 } elseif {$index_lock_type eq "begin-$type"} {
e210e674 919 set index_lock_type $type
131f503b
SP
920 return 1
921 }
922 return 0
923}
cb07fc2a 924
e210e674
SP
925proc unlock_index {} {
926 global index_lock_type disable_on_lock
927
928 set index_lock_type none
929 foreach w $disable_on_lock {
930 uplevel #0 $w normal
931 }
932}
933
934######################################################################
935##
936## status
937
f18e40a1 938proc repository_state {ctvar hdvar mhvar} {
c950c66e 939 global current_branch
f18e40a1
SP
940 upvar $ctvar ct $hdvar hd $mhvar mh
941
942 set mh [list]
ec6b424a 943
d41b43eb 944 load_current_branch
81347223 945 if {[catch {set hd [git rev-parse --verify HEAD]}]} {
4539eacd 946 set hd {}
ec6b424a 947 set ct initial
f18e40a1
SP
948 return
949 }
950
c2758a17 951 set merge_head [gitdir MERGE_HEAD]
f18e40a1 952 if {[file exists $merge_head]} {
ec6b424a 953 set ct merge
f18e40a1
SP
954 set fd_mh [open $merge_head r]
955 while {[gets $fd_mh line] >= 0} {
956 lappend mh $line
957 }
958 close $fd_mh
959 return
ec6b424a 960 }
f18e40a1
SP
961
962 set ct normal
ec6b424a
SP
963}
964
4539eacd
SP
965proc PARENT {} {
966 global PARENT empty_tree
967
f18e40a1
SP
968 set p [lindex $PARENT 0]
969 if {$p ne {}} {
970 return $p
4539eacd
SP
971 }
972 if {$empty_tree eq {}} {
81347223 973 set empty_tree [git mktree << {}]
4539eacd
SP
974 }
975 return $empty_tree
976}
977
46aaf90b 978proc rescan {after {honor_trustmtime 1}} {
f18e40a1 979 global HEAD PARENT MERGE_HEAD commit_type
699d5601 980 global ui_index ui_workdir ui_comm
8f52548a 981 global rescan_active file_states
cf25ddc8 982 global repo_config
cb07fc2a 983
8f52548a 984 if {$rescan_active > 0 || ![lock_index read]} return
cb07fc2a 985
f18e40a1 986 repository_state newType newHEAD newMERGE_HEAD
4539eacd 987 if {[string match amend* $commit_type]
f18e40a1
SP
988 && $newType eq {normal}
989 && $newHEAD eq $HEAD} {
e57ca85e 990 } else {
f18e40a1
SP
991 set HEAD $newHEAD
992 set PARENT $newHEAD
993 set MERGE_HEAD $newMERGE_HEAD
994 set commit_type $newType
e57ca85e
SP
995 }
996
cb07fc2a 997 array unset file_states
cb07fc2a 998
1e0a92fd
SP
999 if {!$::GITGUI_BCK_exists &&
1000 (![$ui_comm edit modified]
1001 || [string trim [$ui_comm get 0.0 end]] eq {})} {
b2f3bb1b
SP
1002 if {[string match amend* $commit_type]} {
1003 } elseif {[load_message GITGUI_MSG]} {
131f503b
SP
1004 } elseif {[load_message MERGE_MSG]} {
1005 } elseif {[load_message SQUASH_MSG]} {
1006 }
b2c6fcf1 1007 $ui_comm edit reset
21d7744f 1008 $ui_comm edit modified false
131f503b
SP
1009 }
1010
46aaf90b 1011 if {$honor_trustmtime && $repo_config(gui.trustmtime) eq {true}} {
8f52548a 1012 rescan_stage2 {} $after
e534f3a8 1013 } else {
8f52548a 1014 set rescan_active 1
1ac17950 1015 ui_status [mc "Refreshing file status..."]
0b812616
SP
1016 set fd_rf [git_read update-index \
1017 -q \
1018 --unmerged \
1019 --ignore-missing \
1020 --refresh \
1021 ]
e534f3a8 1022 fconfigure $fd_rf -blocking 0 -translation binary
390adaea 1023 fileevent $fd_rf readable \
8f52548a 1024 [list rescan_stage2 $fd_rf $after]
e534f3a8 1025 }
131f503b
SP
1026}
1027
2fe167b6
SP
1028if {[is_Cygwin]} {
1029 set is_git_info_link {}
1030 set is_git_info_exclude {}
1031 proc have_info_exclude {} {
1032 global is_git_info_link is_git_info_exclude
1033
1034 if {$is_git_info_link eq {}} {
1035 set is_git_info_link [file isfile [gitdir info.lnk]]
1036 }
1037
1038 if {$is_git_info_link} {
1039 if {$is_git_info_exclude eq {}} {
1040 if {[catch {exec test -f [gitdir info exclude]}]} {
1041 set is_git_info_exclude 0
1042 } else {
1043 set is_git_info_exclude 1
1044 }
1045 }
1046 return $is_git_info_exclude
1047 } else {
1048 return [file readable [gitdir info exclude]]
1049 }
1050 }
1051} else {
1052 proc have_info_exclude {} {
1053 return [file readable [gitdir info exclude]]
1054 }
1055}
1056
8f52548a 1057proc rescan_stage2 {fd after} {
4539eacd 1058 global rescan_active buf_rdi buf_rdf buf_rlo
131f503b 1059
043f7011 1060 if {$fd ne {}} {
e534f3a8
SP
1061 read $fd
1062 if {![eof $fd]} return
1063 close $fd
1064 }
131f503b 1065
0b812616 1066 set ls_others [list --exclude-per-directory=.gitignore]
2fe167b6
SP
1067 if {[have_info_exclude]} {
1068 lappend ls_others "--exclude-from=[gitdir info exclude]"
cb07fc2a 1069 }
94a4dd9b
SP
1070 set user_exclude [get_config core.excludesfile]
1071 if {$user_exclude ne {} && [file readable $user_exclude]} {
1072 lappend ls_others "--exclude-from=$user_exclude"
1073 }
cb07fc2a 1074
868c8752
SP
1075 set buf_rdi {}
1076 set buf_rdf {}
1077 set buf_rlo {}
1078
8f52548a 1079 set rescan_active 3
1ac17950 1080 ui_status [mc "Scanning for modified files ..."]
0b812616
SP
1081 set fd_di [git_read diff-index --cached -z [PARENT]]
1082 set fd_df [git_read diff-files -z]
1083 set fd_lo [eval git_read ls-files --others -z $ls_others]
cb07fc2a 1084
51a989ba
SP
1085 fconfigure $fd_di -blocking 0 -translation binary -encoding binary
1086 fconfigure $fd_df -blocking 0 -translation binary -encoding binary
1087 fconfigure $fd_lo -blocking 0 -translation binary -encoding binary
8f52548a
SP
1088 fileevent $fd_di readable [list read_diff_index $fd_di $after]
1089 fileevent $fd_df readable [list read_diff_files $fd_df $after]
1090 fileevent $fd_lo readable [list read_ls_others $fd_lo $after]
cb07fc2a
SP
1091}
1092
131f503b 1093proc load_message {file} {
c950c66e 1094 global ui_comm
131f503b 1095
c2758a17 1096 set f [gitdir $file]
e57ca85e 1097 if {[file isfile $f]} {
131f503b
SP
1098 if {[catch {set fd [open $f r]}]} {
1099 return 0
1100 }
6eb420ef 1101 fconfigure $fd -eofchar {}
e57ca85e 1102 set content [string trim [read $fd]]
131f503b 1103 close $fd
4e55d19a 1104 regsub -all -line {[ \r\t]+$} $content {} content
131f503b
SP
1105 $ui_comm delete 0.0 end
1106 $ui_comm insert end $content
1107 return 1
1108 }
1109 return 0
1110}
1111
8f52548a 1112proc read_diff_index {fd after} {
cb07fc2a
SP
1113 global buf_rdi
1114
1115 append buf_rdi [read $fd]
868c8752
SP
1116 set c 0
1117 set n [string length $buf_rdi]
1118 while {$c < $n} {
1119 set z1 [string first "\0" $buf_rdi $c]
1120 if {$z1 == -1} break
1121 incr z1
1122 set z2 [string first "\0" $buf_rdi $z1]
1123 if {$z2 == -1} break
1124
868c8752 1125 incr c
86291555 1126 set i [split [string range $buf_rdi $c [expr {$z1 - 2}]] { }]
51a989ba 1127 set p [string range $buf_rdi $z1 [expr {$z2 - 1}]]
1461c5f3 1128 merge_state \
51a989ba 1129 [encoding convertfrom $p] \
86291555
SP
1130 [lindex $i 4]? \
1131 [list [lindex $i 0] [lindex $i 2]] \
1461c5f3
SP
1132 [list]
1133 set c $z2
86291555 1134 incr c
cb07fc2a 1135 }
868c8752
SP
1136 if {$c < $n} {
1137 set buf_rdi [string range $buf_rdi $c end]
1138 } else {
1139 set buf_rdi {}
1140 }
1141
8f52548a 1142 rescan_done $fd buf_rdi $after
cb07fc2a
SP
1143}
1144
8f52548a 1145proc read_diff_files {fd after} {
cb07fc2a
SP
1146 global buf_rdf
1147
1148 append buf_rdf [read $fd]
868c8752
SP
1149 set c 0
1150 set n [string length $buf_rdf]
1151 while {$c < $n} {
1152 set z1 [string first "\0" $buf_rdf $c]
1153 if {$z1 == -1} break
1154 incr z1
1155 set z2 [string first "\0" $buf_rdf $z1]
1156 if {$z2 == -1} break
1157
868c8752 1158 incr c
86291555 1159 set i [split [string range $buf_rdf $c [expr {$z1 - 2}]] { }]
51a989ba 1160 set p [string range $buf_rdf $z1 [expr {$z2 - 1}]]
1461c5f3 1161 merge_state \
51a989ba 1162 [encoding convertfrom $p] \
86291555 1163 ?[lindex $i 4] \
1461c5f3 1164 [list] \
86291555 1165 [list [lindex $i 0] [lindex $i 2]]
1461c5f3 1166 set c $z2
86291555 1167 incr c
868c8752
SP
1168 }
1169 if {$c < $n} {
1170 set buf_rdf [string range $buf_rdf $c end]
1171 } else {
1172 set buf_rdf {}
cb07fc2a 1173 }
868c8752 1174
8f52548a 1175 rescan_done $fd buf_rdf $after
cb07fc2a
SP
1176}
1177
8f52548a 1178proc read_ls_others {fd after} {
cb07fc2a
SP
1179 global buf_rlo
1180
1181 append buf_rlo [read $fd]
1182 set pck [split $buf_rlo "\0"]
1183 set buf_rlo [lindex $pck end]
1184 foreach p [lrange $pck 0 end-1] {
89384101
SP
1185 set p [encoding convertfrom $p]
1186 if {[string index $p end] eq {/}} {
1187 set p [string range $p 0 end-1]
1188 }
1189 merge_state $p ?O
cb07fc2a 1190 }
8f52548a 1191 rescan_done $fd buf_rlo $after
cb07fc2a
SP
1192}
1193
8f52548a 1194proc rescan_done {fd buf after} {
f522c9b5 1195 global rescan_active current_diff_path
f7f8d322 1196 global file_states repo_config
7f1df79b 1197 upvar $buf to_clear
cb07fc2a 1198
f7f8d322
SP
1199 if {![eof $fd]} return
1200 set to_clear {}
1201 close $fd
8f52548a 1202 if {[incr rescan_active -1] > 0} return
93f654df 1203
24263b77 1204 prune_selection
f7f8d322
SP
1205 unlock_index
1206 display_all_files
f522c9b5 1207 if {$current_diff_path ne {}} reshow_diff
8f52548a 1208 uplevel #0 $after
cb07fc2a
SP
1209}
1210
24263b77
SP
1211proc prune_selection {} {
1212 global file_states selected_paths
1213
1214 foreach path [array names selected_paths] {
1215 if {[catch {set still_here $file_states($path)}]} {
1216 unset selected_paths($path)
1217 }
1218 }
1219}
1220
cb07fc2a
SP
1221######################################################################
1222##
f522c9b5 1223## ui helpers
cb07fc2a 1224
f522c9b5
SP
1225proc mapicon {w state path} {
1226 global all_icons
1227
1228 if {[catch {set r $all_icons($state$w)}]} {
1229 puts "error: no icon for $w state={$state} $path"
1230 return file_plain
1231 }
1232 return $r
1233}
cb07fc2a 1234
f522c9b5
SP
1235proc mapdesc {state path} {
1236 global all_descs
03e4ec53 1237
f522c9b5
SP
1238 if {[catch {set r $all_descs($state)}]} {
1239 puts "error: no desc for state={$state} $path"
1240 return $state
1241 }
1242 return $r
1243}
03e4ec53 1244
699d5601 1245proc ui_status {msg} {
51530d17 1246 $::main_status show $msg
699d5601
SP
1247}
1248
1249proc ui_ready {{test {}}} {
1ac17950 1250 $::main_status show [mc "Ready."] $test
699d5601
SP
1251}
1252
f522c9b5
SP
1253proc escape_path {path} {
1254 regsub -all {\\} $path "\\\\" path
1255 regsub -all "\n" $path "\\n" path
1256 return $path
cb07fc2a
SP
1257}
1258
f522c9b5
SP
1259proc short_path {path} {
1260 return [escape_path [lindex [file split $path] end]]
7f1df79b
SP
1261}
1262
f522c9b5
SP
1263set next_icon_id 0
1264set null_sha1 [string repeat 0 40]
16403d0b 1265
f522c9b5
SP
1266proc merge_state {path new_state {head_info {}} {index_info {}}} {
1267 global file_states next_icon_id null_sha1
16403d0b 1268
f522c9b5
SP
1269 set s0 [string index $new_state 0]
1270 set s1 [string index $new_state 1]
16403d0b 1271
f522c9b5
SP
1272 if {[catch {set info $file_states($path)}]} {
1273 set state __
1274 set icon n[incr next_icon_id]
1275 } else {
1276 set state [lindex $info 0]
1277 set icon [lindex $info 1]
1278 if {$head_info eq {}} {set head_info [lindex $info 2]}
1279 if {$index_info eq {}} {set index_info [lindex $info 3]}
1280 }
16403d0b 1281
f522c9b5
SP
1282 if {$s0 eq {?}} {set s0 [string index $state 0]} \
1283 elseif {$s0 eq {_}} {set s0 _}
124355d3 1284
f522c9b5
SP
1285 if {$s1 eq {?}} {set s1 [string index $state 1]} \
1286 elseif {$s1 eq {_}} {set s1 _}
16403d0b 1287
f522c9b5
SP
1288 if {$s0 eq {A} && $s1 eq {_} && $head_info eq {}} {
1289 set head_info [list 0 $null_sha1]
1290 } elseif {$s0 ne {_} && [string index $state 0] eq {_}
1291 && $head_info eq {}} {
1292 set head_info $index_info
1293 }
16403d0b 1294
f522c9b5
SP
1295 set file_states($path) [list $s0$s1 $icon \
1296 $head_info $index_info \
1297 ]
1298 return $state
1299}
cb07fc2a 1300
f522c9b5
SP
1301proc display_file_helper {w path icon_name old_m new_m} {
1302 global file_lists
cb07fc2a 1303
f522c9b5 1304 if {$new_m eq {_}} {
156b2921 1305 set lno [lsearch -sorted -exact $file_lists($w) $path]
5f8b70b1 1306 if {$lno >= 0} {
f522c9b5 1307 set file_lists($w) [lreplace $file_lists($w) $lno $lno]
5f8b70b1 1308 incr lno
f522c9b5
SP
1309 $w conf -state normal
1310 $w delete $lno.0 [expr {$lno + 1}].0
1311 $w conf -state disabled
03e4ec53 1312 }
f522c9b5
SP
1313 } elseif {$old_m eq {_} && $new_m ne {_}} {
1314 lappend file_lists($w) $path
1315 set file_lists($w) [lsort -unique $file_lists($w)]
1316 set lno [lsearch -sorted -exact $file_lists($w) $path]
1317 incr lno
1318 $w conf -state normal
1319 $w image create $lno.0 \
1320 -align center -padx 5 -pady 1 \
1321 -name $icon_name \
1322 -image [mapicon $w $new_m $path]
1323 $w insert $lno.1 "[escape_path $path]\n"
1324 $w conf -state disabled
1325 } elseif {$old_m ne $new_m} {
1326 $w conf -state normal
1327 $w image conf $icon_name -image [mapicon $w $new_m $path]
1328 $w conf -state disabled
03e4ec53 1329 }
f522c9b5
SP
1330}
1331
1332proc display_file {path state} {
1333 global file_states selected_paths
1334 global ui_index ui_workdir
03e4ec53 1335
f522c9b5 1336 set old_m [merge_state $path $state]
cb07fc2a 1337 set s $file_states($path)
f522c9b5
SP
1338 set new_m [lindex $s 0]
1339 set icon_name [lindex $s 1]
82cb8706 1340
f522c9b5
SP
1341 set o [string index $old_m 0]
1342 set n [string index $new_m 0]
1343 if {$o eq {U}} {
1344 set o _
1345 }
1346 if {$n eq {U}} {
1347 set n _
cb07fc2a 1348 }
f522c9b5 1349 display_file_helper $ui_index $path $icon_name $o $n
cb07fc2a 1350
f522c9b5
SP
1351 if {[string index $old_m 0] eq {U}} {
1352 set o U
1353 } else {
1354 set o [string index $old_m 1]
82cb8706 1355 }
f522c9b5
SP
1356 if {[string index $new_m 0] eq {U}} {
1357 set n U
1358 } else {
1359 set n [string index $new_m 1]
82cb8706 1360 }
f522c9b5
SP
1361 display_file_helper $ui_workdir $path $icon_name $o $n
1362
1363 if {$new_m eq {__}} {
1364 unset file_states($path)
1365 catch {unset selected_paths($path)}
cb07fc2a 1366 }
f522c9b5 1367}
cb07fc2a 1368
f522c9b5
SP
1369proc display_all_files_helper {w path icon_name m} {
1370 global file_lists
1371
1372 lappend file_lists($w) $path
1373 set lno [expr {[lindex [split [$w index end] .] 0] - 1}]
1374 $w image create end \
1375 -align center -padx 5 -pady 1 \
1376 -name $icon_name \
1377 -image [mapicon $w $m $path]
1378 $w insert end "[escape_path $path]\n"
cb07fc2a
SP
1379}
1380
f522c9b5
SP
1381proc display_all_files {} {
1382 global ui_index ui_workdir
1383 global file_states file_lists
1384 global last_clicked
cb07fc2a 1385
f522c9b5
SP
1386 $ui_index conf -state normal
1387 $ui_workdir conf -state normal
cb07fc2a 1388
f522c9b5
SP
1389 $ui_index delete 0.0 end
1390 $ui_workdir delete 0.0 end
1391 set last_clicked {}
cb07fc2a 1392
f522c9b5
SP
1393 set file_lists($ui_index) [list]
1394 set file_lists($ui_workdir) [list]
16403d0b 1395
f522c9b5
SP
1396 foreach path [lsort [array names file_states]] {
1397 set s $file_states($path)
1398 set m [lindex $s 0]
1399 set icon_name [lindex $s 1]
1400
1401 set s [string index $m 0]
1402 if {$s ne {U} && $s ne {_}} {
1403 display_all_files_helper $ui_index $path \
1404 $icon_name $s
16403d0b 1405 }
cb07fc2a 1406
f522c9b5
SP
1407 if {[string index $m 0] eq {U}} {
1408 set s U
1409 } else {
1410 set s [string index $m 1]
a25c5189 1411 }
f522c9b5
SP
1412 if {$s ne {_}} {
1413 display_all_files_helper $ui_workdir $path \
1414 $icon_name $s
a25c5189
SP
1415 }
1416 }
1417
f522c9b5
SP
1418 $ui_index conf -state disabled
1419 $ui_workdir conf -state disabled
a25c5189
SP
1420}
1421
ec6b424a
SP
1422######################################################################
1423##
f522c9b5 1424## icons
ec6b424a 1425
f522c9b5
SP
1426set filemask {
1427#define mask_width 14
1428#define mask_height 15
1429static unsigned char mask_bits[] = {
1430 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f,
1431 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f,
1432 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f};
1433}
cb07fc2a
SP
1434
1435image create bitmap file_plain -background white -foreground black -data {
1436#define plain_width 14
1437#define plain_height 15
1438static unsigned char plain_bits[] = {
1439 0xfe, 0x01, 0x02, 0x03, 0x02, 0x05, 0x02, 0x09, 0x02, 0x1f, 0x02, 0x10,
1440 0x02, 0x10, 0x02, 0x10, 0x02, 0x10, 0x02, 0x10, 0x02, 0x10, 0x02, 0x10,
1441 0x02, 0x10, 0x02, 0x10, 0xfe, 0x1f};
1442} -maskdata $filemask
1443
1444image create bitmap file_mod -background white -foreground blue -data {
1445#define mod_width 14
1446#define mod_height 15
1447static unsigned char mod_bits[] = {
1448 0xfe, 0x01, 0x02, 0x03, 0x7a, 0x05, 0x02, 0x09, 0x7a, 0x1f, 0x02, 0x10,
1449 0xfa, 0x17, 0x02, 0x10, 0xfa, 0x17, 0x02, 0x10, 0xfa, 0x17, 0x02, 0x10,
1450 0xfa, 0x17, 0x02, 0x10, 0xfe, 0x1f};
1451} -maskdata $filemask
1452
131f503b
SP
1453image create bitmap file_fulltick -background white -foreground "#007000" -data {
1454#define file_fulltick_width 14
1455#define file_fulltick_height 15
1456static unsigned char file_fulltick_bits[] = {
cb07fc2a
SP
1457 0xfe, 0x01, 0x02, 0x1a, 0x02, 0x0c, 0x02, 0x0c, 0x02, 0x16, 0x02, 0x16,
1458 0x02, 0x13, 0x00, 0x13, 0x86, 0x11, 0x8c, 0x11, 0xd8, 0x10, 0xf2, 0x10,
1459 0x62, 0x10, 0x02, 0x10, 0xfe, 0x1f};
1460} -maskdata $filemask
1461
1462image create bitmap file_parttick -background white -foreground "#005050" -data {
1463#define parttick_width 14
1464#define parttick_height 15
1465static unsigned char parttick_bits[] = {
1466 0xfe, 0x01, 0x02, 0x03, 0x7a, 0x05, 0x02, 0x09, 0x7a, 0x1f, 0x02, 0x10,
1467 0x7a, 0x14, 0x02, 0x16, 0x02, 0x13, 0x8a, 0x11, 0xda, 0x10, 0x72, 0x10,
1468 0x22, 0x10, 0x02, 0x10, 0xfe, 0x1f};
1469} -maskdata $filemask
1470
1471image create bitmap file_question -background white -foreground black -data {
1472#define file_question_width 14
1473#define file_question_height 15
1474static unsigned char file_question_bits[] = {
1475 0xfe, 0x01, 0x02, 0x02, 0xe2, 0x04, 0xf2, 0x09, 0x1a, 0x1b, 0x0a, 0x13,
1476 0x82, 0x11, 0xc2, 0x10, 0x62, 0x10, 0x62, 0x10, 0x02, 0x10, 0x62, 0x10,
1477 0x62, 0x10, 0x02, 0x10, 0xfe, 0x1f};
1478} -maskdata $filemask
1479
1480image create bitmap file_removed -background white -foreground red -data {
1481#define file_removed_width 14
1482#define file_removed_height 15
1483static unsigned char file_removed_bits[] = {
1484 0xfe, 0x01, 0x02, 0x03, 0x02, 0x05, 0x02, 0x09, 0x02, 0x1f, 0x02, 0x10,
1485 0x1a, 0x16, 0x32, 0x13, 0xe2, 0x11, 0xc2, 0x10, 0xe2, 0x11, 0x32, 0x13,
1486 0x1a, 0x16, 0x02, 0x10, 0xfe, 0x1f};
1487} -maskdata $filemask
1488
1489image create bitmap file_merge -background white -foreground blue -data {
1490#define file_merge_width 14
1491#define file_merge_height 15
1492static unsigned char file_merge_bits[] = {
1493 0xfe, 0x01, 0x02, 0x03, 0x62, 0x05, 0x62, 0x09, 0x62, 0x1f, 0x62, 0x10,
1494 0xfa, 0x11, 0xf2, 0x10, 0x62, 0x10, 0x02, 0x10, 0xfa, 0x17, 0x02, 0x10,
1495 0xfa, 0x17, 0x02, 0x10, 0xfe, 0x1f};
1496} -maskdata $filemask
1497
6b292675 1498set ui_index .vpane.files.index.list
0812665e 1499set ui_workdir .vpane.files.workdir.list
21e409ad
SP
1500
1501set all_icons(_$ui_index) file_plain
1502set all_icons(A$ui_index) file_fulltick
1503set all_icons(M$ui_index) file_fulltick
1504set all_icons(D$ui_index) file_removed
1505set all_icons(U$ui_index) file_merge
1506
1507set all_icons(_$ui_workdir) file_plain
1508set all_icons(M$ui_workdir) file_mod
1509set all_icons(D$ui_workdir) file_question
3b4db3c1 1510set all_icons(U$ui_workdir) file_merge
21e409ad
SP
1511set all_icons(O$ui_workdir) file_plain
1512
131f503b 1513set max_status_desc 0
cb07fc2a 1514foreach i {
1ac17950
CS
1515 {__ {mc "Unmodified"}}
1516
1517 {_M {mc "Modified, not staged"}}
1518 {M_ {mc "Staged for commit"}}
1519 {MM {mc "Portions staged for commit"}}
1520 {MD {mc "Staged for commit, missing"}}
1521
1522 {_O {mc "Untracked, not staged"}}
1523 {A_ {mc "Staged for commit"}}
1524 {AM {mc "Portions staged for commit"}}
1525 {AD {mc "Staged for commit, missing"}}
1526
1527 {_D {mc "Missing"}}
1528 {D_ {mc "Staged for removal"}}
1529 {DO {mc "Staged for removal, still present"}}
1530
1531 {U_ {mc "Requires merge resolution"}}
1532 {UU {mc "Requires merge resolution"}}
1533 {UM {mc "Requires merge resolution"}}
1534 {UD {mc "Requires merge resolution"}}
cb07fc2a 1535 } {
1ac17950
CS
1536 set text [eval [lindex $i 1]]
1537 if {$max_status_desc < [string length $text]} {
1538 set max_status_desc [string length $text]
131f503b 1539 }
1ac17950 1540 set all_descs([lindex $i 0]) $text
cb07fc2a 1541}
21e409ad 1542unset i
cb07fc2a
SP
1543
1544######################################################################
1545##
1546## util
1547
35874c16
SP
1548proc scrollbar2many {list mode args} {
1549 foreach w $list {eval $w $mode $args}
1550}
1551
1552proc many2scrollbar {list mode sb top bottom} {
1553 $sb set $top $bottom
1554 foreach w $list {$w $mode moveto $top}
1555}
1556
b4946930
SP
1557proc incr_font_size {font {amt 1}} {
1558 set sz [font configure $font -size]
1559 incr sz $amt
1560 font configure $font -size $sz
1561 font configure ${font}bold -size $sz
debcd0fd 1562 font configure ${font}italic -size $sz
b4946930
SP
1563}
1564
cb07fc2a
SP
1565######################################################################
1566##
1567## ui commands
1568
1ac17950 1569set starting_gitk_msg [mc "Starting gitk... please wait..."]
cc4b1c02 1570
d0752429 1571proc do_gitk {revs} {
20ddfcaa
SP
1572 # -- Always start gitk through whatever we were loaded with. This
1573 # lets us bypass using shell process on Windows systems.
1574 #
02efd48f
SP
1575 set exe [file join [file dirname $::_git] gitk]
1576 set cmd [list [info nameofexecutable] $exe]
7aecb128 1577 if {! [file exists $exe]} {
1ac17950 1578 error_popup [mc "Unable to start gitk:\n\n%s does not exist" $exe]
cb07fc2a 1579 } else {
02efd48f
SP
1580 eval exec $cmd $revs &
1581 ui_status $::starting_gitk_msg
d0752429 1582 after 10000 {
699d5601 1583 ui_ready $starting_gitk_msg
d0752429 1584 }
cb07fc2a
SP
1585 }
1586}
1587
b5834d70 1588set is_quitting 0
c4fe7728 1589
cb07fc2a 1590proc do_quit {} {
c950c66e 1591 global ui_comm is_quitting repo_config commit_type
4578c5cb 1592 global GITGUI_BCK_exists GITGUI_BCK_i
c4fe7728 1593
b5834d70
SP
1594 if {$is_quitting} return
1595 set is_quitting 1
131f503b 1596
db7f34d4
SP
1597 if {[winfo exists $ui_comm]} {
1598 # -- Stash our current commit buffer.
1599 #
1600 set save [gitdir GITGUI_MSG]
4578c5cb
SP
1601 if {$GITGUI_BCK_exists && ![$ui_comm edit modified]} {
1602 file rename -force [gitdir GITGUI_BCK] $save
1603 set GITGUI_BCK_exists 0
db7f34d4 1604 } else {
4578c5cb
SP
1605 set msg [string trim [$ui_comm get 0.0 end]]
1606 regsub -all -line {[ \r\t]+$} $msg {} msg
1607 if {(![string match amend* $commit_type]
1608 || [$ui_comm edit modified])
1609 && $msg ne {}} {
1610 catch {
1611 set fd [open $save w]
1612 puts -nonewline $fd $msg
1613 close $fd
1614 }
1615 } else {
1616 catch {file delete $save}
1617 }
1618 }
1619
1620 # -- Remove our editor backup, its not needed.
1621 #
1622 after cancel $GITGUI_BCK_i
1623 if {$GITGUI_BCK_exists} {
1624 catch {file delete [gitdir GITGUI_BCK]}
131f503b 1625 }
131f503b 1626
db7f34d4
SP
1627 # -- Stash our current window geometry into this repository.
1628 #
1629 set cfg_geometry [list]
1630 lappend cfg_geometry [wm geometry .]
1631 lappend cfg_geometry [lindex [.vpane sash coord 0] 1]
1632 lappend cfg_geometry [lindex [.vpane.files sash coord 0] 0]
1633 if {[catch {set rc_geometry $repo_config(gui.geometry)}]} {
1634 set rc_geometry {}
1635 }
1636 if {$cfg_geometry ne $rc_geometry} {
81347223 1637 catch {git config gui.geometry $cfg_geometry}
db7f34d4 1638 }
51f4d16b
SP
1639 }
1640
cb07fc2a
SP
1641 destroy .
1642}
1643
1644proc do_rescan {} {
699d5601 1645 rescan ui_ready
cb07fc2a
SP
1646}
1647
6e27d826 1648proc do_commit {} {
ec6b424a 1649 commit_tree
6e27d826
SP
1650}
1651
24263b77 1652proc toggle_or_diff {w x y} {
20a53c02 1653 global file_states file_lists current_diff_path ui_index ui_workdir
24263b77 1654 global last_clicked selected_paths
131f503b 1655
cb07fc2a
SP
1656 set pos [split [$w index @$x,$y] .]
1657 set lno [lindex $pos 0]
1658 set col [lindex $pos 1]
24263b77
SP
1659 set path [lindex $file_lists($w) [expr {$lno - 1}]]
1660 if {$path eq {}} {
1661 set last_clicked {}
1662 return
1663 }
1664
1665 set last_clicked [list $w $lno]
1666 array unset selected_paths
1667 $ui_index tag remove in_sel 0.0 end
0812665e 1668 $ui_workdir tag remove in_sel 0.0 end
cb07fc2a 1669
24263b77 1670 if {$col == 0} {
20a53c02 1671 if {$current_diff_path eq $path} {
32e0bcab
SP
1672 set after {reshow_diff;}
1673 } else {
1674 set after {}
1675 }
de5f6d5d 1676 if {$w eq $ui_index} {
74d18d2e 1677 update_indexinfo \
93e912c5 1678 "Unstaging [short_path $path] from commit" \
74d18d2e 1679 [list $path] \
699d5601 1680 [concat $after [list ui_ready]]
de5f6d5d 1681 } elseif {$w eq $ui_workdir} {
74d18d2e 1682 update_index \
4d583c86 1683 "Adding [short_path $path]" \
74d18d2e 1684 [list $path] \
699d5601 1685 [concat $after [list ui_ready]]
74d18d2e 1686 }
24263b77 1687 } else {
03e4ec53 1688 show_diff $path $w $lno
cb07fc2a
SP
1689 }
1690}
1691
24263b77 1692proc add_one_to_selection {w x y} {
833eda73 1693 global file_lists last_clicked selected_paths
7f1df79b 1694
833eda73 1695 set lno [lindex [split [$w index @$x,$y] .] 0]
24263b77
SP
1696 set path [lindex $file_lists($w) [expr {$lno - 1}]]
1697 if {$path eq {}} {
1698 set last_clicked {}
1699 return
1700 }
cb07fc2a 1701
833eda73
SP
1702 if {$last_clicked ne {}
1703 && [lindex $last_clicked 0] ne $w} {
1704 array unset selected_paths
1705 [lindex $last_clicked 0] tag remove in_sel 0.0 end
1706 }
1707
24263b77
SP
1708 set last_clicked [list $w $lno]
1709 if {[catch {set in_sel $selected_paths($path)}]} {
1710 set in_sel 0
1711 }
1712 if {$in_sel} {
1713 unset selected_paths($path)
1714 $w tag remove in_sel $lno.0 [expr {$lno + 1}].0
1715 } else {
1716 set selected_paths($path) 1
1717 $w tag add in_sel $lno.0 [expr {$lno + 1}].0
1718 }
1719}
1720
1721proc add_range_to_selection {w x y} {
833eda73 1722 global file_lists last_clicked selected_paths
24263b77
SP
1723
1724 if {[lindex $last_clicked 0] ne $w} {
1725 toggle_or_diff $w $x $y
1726 return
cb07fc2a 1727 }
24263b77 1728
833eda73 1729 set lno [lindex [split [$w index @$x,$y] .] 0]
24263b77
SP
1730 set lc [lindex $last_clicked 1]
1731 if {$lc < $lno} {
1732 set begin $lc
1733 set end $lno
1734 } else {
1735 set begin $lno
1736 set end $lc
1737 }
1738
1739 foreach path [lrange $file_lists($w) \
1740 [expr {$begin - 1}] \
1741 [expr {$end - 1}]] {
1742 set selected_paths($path) 1
1743 }
1744 $w tag add in_sel $begin.0 [expr {$end + 1}].0
cb07fc2a
SP
1745}
1746
1747######################################################################
1748##
a4bee597 1749## ui construction
db453781 1750
6bbd1cb9 1751load_config 0
92148d80 1752apply_config
2ebba528
SP
1753set ui_comm {}
1754
cb07fc2a 1755# -- Menu Bar
a49c67d1 1756#
b4946930 1757menu .mbar -tearoff 0
1ac17950
CS
1758.mbar add cascade -label [mc Repository] -menu .mbar.repository
1759.mbar add cascade -label [mc Edit] -menu .mbar.edit
64a906f8 1760if {[is_enabled branch]} {
1ac17950 1761 .mbar add cascade -label [mc Branch] -menu .mbar.branch
700a65ce 1762}
2ebba528 1763if {[is_enabled multicommit] || [is_enabled singlecommit]} {
a9813cb5 1764 .mbar add cascade -label [mc Commit@@noun] -menu .mbar.commit
2ebba528 1765}
64a906f8 1766if {[is_enabled transport]} {
1ac17950
CS
1767 .mbar add cascade -label [mc Merge] -menu .mbar.merge
1768 .mbar add cascade -label [mc Fetch] -menu .mbar.fetch
1769 .mbar add cascade -label [mc Push] -menu .mbar.push
4ccdab02 1770}
cb07fc2a
SP
1771. configure -menu .mbar
1772
a4abfa62 1773# -- Repository Menu
a49c67d1 1774#
a4abfa62 1775menu .mbar.repository
35874c16
SP
1776
1777.mbar.repository add command \
1ac17950 1778 -label [mc "Browse Current Branch's Files"] \
c74b6c66 1779 -command {browser::new $current_branch}
a8139888 1780set ui_browse_current [.mbar.repository index last]
8e891fac 1781.mbar.repository add command \
1ac17950 1782 -label [mc "Browse Branch Files..."] \
8e891fac 1783 -command browser_open::dialog
35874c16
SP
1784.mbar.repository add separator
1785
d0752429 1786.mbar.repository add command \
1ac17950 1787 -label [mc "Visualize Current Branch's History"] \
7416bbc6 1788 -command {do_gitk $current_branch}
a8139888 1789set ui_visualize_current [.mbar.repository index last]
5753ef1a 1790.mbar.repository add command \
1ac17950 1791 -label [mc "Visualize All Branch History"] \
7416bbc6 1792 -command {do_gitk --all}
d0752429 1793.mbar.repository add separator
75e355d6 1794
a8139888
SP
1795proc current_branch_write {args} {
1796 global current_branch
1797 .mbar.repository entryconf $::ui_browse_current \
1ac17950 1798 -label [mc "Browse %s's Files" $current_branch]
a8139888 1799 .mbar.repository entryconf $::ui_visualize_current \
1ac17950 1800 -label [mc "Visualize %s's History" $current_branch]
a8139888
SP
1801}
1802trace add variable current_branch write current_branch_write
1803
cf25ddc8 1804if {[is_enabled multicommit]} {
1ac17950 1805 .mbar.repository add command -label [mc "Database Statistics"] \
7416bbc6 1806 -command do_stats
0fd49d0a 1807
1ac17950 1808 .mbar.repository add command -label [mc "Compress Database"] \
7416bbc6 1809 -command do_gc
4aca740b 1810
1ac17950 1811 .mbar.repository add command -label [mc "Verify Database"] \
7416bbc6 1812 -command do_fsck_objects
444f92d0 1813
a4abfa62 1814 .mbar.repository add separator
75e355d6 1815
20ddfcaa
SP
1816 if {[is_Cygwin]} {
1817 .mbar.repository add command \
1ac17950 1818 -label [mc "Create Desktop Icon"] \
7416bbc6 1819 -command do_cygwin_shortcut
20ddfcaa 1820 } elseif {[is_Windows]} {
a4abfa62 1821 .mbar.repository add command \
1ac17950 1822 -label [mc "Create Desktop Icon"] \
7416bbc6 1823 -command do_windows_shortcut
06c31115 1824 } elseif {[is_MacOSX]} {
a4abfa62 1825 .mbar.repository add command \
1ac17950 1826 -label [mc "Create Desktop Icon"] \
7416bbc6 1827 -command do_macosx_app
4aca740b 1828 }
4ccdab02 1829}
85ab313e 1830
1ac17950 1831.mbar.repository add command -label [mc Quit] \
cb07fc2a 1832 -command do_quit \
7416bbc6 1833 -accelerator $M1T-Q
cb07fc2a 1834
9861671d
SP
1835# -- Edit Menu
1836#
1837menu .mbar.edit
1ac17950 1838.mbar.edit add command -label [mc Undo] \
9861671d 1839 -command {catch {[focus] edit undo}} \
7416bbc6 1840 -accelerator $M1T-Z
1ac17950 1841.mbar.edit add command -label [mc Redo] \
9861671d 1842 -command {catch {[focus] edit redo}} \
7416bbc6 1843 -accelerator $M1T-Y
9861671d 1844.mbar.edit add separator
1ac17950 1845.mbar.edit add command -label [mc Cut] \
9861671d 1846 -command {catch {tk_textCut [focus]}} \
7416bbc6 1847 -accelerator $M1T-X
1ac17950 1848.mbar.edit add command -label [mc Copy] \
9861671d 1849 -command {catch {tk_textCopy [focus]}} \
7416bbc6 1850 -accelerator $M1T-C
1ac17950 1851.mbar.edit add command -label [mc Paste] \
9861671d 1852 -command {catch {tk_textPaste [focus]; [focus] see insert}} \
7416bbc6 1853 -accelerator $M1T-V
1ac17950 1854.mbar.edit add command -label [mc Delete] \
9861671d 1855 -command {catch {[focus] delete sel.first sel.last}} \
7416bbc6 1856 -accelerator Del
9861671d 1857.mbar.edit add separator
1ac17950 1858.mbar.edit add command -label [mc "Select All"] \
9861671d 1859 -command {catch {[focus] tag add sel 0.0 end}} \
7416bbc6 1860 -accelerator $M1T-A
9861671d 1861
85ab313e
SP
1862# -- Branch Menu
1863#
64a906f8 1864if {[is_enabled branch]} {
700a65ce
SP
1865 menu .mbar.branch
1866
1ac17950 1867 .mbar.branch add command -label [mc "Create..."] \
b1fa2bff 1868 -command branch_create::dialog \
7416bbc6 1869 -accelerator $M1T-N
700a65ce
SP
1870 lappend disable_on_lock [list .mbar.branch entryconf \
1871 [.mbar.branch index last] -state]
1872
1ac17950 1873 .mbar.branch add command -label [mc "Checkout..."] \
d41b43eb
SP
1874 -command branch_checkout::dialog \
1875 -accelerator $M1T-O
1876 lappend disable_on_lock [list .mbar.branch entryconf \
1877 [.mbar.branch index last] -state]
1878
1ac17950 1879 .mbar.branch add command -label [mc "Rename..."] \
61f82ce7
SP
1880 -command branch_rename::dialog
1881 lappend disable_on_lock [list .mbar.branch entryconf \
1882 [.mbar.branch index last] -state]
1883
1ac17950 1884 .mbar.branch add command -label [mc "Delete..."] \
3206c63d 1885 -command branch_delete::dialog
700a65ce
SP
1886 lappend disable_on_lock [list .mbar.branch entryconf \
1887 [.mbar.branch index last] -state]
fd234dfd 1888
1ac17950 1889 .mbar.branch add command -label [mc "Reset..."] \
a6c9b081 1890 -command merge::reset_hard
fd234dfd
SP
1891 lappend disable_on_lock [list .mbar.branch entryconf \
1892 [.mbar.branch index last] -state]
700a65ce
SP
1893}
1894
cb07fc2a 1895# -- Commit Menu
a49c67d1 1896#
2ebba528
SP
1897if {[is_enabled multicommit] || [is_enabled singlecommit]} {
1898 menu .mbar.commit
1899
1900 .mbar.commit add radiobutton \
1ac17950 1901 -label [mc "New Commit"] \
2ebba528
SP
1902 -command do_select_commit_type \
1903 -variable selected_commit_type \
7416bbc6 1904 -value new
2ebba528
SP
1905 lappend disable_on_lock \
1906 [list .mbar.commit entryconf [.mbar.commit index last] -state]
24ac9b75 1907
2ebba528 1908 .mbar.commit add radiobutton \
1ac17950 1909 -label [mc "Amend Last Commit"] \
2ebba528
SP
1910 -command do_select_commit_type \
1911 -variable selected_commit_type \
7416bbc6 1912 -value amend
2ebba528
SP
1913 lappend disable_on_lock \
1914 [list .mbar.commit entryconf [.mbar.commit index last] -state]
24ac9b75 1915
2ebba528 1916 .mbar.commit add separator
24ac9b75 1917
1ac17950 1918 .mbar.commit add command -label [mc Rescan] \
2ebba528 1919 -command do_rescan \
7416bbc6 1920 -accelerator F5
2ebba528
SP
1921 lappend disable_on_lock \
1922 [list .mbar.commit entryconf [.mbar.commit index last] -state]
24ac9b75 1923
1ac17950 1924 .mbar.commit add command -label [mc "Stage To Commit"] \
7416bbc6 1925 -command do_add_selection
2ebba528
SP
1926 lappend disable_on_lock \
1927 [list .mbar.commit entryconf [.mbar.commit index last] -state]
24ac9b75 1928
1ac17950 1929 .mbar.commit add command -label [mc "Stage Changed Files To Commit"] \
2ebba528 1930 -command do_add_all \
7416bbc6 1931 -accelerator $M1T-I
2ebba528
SP
1932 lappend disable_on_lock \
1933 [list .mbar.commit entryconf [.mbar.commit index last] -state]
24ac9b75 1934
1ac17950 1935 .mbar.commit add command -label [mc "Unstage From Commit"] \
7416bbc6 1936 -command do_unstage_selection
2ebba528
SP
1937 lappend disable_on_lock \
1938 [list .mbar.commit entryconf [.mbar.commit index last] -state]
e734817d 1939
1ac17950 1940 .mbar.commit add command -label [mc "Revert Changes"] \
7416bbc6 1941 -command do_revert_selection
2ebba528
SP
1942 lappend disable_on_lock \
1943 [list .mbar.commit entryconf [.mbar.commit index last] -state]
e734817d 1944
2ebba528 1945 .mbar.commit add separator
1461c5f3 1946
1ac17950 1947 .mbar.commit add command -label [mc "Sign Off"] \
2ebba528 1948 -command do_signoff \
7416bbc6 1949 -accelerator $M1T-S
24ac9b75 1950
a9813cb5 1951 .mbar.commit add command -label [mc Commit@@verb] \
2ebba528 1952 -command do_commit \
7416bbc6 1953 -accelerator $M1T-Return
2ebba528
SP
1954 lappend disable_on_lock \
1955 [list .mbar.commit entryconf [.mbar.commit index last] -state]
1956}
cb07fc2a 1957
9b28a8b9
SP
1958# -- Merge Menu
1959#
1960if {[is_enabled branch]} {
1961 menu .mbar.merge
1ac17950 1962 .mbar.merge add command -label [mc "Local Merge..."] \
a870ddc0
SP
1963 -command merge::dialog \
1964 -accelerator $M1T-M
9b28a8b9
SP
1965 lappend disable_on_lock \
1966 [list .mbar.merge entryconf [.mbar.merge index last] -state]
1ac17950 1967 .mbar.merge add command -label [mc "Abort Merge..."] \
a6c9b081 1968 -command merge::reset_hard
9b28a8b9
SP
1969 lappend disable_on_lock \
1970 [list .mbar.merge entryconf [.mbar.merge index last] -state]
9b28a8b9
SP
1971}
1972
1973# -- Transport Menu
1974#
1975if {[is_enabled transport]} {
1976 menu .mbar.fetch
1977
1978 menu .mbar.push
1ac17950 1979 .mbar.push add command -label [mc "Push..."] \
840bcfa7
SP
1980 -command do_push_anywhere \
1981 -accelerator $M1T-P
1ac17950 1982 .mbar.push add command -label [mc "Delete..."] \
aa252f19 1983 -command remote_branch_delete::dialog
9b28a8b9
SP
1984}
1985
0c8d7839
SP
1986if {[is_MacOSX]} {
1987 # -- Apple Menu (Mac OS X only)
1988 #
1ac17950 1989 .mbar add cascade -label [mc Apple] -menu .mbar.apple
0c8d7839
SP
1990 menu .mbar.apple
1991
1ac17950 1992 .mbar.apple add command -label [mc "About %s" [appname]] \
7416bbc6 1993 -command do_about
1ac17950 1994 .mbar.apple add command -label [mc "Options..."] \
7416bbc6 1995 -command do_options
0c8d7839
SP
1996} else {
1997 # -- Edit Menu
1998 #
1999 .mbar.edit add separator
1ac17950 2000 .mbar.edit add command -label [mc "Options..."] \
7416bbc6 2001 -command do_options
273984fc 2002}
557afe82 2003
273984fc
SP
2004# -- Help Menu
2005#
1ac17950 2006.mbar add cascade -label [mc Help] -menu .mbar.help
273984fc 2007menu .mbar.help
0c8d7839 2008
273984fc 2009if {![is_MacOSX]} {
1ac17950 2010 .mbar.help add command -label [mc "About %s" [appname]] \
7416bbc6 2011 -command do_about
0c8d7839 2012}
82aa2354 2013
273984fc
SP
2014set browser {}
2015catch {set browser $repo_config(instaweb.browser)}
20ddfcaa 2016set doc_path [file dirname [gitexec]]
273984fc
SP
2017set doc_path [file join $doc_path Documentation index.html]
2018
20ddfcaa 2019if {[is_Cygwin]} {
ee405993 2020 set doc_path [exec cygpath --mixed $doc_path]
273984fc
SP
2021}
2022
2023if {$browser eq {}} {
2024 if {[is_MacOSX]} {
2025 set browser open
20ddfcaa 2026 } elseif {[is_Cygwin]} {
273984fc
SP
2027 set program_files [file dirname [exec cygpath --windir]]
2028 set program_files [file join $program_files {Program Files}]
2029 set firefox [file join $program_files {Mozilla Firefox} firefox.exe]
2030 set ie [file join $program_files {Internet Explorer} IEXPLORE.EXE]
2031 if {[file exists $firefox]} {
2032 set browser $firefox
2033 } elseif {[file exists $ie]} {
2034 set browser $ie
2035 }
2036 unset program_files firefox ie
2037 }
2038}
2039
2040if {[file isfile $doc_path]} {
2041 set doc_url "file:$doc_path"
2042} else {
2043 set doc_url {http://www.kernel.org/pub/software/scm/git/docs/}
2044}
2045
2046if {$browser ne {}} {
1ac17950 2047 .mbar.help add command -label [mc "Online Documentation"] \
7416bbc6 2048 -command [list exec $browser $doc_url &]
273984fc
SP
2049}
2050unset browser doc_path doc_url
82aa2354 2051
2ebba528
SP
2052# -- Standard bindings
2053#
39fa2a98 2054wm protocol . WM_DELETE_WINDOW do_quit
2ebba528
SP
2055bind all <$M1B-Key-q> do_quit
2056bind all <$M1B-Key-Q> do_quit
2057bind all <$M1B-Key-w> {destroy [winfo toplevel %W]}
2058bind all <$M1B-Key-W> {destroy [winfo toplevel %W]}
2059
3e45ee1e
SP
2060set subcommand_args {}
2061proc usage {} {
2062 puts stderr "usage: $::argv0 $::subcommand $::subcommand_args"
2063 exit 1
2064}
2065
2ebba528
SP
2066# -- Not a normal commit type invocation? Do that instead!
2067#
258871d3 2068switch -- $subcommand {
85d2d597 2069browser -
2ebba528 2070blame {
c52c9452
SP
2071 set subcommand_args {rev? path}
2072 if {$argv eq {}} usage
a0db0d61 2073 set head {}
3e45ee1e
SP
2074 set path {}
2075 set is_path 0
2076 foreach a $argv {
2077 if {$is_path || [file exists $_prefix$a]} {
2078 if {$path ne {}} usage
6b3d8b97 2079 set path $_prefix$a
3e45ee1e
SP
2080 break
2081 } elseif {$a eq {--}} {
2082 if {$path ne {}} {
a0db0d61
SP
2083 if {$head ne {}} usage
2084 set head $path
3e45ee1e
SP
2085 set path {}
2086 }
2087 set is_path 1
a0db0d61
SP
2088 } elseif {$head eq {}} {
2089 if {$head ne {}} usage
2090 set head $a
c52c9452 2091 set is_path 1
3e45ee1e
SP
2092 } else {
2093 usage
2094 }
2095 }
2096 unset is_path
2097
c52c9452
SP
2098 if {$head ne {} && $path eq {}} {
2099 set path $_prefix$head
2100 set head {}
2101 }
2102
a0db0d61 2103 if {$head eq {}} {
d41b43eb 2104 load_current_branch
a0db0d61 2105 } else {
02087abc
SP
2106 if {[regexp {^[0-9a-f]{1,39}$} $head]} {
2107 if {[catch {
2108 set head [git rev-parse --verify $head]
2109 } err]} {
2110 puts stderr $err
2111 exit 1
2112 }
2113 }
a0db0d61 2114 set current_branch $head
2ebba528 2115 }
a0db0d61 2116
85d2d597
SP
2117 switch -- $subcommand {
2118 browser {
2119 if {$head eq {}} {
2120 if {$path ne {} && [file isdirectory $path]} {
2121 set head $current_branch
2122 } else {
2123 set head $path
2124 set path {}
2125 }
2126 }
2127 browser::new $head $path
2128 }
2129 blame {
2130 if {$head eq {} && ![file exists $path]} {
c8c4854b 2131 puts stderr [mc "fatal: cannot stat path %s: No such file or directory" $path]
85d2d597
SP
2132 exit 1
2133 }
2134 blame::new $head $path
2135 }
c52c9452 2136 }
258871d3
SP
2137 return
2138}
2139citool -
2140gui {
2141 if {[llength $argv] != 0} {
2142 puts -nonewline stderr "usage: $argv0"
2143 if {$subcommand ne {gui} && [appname] ne "git-$subcommand"} {
2144 puts -nonewline stderr " $subcommand"
2145 }
2146 puts stderr {}
2147 exit 1
2148 }
2149 # fall through to setup UI for commits
2ebba528 2150}
2ebba528 2151default {
c0f7a6c3 2152 puts stderr "usage: $argv0 \[{blame|browser|citool}\]"
2ebba528
SP
2153 exit 1
2154}
2155}
2156
8553b772
SP
2157# -- Branch Control
2158#
2159frame .branch \
2160 -borderwidth 1 \
2161 -relief sunken
2162label .branch.l1 \
1ac17950 2163 -text [mc "Current Branch:"] \
8553b772 2164 -anchor w \
7416bbc6 2165 -justify left
8553b772
SP
2166label .branch.cb \
2167 -textvariable current_branch \
2168 -anchor w \
7416bbc6 2169 -justify left
8553b772
SP
2170pack .branch.l1 -side left
2171pack .branch.cb -side left -fill x
2172pack .branch -side top -fill x
2173
cb07fc2a 2174# -- Main Window Layout
a49c67d1 2175#
cb07fc2a
SP
2176panedwindow .vpane -orient vertical
2177panedwindow .vpane.files -orient horizontal
c5a1eb88 2178.vpane add .vpane.files -sticky nsew -height 100 -width 200
cb07fc2a
SP
2179pack .vpane -anchor n -side top -fill both -expand 1
2180
2181# -- Index File List
a49c67d1 2182#
c5a1eb88 2183frame .vpane.files.index -height 100 -width 200
1ac17950 2184label .vpane.files.index.title -text [mc "Staged Changes (Will Be Committed)"] \
9adccb05 2185 -background lightgreen
cb07fc2a 2186text $ui_index -background white -borderwidth 0 \
c5a1eb88 2187 -width 20 -height 10 \
3c236977 2188 -wrap none \
6c6dd01a 2189 -cursor $cursor_ptr \
3c236977
SP
2190 -xscrollcommand {.vpane.files.index.sx set} \
2191 -yscrollcommand {.vpane.files.index.sy set} \
cb07fc2a 2192 -state disabled
3c236977
SP
2193scrollbar .vpane.files.index.sx -orient h -command [list $ui_index xview]
2194scrollbar .vpane.files.index.sy -orient v -command [list $ui_index yview]
cb07fc2a 2195pack .vpane.files.index.title -side top -fill x
3c236977
SP
2196pack .vpane.files.index.sx -side bottom -fill x
2197pack .vpane.files.index.sy -side right -fill y
cb07fc2a
SP
2198pack $ui_index -side left -fill both -expand 1
2199.vpane.files add .vpane.files.index -sticky nsew
2200
0812665e 2201# -- Working Directory File List
a49c67d1 2202#
c5a1eb88 2203frame .vpane.files.workdir -height 100 -width 200
1ac17950 2204label .vpane.files.workdir.title -text [mc "Unstaged Changes (Will Not Be Committed)"] \
9adccb05 2205 -background lightsalmon
0812665e 2206text $ui_workdir -background white -borderwidth 0 \
c5a1eb88 2207 -width 20 -height 10 \
3c236977 2208 -wrap none \
6c6dd01a 2209 -cursor $cursor_ptr \
3c236977
SP
2210 -xscrollcommand {.vpane.files.workdir.sx set} \
2211 -yscrollcommand {.vpane.files.workdir.sy set} \
cb07fc2a 2212 -state disabled
3c236977
SP
2213scrollbar .vpane.files.workdir.sx -orient h -command [list $ui_workdir xview]
2214scrollbar .vpane.files.workdir.sy -orient v -command [list $ui_workdir yview]
0812665e 2215pack .vpane.files.workdir.title -side top -fill x
3c236977
SP
2216pack .vpane.files.workdir.sx -side bottom -fill x
2217pack .vpane.files.workdir.sy -side right -fill y
0812665e
SP
2218pack $ui_workdir -side left -fill both -expand 1
2219.vpane.files add .vpane.files.workdir -sticky nsew
cb07fc2a 2220
0812665e 2221foreach i [list $ui_index $ui_workdir] {
3849bfba
SP
2222 rmsel_tag $i
2223 $i tag conf in_diff -background [$i tag cget in_sel -background]
24263b77
SP
2224}
2225unset i
131f503b 2226
0fb8f9ce 2227# -- Diff and Commit Area
a49c67d1 2228#
8009dcdc 2229frame .vpane.lower -height 300 -width 400
0fb8f9ce
SP
2230frame .vpane.lower.commarea
2231frame .vpane.lower.diff -relief sunken -borderwidth 1
2232pack .vpane.lower.commarea -side top -fill x
2233pack .vpane.lower.diff -side bottom -fill both -expand 1
0fd49d0a 2234.vpane add .vpane.lower -sticky nsew
cb07fc2a
SP
2235
2236# -- Commit Area Buttons
a49c67d1 2237#
0fb8f9ce
SP
2238frame .vpane.lower.commarea.buttons
2239label .vpane.lower.commarea.buttons.l -text {} \
cb07fc2a 2240 -anchor w \
7416bbc6 2241 -justify left
0fb8f9ce
SP
2242pack .vpane.lower.commarea.buttons.l -side top -fill x
2243pack .vpane.lower.commarea.buttons -side left -fill y
131f503b 2244
1ac17950 2245button .vpane.lower.commarea.buttons.rescan -text [mc Rescan] \
7416bbc6 2246 -command do_rescan
0fb8f9ce 2247pack .vpane.lower.commarea.buttons.rescan -side top -fill x
390adaea
SP
2248lappend disable_on_lock \
2249 {.vpane.lower.commarea.buttons.rescan conf -state}
131f503b 2250
1ac17950 2251button .vpane.lower.commarea.buttons.incall -text [mc "Stage Changed"] \
7416bbc6 2252 -command do_add_all
7fe7e733 2253pack .vpane.lower.commarea.buttons.incall -side top -fill x
390adaea
SP
2254lappend disable_on_lock \
2255 {.vpane.lower.commarea.buttons.incall conf -state}
131f503b 2256
1ac17950 2257button .vpane.lower.commarea.buttons.signoff -text [mc "Sign Off"] \
7416bbc6 2258 -command do_signoff
0fb8f9ce 2259pack .vpane.lower.commarea.buttons.signoff -side top -fill x
131f503b 2260
a9813cb5 2261button .vpane.lower.commarea.buttons.commit -text [mc Commit@@verb] \
7416bbc6 2262 -command do_commit
0fb8f9ce 2263pack .vpane.lower.commarea.buttons.commit -side top -fill x
390adaea
SP
2264lappend disable_on_lock \
2265 {.vpane.lower.commarea.buttons.commit conf -state}
cb07fc2a 2266
1ac17950 2267button .vpane.lower.commarea.buttons.push -text [mc Push] \
87b49a53
SP
2268 -command do_push_anywhere
2269pack .vpane.lower.commarea.buttons.push -side top -fill x
2270
cb07fc2a 2271# -- Commit Message Buffer
a49c67d1 2272#
0fb8f9ce 2273frame .vpane.lower.commarea.buffer
24ac9b75 2274frame .vpane.lower.commarea.buffer.header
0fb8f9ce 2275set ui_comm .vpane.lower.commarea.buffer.t
24ac9b75
SP
2276set ui_coml .vpane.lower.commarea.buffer.header.l
2277radiobutton .vpane.lower.commarea.buffer.header.new \
1ac17950 2278 -text [mc "New Commit"] \
24ac9b75
SP
2279 -command do_select_commit_type \
2280 -variable selected_commit_type \
7416bbc6 2281 -value new
24ac9b75
SP
2282lappend disable_on_lock \
2283 [list .vpane.lower.commarea.buffer.header.new conf -state]
2284radiobutton .vpane.lower.commarea.buffer.header.amend \
1ac17950 2285 -text [mc "Amend Last Commit"] \
24ac9b75
SP
2286 -command do_select_commit_type \
2287 -variable selected_commit_type \
7416bbc6 2288 -value amend
24ac9b75
SP
2289lappend disable_on_lock \
2290 [list .vpane.lower.commarea.buffer.header.amend conf -state]
a49c67d1 2291label $ui_coml \
cb07fc2a 2292 -anchor w \
7416bbc6 2293 -justify left
4539eacd
SP
2294proc trace_commit_type {varname args} {
2295 global ui_coml commit_type
2296 switch -glob -- $commit_type {
1ac17950
CS
2297 initial {set txt [mc "Initial Commit Message:"]}
2298 amend {set txt [mc "Amended Commit Message:"]}
2299 amend-initial {set txt [mc "Amended Initial Commit Message:"]}
2300 amend-merge {set txt [mc "Amended Merge Commit Message:"]}
2301 merge {set txt [mc "Merge Commit Message:"]}
2302 * {set txt [mc "Commit Message:"]}
4539eacd
SP
2303 }
2304 $ui_coml conf -text $txt
2305}
2306trace add variable commit_type write trace_commit_type
24ac9b75
SP
2307pack $ui_coml -side left -fill x
2308pack .vpane.lower.commarea.buffer.header.amend -side right
2309pack .vpane.lower.commarea.buffer.header.new -side right
2310
cb07fc2a 2311text $ui_comm -background white -borderwidth 1 \
9861671d 2312 -undo true \
b2c6fcf1 2313 -maxundo 20 \
9861671d 2314 -autoseparators true \
cb07fc2a 2315 -relief sunken \
0fb8f9ce 2316 -width 75 -height 9 -wrap none \
b4946930 2317 -font font_diff \
6c6dd01a 2318 -yscrollcommand {.vpane.lower.commarea.buffer.sby set}
390adaea
SP
2319scrollbar .vpane.lower.commarea.buffer.sby \
2320 -command [list $ui_comm yview]
24ac9b75 2321pack .vpane.lower.commarea.buffer.header -side top -fill x
0fb8f9ce 2322pack .vpane.lower.commarea.buffer.sby -side right -fill y
cb07fc2a 2323pack $ui_comm -side left -fill y
0fb8f9ce
SP
2324pack .vpane.lower.commarea.buffer -side left -fill y
2325
0e794311
SP
2326# -- Commit Message Buffer Context Menu
2327#
e8ab6446
SP
2328set ctxm .vpane.lower.commarea.buffer.ctxm
2329menu $ctxm -tearoff 0
2330$ctxm add command \
1ac17950 2331 -label [mc Cut] \
e8ab6446
SP
2332 -command {tk_textCut $ui_comm}
2333$ctxm add command \
1ac17950 2334 -label [mc Copy] \
e8ab6446
SP
2335 -command {tk_textCopy $ui_comm}
2336$ctxm add command \
1ac17950 2337 -label [mc Paste] \
e8ab6446
SP
2338 -command {tk_textPaste $ui_comm}
2339$ctxm add command \
1ac17950 2340 -label [mc Delete] \
e8ab6446
SP
2341 -command {$ui_comm delete sel.first sel.last}
2342$ctxm add separator
2343$ctxm add command \
1ac17950 2344 -label [mc "Select All"] \
75e78c8a 2345 -command {focus $ui_comm;$ui_comm tag add sel 0.0 end}
e8ab6446 2346$ctxm add command \
1ac17950 2347 -label [mc "Copy All"] \
e8ab6446 2348 -command {
0e794311
SP
2349 $ui_comm tag add sel 0.0 end
2350 tk_textCopy $ui_comm
2351 $ui_comm tag remove sel 0.0 end
e8ab6446
SP
2352 }
2353$ctxm add separator
2354$ctxm add command \
1ac17950 2355 -label [mc "Sign Off"] \
0e794311 2356 -command do_signoff
e8ab6446 2357bind_button3 $ui_comm "tk_popup $ctxm %X %Y"
0e794311 2358
0fb8f9ce 2359# -- Diff Header
a49c67d1 2360#
20a53c02
SP
2361proc trace_current_diff_path {varname args} {
2362 global current_diff_path diff_actions file_states
2363 if {$current_diff_path eq {}} {
e8ab6446
SP
2364 set s {}
2365 set f {}
2366 set p {}
2367 set o disabled
2368 } else {
20a53c02 2369 set p $current_diff_path
e8ab6446 2370 set s [mapdesc [lindex $file_states($p) 0] $p]
1ac17950 2371 set f [mc "File:"]
e8ab6446
SP
2372 set p [escape_path $p]
2373 set o normal
2374 }
2375
2376 .vpane.lower.diff.header.status configure -text $s
2377 .vpane.lower.diff.header.file configure -text $f
2378 .vpane.lower.diff.header.path configure -text $p
2379 foreach w $diff_actions {
2380 uplevel #0 $w $o
2381 }
2382}
20a53c02 2383trace add variable current_diff_path write trace_current_diff_path
e8ab6446 2384
9adccb05 2385frame .vpane.lower.diff.header -background gold
e8ab6446 2386label .vpane.lower.diff.header.status \
9adccb05 2387 -background gold \
3e7b0e1d
SP
2388 -width $max_status_desc \
2389 -anchor w \
7416bbc6 2390 -justify left
e8ab6446 2391label .vpane.lower.diff.header.file \
9adccb05 2392 -background gold \
e8ab6446 2393 -anchor w \
7416bbc6 2394 -justify left
e8ab6446 2395label .vpane.lower.diff.header.path \
9adccb05 2396 -background gold \
fce89e46 2397 -anchor w \
7416bbc6 2398 -justify left
e8ab6446
SP
2399pack .vpane.lower.diff.header.status -side left
2400pack .vpane.lower.diff.header.file -side left
2401pack .vpane.lower.diff.header.path -fill x
2402set ctxm .vpane.lower.diff.header.ctxm
2403menu $ctxm -tearoff 0
2404$ctxm add command \
1ac17950 2405 -label [mc Copy] \
fce89e46
SP
2406 -command {
2407 clipboard clear
2408 clipboard append \
2409 -format STRING \
2410 -type STRING \
20a53c02 2411 -- $current_diff_path
fce89e46 2412 }
e8ab6446
SP
2413lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
2414bind_button3 .vpane.lower.diff.header.path "tk_popup $ctxm %X %Y"
0fb8f9ce
SP
2415
2416# -- Diff Body
a49c67d1 2417#
0fb8f9ce
SP
2418frame .vpane.lower.diff.body
2419set ui_diff .vpane.lower.diff.body.t
2420text $ui_diff -background white -borderwidth 0 \
2421 -width 80 -height 15 -wrap none \
b4946930 2422 -font font_diff \
0fb8f9ce
SP
2423 -xscrollcommand {.vpane.lower.diff.body.sbx set} \
2424 -yscrollcommand {.vpane.lower.diff.body.sby set} \
0fb8f9ce
SP
2425 -state disabled
2426scrollbar .vpane.lower.diff.body.sbx -orient horizontal \
2427 -command [list $ui_diff xview]
2428scrollbar .vpane.lower.diff.body.sby -orient vertical \
2429 -command [list $ui_diff yview]
2430pack .vpane.lower.diff.body.sbx -side bottom -fill x
2431pack .vpane.lower.diff.body.sby -side right -fill y
2432pack $ui_diff -side left -fill both -expand 1
2433pack .vpane.lower.diff.header -side top -fill x
2434pack .vpane.lower.diff.body -side bottom -fill both -expand 1
2435
30b14ed3 2436$ui_diff tag conf d_cr -elide true
ca521566
SP
2437$ui_diff tag conf d_@ -foreground blue -font font_diffbold
2438$ui_diff tag conf d_+ -foreground {#00a000}
fec4a785
SP
2439$ui_diff tag conf d_- -foreground red
2440
ca521566 2441$ui_diff tag conf d_++ -foreground {#00a000}
fec4a785
SP
2442$ui_diff tag conf d_-- -foreground red
2443$ui_diff tag conf d_+s \
ca521566
SP
2444 -foreground {#00a000} \
2445 -background {#e2effa}
fec4a785
SP
2446$ui_diff tag conf d_-s \
2447 -foreground red \
ca521566 2448 -background {#e2effa}
fec4a785 2449$ui_diff tag conf d_s+ \
ca521566
SP
2450 -foreground {#00a000} \
2451 -background ivory1
fec4a785
SP
2452$ui_diff tag conf d_s- \
2453 -foreground red \
ca521566 2454 -background ivory1
fec4a785
SP
2455
2456$ui_diff tag conf d<<<<<<< \
2457 -foreground orange \
2458 -font font_diffbold
2459$ui_diff tag conf d======= \
2460 -foreground orange \
2461 -font font_diffbold
2462$ui_diff tag conf d>>>>>>> \
2463 -foreground orange \
2464 -font font_diffbold
cb07fc2a 2465
ca521566
SP
2466$ui_diff tag raise sel
2467
0e794311
SP
2468# -- Diff Body Context Menu
2469#
e8ab6446
SP
2470set ctxm .vpane.lower.diff.body.ctxm
2471menu $ctxm -tearoff 0
68c30b4a 2472$ctxm add command \
1ac17950 2473 -label [mc Refresh] \
68c30b4a 2474 -command reshow_diff
86773d9b 2475lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
e8ab6446 2476$ctxm add command \
1ac17950 2477 -label [mc Copy] \
e8ab6446
SP
2478 -command {tk_textCopy $ui_diff}
2479lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
2480$ctxm add command \
1ac17950 2481 -label [mc "Select All"] \
75e78c8a 2482 -command {focus $ui_diff;$ui_diff tag add sel 0.0 end}
e8ab6446
SP
2483lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
2484$ctxm add command \
1ac17950 2485 -label [mc "Copy All"] \
e8ab6446 2486 -command {
0e794311
SP
2487 $ui_diff tag add sel 0.0 end
2488 tk_textCopy $ui_diff
2489 $ui_diff tag remove sel 0.0 end
e8ab6446
SP
2490 }
2491lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
2492$ctxm add separator
a25c5189 2493$ctxm add command \
1ac17950 2494 -label [mc "Apply/Reverse Hunk"] \
a25c5189
SP
2495 -command {apply_hunk $cursorX $cursorY}
2496set ui_diff_applyhunk [$ctxm index last]
2497lappend diff_actions [list $ctxm entryconf $ui_diff_applyhunk -state]
2498$ctxm add separator
e8ab6446 2499$ctxm add command \
1ac17950 2500 -label [mc "Decrease Font Size"] \
b4946930 2501 -command {incr_font_size font_diff -1}
e8ab6446
SP
2502lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
2503$ctxm add command \
1ac17950 2504 -label [mc "Increase Font Size"] \
b4946930 2505 -command {incr_font_size font_diff 1}
e8ab6446
SP
2506lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
2507$ctxm add separator
2508$ctxm add command \
1ac17950 2509 -label [mc "Show Less Context"] \
b8848f77 2510 -command {if {$repo_config(gui.diffcontext) >= 1} {
358d8de8
SP
2511 incr repo_config(gui.diffcontext) -1
2512 reshow_diff
2513 }}
e8ab6446
SP
2514lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
2515$ctxm add command \
1ac17950 2516 -label [mc "Show More Context"] \
b8848f77 2517 -command {if {$repo_config(gui.diffcontext) < 99} {
358d8de8
SP
2518 incr repo_config(gui.diffcontext)
2519 reshow_diff
b8848f77 2520 }}
e8ab6446
SP
2521lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
2522$ctxm add separator
1ac17950 2523$ctxm add command -label [mc "Options..."] \
8009dcdc 2524 -command do_options
83751fc1 2525proc popup_diff_menu {ctxm x y X Y} {
ce015c21 2526 global current_diff_path file_states
83751fc1
SP
2527 set ::cursorX $x
2528 set ::cursorY $y
2529 if {$::ui_index eq $::current_diff_side} {
1ac17950 2530 set l [mc "Unstage Hunk From Commit"]
a25c5189 2531 } else {
1ac17950 2532 set l [mc "Stage Hunk For Commit"]
a25c5189 2533 }
047d94d5
SP
2534 if {$::is_3way_diff
2535 || $current_diff_path eq {}
2536 || ![info exists file_states($current_diff_path)]
2537 || {_O} eq [lindex $file_states($current_diff_path) 0]} {
9c9f5fa9 2538 set s disabled
047d94d5
SP
2539 } else {
2540 set s normal
9c9f5fa9 2541 }
9f4119eb 2542 $ctxm entryconf $::ui_diff_applyhunk -state $s -label $l
83751fc1
SP
2543 tk_popup $ctxm $X $Y
2544}
2545bind_button3 $ui_diff [list popup_diff_menu $ctxm %x %y %X %Y]
0e794311 2546
cb07fc2a 2547# -- Status Bar
e8ab6446 2548#
51530d17 2549set main_status [::status_bar::new .status]
cb07fc2a 2550pack .status -anchor w -side bottom -fill x
1ac17950 2551$main_status show [mc "Initializing..."]
cb07fc2a 2552
2d19516d 2553# -- Load geometry
e8ab6446 2554#
2d19516d 2555catch {
51f4d16b 2556set gm $repo_config(gui.geometry)
c4fe7728
SP
2557wm geometry . [lindex $gm 0]
2558.vpane sash place 0 \
2559 [lindex [.vpane sash coord 0] 0] \
2560 [lindex $gm 1]
2561.vpane.files sash place 0 \
2562 [lindex $gm 2] \
2563 [lindex [.vpane.files sash coord 0] 1]
c4fe7728 2564unset gm
390adaea 2565}
2d19516d 2566
cb07fc2a 2567# -- Key Bindings
e8ab6446 2568#
ec6b424a 2569bind $ui_comm <$M1B-Key-Return> {do_commit;break}
93e912c5
SP
2570bind $ui_comm <$M1B-Key-i> {do_add_all;break}
2571bind $ui_comm <$M1B-Key-I> {do_add_all;break}
9861671d
SP
2572bind $ui_comm <$M1B-Key-x> {tk_textCut %W;break}
2573bind $ui_comm <$M1B-Key-X> {tk_textCut %W;break}
2574bind $ui_comm <$M1B-Key-c> {tk_textCopy %W;break}
2575bind $ui_comm <$M1B-Key-C> {tk_textCopy %W;break}
2576bind $ui_comm <$M1B-Key-v> {tk_textPaste %W; %W see insert; break}
2577bind $ui_comm <$M1B-Key-V> {tk_textPaste %W; %W see insert; break}
2578bind $ui_comm <$M1B-Key-a> {%W tag add sel 0.0 end;break}
2579bind $ui_comm <$M1B-Key-A> {%W tag add sel 0.0 end;break}
2580
2581bind $ui_diff <$M1B-Key-x> {tk_textCopy %W;break}
2582bind $ui_diff <$M1B-Key-X> {tk_textCopy %W;break}
2583bind $ui_diff <$M1B-Key-c> {tk_textCopy %W;break}
2584bind $ui_diff <$M1B-Key-C> {tk_textCopy %W;break}
2585bind $ui_diff <$M1B-Key-v> {break}
2586bind $ui_diff <$M1B-Key-V> {break}
2587bind $ui_diff <$M1B-Key-a> {%W tag add sel 0.0 end;break}
2588bind $ui_diff <$M1B-Key-A> {%W tag add sel 0.0 end;break}
b2c6fcf1
SP
2589bind $ui_diff <Key-Up> {catch {%W yview scroll -1 units};break}
2590bind $ui_diff <Key-Down> {catch {%W yview scroll 1 units};break}
2591bind $ui_diff <Key-Left> {catch {%W xview scroll -1 units};break}
2592bind $ui_diff <Key-Right> {catch {%W xview scroll 1 units};break}
60aa065f
SP
2593bind $ui_diff <Key-k> {catch {%W yview scroll -1 units};break}
2594bind $ui_diff <Key-j> {catch {%W yview scroll 1 units};break}
2595bind $ui_diff <Key-h> {catch {%W xview scroll -1 units};break}
2596bind $ui_diff <Key-l> {catch {%W xview scroll 1 units};break}
2597bind $ui_diff <Control-Key-b> {catch {%W yview scroll -1 pages};break}
2598bind $ui_diff <Control-Key-f> {catch {%W yview scroll 1 pages};break}
23effa79 2599bind $ui_diff <Button-1> {focus %W}
49b86f01 2600
64a906f8 2601if {[is_enabled branch]} {
b1fa2bff
SP
2602 bind . <$M1B-Key-n> branch_create::dialog
2603 bind . <$M1B-Key-N> branch_create::dialog
d41b43eb
SP
2604 bind . <$M1B-Key-o> branch_checkout::dialog
2605 bind . <$M1B-Key-O> branch_checkout::dialog
a870ddc0
SP
2606 bind . <$M1B-Key-m> merge::dialog
2607 bind . <$M1B-Key-M> merge::dialog
bd29ebc3 2608}
840bcfa7
SP
2609if {[is_enabled transport]} {
2610 bind . <$M1B-Key-p> do_push_anywhere
2611 bind . <$M1B-Key-P> do_push_anywhere
2612}
bd29ebc3 2613
f1e031bb
SP
2614bind . <Key-F5> do_rescan
2615bind . <$M1B-Key-r> do_rescan
2616bind . <$M1B-Key-R> do_rescan
07123f40
SP
2617bind . <$M1B-Key-s> do_signoff
2618bind . <$M1B-Key-S> do_signoff
93e912c5
SP
2619bind . <$M1B-Key-i> do_add_all
2620bind . <$M1B-Key-I> do_add_all
07123f40 2621bind . <$M1B-Key-Return> do_commit
0812665e 2622foreach i [list $ui_index $ui_workdir] {
24263b77
SP
2623 bind $i <Button-1> "toggle_or_diff $i %x %y; break"
2624 bind $i <$M1B-Button-1> "add_one_to_selection $i %x %y; break"
2625 bind $i <Shift-Button-1> "add_range_to_selection $i %x %y; break"
cb07fc2a 2626}
62aac80b
SP
2627unset i
2628
2629set file_lists($ui_index) [list]
0812665e 2630set file_lists($ui_workdir) [list]
a49c67d1 2631
19c82148 2632wm title . "[appname] ([reponame]) [file normalize [file dirname [gitdir]]]"
cb07fc2a 2633focus -force $ui_comm
1d8b3cbf 2634
85ab313e
SP
2635# -- Warn the user about environmental problems. Cygwin's Tcl
2636# does *not* pass its env array onto any processes it spawns.
2637# This means that git processes get none of our environment.
1d8b3cbf 2638#
20ddfcaa 2639if {[is_Cygwin]} {
1d8b3cbf
SP
2640 set ignored_env 0
2641 set suggest_user {}
c8c4854b 2642 set msg [mc "Possible environment issues exist.
1d8b3cbf
SP
2643
2644The following environment variables are probably
2645going to be ignored by any Git subprocess run
c8c4854b 2646by %s:
1d8b3cbf 2647
c8c4854b 2648" [appname]]
1d8b3cbf
SP
2649 foreach name [array names env] {
2650 switch -regexp -- $name {
2651 {^GIT_INDEX_FILE$} -
2652 {^GIT_OBJECT_DIRECTORY$} -
2653 {^GIT_ALTERNATE_OBJECT_DIRECTORIES$} -
2654 {^GIT_DIFF_OPTS$} -
2655 {^GIT_EXTERNAL_DIFF$} -
2656 {^GIT_PAGER$} -
2657 {^GIT_TRACE$} -
2658 {^GIT_CONFIG$} -
2659 {^GIT_CONFIG_LOCAL$} -
2660 {^GIT_(AUTHOR|COMMITTER)_DATE$} {
2661 append msg " - $name\n"
2662 incr ignored_env
2663 }
2664 {^GIT_(AUTHOR|COMMITTER)_(NAME|EMAIL)$} {
2665 append msg " - $name\n"
2666 incr ignored_env
2667 set suggest_user $name
2668 }
2669 }
2670 }
2671 if {$ignored_env > 0} {
c8c4854b 2672 append msg [mc "
1d8b3cbf 2673This is due to a known issue with the
c8c4854b 2674Tcl binary distributed by Cygwin."]
1d8b3cbf
SP
2675
2676 if {$suggest_user ne {}} {
c8c4854b 2677 append msg [mc "
1d8b3cbf 2678
c8c4854b 2679A good replacement for %s
1d8b3cbf
SP
2680is placing values for the user.name and
2681user.email settings into your personal
2682~/.gitconfig file.
c8c4854b 2683" $suggest_user]
1d8b3cbf
SP
2684 }
2685 warn_popup $msg
2686 }
2687 unset ignored_env msg suggest_user name
2688}
2689
85ab313e
SP
2690# -- Only initialize complex UI if we are going to stay running.
2691#
64a906f8 2692if {[is_enabled transport]} {
4ccdab02 2693 load_all_remotes
85ab313e 2694
3f7fd924
SP
2695 populate_fetch_menu
2696 populate_push_menu
4ccdab02 2697}
85ab313e 2698
4578c5cb
SP
2699if {[winfo exists $ui_comm]} {
2700 set GITGUI_BCK_exists [load_message GITGUI_BCK]
2701
2702 # -- If both our backup and message files exist use the
2703 # newer of the two files to initialize the buffer.
2704 #
2705 if {$GITGUI_BCK_exists} {
2706 set m [gitdir GITGUI_MSG]
2707 if {[file isfile $m]} {
2708 if {[file mtime [gitdir GITGUI_BCK]] > [file mtime $m]} {
2709 catch {file delete [gitdir GITGUI_MSG]}
2710 } else {
2711 $ui_comm delete 0.0 end
2712 $ui_comm edit reset
2713 $ui_comm edit modified false
2714 catch {file delete [gitdir GITGUI_BCK]}
2715 set GITGUI_BCK_exists 0
2716 }
2717 }
2718 unset m
2719 }
2720
2721 proc backup_commit_buffer {} {
2722 global ui_comm GITGUI_BCK_exists
2723
2724 set m [$ui_comm edit modified]
2725 if {$m || $GITGUI_BCK_exists} {
2726 set msg [string trim [$ui_comm get 0.0 end]]
2727 regsub -all -line {[ \r\t]+$} $msg {} msg
2728
2729 if {$msg eq {}} {
2730 if {$GITGUI_BCK_exists} {
2731 catch {file delete [gitdir GITGUI_BCK]}
2732 set GITGUI_BCK_exists 0
2733 }
2734 } elseif {$m} {
2735 catch {
2736 set fd [open [gitdir GITGUI_BCK] w]
2737 puts -nonewline $fd $msg
2738 close $fd
2739 set GITGUI_BCK_exists 1
2740 }
2741 }
2742
2743 $ui_comm edit modified false
2744 }
2745
2746 set ::GITGUI_BCK_i [after 2000 backup_commit_buffer]
2747 }
2748
2749 backup_commit_buffer
2750}
2751
53716a7b 2752lock_index begin-read
301dfaa9
SP
2753if {![winfo ismapped .]} {
2754 wm deiconify .
2755}
8f52548a 2756after 1 do_rescan
3972b987
SP
2757if {[is_enabled multicommit]} {
2758 after 1000 hint_gc
2759}