Skip to content

Instantly share code, notes, and snippets.

@lukasbestle
Last active August 29, 2015 14:02
Show Gist options
  • Save lukasbestle/f95c250ab476d78badcb to your computer and use it in GitHub Desktop.
Save lukasbestle/f95c250ab476d78badcb to your computer and use it in GitHub Desktop.
Shell script to update every Git repository in direct subdirectories of the script directory
#!/usr/bin/env bash
# Updates every Git repository in direct subdirectories of the script directory
# Usage: ./update
# Copyright: Lukas Bestle <lukas@lu-x.me>, 2014
# Check if git is installed
hash git &> /dev/null || {
echo -e "\033[31mYou don't have git installed.\033[0m"
exit 1
}
# Stores the number of failed updates
errors=0
# Iterate through every subdirectory
for repository in `find $(dirname $0) -type d -depth 1`; do
# Change to the directory
cd $repository
# Check if the directory is a git repository
if git rev-parse &> /dev/null; then
# Debug information
echo -e "\033[1mSwitching to repository \033[34m$(basename $repository)\033[0;1m...\033[0m"
# Update repository
if ! git config branch.master.remote &> /dev/null; then
echo -e "\033[33m ==> The \033[34mmaster\033[33m branch does not track any remote branch, skipping.\033[0m"
elif git checkout master && git pull && git submodule update --init --recursive; then
echo -e "\033[32m ==> Done.\033[0m"
else
# Something returned an exit code != 0, error
echo -e "\033[31m ==> Failed.\033[0m"
# Increment error variable
((errors++))
fi
else
# No git repository, error
echo -e "\033[31mThe directory \033[34m$(basename $repository)\033[31m does not contain a git repository.\033[0m"
# Increment error variable
((errors++))
fi
# Go back to the root directory
cd ..
done
# Debug information
echo ""
if [ $errors == 0 ]; then
echo -e "\033[32mAll done.\033[0m"
else
echo -e "\033[31m$errors error(s) occurred!\033[0m"
fi
@lukasbestle
Copy link
Author

So what does work? :)

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