Skip to content

Instantly share code, notes, and snippets.

@Kovrinic
Created May 8, 2016 22:07
Show Gist options
  • Save Kovrinic/6cbb837ae14a496c378fc7b05bbda7ea to your computer and use it in GitHub Desktop.
Save Kovrinic/6cbb837ae14a496c378fc7b05bbda7ea to your computer and use it in GitHub Desktop.
This is my atempt to make a script to update all git directories in a folder
#!/bin/bash
#################### Description ######################
# This is my atempt to make a script
# to update all git directories in a folder
# Last Updated 08/21/2015
##################### Variables #######################
GITOPT="-q"
GITSUBOPT="--quiet"
####################### Usage #########################
mapfile usage <<+
NAME
gitup - extended git update utility
SYNOPSIS
gitup [options] [directory]
DESCRIPTION
gitup is an extended git update utility that updates all git repositories
within a single folder. For example if we have 3 git repositories within a
folder named 'temp', then gitup can recursivly update the repositories
within the 'temp' folder.
OPTIONS
-u, --update [dir] - Update to the latest copy of [dir]
-U, --updatesub [dir] - Update to the latest copy of [dir] inc submodules
--upgradesub [dir] - Upgrade submodules to latest release from source
-v, --verbose - Enable Verbose output
-V, --version - Display program version and exit
-h, --help - Display help message and exit
NOTES
If verbose is enabled it must be the first argument.
Example: Verbose update of directory 'temp'. Direcotry can have trailing "/"
$ gitup -vu temp
$ gitup -v -u temp
$ gitup -v --update temp
+
##################### Version #########################
mapfile version <<+
gitup version 1.1.0
Matthew Rothfuss
+
##################### Functions #######################
listfolders ()
{
cd "$OPTDIR"
FOLDERS=`ls -Al | grep '^d' | awk '{print $9}'` # list all folder names only
for dir in $FOLDERS; do
if [[ -d $dir/.git ]]; then
GFOLDERS+=("$dir")
fi
done
cd ..
}
updategit ()
{
listfolders
echo "*** Pulling the latest changes for:"
printf '%s\n' "${GFOLDERS[@]}"
echo "located in $OPTDIR ***"
echo
cd "$OPTDIR"
for dir in ${GFOLDERS[@]}; do
cd $dir
RELEASE=`git rev-parse --abbrev-ref HEAD`
if [[ $VERBOSE == True ]]; then
echo "*** Pulling latest updates for '$dir origin $RELEASE' ***"
fi
git pull $GITOPT origin $RELEASE
if [[ $opt == updatesub ]]; then
echo "*** Pulling latest changes for submodules ***"
# Required if a submodule origin changed
git submodule $GITSUBOPT sync
git submodule $GITSUBOPT foreach --recursive git fetch
git submodule $GITSUBOPT update --init --recursive
elif [[ $opt == upgradesub ]]; then
echo "*** Pulling latest changes from source for submodules ***"
git submodule $GITSUBOPT foreach --recursive git pull $GITOPT origin $RELEASE
fi
cd ..
if [[ $VERBOSE == True ]]; then
echo
fi
done
cd ..
}
# Process Options
options ()
{
while [[ $# > 1 ]]
#while true
do
key="$1"
case "$key" in
-h|--help)
printf %s "${usage[@]}"
exit
;;
-u|--update)
OPTDIR="$2"
updategit
shift # past argument
;;
-U|--updatesub)
OPTDIR="$2"
opt="updatesub"
updategit
shift # past argument
;;
--upgradesub)
OPTDIR="$2"
opt="upgradesub"
updategit
shift # past argument
;;
-v|--verbose)
VERBOSE=True
GITOPT="-v"
GITSUBOPT=""
shift
;;
-V|--version)
printf %s "${version[@]}"
exit
;;
*) # Unknown Options
exit
;;
esac
done
}
################### Main Program ######################
# Set the bash output to show line by line execution.
# Also automatically mark variables and functions
#which are modified or created for export to the
#environment of subsequent commands.
# This is used for debugging
#
#set -xa
ARGS=`getopt -o hu:U:vV -l help,update:,updatesub:,upgradesub:,verbose,version -- "$@"`
eval set -- "$ARGS"
options "${@}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment