1use git2::Commit;
2use git_graph::print::format::format_date;
3use std::fmt::Write;
4use yansi::Paint;
5
6pub fn format(commit: &Commit, branches: String, hash_color: Option<u8>) -> Vec<String> {
8 let mut out_vec = vec![];
9 let mut out = String::new();
10
11 if let Some(color) = hash_color {
12 write!(out, "{}", Paint::fixed(color, &commit.id()))
13 } else {
14 write!(out, "{}", &commit.id())
15 }
16 .unwrap();
17
18 out_vec.push(out);
19 out = String::new();
20
21 write!(out, "{}", branches).unwrap();
22 out_vec.push(out);
23
24 if commit.parent_count() > 1 {
25 out = String::new();
26 write!(
27 out,
28 " Merge: {} {}",
29 &commit.parent_id(0).unwrap().to_string()[..7],
30 &commit.parent_id(1).unwrap().to_string()[..7]
31 )
32 .unwrap();
33 out_vec.push(out);
34 } else {
35 out = String::new();
36 out_vec.push(out);
37 }
38
39 out = String::new();
40 write!(
41 out,
42 "Author: {} <{}>",
43 commit.author().name().unwrap_or(""),
44 commit.author().email().unwrap_or("")
45 )
46 .unwrap();
47 out_vec.push(out);
48
49 out = String::new();
50 write!(
51 out,
52 "Date: {}",
53 format_date(commit.author().when(), "%a %b %e %H:%M:%S %Y %z")
54 )
55 .unwrap();
56 out_vec.push(out);
57
58 out_vec.push("".to_string());
59 let mut add_line = true;
60 for line in commit.message().unwrap_or("").lines() {
61 if line.is_empty() {
62 out_vec.push(line.to_string());
63 } else {
64 out_vec.push(format!(" {}", line));
65 }
66 add_line = !line.trim().is_empty();
67 }
68 if add_line {
69 out_vec.push("".to_string());
70 }
71
72 out_vec
73}