set -euo pipefail
MIN_VERSION="0.10.1"
REPO="driftsys/git-std"
INSTALL_DIR="${GIT_STD_INSTALL_DIR:-$HOME/.local/bin}"
die() { printf 'error: %s\n' "$1" >&2; exit 1; }
sha256_check() {
if command -v sha256sum >/dev/null 2>&1; then
sha256sum -c "$1"
else
shasum -a 256 -c "$1"
fi
}
detect_target() {
local os arch
os="$(uname -s)"
arch="$(uname -m)"
case "$os" in
Linux)
case "$arch" in
x86_64) echo "x86_64-unknown-linux-gnu" ;;
aarch64) echo "aarch64-unknown-linux-gnu" ;;
*) die "unsupported architecture: $arch" ;;
esac
;;
Darwin)
case "$arch" in
x86_64) echo "x86_64-apple-darwin" ;;
arm64) echo "aarch64-apple-darwin" ;;
*) die "unsupported architecture: $arch" ;;
esac
;;
*)
die "unsupported OS: $os (use WSL on Windows)"
;;
esac
}
version_gte() {
local IFS=.
local i a=($1) b=($2)
for ((i = 0; i < 3; i++)); do
local ai="${a[i]:-0}" bi="${b[i]:-0}"
if ((ai > bi)); then return 0; fi
if ((ai < bi)); then return 1; fi
done
return 0
}
ensure_git_std() {
if command -v git-std >/dev/null 2>&1; then
local current
current="$(git-std --version | grep -oE '[0-9]+\.[0-9]+\.[0-9]+')"
if version_gte "$current" "$MIN_VERSION"; then
return 0
fi
printf 'git-std %s found, need >= %s — upgrading\n' "$current" "$MIN_VERSION"
else
printf 'git-std not found — installing\n'
fi
local target base download_url version tmp_dir
target="$(detect_target)"
version="$(curl -sSf "https://api.github.com/repos/$REPO/releases/latest" \
| grep '"tag_name"' | head -1 | cut -d'"' -f4)"
[ -n "$version" ] || die "could not determine latest release"
base="git-std-$target"
download_url="https://github.com/$REPO/releases/download/$version/$base.tar.gz"
printf 'downloading %s\n' "$download_url"
tmp_dir="$(mktemp -d)"
trap 'rm -rf "${tmp_dir:-}"' EXIT
curl -sSfL "$download_url" -o "$tmp_dir/$base.tar.gz" \
|| die "download failed — check that the release exists for $target"
curl -sSfL "$download_url.sha256" -o "$tmp_dir/$base.tar.gz.sha256" \
|| die "checksum download failed"
(cd "$tmp_dir" && sha256_check "$base.tar.gz.sha256") \
|| die "checksum verification failed"
tar -xzf "$tmp_dir/$base.tar.gz" -C "$tmp_dir"
mkdir -p "$INSTALL_DIR"
mv "$tmp_dir/git-std" "$INSTALL_DIR/git-std"
chmod +x "$INSTALL_DIR/git-std"
printf 'installed git-std %s to %s/git-std\n' "$version" "$INSTALL_DIR"
local man_dir="${GIT_STD_MAN_DIR:-$INSTALL_DIR/../share/man/man1}"
if ls "$tmp_dir"/git-std*.1 >/dev/null 2>&1; then
mkdir -p "$man_dir"
cp "$tmp_dir"/git-std*.1 "$man_dir/"
printf 'installed man pages to %s\n' "$man_dir"
printf "hint: if 'man git-std' doesn't work, add to your shell profile:\n"
printf " export MANPATH=\"\$HOME/.local/share/man:\${MANPATH:-}\"\n"
fi
}
ensure_git_std
exec git std bootstrap