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

Branch popup #303

Merged
merged 25 commits into from
Oct 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
0098b59
Initial implementation
WizardOhio24 Oct 3, 2020
05ed8a2
Fix clippy errors
WizardOhio24 Oct 4, 2020
75ace52
Remove can_create_branch
WizardOhio24 Oct 4, 2020
aa1f25c
Remove From<> in BranchForDisplay
WizardOhio24 Oct 4, 2020
e7c3d33
Add comments
WizardOhio24 Oct 4, 2020
3002a12
Change vim_keys to fix clippy
WizardOhio24 Oct 4, 2020
59d7f09
Fix appearance
WizardOhio24 Oct 5, 2020
5cd7385
Update get_branches_to_display function
WizardOhio24 Oct 8, 2020
970e1d8
Update checkout_branch function
WizardOhio24 Oct 8, 2020
edd7f77
Update select_branch component
WizardOhio24 Oct 8, 2020
b0785fc
Add scopetime to get_branches_to_display
WizardOhio24 Oct 8, 2020
db474f4
Add styling to branch list
WizardOhio24 Oct 8, 2020
4a57b62
Fix clippy warnings
WizardOhio24 Oct 8, 2020
19399a0
Add ... to long commit messages
WizardOhio24 Oct 8, 2020
8a7caa6
switch_to_selected_branch returns result
WizardOhio24 Oct 8, 2020
f453142
Fix long branch names removing commit message
WizardOhio24 Oct 9, 2020
235faa8
Modify checkout to error correctly
WizardOhio24 Oct 9, 2020
8d25dd6
Remove unused imports
WizardOhio24 Oct 9, 2020
64ca407
Add scope_time to checkout_branch
WizardOhio24 Oct 9, 2020
bcc0312
Reset head if the checkout fails
WizardOhio24 Oct 9, 2020
a26853d
Fix error when nothing is changed
WizardOhio24 Oct 9, 2020
7fe2f68
Remove update_branches from app update
WizardOhio24 Oct 9, 2020
317f131
Change blocking items back to number of blocking items
WizardOhio24 Oct 9, 2020
d028c2f
Hide popup on creating branch
WizardOhio24 Oct 9, 2020
34c55d8
Remove ability to create a branch in status
WizardOhio24 Oct 9, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion assets/vim_style_key_config.ron
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@
log_tag_commit: ( code: Char('t'), modifiers: ( bits: 0,),),
commit_amend: ( code: Char('A'), modifiers: ( bits: 1,),),
copy: ( code: Char('y'), modifiers: ( bits: 0,),),
create_branch: ( code: Char('b'), modifiers: ( bits: 0,),),
create_branch: ( code: Char('c'), modifiers: ( bits: 0,),),
select_branch: ( code: Char('b'), modifiers: ( bits: 0,),),
push: ( code: Char('p'), modifiers: ( bits: 0,),),
fetch: ( code: Char('f'), modifiers: ( bits: 0,),),
)
81 changes: 81 additions & 0 deletions asyncgit/src/sync/branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::{
error::{Error, Result},
sync::utils,
};
use git2::BranchType;
use scopetime::scope_time;
use utils::get_head_repo;

Expand All @@ -28,6 +29,86 @@ pub(crate) fn get_branch_name(repo_path: &str) -> Result<String> {
Err(Error::NoHead)
}

///
pub struct BranchForDisplay {
///
pub name: String,
///
pub reference: String,
///
pub top_commit_message: String,
///
pub top_commit_reference: String,
///
pub is_head: bool,
}

/// TODO make this cached
/// Used to return only the nessessary information for displaying a branch
/// rather than an iterator over the actual branches
pub fn get_branches_to_display(
repo_path: &str,
) -> Result<Vec<BranchForDisplay>> {
scope_time!("get_branches_to_display");
let cur_repo = utils::repo(repo_path)?;
let mut branches_for_display = vec![];

for b in cur_repo.branches(Some(BranchType::Local))? {
let branch = &b?.0;
let top_commit = branch.get().peel_to_commit()?;
let mut commit_id = top_commit.id().to_string();
commit_id.truncate(7);

branches_for_display.push(BranchForDisplay {
name: String::from_utf8(Vec::from(branch.name_bytes()?))?,
reference: String::from_utf8(Vec::from(
branch.get().name_bytes(),
))?,
top_commit_message: String::from_utf8(Vec::from(
top_commit.summary_bytes().unwrap_or(&[]),
))?,
top_commit_reference: commit_id,
is_head: branch.is_head(),
})
}
Ok(branches_for_display)
}

/// Modify HEAD to point to a branch then checkout head, does not work if there are uncommitted changes
pub fn checkout_branch(
repo_path: &str,
branch_ref: &str,
) -> Result<()> {
scope_time!("checkout_branch");
// This defaults to a safe checkout, so don't delete anything that
WizardOhio24 marked this conversation as resolved.
Show resolved Hide resolved
// hasn't been committed or stashed, in this case it will Err
let repo = utils::repo(repo_path)?;
let cur_ref = repo.head()?;
if repo
.statuses(Some(
git2::StatusOptions::new().include_ignored(false),
extrawurst marked this conversation as resolved.
Show resolved Hide resolved
))?
.is_empty()
{
repo.set_head(branch_ref)?;

if let Err(e) = repo.checkout_head(Some(
git2::build::CheckoutBuilder::new().force(),
)) {
// This is safe beacuse cur_ref was just found
repo.set_head(cur_ref.name().unwrap_or(""))?;
return Err(Error::Git(e));
}
Ok(())
} else {
Err(Error::Generic(
format!("Cannot change branch. There are unstaged/staged changes which have not been committed/stashed. There is {:?} changes preventing checking out a different branch.", repo.statuses(Some(
git2::StatusOptions::new().include_ignored(false),
))?.len()),
))
}
}

/// creates a new branch pointing to current HEAD commit and updating HEAD to new branch
pub fn create_branch(repo_path: &str, name: &str) -> Result<()> {
scope_time!("create_branch");
Expand Down
5 changes: 4 additions & 1 deletion asyncgit/src/sync/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ pub mod status;
mod tags;
pub mod utils;

pub use branch::create_branch;
pub(crate) use branch::get_branch_name;
pub use branch::{
checkout_branch, create_branch, get_branches_to_display,
BranchForDisplay,
};
pub use commit::{amend, commit, tag};
pub use commit_details::{
get_commit_details, CommitDetails, CommitMessage,
Expand Down
15 changes: 14 additions & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use crate::{
Component, CreateBranchComponent, DrawableComponent,
ExternalEditorComponent, HelpComponent,
InspectCommitComponent, MsgComponent, PushComponent,
ResetComponent, StashMsgComponent, TagCommitComponent,
ResetComponent, SelectBranchComponent, StashMsgComponent,
TagCommitComponent,
},
input::{Input, InputEvent, InputState},
keys::{KeyConfig, SharedKeyConfig},
Expand Down Expand Up @@ -45,6 +46,7 @@ pub struct App {
push_popup: PushComponent,
tag_commit_popup: TagCommitComponent,
create_branch_popup: CreateBranchComponent,
select_branch_popup: SelectBranchComponent,
cmdbar: RefCell<CommandBar>,
tab: usize,
revlog: Revlog,
Expand Down Expand Up @@ -116,6 +118,11 @@ impl App {
theme.clone(),
key_config.clone(),
),
select_branch_popup: SelectBranchComponent::new(
queue.clone(),
theme.clone(),
key_config.clone(),
),
do_quit: false,
cmdbar: RefCell::new(CommandBar::new(
theme.clone(),
Expand Down Expand Up @@ -335,6 +342,7 @@ impl App {
push_popup,
tag_commit_popup,
create_branch_popup,
select_branch_popup,
help,
revlog,
status_tab,
Expand Down Expand Up @@ -487,6 +495,9 @@ impl App {
InternalEvent::CreateBranch => {
self.create_branch_popup.open()?;
}
InternalEvent::SelectBranch => {
self.select_branch_popup.open()?;
}
InternalEvent::TabSwitch => self.set_tab(0)?,
InternalEvent::InspectCommit(id, tags) => {
self.inspect_commit_popup.open(id, tags)?;
Expand Down Expand Up @@ -562,6 +573,7 @@ impl App {
|| self.tag_commit_popup.is_visible()
|| self.create_branch_popup.is_visible()
|| self.push_popup.is_visible()
|| self.select_branch_popup.is_visible()
}

fn draw_popups<B: Backend>(
Expand All @@ -587,6 +599,7 @@ impl App {
self.msg.draw(f, size)?;
self.external_editor_popup.draw(f, size)?;
self.tag_commit_popup.draw(f, size)?;
self.select_branch_popup.draw(f, size)?;
self.create_branch_popup.draw(f, size)?;
self.push_popup.draw(f, size)?;

Expand Down
2 changes: 2 additions & 0 deletions src/components/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ mod inspect_commit;
mod msg;
mod push;
mod reset;
mod select_branch;
mod stashmsg;
mod tag_commit;
mod textinput;
Expand All @@ -34,6 +35,7 @@ pub use inspect_commit::InspectCommitComponent;
pub use msg::MsgComponent;
pub use push::PushComponent;
pub use reset::ResetComponent;
pub use select_branch::SelectBranchComponent;
pub use stashmsg::StashMsgComponent;
pub use tag_commit::TagCommitComponent;
pub use textinput::TextInputComponent;
Expand Down
Loading