889d4c1743ccdfcf8ad0be96a58eb8b60c03ccdc
[git.git] / tools / git-applymbox
1 #!/bin/sh
2 ##
3 ## "dotest" is my stupid name for my patch-application script, which
4 ## I never got around to renaming after I tested it. We're now on the
5 ## second generation of scripts, still called "dotest".
6 ##
7 ## Update: Ryan Anderson finally shamed me into naming this "applymbox".
8 ##
9 ## You give it a mbox-format collection of emails, and it will try to
10 ## apply them to the kernel using "applypatch"
11 ##
12 ## applymbox [ -k ] [ -q ] (-c .dotest/msg-number | mail_archive) [Signoff_file]"
13 ##
14 ## The patch application may fail in the middle.  In which case:
15 ## (1) look at .dotest/patch and fix it up to apply
16 ## (2) re-run applymbox with -c .dotest/msg-number for the current one.
17 ## Pay a special attention to the commit log message if you do this and
18 ## use a Signoff_file, because applypatch wants to append the sign-off
19 ## message to msg-clean every time it is run.
20
21 keep_subject= query_apply= continue= resume=t
22 while case "$#" in 0) break ;; esac
23 do
24         case "$1" in
25         -k)     keep_subject=-k ;;
26         -q)     query_apply=t ;;
27         -c)     continue="$2"; resume=f; shift ;;
28         -*)     usage ;;
29         *)      break ;;
30         esac
31         shift
32 done
33
34 case "$continue" in
35 '')
36         rm -rf .dotest
37         mkdir .dotest
38         git-mailsplit "$1" .dotest || exit 1
39         shift
40 esac
41
42 case "$query_apply" in
43 t)      touch .dotest/.query_apply
44 esac
45 case "$keep_subject" in
46 -k)     : >.dotest/.keep_subject
47 esac
48
49 signoff="$1"
50 set x .dotest/0*
51 shift
52 while case "$#" in 0) break;; esac
53 do
54     i="$1" 
55     case "$resume,$continue" in
56     f,$i)       resume=t;;
57     f,*)        continue;;
58     *)
59             git-mailinfo $keep_subject \
60                 .dotest/msg .dotest/patch <$i >.dotest/info || exit 1
61             git-stripspace < .dotest/msg > .dotest/msg-clean
62             ;;
63     esac
64     while :; # for fixing up and retry
65     do
66         git-applypatch .dotest/msg-clean .dotest/patch .dotest/info "$signoff"
67         case "$?" in
68         0 | 2 )
69                 # 2 is a special exit code from applypatch to indicate that
70                 # the patch wasn't applied, but continue anyway 
71                 ;;
72         *)
73                 ret=$?
74                 if test -f .dotest/.query_apply
75                 then
76                         echo >&2 "* Patch failed."
77                         echo >&2 "* You could fix it up in your editor and"
78                         echo >&2 "  retry.  If you want to do so, say yes here"
79                         echo >&2 "  AFTER fixing .dotest/patch up."
80                         echo >&2 -n "Retry [y/N]? "
81                         read yesno
82                         case "$yesno" in
83                         [Yy]*)
84                                 continue ;;
85                         esac
86                 fi
87                 exit $ret
88         esac
89         break
90     done
91     shift
92 done
93 # return to pristine
94 rm -fr .dotest