(root: &Path)
| 435 | } |
| 436 | |
| 437 | fn inspect_linked_git(root: &Path) -> Result<(), SourceError> { |
| 438 | use std::process::{Command, Stdio}; |
| 439 | |
| 440 | let output = Command::new("git") |
| 441 | .arg("--no-optional-locks") |
| 442 | .arg("-C") |
| 443 | .arg(root) |
| 444 | .args([ |
| 445 | "rev-parse", |
| 446 | "--show-toplevel", |
| 447 | "--git-common-dir", |
| 448 | "--git-dir", |
| 449 | "--git-path", |
| 450 | "index", |
| 451 | ]) |
| 452 | .env_remove("GIT_INDEX_FILE") |
| 453 | .env("GIT_TERMINAL_PROMPT", "0") |
| 454 | .stdin(Stdio::null()) |
| 455 | .stderr(Stdio::null()) |
| 456 | .output() |
| 457 | .map_err(|_| SourceError::WorkspaceLookup)?; |
| 458 | if !output.status.success() { |
| 459 | return Err(SourceError::WorkspaceLookup); |
| 460 | } |
| 461 | let lines: Vec<&[u8]> = output |
| 462 | .stdout |
| 463 | .split(|byte| *byte == b'\n') |
| 464 | .filter(|line| !line.is_empty()) |
| 465 | .map(|line| line.strip_suffix(b"\r").unwrap_or(line)) |
| 466 | .collect(); |
| 467 | let [worktree, common, gitdir, _index] = lines.as_slice() else { |
| 468 | return Err(SourceError::WorkspaceLookup); |
| 469 | }; |
| 470 | let resolve = |bytes: &[u8]| -> Result<PathBuf, SourceError> { |
| 471 | if bytes.is_empty() || bytes.contains(&0) || bytes.contains(&b'\r') { |
| 472 | return Err(SourceError::WorkspaceLookup); |
| 473 | } |
| 474 | #[cfg(unix)] |
| 475 | let path = { |
| 476 | use std::os::unix::ffi::OsStringExt; |
| 477 | PathBuf::from(std::ffi::OsString::from_vec(bytes.to_vec())) |
| 478 | }; |
| 479 | #[cfg(not(unix))] |
| 480 | let path = |
| 481 | PathBuf::from(std::str::from_utf8(bytes).map_err(|_| SourceError::WorkspaceLookup)?); |
| 482 | let path = if path.is_absolute() { |
| 483 | path |
| 484 | } else { |
| 485 | root.join(path) |
| 486 | }; |
| 487 | path.canonicalize() |
| 488 | .map_err(|_| SourceError::WorkspaceLookup) |
| 489 | }; |
| 490 | let worktree = resolve(worktree)?; |
| 491 | let common = resolve(common)?; |
| 492 | let gitdir = resolve(gitdir)?; |
| 493 | if worktree != root || !common.is_dir() || !gitdir.is_dir() { |
| 494 | return Err(SourceError::WorkspaceLookup); |
no test coverage detected