]> git.ipfire.org Git - thirdparty/git.git/blame - git-merge.sh
Allow building of RPM from interim snapshot.
[thirdparty/git.git] / git-merge.sh
CommitLineData
91063bbc
JH
1#!/bin/sh
2#
3# Copyright (c) 2005 Junio C Hamano
4#
5
806f36d4
FK
6
7USAGE='[-n] [--no-commit] [-s <strategy>]... <merge-message> <head> <remote>+'
ae2b0f15 8. git-sh-setup
91063bbc
JH
9
10LF='
11'
12
64da9e60 13all_strategies='recursive octopus resolve stupid ours'
fbf8ac21 14default_strategies='recursive'
91063bbc
JH
15use_strategies=
16
a9358240 17dropsave() {
deca7e8c 18 rm -f -- "$GIT_DIR/MERGE_HEAD" "$GIT_DIR/MERGE_MSG" \
a9358240
JH
19 "$GIT_DIR/MERGE_SAVE" || exit 1
20}
21
22savestate() {
60fa0560 23 # Stash away any local modifications.
50b8e355 24 git-diff-index -z --name-only $head |
88f8f0a5 25 cpio -0 -o >"$GIT_DIR/MERGE_SAVE"
a9358240
JH
26}
27
28restorestate() {
deca7e8c
JH
29 if test -f "$GIT_DIR/MERGE_SAVE"
30 then
31 git reset --hard $head
32 cpio -iuv <"$GIT_DIR/MERGE_SAVE"
33 git-update-index --refresh >/dev/null
34 fi
91063bbc
JH
35}
36
4f692b19
JH
37finish () {
38 test '' = "$2" || echo "$2"
39 case "$merge_msg" in
40 '')
41 echo "No merge message -- not updating HEAD"
42 ;;
43 *)
44 git-update-ref HEAD "$1" "$head" || exit 1
45 ;;
46 esac
47
91063bbc
JH
48 case "$no_summary" in
49 '')
4f692b19 50 git-diff-tree -p -M "$head" "$1" |
91063bbc
JH
51 git-apply --stat --summary
52 ;;
53 esac
54}
55
56while case "$#" in 0) break ;; esac
57do
58 case "$1" in
59 -n|--n|--no|--no-|--no-s|--no-su|--no-sum|--no-summ|\
60 --no-summa|--no-summar|--no-summary)
61 no_summary=t ;;
123ee3ca
JH
62 --no-c|--no-co|--no-com|--no-comm|--no-commi|--no-commit)
63 no_commit=t ;;
91063bbc
JH
64 -s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\
65 --strateg=*|--strategy=*|\
66 -s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy)
67 case "$#,$1" in
68 *,*=*)
69 strategy=`expr "$1" : '-[^=]*=\(.*\)'` ;;
f88ed172 70 1,*)
91063bbc
JH
71 usage ;;
72 *)
73 strategy="$2"
74 shift ;;
75 esac
76 case " $all_strategies " in
77 *" $strategy "*)
78 use_strategies="$use_strategies$strategy " ;;
79 *)
80 die "available strategies are: $all_strategies" ;;
81 esac
82 ;;
83 -*) usage ;;
84 *) break ;;
85 esac
86 shift
87done
88
91063bbc
JH
89test "$#" -le 2 && usage ;# we need at least two heads.
90
91merge_msg="$1"
92shift
8cc01e50 93head_arg="$1"
91063bbc
JH
94head=$(git-rev-parse --verify "$1"^0) || usage
95shift
96
97# All the rest are remote heads
9954f5b8 98remoteheads=
91063bbc
JH
99for remote
100do
9954f5b8 101 remotehead=$(git-rev-parse --verify "$remote"^0) ||
91063bbc 102 die "$remote - not something we can merge"
9954f5b8 103 remoteheads="${remoteheads}$remotehead "
91063bbc 104done
9954f5b8 105set x $remoteheads ; shift
91063bbc 106
13956670
JH
107case "$#" in
1081)
109 common=$(git-merge-base --all $head "$@")
110 ;;
111*)
112 common=$(git-show-branch --merge-base $head "$@")
113 ;;
114esac
91063bbc
JH
115echo "$head" >"$GIT_DIR/ORIG_HEAD"
116
123ee3ca
JH
117case "$#,$common,$no_commit" in
118*,'',*)
88f8f0a5 119 # No common ancestors found. We need a real merge.
91063bbc 120 ;;
123ee3ca 1211,"$1",*)
91063bbc
JH
122 # If head can reach all the merge then we are up to date.
123 # but first the most common case of merging one remote
4f692b19 124 echo "Already up-to-date."
a9358240 125 dropsave
91063bbc
JH
126 exit 0
127 ;;
123ee3ca 1281,"$head",*)
91063bbc
JH
129 # Again the most common case of merging one remote.
130 echo "Updating from $head to $1."
131 git-update-index --refresh 2>/dev/null
bf7960eb 132 new_head=$(git-rev-parse --verify "$1^0") &&
4f692b19
JH
133 git-read-tree -u -m $head "$new_head" &&
134 finish "$new_head" "Fast forward"
a9358240 135 dropsave
91063bbc
JH
136 exit 0
137 ;;
123ee3ca 1381,?*"$LF"?*,*)
91063bbc
JH
139 # We are not doing octopus and not fast forward. Need a
140 # real merge.
141 ;;
123ee3ca 1421,*,)
f9d72413
JH
143 # We are not doing octopus, not fast forward, and have only
144 # one common. See if it is really trivial.
145 echo "Trying really trivial in-index merge..."
146 git-update-index --refresh 2>/dev/null
147 if git-read-tree --trivial -m -u $common $head "$1" &&
148 result_tree=$(git-write-tree)
149 then
150 echo "Wonderful."
151 result_commit=$(
152 echo "$merge_msg" |
153 git-commit-tree $result_tree -p HEAD -p "$1"
154 ) || exit
4f692b19 155 finish "$result_commit" "In-index merge"
f9d72413
JH
156 dropsave
157 exit 0
158 fi
159 echo "Nope."
160 ;;
91063bbc
JH
161*)
162 # An octopus. If we can reach all the remote we are up to date.
163 up_to_date=t
164 for remote
165 do
13956670 166 common_one=$(git-merge-base --all $head $remote)
91063bbc
JH
167 if test "$common_one" != "$remote"
168 then
169 up_to_date=f
170 break
171 fi
172 done
173 if test "$up_to_date" = t
174 then
175 echo "Already up-to-date. Yeeah!"
a9358240 176 dropsave
91063bbc
JH
177 exit 0
178 fi
179 ;;
180esac
181
fbf8ac21
JH
182case "$use_strategies" in
183'')
184 case "$#" in
185 1)
186 use_strategies="$default_strategies" ;;
187 *)
188 use_strategies=octopus ;;
189 esac
190 ;;
191esac
192
a9358240
JH
193# At this point, we need a real merge. No matter what strategy
194# we use, it would operate on the index, possibly affecting the
195# working tree, and when resolved cleanly, have the desired tree
196# in the index -- this means that the index must be in sync with
60fa0560 197# the $head commit. The strategies are responsible to ensure this.
91063bbc 198
a9358240
JH
199case "$use_strategies" in
200?*' '?*)
201 # Stash away the local changes so that we can try more than one.
202 savestate
203 single_strategy=no
204 ;;
205*)
deca7e8c 206 rm -f "$GIT_DIR/MERGE_SAVE"
a9358240
JH
207 single_strategy=yes
208 ;;
209esac
91063bbc
JH
210
211result_tree= best_cnt=-1 best_strategy= wt_strategy=
695bf722 212merge_was_ok=
91063bbc
JH
213for strategy in $use_strategies
214do
215 test "$wt_strategy" = '' || {
216 echo "Rewinding the tree to pristine..."
a9358240 217 restorestate
91063bbc 218 }
a9358240
JH
219 case "$single_strategy" in
220 no)
221 echo "Trying merge strategy $strategy..."
222 ;;
223 esac
224
225 # Remember which strategy left the state in the working tree
91063bbc 226 wt_strategy=$strategy
a9358240 227
123ee3ca
JH
228 git-merge-$strategy $common -- "$head_arg" "$@"
229 exit=$?
230 if test "$no_commit" = t && test "$exit" = 0
231 then
695bf722 232 merge_was_ok=t
123ee3ca
JH
233 exit=1 ;# pretend it left conflicts.
234 fi
235
236 test "$exit" = 0 || {
91063bbc
JH
237
238 # The backend exits with 1 when conflicts are left to be resolved,
239 # with 2 when it does not handle the given merge at all.
240
91063bbc
JH
241 if test "$exit" -eq 1
242 then
243 cnt=`{
244 git-diff-files --name-only
245 git-ls-files --unmerged
246 } | wc -l`
247 if test $best_cnt -le 0 -o $cnt -le $best_cnt
248 then
249 best_strategy=$strategy
250 best_cnt=$cnt
251 fi
252 fi
253 continue
254 }
255
256 # Automerge succeeded.
257 result_tree=$(git-write-tree) && break
258done
259
260# If we have a resulting tree, that means the strategy module
261# auto resolved the merge cleanly.
262if test '' != "$result_tree"
263then
264 parents="-p $head"
265 for remote
266 do
267 parents="$parents -p $remote"
268 done
bf7960eb 269 result_commit=$(echo "$merge_msg" | git-commit-tree $result_tree $parents) || exit
4f692b19 270 finish "$result_commit" "Merge $result_commit, made by $wt_strategy."
a9358240 271 dropsave
91063bbc
JH
272 exit 0
273fi
274
275# Pick the result from the best strategy and have the user fix it up.
276case "$best_strategy" in
277'')
a9358240 278 restorestate
4275df51
FK
279 echo >&2 "No merge strategy handled the merge."
280 exit 2
91063bbc
JH
281 ;;
282"$wt_strategy")
283 # We already have its result in the working tree.
284 ;;
285*)
286 echo "Rewinding the tree to pristine..."
a9358240 287 restorestate
91063bbc 288 echo "Using the $best_strategy to prepare resolving by hand."
a9358240 289 git-merge-$best_strategy $common -- "$head_arg" "$@"
91063bbc
JH
290 ;;
291esac
292for remote
293do
294 echo $remote
295done >"$GIT_DIR/MERGE_HEAD"
deca7e8c
JH
296echo $merge_msg >"$GIT_DIR/MERGE_MSG"
297
695bf722
JH
298if test "$merge_was_ok" = t
299then
300 echo >&2 \
301 "Automatic merge went well; stopped before committing as requested"
302 exit 0
303else
304 die "Automatic merge failed; fix up by hand"
305fi