Skip to content

Instantly share code, notes, and snippets.

@dccampbell
Last active January 5, 2016 10:25
Show Gist options
  • Save dccampbell/747de45e9aafca91e705 to your computer and use it in GitHub Desktop.
Save dccampbell/747de45e9aafca91e705 to your computer and use it in GitHub Desktop.
Scan the current sub-directories for git repositories, recursively, and return the git status for the working tree and all branches. Default max depth is 3. Ignores vendor and modman folders.
#!/usr/bin/env bash
basePath="$PWD"
baseDirs=$(find $basePath -mindepth 1 -maxdepth 1 -type d)
gitPaths=$(find $basePath -mindepth 1 -maxdepth ${1:-3} -name .git -type d -exec dirname {} \; | grep -v '/vendor/\|/.modman/')
unchangedDirs=()
for repo in $gitPaths; do
cd $repo
[[ "$2" = "-f" ]] && git fetch -q &>/dev/null
branch="on branch "$(git rev-parse --abbrev-ref=loose HEAD 2>/dev/null)
changedCount=$(git status -s | wc -l)
[[ $changedCount -gt 0 ]] && branch="$branch \e[0;31m[$changedCount working changes]\e[0m"
branches=$(git for-each-ref --format=' %(refname:short)...%(push:short) %(color:red)%(push:track)%(color:reset)' refs/heads)
[[ "$branch$branches" =~ "]" ]] && echo -e "$repo\n$branch\n$branches\n" || unchangedDirs+=("$repo")
gitDir=${repo#$basePath/}
baseDirs=$(printf "%s" "$baseDirs" | grep -v "${gitDir%%/*}")
done
cd $basePath
printf "\nNo Changes:\n"
printf "%s" "${unchangedDirs[*]}" | tr -s ' ' '\n'
printf "\n\nNon-Repo Directories:\n"
echo $baseDirs | tr -s ' ' '\n'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment