1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
use core::panic;
use std::process::Command;

use anyhow::{Error, Result};

use crate::cli::DryRunAndCopyFlag;
use crate::run_mode::get_run_mode_from_options;
use crate::run_mode::run_copy;
use crate::run_mode::RunMode;
use crate::template::interpolate_on_custom_val;
use crate::template::validate_interpolation_places_on_custom_pattern;
use crate::{
    cli::CommitOperationArguments,
    git_config::GitConfig,
    template::{interpolate, validate_interpolation_places_count},
};

pub fn commit_with_formatted_message(
    options: CommitOperationArguments,
    config: GitConfig,
) -> Result<(), Error> {
    let selected_commit_format = options.use_template.key;

    let _use_autocomplete_values = &options.use_template.use_autocomplete;
    let _auto_complete_values = &config.data.autocomplete_values;

    let picked_commit_format = config
        .data
        .commit_template_variants
        .get(&selected_commit_format)
        .unwrap_or_else(|| {
            panic!(
                "No commit template under given key {} \n \
                You should add it prior to trying to use",
                selected_commit_format
            )
        });

    let is_valid = validate_interpolation_places_count(
        picked_commit_format,
        options.use_template.interpolate_values.len(),
    );

    if is_valid.is_err() {
        let err: Error = is_valid.err().unwrap();
        return Err(err);
    }

    let interpolated_commit = interpolate(
        picked_commit_format,
        options.use_template.interpolate_values,
    )?;

    let interpolated_commit = if options.flags.use_branch_number {
        println!("{}", interpolated_commit);
        let higher_bound_of_digits_in_utf8 = &58;
        let lower_bound_of_digits_in_utf8 = &47;
        let branch_output = Command::new("git").arg("status").output().unwrap().stdout;
        let branch_number: Vec<u8> = branch_output
            .into_iter()
            .filter(|u8_char| {
                u8_char < higher_bound_of_digits_in_utf8 && u8_char > lower_bound_of_digits_in_utf8
            })
            .collect();
        let branch_number: String = String::from_utf8_lossy(&branch_number).to_string();
        let branch_number_as_interpolate_value = vec![branch_number];

        validate_interpolation_places_on_custom_pattern(
            &interpolated_commit,
            branch_number_as_interpolate_value.len(),
            "{b}",
        )
        .unwrap();

        interpolate_on_custom_val(
            &interpolated_commit,
            branch_number_as_interpolate_value,
            "{b}",
        )
        .unwrap()
    } else {
        interpolated_commit
    };

    let run_mode = get_run_mode_from_options(DryRunAndCopyFlag {
        dry_run: options.flags.dry_run,
        copy: options.flags.copy,
    });

    return match run_mode {
        RunMode::Normal => {
            let cmd = Command::new("git")
                .arg("commit")
                .arg("-m")
                .arg(interpolated_commit)
                .output()
                .unwrap()
                .stdout;
            println!("{}", String::from_utf8_lossy(&cmd));

            Ok(())
        }
        RunMode::DryRun => {
            println!(
                "Going to run: \n \
        git commit -m \"{}\"",
                interpolated_commit
            );
            Ok(())
        }
        RunMode::DryRunAndCopy => {
            let copy_command = config.data.clipboard_commands.copy;
            println!(
                "Going to run: \n \
        echo 'git commit -m \"{}\"' > {}",
                interpolated_commit, copy_command
            );
            Ok(())
        }
        RunMode::Copy => run_copy(
            &config,
            format!("git commit -m \"{}\"", interpolated_commit),
        ),
    };
}