#!/bin/sh # gitbash installer - installs gitbash without npm or node. # # Downloads the package published on npm (released only from GitHub Actions, # with provenance, after approval with 2FA) and checks it against the sha512 # integrity npm records for that version before installing anything. # # curl -fsSL https://raw.githubusercontent.com/jaggli/gitbash/main/install.sh | sh # wget -qO- https://raw.githubusercontent.com/jaggli/gitbash/main/install.sh | sh # # Environment: # GITBASH_VERSION version to install, e.g. 3.0.0 or v3.0.0 (default: latest) # GITBASH_INSTALL_DIR where the files go (default: ${XDG_DATA_HOME:-~/.local/share}/gitbash) # GITBASH_BIN_DIR where the gitbash symlink goes (default: ~/.local/bin) # # Works in Git Bash on Windows, where a small launcher script replaces the symlink. # # Run it again, or 'gitbash --update' (which runs the installed copy of this # script), to upgrade. set -eu REGISTRY="https://registry.npmjs.org/gitbash" INSTALL_DIR="${GITBASH_INSTALL_DIR:-${XDG_DATA_HOME:-$HOME/.local/share}/gitbash}" BIN_DIR="${GITBASH_BIN_DIR:-$HOME/.local/bin}" say() { printf '%s\n' "$*"; } die() { printf 'gitbash install: %s\n' "$*" >&2; exit 1; } if command -v curl >/dev/null 2>&1; then fetch() { curl -fsSL "$1"; } fetch_to() { curl -fsSL -o "$2" "$1"; } elif command -v wget >/dev/null 2>&1; then fetch() { wget -qO- "$1"; } fetch_to() { wget -qO "$2" "$1"; } else die "curl or wget is required" fi command -v tar >/dev/null 2>&1 || die "tar is required" command -v bash >/dev/null 2>&1 || die "bash is required to run gitbash" command -v git >/dev/null 2>&1 || say "Warning: git is not installed; gitbash needs git >= 2.23." # SHA-512 of a file, in hex sha512_hex() { if command -v sha512sum >/dev/null 2>&1; then sha512sum "$1" | cut -d ' ' -f 1 elif command -v shasum >/dev/null 2>&1; then shasum -a 512 "$1" | cut -d ' ' -f 1 elif command -v openssl >/dev/null 2>&1; then openssl dgst -sha512 "$1" | sed 's/.*= *//' else return 1 fi } # Base64 (npm's integrity format) to hex base64_to_hex() { { printf '%s' "$1" | base64 -d 2>/dev/null || printf '%s' "$1" | base64 -D 2>/dev/null || printf '%s' "$1" | openssl base64 -d -A 2>/dev/null; } | od -An -v -tx1 | tr -d ' \n' } # A string field of npm's (single-line) version document json_field() { # shellcheck disable=SC2020 # each of , { } becomes a newline printf '%s' "$1" | tr ',{}' '\n\n\n' | sed -n "s/^\"$2\":\"\([^\"]*\)\"\$/\1/p" | head -n 1 } # Returns 0 for a plain version number like 3.0.0 (digits and exactly two dots) valid_version() { case "$1" in *[!0-9.]*|.*|*.|*..*|*.*.*.*) return 1 ;; *.*.*) return 0 ;; esac return 1 } # Resolve the version, then read its tarball URL and checksum from npm version="${GITBASH_VERSION:-latest}" version="${version#v}" if [ "$version" != latest ] && ! valid_version "$version"; then die "invalid GITBASH_VERSION '$version' (use e.g. 3.0.0)" fi if [ "$version" = latest ]; then # Not the version document's "version": its npm scripts have one too tags="$(fetch "https://registry.npmjs.org/-/package/gitbash/dist-tags")" || die "could not look up the latest version on npm (set GITBASH_VERSION to pick one)" version="$(json_field "$tags" latest)" valid_version "$version" || die "could not look up the latest version on npm (set GITBASH_VERSION to pick one)" fi meta="$(fetch "$REGISTRY/$version")" || die "could not find gitbash $version on npm" tarball="$(json_field "$meta" tarball)" integrity="$(json_field "$meta" integrity)" case "$tarball" in "$REGISTRY/-/gitbash-$version.tgz") ;; *) die "unexpected package location '$tarball'" ;; esac case "$integrity" in sha512-?*) ;; *) die "npm has no sha512 checksum for gitbash $version" ;; esac # Refuse to replace a directory that is not a gitbash install if [ -e "$INSTALL_DIR" ] && [ ! -f "$INSTALL_DIR/bin/gitbash" ]; then die "$INSTALL_DIR exists and is not a gitbash install; set GITBASH_INSTALL_DIR to another location" fi tmp="$(mktemp -d 2>/dev/null || mktemp -d -t gitbash)" trap 'rm -rf "$tmp"' EXIT INT TERM say "Downloading gitbash $version..." fetch_to "$tarball" "$tmp/gitbash.tgz" || die "download failed" # Nothing is unpacked before the checksum matches expected="$(base64_to_hex "${integrity#sha512-}")" actual="$(sha512_hex "$tmp/gitbash.tgz")" || die "sha512sum, shasum or openssl is required to verify the download" if [ -z "$expected" ] || [ "$actual" != "$expected" ]; then die "checksum mismatch for gitbash $version: the download was not installed" fi say "Verified sha512 checksum." mkdir "$tmp/src" tar -xzf "$tmp/gitbash.tgz" -C "$tmp/src" src="$tmp/src/package" [ -f "$src/bin/gitbash" ] || die "unexpected package layout" # Copy only what gitbash needs at runtime mkdir "$tmp/install" cp -R "$src/bin" "$src/commands" "$src/package.json" "$tmp/install/" [ -f "$src/LICENSE" ] && cp "$src/LICENSE" "$tmp/install/" # This version's verified installer: 'gitbash --update' runs it from here [ -f "$src/install.sh" ] && cp "$src/install.sh" "$tmp/install/" chmod +x "$tmp/install/bin/gitbash" # Tells 'gitbash --update' how gitbash was installed printf 'method=script\nbin_dir=%s\n' "$BIN_DIR" > "$tmp/install/.gitbash-install" mkdir -p "$(dirname "$INSTALL_DIR")" "$BIN_DIR" rm -rf "$INSTALL_DIR" mv "$tmp/install" "$INSTALL_DIR" rm -f "$BIN_DIR/gitbash" if ln -s "$INSTALL_DIR/bin/gitbash" "$BIN_DIR/gitbash" 2>/dev/null && [ -L "$BIN_DIR/gitbash" ]; then linked="Linked $BIN_DIR/gitbash" else # No symlinks (Git Bash on Windows copies the file instead): a launcher script rm -f "$BIN_DIR/gitbash" target="$(printf '%s' "$INSTALL_DIR/bin/gitbash" | sed "s/'/'\\\\''/g")" printf '#!/bin/sh\nexec bash '"'"'%s'"'"' "$@"\n' "$target" > "$BIN_DIR/gitbash" chmod +x "$BIN_DIR/gitbash" linked="Created launcher $BIN_DIR/gitbash" fi say "Installed gitbash $version to $INSTALL_DIR" say "$linked" case ":$PATH:" in *":$BIN_DIR:"*) found="$(command -v gitbash 2>/dev/null || true)" if [ -n "$found" ] && [ "$found" != "$BIN_DIR/gitbash" ]; then say "" say "Note: 'gitbash' currently resolves to $found, which comes before $BIN_DIR in PATH." fi ;; *) say "" say "$BIN_DIR is not in your PATH. Add this to your .zshrc or .bashrc:" say " export PATH=\"$BIN_DIR:\$PATH\"" ;; esac say "" say "Next: run 'gitbash --config', and see the README for shell integration."