From 5de391101d72f0d2239a69073b08861641d9c878 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Wed, 23 Jun 2021 23:45:47 +0200 Subject: update_mtime(): Extract agefile handling to a separate function The `update_mtime()` function is getting pretty long. Extract this into a new function since it's more of a self-contained unit. --- src/main.rs | 107 +++++++++++++++++++++++++++++++----------------------------- 1 file changed, 56 insertions(+), 51 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index be06c28..a888883 100644 --- a/src/main.rs +++ b/src/main.rs @@ -349,57 +349,7 @@ fn update_mtime>( Err(e) if e.kind() == io::ErrorKind::NotFound => { // In the absence of a 'packed-refs' file, create a CGit // agefile and add the update time to it. - let agefile_dir = repo_path.as_ref().join("info/web"); - - fs::DirBuilder::new() - .create(&agefile_dir) - .with_context(|| format!( - "unable to create directory '{}'", - &agefile_dir.display(), - ))?; - - let agefile_path = agefile_dir.join("last-modified"); - - let mut agefile = fs::OpenOptions::new() - .write(true) - .truncate(true) - .create(true) - .open(&agefile_path) - .with_context(|| format!( - "unable to open '{}'", - &agefile_path.display(), - ))?; - - writeln!(agefile, "{}", &repo.updated_at) - .with_context(|| format!( - "unable to write to '{}'", - &agefile_path.display(), - ))?; - - let cgitrc_path = repo_path - .as_ref() - .join("cgitrc"); - - let mut cgitrc_file = fs::OpenOptions::new() - .append(true) - .create(true) - .open(&cgitrc_path) - .with_context(|| format!( - "unable to open '{}'", - &cgitrc_path.display(), - ))?; - - writeln!( - cgitrc_file, - "{}", - "agefile=info/web/last-modified", - ) - .with_context(|| format!( - "unable to write to '{}'", - &cgitrc_path.display(), - ))?; - - Ok(()) + Ok(set_agefile_time(&repo_path, &repo.updated_at)?) }, Err(e) => Err(e), } @@ -419,3 +369,58 @@ fn update_mtime>( Ok(()) } + +/// Write `update_time` into the repo's `info/web/last-modified` file. +fn set_agefile_time>( + repo_path: P, + update_time: &str, +) -> anyhow::Result<()> { + let agefile_dir = repo_path.as_ref().join("info/web"); + fs::DirBuilder::new() + .create(&agefile_dir) + .with_context(|| format!( + "unable to create directory '{}'", + &agefile_dir.display(), + ))?; + + let agefile_path = agefile_dir.join("last-modified"); + let mut agefile = fs::OpenOptions::new() + .write(true) + .truncate(true) + .create(true) + .open(&agefile_path) + .with_context(|| format!( + "unable to open '{}'", + &agefile_path.display(), + ))?; + + writeln!(agefile, "{}", &update_time) + .with_context(|| format!( + "unable to write to '{}'", + &agefile_path.display(), + ))?; + + let cgitrc_path = repo_path + .as_ref() + .join("cgitrc"); + let mut cgitrc_file = fs::OpenOptions::new() + .append(true) + .create(true) + .open(&cgitrc_path) + .with_context(|| format!( + "unable to open '{}'", + &cgitrc_path.display(), + ))?; + + writeln!( + cgitrc_file, + "{}", + "agefile=info/web/last-modified", + ) + .with_context(|| format!( + "unable to write to '{}'", + &cgitrc_path.display(), + ))?; + + Ok(()) +} -- cgit v1.2.3