#!/bin/bash
# Scan /home/mfritsche and /root for git repos (depth 4), report
# host, path, branch, head, origin, cached origin/<branch>, upstream.
# Output: TSV, one repo per line.
HOST=$(hostname)
for R in /home/mfritsche /root; do
    [ -d "$R" ] || continue
    sudo find "$R" -maxdepth 4 -type d -name .git 2>/dev/null
done | while read -r gitdir; do
    repo="${gitdir%/.git}"
    G="sudo git -c safe.directory=* -C $repo"
    branch=$($G rev-parse --abbrev-ref HEAD 2>/dev/null)
    head=$($G rev-parse --short HEAD 2>/dev/null)
    origin=$($G config --get remote.origin.url 2>/dev/null)
    upstream=$($G config --get remote.upstream.url 2>/dev/null)
    origin_cached=""
    [ -n "$branch" ] && [ "$branch" != "HEAD" ] && origin_cached=$($G rev-parse --short "origin/$branch" 2>/dev/null)
    printf "%s\t%s\tbranch=%s\thead=%s\torigin_cached=%s\torigin=%s\tupstream=%s\n" \
        "$HOST" "$repo" "${branch:-?}" "${head:-?}" "${origin_cached:-?}" "${origin:-none}" "${upstream:-none}"
done
