git-am: do not lose already edited final-commit when resuming.
[git] / git-am.sh
1 #!/bin/sh
2 #
3 #
4 . git-sh-setup || die "Not a git archive"
5
6 files=$(git-diff-index --cached --name-only HEAD) || exit
7 if [ "$files" ]; then
8    echo "Dirty index: cannot apply patches (dirty: $files)" >&2
9    exit 1
10 fi
11
12 usage () {
13     echo >&2 "usage: $0 [--signoff] [--dotest=<dir>] [--utf8] [--3way] <mbox>"
14     echo >&2 "  or, when resuming"
15     echo >&2 "  $0 [--skip]"
16     exit 1;
17 }
18
19 stop_here () {
20     echo "$1" >"$dotest/next"
21     exit 1
22 }
23
24 go_next () {
25         rm -f "$dotest/$msgnum" "$dotest/msg" "$dotest/msg-clean" \
26                 "$dotest/patch" "$dotest/info"
27         echo "$next" >"$dotest/next"
28         this=$next
29 }
30
31 fall_back_3way () {
32     O_OBJECT=`cd "$GIT_OBJECT_DIRECTORY" && pwd`
33
34     rm -fr "$dotest"/patch-merge-*
35     mkdir "$dotest/patch-merge-tmp-dir"
36
37     # First see if the patch records the index info that we can use.
38     if git-apply -z --index-info "$dotest/patch" \
39         >"$dotest/patch-merge-index-info" 2>/dev/null &&
40         GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
41         git-update-index -z --index-info <"$dotest/patch-merge-index-info" &&
42         GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
43         git-write-tree >"$dotest/patch-merge-base+" &&
44         # index has the base tree now.
45         (
46             cd "$dotest/patch-merge-tmp-dir" &&
47             GIT_INDEX_FILE="../patch-merge-tmp-index" \
48             GIT_OBJECT_DIRECTORY="$O_OBJECT" \
49             git-apply --index <../patch
50         )
51     then
52         echo Using index info to reconstruct a base tree...
53         mv "$dotest/patch-merge-base+" "$dotest/patch-merge-base"
54         mv "$dotest/patch-merge-tmp-index" "$dotest/patch-merge-index"
55     else
56         # Otherwise, try nearby trees that can be used to apply the
57         # patch.
58         (
59             N=10
60
61             # Hoping the patch is against our recent commits...
62             git-rev-list --max-count=$N HEAD
63
64             # or hoping the patch is against known tags...
65             git-ls-remote --tags .
66         ) |
67         while read base junk
68         do
69             # See if we have it as a tree...
70             git-cat-file tree "$base" >/dev/null 2>&1 || continue
71
72             rm -fr "$dotest"/patch-merge-* &&
73             mkdir "$dotest/patch-merge-tmp-dir" || break
74             (
75                 cd "$dotest/patch-merge-tmp-dir" &&
76                 GIT_INDEX_FILE=../patch-merge-tmp-index &&
77                 GIT_OBJECT_DIRECTORY="$O_OBJECT" &&
78                 export GIT_INDEX_FILE GIT_OBJECT_DIRECTORY &&
79                 git-read-tree "$base" &&
80                 git-apply --index &&
81                 mv ../patch-merge-tmp-index ../patch-merge-index &&
82                 echo "$base" >../patch-merge-base
83             ) <"$dotest/patch"  2>/dev/null && break
84         done
85     fi
86
87     test -f "$dotest/patch-merge-index" &&
88     his_tree=$(GIT_INDEX_FILE="$dotest/patch-merge-index" git-write-tree) &&
89     orig_tree=$(cat "$dotest/patch-merge-base") &&
90     rm -fr "$dotest"/patch-merge-* || exit 1
91
92     echo Falling back to patching base and 3-way merge...
93
94     # This is not so wrong.  Depending on which base we picked,
95     # orig_tree may be wildly different from ours, but his_tree
96     # has the same set of wildly different changes in parts the
97     # patch did not touch, so resolve ends up cancelling them,
98     # saying that we reverted all those changes.
99
100     git-merge-resolve $orig_tree -- HEAD $his_tree || {
101             echo Failed to merge in the changes.
102             exit 1
103     }
104 }
105
106 prec=4
107 dotest=.dotest sign= utf8= keep= skip= interactive=
108
109 while case "$#" in 0) break;; esac
110 do
111         case "$1" in
112         -d=*|--d=*|--do=*|--dot=*|--dote=*|--dotes=*|--dotest=*)
113         dotest=`expr "$1" : '-[^=]*=\(.*\)'`; shift ;;
114         -d|--d|--do|--dot|--dote|--dotes|--dotest)
115         case "$#" in 1) usage ;; esac; shift
116         dotest="$1"; shift;;
117
118         -i|--i|--in|--int|--inte|--inter|--intera|--interac|--interact|\
119         --interacti|--interactiv|--interactive)
120         interactive=t; shift ;;
121
122         -3|--3|--3w|--3wa|--3way)
123         threeway=t; shift ;;
124         -s|--s|--si|--sig|--sign|--signo|--signof|--signoff)
125         sign=t; shift ;;
126         -u|--u|--ut|--utf|--utf8)
127         utf8=t; shift ;;
128         -k|--k|--ke|--kee|--keep)
129         keep=t; shift ;;
130
131         --sk|--ski|--skip)
132         skip=t; shift ;;
133
134         --)
135         shift; break ;;
136         -*)
137         usage ;;
138         *)
139         break ;;
140         esac
141 done
142
143 if test -d "$dotest" &&
144    last=$(cat "$dotest/last") &&
145    next=$(cat "$dotest/next") &&
146    test $# != 0 &&
147    test "$next" -gt "$last"
148 then
149    rm -fr "$dotest"
150 fi
151
152 if test -d "$dotest"
153 then
154         test ",$#," = ",0," ||
155         die "previous dotest directory $dotest still exists but mbox given."
156         resume=yes
157 else
158         # Make sure we are not given --skip
159         test ",$skip," = ,, ||
160         die "we are not resuming."
161
162         # Start afresh.
163         mkdir -p "$dotest" || exit
164
165         # cat does the right thing for us, including '-' to mean
166         # standard input.
167         cat "$@" |
168         git-mailsplit -d$prec "$dotest/" >"$dotest/last" || {
169                 rm -fr "$dotest"
170                 exit 1
171         }
172
173         echo "$sign" >"$dotest/sign"
174         echo "$utf8" >"$dotest/utf8"
175         echo "$keep" >"$dotest/keep"
176         echo 1 >"$dotest/next"
177 fi
178
179 if test "$(cat "$dotest/utf8")" = t
180 then
181         utf8=-u
182 fi
183 if test "$(cat "$dotest/keep")" = t
184 then
185         keep=-k
186 fi
187 if test "$(cat "$dotest/sign")" = t
188 then
189         SIGNOFF=`git-var GIT_COMMITTER_IDENT | sed -e '
190                         s/>.*/>/
191                         s/^/Signed-off-by: /'
192                 `
193 else
194         SIGNOFF=
195 fi
196
197 last=`cat "$dotest/last"`
198 this=`cat "$dotest/next"`
199 if test "$skip" = t
200 then
201         this=`expr "$this" + 1`
202 fi
203
204 if test "$this" -gt "$last"
205 then
206         echo Nothing to do.
207         rm -fr "$dotest"
208         exit
209 fi
210
211 while test "$this" -le "$last"
212 do
213         msgnum=`printf "%0${prec}d" $this`
214         next=`expr "$this" + 1`
215         test -f "$dotest/$msgnum" || {
216                 go_next
217                 continue
218         }
219         case "$resume" in
220         '')
221                 git-mailinfo $keep $utf8 "$dotest/msg" "$dotest/patch" \
222                         <"$dotest/$msgnum" >"$dotest/info" ||
223                         stop_here $this
224                 git-stripspace < "$dotest/msg" > "$dotest/msg-clean"
225                 ;;
226         esac
227
228         GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$dotest/info")"
229         GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$dotest/info")"
230         GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$dotest/info")"
231         export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
232
233         SUBJECT="$(sed -n '/^Subject/ s/Subject: //p' "$dotest/info")"
234         case "$keep_subject" in -k)  SUBJECT="[PATCH] $SUBJECT" ;; esac
235
236         case "$resume" in
237         '')
238             if test '' != "$SIGNOFF"
239             then
240                 LAST_SIGNED_OFF_BY=`
241                     sed -ne '/^Signed-off-by: /p' \
242                     "$dotest/msg-clean" |
243                     tail -n 1
244                 `
245                 ADD_SIGNOFF=`
246                     test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || {
247                     test '' = "$LAST_SIGNED_OFF_BY" && echo
248                     echo "$SIGNOFF"
249                 }`
250             else
251                 ADD_SIGNOFF=
252             fi
253             {
254                 echo "$SUBJECT"
255                 if test -s "$dotest/msg-clean"
256                 then
257                         echo
258                         cat "$dotest/msg-clean"
259                 fi
260                 if test '' != "$ADD_SIGNOFF"
261                 then
262                         echo "$ADD_SIGNOFF"
263                 fi
264             } >"$dotest/final-commit"
265             ;;
266         esac
267
268         resume=
269         if test "$interactive" = t
270         then
271             test -t 0 ||
272             die "cannot be interactive without stdin connected to a terminal."
273             action=again
274             while test "$action" = again
275             do
276                 echo "Commit Body is:"
277                 echo "--------------------------"
278                 cat "$dotest/final-commit"
279                 echo "--------------------------"
280                 echo -n "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all "
281                 read reply
282                 case "$reply" in
283                 [yY]*) action=yes ;;
284                 [aA]*) action=yes interactive= ;;
285                 [nN]*) action=skip ;;
286                 [eE]*) "${VISUAL:-${EDITOR:-vi}}" "$dotest/final-commit"
287                        action=again ;;
288                 [vV]*) action=again
289                        LESS=-S ${PAGER:-less} "$dotest/patch" ;;
290                 *)     action=again ;;
291                 esac
292             done
293         else
294             action=yes
295         fi
296
297         if test $action = skip
298         then
299                 go_next
300                 continue
301         fi
302
303         if test -x "$GIT_DIR"/hooks/applypatch-msg
304         then
305                 "$GIT_DIR"/hooks/applypatch-msg "$dotest/final-commit" ||
306                 stop_here $this
307         fi
308
309         echo
310         echo "Applying '$SUBJECT'"
311         echo
312
313         git-apply --index "$dotest/patch"; apply_status=$?
314         if test $apply_status = 1 && test "$threeway" = t
315         then
316                 if (fall_back_3way)
317                 then
318                     # Applying the patch to an earlier tree and merging the
319                     # result may have produced the same tree as ours.
320                     changed="$(git-diff-index --cached --name-only -z HEAD)"
321                     if test '' = "$changed"
322                     then
323                             echo No changes -- Patch already applied.
324                             go_next
325                             continue
326                     fi
327                     # clear apply_status -- we have successfully merged.
328                     apply_status=0
329                 fi
330         fi
331         if test $apply_status != 0
332         then
333                 echo Patch failed at $msgnum.
334                 stop_here $this
335         fi
336
337         if test -x "$GIT_DIR"/hooks/pre-applypatch
338         then
339                 "$GIT_DIR"/hooks/pre-applypatch || stop_here $this
340         fi
341
342         tree=$(git-write-tree) &&
343         echo Wrote tree $tree &&
344         parent=$(git-rev-parse --verify HEAD) &&
345         commit=$(git-commit-tree $tree -p $parent <"$dotest/final-commit") &&
346         echo Committed: $commit &&
347         git-update-ref HEAD $commit $parent ||
348         stop_here $this
349
350         if test -x "$GIT_DIR"/hooks/post-applypatch
351         then
352                 "$GIT_DIR"/hooks/post-applypatch
353         fi
354
355         go_next
356 done
357
358 rm -fr "$dotest"