git-tar-tree: no more void pointer arithmetic
[git.git] / git-applymbox.sh
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 ## The patch application may fail in the middle.  In which case:
13 ## (1) look at .dotest/patch and fix it up to apply
14 ## (2) re-run applymbox with -c .dotest/msg-number for the current one.
15 ## Pay a special attention to the commit log message if you do this and
16 ## use a Signoff_file, because applypatch wants to append the sign-off
17 ## message to msg-clean every time it is run.
18 ##
19 ## git-am is supposed to be the newer and better tool for this job.
20
21 USAGE='[-u] [-k] [-q] [-m] (-c .dotest/<num> | mbox) [signoff]'
22 . git-sh-setup
23
24 git var GIT_COMMITTER_IDENT >/dev/null || exit
25
26 keep_subject= query_apply= continue= utf8= resume=t
27 while case "$#" in 0) break ;; esac
28 do
29         case "$1" in
30         -u)     utf8=-u ;;
31         -k)     keep_subject=-k ;;
32         -q)     query_apply=t ;;
33         -c)     continue="$2"; resume=f; shift ;;
34         -m)     fall_back_3way=t ;;
35         -*)     usage ;;
36         *)      break ;;
37         esac
38         shift
39 done
40
41 case "$continue" in
42 '')
43         rm -rf .dotest
44         mkdir .dotest
45         num_msgs=$(git-mailsplit "$1" .dotest) || exit 1
46         echo "$num_msgs patch(es) to process."
47         shift
48 esac
49
50 files=$(git-diff-index --cached --name-only HEAD) || exit
51 if [ "$files" ]; then
52    echo "Dirty index: cannot apply patches (dirty: $files)" >&2
53    exit 1
54 fi
55
56 case "$query_apply" in
57 t)      touch .dotest/.query_apply
58 esac
59 case "$fall_back_3way" in
60 t)      : >.dotest/.3way
61 esac
62 case "$keep_subject" in
63 -k)     : >.dotest/.keep_subject
64 esac
65
66 signoff="$1"
67 set x .dotest/0*
68 shift
69 while case "$#" in 0) break;; esac
70 do
71     i="$1" 
72     case "$resume,$continue" in
73     f,$i)       resume=t;;
74     f,*)        shift
75                 continue;;
76     *)
77             git-mailinfo $keep_subject $utf8 \
78                 .dotest/msg .dotest/patch <$i >.dotest/info || exit 1
79             git-stripspace < .dotest/msg > .dotest/msg-clean
80             ;;
81     esac
82     while :; # for fixing up and retry
83     do
84         git-applypatch .dotest/msg-clean .dotest/patch .dotest/info "$signoff"
85         case "$?" in
86         0)
87                 # Remove the cleanly applied one to reduce clutter.
88                 rm -f .dotest/$i
89                 ;;
90         2)
91                 # 2 is a special exit code from applypatch to indicate that
92                 # the patch wasn't applied, but continue anyway 
93                 ;;
94         *)
95                 ret=$?
96                 if test -f .dotest/.query_apply
97                 then
98                         echo >&2 "* Patch failed."
99                         echo >&2 "* You could fix it up in your editor and"
100                         echo >&2 "  retry.  If you want to do so, say yes here"
101                         echo >&2 "  AFTER fixing .dotest/patch up."
102                         echo >&2 -n "Retry [y/N]? "
103                         read yesno
104                         case "$yesno" in
105                         [Yy]*)
106                                 continue ;;
107                         esac
108                 fi
109                 exit $ret
110         esac
111         break
112     done
113     shift
114 done
115 # return to pristine
116 rm -fr .dotest