From c664c667261d46229830049979dde5ec19ed2e40 Mon Sep 17 00:00:00 2001 From: royalpinto007 Date: Fri, 26 Jan 2024 17:55:57 +0530 Subject: [PATCH] feat: switch_branch func --- coffee_github/src/repository.rs | 20 ++++++++++++++++++++ coffee_lib/src/repository.rs | 3 +++ 2 files changed, 23 insertions(+) diff --git a/coffee_github/src/repository.rs b/coffee_github/src/repository.rs index 97728f1f..3863c96e 100644 --- a/coffee_github/src/repository.rs +++ b/coffee_github/src/repository.rs @@ -310,6 +310,26 @@ impl Repository for Github { } } + async fn switch_branch(&self, branch_name: &str) -> Result<(), CoffeeError> { + let repo = git2::Repository::open(&self.url.path_string) + .map_err(|err| error!("{}", err.message()))?; + let mut remote = repo + .find_remote("origin") + .map_err(|err| error!("{}", err.message()))?; + remote + .fetch(&[&branch_name], None, None) + .map_err(|err| error!("{}", err.message()))?; + let oid = repo + .refname_to_id(&format!("refs/remotes/origin/{}", branch_name)) + .map_err(|err| error!("{}", err.message()))?; + let obj = repo + .find_object(oid, None) + .map_err(|err| error!("{}", err.message()))?; + repo.reset(&obj, git2::ResetType::Hard, None) + .map_err(|err| error!("{}", err.message()))?; + Ok(()) + } + /// list of the plugin installed inside the repository. async fn list(&self) -> Result, CoffeeError> { Ok(self.plugins.clone()) diff --git a/coffee_lib/src/repository.rs b/coffee_lib/src/repository.rs index b34a3ee0..5d2c011b 100644 --- a/coffee_lib/src/repository.rs +++ b/coffee_lib/src/repository.rs @@ -30,6 +30,9 @@ pub trait Repository: Any { /// recover the repository from the commit id. async fn recover(&mut self) -> Result<(), CoffeeError>; + /// switch to the specified branch. + async fn switch_branch(&self, branch_name: &str) -> Result<(), CoffeeError>; + /// return the name of the repository. fn name(&self) -> String;