Last active
December 13, 2015 23:09
-
-
Save dduvnjak/4989949 to your computer and use it in GitHub Desktop.
Repo branch sync
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
function usage { | |
echo "usage: configure_repo.sh [local_repo local_branch remote_repo remote_branch]" | |
} | |
local_repo=$1 | |
local_branch=$2 | |
remote_repo=$3 | |
remote_branch=$4 | |
if [[ $# -ne 4 ]]; then | |
echo -e "ERROR: Invalid number of arguments \n"; | |
usage | |
exit -1; | |
fi; | |
if [[ ! -d ${local_repo} ]]; then | |
echo "Local repo provided does not exist! Check your arguments." | |
exit 1 | |
fi; | |
cat > ${local_repo}/repo.config <<END | |
upstream=${remote_repo} | |
local_branch=${local_branch} | |
remote_branch=${remote_branch} | |
END | |
pre_commit_script='#!/usr/bin/env bash | |
DIR="$( cd -P "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" | |
. ${DIR}/../../repo.config | |
git_cmd="git --git-dir=${DIR}/../../.git --work-tree=${DIR}/../.." | |
if [[ `$git_cmd remote -v | grep upstream` ]]; then | |
echo "Upstream alrady added!" | |
else | |
echo "Adding upstream..." | |
$git_cmd remote add upstream ${upstream} | |
fi; | |
#performing fetch and merging | |
$git_cmd fetch upstream | |
$git_cmd checkout ${local_branch} | |
$git_cmd merge upstream/${remote_branch}' | |
cat > ${local_repo}/.git/hooks/pre-commit <<END | |
${pre_commit_script} | |
END | |
chmod +x ${local_repo}/.git/hooks/pre-commit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment