Elffuss BraidHub

utopia-ia/elffuss-braid public

356f55034aff · 5.1 KiBRawPermalink
1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2026 UtopiaIA
3
4//! What stops a commit: the reasons `status` lists and `commit` refuses
5//! with, so that an agent sees the same list before and after trying.
6
7use std::fmt;
8
9use crate::paths::show;
10use crate::state::RecordedConflict;
11
12/// One reason why `commit` would not record the working copy as it is.
13#[derive(Clone, Debug, PartialEq, Eq)]
14pub enum Blocker {
15 /// Conflict markers remain in this file, from a content conflict of the
16 /// rebase in progress. Editing them out settles it.
17 Markers(Vec<u8>),
18 /// A conflict of the rebase in progress that is not about content, and
19 /// is not marked resolved: `braid resolve` marks it once it is settled.
20 Unresolved {
21 /// Where it is.
22 path: Vec<u8>,
23 /// Its kind, as [`braid_engine::ConflictKind::code`] names it.
24 code: String,
25 },
26 /// A new file the secrets guard holds back until `braid track` marks
27 /// it: its name looks like a secret, or it is large.
28 NeedsTrack(Vec<u8>),
29}
30
31impl Blocker {
32 /// A short, stable name for reports and JSON: `markers`, `unresolved`
33 /// or `needs_track`.
34 pub fn code(&self) -> &'static str {
35 match self {
36 Blocker::Markers(_) => "markers",
37 Blocker::Unresolved { .. } => "unresolved",
38 Blocker::NeedsTrack(_) => "needs_track",
39 }
40 }
41
42 /// The path it is about.
43 pub fn path(&self) -> &[u8] {
44 match self {
45 Blocker::Markers(path) | Blocker::NeedsTrack(path) => path,
46 Blocker::Unresolved { path, .. } => path,
47 }
48 }
49
50 /// The kind of the conflict it comes from, if it comes from one.
51 pub fn conflict(&self) -> Option<&str> {
52 match self {
53 Blocker::Markers(_) => Some("CONTENT"),
54 Blocker::Unresolved { code, .. } => Some(code),
55 Blocker::NeedsTrack(_) => None,
56 }
57 }
58}
59
60impl fmt::Display for Blocker {
61 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
62 match self {
63 Blocker::Markers(path) => write!(
64 f,
65 "conflict markers remain in {:?}: edit them out",
66 show(path)
67 ),
68 Blocker::Unresolved { path, code } => write!(
69 f,
70 "the {code} conflict at {:?} is not marked resolved: settle it, then run `braid resolve` on it",
71 show(path)
72 ),
73 Blocker::NeedsTrack(path) => write!(
74 f,
75 "{:?} is a new file the secrets guard holds back: `braid track` it to commit it",
76 show(path)
77 ),
78 }
79 }
80}
81
82/// The reasons a commit of the paths `in_scope` accepts would be refused:
83/// markers left in the files of content conflicts and other conflicts not
84/// marked resolved, from `conflicts` (each with whether markers remain in
85/// its file), then the new files of `needs_track`.
86pub(crate) fn blockers<'p>(
87 conflicts: &[(RecordedConflict, bool)],
88 needs_track: impl IntoIterator<Item = &'p Vec<u8>>,
89 in_scope: impl Fn(&[u8]) -> bool,
90) -> Vec<Blocker> {
91 let mut found = Vec::new();
92 for (conflict, remain) in conflicts {
93 if conflict.markers {
94 if *remain {
95 found.push(Blocker::Markers(conflict.path.clone()));
96 }
97 } else if !conflict.resolved {
98 found.push(Blocker::Unresolved {
99 path: conflict.path.clone(),
100 code: conflict.code.clone(),
101 });
102 }
103 }
104 for path in needs_track {
105 if in_scope(path) {
106 found.push(Blocker::NeedsTrack(path.clone()));
107 }
108 }
109 found
110}
111
112#[cfg(test)]
113mod tests {
114 use super::*;
115
116 fn conflict(path: &str, code: &str, markers: bool, resolved: bool) -> RecordedConflict {
117 RecordedConflict {
118 path: path.as_bytes().to_vec(),
119 code: code.to_owned(),
120 markers,
121 resolved,
122 }
123 }
124
125 #[test]
126 fn reasons_in_order() {
127 let conflicts = [
128 (conflict("a", "CONTENT", true, false), true),
129 (conflict("b", "CONTENT", true, false), false),
130 (conflict("c", "MODIFY_DELETE", false, false), false),
131 (conflict("d", "DF", false, true), false),
132 ];
133 let guarded = [b".env".to_vec(), b"keys/id_rsa".to_vec()];
134 let found = blockers(&conflicts, &guarded, |path| path != b".env");
135 assert_eq!(
136 found,
137 [
138 Blocker::Markers(b"a".to_vec()),
139 Blocker::Unresolved {
140 path: b"c".to_vec(),
141 code: "MODIFY_DELETE".to_owned()
142 },
143 Blocker::NeedsTrack(b"keys/id_rsa".to_vec()),
144 ]
145 );
146 let codes: Vec<&str> = found.iter().map(Blocker::code).collect();
147 assert_eq!(codes, ["markers", "unresolved", "needs_track"]);
148 assert_eq!(found[1].conflict(), Some("MODIFY_DELETE"));
149 assert!(found[0].to_string().contains("conflict markers remain"));
150 assert!(found[1].to_string().contains("braid resolve"));
151 assert!(found[2].to_string().contains("braid track"));
152 }
153}