#!/usr/bin/env bash # WHICH GIT REMOTE IS HOME — the shell half of the TypeScript host-remote module. # # Forgejo is the one code host, and it is `origin` — on a Mac, in a CI job, in # a fresh clone. Cotton's own code host and its relay remote left with the Rust # server, and with them the rule that found the home remote by the relay's # address. # # Source it and call `host_remote `; it sets three variables: # # HOST_REMOTE the remote name # HOST_REMOTE_MAIN "/main" # HOST_REMOTE_WHY one line naming the remote and why — PRINT IT. A tool that # silently picked the wrong remote is the failure this fixes. # # No `jq`: this runs inside `worktree-new.sh` and the disk sweep, which must # work on a machine that has not installed it. The host is one short JSON # string value, so sed reads it. # The host `.cottondb/host.json` names, or nothing when there is no file or it # names none. host_config_host() { local file="$1/.cottondb/host.json" [ -f "$file" ] || return 0 sed -n 's/.*"host"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' "$file" | head -1 } # Resolve the home remote for the checkout at $1 (default: the current one). # # The three variables are OUT PARAMETERS — shell has no other way to answer three # things — so shellcheck's "appears unused" is about this file, not about the # callers that read them. # shellcheck disable=SC2034 host_remote() { local root="${1:-}" host if [ -z "$root" ]; then root="$(git rev-parse --show-toplevel 2>/dev/null || pwd)" fi host="$(host_config_host "$root")" HOST_REMOTE="origin" HOST_REMOTE_MAIN="origin/main" if [ -n "$host" ] && [ "$host" != forgejo ]; then HOST_REMOTE_WHY="remote \"origin\" — .cottondb/host.json could not be read" elif git -C "$root" remote 2>/dev/null | grep -qxF origin; then HOST_REMOTE_WHY="remote \"origin\" — the host .cottondb/host.json names" else HOST_REMOTE_WHY="remote \"origin\" — this checkout has no \"origin\" remote (host: forgejo)" fi }