aboutsummaryrefslogtreecommitdiff
path: root/src/vcs.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/vcs.rs')
-rw-r--r--src/vcs.rs55
1 files changed, 55 insertions, 0 deletions
diff --git a/src/vcs.rs b/src/vcs.rs
new file mode 100644
index 0000000..db69897
--- /dev/null
+++ b/src/vcs.rs
@@ -0,0 +1,55 @@
1use std::env;
2use git2::{ Repository, Status };
3use colored::*;
4
5pub fn vcs_status() -> Option<(colored::ColoredString, colored::ColoredString)> {
6 let current_dir = env::var("PWD").unwrap();
7
8 let repo = match Repository::open(current_dir) {
9 Ok(r) => r,
10 Err(_) => return None
11 };
12
13 let reference = repo.head().unwrap();
14 let mut branch;
15
16 if reference.is_branch() {
17 branch = format!("{}", reference.shorthand().unwrap()).bright_black();
18 } else {
19 let commit = reference.peel_to_commit().unwrap();
20 let id = commit.id();
21 branch = format!("{:.6}", id).bright_black();
22 }
23
24 let mut repo_stat = "".white();
25 let git_clean_color = env::var("GIT_CLEAN_COLOR").unwrap_or("green".into());
26 let git_wt_modified_color = env::var("GIT_WT_MODIFIED_COLOR").unwrap_or("red".into());
27 let git_index_modified_color = env::var("GIT_INDEX_MODIFIED_COLOR").unwrap_or("yellow".into());
28
29 let file_stats = repo.statuses(None).unwrap();
30 for file in file_stats.iter() {
31 match file.status() {
32 // STATE: unstaged (working tree modified)
33 Status::WT_NEW | Status::WT_MODIFIED |
34 Status::WT_DELETED | Status::WT_TYPECHANGE |
35 Status::WT_RENAMED => {
36 let stat_char = env::var("GIT_WT_MODIFIED").unwrap_or("×".into());
37 repo_stat = stat_char.color(&git_wt_modified_color[..]);
38 break;
39 },
40 // STATE: staged (changes added to index)
41 Status::INDEX_NEW | Status::INDEX_MODIFIED |
42 Status::INDEX_DELETED | Status::INDEX_TYPECHANGE |
43 Status::INDEX_RENAMED => {
44 let stat_char = env::var("GIT_INDEX_MODIFIED").unwrap_or("±".into());
45 repo_stat = stat_char.color(&git_index_modified_color[..]);
46 },
47 // STATE: comitted (changes have been saved in the repo)
48 _ => {
49 let stat_char = env::var("GIT_CLEAN").unwrap_or("·".into());
50 repo_stat = stat_char.color(&git_clean_color[..]);
51 }
52 }
53 }
54 return Some((branch, repo_stat))
55}