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

Cargo init creates duplicates in .gitignore #6377

Closed
collin5 opened this issue Dec 5, 2018 · 5 comments · Fixed by #6521
Closed

Cargo init creates duplicates in .gitignore #6377

collin5 opened this issue Dec 5, 2018 · 5 comments · Fixed by #6521
Labels
E-easy Experience: Easy

Comments

@collin5
Copy link
Contributor

collin5 commented Dec 5, 2018

Doing cargo init in an existing git repository will either create a new .gitignore or append to an existing one. However if we already have a .gitignore file with both /target and **/*.rs.bk ignored, cargo init will still add these to the existing file.

Steps

$ mkdir foo && cd foo 
$ git init
$ echo "/target\n**/*.rs.bk" > .gitignore
$ cargo init

The new .gitignore will now contain

/target
**/*.rs.bk

/target
**/*.rs.bk

Possible Solution(s)

Before creating or adding to the existing .gitignore, we could perhaps check the contents and only add what is missing. Looks like the code handling this is

let ignore = if path.join(".gitignore").exists() {
format!("\n{}", ignore)
} else {
ignore
};
paths::append(&path.join(".gitignore"), ignore.as_bytes())?;
,
let hgignore = if path.join(".hgignore").exists() {
format!("\n{}", hgignore)
} else {
hgignore
};
paths::append(&path.join(".hgignore"), hgignore.as_bytes())?;
and
let ignore = if path.join(".ignore").exists() {
format!("\n{}", ignore)
} else {
ignore
};
paths::append(&path.join(".ignore"), ignore.as_bytes())?;

Version: cargo 1.31.0-nightly (2d0863f 2018-10-20)

@dwijnand dwijnand added the E-easy Experience: Easy label Dec 18, 2018
@starsheriff
Copy link
Contributor

I'd happily start working on that one as a first contribution. Seems to be quite straight forward.

@collin5
Copy link
Contributor Author

collin5 commented Dec 18, 2018

Great! Go for it.

starsheriff added a commit to starsheriff/cargo that referenced this issue Jan 4, 2019
existing ignore files are now checked for duplicates. If the file
already contains an entry it is commented out in the section added by
cargo.

fixes rust-lang#6377
bors added a commit that referenced this issue Jan 6, 2019
…iles, r=dwijnand

avoid duplicates in ignore files

Hi,
here is my first PR for cargo. It's my take on #6377 with some minor refactoring included, mainly to avoid keeping the two different types of ignore file entries (gitignore and hg) in sync.) Basically, the contents of a ignore file are now read if the file exists and filtered out. To filter out I would propose to just comment the entries that cargo would add out, in that way it is nice to see which duplicates were found and more important _what cargo usually adds_. In that way, a user can modify his ignore file and be sure that he can keep everything that cargo would add.

A new ignore file will look like this:
```
/target
**/*.rs.bk
Cargo.lock",
```

An existing ignore file will be modified like this:
```
/target
/some/other/path

#Added by cargo
#
#already existing elements are commented out

#/target
**/*.rs.bk
Cargo.lock
```

Fixes #6377
@bors bors closed this as completed in #6521 Jan 6, 2019
@Lucretiel
Copy link

I'd like to reopen this issue; I'm still seeing this behavior on git when I run cargo init --lib .

@dwijnand
Copy link
Member

@Lucretiel That's because this fix hasn't made it way into stable Rust yet. It's in nightly and beta (try cargo +nightly init or cargo +beta init) and stable is releasing in next week Friday 1 March.

@Lucretiel
Copy link

Oh, sorry, I didn't see the dates that this was so recent. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
E-easy Experience: Easy
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants