commit 38582f431eaf5aec0823100dd0cc65e91c3b3844 Author: mappu Date: Sat May 2 17:25:05 2020 +1200 initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6aa2721 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +workdir diff --git a/README.md b/README.md new file mode 100644 index 0000000..b0c11a4 --- /dev/null +++ b/README.md @@ -0,0 +1,15 @@ +# update-gitea-ctimes + +A script to modify creation time flags in Gitea to match the oldest Git commit. + +This is helpful for ordering repositories chronologically by creation-time after a migration to Gitea (e.g. for [teafolio](https://code.ivysaur.me/teafolio)). + +Written in PHP + +## Usage + +``` +Usage: update-gitea-ctimes gitea.db path/to/repositories +``` + +Running the command will produce SQL on stdout that can be manually inspected before applying it to the live SQLite database. diff --git a/doc/Screenshot_20200502_172303.png b/doc/Screenshot_20200502_172303.png new file mode 100644 index 0000000..f1ef392 Binary files /dev/null and b/doc/Screenshot_20200502_172303.png differ diff --git a/update-gitea-ctimes b/update-gitea-ctimes new file mode 100755 index 0000000..2b0dce1 --- /dev/null +++ b/update-gitea-ctimes @@ -0,0 +1,62 @@ +#!/usr/bin/php +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']);