commitbot 0.6.2

A CLI assistant that generates commit and PR messages from your diffs using LLMs.
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
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
use anyhow::{anyhow, Result};
use clap::Parser;
use commitbot::config::Config;
use commitbot::git::{
    collect_pr_items, current_branch, format_pr_commit_appendix, split_diff_by_file,
    staged_diff_for_file, staged_files, PrSummaryMode,
};
use commitbot::llm::LlmClient;
use commitbot::{Cli, Command, FileCategory, FileChange};
use crossterm::{
    cursor,
    event::{self, Event, KeyCode},
    execute,
    style::{self, Color},
    terminal::{self, Clear, ClearType},
};
use indicatif::{MultiProgress, ProgressBar, ProgressStyle};

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

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,
    }
}

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")
}

fn categorize_file_interactive(idx: usize, total: usize, path: &str) -> Result<FileCategory> {
    use FileCategory::*;

    let mut stdout = io::stdout();
    std::io::stderr()
        .flush()
        .map_err(|e| anyhow!("failed to flush stderr: {e}"))?;
    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 {
                    KeyCode::Up => {
                        if selected_index == 0 {
                            selected_index = labels.len() - 1;
                        } else {
                            selected_index -= 1;
                        }
                    }
                    KeyCode::Down => {
                        selected_index = (selected_index + 1) % labels.len();
                    }
                    KeyCode::Char('1') => return Ok(Main),
                    KeyCode::Char('2') => return Ok(Supporting),
                    KeyCode::Char('3') => return Ok(Consequence),
                    KeyCode::Char('4') => return Ok(Ignored),
                    KeyCode::Enter => {
                        let cat = match selected_index {
                            0 => Main,
                            1 => Supporting,
                            2 => Consequence,
                            3 => Ignored,
                            _ => Main,
                        };
                        return Ok(cat);
                    }
                    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,
}

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);
    let total_files = file_changes.len();

    let results: SummarizeResults = Arc::new(Mutex::new(Vec::new()));

    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...");
                }

                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)
                    })();

                    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));
                });
            }
        });
    }

    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(())
}

fn run_interactive(cli: &Cli, cfg: &Config, llm: &dyn LlmClient) -> Result<()> {
    let (branch, file_pairs) = if let Some(ref diff_arg) = cli.diff {
        let combined = if diff_arg == "-" {
            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)];
        }
        let branch = cli
            .branch
            .clone()
            .unwrap_or_else(|| current_branch().unwrap_or_else(|_| "unknown-branch".to_string()));
        (branch, 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();

    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,
        });
    }

    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()),
    );

    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");

    println!();

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

    println!();
    if let Some((p, c, t)) = llm.take_and_reset_usage() {
        println!("Token usage: prompt={}, completion={}, total={}", p, c, t);
    }

    Ok(())
}

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 {
            let combined = if diff_arg == "-" {
                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)];
            }
            let branch = cli.branch.clone().unwrap_or_else(|| {
                current_branch().unwrap_or_else(|_| "unknown-branch".to_string())
            });
            (branch, 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 ticket_summary = resolved_ticket_summary(cli);

    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())?;
    } else {
        let msg = llm.generate_commit_message(&branch, &file_changes, ticket_summary.as_deref())?;
        if msg.ends_with('\n') {
            print!("{msg}");
        } else {
            println!("{msg}");
        }
    }

    println!();
    if let Some((p, c, t)) = llm.take_and_reset_usage() {
        println!("Token usage: prompt={}, completion={}, total={}", p, c, t);
    }

    Ok(())
}

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(());
    }

    let mode = if pr_flag {
        PrSummaryMode::ByPrs
    } else if commit_flag {
        PrSummaryMode::ByCommits
    } else {
        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())?;

        if msg.ends_with('\n') {
            print!("{msg}");
        } else {
            println!("{msg}");
        }
        msg
    };

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

    Ok(())
}

fn main() -> Result<()> {
    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(());
    }

    commitbot::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)?;

    if cli.stage {
        commitbot::git::stage_all()?;
    }

    let boxed_client = commitbot::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())
            }
        }
    }
}