commitbot 0.6.0

A CLI assistant that generates commit and PR messages from your diffs using LLMs.
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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
mod cli_args;
mod config;
mod git;
mod llm;
mod logging;
mod setup;

use anyhow::{Result, anyhow};
use clap::Parser;
use config::Config;
use indicatif::{MultiProgress, ProgressBar, ProgressStyle};

use std::collections::HashSet;
use std::io::{self, Write};
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::Duration;

use crate::cli_args::{Cli, Command};
use crate::git::{
    PrSummaryMode, collect_pr_items, current_branch, format_pr_commit_appendix, split_diff_by_file,
    stage_all, staged_diff_for_file, staged_files,
};
use crate::llm::LlmClient;

use crossterm::{
    cursor,
    event::{self, Event, KeyCode},
    execute,
    style::{self, Color},
    terminal::{self, Clear, ClearType},
};

/// Re-export for modules like `config` that might refer to `crate::Cli` / `crate::Command`.
pub use cli_args::{Cli as RootCli, Command as RootCommand};

/// How the user categorizes each file in interactive mode.
#[derive(Debug, Clone, Copy, serde::Serialize)]
pub enum FileCategory {
    Main,        // 1
    Supporting,  // 2
    Consequence, // 3
    Ignored,     // 4
}

impl FileCategory {
    pub fn as_str(&self) -> &'static str {
        match self {
            FileCategory::Main => "main",
            FileCategory::Supporting => "supporting",
            FileCategory::Consequence => "consequence",
            FileCategory::Ignored => "ignored",
        }
    }
}

/// Represents a single staged file's change and metadata.
#[derive(Debug, Clone)]
pub struct FileChange {
    pub path: String,
    pub category: FileCategory,
    pub diff: String,
    pub summary: Option<String>, // Filled by per-file model call (or dummy)
}

/// Ask the user a question and return a trimmed input line.
fn prompt_input(prompt: &str) -> Result<String> {
    print!("{prompt}");
    io::stdout().flush()?;

    let mut buf = String::new();
    io::stdin().read_line(&mut buf)?;
    Ok(buf.trim().to_string())
}

fn resolved_ticket_summary(cli: &Cli) -> Option<String> {
    match &cli.command {
        Some(Command::Summary(words)) if !words.is_empty() => Some(words.join(" ")),
        _ => None,
    }
}

/// Helper: write a line in raw mode so we also reset the cursor to column 0.
fn tprintln<W: Write>(out: &mut W, s: &str) -> io::Result<()> {
    write!(out, "{}\r\n", s)
}

fn preview_snippet(text: &str) -> String {
    let trimmed = text.trim();
    let first_line = trimmed.lines().next().unwrap_or("");
    const MAX: usize = 80;
    if first_line.len() > MAX {
        format!("{}…", &first_line[..MAX])
    } else {
        first_line.to_string()
    }
}

fn dimmed(text: &str) -> String {
    format!("\x1b[2m{text}\x1b[0m")
}

/// Arrow-key UI for choosing a FileCategory (no diff preview).
fn categorize_file_interactive(idx: usize, total: usize, path: &str) -> Result<FileCategory> {
    use FileCategory::*;

    let mut stdout = io::stdout();
    terminal::enable_raw_mode().map_err(|e| anyhow!("failed to enable raw mode: {e}"))?;

    let res = (|| -> Result<FileCategory> {
        let labels = [
            "1) Main purpose",
            "2) Supporting change",
            "3) Consequence / ripple",
            "4) Ignore / unrelated cleanup",
        ];

        let mut selected_index: usize = 0;

        loop {
            execute!(stdout, Clear(ClearType::All), cursor::MoveTo(0, 0))?;

            tprintln(&mut stdout, &format!("[{} / {}] {}", idx + 1, total, path))?;
            tprintln(&mut stdout, "")?;
            tprintln(&mut stdout, "How does this file relate to the ticket?")?;
            tprintln(&mut stdout, "")?;

            for (i, label) in labels.iter().enumerate() {
                let color = if i == selected_index {
                    Color::White
                } else {
                    Color::DarkGrey
                };

                execute!(stdout, style::SetForegroundColor(color))?;
                tprintln(&mut stdout, &format!("  {}", label))?;
                execute!(stdout, style::ResetColor)?;
            }

            tprintln(&mut stdout, "")?;
            tprintln(
                &mut stdout,
                "Use ↑/↓ to move, Enter to select, or 1–4 as a shortcut.",
            )?;

            stdout.flush()?;

            let ev = event::read()?;
            if let Event::Key(key) = ev {
                match key.code {
                    // Arrow navigation
                    KeyCode::Up => {
                        if selected_index == 0 {
                            selected_index = labels.len() - 1;
                        } else {
                            selected_index -= 1;
                        }
                    }
                    KeyCode::Down => {
                        selected_index = (selected_index + 1) % labels.len();
                    }

                    // Numeric shortcuts
                    KeyCode::Char('1') => return Ok(Main),
                    KeyCode::Char('2') => return Ok(Supporting),
                    KeyCode::Char('3') => return Ok(Consequence),
                    KeyCode::Char('4') => return Ok(Ignored),

                    // Enter = accept current selection
                    KeyCode::Enter => {
                        let cat = match selected_index {
                            0 => Main,
                            1 => Supporting,
                            2 => Consequence,
                            3 => Ignored,
                            _ => Main,
                        };
                        return Ok(cat);
                    }

                    // Esc = abort
                    KeyCode::Esc => {
                        return Err(anyhow!("aborted by user"));
                    }

                    _ => {}
                }
            }
        }
    })();

    let _ = terminal::disable_raw_mode();
    res
}

type SummarizeResultInner = Vec<(usize, Result<String>)>;
type SummarizeResults = Arc<Mutex<SummarizeResultInner>>;

struct SummarizeContext<'a> {
    branch: &'a str,
    ticket_summary: Option<&'a str>,
    llm: &'a dyn LlmClient,
    max_concurrent_requests: usize,
}

/// Run per-file summaries concurrently, honoring `max_concurrent_requests`.
fn summarize_files_concurrently(
    file_changes: &mut [FileChange],
    indices: &[usize],
    ctx: &SummarizeContext<'_>,
    pb: &ProgressBar,
    file_lines: Option<&[ProgressBar]>,
) -> Result<()> {
    if indices.is_empty() {
        return Ok(());
    }

    let max_concurrent = ctx.max_concurrent_requests.max(1);

    // Store (file_index, result) for all summarizations.
    let results: SummarizeResults = Arc::new(Mutex::new(Vec::new()));

    // Process in chunks of `max_concurrent` so we never have more than that many
    // in-flight LLM calls at once.
    let total_files = file_changes.len();
    for chunk in indices.chunks(max_concurrent) {
        thread::scope(|scope| {
            for &file_idx in chunk {
                let results = Arc::clone(&results);
                let pb = pb.clone();
                let file_line = file_lines.and_then(|lines| lines.get(file_idx)).cloned();

                if let Some(line) = &file_line {
                    line.set_message("summarizing...");
                }

                // Clone just the data we need from this file so we don't share &mut across threads.
                let path = file_changes[file_idx].path.clone();
                let diff = file_changes[file_idx].diff.clone();
                let category = file_changes[file_idx].category;

                scope.spawn(move || {
                    log::debug!("Summarizing file: {}", path);

                    let res = (|| -> Result<String> {
                        let fc = FileChange {
                            path,
                            category,
                            diff,
                            summary: None,
                        };

                        let summary = ctx.llm.summarize_file(
                            ctx.branch,
                            &fc,
                            file_idx,
                            total_files,
                            ctx.ticket_summary,
                        )?;
                        Ok(summary)
                    })();

                    // Always advance the progress bar for this file, even if it errors.
                    pb.inc(1);

                    if let Some(line) = &file_line {
                        match &res {
                            Ok(summary) => {
                                let snippet = preview_snippet(summary);
                                line.finish_with_message(dimmed(&snippet));
                            }
                            Err(err) => {
                                line.finish_with_message(dimmed(&format!("error: {err}")));
                            }
                        }
                    }

                    let mut lock = results.lock().expect("results mutex poisoned");
                    lock.push((file_idx, res));
                });
            }
        });
    }

    // Unwrap Arc and Mutex and apply results back onto file_changes.
    let results = Arc::try_unwrap(results)
        .expect("results Arc still has multiple owners")
        .into_inner()
        .expect("results mutex poisoned");

    let mut first_err: Option<anyhow::Error> = None;

    for (idx, res) in results {
        match res {
            Ok(summary) => {
                file_changes[idx].summary = Some(summary);
            }
            Err(e) => {
                if first_err.is_none() {
                    first_err = Some(e);
                }
            }
        }
    }

    if let Some(err) = first_err {
        return Err(err);
    }

    Ok(())
}

