Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support choosing checkout branch method when status is not empty #2494

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
2 changes: 2 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[*.rs]
Fatpandac marked this conversation as resolved.
Show resolved Hide resolved
indent_style = tab
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

* support choosing checkout branch method when status is not empty
Fatpandac marked this conversation as resolved.
Show resolved Hide resolved

## [0.27.0] - 2024-01-14

**new: manage remotes**
Expand Down
20 changes: 20 additions & 0 deletions asyncgit/src/sync/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,23 @@ pub fn get_status(

Ok(res)
}

/// discard all changes in the working directory
pub fn discard_status(repo_path: &RepoPath) -> Result<bool> {
Fatpandac marked this conversation as resolved.
Show resolved Hide resolved
let repo = repo(repo_path)?;
let statuses = repo.statuses(None)?;

for status in statuses.iter() {
if status.status().is_wt_modified()
|| status.status().is_wt_new()
{
let oid = repo.head()?.target().unwrap();
let obj = repo.find_object(oid, None)?;

repo.checkout_tree(&obj, None)?;
repo.reset(&obj, git2::ResetType::Hard, None)?;
}
}

Ok(true)
}
27 changes: 17 additions & 10 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ use crate::{
options::{Options, SharedOptions},
popup_stack::PopupStack,
popups::{
AppOption, BlameFilePopup, BranchListPopup, CommitPopup,
CompareCommitsPopup, ConfirmPopup, CreateBranchPopup,
CreateRemotePopup, ExternalEditorPopup, FetchPopup,
FileRevlogPopup, FuzzyFindPopup, HelpPopup,
InspectCommitPopup, LogSearchPopupPopup, MsgPopup,
OptionsPopup, PullPopup, PushPopup, PushTagsPopup,
RemoteListPopup, RenameBranchPopup, RenameRemotePopup,
ResetPopup, RevisionFilesPopup, StashMsgPopup,
SubmodulesListPopup, TagCommitPopup, TagListPopup,
UpdateRemoteUrlPopup,
AppOption, BlameFilePopup, BranchListPopup,
CheckoutOptionPopup, CommitPopup, CompareCommitsPopup,
ConfirmPopup, CreateBranchPopup, CreateRemotePopup,
ExternalEditorPopup, FetchPopup, FileRevlogPopup,
FuzzyFindPopup, HelpPopup, InspectCommitPopup,
LogSearchPopupPopup, MsgPopup, OptionsPopup, PullPopup,
PushPopup, PushTagsPopup, RemoteListPopup, RenameBranchPopup,
RenameRemotePopup, ResetPopup, RevisionFilesPopup,
StashMsgPopup, SubmodulesListPopup, TagCommitPopup,
TagListPopup, UpdateRemoteUrlPopup,
},
queue::{
Action, AppTabs, InternalEvent, NeedsUpdate, Queue,
Expand Down Expand Up @@ -98,6 +98,7 @@ pub struct App {
submodule_popup: SubmodulesListPopup,
tags_popup: TagListPopup,
reset_popup: ResetPopup,
checkout_option_popup: CheckoutOptionPopup,
cmdbar: RefCell<CommandBar>,
tab: usize,
revlog: Revlog,
Expand Down Expand Up @@ -218,6 +219,7 @@ impl App {
stashing_tab: Stashing::new(&env),
stashlist_tab: StashList::new(&env),
files_tab: FilesTab::new(&env),
checkout_option_popup: CheckoutOptionPopup::new(&env),
tab: 0,
queue: env.queue,
theme: env.theme,
Expand Down Expand Up @@ -493,6 +495,7 @@ impl App {
fetch_popup,
tag_commit_popup,
reset_popup,
checkout_option_popup,
create_branch_popup,
create_remote_popup,
rename_remote_popup,
Expand Down Expand Up @@ -533,6 +536,7 @@ impl App {
submodule_popup,
tags_popup,
reset_popup,
checkout_option_popup,
create_branch_popup,
rename_branch_popup,
revision_files_popup,
Expand Down Expand Up @@ -905,6 +909,9 @@ impl App {
InternalEvent::CommitSearch(options) => {
self.revlog.search(options);
}
InternalEvent::CheckoutOption(branch, is_local) => {
self.checkout_option_popup.open(branch, is_local)?;
}
};

Ok(flags)
Expand Down
43 changes: 29 additions & 14 deletions src/popups/branchlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::{
ui::{self, Size},
};
use anyhow::Result;
use asyncgit::sync::status::StatusType;
Fatpandac marked this conversation as resolved.
Show resolved Hide resolved
use asyncgit::{
sync::{
self,
Expand Down Expand Up @@ -582,23 +583,37 @@ impl BranchListPopup {
anyhow::bail!("no valid branch selected");
}

if self.local {
checkout_branch(
&self.repo.borrow(),
&self.branches[self.selection as usize].name,
)?;
self.hide();
let status = sync::status::get_status(
&self.repo.borrow(),
StatusType::WorkingDir,
None,
)
.unwrap();

let selected_branch = &self.branches[self.selection as usize];
if status.is_empty() {
if self.local {
checkout_branch(
&self.repo.borrow(),
&selected_branch.name,
)?;
self.hide();
} else {
checkout_remote_branch(
&self.repo.borrow(),
&selected_branch,
)?;
self.local = true;
self.update_branches()?;
}
self.queue.push(InternalEvent::Update(NeedsUpdate::ALL));
} else {
checkout_remote_branch(
&self.repo.borrow(),
&self.branches[self.selection as usize],
)?;
self.local = true;
self.update_branches()?;
self.queue.push(InternalEvent::CheckoutOption(
selected_branch.clone(),
self.local.clone(),
));
}

self.queue.push(InternalEvent::Update(NeedsUpdate::ALL));

Ok(())
}

Expand Down
Loading