Last active
November 18, 2019 14:56
-
-
Save jgriff/0eb7768ab242bcb1acd2c894821fcece to your computer and use it in GitHub Desktop.
Recursively walks a directory and prints git status (if dir is a git repo). Ignores non-git directories.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
dir="$1" | |
display_prefix="$2" | |
option="$3" | |
# No directory has been provided, or a "--" option has been given instead (which can be the case when invoked by the user) | |
# In either case, use current dir | |
if [ -z "$dir" ] || [[ $dir == --* ]]; then | |
option=$dir | |
dir="`pwd`" | |
fi | |
# Make sure directory ends with "/" | |
if [[ $dir != */ ]]; then | |
dir="$dir/*" | |
else | |
dir="$dir*" | |
fi | |
# 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 | |
cd "$f" || exit | |
# Check branch | |
branchName=$(git symbolic-ref --short HEAD) | |
# Check for unpushed changes | |
if [ $(git status | grep 'Your branch is ahead' -c) -ne 0 ]; then | |
mod=1 | |
fi | |
# Check for untracked files | |
if [ $(git status | grep Untracked -c) -ne 0 ]; then | |
mod=2 | |
fi | |
# Check for modified files | |
if [ $(git status | grep modified -c) -ne 0 ]; then | |
mod=3 | |
fi | |
# print out the status line | |
if [ $mod -eq 0 ] | |
then # everything is good | |
echo -en "\033[0;32m" | |
echo -n "✔ " | |
echo -en "\033[0m" | |
else # there are local changes | |
echo -en "\033[0;31m" | |
echo -n "✘ " | |
echo -en "\033[0m" | |
fi | |
echo -en "\033[0;35m" | |
echo -n "${display_prefix}${f##*/}" | |
echo -en "\033[0m" | |
echo -n " is on branch " | |
if [ "$branchName" == "master" ] | |
then | |
echo -en "\033[0;32m" # green | |
else | |
echo -en "\033[0;34m" # blue | |
fi | |
echo -n $branchName | |
echo -en "\033[0m" | |
if [ ! $mod -eq 0 ] | |
then | |
echo -n " with " | |
if [ $mod -eq 1 ] | |
then | |
echo -en "\033[0;33m" | |
echo -n "unpushed commit(s)" | |
echo -en "\033[0m" | |
elif [ $mod -eq 2 ] | |
then | |
echo -en "\033[0;37m" | |
echo -n "untracked file(s)" | |
echo -en "\033[0m" | |
elif [ $mod -eq 3 ] | |
then | |
echo -en "\033[0;31m" | |
echo -n "modified file(s)" | |
echo -en "\033[0m" | |
fi | |
fi | |
if [[ $option == "--v" ]]; then | |
echo -n ". 🔗" | |
git branch -vv --color=always | grep '^*' | awk '{ $1 = ""; $2 = ""; print $0 }' | |
else | |
echo "." | |
fi | |
cd ../ | |
else | |
# recurse into sub-directory | |
git-status $f "${display_prefix}${f##*/}/" $option | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Installation
Place in your
PATH
.Usage
From current directory,
Or specify a specific directory (that you're not currently in),
You can also request verbose output for each repo encountered,
which will print the effect of
git branch -vv
giving you the sha, remote tracking branch, and latest commit message.