Last active
February 15, 2023 10:21
-
-
Save epatrasc/3ef605ff57f7b8730dfae5c86079660d to your computer and use it in GitHub Desktop.
Pull repositories from a monorepo in parallel
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/bash | |
PARALLEL=${1:-5} | |
directories=() | |
for dir in */ ; do | |
if [ -e "$dir/.git" ] ; then | |
directories+=("$dir") | |
fi | |
done | |
parallel_pull() { | |
dir=$1 | |
echo "Updating $dir" | |
cd "$dir" || exit | |
current_branch=$(git rev-parse --abbrev-ref HEAD) | |
if git status --porcelain | grep -q "^ M"; then | |
echo "Stashing local changes in $dir" | |
git stash push --include-untracked --message "Local changes stashed by update script" | |
git fetch -q origin master:master | |
git stash pop | |
else | |
git fetch --all | |
fi | |
git checkout -q "$current_branch" | |
cd .. | |
} | |
export -f parallel_pull | |
printf '%s\0' "${directories[@]}" | xargs -0 -n 1 -P "$PARALLEL" bash -c 'parallel_pull "$@"' _ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
TODOs
handle the different scenario:
join the forked processes as well => otherwise one might get a prompt while some jobs are still running in the background - there are some bash whistles to use here