update-gitea-ctimes/update-gitea-ctimes

63 lines
2.6 KiB
PHP
Executable File

#!/usr/bin/php
<?php
function update_gitea_ctimes(string $gitea_db_path, string $gitea_repos_dir) {
$x = new PDO("sqlite:" . $gitea_db_path);
$repos = $x->query("SELECT
repository.id, user.name owner, repository.name repo, repository.created_unix, repository.updated_unix
FROM
repository
JOIN user on repository.owner_id = user.id")->fetchAll(PDO::FETCH_ASSOC);
foreach($repos as $repo) {
echo "-- Checking repository {$repo['owner']}/{$repo['repo']}\n";
$git_dir = $gitea_repos_dir . $repo['owner'] . '/' . $repo['repo'] . ".git/";
if (! is_dir($git_dir)) {
echo "-- Skipping repository {$repo['owner']}/{$repo['repo']} with missing directory\n";
continue;
}
// Make sure there are any commits (i.e. not a wiki/issues-only repo)
$check = shell_exec("cd ".escapeshellarg($git_dir)." && git log -1 2>&1");
if (strpos($check, 'Date:') === false) {
echo "-- Skipping repository {$repo['owner']}/{$repo['repo']} with no commits\n";
continue; // skip
}
$newest_commit_time = intval(shell_exec("cd ".escapeshellarg($git_dir)." && git log -1 --format=format:%ct"));
$newest_author_time = intval(shell_exec("cd ".escapeshellarg($git_dir)." && git log -1 --format=format:%at"));
$newest_time = max($newest_commit_time, $newest_author_time, $repo['updated_unix']);
if ($repo['updated_unix'] != $newest_time) {
echo "-- Repository {$repo['owner']}/{$repo['repo']} was modified more recently\n";
echo "UPDATE repository SET updated_unix = " . $newest_time . " WHERE id = " . $repo['id'] . ";\n";
}
$oldest_commit_time = intval(shell_exec("cd ".escapeshellarg($git_dir)." && git log --reverse --format=format:%ct | head -n1"));
$oldest_author_time = intval(shell_exec("cd ".escapeshellarg($git_dir)." && git log --reverse --format=format:%at | head -n1"));
$oldest_time = min($oldest_commit_time, $oldest_author_time, $repo['created_unix']);
if ($repo['created_unix'] != $oldest_time) {
echo "-- Repository {$repo['owner']}/{$repo['repo']} was created at an earlier time\n";
echo "UPDATE repository SET created_unix = " . $oldest_time . " WHERE id = " . $repo['id'] . ";\n";
}
}
}
function main(array $argv) {
if (count($argv) != 3) {
die("Usage: update-gitea-ctimes gitea.db path/to/repositories\n");
}
$gitea_db_path = $argv[1];
$gitea_repos_dir = $argv[2];
update_gitea_ctimes($gitea_db_path, $gitea_repos_dir);
}
main($_SERVER['argv']);