1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//! Minimal git CLI helpers used by capture identity. The pure parser is
//! split from the side-effecting subprocess spawn so the parser stays
//! unit-testable; the spawn itself is documented but not unit-tested (it
//! would require tempdir + git init in the test environment, which adds an
//! external dependency for a single helper).
use std::path::{Path, PathBuf};
use std::process::Command;
/// Parse the output of `git rev-parse --show-toplevel`. Returns `None` for
/// empty or whitespace-only input so callers can fall back cleanly.
pub fn parse_toplevel_output(stdout: &str) -> Option<PathBuf> {
let trimmed = stdout.trim();
if trimmed.is_empty() {
None
} else {
Some(PathBuf::from(trimmed))
}
}
/// Spawn `git rev-parse --show-toplevel` in `cwd`. Returns `None` when git
/// is unavailable, the directory is outside any git worktree, or the output
/// is empty. Single subprocess, no network.
pub fn resolve_toplevel(cwd: &Path) -> Option<PathBuf> {
let output = Command::new("git")
.args(["rev-parse", "--show-toplevel"])
.current_dir(cwd)
.output()
.ok()?;
if !output.status.success() {
return None;
}
parse_toplevel_output(&String::from_utf8_lossy(&output.stdout))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parse_returns_path_for_normal_output() {
assert_eq!(
parse_toplevel_output("/Users/foo/project\n"),
Some(PathBuf::from("/Users/foo/project"))
);
}
#[test]
fn parse_returns_none_for_empty_or_whitespace() {
assert_eq!(parse_toplevel_output(""), None);
assert_eq!(parse_toplevel_output("\n"), None);
assert_eq!(parse_toplevel_output(" \t\n "), None);
}
#[test]
fn parse_strips_trailing_whitespace() {
assert_eq!(
parse_toplevel_output("/path/with-spaces \n\t"),
Some(PathBuf::from("/path/with-spaces"))
);
}
}