Repository: iterative/dvc

Issue:
dvc get from dvc-branch in not dvc-repo
```
$ dvc -V
0.70.0+38be14
```

`master` branch in https://github.com/dmpetrov/test_repo is NOT a DVC-repo. However, the extracted branch `b-test` IS DVC-repo and should work:

```
$ dvc get https://github.com/dmpetrov/test_repo --rev  b-test b-file
ERROR: failed to get 'b-file' from 'https://github.com/dmpetrov/test_repo' - URL 'https://github.com/dmpetrov/test_repo' is not a dvc repository.
```

This a regression since this command worked before: `dvc get https://github.com/topusOctopus/mmdetection/ --rev fasterRCNN_resnet50_support model_weights/faster_rcnn_r50_c4_2x-6e4fdf4f.pth`


Discussion:
Reproduction script:
```
#!/bin/bash

rm -rf repo remote_repo storage git_repo
mkdir repo remote_repo storage git_repo

maindir=$(pwd)
pushd remote_repo
git init >> /dev/null 

echo README >> README.md
git add README.md
git commit -m "add readme"

git checkout -b dvc_branch
dvc init -q
echo data >> data
dvc add -q data
dvc remote add -d str $maindir/storage
git add .dvc/config data.dvc .gitignore
dvc push -q

popd
pushd repo

git init >> /dev/null && dvc init -q

set -x
set -e

dvc get $maindir/remote_repo --rev dvc_branch data
```

Resolving patch:
diff --git a/dvc/external_repo.py b/dvc/external_repo.py
--- a/dvc/external_repo.py
+++ b/dvc/external_repo.py
@@ -61,11 +61,14 @@ def _external_repo(url=None, rev=None, cache_dir=None):
         REPO_CACHE[url, None, None] = clean_clone_path
 
     # Adjust new clone/copy to fit rev and cache_dir
+
+    # Checkout needs to be done first because current branch might not be
+    # DVC repository
+    if rev is not None:
+        _git_checkout(new_path, rev)
+
     repo = Repo(new_path)
     try:
-        if rev is not None:
-            repo.scm.checkout(rev)
-
         if cache_dir is not None:
             cache_config = CacheConfig(repo.config)
             cache_config.set_dir(cache_dir, level=Config.LEVEL_LOCAL)
@@ -77,6 +80,16 @@ def _external_repo(url=None, rev=None, cache_dir=None):
     return new_path
 
 
+def _git_checkout(repo_path, revision):
+    from dvc.scm import Git
+
+    git = Git(repo_path)
+    try:
+        git.checkout(revision)
+    finally:
+        git.close()
+
+
 def clean_repos():
     # Outside code should not see cache while we are removing
     repo_paths = list(REPO_CACHE.values())


Test patch:
diff --git a/tests/func/test_get.py b/tests/func/test_get.py
--- a/tests/func/test_get.py
+++ b/tests/func/test_get.py
@@ -1,6 +1,7 @@
 from __future__ import unicode_literals
 
 import filecmp
+import logging
 import os
 
 import pytest
@@ -11,6 +12,7 @@
 from dvc.repo import Repo
 from dvc.system import System
 from dvc.utils import makedirs
+from dvc.utils.compat import fspath
 from tests.utils import trees_equal
 
 
@@ -87,3 +89,19 @@ def test_get_to_dir(dname, erepo):
 
     assert os.path.isdir(dname)
     assert filecmp.cmp(erepo.FOO, dst, shallow=False)
+
+
+def test_get_from_non_dvc_master(erepo, tmp_path, monkeypatch, caplog):
+    monkeypatch.chdir(fspath(tmp_path))
+    erepo.dvc.scm.repo.index.remove([".dvc"], r=True)
+    erepo.dvc.scm.commit("remove .dvc")
+
+    caplog.clear()
+    imported_file = "foo_imported"
+    with caplog.at_level(logging.INFO, logger="dvc"):
+        Repo.get(erepo._root_dir, erepo.FOO, out=imported_file, rev="branch")
+
+    assert caplog.text == ""
+    assert filecmp.cmp(
+        os.path.join(erepo._root_dir, erepo.FOO), imported_file, shallow=False
+    )