-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapply_longcat_patch.sh
More file actions
599 lines (526 loc) · 19.6 KB
/
apply_longcat_patch.sh
File metadata and controls
599 lines (526 loc) · 19.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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
#!/bin/bash
# apply_longcat_patch.sh - Apply longcat_patch to diffusion-pipe safely
# Author: Claude Code Assistant
# Description: Script to apply patches from flet_app/modules/longcat_patch to diffusion-pipe
# set -e # Exit on any error
# Color codes for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Configuration
# Resolve paths relative to this script location to avoid CWD issues
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SOURCE_DIR="${SCRIPT_DIR}/flet_app/modules/longcat_patch"
TARGET_DIR="${SCRIPT_DIR}/diffusion-pipe"
BACKUP_DIR="${TARGET_DIR}/backup_$(date +%Y%m%d_%H%M%S)"
# Files to patch
# Only include files that actually exist in the source patch directory
declare -a FILES_TO_PATCH=(
".gitmodules"
"train.py"
"examples/longcat.toml"
"models/longcat_video.py"
)
# Function to print colored output
print_status() {
echo -e "${BLUE}[INFO]${NC} $1"
return 0
}
print_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
return 0
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
return 0
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
return 0
}
# Function to show help
show_help() {
cat << EOF
Usage: $0 [OPTIONS]
Apply longcat_patch to diffusion-pipe safely with backup support.
OPTIONS:
-h, --help Show this help message
-d, --dry-run Show what would be done without making changes
-r, --rollback Restore from the most recent backup
-v, --verbose Show detailed output
EXAMPLES:
$0 # Apply patch with interactive prompts
$0 --dry-run # Preview changes without applying
$0 --rollback # Restore from backup
EOF
}
# Function to check if directories exist
check_directories() {
if [[ ! -d "$SOURCE_DIR" ]]; then
print_error "Source directory not found: $SOURCE_DIR"
exit 1
fi
if [[ ! -d "$TARGET_DIR" ]]; then
print_error "Target directory not found: $TARGET_DIR"
exit 1
fi
}
# Function to create backup
create_backup() {
print_status "Creating backup in $BACKUP_DIR"
mkdir -p "$BACKUP_DIR"
for file in "${FILES_TO_PATCH[@]}"; do
source_file="$TARGET_DIR/$file"
if [[ -f "$source_file" ]]; then
backup_file="$BACKUP_DIR/$file"
backup_dir=$(dirname "$backup_file")
mkdir -p "$backup_dir"
cp "$source_file" "$backup_file"
if [[ "$VERBOSE" == "true" ]]; then
print_status "Backed up: $file"
fi
fi
done
print_success "Backup created successfully"
}
# Function to check submodule status
verify_submodule_status() {
local submodule_path="$TARGET_DIR/submodules/LongCat-Video"
local submodule_dir="$TARGET_DIR/submodules"
if [[ "$DRY_RUN" == "true" ]]; then
print_status "[DRY RUN] Would check LongCat-Video submodule status"
return
fi
print_status "Checking LongCat-Video submodule status..."
# Check if .gitmodules exists and has LongCat-Video entry
if [[ ! -f "$TARGET_DIR/.gitmodules" ]]; then
print_error ".gitmodules file not found"
return 1
fi
if ! grep -q "LongCat-Video" "$TARGET_DIR/.gitmodules"; then
print_error "LongCat-Video submodule not found in .gitmodules"
return 1
fi
# Check if submodule directory exists
if [[ ! -d "$submodule_path" ]]; then
print_warning "LongCat-Video submodule directory not found (needs initialization)"
return 2
fi
# Check if submodule is properly initialized
cd "$TARGET_DIR"
local status_output=$(git submodule status submodules/LongCat-Video 2>/dev/null)
if [[ -z "$status_output" ]]; then
print_warning "LongCat-Video submodule not initialized"
return 2
fi
# Parse status: first character indicates status
local status_char=${status_output:0:1}
case "$status_char" in
" ")
print_success "LongCat-Video submodule is up to date"
return 0
;;
"+")
print_warning "LongCat-Video submodule has uncommitted changes"
return 3
;;
"-")
print_warning "LongCat-Video submodule is not initialized"
return 2
;;
"U")
print_error "LongCat-Video submodule has merge conflicts"
return 4
;;
*)
print_warning "LongCat-Video submodule status: $status_output"
return 5
;;
esac
}
# Function to prompt user for submodule action
prompt_submodule_action() {
if [[ "$DRY_RUN" == "true" ]]; then
print_status "[DRY RUN] Would prompt for LongCat-Video submodule action"
return 0
fi
local exit_code
verify_submodule_status
exit_code=$?
echo ""
print_status "=== LongCat-Video Submodule Management ==="
case $exit_code in
0)
echo "✓ Submodule is up to date"
echo ""
read -p "Would you like to check for updates? (y/n): " response
if [[ "$response" =~ ^[Yy]$ ]]; then
return 2 # Update action
else
return 0 # Skip action
fi
;;
1)
echo "✗ Configuration error (missing .gitmodules entry)"
echo ""
read -p "Would you like to skip submodule handling? (y/n): " response
if [[ "$response" =~ ^[Yy]$ ]]; then
return 1 # Skip action
else
return 9 # Abort
fi
;;
2)
echo "✗ Submodule not initialized"
echo "The LongCat-Video submodule is required for longcat model functionality."
echo "Estimated download size: ~500MB - 2GB"
echo "This may take several minutes depending on your network speed."
echo ""
read -p "Initialize LongCat-Video submodule? (y/n): " response
if [[ "$response" =~ ^[Yy]$ ]]; then
return 3 # Initialize action
else
return 1 # Skip action
fi
;;
3)
echo "⚠ Submodule has uncommitted changes"
echo ""
read -p "Would you like to reset and update? (y/n): " response
if [[ "$response" =~ ^[Yy]$ ]]; then
return 2 # Update action (with reset)
else
return 0 # Skip action
fi
;;
4)
echo "✗ Submodule has merge conflicts"
echo ""
read -p "Would you like to reset and reinitialize? (y/n): " response
if [[ "$response" =~ ^[Yy]$ ]]; then
return 4 # Reinitialize action
else
return 1 # Skip action
fi
;;
*)
echo "? Unknown submodule status"
echo ""
read -p "Would you like to reinitialize submodule? (y/n): " response
if [[ "$response" =~ ^[Yy]$ ]]; then
return 4 # Reinitialize action
else
return 1 # Skip action
fi
;;
esac
}
# Ensure git knows about LongCat-Video before running git submodule commands
ensure_submodule_registered() {
local submodule_name=$1
local gitmodules_key="submodule.${submodule_name}.url"
# Read URL from .gitmodules
local submodule_url
submodule_url=$(git config --file .gitmodules --get "$gitmodules_key" || true)
if [[ -z "$submodule_url" ]]; then
print_error "Submodule URL not found in .gitmodules for ${submodule_name}"
return 1
fi
# Register in .git/config if missing
if ! git config --get "$gitmodules_key" >/dev/null 2>&1; then
print_status "Registering ${submodule_name} submodule with git"
git config "$gitmodules_key" "$submodule_url"
git config "submodule.${submodule_name}.active" true
git config "submodule.${submodule_name}.update" checkout
git submodule sync "$submodule_name" >/dev/null 2>&1 || git submodule sync >/dev/null 2>&1 || true
elif [[ "$VERBOSE" == "true" ]]; then
print_status "Submodule ${submodule_name} already registered with git"
fi
return 0
}
# Function to initialize/update submodule with retry logic
robust_submodule_operation() {
local action=$1
local max_retries=3
local retry_count=0
local submodule_name="submodules/LongCat-Video"
if [[ "$DRY_RUN" == "true" ]]; then
case $action in
3) print_status "[DRY RUN] Would initialize LongCat-Video submodule" ;;
2) print_status "[DRY RUN] Would update LongCat-Video submodule" ;;
4) print_status "[DRY RUN] Would reinitialize LongCat-Video submodule" ;;
esac
return 0
fi
cd "$TARGET_DIR"
# Ensure git knows about the submodule before running git submodule commands
ensure_submodule_registered "$submodule_name" || return 1
while [[ $retry_count -lt $max_retries ]]; do
case $action in
3) # Initialize
print_status "Initializing LongCat-Video submodule (attempt $((retry_count + 1))/$max_retries)..."
if git submodule update --init --recursive "$submodule_name"; then
print_success "LongCat-Video submodule initialized successfully"; if [[ ! -f "$TARGET_DIR/submodules/LongCat-Video/longcat_video/__init__.py" ]]; then touch "$TARGET_DIR/submodules/LongCat-Video/longcat_video/__init__.py"; print_status "Created: longcat_video/__init__.py"; fi
return 0
fi
;;
2) # Update
print_status "Updating LongCat-Video submodule (attempt $((retry_count + 1))/$max_retries)..."
if git submodule update --remote --merge "$submodule_name"; then
print_success "LongCat-Video submodule updated successfully"; if [[ ! -f "$TARGET_DIR/submodules/LongCat-Video/longcat_video/__init__.py" ]]; then touch "$TARGET_DIR/submodules/LongCat-Video/longcat_video/__init__.py"; print_status "Created: longcat_video/__init__.py"; fi
return 0
fi
;;
4) # Reinitialize
print_status "Reinitializing LongCat-Video submodule (attempt $((retry_count + 1))/$max_retries)..."
git submodule deinit -f "$submodule_name" 2>/dev/null || true
rm -rf "$TARGET_DIR/$submodule_name" 2>/dev/null || true
if git submodule update --init --recursive "$submodule_name"; then
print_success "LongCat-Video submodule reinitialized successfully"; if [[ ! -f "$TARGET_DIR/submodules/LongCat-Video/longcat_video/__init__.py" ]]; then touch "$TARGET_DIR/submodules/LongCat-Video/longcat_video/__init__.py"; print_status "Created: longcat_video/__init__.py"; fi
return 0
fi
;;
esac
retry_count=$((retry_count + 1))
if [[ $retry_count -lt $max_retries ]]; then
print_warning "Operation failed, retrying in 3 seconds..."
sleep 3
fi
done
print_error "Failed to complete submodule operation after $max_retries attempts"
print_error "Please run manually:"
case $action in
3) print_error "cd $TARGET_DIR && git submodule update --init --recursive $submodule_name" ;;
2) print_error "cd $TARGET_DIR && git submodule update --remote --merge $submodule_name" ;;
4) print_error "cd $TARGET_DIR && git submodule deinit -f $submodule_name && git submodule update --init --recursive $submodule_name" ;;
esac
return 1
}
# Auto-initialize LongCat-Video submodule without prompts
auto_initialize_submodule() {
if [[ "$DRY_RUN" == "true" ]]; then
print_status "[DRY RUN] Would auto-initialize LongCat-Video submodule"
return 0
fi
local exit_code
verify_submodule_status
exit_code=$? || true
# Auto-handle based on status without prompting
case $exit_code in
0)
if [[ "$VERBOSE" == "true" ]]; then
print_status "LongCat-Video submodule is already up to date"
fi
return 0
;;
1)
print_error "Configuration error - LongCat-Video submodule not found in .gitmodules"
return 1
;;
2)
print_status "Auto-initializing LongCat-Video submodule..."
cd "$TARGET_DIR/submodules"
if git clone https://github.com/meituan-longcat/LongCat-Video; then
print_success "LongCat-Video submodule cloned successfully"; if [[ ! -f "$TARGET_DIR/submodules/LongCat-Video/longcat_video/__init__.py" ]]; then touch "$TARGET_DIR/submodules/LongCat-Video/longcat_video/__init__.py"; print_status "Created: longcat_video/__init__.py"; fi
return 0
else
# If clone fails because directory exists, check if it's already a valid git repo
if [[ -d "LongCat-Video" && -d "LongCat-Video/.git" ]]; then
print_success "LongCat-Video submodule already exists and is valid"
return 0
else
print_error "Failed to clone LongCat-Video submodule"
return 1
fi
fi
;;
3)
print_status "LongCat-Video submodule has uncommitted changes, updating..."
if robust_submodule_operation 2; then
print_success "LongCat-Video submodule updated successfully"
return 0
else
print_error "Failed to update LongCat-Video submodule"
return 1
fi
;;
4)
print_status "LongCat-Video submodule has conflicts, reinitializing..."
if robust_submodule_operation 4; then
print_success "LongCat-Video submodule reinitialized successfully"
return 0
else
print_error "Failed to reinitialize LongCat-Video submodule"
return 1
fi
;;
*)
print_status "Unknown submodule status, attempting initialization..."
if robust_submodule_operation 3; then
print_success "LongCat-Video submodule handled successfully"
return 0
else
print_warning "Could not resolve LongCat-Video submodule status"
return 1
fi
;;
esac
}
# Enhanced function to initialize git submodules
initialize_submodules() {
if [[ "$DRY_RUN" == "true" ]]; then
print_status "[DRY RUN] Would check and handle LongCat-Video submodule"
return
fi
# Auto-initialize without prompts when running non-interactively
auto_initialize_submodule
}
# Function to apply patch
apply_patch() {
print_status "Applying patch from $SOURCE_DIR to $TARGET_DIR"
local copied_count=0
for file in "${FILES_TO_PATCH[@]}"; do
source_file="$SOURCE_DIR/$file"
target_file="$TARGET_DIR/$file"
if [[ -f "$source_file" ]]; then
target_dir=$(dirname "$target_file")
mkdir -p "$target_dir"
if [[ "$DRY_RUN" == "true" ]]; then
print_status "[DRY RUN] Would copy: $source_file -> $target_file"
else
# Only copy if file is missing or content differs
if [[ ! -f "$target_file" ]] || ! cmp -s "$source_file" "$target_file"; then
cp "$source_file" "$target_file"
copied_count=$((copied_count+1))
fi
if [[ "$VERBOSE" == "true" ]]; then
if [[ -f "$target_file" ]] && cmp -s "$source_file" "$target_file"; then
print_status "Patched (no change): $file"
else
print_status "Patched: $file"
fi
fi
fi
else
print_warning "Source file not found: $source_file"
fi
done
# Initialize submodules after patching .gitmodules (for both dry run and actual run)
if [[ " ${FILES_TO_PATCH[*]} " =~ " .gitmodules " ]]; then
initialize_submodules
fi
if [[ "$DRY_RUN" != "true" ]]; then
if [[ $copied_count -gt 0 ]]; then
print_success "Patch applied successfully ($copied_count file(s) updated)"
else
print_status "No files copied (already up to date or not found)"
fi
fi
}
# Function to rollback changes
rollback_changes() {
local latest_backup=$(ls -1t "$TARGET_DIR"/backup_* 2>/dev/null | head -1)
if [[ -z "$latest_backup" ]]; then
print_error "No backup found for rollback"
exit 1
fi
print_status "Rolling back from $latest_backup"
for file in "${FILES_TO_PATCH[@]}"; do
backup_file="$latest_backup/$file"
target_file="$TARGET_DIR/$file"
if [[ -f "$backup_file" ]]; then
cp "$backup_file" "$target_file"
if [[ "$VERBOSE" == "true" ]]; then
print_status "Restored: $file"
fi
fi
done
print_success "Rollback completed"
}
# No interactive prompts or git operations in this script
# Main execution
main() {
# Default values
DRY_RUN="false"
ROLLBACK="false"
CREATE_BACKUP="true"
VERBOSE="false"
# Additional option: --all to copy entire patch directory recursively
COPY_ALL="false"
# Parse command line arguments
while [[ $# -gt 0 ]]; do
case $1 in
-h|--help)
show_help
exit 0
;;
-d|--dry-run)
DRY_RUN="true"
shift
;;
-r|--rollback)
ROLLBACK="true"
shift
;;
-v|--verbose)
VERBOSE="true"
shift
;;
--all)
COPY_ALL="true"
shift
;;
*)
print_error "Unknown option: $1"
show_help
exit 1
;;
esac
done
print_status "Longcat Patch Application Script"
echo "====================================="
# Check directories
check_directories
# Handle rollback
if [[ "$ROLLBACK" == "true" ]]; then
rollback_changes
exit 0
fi
# No interactive prompts - auto initialize submodule if needed
CREATE_BACKUP="false"
# Create backup if needed
if [[ "$CREATE_BACKUP" == "true" && "$DRY_RUN" != "true" ]]; then
create_backup
fi
# Apply patch
if [[ "$COPY_ALL" == "true" ]]; then
print_status "Copying entire patch directory (recursive)"
if [[ "$DRY_RUN" == "true" ]]; then
print_status "[DRY RUN] Would sync: $SOURCE_DIR -> $TARGET_DIR"
else
# Use rsync if available for better behavior; fallback to cp -r
if command -v rsync >/dev/null 2>&1; then
rsync -av --delete --ignore-errors "$SOURCE_DIR/" "$TARGET_DIR/"
else
cp -rT "$SOURCE_DIR" "$TARGET_DIR"
fi
print_success "Directory sync completed"
fi
else
apply_patch
fi
# Finish up
if [[ "$DRY_RUN" != "true" ]]; then
print_success "Patch application completed!"
if [[ "$CREATE_BACKUP" == "true" ]]; then
print_status "Backup saved at: $BACKUP_DIR"
print_status "Use '$0 --rollback' to restore if needed"
fi
else
print_status "Dry run completed. No changes were made."
fi
}
# Run main function with all arguments
main "$@"