Skip to content

Instantly share code, notes, and snippets.

@brianoflan
Created October 12, 2017 17:36
Show Gist options
  • Save brianoflan/5cc8d07144e6642e980239c42acdbec3 to your computer and use it in GitHub Desktop.
Save brianoflan/5cc8d07144e6642e980239c42acdbec3 to your computer and use it in GitHub Desktop.
Check a folder structure for Git workspaces and report if they are behind their remotes
#!/bin/bash
fix=$1 ;
DEBUG=2 ;
limitLines=20 ;
maxDepth=15 ;
tmp=old ;
thisD=$(dirname "$0") ;
set -e ; set -x ;
# cd "$thisD" || exit 1 ;
cd ~ || exit 1 ;
set +x ; set +e ;
tmp=`readlink -f "$tmp" 2> /dev/null || greadlink -f "$tmp" ` ;
[[ ! -d $tmp ]] && echo "ERROR: Failed to find tmp dir $tmp (in $thisD)." ;
[[ ! -d $tmp ]] && exit 1 ;
find . -type d -maxdepth $maxDepth \
-name 'huge' -prune -o \
-name 'old' -prune -o \
-name '.old*' -prune -o \
-name '_old*' -prune -o \
-name '.git' -print -prune \
> $tmp/findGit 2> $tmp/findGit.stderr ;
#
pwd0=`pwd` ;
touch "$tmp/findGit.status" ;
tmpf=`readlink -f "$tmp/findGit.status" 2> /dev/null || greadlink -f "$tmp/findGit.status" ` ;
while read dx ; do
d=`dirname "$dx" ` ;
if [[ $DEBUG -gt 1 ]] ; then
echo -e "\n\n$d" ;
fi ;
cd $d ;
git fetch > /dev/null 2>&1 ;
git status > $tmpf ;
x=`egrep '^nothing to commit, working directory clean' $tmpf` ;
y=`egrep '^Your branch is' $tmpf > /dev/null && egrep '^Your branch is up[\-]to[\-]date with' $tmpf || echo 'Not blank'.` ;
if [[ -z $x || -z $y ]] ; then
if [[ -s $tmpf ]] ; then
echo "$d" ;
wc=`wc -l $tmpf | awk '{print $1}' ` ;
# echo "wc $wc" ;
if [[ $wc -gt $limitLines ]] ; then
head $tmpf ;
echo -e '\n. . .\n' ;
tail $tmpf ;
else
cat $tmpf ;
fi ;
echo -e '\n' ;
else
echo "ERROR: No status from $d ." 1>&2 ;
fi ;
if [[ "fix" == "$fix" ]] ; then
z=`egrep '^Your branch is behind' $tmpf ` ;
if [[ $z && -z $x ]] ; then
echo -e "\nAttempting rebase." ;
git rebase origin/master ;
echo -e "\n" ;
else
z=`egrep '^Your branch is ahead' $tmpf ` ;
if [[ $z && -z $x ]] ; then
echo -e "\nAttempting push." ;
git push origin master ;
echo -e "\n" ;
fi ;
fi ;
fi ;
fi ;
cd $pwd0 ;
done < $tmp/findGit ;
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment