commitcraft 1.1.0

A fast, intelligent CLI tool that generates conventional commit messages using AI
Documentation
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
use clap::Parser;
use colored::*;
use question::{Answer, Question};
use rustyline::DefaultEditor;
use spinners::{Spinner, Spinners};
use std::process::Command;

use commitcraft::{cli, config, git, providers};

use cli::{Cli, Commands};
use providers::{
    anthropic::AnthropicProvider, gemini::GeminiProvider, openai::OpenAIProvider, AIProvider,
};

fn show_welcome() {
    println!(
        "{}",
        r#"
 ██████╗ ██████╗ ███╗   ███╗███╗   ███╗██╗████████╗ ██████╗██████╗  █████╗ ███████╗████████╗
██╔════╝██╔═══██╗████╗ ████║████╗ ████║██║╚══██╔══╝██╔════╝██╔══██╗██╔══██╗██╔════╝╚══██╔══╝
██║     ██║   ██║██╔████╔██║██╔████╔██║██║   ██║   ██║     ██████╔╝███████║█████╗     ██║   
██║     ██║   ██║██║╚██╔╝██║██║╚██╔╝██║██║   ██║   ██║     ██╔══██╗██╔══██║██╔══╝     ██║   
╚██████╗╚██████╔╝██║ ╚═╝ ██║██║ ╚═╝ ██║██║   ██║   ╚██████╗██║  ██║██║  ██║██║        ██║   
 ╚═════╝ ╚═════╝ ╚═╝     ╚═╝╚═╝     ╚═╝╚═╝   ╚═╝    ╚═════╝╚═╝  ╚═╝╚═╝  ╚═╝╚═╝        ╚═╝   
"#
        .purple()
        .bold()
    );
    println!(
        "{}",
        "🎨 AI-Powered Conventional Commit Messages".cyan().bold()
    );
    println!("{}", "Fast • Intelligent • Developer-Friendly".dimmed());
    println!();
}

#[tokio::main]
async fn main() {
    // Initialize logger
    env_logger::init();

    let cli_args = Cli::parse();

    // Show welcome banner for main functionality (not for subcommands)
    if cli_args.command.is_none() {
        show_welcome();
    }

    // Handle commands
    match cli_args.command {
        Some(Commands::Setup) => {
            if let Err(e) = config::run_setup() {
                eprintln!("{} {}", "Error during setup:".red().bold(), e);
                std::process::exit(1);
            }
            return;
        }
        Some(Commands::Config) => {
            show_config();
            return;
        }
        Some(Commands::List) => {
            list_providers_and_models();
            return;
        }
        None => {}
    }

    // Load configuration
    let config = match config::load_config() {
        Ok(c) => c,
        Err(_) => {
            // Error message is already printed by load_config
            std::process::exit(1);
        }
    };

    // Check if in a git repository
    if !git::is_git_repository() {
        eprintln!("{}", "Error: Not inside a git repository.".red().bold());
        std::process::exit(1);
    }

    // Get staged diff
    let diff = match git::get_staged_diff() {
        Ok(d) => d,
        Err(e) => {
            eprintln!("{} {}", "Error:".red().bold(), e);
            std::process::exit(1);
        }
    };

    // Show verbose output if requested
    if cli_args.verbose {
        println!("{}", "Analyzing the following diff:".bold());
        println!("{}", "".repeat(50));
        println!("{}", diff.dimmed());
        println!("{}", "".repeat(50));
    }

    // Get file context if requested
    let file_context = if cli_args.include_files {
        match git::get_staged_files() {
            Ok(files) => Some(format!("Files modified: {}", files.join(", "))),
            Err(_) => None,
        }
    } else {
        None
    };

    // Get repository context
    let repo_context = match git::get_repo_info() {
        Ok((repo_name, branch)) => Some(format!("Repository: {} (branch: {})", repo_name, branch)),
        Err(_) => None,
    };

    // Enhance diff with context
    let enhanced_diff = if file_context.is_some() || repo_context.is_some() {
        let mut context = Vec::new();
        if let Some(repo) = repo_context {
            context.push(repo);
        }
        if let Some(files) = file_context {
            context.push(files);
        }
        format!("{}\n\n{}", context.join("\n"), diff)
    } else {
        diff
    };

    // Determine provider and model
    let provider_name = cli_args
        .provider
        .or(config.default_provider)
        .unwrap_or_else(|| "gemini".to_string());

    let model_name_or_alias = cli_args.model.unwrap_or_else(|| {
        match provider_name.as_str() {
            "openai" => config.models.openai,
            "gemini" => config.models.gemini,
            "anthropic" => config.models.anthropic,
            _ => None,
        }
        .unwrap_or_else(|| "default".to_string())
    });

    let model_name = config
        .aliases
        .get(&model_name_or_alias)
        .unwrap_or(&model_name_or_alias);

    // Get API key for the selected provider
    let api_key = match provider_name.as_str() {
        "openai" => config.api_keys.openai,
        "gemini" => config.api_keys.gemini,
        "anthropic" => config.api_keys.anthropic,
        _ => None,
    }
    .ok_or_else(|| {
        format!(
            "API key for provider '{}' not found. Please run 'commitcraft setup'.",
            provider_name
        )
    })
    .unwrap_or_else(|e| {
        eprintln!("{} {}", "Configuration Error:".red().bold(), e);
        std::process::exit(1);
    });

    // Instantiate the provider
    let provider: Box<dyn AIProvider> = match provider_name.as_str() {
        "openai" => Box::new(OpenAIProvider::new(api_key, model_name.to_string())),
        "gemini" => Box::new(GeminiProvider::new(api_key, model_name.to_string())),
        "anthropic" => Box::new(AnthropicProvider::new(api_key, model_name.to_string())),
        _ => {
            eprintln!(
                "{} Unknown provider '{}'",
                "Error:".red().bold(),
                provider_name
            );
            std::process::exit(1);
        }
    };

    println!(
        "Using provider: {} ({})",
        provider_name.cyan(),
        model_name.cyan()
    );

    let mut sp = Spinner::new(Spinners::Dots, "Generating commit message...".into());

    let commit_msg = match provider.generate_commit_message(&enhanced_diff).await {
        Ok(msg) => {
            sp.stop_with_message("✓ Message generated successfully!".into());

            // Validate the generated commit message
            if let Err(validation_error) = msg.validate() {
                eprintln!("{} {}", "Warning:".yellow().bold(), validation_error);
                eprintln!(
                    "The generated message may not follow conventional commits format exactly."
                );
            }

            msg
        }
        Err(e) => {
            sp.stop_with_message("✗ Error generating message.".into());
            eprintln!("{} {}", "API Error:".red().bold(), e);
            std::process::exit(1);
        }
    };

    let commit_str = commit_msg.to_string();

    // Handle different modes
    if cli_args.dry_run {
        println!(
            "\n{}\n---\n{}\n---",
            "Generated Commit Message:".bold(),
            commit_str.green()
        );
        return;
    }

    if cli_args.show_command {
        let git_command = format_git_command(&commit_str, cli_args.review);
        println!("\n{}", "Generated git command:".bold());
        println!("{}", git_command.cyan());
        return;
    }

    if cli_args.legacy {
        // Use the old confirmation-based flow
        legacy_commit_flow(&commit_str, cli_args.force, cli_args.review);
        return;
    }

    if cli_args.yes {
        // Skip interactive editing, commit immediately
        if let Err(e) = execute_git_commit(&commit_str, cli_args.review) {
            eprintln!("{} {}", "Error during commit:".red().bold(), e);
            std::process::exit(1);
        }
        return;
    }

    // Default: Interactive command editing
    interactive_commit_flow(&commit_str, cli_args.review);
}

/// Format the git commit command with proper escaping
fn format_git_command(message: &str, review: bool) -> String {
    let review_flag = if review { " -e" } else { "" };

    // For multi-line messages, we need to handle them properly
    if message.contains('\n') {
        // Use heredoc-style for multi-line messages
        format!("git commit{} -F- <<'EOF'\n{}\nEOF", review_flag, message)
    } else {
        // Simple single-line message
        format!(
            "git commit{} -m \"{}\"",
            review_flag,
            message.replace('"', "\\\"")
        )
    }
}

/// Interactive command editing flow (new default)
fn interactive_commit_flow(commit_message: &str, review: bool) {
    println!("\n{}", "📝 Generated commit message:".bold());
    println!("{}", "".repeat(50));
    println!("{}", commit_message.green());
    println!("{}", "".repeat(50));

    let git_command = if commit_message.contains('\n') {
        // For multi-line, show a simplified version for editing
        let title = commit_message.lines().next().unwrap_or(commit_message);
        format!(
            "git commit{} -m \"{}\"",
            if review { " -e" } else { "" },
            title
        )
    } else {
        format_git_command(commit_message, review)
    };

    println!(
        "\n{}",
        "Edit the command below (or press Enter to execute):".bold()
    );

    let mut rl = match DefaultEditor::new() {
        Ok(editor) => editor,
        Err(e) => {
            eprintln!(
                "{} Failed to create interactive editor: {}",
                "Error:".red().bold(),
                e
            );
            println!("Falling back to legacy mode...");
            legacy_commit_flow(commit_message, false, review);
            return;
        }
    };

    match rl.readline_with_initial("$ ", (&git_command, "")) {
        Ok(edited_command) => {
            let edited_command = edited_command.trim();
            if edited_command.is_empty() {
                println!("{}", "Commit cancelled.".yellow());
                return;
            }

            // Execute the edited command
            println!("\n{} {}", "Executing:".bold(), edited_command.cyan());
            match execute_shell_command(edited_command) {
                Ok(output) => {
                    println!("{}", "✓ Commit successful!".green().bold());
                    if !output.trim().is_empty() {
                        println!("{}", output);
                    }
                }
                Err(e) => {
                    eprintln!("{} {}", "Error:".red().bold(), e);
                    std::process::exit(1);
                }
            }
        }
        Err(rustyline::error::ReadlineError::Interrupted) => {
            println!("{}", "Commit cancelled.".yellow());
        }
        Err(e) => {
            eprintln!("{} Failed to read input: {}", "Error:".red().bold(), e);
            std::process::exit(1);
        }
    }
}

