initial commit

This commit is contained in:
mappu 2020-05-02 17:25:05 +12:00
commit 38582f431e
4 changed files with 78 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
workdir

15
README.md Normal file
View File

@ -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.

Binary file not shown.

After

Width:  |  Height:  |  Size: 90 KiB

62
update-gitea-ctimes Executable file
View File

@ -0,0 +1,62 @@
#!/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']);