Skip to content

Commit dd34668

Browse files
committed
Auto merge of #7570 - twe4ked:already-existing-git-ignore-comment, r=alexcrichton
Only include "already existing ..." comment in gitignore on conflict I found the comment a bit confusing when I've used `cargo init` when I haven't had any conflicts. I thought it was meaning that my existing entries would be all commented out so I thought there was a bug of some kind. This PR changes the comment to only be included when there are conflicting entries in the existing file.
2 parents cb50a19 + c42f2ee commit dd34668

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

src/cargo/ops/cargo_new.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -487,10 +487,14 @@ impl IgnoreList {
487487
_ => &self.ignore,
488488
};
489489

490-
let mut out = "\n\n#Added by cargo\n\
491-
#\n\
492-
#already existing elements are commented out\n\n"
493-
.to_string();
490+
let mut out = "\n\n#Added by cargo\n".to_string();
491+
if ignore_items
492+
.iter()
493+
.any(|item| existing_items.contains(item))
494+
{
495+
out.push_str("#\n#already existing elements were commented out\n");
496+
}
497+
out.push('\n');
494498

495499
for item in ignore_items {
496500
if existing_items.contains(item) {

tests/testsuite/init.rs

+29-2
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ fn simple_bin() {
5959

6060
#[cargo_test]
6161
fn simple_git_ignore_exists() {
62-
// write a .gitignore file with one entry
62+
// write a .gitignore file with two entries
6363
fs::create_dir_all(paths::root().join("foo")).unwrap();
6464
fs::write(
6565
paths::root().join("foo/.gitignore"),
@@ -89,7 +89,7 @@ fn simple_git_ignore_exists() {
8989
**/some.file\n\n\
9090
#Added by cargo\n\
9191
#\n\
92-
#already existing elements are commented out\n\
92+
#already existing elements were commented out\n\
9393
\n\
9494
#/target\n\
9595
**/*.rs.bk\n\
@@ -99,6 +99,33 @@ fn simple_git_ignore_exists() {
9999
cargo_process("build").cwd(&paths::root().join("foo")).run();
100100
}
101101

102+
#[cargo_test]
103+
fn git_ignore_exists_no_conflicting_entries() {
104+
// write a .gitignore file with one entry
105+
fs::create_dir_all(paths::root().join("foo")).unwrap();
106+
fs::write(paths::root().join("foo/.gitignore"), "**/some.file").unwrap();
107+
108+
cargo_process("init --lib foo --edition 2015")
109+
.env("USER", "foo")
110+
.run();
111+
112+
let fp = paths::root().join("foo/.gitignore");
113+
let mut contents = String::new();
114+
File::open(&fp)
115+
.unwrap()
116+
.read_to_string(&mut contents)
117+
.unwrap();
118+
assert_eq!(
119+
contents,
120+
"**/some.file\n\n\
121+
#Added by cargo\n\
122+
\n\
123+
/target\n\
124+
**/*.rs.bk\n\
125+
Cargo.lock\n",
126+
);
127+
}
128+
102129
#[cargo_test]
103130
fn both_lib_and_bin() {
104131
cargo_process("init --lib --bin")

0 commit comments

Comments
 (0)