#!/bin/sh die() { ec=$1 shift echo "$@" >&2 exit "$ec" } usage() { die 1 "Usage: got-archive [-afsV] [-o outdir] [-t tmpdir] repo [ref...]" } args=$(getopt afo:st:V $*) [ $? -ne 0 ] && usage set -- $args strip=0 force=0 outdir=. tmpdir="/tmp/got-archive" while [ $# -ne 0 ]; do case "$1" in -a) all=1 shift ;; -f) force=1 shift ;; -o) outdir=$2 shift shift ;; -s) strip=1 shift ;; -t) tmpdir=$2 shift shift ;; -V) echo "got-archive-@VERSION@" exit 1 ;; --) shift break ;; esac done [ $# -lt 1 ] && usage repo=$1 name=$(basename "$repo" .git) mkdir -p "$tmpdir" shift [ -d "$repo" ] || die 2 "Error: invalid repo: $repo" # $1 - ref # $2 - name gen() { cdir="$tmpdir/$2" tarfile="$outdir/$2.tgz" [ -e "$tarfile" ] && [ "$force" = 0 ] && return rm -rf "$cdir" got checkout -q -c "$1" "$repo" "$cdir" || die 3 "Error: $2: failed to checkout: $2" rm -rf "$cdir/.got" tar -czf "$tarfile" -C "$tmpdir" "$2" || die 4 "Error: $2: failed to create tarball: $tarfile" rm -rf "$cdir" } if [ "$all" = 1 ]; then [ $# -ne 0 ] && die 1 "Error: Can't combine -a and explicit refs" tags=$(find "$repo/refs/tags" -type f -exec basename {} \; 2>/dev/null) [ $? -ne 0 ] && exit 0 set -- $tags fi mkdir -p "$outdir" if [ $# -gt 0 ]; then while [ $# -ne 0 ]; do if [ "$strip" = 1 ]; then ref=$(echo "$1" | sed 's/^v//g') else ref=$1 fi gen "$1" "$name-$ref" shift done else gen "HEAD" "$name" fi