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