From f8971cea4f8f916bbb528169f06c513c2a18802e Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sun, 30 May 2021 17:14:25 +0200 Subject: main::update(): Update repository description on fetch update If the repository was updated, write the description into the `description` file. Add a `github::Repo.description()` method to get an empty string if the description is `None`. This facilitates writing to the `description` file. --- src/git.rs | 35 ++++++++++++++++++++++++++++------- src/github.rs | 9 +++++++++ src/main.rs | 12 ++++++++---- 3 files changed, 45 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/git.rs b/src/git.rs index 479a3c5..a96adf5 100644 --- a/src/git.rs +++ b/src/git.rs @@ -1,5 +1,7 @@ use thiserror; +use std::fs; +use std::io::Write; use std::path::Path; @@ -7,6 +9,9 @@ use std::path::Path; pub enum Error { #[error("git error")] Git(#[from] git2::Error), + + #[error(transparent)] + Io(#[from] std::io::Error), } @@ -20,18 +25,13 @@ pub enum Error { pub fn mirror>( url: &str, path: P, - description: Option<&str>, + description: &str, ) -> Result<(), Error> { - let description_str = match description { - Some(d) => d, - None => "", - }; - let repo = git2::Repository::init_opts( path, &git2::RepositoryInitOptions::new() .bare(true) - .description(description_str), + .description(description), )?; let remote_name = "origin"; @@ -82,3 +82,24 @@ pub fn update>( Ok(()) } + +/// Update the repository's description file. +pub fn update_description>( + repo_path: P, + description: &str, +) -> Result<(), Error> { + let description_path = repo_path.as_ref().join("description"); + + let mut file = fs::OpenOptions::new() + .write(true) + .truncate(true) + .open(description_path)?; + + if description.is_empty() { + file.set_len(0)?; + } else { + writeln!(file, "{}", description)?; + } + + Ok(()) +} diff --git a/src/github.rs b/src/github.rs index 0b20abd..b937f9b 100644 --- a/src/github.rs +++ b/src/github.rs @@ -31,6 +31,15 @@ pub struct Repo { pub updated_at: String, // TODO: Maybe parse to date? } +impl Repo { + /// Get the repository description or an empty string if `None`. + pub fn description(&self) -> &str { + self.description + .as_deref() + .unwrap_or("") + } +} + pub fn fetch_repos() -> Result, Error> { let mut headers = reqwest::header::HeaderMap::new(); diff --git a/src/main.rs b/src/main.rs index 3f2c9a7..ec94601 100644 --- a/src/main.rs +++ b/src/main.rs @@ -64,7 +64,7 @@ async fn main() { match db.repo_get(id).await { Ok(_) => { if db.repo_is_updated(&db_repo).await.unwrap() { - update(&path).unwrap(); + update(&path, &repo).unwrap(); db.repo_update(&db_repo).await.unwrap(); } @@ -108,14 +108,18 @@ fn mirror>( git::mirror( &repo.git_url, &clone_path, - repo.description.as_deref(), + repo.description(), )?; Ok(()) } -fn update>(repo_path: P) -> anyhow::Result<()> { - git::update(repo_path)?; +fn update>( + repo_path: P, + repo: &github::Repo, +) -> anyhow::Result<()> { + git::update(&repo_path)?; + git::update_description(&repo_path, repo.description())?; Ok(()) } -- cgit v1.2.3