/// Interactive mode: classify files, then do all LLM calls afterward (batched with concurrency).
/// Supports --diff for prompt testing (splits combined diff into per-file entries).
fn run_interactive(cli: &Cli, cfg: &Config, llm: &dyn LlmClient) -> Result<()> {
    // Build (branch, file_pairs) — works with both staged git and --diff.
    let (branch, file_pairs) = if let Some(ref diff_arg) = cli.diff {
        let combined = if diff_arg == "-" {
            use std::io::Read;
            let mut buf = String::new();
            io::stdin().read_to_string(&mut buf)?;
            buf
        } else {
            std::fs::read_to_string(diff_arg)
                .map_err(|e| anyhow!("Failed to read diff file '{}': {}", diff_arg, e))?
        };
        if combined.trim().is_empty() {
            println!("No diff content found.");
            return Ok(());
        }
        let mut per_file = split_diff_by_file(&combined);
        if per_file.is_empty() {
            per_file = vec![("(diff)".to_string(), combined)];
        }
        (cli.branch.clone(), per_file)
    } else {
        let branch = current_branch()?;
        let files = staged_files()?;
        if files.is_empty() {
            println!("No staged changes found.");
            return Ok(());
        }
        let mut pairs = Vec::new();
        for path in files {
            let diff = staged_diff_for_file(&path)?;
            pairs.push((path, diff));
        }
        (branch, pairs)
    };

    let mut ticket_summary = resolved_ticket_summary(cli);
    if ticket_summary.is_none() {
        let ans = prompt_input("Optional: brief ticket summary (enter to skip): ")?;
        if !ans.is_empty() {
            ticket_summary = Some(ans);
        }
    }

    let mut file_changes: Vec<FileChange> = Vec::new();

    // Phase 1: Interactive classification
    let total_files = file_pairs.len();
    for (idx, (path, diff)) in file_pairs.into_iter().enumerate() {
        let category = categorize_file_interactive(idx, total_files, &path)?;
        file_changes.push(FileChange {
            path,
            category,
            diff,
            summary: None,
        });
    }

    // Phase 2: LLM calls
    println!();
    println!("Asking {}...", cfg.model);

    let total = file_changes.len();
    let mp = MultiProgress::new();
    let mut file_lines = Vec::new();

    for fc in &file_changes {
        let line = mp.add(ProgressBar::new_spinner());
        line.set_style(
            ProgressStyle::with_template("{spinner:.cyan} {prefix:.bold}: {msg}")
                .expect("progress style template"),
        );
        line.set_prefix(fc.path.clone());
        if matches!(fc.category, FileCategory::Ignored) {
            line.finish_with_message(dimmed("ignored"));
        } else {
            line.enable_steady_tick(Duration::from_millis(120));
            line.set_message("waiting");
        }
        file_lines.push(line);
    }

    let pb = mp.add(ProgressBar::new((total + 1) as u64));
    pb.set_style(
        ProgressStyle::with_template("{wide_bar:.green} {pos}/{len} files")
            .unwrap_or_else(|_| ProgressStyle::default_bar()),
    );

    // Pre-increment for ignored files and collect indices that actually need summarization.
    let mut indices_to_summarize = Vec::new();
    let mut ignored_count = 0usize;

    for (idx, fc) in file_changes.iter().enumerate() {
        if matches!(fc.category, FileCategory::Ignored) {
            pb.inc(1);
            ignored_count += 1;
        } else {
            indices_to_summarize.push(idx);
        }
    }

    log::info!(
        "Summarizing {} files ({} ignored). max_concurrent_requests = {}",
        indices_to_summarize.len(),
        ignored_count,
        cfg.max_concurrent_requests,
    );

    let ctx = SummarizeContext {
        branch: &branch,
        ticket_summary: ticket_summary.as_deref(),
        llm,
        max_concurrent_requests: cfg.max_concurrent_requests,
    };

    summarize_files_concurrently(
        &mut file_changes,
        &indices_to_summarize,
        &ctx,
        &pb,
        Some(&file_lines),
    )?;

    pb.inc(1);
    pb.finish_with_message("Done");

    // Final commit message
    println!();

    if cfg.stream {
        let _msg =
            llm.generate_commit_message(&branch, &file_changes, ticket_summary.as_deref())?;
        println!();
    } else {
        let msg = llm.generate_commit_message(&branch, &file_changes, ticket_summary.as_deref())?;
        println!("{msg}");
    }

    Ok(())
}

/// Auto mode (default): per-file LLM summaries then final commit message — no human classification.
/// When `--diff` is supplied the combined diff is split by file; otherwise staged files are used.
fn run_auto(cli: &Cli, cfg: &Config, llm: &dyn LlmClient) -> Result<()> {
    let using_external_diff = cli.diff.is_some();
    let (branch, file_pairs): (String, Vec<(String, String)>) = if let Some(ref diff_arg) = cli.diff
    {
        // Read the combined diff from file or stdin.
        let combined = if diff_arg == "-" {
            use std::io::Read;
            let mut buf = String::new();
            io::stdin().read_to_string(&mut buf)?;
            buf
        } else {
            std::fs::read_to_string(diff_arg)
                .map_err(|e| anyhow!("Failed to read diff file '{}': {}", diff_arg, e))?
        };

        if combined.trim().is_empty() {
            println!("No diff content found.");
            return Ok(());
        }

        let mut per_file = split_diff_by_file(&combined);
        if per_file.is_empty() {
            per_file = vec![("(diff)".to_string(), combined)];
        }
        (cli.branch.clone(), per_file)
    } else {
        // Normal git mode: get per-file staged diffs.
        let branch = current_branch()?;
        let files = staged_files()?;
        if files.is_empty() {
            println!("No staged changes found.");
            return Ok(());
        }
        let mut pairs = Vec::new();
        for path in files {
            let diff = staged_diff_for_file(&path)?;
            pairs.push((path, diff));
        }
        (branch, pairs)
    };

    let ticket_summary = resolved_ticket_summary(cli);

    // Build FileChange list — all files are categorized as Main automatically.
    let mut file_changes: Vec<FileChange> = file_pairs
        .into_iter()
        .map(|(path, diff)| FileChange {
            path,
            category: FileCategory::Main,
            diff,
            summary: None,
        })
        .collect();

    println!();
    println!("Asking {}...", cfg.model);
    if let Some(ref diff_arg) = cli.diff {
        let diff_source = if diff_arg == "-" { "stdin" } else { diff_arg };
        println!("Using external diff: {diff_source}");
    }

    let total = file_changes.len();
    let mp = MultiProgress::new();
    let mut file_lines = Vec::new();

    for fc in &file_changes {
        let line = mp.add(ProgressBar::new_spinner());
        line.set_style(
            ProgressStyle::with_template("{spinner:.cyan} {prefix:.bold}: {msg}")
                .expect("progress style template"),
        );
        let prefix = if using_external_diff {
            format!("[diff] {}", fc.path)
        } else {
            fc.path.clone()
        };
        line.set_prefix(prefix);
        line.enable_steady_tick(Duration::from_millis(120));
        line.set_message("waiting");
        file_lines.push(line);
    }

    let pb = mp.add(ProgressBar::new((total + 1) as u64));
    pb.set_style(
        ProgressStyle::with_template("{wide_bar:.green} {pos}/{len} files")
            .unwrap_or_else(|_| ProgressStyle::default_bar()),
    );

    let indices_to_summarize: Vec<usize> = (0..total).collect();

    log::info!(
        "Auto-summarizing {} files. max_concurrent_requests = {}",
        total,
        cfg.max_concurrent_requests,
    );

    let ctx = SummarizeContext {
        branch: &branch,
        ticket_summary: ticket_summary.as_deref(),
        llm,
        max_concurrent_requests: cfg.max_concurrent_requests,
    };

    summarize_files_concurrently(
        &mut file_changes,
        &indices_to_summarize,
        &ctx,
        &pb,
        Some(&file_lines),
    )?;

    pb.inc(1);
    pb.finish_with_message("Done");

    println!();

    if cfg.stream {
        let _msg =
            llm.generate_commit_message(&branch, &file_changes, ticket_summary.as_deref())?;
        println!();
    } else {
        let msg = llm.generate_commit_message(&branch, &file_changes, ticket_summary.as_deref())?;
        println!("{msg}");
    }

    Ok(())
}

/// PR mode: summarize commits/PRs between base..from into a PR description.
fn run_pr(
    cli: &Cli,
    cfg: &Config,
    llm: &dyn LlmClient,
    base: &str,
    from_opt: Option<&str>,
    pr_flag: bool,
    commit_flag: bool,
) -> Result<()> {
    let from_branch = match from_opt {
        Some(name) => name.to_string(),
        None => current_branch()?,
    };

    let items = collect_pr_items(base, &from_branch)?;
    if items.is_empty() {
        println!("No commits found between {base} and {from_branch}.");
        return Ok(());
    }

    // Determine mode
    let mode = if pr_flag {
        PrSummaryMode::ByPrs
    } else if commit_flag {
        PrSummaryMode::ByCommits
    } else {
        // Auto-detect: if multiple distinct PR numbers are present, use PR mode.
        let mut distinct_prs: HashSet<u32> = HashSet::new();
        for item in &items {
            if let Some(n) = item.pr_number {
                distinct_prs.insert(n);
            }
        }
        if distinct_prs.len() >= 2 {
            PrSummaryMode::ByPrs
        } else {
            PrSummaryMode::ByCommits
        }
    };

    log::info!(
        "PR mode: base={base}, from={from}, mode={mode}",
        base = base,
        from = from_branch,
        mode = mode.as_str()
    );
    log::info!("Found {} commits in range.", items.len());

    let ticket_summary = resolved_ticket_summary(cli);
    let _pr_message = if cfg.stream {
        println!();
        let msg =
            llm.generate_pr_message(base, &from_branch, mode, &items, ticket_summary.as_deref())?;
        println!();
        msg
    } else {
        println!();
        let msg =
            llm.generate_pr_message(base, &from_branch, mode, &items, ticket_summary.as_deref())?;

        println!("{msg}");
        msg
    };

    let appendix = format_pr_commit_appendix(&items);
    if !appendix.is_empty() {
        println!();
        println!("{appendix}");
    }

    Ok(())
}

fn main() -> Result<()> {
    // CLI + config
    let cli = Cli::parse();

    // Handle custom --version early so we print just the version number.
    if cli.version {
        println!("{}", env!("CARGO_PKG_VERSION"));
        return Ok(());
    }

    // Initialize logging
    logging::init_logger(cli.verbose);

    if cli.diff.is_some() && matches!(&cli.command, Some(Command::Pr { .. })) {
        return Err(anyhow!(
            "The --diff flag cannot be used with the 'pr' command.\n\
             PR mode analyzes commit history, not staged diffs."
        ));
    }

    let cfg = Config::from_sources(&cli);

    // Pre-Work Items
    if cli.stage {
        stage_all()?;
    }

    // LLM client setup
    let boxed_client = setup::build_llm_client(&cfg);
    boxed_client.validate_model()?;

    match &cli.command {
        Some(Command::Pr {
            base,
            from,
            pr_mode,
            commit_mode,
        }) => run_pr(
            &cli,
            &cfg,
            boxed_client.as_ref(),
            base.as_str(),
            from.as_deref(),
            *pr_mode,
            *commit_mode,
        ),
        Some(Command::Summary(_)) | None => {
            if cli.ask {
                run_interactive(&cli, &cfg, boxed_client.as_ref())
            } else {
                run_auto(&cli, &cfg, boxed_client.as_ref())
            }
        }
    }
}