1use super::json_record::quote;
16
17pub const SELF_HINTED: &[(&str, &str)] = &[
19 ("require-root", "not-at-root"),
20 ("linked-worktree-only", "linked-worktree-required"),
21 ("branch-worktree-only", "branch-creation-requires-worktree"),
22];
23
24pub fn finding_hint(policy: &str, code: &str, fix_available: bool) -> Option<&'static str> {
35 match (policy, code, fix_available) {
36 ("add-explicit", "bulk-add-rejected", _) => {
37 return Some(
38 "Hint: if you meant to stage everything you changed, run `git status --short` to list it and name \
39 each path, as in `git add -- <path> <path>`; if you meant only files git already tracks, name those \
40 paths the same way.",
41 );
42 }
43 ("final-newline", "noncanonical-final-newline", true) => {
44 return Some(
45 "Hint: if you are committing or checking this file, a commit corrects the ending for you; \
46 to correct it now without committing, run `git cli-git fix -- <path>`.",
47 );
48 }
49 ("final-newline", "noncanonical-final-newline", false) => {
50 return Some(
51 "Hint: if you are committing or checking this file, edit it so it ends with exactly one newline, \
52 then stage it again.",
53 );
54 }
55 ("markdown/autofix", "markdown-autofix", _) => {
56 return Some(
57 "Hint: if you are committing this Markdown, the commit applies the linter's corrections itself; \
58 to apply them now, run `git cli-git fix -- <path>`.",
59 );
60 }
61 ("markdown/autofix", "markdown-violation", _) => {
62 return Some(
63 "Hint: if you are committing this Markdown, the linter cannot repair this rule by itself; \
64 edit the file at the reported line and column, stage it again, and retry.",
65 );
66 }
67 ("mono/forbidden-root-context", "root-context-forbidden", _) => {
68 return Some(
69 "Hint: if you meant to keep project notes or terms for later readers, put them in a file under doc/; \
70 to drop CONTEXT.md from this commit and keep the file on disk, run `git rm --cached CONTEXT.md`.",
71 );
72 }
73 ("mono/dependent-version-bump", "dependent-version-stale", _) => {
74 return Some(
75 "Hint: if you changed a package that others depend on, raise its dependents' version ranges to match \
76 the new version in the same commit, then stage those files; `git cli-git fix -- <path>` applies the \
77 correction when one is available.",
78 );
79 }
80 ("mono/dependent-version-bump", "dependent-version-unsupported", _) => {
81 return Some(
82 "Hint: if you edited a dependency declaration of a shape this check does not read, update the \
83 dependents by hand in the same commit; the message names the file.",
84 );
85 }
86 ("security/forbidden-strings", "forbidden-string", _) => {
87 return Some(
88 "Hint: if you are committing text or a path that contains a secret or a banned term, remove or \
89 replace it where the message points and stage the file again; if you believe the rule is wrong, \
90 review it in the rules file named by the configuration instead of splitting the commit.",
91 );
92 }
93 _ => return None,
94 }
95}
96
97pub fn engine_failure_hint(code: &str) -> Option<&'static str> {
108 match code {
109 "config-invalid" => {
110 return Some(
111 "Hint: if you just edited cli-git.config.jsonc, fix the key the message names or revert the edit; \
112 an unknown key, a wrong type or an unknown policy id fails before any policy runs.",
113 );
114 }
115 "core-incomplete" => {
116 return Some(
117 "Hint: if another session was changing the repository at the same moment (a commit, a checkout or a \
118 rebase running elsewhere), run the command again; if it repeats, run `git status` to check that the \
119 repository is healthy.",
120 );
121 }
122 "content-unavailable" => {
123 return Some(
124 "Hint: if you are committing or pushing content that is not in this clone (a missing object, a \
125 shallow history, or a file removed during the command), fetch the history with `git fetch`, make \
126 sure the files exist, and retry.",
127 );
128 }
129 "policy-incomplete" => {
130 return Some(
131 "Hint: if you are committing or pushing, a policy could not finish reading the content, so it \
132 decided nothing; fix the cause the message names (an unreadable file, a path with a line break) \
133 and retry.",
134 );
135 }
136 "patch-invalid" | "patch-conflict" => {
137 return Some(
138 "Hint: if you are committing files that changed while the command ran or that another policy also \
139 rewrites, save your edits, run `git cli-git fix -- <path>` to apply the corrections, review the \
140 result with `git diff`, and commit again.",
141 );
142 }
143 "fix-cycle" | "fix-pass-limit" => {
144 return Some(
145 "Hint: two corrections keep undoing each other on this file; apply one of them by hand, stage the \
146 file, and commit again. Report the pair of policies if the file is ordinary text.",
147 );
148 }
149 "transaction-failed" => {
150 return Some(
151 "Hint: if you were committing, run `git status` and `git log --oneline -3` to see whether the \
152 commit landed before you retry; staged files stay staged unless the message says otherwise, and the \
153 message names the step that failed.",
154 );
155 }
156 "index-lock-unproven-owner" => {
157 return Some(
158 "Hint: if no other Git command is running in this repository, a crashed command left the lock \
159 behind: check with `ps` that no Git process uses this repository, then remove the .git/index.lock \
160 file the message names and retry.",
161 );
162 }
163 "legacy-config-ignored" => {
164 return Some(
165 "Hint: if you are still keeping the old configuration files, delete them once \
166 cli-git.config.jsonc holds the settings; they are no longer read.",
167 );
168 }
169 _ => return None,
170 }
171}
172
173pub const WARN_UNSAFE_HINT: &str = "Hint: if you set it to warn to keep commits flowing, note that this policy \
184 changes or misses content when it only warns; set its severity to error to enforce it, or to off to disable it, \
185 in cli-git.config.jsonc.";
186
187pub const POST_COMMIT_BLOCKED_HINT: &str = "Hint: if you wanted this commit backed up, fix what the gate reported \
189 and run `git push` yourself; the commit stays in your local history until then.";
190
191pub const REPLAY_HEADERS_DROPPED_HINT: &str = "Hint: if this commit must carry your original signature, run \
193 `git commit --amend --no-edit -S` after the replay to sign the replayed commit again.";
194
195pub fn push_hint_field(line: &mut String, hint: Option<&str>) {
207 if let Some(text) = hint {
208 line.push_str(",\"hint\":");
209 line.push_str(quote(text).as_str());
210 }
211}
212
213#[cfg(test)]
215#[path = "event_hints_tests.rs"]
216mod tests;