Skip to content

Instantly share code, notes, and snippets.

@J00MZ
Last active January 17, 2023 18:41
Show Gist options
  • Save J00MZ/3f7d0b7fbeb1060c88a1f1e229a30f85 to your computer and use it in GitHub Desktop.
Save J00MZ/3f7d0b7fbeb1060c88a1f1e229a30f85 to your computer and use it in GitHub Desktop.
Pull All Repos Command
# Version 1 - one repo at a time
function pull_all_repos(){
CURRENT_DIR="$(pwd)"
DIRLIST=($(cdr && ls -1|xargs))
REPODIR="$(cdr && pwd)"
for dir in "${DIRLIST[@]}";do
echo "Pulling latest from [${dir}]"
(cd "${REPODIR}/${dir}" && gfa) || echo "Failed to access ${dir}"
done
cd "${CURRENT_DIR}" || exit
}
# Version 2 - fetch only, all repos in parallel
function pull_all_repos(){
CURRENT_DIR="$(pwd)"
REPODIR="${1:-$(cdr && pwd)}"
REPOLOCATION="$(dirname "${REPODIR}")/$(basename "${REPODIR}")"
echo "Pulling latest in [${REPOLOCATION}]"
REPOLIST=$(find "${REPODIR}" -maxdepth 2 -name .git -type d -print| sed 's/.git//'|xargs -I {} basename {}|sort)
REPOLIST_LENGTH="$(echo "${REPOLIST}"|wc -w|xargs)"
GFA='git fetch --all --prune --jobs=10'
GL='git pull'
parallel --no-notice --bar --eta -j "${REPOLIST_LENGTH}" --tag \
"echo 'Pulling latest from {}'; cd ${REPODIR}/{} && eval ${GFA} && eval ${GL}" ::: "${REPOLIST}"
cd "${CURRENT_DIR}" || exit
}
alias gfa='git fetch --all --prune --jobs=10'
alias cdr='cd ${HOME}/code/repos'
alias pullall="pull_all_repos"
alias fetchall="fetch_all_repos"
@guychouk
Copy link

guychouk commented Jan 10, 2022

Awesome alias, thank you!

If you want, I wrote a one-liner that achieves pretty much the same (without error handling and messages of course 😉).

It works by running find from the parent directory of your Git projects, iterates all sub-directories and simply attempts to git pull each one, showing the sub-directory in red:

find . -type d -maxdepth 1 | xargs -I{} zsh -c 'echo "\u1b[31m{}\033[m" && cd {} && git pull'

@J00MZ
Copy link
Author

J00MZ commented Jan 12, 2022

Nice @guychouk !
Yours does lend itself much easier to implementation in parallel with a tool like GNU parallel like so:

find . -type d -maxdepth 1 | parallel zsh -c 'echo "\u1b[31m{}\033[m" && cd {} && git pull'

I do prefer the longer safer version though 😄

@guychouk
Copy link

@J00MZ Oooohh I love the use of parallel!
Thank you, adding it to my alias!

@J00MZ
Copy link
Author

J00MZ commented Jan 13, 2022

Make sure to approve the citation on first use using parallel --will-cite

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