1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
use {
git2::{
Repository,
},
};
#[derive(Debug, Clone)]
pub struct GitInfo {
pub commit_id: String,
}
impl GitInfo {
pub fn read() -> Option<Self> {
std::env::current_dir().ok()
.and_then(|dir| Repository::discover(dir).ok())
.and_then(|repo| {
repo.head().ok()
.and_then(|head| {
head.peel_to_commit().ok()
.map(|commit| {
GitInfo {
commit_id: commit.id().to_string(),
}
})
})
})
}
pub(crate) fn diff(old_gi: &Option<GitInfo>, new_gi: &Option<GitInfo>) -> String {
match (old_gi, new_gi) {
(Some(old_gi), Some(new_gi)) => {
if old_gi.commit_id == new_gi.commit_id {
"(same commit)".to_string()
} else {
format!(
"(last commit: {})",
old_gi.commit_id.chars().take(8).collect::<String>(),
)
}
}
_ => {
"".to_string()
}
}
}
}