From e9dc01ffb547d0fa605bfe38b34672ddd5161be4 Mon Sep 17 00:00:00 2001 From: Max Audron Date: Tue, 7 Jun 2022 12:28:18 +0200 Subject: reorganize file structure and cleanup lints --- src/update/mod.rs | 114 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 112 insertions(+), 2 deletions(-) (limited to 'src/update/mod.rs') diff --git a/src/update/mod.rs b/src/update/mod.rs index 0991b1a..dfc800c 100644 --- a/src/update/mod.rs +++ b/src/update/mod.rs @@ -1,9 +1,14 @@ -use gtree::local::Repos; +use std::fmt::{Debug, Display}; + +use git2::BranchType; +use tracing::debug; + +use crate::repo::{Repo, RepoError, Repos}; impl crate::GTree { pub async fn update(&self, repos: Repos) { for mut repo in repos { - if let Some(_) = repo.repo { + if repo.repo.is_some() { match repo.update() { Ok(u) => println!("{}", u), Err(u) => println!("{}", u), @@ -12,3 +17,108 @@ impl crate::GTree { } } } + +impl Repo { + /// Fetch any new state from the remote and fast forward merge changes into local branches + #[tracing::instrument(level = "trace")] + pub fn update(&mut self) -> Result { + let repo_name = self.name.clone(); + if self.repo.is_some() { + self.update_inner() + .map_err(|e| UpdateResult::err(repo_name, e)) + } else { + Ok(UpdateResult::err(repo_name, RepoError::NoLocalRepo)) + } + } + + fn update_inner(&mut self) -> Result { + let repo = self.repo.as_ref().unwrap(); + let mut remote = self.main_remote(repo)?; + + self.fetch(&mut remote)?; + + self.default_branch = remote.default_branch()?.as_str().unwrap().to_string(); + + debug!("default branch: {}", self.default_branch); + + if self.is_clean()? { + debug!("repo is clean"); + + let merged = repo.branches(Some(BranchType::Local))? + .filter_map(|x| x.ok()) + .try_fold(false, |mut merged, (mut branch, _)| { + let name = format!("refs/heads/{}", Repo::branch_name(&branch)); + + if branch.upstream().is_ok() { + let upstream = branch.upstream().unwrap(); + + debug!("branch: {}", name); + + merged |= self.merge(repo, &mut branch, &upstream)?; + Ok::(merged) + } else { + debug!("not updating branch: {}: branch does not have upstream tracking branch set", name); + Ok(merged) + } + })?; + + if merged { + Ok(UpdateResult::merged(self.name.clone())) + } else { + Ok(UpdateResult::no_changes(self.name.clone())) + } + } else { + Ok(UpdateResult::dirty(self.name.clone())) + } + } +} + +#[derive(Debug)] +pub enum UpdateResult { + NoChanges { name: String }, + Dirty { name: String }, + Merged { name: String }, + Error { name: String, error: RepoError }, +} + +impl UpdateResult { + pub fn err(name: String, error: RepoError) -> UpdateResult { + UpdateResult::Error { name, error } + } + + pub fn merged(name: String) -> UpdateResult { + UpdateResult::Merged { name } + } + + pub fn dirty(name: String) -> UpdateResult { + UpdateResult::Dirty { name } + } + + pub fn no_changes(name: String) -> UpdateResult { + UpdateResult::NoChanges { name } + } +} + +impl Display for UpdateResult { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + use ansi_term::Colour::{Blue, Green, Red, Yellow}; + + match self { + UpdateResult::NoChanges { name } => { + f.write_fmt(format_args!("{} {}", Blue.paint("FETCHED"), name)) + } + UpdateResult::Dirty { name } => { + f.write_fmt(format_args!("{} {}", Yellow.paint("DIRTY "), name)) + } + UpdateResult::Merged { name } => { + f.write_fmt(format_args!("{} {}", Green.paint("PULLED "), name)) + } + UpdateResult::Error { name, error } => f.write_fmt(format_args!( + "{} {} [{}]", + Red.paint("ERROR "), + name, + error + )), + } + } +} -- cgit v1.2.3 e='2025-12-14 02:30:05 +0100'>2025-12-14update debug config filesMax Audron-7/+6 2025-12-14release 1.7.11.7.1Max Audron-2/+2 2025-12-14fix non-sasl connection registrationMax Audron-9/+35 2025-12-14fix readme badge linksMax Audron-3/+3 2025-12-14update readme and remotesMax Audron-1/+14 2025-12-14release 1.7.01.7.0Max Audron-2/+2 2025-12-14add server passwordMax Audron-1/+6 2025-12-14update cargo dependenciesMax Audron-670/+505 2025-12-14update flake inputs to nixpkgs 25.11Max Audron-36/+41 2025-05-06fix rustls missing cryptoproviderMax Audron-1/+6 2025-05-06remove too commonly used used for shifty_eyesMax Audron-1/+1 2025-05-06fix truncate on unicodeMax Audron-10/+20 2025-05-06update dependenciesMax Audron-691/+1659 2024-08-12add nix build and moduleMax Audron-5/+427 2022-02-19remove jsonnet lock fileMax Audron-36/+0 2022-02-19fix deploy to work with gitlab agentMax Audron-4/+4 2021-10-22write tons of documentation and reorganize some modulesMax Audron-65/+300 2021-10-22remove wolfram alpha url shorteningMax Audron-1/+2 2021-10-20remove failing wolfram alpha test casesMax Audron-105/+55 2021-10-20bump version to 1.6.2Max Audron-3/+2 2021-10-20prepare for release on crates.ioMax Audron-39/+65 2021-10-20add async docs to macro crate and bump versionMax Audron-9/+10 2021-10-20change hook errors to be logged as warningsMax Audron-3/+3 2021-10-20fix configuration not loading correctly on release buildsMax Audron-8/+23 2021-10-19replace sedregex crate8-rework-sedMax Audron-20/+358 2021-10-19add formatting trait for irc codesMax Audron-0/+129 2021-10-17fix links in readmeMax Audron-2/+2