aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-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