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

Fix overzealous cleanliness check in git-merge

Being able to try multiple strategies and automatically picking one that seems to give less conflicting result may or may not much sense in practice. At least that should not force normal use case to additionally require the working tree to be fully clean. As Linus shouted, local changes do not matter unless they interfere with the merge. This commit changes git-merge not to require a clean working tree. Only when we will iterate through more than one merge strategies, local changes are stashed away before trying the first merge, and restored before second and later merges are attempted. The index file must be in sync with HEAD in any case -- otherwise the merge result would contain changes since HEAD that was done locally and registered in the index. This check is already enforced by three-way read-tree existing merge strategies use, but is done here as a safeguard as well. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Sep 23, 2005 at 00:43 UTC a935824036dc2c5a8ef1980a4b05a12228712754
1 file changed +50 -18
git-merge.sh
+50 -18
@@ -18,8 +18,19 @@ all_strategies='recursive octopus resolve stupid'
18 default_strategies='resolve octopus'
19 use_strategies=
20
21 -dropheads() {
22 - rm -f -- "$GIT_DIR/MERGE_HEAD" || exit 1
21 +dropsave() {
22 + rm -f -- "$GIT_DIR/MERGE_HEAD" \
23 + "$GIT_DIR/MERGE_SAVE" || exit 1
24 +}
25 +
26 +savestate() {
27 + git diff -r -z --name-only $head | cpio -0 -o >"$GIR_DIR/MERGE_SAVE"
28 +}
29 +
30 +restorestate() {
31 + git reset --hard $head
32 + cpio -iuv <"$GIT_DIR/MERGE_SAVE"
33 + git-update-index --refresh >/dev/null
34 }
35
36 summary() {
@@ -93,7 +104,7 @@ case "$#,$common" in
104 # If head can reach all the merge then we are up to date.
105 # but first the most common case of merging one remote
106 echo "Already up-to-date. Yeeah!"
96 - dropheads
107 + dropsave
108 exit 0
109 ;;
110 1,"$head")
@@ -103,7 +114,7 @@ case "$#,$common" in
114 git-read-tree -u -m $head "$1" || exit 1
115 git-rev-parse --verify "$1^0" > "$GIT_DIR/HEAD"
116 summary "$1"
106 - dropheads
117 + dropsave
118 exit 0
119 ;;
120 1,*)
@@ -125,30 +136,51 @@ case "$#,$common" in
136 if test "$up_to_date" = t
137 then
138 echo "Already up-to-date. Yeeah!"
128 - dropheads
139 + dropsave
140 exit 0
141 fi
142 ;;
143 esac
144
134 -# At this point we need a real merge. Require that the tree matches
135 -# exactly our head.
145 +# At this point, we need a real merge. No matter what strategy
146 +# we use, it would operate on the index, possibly affecting the
147 +# working tree, and when resolved cleanly, have the desired tree
148 +# in the index -- this means that the index must be in sync with
149 +# the $head commit.
150 +files=$(git-diff-index --cached --name-only $head) || exit
151 +if [ "$files" ]; then
152 + echo >&2 "Dirty index: cannot merge (dirty: $files)"
153 + exit 1
154 +fi
155
137 -git-update-index --refresh &&
138 -test '' = "`git-diff-index --cached --name-only $head`" || {
139 - die "Need real merge but the working tree has local changes."
140 -}
156 +case "$use_strategies" in
157 +?*' '?*)
158 + # Stash away the local changes so that we can try more than one.
159 + savestate
160 + single_strategy=no
161 + ;;
162 +*)
163 + single_strategy=yes
164 + ;;
165 +esac
166
167 result_tree= best_cnt=-1 best_strategy= wt_strategy=
168 for strategy in $use_strategies
169 do
170 test "$wt_strategy" = '' || {
171 echo "Rewinding the tree to pristine..."
147 - git reset --hard $head
172 + restorestate
173 }
149 - echo "Trying merge strategy $strategy..."
174 + case "$single_strategy" in
175 + no)
176 + echo "Trying merge strategy $strategy..."
177 + ;;
178 + esac
179 +
180 + # Remember which strategy left the state in the working tree
181 wt_strategy=$strategy
151 - git-merge-$strategy $common -- $head_arg "$@" || {
182 +
183 + git-merge-$strategy $common -- "$head_arg" "$@" || {
184
185 # The backend exits with 1 when conflicts are left to be resolved,
186 # with 2 when it does not handle the given merge at all.
@@ -186,14 +218,14 @@ then
218 echo "Committed merge $result_commit, made by $wt_strategy."
219 echo $result_commit >"$GIT_DIR/HEAD"
220 summary $result_commit
189 - dropheads
221 + dropsave
222 exit 0
223 fi
224
225 # Pick the result from the best strategy and have the user fix it up.
226 case "$best_strategy" in
227 '')
196 - git reset --hard $head
228 + restorestate
229 die "No merge strategy handled the merge."
230 ;;
231 "$wt_strategy")
@@ -201,9 +233,9 @@ case "$best_strategy" in
233 ;;
234 *)
235 echo "Rewinding the tree to pristine..."
204 - git reset --hard $head
236 + restorestate
237 echo "Using the $best_strategy to prepare resolving by hand."
206 - git-merge-$best_strategy $common -- $head_arg "$@"
238 + git-merge-$best_strategy $common -- "$head_arg" "$@"
239 ;;
240 esac
241 for remote