Skip to content

Commit

Permalink
Search upward if path not exist when cargo new
Browse files Browse the repository at this point in the history
Signed-off-by: hi-rustin <[email protected]>
  • Loading branch information
Rustin170506 committed Aug 14, 2022
1 parent 8494149 commit 379c351
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/cargo/util/vcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,17 @@ use std::path::Path;
// 2. We are in an HG repo.
pub fn existing_vcs_repo(path: &Path, cwd: &Path) -> bool {
fn in_git_repo(path: &Path, cwd: &Path) -> bool {
if let Ok(repo) = GitRepo::discover(path, cwd) {
// Try to find the first existing parent of the path.
// Otherwise, we can't find the git repo when the path has non-existing parent directories.
let mut first_exist_base_path = Some(path);
while let Some(p) = first_exist_base_path {
if p.exists() {
break;
}
first_exist_base_path = p.parent();
}

if let Ok(repo) = GitRepo::discover(first_exist_base_path.unwrap_or(path), cwd) {
// Don't check if the working directory itself is ignored.
if repo.workdir().map_or(false, |workdir| workdir == path) {
true
Expand Down
37 changes: 37 additions & 0 deletions tests/testsuite/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,43 @@ fn subpackage_git_with_gitignore() {
.is_file());
}

#[cargo_test]
fn subpackage_no_git_with_git_in_ancestor() {
cargo_process("new foo").run();

assert!(paths::root().join("foo/.git").is_dir());
assert!(paths::root().join("foo/.gitignore").is_file());

cargo_process("new foo/non-existent/subcomponent").run();

assert!(!paths::root()
.join("foo/non-existent/subcomponent/.git")
.is_dir());
assert!(!paths::root()
.join("foo/non-existent/subcomponent/.gitignore")
.is_file());
}

#[cargo_test]
fn subpackage_git_with_gitignore_in_ancestor() {
cargo_process("new foo").run();

assert!(paths::root().join("foo/.git").is_dir());
assert!(paths::root().join("foo/.gitignore").is_file());

let gitignore = paths::root().join("foo/.gitignore");
fs::write(gitignore, b"non-existent").unwrap();

cargo_process("new foo/non-existent/subcomponent").run();

assert!(paths::root()
.join("foo/non-existent/subcomponent/.git")
.is_dir());
assert!(paths::root()
.join("foo/non-existent/subcomponent/.gitignore")
.is_file());
}

#[cargo_test]
fn subpackage_git_with_vcs_arg() {
cargo_process("new foo").run();
Expand Down

0 comments on commit 379c351

Please sign in to comment.