#!/bin/sh
# Copyright (c) 2021 Sebastian LaVine <mail@smlavine.com>
# Licensed under the MIT license. See MIT.txt for details.
#
# File: cgs
# Description: Checks the git statuses of given git repositories. By default,
# cgs checks the git statuses of all git repositories in the
# current directory.
# Options: -r Also check the status of repositories
# within the given directories.
# Arguments: Paths to git repositories to check the status of.
# Unless -r is specified, we only want to look for git repositories at the top
# level of this directory. We use a maxdepth of 2 by default because we are
# `find`-ing for .git directories, not the directories that contain them.
maxdepth="-maxdepth 2"
options="r"
while getopts "$options" o; do
case "$o" in
r) maxdepth="" ;;
*) echo "Usage: cgs [-$options]"; exit ;;
esac
done
shift $((OPTIND - 1))
[ "$#" -eq 0 ] && directories='.' || directories="$*"
# Cheers to e36freak of #bash@libera.chat for help with the `find`.
# $directories and $maxdepth each potentially contain multiple arguments and
# are intentionally unquoted.
# shellcheck disable=SC2086
repos="$(find $directories $maxdepth -type d -name .git -prune \
| sed -e 's:/\.git$::' -e 's:^\./::' | sort)"
for repo in $repos; do
st="$(git -C "$repo" -c color.status=always status -sb)"
if [ "$(echo "$st" | wc -l)" -ge 2 ] || echo "$st" | grep -q ']$'; then
echo "$repo $st"
fi
done