WIP recovery refs (`refs/atomic/wip/…`) are referenced by at least one session record or are orphaned. A ref whose target change cannot load is broken regardless of references.
(repo: &Repository, root: &Path)
| 228 | /// session record or are orphaned. A ref whose target change cannot load is |
| 229 | /// broken regardless of references. |
| 230 | fn wip_census_class(repo: &Repository, root: &Path) -> CensusClass { |
| 231 | let name = "census/wip"; |
| 232 | let class = (|| -> Result<CensusClass, String> { |
| 233 | let git = git2::Repository::open(root).map_err(|e| e.to_string())?; |
| 234 | let mut refs: Vec<String> = Vec::new(); |
| 235 | for reference in git |
| 236 | .references_glob("refs/atomic/wip/*") |
| 237 | .map_err(|e| e.to_string())? |
| 238 | { |
| 239 | let reference = reference.map_err(|e| e.to_string())?; |
| 240 | let Ok(name) = reference.name() else { |
| 241 | continue; |
| 242 | }; |
| 243 | refs.push(name.to_string()); |
| 244 | } |
| 245 | if refs.is_empty() { |
| 246 | return Ok(CensusClass { |
| 247 | name, |
| 248 | clean: true, |
| 249 | detail: "no WIP recovery refs".to_string(), |
| 250 | }); |
| 251 | } |
| 252 | // Sessions reference their recovery refs by name (the retention |
| 253 | // scan reads the same text surface). |
| 254 | let sessions_dir = Repository::canonical_dot_dir(root) |
| 255 | .map(|dot| dot.join("sessions")) |
| 256 | .unwrap_or_else(|_| root.join(".atomic").join("sessions")); |
| 257 | let mut session_text = String::new(); |
| 258 | if let Ok(entries) = std::fs::read_dir(&sessions_dir) { |
| 259 | for entry in entries.filter_map(|entry| entry.ok()) { |
| 260 | let path = entry.path(); |
| 261 | if path.extension().and_then(|extension| extension.to_str()) == Some("json") { |
| 262 | if let Ok(bytes) = std::fs::read(&path) { |
| 263 | session_text.push_str(&String::from_utf8_lossy(&bytes)); |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | } |
| 268 | let mut orphaned: Vec<String> = Vec::new(); |
| 269 | let mut broken: Vec<String> = Vec::new(); |
| 270 | for reference in &refs { |
| 271 | let target_ok = git |
| 272 | .find_reference(reference) |
| 273 | .and_then(|reference| reference.peel_to_commit()) |
| 274 | .is_ok(); |
| 275 | if !target_ok { |
| 276 | broken.push(reference.to_string()); |
| 277 | continue; |
| 278 | } |
| 279 | if !session_text.contains(reference) { |
| 280 | orphaned.push(reference.to_string()); |
| 281 | } |
| 282 | } |
| 283 | if broken.is_empty() && orphaned.is_empty() { |
| 284 | Ok(CensusClass { |
| 285 | name, |
| 286 | clean: true, |
| 287 | detail: format!("{} WIP recovery ref(s) referenced", refs.len()), |