aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.rs
diff options
context:
space:
mode:
authorTeddy Wing2021-06-23 23:45:47 +0200
committerTeddy Wing2021-06-23 23:45:47 +0200
commit5de391101d72f0d2239a69073b08861641d9c878 (patch)
tree7691c8ae347b10ad308a41c8e4b1cee237d67330 /src/main.rs
parent0e2faed4ac9b5950eb786a7127f9fc02df050c25 (diff)
downloadreflectub-5de391101d72f0d2239a69073b08861641d9c878.tar.bz2
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.
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs107
1 files changed, 56 insertions, 51 deletions
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<P: AsRef<Path>>(
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<P: AsRef<Path>>(
Ok(())
}
+
+/// Write `update_time` into the repo's `info/web/last-modified` file.
+fn set_agefile_time<P: AsRef<Path>>(
+ 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(())
+}