Last active
November 7, 2023 09:25
-
-
Save eik3/cc10e68c60e381f46bea8b5e0c1320e2 to your computer and use it in GitHub Desktop.
A bash script to update all git 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
#!/usr/bin/env bash | |
# A script to update all git repositories in a directory tree | |
# Run `rp status` to see the status of all repositories | |
# Run `rp update` to update all repositories that are on the main branch and have no uncommitted changes | |
my_project_dir=/path/to/my/git/repos | |
function print_status() { | |
tput bold | |
echo "${repository}" | |
tput sgr0 | |
echo Branch: "$(git rev-parse --abbrev-ref HEAD)" | |
git status --short | |
echo | |
} | |
function update_repository() { | |
git fetch &> /dev/null | |
git rebase &> /dev/null | |
git remote update --prune origin &> /dev/null | |
git gc &> /dev/null | |
} | |
OLDPWD=$PWD | |
cd "$my_project_dir" || exit | |
if [[ "$1" == "status" ]]; then | |
for repository in $(find . -maxdepth 2 -type d -name .git | sort | cut -d/ -f2 2>/dev/null); do | |
( | |
cd "${repository}" || exit | |
if [[ "$(git status --short | wc --lines)" != "0" || ! "$(git rev-parse --abbrev-ref HEAD)" =~ (main|master|gh-pages) ]]; then | |
print_status | |
fi | |
) | |
done | |
fi | |
if [[ "$1" == "update" ]]; then | |
for repository in $(find . -maxdepth 2 -type d -name .git | sort | cut -d/ -f2 2>/dev/null); do | |
( | |
cd "${repository}" || exit | |
if [[ "$(git status --short | wc --lines)" != "0" || ! "$(git rev-parse --abbrev-ref HEAD)" =~ (main|master|gh-pages) ]]; then | |
echo skipping $repository | |
else | |
update_repository | |
fi | |
) & | |
sleep 0.1 | |
done | |
wait | |
fi | |
cd $OLDPWD || exit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment