feat(gitpull): execute git pull on page load and add retry button

- Changed git pull execution from POST-only to always run on page load
- Replaced sudo with su for better user switching in command execution
- Removed form submission in favor of direct execution with retry button
- Simplified UI by always showing result message and conditionally showing retry button
This commit is contained in:
2026-03-13 15:36:13 +08:00
parent 73049b71b6
commit 66bf57911b

View File

@@ -21,13 +21,12 @@ $src_path = '/home/judge/src';
$output = []; $output = [];
$return_code = -1; $return_code = -1;
if ($_SERVER['REQUEST_METHOD'] === 'POST') { // 打开页面或点击重试按钮时均执行更新
$cmd = "echo " . escapeshellarg($SUDO_PASSWORD) $cmd = "echo " . escapeshellarg($SUDO_PASSWORD)
. " | sudo -S -u " . escapeshellarg($SUDO_USER) . " | su -c " . escapeshellarg("git -C " . $src_path . " pull")
. " git -C " . escapeshellarg($src_path) . " " . escapeshellarg($SUDO_USER)
. " pull 2>&1"; . " 2>&1";
exec($cmd, $output, $return_code); exec($cmd, $output, $return_code);
}
$show_title = "Git Pull - " . $OJ_NAME; $show_title = "Git Pull - " . $OJ_NAME;
require("template/".$OJ_TEMPLATE."/header.php"); require("template/".$OJ_TEMPLATE."/header.php");
@@ -39,19 +38,19 @@ require("template/".$OJ_TEMPLATE."/header.php");
<div class="content">服务器更新 <small style="font-size:0.6em; color:#888;">(git pull <?php echo htmlspecialchars($src_path); ?>)</small></div> <div class="content">服务器更新 <small style="font-size:0.6em; color:#888;">(git pull <?php echo htmlspecialchars($src_path); ?>)</small></div>
</h2> </h2>
<?php if ($_SERVER['REQUEST_METHOD'] === 'POST'): ?> <div class="ui <?php echo $return_code === 0 ? 'success' : 'error'; ?> message">
<div class="ui <?php echo $return_code === 0 ? 'success' : 'error'; ?> message"> <div class="header"><?php echo $return_code === 0 ? '更新成功' : '更新失败(返回码:' . $return_code . ''; ?></div>
<div class="header"><?php echo $return_code === 0 ? '更新成功' : '更新失败(返回码:' . $return_code . ''; ?></div> <pre style="margin-top:8px; white-space:pre-wrap; word-break:break-all;"><?php echo htmlspecialchars(implode("\n", $output)); ?></pre>
<pre style="margin-top:8px; white-space:pre-wrap; word-break:break-all;"><?php echo htmlspecialchars(implode("\n", $output)); ?></pre> </div>
</div>
<?php endif; ?>
<form method="post" action="gitpull.php" style="margin-top:16px;"> <div style="margin-top:16px;">
<button type="submit" class="ui primary button"> <?php if ($return_code !== 0): ?>
<i class="refresh icon"></i> 立即执行 git pull <a class="ui primary button" href="gitpull.php">
</button> <i class="refresh icon"></i> 重试 git pull
</a>
<?php endif; ?>
<a class="ui button" href="javascript:history.back()">返回</a> <a class="ui button" href="javascript:history.back()">返回</a>
</form> </div>
</div> </div>
<?php require("template/".$OJ_TEMPLATE."/footer.php"); ?> <?php require("template/".$OJ_TEMPLATE."/footer.php"); ?>