new: skip the git-init question inside existing repositories
This commit is contained in:
@@ -28,6 +28,23 @@ pub fn ensure_repository(dir: &Path, init: bool) -> Result<bool, Box<dyn std::er
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether `dir` sits inside a git work tree (its own or a parent's), in the
|
||||
/// spirit of `git rev-parse --is-inside-work-tree` (like
|
||||
/// [`crate::new::origin::GitOrigin::detect`]). Fail-soft: when `git` cannot
|
||||
/// be run or the probe fails, `dir` counts as outside — the caller keeps the
|
||||
/// behavior it would have without the probe, and the scaffold-time
|
||||
/// repository discovery in [`ensure_repository`] has the final word anyway.
|
||||
pub fn inside_work_tree(dir: &Path) -> bool {
|
||||
let Ok(output) = std::process::Command::new("git")
|
||||
.args(["rev-parse", "--is-inside-work-tree"])
|
||||
.current_dir(dir)
|
||||
.output()
|
||||
else {
|
||||
return false;
|
||||
};
|
||||
output.status.success() && String::from_utf8_lossy(&output.stdout).trim() == "true"
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -61,4 +78,22 @@ mod tests {
|
||||
assert!(!ensure_repository(&sub, true).unwrap());
|
||||
assert!(!sub.join(".git").exists());
|
||||
}
|
||||
|
||||
/// The probe answers like `git rev-parse --is-inside-work-tree`:
|
||||
/// outside any repository it says no, inside one (at any depth, as a
|
||||
/// skeleton mode scaffold would probe through its parent directory) it
|
||||
/// says yes without touching the tree.
|
||||
#[test]
|
||||
fn inside_work_tree_follows_the_parent_repository() {
|
||||
let dir = tempdir().unwrap();
|
||||
assert!(!inside_work_tree(dir.path()));
|
||||
|
||||
git2::Repository::init(dir.path()).unwrap();
|
||||
assert!(inside_work_tree(dir.path()));
|
||||
|
||||
let sub = dir.path().join("sub");
|
||||
std::fs::create_dir(&sub).unwrap();
|
||||
assert!(inside_work_tree(&sub));
|
||||
assert!(!sub.join(".git").exists());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user