Last active
November 8, 2017 16:59
-
-
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
This file contains 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
#!/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