Skip to content

Cargo init creates duplicates in .gitignore #6377

Closed
@collin5

Description

@collin5

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    E-easyExperience: Easy

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions