Skip to content

Instantly share code, notes, and snippets.

@gnue
Created June 13, 2012 04:00
Show Gist options
  • Save gnue/2921792 to your computer and use it in GitHub Desktop.
Save gnue/2921792 to your computer and use it in GitHub Desktop.
git に export サブコマンドを追加する(tgz, tb2, zip, ディレクトリ出力も簡単)
#!/bin/bash
die() {
echo "$1" 1>&2
exit 1
}
usage() {
die "Usage: $(basename $0) [-o outfile] <tree-ish> [<path>...]"
}
dir="${PWD##*/}"
while getopts "o:h" opts; do
case $opts in
o) shift; outfile=$OPTARG; shift;;
h) shift; usage;;
esac
done
[ -z "$outfile" ] && outfile="$dir.tgz"
[ -z "$1" ] && usage
subdir="$(basename ${outfile%.*})"
case "${outfile##*.}" in
tgz) git archive --prefix="$subdir/" --format=tar "$@" | gzip > "$outfile";;
tb2|tbz2) git archive --prefix="$subdir/" "$@" | bzip2 > "$outfile";;
zip) git archive --prefix="$subdir/" --format=zip -o "$outfile" "$@";;
*) git archive --prefix="${outfile##*/}/" --format=tar "$@" | tar -C "$(dirname "$outfile")" -xf -;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment