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