Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100755 781 lines (721 sloc) 18.193 kb
d1c5f2a Add git-am, applymbox replacement.
Junio C Hamano authored
1 #!/bin/sh
2 #
d64e6b0 Keep Porcelainish from failing by broken ident after making changes.
Junio C Hamano authored
3 # Copyright (c) 2005, 2006 Junio C Hamano
d1c5f2a Add git-am, applymbox replacement.
Junio C Hamano authored
4
c149184 @peff allow git-am to run in a subdirectory
peff authored
5 SUBDIRECTORY_OK=Yes
78443d9 @MadCoder Migrate git-am.sh to use git-rev-parse --parseopt
MadCoder authored
6 OPTIONS_KEEPDASHDASH=
7 OPTIONS_SPEC="\
588c038 @gitster Merge branch 'sb/dashless'
authored
8 git am [options] [<mbox>|<Maildir>...]
09f8d05 @sbeyer git-am: Mention --abort in usage string part of OPTIONS_SPEC
sbeyer authored
9 git am [options] (--resolved | --skip | --abort)
78443d9 @MadCoder Migrate git-am.sh to use git-rev-parse --parseopt
MadCoder authored
10 --
66006c6 @gitster git-am: -i does not take a string parameter.
authored
11 i,interactive run interactively
98ef23b @jaysoffian git-am: minor cleanups
jaysoffian authored
12 b,binary* (historical option -- no-op)
78443d9 @MadCoder Migrate git-am.sh to use git-rev-parse --parseopt
MadCoder authored
13 3,3way allow fall back on 3way merging if needed
0e987a1 @bebarino am, rebase: teach quiet option
bebarino authored
14 q,quiet be quiet
78443d9 @MadCoder Migrate git-am.sh to use git-rev-parse --parseopt
MadCoder authored
15 s,signoff add a Signed-off-by line to the commit message
16 u,utf8 recode into utf8 (default)
fe1fa94 git-am: fix type in its usage string
Jörg Sommer authored
17 k,keep pass -k flag to git-mailinfo
017678b @gitster am/mailinfo: Disable scissors processing by default
authored
18 c,scissors strip everything before a scissors line
78443d9 @MadCoder Migrate git-am.sh to use git-rev-parse --parseopt
MadCoder authored
19 whitespace= pass it through git-apply
86c91f9 @Oblomov git apply: option to ignore whitespace differences
Oblomov authored
20 ignore-space-change pass it through git-apply
21 ignore-whitespace pass it through git-apply
b47dfe9 @gitster git-am: add --directory=<dir> option
authored
22 directory= pass it through git-apply
78443d9 @MadCoder Migrate git-am.sh to use git-rev-parse --parseopt
MadCoder authored
23 C= pass it through git-apply
24 p= pass it through git-apply
a5a6755 @Oblomov git-am foreign patch support: introduce patch_format
Oblomov authored
25 patch-format= format the patch(es) are in
b80da42 @madduck git-am: implement --reject option passed to git-apply
madduck authored
26 reject pass it through git-apply
78443d9 @MadCoder Migrate git-am.sh to use git-rev-parse --parseopt
MadCoder authored
27 resolvemsg= override error message when patch failure occurs
28 r,resolved to be used after a patch failure
3041c32 @gitster am: --rebasing
authored
29 skip skip the current patch
3e5057a git am --abort
Nanako Shiraishi authored
30 abort restore the original branch and abort the patching operation.
3f01ad6 @gitster am: Add --committer-date-is-author-date option
authored
31 committer-date-is-author-date lie about committer date
a79ec62 git-am: Add --ignore-date option
Nanako Shiraishi authored
32 ignore-date use current timestamp for author date
cb6020b @gitster Teach --[no-]rerere-autoupdate option to merge, revert and friends
authored
33 rerere-autoupdate update the index with reused conflict resolution if possible
98ef23b @jaysoffian git-am: minor cleanups
jaysoffian authored
34 rebasing* (internal use for git-rebase)"
78443d9 @MadCoder Migrate git-am.sh to use git-rev-parse --parseopt
MadCoder authored
35
cf1fe88 git-am: Usage string clean-up
freku045@student.liu.se authored
36 . git-sh-setup
bb034f8 @gitster am: read from the right mailbox when started from a subdirectory
authored
37 prefix=$(git rev-parse --show-prefix)
f947413 @spearce Use GIT_REFLOG_ACTION environment variable instead.
spearce authored
38 set_reflog_action am
7eff28a @spearce Disallow working directory commands in a bare repository.
spearce authored
39 require_work_tree
c149184 @peff allow git-am to run in a subdirectory
peff authored
40 cd_to_toplevel
d1c5f2a Add git-am, applymbox replacement.
Junio C Hamano authored
41
460abee @sbeyer git-am: Do not exit silently if committer is unset
sbeyer authored
42 git var GIT_COMMITTER_IDENT >/dev/null ||
43 die "You need to set your committer info first"
d64e6b0 Keep Porcelainish from failing by broken ident after making changes.
Junio C Hamano authored
44
f79d4c8 git-am: teach git-am to apply a patch to an unborn branch
Nanako Shiraishi authored
45 if git rev-parse --verify -q HEAD >/dev/null
46 then
47 HAS_HEAD=yes
48 else
49 HAS_HEAD=
50 fi
51
b47dfe9 @gitster git-am: add --directory=<dir> option
authored
52 sq () {
47c9739 @chriscool am: simplify "sq" function by using "git rev-parse --sq-quote"
chriscool authored
53 git rev-parse --sq-quote "$@"
b47dfe9 @gitster git-am: add --directory=<dir> option
authored
54 }
55
d1c5f2a Add git-am, applymbox replacement.
Junio C Hamano authored
56 stop_here () {
57 echo "$1" >"$dotest/next"
58 exit 1
59 }
60
ced9456 Give the user a hint for how to continue in the case that git-am fail…
Robert Shearman authored
61 stop_here_user_resolve () {
cc12005 @loops Make git rebase interactive help match documentation.
loops authored
62 if [ -n "$resolvemsg" ]; then
a23bfae @peff More echo "$user_message" fixes.
peff authored
63 printf '%s\n' "$resolvemsg"
cc12005 @loops Make git rebase interactive help match documentation.
loops authored
64 stop_here $1
65 fi
75e1645 git-am: remove dash from help message
Olivier Marin authored
66 cmdline="git am"
ced9456 Give the user a hint for how to continue in the case that git-am fail…
Robert Shearman authored
67 if test '' != "$interactive"
68 then
69 cmdline="$cmdline -i"
70 fi
71 if test '' != "$threeway"
72 then
73 cmdline="$cmdline -3"
74 fi
75 echo "When you have resolved this problem run \"$cmdline --resolved\"."
76 echo "If you would prefer to skip this patch, instead run \"$cmdline --skip\"."
3e5057a git am --abort
Nanako Shiraishi authored
77 echo "To restore the original branch and stop patching run \"$cmdline --abort\"."
ced9456 Give the user a hint for how to continue in the case that git-am fail…
Robert Shearman authored
78
79 stop_here $1
80 }
81
d1c5f2a Add git-am, applymbox replacement.
Junio C Hamano authored
82 go_next () {
83 rm -f "$dotest/$msgnum" "$dotest/msg" "$dotest/msg-clean" \
84 "$dotest/patch" "$dotest/info"
85 echo "$next" >"$dotest/next"
86 this=$next
87 }
88
fd7bcfb git-am: give better diagnostics when the patch does not apply during …
Junio C Hamano authored
89 cannot_fallback () {
90 echo "$1"
91 echo "Cannot fall back to three-way merge."
92 exit 1
93 }
94
d1c5f2a Add git-am, applymbox replacement.
Junio C Hamano authored
95 fall_back_3way () {
96 O_OBJECT=`cd "$GIT_OBJECT_DIRECTORY" && pwd`
97
98 rm -fr "$dotest"/patch-merge-*
99 mkdir "$dotest/patch-merge-tmp-dir"
100
101 # First see if the patch records the index info that we can use.
7a98869 @dscho apply: get rid of --index-info in favor of --build-fake-ancestor
dscho authored
102 git apply --build-fake-ancestor "$dotest/patch-merge-tmp-index" \
103 "$dotest/patch" &&
fd7bcfb git-am: give better diagnostics when the patch does not apply during …
Junio C Hamano authored
104 GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
5be6007 @gitster Rewrite "git-frotz" to "git frotz"
authored
105 git write-tree >"$dotest/patch-merge-base+" ||
b1440cc Reword git-am 3-way fallback failure message.
Junio C Hamano authored
106 cannot_fallback "Repository lacks necessary blobs to fall back on 3-way merge."
fd7bcfb git-am: give better diagnostics when the patch does not apply during …
Junio C Hamano authored
107
0e987a1 @bebarino am, rebase: teach quiet option
bebarino authored
108 say Using index info to reconstruct a base tree...
fd7bcfb git-am: give better diagnostics when the patch does not apply during …
Junio C Hamano authored
109 if GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
cb3a160 @sbeyer git-am: ignore --binary option
sbeyer authored
110 git apply --cached <"$dotest/patch"
d1c5f2a Add git-am, applymbox replacement.
Junio C Hamano authored
111 then
112 mv "$dotest/patch-merge-base+" "$dotest/patch-merge-base"
113 mv "$dotest/patch-merge-tmp-index" "$dotest/patch-merge-index"
fd7bcfb git-am: give better diagnostics when the patch does not apply during …
Junio C Hamano authored
114 else
115 cannot_fallback "Did you hand edit your patch?
116 It does not apply to blobs recorded in its index."
d1c5f2a Add git-am, applymbox replacement.
Junio C Hamano authored
117 fi
118
119 test -f "$dotest/patch-merge-index" &&
5be6007 @gitster Rewrite "git-frotz" to "git frotz"
authored
120 his_tree=$(GIT_INDEX_FILE="$dotest/patch-merge-index" git write-tree) &&
d1c5f2a Add git-am, applymbox replacement.
Junio C Hamano authored
121 orig_tree=$(cat "$dotest/patch-merge-base") &&
122 rm -fr "$dotest"/patch-merge-* || exit 1
123
0e987a1 @bebarino am, rebase: teach quiet option
bebarino authored
124 say Falling back to patching base and 3-way merge...
d1c5f2a Add git-am, applymbox replacement.
Junio C Hamano authored
125
126 # This is not so wrong. Depending on which base we picked,
127 # orig_tree may be wildly different from ours, but his_tree
128 # has the same set of wildly different changes in parts the
579c9bb @spearce Use merge-recursive in git-am -3.
spearce authored
129 # patch did not touch, so recursive ends up canceling them,
d1c5f2a Add git-am, applymbox replacement.
Junio C Hamano authored
130 # saying that we reverted all those changes.
131
2e6e3e8 @torvalds git-am: cope better with an empty Subject: line
torvalds authored
132 eval GITHEAD_$his_tree='"$FIRSTLINE"'
579c9bb @spearce Use merge-recursive in git-am -3.
spearce authored
133 export GITHEAD_$his_tree
0e987a1 @bebarino am, rebase: teach quiet option
bebarino authored
134 if test -n "$GIT_QUIET"
135 then
136 export GIT_MERGE_VERBOSITY=0
137 fi
579c9bb @spearce Use merge-recursive in git-am -3.
spearce authored
138 git-merge-recursive $orig_tree -- HEAD $his_tree || {
cb6020b @gitster Teach --[no-]rerere-autoupdate option to merge, revert and friends
authored
139 git rerere $allow_rerere_autoupdate
d1c5f2a Add git-am, applymbox replacement.
Junio C Hamano authored
140 echo Failed to merge in the changes.
141 exit 1
142 }
579c9bb @spearce Use merge-recursive in git-am -3.
spearce authored
143 unset GITHEAD_$his_tree
d1c5f2a Add git-am, applymbox replacement.
Junio C Hamano authored
144 }
145
0cd29a0 @Oblomov git-am: refactor 'cleaning up and aborting'
Oblomov authored
146 clean_abort () {
147 test $# = 0 || echo >&2 "$@"
148 rm -fr "$dotest"
149 exit 1
150 }
151
a5a6755 @Oblomov git-am foreign patch support: introduce patch_format
Oblomov authored
152 patch_format=
153
154 check_patch_format () {
155 # early return if patch_format was set from the command line
156 if test -n "$patch_format"
157 then
158 return 0
159 fi
15ced75 @Oblomov git-am foreign patch support: autodetect some patch formats
Oblomov authored
160
161 # we default to mbox format if input is from stdin and for
162 # directories
163 if test $# = 0 || test "x$1" = "x-" || test -d "$1"
164 then
165 patch_format=mbox
166 return 0
167 fi
168
169 # otherwise, check the first few lines of the first patch to try
170 # to detect its format
171 {
172 read l1
173 read l2
174 read l3
175 case "$l1" in
176 "From "* | "From: "*)
177 patch_format=mbox
178 ;;
179 '# This series applies on GIT commit'*)
180 patch_format=stgit-series
181 ;;
182 "# HG changeset patch")
183 patch_format=hg
184 ;;
185 *)
186 # if the second line is empty and the third is
187 # a From, Author or Date entry, this is very
188 # likely an StGIT patch
189 case "$l2,$l3" in
190 ,"From: "* | ,"Author: "* | ,"Date: "*)
191 patch_format=stgit
192 ;;
193 *)
194 ;;
195 esac
196 ;;
197 esac
0fcb2ca @gitster am: allow individual e-mail files as input
authored
198 if test -z "$patch_format" &&
199 test -n "$l1" &&
200 test -n "$l2" &&
201 test -n "$l3"
202 then
203 # This begins with three non-empty lines. Is this a
204 # piece of e-mail a-la RFC2822? Grab all the headers,
205 # discarding the indented remainder of folded lines,
206 # and see if it looks like that they all begin with the
207 # header field names...
e3f67d3 @bebarino am: fix patch format detection for Thunderbird "Save As" emails
bebarino authored
208 tr -d '\015' <"$1" |
209 sed -n -e '/^$/q' -e '/^[ ]/d' -e p |
e1622bf @gitster Protect scripted Porcelains from GREP_OPTIONS insanity
authored
210 sane_egrep -v '^[!-9;-~]+:' >/dev/null ||
0fcb2ca @gitster am: allow individual e-mail files as input
authored
211 patch_format=mbox
212 fi
0cd29a0 @Oblomov git-am: refactor 'cleaning up and aborting'
Oblomov authored
213 } < "$1" || clean_abort
a5a6755 @Oblomov git-am foreign patch support: introduce patch_format
Oblomov authored
214 }
215
216 split_patches () {
217 case "$patch_format" in
218 mbox)
c2ca1d7 @gitster Allow mailsplit (and hence git-am) to handle mails with CRLF line-end…
authored
219 case "$rebasing" in
220 '')
221 keep_cr= ;;
222 ?*)
223 keep_cr=--keep-cr ;;
224 esac
225 git mailsplit -d"$prec" -o"$dotest" -b $keep_cr -- "$@" > "$dotest/last" ||
0cd29a0 @Oblomov git-am: refactor 'cleaning up and aborting'
Oblomov authored
226 clean_abort
a5a6755 @Oblomov git-am foreign patch support: introduce patch_format
Oblomov authored
227 ;;
c574e68 @Oblomov git-am foreign patch support: StGIT support
Oblomov authored
228 stgit-series)
229 if test $# -ne 1
230 then
0cd29a0 @Oblomov git-am: refactor 'cleaning up and aborting'
Oblomov authored
231 clean_abort "Only one StGIT patch series can be applied at once"
c574e68 @Oblomov git-am foreign patch support: StGIT support
Oblomov authored
232 fi
233 series_dir=`dirname "$1"`
234 series_file="$1"
235 shift
236 {
237 set x
238 while read filename
239 do
240 set "$@" "$series_dir/$filename"
241 done
242 # remove the safety x
243 shift
244 # remove the arg coming from the first-line comment
245 shift
0cd29a0 @Oblomov git-am: refactor 'cleaning up and aborting'
Oblomov authored
246 } < "$series_file" || clean_abort
c574e68 @Oblomov git-am foreign patch support: StGIT support
Oblomov authored
247 # set the patch format appropriately
248 patch_format=stgit
249 # now handle the actual StGIT patches
250 split_patches "$@"
251 ;;
252 stgit)
253 this=0
254 for stgit in "$@"
255 do
256 this=`expr "$this" + 1`
257 msgnum=`printf "%0${prec}d" $this`
258 # Perl version of StGIT parse_patch. The first nonemptyline
259 # not starting with Author, From or Date is the
260 # subject, and the body starts with the next nonempty
261 # line not starting with Author, From or Date
262 perl -ne 'BEGIN { $subject = 0 }
263 if ($subject > 1) { print ; }
264 elsif (/^\s+$/) { next ; }
265 elsif (/^Author:/) { print s/Author/From/ ; }
266 elsif (/^(From|Date)/) { print ; }
267 elsif ($subject) {
268 $subject = 2 ;
269 print "\n" ;
270 print ;
271 } else {
272 print "Subject: ", $_ ;
273 $subject = 1;
274 }
0cd29a0 @Oblomov git-am: refactor 'cleaning up and aborting'
Oblomov authored
275 ' < "$stgit" > "$dotest/$msgnum" || clean_abort
c574e68 @Oblomov git-am foreign patch support: StGIT support
Oblomov authored
276 done
277 echo "$this" > "$dotest/last"
278 this=
279 msgnum=
280 ;;
a5a6755 @Oblomov git-am foreign patch support: introduce patch_format
Oblomov authored
281 *)
46caf50 @nicolas33 git-am: print fair error message when format detection fails
nicolas33 authored
282 if test -n "$parse_patch" ; then
283 clean_abort "Patch format $patch_format is not supported."
284 else
285 clean_abort "Patch format detection failed."
286 fi
a5a6755 @Oblomov git-am foreign patch support: introduce patch_format
Oblomov authored
287 ;;
288 esac
289 }
290
d1c5f2a Add git-am, applymbox replacement.
Junio C Hamano authored
291 prec=4
51ef1da @dscho Rename .git/rebase to .git/rebase-apply
dscho authored
292 dotest="$GIT_DIR/rebase-apply"
a200337 @gitster git-am: propagate -C<n>, -p<n> options as well
authored
293 sign= utf8=t keep= skip= interactive= resolved= rebasing= abort=
d25e515 @luksan git am/mailinfo: Don't look at in-body headers when rebasing
luksan authored
294 resolvemsg= resume= scissors= no_inbody_headers=
67dad68 add -C[NUM] to git-am
Michael S. Tsirkin authored
295 git_apply_opt=
3f01ad6 @gitster am: Add --committer-date-is-author-date option
authored
296 committer_date_is_author_date=
a79ec62 git-am: Add --ignore-date option
Nanako Shiraishi authored
297 ignore_date=
cb6020b @gitster Teach --[no-]rerere-autoupdate option to merge, revert and friends
authored
298 allow_rerere_autoupdate=
d1c5f2a Add git-am, applymbox replacement.
Junio C Hamano authored
299
822f7c7 Supplant the "while case ... break ;; esac" idiom
David Kastrup authored
300 while test $# != 0
d1c5f2a Add git-am, applymbox replacement.
Junio C Hamano authored
301 do
302 case "$1" in
78443d9 @MadCoder Migrate git-am.sh to use git-rev-parse --parseopt
MadCoder authored
303 -i|--interactive)
304 interactive=t ;;
305 -b|--binary)
cb3a160 @sbeyer git-am: ignore --binary option
sbeyer authored
306 : ;;
78443d9 @MadCoder Migrate git-am.sh to use git-rev-parse --parseopt
MadCoder authored
307 -3|--3way)
308 threeway=t ;;
dfdd7e6 Give git-am back the ability to add Signed-off-by lines.
Johannes Sixt authored
309 -s|--signoff)
78443d9 @MadCoder Migrate git-am.sh to use git-rev-parse --parseopt
MadCoder authored
310 sign=t ;;
311 -u|--utf8)
312 utf8=t ;; # this is now default
313 --no-utf8)
314 utf8= ;;
315 -k|--keep)
316 keep=t ;;
017678b @gitster am/mailinfo: Disable scissors processing by default
authored
317 -c|--scissors)
318 scissors=t ;;
319 --no-scissors)
320 scissors=f ;;
78443d9 @MadCoder Migrate git-am.sh to use git-rev-parse --parseopt
MadCoder authored
321 -r|--resolved)
322 resolved=t ;;
323 --skip)
324 skip=t ;;
3e5057a git am --abort
Nanako Shiraishi authored
325 --abort)
326 abort=t ;;
3041c32 @gitster am: --rebasing
authored
327 --rebasing)
d25e515 @luksan git am/mailinfo: Don't look at in-body headers when rebasing
luksan authored
328 rebasing=t threeway=t keep=t scissors=f no_inbody_headers=t ;;
78443d9 @MadCoder Migrate git-am.sh to use git-rev-parse --parseopt
MadCoder authored
329 -d|--dotest)
e72c740 @gitster am: remove support for -d .dotest
authored
330 die "-d option is no longer supported. Do not use."
331 ;;
78443d9 @MadCoder Migrate git-am.sh to use git-rev-parse --parseopt
MadCoder authored
332 --resolvemsg)
333 shift; resolvemsg=$1 ;;
b47dfe9 @gitster git-am: add --directory=<dir> option
authored
334 --whitespace|--directory)
335 git_apply_opt="$git_apply_opt $(sq "$1=$2")"; shift ;;
78443d9 @MadCoder Migrate git-am.sh to use git-rev-parse --parseopt
MadCoder authored
336 -C|-p)
b47dfe9 @gitster git-am: add --directory=<dir> option
authored
337 git_apply_opt="$git_apply_opt $(sq "$1$2")"; shift ;;
a5a6755 @Oblomov git-am foreign patch support: introduce patch_format
Oblomov authored
338 --patch-format)
339 shift ; patch_format="$1" ;;
86c91f9 @Oblomov git apply: option to ignore whitespace differences
Oblomov authored
340 --reject|--ignore-whitespace|--ignore-space-change)
b80da42 @madduck git-am: implement --reject option passed to git-apply
madduck authored
341 git_apply_opt="$git_apply_opt $1" ;;
3f01ad6 @gitster am: Add --committer-date-is-author-date option
authored
342 --committer-date-is-author-date)
343 committer_date_is_author_date=t ;;
a79ec62 git-am: Add --ignore-date option
Nanako Shiraishi authored
344 --ignore-date)
345 ignore_date=t ;;
cb6020b @gitster Teach --[no-]rerere-autoupdate option to merge, revert and friends
authored
346 --rerere-autoupdate|--no-rerere-autoupdate)
347 allow_rerere_autoupdate="$1" ;;
0e987a1 @bebarino am, rebase: teach quiet option
bebarino authored
348 -q|--quiet)
349 GIT_QUIET=t ;;
d1c5f2a Add git-am, applymbox replacement.
Junio C Hamano authored
350 --)
78443d9 @MadCoder Migrate git-am.sh to use git-rev-parse --parseopt
MadCoder authored
351 shift; break ;;
d1c5f2a Add git-am, applymbox replacement.
Junio C Hamano authored
352 *)
78443d9 @MadCoder Migrate git-am.sh to use git-rev-parse --parseopt
MadCoder authored
353 usage ;;
d1c5f2a Add git-am, applymbox replacement.
Junio C Hamano authored
354 esac
78443d9 @MadCoder Migrate git-am.sh to use git-rev-parse --parseopt
MadCoder authored
355 shift
d1c5f2a Add git-am, applymbox replacement.
Junio C Hamano authored
356 done
357
0c15cc9 git-am: --resolved.
Junio C Hamano authored
358 # If the dotest directory exists, but we have finished applying all the
359 # patches in them, clear it out.
d1c5f2a Add git-am, applymbox replacement.
Junio C Hamano authored
360 if test -d "$dotest" &&
361 last=$(cat "$dotest/last") &&
362 next=$(cat "$dotest/next") &&
363 test $# != 0 &&
364 test "$next" -gt "$last"
365 then
366 rm -fr "$dotest"
367 fi
368
369 if test -d "$dotest"
370 then
3e5057a git am --abort
Nanako Shiraishi authored
371 case "$#,$skip$resolved$abort" in
c95b138 Fix git-am safety checks
Junio C Hamano authored
372 0,*t*)
373 # Explicit resume command and we do not have file, so
374 # we are happy.
375 : ;;
376 0,)
377 # No file input but without resume parameters; catch
378 # user error to feed us a patch from standard input
e72c740 @gitster am: remove support for -d .dotest
authored
379 # when there is already $dotest. This is somewhat
c95b138 Fix git-am safety checks
Junio C Hamano authored
380 # unreliable -- stdin could be /dev/null for example
381 # and the caller did not intend to feed us a patch but
382 # wanted to continue unattended.
98ef23b @jaysoffian git-am: minor cleanups
jaysoffian authored
383 test -t 0
c95b138 Fix git-am safety checks
Junio C Hamano authored
384 ;;
385 *)
386 false
387 ;;
388 esac ||
28ed6e7 @dscho Rename ".dotest/" to ".git/rebase" and ".dotest-merge" to "rebase-merge"
dscho authored
389 die "previous rebase directory $dotest still exists but mbox given."
271440e git-am: make it easier after fixing up an unapplicable patch.
Junio C Hamano authored
390 resume=yes
3e5057a git am --abort
Nanako Shiraishi authored
391
95f8ebb git am --skip: clean the index while preserving local changes
Olivier Marin authored
392 case "$skip,$abort" in
2d56a13 @gitster git-am: make --abort less dangerous
authored
393 t,t)
394 die "Please make up your mind. --skip or --abort?"
395 ;;
95f8ebb git am --skip: clean the index while preserving local changes
Olivier Marin authored
396 t,)
397 git rerere clear
398 git read-tree --reset -u HEAD HEAD
399 orig_head=$(cat "$GIT_DIR/ORIG_HEAD")
400 git reset HEAD
401 git update-ref ORIG_HEAD $orig_head
402 ;;
403 ,t)
2d56a13 @gitster git-am: make --abort less dangerous
authored
404 if test -f "$dotest/rebasing"
405 then
406 exec git rebase --abort
407 fi
3e5057a git am --abort
Nanako Shiraishi authored
408 git rerere clear
c767184 @mjg git-am: Keep index in case of abort with dirty index
mjg authored
409 test -f "$dotest/dirtyindex" || {
410 git read-tree --reset -u HEAD ORIG_HEAD
411 git reset ORIG_HEAD
412 }
3e5057a git am --abort
Nanako Shiraishi authored
413 rm -fr "$dotest"
414 exit ;;
415 esac
c767184 @mjg git-am: Keep index in case of abort with dirty index
mjg authored
416 rm -f "$dotest/dirtyindex"
d1c5f2a Add git-am, applymbox replacement.
Junio C Hamano authored
417 else
3e5057a git am --abort
Nanako Shiraishi authored
418 # Make sure we are not given --skip, --resolved, nor --abort
419 test "$skip$resolved$abort" = "" ||
cc12005 @loops Make git rebase interactive help match documentation.
loops authored
420 die "Resolve operation not in progress, we are not resuming."
d1c5f2a Add git-am, applymbox replacement.
Junio C Hamano authored
421
422 # Start afresh.
423 mkdir -p "$dotest" || exit
424
bb034f8 @gitster am: read from the right mailbox when started from a subdirectory
authored
425 if test -n "$prefix" && test $# != 0
426 then
427 first=t
428 for arg
429 do
430 test -n "$first" && {
431 set x
432 first=
433 }
434 case "$arg" in
435 /*)
436 set "$@" "$arg" ;;
437 *)
438 set "$@" "$prefix$arg" ;;
439 esac
440 done
441 shift
442 fi
a5a6755 @Oblomov git-am foreign patch support: introduce patch_format
Oblomov authored
443
444 check_patch_format "$@"
445
446 split_patches "$@"
d1c5f2a Add git-am, applymbox replacement.
Junio C Hamano authored
447
017678b @gitster am/mailinfo: Disable scissors processing by default
authored
448 # -i can and must be given when resuming; everything
449 # else is kept
9b9f5a2 @gitster git-am: rename apply_opt_extra file to apply-opt
authored
450 echo " $git_apply_opt" >"$dotest/apply-opt"
22db240 @gitster git-am: propagate --3way options as well
authored
451 echo "$threeway" >"$dotest/threeway"
d1c5f2a Add git-am, applymbox replacement.
Junio C Hamano authored
452 echo "$sign" >"$dotest/sign"
453 echo "$utf8" >"$dotest/utf8"
454 echo "$keep" >"$dotest/keep"
017678b @gitster am/mailinfo: Disable scissors processing by default
authored
455 echo "$scissors" >"$dotest/scissors"
d25e515 @luksan git am/mailinfo: Don't look at in-body headers when rebasing
luksan authored
456 echo "$no_inbody_headers" >"$dotest/no_inbody_headers"
0e987a1 @bebarino am, rebase: teach quiet option
bebarino authored
457 echo "$GIT_QUIET" >"$dotest/quiet"
d1c5f2a Add git-am, applymbox replacement.
Junio C Hamano authored
458 echo 1 >"$dotest/next"
3041c32 @gitster am: --rebasing
authored
459 if test -n "$rebasing"
460 then
461 : >"$dotest/rebasing"
462 else
463 : >"$dotest/applying"
f79d4c8 git-am: teach git-am to apply a patch to an unborn branch
Nanako Shiraishi authored
464 if test -n "$HAS_HEAD"
465 then
466 git update-ref ORIG_HEAD HEAD
467 else
468 git update-ref -d ORIG_HEAD >/dev/null 2>&1
469 fi
3041c32 @gitster am: --rebasing
authored
470 fi
d1c5f2a Add git-am, applymbox replacement.
Junio C Hamano authored
471 fi
472
0c15cc9 git-am: --resolved.
Junio C Hamano authored
473 case "$resolved" in
474 '')
f79d4c8 git-am: teach git-am to apply a patch to an unborn branch
Nanako Shiraishi authored
475 case "$HAS_HEAD" in
476 '')
477 files=$(git ls-files) ;;
478 ?*)
479 files=$(git diff-index --cached --name-only HEAD --) ;;
480 esac || exit
c767184 @mjg git-am: Keep index in case of abort with dirty index
mjg authored
481 if test "$files"
482 then
f79d4c8 git-am: teach git-am to apply a patch to an unborn branch
Nanako Shiraishi authored
483 test -n "$HAS_HEAD" && : >"$dotest/dirtyindex"
c767184 @mjg git-am: Keep index in case of abort with dirty index
mjg authored
484 die "Dirty index: cannot apply patches (dirty: $files)"
485 fi
0c15cc9 git-am: --resolved.
Junio C Hamano authored
486 esac
487
d1c5f2a Add git-am, applymbox replacement.
Junio C Hamano authored
488 if test "$(cat "$dotest/utf8")" = t
489 then
490 utf8=-u
bb1091a -u is now default for 'git-mailinfo'.
Junio C Hamano authored
491 else
492 utf8=-n
d1c5f2a Add git-am, applymbox replacement.
Junio C Hamano authored
493 fi
494 if test "$(cat "$dotest/keep")" = t
495 then
496 keep=-k
497 fi
017678b @gitster am/mailinfo: Disable scissors processing by default
authored
498 case "$(cat "$dotest/scissors")" in
499 t)
500 scissors=--scissors ;;
501 f)
502 scissors=--no-scissors ;;
503 esac
d25e515 @luksan git am/mailinfo: Don't look at in-body headers when rebasing
luksan authored
504 if test "$(cat "$dotest/no_inbody_headers")" = t
505 then
506 no_inbody_headers=--no-inbody-headers
507 else
508 no_inbody_headers=
509 fi
0e987a1 @bebarino am, rebase: teach quiet option
bebarino authored
510 if test "$(cat "$dotest/quiet")" = t
511 then
512 GIT_QUIET=t
513 fi
22db240 @gitster git-am: propagate --3way options as well
authored
514 if test "$(cat "$dotest/threeway")" = t
515 then
516 threeway=t
517 fi
9b9f5a2 @gitster git-am: rename apply_opt_extra file to apply-opt
authored
518 git_apply_opt=$(cat "$dotest/apply-opt")
d1c5f2a Add git-am, applymbox replacement.
Junio C Hamano authored
519 if test "$(cat "$dotest/sign")" = t
520 then
5354a56 @tmzullinger Replace uses of "git-var" with "git var"
tmzullinger authored
521 SIGNOFF=`git var GIT_COMMITTER_IDENT | sed -e '
d1c5f2a Add git-am, applymbox replacement.
Junio C Hamano authored
522 s/>.*/>/
523 s/^/Signed-off-by: /'
524 `
525 else
526 SIGNOFF=
527 fi
528
529 last=`cat "$dotest/last"`
530 this=`cat "$dotest/next"`
531 if test "$skip" = t
532 then
533 this=`expr "$this" + 1`
6922471 @jaharkes Fix git-am --skip
jaharkes authored
534 resume=
d1c5f2a Add git-am, applymbox replacement.
Junio C Hamano authored
535 fi
536
537 if test "$this" -gt "$last"
538 then
0e987a1 @bebarino am, rebase: teach quiet option
bebarino authored
539 say Nothing to do.
d1c5f2a Add git-am, applymbox replacement.
Junio C Hamano authored
540 rm -fr "$dotest"
541 exit
542 fi
543
544 while test "$this" -le "$last"
545 do
546 msgnum=`printf "%0${prec}d" $this`
547 next=`expr "$this" + 1`
548 test -f "$dotest/$msgnum" || {
6922471 @jaharkes Fix git-am --skip
jaharkes authored
549 resume=
d1c5f2a Add git-am, applymbox replacement.
Junio C Hamano authored
550 go_next
551 continue
552 }
0c15cc9 git-am: --resolved.
Junio C Hamano authored
553
554 # If we are not resuming, parse and extract the patch information
555 # into separate files:
556 # - info records the authorship and title
557 # - msg is the rest of commit log message
558 # - patch is the patch body.
559 #
560 # When we are resuming, these files are either already prepared
561 # by the user, or the user can tell us to do so by --resolved flag.
271440e git-am: make it easier after fixing up an unapplicable patch.
Junio C Hamano authored
562 case "$resume" in
563 '')
d25e515 @luksan git am/mailinfo: Don't look at in-body headers when rebasing
luksan authored
564 git mailinfo $keep $no_inbody_headers $scissors $utf8 "$dotest/msg" "$dotest/patch" \
271440e git-am: make it easier after fixing up an unapplicable patch.
Junio C Hamano authored
565 <"$dotest/$msgnum" >"$dotest/info" ||
566 stop_here $this
ca193cf @dscho git am: skip pine's internal folder data
dscho authored
567
568 # skip pine's internal folder data
e1622bf @gitster Protect scripted Porcelains from GREP_OPTIONS insanity
authored
569 sane_grep '^Author: Mail System Internal Data$' \
ca193cf @dscho git am: skip pine's internal folder data
dscho authored
570 <"$dotest"/info >/dev/null &&
571 go_next && continue
572
28ed6e7 @dscho Rename ".dotest/" to ".git/rebase" and ".dotest-merge" to "rebase-merge"
dscho authored
573 test -s "$dotest/patch" || {
6777c38 @joshtriplett Fix typo in git-am: s/Was is/Was it/
joshtriplett authored
574 echo "Patch is empty. Was it split wrong?"
87ab799 builtin-mailinfo.c infrastrcture changes
Don Zickus authored
575 stop_here $this
576 }
5e835ca @gitster rebase: do not munge commit log message
authored
577 if test -f "$dotest/rebasing" &&
578 commit=$(sed -e 's/^From \([0-9a-f]*\) .*/\1/' \
579 -e q "$dotest/$msgnum") &&
580 test "$(git cat-file -t "$commit")" = commit
581 then
582 git cat-file commit "$commit" |
583 sed -e '1,/^$/d' >"$dotest/msg-clean"
584 else
c970a6f @gitster Remove dead code from "git am"
authored
585 {
586 sed -n '/^Subject/ s/Subject: //p' "$dotest/info"
587 echo
588 cat "$dotest/msg"
589 } |
590 git stripspace > "$dotest/msg-clean"
5e835ca @gitster rebase: do not munge commit log message
authored
591 fi
271440e git-am: make it easier after fixing up an unapplicable patch.
Junio C Hamano authored
592 ;;
593 esac
d1c5f2a Add git-am, applymbox replacement.
Junio C Hamano authored
594
595 GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$dotest/info")"
596 GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$dotest/info")"
597 GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$dotest/info")"
e0e3ba2 mailinfo and git-am: allow "John Doe <johndoe>"
Junio C Hamano authored
598
3b78959 @gitster Revert "git-am: catch missing author date early."
authored
599 if test -z "$GIT_AUTHOR_EMAIL"
e0e3ba2 mailinfo and git-am: allow "John Doe <johndoe>"
Junio C Hamano authored
600 then
3b78959 @gitster Revert "git-am: catch missing author date early."
authored
601 echo "Patch does not have a valid e-mail address."
e0e3ba2 mailinfo and git-am: allow "John Doe <johndoe>"
Junio C Hamano authored
602 stop_here $this
603 fi
604
2c67419 Fix missing exports in git-am
Junio C Hamano authored
605 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
d1c5f2a Add git-am, applymbox replacement.
Junio C Hamano authored
606
4bfb6b6 git-am: do not lose already edited final-commit when resuming.
Junio C Hamano authored
607 case "$resume" in
608 '')
609 if test '' != "$SIGNOFF"
610 then
d1c5f2a Add git-am, applymbox replacement.
Junio C Hamano authored
611 LAST_SIGNED_OFF_BY=`
4bfb6b6 git-am: do not lose already edited final-commit when resuming.
Junio C Hamano authored
612 sed -ne '/^Signed-off-by: /p' \
613 "$dotest/msg-clean" |
b4ce54f @peff remove use of "tail -n 1" and "tail -1"
peff authored
614 sed -ne '$p'
d1c5f2a Add git-am, applymbox replacement.
Junio C Hamano authored
615 `
4bfb6b6 git-am: do not lose already edited final-commit when resuming.
Junio C Hamano authored
616 ADD_SIGNOFF=`
617 test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || {
d1c5f2a Add git-am, applymbox replacement.
Junio C Hamano authored
618 test '' = "$LAST_SIGNED_OFF_BY" && echo
619 echo "$SIGNOFF"
4bfb6b6 git-am: do not lose already edited final-commit when resuming.
Junio C Hamano authored
620 }`
621 else
d1c5f2a Add git-am, applymbox replacement.
Junio C Hamano authored
622 ADD_SIGNOFF=
4bfb6b6 git-am: do not lose already edited final-commit when resuming.
Junio C Hamano authored
623 fi
624 {
d1c5f2a Add git-am, applymbox replacement.
Junio C Hamano authored
625 if test -s "$dotest/msg-clean"
626 then
627 cat "$dotest/msg-clean"
628 fi
629 if test '' != "$ADD_SIGNOFF"
630 then
631 echo "$ADD_SIGNOFF"
632 fi
4bfb6b6 git-am: do not lose already edited final-commit when resuming.
Junio C Hamano authored
633 } >"$dotest/final-commit"
634 ;;
0c15cc9 git-am: --resolved.
Junio C Hamano authored
635 *)
6d28644 git-am: do not allow empty commits by mistake.
Junio C Hamano authored
636 case "$resolved$interactive" in
0c15cc9 git-am: --resolved.
Junio C Hamano authored
637 tt)
638 # This is used only for interactive view option.
38762c4 @gitster scripts: do not get confused with HEAD in work tree
authored
639 git diff-index -p --cached HEAD -- >"$dotest/patch"
0c15cc9 git-am: --resolved.
Junio C Hamano authored
640 ;;
641 esac
4bfb6b6 git-am: do not lose already edited final-commit when resuming.
Junio C Hamano authored
642 esac
d1c5f2a Add git-am, applymbox replacement.
Junio C Hamano authored
643
4bfb6b6 git-am: do not lose already edited final-commit when resuming.
Junio C Hamano authored
644 resume=
d1c5f2a Add git-am, applymbox replacement.
Junio C Hamano authored
645 if test "$interactive" = t
646 then
a145110 git-am: interactive should fail gracefully.
Junio C Hamano authored
647 test -t 0 ||
648 die "cannot be interactive without stdin connected to a terminal."
d1c5f2a Add git-am, applymbox replacement.
Junio C Hamano authored
649 action=again
650 while test "$action" = again
651 do
652 echo "Commit Body is:"
653 echo "--------------------------"
654 cat "$dotest/final-commit"
655 echo "--------------------------"
9754563 Use printf rather than echo -n.
Jason Riedy authored
656 printf "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all "
d1c5f2a Add git-am, applymbox replacement.
Junio C Hamano authored
657 read reply
658 case "$reply" in
f89ad67 Add [v]iew patch in git-am interactive.
Junio C Hamano authored
659 [yY]*) action=yes ;;
660 [aA]*) action=yes interactive= ;;
661 [nN]*) action=skip ;;
ef0c2ab @aroben Add GIT_EDITOR environment and core.editor configuration variables
aroben authored
662 [eE]*) git_editor "$dotest/final-commit"
d1c5f2a Add git-am, applymbox replacement.
Junio C Hamano authored
663 action=again ;;
f89ad67 Add [v]iew patch in git-am interactive.
Junio C Hamano authored
664 [vV]*) action=again
dec543e @jrn am -i, git-svn: use "git var GIT_PAGER"
jrn authored
665 : ${GIT_PAGER=$(git var GIT_PAGER)}
666 : ${LESS=-FRSX}
667 export LESS
668 $GIT_PAGER "$dotest/patch" ;;
f89ad67 Add [v]iew patch in git-am interactive.
Junio C Hamano authored
669 *) action=again ;;
d1c5f2a Add git-am, applymbox replacement.
Junio C Hamano authored
670 esac
671 done
672 else
673 action=yes
674 fi
1d9b265 @amery git-am: head -1 is obsolete and doesn't work on some new systems
amery authored
675 FIRSTLINE=$(sed 1q "$dotest/final-commit")
d1c5f2a Add git-am, applymbox replacement.
Junio C Hamano authored
676
677 if test $action = skip
678 then
679 go_next
680 continue
681 fi
682
683 if test -x "$GIT_DIR"/hooks/applypatch-msg
684 then
685 "$GIT_DIR"/hooks/applypatch-msg "$dotest/final-commit" ||
686 stop_here $this
687 fi
688
0e987a1 @bebarino am, rebase: teach quiet option
bebarino authored
689 say "Applying: $FIRSTLINE"
d1c5f2a Add git-am, applymbox replacement.
Junio C Hamano authored
690
0c15cc9 git-am: --resolved.
Junio C Hamano authored
691 case "$resolved" in
692 '')
3ddd170 @bebarino am: suppress apply errors when using 3-way
bebarino authored
693 # When we are allowed to fall back to 3-way later, don't give
694 # false errors during the initial attempt.
695 squelch=
696 if test "$threeway" = t
697 then
698 squelch='>/dev/null 2>&1 '
699 fi
700 eval "git apply $squelch$git_apply_opt"' --index "$dotest/patch"'
0c15cc9 git-am: --resolved.
Junio C Hamano authored
701 apply_status=$?
702 ;;
703 t)
704 # Resolved means the user did all the hard work, and
705 # we do not have to do any patch application. Just
706 # trust what the user has in the index file and the
707 # working tree.
708 resolved=
38762c4 @gitster scripts: do not get confused with HEAD in work tree
authored
709 git diff-index --quiet --cached HEAD -- && {
64646d1 @spearce Suggest 'add' in am/revert/cherry-pick.
spearce authored
710 echo "No changes - did you forget to use 'git add'?"
ced9456 Give the user a hint for how to continue in the case that git-am fail…
Robert Shearman authored
711 stop_here_user_resolve $this
06aff47 @raalkml Use diff* with --exit-code in git-am, git-rebase and git-merge-ours
raalkml authored
712 }
5be6007 @gitster Rewrite "git-frotz" to "git frotz"
authored
713 unmerged=$(git ls-files -u)
c1d1128 git-am --resolved: more usable error message.
Junio C Hamano authored
714 if test -n "$unmerged"
715 then
716 echo "You still have unmerged paths in your index"
64646d1 @spearce Suggest 'add' in am/revert/cherry-pick.
spearce authored
717 echo "did you forget to use 'git add'?"
ced9456 Give the user a hint for how to continue in the case that git-am fail…
Robert Shearman authored
718 stop_here_user_resolve $this
c1d1128 git-am --resolved: more usable error message.
Junio C Hamano authored
719 fi
0c15cc9 git-am: --resolved.
Junio C Hamano authored
720 apply_status=0
b4372ef @dscho Enable "git rerere" by the config variable rerere.enabled
dscho authored
721 git rerere
0c15cc9 git-am: --resolved.
Junio C Hamano authored
722 ;;
723 esac
724
d1c5f2a Add git-am, applymbox replacement.
Junio C Hamano authored
725 if test $apply_status = 1 && test "$threeway" = t
726 then
7331903 git-am: do not fail if 3-way fallback succeeds.
Junio C Hamano authored
727 if (fall_back_3way)
d1c5f2a Add git-am, applymbox replacement.
Junio C Hamano authored
728 then
7331903 git-am: do not fail if 3-way fallback succeeds.
Junio C Hamano authored
729 # Applying the patch to an earlier tree and merging the
730 # result may have produced the same tree as ours.
38762c4 @gitster scripts: do not get confused with HEAD in work tree
authored
731 git diff-index --quiet --cached HEAD -- && {
0e987a1 @bebarino am, rebase: teach quiet option
bebarino authored
732 say No changes -- Patch already applied.
06aff47 @raalkml Use diff* with --exit-code in git-am, git-rebase and git-merge-ours
raalkml authored
733 go_next
734 continue
735 }
7331903 git-am: do not fail if 3-way fallback succeeds.
Junio C Hamano authored
736 # clear apply_status -- we have successfully merged.
737 apply_status=0
d1c5f2a Add git-am, applymbox replacement.
Junio C Hamano authored
738 fi
739 fi
740 if test $apply_status != 0
741 then
4d2e283 @gitster git-am: re-fix the diag message printing
authored
742 printf 'Patch failed at %s %s\n' "$msgnum" "$FIRSTLINE"
ced9456 Give the user a hint for how to continue in the case that git-am fail…
Robert Shearman authored
743 stop_here_user_resolve $this
d1c5f2a Add git-am, applymbox replacement.
Junio C Hamano authored
744 fi
745
746 if test -x "$GIT_DIR"/hooks/pre-applypatch
747 then
748 "$GIT_DIR"/hooks/pre-applypatch || stop_here $this
749 fi
750
5be6007 @gitster Rewrite "git-frotz" to "git frotz"
authored
751 tree=$(git write-tree) &&
3f01ad6 @gitster am: Add --committer-date-is-author-date option
authored
752 commit=$(
a79ec62 git-am: Add --ignore-date option
Nanako Shiraishi authored
753 if test -n "$ignore_date"
754 then
755 GIT_AUTHOR_DATE=
756 fi
f79d4c8 git-am: teach git-am to apply a patch to an unborn branch
Nanako Shiraishi authored
757 parent=$(git rev-parse --verify -q HEAD) ||
0e987a1 @bebarino am, rebase: teach quiet option
bebarino authored
758 say >&2 "applying to an empty history"
f79d4c8 git-am: teach git-am to apply a patch to an unborn branch
Nanako Shiraishi authored
759
3f01ad6 @gitster am: Add --committer-date-is-author-date option
authored
760 if test -n "$committer_date_is_author_date"
761 then
762 GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE"
763 export GIT_COMMITTER_DATE
764 fi &&
ea10b60 @bjj Work around ash "alternate value" expansion bug
bjj authored
765 git commit-tree $tree ${parent:+-p} $parent <"$dotest/final-commit"
3f01ad6 @gitster am: Add --committer-date-is-author-date option
authored
766 ) &&
2e6e3e8 @torvalds git-am: cope better with an empty Subject: line
torvalds authored
767 git update-ref -m "$GIT_REFLOG_ACTION: $FIRSTLINE" HEAD $commit $parent ||
d1c5f2a Add git-am, applymbox replacement.
Junio C Hamano authored
768 stop_here $this
769
770 if test -x "$GIT_DIR"/hooks/post-applypatch
771 then
772 "$GIT_DIR"/hooks/post-applypatch
773 fi
774
775 go_next
776 done
777
e0cd252 git-am: Run git gc only once and not for every patch.
Michael Stefaniuc authored
778 git gc --auto
779
d1c5f2a Add git-am, applymbox replacement.
Junio C Hamano authored
780 rm -fr "$dotest"
Something went wrong with that request. Please try again.