Skip to content

Instantly share code, notes, and snippets.

@mricon
Created May 24, 2016 13:55
Show Gist options
  • Save mricon/fdefb66cb1e633fc25f7b7f86744c82f to your computer and use it in GitHub Desktop.
Save mricon/fdefb66cb1e633fc25f7b7f86744c82f to your computer and use it in GitHub Desktop.
#!/bin/bash
if [ -z "$GL_USER" ]; then
echo "Run me from gitolite"
exit 1
fi
function showsetusage {
echo
echo 'USAGE: track set your-repo orig-repo remote-name [--tags]'
echo
echo 'This will set up the following remote in your repo:'
echo '[remote "remote-name"]'
echo ' url = file:///pub/scm/path/to/orig/repo.git'
echo ' fetch = +refs/heads/*:refs/remotes/remote-name/*'
echo
echo 'If you optionally pass --tags at the end, it will also set:'
echo
echo ' tagopt = --tags'
echo
echo 'EXAMPLE:'
echo "track set pub/scm/linux/kernel/git/${GL_USER}/linux \\"
echo " pub/scm/linux/kernel/git/next/linux-next \\"
echo " next"
}
function showfetchusage {
echo
echo 'USAGE: track fetch pub/scm/path/to/your/repo remote-name'
echo
echo 'This will perform "git fetch remote-name --prune"'
echo
echo 'EXAMPLE:'
echo "track fetch pub/scm/linux/kernel/git/${GL_USER}/linux torvalds"
}
function showlistusage {
echo
echo 'USAGE: track list pub/scm/path/to/your/repo'
echo
echo 'This will list all configured remotes'
echo
echo 'EXAMPLE:'
echo "track list pub/scm/linux/kernel/git/${GL_USER}/linux"
}
function showrmusage {
echo
echo 'USAGE: track rm pub/scm/path/to/your/repo remote-name'
echo
echo 'This will perform "git remote remove remote-name"'
echo
echo 'EXAMPLE:'
echo "track rm pub/scm/linux/kernel/git/${GL_USER}/linux next"
}
function canread {
# Does this path exist and are you allowed to read it?
if [ -z "${1}" ]; then
return 1
fi
if [ ! -d "${TOPDIR}/${1}.git" ]; then
echo "ERROR: repo ${1}"
echo " does not appear to exist"
return 1
fi
if ! gitolite access -q "${1}" "${GL_USER}" R any; then
echo "ERROR: repo ${$1}"
echo " does not exist or you are not allowed to read it"
return 1
fi
return 0
}
function canwrite {
if ! canread $1; then
return 1
fi
# Are you allowed to write to it?
if ! gitolite access -q "${1}" "${GL_USER}" W any; then
echo "ERROR: repo ${1}"
echo " exists but you are not allowed to write to it"
return 1
fi
return 0
}
TOPDIR="/var/lib/gitolite3/repositories"
ACTION=$1; shift
if [ "$ACTION" == "set" ]; then
if [ "$1" == "help" -o "$1" == "--help" ]; then
showsetusage
exit 1
fi
TOREPO=`echo $1 | sed -e 's|\.git$||' -e 's|^/||'`; shift
if ! canwrite "${TOREPO}"; then
showsetusage
exit 1
fi
FROMREPO=`echo $1 | sed -e 's|\.git$||' -e 's|^/||'`; shift
# Does this path exist and are you allowed to read it?
if ! canread "${FROMREPO}"; then
showsetusage
exit 1
fi
REMNAME="$1"; shift
if [ "${REMNAME}" == "origin" ]; then
echo "ERROR: please do not call it 'origin'"
showsetusage
exit 1
fi
if [ ! -z "$1" -a "$1" != "--tags" ]; then
echo "The last argument must be '--tags' or nothing at all"
showsetusage
exit 1
fi
TAGOPTS="--no-tags"
if [ "$1" == "--tags" ]; then
TAGOPTS="--tags"
fi
echo "Setting remote '${REMNAME}' in ${TOREPO}"
FULLFROM="${TOPDIR}/${FROMREPO}.git"
FULLTO="${TOPDIR}/${TOREPO}.git"
export GIT_DIR="$FULLTO"
# Make sure we don't have an origin set
git remote remove origin >/dev/null 2>&1
git remote add "${TAGOPTS}" "${REMNAME}" "${FULLFROM}"
if [ "$?" != "0" ]; then
exit $?
fi
echo "Fetching the new remote..."
git fetch "${REMNAME}" --prune
elif [ "$ACTION" == "fetch" ]; then
if [ "$1" == "help" -o "$1" == "--help" ]; then
showfetchusage
exit 1
fi
UPREPO=`echo $1 | sed -e 's|\.git$||' -e 's|^/||'`; shift
# Does this repo exist?
if ! canwrite "${UPREPO}"; then
showfetchusage
exit 1
fi
# Do we have a remote called [remote-name]?
REMNAME="$1"
if [ -z "${REMNAME}" ]; then
showfetchusage
exit 1
fi
if [ "${REMNAME}" == "origin" ]; then
echo 'ERROR: refusing to fetch "origin" out of sheer spite'
echo
showfetchusage
exit 1
fi
FULLUP="${TOPDIR}/${UPREPO}.git"
export GIT_DIR="$FULLUP"
if git config --get remote.${REMNAME}.url >/dev/null; then
echo "Performing 'git fetch ${REMNAME} --prune' in ${UPREPO}"
git fetch "${REMNAME}" --prune
else
echo "ERROR: Could not find a remote named ${REMNAME} in ${UPREPO}"
echo " Below is a list of remotes in ${UPREPO}"
echo
git remote -v | grep '(fetch)' | sed -e "s|${TOPDIR}/||" -e 's|\.git||' -e 's|(fetch)||' | grep -v '^origin'
fi
elif [ "${ACTION}" == "list" ]; then
if [ "$1" == "help" -o "$1" == "--help" ]; then
showlistusage
exit 1
fi
LSREPO=`echo $1 | sed -e 's|\.git$||' -e 's|^/||'`; shift
# Does this repo exist?
if ! canwrite "${LSREPO}"; then
showlistusage
exit 1
fi
FULLLS="${TOPDIR}/${LSREPO}.git"
export GIT_DIR="$FULLLS"
git remote -v | grep '(fetch)' | sed -e "s|${TOPDIR}/||" -e 's|\.git||' -e 's|(fetch)||' | grep -v '^origin'
elif [ "$ACTION" == "rm" -o "$ACTION" == "remove" ]; then
if [ "$1" == "help" -o "$1" == "--help" ]; then
showrmusage
exit 1
fi
RMREPO=`echo $1 | sed -e 's|\.git$||' -e 's|^/||'`; shift
# Does this repo exist?
if ! canwrite "${RMREPO}"; then
showrmusage
exit 1
fi
# Do we have a remote called [remote-name]?
REMNAME="$1"
if [ -z "${REMNAME}" ]; then
showrmusage
exit 1
fi
if [ "${REMNAME}" == "origin" ]; then
echo 'ERROR: refusing to remove "origin" out of sheer spite'
echo
showrmusage
exit 1
fi
FULLRM="${TOPDIR}/${RMREPO}.git"
export GIT_DIR="$FULLRM"
if git config --get remote.${REMNAME}.url >/dev/null; then
echo "Performing 'git remote remove ${REMNAME}' in ${RMREPO}"
git remote remove "${REMNAME}"
echo
echo 'The remotes are now as follows:'
echo
else
echo "ERROR: Could not find a remote named ${REMNAME} in ${RMREPO}"
echo " Below is a list of remotes in ${RMREPO}"
echo
fi
git remote -v | grep '(fetch)' | sed -e "s|${TOPDIR}/||" -e 's|\.git||' -e 's|(fetch)||' | grep -v '^origin'
else
echo 'Valid commands:'
echo ' set your-repo-path orig-repo-path remote-name [--tags]'
echo ' fetch your-repo-path remote-name'
echo ' rm your-repo-path remote-name'
echo ' list your-repo-path'
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment