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

Respect .gitignore during cargo new #5733

Merged
merged 1 commit into from
Jul 26, 2018

Conversation

withoutboats
Copy link
Contributor

@withoutboats withoutboats commented Jul 17, 2018

When running cargo new, we check to see if you are inside a git repository. If you are, we do not initialize a new git repo for your project unless you specifically asked for it using --vcs. (See #1210 for more background).

This commit changes that behavior to also create a new repo if the project would be an ignored path in the parent repository. This way, if your home directory is a git repository, as long as you have ignored the directory you are creating a new project in, we will instantiate a git repository without you having to specifically request it.

@rust-highfive
Copy link

r? @matklad

(rust_highfive has picked a reviewer for you, use r? to override)

@withoutboats
Copy link
Contributor Author

cc @joshtriplett I figure you may be interested in this, since you filed #1210 way back.

@alexcrichton
Copy link
Member

Looks good to me! Can you add a test for this as well?

@bors
Copy link
Contributor

bors commented Jul 17, 2018

☔ The latest upstream changes (presumably #5723) made this pull request unmergeable. Please resolve the merge conflicts.

@joshtriplett
Copy link
Member

This seems like a plausible heuristic.

@withoutboats withoutboats force-pushed the cargo-new-respects-gitignore branch 2 times, most recently from c259f89 to 4f2d554 Compare July 19, 2018 11:31
@withoutboats
Copy link
Contributor Author

@bors r=alexcrichton

@bors
Copy link
Contributor

bors commented Jul 19, 2018

📌 Commit 4f2d55472c0d2a7f6f1931ad8441c110ea477c49 has been approved by alexcrichton

@bors
Copy link
Contributor

bors commented Jul 19, 2018

⌛ Testing commit 4f2d55472c0d2a7f6f1931ad8441c110ea477c49 with merge 09f35b8c58816bd625e6125f727f9d7852229b8a...

@bors
Copy link
Contributor

bors commented Jul 19, 2018

💔 Test failed - status-appveyor

@withoutboats withoutboats force-pushed the cargo-new-respects-gitignore branch from 4f2d554 to a63f31d Compare July 19, 2018 12:55
@withoutboats
Copy link
Contributor Author

It appears this behavior doesn't work on windows, I am unable to test locally & attempts to fix path issues have been unsuccessful.

@Eh2406
Copy link
Contributor

Eh2406 commented Jul 19, 2018

I am a windows based lifeform, I am able to reproduce locally. Investigating.

@dwijnand
Copy link
Member

Does this fix it? dwijnand@ec27a6a

@Eh2406
Copy link
Contributor

Eh2406 commented Jul 19, 2018

edit: @dwijnand good thought, but no. I don't think it is the test.
(sorry @daboross)

I don't think the functionality is working. I don't have any .git anyware in target\cit at all. The reason is described here. That also means that the test subpackage_no_git is not testing anything. Looks like the proposal in this pr is so intuitive that we write tests having assume it was already inplace.

@Eh2406
Copy link
Contributor

Eh2406 commented Jul 19, 2018

looks like is_path_ignored is not working correctly. I added some println and it doesn't look correct.

code with println

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) {
            println!("repo: {:?}", repo.path().display());
            repo.is_path_ignored(path).map(|ignored| {
                println!("path: {:?} ignored: {:?}", path.display(), ignored);
                !ignored
            }).unwrap_or(true)
        } else { false }
    }

    in_git_repo(path, cwd) || HgRepo::discover(path, cwd).is_ok()
}

full output:

running 1 test
running `C:\\cargo\target\debug\cargo.exe new foo`
repo: "C:/cargo/.git/"
path: "C:\\cargo\\target\\cit\\t0" ignored: false
     Created binary (application) `foo` project
thread 'new::subpackage_no_git' panicked at '
Expected: ExistingDir
    but: C:\cargo\target\cit\t0\foo/.git was not a dir', tests\testsuite\hamcrest.rs:13:9
stack backtrace:

the kee output is:

repo: "C:/cargo/.git/"
path: "C:\\cargo\\target\\cit\\t0" ignored: false

target\\cit\\t0 is not ignored by C:/cargo/.git/ !?

@daboross
Copy link
Contributor

Unintentional ping? Forwarding to @dwijnand.

@withoutboats
Copy link
Contributor Author

It sounds like is_path_ignored does not work correctly on Windows? Probably a bug in libgit2?

Maybe for now we merge with this functionality only on unix & open a tracking issue for supporting it on windows?

@dwijnand
Copy link
Member

It sounds like is_path_ignored does not work correctly on Windows? Probably a bug in libgit2?

Yup, see https://github.com/alexcrichton/git2-rs/issues/334

@Eh2406
Copy link
Contributor

Eh2406 commented Jul 20, 2018

Looks like is_path_ignored only accepts linux like paths, even though discover works with windows paths. I am working on a PR upstream to work around this bug by using to_string_lossy and replace.

We can wait for that PR and for a new version of libgit, or we can add the same workaround to this PR. Please don't leave a bunch of test broken on windows.

@withoutboats
Copy link
Contributor Author

Please don't leave a bunch of test broken on windows.

I'm suggesting merging this PR by cfging out the one new test of this new functionality until the git2 patch has merged.

@Eh2406
Copy link
Contributor

Eh2406 commented Jul 20, 2018

In looking the new test to find this bug I realized that there are a lot of test that are nop, because they don't realize that no .git folders are created in target at all, and there are other test with workarounds for it.

I guess we could clean that mess up when we get it working on windows.

@dwijnand
Copy link
Member

I'm with boats: let's accept the feature, with the test for Unix only, and forward fix trailing concerns.

@alexcrichton
Copy link
Member

We can land this as a gated feature perhaps if it only works on Unix, but otherwise we can't land new functionality in Cargo that doesn't work on one of our tier 1 platforms

@dwijnand
Copy link
Member

Oh, sorry, I'm still confused thinking the failure is the test on Windows, rather than the feature itself.

@Eh2406
Copy link
Contributor

Eh2406 commented Jul 23, 2018

This should work with git2 = "0.7.3". Thanks @alexcrichton for pushing that update so quickly!

I'd like to see the other test reviewed before this gets merged, but as long as they pass we can clean them up later.

@Eh2406 Eh2406 mentioned this pull request Jul 24, 2018
@dwijnand
Copy link
Member

This is failing CI validation because the pre-existing test fix::does_not_warn_about_dirty_ignored_files fails.

I think the failure is a git2 error (git2::Repository::discover(Path::new("/s/foo")).is_path_ignored("/s/foo") returns true with a .gitignore containing "foo"), and the upstream issue should be fixed to avoid it misfiring in cargo new and cargo fix

But, in the meanwhile, I've found you can tweak the test to make it pass by either:

  • making it gitignore and create a non "foo" file
  • move the project to a non "foo" directory (project().at("bar"))

When running `cargo new`, we check to see if you are inside a git
repository. If you are, we do not initialize a new git repo for
your project unless you specifically asked for it using --vcs.
(See rust-lang#1210 for more background).

This commit changes that behavior to *also* create a new repo if
the project would be an ignored path in the parent repository.
This way, if your home directory is a git repository, as long as
you have ignored the directory you are creating a new project in,
we will instantiate a git repository without you having to
specifically request it.
@withoutboats withoutboats force-pushed the cargo-new-respects-gitignore branch from a29dd74 to 064a146 Compare July 26, 2018 13:40
@alexcrichton
Copy link
Member

@bors: r+

@bors
Copy link
Contributor

bors commented Jul 26, 2018

📌 Commit 064a146 has been approved by alexcrichton

@alexcrichton
Copy link
Member

Thanks for the diagnosis @dwijnand!

@bors
Copy link
Contributor

bors commented Jul 26, 2018

⌛ Testing commit 064a146 with merge b5a1dbd...

bors added a commit that referenced this pull request Jul 26, 2018
…excrichton

Respect .gitignore during `cargo new`

When running `cargo new`, we check to see if you are inside a git repository. If you are, we do not initialize a new git repo for your project unless you specifically asked for it using --vcs. (See #1210 for more background).

This commit changes that behavior to *also* create a new repo if the project would be an ignored path in the parent repository. This way, if your home directory is a git repository, as long as you have ignored the directory you are creating a new project in, we will instantiate a git repository without you having to specifically request it.
Eh2406 added a commit to Eh2406/cargo that referenced this pull request Jul 26, 2018
@bors
Copy link
Contributor

bors commented Jul 26, 2018

☀️ Test successful - status-appveyor, status-travis
Approved by: alexcrichton
Pushing b5a1dbd to master...

@bors bors merged commit 064a146 into rust-lang:master Jul 26, 2018
bors added a commit that referenced this pull request Jul 26, 2018
now that we respect gitignore tests can be simplified

There are a lot of test that used a tempfile to avoid the fact that cargo would not init a git in the test folder. (or because they were copy/pasted from one that did.) Now that #5733 landed we can remove them all.
bors added a commit that referenced this pull request Jul 27, 2018
now that we respect gitignore tests can be simplified

There are a lot of test that used a tempfile to avoid the fact that cargo would not init a git in the test folder. (or because they were copy/pasted from one that did.) Now that #5733 landed we can remove them all.
@ehuss ehuss added this to the 1.29.0 milestone Feb 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants