Adrian Schröter
651b9dc1b7
OBS-URL: https://build.opensuse.org/package/show/openSUSE:Tools/obs-service-tar_scm?expand=0&rev=6
227 lines
5.5 KiB
Bash
227 lines
5.5 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
|
|
|
|
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 _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 "$SUBDIR" -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 "$MYREVISION" ]; then
|
|
svn co -r"$MYREVISION" "$MYURL" "${FILE}" || exit 1
|
|
else
|
|
svn co "$MYURL" "${FILE}" || exit 1
|
|
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 "$SUBDIR" -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 "$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 "$SUBDIR" -a -d "$TAR_DIRECTORY" ]; then
|
|
# update existing content for speed/bandwidth reasons
|
|
cd "$TAR_DIRECTORY"
|
|
OLDVERSION=`hg tip | head -n 1 | sed -e 's/\s*//g' | awk -F':' '{ print $2 }'`
|
|
hg pull || exit 1
|
|
if [ -n "$MYREVISION" ]; then
|
|
hg checkout "$MYREVISION" || exit 1
|
|
fi
|
|
NEWVERSION=`hg tip | head -n 1 | sed -e 's/\s*//g' | awk -F':' '{ print $2 }'`
|
|
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
|
|
if [ -n "$MYREVISION" ]; then
|
|
cd "$FILE"
|
|
hg checkout "$MYREVISION" || exit 1
|
|
cd -
|
|
fi
|
|
fi
|
|
if [ -z "$VERSION" ]; then
|
|
cd "$FILE"
|
|
[ -n "$MYPREFIX" ] && MYPREFIX="$MYPREFIX."
|
|
VERSION="$MYPREFIX"`hg tip | head -n 1 | sed -e 's/\s*//g' | awk -F':' '{ print $2 }'`
|
|
cd -
|
|
fi
|
|
else
|
|
echo "ERROR: unknown scm $MYSCM"
|
|
exit 1
|
|
fi
|
|
|
|
if [ ! -e "$FILE/$SUBDIR" ]; then
|
|
echo "Directory does not exist: $FILE/$SUBDIR"
|
|
exit 1
|
|
fi
|
|
|
|
mv "$FILE/$SUBDIR" "${FILE}-${VERSION}" || exit 1
|
|
|
|
tar cf "$MYOUTDIR/${FILE}-${VERSION}.tar" $EXCLUDES "${FILE}-${VERSION}" || exit 1
|
|
rm -rf "${FILE}-${VERSION}"
|
|
|
|
exit 0
|