Git 2.2
[git/git.git] / git-quiltimport.sh
CommitLineData
d3d8f361 1#!/bin/sh
e01fbf1a 2OPTIONS_KEEPDASHDASH=
51ba8ce3 3OPTIONS_STUCKLONG=
e01fbf1a 4OPTIONS_SPEC="\
1b1dd23f 5git quiltimport [options]
e01fbf1a
PH
6--
7n,dry-run dry run
8author= author name and email address for patches without any
9patches= path to the quilt series and patches
10"
d3d8f361
EB
11SUBDIRECTORY_ON=Yes
12. git-sh-setup
13
d3bd4ee1 14dry_run=""
d3d8f361 15quilt_author=""
822f7c73 16while test $# != 0
d3d8f361
EB
17do
18 case "$1" in
e01fbf1a 19 --author)
d3d8f361
EB
20 shift
21 quilt_author="$1"
d3d8f361 22 ;;
e01fbf1a 23 -n|--dry-run)
d3bd4ee1
EB
24 dry_run=1
25 ;;
e01fbf1a 26 --patches)
d3d8f361 27 shift
9e384b45 28 QUILT_PATCHES="$1"
d3d8f361 29 ;;
e01fbf1a
PH
30 --)
31 shift
32 break;;
d3d8f361 33 *)
e01fbf1a 34 usage
d3d8f361
EB
35 ;;
36 esac
e01fbf1a 37 shift
d3d8f361
EB
38done
39
40# Quilt Author
41if [ -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" ||
82e5a82f 46 die "malformed --author parameter"
d3d8f361
EB
47fi
48
49# Quilt patch directory
50: ${QUILT_PATCHES:=patches}
51if ! [ -d "$QUILT_PATCHES" ] ; then
52 echo "The \"$QUILT_PATCHES\" directory does not exist."
53 exit 1
54fi
55
3dff5379 56# Temporary directories
51ef1daa 57tmp_dir="$GIT_DIR"/rebase-apply
d3d8f361
EB
58tmp_msg="$tmp_dir/msg"
59tmp_patch="$tmp_dir/patch"
60tmp_info="$tmp_dir/info"
61
62
41ccfdd9 63# Find the initial commit
5be60078 64commit=$(git rev-parse HEAD)
d3d8f361
EB
65
66mkdir $tmp_dir || exit 2
6ab149ea 67while read patch_name level garbage <&3
9dd5bded
PH
68do
69 case "$patch_name" in ''|'#'*) continue;; esac
70 case "$level" in
18d077c1 71 -p*) ;;
9dd5bded
PH
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
9f569fe5
DN
84 if ! [ -f "$QUILT_PATCHES/$patch_name" ] ; then
85 echo "$patch_name doesn't exist. Skipping."
86 continue
87 fi
d3d8f361 88 echo $patch_name
9d6f220c
JT
89 git mailinfo "$tmp_msg" "$tmp_patch" \
90 <"$QUILT_PATCHES/$patch_name" >"$tmp_info" || exit 3
91 test -s "$tmp_patch" || {
7d4f4a2f 92 echo "Patch is empty. Was it split wrong?"
1fa9bf36 93 exit 1
87ab7992 94 }
d3d8f361
EB
95
96 # Parse the author information
3addc94a
JS
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
d3d8f361
EB
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";
d3bd4ee1
EB
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";
d3d8f361 108 else
d3bd4ee1 109 echo "No author found in $patch_name" >&2;
d3d8f361
EB
110 echo "---"
111 cat $tmp_msg
2aad957a 112 printf "Author: ";
d3d8f361
EB
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
3addc94a
JS
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
d3d8f361
EB
128 if [ -z "$SUBJECT" ] ; then
129 SUBJECT=$(echo $patch_name | sed -e 's/.patch$//')
130 fi
131
d3bd4ee1 132 if [ -z "$dry_run" ] ; then
18d077c1 133 git apply --index -C1 ${level:+"$level"} "$tmp_patch" &&
5be60078
JH
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
d3bd4ee1 137 fi
6ab149ea 138done 3<"$QUILT_PATCHES/series"
d3d8f361 139rm -rf $tmp_dir || exit 5