diff options
author | Teddy Wing | 2022-06-04 19:15:35 +0200 |
---|---|---|
committer | Teddy Wing | 2022-06-04 19:15:35 +0200 |
commit | 57cd001192adc095843af0c18044ff996fca0480 (patch) | |
tree | 0bc91c1c3b41abe4cfccf039d6f5c64bb55b5ae1 | |
parent | 627d6c7e2b0e218213fa2a9c19036489c66c3141 (diff) | |
parent | 6fe38d83ec90f5adb411706ed23d9a8b2929aed8 (diff) | |
download | reflectub-57cd001192adc095843af0c18044ff996fca0480.tar.bz2 |
Merge branch 'make-errors-more-traceable-2'
-rw-r--r-- | Cargo.lock | 12 | ||||
-rw-r--r-- | Cargo.toml | 2 | ||||
-rw-r--r-- | README.md | 4 | ||||
-rw-r--r-- | src/database.rs | 24 | ||||
-rw-r--r-- | src/git.rs | 124 | ||||
-rw-r--r-- | src/github.rs | 4 | ||||
-rw-r--r-- | src/main.rs | 4 |
7 files changed, 144 insertions, 30 deletions
@@ -1,5 +1,7 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. +version = 3 + [[package]] name = "ahash" version = "0.7.4" @@ -544,7 +546,7 @@ dependencies = [ [[package]] name = "reflectub" -version = "0.0.1" +version = "0.0.2" dependencies = [ "anyhow", "chrono", @@ -692,18 +694,18 @@ dependencies = [ [[package]] name = "thiserror" -version = "1.0.25" +version = "1.0.31" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fa6f76457f59514c7eeb4e59d891395fab0b2fd1d40723ae737d64153392e9c6" +checksum = "bd829fe32373d27f76265620b5309d0340cb8550f523c1dda251d6298069069a" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.25" +version = "1.0.31" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8a36768c0fbf1bb15eca10defa29526bda730a2376c2ab4393ccfa16fb1a318d" +checksum = "0396bc89e626244658bef819e22d0cc459e795a5ebe878e6ec336d1674a8d79a" dependencies = [ "proc-macro2", "quote", @@ -16,5 +16,5 @@ r2d2_sqlite = "0.18.0" rayon = "1.5.1" rusqlite = "0.25.3" serde = { version = "1.0.126", features = ["derive"] } -thiserror = "1.0.25" +thiserror = "1.0.31" ureq = { version = "2.1.1", features = ["json"] } @@ -17,8 +17,8 @@ web frontend [CGit]. ## License -Copyright © 2021 Teddy Wing. Licensed under the GNU GPLv3+ (see the included -COPYING file). +Copyright © 2021, 2022 Teddy Wing. Licensed under the GNU GPLv3+ (see the +included COPYING file). [CGit]: https://git.zx2c4.com/cgit/about/ diff --git a/src/database.rs b/src/database.rs index ed5d327..09fedd7 100644 --- a/src/database.rs +++ b/src/database.rs @@ -1,4 +1,4 @@ -// Copyright (c) 2021 Teddy Wing +// Copyright (c) 2021, 2022 Teddy Wing // // This file is part of Reflectub. // @@ -44,12 +44,32 @@ impl Repo { impl From<&github::Repo> for Repo { fn from(repo: &github::Repo) -> Self { + use chrono::DateTime; + + let repo_updated_at = DateTime::parse_from_rfc3339(&repo.updated_at).ok(); + let repo_pushed_at = DateTime::parse_from_rfc3339(&repo.pushed_at).ok(); + + // Set `updated_at` to the most recent of `repo_updated_at` or + // `repo_pushed_at`. + let updated_at = + if repo_updated_at.is_none() && repo_pushed_at.is_none() { + repo.updated_at.clone() + + // `repo_updated_at` and `repo_pushed_at` are both Some. + } else if repo_pushed_at.unwrap() > repo_updated_at.unwrap() { + repo.pushed_at.clone() + + // Default to `repo.updated_at`. + } else { + repo.updated_at.clone() + }; + Self { id: repo.id, name: Some(repo.name.clone()), description: repo.description.clone(), default_branch: Some(repo.default_branch.clone()), - updated_at: Some(repo.updated_at.clone()), + updated_at: Some(updated_at), } } } @@ -1,4 +1,4 @@ -// Copyright (c) 2021 Teddy Wing +// Copyright (c) 2021, 2022 Teddy Wing // // This file is part of Reflectub. // @@ -25,6 +25,58 @@ use std::path::Path; #[derive(Debug, thiserror::Error)] pub enum Error { + #[error("mirror: cannot create repo '{path}'")] + MirrorCreateRepo { + source: git2::Error, + path: String, + }, + #[error("mirror: cannot add remote '{remote_name}:{url}'")] + MirrorAddRemote { + source: git2::Error, + remote_name: String, + url: String, + }, + #[error("mirror: cannot get repo config")] + MirrorConfigGet(#[source] git2::Error), + #[error("mirror: cannot set 'mirror' flag on remote '{remote_name}'")] + MirrorRemoteEnableMirror { + source: git2::Error, + remote_name: String, + }, + #[error("mirror: cannot fetch from remote '{remote_name}'")] + MirrorFetch { + source: git2::Error, + remote_name: String, + }, + + #[error("update: cannot open repo '{path}'")] + UpdateOpenRepo { + source: git2::Error, + path: String, + }, + #[error("update: cannot get remotes for '{path}'")] + UpdateGetRemotes { + source: git2::Error, + path: String, + }, + #[error("update: cannot find remote '{remote_name}'")] + UpdateFindRemote { + source: git2::Error, + remote_name: String, + }, + #[error("update: cannot fetch from remote '{remote_name}")] + UpdateFetch { + source: git2::Error, + remote_name: String, + }, + + #[error("{action}: cannot switch to branch '{branch}'")] + GitChangeBranch { + source: git2::Error, + action: String, + branch: String, + }, + #[error("git error")] Git(#[from] git2::Error), @@ -40,7 +92,7 @@ pub enum Error { /// ```shell /// git clone --mirror URL /// ``` -pub fn mirror<P: AsRef<Path>>( +pub fn mirror<P: AsRef<Path> + Copy>( url: &str, path: P, description: &str, @@ -56,7 +108,11 @@ pub fn mirror<P: AsRef<Path>>( // Mac OS. .external_template(false) .description(description), - )?; + ) + .map_err(|e| Error::MirrorCreateRepo { + source: e, + path: format!("{}", path.as_ref().display()), + })?; let remote_name = "origin"; @@ -64,19 +120,38 @@ pub fn mirror<P: AsRef<Path>>( remote_name, url, "+refs/*:refs/*", - )?; - - let mut config = repo.config()?; + ) + .map_err(|e| Error::MirrorAddRemote { + source: e, + remote_name: remote_name.to_owned(), + url: url.to_owned(), + })?; + + let mut config = repo.config() + .map_err(|e| Error::MirrorConfigGet(e))?; config.set_bool( &format!("remote.{}.mirror", remote_name), true, - )?; + ) + .map_err(|e| Error::MirrorRemoteEnableMirror { + source: e, + remote_name: remote_name.to_owned(), + })?; let refspecs: [&str; 0] = []; - remote.fetch(&refspecs, None, None)?; + remote.fetch(&refspecs, None, None) + .map_err(|e| Error::MirrorFetch { + source: e, + remote_name: remote_name.to_owned(), + })?; if default_branch != "master" { - repo_change_current_branch(&repo, default_branch)?; + repo_change_current_branch(&repo, default_branch) + .map_err(|e| Error::GitChangeBranch { + source: e, + action: "mirror".to_owned(), + branch: default_branch.to_owned(), + })?; } Ok(()) @@ -89,14 +164,27 @@ pub fn mirror<P: AsRef<Path>>( /// ```shell /// git remote update /// ``` -pub fn update<P: AsRef<Path>>( +pub fn update<P: AsRef<Path> + Copy>( path: P, ) -> Result<(), Error> { - let repo = git2::Repository::open_bare(path)?; - - for remote_opt in &repo.remotes()? { + let repo = git2::Repository::open_bare(path) + .map_err(|e| Error::UpdateOpenRepo { + source: e, + path: format!("{}", path.as_ref().display()), + })?; + + let remotes = &repo.remotes() + .map_err(|e| Error::UpdateGetRemotes { + source: e, + path: format!("{}", path.as_ref().display()), + })?; + for remote_opt in remotes { if let Some(remote_name) = remote_opt { - let mut remote = repo.find_remote(remote_name)?; + let mut remote = repo.find_remote(remote_name) + .map_err(|e| Error::UpdateFindRemote { + source: e, + remote_name: remote_name.to_owned(), + })?; let mut fetch_options = git2::FetchOptions::new(); fetch_options @@ -104,7 +192,11 @@ pub fn update<P: AsRef<Path>>( .download_tags(git2::AutotagOption::All); let refspecs: [&str; 0] = []; - remote.fetch(&refspecs, Some(&mut fetch_options), None)?; + remote.fetch(&refspecs, Some(&mut fetch_options), None) + .map_err(|e| Error::UpdateFetch { + source: e, + remote_name: remote_name.to_owned(), + })?; } } @@ -149,7 +241,7 @@ pub fn change_current_branch<P: AsRef<Path>>( fn repo_change_current_branch( repo: &git2::Repository, default_branch: &str, -) -> Result<(), Error> { +) -> Result<(), git2::Error> { Ok( repo.set_head( &format!("refs/heads/{}", default_branch), diff --git a/src/github.rs b/src/github.rs index a3bb74e..c48dda6 100644 --- a/src/github.rs +++ b/src/github.rs @@ -1,4 +1,4 @@ -// Copyright (c) 2021 Teddy Wing +// Copyright (c) 2021, 2022 Teddy Wing // // This file is part of Reflectub. // @@ -43,7 +43,7 @@ pub struct Repo { pub name: String, pub description: Option<String>, pub fork: bool, - pub git_url: String, + pub clone_url: String, pub default_branch: String, pub size: u64, pub updated_at: String, diff --git a/src/main.rs b/src/main.rs index 6ff441b..61996e8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,4 @@ -// Copyright (c) 2021 Teddy Wing +// Copyright (c) 2021, 2022 Teddy Wing // // This file is part of Reflectub. // @@ -241,7 +241,7 @@ where P2: AsRef<Path>, { git::mirror( - &repo.git_url, + &repo.clone_url, &clone_path, repo.description(), &repo.default_branch, |