Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search upward if the path does not exist when cargo new #10987

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See instead libgit2/libgit2#6383

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. See instead #10981 (comment).

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