gitai/features/commit/
mod.rs

1#[allow(clippy::uninlined_format_args)]
2mod cli;
3#[allow(clippy::uninlined_format_args)]
4pub mod completion;
5mod relevance;
6pub mod types;
7
8#[allow(clippy::uninlined_format_args)]
9pub mod prompt;
10#[allow(clippy::uninlined_format_args)]
11pub mod service;
12
13pub use cli::{handle_completion_command, handle_message_command, handle_pr_command};
14use git2::FileMode;
15pub use service::CommitService;
16pub use types::{
17    GeneratedMessage, GeneratedPullRequest, format_commit_message, format_pull_request,
18};
19
20use crate::git::CommitResult;
21use std::fmt::Write;
22
23pub fn format_commit_result(result: &CommitResult, message: &str) -> String {
24    let mut output = format!(
25        "[{} {}] {}\n",
26        result.branch,
27        result.commit_hash,
28        message.lines().next().unwrap_or("")
29    );
30
31    writeln!(
32        &mut output,
33        " {} file{} changed, {} insertion{}(+), {} deletion{}(-)",
34        result.files_changed,
35        if result.files_changed == 1 { "" } else { "s" },
36        result.insertions,
37        if result.insertions == 1 { "" } else { "s" },
38        result.deletions,
39        if result.deletions == 1 { "" } else { "s" }
40    )
41    .expect("writing to string should never fail");
42
43    for (file, mode) in &result.new_files {
44        writeln!(
45            &mut output,
46            " create mode {} {}",
47            format_file_mode(*mode),
48            file
49        )
50        .expect("writing to string should never fail");
51    }
52
53    output
54}
55
56fn format_file_mode(mode: FileMode) -> String {
57    match mode {
58        FileMode::Blob => "100644",
59        FileMode::BlobExecutable => "100755",
60        FileMode::Link => "120000",
61        FileMode::Commit => "160000",
62        FileMode::Tree => "040000",
63        _ => "000000",
64    }
65    .to_string()
66}