[PATCH] git-daemon --syslog to log through syslog
[git/dscho.git] / git-merge.sh
blob818e6b772d30c060020640deb051f2428e2c9fe5
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 dropheads() {
22 rm -f -- "$GIT_DIR/MERGE_HEAD" || exit 1
25 summary() {
26 case "$no_summary" in
27 '')
28 git-diff-tree -p -M $head "$1" |
29 git-apply --stat --summary
31 esac
34 while case "$#" in 0) break ;; esac
36 case "$1" in
37 -n|--n|--no|--no-|--no-s|--no-su|--no-sum|--no-summ|\
38 --no-summa|--no-summar|--no-summary)
39 no_summary=t ;;
40 -s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\
41 --strateg=*|--strategy=*|\
42 -s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy)
43 case "$#,$1" in
44 *,*=*)
45 strategy=`expr "$1" : '-[^=]*=\(.*\)'` ;;
46 1,*)
47 usage ;;
49 strategy="$2"
50 shift ;;
51 esac
52 case " $all_strategies " in
53 *" $strategy "*)
54 use_strategies="$use_strategies$strategy " ;;
56 die "available strategies are: $all_strategies" ;;
57 esac
59 -*) usage ;;
60 *) break ;;
61 esac
62 shift
63 done
65 case "$use_strategies" in
66 '')
67 use_strategies=$default_strategies
69 esac
70 test "$#" -le 2 && usage ;# we need at least two heads.
72 merge_msg="$1"
73 shift
74 head=$(git-rev-parse --verify "$1"^0) || usage
75 shift
77 # All the rest are remote heads
78 for remote
80 git-rev-parse --verify "$remote"^0 >/dev/null ||
81 die "$remote - not something we can merge"
82 done
84 common=$(git-show-branch --merge-base $head "$@")
85 echo "$head" >"$GIT_DIR/ORIG_HEAD"
87 case "$#,$common" in
88 *,'')
89 die "Unable to find common commit between $head and $*"
91 1,"$1")
92 # If head can reach all the merge then we are up to date.
93 # but first the most common case of merging one remote
94 echo "Already up-to-date. Yeeah!"
95 dropheads
96 exit 0
98 1,"$head")
99 # Again the most common case of merging one remote.
100 echo "Updating from $head to $1."
101 git-update-index --refresh 2>/dev/null
102 git-read-tree -u -m $head "$1" || exit 1
103 git-rev-parse --verify "$1^0" > "$GIT_DIR/HEAD"
104 summary "$1"
105 dropheads
106 exit 0
108 1,*)
109 # We are not doing octopus and not fast forward. Need a
110 # real merge.
113 # An octopus. If we can reach all the remote we are up to date.
114 up_to_date=t
115 for remote
117 common_one=$(git-merge-base $head $remote)
118 if test "$common_one" != "$remote"
119 then
120 up_to_date=f
121 break
123 done
124 if test "$up_to_date" = t
125 then
126 echo "Already up-to-date. Yeeah!"
127 dropheads
128 exit 0
131 esac
133 # At this point we need a real merge. Require that the tree matches
134 # exactly our head.
136 git-update-index --refresh &&
137 test '' = "`git-diff-index --cached --name-only $head`" || {
138 die "Need real merge but the working tree has local changes."
141 result_tree= best_cnt=-1 best_strategy= wt_strategy=
142 for strategy in $use_strategies
144 test "$wt_strategy" = '' || {
145 echo "Rewinding the tree to pristine..."
146 git reset --hard $head
148 echo "Trying merge strategy $strategy..."
149 wt_strategy=$strategy
150 git-merge-$strategy $common -- $head "$@" || {
152 # The backend exits with 1 when conflicts are left to be resolved,
153 # with 2 when it does not handle the given merge at all.
155 exit=$?
156 if test "$exit" -eq 1
157 then
158 cnt=`{
159 git-diff-files --name-only
160 git-ls-files --unmerged
161 } | wc -l`
162 if test $best_cnt -le 0 -o $cnt -le $best_cnt
163 then
164 best_strategy=$strategy
165 best_cnt=$cnt
168 continue
171 # Automerge succeeded.
172 result_tree=$(git-write-tree) && break
173 done
175 # If we have a resulting tree, that means the strategy module
176 # auto resolved the merge cleanly.
177 if test '' != "$result_tree"
178 then
179 parents="-p $head"
180 for remote
182 parents="$parents -p $remote"
183 done
184 result_commit=$(echo "$merge_msg" | git-commit-tree $result_tree $parents)
185 echo "Committed merge $result_commit, made by $wt_strategy."
186 echo $result_commit >"$GIT_DIR/HEAD"
187 summary $result_commit
188 dropheads
189 exit 0
192 # Pick the result from the best strategy and have the user fix it up.
193 case "$best_strategy" in
195 git reset --hard $head
196 die "No merge strategy handled the merge."
198 "$wt_strategy")
199 # We already have its result in the working tree.
202 echo "Rewinding the tree to pristine..."
203 git reset --hard $head
204 echo "Using the $best_strategy to prepare resolving by hand."
205 git-merge-$best_strategy $common -- $head "$@"
207 esac
208 for remote
210 echo $remote
211 done >"$GIT_DIR/MERGE_HEAD"
212 die "Automatic merge failed; fix up by hand"