Git updater shell script - Programming On Unix

Users browsing this thread: 1 Guest(s)
jkl
Long time nixers
I have mentioned a couple of times that I refuse to use Git for versioning my files. Yet, I sometimes need to compile something which resides in a Git repository, usually from GitHub. I keep those repositories in $HOME/github and (randomly) other folders below $HOME. Keeping them updated is relatively annoying though, so I automatized it.

You might want to modify $BLACKLIST and $GITDIRS, especially if you don’t use the bosh for scripting and/or you keep your repositories elsewhere. (On standard POSIX shells, $GITDIRS won’t work like this. Sorry. I might rewrite that line some day.)

Usage: ./gitup.sh.

Code:
#!/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 $DIR needs to be merged."
        echo "This cannot be done automatically."
    fi
done

if [ $repocount -gt 0 ]; then
    echo "Successfully processed $repocount repositories."
fi

Enjoy. Or modify as you need.

--
<mort> choosing a terrible license just to be spiteful towards others is possibly the most tux0r thing I've ever seen


Messages In This Thread
Git updater shell script - by jkl - 10-09-2020, 02:30 AM
RE: Git updater shell script - by venam - 10-09-2020, 02:43 AM
RE: Git updater shell script - by jkl - 10-09-2020, 02:51 AM
RE: Git updater shell script - by jkl - 13-10-2020, 06:16 AM
RE: Git updater shell script - by mcol - 14-10-2020, 03:19 PM
RE: Git updater shell script - by jkl - 14-10-2020, 07:19 PM
RE: Git updater shell script - by mcol - 15-10-2020, 09:03 AM
RE: Git updater shell script - by Halfwit - 28-11-2020, 09:00 PM