reset.c: replace switch by if-else
[git/jrn.git] / git-rebase--merge.sh
blobb10f2cf21b904bbbc3fafc27d183d8ad7917b479
1 #!/bin/sh
3 # Copyright (c) 2010 Junio C Hamano.
6 prec=4
8 read_state () {
9 onto_name=$(cat "$state_dir"/onto_name) &&
10 end=$(cat "$state_dir"/end) &&
11 msgnum=$(cat "$state_dir"/msgnum)
14 continue_merge () {
15 test -d "$state_dir" || die "$state_dir directory does not exist"
17 unmerged=$(git ls-files -u)
18 if test -n "$unmerged"
19 then
20 echo "You still have unmerged paths in your index"
21 echo "did you forget to use git add?"
22 die "$resolvemsg"
25 cmt=`cat "$state_dir/current"`
26 if ! git diff-index --quiet --ignore-submodules HEAD --
27 then
28 if ! git commit --no-verify -C "$cmt"
29 then
30 echo "Commit failed, please do not call \"git commit\""
31 echo "directly, but instead do one of the following: "
32 die "$resolvemsg"
34 if test -z "$GIT_QUIET"
35 then
36 printf "Committed: %0${prec}d " $msgnum
38 echo "$cmt $(git rev-parse HEAD^0)" >> "$state_dir/rewritten"
39 else
40 if test -z "$GIT_QUIET"
41 then
42 printf "Already applied: %0${prec}d " $msgnum
45 test -z "$GIT_QUIET" &&
46 GIT_PAGER='' git log --format=%s -1 "$cmt"
48 # onto the next patch:
49 msgnum=$(($msgnum + 1))
50 echo "$msgnum" >"$state_dir/msgnum"
53 call_merge () {
54 cmt="$(cat "$state_dir/cmt.$1")"
55 echo "$cmt" > "$state_dir/current"
56 hd=$(git rev-parse --verify HEAD)
57 cmt_name=$(git symbolic-ref HEAD 2> /dev/null || echo HEAD)
58 msgnum=$(cat "$state_dir/msgnum")
59 eval GITHEAD_$cmt='"${cmt_name##refs/heads/}~$(($end - $msgnum))"'
60 eval GITHEAD_$hd='$onto_name'
61 export GITHEAD_$cmt GITHEAD_$hd
62 if test -n "$GIT_QUIET"
63 then
64 GIT_MERGE_VERBOSITY=1 && export GIT_MERGE_VERBOSITY
66 test -z "$strategy" && strategy=recursive
67 eval 'git-merge-$strategy' $strategy_opts '"$cmt^" -- "$hd" "$cmt"'
68 rv=$?
69 case "$rv" in
71 unset GITHEAD_$cmt GITHEAD_$hd
72 return
75 git rerere $allow_rerere_autoupdate
76 die "$resolvemsg"
79 echo "Strategy: $strategy failed, try another" 1>&2
80 die "$resolvemsg"
83 die "Unknown exit code ($rv) from command:" \
84 "git-merge-$strategy $cmt^ -- HEAD $cmt"
86 esac
89 finish_rb_merge () {
90 move_to_original_branch
91 if test -s "$state_dir"/rewritten
92 then
93 git notes copy --for-rewrite=rebase <"$state_dir"/rewritten
94 if test -x "$GIT_DIR"/hooks/post-rewrite
95 then
96 "$GIT_DIR"/hooks/post-rewrite rebase <"$state_dir"/rewritten
99 rm -r "$state_dir"
100 say All done.
103 case "$action" in
104 continue)
105 read_state
106 continue_merge
107 while test "$msgnum" -le "$end"
109 call_merge "$msgnum"
110 continue_merge
111 done
112 finish_rb_merge
113 exit
115 skip)
116 read_state
117 git rerere clear
118 msgnum=$(($msgnum + 1))
119 while test "$msgnum" -le "$end"
121 call_merge "$msgnum"
122 continue_merge
123 done
124 finish_rb_merge
125 exit
127 esac
129 mkdir -p "$state_dir"
130 echo "$onto_name" > "$state_dir/onto_name"
131 write_basic_state
133 msgnum=0
134 for cmt in `git rev-list --reverse --no-merges "$revisions"`
136 msgnum=$(($msgnum + 1))
137 echo "$cmt" > "$state_dir/cmt.$msgnum"
138 done
140 echo 1 >"$state_dir/msgnum"
141 echo $msgnum >"$state_dir/end"
143 end=$msgnum
144 msgnum=1
146 while test "$msgnum" -le "$end"
148 call_merge "$msgnum"
149 continue_merge
150 done
152 finish_rb_merge