| 209 | } |
| 210 | |
| 211 | pub(crate) fn ensure_git_shadow_excludes(git_dir: &Path) -> CliResult<bool> { |
| 212 | let info_dir = git_dir.join("info"); |
| 213 | std::fs::create_dir_all(&info_dir)?; |
| 214 | |
| 215 | let exclude_path = info_dir.join("exclude"); |
| 216 | let mut content = match std::fs::read_to_string(&exclude_path) { |
| 217 | Ok(content) => content, |
| 218 | Err(e) if e.kind() == ErrorKind::NotFound => String::new(), |
| 219 | Err(e) => return Err(e.into()), |
| 220 | }; |
| 221 | |
| 222 | let missing: Vec<&str> = GIT_SHADOW_EXCLUDE_PATTERNS |
| 223 | .iter() |
| 224 | .copied() |
| 225 | .filter(|pattern| !content.lines().any(|line| line.trim() == *pattern)) |
| 226 | .collect(); |
| 227 | |
| 228 | if missing.is_empty() { |
| 229 | return Ok(false); |
| 230 | } |
| 231 | |
| 232 | if !content.is_empty() && !content.ends_with('\n') { |
| 233 | content.push('\n'); |
| 234 | } |
| 235 | if !content.is_empty() { |
| 236 | content.push('\n'); |
| 237 | } |
| 238 | content.push_str("# Atomic local state (managed by atomic git import)\n"); |
| 239 | for pattern in missing { |
| 240 | content.push_str(pattern); |
| 241 | content.push('\n'); |
| 242 | } |
| 243 | |
| 244 | std::fs::write(exclude_path, content)?; |
| 245 | Ok(true) |
| 246 | } |
| 247 | |
| 248 | /// Read-only variant of [`ensure_git_shadow_excludes`]: reports whether the |
| 249 | /// Git exclude file is missing any shadow-exclude pattern, without writing |