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