git-std 0.11.11

Standard git workflow — commits, versioning, hooks
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
//! `git std doctor` — single "show me everything" command.
//!
//! Three sections: **Status**, **Hooks**, **Configuration**.
//! Problems surface as hints at the bottom. Absorbs the old `config` command.

use std::path::Path;

use yansi::Paint;

use standard_githooks::{HookCommand, KNOWN_HOOKS, Prefix};

use crate::app::OutputFormat;
use crate::config::{self, ScopesConfig};
use crate::git::workdir;
use crate::ui;

// ── constants ────────────────────────────────────────────────────────────────

const CURRENT_VERSION: &str = env!("CARGO_PKG_VERSION");

/// Bump lifecycle hooks invoked directly by `git std bump` (not by git).
const LIFECYCLE_HOOKS: &[&str] = &["pre-bump", "post-version", "post-changelog", "post-bump"];

// ── Data model ───────────────────────────────────────────────────────────────

/// A collected hint to print after all sections.
struct Hint(String);

// ── Status section ────────────────────────────────────────────────────────────

struct ToolVersion {
    name: &'static str,
    version: Option<String>,
    /// Optional update notice: "update available: X.Y.Z"
    update_notice: Option<String>,
}

fn git_version() -> Option<String> {
    let out = std::process::Command::new("git")
        .args(["--version"])
        .output()
        .ok()?;
    if out.status.success() {
        let s = String::from_utf8_lossy(&out.stdout);
        // "git version 2.43.0" → "2.43.0"
        s.trim()
            .strip_prefix("git version ")
            .map(str::trim)
            .map(String::from)
    } else {
        None
    }
}

fn git_lfs_version() -> Option<String> {
    let out = std::process::Command::new("git")
        .args(["lfs", "version"])
        .output()
        .ok()?;
    if out.status.success() {
        let s = String::from_utf8_lossy(&out.stdout);
        // "git-lfs/3.4.1 ..." → "3.4.1"
        let first = s.split_whitespace().next()?;
        first
            .strip_prefix("git-lfs/")
            .map(str::trim)
            .map(String::from)
    } else {
        None
    }
}

fn has_lfs_in_gitattributes(root: &Path) -> bool {
    let path = root.join(".gitattributes");
    std::fs::read_to_string(path)
        .map(|c| c.lines().any(|l| l.contains("filter=lfs")))
        .unwrap_or(false)
}

fn read_update_cache() -> Option<String> {
    // Read cached latest version from XDG_CONFIG_HOME/git-std/update-check.json
    // (written by the background update check from PR #383).
    let base = std::env::var("XDG_CONFIG_HOME")
        .ok()
        .filter(|s| !s.is_empty())
        .or_else(|| std::env::var("HOME").ok().map(|h| format!("{h}/.config")))?;
    let path = std::path::PathBuf::from(base).join("git-std/update-check.json");
    let data = std::fs::read_to_string(path).ok()?;
    let val: serde_json::Value = serde_json::from_str(&data).ok()?;
    val.get("latest_version")?.as_str().map(String::from)
}

fn build_status_section(root: &Path) -> (Vec<ToolVersion>, Vec<Hint>) {
    let mut tools = Vec::new();
    let mut hints = Vec::new();

    // git
    let git_ver = git_version();
    if git_ver.is_none() {
        hints.push(Hint(
            "git not found — install from https://git-scm.com".to_owned(),
        ));
    }
    tools.push(ToolVersion {
        name: "git",
        version: git_ver,
        update_notice: None,
    });

    // git-lfs (only when .gitattributes has filter=lfs)
    if has_lfs_in_gitattributes(root) {
        let lfs_ver = git_lfs_version();
        if lfs_ver.is_none() {
            hints.push(Hint(
                "git-lfs not found — required by .gitattributes".to_owned(),
            ));
        }
        tools.push(ToolVersion {
            name: "git-lfs",
            version: lfs_ver,
            update_notice: None,
        });
    }

    // git-std with optional update notice
    let update_notice = read_update_cache().and_then(|latest| {
        let cur = semver::Version::parse(CURRENT_VERSION).ok()?;
        let lat = semver::Version::parse(&latest).ok()?;
        if lat > cur {
            Some(format!("update available: {latest}"))
        } else {
            None
        }
    });

    tools.push(ToolVersion {
        name: "git-std",
        version: Some(CURRENT_VERSION.to_owned()),
        update_notice,
    });

    (tools, hints)
}

// ── Hooks section ─────────────────────────────────────────────────────────────

/// A single hook entry for display.
struct HookEntry {
    name: &'static str,
    enabled: bool,
    commands: Vec<HookCommand>,
}

fn build_hooks_section(root: &Path) -> (Vec<HookEntry>, Vec<Hint>) {
    let hooks_dir = root.join(".githooks");
    let mut hints = Vec::new();

    // Health checks — only emit hints when something is wrong.
    if !hooks_dir.exists() {
        hints.push(Hint(".githooks/ not found — run 'git std init'".to_owned()));
    } else {
        // Check core.hooksPath is set to .githooks/
        let hooks_path = std::process::Command::new("git")
            .current_dir(root)
            .args(["config", "core.hooksPath"])
            .output()
            .ok()
            .and_then(|o| {
                if o.status.success() {
                    Some(String::from_utf8_lossy(&o.stdout).trim().to_owned())
                } else {
                    None
                }
            });
        match hooks_path.as_deref() {
            Some(".githooks") => {}
            Some(other) => hints.push(Hint(format!(
                "core.hooksPath is '{other}', expected '.githooks' — run 'git std init'"
            ))),
            None => hints.push(Hint(
                "core.hooksPath not configured — run 'git std init'".to_owned(),
            )),
        }

        // Check shim executability for enabled hooks.
        for hook_name in KNOWN_HOOKS {
            let shim = hooks_dir.join(hook_name);
            if shim.exists() {
                #[cfg(unix)]
                {
                    use std::os::unix::fs::PermissionsExt;
                    if let Ok(meta) = std::fs::metadata(&shim)
                        && meta.permissions().mode() & 0o111 == 0
                    {
                        hints.push(Hint(format!("{hook_name} shim is not executable")));
                    }
                }
            }
        }
    }

    // Build hook entries from all known git hooks + lifecycle hooks.
    let all_hooks: Vec<&str> = KNOWN_HOOKS
        .iter()
        .copied()
        .chain(LIFECYCLE_HOOKS.iter().copied())
        .collect();

    let entries = all_hooks
        .iter()
        .filter_map(|hook_name| {
            let template = hooks_dir.join(format!("{hook_name}.hooks"));
            if !template.exists() {
                return None; // skip hooks with no .hooks file
            }
            let content = std::fs::read_to_string(&template).unwrap_or_default();
            let commands = standard_githooks::parse(&content);
            // Lifecycle hooks have no shim — always show as n/a (not enabled/disabled).
            let is_lifecycle = LIFECYCLE_HOOKS.contains(hook_name);
            let enabled = !is_lifecycle && hooks_dir.join(hook_name).exists();
            Some(HookEntry {
                name: hook_name,
                enabled,
                commands,
            })
        })
        .collect();

    (entries, hints)
}

// ── Configuration section ─────────────────────────────────────────────────────

/// A single configuration row.
struct ConfigRow {
    key: &'static str,
    value: String,
    /// `true` = came from file (bold), `false` = default (plain/dim).
    from_file: bool,
}

fn build_config_section(root: &Path) -> (Vec<ConfigRow>, Vec<Hint>) {
    let mut hints = Vec::new();

    // Bootstrap health checks: .git-blame-ignore-revs and blame.ignoreRevsFile.
    let ignore_revs = root.join(".git-blame-ignore-revs");
    if !ignore_revs.exists() {
        hints.push(Hint(
            ".git-blame-ignore-revs not found — add to suppress noise in git blame".to_owned(),
        ));
    } else {
        let configured = std::process::Command::new("git")
            .current_dir(root)
            .args(["config", "blame.ignoreRevsFile"])
            .output()
            .ok()
            .and_then(|o| {
                if o.status.success() {
                    Some(String::from_utf8_lossy(&o.stdout).trim().to_owned())
                } else {
                    None
                }
            });
        if configured.as_deref() != Some(".git-blame-ignore-revs") {
            hints.push(Hint(
                "blame.ignoreRevsFile not set \
                 — run 'git config blame.ignoreRevsFile .git-blame-ignore-revs'"
                    .to_owned(),
            ));
        }
    }

    // Try to load config file to get both effective config and raw table.
    // We need to detect parse errors to show as hints.
    let config_path = root.join(".git-std.toml");
    let has_file = config_path.exists();
    if !has_file {
        hints.push(Hint(".git-std.toml not found — using defaults".to_owned()));
    }

    // Check for invalid TOML — if so, add a hint but still display defaults.
    // We parse the file ourselves to avoid load_with_raw emitting an eprintln!
    // warning to stderr (which would break JSON output).
    let mut toml_is_valid = true;
    if has_file {
        let content = std::fs::read_to_string(&config_path).unwrap_or_default();
        if let Err(e) = content.parse::<toml::Table>() {
            hints.push(Hint(format!(".git-std.toml invalid: {e}")));
            toml_is_valid = false;
        }
    }

    // When TOML is invalid we use defaults directly to avoid the warning
    // eprintln! inside load_with_raw/load. When valid (or absent) call normally.
    let (cfg, raw) = if toml_is_valid {
        config::load_with_raw(root)
    } else {
        // Return defaults without any stderr output.
        (config::ProjectConfig::default(), None)
    };
    let raw = raw.unwrap_or_default();

    let has_key = |key: &str| has_file && raw.contains_key(key);
    let has_versioning_key = |key: &str| {
        has_file
            && raw
                .get("versioning")
                .and_then(|v| v.as_table())
                .is_some_and(|t| t.contains_key(key))
    };
    let has_changelog_key = |key: &str| {
        has_file
            && raw
                .get("changelog")
                .and_then(|v| v.as_table())
                .is_some_and(|t| t.contains_key(key))
    };

    let default_cl = standard_changelog::ChangelogConfig::default();

    let scheme_label = match cfg.scheme {
        config::Scheme::Semver => "semver",
        config::Scheme::Calver => "calver",
        config::Scheme::Patch => "patch",
    };

    let scopes_value = match &cfg.scopes {
        ScopesConfig::None => "none".to_owned(),
        ScopesConfig::Auto => "auto".to_owned(),
        ScopesConfig::List(list) => format!("[{}]", list.len()),
    };

    let types_value = format!("[{}]", cfg.types.len());

    let hidden_value = {
        let h = cfg.changelog.hidden.as_ref().unwrap_or(&default_cl.hidden);
        format!("[{}]", h.len())
    };

    let rows = vec![
        ConfigRow {
            key: "scheme",
            value: scheme_label.to_owned(),
            from_file: has_key("scheme"),
        },
        ConfigRow {
            key: "strict",
            value: cfg.strict.to_string(),
            from_file: has_key("strict"),
        },
        ConfigRow {
            key: "scopes",
            value: scopes_value,
            from_file: has_key("scopes"),
        },
        ConfigRow {
            key: "tag_prefix",
            value: cfg.versioning.tag_prefix.clone(),
            from_file: has_versioning_key("tag_prefix"),
        },
        ConfigRow {
            key: "prerelease_tag",
            value: cfg.versioning.prerelease_tag.clone(),
            from_file: has_versioning_key("prerelease_tag"),
        },
        ConfigRow {
            key: "calver_format",
            value: cfg.versioning.calver_format.clone(),
            from_file: has_versioning_key("calver_format"),
        },
        ConfigRow {
            key: "types",
            value: types_value,
            from_file: has_key("types"),
        },
        ConfigRow {
            key: "hidden",
            value: hidden_value,
            from_file: has_changelog_key("hidden"),
        },
    ];

    (rows, hints)
}

// ── Entry point ───────────────────────────────────────────────────────────────

/// Run `git std doctor`. Returns the process exit code.
pub fn run(cwd: &Path, format: OutputFormat) -> i32 {
    let root = match workdir(cwd) {
        Ok(p) => p,
        Err(e) => {
            ui::error(&format!("not a git repository: {e}"));
            return 2;
        }
    };

    // Resolve the actual repo root for config/hooks (handles worktrees + subfolders).
    let repo_root = root.clone();

    let (status_tools, status_hints) = build_status_section(&repo_root);
    let (hooks, hooks_hints) = build_hooks_section(&repo_root);
    let (config_rows, config_hints) = build_config_section(&repo_root);

    // Collect all hints
    let mut all_hints: Vec<Hint> = Vec::new();
    all_hints.extend(status_hints);
    all_hints.extend(hooks_hints);
    all_hints.extend(config_hints);

    if format == OutputFormat::Json {
        return render_json(&status_tools, &hooks, &config_rows, &all_hints);
    }

    render_text(&status_tools, &hooks, &config_rows, &all_hints)
}

// ── Text rendering ────────────────────────────────────────────────────────────

fn render_text(
    tools: &[ToolVersion],
    hooks: &[HookEntry],
    config_rows: &[ConfigRow],
    hints: &[Hint],
) -> i32 {
    // Status section
    ui::blank();
    ui::info("Status");
    for tool in tools {
        match &tool.version {
            Some(ver) => {
                if let Some(notice) = &tool.update_notice {
                    ui::detail(&format!("{} {} ({})", tool.name, ver, notice));
                } else {
                    ui::detail(&format!("{} {}", tool.name, ver));
                }
            }
            None => {
                ui::detail(&format!("{} (not found)", tool.name));
            }
        }
    }

    // Hooks section
    if !hooks.is_empty() {
        ui::blank();
        ui::info("Hooks");
        for hook in hooks {
            let header = if hook.enabled {
                hook.name.to_owned()
            } else {
                format!("{} (disabled)", hook.name)
            };
            ui::detail(&header);
            for cmd in &hook.commands {
                let sigil = match cmd.prefix {
                    Prefix::FailFast => "!",
                    Prefix::Advisory => "?",
                    Prefix::Fix => "~",
                    Prefix::Default => " ",
                };
                ui::check_line(sigil, &cmd.command);
            }
        }
    }

    // Configuration section
    ui::blank();
    ui::info("Configuration");

    // Compute column alignment
    let key_width = config_rows.iter().map(|r| r.key.len()).max().unwrap_or(0);

    for row in config_rows {
        let key_padded = format!("{:<width$}", row.key, width = key_width);
        if row.from_file {
            // Bold for explicit file values
            ui::detail(&format!("{}   {}", key_padded.bold(), row.value.bold()));
        } else {
            // Dim for defaults
            ui::detail(&format!("{}   {}", key_padded.dim(), row.value.dim()));
        }
    }

    // Hints at the bottom
    if !hints.is_empty() {
        ui::blank();
        for hint in hints {
            ui::hint(&hint.0);
        }
    }

    if hints.is_empty() { 0 } else { 1 }
}

// ── JSON rendering ────────────────────────────────────────────────────────────

fn render_json(
    tools: &[ToolVersion],
    hooks: &[HookEntry],
    config_rows: &[ConfigRow],
    hints: &[Hint],
) -> i32 {
    let has_problems = !hints.is_empty();

    let status_json: Vec<serde_json::Value> = tools
        .iter()
        .map(|t| {
            let mut obj = serde_json::json!({
                "name": t.name,
            });
            if let Some(ref ver) = t.version {
                obj["version"] = serde_json::Value::String(ver.clone());
            }
            if let Some(ref notice) = t.update_notice {
                obj["update_notice"] = serde_json::Value::String(notice.clone());
            }
            obj
        })
        .collect();

    let hooks_json: Vec<serde_json::Value> = hooks
        .iter()
        .map(|h| {
            let commands_json: Vec<serde_json::Value> = h
                .commands
                .iter()
                .map(|c| {
                    serde_json::json!({
                        "command": c.command,
                        "sigil": match c.prefix {
                            Prefix::FailFast => "!",
                            Prefix::Advisory => "?",
                            Prefix::Fix => "~",
                            Prefix::Default => " ",
                        },
                    })
                })
                .collect();
            serde_json::json!({
                "name": h.name,
                "enabled": h.enabled,
                "commands": commands_json,
            })
        })
        .collect();

    let config_json: Vec<serde_json::Value> = config_rows
        .iter()
        .map(|r| {
            serde_json::json!({
                "key": r.key,
                "value": r.value,
                "source": if r.from_file { "file" } else { "default" },
            })
        })
        .collect();

    let hints_json: Vec<serde_json::Value> = hints
        .iter()
        .map(|h| serde_json::Value::String(h.0.clone()))
        .collect();

    let output = serde_json::json!({
        "status": if has_problems { "fail" } else { "pass" },
        "sections": {
            "status": status_json,
            "hooks": hooks_json,
            "configuration": config_json,
        },
        "hints": hints_json,
    });

    println!("{output}");
    if has_problems { 1 } else { 0 }
}

// ── Tests ─────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn build_status_shows_git_std_version() {
        // Verify git-std always appears with its version
        let dir = tempfile::tempdir().unwrap();
        let (tools, _hints) = build_status_section(dir.path());
        let git_std = tools.iter().find(|t| t.name == "git-std");
        assert!(git_std.is_some(), "git-std must appear in status");
        assert_eq!(git_std.unwrap().version.as_deref(), Some(CURRENT_VERSION));
    }

    #[test]
    fn build_status_skips_lfs_without_gitattributes() {
        let dir = tempfile::tempdir().unwrap();
        let (tools, hints) = build_status_section(dir.path());
        assert!(
            tools.iter().all(|t| t.name != "git-lfs"),
            "git-lfs should not appear without .gitattributes filter=lfs"
        );
        assert!(
            hints.iter().all(|h| !h.0.contains("lfs")),
            "no lfs hint without filter=lfs"
        );
    }

    #[test]
    fn build_status_includes_lfs_when_gitattributes_has_filter() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(dir.path().join(".gitattributes"), "*.bin filter=lfs\n").unwrap();
        let (tools, _hints) = build_status_section(dir.path());
        assert!(
            tools.iter().any(|t| t.name == "git-lfs"),
            "git-lfs should appear when .gitattributes has filter=lfs"
        );
    }

    #[test]
    fn build_hooks_section_shows_only_hooks_with_files() {
        let dir = tempfile::tempdir().unwrap();
        let hooks_dir = dir.path().join(".githooks");
        std::fs::create_dir_all(&hooks_dir).unwrap();
        std::fs::write(hooks_dir.join("pre-commit.hooks"), "! cargo fmt --check\n").unwrap();

        let (entries, _hints) = build_hooks_section(dir.path());
        assert_eq!(entries.len(), 1);
        assert_eq!(entries[0].name, "pre-commit");
        assert_eq!(entries[0].commands.len(), 1);
    }

    #[test]
    fn build_hooks_section_disabled_when_no_shim() {
        let dir = tempfile::tempdir().unwrap();
        let hooks_dir = dir.path().join(".githooks");
        std::fs::create_dir_all(&hooks_dir).unwrap();
        std::fs::write(hooks_dir.join("pre-commit.hooks"), "! cargo fmt\n").unwrap();
        // No shim file → disabled

        let (entries, _hints) = build_hooks_section(dir.path());
        assert!(!entries[0].enabled);
    }

    #[test]
    fn build_hooks_section_enabled_when_shim_exists() {
        let dir = tempfile::tempdir().unwrap();
        let hooks_dir = dir.path().join(".githooks");
        std::fs::create_dir_all(&hooks_dir).unwrap();
        std::fs::write(hooks_dir.join("pre-commit.hooks"), "! cargo fmt\n").unwrap();
        std::fs::write(hooks_dir.join("pre-commit"), "#!/bin/sh\n").unwrap();

        let (entries, _hints) = build_hooks_section(dir.path());
        assert!(entries[0].enabled);
    }

    #[test]
    fn build_config_section_defaults_have_from_file_false() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(dir.path().join(".git-std.toml"), "").unwrap();
        std::fs::write(dir.path().join(".git-blame-ignore-revs"), "").unwrap();
        let (rows, _hints) = build_config_section(dir.path());
        let scheme = rows.iter().find(|r| r.key == "scheme").unwrap();
        assert!(!scheme.from_file, "scheme should be default when no file");
        assert_eq!(scheme.value, "semver");
    }

    #[test]
    fn build_config_section_file_values_have_from_file_true() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(dir.path().join(".git-std.toml"), "scheme = \"calver\"\n").unwrap();
        let (rows, _hints) = build_config_section(dir.path());
        let scheme = rows.iter().find(|r| r.key == "scheme").unwrap();
        assert!(scheme.from_file, "scheme from file should be true");
        assert_eq!(scheme.value, "calver");
    }

    #[test]
    fn build_config_section_invalid_toml_produces_hint() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(dir.path().join(".git-std.toml"), "[[invalid\n").unwrap();
        let (_rows, hints) = build_config_section(dir.path());
        assert!(
            hints.iter().any(|h| h.0.contains(".git-std.toml invalid")),
            "should produce hint for invalid TOML"
        );
    }

    #[test]
    fn build_config_section_hint_when_toml_absent() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(dir.path().join(".git-blame-ignore-revs"), "").unwrap();
        let (_, hints) = build_config_section(dir.path());
        assert!(
            hints
                .iter()
                .any(|h| h.0.contains(".git-std.toml not found")),
            "should emit hint when .git-std.toml is absent"
        );
    }

    #[test]
    fn build_config_section_no_absent_hint_when_toml_present() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(dir.path().join(".git-std.toml"), "").unwrap();
        std::fs::write(dir.path().join(".git-blame-ignore-revs"), "").unwrap();
        let (_, hints) = build_config_section(dir.path());
        assert!(
            hints
                .iter()
                .all(|h| !h.0.contains(".git-std.toml not found")),
            "should not emit absent hint when .git-std.toml exists"
        );
    }

    #[test]
    fn build_config_section_hint_when_blame_ignore_revs_absent() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(dir.path().join(".git-std.toml"), "").unwrap();
        let (_, hints) = build_config_section(dir.path());
        assert!(
            hints
                .iter()
                .any(|h| h.0.contains(".git-blame-ignore-revs not found")),
            "should emit hint when .git-blame-ignore-revs is absent"
        );
    }

    #[test]
    fn build_config_section_hint_when_blame_config_missing() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(dir.path().join(".git-std.toml"), "").unwrap();
        std::fs::write(dir.path().join(".git-blame-ignore-revs"), "").unwrap();
        // blame.ignoreRevsFile is not configured (no git repo, so git config returns nothing)
        let (_, hints) = build_config_section(dir.path());
        assert!(
            hints
                .iter()
                .any(|h| h.0.contains("blame.ignoreRevsFile not set")),
            "should emit hint when blame.ignoreRevsFile is not configured"
        );
    }
}