Merge branch 'mm/multimail-1.5'
[git] / git-quiltimport.sh
1 #!/bin/sh
2 OPTIONS_KEEPDASHDASH=
3 OPTIONS_STUCKLONG=
4 OPTIONS_SPEC="\
5 git quiltimport [options]
6 --
7 n,dry-run     dry run
8 author=       author name and email address for patches without any
9 patches=      path to the quilt patches
10 series=       path to the quilt series file
11 keep-non-patch Pass -b to git mailinfo
12 "
13 SUBDIRECTORY_ON=Yes
14 . git-sh-setup
15
16 dry_run=""
17 quilt_author=""
18 while test $# != 0
19 do
20         case "$1" in
21         --author)
22                 shift
23                 quilt_author="$1"
24                 ;;
25         -n|--dry-run)
26                 dry_run=1
27                 ;;
28         --patches)
29                 shift
30                 QUILT_PATCHES="$1"
31                 ;;
32         --series)
33                 shift
34                 QUILT_SERIES="$1"
35                 ;;
36         --keep-non-patch)
37                 MAILINFO_OPT="-b"
38                 ;;
39         --)
40                 shift
41                 break;;
42         *)
43                 usage
44                 ;;
45         esac
46         shift
47 done
48
49 # Quilt Author
50 if [ -n "$quilt_author" ] ; then
51         quilt_author_name=$(expr "z$quilt_author" : 'z\(.*[^ ]\) *<.*') &&
52         quilt_author_email=$(expr "z$quilt_author" : '.*<\([^>]*\)') &&
53         test '' != "$quilt_author_name" &&
54         test '' != "$quilt_author_email" ||
55         die "malformed --author parameter"
56 fi
57
58 # Quilt patch directory
59 : ${QUILT_PATCHES:=patches}
60 if ! [ -d "$QUILT_PATCHES" ] ; then
61         echo "The \"$QUILT_PATCHES\" directory does not exist."
62         exit 1
63 fi
64
65 # Quilt series file
66 : ${QUILT_SERIES:=$QUILT_PATCHES/series}
67 if ! [ -e "$QUILT_SERIES" ] ; then
68         echo "The \"$QUILT_SERIES\" file does not exist."
69         exit 1
70 fi
71
72 # Temporary directories
73 tmp_dir="$GIT_DIR"/rebase-apply
74 tmp_msg="$tmp_dir/msg"
75 tmp_patch="$tmp_dir/patch"
76 tmp_info="$tmp_dir/info"
77
78
79 # Find the initial commit
80 commit=$(git rev-parse HEAD)
81
82 mkdir $tmp_dir || exit 2
83 while read patch_name level garbage <&3
84 do
85         case "$patch_name" in ''|'#'*) continue;; esac
86         case "$level" in
87         -p*)    ;;
88         ''|'#'*)
89                 level=;;
90         *)
91                 echo "unable to parse patch level, ignoring it."
92                 level=;;
93         esac
94         case "$garbage" in
95         ''|'#'*);;
96         *)
97                 echo "trailing garbage found in series file: $garbage"
98                 exit 1;;
99         esac
100         if ! [ -f "$QUILT_PATCHES/$patch_name" ] ; then
101                 echo "$patch_name doesn't exist. Skipping."
102                 continue
103         fi
104         echo $patch_name
105         git mailinfo $MAILINFO_OPT "$tmp_msg" "$tmp_patch" \
106                 <"$QUILT_PATCHES/$patch_name" >"$tmp_info" || exit 3
107         test -s "$tmp_patch" || {
108                 echo "Patch is empty.  Was it split wrong?"
109                 exit 1
110         }
111
112         # Parse the author information
113         GIT_AUTHOR_NAME=$(sed -ne 's/Author: //p' "$tmp_info")
114         GIT_AUTHOR_EMAIL=$(sed -ne 's/Email: //p' "$tmp_info")
115         export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL
116         while test -z "$GIT_AUTHOR_EMAIL" && test -z "$GIT_AUTHOR_NAME" ; do
117                 if [ -n "$quilt_author" ] ; then
118                         GIT_AUTHOR_NAME="$quilt_author_name";
119                         GIT_AUTHOR_EMAIL="$quilt_author_email";
120                 elif [ -n "$dry_run" ]; then
121                         echo "No author found in $patch_name" >&2;
122                         GIT_AUTHOR_NAME="dry-run-not-found";
123                         GIT_AUTHOR_EMAIL="dry-run-not-found";
124                 else
125                         echo "No author found in $patch_name" >&2;
126                         echo "---"
127                         cat $tmp_msg
128                         printf "Author: ";
129                         read patch_author
130
131                         echo "$patch_author"
132
133                         patch_author_name=$(expr "z$patch_author" : 'z\(.*[^ ]\) *<.*') &&
134                         patch_author_email=$(expr "z$patch_author" : '.*<\([^>]*\)') &&
135                         test '' != "$patch_author_name" &&
136                         test '' != "$patch_author_email" &&
137                         GIT_AUTHOR_NAME="$patch_author_name" &&
138                         GIT_AUTHOR_EMAIL="$patch_author_email"
139                 fi
140         done
141         GIT_AUTHOR_DATE=$(sed -ne 's/Date: //p' "$tmp_info")
142         SUBJECT=$(sed -ne 's/Subject: //p' "$tmp_info")
143         export GIT_AUTHOR_DATE SUBJECT
144         if [ -z "$SUBJECT" ] ; then
145                 SUBJECT=$(echo $patch_name | sed -e 's/.patch$//')
146         fi
147
148         if [ -z "$dry_run" ] ; then
149                 git apply --index -C1 ${level:+"$level"} "$tmp_patch" &&
150                 tree=$(git write-tree) &&
151                 commit=$( (echo "$SUBJECT"; echo; cat "$tmp_msg") | git commit-tree $tree -p $commit) &&
152                 git update-ref -m "quiltimport: $patch_name" HEAD $commit || exit 4
153         fi
154 done 3<"$QUILT_SERIES"
155 rm -rf $tmp_dir || exit 5