@matheusbsbs / matheus-bagliano / commits / 6e27d826c8
Fork 0

git-gui: Verify we should actually perform a commit when asked to do so.

A user shouldn't perform a commit if any of the following are true: * The repository state has changed since the last rescan. * There are no files updated in the index to commit. * There are unmerged stages still in the index. * The commit message has not been provided. * The pre-commit hook is executable and declined. Signed-off-by: Shawn O. Pearce <spearce@spearce.org>

Shawn O. Pearce committed Nov 6, 2006 at 20:03 UTC 6e27d826c807153a4773f197e5056d66a6a809c0
1 file changed +137 -2
git-gui
+137 -2
@@ -599,15 +599,62 @@ proc error_popup {msg} {
599 }
600
601 proc show_msg {w top msg} {
602 - message $w.m -text $msg -justify center -aspect 400
602 + global gitdir
603 +
604 + 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"
605 - pack $w.ok -side bottom -fill x
607 + pack $w.ok -side bottom
608 bind $top <Visibility> "grab $top; focus $top"
609 bind $top <Key-Return> "destroy $top"
610 + wm title $top "error: git-ui ([file normalize [file dirname $gitdir]])"
611 tkwait window $top
612 }
613
614 +proc hook_failed_popup {hook msg} {
615 + global gitdir mainfont difffont
616 +
617 + set w .hookfail
618 + toplevel $w
619 + wm transient $w .
620 +
621 + frame $w.m
622 + label $w.m.l1 -text "$hook hook failed:" \
623 + -anchor w \
624 + -justify left \
625 + -font [concat $mainfont bold]
626 + text $w.m.t \
627 + -background white -borderwidth 1 \
628 + -relief sunken \
629 + -width 80 -height 10 \
630 + -font $difffont \
631 + -yscrollcommand [list $w.m.sby set]
632 + label $w.m.l2 \
633 + -text {You must correct the above errors before committing.} \
634 + -anchor w \
635 + -justify left \
636 + -font [concat $mainfont bold]
637 + scrollbar $w.m.sby -command [list $w.m.t yview]
638 + pack $w.m.l1 -side top -fill x
639 + pack $w.m.l2 -side bottom -fill x
640 + pack $w.m.sby -side right -fill y
641 + pack $w.m.t -side left -fill both -expand 1
642 + pack $w.m -side top -fill both -expand 1 -padx 5 -pady 10
643 +
644 + $w.m.t insert 1.0 $msg
645 + $w.m.t conf -state disabled
646 +
647 + button $w.ok -text OK \
648 + -width 15 \
649 + -command "destroy $w"
650 + pack $w.ok -side bottom
651 +
652 + bind $w <Visibility> "grab $w; focus $w"
653 + bind $w <Key-Return> "destroy $w"
654 + wm title $w "error: git-ui ([file normalize [file dirname $gitdir]])"
655 + tkwait window $w
656 +}
657 +
658 ######################################################################
659 ##
660 ## ui commands
@@ -693,6 +740,94 @@ proc do_signoff {} {
740 }
741 }
742
743 +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 + }
829 +}
830 +
831 # shift == 1: left click
832 # 3: right click
833 proc click {w x y shift wx wy} {