git-iris 2.0.8

AI-powered Git workflow assistant for smart commits, code reviews, changelogs, and release notes
Documentation
//! Git output formatting utilities

use crate::git::CommitResult;
use git2::FileMode;
use std::fmt::Write;

/// Formats a commit result into a human-readable string
#[must_use]
pub fn format_commit_result(result: &CommitResult, message: &str) -> String {
    let mut output = format!(
        "[{} {}] {}\n",
        result.branch,
        result.commit_hash,
        message.lines().next().unwrap_or("")
    );

    writeln!(
        &mut output,
        " {} file{} changed, {} insertion{}(+), {} deletion{}(-)",
        result.files_changed,
        if result.files_changed == 1 { "" } else { "s" },
        result.insertions,
        if result.insertions == 1 { "" } else { "s" },
        result.deletions,
        if result.deletions == 1 { "" } else { "s" }
    )
    .expect("writing to string should never fail");

    for (file, mode) in &result.new_files {
        writeln!(
            &mut output,
            " create mode {} {}",
            format_file_mode(*mode),
            file
        )
        .expect("writing to string should never fail");
    }

    output
}

fn format_file_mode(mode: FileMode) -> String {
    match mode {
        FileMode::Blob => "100644",
        FileMode::BlobExecutable => "100755",
        FileMode::Link => "120000",
        FileMode::Commit => "160000",
        FileMode::Tree => "040000",
        _ => "000000",
    }
    .to_string()
}