Pass CVSps generated A U Thor <author@domain.xz> intact.
[git/jnareb-git.git] / git-merge.sh
blob29e86c695320b3d575314bc7c0be9eea0fd1b04a
1 #!/bin/sh
3 # Copyright (c) 2005 Junio C Hamano
6 . git-sh-setup || die "Not a git archive"
8 LF='
11 usage () {
12 die "git-merge [-n] [-s <strategy>]... <merge-message> <head> <remote>+"
15 # all_strategies='resolve recursive stupid octopus'
17 all_strategies='recursive octopus resolve stupid'
18 default_strategies='resolve octopus'
19 use_strategies=
21 dropsave() {
22 rm -f -- "$GIT_DIR/MERGE_HEAD" "$GIT_DIR/MERGE_MSG" \
23 "$GIT_DIR/MERGE_SAVE" || exit 1
26 savestate() {
27 # Stash away any local modifications.
28 git-diff-index -r -z --name-only $head |
29 cpio -0 -o >"$GIR_DIR/MERGE_SAVE"
32 restorestate() {
33 if test -f "$GIT_DIR/MERGE_SAVE"
34 then
35 git reset --hard $head
36 cpio -iuv <"$GIT_DIR/MERGE_SAVE"
37 git-update-index --refresh >/dev/null
41 summary() {
42 case "$no_summary" in
43 '')
44 git-diff-tree -p -M $head "$1" |
45 git-apply --stat --summary
47 esac
50 while case "$#" in 0) break ;; esac
52 case "$1" in
53 -n|--n|--no|--no-|--no-s|--no-su|--no-sum|--no-summ|\
54 --no-summa|--no-summar|--no-summary)
55 no_summary=t ;;
56 -s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\
57 --strateg=*|--strategy=*|\
58 -s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy)
59 case "$#,$1" in
60 *,*=*)
61 strategy=`expr "$1" : '-[^=]*=\(.*\)'` ;;
62 1,*)
63 usage ;;
65 strategy="$2"
66 shift ;;
67 esac
68 case " $all_strategies " in
69 *" $strategy "*)
70 use_strategies="$use_strategies$strategy " ;;
72 die "available strategies are: $all_strategies" ;;
73 esac
75 -*) usage ;;
76 *) break ;;
77 esac
78 shift
79 done
81 case "$use_strategies" in
82 '')
83 use_strategies=$default_strategies
85 esac
86 test "$#" -le 2 && usage ;# we need at least two heads.
88 merge_msg="$1"
89 shift
90 head_arg="$1"
91 head=$(git-rev-parse --verify "$1"^0) || usage
92 shift
94 # All the rest are remote heads
95 for remote
97 git-rev-parse --verify "$remote"^0 >/dev/null ||
98 die "$remote - not something we can merge"
99 done
101 common=$(git-show-branch --merge-base $head "$@")
102 echo "$head" >"$GIT_DIR/ORIG_HEAD"
104 case "$#,$common" in
105 *,'')
106 die "Unable to find common commit between $head_arg and $*"
108 1,"$1")
109 # If head can reach all the merge then we are up to date.
110 # but first the most common case of merging one remote
111 echo "Already up-to-date. Yeeah!"
112 dropsave
113 exit 0
115 1,"$head")
116 # Again the most common case of merging one remote.
117 echo "Updating from $head to $1."
118 git-update-index --refresh 2>/dev/null
119 git-read-tree -u -m $head "$1" &&
120 new_head=$(git-rev-parse --verify "$1^0") &&
121 git-update-ref HEAD "$new_head" "$head" || exit 1
122 summary "$1"
123 dropsave
124 exit 0
126 1,*)
127 # We are not doing octopus and not fast forward. Need a
128 # real merge.
131 # An octopus. If we can reach all the remote we are up to date.
132 up_to_date=t
133 for remote
135 common_one=$(git-merge-base $head $remote)
136 if test "$common_one" != "$remote"
137 then
138 up_to_date=f
139 break
141 done
142 if test "$up_to_date" = t
143 then
144 echo "Already up-to-date. Yeeah!"
145 dropsave
146 exit 0
149 esac
151 # At this point, we need a real merge. No matter what strategy
152 # we use, it would operate on the index, possibly affecting the
153 # working tree, and when resolved cleanly, have the desired tree
154 # in the index -- this means that the index must be in sync with
155 # the $head commit. The strategies are responsible to ensure this.
157 case "$use_strategies" in
158 ?*' '?*)
159 # Stash away the local changes so that we can try more than one.
160 savestate
161 single_strategy=no
164 rm -f "$GIT_DIR/MERGE_SAVE"
165 single_strategy=yes
167 esac
169 result_tree= best_cnt=-1 best_strategy= wt_strategy=
170 for strategy in $use_strategies
172 test "$wt_strategy" = '' || {
173 echo "Rewinding the tree to pristine..."
174 restorestate
176 case "$single_strategy" in
178 echo "Trying merge strategy $strategy..."
180 esac
182 # Remember which strategy left the state in the working tree
183 wt_strategy=$strategy
185 git-merge-$strategy $common -- "$head_arg" "$@" || {
187 # The backend exits with 1 when conflicts are left to be resolved,
188 # with 2 when it does not handle the given merge at all.
190 exit=$?
191 if test "$exit" -eq 1
192 then
193 cnt=`{
194 git-diff-files --name-only
195 git-ls-files --unmerged
196 } | wc -l`
197 if test $best_cnt -le 0 -o $cnt -le $best_cnt
198 then
199 best_strategy=$strategy
200 best_cnt=$cnt
203 continue
206 # Automerge succeeded.
207 result_tree=$(git-write-tree) && break
208 done
210 # If we have a resulting tree, that means the strategy module
211 # auto resolved the merge cleanly.
212 if test '' != "$result_tree"
213 then
214 parents="-p $head"
215 for remote
217 parents="$parents -p $remote"
218 done
219 result_commit=$(echo "$merge_msg" | git-commit-tree $result_tree $parents) || exit
220 echo "Committed merge $result_commit, made by $wt_strategy."
221 git-update-ref HEAD $result_commit $head
222 summary $result_commit
223 dropsave
224 exit 0
227 # Pick the result from the best strategy and have the user fix it up.
228 case "$best_strategy" in
230 restorestate
231 die "No merge strategy handled the merge."
233 "$wt_strategy")
234 # We already have its result in the working tree.
237 echo "Rewinding the tree to pristine..."
238 restorestate
239 echo "Using the $best_strategy to prepare resolving by hand."
240 git-merge-$best_strategy $common -- "$head_arg" "$@"
242 esac
243 for remote
245 echo $remote
246 done >"$GIT_DIR/MERGE_HEAD"
247 echo $merge_msg >"$GIT_DIR/MERGE_MSG"
249 die "Automatic merge failed; fix up by hand"