Skip to content

Commit 3e8cdd7

Browse files
committed
Only include "already existing ..." comment in gitignore on conflict
This comment was previously confusing when there were no existing items that conflict.
1 parent 0c0f9ae commit 3e8cdd7

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

src/cargo/ops/cargo_new.rs

Lines changed: 8 additions & 4 deletions
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 are 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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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)