# Push to Production Fix

## Problem
The "Push to Production" script was timing out when executed from the web interface at `/admin/system-scripts`.

## Root Causes Identified

### 1. Missing Variable in deploy.php
**File**: `scripts/deploy.php`  
**Line**: 59  
**Issue**: Used undefined variable `$phpPath`
```php
// BEFORE (broken)
passthru($phpPath . " " . escapeshellarg($scriptPath) . " pull", $exitCode);

// AFTER (fixed)
$phpPath = 'php'; // Use system PHP
passthru($phpPath . " " . escapeshellarg($scriptPath) . " pull", $exitCode);
```

### 2. Slow Git Operations from Web Context
**Issue**: The original `git_push.php` script was designed for CLI use with verbose output and multiple operations that were slow when executed via web interface.

## Solutions Implemented

### 1. Fixed deploy.php
Added proper PHP path definition for production environment.

### 2. Enhanced git_push.php with Timeout Handling
- Added timeout parameter to `runGit()` function
- Set appropriate `set_time_limit()` for web execution
- Added specific timeouts for each operation:
  - Stage changes: 20 seconds
  - Commit: 20 seconds  
  - Push: 60 seconds

### 3. Created quick_push.php (NEW)
**Purpose**: Streamlined script optimized for fast web execution

**Features**:
- Minimal overhead - no version bumping, no colored output
- Direct execution with minimal checks
- 120-second timeout limit
- Optimized for speed over verbosity
- Only essential git operations

**Usage**:
```bash
php scripts/quick_push.php [optional_message]
```

### 4. Updated SystemScriptsController
- Added 120-second time limit for git operations
- Changed deploy function to use `quick_push.php` instead of `git_push.php`
- Better timeout handling throughout

## Files Modified

1. `scripts/deploy.php` - Fixed missing `$phpPath` variable
2. `scripts/git_push.php` - Added timeout handling for all git operations
3. `scripts/quick_push.php` - NEW streamlined script for web execution
4. `controllers/SystemScriptsController.php` - Updated to use quick_push.php

## Authentication Issue

**IMPORTANT**: The main cause of timeouts is Git authentication from web context.

When running git push from the web server, it cannot access your macOS Keychain interactively.

### Solutions:

#### Option A: Use SSH Instead of HTTPS (Recommended)
```bash
# 1. Switch remote to SSH
cd /Users/rpmbbu/LocalPHPStorm/m1_erp_web
git remote set-url origin git@github.com:merkuriddg/m1_erp.git

# 2. Verify SSH key is added to ssh-agent
ssh-add -l

# 3. Test connection
ssh -T git@github.com
```

#### Option B: Use Git Credential Cache
```bash
# Cache credentials for 1 hour
git config --global credential.helper 'cache --timeout=3600'

# Push once from terminal to cache credentials
git push
```

#### Option C: Use Personal Access Token
1. Create token at: https://github.com/settings/tokens
2. Store in git config:
```bash
git config credential.helper store
git push  # Enter token as password
```

## Testing

After fixing authentication:

1. Make a small change to any file
2. Navigate to: `http://localhost:8080/admin/system-scripts`
3. Click "Push to Production"
4. Enter a commit message (optional)
5. Click "Run Script"

**Expected Result**: Should complete in 10-30 seconds (depending on repository size)

## Fallback Options

If you still experience timeouts:

### Option 1: Use CLI Instead
```bash
cd /Users/rpmbbu/LocalPHPStorm/m1_erp_web
php scripts/quick_push.php "Your commit message"
```

### Option 2: Increase PHP Timeout
Edit `php.ini` and increase:
```ini
max_execution_time = 300
```

### Option 3: Direct Git Commands
```bash
cd /Users/rpmbbu/LocalPHPStorm/m1_erp_web
git add -A
git commit -m "Your message"
git push origin main
```

## Performance Comparison

| Script | Typical Execution Time | Use Case |
|--------|----------------------|----------|
| `git_push.php` | 20-45 seconds | CLI, detailed output |
| `quick_push.php` | 10-25 seconds | Web, fast execution |
| `deploy.php` | Depends on environment | Unified dev/prod script |

## Additional Optimizations

If you have a large repository and still experience slowness:

1. **Check for large files**:
   ```bash
   find . -type f -size +10M -not -path "./.git/*"
   ```

2. **Add to .gitignore**:
   - Large backup files
   - Temporary files
   - Upload directories with user content

3. **Clean git history** (careful!):
   ```bash
   git gc --aggressive --prune=now
   ```

## Notes

- The original `git_push.php` still exists for CLI use with verbose output
- Both scripts are functionally equivalent but optimized for different contexts
- `quick_push.php` skips version bumping for speed (can add back if needed)
- Timeout settings can be adjusted in the scripts if needed

## Date
2026-01-07
