Skip to content

Commit

Permalink
feat: Feat: Add automated install script
Browse files Browse the repository at this point in the history
- Adds an automated install script for easy setup.
- Includes updated installation instructions in README.
  • Loading branch information
renatogalera committed Feb 17, 2025
1 parent 93afd8f commit ddc0fa0
Show file tree
Hide file tree
Showing 2 changed files with 182 additions and 30 deletions.
71 changes: 41 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,19 +119,11 @@ API Keys via Environment Variables:
git add .
```

2. **Run AI-Commit for Commit Message (with optional Style Review)**:
2. **Commit Message Generation**
```bash
ai-commit [--review-message]
```
Launches the interactive TUI (or non-interactive commit if `--force` is used). Use the `--review-message` flag to enable AI-based style review of the commit message.

3. **Run AI-Commit for Code Review**:
```bash
ai-commit review
ai-commit
```
Executes an AI code review on staged changes, displaying suggestions.

4. **Interactive TUI Options**: When the TUI is active (after running `ai-commit` without `review`), utilize these options:
Standard interactive commit message generation, without style review.

- **Confirm Commit**: `Enter` or `y` to commit with the generated message.
- **Regenerate Message**: `r` to generate a new commit message. Track regen attempts in the UI.
Expand Down Expand Up @@ -178,70 +170,89 @@ API Keys via Environment Variables:

---

## ✍️ Examples
## ✍️ More Examples

1. **Interactive Commit with Style Review**:
```bash
ai-commit --review-message
```
Launches the interactive TUI after generating and AI-reviewing the commit message style.

2. **Force Commit with Style Review (Non-Interactive)**:
```bash
ai-commit --force --review-message
```
Directly commits staged changes after generating and AI-reviewing the commit message style, skipping the TUI. Style review feedback is printed to the terminal before commit.

3. **Basic Commit Message Generation**:
```bash
ai-commit
```
Standard interactive commit message generation, without style review.

4. **AI-Powered Code Review**:
3. **AI-Powered Code Review**:
```bash
ai-commit review
```
Executes AI code review and outputs suggestions to the terminal.

5. **Semantic Release (Manual Version)**:
4. **Semantic Release (Manual Version)**:
```bash
ai-commit --semantic-release --manual-semver
```
Semantic release with manual version selection TUI.

6. **Provider and Model Selection**:
5. **Provider and Model Selection**:
```bash
ai-commit --provider=openai --model=gpt-4 --apiKey=sk-...
ai-commit --provider=gemini --model=models/gemini-2.0-flash --geminiApiKey=...
ai-commit --provider=anthropic --model=claude-3-sonnet --anthropicApiKey=...
ai-commit --provider=deepseek --model=deepseek-chat --deepseekApiKey=...
ai-commit --provider=phind --model=Phind-70B # Phind model is currently free; API key is optional
```

7. **Interactive Split Commit**:
6. **Interactive Split Commit**:
```bash
ai-commit --interactive-split
```
Starts interactive split TUI.
8. **Summarize a Commit**:
7. **Summarize a Commit**:
```bash
ai-commit summarize
```
Lists commits with `fzf`, and after you pick one, shows an AI-generated summary in the terminal.

---

## 🚀 Get Started
Below is an updated **Installation** section for the README, which now includes instructions for installing AI-Commit via the automated installation script.

Take your commit messages and code quality to the next level with AI-Commit! Install it now to generate smarter commits, enforce message styles, and gain AI-driven insights into your code changes.
---

## 🛠️ Installation

You can install **AI-Commit** using one of two methods: via our automated installation script or by building from source.

### Automated Installation via Script

The installation script will:

- **Detect** your operating system and CPU architecture.
- **Fetch** the latest release of AI-Commit from GitHub.
- **Download** the appropriate binary asset.
- **Set** the executable permission.
- **Install** the binary to `/usr/local/bin` (using `sudo` if required).

To install via the script, run the following commands in your terminal:

```bash
curl -sL https://raw.githubusercontent.com/renatogalera/ai-commit/main/scripts/install_ai_commit.sh | bash
```

*Note*: If you are not running as root, the script will prompt for your password to use `sudo` when moving the binary.

### Building from Source

If you prefer to build AI-Commit manually from source, follow these steps:

```bash
git clone https://github.com/renatogalera/ai-commit.git
cd ai-commit
go build -o ai-commit ./cmd/ai-commit
# Optionally, move the binary into your PATH for global access:
sudo mv ai-commit /usr/local/bin/
```

---
Once installed, you can run `ai-commit` from your terminal to start generating AI-powered commit messages and reviews.

---
141 changes: 141 additions & 0 deletions scripts/install_ai_commit.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
#!/bin/bash
# install_ai_commit.sh
# This script automatically downloads the latest release of ai-commit from GitHub,
# detects your OS and CPU architecture, selects the matching asset,
# downloads it, makes it executable, and installs it to /usr/local/bin.
#
# Requirements: curl, jq, and sudo (if not running as root).

set -euo pipefail

###########################################
# Function: error_exit
# Prints an error message and exits.
###########################################
error_exit() {
echo "Error: $1" >&2
exit 1
}

###########################################
# Check for required commands: curl and jq
###########################################
if ! command -v curl &>/dev/null; then
error_exit "curl is not installed. Please install curl."
fi

if ! command -v jq &>/dev/null; then
error_exit "jq is not installed. Please install jq."
fi

###########################################
# Hard-coded GitHub repository details
###########################################
GITHUB_OWNER="renatogalera"
GITHUB_REPO="ai-commit"
API_URL="https://api.github.com/repos/${GITHUB_OWNER}/${GITHUB_REPO}/releases/latest"

echo "Fetching latest release information for ${GITHUB_OWNER}/${GITHUB_REPO}..."

###########################################
# Retrieve the latest release data
###########################################
API_RESPONSE=$(curl -sL "$API_URL")
if echo "$API_RESPONSE" | jq -e 'has("message") and .message == "Not Found"' >/dev/null; then
error_exit "Repository ${GITHUB_OWNER}/${GITHUB_REPO} not found or no releases available."
fi

# Extract release information for logging
RELEASE_TAG=$(echo "$API_RESPONSE" | jq -r '.tag_name')
RELEASE_NAME=$(echo "$API_RESPONSE" | jq -r '.name')
echo "Latest release: ${RELEASE_TAG} - ${RELEASE_NAME}"

###########################################
# Detect OS and CPU architecture
###########################################
OS_TYPE=$(uname -s)
case "$OS_TYPE" in
Linux*) os="linux" ;;
Darwin*) os="darwin" ;;
CYGWIN*|MINGW*|MSYS*) os="windows" ;;
*) error_exit "Unsupported OS: $OS_TYPE" ;;
esac

ARCH_TYPE=$(uname -m)
case "$ARCH_TYPE" in
x86_64) arch="amd64" ;;
aarch64|arm64) arch="arm64" ;;
armv7l) arch="armv7" ;;
*) error_exit "Unsupported architecture: $ARCH_TYPE" ;;
esac

echo "Detected system: OS=${os}, Architecture=${arch}"

###########################################
# Find the asset matching the system details
# (case-insensitive search)
###########################################
ASSET_INFO=$(echo "$API_RESPONSE" | jq --arg os "$os" --arg arch "$arch" '
.assets[] | select(
(.name | ascii_downcase | contains($os)) and
(.name | ascii_downcase | contains($arch))
)
')

if [ -z "$ASSET_INFO" ]; then
error_exit "No asset found for OS '${os}' and architecture '${arch}'."
fi

# Ensure there is only one matching asset
MATCH_COUNT=$(echo "$API_RESPONSE" | jq --arg os "$os" --arg arch "$arch" '
[.assets[] | select(
(.name | ascii_downcase | contains($os)) and
(.name | ascii_downcase | contains($arch))
)] | length
')
if [ "$MATCH_COUNT" -gt 1 ]; then
error_exit "Multiple assets found for OS '${os}' and architecture '${arch}'. Please refine your asset naming."
fi

ASSET_URL=$(echo "$ASSET_INFO" | jq -r '.browser_download_url')
ASSET_NAME=$(echo "$ASSET_INFO" | jq -r '.name')

echo "Selected asset: ${ASSET_NAME}"
echo "Downloading asset from: ${ASSET_URL}..."

###########################################
# Download the asset to a temporary file
###########################################
TMP_FILE=$(mktemp /tmp/"${ASSET_NAME}".XXXXXX) || error_exit "Failed to create a temporary file."

HTTP_STATUS=$(curl -L -o "$TMP_FILE" -w "%{http_code}" "$ASSET_URL")
if [ "$HTTP_STATUS" -ne 200 ]; then
rm -f "$TMP_FILE"
error_exit "Download failed. HTTP status code: $HTTP_STATUS"
fi

echo "Download completed."

###########################################
# Validate the downloaded file and set permissions
###########################################
if [ ! -s "$TMP_FILE" ]; then
rm -f "$TMP_FILE"
error_exit "Downloaded file is empty."
fi

chmod +x "$TMP_FILE" || error_exit "Failed to set execute permission on the downloaded file."

###########################################
# Install the asset to /usr/local/bin
###########################################
DESTINATION="/usr/local/bin/ai-commit"
echo "Installing to ${DESTINATION}..."

if [ "$EUID" -ne 0 ]; then
sudo mv "$TMP_FILE" "$DESTINATION" || { rm -f "$TMP_FILE"; error_exit "Failed to move file to ${DESTINATION}."; }
else
mv "$TMP_FILE" "$DESTINATION" || { rm -f "$TMP_FILE"; error_exit "Failed to move file to ${DESTINATION}."; }
fi

echo "Installation complete. 'ai-commit' is now available in /usr/local/bin."

0 comments on commit ddc0fa0

Please sign in to comment.