Skip to content

Commit 44ba5a8

Browse files
author
Stephan Dilly
authored
show branch ahead/behind only if upstream is set (#451)
closes #385
1 parent 779b2d5 commit 44ba5a8

File tree

2 files changed

+40
-14
lines changed

2 files changed

+40
-14
lines changed

asyncgit/src/sync/branch.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,25 @@ mod tests_create_branch {
247247
}
248248
}
249249

250+
#[cfg(test)]
251+
mod tests_branch_compare {
252+
use super::*;
253+
use crate::sync::tests::repo_init;
254+
255+
#[test]
256+
fn test_smoke() {
257+
let (_td, repo) = repo_init().unwrap();
258+
let root = repo.path().parent().unwrap();
259+
let repo_path = root.as_os_str().to_str().unwrap();
260+
261+
create_branch(repo_path, "test").unwrap();
262+
263+
let res = branch_compare_upstream(repo_path, "test");
264+
265+
assert_eq!(res.is_err(), true);
266+
}
267+
}
268+
250269
#[cfg(test)]
251270
mod tests_branches {
252271
use super::*;

src/tabs/status.rs

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ pub struct Status {
5050
git_diff: AsyncDiff,
5151
git_status_workdir: AsyncStatus,
5252
git_status_stage: AsyncStatus,
53-
git_branch_state: BranchCompare,
53+
git_branch_state: Option<BranchCompare>,
5454
git_branch_name: cached::BranchName,
5555
queue: Queue,
5656
git_action_executed: bool,
@@ -149,7 +149,7 @@ impl Status {
149149
git_status_workdir: AsyncStatus::new(sender.clone()),
150150
git_status_stage: AsyncStatus::new(sender.clone()),
151151
git_action_executed: false,
152-
git_branch_state: BranchCompare::default(),
152+
git_branch_state: None,
153153
git_branch_name: cached::BranchName::new(CWD),
154154
key_config,
155155
}
@@ -161,11 +161,18 @@ impl Status {
161161
chunks: &[tui::layout::Rect],
162162
) {
163163
if let Some(branch_name) = self.git_branch_name.last() {
164+
let ahead_behind =
165+
if let Some(state) = &self.git_branch_state {
166+
format!(
167+
"\u{2191}{} \u{2193}{} ",
168+
state.ahead, state.behind,
169+
)
170+
} else {
171+
String::new()
172+
};
164173
let w = Paragraph::new(format!(
165-
"\u{2191}{} \u{2193}{} {{{}}}",
166-
self.git_branch_state.ahead,
167-
self.git_branch_state.behind,
168-
branch_name
174+
"{}{{{}}}",
175+
ahead_behind, branch_name
169176
))
170177
.alignment(Alignment::Right);
171178

@@ -402,17 +409,17 @@ impl Status {
402409
}
403410

404411
fn check_branch_state(&mut self) {
405-
self.git_branch_state = self.git_branch_name.last().map_or(
406-
BranchCompare::default(),
407-
|branch| {
412+
self.git_branch_state =
413+
self.git_branch_name.last().and_then(|branch| {
408414
sync::branch_compare_upstream(CWD, branch.as_str())
409-
.unwrap_or_default()
410-
},
411-
);
415+
.ok()
416+
});
412417
}
413418

414-
const fn can_push(&self) -> bool {
415-
self.git_branch_state.ahead > 0
419+
fn can_push(&self) -> bool {
420+
self.git_branch_state
421+
.as_ref()
422+
.map_or(true, |state| state.ahead > 0)
416423
}
417424
}
418425

0 commit comments

Comments
 (0)