Skip to content

Instantly share code, notes, and snippets.

@dbrgn
Last active October 9, 2021 15:20
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dbrgn/1e91b7e0a4a9b703320b to your computer and use it in GitHub Desktop.
Save dbrgn/1e91b7e0a4a9b703320b to your computer and use it in GitHub Desktop.
Determine whether subdirectories containing git repos are dirty or diverged
#!/bin/bash
function git_is_dirty {
[[ $(git diff --shortstat 2> /dev/null | tail -n1) != "" ]] && echo "yes"
}
function git_is_diverged {
LOCAL=$(git rev-parse --abbrev-ref HEAD 2> /dev/null || echo "none")
REMOTE=$(git rev-parse --abbrev-ref --symbolic-full-name @{u} 2> /dev/null || echo "none")
if [[ "$LOCAL" == "none" ]]; then # No commits yet
echo "maybe"
return
fi
if [[ "$REMOTE" == "none" ]]; then # No remote branch
echo "maybe"
return
fi
if [ -n "$(git log ${REMOTE}..${LOCAL})" ]; then
echo "yes"
return
fi
}
DIRS=$(find . -maxdepth 1 -type d)
for d in $DIRS; do (
cd $d
git status > /dev/null 2>&1
if [ $? -ne 0 ]; then continue; fi
if [[ "$(git_is_dirty)" == "yes" ]]; then
echo "$d is dirty"
fi
if [[ "$(git_is_diverged)" == "yes" ]]; then
echo "$d is diverged"
elif [[ "$(git_is_diverged)" == "maybe" ]]; then
echo "$d might be diverged"
fi
)
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment