show branch ahead/behind only if upstream is set (#451)

closes #385
This commit is contained in:
Stephan Dilly 2020-12-20 16:01:04 +01:00 committed by GitHub
parent 779b2d572f
commit 44ba5a83c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 14 deletions

View File

@ -247,6 +247,25 @@ mod tests_create_branch {
}
}
#[cfg(test)]
mod tests_branch_compare {
use super::*;
use crate::sync::tests::repo_init;
#[test]
fn test_smoke() {
let (_td, repo) = repo_init().unwrap();
let root = repo.path().parent().unwrap();
let repo_path = root.as_os_str().to_str().unwrap();
create_branch(repo_path, "test").unwrap();
let res = branch_compare_upstream(repo_path, "test");
assert_eq!(res.is_err(), true);
}
}
#[cfg(test)]
mod tests_branches {
use super::*;

View File

@ -50,7 +50,7 @@ pub struct Status {
git_diff: AsyncDiff,
git_status_workdir: AsyncStatus,
git_status_stage: AsyncStatus,
git_branch_state: BranchCompare,
git_branch_state: Option<BranchCompare>,
git_branch_name: cached::BranchName,
queue: Queue,
git_action_executed: bool,
@ -149,7 +149,7 @@ impl Status {
git_status_workdir: AsyncStatus::new(sender.clone()),
git_status_stage: AsyncStatus::new(sender.clone()),
git_action_executed: false,
git_branch_state: BranchCompare::default(),
git_branch_state: None,
git_branch_name: cached::BranchName::new(CWD),
key_config,
}
@ -161,11 +161,18 @@ impl Status {
chunks: &[tui::layout::Rect],
) {
if let Some(branch_name) = self.git_branch_name.last() {
let ahead_behind =
if let Some(state) = &self.git_branch_state {
format!(
"\u{2191}{} \u{2193}{} ",
state.ahead, state.behind,
)
} else {
String::new()
};
let w = Paragraph::new(format!(
"\u{2191}{} \u{2193}{} {{{}}}",
self.git_branch_state.ahead,
self.git_branch_state.behind,
branch_name
"{}{{{}}}",
ahead_behind, branch_name
))
.alignment(Alignment::Right);
@ -402,17 +409,17 @@ impl Status {
}
fn check_branch_state(&mut self) {
self.git_branch_state = self.git_branch_name.last().map_or(
BranchCompare::default(),
|branch| {
self.git_branch_state =
self.git_branch_name.last().and_then(|branch| {
sync::branch_compare_upstream(CWD, branch.as_str())
.unwrap_or_default()
},
);
.ok()
});
}
const fn can_push(&self) -> bool {
self.git_branch_state.ahead > 0
fn can_push(&self) -> bool {
self.git_branch_state
.as_ref()
.map_or(true, |state| state.ahead > 0)
}
}