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