forked from danielmiessler/Personal_AI_Infrastructure
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate-pai.sh
More file actions
executable file
·350 lines (285 loc) · 14 KB
/
update-pai.sh
File metadata and controls
executable file
·350 lines (285 loc) · 14 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
#!/bin/bash
# ═══════════════════════════════════════════════════════════════════════════════
# PAI Update Script - Safe upstream sync with customization preservation
# ═══════════════════════════════════════════════════════════════════════════════
#
# USAGE:
# ./update-pai.sh # Interactive update with preview
# ./update-pai.sh --auto # Automatic update (careful!)
# ./update-pai.sh --check # Check for updates without applying
#
# WHAT IT DOES:
# 1. Fetches latest changes from upstream (danielmiessler/PAI)
# 2. Shows you what changed
# 3. Merges changes, preserving your customizations
# 4. Pushes to your fork (HyggeHacker/PAI)
# 5. Updates ~/.claude installation
#
# SAFETY:
# - Preserves USER/ directories (your customizations)
# - Checks for uncommitted changes before updating
# - Creates backup before major operations
# - Allows review before applying changes
# ═══════════════════════════════════════════════════════════════════════════════
set -euo pipefail
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
RESET='\033[0m'
# Configuration
REPO_DIR="$HOME/PAI"
INSTALL_DIR="$HOME/.claude"
UPSTREAM_REMOTE="upstream"
ORIGIN_REMOTE="origin"
BRANCH="main"
# Parse arguments
AUTO_MODE=false
CHECK_ONLY=false
while [[ $# -gt 0 ]]; do
case $1 in
--auto)
AUTO_MODE=true
shift
;;
--check)
CHECK_ONLY=true
shift
;;
-h|--help)
echo "Usage: $0 [--auto] [--check]"
echo ""
echo "Options:"
echo " --auto Automatic mode (skip confirmations)"
echo " --check Check for updates without applying"
echo " --help Show this help"
exit 0
;;
*)
echo -e "${RED}Unknown option: $1${RESET}"
exit 1
;;
esac
done
# ─────────────────────────────────────────────────────────────────────────────
# Helper Functions
# ─────────────────────────────────────────────────────────────────────────────
print_header() {
echo ""
echo -e "${CYAN}═══════════════════════════════════════════════════════════${RESET}"
echo -e "${CYAN} $1${RESET}"
echo -e "${CYAN}═══════════════════════════════════════════════════════════${RESET}"
echo ""
}
print_success() {
echo -e "${GREEN}✓${RESET} $1"
}
print_warning() {
echo -e "${YELLOW}⚠${RESET} $1"
}
print_error() {
echo -e "${RED}✗${RESET} $1"
}
print_info() {
echo -e "${BLUE}→${RESET} $1"
}
confirm() {
if [ "$AUTO_MODE" = true ]; then
return 0
fi
local prompt="$1"
read -p "$prompt [y/N]: " -n 1 -r
echo
[[ $REPLY =~ ^[Yy]$ ]]
}
# ─────────────────────────────────────────────────────────────────────────────
# Pre-flight Checks
# ─────────────────────────────────────────────────────────────────────────────
print_header "PAI Update - Pre-flight Checks"
# Check if we're in the right directory
if [ ! -d "$REPO_DIR" ]; then
print_error "Repository directory not found: $REPO_DIR"
exit 1
fi
cd "$REPO_DIR"
print_success "Found repository: $REPO_DIR"
# Check if it's a git repo
if [ ! -d ".git" ]; then
print_error "Not a git repository: $REPO_DIR"
exit 1
fi
# Check for uncommitted changes
if ! git diff-index --quiet HEAD -- 2>/dev/null; then
print_warning "You have uncommitted changes:"
git status --short
echo ""
if ! confirm "Continue anyway?"; then
print_info "Commit or stash your changes first, then run again"
exit 0
fi
fi
# Verify remotes
if ! git remote | grep -q "^${UPSTREAM_REMOTE}$"; then
print_error "Upstream remote not configured"
print_info "Run: git remote add upstream https://github.com/danielmiessler/Personal_AI_Infrastructure"
exit 1
fi
if ! git remote | grep -q "^${ORIGIN_REMOTE}$"; then
print_error "Origin remote not configured"
exit 1
fi
print_success "Git remotes configured correctly"
# ─────────────────────────────────────────────────────────────────────────────
# Fetch Updates
# ─────────────────────────────────────────────────────────────────────────────
print_header "Fetching Updates from Upstream"
print_info "Fetching from upstream (danielmiessler/PAI)..."
git fetch upstream
print_info "Fetching from origin (your fork)..."
git fetch origin
print_success "Fetch complete"
# ─────────────────────────────────────────────────────────────────────────────
# Check for Updates
# ─────────────────────────────────────────────────────────────────────────────
print_header "Checking for Updates"
# Get commit counts
LOCAL_COMMIT=$(git rev-parse HEAD)
UPSTREAM_COMMIT=$(git rev-parse upstream/$BRANCH)
COMMITS_BEHIND=$(git rev-list --count HEAD..upstream/$BRANCH)
COMMITS_AHEAD=$(git rev-list --count upstream/$BRANCH..HEAD)
echo -e "Current commit: ${YELLOW}$(git rev-parse --short HEAD)${RESET}"
echo -e "Upstream commit: ${YELLOW}$(git rev-parse --short upstream/$BRANCH)${RESET}"
echo ""
if [ "$COMMITS_BEHIND" -eq 0 ]; then
print_success "Already up to date!"
if [ "$COMMITS_AHEAD" -gt 0 ]; then
print_info "You have $COMMITS_AHEAD local commits not in upstream"
print_info "Your customizations are preserved"
fi
exit 0
fi
print_info "Your repository is ${YELLOW}$COMMITS_BEHIND commits${RESET} behind upstream"
if [ "$COMMITS_AHEAD" -gt 0 ]; then
print_info "You have ${YELLOW}$COMMITS_AHEAD local commits${RESET} not in upstream"
fi
echo ""
print_info "Recent upstream changes:"
echo ""
git log --oneline --decorate --graph HEAD..upstream/$BRANCH | head -20
if [ "$CHECK_ONLY" = true ]; then
echo ""
print_info "Check complete. Run without --check to apply updates."
exit 0
fi
# ─────────────────────────────────────────────────────────────────────────────
# Show Changed Files
# ─────────────────────────────────────────────────────────────────────────────
echo ""
print_header "Files Changed in Upstream"
echo ""
print_info "Files that will be updated:"
echo ""
git diff --name-status HEAD..upstream/$BRANCH | head -30
TOTAL_CHANGES=$(git diff --name-status HEAD..upstream/$BRANCH | wc -l)
if [ "$TOTAL_CHANGES" -gt 30 ]; then
echo ""
print_info "... and $(($TOTAL_CHANGES - 30)) more files"
fi
# Check for USER/ directory conflicts
echo ""
USER_FILE_CONFLICTS=$(git diff --name-status HEAD..upstream/$BRANCH | grep "USER/" | wc -l || true)
if [ "$USER_FILE_CONFLICTS" -gt 0 ]; then
print_warning "Upstream changed $USER_FILE_CONFLICTS files in USER/ directories"
print_warning "Your customizations may need manual merge"
echo ""
git diff --name-status HEAD..upstream/$BRANCH | grep "USER/"
fi
# ─────────────────────────────────────────────────────────────────────────────
# Confirm Update
# ─────────────────────────────────────────────────────────────────────────────
echo ""
if ! confirm "Apply these updates?"; then
print_info "Update cancelled"
exit 0
fi
# ─────────────────────────────────────────────────────────────────────────────
# Create Backup
# ─────────────────────────────────────────────────────────────────────────────
print_header "Creating Backup"
BACKUP_BRANCH="backup-$(date +%Y%m%d-%H%M%S)"
git branch "$BACKUP_BRANCH"
print_success "Created backup branch: $BACKUP_BRANCH"
print_info "Restore with: git reset --hard $BACKUP_BRANCH"
# ─────────────────────────────────────────────────────────────────────────────
# Merge Upstream Changes
# ─────────────────────────────────────────────────────────────────────────────
print_header "Merging Upstream Changes"
print_info "Merging upstream/$BRANCH into local $BRANCH..."
if git merge upstream/$BRANCH --no-edit; then
print_success "Merge successful!"
else
print_error "Merge conflicts detected"
echo ""
print_info "Conflicts:"
git status --short | grep "^UU"
echo ""
print_info "Resolve conflicts manually:"
print_info " 1. Edit conflicted files"
print_info " 2. git add <resolved-files>"
print_info " 3. git commit"
print_info " 4. ./update-pai.sh --auto (to complete)"
exit 1
fi
# ─────────────────────────────────────────────────────────────────────────────
# Push to Fork
# ─────────────────────────────────────────────────────────────────────────────
print_header "Pushing to Your Fork"
if confirm "Push changes to your fork (origin)?"; then
print_info "Pushing to origin/$BRANCH..."
git push origin $BRANCH
print_success "Pushed to your fork"
else
print_warning "Skipped push to fork"
print_info "Push manually later with: git push origin $BRANCH"
fi
# ─────────────────────────────────────────────────────────────────────────────
# Update Installation
# ─────────────────────────────────────────────────────────────────────────────
print_header "Updating ~/.claude Installation"
if [ -d "$INSTALL_DIR" ]; then
print_info "Reinstalling PAI to $INSTALL_DIR..."
if [ -f "Bundles/Official/install.ts" ]; then
if confirm "Run PAI installer to update ~/.claude?"; then
cd Bundles/Official
bun run install.ts --update
print_success "Installation updated"
else
print_warning "Skipped installation update"
print_info "Update manually: cd Bundles/Official && bun run install.ts --update"
fi
else
print_warning "Installer not found, skipping installation update"
fi
else
print_warning "$INSTALL_DIR not found, skipping installation update"
fi
# ─────────────────────────────────────────────────────────────────────────────
# Summary
# ─────────────────────────────────────────────────────────────────────────────
print_header "Update Complete!"
echo -e "${GREEN}✓${RESET} Merged ${YELLOW}$COMMITS_BEHIND commits${RESET} from upstream"
echo -e "${GREEN}✓${RESET} Backup created: ${CYAN}$BACKUP_BRANCH${RESET}"
if git remote | grep -q "^origin$"; then
ORIGIN_STATUS=$(git rev-list --count origin/$BRANCH..HEAD 2>/dev/null || echo "?")
if [ "$ORIGIN_STATUS" != "0" ] && [ "$ORIGIN_STATUS" != "?" ]; then
echo -e "${YELLOW}⚠${RESET} Your fork is ${YELLOW}$ORIGIN_STATUS commits${RESET} behind local"
echo -e " ${BLUE}→${RESET} Push with: git push origin $BRANCH"
fi
fi
echo ""
print_info "Your customizations in USER/ directories are preserved"
print_info "SYSTEM/ directories updated from upstream"
echo ""
print_success "PAI is now up to date!"