#!/usr/bin/env bash

ls-git-folder() {
    if [[ -z "$1" ]]; then
        echo '$1/filter is empty.' >&2
        return 1
    fi

    if [[ ! -f ".gitmodules" ]]; then
        # Fallback: search for .git folders in subdirectories up to depth 3
        find . -maxdepth 3 -type d -name ".git" -not -path "./.git" -not -path "./node_modules/*" -not -path "./_site/*" | sed 's/\/.git$//' | sed 's|^\./||' | grep "^$1" | tr '\n' ' ' | sed 's/ $//' || true
        return 0
    fi

    # Get submodule paths that start with filter, output space-separated
    git config --file .gitmodules --get-regexp '^submodule\..*\.path$' 2>/dev/null \
        | cut -d' ' -f2 \
        | grep "^$1" \
        | tr '\n' ' ' \
        | sed 's/ $//'
}
lsgf() {
    ls-git-folder "$@"
}
