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

git-gui: Finished commit implementation.

We can now commit any type of commit (initial, normal or merge) using the same techniques as git-commit.sh does for these types of things. If invoked as git-citool we run exit immediately after the commit was finished. If invoked as git-gui then we stay running. Also fixed a bug which caused the commit message buffer to be lost when the application shutdown and restarted. Signed-off-by: Shawn O. Pearce <spearce@spearce.org>

Shawn O. Pearce committed Nov 6, 2006 at 20:50 UTC ec6b424abb8d0a5c6399bcffdfde19aa47f16d18
2 files changed +238 -102
git-citool new
+1
@@ -0,0 +1 @@
1 +git-gui
\ No newline at end of file
git-gui
+237 -102
@@ -11,9 +11,11 @@ exec wish "$0" -- "$@"
11 ##
12 ## task management
13
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 {}
20
21 set disable_on_lock [list]
@@ -48,13 +50,27 @@ proc unlock_index {} {
50 ##
51 ## status
52
53 +proc repository_state {hdvar ctvar} {
54 + global gitdir
55 + upvar $hdvar hd $ctvar ct
56 +
57 + if {[catch {set hd [exec git rev-parse --verify HEAD]}]} {
58 + set ct initial
59 + } elseif {[file exists [file join $gitdir MERGE_HEAD]]} {
60 + set ct merge
61 + } else {
62 + set ct normal
63 + }
64 +}
65 +
66 proc update_status {} {
52 - global gitdir HEAD commit_type
67 + global HEAD commit_type
68 global ui_index ui_other ui_status_value ui_comm
69 global status_active file_states
70
71 if {$status_active || ![lock_index read]} return
72
73 + repository_state HEAD commit_type
74 array unset file_states
75 foreach w [list $ui_index $ui_other] {
76 $w conf -state normal
@@ -62,12 +78,6 @@ proc update_status {} {
78 $w conf -state disabled
79 }
80
65 - if {[catch {set HEAD [exec git rev-parse --verify HEAD]}]} {
66 - set commit_type initial
67 - } else {
68 - set commit_type normal
69 - }
70 -
81 if {![$ui_comm edit modified]
82 || [string trim [$ui_comm get 0.0 end]] == {}} {
83 if {[load_message GITGUI_MSG]} {
@@ -306,6 +316,207 @@ proc read_diff {fd} {
316 }
317 }
318
319 +######################################################################
320 +##
321 +## commit
322 +
323 +proc commit_tree {} {
324 + global tcl_platform HEAD gitdir commit_type file_states
325 + global commit_active ui_status_value
326 + global ui_comm
327 +
328 + if {$commit_active || ![lock_index update]} return
329 +
330 + # -- Our in memory state should match the repository.
331 + #
332 + repository_state curHEAD cur_type
333 + if {$commit_type != $cur_type || $HEAD != $curHEAD} {
334 + error_popup {Last scanned state does not match repository state.
335 +
336 +Its highly likely that another Git program modified the
337 +repository since our last scan. A rescan is required
338 +before committing.
339 +}
340 + unlock_index
341 + update_status
342 + return
343 + }
344 +
345 + # -- At least one file should differ in the index.
346 + #
347 + set files_ready 0
348 + foreach path [array names file_states] {
349 + set s $file_states($path)
350 + switch -glob -- [lindex $s 0] {
351 + _* {continue}
352 + A* -
353 + D* -
354 + M* {set files_ready 1; break}
355 + U* {
356 + error_popup "Unmerged files cannot be committed.
357 +
358 +File $path has merge conflicts.
359 +You must resolve them and check the file in before committing.
360 +"
361 + unlock_index
362 + return
363 + }
364 + default {
365 + error_popup "Unknown file state [lindex $s 0] detected.
366 +
367 +File $path cannot be committed by this program.
368 +"
369 + }
370 + }
371 + }
372 + if {!$files_ready} {
373 + error_popup {No checked-in files to commit.
374 +
375 +You must check-in at least 1 file before you can commit.
376 +}
377 + unlock_index
378 + return
379 + }
380 +
381 + # -- A message is required.
382 + #
383 + set msg [string trim [$ui_comm get 1.0 end]]
384 + if {$msg == {}} {
385 + error_popup {Please supply a commit message.
386 +
387 +A good commit message has the following format:
388 +
389 +- First line: Describe in one sentance what you did.
390 +- Second line: Blank
391 +- Remaining lines: Describe why this change is good.
392 +}
393 + unlock_index
394 + return
395 + }
396 +
397 + # -- Ask the pre-commit hook for the go-ahead.
398 + #
399 + set pchook [file join $gitdir hooks pre-commit]
400 + if {$tcl_platform(platform) == {windows} && [file exists $pchook]} {
401 + set pchook [list sh -c \
402 + "if test -x \"$pchook\"; then exec \"$pchook\"; fi"]
403 + } elseif {[file executable $pchook]} {
404 + set pchook [list $pchook]
405 + } else {
406 + set pchook {}
407 + }
408 + if {$pchook != {} && [catch {eval exec $pchook} err]} {
409 + hook_failed_popup pre-commit $err
410 + unlock_index
411 + return
412 + }
413 +
414 + # -- Write the tree in the background.
415 + #
416 + set commit_active 1
417 + set ui_status_value {Committing changes...}
418 +
419 + set fd_wt [open "| git write-tree" r]
420 + fileevent $fd_wt readable \
421 + [list commit_stage2 $fd_wt $curHEAD $msg]
422 +}
423 +
424 +proc commit_stage2 {fd_wt curHEAD msg} {
425 + global single_commit gitdir HEAD commit_type
426 + global commit_active ui_status_value comm_ui
427 +
428 + gets $fd_wt tree_id
429 + close $fd_wt
430 +
431 + if {$tree_id == {}} {
432 + error_popup "write-tree failed"
433 + set commit_active 0
434 + set ui_status_value {Commit failed.}
435 + unlock_index
436 + return
437 + }
438 +
439 + # -- Create the commit.
440 + #
441 + set cmd [list git commit-tree $tree_id]
442 + if {$commit_type != {initial}} {
443 + lappend cmd -p $HEAD
444 + }
445 + if {$commit_type == {merge}} {
446 + if {[catch {
447 + set fd_mh [open [file join $gitdir MERGE_HEAD] r]
448 + while {[gets $fd_mh merge_head] > 0} {
449 + lappend -p $merge_head
450 + }
451 + close $fd_mh
452 + } err]} {
453 + error_popup "Loading MERGE_HEADs failed:\n$err"
454 + set commit_active 0
455 + set ui_status_value {Commit failed.}
456 + unlock_index
457 + return
458 + }
459 + }
460 + if {$commit_type == {initial}} {
461 + # git commit-tree writes to stderr during initial commit.
462 + lappend cmd 2>/dev/null
463 + }
464 + lappend cmd << $msg
465 + if {[catch {set cmt_id [eval exec $cmd]} err]} {
466 + error_popup "commit-tree failed:\n$err"
467 + set commit_active 0
468 + set ui_status_value {Commit failed.}
469 + unlock_index
470 + return
471 + }
472 +
473 + # -- Update the HEAD ref.
474 + #
475 + set reflogm commit
476 + if {$commit_type != {normal}} {
477 + append reflogm " ($commit_type)"
478 + }
479 + set i [string first "\n" $msg]
480 + if {$i >= 0} {
481 + append reflogm {: } [string range $msg 0 [expr $i - 1]]
482 + } else {
483 + append reflogm {: } $msg
484 + }
485 + set cmd [list git update-ref \
486 + -m $reflogm \
487 + HEAD $cmt_id $curHEAD]
488 + if {[catch {eval exec $cmd} err]} {
489 + error_popup "update-ref failed:\n$err"
490 + set commit_active 0
491 + set ui_status_value {Commit failed.}
492 + unlock_index
493 + return
494 + }
495 +
496 + # -- Cleanup after ourselves.
497 + #
498 + catch {file delete [file join $gitdir MERGE_HEAD]}
499 + catch {file delete [file join $gitdir MERGE_MSG]}
500 + catch {file delete [file join $gitdir SQUASH_MSG]}
501 + catch {file delete [file join $gitdir GITGUI_MSG]}
502 +
503 + # -- Let rerere do its thing.
504 + #
505 + if {[file isdirectory [file join $gitdir rr-cache]]} {
506 + catch {exec git rerere}
507 + }
508 +
509 + $comm_ui delete 0.0 end
510 + $comm_ui edit modified false
511 +
512 + if {$single_commit} do_quit
513 +
514 + set commit_active 0
515 + set ui_status_value "Changes committed as $cmt_id."
516 + unlock_index
517 + update_status
518 +}
519 +
520 ######################################################################
521 ##
522 ## ui helpers
@@ -599,20 +810,22 @@ proc error_popup {msg} {
810 }
811
812 proc show_msg {w top msg} {
602 - global gitdir
813 + global gitdir appname
814
815 message $w.m -text $msg -justify left -aspect 400
605 - pack $w.m -side top -fill x -padx 20 -pady 20
606 - button $w.ok -text OK -command "destroy $top"
816 + pack $w.m -side top -fill x -padx 5 -pady 10
817 + button $w.ok -text OK \
818 + -width 15 \
819 + -command "destroy $top"
820 pack $w.ok -side bottom
821 bind $top <Visibility> "grab $top; focus $top"
822 bind $top <Key-Return> "destroy $top"
610 - wm title $top "error: git-ui ([file normalize [file dirname $gitdir]])"
823 + wm title $top "error: $appname ([file normalize [file dirname $gitdir]])"
824 tkwait window $top
825 }
826
827 proc hook_failed_popup {hook msg} {
615 - global gitdir mainfont difffont
828 + global gitdir mainfont difffont appname
829
830 set w .hookfail
831 toplevel $w
@@ -651,7 +864,7 @@ proc hook_failed_popup {hook msg} {
864
865 bind $w <Visibility> "grab $w; focus $w"
866 bind $w <Key-Return> "destroy $w"
654 - wm title $w "error: git-ui ([file normalize [file dirname $gitdir]])"
867 + wm title $w "error: $appname ([file normalize [file dirname $gitdir]])"
868 tkwait window $w
869 }
870
@@ -681,14 +894,14 @@ proc do_quit {} {
894 global gitdir ui_comm
895
896 set save [file join $gitdir GITGUI_MSG]
684 - if {[$ui_comm edit modified]
685 - && [string trim [$ui_comm get 0.0 end]] != {}} {
897 + set msg [string trim [$ui_comm get 0.0 end]]
898 + if {[$ui_comm edit modified] && $msg != {}} {
899 catch {
900 set fd [open $save w]
901 puts $fd [string trim [$ui_comm get 0.0 end]]
902 close $fd
903 }
691 - } elseif {[file exists $save]} {
904 + } elseif {$msg == {} && [file exists $save]} {
905 file delete $save
906 }
907
@@ -741,91 +954,7 @@ proc do_signoff {} {
954 }
955
956 proc do_commit {} {
744 - global tcl_platform HEAD gitdir commit_type file_states
745 - global ui_comm
746 -
747 - # -- Our in memory state should match the repository.
748 - #
749 - if {[catch {set curHEAD [exec git rev-parse --verify HEAD]}]} {
750 - set cur_type initial
751 - } else {
752 - set cur_type normal
753 - }
754 - if {$commit_type != $commit_type || $HEAD != $curHEAD} {
755 - error_popup {Last scanned state does not match repository state.
756 -
757 -Its highly likely that another Git program modified the
758 -repository since our last scan. A rescan is required
759 -before committing.
760 -}
761 - update_status
762 - return
763 - }
764 -
765 - # -- At least one file should differ in the index.
766 - #
767 - set files_ready 0
768 - foreach path [array names file_states] {
769 - set s $file_states($path)
770 - switch -glob -- [lindex $s 0] {
771 - _* {continue}
772 - A* -
773 - D* -
774 - M* {set files_ready 1; break}
775 - U* {
776 - error_popup "Unmerged files cannot be committed.
777 -
778 -File $path has merge conflicts.
779 -You must resolve them and check the file in before committing.
780 -"
781 - return
782 - }
783 - default {
784 - error_popup "Unknown file state [lindex $s 0] detected.
785 -
786 -File $path cannot be committed by this program.
787 -"
788 - }
789 - }
790 - }
791 - if {!$files_ready} {
792 - error_popup {No checked-in files to commit.
793 -
794 -You must check-in at least 1 file before you can commit.
795 -}
796 - return
797 - }
798 -
799 - # -- A message is required.
800 - #
801 - set msg [string trim [$ui_comm get 1.0 end]]
802 - if {$msg == {}} {
803 - error_popup {Please supply a commit message.
804 -
805 -A good commit message has the following format:
806 -
807 -- First line: Describe in one sentance what you did.
808 -- Second line: Blank
809 -- Remaining lines: Describe why this change is good.
810 -}
811 - return
812 - }
813 -
814 - # -- Ask the pre-commit hook for the go-ahead.
815 - #
816 - set pchook [file join $gitdir hooks pre-commit]
817 - if {$tcl_platform(platform) == {windows} && [file exists $pchook]} {
818 - set pchook [list sh -c \
819 - "if test -x \"$pchook\"; then exec \"$pchook\"; fi"]
820 - } elseif {[file executable $pchook]} {
821 - set pchook [list $pchook]
822 - } else {
823 - set pchook {}
824 - }
825 - if {$pchook != {} && [catch {eval exec $pchook} err]} {
826 - hook_failed_popup pre-commit $err
827 - return
828 - }
957 + commit_tree
958 }
959
960 # shift == 1: left click
@@ -1081,6 +1210,7 @@ label .status -textvariable ui_status_value \
1210 pack .status -anchor w -side bottom -fill x
1211
1212 # -- Key Bindings
1213 +bind $ui_comm <$M1B-Key-Return> {do_commit;break}
1214 bind . <Destroy> do_quit
1215 bind . <Key-F5> do_rescan
1216 bind . <$M1B-Key-r> do_rescan
@@ -1108,6 +1238,11 @@ if {[catch {set gitdir [exec git rev-parse --git-dir]} err]} {
1238 exit 1
1239 }
1240
1111 -wm title . "git-ui ([file normalize [file dirname $gitdir]])"
1241 +set appname [lindex [file split $argv0] end]
1242 +if {$appname == {git-citool}} {
1243 + set single_commit 1
1244 +}
1245 +
1246 +wm title . "$appname ([file normalize [file dirname $gitdir]])"
1247 focus -force $ui_comm
1248 update_status