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 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) (limited to 'src/git.rs') 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(()) +} -- cgit v1.2.3