# HG changeset patch
# User Georges Racinet <georges.racinet@octobus.net>
# Date 1592345565 -7200
#      Wed Jun 17 00:12:45 2020 +0200
# Node ID 8f37591c0dcc692411a971ab008255adafa3a409
# Parent  8738f8e1ab9fb74194f45c5de4f3fd1f3c433046
default branch: getting method, in RefService

this is where reading the default branch is implemented.

diff --git a/conftest.py b/conftest.py
--- a/conftest.py
+++ b/conftest.py
@@ -1,7 +1,9 @@
 import pytest
 
 from hgitaly.service.commit import CommitServicer
+from hgitaly.service.ref import RefServicer
 from hgitaly.stub.commit_pb2_grpc import add_CommitServiceServicer_to_server
+from hgitaly.stub.ref_pb2_grpc import add_RefServiceServicer_to_server
 
 
 @pytest.fixture(scope='module')
@@ -10,6 +12,8 @@
 
     add_CommitServiceServicer_to_server(CommitServicer(repos_root_bytes),
                                         _grpc_server)
+    add_RefServiceServicer_to_server(RefServicer(repos_root_bytes),
+                                     _grpc_server)
     _grpc_server.add_insecure_port(grpc_addr)
     _grpc_server.start()
     yield _grpc_server
diff --git a/hgitaly/service/ref.py b/hgitaly/service/ref.py
new file mode 100644
--- /dev/null
+++ b/hgitaly/service/ref.py
@@ -0,0 +1,35 @@
+# Copyright 2020 Georges Racinet <georges.racinet@octobus.net>
+#
+# This software may be used and distributed according to the terms of the
+# GNU General Public License version 2 or any later version.
+#
+# SPDX-License-Identifier: GPL-2.0-or-later
+import logging
+
+from hgext3rd.heptapod.branch import get_default_gitlab_branch
+
+from ..stub.ref_pb2 import (
+    FindDefaultBranchNameRequest,
+    FindDefaultBranchNameResponse,
+)
+from ..stub.ref_pb2_grpc import RefServiceServicer
+from ..servicer import HGitalyServicer
+
+logger = logging.getLogger(__name__)
+DEFAULT_BRANCH_FILE_NAME = b'default_gitlab_branch'
+
+
+class RefServicer(RefServiceServicer, HGitalyServicer):
+    """RefService implementation.
+    """
+    def FindDefaultBranchName(
+            self,
+            request: FindDefaultBranchNameRequest,
+            context) -> FindDefaultBranchNameResponse:
+        repo = self.load_repo(request.repository)
+        branch = get_default_gitlab_branch(repo)
+        if branch is None:
+            # not set, 'branch/default' is not worse than the 'master'
+            # that standard GitLab has for Git repos
+            branch = b'branch/default'
+        return FindDefaultBranchNameResponse(name=branch)
diff --git a/hgitaly/tests/test_default_branch.py b/hgitaly/tests/test_default_branch.py
new file mode 100644
--- /dev/null
+++ b/hgitaly/tests/test_default_branch.py
@@ -0,0 +1,37 @@
+# Copyright 2020 Georges Racinet <georges.racinet@octobus.net>
+#
+# This software may be used and distributed according to the terms of the
+# GNU General Public License version 2 or any later version.
+#
+# SPDX-License-Identifier: GPL-2.0-or-later
+from mercurial import pycompat
+
+from heptapod.testhelpers import LocalRepoWrapper
+
+from ..stub.ref_pb2 import FindDefaultBranchNameRequest
+from ..stub.ref_pb2_grpc import RefServiceStub
+from ..stub.shared_pb2 import Repository
+from ..service.ref import DEFAULT_BRANCH_FILE_NAME
+
+DEFAULT_BRANCH_FILE_NAME = pycompat.sysstr(DEFAULT_BRANCH_FILE_NAME)
+
+
+def test_default_branch(grpc_channel, server_repos_root):
+    grpc_stub = RefServiceStub(grpc_channel)
+    repo_path = server_repos_root / 'repo1'
+    LocalRepoWrapper.init(repo_path)
+
+    grpc_repo = Repository(relative_path='repo1')
+
+    request = FindDefaultBranchNameRequest(repository=grpc_repo)
+    response = grpc_stub.FindDefaultBranchName(request)
+    assert response.name == b'branch/default'
+
+    # TODO replace by actual request for setting default branch when it's
+    # implemented
+    with (repo_path / '.hg' / DEFAULT_BRANCH_FILE_NAME).open('w') as f:
+        f.write('branch/other')
+    request = FindDefaultBranchNameRequest(
+        repository=Repository(relative_path='repo1'))
+    response = grpc_stub.FindDefaultBranchName(request)
+    assert response.name == b'branch/other'