|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Description: |
| 4 | +# A wrapper script to run `git-conceal` (https://github.com/Automattic/git-conceal) |
| 5 | +# |
| 6 | +# The wrapper automatically downloads the `git-conceal` binary from GitHub releases |
| 7 | +# for the current platform and architecture if it's not already available on the system |
| 8 | +# before executing it. |
| 9 | +# |
| 10 | +# Notes: |
| 11 | +# - On macOS, the script removes the quarantine attribute from downloaded binaries. |
| 12 | +# - The script supports macOS (Intel and ARM), Linux (x86_64), and Windows (x86_64). |
| 13 | +# |
| 14 | + |
| 15 | +set -euo pipefail |
| 16 | + |
| 17 | +# Determine the executable name based on platform |
| 18 | +OS=$(uname -s) |
| 19 | +ARCH=$(uname -m) |
| 20 | + |
| 21 | +if [[ "$OS" == "MINGW"* ]] || [[ "$OS" == "MSYS"* ]] || [[ "$OS" == "CYGWIN"* ]]; then |
| 22 | + EXECUTABLE_NAME="git-conceal.exe" |
| 23 | + IS_WINDOWS=true |
| 24 | +else |
| 25 | + EXECUTABLE_NAME="git-conceal" |
| 26 | + IS_WINDOWS=false |
| 27 | +fi |
| 28 | + |
| 29 | +# Checks if git-conceal binary exists in PATH or current directory |
| 30 | +find_git_conceal_binary() { |
| 31 | + # Check in PATH first |
| 32 | + if command -v "$EXECUTABLE_NAME" >/dev/null 2>&1; then |
| 33 | + command -v "$EXECUTABLE_NAME" |
| 34 | + return 0 |
| 35 | + fi |
| 36 | + |
| 37 | + # Check in current directory otherwise |
| 38 | + if [[ -f "./$EXECUTABLE_NAME" ]]; then |
| 39 | + echo "./$EXECUTABLE_NAME" |
| 40 | + return 0 |
| 41 | + fi |
| 42 | + |
| 43 | + return 1 |
| 44 | +} |
| 45 | + |
| 46 | +# Try to find existing git-conceal binary |
| 47 | +if EXECUTABLE=$(find_git_conceal_binary); then |
| 48 | + exec "$EXECUTABLE" "$@" |
| 49 | +fi |
| 50 | + |
| 51 | +# If not found, download it |
| 52 | +echo "git-conceal not found locally. Downloading from GitHub releases..." |
| 53 | + |
| 54 | +# Map platform and architecture to Rust target triple format |
| 55 | +case "$OS" in |
| 56 | + Darwin) |
| 57 | + case "$ARCH" in |
| 58 | + arm64) |
| 59 | + TARGET_TRIPLE="aarch64-apple-darwin" |
| 60 | + ;; |
| 61 | + x86_64) |
| 62 | + TARGET_TRIPLE="x86_64-apple-darwin" |
| 63 | + ;; |
| 64 | + *) |
| 65 | + echo "Unsupported architecture on macOS: $ARCH" |
| 66 | + exit 2 |
| 67 | + ;; |
| 68 | + esac |
| 69 | + ;; |
| 70 | + Linux) |
| 71 | + case "$ARCH" in |
| 72 | + x86_64) |
| 73 | + TARGET_TRIPLE="x86_64-unknown-linux-gnu" |
| 74 | + ;; |
| 75 | + aarch64|arm64) |
| 76 | + TARGET_TRIPLE="aarch64-unknown-linux-gnu" |
| 77 | + ;; |
| 78 | + *) |
| 79 | + echo "Unsupported architecture on Linux: $ARCH" |
| 80 | + exit 2 |
| 81 | + ;; |
| 82 | + esac |
| 83 | + ;; |
| 84 | + MINGW*|MSYS*|CYGWIN*) |
| 85 | + case "$ARCH" in |
| 86 | + x86_64) |
| 87 | + TARGET_TRIPLE="x86_64-pc-windows-gnu" |
| 88 | + ;; |
| 89 | + *) |
| 90 | + echo "Unsupported architecture on Windows: $ARCH" |
| 91 | + exit 2 |
| 92 | + ;; |
| 93 | + esac |
| 94 | + ;; |
| 95 | + *) |
| 96 | + echo "Unsupported operating system: $OS" |
| 97 | + exit 2 |
| 98 | + ;; |
| 99 | +esac |
| 100 | + |
| 101 | +# Get the latest release tag from GitHub API |
| 102 | +LATEST_RELEASE_URL="https://api.github.com/repos/Automattic/git-conceal/releases/latest" |
| 103 | +RELEASE_INFO=$(curl -s -L "$LATEST_RELEASE_URL") |
| 104 | +if [[ -z "$RELEASE_INFO" ]]; then |
| 105 | + echo "Failed to fetch release information from GitHub" |
| 106 | + exit 1 |
| 107 | +fi |
| 108 | + |
| 109 | +# Extract the version (tag_name) from the release info |
| 110 | +VERSION=$(echo "$RELEASE_INFO" | jq -r '.tag_name // empty') |
| 111 | +if [[ -z "$VERSION" ]] || [[ "$VERSION" == "null" ]]; then |
| 112 | + echo "Failed to extract version from release information" |
| 113 | + exit 1 |
| 114 | +fi |
| 115 | + |
| 116 | +# Construct the asset name |
| 117 | +if [[ "$IS_WINDOWS" == "true" ]]; then |
| 118 | + ASSET_NAME="git-conceal-${TARGET_TRIPLE}-${VERSION}.exe" |
| 119 | +else |
| 120 | + ASSET_NAME="git-conceal-${TARGET_TRIPLE}-${VERSION}" |
| 121 | +fi |
| 122 | + |
| 123 | +# Download the binary |
| 124 | +DOWNLOAD_URL="https://github.com/Automattic/git-conceal/releases/download/${VERSION}/${ASSET_NAME}" |
| 125 | +echo "Downloading $ASSET_NAME from GitHub releases..." |
| 126 | +if ! curl -L -f -o "$EXECUTABLE_NAME" "$DOWNLOAD_URL"; then |
| 127 | + echo "Failed to download $ASSET_NAME from $DOWNLOAD_URL" |
| 128 | + exit 1 |
| 129 | +fi |
| 130 | + |
| 131 | +# Make it executable on Unix platforms |
| 132 | +if [[ "$IS_WINDOWS" == "false" ]]; then |
| 133 | + chmod +x "$EXECUTABLE_NAME" |
| 134 | + |
| 135 | + # Remove quarantine attribute on macOS |
| 136 | + if [[ "$OS" == "Darwin" ]]; then |
| 137 | + if xattr -d com.apple.quarantine "$EXECUTABLE_NAME" 2>/dev/null || true; then |
| 138 | + echo "Removed quarantine attribute from $EXECUTABLE_NAME" |
| 139 | + fi |
| 140 | + fi |
| 141 | +fi |
| 142 | + |
| 143 | +# Execute the binary |
| 144 | +exec "./$EXECUTABLE_NAME" "$@" |
0 commit comments