Created
October 26, 2018 13:27
-
-
Save tsprlng/d28e540ed6b49a37b7bbb2e6db996ff6 to your computer and use it in GitHub Desktop.
update-all-repos
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
#!/bin/zsh -eu | |
set -o pipefail | |
# Main directory full of Git repos | |
GIT_DIRECTORY=~/work/git | |
# Branches to ensure (if they exist) are checked out and constantly updated from origin. | |
# Anything weird I'm doing that could fail to rebase should be in a nicely separated branch / worktree anyway! | |
# TODO replace this fixed list with pattern filter on remote branches | |
CANONICAL_SYNC_BRANCHES=(master develop v2.{2..9}) | |
# Are we in one of the individual repos under the main directory? | |
maybe_in_repo='' | |
if [[ $PWD == "$GIT_DIRECTORY"* ]]; then | |
maybe_in_repo="${${PWD#$GIT_DIRECTORY/}%%/*}" | |
fi | |
if [[ -n "$maybe_in_repo" ]]; then | |
# Do single repo if in repo | |
to_update=($maybe_in_repo/.bare) | |
else | |
# Do everything, by default | |
to_update=(*/.bare aaarchive/*/.bare) | |
fi | |
# Only do specified repos, if repos are specified | |
if [[ -n "$*" ]]; then | |
to_update=(${^@}"/.bare") | |
fi | |
cd "$GIT_DIRECTORY" | |
# Create stdout-indenter | |
exec {indent}> >(perl -pe 's[^][ ]') | |
red_echo() { echo "\e[31m$*\e[0m" } | |
for repo_master in $to_update; do | |
repo_base="$GIT_DIRECTORY/${repo_master%/.bare}" | |
[[ -d "$repo_master" ]] && [[ -d "$repo_base" ]] || (echo "??? $repo_master" && continue) | |
echo "${repo_master%/.bare}" | |
# Fetch updates from remote | |
>&$indent 2>&1 ( | |
git -C "$repo_master" fetch || (red_echo ' FETCH FAIL' && false) || continue | |
) | |
for branch in $CANONICAL_SYNC_BRANCHES; do | |
# See if branch exists | |
>/dev/null 2>&1 git -C "$repo_master" rev-parse "origin/$branch" || continue | |
echo " $branch" | |
# Check out worktree for branch if not already checked out | |
if [[ ! -d "$repo_base/$branch" ]]; then | |
>&$indent 2>&1 ( | |
cd "$repo_master" | |
git worktree add "$repo_base/$branch" "$branch" | |
) | |
fi | |
# Update (try-rebase) branch on top of origin version. | |
>&$indent 2>&1 ( | |
cd "$repo_base/$branch" | |
chronic git rebase || (git rebase --abort && false) || (red_echo ' UPDATE FAIL' && false) || continue | |
) | |
done | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment