aboutsummaryrefslogtreecommitdiff
path: root/src/vcs.rs
diff options
context:
space:
mode:
authorNerdyPepper <[email protected]>2019-05-21 18:18:40 +0100
committerNerdyPepper <[email protected]>2019-05-21 18:18:40 +0100
commit78230815814ee4f4e8af4c1ad72b83b1ede242d0 (patch)
tree1578d4753350d297a7f8aa05094385b3b9767b0c /src/vcs.rs
parentd92c7ca64023f6d321a4938e3d47a4ce6b489105 (diff)
add commit distance indicator
Diffstat (limited to 'src/vcs.rs')
-rw-r--r--src/vcs.rs27
1 files changed, 27 insertions, 0 deletions
diff --git a/src/vcs.rs b/src/vcs.rs
index 8d493de..5201886 100644
--- a/src/vcs.rs
+++ b/src/vcs.rs
@@ -21,6 +21,17 @@ pub fn vcs_status() -> Option<(colored::ColoredString, colored::ColoredString)>
21 return None 21 return None
22 } 22 }
23 let repo = repo.unwrap(); 23 let repo = repo.unwrap();
24
25 let mut commit_dist: String = "".into();
26 if let Some((ahead, behind)) = get_ahead_behind(&repo) {
27 if ahead > 0 {
28 commit_dist.push_str(" ↑");
29 }
30 if behind > 0 {
31 commit_dist.push_str(" ↓");
32 }
33 }
34
24 let reference = match repo.head() { 35 let reference = match repo.head() {
25 Ok(r) => r, 36 Ok(r) => r,
26 Err(_) => return None 37 Err(_) => return None
@@ -71,3 +82,19 @@ pub fn vcs_status() -> Option<(colored::ColoredString, colored::ColoredString)>
71 } 82 }
72 return Some((branch, repo_stat)) 83 return Some((branch, repo_stat))
73} 84}
85
86fn get_ahead_behind(r: &Repository) -> Option<(usize, usize)> {
87 let head = (r.head().ok())?;
88 if !head.is_branch() {
89 return None
90 }
91
92 let head_name = (head.shorthand())?;
93 let head_branch = (r.find_branch(head_name, git2::BranchType::Local).ok())?;
94 let upstream = (head_branch.upstream().ok())?;
95 let head_oid = (head.target())?;
96 let upstream_oid = (upstream.get().target())?;
97
98 r.graph_ahead_behind(head_oid, upstream_oid).ok()
99}
100