clr: Check Local Repositories for uncommitted changes

#!/usr/bin/env bash

help_text="
NAME
    clr - Check Local Repositories for uncommitted changes.

USAGE
    clr [options]

OPTIONS
    -p|--push
        Interactively commit and push changes for each repository
        with uncommitted changes.

    -h|--help
        Show help text.

DESCRIPTION
    Checks a predefined list of local git repositories for uncommitted
    changes. When run without options, displays a list of repositories
    that have uncommitted changes. With the -p option, iterates through
    each repository with changes and prompts to commit and push.

AUTHOR
    mjnurse.github.io - 2026
"
help_line="Check Local Repositories for uncommitted changes"
web_desc_line="Check Local Repositories for uncommitted changes"

# set -x

# Terminal Colours
cdef="\e[39m" # default colour
cbla="\e[30m"; cgra="\e[90m"; clgra="\e[37m"; cwhi="\e[97m"
cred="\e[31m"; cgre="\e[32m"; cyel="\e[33m" 
cblu="\e[34m"; cmag="\e[35m"; ccya="\e[36m";
clred="\e[91m"; clgre="\e[92m"; clyel="\e[93m"
clblu="\e[94m"; clmag="\e[95m"; clcya="\e[96m"

function cecho {
    color=c$1; shift
    echo -e $"${!color}$*${cdef}"
}

try="Try ${0##*/} -h for more information"
tmp="${help_text##*USAGE}"
usage=$(echo "Usage: ${tmp%%OPTIONS*}" | tr -d "\n" | sed "s/  */ /g")

push_yn=n
while [[ "$1" != "" ]]; do
    case $1 in
        -h|--help)
            echo "$help_text"
            exit
            ;;
        -p|--push)
            push_yn=y
            ;;
        ?*)
            break
            ;;
    esac
    shift
done

echo
cecho lgre "Checking local git repositories for uncommitted changes..."

if [[ "$push_yn" == "n" ]]; then 
    changes_yn=n
    for d in ~/mjnurse/bash \
             ~/mjnurse/python \
             ~/mjnurse/cli-builder \
             ~/mjnurse/website-builder \
             ~/mjnurse/mjnurse-website \
             ~/mjnurse/mjnurse.github.io \
            ; do
        if [[ -d $d ]]; then
            cd $d
            if [[ "$(git status --short)" != "" ]]; then
                cecho cya "Uncommitted changes in repo: ${clcya}$d"
                changes_yn=y
            fi
        fi
    done
    if [[ "$changes_yn" == "y" ]]; then
        echo
        cecho lmag "Run clr -p to selectively commit/push changes."
    fi
    exit 1
fi

for d in ~/mjnurse/bash \
         ~/mjnurse/python \
         ~/mjnurse/cli-builder \
         ~/mjnurse/website-builder \
         ~/mjnurse/mjnurse-website \
         ~/mjnurse/mjnurse.github.io \
        ; do
    if [[ -d $d ]]; then
        cd $d
        echo "Checking repository: $d"
        git fetch
        if [[ "$(git status --short)" != "" ]]; then
            echo
            cecho lmag "------------------------------------------------------------"
            cecho lmag "Repository: $d"
            cecho lmag "------------------------------------------------------------"
            git status
            yn=""
            while [[ "${yn^}" != "Y" && "${yn^}" != "N" ]]; do
                echo
                printf "${clcya}Commit and Push changes with comment Various (YN), custom comment (C) or view diff (D) [cdyN]? ${cdef}"
                read yn
                yn="${yn:-N}"

                if [[ "${yn^}" == "D" ]]; then
                    git diff
                fi
                if [[ "${yn^}" == "C" ]]; then
                    printf "${clcya}Commit Message: ${cdef}" 
                    read cm
                    yn="Y"
                else
                    cm="Various"
                    echo -e $"${clcya}Using default commit message:${cdef} Various"
                fi
                if [[ "${yn^}" == "Y" ]]; then
                    git add .
                    git commit -m "$cm"
                    git pull --rebase
                    git push
                fi
            done
        fi
    fi
done