Skip to main content

git_policy_cli/
commit_no_change.rs

1//! What:
2//!  The normalization-only commit: a correction whose complete intended tree equals the recorded base ends without an empty
3//!  commit, and says so.
4//! Why:
5//!  The preparation (`service_prepare.rs`) decides it before anything is sent to the service; this module holds the finding's
6//!  identity and its message, so the words of the finding stay in one place
7//!  (`src/policy-engine/commit-transaction-no-change.ts`).
8//!
9//! In TS you'd write (pseudocode):
10//! ```ts
11//! const message = noChangeMessage(conflicted);
12//! ```
13
14use super::path_bytes::path_message_text;
15
16/// The core of the no-change finding.
17pub const NO_CHANGE_CORE_ID: &str = "commit-normalization";
18
19/// The code of the no-change finding.
20pub const NO_CHANGE_CODE: &str = "no-change";
21
22/// What:
23///  The message of the no-change finding.
24/// Why:
25///   Worktree copies that changed since are kept and named.
26///
27/// In TS you'd write (pseudocode):
28/// ```ts
29/// hasConflicts ? `No commit created: ... ${JSON.stringify(conflicted)}. Compare them with HEAD before retrying.` : '...'
30/// ```
31pub fn no_change_message(conflicted: &[Vec<u8>]) -> String {
32    if conflicted.is_empty() {
33        return String::from(
34            "No commit created: normalization removed every selected change; matching worktree and index copies were reconciled. Hint: if you were committing only whitespace or final-newline edits, they were normalized away, so there is nothing to commit; run `git status` to confirm.",
35        );
36    }
37    // The message shows each name as text; a name that is not UTF-8 shows its replacement characters.
38    let names: Vec<String> = conflicted.iter().map(|path| { return path_message_text(path.as_slice()); }).collect();
39    let listed: String = serde_json::to_string(&names).unwrap_or_default();
40    return format!(
41        "No commit created: normalization removed every selected change; index copies were reconciled, but worktree copies changed and were kept: {listed}. Compare them with HEAD before retrying. Hint: if you were committing only whitespace or final-newline edits, they were normalized away; `git diff HEAD -- <path>` shows what still differs."
42    );
43}
44
45/// Controls for this module stay out of the release executable.
46#[cfg(test)]
47#[path = "commit_no_change_tests.rs"]
48mod tests;