Skip to content

Instantly share code, notes, and snippets.

@mikermcneil
Created November 4, 2017 16:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikermcneil/685e47be6b473ecb82e3f88933d653f5 to your computer and use it in GitHub Desktop.
Save mikermcneil/685e47be6b473ecb82e3f88933d653f5 to your computer and use it in GitHub Desktop.
Gist for iterating over files in a particular folder and checking their git status
#!/bin/bash
# smry
#
# Copyright Nov 4, 2017, Mike McNeil
# MIT License
#
# Adapted from https://gist.github.com/lmj0011/1a8dd1e376234ac7bf0fba2748ecdd0f
# and https://gist.github.com/mzabriskie/6631607#gistcomment-1738920
# which was originally adapted from https://gist.github.com/mzabriskie/6631607
# :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
#!/bin/bash
####################################################################################
# MODIFIED, WORKING VERSION CUSTOMIZED FOR MIKE:
summarizeGitSituation()
{
local dir="$1"
# No directory has been provided, use `/code`
if [ -z "$dir" ]
then
dir="/code"
# FUTURE: If `/code` doesn't exist, use current (pwd)
# dir="`pwd`"
fi
# Make sure directory ends with "/"
if [[ $dir != */ ]]
then
dir="$dir/*"
else
dir="$dir*"
fi
local mod;
local BRANCH_CHECK;
local IS_NOT_MASTER;
local HAS_MODIFIED_FILES;
local HAS_UNTRACKED_FILES;
local HAS_UNPUSHED_COMMITS;
# Loop all sub-directories
for f in $dir
do
# Only interested in directories
[ -d "${f}" ] || continue
# Check if directory is a git repository
if [ -d "$f/.git" ]
then
mod=0
HAS_MODIFIED_FILES=false;
HAS_UNTRACKED_FILES=false;
HAS_UNPUSHED_COMMITS=false;
cd $f
# Check branch
local IS_NOT_MASTER;
local BRANCH_CHECK="`git status | head -n1`"
if [ "$BRANCH_CHECK" == 'On branch master' ]
then
IS_NOT_MASTER=false;
else
IS_NOT_MASTER=true;
fi
# Check for modified files
if [ $(git status | grep modified -c) -ne 0 ]
then
mod=1
HAS_MODIFIED_FILES=true;
fi
# Check for untracked files
if [ $(git status | grep Untracked -c) -ne 0 ]
then
mod=1
HAS_UNTRACKED_FILES=true;
fi
# Check for unpushed changes
if [ $(git status | grep 'Your branch is ahead' -c) -ne 0 ]
then
mod=1
HAS_UNPUSHED_COMMITS=true;
fi
# Check if everything is peachy keen
if [ $mod -eq 0 ]
then
$(); # nothing to commit, all good
else
echo -en "\033[0;35m${f}\033[0m $([ "$IS_NOT_MASTER" == true ] && echo -en $BRANCH_CHECK)" $([ "$HAS_MODIFIED_FILES" == true ] && echo -en "\033[0;31mModified file(s)\033[0m") $([ "$HAS_UNTRACKED_FILES" == true ] && echo -en "\033[0;31mUntracked file(s)\033[0m") $([ "$HAS_UNPUSHED_COMMITS" == true ] && echo -en "\033[0;31mUnpushed commit(s)\033[0m")
echo
fi
cd ../
# echo
fi
done
}
# Run it
summarizeGitSituation $1
@mikermcneil
Copy link
Author

mikermcneil commented Nov 4, 2017

example usage + output:

∑ smry
/code/chatkin     On branch provinces Modified file(s)
/code/mavenlink-sdk      Untracked file(s)
/code/moneywave-paybutton-example      Untracked file(s)
/code/monmonphoto.com      Modified file(s)
/code/treehouse-api     On branch development Modified file(s) Untracked file(s)
∑ 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment