Last active
August 29, 2015 14:02
-
-
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
This file contains hidden or 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 | |
# Updates every Git repository in direct subdirectories of the script directory | |
# Usage: ./update | |
# Copyright: Lukas Bestle <[email protected]>, 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 |
I don't have an Arch machine at hand to test it, does find -type d -depth 1 $(dirname $0)
fix it?
you could also test it with any GNU find (a Linux VM), but this does not work...
So what does work? :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
On Archlinux with
find (GNU findutils) 4.4.2
There's this problem: