Skip to content

Instantly share code, notes, and snippets.

@MartinEden
Last active November 8, 2017 16:59
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 MartinEden/344c01066cd61ce8251c2cd6737e4908 to your computer and use it in GitHub Desktop.
Save MartinEden/344c01066cd61ce8251c2cd6737e4908 to your computer and use it in GitHub Desktop.
Script that lists outgoing changes on all branches in the current Git repo, or in Git repos in all immediate subfolders
#!/usr/bin/env bash
# This script lists outgoing changes on all branches in the current Git repo.
# If the current folder is not a Git repo, it instead runs on each subfolder.
function get_branch_status {
# https://stackoverflow.com/a/7774433
git for-each-ref --format="%(refname:short) %(upstream:short)" refs/heads | \
while read local remote
do
[ -z "$remote" ] && continue
git rev-list --left-right ${local}...${remote} -- 2>/dev/null >/tmp/git_upstream_status_delta || continue
AHEAD=$(grep -c '^<' /tmp/git_upstream_status_delta)
if (( AHEAD > 0 )); then
echo "- $local $AHEAD commits ahead of $remote"
fi
done
STATUS=$(git status -s)
if [ "$STATUS" ]; then
echo "- not clean"
fi
}
# https://stackoverflow.com/a/2180367
inside_git_repo="$(git rev-parse --is-inside-work-tree 2>/dev/null)"
if [ "$inside_git_repo" ]; then
get_branch_status
else
echo "No git repository detected. Checking each subfolder:"
# https://stackoverflow.com/a/26495371
dirs=($(find . -maxdepth 1 -type d \( ! -name . \)))
for dir in "${dirs[@]}"; do
echo $(basename $dir)
(cd "$dir" && get_branch_status)
done
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment