Skip to content

Commit 160f9f8

Browse files
committed
feat: support choosing checkout branch method
Allow choosing a checkout branch method when status is not empty
1 parent aaa1bfa commit 160f9f8

File tree

5 files changed

+351
-17
lines changed

5 files changed

+351
-17
lines changed

src/app.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use crate::{
1919
RemoteListPopup, RenameBranchPopup, RenameRemotePopup,
2020
ResetPopup, RevisionFilesPopup, StashMsgPopup,
2121
SubmodulesListPopup, TagCommitPopup, TagListPopup,
22-
UpdateRemoteUrlPopup,
22+
UpdateRemoteUrlPopup, CheckoutOptionPopup
2323
},
2424
queue::{
2525
Action, AppTabs, InternalEvent, NeedsUpdate, Queue,
@@ -98,6 +98,7 @@ pub struct App {
9898
submodule_popup: SubmodulesListPopup,
9999
tags_popup: TagListPopup,
100100
reset_popup: ResetPopup,
101+
checkout_option_popup: CheckoutOptionPopup,
101102
cmdbar: RefCell<CommandBar>,
102103
tab: usize,
103104
revlog: Revlog,
@@ -218,6 +219,7 @@ impl App {
218219
stashing_tab: Stashing::new(&env),
219220
stashlist_tab: StashList::new(&env),
220221
files_tab: FilesTab::new(&env),
222+
checkout_option_popup: CheckoutOptionPopup::new(&env),
221223
tab: 0,
222224
queue: env.queue,
223225
theme: env.theme,
@@ -493,6 +495,7 @@ impl App {
493495
fetch_popup,
494496
tag_commit_popup,
495497
reset_popup,
498+
checkout_option_popup,
496499
create_branch_popup,
497500
create_remote_popup,
498501
rename_remote_popup,
@@ -533,6 +536,7 @@ impl App {
533536
submodule_popup,
534537
tags_popup,
535538
reset_popup,
539+
checkout_option_popup,
536540
create_branch_popup,
537541
rename_branch_popup,
538542
revision_files_popup,
@@ -904,7 +908,10 @@ impl App {
904908
}
905909
InternalEvent::CommitSearch(options) => {
906910
self.revlog.search(options);
907-
}
911+
},
912+
InternalEvent::CheckoutOption(branch, is_local) => {
913+
self.checkout_option_popup.open(branch, is_local)?;
914+
}
908915
};
909916

910917
Ok(flags)

src/popups/branchlist.rs

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use crate::{
1313
ui::{self, Size},
1414
};
1515
use anyhow::Result;
16+
use asyncgit::sync::status::StatusType;
1617
use asyncgit::{
1718
sync::{
1819
self,
@@ -582,23 +583,37 @@ impl BranchListPopup {
582583
anyhow::bail!("no valid branch selected");
583584
}
584585

585-
if self.local {
586-
checkout_branch(
587-
&self.repo.borrow(),
588-
&self.branches[self.selection as usize].name,
589-
)?;
590-
self.hide();
586+
let status = sync::status::get_status(
587+
&self.repo.borrow(),
588+
StatusType::WorkingDir,
589+
None,
590+
)
591+
.unwrap();
592+
593+
let selected_branch = &self.branches[self.selection as usize];
594+
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+
}
609+
self.queue.push(InternalEvent::Update(NeedsUpdate::ALL));
591610
} else {
592-
checkout_remote_branch(
593-
&self.repo.borrow(),
594-
&self.branches[self.selection as usize],
595-
)?;
596-
self.local = true;
597-
self.update_branches()?;
611+
self.queue.push(InternalEvent::CheckoutOption(
612+
selected_branch.clone(),
613+
self.local.clone()
614+
));
598615
}
599616

600-
self.queue.push(InternalEvent::Update(NeedsUpdate::ALL));
601-
602617
Ok(())
603618
}
604619

0 commit comments

Comments
 (0)