Skip to content

Instantly share code, notes, and snippets.

@leocencetti
Last active July 20, 2023 13:00
Show Gist options
  • Save leocencetti/00d3cf3149054bad9511bb097e7eb2d7 to your computer and use it in GitHub Desktop.
Save leocencetti/00d3cf3149054bad9511bb097e7eb2d7 to your computer and use it in GitHub Desktop.
git-submodule-status
#!/usr/bin/env bash
## constants ##
readonly USE_ANSI_COLOR=$( ([[ "$GBS_USE_ANSI_COLOR" =~ ^[01]$ ]] && echo -n "$GBS_USE_ANSI_COLOR") ||
([[ "$CFG_USE_ANSI_COLOR" =~ ^[01]$ ]] && echo -n "$CFG_USE_ANSI_COLOR") ||
echo -n '1')
readonly CBLUE=$( (($USE_ANSI_COLOR)) && echo '\033[1;34m')
readonly CTHINBLUE=$( (($USE_ANSI_COLOR)) && echo '\e[0;34m')
readonly CEND=$( (($USE_ANSI_COLOR)) && echo '\033[0m')
## functions ##
Init() {
N=1
RECURSIVE=false
readonly SUBMODULES="$(git submodule status | sed -e "s/^[+\ ][^\ ]*\ //" -e s/\ .*$//)"
readonly SUBMODULES_RECURSIVE="$(git submodule status --recursive | sed -e "s/^[+\ ][^\ ]*\ //" -e s/\ .*$//)"
}
Help() {
echo "Usage: submodule-status [ -n N ] [ -s | --submodule SUBMODULE ] [ -h | --help ]"
exit 2
}
GetArgs() {
SHORT_ARGS="hrn:s:"
LONG_ARGS="submodule:,recursive,help"
OPTS=$(getopt -a -n submodule-status --options "$SHORT_ARGS" --longoptions "$LONG_ARGS" -- "$@")
eval set -- "$OPTS"
while :; do
case "$1" in
-s | --submodule)
readonly SUBMODULE_FILTER="$2"
shift 2
;;
-r | --recursive)
RECURSIVE=true
shift 1
;;
-n)
N="$2"
shift 2
;;
-h | --help)
Help
;;
--)
shift
break
;;
*)
echo "Unexpected option: $1"
Help
;;
esac
done
}
PrintStatus() {
[[ $RECURSIVE == true ]] && SUBS=$SUBMODULES_RECURSIVE || SUBS=$SUBMODULES
for submodule in $SUBS; do
sub_prefix="${submodule%/*}/"
sub_name="${submodule##*/}"
if [[ -z "$SUBMODULE_FILTER" || $sub_name == $SUBMODULE_FILTER ]]; then
echo -e "$CTHINBLUE$sub_prefix$CBLUE$sub_name$CEND"
echo -e "$(git -C $submodule olog -$N | sed 's/^/\t/')\n"
fi
done
}
## main ##
Init
GetArgs $*
PrintStatus
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment