Skip to content

Instantly share code, notes, and snippets.

@heckler
Created August 7, 2023 19:44
Show Gist options
  • Save heckler/43a520198c7432437ec104b1d26d8b59 to your computer and use it in GitHub Desktop.
Save heckler/43a520198c7432437ec104b1d26d8b59 to your computer and use it in GitHub Desktop.
List the git branch of repositories in sub-directory
#!/bin/bash
#
# 'g-list-branch' -> "git list branch(es)"
#
# iterate through subfolders of the current directory and
# list the current branch they are in - useful to find work
# that was forgotten and turn them into a PR
#
# if the current folder is a git repo, just list the branch for
# this repo and quit, do not proceed to list subfolders
#
# CAH 2023-08-07
#
parent_folder=$(pwd)
# is the current folder a git repo? If so, list the branch and quit
git diff --exit-code > /dev/null 2>&1
retVal=$?
if [[ "$retVal" -eq 0 ]]; then
git rev-parse --abbrev-ref HEAD
exit 0
fi
# determine the lenght of the longest directory name, just to make presentation prettier:
max_length=0
for repo in */
do
len=${#repo}
if (( len > max_length )); then
max_length=$len
fi
done
# iterate over the subdirectories of the current directory
for repo in */
do
cd "$parent_folder"
cd "$repo"
git diff --exit-code > /dev/null 2>&1
retVal=$?
if [[ "$retVal" -eq 0 ]] || [[ "$retVal" -eq 1 ]]; then
branch="$(git rev-parse --abbrev-ref HEAD)"
printf "\e[1;34m%-${max_length}s\e[0m %s\n" "$repo" "$branch"
fi
done
cd "$parent_folder"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment