1use crate::git::CommitResult;
4use git2::FileMode;
5use std::fmt::Write;
6
7pub fn format_commit_result(result: &CommitResult, message: &str) -> String {
9 let mut output = format!(
10 "[{} {}] {}\n",
11 result.branch,
12 result.commit_hash,
13 message.lines().next().unwrap_or("")
14 );
15
16 writeln!(
17 &mut output,
18 " {} file{} changed, {} insertion{}(+), {} deletion{}(-)",
19 result.files_changed,
20 if result.files_changed == 1 { "" } else { "s" },
21 result.insertions,
22 if result.insertions == 1 { "" } else { "s" },
23 result.deletions,
24 if result.deletions == 1 { "" } else { "s" }
25 )
26 .expect("writing to string should never fail");
27
28 for (file, mode) in &result.new_files {
29 writeln!(
30 &mut output,
31 " create mode {} {}",
32 format_file_mode(*mode),
33 file
34 )
35 .expect("writing to string should never fail");
36 }
37
38 output
39}
40
41fn format_file_mode(mode: FileMode) -> String {
42 match mode {
43 FileMode::Blob => "100644",
44 FileMode::BlobExecutable => "100755",
45 FileMode::Link => "120000",
46 FileMode::Commit => "160000",
47 FileMode::Tree => "040000",
48 _ => "000000",
49 }
50 .to_string()
51}