1#!/bin/sh
2# Tcl ignores the next line -*- tcl -*- \
3exec wish "$0" -- "$@"
4
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.
9
10######################################################################
11##
12## task management
13
14set single_commit 0
15set status_active 0
16set diff_active 0
17set checkin_active 0
18set commit_active 0
19set update_index_fd {}
20
21set disable_on_lock [list]
22set index_lock_type none
23
24set HEAD {}
25set PARENT {}
26set commit_type {}
27
28proc lock_index {type} {
29 global index_lock_type disable_on_lock
30
31 if {$index_lock_type == {none}} {
32 set index_lock_type $type
33 foreach w $disable_on_lock {
34 uplevel #0 $w disabled
35 }
36 return 1
37 } elseif {$index_lock_type == {begin-update} && $type == {update}} {
38 set index_lock_type $type
39 return 1
40 }
41 return 0
42}
43
44proc unlock_index {} {
45 global index_lock_type disable_on_lock
46
47 set index_lock_type none
48 foreach w $disable_on_lock {
49 uplevel #0 $w normal
50 }
51}
52
53######################################################################
54##
55## status
56
57proc repository_state {hdvar ctvar} {
58 global gitdir
59 upvar $hdvar hd $ctvar ct
60
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
67 }
68}
69
70proc 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
74
75 if {$status_active || ![lock_index read]} return
76
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
85 }
86
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
92 }
93
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]} {
99 }
100 $ui_comm edit modified false
101 }
102
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]
108}
109
110proc 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
114
115 read $fd
116 if {![eof $fd]} return
117 close $fd
118
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"
124 }
125
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]
131
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]
138}
139
140proc load_message {file} {
141 global gitdir ui_comm
142
143 set f [file join $gitdir $file]
144 if {[file isfile $f]} {
145 if {[catch {set fd [open $f r]}]} {
146 return 0
147 }
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
153 }
154 return 0
155}
156
157proc read_diff_index {fd final} {
158 global buf_rdi
159
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]_
166 }
167 }
168 status_eof $fd buf_rdi $final
169}
170
171proc read_diff_files {fd final} {
172 global buf_rdf
173
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]
180 }
181 }
182 status_eof $fd buf_rdf $final
183}
184
185proc read_ls_others {fd final} {
186 global buf_rlo
187
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
193 }
194 status_eof $fd buf_rlo $final
195}
196
197proc status_eof {fd buf final} {
198 global status_active $buf
199 global ui_fname_value ui_status_value file_states
200
201 if {[eof $fd]} {
202 set $buf {}
203 close $fd
204 if {[incr status_active -1] == 0} {
205 unlock_index
206
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
213 }
214 }
215 }
216}
217
218######################################################################
219##
220## diff
221
222proc clear_diff {} {
223 global ui_diff ui_fname_value ui_fstatus_value
224
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 {}
230}
231
232proc 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
235
236 if {$diff_active || ![lock_index read]} return
237
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..."
246
247 set cmd [list | git diff-index -p $PARENT -- $path]
248 switch $m {
249 AM {
250 }
251 MM {
252 set cmd [list | git diff-index -p -c $PARENT $path]
253 }
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
265 }
266 $ui_diff conf -state normal
267 $ui_diff insert end $content
268 $ui_diff conf -state disabled
269 set diff_active 0
270 unlock_index
271 set ui_status_value {Ready.}
272 return
273 }
274 }
275
276 if {[catch {set fd [open $cmd r]} err]} {
277 set diff_active 0
278 unlock_index
279 set ui_status_value "Unable to display $path"
280 error_popup "Error loading diff:\n$err"
281 return
282 }
283
284 fconfigure $fd -blocking 0 -translation auto
285 fileevent $fd readable [list read_diff $fd]
286}
287
288proc read_diff {fd} {
289 global ui_diff ui_status_value diff_3way diff_active
290
291 while {[gets $fd line] >= 0} {
292 if {[string match {diff --git *} $line]} continue
293 if {[string match {diff --combined *} $line]} continue
294 if {[string match {--- *} $line]} continue
295 if {[string match {+++ *} $line]} continue
296 if {[string match index* $line]} {
297 if {[string first , $line] >= 0} {
298 set diff_3way 1
299 }
300 }
301
302 $ui_diff conf -state normal
303 if {!$diff_3way} {
304 set x [string index $line 0]
305 switch -- $x {
306 "@" {set tags da}
307 "+" {set tags dp}
308 "-" {set tags dm}
309 default {set tags {}}
310 }
311 } else {
312 set x [string range $line 0 1]
313 switch -- $x {
314 default {set tags {}}
315 "@@" {set tags da}
316 "++" {set tags dp; set x " +"}
317 " +" {set tags {di bold}; set x "++"}
318 "+ " {set tags dni; set x "-+"}
319 "--" {set tags dm; set x " -"}
320 " -" {set tags {dm bold}; set x "--"}
321 "- " {set tags di; set x "+-"}
322 default {set tags {}}
323 }
324 set line [string replace $line 0 1 $x]
325 }
326 $ui_diff insert end $line $tags
327 $ui_diff insert end "\n"
328 $ui_diff conf -state disabled
329 }
330
331 if {[eof $fd]} {
332 close $fd
333 set diff_active 0
334 unlock_index
335 set ui_status_value {Ready.}
336 }
337}
338
339######################################################################
340##
341## commit
342
343proc load_last_commit {} {
344 global HEAD PARENT commit_type ui_comm
345
346 if {$commit_type == {amend}} return
347 if {$commit_type != {normal}} {
348 error_popup "Can't amend a $commit_type commit."
349 return
350 }
351
352 set msg {}
353 set parent {}
354 set parent_count 0
355 if {[catch {
356 set fd [open "| git cat-file commit $HEAD" r]
357 while {[gets $fd line] > 0} {
358 if {[string match {parent *} $line]} {
359 set parent [string range $line 7 end]
360 incr parent_count
361 }
362 }
363 set msg [string trim [read $fd]]
364 close $fd
365 } err]} {
366 error_popup "Error loading commit data for amend:\n$err"
367 return
368 }
369
370 if {$parent_count == 0} {
371 set commit_type amend
372 set HEAD {}
373 set PARENT {}
374 update_status
375 } elseif {$parent_count == 1} {
376 set commit_type amend
377 set PARENT $parent
378 $ui_comm delete 0.0 end
379 $ui_comm insert end $msg
380 $ui_comm edit modified false
381 update_status
382 } else {
383 error_popup {You can't amend a merge commit.}
384 return
385 }
386}
387
388proc commit_tree {} {
389 global tcl_platform HEAD gitdir commit_type file_states
390 global commit_active ui_status_value
391 global ui_comm
392
393 if {$commit_active || ![lock_index update]} return
394
395 # -- Our in memory state should match the repository.
396 #
397 repository_state curHEAD cur_type
398 if {$commit_type == {amend}
399 && $cur_type == {normal}
400 && $curHEAD == $HEAD} {
401 } elseif {$commit_type != $cur_type || $HEAD != $curHEAD} {
402 error_popup {Last scanned state does not match repository state.
403
404Its highly likely that another Git program modified the
405repository since our last scan. A rescan is required
406before committing.
407}
408 unlock_index
409 update_status
410 return
411 }
412
413 # -- At least one file should differ in the index.
414 #
415 set files_ready 0
416 foreach path [array names file_states] {
417 set s $file_states($path)
418 switch -glob -- [lindex $s 0] {
419 _* {continue}
420 A* -
421 D* -
422 M* {set files_ready 1; break}
423 U* {
424 error_popup "Unmerged files cannot be committed.
425
426File $path has merge conflicts.
427You must resolve them and check the file in before committing.
428"
429 unlock_index
430 return
431 }
432 default {
433 error_popup "Unknown file state [lindex $s 0] detected.
434
435File $path cannot be committed by this program.
436"
437 }
438 }
439 }
440 if {!$files_ready} {
441 error_popup {No checked-in files to commit.
442
443You must check-in at least 1 file before you can commit.
444}
445 unlock_index
446 return
447 }
448
449 # -- A message is required.
450 #
451 set msg [string trim [$ui_comm get 1.0 end]]
452 if {$msg == {}} {
453 error_popup {Please supply a commit message.
454
455A good commit message has the following format:
456
457- First line: Describe in one sentance what you did.
458- Second line: Blank
459- Remaining lines: Describe why this change is good.
460}
461 unlock_index
462 return
463 }
464
465 # -- Ask the pre-commit hook for the go-ahead.
466 #
467 set pchook [file join $gitdir hooks pre-commit]
468 if {$tcl_platform(platform) == {windows} && [file isfile $pchook]} {
469 set pchook [list sh -c \
470 "if test -x \"$pchook\"; then exec \"$pchook\"; fi"]
471 } elseif {[file executable $pchook]} {
472 set pchook [list $pchook]
473 } else {
474 set pchook {}
475 }
476 if {$pchook != {} && [catch {eval exec $pchook} err]} {
477 hook_failed_popup pre-commit $err
478 unlock_index
479 return
480 }
481
482 # -- Write the tree in the background.
483 #
484 set commit_active 1
485 set ui_status_value {Committing changes...}
486
487 set fd_wt [open "| git write-tree" r]
488 fileevent $fd_wt readable [list commit_stage2 $fd_wt $curHEAD $msg]
489}
490
491proc commit_stage2 {fd_wt curHEAD msg} {
492 global single_commit gitdir PARENT commit_type
493 global commit_active ui_status_value ui_comm
494
495 gets $fd_wt tree_id
496 close $fd_wt
497
498 if {$tree_id == {}} {
499 error_popup "write-tree failed"
500 set commit_active 0
501 set ui_status_value {Commit failed.}
502 unlock_index
503 return
504 }
505
506 # -- Create the commit.
507 #
508 set cmd [list git commit-tree $tree_id]
509 if {$PARENT != {}} {
510 lappend cmd -p $PARENT
511 }
512 if {$commit_type == {merge}} {
513 if {[catch {
514 set fd_mh [open [file join $gitdir MERGE_HEAD] r]
515 while {[gets $fd_mh merge_head] >= 0} {
516 lappend cmd -p $merge_head
517 }
518 close $fd_mh
519 } err]} {
520 error_popup "Loading MERGE_HEADs failed:\n$err"
521 set commit_active 0
522 set ui_status_value {Commit failed.}
523 unlock_index
524 return
525 }
526 }
527 if {$PARENT == {}} {
528 # git commit-tree writes to stderr during initial commit.
529 lappend cmd 2>/dev/null
530 }
531 lappend cmd << $msg
532 if {[catch {set cmt_id [eval exec $cmd]} err]} {
533 error_popup "commit-tree failed:\n$err"
534 set commit_active 0
535 set ui_status_value {Commit failed.}
536 unlock_index
537 return
538 }
539
540 # -- Update the HEAD ref.
541 #
542 set reflogm commit
543 if {$commit_type != {normal}} {
544 append reflogm " ($commit_type)"
545 }
546 set i [string first "\n" $msg]
547 if {$i >= 0} {
548 append reflogm {: } [string range $msg 0 [expr $i - 1]]
549 } else {
550 append reflogm {: } $msg
551 }
552 set cmd [list git update-ref -m $reflogm HEAD $cmt_id $curHEAD]
553 if {[catch {eval exec $cmd} err]} {
554 error_popup "update-ref failed:\n$err"
555 set commit_active 0
556 set ui_status_value {Commit failed.}
557 unlock_index
558 return
559 }
560
561 # -- Cleanup after ourselves.
562 #
563 catch {file delete [file join $gitdir MERGE_HEAD]}
564 catch {file delete [file join $gitdir MERGE_MSG]}
565 catch {file delete [file join $gitdir SQUASH_MSG]}
566 catch {file delete [file join $gitdir GITGUI_MSG]}
567
568 # -- Let rerere do its thing.
569 #
570 if {[file isdirectory [file join $gitdir rr-cache]]} {
571 catch {exec git rerere}
572 }
573
574 $ui_comm delete 0.0 end
575 $ui_comm edit modified false
576
577 if {$single_commit} do_quit
578
579 set commit_type {}
580 set commit_active 0
581 set HEAD $cmt_id
582 set PARENT $cmt_id
583 unlock_index
584 update_status "Changes committed as $cmt_id."
585}
586
587######################################################################
588##
589## ui helpers
590
591proc mapcol {state path} {
592 global all_cols
593
594 if {[catch {set r $all_cols($state)}]} {
595 puts "error: no column for state={$state} $path"
596 return o
597 }
598 return $r
599}
600
601proc mapicon {state path} {
602 global all_icons
603
604 if {[catch {set r $all_icons($state)}]} {
605 puts "error: no icon for state={$state} $path"
606 return file_plain
607 }
608 return $r
609}
610
611proc mapdesc {state path} {
612 global all_descs
613
614 if {[catch {set r $all_descs($state)}]} {
615 puts "error: no desc for state={$state} $path"
616 return $state
617 }
618 return $r
619}
620
621proc bsearch {w path} {
622 set hi [expr [lindex [split [$w index end] .] 0] - 2]
623 if {$hi == 0} {
624 return -1
625 }
626 set lo 0
627 while {$lo < $hi} {
628 set mi [expr [expr $lo + $hi] / 2]
629 set ti [expr $mi + 1]
630 set cmp [string compare [$w get $ti.1 $ti.end] $path]
631 if {$cmp < 0} {
632 set lo $ti
633 } elseif {$cmp == 0} {
634 return $mi
635 } else {
636 set hi $mi
637 }
638 }
639 return -[expr $lo + 1]
640}
641
642proc merge_state {path state} {
643 global file_states
644
645 if {[array names file_states -exact $path] == {}} {
646 set o __
647 set s [list $o none none]
648 } else {
649 set s $file_states($path)
650 set o [lindex $s 0]
651 }
652
653 set m [lindex $s 0]
654 if {[string index $state 0] == "_"} {
655 set state [string index $m 0][string index $state 1]
656 } elseif {[string index $state 0] == "*"} {
657 set state _[string index $state 1]
658 }
659
660 if {[string index $state 1] == "_"} {
661 set state [string index $state 0][string index $m 1]
662 } elseif {[string index $state 1] == "*"} {
663 set state [string index $state 0]_
664 }
665
666 set file_states($path) [lreplace $s 0 0 $state]
667 return $o
668}
669
670proc display_file {path state} {
671 global ui_index ui_other file_states
672
673 set old_m [merge_state $path $state]
674 set s $file_states($path)
675 set m [lindex $s 0]
676
677 if {[mapcol $m $path] == "o"} {
678 set ii 1
679 set ai 2
680 set iw $ui_index
681 set aw $ui_other
682 } else {
683 set ii 2
684 set ai 1
685 set iw $ui_other
686 set aw $ui_index
687 }
688
689 set d [lindex $s $ii]
690 if {$d != "none"} {
691 set lno [bsearch $iw $path]
692 if {$lno >= 0} {
693 incr lno
694 $iw conf -state normal
695 $iw delete $lno.0 [expr $lno + 1].0
696 $iw conf -state disabled
697 set s [lreplace $s $ii $ii none]
698 }
699 }
700
701 set d [lindex $s $ai]
702 if {$d == "none"} {
703 set lno [expr abs([bsearch $aw $path] + 1) + 1]
704 $aw conf -state normal
705 set ico [$aw image create $lno.0 \
706 -align center -padx 5 -pady 1 \
707 -image [mapicon $m $path]]
708 $aw insert $lno.1 "$path\n"
709 $aw conf -state disabled
710 set file_states($path) [lreplace $s $ai $ai [list $ico]]
711 } elseif {[mapicon $m $path] != [mapicon $old_m $path]} {
712 set ico [lindex $d 0]
713 $aw image conf $ico -image [mapicon $m $path]
714 }
715}
716
717proc with_update_index {body} {
718 global update_index_fd
719
720 if {$update_index_fd == {}} {
721 if {![lock_index update]} return
722 set update_index_fd [open \
723 "| git update-index --add --remove -z --stdin" \
724 w]
725 fconfigure $update_index_fd -translation binary
726 uplevel 1 $body
727 close $update_index_fd
728 set update_index_fd {}
729 unlock_index
730 } else {
731 uplevel 1 $body
732 }
733}
734
735proc update_index {path} {
736 global update_index_fd
737
738 if {$update_index_fd == {}} {
739 error {not in with_update_index}
740 } else {
741 puts -nonewline $update_index_fd "$path\0"
742 }
743}
744
745proc toggle_mode {path} {
746 global file_states ui_fname_value
747
748 set s $file_states($path)
749 set m [lindex $s 0]
750
751 switch -- $m {
752 AM -
753 _O {set new A*}
754 _M -
755 MM {set new M*}
756 AD -
757 _D {set new D*}
758 default {return}
759 }
760
761 with_update_index {update_index $path}
762 display_file $path $new
763 if {$ui_fname_value == $path} {
764 show_diff $path
765 }
766}
767
768######################################################################
769##
770## icons
771
772set filemask {
773#define mask_width 14
774#define mask_height 15
775static unsigned char mask_bits[] = {
776 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f,
777 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f,
778 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f};
779}
780
781image create bitmap file_plain -background white -foreground black -data {
782#define plain_width 14
783#define plain_height 15
784static unsigned char plain_bits[] = {
785 0xfe, 0x01, 0x02, 0x03, 0x02, 0x05, 0x02, 0x09, 0x02, 0x1f, 0x02, 0x10,
786 0x02, 0x10, 0x02, 0x10, 0x02, 0x10, 0x02, 0x10, 0x02, 0x10, 0x02, 0x10,
787 0x02, 0x10, 0x02, 0x10, 0xfe, 0x1f};
788} -maskdata $filemask
789
790image create bitmap file_mod -background white -foreground blue -data {
791#define mod_width 14
792#define mod_height 15
793static unsigned char mod_bits[] = {
794 0xfe, 0x01, 0x02, 0x03, 0x7a, 0x05, 0x02, 0x09, 0x7a, 0x1f, 0x02, 0x10,
795 0xfa, 0x17, 0x02, 0x10, 0xfa, 0x17, 0x02, 0x10, 0xfa, 0x17, 0x02, 0x10,
796 0xfa, 0x17, 0x02, 0x10, 0xfe, 0x1f};
797} -maskdata $filemask
798
799image create bitmap file_fulltick -background white -foreground "#007000" -data {
800#define file_fulltick_width 14
801#define file_fulltick_height 15
802static unsigned char file_fulltick_bits[] = {
803 0xfe, 0x01, 0x02, 0x1a, 0x02, 0x0c, 0x02, 0x0c, 0x02, 0x16, 0x02, 0x16,
804 0x02, 0x13, 0x00, 0x13, 0x86, 0x11, 0x8c, 0x11, 0xd8, 0x10, 0xf2, 0x10,
805 0x62, 0x10, 0x02, 0x10, 0xfe, 0x1f};
806} -maskdata $filemask
807
808image create bitmap file_parttick -background white -foreground "#005050" -data {
809#define parttick_width 14
810#define parttick_height 15
811static unsigned char parttick_bits[] = {
812 0xfe, 0x01, 0x02, 0x03, 0x7a, 0x05, 0x02, 0x09, 0x7a, 0x1f, 0x02, 0x10,
813 0x7a, 0x14, 0x02, 0x16, 0x02, 0x13, 0x8a, 0x11, 0xda, 0x10, 0x72, 0x10,
814 0x22, 0x10, 0x02, 0x10, 0xfe, 0x1f};
815} -maskdata $filemask
816
817image create bitmap file_question -background white -foreground black -data {
818#define file_question_width 14
819#define file_question_height 15
820static unsigned char file_question_bits[] = {
821 0xfe, 0x01, 0x02, 0x02, 0xe2, 0x04, 0xf2, 0x09, 0x1a, 0x1b, 0x0a, 0x13,
822 0x82, 0x11, 0xc2, 0x10, 0x62, 0x10, 0x62, 0x10, 0x02, 0x10, 0x62, 0x10,
823 0x62, 0x10, 0x02, 0x10, 0xfe, 0x1f};
824} -maskdata $filemask
825
826image create bitmap file_removed -background white -foreground red -data {
827#define file_removed_width 14
828#define file_removed_height 15
829static unsigned char file_removed_bits[] = {
830 0xfe, 0x01, 0x02, 0x03, 0x02, 0x05, 0x02, 0x09, 0x02, 0x1f, 0x02, 0x10,
831 0x1a, 0x16, 0x32, 0x13, 0xe2, 0x11, 0xc2, 0x10, 0xe2, 0x11, 0x32, 0x13,
832 0x1a, 0x16, 0x02, 0x10, 0xfe, 0x1f};
833} -maskdata $filemask
834
835image create bitmap file_merge -background white -foreground blue -data {
836#define file_merge_width 14
837#define file_merge_height 15
838static unsigned char file_merge_bits[] = {
839 0xfe, 0x01, 0x02, 0x03, 0x62, 0x05, 0x62, 0x09, 0x62, 0x1f, 0x62, 0x10,
840 0xfa, 0x11, 0xf2, 0x10, 0x62, 0x10, 0x02, 0x10, 0xfa, 0x17, 0x02, 0x10,
841 0xfa, 0x17, 0x02, 0x10, 0xfe, 0x1f};
842} -maskdata $filemask
843
844set max_status_desc 0
845foreach i {
846 {__ i plain "Unmodified"}
847 {_M i mod "Modified"}
848 {M_ i fulltick "Checked in"}
849 {MM i parttick "Partially checked in"}
850
851 {_O o plain "Untracked"}
852 {A_ o fulltick "Added"}
853 {AM o parttick "Partially added"}
854 {AD o question "Added (but now gone)"}
855
856 {_D i question "Missing"}
857 {D_ i removed "Removed"}
858 {DD i removed "Removed"}
859 {DO i removed "Removed (still exists)"}
860
861 {UM i merge "Merge conflicts"}
862 {U_ i merge "Merge conflicts"}
863 } {
864 if {$max_status_desc < [string length [lindex $i 3]]} {
865 set max_status_desc [string length [lindex $i 3]]
866 }
867 set all_cols([lindex $i 0]) [lindex $i 1]
868 set all_icons([lindex $i 0]) file_[lindex $i 2]
869 set all_descs([lindex $i 0]) [lindex $i 3]
870}
871unset filemask i
872
873######################################################################
874##
875## util
876
877proc error_popup {msg} {
878 set w .error
879 toplevel $w
880 wm transient $w .
881 show_msg $w $w $msg
882}
883
884proc show_msg {w top msg} {
885 global gitdir appname
886
887 message $w.m -text $msg -justify left -aspect 400
888 pack $w.m -side top -fill x -padx 5 -pady 10
889 button $w.ok -text OK \
890 -width 15 \
891 -command "destroy $top"
892 pack $w.ok -side bottom
893 bind $top <Visibility> "grab $top; focus $top"
894 bind $top <Key-Return> "destroy $top"
895 wm title $top "error: $appname ([file normalize [file dirname $gitdir]])"
896 tkwait window $top
897}
898
899proc hook_failed_popup {hook msg} {
900 global gitdir mainfont difffont appname
901
902 set w .hookfail
903 toplevel $w
904 wm transient $w .
905
906 frame $w.m
907 label $w.m.l1 -text "$hook hook failed:" \
908 -anchor w \
909 -justify left \
910 -font [concat $mainfont bold]
911 text $w.m.t \
912 -background white -borderwidth 1 \
913 -relief sunken \
914 -width 80 -height 10 \
915 -font $difffont \
916 -yscrollcommand [list $w.m.sby set]
917 label $w.m.l2 \
918 -text {You must correct the above errors before committing.} \
919 -anchor w \
920 -justify left \
921 -font [concat $mainfont bold]
922 scrollbar $w.m.sby -command [list $w.m.t yview]
923 pack $w.m.l1 -side top -fill x
924 pack $w.m.l2 -side bottom -fill x
925 pack $w.m.sby -side right -fill y
926 pack $w.m.t -side left -fill both -expand 1
927 pack $w.m -side top -fill both -expand 1 -padx 5 -pady 10
928
929 $w.m.t insert 1.0 $msg
930 $w.m.t conf -state disabled
931
932 button $w.ok -text OK \
933 -width 15 \
934 -command "destroy $w"
935 pack $w.ok -side bottom
936
937 bind $w <Visibility> "grab $w; focus $w"
938 bind $w <Key-Return> "destroy $w"
939 wm title $w "error: $appname ([file normalize [file dirname $gitdir]])"
940 tkwait window $w
941}
942
943######################################################################
944##
945## ui commands
946
947set starting_gitk_msg {Please wait... Starting gitk...}
948proc do_gitk {} {
949 global tcl_platform ui_status_value starting_gitk_msg
950
951 set ui_status_value $starting_gitk_msg
952 after 10000 {
953 if {$ui_status_value == $starting_gitk_msg} {
954 set ui_status_value {Ready.}
955 }
956 }
957
958 if {$tcl_platform(platform) == "windows"} {
959 exec sh -c gitk &
960 } else {
961 exec gitk &
962 }
963}
964
965proc do_quit {} {
966 global gitdir ui_comm
967
968 set save [file join $gitdir GITGUI_MSG]
969 set msg [string trim [$ui_comm get 0.0 end]]
970 if {[$ui_comm edit modified] && $msg != {}} {
971 catch {
972 set fd [open $save w]
973 puts $fd [string trim [$ui_comm get 0.0 end]]
974 close $fd
975 }
976 } elseif {$msg == {} && [file exists $save]} {
977 file delete $save
978 }
979
980 destroy .
981}
982
983proc do_rescan {} {
984 update_status
985}
986
987proc do_checkin_all {} {
988 global checkin_active ui_status_value
989
990 if {$checkin_active || ![lock_index begin-update]} return
991
992 set checkin_active 1
993 set ui_status_value {Checking in all files...}
994 after 1 {
995 with_update_index {
996 foreach path [array names file_states] {
997 set s $file_states($path)
998 set m [lindex $s 0]
999 switch -- $m {
1000 AM -
1001 MM -
1002 _M -
1003 _D {toggle_mode $path}
1004 }
1005 }
1006 }
1007 set checkin_active 0
1008 set ui_status_value {Ready.}
1009 }
1010}
1011
1012proc do_signoff {} {
1013 global ui_comm
1014
1015 catch {
1016 set me [exec git var GIT_COMMITTER_IDENT]
1017 if {[regexp {(.*) [0-9]+ [-+0-9]+$} $me me name]} {
1018 set str "Signed-off-by: $name"
1019 if {[$ui_comm get {end -1c linestart} {end -1c}] != $str} {
1020 $ui_comm insert end "\n"
1021 $ui_comm insert end $str
1022 $ui_comm see end
1023 }
1024 }
1025 }
1026}
1027
1028proc do_amend_last {} {
1029 load_last_commit
1030}
1031
1032proc do_commit {} {
1033 commit_tree
1034}
1035
1036# shift == 1: left click
1037# 3: right click
1038proc click {w x y shift wx wy} {
1039 global ui_index ui_other
1040
1041 set pos [split [$w index @$x,$y] .]
1042 set lno [lindex $pos 0]
1043 set col [lindex $pos 1]
1044 set path [$w get $lno.1 $lno.end]
1045 if {$path == {}} return
1046
1047 if {$col > 0 && $shift == 1} {
1048 $ui_index tag remove in_diff 0.0 end
1049 $ui_other tag remove in_diff 0.0 end
1050 $w tag add in_diff $lno.0 [expr $lno + 1].0
1051 show_diff $path
1052 }
1053}
1054
1055proc unclick {w x y} {
1056 set pos [split [$w index @$x,$y] .]
1057 set lno [lindex $pos 0]
1058 set col [lindex $pos 1]
1059 set path [$w get $lno.1 $lno.end]
1060 if {$path == {}} return
1061
1062 if {$col == 0} {
1063 toggle_mode $path
1064 }
1065}
1066
1067######################################################################
1068##
1069## ui init
1070
1071set mainfont {Helvetica 10}
1072set difffont {Courier 10}
1073set maincursor [. cget -cursor]
1074
1075switch -- $tcl_platform(platform) {
1076windows {set M1B Control; set M1T Ctrl}
1077default {set M1B M1; set M1T M1}
1078}
1079
1080# -- Menu Bar
1081menu .mbar -tearoff 0
1082.mbar add cascade -label Project -menu .mbar.project
1083.mbar add cascade -label Commit -menu .mbar.commit
1084.mbar add cascade -label Fetch -menu .mbar.fetch
1085.mbar add cascade -label Pull -menu .mbar.pull
1086. configure -menu .mbar
1087
1088# -- Project Menu
1089menu .mbar.project
1090.mbar.project add command -label Visualize \
1091 -command do_gitk \
1092 -font $mainfont
1093.mbar.project add command -label Quit \
1094 -command do_quit \
1095 -accelerator $M1T-Q \
1096 -font $mainfont
1097
1098# -- Commit Menu
1099menu .mbar.commit
1100.mbar.commit add command -label Rescan \
1101 -command do_rescan \
1102 -accelerator F5 \
1103 -font $mainfont
1104lappend disable_on_lock \
1105 [list .mbar.commit entryconf [.mbar.commit index last] -state]
1106.mbar.commit add command -label {Amend Last Commit} \
1107 -command do_amend_last \
1108 -font $mainfont
1109lappend disable_on_lock \
1110 [list .mbar.commit entryconf [.mbar.commit index last] -state]
1111.mbar.commit add command -label {Check-in All Files} \
1112 -command do_checkin_all \
1113 -accelerator $M1T-U \
1114 -font $mainfont
1115lappend disable_on_lock \
1116 [list .mbar.commit entryconf [.mbar.commit index last] -state]
1117.mbar.commit add command -label {Sign Off} \
1118 -command do_signoff \
1119 -accelerator $M1T-S \
1120 -font $mainfont
1121.mbar.commit add command -label Commit \
1122 -command do_commit \
1123 -accelerator $M1T-Return \
1124 -font $mainfont
1125lappend disable_on_lock \
1126 [list .mbar.commit entryconf [.mbar.commit index last] -state]
1127
1128# -- Fetch Menu
1129menu .mbar.fetch
1130
1131# -- Pull Menu
1132menu .mbar.pull
1133
1134# -- Main Window Layout
1135panedwindow .vpane -orient vertical
1136panedwindow .vpane.files -orient horizontal
1137.vpane add .vpane.files -sticky nsew -height 100 -width 400
1138pack .vpane -anchor n -side top -fill both -expand 1
1139
1140# -- Index File List
1141set ui_index .vpane.files.index.list
1142frame .vpane.files.index -height 100 -width 400
1143label .vpane.files.index.title -text {Modified Files} \
1144 -background green \
1145 -font $mainfont
1146text $ui_index -background white -borderwidth 0 \
1147 -width 40 -height 10 \
1148 -font $mainfont \
1149 -yscrollcommand {.vpane.files.index.sb set} \
1150 -cursor $maincursor \
1151 -state disabled
1152scrollbar .vpane.files.index.sb -command [list $ui_index yview]
1153pack .vpane.files.index.title -side top -fill x
1154pack .vpane.files.index.sb -side right -fill y
1155pack $ui_index -side left -fill both -expand 1
1156.vpane.files add .vpane.files.index -sticky nsew
1157
1158# -- Other (Add) File List
1159set ui_other .vpane.files.other.list
1160frame .vpane.files.other -height 100 -width 100
1161label .vpane.files.other.title -text {Untracked Files} \
1162 -background red \
1163 -font $mainfont
1164text $ui_other -background white -borderwidth 0 \
1165 -width 40 -height 10 \
1166 -font $mainfont \
1167 -yscrollcommand {.vpane.files.other.sb set} \
1168 -cursor $maincursor \
1169 -state disabled
1170scrollbar .vpane.files.other.sb -command [list $ui_other yview]
1171pack .vpane.files.other.title -side top -fill x
1172pack .vpane.files.other.sb -side right -fill y
1173pack $ui_other -side left -fill both -expand 1
1174.vpane.files add .vpane.files.other -sticky nsew
1175
1176$ui_index tag conf in_diff -font [concat $mainfont bold]
1177$ui_other tag conf in_diff -font [concat $mainfont bold]
1178
1179# -- Diff Header
1180set ui_fname_value {}
1181set ui_fstatus_value {}
1182frame .vpane.diff -height 200 -width 400
1183frame .vpane.diff.header
1184label .vpane.diff.header.l1 -text {File:} -font $mainfont
1185label .vpane.diff.header.l2 -textvariable ui_fname_value \
1186 -anchor w \
1187 -justify left \
1188 -font $mainfont
1189label .vpane.diff.header.l3 -text {Status:} -font $mainfont
1190label .vpane.diff.header.l4 -textvariable ui_fstatus_value \
1191 -width $max_status_desc \
1192 -anchor w \
1193 -justify left \
1194 -font $mainfont
1195pack .vpane.diff.header.l1 -side left
1196pack .vpane.diff.header.l2 -side left -fill x
1197pack .vpane.diff.header.l4 -side right
1198pack .vpane.diff.header.l3 -side right
1199
1200# -- Diff Body
1201frame .vpane.diff.body
1202set ui_diff .vpane.diff.body.t
1203text $ui_diff -background white -borderwidth 0 \
1204 -width 80 -height 15 -wrap none \
1205 -font $difffont \
1206 -xscrollcommand {.vpane.diff.body.sbx set} \
1207 -yscrollcommand {.vpane.diff.body.sby set} \
1208 -cursor $maincursor \
1209 -state disabled
1210scrollbar .vpane.diff.body.sbx -orient horizontal \
1211 -command [list $ui_diff xview]
1212scrollbar .vpane.diff.body.sby -orient vertical \
1213 -command [list $ui_diff yview]
1214pack .vpane.diff.body.sbx -side bottom -fill x
1215pack .vpane.diff.body.sby -side right -fill y
1216pack $ui_diff -side left -fill both -expand 1
1217pack .vpane.diff.header -side top -fill x
1218pack .vpane.diff.body -side bottom -fill both -expand 1
1219.vpane add .vpane.diff -stick nsew
1220
1221$ui_diff tag conf dm -foreground red
1222$ui_diff tag conf dp -foreground blue
1223$ui_diff tag conf da -font [concat $difffont bold]
1224$ui_diff tag conf di -foreground "#00a000"
1225$ui_diff tag conf dni -foreground "#a000a0"
1226$ui_diff tag conf bold -font [concat $difffont bold]
1227
1228# -- Commit Area
1229frame .vpane.commarea -height 170
1230.vpane add .vpane.commarea -stick nsew
1231
1232# -- Commit Area Buttons
1233frame .vpane.commarea.buttons
1234label .vpane.commarea.buttons.l -text {} \
1235 -anchor w \
1236 -justify left \
1237 -font $mainfont
1238pack .vpane.commarea.buttons.l -side top -fill x
1239pack .vpane.commarea.buttons -side left -fill y
1240
1241button .vpane.commarea.buttons.rescan -text {Rescan} \
1242 -command do_rescan \
1243 -font $mainfont
1244pack .vpane.commarea.buttons.rescan -side top -fill x
1245lappend disable_on_lock {.vpane.commarea.buttons.rescan conf -state}
1246
1247button .vpane.commarea.buttons.amend -text {Amend Last} \
1248 -command do_amend_last \
1249 -font $mainfont
1250pack .vpane.commarea.buttons.amend -side top -fill x
1251lappend disable_on_lock {.vpane.commarea.buttons.amend conf -state}
1252
1253button .vpane.commarea.buttons.ciall -text {Check-in All} \
1254 -command do_checkin_all \
1255 -font $mainfont
1256pack .vpane.commarea.buttons.ciall -side top -fill x
1257lappend disable_on_lock {.vpane.commarea.buttons.ciall conf -state}
1258
1259button .vpane.commarea.buttons.signoff -text {Sign Off} \
1260 -command do_signoff \
1261 -font $mainfont
1262pack .vpane.commarea.buttons.signoff -side top -fill x
1263
1264button .vpane.commarea.buttons.commit -text {Commit} \
1265 -command do_commit \
1266 -font $mainfont
1267pack .vpane.commarea.buttons.commit -side top -fill x
1268lappend disable_on_lock {.vpane.commarea.buttons.commit conf -state}
1269
1270# -- Commit Message Buffer
1271frame .vpane.commarea.buffer
1272set ui_comm .vpane.commarea.buffer.t
1273set ui_coml .vpane.commarea.buffer.l
1274label $ui_coml -text {Commit Message:} \
1275 -anchor w \
1276 -justify left \
1277 -font $mainfont
1278trace add variable commit_type write {uplevel #0 {
1279 switch -glob $commit_type \
1280 initial {$ui_coml conf -text {Initial Commit Message:}} \
1281 amend {$ui_coml conf -text {Amended Commit Message:}} \
1282 merge {$ui_coml conf -text {Merge Commit Message:}} \
1283 * {$ui_coml conf -text {Commit Message:}}
1284}}
1285text $ui_comm -background white -borderwidth 1 \
1286 -relief sunken \
1287 -width 75 -height 10 -wrap none \
1288 -font $difffont \
1289 -yscrollcommand {.vpane.commarea.buffer.sby set} \
1290 -cursor $maincursor
1291scrollbar .vpane.commarea.buffer.sby -command [list $ui_comm yview]
1292pack $ui_coml -side top -fill x
1293pack .vpane.commarea.buffer.sby -side right -fill y
1294pack $ui_comm -side left -fill y
1295pack .vpane.commarea.buffer -side left -fill y
1296
1297# -- Status Bar
1298set ui_status_value {Initializing...}
1299label .status -textvariable ui_status_value \
1300 -anchor w \
1301 -justify left \
1302 -borderwidth 1 \
1303 -relief sunken \
1304 -font $mainfont
1305pack .status -anchor w -side bottom -fill x
1306
1307# -- Key Bindings
1308bind $ui_comm <$M1B-Key-Return> {do_commit;break}
1309bind . <Destroy> do_quit
1310bind . <Key-F5> do_rescan
1311bind . <$M1B-Key-r> do_rescan
1312bind . <$M1B-Key-R> do_rescan
1313bind . <$M1B-Key-s> do_signoff
1314bind . <$M1B-Key-S> do_signoff
1315bind . <$M1B-Key-u> do_checkin_all
1316bind . <$M1B-Key-U> do_checkin_all
1317bind . <$M1B-Key-Return> do_commit
1318bind . <$M1B-Key-q> do_quit
1319bind . <$M1B-Key-Q> do_quit
1320foreach i [list $ui_index $ui_other] {
1321 bind $i <Button-1> {click %W %x %y 1 %X %Y; break}
1322 bind $i <Button-3> {click %W %x %y 3 %X %Y; break}
1323 bind $i <ButtonRelease-1> {unclick %W %x %y; break}
1324}
1325unset i M1B M1T
1326
1327######################################################################
1328##
1329## main
1330
1331if {[catch {set gitdir [exec git rev-parse --git-dir]} err]} {
1332 show_msg {} . "Cannot find the git directory: $err"
1333 exit 1
1334}
1335set cdup [exec git rev-parse --show-cdup]
1336if {$cdup != ""} {
1337 cd $cdup
1338}
1339unset cdup
1340
1341set appname [lindex [file split $argv0] end]
1342if {$appname == {git-citool}} {
1343 set single_commit 1
1344}
1345
1346wm title . "$appname ([file normalize [file dirname $gitdir]])"
1347focus -force $ui_comm
1348update_status