/// Execute a shell command and return output
fn execute_shell_command(command: &str) -> Result<String, String> {
    let output = Command::new("sh")
        .arg("-c")
        .arg(command)
        .output()
        .map_err(|e| format!("Failed to execute command: {}", e))?;

    if output.status.success() {
        Ok(String::from_utf8_lossy(&output.stdout).to_string())
    } else {
        Err(String::from_utf8_lossy(&output.stderr).to_string())
    }
}

/// Legacy commit flow (old behavior)
fn legacy_commit_flow(commit_message: &str, force: bool, review: bool) {
    println!(
        "\n{}\n---\n{}\n---",
        "Proposed Commit:".bold(),
        commit_message.green()
    );

    if !force {
        let answer = Question::new("Do you want to commit with this message? (Y/n)")
            .yes_no()
            .default(Answer::YES)
            .ask()
            .expect("Couldn't ask question.");

        if answer == Answer::NO {
            println!("{}", "Commit aborted by user.".yellow());
            std::process::exit(0);
        }
    }

    if let Err(e) = execute_git_commit(commit_message, review) {
        eprintln!("{} {}", "Error during commit:".red().bold(), e);
        std::process::exit(1);
    }
}

/// Execute git commit with the given message
fn execute_git_commit(message: &str, review: bool) -> Result<(), String> {
    git::commit(message, review)
}

fn show_config() {
    match config::load_config() {
        Ok(config) => {
            println!("{}", "📋 Current Configuration".bold().cyan());
            println!("{}", "".repeat(50));

            // Provider info
            if let Some(provider) = &config.default_provider {
                println!("🤖 Default Provider: {}", provider.green());
            } else {
                println!("🤖 Default Provider: {}", "Not set".yellow());
            }

            // API keys (masked)
            println!("\n🔑 API Keys:");
            println!(
                "  OpenAI:    {}",
                if config.api_keys.openai.is_some() {
                    "✓ Configured".green()
                } else {
                    "✗ Not set".red()
                }
            );
            println!(
                "  Gemini:    {}",
                if config.api_keys.gemini.is_some() {
                    "✓ Configured".green()
                } else {
                    "✗ Not set".red()
                }
            );
            println!(
                "  Anthropic: {}",
                if config.api_keys.anthropic.is_some() {
                    "✓ Configured".green()
                } else {
                    "✗ Not set".red()
                }
            );

            // Models
            println!("\n🎯 Default Models:");
            if let Some(model) = &config.models.openai {
                println!("  OpenAI:    {}", model.cyan());
            }
            if let Some(model) = &config.models.gemini {
                println!("  Gemini:    {}", model.cyan());
            }
            if let Some(model) = &config.models.anthropic {
                println!("  Anthropic: {}", model.cyan());
            }

            // Aliases
            if !config.aliases.is_empty() {
                println!("\n🏷️  Model Aliases:");
                for (alias, model) in &config.aliases {
                    println!("  {}{}", alias.yellow(), model.cyan());
                }
            }

            println!("\n💡 Run '{}' to reconfigure", "commitcraft setup".bold());
        }
        Err(e) => {
            eprintln!("{} {}", "Error loading config:".red().bold(), e);
            println!(
                "Run '{}' to set up configuration.",
                "commitcraft setup".bold().cyan()
            );
        }
    }
}

fn list_providers_and_models() {
    println!("{}", "🤖 Available Providers & Models".bold().cyan());
    println!("{}", "".repeat(50));

    println!("\n{}:", "OpenAI".bold().green());
    println!("  • gpt-4o (latest, most capable)");
    println!("  • gpt-4o-mini (fast and efficient)");
    println!("  • gpt-4-turbo");
    println!("  • gpt-3.5-turbo");

    println!("\n{}:", "Google Gemini".bold().blue());
    println!("  • gemini-1.5-pro-latest (most capable)");
    println!("  • gemini-1.5-flash-latest (fast, default)");
    println!("  • gemini-1.0-pro");

    println!("\n{}:", "Anthropic Claude".bold().purple());
    println!("  • claude-3-5-sonnet-20241022 (latest, most capable)");
    println!("  • claude-3-haiku-20240307 (fast, default)");
    println!("  • claude-3-opus-20240229 (most powerful)");

    println!("\n{}:", "Usage Examples".bold().yellow());
    println!("  commitcraft --provider openai --model gpt-4o");
    println!("  commitcraft --provider gemini --model gemini-1.5-pro-latest");
    println!("  commitcraft --provider anthropic --model claude-3-5-sonnet-20241022");

    println!("\n💡 Set up aliases with '{}'", "commitcraft setup".bold());
}