Skip to content

Instantly share code, notes, and snippets.

@dbrtly
Last active May 19, 2025 17:45
Show Gist options
  • Select an option

  • Save dbrtly/bc2f362de4daa6c005658e4930d525da to your computer and use it in GitHub Desktop.

Select an option

Save dbrtly/bc2f362de4daa6c005658e4930d525da to your computer and use it in GitHub Desktop.
#!/bin/sh
VERSION=1.11.5
# Determine the platform and architecture
get_architecture() {
UNAME_S="$(uname -s)"
UNAME_M="$(uname -m)"
case "$UNAME_S" in
Darwin)
case "$UNAME_M" in
arm64)
RETVAL="MacOS_arm64"
;;
x86_64)
RETVAL="MacOS_x86_64"
;;
*)
echo "Unsupported macOS architecture: $UNAME_M"
return 1
;;
esac
;;
Windows_NT)
case "$UNAME_M" in
arm64)
RETVAL="Windows_arm64"
;;
i386)
RETVAL="Windows_i386"
;;
x86_64)
RETVAL="Windows_x86_64"
;;
*)
echo "Unsupported Windows architecture: $UNAME_M"
return 1
;;
esac
;;
*)
echo "Unsupported platform: $UNAME_S"
return 1
;;
esac
}
# Download and install lefthook
install_lefthook() {
get_architecture || return 1
local ARCH="$RETVAL"
local DOWNLOAD_URL="https://github.com/evilmartians/lefthook/releases/download/v${VERSION}/lefthook_${VERSION}_${ARCH}.gz"
local INSTALL_DIR="$HOME/.local/bin" # Or choose another preferred location
local BINARY_NAME="lefthook"
local EXT=""
# Determine the binary extension for the platform
case "$UNAME_S" in
Windows_NT)
EXT=".exe"
BINARY_NAME="lefthook.exe"
;;
esac
# Create install directory if it doesn't exist
mkdir -p "$INSTALL_DIR"
# Check if the install directory is already in PATH
case ":$PATH:" in
*:"$INSTALL_DIR":*)
echo "${INSTALL_DIR} is already in PATH, skipping addition."
;;
*)
echo "${INSTALL_DIR} is not in PATH, adding it."
# Add the install directory to PATH
echo "Adding $INSTALL_DIR to PATH"
export PATH="$PATH:$INSTALL_DIR"
echo 'export PATH="$PATH:$INSTALL_DIR"' >> ~/.profile # Or appropriate rc file
echo 'export PATH="$PATH:$INSTALL_DIR"' >> ~/.zshrc
echo "Please ensure that ${INSTALL_DIR} is in your PATH."
;;
esac
# Download the appropriate lefthook binary
echo "Downloading lefthook from $DOWNLOAD_URL"
curl -L "$DOWNLOAD_URL" -o /tmp/lefthook.gz
# Unzip the downloaded file
echo "Unzipping lefthook"
gzip -d /tmp/lefthook.gz
# Move the binary to the install directory
echo "Installing lefthook to $INSTALL_DIR"
mv "/tmp/lefthook$EXT" "$INSTALL_DIR/$BINARY_NAME"
# Set execute permissions
echo "Setting execute permissions"
chmod +x "$INSTALL_DIR/$BINARY_NAME"
echo "Lefthook installed successfully to $INSTALL_DIR"
}
lefthook self-update || install_lefthook
@Hegi
Copy link

Hegi commented May 16, 2025

On windows you can just

winget install evilmartians.lefthook
lefthook self-update

And that's it. No need for the extra fluff. Also this works in cmd, powershell and git bash alike.

@dbrtly
Copy link
Author

dbrtly commented May 19, 2025

On windows you can just

winget install evilmartians.lefthook
lefthook self-update

And that's it. No need for the extra fluff. Also this works in cmd, powershell and git bash alike.

I use macos

@Hegi
Copy link

Hegi commented May 19, 2025

On windows you can just

winget install evilmartians.lefthook
lefthook self-update

And that's it. No need for the extra fluff. Also this works in cmd, powershell and git bash alike.

I use macos

And yet, half your code is windows related

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment