Pull all git repositories in folder concurrently
#!/usr/bin/env bash | |
## | |
# pull-all | |
## | |
# AUTHOR: David J. Sequero | |
# Pull latest changes from all repositories within a directory. | |
# | |
# DEPENDENCIES | |
# - GNU parallel | |
REPOS_PATH="./" | |
if [[ ${1:0:1} == "-" ]]; then | |
# Get the last argument passed (optional repo path) | |
if [[ ${#} -gt 1 && -z "${@:-1}" ]]; then | |
REPOS_PATH=${@:-1} | |
# Get only the flags passed | |
shift | |
fi | |
while getopts 'bh' OPTION | |
do | |
case ${OPTION} in | |
b) echo "Current branches:" | |
for dir in `ls -d ${REPOS_PATH}*/`; do | |
pushd ${dir} &> /dev/null | |
BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD) | |
echo "${dir} ===> ${BRANCH_NAME}" | |
popd &> /dev/null | |
done | |
exit 0 | |
;; | |
h) cat <<_HELP_MSG | |
NAME | |
pull-all - update all repositories located in specified directory | |
SYNOPSIS | |
pull-all [-b] [path] | |
Script defaults to current working directory if no path is supplied. | |
OPTIONS | |
-b | |
list current branches in specified directory (or CWD if none) without updating the trees | |
_HELP_MSG | |
exit 0 | |
;; | |
?) echo "Unrecognised argument. Usage: pull-all [-b] [path]" | |
exit 1 | |
;; | |
esac | |
done | |
fi | |
function pullrepo() { | |
pushd ${1} &> /dev/null | |
echo "Pulling ${1} repo..." | |
git pull | |
if [[ ${?} == 0 ]]; then | |
echo "Pulled ${1}" | |
fi | |
popd &> /dev/null | |
} | |
# Export the `pullrepo` function above so that it can be passed to `parallel` | |
export -f pullrepo | |
# Pull all repos under the directory simultaneously | |
ls -d ${REPOS_PATH}*/ | parallel pullrepo | |
# Wait for all jobs to complete | |
while [[ ! -z `jobs -r` ]]; do | |
sleep 1 | |
done | |
echo "Done." | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment