From 85c7562d28a02c2b8d86f24ef7611757db4b89ed Mon Sep 17 00:00:00 2001 From: Peter Wu Date: Mon, 28 Oct 2013 11:02:47 +0100 Subject: 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. --- get-patch | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100755 get-patch (limited to 'get-patch') 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" -- cgit v1.2.1