Repository: iterative/dvc

Issue:
diff: unexpected error when rev_a & rev_b point to the same revision
## Description

`dvc diff <rev_a> <rev_b>` outputs `ERROR: unexpected error - '<rev_a>'` when <rev_a> and <rev_b> happen to point to the same revision.

### Reproduce

There is a Git repo with 4 branches:
<img width="400" src="https://github.com/iterative/dvc/assets/12026360/087d85eb-56e0-4f6c-8904-bc0817bceb22">
- *main* & *new-1* point to the same revision
- *new-main* & *new-2* point to the same revision

Run the following commands:

1. `dvc diff main new-main`
2. `dvc diff main new-1`
3. `dvc diff new-main new-2`

### Expected

All the commands should work.

However, only the 1st command work and the others fail with unexpected error:

```console
root@dc53a8a4f5c3:~/data/my-ds/VC-Project-Dev# dvc diff main new-main
Added:
    data/raw/new_dir/f1.txt

Deleted:
    data/raw/new_dir1/new.txt
    data/rsc/images/f1.txt

Modified:
    data/

files summary: 1 added, 2 deleted
root@dc53a8a4f5c3:~/data/my-ds/VC-Project-Dev# dvc diff main new-1
ERROR: unexpected error - 'main'                                      

Having any troubles? Hit us up at https://dvc.org/support, we are always happy to help!
root@dc53a8a4f5c3:~/data/my-ds/VC-Project-Dev# dvc diff new-main new-2
ERROR: unexpected error - 'new-main'                                  

Having any troubles? Hit us up at https://dvc.org/support, we are always happy to help!
```

With verbose output:

```console
# dvc diff -v new-main new-2
2023-06-14 10:03:55,347 DEBUG: v3.0.0 (pip), CPython 3.8.12 on Linux-5.4.0-84-generic-x86_64-with-glibc2.10
2023-06-14 10:03:55,347 DEBUG: command: /opt/conda/bin/dvc diff -v new-main new-2
2023-06-14 10:03:56,359 ERROR: unexpected error - 'new-main'          
Traceback (most recent call last):
  File "/opt/conda/lib/python3.8/site-packages/dvc/cli/__init__.py", line 209, in main
    ret = cmd.do_run()
  File "/opt/conda/lib/python3.8/site-packages/dvc/cli/command.py", line 26, in do_run
    return self.run()
  File "/opt/conda/lib/python3.8/site-packages/dvc/commands/diff.py", line 123, in run
    diff = self.repo.diff(self.args.a_rev, self.args.b_rev, self.args.targets)
  File "/opt/conda/lib/python3.8/site-packages/dvc/repo/__init__.py", line 64, in wrapper
    return f(repo, *args, **kwargs)
  File "/opt/conda/lib/python3.8/site-packages/dvc/repo/diff.py", line 159, in diff
    old = indexes[a_rev]
KeyError: 'new-main'

2023-06-14 10:03:56,573 DEBUG: Version info for developers:
DVC version: 3.0.0 (pip)
------------------------
Platform: Python 3.8.12 on Linux-5.4.0-84-generic-x86_64-with-glibc2.10
Subprojects:
        dvc_data = 1.11.0
        dvc_objects = 0.23.0
        dvc_render = 0.5.3
        dvc_task = 0.3.0
        scmrepo = 1.0.3
Supports:
        http (aiohttp = 3.8.4, aiohttp-retry = 2.8.3),
        https (aiohttp = 3.8.4, aiohttp-retry = 2.8.3),
        s3 (s3fs = 2023.6.0, boto3 = 1.26.76)
Config:
        Global: /mlsteam/.config/dvc
        System: /etc/xdg/dvc
Cache types: <https://error.dvc.org/no-dvc-cache>
Caches: local
Remotes: s3
Workspace directory: xfs on /dev/sdb1
Repo: dvc, git
Repo.site_cache_dir: /var/tmp/dvc/repo/91479b46e9e0a07ae16a57a5b4209cf5

Having any troubles? Hit us up at https://dvc.org/support, we are always happy to help!
2023-06-14 10:03:56,577 DEBUG: Analytics is disabled.
```

### Environment information

**Output of `dvc doctor`:**

```console
# dvc doctor
DVC version: 3.0.0 (pip)
------------------------
Platform: Python 3.8.12 on Linux-5.4.0-84-generic-x86_64-with-glibc2.10
Subprojects:
        dvc_data = 1.11.0
        dvc_objects = 0.23.0
        dvc_render = 0.5.3
        dvc_task = 0.3.0
        scmrepo = 1.0.3
Supports:
        http (aiohttp = 3.8.4, aiohttp-retry = 2.8.3),
        https (aiohttp = 3.8.4, aiohttp-retry = 2.8.3),
        s3 (s3fs = 2023.6.0, boto3 = 1.26.76)
Config:
        Global: /mlsteam/.config/dvc
        System: /etc/xdg/dvc
Cache types: <https://error.dvc.org/no-dvc-cache>
Caches: local
Remotes: s3
Workspace directory: xfs on /dev/sdb1
Repo: dvc, git
Repo.site_cache_dir: /var/tmp/dvc/repo/91479b46e9e0a07ae16a57a5b4209cf5
```



Discussion:
Looks like [our brancher concats the revs into](https://github.com/iterative/dvc/blob/main/dvc/repo/brancher.py#L96) `"main,newmain"`, which might be intended in some other place, but obviously is not inteded here. Taking a closer look...

Resolving patch:
diff --git a/dvc/repo/diff.py b/dvc/repo/diff.py
--- a/dvc/repo/diff.py
+++ b/dvc/repo/diff.py
@@ -156,8 +156,13 @@ def onerror(target, _exc):
         for target in old_missing & new_missing:
             raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), target)
 
-    old = indexes[a_rev]
-    new = indexes[b_rev]
+    if len(indexes.keys()) == 1:
+        # both a_rev and b_rev point to the same sha, nothing to compare
+        old = None
+        new = None
+    else:
+        old = indexes[a_rev]
+        new = indexes[b_rev]
 
     with ui.status("Calculating diff"):
         return _diff(old, new, with_missing=with_missing)


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
@@ -21,6 +21,14 @@ def test_no_scm(tmp_dir, dvc):
         dvc.diff()
 
 
+def test_same_rev(tmp_dir, scm, dvc):
+    tmp_dir.dvc_gen("file", "text", commit="add file")
+
+    assert not dvc.diff("HEAD", "HEAD")
+    assert not dvc.diff("HEAD", "master")
+    assert not dvc.diff("master", "HEAD")
+
+
 def test_added(tmp_dir, scm, dvc):
     tmp_dir.dvc_gen("file", "text")