#!/bin/sh # ---------------------------------------------------------------------- # Clone a git repository in get stage or check if it's the right one # in case already cloned. # # Authors: Alexander Dahl # # Copyright 2011,2012,2013 Alexander Dahl # ---------------------------------------------------------------------- #set -x #set -e _RED="\033[31m" _YELLOW="\033[33m\033[40m\033[1m" _NOCOLOR="\033[0m\033[49m" GITURL="$1" DESTDIR="$2" print_usage() { echo "Usage: $0 giturl destdir" } if [ -z "$1" -o -z "$2" ] then print_usage exit 1 fi CHECKOUTDIR="$(basename ${DESTDIR})" SRCDIR="$(dirname ${DESTDIR})" if [ ! -d "${SRCDIR}" ] then echo "folder ${_YELLOW}local_src${_NOCOLOR} does not exist, please create it!" exit 4 fi cd "${SRCDIR}" if [ ! -d "${CHECKOUTDIR}" ] then if [ -e "${CHECKOUTDIR}" ] then echo "${CHECKOUTDIR} exists but is no directory!" exit 2 else echo "${CHECKOUTDIR} does not exist." fi if git clone "${GITURL}" "${CHECKOUTDIR}" then echo 'git clone successful ...' else exit $? fi cd "${CHECKOUTDIR}" if git fetch --all then echo 'git fetch successful ...' else exit $? fi else if [ -L "${CHECKOUTDIR}" ] then echo "${_RED}WARNING: ${_YELLOW}${CHECKOUTDIR} is a symbolic link!${_NOCOLOR}" exit 0 fi cd "${CHECKOUTDIR}" REMOTEURL="$(git remote -v | awk '/^origin.+(fetch)/ {print $2;}')" if [ "${REMOTEURL}" = "${GITURL}" ] then echo "This already cloned repo is the right one." exit 0 else echo "Cloned repo remote is '${REMOTEURL}' but '${GITURL}' was expected!" exit 3 fi fi