Skip to content

Commit

Permalink
feat: switch_branch func
Browse files Browse the repository at this point in the history
  • Loading branch information
royalpinto007 committed Jan 26, 2024
1 parent 94095f1 commit c664c66
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
20 changes: 20 additions & 0 deletions coffee_github/src/repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<Plugin>, CoffeeError> {
Ok(self.plugins.clone())
Expand Down
3 changes: 3 additions & 0 deletions coffee_lib/src/repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit c664c66

Please sign in to comment.