Repository: readthedocs/readthedocs.org

Issue:
Remove the `LOCAL_GIT_BRANCHES` setting
We introduced this here to make tests work, but it isn't how things work in production: https://github.com/rtfd/readthedocs.org/pull/4984

We should remove this setting, and make our tests work properly with remote repos. I see a couple ways we could handle this:

* The `make_get_branch` function could manipulate the git repo so that it has a *remote* branch instead of a local branch
* We could mock `repo.branches` in the places we use it. I went down this road briefly, but it was a little bit complex.

I think the first approach is best, but I'm not sure how to do it easily. 


Discussion:
> I think the first approach is best, but I'm not sure how to do it easily.

We should follow the same workflow that we do in production:

1. The repository is created by another person with all its branches: we can create a temporal repository
1. We clone it from there (localhost)
1. We start running our test over the cloned one instead of the originally created

That way, I suppose that branches will be remotes and the structure of the repository will be pretty similar to what we have in production.
There are some tests that use the clone step, I guess we only need to put that step in our tests

Resolving patch:
diff --git a/readthedocs/vcs_support/backends/git.py b/readthedocs/vcs_support/backends/git.py
--- a/readthedocs/vcs_support/backends/git.py
+++ b/readthedocs/vcs_support/backends/git.py
@@ -183,8 +183,6 @@ def branches(self):
         # ``repo.remotes.origin.refs`` returns remote branches
         if repo.remotes:
             branches += repo.remotes.origin.refs
-        if getattr(settings, 'LOCAL_GIT_BRANCHES', False):
-            branches += repo.branches
 
         for branch in branches:
             verbose_name = branch.name


Test patch:
diff --git a/readthedocs/rtd_tests/tests/test_backend.py b/readthedocs/rtd_tests/tests/test_backend.py
--- a/readthedocs/rtd_tests/tests/test_backend.py
+++ b/readthedocs/rtd_tests/tests/test_backend.py
@@ -13,6 +13,7 @@
 
 import django_dynamic_fixture as fixture
 import pytest
+import six
 from django.contrib.auth.models import User
 from mock import Mock, patch
 
@@ -49,7 +50,8 @@ def setUp(self):
         self.dummy_conf.submodules.include = ALL
         self.dummy_conf.submodules.exclude = []
 
-    def test_git_branches(self):
+    @patch('readthedocs.projects.models.Project.checkout_path')
+    def test_git_branches(self, checkout_path):
         repo_path = self.project.repo
         default_branches = [
             # comes from ``make_test_git`` function
@@ -63,15 +65,47 @@ def test_git_branches(self):
             '2.0.X',
             'release/2.0.0',
             'release/foo/bar',
+        ]
+        for branch in branches:
+            create_git_branch(repo_path, branch)
+
+        # Create dir where to clone the repo
+        local_repo = os.path.join(mkdtemp(), 'local')
+        os.mkdir(local_repo)
+        checkout_path.return_value = local_repo
+
+        repo = self.project.vcs_repo()
+        repo.clone()
+
+        self.assertEqual(
+            set(branches + default_branches),
+            {branch.verbose_name for branch in repo.branches},
+        )
+
+    @pytest.mark.skipif(six.PY2, reason='Only for python3')
+    @patch('readthedocs.projects.models.Project.checkout_path')
+    def test_git_branches_unicode(self, checkout_path):
+        repo_path = self.project.repo
+        default_branches = [
+            # comes from ``make_test_git`` function
+            'submodule',
+            'relativesubmodule',
+            'invalidsubmodule',
+        ]
+        branches = [
+            'master',
             'release-ünîø∂é',
         ]
         for branch in branches:
             create_git_branch(repo_path, branch)
 
+        # Create dir where to clone the repo
+        local_repo = os.path.join(mkdtemp(), 'local')
+        os.mkdir(local_repo)
+        checkout_path.return_value = local_repo
+
         repo = self.project.vcs_repo()
-        # We aren't cloning the repo,
-        # so we need to hack the repo path
-        repo.working_dir = repo_path
+        repo.clone()
 
         self.assertEqual(
             set(branches + default_branches),
diff --git a/readthedocs/rtd_tests/tests/test_celery.py b/readthedocs/rtd_tests/tests/test_celery.py
--- a/readthedocs/rtd_tests/tests/test_celery.py
+++ b/readthedocs/rtd_tests/tests/test_celery.py
@@ -126,10 +126,14 @@ def test_sync_repository(self):
     @patch('readthedocs.projects.tasks.api_v2')
     @patch('readthedocs.projects.models.Project.checkout_path')
     def test_check_duplicate_reserved_version_latest(self, checkout_path, api_v2):
-        checkout_path.return_value = self.project.repo
         create_git_branch(self.repo, 'latest')
         create_git_tag(self.repo, 'latest')
 
+        # Create dir where to clone the repo
+        local_repo = os.path.join(mkdtemp(), 'local')
+        os.mkdir(local_repo)
+        checkout_path.return_value = local_repo
+
         version = self.project.versions.get(slug=LATEST)
         sync_repository = tasks.UpdateDocsTaskStep()
         sync_repository.version = version
@@ -148,10 +152,14 @@ def test_check_duplicate_reserved_version_latest(self, checkout_path, api_v2):
     @patch('readthedocs.projects.tasks.api_v2')
     @patch('readthedocs.projects.models.Project.checkout_path')
     def test_check_duplicate_reserved_version_stable(self, checkout_path, api_v2):
-        checkout_path.return_value = self.project.repo
         create_git_branch(self.repo, 'stable')
         create_git_tag(self.repo, 'stable')
 
+        # Create dir where to clone the repo
+        local_repo = os.path.join(mkdtemp(), 'local')
+        os.mkdir(local_repo)
+        checkout_path.return_value = local_repo
+
         version = self.project.versions.get(slug=LATEST)
         sync_repository = tasks.UpdateDocsTaskStep()
         sync_repository.version = version
diff --git a/readthedocs/settings/test.py b/readthedocs/settings/test.py
--- a/readthedocs/settings/test.py
+++ b/readthedocs/settings/test.py
@@ -16,7 +16,6 @@ class CommunityTestSettings(CommunityDevSettings):
 
     DEBUG = False
     TEMPLATE_DEBUG = False
-    LOCAL_GIT_BRANCHES = True
 
     @property
     def LOGGING(self):  # noqa - avoid pep8 N802