git-agent 0.5.0

Generate git commit messages from your changes with a language model
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
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
mod cli;
mod config;
mod fit;
mod git;
mod github;
mod menu;
mod models;
mod prompt;
mod providers;
mod redact;
mod secrets;
mod server;
mod ui;

use anyhow::{Context, Result, bail};
use clap::Parser;
use cli::{Cli, Command, ConfigCommand, GenerateArgs, PrArgs, SettingArgs};
use config::{Config, OnSecrets};
use fit::Kept;
use git::{CommitPatch, Repo, Scope};
use github::PullRequest;
use models::{ContextSource, Model, Pick};
use prompt::Fit;
use redact::{Change, Finding};
use server::{Message, ModelServer, Server};
use std::fs;
use std::io::{self, IsTerminal};
use std::process::ExitCode;
use ui::{Action, Choice, Review, thousands};

/// Accepting a commit message: the value says whether to push afterwards.
const COMMIT_REVIEW: Review<bool> = Review {
    heading: "Proposed commit message",
    first_line: "subject line",
    noun: "message",
    what: "commit message",
    accept: &[
        Choice {
            word: "commit",
            help: "commit with this message",
            value: false,
        },
        Choice {
            word: "push",
            help: "commit with this message, then push",
            value: true,
        },
    ],
    quit: "quit without committing",
};

/// Accepting a pull request: the value says whether it is a draft.
const PULL_REQUEST_REVIEW: Review<bool> = Review {
    heading: "Proposed pull request",
    first_line: "title",
    noun: "pull request",
    what: "pull request title and description",
    accept: &[
        Choice {
            word: "create",
            help: "push the branch if needed, then create the pull request",
            value: false,
        },
        Choice {
            word: "draft",
            help: "the same, as a draft pull request",
            value: true,
        },
    ],
    quit: "quit without creating it",
};

#[tokio::main(flavor = "current_thread")]
async fn main() -> ExitCode {
    let cli = Cli::parse();
    let result = match cli.command {
        None => generate(&cli.generate, &cli.settings).await,
        Some(Command::Pr(args)) => pull_request(&args, &cli.settings).await,
        Some(Command::Models) => list_models(&cli.settings).await,
        Some(Command::Config { command: None }) => menu::run(&cli.settings).await,
        Some(Command::Config {
            command: Some(ConfigCommand::Init { force }),
        }) => init_config(&cli.settings, force),
        Some(Command::Config {
            command: Some(ConfigCommand::Show),
        }) => show_config(&cli.settings),
    };
    match result {
        Ok(()) => ExitCode::SUCCESS,
        Err(err) => {
            ui::error(&err);
            ExitCode::FAILURE
        }
    }
}

async fn generate(args: &GenerateArgs, settings: &SettingArgs) -> Result<()> {
    const NOT_PUSHED: &str = "the commit was made, but not pushed";

    let config = Config::load(settings)?.config;
    let repo = Repo::open(".")?;
    if args.all {
        repo.stage_all()?;
    }

    let Some((scope, mut changes)) = repo.changes(&config.exclude)? else {
        match repo.untracked_count()? {
            0 => ui::info("✨ No changes to commit."),
            n => ui::info(format!(
                "No changes to tracked files, but {n} untracked path(s). Use --all to include them."
            )),
        }
        return Ok(());
    };

    let summary = changes.stat.lines().last().unwrap_or_default().trim();
    match scope {
        Scope::Staged => ui::info(format!("🔍 Staged changes: {summary}")),
        Scope::Tracked => ui::info(format!(
            "🔍 Nothing staged; using all changes to tracked files: {summary}\n   {}",
            ui::dim("Committing will include them (git commit --all).")
        )),
    }
    let untracked = repo.untracked_count()?;
    if untracked > 0 {
        ui::info(ui::dim(&format!(
            "   {untracked} untracked path(s) not included; use --all to include them."
        )));
    }
    let redacted = redact::diff(&changes.diff);
    changes.diff = redacted.text;
    if config.on_secrets == OnSecrets::Reject {
        reject_added(
            &redacted.findings,
            "the changes add what look like secrets, so git-agent didn't commit them",
            "Take them out of the changes.",
        )?;
    }
    report_secrets(
        &redacted.findings,
        "If they are real, take them out of the files before you commit.",
    );

    let (server, model) = connect(&config).await?;
    let (conversation, fit) = prompt::initial_messages(
        &config.system_prompt,
        &changes,
        args.hint.as_deref(),
        model.context_length,
        config.max_output_tokens,
    );
    report_fit(
        &model,
        &fit,
        "message",
        Some("commit in smaller parts\n   (stage some files, then run git agent)"),
    );
    let message = generate_message(&server, &model, &conversation).await?;

    if args.raw {
        println!("{message}");
        return Ok(());
    }
    ui::show_proposal(&COMMIT_REVIEW, &message);
    if args.commit {
        commit(&repo, &message, scope, &args.git_args)?;
        if args.push {
            check_outgoing(&repo, &config).context(NOT_PUSHED)?;
            push(&repo, false).context(NOT_PUSHED)?;
        }
        return Ok(());
    }
    if !io::stdin().is_terminal() {
        ui::info(ui::dim(
            "Input is not a terminal, so not asking for review. \
             Use --commit to commit directly (add --push to also push).",
        ));
        return Ok(());
    }

    let edit = |message: &str| ui::edit_message(&repo, message);
    let reviewed = review(
        &COMMIT_REVIEW,
        message,
        &server,
        &model,
        &conversation,
        edit,
    )
    .await?;
    let Some((and_push, message)) = reviewed else {
        ui::info("Nothing committed.");
        return Ok(());
    };
    commit(&repo, &message, scope, &args.git_args)?;
    if and_push || args.push {
        check_outgoing(&repo, &config).context(NOT_PUSHED)?;
        push(&repo, true).context(NOT_PUSHED)?;
    }
    Ok(())
}

async fn pull_request(args: &PrArgs, settings: &SettingArgs) -> Result<()> {
    let config = Config::load(settings)?.config;
    let repo = Repo::open(".")?;
    let branch = repo.current_branch()?;
    // Also finds out early whether gh is installed and logged in.
    if !args.raw
        && let Some(url) = github::open_pull_request(repo.workdir(), &branch)?
    {
        ui::info(format!("{branch} already has an open pull request: {url}"));
        return Ok(());
    }
    let base = match &args.base {
        Some(base) => base.clone(),
        None => github::default_branch(repo.workdir())?,
    };
    if branch == base {
        bail!(
            "{branch} is the branch the pull request would merge into; switch to the branch \
             with your changes, or create one with `git switch -c <name>`"
        );
    }

    let base_ref = repo.fetch_branch(&repo.remote()?, &base)?;
    let mut commits = repo.commits_since(&base_ref)?;
    if commits.is_empty() {
        ui::info(format!(
            "✨ {branch} has no commits that {base_ref} doesn't have, so there is nothing to \
             open a pull request for."
        ));
        return Ok(());
    }
    let mut changes = repo.branch_changes(&base_ref, &config.exclude)?;
    let summary = changes.stat.lines().last().unwrap_or_default().trim();
    ui::info(format!(
        "🔍 {} commit(s) on {branch} that {base_ref} doesn't have: {summary}",
        commits.len()
    ));
    if repo.has_uncommitted_changes()? {
        ui::info(ui::dim(
            "   Uncommitted changes are not part of the pull request; commit them to include them.",
        ));
    }

    if config.on_secrets == OnSecrets::Reject {
        // Commit by commit rather than the branch's overall diff: a secret added
        // in one commit and removed in a later one is still in the history a
        // push publishes.
        let patches = repo.commit_patches(&[&format!("{base_ref}..HEAD")], &config.exclude)?;
        reject_added(
            &scan_commits(&patches),
            "the branch's commits add what look like secrets, so git-agent won't open a pull \
             request for it",
            "Remove them from the branch's history, for example with `git rebase -i`, and \
             revoke any that were pushed already.",
        )?;
    }
    let redacted = redact::diff(&changes.diff);
    changes.diff = redacted.text;
    let mut findings = redacted.findings;
    for commit in &mut commits {
        let place = format!("commit {} message", commit.hash);
        let redacted = redact::text(&commit.message, &place);
        commit.message = redacted.text;
        findings.extend(redacted.findings);
    }
    report_secrets(
        &findings,
        "They are in the branch's commits. If they are real, remove them from its history \
         before you push it, and revoke any that were pushed already.",
    );

    let (server, model) = connect(&config).await?;
    let messages: Vec<String> = commits.into_iter().map(|commit| commit.message).collect();
    let (conversation, fit) = prompt::pull_request_messages(
        &messages,
        &changes,
        args.hint.as_deref(),
        model.context_length,
        config.max_output_tokens,
    );
    report_fit(&model, &fit, "pull request", None);
    let text = generate_message(&server, &model, &conversation).await?;

    if args.raw {
        println!("{text}");
        return Ok(());
    }
    ui::show_proposal(&PULL_REQUEST_REVIEW, &text);
    let (draft, text) = if args.create {
        (false, text)
    } else if !io::stdin().is_terminal() {
        ui::info(ui::dim(
            "Input is not a terminal, so not asking for review. \
             Use --create to create the pull request directly.",
        ));
        return Ok(());
    } else {
        let edit = |text: &str| ui::edit_pull_request(&repo, text);
        let reviewed = review(
            &PULL_REQUEST_REVIEW,
            text,
            &server,
            &model,
            &conversation,
            edit,
        )
        .await?;
        let Some(reviewed) = reviewed else {
            ui::info("No pull request created.");
            return Ok(());
        };
        reviewed
    };

    let (title, body) = prompt::split_title(&text);
    let pull_request = PullRequest {
        title: &title,
        body: &body,
        base: &base,
        head: &branch,
        draft: draft || args.draft,
        extra_args: &args.gh_args,
    };
    create_pull_request(&repo, &config, &pull_request, !args.create)
}

/// Connects to the configured server and picks the model to use.
async fn connect(config: &Config) -> Result<(Server, Model)> {
    let server = Server::connect(config)?;
    if config.temperature.is_some() && !server.takes_temperature() {
        ui::warn("Anthropic's API takes no temperature, so git-agent doesn't send the one you set");
    }
    let model = models::resolve(&server, config).await?;
    Ok((server, model))
}

/// Lets the user review `text` until they accept or quit it, revising it as
/// they ask. Returns the accept choice and the final text, or `None` on quit.
async fn review<T: Copy>(
    review: &Review<T>,
    mut text: String,
    server: &impl ModelServer,
    model: &Model,
    conversation: &[Message],
    edit: impl Fn(&str) -> Result<Option<String>>,
) -> Result<Option<(T, String)>> {
    loop {
        let revised = match ui::ask_action(review)? {
            Action::Accept(choice) => return Ok(Some((choice, text))),
            Action::Quit => return Ok(None),
            Action::Edit => edit(&text),
            Action::Regenerate => generate_message(server, model, conversation)
                .await
                .map(Some),
            Action::Refine(feedback) => {
                let refined = prompt::refine_messages(conversation, &text, &feedback, review.what);
                generate_message(server, model, &refined).await.map(Some)
            }
        };
        // A failed edit or generation keeps the current text so the review can go on.
        match revised {
            Ok(Some(revised)) => text = revised,
            Ok(None) => ui::warn(format!(
                "the edited {} was empty; keeping the previous one",
                review.noun
            )),
            Err(err) => ui::error(&err),
        }
        ui::show_proposal(review, &text);
    }
}

/// Pushes the branch if GitHub lacks some of its commits, then creates the
/// pull request. With `ask`, the user confirms a rebase before the push.
fn create_pull_request(
    repo: &Repo,
    config: &Config,
    pull_request: &PullRequest,
    ask: bool,
) -> Result<()> {
    const NOT_PUSHED: &str = "the branch was not pushed, so no pull request was created";
    if repo.has_unpushed_commits()? {
        check_outgoing(repo, config).context(NOT_PUSHED)?;
        ui::info(format!(
            "Pushing {} so GitHub has its commits.",
            pull_request.head
        ));
        let pushed = push(repo, ask).context(NOT_PUSHED)?;
        if !pushed {
            ui::info("No pull request created.");
            return Ok(());
        }
    }
    if let Err(err) = github::create_pull_request(repo.workdir(), pull_request) {
        // Like a failed commit, keep the text so it isn't lost.
        let path = repo.git_path("GIT_AGENT_PR.md")?;
        fs::write(&path, format!("{}\n", pull_request.body))
            .with_context(|| format!("failed to write {}", path.display()))?;
        bail!(
            "{err}; the description was saved, create the pull request with:\n  \
             gh pr create --base {} --title {} --body-file {}",
            pull_request.base,
            shell_quote(pull_request.title),
            path.display()
        );
    }
    ui::info(ui::green("✅ Pull request created."));
    Ok(())
}

/// `text` quoted for a POSIX shell.
fn shell_quote(text: &str) -> String {
    format!("'{}'", text.replace('\'', r"'\''"))
}

/// Lists the secrets that were replaced before sending, then gives `advice`.
fn report_secrets(findings: &[Finding], advice: &str) {
    if findings.is_empty() {
        return;
    }
    ui::warn(format!(
        "found {} possible secret(s); the model gets {} in their place:",
        findings.len(),
        redact::REDACTED
    ));
    ui::info(list_findings(&findings.iter().collect::<Vec<_>>()));
    ui::info(ui::dim(&format!("   {advice}")));
}

/// Fails if the change adds any of the secrets in `findings`, listing them
/// after `what` happened, then saying how to `fix` it.
fn reject_added(findings: &[Finding], what: &str, fix: &str) -> Result<()> {
    let added = added(findings);
    if added.is_empty() {
        return Ok(());
    }
    bail!(
        "{what}:\n{}\n{fix} If they aren't real secrets, add --on-secrets warn, or choose Warn \
         under Secrets in `git agent config`.",
        list_findings(&added)
    );
}

/// Checks the commits a push would send. If one adds what looks like a
/// secret, fails when `on_secrets` is reject and warns otherwise.
fn check_outgoing(repo: &Repo, config: &Config) -> Result<()> {
    let findings = scan_commits(&repo.outgoing_patches(&config.exclude)?);
    match config.on_secrets {
        OnSecrets::Reject => reject_added(
            &findings,
            "commits the push would send add what look like secrets",
            "Remove them from those commits, for example with `git rebase -i`.",
        ),
        OnSecrets::Warn => {
            let added = added(&findings);
            if !added.is_empty() {
                ui::warn(format!(
                    "pushing commits that add what look like secrets:\n{}",
                    list_findings(&added)
                ));
            }
            Ok(())
        }
    }
}

fn scan_commits(commits: &[CommitPatch]) -> Vec<Finding> {
    commits
        .iter()
        .flat_map(|commit| redact::commit(&commit.hash, &commit.message, &commit.patch))
        .collect()
}

/// The findings the change adds, rather than leaves alone or removes.
fn added(findings: &[Finding]) -> Vec<&Finding> {
    findings
        .iter()
        .filter(|finding| finding.change == Change::Added)
        .collect()
}

/// The first few findings, one per line.
fn list_findings(findings: &[&Finding]) -> String {
    const SHOWN: usize = 5;
    let mut lines: Vec<String> = findings
        .iter()
        .take(SHOWN)
        .map(|finding| {
            let place = match finding.line {
                Some(line) => format!("{}:{line}", finding.place),
                None => finding.place.clone(),
            };
            let change = match finding.change {
                Change::Added => "",
                Change::Unchanged => ", on an unchanged line",
                Change::Removed => ", on a removed line",
            };
            format!("   {place}  {}{change}", finding.kind)
        })
        .collect();
    if findings.len() > SHOWN {
        lines.push(format!("   … and {} more", findings.len() - SHOWN));
    }
    lines.join("\n")
}

/// Says which model gets the changes and, if they didn't fit its context, what
/// was left out. `noun` names what the model writes; `smaller` says how to
/// send less at a time, if there is a way.
fn report_fit(model: &Model, fit: &Fit, noun: &str, smaller: Option<&str>) {
    let chosen = match model.picked {
        Some(Pick::Loaded) => " (loaded on the server)",
        Some(Pick::Only) => " (the server's only model)",
        None => "",
    };
    ui::info(format!(
        "🤖 {}{chosen} {}",
        model.id,
        ui::dim(&format!(
            "· prompt about {} of {} context tokens",
            thousands(fit.prompt_tokens),
            thousands(model.context_length)
        ))
    ));
    if model.context_source == ContextSource::Assumed {
        ui::info(ui::dim(&format!(
            "   The server didn't report the model's context length, so git-agent assumes {} \
             tokens.\n   If the model has more, set `context_length` in the config file or pass \
             --context-length.",
            thousands(model.context_length)
        )));
    }
    if fit.is_complete() {
        if fit.context_trimmed {
            ui::info(ui::dim(
                "   To fit, git-agent left out the unchanged lines around the changes; \
                 every changed line was sent.",
            ));
        }
        return;
    }

    ui::warn(format!(
        "the changes need about {} tokens, more than fit in the context, so git-agent sent \
         part of them:",
        thousands(fit.full_tokens)
    ));
    let whole = fit.files.iter().filter(|file| file.is_whole()).count();
    let cut: Vec<String> = fit
        .files
        .iter()
        .filter(|file| file.kept == Kept::Start)
        .map(|file| {
            format!(
                "{} ({} of {} changed lines)",
                file.path, file.kept_changed_lines, file.changed_lines
            )
        })
        .collect();
    let dropped: Vec<String> = fit
        .files
        .iter()
        .filter(|file| file.is_dropped())
        .map(|file| file.path.clone())
        .collect();
    ui::info(format!(
        "   {whole} of {} files in full, without the unchanged lines around the changes",
        fit.files.len()
    ));
    if !cut.is_empty() {
        ui::info(format!("   {} cut short: {}", cut.len(), first_few(&cut)));
    }
    if !dropped.is_empty() {
        ui::info(format!(
            "   {} by name only: {}",
            dropped.len(),
            first_few(&dropped)
        ));
    }
    if fit.list_lines_left_out > 0 {
        ui::info(format!(
            "   {} files left out of the file list too",
            fit.list_lines_left_out
        ));
    }
    if fit.commit_bodies_left_out {
        ui::info("   the commits by their subjects only, without the rest of their messages");
    }
    if fit.commits_left_out > 0 {
        ui::info(format!(
            "   {} commits left out of the commit list",
            fit.commits_left_out
        ));
    }
    let larger_context = match model.context_source {
        ContextSource::Server => "load the model with a larger context length",
        ContextSource::Config => "raise `context_length` if the model allows more",
        ContextSource::Assumed => "set `context_length` if the model has more",
    };
    let ways = match smaller {
        Some(smaller) => format!("{smaller}, or {larger_context}"),
        None => larger_context.to_owned(),
    };
    ui::info(ui::dim(&format!(
        "   The {noun} may miss details from these files. To send more, {ways}."
    )));
}

/// The first three items, then how many more there are.
fn first_few(items: &[String]) -> String {
    const SHOWN: usize = 3;
    let mut text = items[..items.len().min(SHOWN)].join(", ");
    if items.len() > SHOWN {
        text.push_str(&format!(", and {} more", items.len() - SHOWN));
    }
    text
}

async fn generate_message(
    server: &impl ModelServer,
    model: &Model,
    conversation: &[Message],
) -> Result<String> {
    let reply = {
        let _spinner = ui::Spinner::start(format!("Generating with {}", model.id));
        server.complete(&model.id, conversation).await?
    };
    let message = prompt::clean_response(&reply);
    if message.is_empty() {
        bail!("the model returned an empty message");
    }
    Ok(message)
}

fn commit(repo: &Repo, message: &str, scope: Scope, git_args: &[String]) -> Result<()> {
    repo.commit(message, scope, git_args)?;
    ui::info(ui::green("✅ Committed."));
    Ok(())
}

/// Pushes, first rebasing onto commits that reached the remote from elsewhere
/// (e.g. edits made on GitHub). With `ask`, the user confirms the rebase.
/// Returns whether it pushed: the user may decline the rebase.
fn push(repo: &Repo, ask: bool) -> Result<bool> {
    const SHOWN: usize = 5;

    let incoming = repo.incoming_commits()?;
    if !incoming.is_empty() {
        ui::info(format!(
            "The remote has {} commit(s) you don't have yet:",
            incoming.len()
        ));
        for commit in incoming.iter().take(SHOWN) {
            ui::info(format!("   {commit}"));
        }
        if incoming.len() > SHOWN {
            ui::info(format!("   … and {} more", incoming.len() - SHOWN));
        }
        if ask && !ui::confirm("Rebase your commits on top of them and push?")? {
            ui::info("Not pushed. When you're ready, run `git pull --rebase` and `git push`.");
            return Ok(false);
        }
        repo.rebase_onto_upstream()?;
        ui::info("🔀 Rebased onto the remote commits.");
    }
    repo.push()?;
    ui::info(ui::green("✅ Pushed."));
    Ok(true)
}

async fn list_models(settings: &SettingArgs) -> Result<()> {
    let config = Config::load(settings)?.config;
    let models = Server::connect(&config)?.models().await?;
    let chosen = match &config.model {
        Some(model) => Some(model.clone()),
        None => models::choose(models.clone()).ok().map(|model| model.id),
    };
    let width = models.iter().map(|model| model.id.len()).max().unwrap_or(0);
    for model in &models {
        let marker = if chosen.as_ref() == Some(&model.id) {
            "*"
        } else {
            " "
        };
        let mut details = Vec::new();
        if model.loaded == Some(true) {
            details.push("loaded".to_owned());
        }
        if let Some(tokens) = model.context_length {
            details.push(format!("{} tokens of context", thousands(tokens)));
        }
        if details.is_empty() {
            println!("{marker} {}", model.id);
        } else {
            let details = ui::dim(&details.join(", "));
            println!("{marker} {:width$}  {details}", model.id);
        }
    }

    match (&config.model, chosen) {
        (Some(model), _) if !models.iter().any(|listed| listed.id == *model) => ui::warn(format!(
            "the configured model {model:?} is not in this list"
        )),
        (None, None) => ui::info(
            "No model is set, and git-agent can't tell which one to use: \
             none or several are loaded.",
        ),
        _ => {}
    }
    ui::info(ui::dim(
        "* marks the model git-agent uses: the one set with `git agent config`, --model or \
         GIT_AGENT_MODEL,\n  or else the one loaded on the server.",
    ));
    Ok(())
}

fn init_config(settings: &SettingArgs, force: bool) -> Result<()> {
    let path = config::resolve_path(settings)
        .context("could not find a config directory; set HOME or pass --config")?;
    if path.exists() && !force {
        bail!(
            "{} already exists; use --force to overwrite it",
            path.display()
        );
    }
    let mut config = Config::default();
    config.apply(settings);
    config.save(&path)?;
    ui::info(format!("Wrote {}", path.display()));
    Ok(())
}

fn show_config(settings: &SettingArgs) -> Result<()> {
    let loaded = Config::load(settings)?;
    let source = match (&loaded.path, loaded.file_found) {
        (Some(path), true) => format!("# Loaded from {}", path.display()),
        (Some(path), false) => format!("# No file at {}; using defaults", path.display()),
        (None, _) => "# No config location found; using defaults".to_owned(),
    };
    println!("{source}\n# Flags and GIT_AGENT_* environment variables are applied.");
    if loaded.config.model.is_none() {
        println!("# `model` is not set: git-agent uses the model loaded on the server.");
    }
    if loaded.config.context_length.is_none() {
        println!(
            "# `context_length` is not set: git-agent asks the server, or assumes {} tokens.",
            models::FALLBACK_CONTEXT_LENGTH
        );
    }
    let flag_key = settings
        .api_key
        .as_deref()
        .is_some_and(|key| !key.trim().is_empty());
    match (flag_key, loaded.config.api_key.is_some()) {
        (true, _) => println!("# API key: from --api-key or GIT_AGENT_API_KEY."),
        (false, true) => println!("# API key: saved in the macOS keychain."),
        (false, false) => println!("# API key: none."),
    }
    print!("\n{}", toml::to_string_pretty(&loaded.config)?);
    Ok(())
}