-
Notifications
You must be signed in to change notification settings - Fork 154
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·464 lines (397 loc) · 14.6 KB
/
install.sh
File metadata and controls
executable file
·464 lines (397 loc) · 14.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
#!/bin/bash
set -euo pipefail
IFS=$'\n\t'
# ============================================================
# Ensure HOME is set when running via MDMs (e.g. JAMF) or other environments where HOME may be unbound.
# ============================================================
INSTALL_USER=""
if [ -z "${HOME:-}" ]; then
if command -v scutil >/dev/null 2>&1; then
CURRENT_USER=$( /usr/sbin/scutil <<< "show State:/Users/ConsoleUser" | awk '/Name :/ { print $3 }' || true )
if [ -n "${CURRENT_USER:-}" ] && [ "$CURRENT_USER" != "loginwindow" ] && [ "$CURRENT_USER" != "_mbsetupuser" ]; then
export HOME=$( /usr/bin/dscl . -read "/Users/$CURRENT_USER" NFSHomeDirectory | awk '{print $2}' )
INSTALL_USER="$CURRENT_USER"
else
echo "Error: No console user logged in. Deferring installation." >&2
exit 1
fi
elif id -un >/dev/null 2>&1; then
INSTALL_USER="$(id -un)"
export HOME=$(getent passwd "$INSTALL_USER" | cut -d: -f6)
if [ -z "$HOME" ]; then
export HOME="/root"
fi
else
export HOME="/root"
fi
fi
# Ensure SHELL is set (also may be unbound in JAMF)
if [ -z "${SHELL:-}" ]; then
if command -v zsh >/dev/null 2>&1; then
SHELL="$(command -v zsh)"
elif command -v bash >/dev/null 2>&1; then
SHELL="$(command -v bash)"
else
SHELL="/bin/sh"
fi
export SHELL
fi
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
NC='\033[0m' # No Color
# GitHub repository details
# Replaced during release builds with the actual repository (e.g., "git-ai-project/git-ai")
# When set to __REPO_PLACEHOLDER__, defaults to "git-ai-project/git-ai"
REPO="__REPO_PLACEHOLDER__"
if [ "$REPO" = "__REPO_PLACEHOLDER__" ]; then
REPO="git-ai-project/git-ai"
fi
# Version placeholder - replaced during release builds with actual version (e.g., "v1.0.24")
# When set to __VERSION_PLACEHOLDER__, defaults to "latest"
PINNED_VERSION="__VERSION_PLACEHOLDER__"
# Embedded checksums - replaced during release builds with actual SHA256 checksums
# Format: "hash filename|hash filename|..." (pipe-separated)
# When set to __CHECKSUMS_PLACEHOLDER__, checksum verification is skipped
EMBEDDED_CHECKSUMS="__CHECKSUMS_PLACEHOLDER__"
# Function to print error messages
error() {
echo -e "${RED}Error: $1${NC}" >&2
exit 1
}
warn() {
echo -e "${YELLOW}Warning: $1${NC}" >&2
}
# Function to print success messages
success() {
echo -e "${GREEN}$1${NC}"
}
# Function to verify checksum of downloaded binary
verify_checksum() {
local file="$1"
local binary_name="$2"
# Skip verification if no checksums are embedded
if [ "$EMBEDDED_CHECKSUMS" = "__CHECKSUMS_PLACEHOLDER__" ]; then
return 0
fi
# Extract expected checksum for this binary
local expected=""
local old_ifs="$IFS"
IFS='|' read -ra CHECKSUM_ENTRIES <<< "$EMBEDDED_CHECKSUMS"
IFS="$old_ifs"
for entry in "${CHECKSUM_ENTRIES[@]}"; do
if [[ "$entry" =~ ^[[:xdigit:]]+[[:space:]]+$binary_name$ ]]; then
expected=$(echo "$entry" | awk '{print $1}')
break
fi
done
if [ -z "$expected" ]; then
error "No checksum found for $binary_name"
fi
# Calculate actual checksum
local actual=""
if command -v sha256sum >/dev/null 2>&1; then
actual=$(sha256sum "$file" | awk '{print $1}')
elif command -v shasum >/dev/null 2>&1; then
actual=$(shasum -a 256 "$file" | awk '{print $1}')
else
warn "Neither sha256sum nor shasum available, skipping checksum verification"
return 0
fi
if [ "$expected" != "$actual" ]; then
rm -f "$file" 2>/dev/null || true
error "Checksum verification failed for $binary_name\nExpected: $expected\nActual: $actual"
fi
success "Checksum verified for $binary_name"
}
# Function to detect all shells with existing config files
# Returns shell configurations in format: "shell_name|config_file" (one per line)
detect_all_shells() {
local shells=""
# Check for bash configs (prefer .bashrc over .bash_profile)
if [ -f "$HOME/.bashrc" ]; then
shells="${shells}bash|$HOME/.bashrc\n"
elif [ -f "$HOME/.bash_profile" ]; then
shells="${shells}bash|$HOME/.bash_profile\n"
fi
# Check for zsh config
if [ -f "$HOME/.zshrc" ]; then
shells="${shells}zsh|$HOME/.zshrc\n"
fi
# Check for fish config
if [ -f "$HOME/.config/fish/config.fish" ]; then
shells="${shells}fish|$HOME/.config/fish/config.fish\n"
fi
# If no configs found, fall back to $SHELL detection and create config for that shell only
if [ -z "$shells" ]; then
local login_shell=""
if [ -n "${SHELL:-}" ]; then
login_shell=$(basename "$SHELL")
fi
case "$login_shell" in
fish)
shells="fish|$HOME/.config/fish/config.fish"
;;
zsh)
shells="zsh|$HOME/.zshrc"
;;
bash|*)
shells="bash|$HOME/.bashrc"
;;
esac
fi
# Remove trailing newline and output
printf '%b' "$shells" | sed '/^$/d'
}
detect_std_git() {
local git_path=""
# Prefer the actual executable path, ignoring aliases and functions
if git_path=$(type -P git 2>/dev/null); then
:
else
git_path=$(command -v git 2>/dev/null || true)
fi
# Last resort
if [ -z "$git_path" ]; then
git_path=$(which git 2>/dev/null || true)
fi
# Ensure we never return a path for git that contains git-ai (recursive)
if [ -n "$git_path" ] && [[ "$git_path" == *"git-ai"* ]]; then
git_path=""
fi
# If detection failed or was our own shim, try to recover from saved config
if [ -z "$git_path" ]; then
local cfg_json="$HOME/.git-ai/config.json"
if [ -f "$cfg_json" ]; then
# Extract git_path value without jq
local cfg_git_path
cfg_git_path=$(sed -n 's/.*"git_path"[[:space:]]*:[[:space:]]*"\(.*\)".*/\1/p' "$cfg_json" | head -n1 || true)
if [ -n "$cfg_git_path" ] && [[ "$cfg_git_path" != *"git-ai"* ]]; then
if "$cfg_git_path" --version >/dev/null 2>&1; then
git_path="$cfg_git_path"
fi
fi
fi
fi
# Fail if we couldn't find a standard git
if [ -z "$git_path" ]; then
error "Could not detect a standard git binary on PATH. Please ensure you have Git installed and available on your PATH. If you believe this is a bug with the installer, please file an issue at https://github.com/git-ai-project/git-ai/issues."
fi
# Verify detected git is usable
if ! "$git_path" --version >/dev/null 2>&1; then
error "Detected git at $git_path is not usable (--version failed). Please ensure you have Git installed and available on your PATH. If you believe this is a bug with the installer, please file an issue at https://github.com/git-ai-project/git-ai/issues."
fi
echo "$git_path"
}
# Detect standard git path (needed early for install)
STD_GIT_PATH=$(detect_std_git)
# Detect OS and architecture
OS=$(uname -s | tr '[:upper:]' '[:lower:]')
ARCH=$(uname -m)
# Map architecture to binary name
case $ARCH in
"x86_64")
ARCH="x64"
;;
"aarch64"|"arm64")
ARCH="arm64"
;;
*)
error "Unsupported architecture: $ARCH"
;;
esac
# Map OS to binary name
case $OS in
"darwin")
OS="macos"
;;
"linux")
OS="linux"
;;
*)
error "Unsupported operating system: $OS"
;;
esac
# Determine binary name
BINARY_NAME="git-ai-${OS}-${ARCH}"
# Determine release tag
# Priority: 1. Local binary override, 2. Pinned version (for release builds), 3. Environment variable, 4. "latest"
if [ -n "${GIT_AI_LOCAL_BINARY:-}" ]; then
RELEASE_TAG="local"
DOWNLOAD_URL=""
elif [ "$PINNED_VERSION" != "__VERSION_PLACEHOLDER__" ]; then
# Version-pinned install script from a release
RELEASE_TAG="$PINNED_VERSION"
DOWNLOAD_URL="https://github.com/${REPO}/releases/download/${RELEASE_TAG}/${BINARY_NAME}"
elif [ -n "${GIT_AI_RELEASE_TAG:-}" ] && [ "${GIT_AI_RELEASE_TAG:-}" != "latest" ]; then
# Environment variable override
RELEASE_TAG="$GIT_AI_RELEASE_TAG"
DOWNLOAD_URL="https://github.com/${REPO}/releases/download/${RELEASE_TAG}/${BINARY_NAME}"
else
# Default to latest
RELEASE_TAG="latest"
DOWNLOAD_URL="https://github.com/${REPO}/releases/latest/download/${BINARY_NAME}"
fi
# Install into the user's bin directory ~/.git-ai/bin
INSTALL_DIR="$HOME/.git-ai/bin"
# Create directory if it doesn't exist
mkdir -p "$INSTALL_DIR"
# Download and install
TMP_FILE="${INSTALL_DIR}/git-ai.tmp.$$"
if [ -n "${GIT_AI_LOCAL_BINARY:-}" ]; then
echo "Using local git-ai binary (release: ${RELEASE_TAG})..."
if [ ! -f "$GIT_AI_LOCAL_BINARY" ]; then
error "Local binary not found at $GIT_AI_LOCAL_BINARY"
fi
cp "$GIT_AI_LOCAL_BINARY" "$TMP_FILE"
else
echo "Downloading git-ai (release: ${RELEASE_TAG})..."
if ! curl --fail --location --silent --show-error -o "$TMP_FILE" "$DOWNLOAD_URL"; then
rm -f "$TMP_FILE" 2>/dev/null || true
error "Failed to download binary (HTTP error)"
fi
fi
# Basic validation: ensure file is not empty
if [ ! -s "$TMP_FILE" ]; then
rm -f "$TMP_FILE" 2>/dev/null || true
error "Downloaded file is empty"
fi
# Verify checksum if embedded (release builds only)
verify_checksum "$TMP_FILE" "$BINARY_NAME"
mv -f "$TMP_FILE" "${INSTALL_DIR}/git-ai"
# Make executable
chmod +x "${INSTALL_DIR}/git-ai"
# Symlink git to git-ai
ln -sf "${INSTALL_DIR}/git-ai" "${INSTALL_DIR}/git"
# Symlink git-og to the detected standard git path
ln -sf "$STD_GIT_PATH" "${INSTALL_DIR}/git-og"
# Remove quarantine attribute on macOS
if [ "$OS" = "macos" ]; then
xattr -d com.apple.quarantine "${INSTALL_DIR}/git-ai" 2>/dev/null || true
fi
# Create ~/.local/bin/git-ai symlink for systems where ~/.local/bin is already on PATH
LOCAL_BIN_DIR="$HOME/.local/bin"
if mkdir -p "$LOCAL_BIN_DIR" 2>/dev/null && ln -sf "${INSTALL_DIR}/git-ai" "${LOCAL_BIN_DIR}/git-ai" 2>/dev/null; then
success "Created symlink at ${LOCAL_BIN_DIR}/git-ai"
else
warn "Failed to create ~/.local/bin/git-ai symlink. This is non-fatal."
fi
success "Successfully installed git-ai into ${INSTALL_DIR}"
success "You can now run 'git-ai' from your terminal"
# Print installed version
INSTALLED_VERSION=$(${INSTALL_DIR}/git-ai --version 2>&1 || echo "unknown")
echo "Installed git-ai ${INSTALLED_VERSION}"
# Login user with install token if provided
NEED_LOGIN=false
if [ -n "${INSTALL_NONCE:-}" ] && [ -n "${API_BASE:-}" ]; then
if ! ${INSTALL_DIR}/git-ai exchange-nonce; then
NEED_LOGIN=true
fi
fi
echo "Setting up IDE/agent hooks..."
if ! ${INSTALL_DIR}/git-ai install-hooks; then
warn "Warning: Failed to set up IDE/agent hooks. Please try running 'git-ai install-hooks' manually."
else
success "Successfully set up IDE/agent hooks"
fi
# Write JSON config at ~/.git-ai/config.json (only if it doesn't exist)
CONFIG_DIR="$HOME/.git-ai"
CONFIG_JSON_PATH="$CONFIG_DIR/config.json"
mkdir -p "$CONFIG_DIR"
if [ ! -f "$CONFIG_JSON_PATH" ]; then
TMP_CFG="$CONFIG_JSON_PATH.tmp.$$"
cat >"$TMP_CFG" <<EOF
{
"git_path": "${STD_GIT_PATH}",
"feature_flags": {
"async_mode": true
}
}
EOF
mv -f "$TMP_CFG" "$CONFIG_JSON_PATH"
fi
# Add to PATH in all detected shell configurations
SHELLS_CONFIGURED=""
SHELLS_ALREADY_CONFIGURED=""
CREATED_SHELL_PATHS=""
while IFS='|' read -r shell_name config_file; do
[ -z "$shell_name" ] && continue
# Generate shell-appropriate PATH command
if [ "$shell_name" = "fish" ]; then
path_cmd="fish_add_path -g \"$INSTALL_DIR\""
# Create fish config directory if it doesn't exist (for fallback case)
config_dir="$(dirname "$config_file")"
if [ ! -d "$config_dir" ]; then
mkdir -p "$config_dir"
CREATED_SHELL_PATHS="${CREATED_SHELL_PATHS}${config_dir}\n"
fi
else
path_cmd="export PATH=\"$INSTALL_DIR:\$PATH\""
fi
# Create config file if it doesn't exist (for fallback case when no configs found)
if [ ! -f "$config_file" ]; then
CREATED_SHELL_PATHS="${CREATED_SHELL_PATHS}${config_file}\n"
fi
touch "$config_file"
# Append if not already present
if ! grep -qsF "$INSTALL_DIR" "$config_file"; then
echo "" >> "$config_file"
echo "# Added by git-ai installer on $(date)" >> "$config_file"
echo "$path_cmd" >> "$config_file"
SHELLS_CONFIGURED="${SHELLS_CONFIGURED}${shell_name}|${config_file}\n"
else
SHELLS_ALREADY_CONFIGURED="${SHELLS_ALREADY_CONFIGURED}${shell_name}|${config_file}\n"
fi
done <<< "$(detect_all_shells)"
# Display results to user
if [ -n "$SHELLS_CONFIGURED" ]; then
echo ""
echo "Updated shell configurations:"
printf '%b' "$SHELLS_CONFIGURED" | while IFS='|' read -r shell_name config_file; do
[ -z "$shell_name" ] && continue
success " ✓ $config_file"
done
echo ""
echo "To apply changes immediately:"
printf '%b' "$SHELLS_CONFIGURED" | while IFS='|' read -r shell_name config_file; do
[ -z "$shell_name" ] && continue
if [ "$shell_name" = "fish" ]; then
echo " - For fish: source $config_file"
else
echo " - For $shell_name: source $config_file"
fi
done
fi
if [ -n "$SHELLS_ALREADY_CONFIGURED" ]; then
echo ""
echo "Already configured (no changes needed):"
printf '%b' "$SHELLS_ALREADY_CONFIGURED" | while IFS='|' read -r shell_name config_file; do
[ -z "$shell_name" ] && continue
echo " ✓ $config_file"
done
fi
if [ -z "$SHELLS_CONFIGURED" ] && [ -z "$SHELLS_ALREADY_CONFIGURED" ]; then
echo ""
echo "Could not detect any shell config files."
echo "Please add the following line to your shell config and restart:"
echo " export PATH=\"$INSTALL_DIR:\$PATH\""
fi
# Fix file ownership when running as root for a different user (MDM deployments)
if [ "$(id -u)" = "0" ] && [ -n "$INSTALL_USER" ]; then
chown -R "$INSTALL_USER" "$HOME/.git-ai" 2>/dev/null || true
if [ -n "$CREATED_SHELL_PATHS" ]; then
printf '%b' "$CREATED_SHELL_PATHS" | while IFS= read -r created_path; do
[ -z "$created_path" ] && continue
chown "$INSTALL_USER" "$created_path" 2>/dev/null || true
done
fi
fi
echo ""
echo -e "${YELLOW}Close and reopen your terminal and IDE sessions to use git-ai.${NC}"
# If nonce exchange failed, run interactive login
if [ "$NEED_LOGIN" = true ]; then
echo ""
echo "Launching login..."
${INSTALL_DIR}/git-ai login
fi