Skip to content

Instantly share code, notes, and snippets.

@loosebits
Last active November 3, 2016 11:27
Show Gist options
  • Save loosebits/88a881e32da5987534ed97ccfe5cb839 to your computer and use it in GitHub Desktop.
Save loosebits/88a881e32da5987534ed97ccfe5cb839 to your computer and use it in GitHub Desktop.
Tools for git subtree
#! /bin/bash
ROOT=$(git rev-parse --show-toplevel)
usage()
{
echo "Configures a subtree for git stpull"
echo ""
echo "git stconfig [-b=<branch>] -r=<remote> -p=<prefix> [--pull] <subtree-config-name>"
echo "git stconfig [-h | --help]"
echo ""
}
PULL=0
while [ "$1" != "" ]; do
PARAM=`echo $1 | awk -F= '{print $1}'`
VALUE=`echo $1 | awk -F= '{print $2}'`
case $PARAM in
-h | --help)
usage
exit
;;
-b | --branch)
BRANCH=$VALUE
;;
-r | --remote)
REMOTE=$VALUE
;;
-p | --prefix)
PREFIX=$VALUE
;;
--pull)
PULL=1
;;
-*)
echo "ERROR: unknown parameter \"$PARAM\""
usage
exit 1
;;
*)
if [ -z $CONFIG ]; then
CONFIG=$1;
else
echo "ERROR: too many parameters"
usage;
exit 1
fi
esac
shift
done
if [ -z $CONFIG ]; then
echo "ERROR: subtree config name not specified";
usage;
exit 1
fi
if [ -z $REMOTE ]; then
echo "ERROR: Remote not specified"
usage
exit 1
fi
if [ -z $PREFIX ]; then
echo "ERROR: Prefix not specified"
usage
exit 1
fi
if [ $PULL -eq 1 ]; then
bash -c "git config --bool subtreepull.$CONFIG.pull true"
fi
bash -c "git config subtreepull.$CONFIG.prefix $PREFIX"
bash -c "git config subtreepull.$CONFIG.remote $REMOTE"
if [ -n $BRANCH ]; then
bash -c "git config subtreepull.$CONFIG.branch $BRANCH"
fi
#! /bin/bash
ROOT=$(git rev-parse --show-toplevel)
usage()
{
echo "Pulls from a subtree using defined configuration"
echo ""
echo "git stpull <subtree-name> [<branch>]"
echo "git stpull [-h | --help]"
echo ""
echo "See git stconfig"
}
while [ "$1" != "" ]; do
PARAM=`echo $1 | awk -F= '{print $1}'`
VALUE=`echo $1 | awk -F= '{print $2}'`
case $PARAM in
-h | --help)
usage
exit
;;
--pull)
PULL=1
OVERRIDE=1
;;
--nopull)
PULL=0
OVERRIDE=1
;;
-*)
echo "ERROR: unknown parameter \"$PARAM\""
usage
exit 1
;;
*)
if [ -z $CONFIG ]; then
CONFIG=$1;
elif [ -z $BRANCH ]; then
BRANCH=$1;
else
echo "ERROR: too many parameters"
usage;
exit 1
fi
esac
shift
done
if [ -z $CONFIG ]; then
echo "ERROR: subtree config name not specified";
usage;
exit 1
fi
if [ -z $BRANCH ]; then
BRANCH=$(git config --get "subtreepull.$CONFIG.branch");
fi
PREFIX=$(git config --get "subtreepull.$CONFIG.prefix");
REMOTE=$(git config --get "subtreepull.$CONFIG.remote");
if [ -z $REMOTE ]; then
echo "ERROR: Remote is not specified in git config!"
exit 1
fi
if [ -z $PREFIX ]; then
echo "ERROR: Prefix is not specified in git config!"
exit 1
fi
if [ -z $BRANCH ]; then
echo "ERROR: Branch must be specified in git config or passed to the command line!"
exit 1
fi
cd $ROOT
if [ "$OVERRIDE" != "1" ]; then
PULL=$(git config --get "subtreepull.$CONFIG.pull");
if [ "$PULL" == "true" ]; then
PULL=1
else
PULL=0
fi
fi
if [ $PULL -eq 1 ]; then
bash -c "git pull --rebase=false"
if [ $? -ne 0 ]; then
exit 1
fi
fi
bash -c "git subtree pull --prefix=$PREFIX $REMOTE $BRANCH"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment