Skip to main content

git_iris/
output.rs

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