quilt fetch: pull series and patches over ftp/http

I have seen a number of people posting patch series on ftp and http sites but
there is no good way of pulling these series into a tree, until now.  :)

example usage:

quilt fetch http://www.kernel.org/pub/linux/kernel/people/akpm/patches/2.6/2.6.2-rc2/2.6.2-rc2-mm1/broken-out/series

This will download all patches in the series file and import them into the tree
in the proper order.

Signed-off-by: Brandon Philips <brandon@ifup.org>

---
 quilt/fetch.in |  135 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 135 insertions(+)

Index: quilt/quilt/fetch.in
===================================================================
--- /dev/null
+++ quilt/quilt/fetch.in
@@ -0,0 +1,135 @@
+#! @BASH@
+
+#  This script is free software; you can redistribute it and/or modify
+#  it under the terms of the GNU General Public License version 2 as
+#  published by the Free Software Foundation.
+#
+#  See the COPYING and AUTHORS files for more details.
+
+# Read in library functions
+if [ "$(type -t patch_file_name)" != function ]
+then
+	if ! [ -r $QUILT_DIR/scripts/patchfns ]
+	then
+		echo "Cannot read library $QUILT_DIR/scripts/patchfns" >&2
+		exit 1
+	fi
+	. $QUILT_DIR/scripts/patchfns
+fi
+
+usage()
+{
+	if [ x$1 = x-h ]
+	then
+		printf $"
+Import all patches in the series file pointed at by URL.  The patches will be
+inserted following the current top patch, and must be pushed after import to
+apply them.
+
+-f	Overwite/update existing patches.
+
+-d {o|a|n}
+	When overwriting in existing patch, keep the old (o), all (a), or
+	new (n) patch header. If both patches include headers, this option
+	must be specified. This option is only effective when -f is used.
+"
+		exit 0
+	else
+		exit 1
+	fi
+}
+
+options=`getopt -o d:fp:Rh -- "$@"`
+
+if [ $? -ne 0 ]
+then
+        usage
+fi
+
+eval set -- "$options"
+
+while true
+do
+        case "$1" in
+	-h)
+		usage -h ;;
+	-d)
+		case "$2" in
+			o|n|a) import_opts+=" -d $2" ;;
+			*) usage ;;
+		esac
+		shift 2 ;;
+	-f)
+		import_opts+=" -f"
+		shift ;;
+        --)
+                shift
+		break ;;
+        esac
+done
+
+get_url_to_tmp()
+{
+	tmp=$(gen_tempfile)
+	curl -s -S -f "$1" -o "$tmp"
+
+	echo "$tmp"
+}
+
+make_url()
+{
+	echo "$1/$2"
+}
+
+check_line()
+{
+	local line=$1
+
+	if [ "$line" = "" ]
+	then
+		return 1
+	fi
+
+	echo "$line" | egrep -q '^[[:space:]]*#.*'
+	if [ $? -eq 0 ]
+	then
+		return 1
+	fi
+
+	echo $line | egrep -q '^[a-zA-Z0-9]+[_a-zA-Z0-9.-]+([[:space:]]\-p[[:space:]]*[0-9]?)?[[:space:]]*$'
+	if [ $? -ne 0 ]
+	then
+		echo "$line is not a valid series entry, exitting"
+		exit 1
+	fi
+
+	return 0
+}
+
+get_patch_name()
+{
+	echo $1 | egrep -o "^[_a-zA-Z0-9\.\-]+"
+}
+
+dirname=`dirname $1`
+
+series=$(get_url_to_tmp $1)
+tac $series | while read line
+do
+	check_line "$line" || continue
+	name=$(get_patch_name $line)
+	echo "Fetching $name"
+	url=$(make_url $dirname $name)
+	tmp=$(get_url_to_tmp $url)
+
+	quilt import $import_opts -P $line $tmp
+
+	rm $tmp
+done
+
+rm $series
+
+### Local Variables:
+### mode: shell-script
+### End:
+# vim:filetype=sh
