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

Only include "already existing ..." comment in gitignore on conflict #7570

Merged
merged 3 commits into from
Nov 11, 2019
Merged
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: 8 additions & 4 deletions src/cargo/ops/cargo_new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -487,10 +487,14 @@ impl IgnoreList {
_ => &self.ignore,
};

let mut out = "\n\n#Added by cargo\n\
#\n\
#already existing elements are commented out\n\n"
.to_string();
let mut out = "\n\n#Added by cargo\n".to_string();
Copy link
Contributor Author

@twe4ked twe4ked Nov 7, 2019

Choose a reason for hiding this comment

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

I'm tempted to add some spaces before the # just for subjectively nicer formatting.

/foo
/target

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

# /target
**/*.rs.bk

Thoughts?

if ignore_items
.iter()
.any(|item| existing_items.contains(item))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

There is probably a more efficient way of doing this. Suggestions welcome!

{
out.push_str("#\n#already existing elements were commented out\n");
}
out.push('\n');

for item in ignore_items {
if existing_items.contains(item) {
Expand Down
31 changes: 29 additions & 2 deletions tests/testsuite/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ fn simple_bin() {

#[cargo_test]
fn simple_git_ignore_exists() {
// write a .gitignore file with one entry
// write a .gitignore file with two entries
fs::create_dir_all(paths::root().join("foo")).unwrap();
fs::write(
paths::root().join("foo/.gitignore"),
Expand Down Expand Up @@ -89,7 +89,7 @@ fn simple_git_ignore_exists() {
**/some.file\n\n\
#Added by cargo\n\
#\n\
#already existing elements are commented out\n\
#already existing elements were commented out\n\
\n\
#/target\n\
**/*.rs.bk\n\
Expand All @@ -99,6 +99,33 @@ fn simple_git_ignore_exists() {
cargo_process("build").cwd(&paths::root().join("foo")).run();
}

#[cargo_test]
fn git_ignore_exists_no_conflicting_entries() {
// write a .gitignore file with one entry
fs::create_dir_all(paths::root().join("foo")).unwrap();
fs::write(paths::root().join("foo/.gitignore"), "**/some.file").unwrap();

cargo_process("init --lib foo --edition 2015")
.env("USER", "foo")
.run();

let fp = paths::root().join("foo/.gitignore");
let mut contents = String::new();
File::open(&fp)
.unwrap()
.read_to_string(&mut contents)
.unwrap();
assert_eq!(
contents,
"**/some.file\n\n\
#Added by cargo\n\
\n\
/target\n\
**/*.rs.bk\n\
Cargo.lock\n",
);
}

#[cargo_test]
fn both_lib_and_bin() {
cargo_process("init --lib --bin")
Expand Down