(worktree_git_dir: &Path)
| 1437 | } |
| 1438 | |
| 1439 | pub(super) fn resolve_common_dir(worktree_git_dir: &Path) -> Result<PathBuf, ObservationError> { |
| 1440 | let path = worktree_git_dir.join("commondir"); |
| 1441 | let bytes = match fs::read(&path) { |
| 1442 | Ok(bytes) => bytes, |
| 1443 | Err(error) if error.kind() == io::ErrorKind::NotFound => { |
| 1444 | return Ok(worktree_git_dir.to_path_buf()); |
| 1445 | } |
| 1446 | Err(error) => { |
| 1447 | return Err(ObservationError::new(format!( |
| 1448 | "cannot read Git common-dir pointer '{}': {error}", |
| 1449 | path.display() |
| 1450 | ))); |
| 1451 | } |
| 1452 | }; |
| 1453 | let value = bytes.strip_suffix(b"\n").unwrap_or(&bytes); |
| 1454 | let value = value.strip_suffix(b"\r").unwrap_or(value); |
| 1455 | if value.is_empty() || value.contains(&0) || value.contains(&b'\n') || value.contains(&b'\r') { |
| 1456 | return Err(ObservationError::new(format!( |
| 1457 | "Git common-dir pointer '{}' is malformed", |
| 1458 | path.display() |
| 1459 | ))); |
| 1460 | } |
| 1461 | let value = std::str::from_utf8(value).map_err(|_| { |
| 1462 | ObservationError::new(format!( |
| 1463 | "Git common-dir pointer '{}' is not valid UTF-8", |
| 1464 | path.display() |
| 1465 | )) |
| 1466 | })?; |
| 1467 | resolve_path(Path::new(value), worktree_git_dir) |
| 1468 | } |
| 1469 | |
| 1470 | fn resolve_path(path: &Path, base: &Path) -> Result<PathBuf, ObservationError> { |
| 1471 | let absolute = if path.is_absolute() { |
no test coverage detected