Fix version.php to read from .version file

This commit is contained in:
2026-01-01 19:09:06 +11:00
parent d9faee1114
commit c9677d5e3e
2 changed files with 51 additions and 42 deletions

View File

@@ -37,7 +37,13 @@ if ($action === 'update') {
"cd $installDir && git reset --hard origin/main", "cd $installDir && git reset --hard origin/main",
"cd $installDir && git pull origin main", "cd $installDir && git pull origin main",
"cp -r $installDir/webui/* $webDir/", "cp -r $installDir/webui/* $webDir/",
"chown -R www-data:www-data $webDir" "chown -R www-data:www-data $webDir",
// Create .version file with git info
"cd $installDir && git rev-parse --short HEAD > $webDir/.version",
"cd $installDir && git rev-parse HEAD >> $webDir/.version",
"cd $installDir && git rev-parse --abbrev-ref HEAD >> $webDir/.version",
"cd $installDir && git log -1 --format=%ci >> $webDir/.version",
"chown www-data:www-data $webDir/.version"
]; ];
$output = []; $output = [];

View File

@@ -2,54 +2,57 @@
/** /**
* UltyScan Web Interface - Git Version Info * UltyScan Web Interface - Git Version Info
* Returns current git commit ID and age * Reads version from .version file (created during deployment)
*/ */
header('Content-Type: application/json'); header('Content-Type: application/json');
$gitDir = '/usr/share/sniper'; $versionFile = __DIR__ . '/.version';
// Get commit hash // Default values
$commitHash = trim(shell_exec("cd $gitDir && git rev-parse --short HEAD 2>/dev/null")); $data = [
$fullHash = trim(shell_exec("cd $gitDir && git rev-parse HEAD 2>/dev/null")); 'commit' => '',
'fullCommit' => '',
'branch' => '',
'date' => '',
'age' => '',
'timestamp' => false
];
// Get commit date // Try reading from .version file first
$commitDate = trim(shell_exec("cd $gitDir && git log -1 --format=%ci 2>/dev/null")); if (file_exists($versionFile)) {
$commitTimestamp = strtotime($commitDate); $lines = file($versionFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
if (count($lines) >= 4) {
// Calculate age $data['commit'] = trim($lines[0]);
$age = ''; $data['fullCommit'] = trim($lines[1]);
if ($commitTimestamp) { $data['branch'] = trim($lines[2]);
$diff = time() - $commitTimestamp; $data['date'] = trim($lines[3]);
$data['timestamp'] = strtotime($data['date']);
if ($diff < 60) {
$age = $diff . ' seconds ago';
} elseif ($diff < 3600) {
$mins = floor($diff / 60);
$age = $mins . ' minute' . ($mins > 1 ? 's' : '') . ' ago';
} elseif ($diff < 86400) {
$hours = floor($diff / 3600);
$age = $hours . ' hour' . ($hours > 1 ? 's' : '') . ' ago';
} elseif ($diff < 604800) {
$days = floor($diff / 86400);
$age = $days . ' day' . ($days > 1 ? 's' : '') . ' ago';
} elseif ($diff < 2592000) {
$weeks = floor($diff / 604800);
$age = $weeks . ' week' . ($weeks > 1 ? 's' : '') . ' ago';
} else {
$months = floor($diff / 2592000);
$age = $months . ' month' . ($months > 1 ? 's' : '') . ' ago';
} }
} }
// Get branch // Calculate age if we have a timestamp
$branch = trim(shell_exec("cd $gitDir && git rev-parse --abbrev-ref HEAD 2>/dev/null")); if ($data['timestamp']) {
$diff = time() - $data['timestamp'];
echo json_encode([ if ($diff < 60) {
'commit' => $commitHash, $data['age'] = $diff . ' seconds ago';
'fullCommit' => $fullHash, } elseif ($diff < 3600) {
'branch' => $branch, $mins = floor($diff / 60);
'date' => $commitDate, $data['age'] = $mins . ' minute' . ($mins > 1 ? 's' : '') . ' ago';
'age' => $age, } elseif ($diff < 86400) {
'timestamp' => $commitTimestamp $hours = floor($diff / 3600);
]); $data['age'] = $hours . ' hour' . ($hours > 1 ? 's' : '') . ' ago';
} elseif ($diff < 604800) {
$days = floor($diff / 86400);
$data['age'] = $days . ' day' . ($days > 1 ? 's' : '') . ' ago';
} elseif ($diff < 2592000) {
$weeks = floor($diff / 604800);
$data['age'] = $weeks . ' week' . ($weeks > 1 ? 's' : '') . ' ago';
} else {
$months = floor($diff / 2592000);
$data['age'] = $months . ' month' . ($months > 1 ? 's' : '') . ' ago';
}
}
echo json_encode($data);