git_igitt/util/
format.rs

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