[PATCH] Make "git resolve" less scary
[git/dscho.git] / tools / git-applymbox
blob2b32dab5f54e8542fc6f5be481fd0f0f8f461137
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"
12 ## applymbox [-u] [-k] [-q] (-c .dotest/msg-number | mail_archive) [Signoff_file]"
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.
21 . git-sh-setup-script || die "Not a git archive"
23 usage () {
24 echo >&2 "applymbox [-u] [-k] [-q] (-c .dotest/<num> | mbox) [signoff]"
25 exit 1
28 keep_subject= query_apply= continue= utf8= resume=t
29 while case "$#" in 0) break ;; esac
31 case "$1" in
32 -u) utf8=-u ;;
33 -k) keep_subject=-k ;;
34 -q) query_apply=t ;;
35 -c) continue="$2"; resume=f; shift ;;
36 -*) usage ;;
37 *) break ;;
38 esac
39 shift
40 done
42 case "$continue" in
43 '')
44 rm -rf .dotest
45 mkdir .dotest
46 git-mailsplit "$1" .dotest || exit 1
47 shift
48 esac
50 files=$(git-diff-cache --cached --name-only HEAD) || exit
51 if [ "$files" ]; then
52 echo "Dirty index: cannot apply patches (dirty: $files)" >&2
53 exit 1
56 case "$query_apply" in
57 t) touch .dotest/.query_apply
58 esac
59 case "$keep_subject" in
60 -k) : >.dotest/.keep_subject
61 esac
63 signoff="$1"
64 set x .dotest/0*
65 shift
66 while case "$#" in 0) break;; esac
68 i="$1"
69 case "$resume,$continue" in
70 f,$i) resume=t;;
71 f,*) continue;;
73 git-mailinfo $keep_subject $utf8 \
74 .dotest/msg .dotest/patch <$i >.dotest/info || exit 1
75 git-stripspace < .dotest/msg > .dotest/msg-clean
77 esac
78 while :; # for fixing up and retry
80 git-applypatch .dotest/msg-clean .dotest/patch .dotest/info "$signoff"
81 case "$?" in
82 0 | 2 )
83 # 2 is a special exit code from applypatch to indicate that
84 # the patch wasn't applied, but continue anyway
87 ret=$?
88 if test -f .dotest/.query_apply
89 then
90 echo >&2 "* Patch failed."
91 echo >&2 "* You could fix it up in your editor and"
92 echo >&2 " retry. If you want to do so, say yes here"
93 echo >&2 " AFTER fixing .dotest/patch up."
94 echo >&2 -n "Retry [y/N]? "
95 read yesno
96 case "$yesno" in
97 [Yy]*)
98 continue ;;
99 esac
101 exit $ret
102 esac
103 break
104 done
105 shift
106 done
107 # return to pristine
108 rm -fr .dotest