diff options
-rw-r--r-- | src/git.rs | 35 | ||||
-rw-r--r-- | src/github.rs | 9 | ||||
-rw-r--r-- | src/main.rs | 12 |
3 files changed, 45 insertions, 11 deletions
@@ -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<P: AsRef<Path>>( 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<P: AsRef<Path>>( Ok(()) } + +/// Update the repository's description file. +pub fn update_description<P: AsRef<Path>>( + 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<Vec<Repo>, 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<P: AsRef<Path>>( git::mirror( &repo.git_url, &clone_path, - repo.description.as_deref(), + repo.description(), )?; Ok(()) } -fn update<P: AsRef<Path>>(repo_path: P) -> anyhow::Result<()> { - git::update(repo_path)?; +fn update<P: AsRef<Path>>( + repo_path: P, + repo: &github::Repo, +) -> anyhow::Result<()> { + git::update(&repo_path)?; + git::update_description(&repo_path, repo.description())?; Ok(()) } |