#!/bin/bash # Show actions needed for a list of git directories # # Usage: gitcheck [-v] [DIR [DIR]] # set -e; set -u # Helper functions PROG=`basename "$0" | tr -d '\n'` function info() { echo `date +%c` ${PROG}\: info: "$@" 1>&2; } function warn() { echo `date +%c` ${PROG}\: warning: "$@" 1>&2; } function error() { echo `date +%c` ${PROG}\: error: "$@" 1>&2; } function debug() { [[ -v DEBUG ]] && echo `date +%c` ${PROG}\: debug: "$@" 1>&2 || true ; } function die() { echo `date +%c` ${PROG}\: fatal: "$@" 1>&2 && exit 1; } # Global Variables declare csv_row csv_row="" # build up a one line output for each repo function isGitPushOrPullNeeded() { # Show if git push or pull is needed to upstream # # Source: http://stackoverflow.com/questions/3258243/git-check-if-pull-needed # # TO DOs: # TODO collect all info in a CSV by default, put put one summary line per repo # TODO add number of modified, untracked, etc files from "git ls-files [--optione]" # # Here are the "git branch --format" options # # $ git branch --format "refname %(refname) objecttype %(objecttype) objectsize %(objectsize) objectname %(objectname) deltabase %(deltabase) upstream %(upstream) push %(push) HEAD %(HEAD) symref %(symref) worktreepath %(worktreepath) contents:subject %(contents:subject) contents:body %(contents:body) contents:signature %(contents:signature)" # # They are likely useful for getting info # Fetch so we can compare local state with upstream without pull git fetch --all LOCAL=$(git rev-parse @) REMOTE=$(git rev-parse @{u}) BASE=$(git merge-base @ @{u}) push_or_pull_needed=":" if [ $LOCAL = $REMOTE ]; then push_or_pull_needed="UP-TO-DATE" elif [ $LOCAL = $BASE ]; then push_or_pull_needed="pull" elif [ $REMOTE = $BASE ]; then push_or_pull_needed="push" else push_or_pull_needed="DIVERGED" fi info "push_or_pull_needed: $push_or_pull_needed" csv_row="${csv_row}:${push_or_pull_needed}" } function gitChecks() { # Perform various checks of status of git in currenct directory # Show any actions needed DIR=${1:-`pwd`} # get directory name if [ ! -d "$DIR/.git" ]; then warn "$DIR is not a git repo. Skipping." echo return fi pushd ${DIR} > /dev/null # go there info "Checking ${DIR}" csv_row="${DIR}:" isGitPushOrPullNeeded echo info git status for $DIR git status . echo popd > /dev/null # back to original directory } ARGC=$# # Number of args, not counting $0 if [ "$ARGC" == "0" ]; then set -- `pwd` "$@" # add pwd if no args fi i=1 # Used as argument index while true; do DIR=${1:-""} if [ "$DIR" ]; then gitChecks "$DIR" shift else break # Stop loop when no more args. fi i=$((i+1)) done echo info "Done."