Adrian Schröter
e7fbd5920f
Mon Feb 13 15:52:19 GMT 2012 - aspiers@suse.com Add test suite and fix two bugs it found: 1. --subdir was not working 2. --scm bzr was not working FWIW it also works on SLE11 now. I will issue a separate request for my enhancements to tar_scm, since they are much more intrusive (but have about 90% test coverage). OBS-URL: https://build.opensuse.org/request/show/105040 OBS-URL: https://build.opensuse.org/package/show/openSUSE:Tools/obs-service-tar_scm?expand=0&rev=32
305 lines
7.3 KiB
Bash
305 lines
7.3 KiB
Bash
#!/bin/bash
|
|
|
|
# A simple script to checkout or update a svn or git repo as source service
|
|
#
|
|
# (C) 2010 by Adrian Schröter <adrian@suse.de>
|
|
#
|
|
# This program is free software; you can redistribute it and/or
|
|
# modify it under the terms of the GNU General Public License
|
|
# as published by the Free Software Foundation; either version 2
|
|
# of the License, or (at your option) any later version.
|
|
# See http://www.gnu.org/licenses/gpl-2.0.html for full license text.
|
|
|
|
# defaults
|
|
MYSCM=""
|
|
MYURL=""
|
|
MYVERSION="_auto_"
|
|
MYPREFIX=""
|
|
MYFILENAME=""
|
|
MYREVISION=""
|
|
MYPACKAGEMETA=""
|
|
MYGITARGS="--depth 1"
|
|
INCLUDES=""
|
|
|
|
while test $# -gt 0; do
|
|
case $1 in
|
|
*-scm)
|
|
MYSCM="$2"
|
|
shift
|
|
;;
|
|
*-url)
|
|
MYURL="$2"
|
|
shift
|
|
;;
|
|
*-subdir)
|
|
MYSUBDIR="$2"
|
|
shift
|
|
;;
|
|
*-revision)
|
|
MYREVISION="$2"
|
|
shift
|
|
;;
|
|
*-version)
|
|
MYVERSION="$2"
|
|
shift
|
|
;;
|
|
*-include)
|
|
INCLUDES="$INCLUDES $2"
|
|
shift
|
|
;;
|
|
*-versionprefix)
|
|
MYPREFIX="$2"
|
|
shift
|
|
;;
|
|
*-exclude)
|
|
EXCLUDES="$EXCLUDES --exclude=${2#/}"
|
|
shift
|
|
;;
|
|
*-filename)
|
|
MYFILENAME="${2#/}"
|
|
shift
|
|
;;
|
|
*-package-meta)
|
|
MYPACKAGEMETA="${2#/}"
|
|
shift
|
|
;;
|
|
*-outdir)
|
|
MYOUTDIR="$2"
|
|
shift
|
|
;;
|
|
*-history-depth)
|
|
if [ "$2" == "full" ]; then
|
|
MYGITARGS="--depth 999999999"
|
|
else
|
|
MYGITARGS="--depth $2"
|
|
fi
|
|
shift
|
|
;;
|
|
*)
|
|
echo Unknown parameter $1.
|
|
echo 'Usage: tar_scm --scm $SCM --url $URL --outdir $OUT'
|
|
exit 1
|
|
;;
|
|
esac
|
|
shift
|
|
done
|
|
|
|
FILE="$MYFILENAME"
|
|
VERSION="$MYVERSION"
|
|
if [ -z "$MYPACKAGEMETA" ]; then
|
|
EXCLUDES="$EXCLUDES --exclude=.$MYSCM"
|
|
fi
|
|
|
|
if [ -z "$MYSCM" ]; then
|
|
echo "ERROR: no scm is given via --scm parameter (git/svn/hg/bzr)!"
|
|
exit 1
|
|
fi
|
|
if [ -z "$MYURL" ]; then
|
|
echo "ERROR: no checkout URL is given via --url parameter!"
|
|
exit 1
|
|
fi
|
|
if [ -z "$MYOUTDIR" ]; then
|
|
echo "ERROR: no output directory is given via --outdir parameter!"
|
|
exit 1
|
|
fi
|
|
|
|
SRCDIR=$(pwd)
|
|
cd "$MYOUTDIR"
|
|
|
|
if [ -z "$FILE" ]; then
|
|
case "$MYSCM" in
|
|
git)
|
|
FILE="${MYURL%/}"
|
|
FILE="${FILE##*/}"
|
|
FILE="${FILE%.git}"
|
|
FILE="${FILE#*@*:}"
|
|
;;
|
|
svn|hg|bzr)
|
|
FILE="${MYURL%/}"
|
|
FILE="${FILE##*/}"
|
|
;;
|
|
esac
|
|
fi
|
|
|
|
# Try to find an existing tar ball, which can be upgraded instead of complete full download.
|
|
existing_tar=$(echo $SRCDIR/.old/_service:*tar_scm:${FILE}-*.tar*)
|
|
if [ ! -e "$existing_tar" ]; then
|
|
# for OBS < 2.3
|
|
existing_tar=$(echo $SRCDIR/_service:*tar_scm:${FILE}-*.tar*)
|
|
fi
|
|
if [ -e "$existing_tar" ]; then
|
|
UNCOMPRESS="cat"
|
|
if [ "${existing_tar%.tar.gz}" != "$existing_tar" ]; then
|
|
UNCOMPRESS="gunzip -c"
|
|
elif [ "${existing_tar%.tar.bz2}" != "$existing_tar" ]; then
|
|
UNCOMPRESS="bunzip2 -c"
|
|
elif [ "${existing_tar%.tar.xz}" != "$existing_tar" ]; then
|
|
UNCOMPRESS="xz -dc"
|
|
fi
|
|
if $UNCOMPRESS "$existing_tar" | tar xf -; then
|
|
TAR_DIRECTORY=`find * -maxdepth 0 -a -type d`
|
|
else
|
|
existing_tar=""
|
|
fi
|
|
else
|
|
existing_tar=""
|
|
fi
|
|
|
|
if [ "$MYSCM" == "svn" ]; then
|
|
if [[ $(svn --version --quiet) > "1.5.99" ]]; then
|
|
TRUST_SERVER_CERT="--trust-server-cert";
|
|
fi
|
|
if [ -z "$MYSUBDIR" -a -d "$TAR_DIRECTORY/.svn" ]; then
|
|
# update existing content for speed/bandwidth reasons
|
|
cd "$TAR_DIRECTORY"
|
|
OLDVERSION=`LC_ALL=C svn info | sed -n 's,^Last Changed Rev: \(.*\),\1,p'` || exit 1
|
|
if [ -n "$MYREVISION" ]; then
|
|
svn up -r"$MYREVISION" || exit 1
|
|
else
|
|
svn up || exit 1
|
|
fi
|
|
NEWVERSION=`LC_ALL=C svn info | sed -n 's,^Last Changed Rev: \(.*\),\1,p'` || exit 1
|
|
cd -
|
|
mv "$TAR_DIRECTORY" "${FILE}" || exit 1
|
|
else
|
|
# new checkout
|
|
if [ -n "$MYSUBDIR" ]; then
|
|
# just checkout the subdir
|
|
mkdir -p "$FILE"
|
|
cd "$FILE"
|
|
if [ "$MYSUBDIR" != "${MYSUBDIR#/}" ]; then
|
|
echo "ERROR: Absolute paths not permitted for --subdir"
|
|
exit 1
|
|
fi
|
|
fi
|
|
if [ -n "$MYREVISION" ]; then
|
|
svn co --non-interactive $TRUST_SERVER_CERT -r"$MYREVISION" "$MYURL/$MYSUBDIR" "${MYSUBDIR:-$FILE}" || exit 1
|
|
else
|
|
svn co --non-interactive $TRUST_SERVER_CERT "$MYURL/$MYSUBDIR" "${MYSUBDIR:-$FILE}" || exit 1
|
|
fi
|
|
if [ -n "$MYSUBDIR" ]; then
|
|
cd -
|
|
fi
|
|
fi
|
|
if [ "$VERSION" == "_auto_" ]; then
|
|
cd "$FILE/$MYSUBDIR"
|
|
[ -n "$MYPREFIX" ] && MYPREFIX="$MYPREFIX.rev"
|
|
VERSION="$MYPREFIX"`LC_ALL=C svn info | sed -n 's,^Last Changed Rev: \(.*\),\1,p'` || exit 1
|
|
cd -
|
|
fi
|
|
elif [ "$MYSCM" == "git" ]; then
|
|
if [ -z "$MYSUBDIR" -a -d "$TAR_DIRECTORY/.git" ]; then
|
|
# update existing content for speed/bandwidth reasons
|
|
cd "$TAR_DIRECTORY"
|
|
OLDVERSION=`git show --pretty=format:%at | head -n 1`
|
|
git pull $MYGITARGS || exit 1
|
|
if [ -n "$MYREVISION" ]; then
|
|
git checkout "$MYREVISION" || exit 1
|
|
fi
|
|
NEWVERSION=`git show --pretty=format:%at | head -n 1`
|
|
cd -
|
|
mv "$TAR_DIRECTORY" "${FILE}" || exit 1
|
|
else
|
|
# new checkout
|
|
if [ -n "$MYREVISION" ]; then
|
|
#Clone with full depth; so that the revision can be found
|
|
git clone "$MYURL" "${FILE}" || exit 1
|
|
cd "$FILE"
|
|
git checkout "$MYREVISION" || exit 1
|
|
cd -
|
|
else
|
|
#Clone only the latest revision
|
|
git clone --depth 1 "$MYURL" "${FILE}" || exit 1
|
|
fi
|
|
fi
|
|
if [ "$VERSION" == "_auto_" ]; then
|
|
cd "$FILE"
|
|
[ -n "$MYPREFIX" ] && MYPREFIX="$MYPREFIX."
|
|
VERSION="$MYPREFIX"`git show --pretty=format:%at | head -n 1`
|
|
cd -
|
|
fi
|
|
elif [ "$MYSCM" == "hg" ]; then
|
|
if [ -z "$MYSUBDIR" -a -d "$TAR_DIRECTORY/.hg" ]; then
|
|
# update existing content for speed/bandwidth reasons
|
|
cd "$TAR_DIRECTORY"
|
|
OLDVERSION=`hg id -i -rtip`
|
|
hg pull || exit 1
|
|
NEWVERSION=`hg id -i -rtip`
|
|
cd -
|
|
mv "$TAR_DIRECTORY" "${FILE}" || exit 1
|
|
else
|
|
# new checkout
|
|
hg clone "$MYURL" "${FILE}" || exit 1
|
|
fi
|
|
if [ -n "$MYREVISION" ]; then
|
|
cd "$FILE"
|
|
hg update "$MYREVISION" || exit 1
|
|
cd -
|
|
fi
|
|
if [ "$VERSION" == "_auto_" ]; then
|
|
cd "$FILE"
|
|
[ -n "$MYPREFIX" ] && MYPREFIX="$MYPREFIX."
|
|
# current working revision
|
|
VERSION="$MYPREFIX"`hg id -n`
|
|
cd -
|
|
fi
|
|
elif [ "$MYSCM" == "bzr" ]; then
|
|
if [ -z "$MYSUBDIR" -a -d "$TAR_DIRECTORY/.bzr" ]; then
|
|
# update existing content for speed/bandwidth reasons
|
|
cd "$TAR_DIRECTORY"
|
|
OLDVERSION=`bzr revno`
|
|
bzr update || exit 1
|
|
if [ -n "$MYREVISION" ]; then
|
|
bzr revert -r "$MYREVISION" || exit 1
|
|
fi
|
|
NEWVERSION=`bzr revno`
|
|
cd -
|
|
mv "$TAR_DIRECTORY" "${FILE}" || exit 1
|
|
else
|
|
# new checkout
|
|
if [ -n "$MYREVISION" ]; then
|
|
bzr checkout -r "$MYREVISION" "$MYURL" "${FILE}" || exit 1
|
|
else
|
|
bzr checkout "$MYURL" "${FILE}" || exit 1
|
|
fi
|
|
fi
|
|
if [ "$VERSION" == "_auto_" ]; then
|
|
cd "$FILE"
|
|
[ -n "$MYPREFIX" ] && MYPREFIX="$MYPREFIX."
|
|
VERSION="$MYPREFIX"`bzr revno`
|
|
cd -
|
|
fi
|
|
else
|
|
echo "ERROR: unknown scm $MYSCM"
|
|
exit 1
|
|
fi
|
|
|
|
if [ ! -e "$FILE/$MYSUBDIR" ]; then
|
|
echo "Directory does not exist: $FILE/$MYSUBDIR"
|
|
exit 1
|
|
fi
|
|
|
|
if [ -z "$VERSION" ]; then
|
|
FILENAME="$FILE"
|
|
else
|
|
FILENAME="${FILE}-${VERSION}"
|
|
fi
|
|
|
|
MYINCLUDES=""
|
|
|
|
for INC in $INCLUDES; do
|
|
MYINCLUDES="$MYINCLUDES $FILENAME/$INC"
|
|
done
|
|
if [ -z "$MYINCLUDES" ]; then
|
|
MYINCLUDES="$FILENAME"
|
|
fi
|
|
|
|
if [ "$FILE" != "$FILENAME" ]; then
|
|
mv "$FILE/$MYSUBDIR" "${FILENAME}" || exit 1
|
|
fi
|
|
|
|
tar cf "$MYOUTDIR/${FILENAME}.tar" $EXCLUDES $MYINCLUDES || exit 1
|
|
rm -rf "${FILENAME}" "$FILE"
|
|
|
|
exit 0
|