# git_repo_web_sync Simple php script that after being requested syncs git repo with a catalog on the server ```php Access Denied"; echo "

Invalid or missing synchronization token.

"; exit; } echo "

Starting Synchronization...

"; echo "
"; // --- 2. FORCE CLEANUP STRATEGY (KEEP DIRECTORY, WIPE CONTENTS) --- if (is_dir($targetDir)) { echo "

Target directory exists. Cleaning contents (including hidden files)...

"; // HOW: // 1. cd into the directory // 2. rm -rf * .* (excluding . and .. which is x_x) // I use a subshell (cd ... && ...) to ensure we only delete inside targetDir // Note: I must be very careful with '.*'. I excluded '.' and '..' manually. // Command: find . -mindepth 1 -delete is safer and handles hidden files WHICH IS IMPORTANT. $cleanCmd = "cd " . escapeshellarg($targetDir) . " && find . -mindepth 1 -delete"; exec($cleanCmd, $cleanOut, $cleanStatus); if ($cleanStatus !== 0) { echo "

Error: Failed to clean directory contents.

"; echo "

Path: " . htmlspecialchars($targetDir) . "

"; echo "
" . htmlspecialchars(implode("\n", $cleanOut)) . "
"; exit; } echo "

Directory contents cleaned successfully.

"; } else { echo "

Target directory did not exist. Creating...

"; // Create the directory if it doesn't exist SHOULDN'T be needed but addedd just in case if (!mkdir($targetDir, 0755, true)) { echo "

Error: Failed to create directory.

"; exit; } } //safety check $parentDir = dirname($targetDir); if (!is_dir($parentDir)) { echo "

Error: Parent directory ($parentDir) does not exist.

"; exit; } /* //dummy if (!is_writable($parentDir)) { echo "

Error: Web server cannot write to parent directory ($parentDir).

"; echo "

Run: sudo chown -R www-data:www-data $parentDir

"; exit; } */ // --- 3. GIT CLONE --- echo "

Cloning $repoUrl (branch: $branch) into $targetDir...

"; $cloneCmd = "git clone --depth 1 --branch " . escapeshellarg($branch) . " " . escapeshellarg($repoUrl) . " " . escapeshellarg($targetDir) . " 2>&1"; exec($cloneCmd, $output, $status); // --- 4. RESULT --- if ($status === 0) { echo "

SUCCESS: Repository synchronized successfully!

"; echo "

Files are now located at: $targetDir

"; } else { echo "

FAILED: Git clone failed.

"; echo "
"; echo "Click to see error details"; echo "
";
    echo htmlspecialchars(implode("\n", $output));
    echo "
"; echo "
"; } echo "
"; echo "

Synchronization finished.

"; ?> ```