Skip to content

Instantly share code, notes, and snippets.

@lukasbestle
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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
@sternenseemann
Copy link

On Archlinux with find (GNU findutils) 4.4.2 There's this problem:

find: warning: you have specified the -depth option after a non-option argument -type, but options are not positional (-depth affects tests specified before it as well as those specified after it).  Please specify options before other arguments.

find: Der Pfad muß vor dem Suchkriterium stehen: 1
Aufruf: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [Pfad...] [Suchkriterium]

@lukasbestle
Copy link
Author

I don't have an Arch machine at hand to test it, does find -type d -depth 1 $(dirname $0) fix it?

@sternenseemann
Copy link

you could also test it with any GNU find (a Linux VM), but this does not work...

@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