All pastes #1260554 Raw Edit

Something

public text v1 · immutable
#1260554 ·published 2008-11-18 17:46 UTC
rendered paste body
#!/bin/sh
#
# /etc/ports/drivers/git: git driver script for ports(8)
#
# Originally written by Richard Poettler, richard.poettler@gmail.com
# Extended by Johannes Winkelmann, jw@smts.ch
#
# The configuration files got to have following variables defined:
# URL: 		The URL from where to clone the repository
# BRANCH: 	The branch of the repository to use
# DESTINATION:	The destination directory for the checked out repository

export PAGER=cat
export GIT_DIR=.gitports

# remove existing directory unless it's a valid git dir pointing to
# the source repository
verifyDestination() {
    if [ -d $DESTINATION/$GIT_DIR ]; then
	cd $DESTINATION
	if  [ "$URL" ==  "`git config --get remote.origin.url`" ]; then
	    return
	fi
    fi
    rm -rf $DESTINATION
}

checkVariables() {
    if [ -z "$URL" ] || [ -z "$BRANCH" ] || [ -z "$DESTINATION" ]; then
	echo "Invalid input file: URL, BRANCH or DESTINATION not set"
	exit 1
    fi
}

errorExit() {
    echo "Finished with errors"
    exit 1
}

printChanges() {
    git-diff --name-status -R $BRANCH | \
	sed -e 's|^A\t| Checkout: |g' \
	    -e 's|^M\t| Edit: |g' \
	    -e 's|^D\t| Remove: |g'
}


trap "errorExit" ERR
set -e

# check, whether we got a configuration file given
if [ $# -ne 1 ]; then
	echo "usage: $0 <configuration file>"
	exit 1
fi
. $1
checkVariables

echo "Updating collection `basename $DESTINATION` from $URL"
verifyDestination

if [ ! -d $DESTINATION ]; then
	# clone the repository
	git-clone --no-checkout --depth 1 --quiet $URL $DESTINATION > /dev/null
	cd $DESTINATION
	mv .git $GIT_DIR # git-clone doesn't honour GIT_DIR...
else
	# there was already a clone -> fetch the changes
	cd $DESTINATION
	set -x

	# reset to prestine state
	printChanges
	git-reset --hard -q

	# fetch remote changes
	git-fetch --quiet
	git-gc --quiet
	set +x
fi

# checkout latest version
printChanges
git-checkout -q $BRANCH

echo "Finished successfully"