Last active
August 29, 2015 13:58
-
-
Save coiscir/10217869 to your computer and use it in GitHub Desktop.
Find and cautiously update multiple git repositories. Base: http://stackoverflow.com/q/11981716/15031
This file contains 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
#!/bin/bash | |
# Update all git repositories under the current or specified directory | |
OPTIND=1 | |
usage=0 | |
list=0 | |
update=0 | |
forced=0 | |
while getopts "h?l?u?f?" opt; do | |
case "$opt" in | |
h|\?) | |
usage=1 | |
;; | |
l) | |
list=1 | |
;; | |
u) | |
update=1 | |
;; | |
f) | |
forced=1 | |
;; | |
esac | |
done | |
if [ $usage = 1 ] || [ "$#" = "0" ]; then | |
echo "Usage: $0 [-h? | -l | -ufv] -- path" 1>&2 | |
exit 1 | |
else | |
shift `expr $OPTIND - 1` | |
fi | |
function update { | |
if [ -d "$1" ]; then | |
cd $1 > /dev/null | |
if [ -d ".git" ]; then | |
printf '\e[0;33m%s: \e[0;34m%s \e[0m\n' 'Updating' $1 | |
local edits=$(git status --porcelain | grep -iEc '^ ?[a-z]') | |
local latest=$(git rev-parse HEAD) | |
git status -s | |
if [ $edits -eq 0 ] || [ $forced = 1 ]; then | |
git fetch -p | |
git pull | |
local updated=$(git rev-parse HEAD) | |
if [ $latest != $updated ]; then | |
echo '' | |
git --no-pager log --reverse --format='%h (%ar, %an) %s' "$latest..$updated" | |
fi | |
echo '' | |
fi | |
git gc --auto | |
echo "" | |
else | |
echo "$1 is not a git repo" | |
fi | |
cd - > /dev/null | |
fi | |
} | |
function scan { | |
local base=$([ "$1" != "" ] && echo "$1" || echo ".") | |
echo "Searching: $base" | |
for repo in `find "$base" -name .git -type d`; do | |
local wd=`dirname "$repo"` | |
if [ $list = 1 ]; then | |
echo "$wd" | |
elif [ $update = 1 ]; then | |
update "$wd" | |
fi | |
done | |
} | |
scan "$1" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment