summaryrefslogtreecommitdiff
path: root/get-patch
diff options
context:
space:
mode:
authorPeter Wu <lekensteyn@gmail.com>2013-10-28 11:02:47 +0100
committerPeter Wu <lekensteyn@gmail.com>2013-10-28 11:02:47 +0100
commit85c7562d28a02c2b8d86f24ef7611757db4b89ed (patch)
treec31c1ee3120dd9c141936b195f2e5835aac9bc2d /get-patch
parent7867923e113c53472ffc9acbc55b80d167103161 (diff)
downloadscripts-85c7562d28a02c2b8d86f24ef7611757db4b89ed.tar.gz
get-patch: download a git patch
Given a URL (and optionally a subject), determine the patch location and save to a file based on the sanitized subject line.
Diffstat (limited to 'get-patch')
-rwxr-xr-xget-patch51
1 files changed, 51 insertions, 0 deletions
diff --git a/get-patch b/get-patch
new file mode 100755
index 0000000..c0a34bb
--- /dev/null
+++ b/get-patch
@@ -0,0 +1,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"