-
Notifications
You must be signed in to change notification settings - Fork 3
/
commit_info.rs
80 lines (62 loc) · 1.86 KB
/
commit_info.rs
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
use std::fmt;
use sha1::{Sha1, Digest};
#[derive(Clone)]
pub struct CommitInfo {
headers: String,
body: String,
}
impl fmt::Display for CommitInfo {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}\n\n{}", self.headers, self.body)
}
}
impl CommitInfo {
pub fn from_str(str: &str) -> Option<CommitInfo> {
let parts: Vec<&str> = str.splitn(2, "\n\n").collect();
match *parts.as_slice() {
[headers, body] =>
Some(CommitInfo{
headers: headers.to_string(),
body: body.to_string(),
}),
_ =>
None,
}
}
pub fn add_header(&self, name: &str, value: &str) -> CommitInfo {
let new_headers = format!("{}\n{} {}", self.headers, name, value);
CommitInfo{
headers: new_headers,
body: self.body.clone(),
}
}
pub fn remove_header(&self, name: &str) -> CommitInfo {
let new_headers = self.headers
.split('\n')
.filter(|header| !header.starts_with(name))
.collect::<Vec<&str>>()
.join("\n");
CommitInfo{
headers: new_headers,
body: self.body.clone(),
}
}
pub fn has_header(&self, name: &str) -> bool {
self.headers
.split('\n')
.filter(|header| header.starts_with(name))
.count() > 0
}
pub fn hash(&self) -> String {
let str = CommitInfo::add_length_prefix(&self.to_string());
sha1(&str)
}
fn add_length_prefix(commit_info_str: &str) -> String {
format!("commit {}{}{}", commit_info_str.len(), '\0', commit_info_str)
}
}
fn sha1(str: &str) -> String {
let mut hasher = Sha1::new();
hasher.update(str.as_bytes());
format!("{:x}", hasher.finalize())
}