浏览代码

Update 'README.md'

computer_glamour 1 天之前
父节点
当前提交
415def7abc
共有 1 个文件被更改,包括 94 次插入1 次删除
  1. 94 1
      README.md

+ 94 - 1
README.md

@@ -1,3 +1,96 @@
 # git_repo_web_sync
 
-Simple php script that after being requested syncs git repo with a catalog on the server
+Simple php script that after being requested syncs git repo with a catalog on the server
+
+```php
+<?php
+// --- CONFIGURATION ---
+$allowedToken = "PUT YOUR TOKEN HERE";
+$repoUrl = "PUT GIT REPO URL HERE";
+$targetDir = "/path/to/your/www/root";
+$branch = "NAME OF THE BRANCH"; 
+
+// --- 1. AUTHENTICATION ---
+$token = $_GET['s'] ?? null;
+
+if ($token !== $allowedToken) {
+    http_response_code(403);
+    echo "<h1 style='color:red;'>Access Denied</h1>";
+    echo "<p>Invalid or missing synchronization token.</p>";
+    exit;
+}
+
+echo "<h2>Starting Synchronization...</h2>";
+echo "<hr>";
+
+// --- 2. FORCE CLEANUP STRATEGY (KEEP DIRECTORY, WIPE CONTENTS) ---
+if (is_dir($targetDir)) {
+    echo "<p>Target directory exists. Cleaning contents (including hidden files)...</p>";
+    
+    // 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 "<p style='color:red;'>Error: Failed to clean directory contents.</p>";
+        echo "<p>Path: " . htmlspecialchars($targetDir) . "</p>";
+        echo "<pre>" . htmlspecialchars(implode("\n", $cleanOut)) . "</pre>";
+        exit;
+    }
+    echo "<p>Directory contents cleaned successfully.</p>";
+} else {
+    echo "<p>Target directory did not exist. Creating...</p>";
+    // Create the directory if it doesn't exist SHOULDN'T be needed but addedd just in case
+    if (!mkdir($targetDir, 0755, true)) {
+        echo "<p style='color:red;'>Error: Failed to create directory.</p>";
+        exit;
+    }
+}
+
+//safety check
+$parentDir = dirname($targetDir);
+if (!is_dir($parentDir)) {
+    echo "<p style='color:red;'>Error: Parent directory ($parentDir) does not exist.</p>";
+    exit;
+}
+/*
+//dummy
+if (!is_writable($parentDir)) {
+    echo "<p style='color:red;'>Error: Web server cannot write to parent directory ($parentDir).</p>";
+    echo "<p>Run: <code>sudo chown -R www-data:www-data $parentDir</code></p>";
+    exit;
+}
+*/
+// --- 3. GIT CLONE ---
+echo "<p>Cloning <code>$repoUrl</code> (branch: <code>$branch</code>) into <code>$targetDir</code>...</p>";
+
+$cloneCmd = "git clone --depth 1 --branch " . escapeshellarg($branch) . " " . escapeshellarg($repoUrl) . " " . escapeshellarg($targetDir) . " 2>&1";
+
+exec($cloneCmd, $output, $status);
+
+// --- 4. RESULT ---
+if ($status === 0) {
+    echo "<p style='color:green; font-weight:bold;'>SUCCESS: Repository synchronized successfully!</p>";
+    echo "<p>Files are now located at: <code>$targetDir</code></p>";
+} else {
+    echo "<p style='color:red; font-weight:bold;'>FAILED: Git clone failed.</p>";
+    echo "<details>";
+    echo "<summary>Click to see error details</summary>";
+    echo "<pre style='background:#f44; color:white; padding:10px;'>";
+    echo htmlspecialchars(implode("\n", $output));
+    echo "</pre>";
+    echo "</details>";
+}
+
+echo "<hr>";
+echo "<p><small>Synchronization finished.</small></p>";
+?>
+```