Commit Diff


commit - /dev/null
commit + db6c077fdc0951c3e3dfb092d0cf7eac52da2be4
blob - /dev/null
blob + f1b7f9e6c0d5d9d35f3f6372ae6a5448bb3fa043 (mode 755)
--- /dev/null
+++ got-archive
@@ -0,0 +1,80 @@
+#!/bin/sh
+
+die() {
+	ec=$1
+	shift
+	echo "$@" >&2
+	exit "$ec"
+}
+
+usage() {
+	die 1 "Usage: got-archive [-s] [-b branch/tag] [-o outdir] [-t tmpdir] repo"
+}
+
+args=$(getopt b:o:st: $*)
+[ $? -ne 0 ] && usage
+
+set -- $args
+
+strip=0
+branch=
+outdir=.
+tmpdir="/tmp/got-archive"
+
+while [ $# -ne 0 ]; do
+	case "$1" in
+	-s)
+		strip=1
+		shift
+		;;
+	-b)
+		branch=$2
+		shift
+		shift
+		;;
+	-o)
+		outdir=$2
+		shift
+		shift
+		;;
+	-t)
+		tmpdir=$2
+		shift
+		shift
+		;;
+	--)
+		shift
+		break
+		;;
+	*)
+		shift
+		exit 1
+		;;
+	esac
+done
+
+[ $# -ne 1 ] && usage
+
+repo=$1
+name=$(basename "$repo" | sed 's/\.git$//')
+if [ "$strip" = 1 ]; then
+	ver=$(echo "$branch" | sed 's/^v//g')
+else
+	ver=$branch
+fi
+
+[ -d "$repo" ] || die 2 "Error: invalid repo: $repo"
+
+mkdir -p "$tmpdir"
+
+if [ "$branch" ]; then
+	cname="$name-$ver"
+	got checkout -q -b "$branch" "$repo" "$tmpdir/$cname" || die 3 "Error: failed to checkout"
+else
+	cname="$name"
+	got checkout -q "$repo" "$tmpdir/$cname" || die 3 "Error: failed to checkout"
+fi
+
+rm -rf "$tmpdir/$cname/.got"
+tar -czf "$outdir/$cname.tgz" -C "$tmpdir" "$cname" || die 4 "Error: failed to create tarball"
+rm -rf "$tmpdir/$cname"