~smlavine/scripts

ref: 5af0022d2df522079823605c7340e26ab9847202 scripts/src/cgs -rwxr-xr-x 1.4 KiB
5af0022dSebastian LaVine dotfiles -> scripts 10 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/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