Skip to content

Instantly share code, notes, and snippets.

@epatrasc
Last active February 15, 2023 10:21
Show Gist options
  • Save epatrasc/3ef605ff57f7b8730dfae5c86079660d to your computer and use it in GitHub Desktop.
Save epatrasc/3ef605ff57f7b8730dfae5c86079660d to your computer and use it in GitHub Desktop.
Pull repositories from a monorepo in parallel
#!/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 "$@"' _
@epatrasc
Copy link
Author

TODOs

handle the different scenario:

  • master no local changes
  • master with local changes
  • different branch no local changes
  • different branch with local changes

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment