Skip to content

Commit c664c66

Browse files
committed
feat: switch_branch func
1 parent 94095f1 commit c664c66

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

coffee_github/src/repository.rs

+20
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,26 @@ impl Repository for Github {
310310
}
311311
}
312312

313+
async fn switch_branch(&self, branch_name: &str) -> Result<(), CoffeeError> {
314+
let repo = git2::Repository::open(&self.url.path_string)
315+
.map_err(|err| error!("{}", err.message()))?;
316+
let mut remote = repo
317+
.find_remote("origin")
318+
.map_err(|err| error!("{}", err.message()))?;
319+
remote
320+
.fetch(&[&branch_name], None, None)
321+
.map_err(|err| error!("{}", err.message()))?;
322+
let oid = repo
323+
.refname_to_id(&format!("refs/remotes/origin/{}", branch_name))
324+
.map_err(|err| error!("{}", err.message()))?;
325+
let obj = repo
326+
.find_object(oid, None)
327+
.map_err(|err| error!("{}", err.message()))?;
328+
repo.reset(&obj, git2::ResetType::Hard, None)
329+
.map_err(|err| error!("{}", err.message()))?;
330+
Ok(())
331+
}
332+
313333
/// list of the plugin installed inside the repository.
314334
async fn list(&self) -> Result<Vec<Plugin>, CoffeeError> {
315335
Ok(self.plugins.clone())

coffee_lib/src/repository.rs

+3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ pub trait Repository: Any {
3030
/// recover the repository from the commit id.
3131
async fn recover(&mut self) -> Result<(), CoffeeError>;
3232

33+
/// switch to the specified branch.
34+
async fn switch_branch(&self, branch_name: &str) -> Result<(), CoffeeError>;
35+
3336
/// return the name of the repository.
3437
fn name(&self) -> String;
3538

0 commit comments

Comments
 (0)