summaryrefslogtreecommitdiff
path: root/get-patch
blob: c0a34bbb921d4d9e1ba483bdf252fd0901360507 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/bin/bash
url=$1
subject=$2
outfile=

case $url in
*/gitweb/*)
	url=$(sed -r 's/([;?])a=[^;]*/\1a=patch/' <<<"$url")
	;;
esac

if [ -z "$url" ]; then
	echo "Usage: $0 patch-url [subject]"
	exit
fi

normalize_name() {
	tr -c a-zA-Z0-9._ - | sed 's/--*/-/g;s/^[.-]*//;s/[.-]*$//' | cut -c1-50
}
find_subject() {
	awk '/^ /{if(s)s=s$0}
	{if(s)exit}
	/^Subject:/{s=s$0}
	END{sub(/^Subject: \[PATCH[^\]]*\] /,"",s);print s}'
}

subject=$(echo "$subject" | normalize_name)

if [ -n "$subject" ]; then
	outfile="$subject.patch"
	wget -O "$outfile" "$url" || exit 1
else
	outfile="$(mktemp patch-XXXXXXXXXX)"
	if wget -O "$outfile" "$url"; then
		subject=$(find_subject <"$outfile")
		if [ -z "$subject" ]; then
			echo "Subject not found!"
			rm "$outfile"
			exit 1
		fi

		patchfile="$(echo "$subject" | normalize_name).patch"
		mv "$outfile" "$patchfile"
		outfile="$patchfile"
	else
		rm "$outfile"
		exit 1
	fi
fi

echo "$outfile"