Last active
August 29, 2015 14:03
-
-
Save larruda/1c813d8f459a947b673c to your computer and use it in GitHub Desktop.
Syncs a forked repository against its upstream.
This file contains hidden or 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
#!/bin/sh | |
git_repo=$1 | |
[ -z "$git_repo" ] && git_repo="." | |
cd $git_repo > /dev/null 2>&1 | |
if [ $? -ne 0 ]; then | |
printf "Invalid directory path.\n" | |
exit 1 | |
fi | |
RED=$(tput setaf 1) | |
GREEN=$(tput setaf 2) | |
NORMAL=$(tput sgr0) | |
col=$(tput cols) | |
git status > /dev/null 2>&1 | |
git fetch upstream > /dev/null 2>&1 | |
if [ $? -ne 0 ]; then | |
printf "Not a git repository or no upstream found for %s.\n" "$git_repo" | |
exit 1 | |
fi | |
upstream_branches=`git branch -r | grep upstream | cut -c3-` | |
for branch_name in $upstream_branches | |
do | |
printf "Syncing branch %s of %s... " "$branch_name" "$git_repo" | |
local_name=`echo $branch_name | cut -c10-` | |
git checkout -b $local_name $branch_name > /dev/null 2>&1 | |
git checkout $local_name | |
git merge $branch_name | |
git push origin $local_name | |
if [ $? -eq 0 ]; then | |
printf '%s%*s%s' "$GREEN" $col "[OK]" "$NORMAL" | |
else | |
printf '%s%*s%s' "$RED" $col "[FAIL]" "$NORMAL" | |
fi | |
done |
Put it on your crontab and be happy!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage: ./forksync.sh [repository_path]