forked from gitui-org/gitui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommit_details.rs
More file actions
161 lines (134 loc) · 3.66 KB
/
commit_details.rs
File metadata and controls
161 lines (134 loc) · 3.66 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
use super::{commits_info::get_message, utils::repo, CommitId};
use crate::error::Result;
use git2::Signature;
use scopetime::scope_time;
///
#[derive(Debug, PartialEq)]
pub struct CommitSignature {
///
pub name: String,
///
pub email: String,
/// time in secs since Unix epoch
pub time: i64,
}
impl CommitSignature {
/// convert from git2-rs `Signature`
pub fn from(s: Signature<'_>) -> Self {
Self {
name: s.name().unwrap_or("").to_string(),
email: s.email().unwrap_or("").to_string(),
time: s.when().seconds(),
}
}
}
///
pub struct CommitMessage {
/// first line
pub subject: String,
/// remaining lines if more than one
pub body: Option<String>,
}
impl CommitMessage {
///
pub fn from(s: &str) -> Self {
let mut lines = s.lines();
let subject = if let Some(subject) = lines.next() {
subject.to_string()
} else {
String::new()
};
let body: Vec<String> =
lines.map(|line| line.to_string()).collect();
Self {
subject,
body: if body.is_empty() {
None
} else {
Some(body.join("\n"))
},
}
}
///
pub fn combine(self) -> String {
if let Some(body) = self.body {
format!("{}{}", self.subject, body)
} else {
self.subject
}
}
}
///
pub struct CommitDetails {
///
pub author: CommitSignature,
/// committer when differs to `author` otherwise None
pub committer: Option<CommitSignature>,
///
pub message: Option<CommitMessage>,
///
pub hash: String,
}
///
pub fn get_commit_details(
repo_path: &str,
id: CommitId,
) -> Result<CommitDetails> {
scope_time!("get_commit_details");
let repo = repo(repo_path)?;
let commit = repo.find_commit(id.into())?;
let author = CommitSignature::from(commit.author());
let committer = CommitSignature::from(commit.committer());
let committer = if author == committer {
None
} else {
Some(committer)
};
let msg =
CommitMessage::from(get_message(&commit, None).as_str());
let details = CommitDetails {
author,
committer,
message: Some(msg),
hash: id.to_string(),
};
Ok(details)
}
#[cfg(test)]
mod tests {
use super::{get_commit_details, CommitMessage};
use crate::error::Result;
use crate::sync::{
commit, stage_add_file, tests::repo_init_empty,
};
use std::{fs::File, io::Write, path::Path};
#[test]
fn test_msg_invalid_utf8() -> Result<()> {
let file_path = Path::new("foo");
let (_td, repo) = repo_init_empty().unwrap();
let root = repo.path().parent().unwrap();
let repo_path = root.as_os_str().to_str().unwrap();
File::create(&root.join(file_path))?.write_all(b"a")?;
stage_add_file(repo_path, file_path).unwrap();
let msg = invalidstring::invalid_utf8("test msg");
let id = commit(repo_path, msg.as_str()).unwrap();
let res = get_commit_details(repo_path, id).unwrap();
dbg!(&res.message.as_ref().unwrap().subject);
assert_eq!(
res.message
.as_ref()
.unwrap()
.subject
.starts_with("test msg"),
true
);
Ok(())
}
#[test]
fn test_msg_linefeeds() -> Result<()> {
let msg = CommitMessage::from("foo\nbar\r\ntest");
assert_eq!(msg.subject, String::from("foo"),);
assert_eq!(msg.body, Some(String::from("bar\ntest")),);
Ok(())
}
}