From c88dc471fae44101a7029341a1c7311e105f8a72 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sun, 30 May 2021 23:34:06 +0200 Subject: Set repository mtime to GitHub `updated_at` time CGit reads the repository modification time from the following locations, in order from top to bottom: 1. agefile 2. repo.git/refs/heads/{default_branch | "master"} 3. repo.git/packed-refs (https://git.zx2c4.com/cgit/tree/ui-repolist.c?id=bd6f5683f6cde4212364354b3139c1d521f40f39#n35) Update the `/refs/heads/{default_branch}` file mtime when cloning and updating the repo to match the GitHub `updated_at` time. This ensures that when mirroring old repositories, they don't appear at the top of the CGit repository index list when sorting by age. --- src/main.rs | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 9689d3c..bae2da8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,6 @@ use anyhow::{self, Context}; +use chrono::DateTime; +use filetime; use sqlx; use tokio; @@ -10,9 +12,9 @@ use std::path::{Path, PathBuf}; #[tokio::main] async fn main() { - let repos = github::fetch_repos("teddywing").await.unwrap(); - - dbg!(&repos); + // let repos = github::fetch_repos("teddywing").await.unwrap(); + // + // dbg!(&repos); // git::mirror( // "https://github.com/teddywing/google-calendar-rsvp.git", @@ -22,6 +24,8 @@ async fn main() { // git::update( // Path::new("/tmp/grsvp"), // ).unwrap(); + + run().await.unwrap(); } async fn run() -> anyhow::Result<()> { @@ -129,6 +133,8 @@ fn mirror>( ))?; } + update_mtime(&clone_path, &repo)?; + Ok(()) } @@ -145,5 +151,29 @@ fn update>( git::update_description(&repo_path, remote_description)?; } + update_mtime(&repo_path, &updated_repo)?; + + Ok(()) +} + +fn update_mtime>( + repo_path: P, + repo: &github::Repo, +) -> anyhow::Result<()> { + let default_branch_ref = repo_path + .as_ref() + .join("refs/heads") + .join(&repo.default_branch); + + let update_time = filetime::FileTime::from_system_time( + DateTime::parse_from_rfc3339(&repo.updated_at)?.into() + ); + + filetime::set_file_times(&default_branch_ref, update_time, update_time) + .with_context(|| format!( + "unable to set mtime on '{}'", + &default_branch_ref.display(), + ))?; + Ok(()) } -- cgit v1.2.3