diff options
author | Teddy Wing | 2021-05-30 23:34:06 +0200 |
---|---|---|
committer | Teddy Wing | 2021-05-30 23:34:06 +0200 |
commit | c88dc471fae44101a7029341a1c7311e105f8a72 (patch) | |
tree | ce37d91179a3770efe9df3d9c2ccb0afc6110b4b | |
parent | 94dc9d677ba0f25261f55b17f38b16a4b9d712af (diff) | |
download | reflectub-c88dc471fae44101a7029341a1c7311e105f8a72.tar.bz2 |
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.
-rw-r--r-- | Cargo.lock | 30 | ||||
-rw-r--r-- | Cargo.toml | 2 | ||||
-rw-r--r-- | src/main.rs | 36 |
3 files changed, 63 insertions, 5 deletions
@@ -128,6 +128,7 @@ dependencies = [ "libc", "num-integer", "num-traits", + "time", "winapi", ] @@ -227,6 +228,18 @@ dependencies = [ ] [[package]] +name = "filetime" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d34cfa13a63ae058bfa601fe9e313bbdb3746427c1459185464ce0fcf62e1e8" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "winapi", +] + +[[package]] name = "fnv" version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1010,6 +1023,8 @@ name = "reflectub" version = "0.0.1" dependencies = [ "anyhow", + "chrono", + "filetime", "git2", "reqwest", "serde", @@ -1412,6 +1427,17 @@ dependencies = [ ] [[package]] +name = "time" +version = "0.1.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6db9e6914ab8b1ae1c260a4ae7a49b6c5611b40328a735b21862567685e73255" +dependencies = [ + "libc", + "wasi", + "winapi", +] + +[[package]] name = "tinyvec" version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1615,9 +1641,9 @@ dependencies = [ [[package]] name = "wasi" -version = "0.10.2+wasi-snapshot-preview1" +version = "0.10.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" +checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f" [[package]] name = "wasm-bindgen" @@ -5,6 +5,8 @@ edition = "2018" [dependencies] anyhow = "1.0.40" +chrono = "0.4.19" +filetime = "0.2.14" git2 = "0.13.20" reqwest = { version = "0.11.3", features = ["json"] } serde = { version = "1.0.126", features = ["derive"] } 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<P: AsRef<Path>>( ))?; } + update_mtime(&clone_path, &repo)?; + Ok(()) } @@ -145,5 +151,29 @@ fn update<P: AsRef<Path>>( git::update_description(&repo_path, remote_description)?; } + update_mtime(&repo_path, &updated_repo)?; + + Ok(()) +} + +fn update_mtime<P: AsRef<Path>>( + 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(()) } |