# HG changeset patch
# User Georges Racinet <georges.racinet@octobus.net>
# Date 1682526873 -7200
#      Wed Apr 26 18:34:33 2023 +0200
# Node ID 141dab178a071cba1e78753025932179e3a634b3
# Parent  32b0304d753bad5a40b23edec96d8f83fc36bd88
branch: avoid fallback analysis on empty repos

We've been worried to see so many "defaulting to slow direct
analysis" in the logs (especially in developer setups), as we
are on the verge to rely on the GitLab state files only in some
cases (RHGitaly).

It turns out that all such logs emitted by a local functional tests
run were actually about empty repos
(`heptapod-tests-run --heptapod-remote --heptapod-hg-native=full`).
It will be interesting to check foss.heptapod.net logs. In theory, we
should not see any such warning.

The early returns are of course much simpler, not sure if that
is a performance boost in general, though, since the condition will be
checked for all calls and most of them are not on empty repositories.

diff --git a/hgitaly/branch.py b/hgitaly/branch.py
--- a/hgitaly/branch.py
+++ b/hgitaly/branch.py
@@ -104,6 +104,9 @@
     :return: a :class:`changectx` or ``None`` if there's no changeset for
              the given ``gl_branch``.
     """
+    if not len(repo):
+        return None
+
     gl_branches = gitlab_branches(repo)
     if gl_branches is not GITLAB_BRANCHES_MISSING:
         sha = gl_branches.get(gl_branch)
@@ -165,6 +168,9 @@
       instance, ignoring lookup errors. If ``False``, yield targets as
       hexadecimal node ids.
     """
+    if not len(repo):
+        return
+
     gl_branches = gitlab_branches(repo)
     if gl_branches is not GITLAB_BRANCHES_MISSING:
         for branch, sha in gl_branches.items():
diff --git a/hgitaly/service/tests/test_mercurial_repository.py b/hgitaly/service/tests/test_mercurial_repository.py
--- a/hgitaly/service/tests/test_mercurial_repository.py
+++ b/hgitaly/service/tests/test_mercurial_repository.py
@@ -420,6 +420,7 @@
 
 def test_push_unexpected_error(push_fixture):
     wrapper, target_wrapper, push = push_fixture
+    wrapper.commit_file('foo')
 
     # we don't have many ways to trigger internal errors when
     # monkeypatch can't work (server is not in the same process):