Skip to content

Instantly share code, notes, and snippets.

@klkyy2020
Created May 9, 2020 10:44
Show Gist options
  • Save klkyy2020/7d98fb91fa410f0eb05ad6b6aebf178a to your computer and use it in GitHub Desktop.
Save klkyy2020/7d98fb91fa410f0eb05ad6b6aebf178a to your computer and use it in GitHub Desktop.
sync upstream everywhere
#!/bin/bash
# https://gist.github.com/Juraci/d1414f7c780e2dbe8204
# modified by klkyy2020(klkyy2020 at 163 dot com)
red=$(tput setaf 1)
green=$(tput setaf 2)
yellow=$(tput setaf 3)
myexit() {
reason=$1
printf "${red}Failed: ${reason}!!!\n"
exit 1
}
# default "`pwd`"
repo_path=$1
[ -z ${repo_path} ] && repo_path=`pwd`
[ -d $repo_path ] || myexit "First parameter is not directory"
# default "master"
branch=$2
# checks if it's a git repo
check_repo() {
if [ ! -d `pwd`/.git ]; then
myexit "`pwd` is not a git repo"
fi
}
# checks if there is a remote repo called upstream
check_upstream() {
git remote -v | grep -w upstream > /dev/null
# if there are no remote repository called upstream
if [ $? -ne 0 ]; then
myexit "Upstream not configured in this repo(`pwd`)"
fi
}
# check if a branch was passed as parameter and if not it makes the master as the default branch
check_branch() {
if [[ $branch ]]; then
printf "${green}Branch to sync to is set to {$branch}\n"
else
printf "${yellow}Branch to sync to not supplied, using *master* as the default branch to sync to\n"
branch="master"
fi
}
# fetch updates from upstream
# merges upstream with a given branch
# push the changes
sync() {
printf "${green}Fetching its updates...\n"
git fetch upstream
if [ $? -eq 0 ]; then
printf "${green}Merging updates with ${branch}\n"
git merge upstream/$branch
git push
else
myexit "Failed fetch upstream"
fi
}
cur_path=`pwd`
cd $repo_path
check_repo
check_upstream
check_branch
sync
printf "${green}Succ!!!\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment