Skip to main content

git_policy_cli/
doctor_config.rs

1//! What:
2//!  What `git cli-git doctor` reports from the configuration: the external hooks Git configuration defines, and the
3//!  retired `hooks.concurrentCommits`, `indexLock.unprovenOwnerTimeoutMs` and `landing.reserveAfterLostRaces` keys.
4//! Why:
5//!  The wrapper runs no external hook, so a configured hook is a finding the author must act on, and each retired key in
6//!  `cli-git.config.jsonc` is accepted for older files but means nothing; doctor says both plainly.
7//!  The commit path prints the first as a warning from the snapshot it already holds; doctor lists it on request.
8//!
9//! In TS you'd write (pseudocode):
10//! ```ts
11//! const findings = await configFindings({ context, cwd }); // { hooks: [...], retiredHooksKey, retiredIndexLockKey, retiredLandingKey }
12//! ```
13
14use super::config_file::load_repository_config;
15use super::config_schema::ConcurrencyConfig;
16use super::config_snapshot::read_config_snapshot;
17use super::external_hooks::{ConfiguredHook, configured_hooks, describe_hook, explain_external_hooks};
18use super::transaction_git::{GitContext, GitRequest, run_git};
19use std::path::{Path, PathBuf};
20
21/// What:
22///  What the configuration contributes to the doctor report.
23/// Why:
24///   Standard output lists the hooks one per line, and standard error carries the explanation and the notes.
25///
26/// In TS you'd write (pseudocode):
27/// ```ts
28/// type ConfigFindings = { hooks: ConfiguredHook[]; retiredHooksKey: boolean;
29///   retiredIndexLockKey: boolean; retiredLandingKey: boolean };
30/// ```
31#[derive(Clone, Debug, Eq, PartialEq)]
32pub struct ConfigFindings {
33    /// The configured external hooks, none of which runs.
34    pub hooks: Vec<ConfiguredHook>,
35    /// Whether `cli-git.config.jsonc` has the retired `hooks` key.
36    pub retired_hooks_key: bool,
37    /// Whether `cli-git.config.jsonc` sets the retired `indexLock.unprovenOwnerTimeoutMs`.
38    pub retired_index_lock_key: bool,
39    /// Whether `cli-git.config.jsonc` sets the retired `landing.reserveAfterLostRaces`.
40    pub retired_landing_key: bool,
41}
42
43/// What:
44///  The worktree top level, when Git reports one.
45/// Why:
46///   The JSONC file sits at the top level; a bare repository has none and so no file.
47///
48/// In TS you'd write (pseudocode):
49/// ```ts
50/// await git(['rev-parse', '--show-toplevel']).catch(() => undefined)
51/// ```
52fn top_level(context: &GitContext, cwd: &Path) -> Option<PathBuf> {
53    let request: GitRequest = GitRequest::new(cwd, &["rev-parse", "--show-toplevel"]);
54    let output = run_git(context, &request).ok()?;
55    if !output.succeeded() {
56        return None;
57    }
58    let text: String = String::from_utf8(output.stdout).ok()?;
59    let trimmed: &str = text.trim_end_matches('\n');
60    if trimmed.is_empty() {
61        return None;
62    }
63    return Some(PathBuf::from(trimmed));
64}
65
66/// What:
67///  Read the configuration findings for `cwd`.
68/// Why:
69///   One snapshot read gives the hooks; one file read gives the retired key.
70///
71/// In TS you'd write (pseudocode):
72/// ```ts
73/// configFindings({ context, cwd });
74/// ```
75pub fn config_findings(context: &GitContext, cwd: &Path) -> ConfigFindings {
76    let hooks: Vec<ConfiguredHook> = read_config_snapshot(context, cwd)
77        .map(|snapshot| { return configured_hooks(&snapshot); })
78        .unwrap_or_default();
79    // A configuration that cannot be loaded is not doctor's to report: the command works beside a broken file, and
80    // every other command names the problem.
81    let concurrency: Option<ConcurrencyConfig> = top_level(context, cwd)
82        .and_then(|root| { return load_repository_config(root.as_path()).ok(); })
83        .map(|loaded| { return loaded.config.concurrency; });
84    return ConfigFindings {
85        hooks,
86        retired_hooks_key: concurrency.is_some_and(|retired| { return retired.hooks.present; }),
87        retired_index_lock_key: concurrency.is_some_and(|retired| { return retired.index_lock.present; }),
88        retired_landing_key: concurrency.is_some_and(|retired| { return retired.landing.present; }),
89    };
90}
91
92/// What:
93///  The lines for standard output: one per configured hook.
94/// Why:
95///   The hooks sit beside the traditional hooks in the report a script can read.
96///
97/// In TS you'd write (pseudocode):
98/// ```ts
99/// findings.hooks.map(describeHook).join('\n')
100/// ```
101pub fn render_hook_lines(findings: &ConfigFindings) -> String {
102    let mut text: String = String::new();
103    for hook in &findings.hooks {
104        text.push_str(describe_hook(hook).as_str());
105        text.push('\n');
106    }
107    return text;
108}
109
110/// What:
111///  The explanation and notes for standard error.
112/// Why:
113///   The ways forward for hooks, and a note for each retired key that is accepted and ignored, with the remedy of
114///  removing it.
115///
116/// In TS you'd write (pseudocode):
117/// ```ts
118/// renderNotes(findings);
119/// ```
120pub fn render_notes(findings: &ConfigFindings) -> String {
121    let mut text: String = String::new();
122    if !findings.hooks.is_empty() {
123        text.push_str("git cli-git doctor: ");
124        text.push_str(explain_external_hooks(findings.hooks.as_slice()).as_str());
125        text.push('\n');
126    }
127    if findings.retired_hooks_key {
128        text.push_str(
129            "git cli-git doctor: cli-git.config.jsonc has a `hooks` key (`hooks.concurrentCommits`). It is accepted so \
130             older files still load, and ignored: the wrapper runs no external hook, so there is no hook lock to \
131             tune. Delete the key when convenient.\n",
132        );
133    }
134    if findings.retired_index_lock_key {
135        text.push_str(
136            "git cli-git doctor: cli-git.config.jsonc sets `indexLock.unprovenOwnerTimeoutMs`. The key does nothing: the \
137             commit service waits for another process's `index.lock` for as long as `CLI_GIT_LOCK_WAIT_SECONDS` allows \
138             and reads no timeout from this file. Remove the key from cli-git.config.jsonc.\n",
139        );
140    }
141    if findings.retired_landing_key {
142        text.push_str(
143            "git cli-git doctor: cli-git.config.jsonc sets `landing.reserveAfterLostRaces`. The key does nothing: the \
144             commit service lands commits in the order they arrive and no longer reserves a landing slot for a commit \
145             that lost races. Remove the key from cli-git.config.jsonc.\n",
146        );
147    }
148    return text;
149}
150
151/// Reading the findings is controlled in the test module.
152#[cfg(test)]
153#[path = "doctor_config_tests.rs"]
154mod tests;