Repository: iterative/dvc

Issue:
`diff`: shows all files as renamed for repo created using `init --subdir`
@efiop looks like the change that introduce the [renamed](https://github.com/iterative/dvc/issues/7756#issuecomment-1129443387) issue was this one: https://github.com/iterative/dvc/commit/a80a85e6bd144189bf63df535483ae628136ce14.

Was there anything in there that would make a subrepo exhibit this diff no matter what (`~/vscode-dvc` is the git repo, `~/vscode-dvc/demo` is the dvc project):

```bash
~/vscode-dvc/demo ❯  dvc diff
Renamed:                                                              
    demo/data/MNIST/raw/ -> data/MNIST/raw/
    demo/data/MNIST/raw/t10k-images-idx3-ubyte -> data/MNIST/raw/t10k-images-idx3-ubyte
    demo/data/MNIST/raw/t10k-images-idx3-ubyte.gz -> data/MNIST/raw/t10k-images-idx3-ubyte.gz
    demo/data/MNIST/raw/t10k-labels-idx1-ubyte -> data/MNIST/raw/t10k-labels-idx1-ubyte
    demo/data/MNIST/raw/t10k-labels-idx1-ubyte.gz -> data/MNIST/raw/t10k-labels-idx1-ubyte.gz
    demo/data/MNIST/raw/train-images-idx3-ubyte -> data/MNIST/raw/train-images-idx3-ubyte
    demo/data/MNIST/raw/train-images-idx3-ubyte.gz -> data/MNIST/raw/train-images-idx3-ubyte.gz
    demo/data/MNIST/raw/train-labels-idx1-ubyte -> data/MNIST/raw/train-labels-idx1-ubyte
    demo/data/MNIST/raw/train-labels-idx1-ubyte.gz -> data/MNIST/raw/train-labels-idx1-ubyte.gz
    demo/misclassified.jpg -> misclassified.jpg
    demo/model.pt -> model.pt
    demo/predictions.json -> predictions.json
    demo/training_metrics.json -> training_metrics.json
    demo/training_metrics/ -> training_metrics/
    demo/training_metrics/scalars/acc.tsv -> training_metrics/scalars/acc.tsv
    demo/training_metrics/scalars/loss.tsv -> training_metrics/scalars/loss.tsv
```

I can see that scmrepo got bumped from `0.0.19` to `0.0.22` in that commit.

LMK if you want a separate issue for this.

Thanks.

_Originally posted by @mattseddon in https://github.com/iterative/dvc/issues/7756#issuecomment-1129633756_


Discussion:

Resolving patch:
diff --git a/dvc/repo/brancher.py b/dvc/repo/brancher.py
--- a/dvc/repo/brancher.py
+++ b/dvc/repo/brancher.py
@@ -38,8 +38,10 @@ def brancher(  # noqa: E302
         )
 
     cwd_parts = ()
-    if self.fs.path.isin(self.fs.path.getcwd(), self.root_dir):
-        cwd_parts = self.fs.path.relparts(self.fs.path.getcwd(), self.root_dir)
+    if self.fs.path.isin(self.fs.path.getcwd(), self.scm.root_dir):
+        cwd_parts = self.fs.path.relparts(
+            self.fs.path.getcwd(), self.scm.root_dir
+        )
 
     saved_fs = self.fs
     saved_root = self.root_dir


Test patch:
diff --git a/tests/func/test_diff.py b/tests/func/test_diff.py
--- a/tests/func/test_diff.py
+++ b/tests/func/test_diff.py
@@ -117,6 +117,37 @@ def test_modified(tmp_dir, scm, dvc):
     }
 
 
+def test_modified_subrepo(tmp_dir, scm, dvc):
+    from dvc.repo import Repo
+
+    tmp_dir.gen({"subdir": {"file": "first"}})
+    subrepo_dir = tmp_dir / "subdir"
+
+    with subrepo_dir.chdir():
+        subrepo = Repo.init(subdir=True)
+        subrepo.add("file")
+
+    scm.add(os.path.join("subdir", "file.dvc"))
+    scm.commit("init")
+
+    (subrepo_dir / "file").write_text("second")
+
+    with subrepo_dir.chdir():
+        subrepo = Repo()
+        assert subrepo.diff() == {
+            "added": [],
+            "deleted": [],
+            "modified": [
+                {
+                    "path": "file",
+                    "hash": {"old": digest("first"), "new": digest("second")},
+                }
+            ],
+            "not in cache": [],
+            "renamed": [],
+        }
+
+
 def test_refs(tmp_dir, scm, dvc):
     tmp_dir.dvc_gen("file", "first", commit="first version")
     tmp_dir.dvc_gen("file", "second", commit="second version")