Examples of resetting.
[git/jrn.git] / git-merge.sh
blobe50fbb1160ad6ccfebc0d65fcc54bc3488e597cd
1 #!/bin/sh
3 # Copyright (c) 2005 Junio C Hamano
7 USAGE='[-n] [--no-commit] [-s <strategy>]... <merge-message> <head> <remote>+'
8 . git-sh-setup
10 LF='
13 all_strategies='recursive octopus resolve stupid ours'
14 default_strategies='recursive'
15 use_strategies=
17 dropsave() {
18 rm -f -- "$GIT_DIR/MERGE_HEAD" "$GIT_DIR/MERGE_MSG" \
19 "$GIT_DIR/MERGE_SAVE" || exit 1
22 savestate() {
23 # Stash away any local modifications.
24 git-diff-index -z --name-only $head |
25 cpio -0 -o >"$GIT_DIR/MERGE_SAVE"
28 restorestate() {
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
37 finish () {
38 test '' = "$2" || echo "$2"
39 case "$merge_msg" in
40 '')
41 echo "No merge message -- not updating HEAD"
44 git-update-ref HEAD "$1" "$head" || exit 1
46 esac
48 case "$no_summary" in
49 '')
50 git-diff-tree -p -M "$head" "$1" |
51 git-apply --stat --summary
53 esac
56 while case "$#" in 0) break ;; esac
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 ;;
62 --no-c|--no-co|--no-com|--no-comm|--no-commi|--no-commit)
63 no_commit=t ;;
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" : '-[^=]*=\(.*\)'` ;;
70 1,*)
71 usage ;;
73 strategy="$2"
74 shift ;;
75 esac
76 case " $all_strategies " in
77 *" $strategy "*)
78 use_strategies="$use_strategies$strategy " ;;
80 die "available strategies are: $all_strategies" ;;
81 esac
83 -*) usage ;;
84 *) break ;;
85 esac
86 shift
87 done
89 test "$#" -le 2 && usage ;# we need at least two heads.
91 merge_msg="$1"
92 shift
93 head_arg="$1"
94 head=$(git-rev-parse --verify "$1"^0) || usage
95 shift
97 # All the rest are remote heads
98 remoteheads=
99 for remote
101 remotehead=$(git-rev-parse --verify "$remote"^0) ||
102 die "$remote - not something we can merge"
103 remoteheads="${remoteheads}$remotehead "
104 done
105 set x $remoteheads ; shift
107 case "$#" in
109 common=$(git-merge-base --all $head "$@")
112 common=$(git-show-branch --merge-base $head "$@")
114 esac
115 echo "$head" >"$GIT_DIR/ORIG_HEAD"
117 case "$#,$common,$no_commit" in
118 *,'',*)
119 # No common ancestors found. We need a real merge.
121 1,"$1",*)
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
124 echo "Already up-to-date."
125 dropsave
126 exit 0
128 1,"$head",*)
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
132 new_head=$(git-rev-parse --verify "$1^0") &&
133 git-read-tree -u -m $head "$new_head" &&
134 finish "$new_head" "Fast forward"
135 dropsave
136 exit 0
138 1,?*"$LF"?*,*)
139 # We are not doing octopus and not fast forward. Need a
140 # real merge.
142 1,*,)
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
155 finish "$result_commit" "In-index merge"
156 dropsave
157 exit 0
159 echo "Nope."
162 # An octopus. If we can reach all the remote we are up to date.
163 up_to_date=t
164 for remote
166 common_one=$(git-merge-base --all $head $remote)
167 if test "$common_one" != "$remote"
168 then
169 up_to_date=f
170 break
172 done
173 if test "$up_to_date" = t
174 then
175 echo "Already up-to-date. Yeeah!"
176 dropsave
177 exit 0
180 esac
182 case "$use_strategies" in
184 case "$#" in
186 use_strategies="$default_strategies" ;;
188 use_strategies=octopus ;;
189 esac
191 esac
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
197 # the $head commit. The strategies are responsible to ensure this.
199 case "$use_strategies" in
200 ?*' '?*)
201 # Stash away the local changes so that we can try more than one.
202 savestate
203 single_strategy=no
206 rm -f "$GIT_DIR/MERGE_SAVE"
207 single_strategy=yes
209 esac
211 result_tree= best_cnt=-1 best_strategy= wt_strategy=
212 for strategy in $use_strategies
214 test "$wt_strategy" = '' || {
215 echo "Rewinding the tree to pristine..."
216 restorestate
218 case "$single_strategy" in
220 echo "Trying merge strategy $strategy..."
222 esac
224 # Remember which strategy left the state in the working tree
225 wt_strategy=$strategy
227 git-merge-$strategy $common -- "$head_arg" "$@"
228 exit=$?
229 if test "$no_commit" = t && test "$exit" = 0
230 then
231 exit=1 ;# pretend it left conflicts.
234 test "$exit" = 0 || {
236 # The backend exits with 1 when conflicts are left to be resolved,
237 # with 2 when it does not handle the given merge at all.
239 if test "$exit" -eq 1
240 then
241 cnt=`{
242 git-diff-files --name-only
243 git-ls-files --unmerged
244 } | wc -l`
245 if test $best_cnt -le 0 -o $cnt -le $best_cnt
246 then
247 best_strategy=$strategy
248 best_cnt=$cnt
251 continue
254 # Automerge succeeded.
255 result_tree=$(git-write-tree) && break
256 done
258 # If we have a resulting tree, that means the strategy module
259 # auto resolved the merge cleanly.
260 if test '' != "$result_tree"
261 then
262 parents="-p $head"
263 for remote
265 parents="$parents -p $remote"
266 done
267 result_commit=$(echo "$merge_msg" | git-commit-tree $result_tree $parents) || exit
268 finish "$result_commit" "Merge $result_commit, made by $wt_strategy."
269 dropsave
270 exit 0
273 # Pick the result from the best strategy and have the user fix it up.
274 case "$best_strategy" in
276 restorestate
277 echo >&2 "No merge strategy handled the merge."
278 exit 2
280 "$wt_strategy")
281 # We already have its result in the working tree.
284 echo "Rewinding the tree to pristine..."
285 restorestate
286 echo "Using the $best_strategy to prepare resolving by hand."
287 git-merge-$best_strategy $common -- "$head_arg" "$@"
289 esac
290 for remote
292 echo $remote
293 done >"$GIT_DIR/MERGE_HEAD"
294 echo $merge_msg >"$GIT_DIR/MERGE_MSG"
296 die "Automatic merge failed/prevented; fix up by hand"