File tar_scm of Package obs-service-tar_scm

#!/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"

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
    ;;
    *-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-vcs"
fi

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

# 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 [ -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'`
    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 -
    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 [ "$VERSION" == "_auto_" ]; 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/.git" ]; then
    # update existing content for speed/bandwidth reasons
    cd "$TAR_DIRECTORY"
    OLDVERSION=`git show --pretty=%at  | head -n 1`
    git pull $MYGITARGS || exit 1
    if [ -n "$MYREVISION" ]; then
      git checkout "$MYREVISION" || exit 1
    fi
    NEWVERSION=`git show --pretty=%at  | head -n 1`
    cd -
    mv "$TAR_DIRECTORY" "${FILE}" || exit 1
  else
    # new checkout
    git clone $MYGITARGS "$MYURL" "${FILE}" || exit 1
    if [ -n "$MYREVISION" ]; then
      cd "$FILE"
      git checkout "$MYREVISION" || exit 1
      cd -
    fi
  fi
  if [ "$VERSION" == "_auto_" ]; 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/.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

mv "$FILE/$MYSUBDIR" "${FILENAME}" || exit 1

tar cf "$MYOUTDIR/${FILENAME}.tar" $EXCLUDES "${FILENAME}" || exit 1
rm -rf "${FILENAME}" "$FILE"

exit 0
openSUSE Build Service is sponsored by