maketag.sh: adjust defaults for OpenWrt
[maintainer-tools.git] / patchwork-apply.sh
1 #!/usr/bin/env bash
2
3 yesno() {
4 local prompt="$1"
5 local default="${2:-n}"
6 local input
7
8 while [ 1 ]; do
9 printf "%s y/n [%s] > " "$prompt" "$default"
10 read input
11 case "${input:-$default}" in
12 y*) return 0 ;;
13 n*) return 1 ;;
14 esac
15 done
16 }
17
18 fetch() {(
19 set -e
20 mkdir "pwclient.get.$$"
21 cd "pwclient.get.$$"
22 pwclient get "$1"
23 mv * "../$1.patch"
24 cd ..
25 rmdir "pwclient.get.$$"
26 )}
27
28 get_date() {
29 date +"%a, %d %b %Y %H:%M:%S %z" | sed -e 's|, 0|, |'
30 }
31
32 get_subject() {
33 local subject line
34 local IFS="
35 "
36
37 for line in $(sed -ne '/^Subject: */ { s/^Subject: *//p; :next; n; s/^ \+//p; t next; b }' "$1"); do
38 subject="$subject$line"
39 done
40
41 printf "%s\n" "$subject" | sed -e 's/^\[.*\] \+//'
42 }
43
44 get_hdr_list() {
45 local file="$1"
46 local field="$2"
47 local addr list
48
49 local IFS=",
50 "
51
52 for addr in $(sed -ne "/^$field: */ { s/^$field: *//p; :next; n; s/^ \\+//p; t next; b }" "$file"); do
53 list="${list:+$list, }$(echo "$addr" | sed -e 's/^ \+//; s/ \+$//')"
54 done
55
56 [ -n "$list" ] && printf "%s: %s\n" "$field" "$list"
57 }
58
59 get_hdr() {
60 sed -ne "s/^$2: *//p" "$1" | head -n1
61 }
62
63 format_reply() {
64 local remote_ref remote_url remote_host remote_host remote_repo remote_user
65
66 remote_ref="$(git for-each-ref --format='%(push:short)' $(git symbolic-ref -q HEAD))"
67
68 [ -n "$remote_ref" ] || \
69 remote_ref="$(git for-each-ref --format='%(upstream:short)' $(git symbolic-ref -q HEAD))"
70
71 [ -n "$remote_ref" ] || \
72 return 1
73
74 remote_url="$(git remote get-url "${remote_ref%%/*}")"
75
76 case "$remote_url" in
77 http*://*)
78 remote_host="${remote_url##http*://}"
79 case "$remote_host" in *@*)
80 remote_user="${remote_host%%@*}"
81 remote_host="${remote_host##*@}"
82 esac
83 case "$remote_host" in */*)
84 remote_repo="${remote_host#*/}"
85 remote_host="${remote_host%%/*}"
86 esac
87 ;;
88 *:*)
89 remote_host="$remote_url"
90 case "$remote_host" in *@*)
91 remote_user="${remote_host%%@*}"
92 remote_host="${remote_host##*@}"
93 esac
94 case "$remote_host" in *:*)
95 remote_repo="${remote_host##*:}"
96 remote_host="${remote_host%%:*}"
97 esac
98 ;;
99 esac
100
101 case "$remote_host" in
102 git.openwrt.org|git.lede-project.org)
103 case "$remote_repo" in
104 source.git|openwrt/openwrt.git)
105 echo "Merged into ${remote_ref##*/}."
106 ;;
107 lede/*/staging.git|openwrt/staging/*.git)
108 echo "Merged into my staging tree."
109 ;;
110 *)
111 echo "Merged into ${remote_repo:-the repository}, branch ${remote_ref##*/}."
112 ;;
113 esac
114 ;;
115 *)
116 echo "Merged."
117 ;;
118 esac
119
120 echo "Thank you!"
121 echo ""
122 }
123
124 echo "$1" | grep -sqE '^[0-9]+$' || {
125 echo "Usage: $0 <patch-id>" >&2
126 exit 1
127 }
128
129 [ -f "$1.patch" ] || {
130 pwclient info "$1" >/dev/null 2>/dev/null || {
131 echo "Unknown patch ID: $1" >&2
132 exit 2
133 }
134
135 fetch "$1" || {
136 echo "Failed to download patch" >&2
137 exit 3
138 }
139 }
140
141 git am "$1.patch" || {
142 echo "Failed to apply patch $1" >&2
143 git am --abort
144 exit 4
145 }
146
147 git log -p -1
148
149 if ! yesno "Keep change?" "y"; then
150 git reset --hard HEAD^
151
152 if yesno "Set to 'Changes Requested'?"; then
153 pwclient update -s "Changes Requested" "$1"
154 fi
155 else
156 if yesno "Set to 'Accepted'?" "y"; then
157 pwclient update -s "Accepted" "$1"
158
159 if yesno "Send reply mail?" "y"; then
160 {
161 printf "From: %s <%s>\n" "$(git config user.name)" "$(git config user.email)"
162
163 get_hdr_list "$1.patch" Cc
164
165 printf "Date: %s\n" "$(get_date)"
166 printf "Subject: Merged: %s\n\n" "$(get_subject "$1.patch")"
167
168 format_reply
169 } > "$1.reply"
170
171 echo "==="
172 cat "$1.reply"
173 echo "==="
174
175 if yesno "Edit reply?" "n"; then
176 git send-email \
177 --to "$(get_hdr "$1.patch" To)" \
178 --cc "$(get_hdr "$1.patch" From)" \
179 --in-reply-to "$(get_hdr "$1.patch" Message-Id)" \
180 --compose "$1.reply"
181 else
182 git send-email \
183 --to "$(get_hdr "$1.patch" To)" \
184 --cc "$(get_hdr "$1.patch" From)" \
185 --in-reply-to "$(get_hdr "$1.patch" Message-Id)" \
186 --confirm=never "$1.reply"
187 fi
188
189 rm -f "$1.reply" "$1.patch"
190 fi
191 fi
192 fi