git-gui: Implemented amended commits.
[git/dkf.git] / git-gui
blobbb89da43320ed53b819fa9f973c6cdaaf8783128
1 #!/bin/sh
2 # Tcl ignores the next line -*- tcl -*- \
3 exec wish "$0" -- "$@"
5 # Copyright (C) 2006 Shawn Pearce, Paul Mackerras. All rights reserved.
6 # This program is free software; it may be used, copied, modified
7 # and distributed under the terms of the GNU General Public Licence,
8 # either version 2, or (at your option) any later version.
10 ######################################################################
12 ## task management
14 set single_commit 0
15 set status_active 0
16 set diff_active 0
17 set checkin_active 0
18 set commit_active 0
19 set update_index_fd {}
21 set disable_on_lock [list]
22 set index_lock_type none
24 set HEAD {}
25 set PARENT {}
26 set commit_type {}
28 proc lock_index {type} {
29 global index_lock_type disable_on_lock
31 if {$index_lock_type == {none}} {
32 set index_lock_type $type
33 foreach w $disable_on_lock {
34 uplevel #0 $w disabled
36 return 1
37 } elseif {$index_lock_type == {begin-update} && $type == {update}} {
38 set index_lock_type $type
39 return 1
41 return 0
44 proc unlock_index {} {
45 global index_lock_type disable_on_lock
47 set index_lock_type none
48 foreach w $disable_on_lock {
49 uplevel #0 $w normal
53 ######################################################################
55 ## status
57 proc repository_state {hdvar ctvar} {
58 global gitdir
59 upvar $hdvar hd $ctvar ct
61 if {[catch {set hd [exec git rev-parse --verify HEAD]}]} {
62 set ct initial
63 } elseif {[file exists [file join $gitdir MERGE_HEAD]]} {
64 set ct merge
65 } else {
66 set ct normal
70 proc update_status {{final Ready.}} {
71 global HEAD PARENT commit_type
72 global ui_index ui_other ui_status_value ui_comm
73 global status_active file_states
75 if {$status_active || ![lock_index read]} return
77 repository_state new_HEAD new_type
78 if {$commit_type == {amend}
79 && $new_type == {normal}
80 && $new_HEAD == $HEAD} {
81 } else {
82 set HEAD $new_HEAD
83 set PARENT $new_HEAD
84 set commit_type $new_type
87 array unset file_states
88 foreach w [list $ui_index $ui_other] {
89 $w conf -state normal
90 $w delete 0.0 end
91 $w conf -state disabled
94 if {![$ui_comm edit modified]
95 || [string trim [$ui_comm get 0.0 end]] == {}} {
96 if {[load_message GITGUI_MSG]} {
97 } elseif {[load_message MERGE_MSG]} {
98 } elseif {[load_message SQUASH_MSG]} {
100 $ui_comm edit modified false
103 set status_active 1
104 set ui_status_value {Refreshing file status...}
105 set fd_rf [open "| git update-index -q --unmerged --refresh" r]
106 fconfigure $fd_rf -blocking 0 -translation binary
107 fileevent $fd_rf readable [list read_refresh $fd_rf $final]
110 proc read_refresh {fd final} {
111 global gitdir PARENT commit_type
112 global ui_index ui_other ui_status_value ui_comm
113 global status_active file_states
115 read $fd
116 if {![eof $fd]} return
117 close $fd
119 set ls_others [list | git ls-files --others -z \
120 --exclude-per-directory=.gitignore]
121 set info_exclude [file join $gitdir info exclude]
122 if {[file readable $info_exclude]} {
123 lappend ls_others "--exclude-from=$info_exclude"
126 set status_active 3
127 set ui_status_value {Scanning for modified files ...}
128 set fd_di [open "| git diff-index --cached -z $PARENT" r]
129 set fd_df [open "| git diff-files -z" r]
130 set fd_lo [open $ls_others r]
132 fconfigure $fd_di -blocking 0 -translation binary
133 fconfigure $fd_df -blocking 0 -translation binary
134 fconfigure $fd_lo -blocking 0 -translation binary
135 fileevent $fd_di readable [list read_diff_index $fd_di $final]
136 fileevent $fd_df readable [list read_diff_files $fd_df $final]
137 fileevent $fd_lo readable [list read_ls_others $fd_lo $final]
140 proc load_message {file} {
141 global gitdir ui_comm
143 set f [file join $gitdir $file]
144 if {[file isfile $f]} {
145 if {[catch {set fd [open $f r]}]} {
146 return 0
148 set content [string trim [read $fd]]
149 close $fd
150 $ui_comm delete 0.0 end
151 $ui_comm insert end $content
152 return 1
154 return 0
157 proc read_diff_index {fd final} {
158 global buf_rdi
160 append buf_rdi [read $fd]
161 set pck [split $buf_rdi "\0"]
162 set buf_rdi [lindex $pck end]
163 foreach {m p} [lrange $pck 0 end-1] {
164 if {$m != {} && $p != {}} {
165 display_file $p [string index $m end]_
168 status_eof $fd buf_rdi $final
171 proc read_diff_files {fd final} {
172 global buf_rdf
174 append buf_rdf [read $fd]
175 set pck [split $buf_rdf "\0"]
176 set buf_rdf [lindex $pck end]
177 foreach {m p} [lrange $pck 0 end-1] {
178 if {$m != {} && $p != {}} {
179 display_file $p _[string index $m end]
182 status_eof $fd buf_rdf $final
185 proc read_ls_others {fd final} {
186 global buf_rlo
188 append buf_rlo [read $fd]
189 set pck [split $buf_rlo "\0"]
190 set buf_rlo [lindex $pck end]
191 foreach p [lrange $pck 0 end-1] {
192 display_file $p _O
194 status_eof $fd buf_rlo $final
197 proc status_eof {fd buf final} {
198 global status_active $buf
199 global ui_fname_value ui_status_value file_states
201 if {[eof $fd]} {
202 set $buf {}
203 close $fd
204 if {[incr status_active -1] == 0} {
205 unlock_index
207 set ui_status_value $final
208 if {$ui_fname_value != {} && [array names file_states \
209 -exact $ui_fname_value] != {}} {
210 show_diff $ui_fname_value
211 } else {
212 clear_diff
218 ######################################################################
220 ## diff
222 proc clear_diff {} {
223 global ui_diff ui_fname_value ui_fstatus_value
225 $ui_diff conf -state normal
226 $ui_diff delete 0.0 end
227 $ui_diff conf -state disabled
228 set ui_fname_value {}
229 set ui_fstatus_value {}
232 proc show_diff {path} {
233 global file_states PARENT diff_3way diff_active
234 global ui_diff ui_fname_value ui_fstatus_value ui_status_value
236 if {$diff_active || ![lock_index read]} return
238 clear_diff
239 set s $file_states($path)
240 set m [lindex $s 0]
241 set diff_3way 0
242 set diff_active 1
243 set ui_fname_value $path
244 set ui_fstatus_value [mapdesc $m $path]
245 set ui_status_value "Loading diff of $path..."
247 set cmd [list | git diff-index -p $PARENT -- $path]
248 switch $m {
249 AM {
251 MM {
252 set cmd [list | git diff-index -p -c $PARENT $path]
254 _O {
255 if {[catch {
256 set fd [open $path r]
257 set content [read $fd]
258 close $fd
259 } err ]} {
260 set diff_active 0
261 unlock_index
262 set ui_status_value "Unable to display $path"
263 error_popup "Error loading file:\n$err"
264 return
266 $ui_diff conf -state normal
267 $ui_diff insert end $content
268 $ui_diff conf -state disabled
269 return
273 if {[catch {set fd [open $cmd r]} err]} {
274 set diff_active 0
275 unlock_index
276 set ui_status_value "Unable to display $path"
277 error_popup "Error loading diff:\n$err"
278 return
281 fconfigure $fd -blocking 0 -translation auto
282 fileevent $fd readable [list read_diff $fd]
285 proc read_diff {fd} {
286 global ui_diff ui_status_value diff_3way diff_active
288 while {[gets $fd line] >= 0} {
289 if {[string match {diff --git *} $line]} continue
290 if {[string match {diff --combined *} $line]} continue
291 if {[string match {--- *} $line]} continue
292 if {[string match {+++ *} $line]} continue
293 if {[string match index* $line]} {
294 if {[string first , $line] >= 0} {
295 set diff_3way 1
299 $ui_diff conf -state normal
300 if {!$diff_3way} {
301 set x [string index $line 0]
302 switch -- $x {
303 "@" {set tags da}
304 "+" {set tags dp}
305 "-" {set tags dm}
306 default {set tags {}}
308 } else {
309 set x [string range $line 0 1]
310 switch -- $x {
311 default {set tags {}}
312 "@@" {set tags da}
313 "++" {set tags dp; set x " +"}
314 " +" {set tags {di bold}; set x "++"}
315 "+ " {set tags dni; set x "-+"}
316 "--" {set tags dm; set x " -"}
317 " -" {set tags {dm bold}; set x "--"}
318 "- " {set tags di; set x "+-"}
319 default {set tags {}}
321 set line [string replace $line 0 1 $x]
323 $ui_diff insert end $line $tags
324 $ui_diff insert end "\n"
325 $ui_diff conf -state disabled
328 if {[eof $fd]} {
329 close $fd
330 set diff_active 0
331 unlock_index
332 set ui_status_value {Ready.}
336 ######################################################################
338 ## commit
340 proc load_last_commit {} {
341 global HEAD PARENT commit_type ui_comm
343 if {$commit_type == {amend}} return
344 if {$commit_type != {normal}} {
345 error_popup "Can't amend a $commit_type commit."
346 return
349 set msg {}
350 set parent {}
351 set parent_count 0
352 if {[catch {
353 set fd [open "| git cat-file commit $HEAD" r]
354 while {[gets $fd line] > 0} {
355 if {[string match {parent *} $line]} {
356 set parent [string range $line 7 end]
357 incr parent_count
360 set msg [string trim [read $fd]]
361 close $fd
362 } err]} {
363 error_popup "Error loading commit data for amend:\n$err"
364 return
367 if {$parent_count == 0} {
368 set commit_type amend
369 set HEAD {}
370 set PARENT {}
371 update_status
372 } elseif {$parent_count == 1} {
373 set commit_type amend
374 set PARENT $parent
375 $ui_comm delete 0.0 end
376 $ui_comm insert end $msg
377 $ui_comm edit modified false
378 update_status
379 } else {
380 error_popup {You can't amend a merge commit.}
381 return
385 proc commit_tree {} {
386 global tcl_platform HEAD gitdir commit_type file_states
387 global commit_active ui_status_value
388 global ui_comm
390 if {$commit_active || ![lock_index update]} return
392 # -- Our in memory state should match the repository.
394 repository_state curHEAD cur_type
395 if {$commit_type == {amend}
396 && $cur_type == {normal}
397 && $curHEAD == $HEAD} {
398 } elseif {$commit_type != $cur_type || $HEAD != $curHEAD} {
399 error_popup {Last scanned state does not match repository state.
401 Its highly likely that another Git program modified the
402 repository since our last scan. A rescan is required
403 before committing.
405 unlock_index
406 update_status
407 return
410 # -- At least one file should differ in the index.
412 set files_ready 0
413 foreach path [array names file_states] {
414 set s $file_states($path)
415 switch -glob -- [lindex $s 0] {
416 _* {continue}
417 A* -
418 D* -
419 M* {set files_ready 1; break}
420 U* {
421 error_popup "Unmerged files cannot be committed.
423 File $path has merge conflicts.
424 You must resolve them and check the file in before committing.
426 unlock_index
427 return
429 default {
430 error_popup "Unknown file state [lindex $s 0] detected.
432 File $path cannot be committed by this program.
437 if {!$files_ready} {
438 error_popup {No checked-in files to commit.
440 You must check-in at least 1 file before you can commit.
442 unlock_index
443 return
446 # -- A message is required.
448 set msg [string trim [$ui_comm get 1.0 end]]
449 if {$msg == {}} {
450 error_popup {Please supply a commit message.
452 A good commit message has the following format:
454 - First line: Describe in one sentance what you did.
455 - Second line: Blank
456 - Remaining lines: Describe why this change is good.
458 unlock_index
459 return
462 # -- Ask the pre-commit hook for the go-ahead.
464 set pchook [file join $gitdir hooks pre-commit]
465 if {$tcl_platform(platform) == {windows} && [file isfile $pchook]} {
466 set pchook [list sh -c \
467 "if test -x \"$pchook\"; then exec \"$pchook\"; fi"]
468 } elseif {[file executable $pchook]} {
469 set pchook [list $pchook]
470 } else {
471 set pchook {}
473 if {$pchook != {} && [catch {eval exec $pchook} err]} {
474 hook_failed_popup pre-commit $err
475 unlock_index
476 return
479 # -- Write the tree in the background.
481 set commit_active 1
482 set ui_status_value {Committing changes...}
484 set fd_wt [open "| git write-tree" r]
485 fileevent $fd_wt readable \
486 [list commit_stage2 $fd_wt $curHEAD $msg]
489 proc commit_stage2 {fd_wt curHEAD msg} {
490 global single_commit gitdir PARENT commit_type
491 global commit_active ui_status_value ui_comm
493 gets $fd_wt tree_id
494 close $fd_wt
496 if {$tree_id == {}} {
497 error_popup "write-tree failed"
498 set commit_active 0
499 set ui_status_value {Commit failed.}
500 unlock_index
501 return
504 # -- Create the commit.
506 set cmd [list git commit-tree $tree_id]
507 if {$PARENT != {}} {
508 lappend cmd -p $PARENT
510 if {$commit_type == {merge}} {
511 if {[catch {
512 set fd_mh [open [file join $gitdir MERGE_HEAD] r]
513 while {[gets $fd_mh merge_head] > 0} {
514 lappend -p $merge_head
516 close $fd_mh
517 } err]} {
518 error_popup "Loading MERGE_HEADs failed:\n$err"
519 set commit_active 0
520 set ui_status_value {Commit failed.}
521 unlock_index
522 return
525 if {$PARENT == {}} {
526 # git commit-tree writes to stderr during initial commit.
527 lappend cmd 2>/dev/null
529 lappend cmd << $msg
530 if {[catch {set cmt_id [eval exec $cmd]} err]} {
531 error_popup "commit-tree failed:\n$err"
532 set commit_active 0
533 set ui_status_value {Commit failed.}
534 unlock_index
535 return
538 # -- Update the HEAD ref.
540 set reflogm commit
541 if {$commit_type != {normal}} {
542 append reflogm " ($commit_type)"
544 set i [string first "\n" $msg]
545 if {$i >= 0} {
546 append reflogm {: } [string range $msg 0 [expr $i - 1]]
547 } else {
548 append reflogm {: } $msg
550 set cmd [list git update-ref -m $reflogm HEAD $cmt_id $curHEAD]
551 if {[catch {eval exec $cmd} err]} {
552 error_popup "update-ref failed:\n$err"
553 set commit_active 0
554 set ui_status_value {Commit failed.}
555 unlock_index
556 return
559 # -- Cleanup after ourselves.
561 catch {file delete [file join $gitdir MERGE_HEAD]}
562 catch {file delete [file join $gitdir MERGE_MSG]}
563 catch {file delete [file join $gitdir SQUASH_MSG]}
564 catch {file delete [file join $gitdir GITGUI_MSG]}
566 # -- Let rerere do its thing.
568 if {[file isdirectory [file join $gitdir rr-cache]]} {
569 catch {exec git rerere}
572 $ui_comm delete 0.0 end
573 $ui_comm edit modified false
575 if {$single_commit} do_quit
577 set commit_type {}
578 set commit_active 0
579 unlock_index
580 update_status "Changes committed as $cmt_id."
583 ######################################################################
585 ## ui helpers
587 proc mapcol {state path} {
588 global all_cols
590 if {[catch {set r $all_cols($state)}]} {
591 puts "error: no column for state={$state} $path"
592 return o
594 return $r
597 proc mapicon {state path} {
598 global all_icons
600 if {[catch {set r $all_icons($state)}]} {
601 puts "error: no icon for state={$state} $path"
602 return file_plain
604 return $r
607 proc mapdesc {state path} {
608 global all_descs
610 if {[catch {set r $all_descs($state)}]} {
611 puts "error: no desc for state={$state} $path"
612 return $state
614 return $r
617 proc bsearch {w path} {
618 set hi [expr [lindex [split [$w index end] .] 0] - 2]
619 if {$hi == 0} {
620 return -1
622 set lo 0
623 while {$lo < $hi} {
624 set mi [expr [expr $lo + $hi] / 2]
625 set ti [expr $mi + 1]
626 set cmp [string compare [$w get $ti.1 $ti.end] $path]
627 if {$cmp < 0} {
628 set lo $ti
629 } elseif {$cmp == 0} {
630 return $mi
631 } else {
632 set hi $mi
635 return -[expr $lo + 1]
638 proc merge_state {path state} {
639 global file_states
641 if {[array names file_states -exact $path] == {}} {
642 set o __
643 set s [list $o none none]
644 } else {
645 set s $file_states($path)
646 set o [lindex $s 0]
649 set m [lindex $s 0]
650 if {[string index $state 0] == "_"} {
651 set state [string index $m 0][string index $state 1]
652 } elseif {[string index $state 0] == "*"} {
653 set state _[string index $state 1]
656 if {[string index $state 1] == "_"} {
657 set state [string index $state 0][string index $m 1]
658 } elseif {[string index $state 1] == "*"} {
659 set state [string index $state 0]_
662 set file_states($path) [lreplace $s 0 0 $state]
663 return $o
666 proc display_file {path state} {
667 global ui_index ui_other file_states
669 set old_m [merge_state $path $state]
670 set s $file_states($path)
671 set m [lindex $s 0]
673 if {[mapcol $m $path] == "o"} {
674 set ii 1
675 set ai 2
676 set iw $ui_index
677 set aw $ui_other
678 } else {
679 set ii 2
680 set ai 1
681 set iw $ui_other
682 set aw $ui_index
685 set d [lindex $s $ii]
686 if {$d != "none"} {
687 set lno [bsearch $iw $path]
688 if {$lno >= 0} {
689 incr lno
690 $iw conf -state normal
691 $iw delete $lno.0 [expr $lno + 1].0
692 $iw conf -state disabled
693 set s [lreplace $s $ii $ii none]
697 set d [lindex $s $ai]
698 if {$d == "none"} {
699 set lno [expr abs([bsearch $aw $path] + 1) + 1]
700 $aw conf -state normal
701 set ico [$aw image create $lno.0 \
702 -align center -padx 5 -pady 1 \
703 -image [mapicon $m $path]]
704 $aw insert $lno.1 "$path\n"
705 $aw conf -state disabled
706 set file_states($path) [lreplace $s $ai $ai [list $ico]]
707 } elseif {[mapicon $m $path] != [mapicon $old_m $path]} {
708 set ico [lindex $d 0]
709 $aw image conf $ico -image [mapicon $m $path]
713 proc with_update_index {body} {
714 global update_index_fd
716 if {$update_index_fd == {}} {
717 if {![lock_index update]} return
718 set update_index_fd [open \
719 "| git update-index --add --remove -z --stdin" \
721 fconfigure $update_index_fd -translation binary
722 uplevel 1 $body
723 close $update_index_fd
724 set update_index_fd {}
725 unlock_index
726 } else {
727 uplevel 1 $body
731 proc update_index {path} {
732 global update_index_fd
734 if {$update_index_fd == {}} {
735 error {not in with_update_index}
736 } else {
737 puts -nonewline $update_index_fd "$path\0"
741 proc toggle_mode {path} {
742 global file_states
744 set s $file_states($path)
745 set m [lindex $s 0]
747 switch -- $m {
748 AM -
749 _O {set new A*}
750 _M -
751 MM {set new M*}
752 _D {set new D*}
753 default {return}
756 with_update_index {update_index $path}
757 display_file $path $new
760 ######################################################################
762 ## icons
764 set filemask {
765 #define mask_width 14
766 #define mask_height 15
767 static unsigned char mask_bits[] = {
768 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f,
769 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f,
770 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f};
773 image create bitmap file_plain -background white -foreground black -data {
774 #define plain_width 14
775 #define plain_height 15
776 static unsigned char plain_bits[] = {
777 0xfe, 0x01, 0x02, 0x03, 0x02, 0x05, 0x02, 0x09, 0x02, 0x1f, 0x02, 0x10,
778 0x02, 0x10, 0x02, 0x10, 0x02, 0x10, 0x02, 0x10, 0x02, 0x10, 0x02, 0x10,
779 0x02, 0x10, 0x02, 0x10, 0xfe, 0x1f};
780 } -maskdata $filemask
782 image create bitmap file_mod -background white -foreground blue -data {
783 #define mod_width 14
784 #define mod_height 15
785 static unsigned char mod_bits[] = {
786 0xfe, 0x01, 0x02, 0x03, 0x7a, 0x05, 0x02, 0x09, 0x7a, 0x1f, 0x02, 0x10,
787 0xfa, 0x17, 0x02, 0x10, 0xfa, 0x17, 0x02, 0x10, 0xfa, 0x17, 0x02, 0x10,
788 0xfa, 0x17, 0x02, 0x10, 0xfe, 0x1f};
789 } -maskdata $filemask
791 image create bitmap file_fulltick -background white -foreground "#007000" -data {
792 #define file_fulltick_width 14
793 #define file_fulltick_height 15
794 static unsigned char file_fulltick_bits[] = {
795 0xfe, 0x01, 0x02, 0x1a, 0x02, 0x0c, 0x02, 0x0c, 0x02, 0x16, 0x02, 0x16,
796 0x02, 0x13, 0x00, 0x13, 0x86, 0x11, 0x8c, 0x11, 0xd8, 0x10, 0xf2, 0x10,
797 0x62, 0x10, 0x02, 0x10, 0xfe, 0x1f};
798 } -maskdata $filemask
800 image create bitmap file_parttick -background white -foreground "#005050" -data {
801 #define parttick_width 14
802 #define parttick_height 15
803 static unsigned char parttick_bits[] = {
804 0xfe, 0x01, 0x02, 0x03, 0x7a, 0x05, 0x02, 0x09, 0x7a, 0x1f, 0x02, 0x10,
805 0x7a, 0x14, 0x02, 0x16, 0x02, 0x13, 0x8a, 0x11, 0xda, 0x10, 0x72, 0x10,
806 0x22, 0x10, 0x02, 0x10, 0xfe, 0x1f};
807 } -maskdata $filemask
809 image create bitmap file_question -background white -foreground black -data {
810 #define file_question_width 14
811 #define file_question_height 15
812 static unsigned char file_question_bits[] = {
813 0xfe, 0x01, 0x02, 0x02, 0xe2, 0x04, 0xf2, 0x09, 0x1a, 0x1b, 0x0a, 0x13,
814 0x82, 0x11, 0xc2, 0x10, 0x62, 0x10, 0x62, 0x10, 0x02, 0x10, 0x62, 0x10,
815 0x62, 0x10, 0x02, 0x10, 0xfe, 0x1f};
816 } -maskdata $filemask
818 image create bitmap file_removed -background white -foreground red -data {
819 #define file_removed_width 14
820 #define file_removed_height 15
821 static unsigned char file_removed_bits[] = {
822 0xfe, 0x01, 0x02, 0x03, 0x02, 0x05, 0x02, 0x09, 0x02, 0x1f, 0x02, 0x10,
823 0x1a, 0x16, 0x32, 0x13, 0xe2, 0x11, 0xc2, 0x10, 0xe2, 0x11, 0x32, 0x13,
824 0x1a, 0x16, 0x02, 0x10, 0xfe, 0x1f};
825 } -maskdata $filemask
827 image create bitmap file_merge -background white -foreground blue -data {
828 #define file_merge_width 14
829 #define file_merge_height 15
830 static unsigned char file_merge_bits[] = {
831 0xfe, 0x01, 0x02, 0x03, 0x62, 0x05, 0x62, 0x09, 0x62, 0x1f, 0x62, 0x10,
832 0xfa, 0x11, 0xf2, 0x10, 0x62, 0x10, 0x02, 0x10, 0xfa, 0x17, 0x02, 0x10,
833 0xfa, 0x17, 0x02, 0x10, 0xfe, 0x1f};
834 } -maskdata $filemask
836 set max_status_desc 0
837 foreach i {
838 {__ i plain "Unmodified"}
839 {_M i mod "Modified"}
840 {M_ i fulltick "Checked in"}
841 {MM i parttick "Partially checked in"}
843 {_O o plain "Untracked"}
844 {A_ o fulltick "Added"}
845 {AM o parttick "Partially added"}
847 {_D i question "Missing"}
848 {D_ i removed "Removed"}
849 {DD i removed "Removed"}
850 {DO i removed "Removed (still exists)"}
852 {UM i merge "Merge conflicts"}
853 {U_ i merge "Merge conflicts"}
855 if {$max_status_desc < [string length [lindex $i 3]]} {
856 set max_status_desc [string length [lindex $i 3]]
858 set all_cols([lindex $i 0]) [lindex $i 1]
859 set all_icons([lindex $i 0]) file_[lindex $i 2]
860 set all_descs([lindex $i 0]) [lindex $i 3]
862 unset filemask i
864 ######################################################################
866 ## util
868 proc error_popup {msg} {
869 set w .error
870 toplevel $w
871 wm transient $w .
872 show_msg $w $w $msg
875 proc show_msg {w top msg} {
876 global gitdir appname
878 message $w.m -text $msg -justify left -aspect 400
879 pack $w.m -side top -fill x -padx 5 -pady 10
880 button $w.ok -text OK \
881 -width 15 \
882 -command "destroy $top"
883 pack $w.ok -side bottom
884 bind $top <Visibility> "grab $top; focus $top"
885 bind $top <Key-Return> "destroy $top"
886 wm title $top "error: $appname ([file normalize [file dirname $gitdir]])"
887 tkwait window $top
890 proc hook_failed_popup {hook msg} {
891 global gitdir mainfont difffont appname
893 set w .hookfail
894 toplevel $w
895 wm transient $w .
897 frame $w.m
898 label $w.m.l1 -text "$hook hook failed:" \
899 -anchor w \
900 -justify left \
901 -font [concat $mainfont bold]
902 text $w.m.t \
903 -background white -borderwidth 1 \
904 -relief sunken \
905 -width 80 -height 10 \
906 -font $difffont \
907 -yscrollcommand [list $w.m.sby set]
908 label $w.m.l2 \
909 -text {You must correct the above errors before committing.} \
910 -anchor w \
911 -justify left \
912 -font [concat $mainfont bold]
913 scrollbar $w.m.sby -command [list $w.m.t yview]
914 pack $w.m.l1 -side top -fill x
915 pack $w.m.l2 -side bottom -fill x
916 pack $w.m.sby -side right -fill y
917 pack $w.m.t -side left -fill both -expand 1
918 pack $w.m -side top -fill both -expand 1 -padx 5 -pady 10
920 $w.m.t insert 1.0 $msg
921 $w.m.t conf -state disabled
923 button $w.ok -text OK \
924 -width 15 \
925 -command "destroy $w"
926 pack $w.ok -side bottom
928 bind $w <Visibility> "grab $w; focus $w"
929 bind $w <Key-Return> "destroy $w"
930 wm title $w "error: $appname ([file normalize [file dirname $gitdir]])"
931 tkwait window $w
934 ######################################################################
936 ## ui commands
938 set starting_gitk_msg {Please wait... Starting gitk...}
939 proc do_gitk {} {
940 global tcl_platform ui_status_value starting_gitk_msg
942 set ui_status_value $starting_gitk_msg
943 after 10000 {
944 if {$ui_status_value == $starting_gitk_msg} {
945 set ui_status_value {Ready.}
949 if {$tcl_platform(platform) == "windows"} {
950 exec sh -c gitk &
951 } else {
952 exec gitk &
956 proc do_quit {} {
957 global gitdir ui_comm
959 set save [file join $gitdir GITGUI_MSG]
960 set msg [string trim [$ui_comm get 0.0 end]]
961 if {[$ui_comm edit modified] && $msg != {}} {
962 catch {
963 set fd [open $save w]
964 puts $fd [string trim [$ui_comm get 0.0 end]]
965 close $fd
967 } elseif {$msg == {} && [file exists $save]} {
968 file delete $save
971 destroy .
974 proc do_rescan {} {
975 update_status
978 proc do_checkin_all {} {
979 global checkin_active ui_status_value
981 if {$checkin_active || ![lock_index begin-update]} return
983 set checkin_active 1
984 set ui_status_value {Checking in all files...}
985 after 1 {
986 with_update_index {
987 foreach path [array names file_states] {
988 set s $file_states($path)
989 set m [lindex $s 0]
990 switch -- $m {
991 AM -
992 MM -
993 _M -
994 _D {toggle_mode $path}
998 set checkin_active 0
999 set ui_status_value {Ready.}
1003 proc do_signoff {} {
1004 global ui_comm
1006 catch {
1007 set me [exec git var GIT_COMMITTER_IDENT]
1008 if {[regexp {(.*) [0-9]+ [-+0-9]+$} $me me name]} {
1009 set str "Signed-off-by: $name"
1010 if {[$ui_comm get {end -1c linestart} {end -1c}] != $str} {
1011 $ui_comm insert end "\n"
1012 $ui_comm insert end $str
1013 $ui_comm see end
1019 proc do_amend_last {} {
1020 load_last_commit
1023 proc do_commit {} {
1024 commit_tree
1027 # shift == 1: left click
1028 # 3: right click
1029 proc click {w x y shift wx wy} {
1030 global ui_index ui_other
1032 set pos [split [$w index @$x,$y] .]
1033 set lno [lindex $pos 0]
1034 set col [lindex $pos 1]
1035 set path [$w get $lno.1 $lno.end]
1036 if {$path == {}} return
1038 if {$col > 0 && $shift == 1} {
1039 $ui_index tag remove in_diff 0.0 end
1040 $ui_other tag remove in_diff 0.0 end
1041 $w tag add in_diff $lno.0 [expr $lno + 1].0
1042 show_diff $path
1046 proc unclick {w x y} {
1047 set pos [split [$w index @$x,$y] .]
1048 set lno [lindex $pos 0]
1049 set col [lindex $pos 1]
1050 set path [$w get $lno.1 $lno.end]
1051 if {$path == {}} return
1053 if {$col == 0} {
1054 toggle_mode $path
1058 ######################################################################
1060 ## ui init
1062 set mainfont {Helvetica 10}
1063 set difffont {Courier 10}
1064 set maincursor [. cget -cursor]
1066 switch -- $tcl_platform(platform) {
1067 windows {set M1B Control; set M1T Ctrl}
1068 default {set M1B M1; set M1T M1}
1071 # -- Menu Bar
1072 menu .mbar -tearoff 0
1073 .mbar add cascade -label Project -menu .mbar.project
1074 .mbar add cascade -label Commit -menu .mbar.commit
1075 .mbar add cascade -label Fetch -menu .mbar.fetch
1076 .mbar add cascade -label Pull -menu .mbar.pull
1077 . configure -menu .mbar
1079 # -- Project Menu
1080 menu .mbar.project
1081 .mbar.project add command -label Visualize \
1082 -command do_gitk \
1083 -font $mainfont
1084 .mbar.project add command -label Quit \
1085 -command do_quit \
1086 -accelerator $M1T-Q \
1087 -font $mainfont
1089 # -- Commit Menu
1090 menu .mbar.commit
1091 .mbar.commit add command -label Rescan \
1092 -command do_rescan \
1093 -accelerator F5 \
1094 -font $mainfont
1095 lappend disable_on_lock \
1096 [list .mbar.commit entryconf [.mbar.commit index last] -state]
1097 .mbar.commit add command -label {Amend Last Commit} \
1098 -command do_amend_last \
1099 -font $mainfont
1100 lappend disable_on_lock \
1101 [list .mbar.commit entryconf [.mbar.commit index last] -state]
1102 .mbar.commit add command -label {Check-in All Files} \
1103 -command do_checkin_all \
1104 -accelerator $M1T-U \
1105 -font $mainfont
1106 lappend disable_on_lock \
1107 [list .mbar.commit entryconf [.mbar.commit index last] -state]
1108 .mbar.commit add command -label {Sign Off} \
1109 -command do_signoff \
1110 -accelerator $M1T-S \
1111 -font $mainfont
1112 .mbar.commit add command -label Commit \
1113 -command do_commit \
1114 -accelerator $M1T-Return \
1115 -font $mainfont
1116 lappend disable_on_lock \
1117 [list .mbar.commit entryconf [.mbar.commit index last] -state]
1119 # -- Fetch Menu
1120 menu .mbar.fetch
1122 # -- Pull Menu
1123 menu .mbar.pull
1125 # -- Main Window Layout
1126 panedwindow .vpane -orient vertical
1127 panedwindow .vpane.files -orient horizontal
1128 .vpane add .vpane.files -sticky nsew -height 100 -width 400
1129 pack .vpane -anchor n -side top -fill both -expand 1
1131 # -- Index File List
1132 set ui_index .vpane.files.index.list
1133 frame .vpane.files.index -height 100 -width 400
1134 label .vpane.files.index.title -text {Modified Files} \
1135 -background green \
1136 -font $mainfont
1137 text $ui_index -background white -borderwidth 0 \
1138 -width 40 -height 10 \
1139 -font $mainfont \
1140 -yscrollcommand {.vpane.files.index.sb set} \
1141 -cursor $maincursor \
1142 -state disabled
1143 scrollbar .vpane.files.index.sb -command [list $ui_index yview]
1144 pack .vpane.files.index.title -side top -fill x
1145 pack .vpane.files.index.sb -side right -fill y
1146 pack $ui_index -side left -fill both -expand 1
1147 .vpane.files add .vpane.files.index -sticky nsew
1149 # -- Other (Add) File List
1150 set ui_other .vpane.files.other.list
1151 frame .vpane.files.other -height 100 -width 100
1152 label .vpane.files.other.title -text {Untracked Files} \
1153 -background red \
1154 -font $mainfont
1155 text $ui_other -background white -borderwidth 0 \
1156 -width 40 -height 10 \
1157 -font $mainfont \
1158 -yscrollcommand {.vpane.files.other.sb set} \
1159 -cursor $maincursor \
1160 -state disabled
1161 scrollbar .vpane.files.other.sb -command [list $ui_other yview]
1162 pack .vpane.files.other.title -side top -fill x
1163 pack .vpane.files.other.sb -side right -fill y
1164 pack $ui_other -side left -fill both -expand 1
1165 .vpane.files add .vpane.files.other -sticky nsew
1167 $ui_index tag conf in_diff -font [concat $mainfont bold]
1168 $ui_other tag conf in_diff -font [concat $mainfont bold]
1170 # -- Diff Header
1171 set ui_fname_value {}
1172 set ui_fstatus_value {}
1173 frame .vpane.diff -height 200 -width 400
1174 frame .vpane.diff.header
1175 label .vpane.diff.header.l1 -text {File:} -font $mainfont
1176 label .vpane.diff.header.l2 -textvariable ui_fname_value \
1177 -anchor w \
1178 -justify left \
1179 -font $mainfont
1180 label .vpane.diff.header.l3 -text {Status:} -font $mainfont
1181 label .vpane.diff.header.l4 -textvariable ui_fstatus_value \
1182 -width $max_status_desc \
1183 -anchor w \
1184 -justify left \
1185 -font $mainfont
1186 pack .vpane.diff.header.l1 -side left
1187 pack .vpane.diff.header.l2 -side left -fill x
1188 pack .vpane.diff.header.l4 -side right
1189 pack .vpane.diff.header.l3 -side right
1191 # -- Diff Body
1192 frame .vpane.diff.body
1193 set ui_diff .vpane.diff.body.t
1194 text $ui_diff -background white -borderwidth 0 \
1195 -width 80 -height 15 -wrap none \
1196 -font $difffont \
1197 -xscrollcommand {.vpane.diff.body.sbx set} \
1198 -yscrollcommand {.vpane.diff.body.sby set} \
1199 -cursor $maincursor \
1200 -state disabled
1201 scrollbar .vpane.diff.body.sbx -orient horizontal \
1202 -command [list $ui_diff xview]
1203 scrollbar .vpane.diff.body.sby -orient vertical \
1204 -command [list $ui_diff yview]
1205 pack .vpane.diff.body.sbx -side bottom -fill x
1206 pack .vpane.diff.body.sby -side right -fill y
1207 pack $ui_diff -side left -fill both -expand 1
1208 pack .vpane.diff.header -side top -fill x
1209 pack .vpane.diff.body -side bottom -fill both -expand 1
1210 .vpane add .vpane.diff -stick nsew
1212 $ui_diff tag conf dm -foreground red
1213 $ui_diff tag conf dp -foreground blue
1214 $ui_diff tag conf da -font [concat $difffont bold]
1215 $ui_diff tag conf di -foreground "#00a000"
1216 $ui_diff tag conf dni -foreground "#a000a0"
1217 $ui_diff tag conf bold -font [concat $difffont bold]
1219 # -- Commit Area
1220 frame .vpane.commarea -height 170
1221 .vpane add .vpane.commarea -stick nsew
1223 # -- Commit Area Buttons
1224 frame .vpane.commarea.buttons
1225 label .vpane.commarea.buttons.l -text {} \
1226 -anchor w \
1227 -justify left \
1228 -font $mainfont
1229 pack .vpane.commarea.buttons.l -side top -fill x
1230 pack .vpane.commarea.buttons -side left -fill y
1232 button .vpane.commarea.buttons.rescan -text {Rescan} \
1233 -command do_rescan \
1234 -font $mainfont
1235 pack .vpane.commarea.buttons.rescan -side top -fill x
1236 lappend disable_on_lock {.vpane.commarea.buttons.rescan conf -state}
1238 button .vpane.commarea.buttons.amend -text {Amend Last} \
1239 -command do_amend_last \
1240 -font $mainfont
1241 pack .vpane.commarea.buttons.amend -side top -fill x
1242 lappend disable_on_lock {.vpane.commarea.buttons.amend conf -state}
1244 button .vpane.commarea.buttons.ciall -text {Check-in All} \
1245 -command do_checkin_all \
1246 -font $mainfont
1247 pack .vpane.commarea.buttons.ciall -side top -fill x
1248 lappend disable_on_lock {.vpane.commarea.buttons.ciall conf -state}
1250 button .vpane.commarea.buttons.signoff -text {Sign Off} \
1251 -command do_signoff \
1252 -font $mainfont
1253 pack .vpane.commarea.buttons.signoff -side top -fill x
1255 button .vpane.commarea.buttons.commit -text {Commit} \
1256 -command do_commit \
1257 -font $mainfont
1258 pack .vpane.commarea.buttons.commit -side top -fill x
1259 lappend disable_on_lock {.vpane.commarea.buttons.commit conf -state}
1261 # -- Commit Message Buffer
1262 frame .vpane.commarea.buffer
1263 set ui_comm .vpane.commarea.buffer.t
1264 label .vpane.commarea.buffer.l -text {Commit Message:} \
1265 -anchor w \
1266 -justify left \
1267 -font $mainfont
1268 text $ui_comm -background white -borderwidth 1 \
1269 -relief sunken \
1270 -width 75 -height 10 -wrap none \
1271 -font $difffont \
1272 -yscrollcommand {.vpane.commarea.buffer.sby set} \
1273 -cursor $maincursor
1274 scrollbar .vpane.commarea.buffer.sby -command [list $ui_comm yview]
1275 pack .vpane.commarea.buffer.l -side top -fill x
1276 pack .vpane.commarea.buffer.sby -side right -fill y
1277 pack $ui_comm -side left -fill y
1278 pack .vpane.commarea.buffer -side left -fill y
1280 # -- Status Bar
1281 set ui_status_value {Initializing...}
1282 label .status -textvariable ui_status_value \
1283 -anchor w \
1284 -justify left \
1285 -borderwidth 1 \
1286 -relief sunken \
1287 -font $mainfont
1288 pack .status -anchor w -side bottom -fill x
1290 # -- Key Bindings
1291 bind $ui_comm <$M1B-Key-Return> {do_commit;break}
1292 bind . <Destroy> do_quit
1293 bind . <Key-F5> do_rescan
1294 bind . <$M1B-Key-r> do_rescan
1295 bind . <$M1B-Key-R> do_rescan
1296 bind . <$M1B-Key-s> do_signoff
1297 bind . <$M1B-Key-S> do_signoff
1298 bind . <$M1B-Key-u> do_checkin_all
1299 bind . <$M1B-Key-U> do_checkin_all
1300 bind . <$M1B-Key-Return> do_commit
1301 bind . <$M1B-Key-q> do_quit
1302 bind . <$M1B-Key-Q> do_quit
1303 foreach i [list $ui_index $ui_other] {
1304 bind $i <Button-1> {click %W %x %y 1 %X %Y; break}
1305 bind $i <Button-3> {click %W %x %y 3 %X %Y; break}
1306 bind $i <ButtonRelease-1> {unclick %W %x %y; break}
1308 unset i M1B M1T
1310 ######################################################################
1312 ## main
1314 if {[catch {set gitdir [exec git rev-parse --git-dir]} err]} {
1315 show_msg {} . "Cannot find the git directory: $err"
1316 exit 1
1319 set appname [lindex [file split $argv0] end]
1320 if {$appname == {git-citool}} {
1321 set single_commit 1
1324 wm title . "$appname ([file normalize [file dirname $gitdir]])"
1325 focus -force $ui_comm
1326 update_status