Last active
June 26, 2025 12:45
-
-
Save heckler/43a520198c7432437ec104b1d26d8b59 to your computer and use it in GitHub Desktop.
List the git branch of repositories in sub-directory
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 | |
# | |
# '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 | |
# | |
starting_folder=$(pwd) | |
scan_folder() { | |
local folder=$1 | |
cd "$folder" | |
# fetch the subdirectories of the current directory | |
subdirs=($(find . -maxdepth 1 -type d ! -path . ! -name ".venv")) | |
if [ ${#subdirs[@]} -gt 0 ]; then | |
for subfolder in "${subdirs[@]}"; | |
do | |
local full_path="${folder}/${subfolder}" | |
cd "$full_path" | |
# determine the lenght of the longest directory name, just to make presentation prettier: | |
max_length=30 | |
#for repo in */ | |
#do | |
# len=${#repo} | |
# if (( len > max_length )); then | |
# max_length=$len | |
# fi | |
#done | |
# is the current folder a git repo? If so, list the branch and quit | |
git diff --exit-code > /dev/null 2>&1 | |
retVal=$? | |
case $retVal in | |
0) | |
# it's a git repo, no local changes | |
branch="$(git rev-parse --abbrev-ref HEAD)" | |
printf "\e[1;34m%-${max_length}s\e[0m %s\n" "$subfolder" "$branch" | |
;; | |
1) | |
# it's a git repo, and has local changes | |
branch="$(git rev-parse --abbrev-ref HEAD)" | |
printf "\e[1;34m%-${max_length}s\e[0m %-15s \e[1;33m[ changed ]\e[0m\n" "$subfolder" "$branch" | |
;; | |
*) | |
# not a git repo | |
scan_folder "$full_path" | |
#echo '-------------------------------------------------------------------------------' | |
;; | |
esac | |
done | |
fi | |
} | |
scan_folder "$starting_folder" | |
cd "$starting_folder" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment