Last active
October 9, 2021 15:20
-
-
Save dbrgn/1e91b7e0a4a9b703320b to your computer and use it in GitHub Desktop.
Determine whether subdirectories containing git repos are dirty or diverged
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 | |
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