Skip to content

Instantly share code, notes, and snippets.

@gnumoksha
Created June 20, 2016 19:39
Show Gist options
  • Save gnumoksha/8cc4547f469ee364847c7e24b01dd8b0 to your computer and use it in GitHub Desktop.
Save gnumoksha/8cc4547f469ee364847c7e24b01dd8b0 to your computer and use it in GitHub Desktop.
Does the git pull of all subdirectories
#!/bin/bash
# This script syncs (i.e. makes git pull) on a specified directory or, se no
# parameter was specified, in all the subdirectories that are git projects.
#
# Usage:
# ./auto_sync_git.sh directory_of_my_project
# ./auto_sync_git.sh
# An alias to the git binary
GIT=$(which git)
# This function does the git pull
function make_pull {
directory="$1"; shift
cd $directory
# if the current dir is a git repository
# http://stackoverflow.com/questions/2180270/check-if-current-directory-is-a-git-repository
if [[ -d ".git" || $($GIT rev-parse --git-dir > /dev/null 2>&1) ]]; then
echo "Pulling $(pwd)"
$GIT pull
if [[ $? -eq 0 ]]; then
echo 'SUCCESS!'
else
echo 'FAILURE!'
fi
echo
fi
cd - > /dev/null
}
# The "main" function
function main {
if [[ -d "$1" ]]; then
make_pull "$1"
else
echo "Pulling all the subdirectories!"
for directory in $(find . -maxdepth 1 -type d); do
if [[ $directory != '.' && $directory != '..' ]]; then
make_pull $directory
fi
done
fi
}
# Invokes the main function and pass all script parameters to it
main $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment