1 #!/bin/sh
2 #
3 # Copyright (c) 2005 Junio C Hamano
4 #
6 . git-sh-setup || die "Not a git archive"
8 LF='
9 '
11 usage () {
12 die "git-merge [-n] [-s <strategy>]... <merge-message> <head> <remote>+"
13 }
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" \
23 "$GIT_DIR/MERGE_SAVE" || exit 1
24 }
26 savestate() {
27 git diff -r -z --name-only $head | cpio -0 -o >"$GIR_DIR/MERGE_SAVE"
28 }
30 restorestate() {
31 git reset --hard $head
32 cpio -iuv <"$GIT_DIR/MERGE_SAVE"
33 git-update-index --refresh >/dev/null
34 }
36 summary() {
37 case "$no_summary" in
38 '')
39 git-diff-tree -p -M $head "$1" |
40 git-apply --stat --summary
41 ;;
42 esac
43 }
45 while case "$#" in 0) break ;; esac
46 do
47 case "$1" in
48 -n|--n|--no|--no-|--no-s|--no-su|--no-sum|--no-summ|\
49 --no-summa|--no-summar|--no-summary)
50 no_summary=t ;;
51 -s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\
52 --strateg=*|--strategy=*|\
53 -s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy)
54 case "$#,$1" in
55 *,*=*)
56 strategy=`expr "$1" : '-[^=]*=\(.*\)'` ;;
57 1,*)
58 usage ;;
59 *)
60 strategy="$2"
61 shift ;;
62 esac
63 case " $all_strategies " in
64 *" $strategy "*)
65 use_strategies="$use_strategies$strategy " ;;
66 *)
67 die "available strategies are: $all_strategies" ;;
68 esac
69 ;;
70 -*) usage ;;
71 *) break ;;
72 esac
73 shift
74 done
76 case "$use_strategies" in
77 '')
78 use_strategies=$default_strategies
79 ;;
80 esac
81 test "$#" -le 2 && usage ;# we need at least two heads.
83 merge_msg="$1"
84 shift
85 head_arg="$1"
86 head=$(git-rev-parse --verify "$1"^0) || usage
87 shift
89 # All the rest are remote heads
90 for remote
91 do
92 git-rev-parse --verify "$remote"^0 >/dev/null ||
93 die "$remote - not something we can merge"
94 done
96 common=$(git-show-branch --merge-base $head "$@")
97 echo "$head" >"$GIT_DIR/ORIG_HEAD"
99 case "$#,$common" in
100 *,'')
101 die "Unable to find common commit between $head_arg and $*"
102 ;;
103 1,"$1")
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!"
107 dropsave
108 exit 0
109 ;;
110 1,"$head")
111 # Again the most common case of merging one remote.
112 echo "Updating from $head to $1."
113 git-update-index --refresh 2>/dev/null
114 git-read-tree -u -m $head "$1" || exit 1
115 git-rev-parse --verify "$1^0" > "$GIT_DIR/HEAD"
116 summary "$1"
117 dropsave
118 exit 0
119 ;;
120 1,*)
121 # We are not doing octopus and not fast forward. Need a
122 # real merge.
123 ;;
124 *)
125 # An octopus. If we can reach all the remote we are up to date.
126 up_to_date=t
127 for remote
128 do
129 common_one=$(git-merge-base $head $remote)
130 if test "$common_one" != "$remote"
131 then
132 up_to_date=f
133 break
134 fi
135 done
136 if test "$up_to_date" = t
137 then
138 echo "Already up-to-date. Yeeah!"
139 dropsave
140 exit 0
141 fi
142 ;;
143 esac
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
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
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..."
172 restorestate
173 }
174 case "$single_strategy" in
175 no)
176 echo "Trying merge strategy $strategy..."
177 ;;
178 esac
180 # Remember which strategy left the state in the working tree
181 wt_strategy=$strategy
183 git-merge-$strategy $common -- "$head_arg" "$@" || {
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.
188 exit=$?
189 if test "$exit" -eq 1
190 then
191 cnt=`{
192 git-diff-files --name-only
193 git-ls-files --unmerged
194 } | wc -l`
195 if test $best_cnt -le 0 -o $cnt -le $best_cnt
196 then
197 best_strategy=$strategy
198 best_cnt=$cnt
199 fi
200 fi
201 continue
202 }
204 # Automerge succeeded.
205 result_tree=$(git-write-tree) && break
206 done
208 # If we have a resulting tree, that means the strategy module
209 # auto resolved the merge cleanly.
210 if test '' != "$result_tree"
211 then
212 parents="-p $head"
213 for remote
214 do
215 parents="$parents -p $remote"
216 done
217 result_commit=$(echo "$merge_msg" | git-commit-tree $result_tree $parents)
218 echo "Committed merge $result_commit, made by $wt_strategy."
219 echo $result_commit >"$GIT_DIR/HEAD"
220 summary $result_commit
221 dropsave
222 exit 0
223 fi
225 # Pick the result from the best strategy and have the user fix it up.
226 case "$best_strategy" in
227 '')
228 restorestate
229 die "No merge strategy handled the merge."
230 ;;
231 "$wt_strategy")
232 # We already have its result in the working tree.
233 ;;
234 *)
235 echo "Rewinding the tree to pristine..."
236 restorestate
237 echo "Using the $best_strategy to prepare resolving by hand."
238 git-merge-$best_strategy $common -- "$head_arg" "$@"
239 ;;
240 esac
241 for remote
242 do
243 echo $remote
244 done >"$GIT_DIR/MERGE_HEAD"
245 die "Automatic merge failed; fix up by hand"