Skip to content

Commit d8500df

Browse files
committed
chore: format
1 parent 07cf88b commit d8500df

File tree

3 files changed

+35
-33
lines changed

3 files changed

+35
-33
lines changed

asyncgit/src/sync/branch/mod.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,25 @@ impl BranchInfo {
109109
pub const fn is_local(&self) -> bool {
110110
matches!(self.details, BranchDetails::Local(_))
111111
}
112+
113+
/// switches to this branch in given repo
114+
pub fn checkout<F>(
115+
&self,
116+
repo: &RepoPath,
117+
method: F,
118+
) -> Result<()>
119+
where
120+
F: FnOnce(&RepoPath, &Self) -> Result<()>,
121+
{
122+
if self.is_local() {
123+
checkout_branch(repo, &self.name)?;
124+
} else {
125+
checkout_remote_branch(repo, self)?;
126+
}
127+
128+
method(repo, &self)?;
129+
Ok(())
130+
}
112131
}
113132

114133
///

src/popups/branchlist.rs

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,8 @@ use anyhow::Result;
1616
use asyncgit::{
1717
sync::{
1818
self,
19-
branch::{
20-
checkout_remote_branch, BranchDetails, LocalBranch,
21-
RemoteBranch,
22-
},
23-
checkout_branch, get_branches_info,
19+
branch::{BranchDetails, LocalBranch, RemoteBranch},
20+
get_branches_info,
2421
status::StatusType,
2522
BranchInfo, BranchType, CommitId, RepoPathRef, RepoState,
2623
},
@@ -592,20 +589,18 @@ impl BranchListPopup {
592589

593590
let selected_branch = &self.branches[self.selection as usize];
594591
if status.is_empty() {
595-
if self.local {
596-
checkout_branch(
597-
&self.repo.borrow(),
598-
&selected_branch.name,
599-
)?;
600-
self.hide();
601-
} else {
602-
checkout_remote_branch(
603-
&self.repo.borrow(),
604-
selected_branch,
605-
)?;
606-
self.local = true;
607-
self.update_branches()?;
608-
}
592+
selected_branch.checkout(
593+
&self.repo.borrow(),
594+
|repo, branch| {
595+
if branch.is_local() {
596+
self.hide();
597+
Ok(())
598+
} else {
599+
self.local = true;
600+
self.update_branches()?;
601+
}
602+
},
603+
)?;
609604
self.queue.push(InternalEvent::Update(NeedsUpdate::ALL));
610605
} else {
611606
self.queue.push(InternalEvent::CheckoutOption(

src/popups/checkout_option.rs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@ use crate::{
1515
use anyhow::{Ok, Result};
1616
use asyncgit::sync::branch::checkout_remote_branch;
1717
use asyncgit::sync::status::discard_status;
18-
use asyncgit::sync::{
19-
checkout_branch, BranchInfo, RepoPath,
20-
};
18+
use asyncgit::sync::{checkout_branch, BranchInfo, RepoPath};
2119
use crossterm::event::Event;
2220
use ratatui::{
2321
layout::{Alignment, Rect},
@@ -89,17 +87,7 @@ impl CheckoutOptionPopup {
8987

9088
fn checkout(&self) -> Result<()> {
9189
if let Some(branch) = &self.branch {
92-
if branch.is_local() {
93-
checkout_branch(
94-
&self.repo,
95-
&self.branch.as_ref().expect("No branch").name,
96-
)?;
97-
} else {
98-
checkout_remote_branch(
99-
&self.repo,
100-
self.branch.as_ref().expect("No branch"),
101-
)?;
102-
}
90+
branch.checkout(&self.repo)?;
10391
}
10492

10593
Ok(())

0 commit comments

Comments
 (0)