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

git-gui: Improve handling of merge commits.

Its useful to be able to amend the last commit even if it was a merge commit, so we really should support that in the gui. We now do so by making PARENT a list. We always diff against the first parent but we create a commit consisting of the parent(s) listed in this list, in order. We also should recheck the repository state during an amend. Earlier I was bitten by this exact bug when I switched branches through a command prompt and then did not do a rescan in git-gui. When I hit "Amend Last Commit" I was surprised to see information from the prior branch appear. This was due to git-gui caching the data from the last rescan and using that data form the amend data load request, rather than the data of the current branch. Improved error text in the dialogs used to tell the user why an amend is being refused by git-gui. In general this is only during an initial commit (nothing prior to amend) and during a merge commit (it is simply too confusing to amend the last commit while also trying to complete a merge). Fixed a couple of minor bugs in the pull logic. Since this code isn't really useful nobody has recently tested it and noticed the breakage. It really needs to be rewritten anyway. Signed-off-by: Shawn O. Pearce <spearce@spearce.org>

Shawn O. Pearce committed Nov 20, 2006 at 21:27 UTC f18e40a1a60d506065b6cc0e45704ce29ea0a035
1 file changed +78 -63
git-gui
+78 -63
@@ -231,25 +231,38 @@ proc unlock_index {} {
231 ##
232 ## status
233
234 -proc repository_state {hdvar ctvar} {
234 +proc repository_state {ctvar hdvar mhvar} {
235 global gitdir
236 - upvar $hdvar hd $ctvar ct
236 + upvar $ctvar ct $hdvar hd $mhvar mh
237 +
238 + set mh [list]
239
240 if {[catch {set hd [exec git rev-parse --verify HEAD]}]} {
241 set hd {}
242 set ct initial
241 - } elseif {[file exists [file join $gitdir MERGE_HEAD]]} {
243 + return
244 + }
245 +
246 + set merge_head [file join $gitdir MERGE_HEAD]
247 + if {[file exists $merge_head]} {
248 set ct merge
243 - } else {
244 - set ct normal
249 + set fd_mh [open $merge_head r]
250 + while {[gets $fd_mh line] >= 0} {
251 + lappend mh $line
252 + }
253 + close $fd_mh
254 + return
255 }
256 +
257 + set ct normal
258 }
259
260 proc PARENT {} {
261 global PARENT empty_tree
262
251 - if {$PARENT ne {}} {
252 - return $PARENT
263 + set p [lindex $PARENT 0]
264 + if {$p ne {}} {
265 + return $p
266 }
267 if {$empty_tree eq {}} {
268 set empty_tree [exec git mktree << {}]
@@ -258,21 +271,22 @@ proc PARENT {} {
271 }
272
273 proc rescan {after} {
261 - global HEAD PARENT commit_type
274 + global HEAD PARENT MERGE_HEAD commit_type
275 global ui_index ui_other ui_status_value ui_comm
276 global rescan_active file_states
277 global repo_config
278
279 if {$rescan_active > 0 || ![lock_index read]} return
280
268 - repository_state new_HEAD new_type
281 + repository_state newType newHEAD newMERGE_HEAD
282 if {[string match amend* $commit_type]
270 - && $new_type eq {normal}
271 - && $new_HEAD eq $HEAD} {
283 + && $newType eq {normal}
284 + && $newHEAD eq $HEAD} {
285 } else {
273 - set HEAD $new_HEAD
274 - set PARENT $new_HEAD
275 - set commit_type $new_type
286 + set HEAD $newHEAD
287 + set PARENT $newHEAD
288 + set MERGE_HEAD $newMERGE_HEAD
289 + set commit_type $newType
290 }
291
292 array unset file_states
@@ -686,23 +700,36 @@ proc read_diff {fd} {
700 ## commit
701
702 proc load_last_commit {} {
689 - global HEAD PARENT commit_type ui_comm
703 + global HEAD PARENT MERGE_HEAD commit_type ui_comm
704
691 - if {[string match amend* $commit_type]} return
692 - if {$commit_type ne {normal}} {
693 - error_popup "Can't amend a $commit_type commit."
705 + if {[llength $PARENT] == 0} {
706 + error_popup {There is nothing to amend.
707 +
708 +You are about to create the initial commit.
709 +There is no commit before this to amend.
710 +}
711 + return
712 + }
713 +
714 + repository_state curType curHEAD curMERGE_HEAD
715 + if {$curType eq {merge}} {
716 + error_popup {Cannot amend while merging.
717 +
718 +You are currently in the middle of a merge that
719 +has not been fully completed. You cannot amend
720 +the prior commit unless you first abort the
721 +current merge activity.
722 +}
723 return
724 }
725
726 set msg {}
698 - set parent {}
699 - set parent_count 0
727 + set parents [list]
728 if {[catch {
701 - set fd [open "| git cat-file commit $HEAD" r]
729 + set fd [open "| git cat-file commit $curHEAD" r]
730 while {[gets $fd line] > 0} {
731 if {[string match {parent *} $line]} {
704 - set parent [string range $line 7 end]
705 - incr parent_count
732 + lappend parents [string range $line 7 end]
733 }
734 }
735 set msg [string trim [read $fd]]
@@ -712,17 +739,13 @@ proc load_last_commit {} {
739 return
740 }
741
715 - if {$parent_count > 1} {
716 - error_popup {Can't amend a merge commit.}
717 - return
718 - }
719 -
720 - if {$parent_count == 0} {
721 - set commit_type amend-initial
722 - set PARENT {}
723 - } elseif {$parent_count == 1} {
724 - set commit_type amend
725 - set PARENT $parent
742 + set HEAD $curHEAD
743 + set PARENT $parents
744 + set MERGE_HEAD [list]
745 + switch -- [llength $parents] {
746 + 0 {set commit_type amend-initial}
747 + 1 {set commit_type amend}
748 + default {set commit_type amend-merge}
749 }
750
751 $ui_comm delete 0.0 end
@@ -770,11 +793,11 @@ proc commit_tree {} {
793
794 # -- Our in memory state should match the repository.
795 #
773 - repository_state curHEAD cur_type
796 + repository_state curType curHEAD curMERGE_HEAD
797 if {[string match amend* $commit_type]
775 - && $cur_type eq {normal}
798 + && $curType eq {normal}
799 && $curHEAD eq $HEAD} {
777 - } elseif {$commit_type ne $cur_type || $HEAD ne $curHEAD} {
800 + } elseif {$commit_type ne $curType || $HEAD ne $curHEAD} {
801 info_popup {Last scanned state does not match repository state.
802
803 Another Git program has modified this repository
@@ -920,7 +943,8 @@ proc commit_writetree {curHEAD msg} {
943 }
944
945 proc commit_committree {fd_wt curHEAD msg} {
923 - global single_commit gitdir HEAD PARENT commit_type tcl_platform
946 + global HEAD PARENT MERGE_HEAD commit_type
947 + global single_commit gitdir tcl_platform
948 global ui_status_value ui_comm selected_commit_type
949 global file_states selected_paths rescan_active
950
@@ -935,24 +959,12 @@ proc commit_committree {fd_wt curHEAD msg} {
959 # -- Create the commit.
960 #
961 set cmd [list git commit-tree $tree_id]
938 - if {$PARENT ne {}} {
939 - lappend cmd -p $PARENT
940 - }
941 - if {$commit_type eq {merge}} {
942 - if {[catch {
943 - set fd_mh [open [file join $gitdir MERGE_HEAD] r]
944 - while {[gets $fd_mh merge_head] >= 0} {
945 - lappend cmd -p $merge_head
946 - }
947 - close $fd_mh
948 - } err]} {
949 - error_popup "Loading MERGE_HEAD failed:\n\n$err"
950 - set ui_status_value {Commit failed.}
951 - unlock_index
952 - return
962 + set parents [concat $PARENT $MERGE_HEAD]
963 + if {[llength $parents] > 0} {
964 + foreach p $parents {
965 + lappend cmd -p $p
966 }
954 - }
955 - if {$PARENT eq {}} {
967 + } else {
968 # git commit-tree writes to stderr during initial commit.
969 lappend cmd 2>/dev/null
970 }
@@ -1020,10 +1032,11 @@ proc commit_committree {fd_wt curHEAD msg} {
1032
1033 # -- Update in memory status
1034 #
1023 - set commit_type normal
1035 set selected_commit_type new
1036 + set commit_type normal
1037 set HEAD $cmt_id
1038 set PARENT $cmt_id
1039 + set MERGE_HEAD [list]
1040
1041 foreach path [array names file_states] {
1042 set s $file_states($path)
@@ -1081,8 +1094,8 @@ proc pull_remote {remote branch} {
1094
1095 # -- Our in memory state should match the repository.
1096 #
1084 - repository_state curHEAD cur_type
1085 - if {$commit_type ne $cur_type || $HEAD ne $curHEAD} {
1097 + repository_state curType curHEAD curMERGE_HEAD
1098 + if {$commit_type ne $curType || $HEAD ne $curHEAD} {
1099 error_popup {Last scanned state does not match repository state.
1100
1101 Its highly likely that another Git program modified the
@@ -1120,18 +1133,18 @@ Commit or throw away all changes before starting a pull operation.
1133 }
1134
1135 proc post_pull_remote {remote branch success} {
1123 - global HEAD PARENT commit_type selected_commit_type
1136 + global HEAD PARENT MERGE_HEAD commit_type selected_commit_type
1137 global ui_status_value
1138
1139 unlock_index
1140 if {$success} {
1128 - repository_state HEAD commit_type
1141 + repository_state commit_type HEAD MERGE_HEAD
1142 set PARENT $HEAD
1143 set selected_commit_type new
1131 - set $ui_status_value "Pulling $branch from $remote complete."
1144 + set ui_status_value "Pulling $branch from $remote complete."
1145 } else {
1133 - set m "Conflicts detected while pulling $branch from $remote."
1134 - rescan "set ui_status_value {$m}"
1146 + rescan [list set ui_status_value \
1147 + "Conflicts detected while pulling $branch from $remote."]
1148 }
1149 }
1150
@@ -2852,6 +2865,7 @@ proc trace_commit_type {varname args} {
2865 initial {set txt {Initial Commit Message:}}
2866 amend {set txt {Amended Commit Message:}}
2867 amend-initial {set txt {Amended Initial Commit Message:}}
2868 + amend-merge {set txt {Amended Merge Commit Message:}}
2869 merge {set txt {Merge Commit Message:}}
2870 * {set txt {Commit Message:}}
2871 }
@@ -3146,6 +3160,7 @@ set file_lists($ui_other) [list]
3160
3161 set HEAD {}
3162 set PARENT {}
3163 +set MERGE_HEAD [list]
3164 set commit_type {}
3165 set empty_tree {}
3166 set current_diff {}