import os

from dvc.cache import Cache
from dvc.repo import Repo
from dvc.system import System
from dvc.utils.compat import fspath


def test_destroy(tmp_dir, dvc):
    dvc.config.set("cache", "type", "symlink")
    dvc.cache = Cache(dvc)

    tmp_dir.dvc_gen("file", "text")
    tmp_dir.dvc_gen({"dir": {"file": "lorem", "subdir/file": "ipsum"}})

    dvc.destroy()

    # Remove all the files related to DVC
    for path in [".dvc", "file.dvc", "dir.dvc"]:
        assert not (tmp_dir / path).exists()

    # Leave the rest of the files
    for path in ["file", "dir/file", "dir/subdir/file"]:
        assert (tmp_dir / path).is_file()

    # Make sure that data was unprotected after `destroy`
    for path in ["file", "dir", "dir/file", "dir/subdir", "dir/subdir/file"]:
        assert not System.is_symlink(fspath(tmp_dir / path))


def test_collect(tmp_dir, scm, dvc, run_copy):
    def collect_outs(*args, **kwargs):
        return set(
            str(out.path_info)
            for stage in dvc.collect(*args, **kwargs)
            for out in stage.outs
        )

    tmp_dir.dvc_gen("foo", "foo")
    run_copy("foo", "bar")
    assert collect_outs("bar.dvc", with_deps=True) == {"foo", "bar"}

    run_copy("bar", "buzz")
    assert collect_outs("buzz.dvc", with_deps=True) == {"foo", "bar", "buzz"}
    assert collect_outs("buzz.dvc", with_deps=False) == {"buzz"}


def test_stages(tmp_dir, dvc):
    def stages():
        return set(stage.relpath for stage in Repo(fspath(tmp_dir)).stages)

    tmp_dir.dvc_gen({"file": "a", "dir/file": "b", "dir/subdir/file": "c"})

    assert stages() == {
        "file.dvc",
        os.path.join("dir", "file.dvc"),
        os.path.join("dir", "subdir", "file.dvc"),
    }

    tmp_dir.gen(".dvcignore", "dir")

    assert stages() == {"file.dvc"}

@@ -4,16 +4,53 @@
 ort os%0A%0A
+from dvc.scm.git.tree import GitTree%0A
 from dvc
@@ -1165,16 +1165,246 @@
  %22bar%22)%0A
+    scm.add(%5B%22.gitignore%22, %22foo.dvc%22, %22bar.dvc%22%5D)%0A    scm.commit(%22Add foo and bar%22)%0A%0A    scm.checkout(%22new-branch%22, create_new=True)%0A%0A    run_copy(%22bar%22, %22buzz%22)%0A    scm.add(%5B%22.gitignore%22, %22buzz.dvc%22%5D)%0A    scm.commit(%22Add buzz%22)%0A%0A
     asse
@@ -1473,32 +1473,52 @@
     
-run_copy(%22bar%22, %22buzz
+dvc.tree = GitTree(scm.repo, %22new-branch
 %22)%0A
+%0A
     

bring back the old branch logic