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