Skip to content

Instantly share code, notes, and snippets.

@derTobsch
Last active December 6, 2018 13:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save derTobsch/924eff7c50486de930721469e9632af0 to your computer and use it in GitHub Desktop.
Save derTobsch/924eff7c50486de930721469e9632af0 to your computer and use it in GitHub Desktop.
Bash alias to pull (with rebase) all repository in the first depth of the actual directory
refresh_all_repos() {
count_all=0;
count_up_to_date=0;
count_updated=0;
count_need_to_push=0;
count_diverged=0;
count_conflict=0;
count_no_remote=0;
for repo in $(find `pwd` -name .git -type d -prune -exec dirname {} \;)
do
echo -e "\n\033[38;5;246m ############# $repo \033[0m";
count_all=$((count_all+1));
/usr/bin/git -C $repo ls-remote --exit-code --quiet origin 2>&1 >/dev/null;
NO_REMOTE=$?
if [[ "NO_REMOTE" -ne 0 ]]; then
echo -e "> No remote (origin)\n"
count_no_remote=$((count_no_remote+1));
continue;
fi
/usr/bin/git -C $repo remote update 2>&1 >/dev/null;
ERROR=$?
if [ "$ERROR" -eq 0 ]; then
CURRENT_BRANCH=$(/usr/bin/git -C $repo rev-parse --abbrev-ref HEAD)
if [[ "$CURRENT_BRANCH" != "master" ]]; then
/usr/bin/git -C $repo checkout master
echo -e "> Changed to master branch\n"
fi
LOCAL=$(/usr/bin/git -C $repo rev-parse @)
REMOTE=$(/usr/bin/git -C $repo rev-parse @{u})
BASE=$(/usr/bin/git -C $repo merge-base @ @{u})
if [ $LOCAL = $REMOTE ]; then
count_up_to_date=$((count_up_to_date+1));
echo -e "\033[38;5;36m 👍 Already up to date \033[0m";
elif [ $LOCAL = $BASE ]; then
/usr/bin/git -C $repo fetch -p | awk '{ print " " "\033[38;5;239m" $ "\033[0m" }';
/usr/bin/git -C $repo rebase --autostash | awk '{ print " " "\033[38;5;239m" $ "\033[0m" }';
count_updated=$((count_updated+1));
echo -e "\033[38;5;120m ✓ Updated \033[0m";
elif [ $REMOTE = $BASE ]; then
count_need_to_push=$((count_need_to_push+1));
echo -e "\033[38;5:124m 📌 Need to push \033[0m"
else
count_diverged=$((count_diverged+1));
echo -e "\033[38;5;160m 🖓 Diverged \033[0m"
fi
else
count_conflict=$((count_conflict+1));
echo -e "\033[38;5;160m ⚡ Failure \033[0m"
fi
done
echo -e "\n\033[38;5;239m 📊 of ${count_all} Git Repos \033[0m"
echo -e "\033[38;5;239m ${count_up_to_date} are up to date \033[0m"
echo -e "\033[38;5;239m ${count_updated} were updated \033[0m"
echo -e "\033[38;5;239m ${count_need_to_push} need a push \033[0m"
echo -e "\033[38;5;239m ${count_diverged} are diverged \033[0m"
echo -e "\033[38;5;239m ${count_conflict} have conflicts \033[0m"
echo -e "\033[38;5;239m ${count_no_remote} have no remote \033[0m"
}
alias fetch-all=refresh_all_repos
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment