#!/usr/bin/env sh

set -eu

command -v gfind >/dev/null 2>&1 && find() { gfind "$@"; }

# change to a git repo
# local target
repo() {
  cd ~/projects/"$1"
}

# fall back to what was originally given if a short form cant be resolved
repo=${repo:-$1}

target=$(
  find "${HOME}/projects" -mindepth 3 -maxdepth 3 -type d \
    | sed -E 's/^(.+\/gh(\/.+?))$/\2\t\1/' \
    | fzf --with-nth=1 --select-1 --query="${repo}" --height=60% --reverse \
      --preview 'eza --color=always -TL2 {} 2>/dev/null | head -200' \
      --preview-window 'default:right:40%' \
    | cut -f 2
)

if [ -n "${target}" ]; then
  cd "${target}" && git status --short --branch
fi
