Adrian Schröter
932f419e54
OBS-URL: https://build.opensuse.org/package/show/openSUSE:Tools/obs-service-tar_scm?expand=0&rev=14
234 lines
5.6 KiB
Bash
234 lines
5.6 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=""
|
|
MYPREFIX=""
|
|
MYFILENAME=""
|
|
MYREVISION=""
|
|
|
|
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
|
|
;;
|
|
*-versionprefix)
|
|
MYPREFIX="$2"
|
|
shift
|
|
;;
|
|
*-exclude)
|
|
EXCLUDES="$EXCLUDES --exclude='${2#/}'"
|
|
shift
|
|
;;
|
|
*-filename)
|
|
MYFILENAME="${2#/}"
|
|
shift
|
|
;;
|
|
*-outdir)
|
|
MYOUTDIR="$2"
|
|
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 "$MYSCM" ]; then
|
|
echo "ERROR: no scm is given via --scm parameter (git/svn/hg)!"
|
|
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" -a "$MYSCM" == "git" ]; then
|
|
FILE="${MYURL%/}"
|
|
FILE="${FILE##*/}"
|
|
FILE="${FILE%.git}"
|
|
fi
|
|
if [ -z "$FILE" -a "$MYSCM" == "svn" ]; then
|
|
FILE="${MYURL%/}"
|
|
FILE="${FILE##*/}"
|
|
fi
|
|
if [ -z "$FILE" -a "$MYSCM" == "hg" ]; then
|
|
FILE="${MYURL%/}"
|
|
FILE="${FILE##*/}"
|
|
fi
|
|
|
|
existing_tar=$(echo $SRCDIR/_service:*tar_scm:${FILE}-*.tar*)
|
|
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`
|
|
fi
|
|
fi
|
|
|
|
if [ "$MYSCM" == "svn" ]; then
|
|
if [ -z "$MYSUBDIR" -a -d "$TAR_DIRECTORY" ]; 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'`
|
|
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'`
|
|
cd -
|
|
if [ "$OLDVERSION" == "$NEWVERSION" ]; then
|
|
# cleanup and skip
|
|
rm -rf "$TAR_DIRECTORY"
|
|
exit 0
|
|
fi
|
|
mv "$TAR_DIRECTORY" "${FILE}" || exit 1
|
|
else
|
|
# new checkout
|
|
if [ -n "$MYSUBDIR" ]; then
|
|
# just checkout the subdir
|
|
mkdir -p "$MYSUBDIR"
|
|
cd "$MYSUBDIR"
|
|
fi
|
|
if [ -n "$MYREVISION" ]; then
|
|
svn co --non-interactive --trust-server-cert -r"$MYREVISION" "$MYURL/$MYSUBDIR" "${FILE}" || exit 1
|
|
else
|
|
svn co --non-interactive --trust-server-cert "$MYURL/$MYSUBDIR" "${FILE}" || exit 1
|
|
fi
|
|
if [ -n "$MYSUBDIR" ]; then
|
|
cd -
|
|
fi
|
|
fi
|
|
if [ -z "$VERSION" ]; then
|
|
cd "$FILE"
|
|
[ -n "$MYPREFIX" ] && MYPREFIX="$MYPREFIX.rev"
|
|
VERSION="$MYPREFIX"`LC_ALL=C svn info | sed -n 's,^Last Changed Rev: \(.*\),\1,p'`
|
|
cd -
|
|
fi
|
|
elif [ "$MYSCM" == "git" ]; then
|
|
if [ -z "$MYSUBDIR" -a -d "$TAR_DIRECTORY" ]; then
|
|
# update existing content for speed/bandwidth reasons
|
|
cd "$TAR_DIRECTORY"
|
|
OLDVERSION=`git show --pretty=%at | head -n 1`
|
|
git pull || exit 1
|
|
if [ -n "$MYREVISION" ]; then
|
|
git checkout "$MYREVISION" || exit 1
|
|
fi
|
|
NEWVERSION=`git show --pretty=%at | head -n 1`
|
|
cd -
|
|
if [ "$OLDVERSION" == "$NEWVERSION" ]; then
|
|
# cleanup and skip
|
|
rm -rf "$TAR_DIRECTORY"
|
|
exit 0
|
|
fi
|
|
mv "$TAR_DIRECTORY" "${FILE}" || exit 1
|
|
else
|
|
# new checkout
|
|
git clone --depth 1 "$MYURL" "${FILE}" || exit 1
|
|
if [ -n "$MYREVISION" ]; then
|
|
cd "$FILE"
|
|
git checkout "$MYREVISION" || exit 1
|
|
cd -
|
|
fi
|
|
fi
|
|
if [ -z "$VERSION" ]; then
|
|
cd "$FILE"
|
|
[ -n "$MYPREFIX" ] && MYPREFIX="$MYPREFIX."
|
|
VERSION="$MYPREFIX"`git show --pretty=%at | head -n 1`
|
|
cd -
|
|
fi
|
|
elif [ "$MYSCM" == "hg" ]; then
|
|
if [ -z "$MYSUBDIR" -a -d "$TAR_DIRECTORY" ]; 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 -
|
|
if [ "$OLDVERSION" == "$NEWVERSION" ]; then
|
|
# cleanup and skip
|
|
rm -rf "$TAR_DIRECTORY"
|
|
exit 0
|
|
fi
|
|
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 [ -z "$VERSION" ]; then
|
|
cd "$FILE"
|
|
[ -n "$MYPREFIX" ] && MYPREFIX="$MYPREFIX."
|
|
# current working revision
|
|
VERSION="$MYPREFIX"`hg id -n`
|
|
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
|
|
|
|
mv "$FILE/$MYSUBDIR" "${FILE}-${VERSION}" || exit 1
|
|
|
|
tar cf "$MYOUTDIR/${FILE}-${VERSION}.tar" $EXCLUDES "${FILE}-${VERSION}" || exit 1
|
|
rm -rf "${FILE}-${VERSION}" "$FILE"
|
|
|
|
exit 0
|