Skip to content

Instantly share code, notes, and snippets.

@dertuxmalwieder
Last active October 16, 2020 12:40
Show Gist options
  • Save dertuxmalwieder/8e6789584ec0098cf332888a13439e0c to your computer and use it in GitHub Desktop.
Save dertuxmalwieder/8e6789584ec0098cf332888a13439e0c to your computer and use it in GitHub Desktop.
Keep all Git repositories in $HOME up-to-date
#!/opt/schily/bin/bosh
# Update all Git repositories in/below $HOME.
# ---- VARIABLES
# Contains a list of (substrings of) directories to skip.
# It makes sense to not interfere with Go's own version control,
# and on macOS the ~/Library folder should not be traversed
# either.
BLACKLIST="$HOME/go/ $HOME/Library"
# Contains a list of Git repository directories.
# Currently, this is collected automatically, but it can be a
# manual list as well. Note that "find" on non-Schily shells
# usually does not have a "-call" parameter.
GITDIRS=`find $HOME -maxdepth 3 -name '.?*' -prune -o -type d -call '[ -e "$1/.git" ]' {} \; -prune -print`
# ---- CODE
repocount=0
for DIR in ${GITDIRS}; do
# Blacklist check:
allowed=1
for BAN in ${BLACKLIST}; do
substrings=`expr "$DIR" : "$BAN"`
if [ $substrings -ne 0 ] ; then
# This directory is in/below a blacklisted one.
allowed=0
break
fi
done
if [ $allowed -eq 0 ]; then
continue
fi
# This directory is promised to be not blacklisted.
# Process it.
repocount=`expr $repocount + 1`
cd $DIR
# Check the remote state:
git fetch -q
UPSTREAM=${1:-'@{u}'}
LOCAL=`git rev-parse @`
REMOTE=`git rev-parse "$UPSTREAM"`
BASE=`git merge-base @ "$UPSTREAM"`
if [ $LOCAL = $REMOTE ]; then
# This repository is up-to-date.
# Do nothing with it.
continue
elif [ $LOCAL = $BASE ]; then
# The remote version is newer.
echo "Updating Git repository: $DIR"
git pull -q
git log $LOCAL..$REMOTE --date=short --pretty=format:" - New [%ad] : %s"
else
# Something is different.
echo "The Git repository in $1 needs to be merged."
echo "This cannot be done automatically."
fi
done
if [ $repocount -gt 0 ]; then
echo "Successfully processed $repocount repositories."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment