#!/bin/sh # Copyright The Retrofit Authors. All rights reserved. # Use of this source code is governed by the LICENSE file. # Install retrofit, its man pages and its shell completions from its GitHub # releases. # # curl -fsSL https://retrofit.sh/install | sh # curl -fsSL https://retrofit.sh/install | sh -s -- --wip # curl -fsSL https://retrofit.sh/install | sh -s -- --version 0.10.0 # curl -fsSL https://retrofit.sh/install | sh -s -- --binary-only # # The default installs the latest full release. It resolves the # releases/latest redirect to one tag and fetches the archive and # checksums.txt from that tag, so the two never straddle a publish, and it # calls no GitHub API. --wip installs the newest WIP pre-release through the # API, honouring GITHUB_TOKEN. The archive's SHA-256 is checked against # checksums.txt before anything is extracted. The binary goes to # $RETROFIT_INSTALL_DIR, else ~/.local/bin, without sudo. # # When that directory is /bin, the archive's man pages go to # /share/man/man1, where man finds them through PATH, and its zsh # completion to /share/zsh/site-functions; the bash and fish # completions go to their per-user directories under $XDG_DATA_HOME and # $XDG_CONFIG_HOME. Any other directory, or --binary-only, gets the binary # alone. # # RETROFIT_INSTALL_GITHUB and RETROFIT_INSTALL_API name other release and # API bases (a mirror, or the offline test's fixture server). set -eu # The public tag of a version. Binary releases carry `bin-vX.Y.Z` until the # source release, then plain `vX.Y.Z` (process # rfd/0015/evidence/tag-namespace-2026-10-21.md); flip this at that release. TAG_PREFIX=bin-v GITHUB_BASE=${RETROFIT_INSTALL_GITHUB:-https://github.com/MonotonicHQ/retrofit} API_BASE=${RETROFIT_INSTALL_API:-https://api.github.com/repos/MonotonicHQ/retrofit} say() { printf 'retrofit-install: %s\n' "$*" >&2 } die() { say "error: $*" exit 1 } usage() { cat <<'EOF' Install retrofit from its GitHub releases. usage: install.sh [--wip | --version X] [--binary-only] (no flag) install the latest release --wip install the newest WIP pre-release (uses the GitHub API; set GITHUB_TOKEN to raise its rate limit) --version X install release X (0.10.0, v0.10.0 or bin-v0.10.0) --binary-only install the binary alone, without the man pages and shell completions --help print this help Through a pipe, pass flags after `sh -s --`: curl -fsSL https://retrofit.sh/install | sh -s -- --wip The binary is installed to $RETROFIT_INSTALL_DIR, else ~/.local/bin. When that directory is /bin, the man pages go to /share/man/man1 and the zsh completion to /share/zsh/site-functions; the bash completion goes to ${XDG_DATA_HOME:-~/.local/share}/bash-completion/completions and the fish completion to ${XDG_CONFIG_HOME:-~/.config}/fish/completions. EOF } need() { command -v "$1" >/dev/null 2>&1 || die "$1 is required but was not found on PATH" } # platform prints _ in Go's names, the release asset's suffix. platform() { s=$(uname -s) m=$(uname -m) case $s in Linux) os=linux ;; Darwin) os=darwin ;; *) die "unsupported operating system: $s (releases cover Linux and macOS)" ;; esac case $m in x86_64 | amd64) arch=amd64 ;; aarch64 | arm64) arch=arm64 ;; *) die "unsupported architecture: $m (releases cover amd64 and arm64)" ;; esac printf '%s_%s\n' "$os" "$arch" } # valid_tag refuses anything that is not a plain tag name before it reaches # a URL or a message. valid_tag() { case $1 in '' | *[!A-Za-z0-9._-]*) return 1 ;; esac return 0 } # latest_tag follows releases/latest to /releases/tag/. Before the # first full release GitHub answers 404, or sends the reader to the release # list; both mean there is nothing to install by default. latest_tag() { url="$GITHUB_BASE/releases/latest" out=$(curl -sSIL -o /dev/null -w '%{http_code} %{url_effective}' "$url") || die "could not reach $url" code=${out%% *} effective=${out#* } case $effective in */releases/tag/*) ;; *) code=404 ;; esac if [ "$code" = 404 ]; then say "no release yet: retrofit has no full release to install." say "install the newest WIP build with --wip:" say " curl -fsSL https://retrofit.sh/install | sh -s -- --wip" exit 1 fi [ "$code" = 200 ] || die "unexpected HTTP $code from $url" tag=${effective##*/releases/tag/} valid_tag "$tag" || die "unexpected release URL: $effective" printf '%s\n' "$tag" } # wip_tag picks the highest-versioned WIP among the published pre-releases, # whatever their order in the API's list. The JSON is read without jq: each # key of interest starts a fragment once the text is split at commas and # braces, and quotes inside strings are escaped, so a release body cannot # forge one. Drafts are skipped: their assets cannot be downloaded. wip_tag() { url="$API_BASE/releases?per_page=100" if [ -n "${GITHUB_TOKEN:-}" ]; then json=$(curl -fsSL -H 'Accept: application/vnd.github+json' \ -H "Authorization: Bearer $GITHUB_TOKEN" "$url") || die "could not list releases from $url" else json=$(curl -fsSL -H 'Accept: application/vnd.github+json' "$url") || die "could not list releases from $url (set GITHUB_TOKEN if rate-limited)" fi # shellcheck disable=SC2020 # each of the three characters becomes a newline tag=$(printf '%s\n' "$json" | tr ',{}' '\n\n\n' | awk ' function flush() { if (t != "" && d == "false" && p == "true") print t t = ""; d = ""; p = "" } /^[ \t]*"tag_name"[ \t]*:/ { flush() v = $0 sub(/^[ \t]*"tag_name"[ \t]*:[ \t]*"/, "", v) sub(/"[ \t]*$/, "", v) t = v next } /^[ \t]*"draft"[ \t]*:/ { d = ($0 ~ /true/) ? "true" : "false"; next } /^[ \t]*"prerelease"[ \t]*:/ { p = ($0 ~ /true/) ? "true" : "false"; next } END { flush() } ' | sed -n 's/^\(bin-\)\{0,1\}v\([0-9][0-9]*\)\.\([0-9][0-9]*\)\.\([0-9][0-9]*\)-wip\.\([0-9][0-9]*\)$/\2 \3 \4 \5 &/p' | sort -k1,1n -k2,2n -k3,3n -k4,4n | tail -n 1 | cut -d' ' -f5) [ -n "$tag" ] || die "no WIP release found at $url" printf '%s\n' "$tag" } # version_tag maps 0.10.0, v0.10.0 and bin-v0.10.0 to the public tag. version_tag() { v=$1 case $v in bin-v*) v=${v#bin-v} ;; v*) v=${v#v} ;; esac case $v in [0-9]*.[0-9]*.[0-9]*) ;; *) die "not a version: $1 (expected X.Y.Z, e.g. 0.10.0)" ;; esac valid_tag "$v" || die "not a version: $1 (expected X.Y.Z, e.g. 0.10.0)" printf '%s%s\n' "$TAG_PREFIX" "$v" } sha256() { if command -v sha256sum >/dev/null 2>&1; then sha256sum "$1" | cut -d' ' -f1 elif command -v shasum >/dev/null 2>&1; then shasum -a 256 "$1" | cut -d' ' -f1 else die "sha256sum or shasum is required to verify the download" fi } # resolve follows a chain of symlinks to the file it names. resolve() { p=$1 n=0 while [ -L "$p" ] && [ "$n" -lt 40 ]; do l=$(readlink "$p") || break case $l in /*) p=$l ;; *) p=$(dirname "$p")/$l ;; esac n=$((n + 1)) done printf '%s\n' "$p" } is_homebrew() { case $1 in */Caskroom/* | */Cellar/*) return 0 ;; esac if command -v brew >/dev/null 2>&1; then prefix=$(brew --prefix 2>/dev/null) || prefix= if [ -n "$prefix" ]; then case $1 in "$prefix"/*) return 0 ;; esac fi fi return 1 } # path_advice says whether the shell will find the new binary: a PATH hint # when the install dir is not on PATH, a warning when another retrofit, # such as a Homebrew one, comes before it. path_advice() { dir=$1 case ":$PATH:" in *":$dir:"* | *":$dir/:"*) ;; *) say "$dir is not on PATH; add it to your shell profile:" say " export PATH=\"$dir:\$PATH\"" return 0 ;; esac found=$(command -v retrofit 2>/dev/null) || return 0 [ "$found" = "$dir/retrofit" ] && return 0 real=$(resolve "$found") if is_homebrew "$real"; then say "warning: a Homebrew retrofit at $found comes before $dir on PATH," say "so \`retrofit\` still runs that one. Remove it (brew uninstall retrofit)" say "or put $dir earlier on PATH." else say "warning: another retrofit at $found comes before $dir on PATH," say "so \`retrofit\` still runs that one." fi } # install_file copies src to destdir/name beside the target, then renames # it over the target: a running retrofit keeps its old file, and a failed # copy leaves the old file in place. It returns non-zero on any failure and # leaves partial set for cleanup until the rename lands. install_file() { src=$1 destdir=$2 name=$3 perm=$4 mkdir -p "$destdir" || return 1 partial="$destdir/.$name.$$" cp "$src" "$partial" || return 1 chmod "$perm" "$partial" || return 1 mv -f "$partial" "$destdir/$name" || return 1 partial= } # install_extra installs one man page or completion script, or stops naming # the file; the binary is already in place by then. install_extra() { install_file "$1" "$2" "$3" 0644 || die "could not install $2/$3; retrofit itself is installed at $dir/retrofit (--binary-only skips the man pages and completions)" say "installed $2/$3" } # shquote prints its argument single-quoted for a shell, so a printed line # a user pastes keeps a path with spaces or quotes as one word. shquote() { printf "'%s'" "$(printf '%s' "$1" | sed "s/'/'\\\\''/g")" } # man_advice prints the MANPATH line to add when MANPATH is set without an # empty component, which is when man stops deriving its path from PATH. man_advice() { mandir=$1 [ -n "${MANPATH:-}" ] || return 0 case ":$MANPATH:" in *::* | *":$mandir:"* | *":$mandir/:"*) return 0 ;; esac say "MANPATH is set, so man will not find $mandir; add it to your shell profile:" say " export MANPATH=\"$mandir:\$MANPATH\"" } # install_extras installs the archive's man pages and completion scripts # when the install dir is /bin. An archive from before they shipped # installs the binary alone. install_extras() { x=$tmp/x if [ ! -d "$x/man/man1" ] && [ ! -d "$x/completions" ]; then say "$tag carries no man pages or completions; installed the binary alone" return 0 fi d=${dir%/} case $d in ?*/bin) pfx=${d%/bin} ;; *) say "$dir is not a /bin directory, so only the binary is installed;" say "the man pages (man/man1/) and completions (completions/) are in" say " $base/$asset" return 0 ;; esac # The bash and fish completion dirs sit under HOME unless XDG names # them; with neither, that completion is skipped. bdir= if [ -n "${XDG_DATA_HOME:-}" ]; then bdir=$XDG_DATA_HOME/bash-completion/completions elif [ -n "${HOME:-}" ]; then bdir=$HOME/.local/share/bash-completion/completions fi fdir= if [ -n "${XDG_CONFIG_HOME:-}" ]; then fdir=$XDG_CONFIG_HOME/fish/completions elif [ -n "${HOME:-}" ]; then fdir=$HOME/.config/fish/completions fi mans=0 for f in "$x"/man/man1/*.1; do if [ -f "$f" ] && [ ! -L "$f" ]; then install_extra "$f" "$pfx/share/man/man1" "${f##*/}" mans=1 fi done c=$x/completions if [ -f "$c/retrofit.bash" ] && [ ! -L "$c/retrofit.bash" ]; then if [ -n "$bdir" ]; then install_extra "$c/retrofit.bash" "$bdir" retrofit else say "HOME and XDG_DATA_HOME are not set, so the bash completion is not installed" fi fi if [ -f "$c/retrofit.fish" ] && [ ! -L "$c/retrofit.fish" ]; then if [ -n "$fdir" ]; then install_extra "$c/retrofit.fish" "$fdir" retrofit.fish else say "HOME and XDG_CONFIG_HOME are not set, so the fish completion is not installed" fi fi if [ -f "$c/_retrofit" ] && [ ! -L "$c/_retrofit" ]; then install_extra "$c/_retrofit" "$pfx/share/zsh/site-functions" _retrofit say "zsh has no per-user completion directory; add this line to ~/.zshrc before compinit:" say " fpath=($(shquote "$pfx/share/zsh/site-functions") \$fpath)" fi if [ "$mans" = 1 ]; then man_advice "$pfx/share/man" fi } cleanup() { rm -rf "$tmp" if [ -n "$partial" ]; then rm -f "$partial" fi } main() { mode=latest want= extras=1 while [ $# -gt 0 ]; do case $1 in --wip) mode=wip ;; --version) [ $# -ge 2 ] || die "--version needs a value, e.g. --version 0.10.0" mode=version want=$2 shift ;; --version=*) mode=version want=${1#--version=} ;; --binary-only) extras= ;; -h | --help) usage exit 0 ;; *) die "unknown option: $1 (see --help)" ;; esac shift done need curl need tar need mktemp plat=$(platform) asset="retrofit_${plat}.tar.gz" case $mode in latest) tag=$(latest_tag) ;; wip) tag=$(wip_tag) ;; version) tag=$(version_tag "$want") ;; esac dir=${RETROFIT_INSTALL_DIR:-${HOME:?HOME is not set; set RETROFIT_INSTALL_DIR}/.local/bin} tmp=$(mktemp -d) partial= trap cleanup EXIT trap 'exit 1' HUP INT TERM base="$GITHUB_BASE/releases/download/$tag" say "installing retrofit $tag ($plat)" curl -fsSL -o "$tmp/checksums.txt" "$base/checksums.txt" || die "could not download $base/checksums.txt (is $tag a release?)" curl -fsSL -o "$tmp/$asset" "$base/$asset" || die "could not download $base/$asset" want_sum=$(awk -v f="$asset" '$2 == f || $2 == "*" f { print $1 }' "$tmp/checksums.txt") case $want_sum in '') die "checksums.txt for $tag has no entry for $asset; not installing" ;; *[!0-9a-f]*) die "checksums.txt for $tag has a malformed entry for $asset; not installing" ;; esac got_sum=$(sha256 "$tmp/$asset") [ "$got_sum" = "$want_sum" ] || die "checksum mismatch for $asset: checksums.txt says $want_sum, download is $got_sum; not installing" mkdir "$tmp/x" if [ -n "$extras" ]; then tar -xzf "$tmp/$asset" -C "$tmp/x" || die "could not extract $asset" else tar -xzf "$tmp/$asset" -C "$tmp/x" retrofit || die "could not extract retrofit from $asset" fi if [ ! -f "$tmp/x/retrofit" ] || [ -L "$tmp/x/retrofit" ]; then die "$asset holds no retrofit binary" fi install_file "$tmp/x/retrofit" "$dir" retrofit 0755 || die "could not install $dir/retrofit" say "installed retrofit $tag to $dir/retrofit" if [ -n "$extras" ]; then install_extras fi path_advice "$dir" } main "$@"