| 1 |
#!/bin/sh |
| 2 |
set -e |
| 3 |
|
| 4 |
TEMP_D="" |
| 5 |
sourcename="cloud-utils" |
| 6 |
|
| 7 |
cleanup() { |
| 8 |
[ -z "$TEMP_D" ] || rm -Rf "${TEMP_D}" |
| 9 |
} |
| 10 |
trap cleanup EXIT |
| 11 |
|
| 12 |
Usage() { |
| 13 |
cat <<EOF |
| 14 |
Usage: ${0##*/} [committish] |
| 15 |
create a tarball of committish(default HEAD) |
| 16 |
|
| 17 |
options: |
| 18 |
-h | --help print usage |
| 19 |
-o | --output FILE write to file |
| 20 |
--orig-tarball Write file ${sourcename}_<version>.orig.tar.gz |
| 21 |
--long Use git describe --long for versioning |
| 22 |
EOF |
| 23 |
} |
| 24 |
|
| 25 |
short_opts="ho:v" |
| 26 |
long_opts="help,output:,orig-tarball,long" |
| 27 |
getopt_out=$(getopt --name "${0##*/}" \ |
| 28 |
--options "${short_opts}" --long "${long_opts}" -- "$@") && |
| 29 |
eval set -- "${getopt_out}" || { Usage 1>&2; exit 1; } |
| 30 |
|
| 31 |
long_opt="" |
| 32 |
orig_opt="" |
| 33 |
while [ $# -ne 0 ]; do |
| 34 |
cur=$1; next=$2 |
| 35 |
case "$cur" in |
| 36 |
-h|--help) Usage; exit 0;; |
| 37 |
-o|--output) output=$next; shift;; |
| 38 |
--long) long_opt="--long";; |
| 39 |
--orig-tarball) orig_opt=".orig";; |
| 40 |
--) shift; break;; |
| 41 |
esac |
| 42 |
shift; |
| 43 |
done |
| 44 |
|
| 45 |
rev=${1:-HEAD} |
| 46 |
version=$(git describe --abbrev=8 "--match=[0-9]*" ${long_opt} $rev) |
| 47 |
|
| 48 |
archive_base="${sourcename}-$version" |
| 49 |
if [ -z "$output" ]; then |
| 50 |
if [ ! -z "$orig_opt" ]; then |
| 51 |
archive_base="${sourcename}_$version" |
| 52 |
fi |
| 53 |
output="$archive_base$orig_opt.tar.gz" |
| 54 |
fi |
| 55 |
|
| 56 |
# when building an archiving from HEAD, ensure that there aren't any |
| 57 |
# uncommitted changes in the working directory (because these would not |
| 58 |
# end up in the archive). |
| 59 |
if [ "$rev" = HEAD ] && ! git diff-index --quiet HEAD --; then |
| 60 |
cat 1>&2 <<EOF |
| 61 |
WARNING: There are uncommitted changes in your working directory. |
| 62 |
These changes will not be included in the archive. |
| 63 |
EOF |
| 64 |
fi |
| 65 |
|
| 66 |
TEMP_D=$(mktemp -d) |
| 67 |
tar=${output##*/} |
| 68 |
tar="$TEMP_D/${tar%.gz}" |
| 69 |
git archive --format=tar --prefix="$archive_base/" "$rev" > "$tar" |
| 70 |
gzip -9 -c "$tar" > "$output" |
| 71 |
echo "$output" |