#!/bin/sh
# AGENTS77 git credential helper (scope 474; owner rule 2026-11-25: no system keychain dialog, ever).
#
# Every git the desktop starts (and every git run inside a Claude Code session, a project action or
# a terminal the desktop opened) has, in command scope, `credential.helper=` (the person's helpers —
# osxkeychain, the credential manager — are cleared) then this helper. It answers `get` for
# https://github.com only, with a short-lived token the running desktop asks the platform for; for
# anything else, or when the desktop is not running, it prints nothing — git then fails with
# "could not read Username" (GIT_TERMINAL_PROMPT=0) and the app shows its sign-in chip. Never a dialog.
#
# The desktop's local endpoint is read from a curl config file (AGENTS77_GIT_HELPER_FILE: its URL and
# per-launch bearer, mode 0600), so no secret ever appears on a command line. `store` / `erase`: nothing
# is kept anywhere. Copied into the bundled git's libexec/git-core by scripts/fetch-portable-git.mjs.
if [ "$1" != get ]; then
  cat >/dev/null 2>&1
  exit 0
fi
protocol=
host=
path=
while IFS= read -r line; do
  [ -z "$line" ] && break
  case "$line" in
    protocol=*) protocol=${line#protocol=} ;;
    host=*) host=${line#host=} ;;
    path=*) path=${line#path=} ;;
  esac
done
[ "$protocol" = https ] || exit 0
[ "$host" = github.com ] || exit 0
file=$AGENTS77_GIT_HELPER_FILE
[ -n "$file" ] && [ -f "$file" ] || exit 0
command -v curl >/dev/null 2>&1 || exit 0
cwd=$(pwd -W 2>/dev/null || pwd -P)
answer=$(printf 'protocol=%s\nhost=%s\npath=%s\ncwd=%s\n' "$protocol" "$host" "$path" "$cwd" | curl -fsS --max-time 20 -K "$file" --data-binary @- 2>/dev/null) || exit 0
printf '%s\n' "$answer" | grep -E '^(username|password)=[^[:space:]]+$'
exit 0
