Skip to content
17 changes: 16 additions & 1 deletion dvc/ignore.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def from_files(cls, ignore_file_path, tree):
for line_no, line in enumerate(
map(str.strip, fobj.readlines())
)
if line
if line and not (line.strip().startswith("#"))
]

return cls(path_spec_lines, dirname)
Expand Down Expand Up @@ -314,3 +314,18 @@ def check_ignore(self, target):
if matches:
return CheckIgnoreResult(target, True, matches)
return CheckIgnoreResult(target, False, ["::"])


def init(path):
dvcignore = os.path.join(path, DvcIgnore.DVCIGNORE_FILE)
if os.path.exists(dvcignore):
return dvcignore

with open(dvcignore, "w") as fobj:
fobj.write(
"# Add patterns of files dvc should ignore, which could improve\n"
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
"# Add patterns of files dvc should ignore, which could improve\n"
"# Add patterns of files DVC should ignore, which could improve\n"

"# the performance. Learn more at\n"
"# https://dvc.org/doc/user-guide/dvcignore\n"
)

return dvcignore
7 changes: 6 additions & 1 deletion dvc/repo/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from dvc import analytics
from dvc.config import Config
from dvc.exceptions import InitError, InvalidArgumentError
from dvc.ignore import init as init_dvcignore
from dvc.repo import Repo
from dvc.scm import SCM
from dvc.scm.base import SCMError
Expand Down Expand Up @@ -99,9 +100,13 @@ def init(root_dir=os.curdir, no_scm=False, force=False, subdir=False):
with config.edit() as conf:
conf["core"]["no_scm"] = True

dvcignore = init_dvcignore(root_dir)

proj = Repo(root_dir)

scm.add([config.files["repo"], proj.plot_templates.templates_dir])
scm.add(
[config.files["repo"], dvcignore, proj.plot_templates.templates_dir]
)

if scm.ignore_file:
scm.add([os.path.join(dvc_dir, scm.ignore_file)])
Expand Down
1 change: 1 addition & 0 deletions tests/func/test_ignore.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ def test_ignore_on_branch(tmp_dir, scm, dvc):
assert set(dvc.tree.walk_files(path)) == {
path / "foo",
path / "bar",
path / ".dvcignore",
}

dvc.tree = scm.get_tree("branch", use_dvcignore=True)
Expand Down
10 changes: 10 additions & 0 deletions tests/func/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,13 @@ def test_subdir_init_no_option(tmp_dir, scm, monkeypatch, caplog):
"`--subdir` if initializing inside a subdirectory of a parent SCM "
"repository.".format(os.fspath(tmp_dir / "subdir"))
) in caplog.text


def test_gen_dvcignore(tmp_dir):
DvcRepo.init(no_scm=True)
text = (
"# Add patterns of files dvc should ignore, which could improve\n"
"# the performance. Learn more at\n"
"# https://dvc.org/doc/user-guide/dvcignore\n"
)
assert text == (tmp_dir / ".dvcignore").read_text()
9 changes: 9 additions & 0 deletions tests/func/test_ls.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ def test_ls_repo(tmp_dir, dvc, scm):
match_files(
files,
(
((".dvcignore",), False),
((".gitignore",), False),
(("README.md",), False),
(("structure.xml.dvc",), False),
Expand Down Expand Up @@ -89,6 +90,7 @@ def test_ls_repo_with_color(tmp_dir, dvc, scm, mocker, monkeypatch, caplog):

assert caplog.records[-1].msg == "\n".join(
[
".dvcignore",
".gitignore",
"README.md",
"\x1b[01;34mdata\x1b[0m",
Expand All @@ -107,6 +109,7 @@ def test_ls_repo_recursive(tmp_dir, dvc, scm):
match_files(
files,
(
((".dvcignore",), False),
((".gitignore",), False),
(("README.md",), False),
(("structure.xml.dvc",), False),
Expand Down Expand Up @@ -258,6 +261,7 @@ def test_ls_repo_with_removed_dvc_dir(tmp_dir, dvc, scm):
(("out.dvc",), False),
(("dep",), True),
(("out",), False),
((".dvcignore",), False),
((".gitignore",), False),
),
)
Expand All @@ -275,6 +279,7 @@ def test_ls_repo_with_removed_dvc_dir_recursive(tmp_dir, dvc, scm):
(("out.dvc",), False),
(("dep",), True),
(("out", "file"), True),
((".dvcignore",), False),
((".gitignore",), False),
),
)
Expand Down Expand Up @@ -306,6 +311,7 @@ def test_ls_remote_repo(erepo_dir):
match_files(
files,
(
((".dvcignore",), False),
((".gitignore",), False),
(("README.md",), False),
(("structure.xml.dvc",), False),
Expand All @@ -326,6 +332,7 @@ def test_ls_remote_repo_recursive(erepo_dir):
match_files(
files,
(
((".dvcignore",), False),
((".gitignore",), False),
(("README.md",), False),
(("structure.xml.dvc",), False),
Expand Down Expand Up @@ -393,6 +400,7 @@ def test_ls_remote_repo_with_rev(erepo_dir):
match_files(
files,
(
((".dvcignore",), False),
((".gitignore",), False),
(("README.md",), False),
(("model",), False),
Expand Down Expand Up @@ -422,6 +430,7 @@ def test_ls_remote_repo_with_rev_recursive(erepo_dir):
(("model", "people.csv"), True),
(("model", ".gitignore"), False),
(("structure.xml",), True),
((".dvcignore",), False),
((".gitignore",), False),
),
)
Expand Down