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