# HG changeset patch
# User Georges Racinet <georges.racinet@octobus.net>
# Date 1589914573 -7200
#      Tue May 19 20:56:13 2020 +0200
# Branch heptapod-0-12
# Node ID 89d5c94a630e64eab39cf10ef551b37cfa116070
# Parent  83f11149174f2a8551217deb1f1d84321ce1388b
Clarity refactor and line lengths in tests

(were actually uncommitted local mods)

diff --git a/hgext3rd/heptapod/git.py b/hgext3rd/heptapod/git.py
--- a/hgext3rd/heptapod/git.py
+++ b/hgext3rd/heptapod/git.py
@@ -40,6 +40,19 @@
     return 'refs/heads/' + branch
 
 
+TOPIC_REF_PREFIX = git_branch_ref('topic/')
+
+NAMED_BRANCH_REF_PREFIX = git_branch_ref('branch/')
+
+
+def ref_is_topic(ref):
+    return ref.startswith(TOPIC_REF_PREFIX)
+
+
+def ref_is_named_branch(ref):
+    return ref.startswith(NAMED_BRANCH_REF_PREFIX)
+
+
 def parse_git_branch_ref(ref):
     """Parse a Git branch ref for named branch and topic information
 
@@ -109,6 +122,9 @@
         self.before = before
         self.after = after
 
+    def is_creation(self):
+        return self.before == ZERO_SHA and self.after != ZERO_SHA
+
 
 class HeptapodGitHandler(GitHandler):
 
@@ -524,17 +540,15 @@
         # a random value - we don't want it to select topics if possible
         # and if it has, we want that to change.
         default_branch_ref = self.git.refs.get_symrefs().get(b'HEAD')
-        update_default_branch = (
-            default_branch_ref not in git_refs
-            or default_branch_ref.startswith(git_branch_ref(b'topic/')))
+        update_default_branch = (default_branch_ref not in git_refs
+                                 or ref_is_topic(default_branch_ref))
         new_named_branch_refs = []
         for change in pycompat.itervalues(changes):
             if change.after == ZERO_SHA:
                 del git_refs[change.ref]
             else:
                 git_refs[change.ref] = change.after
-            if (change.before == ZERO_SHA
-                    and change.ref.startswith(git_branch_ref(b'branch/'))):
+            if change.is_creation() and ref_is_named_branch(change.ref):
                 new_named_branch_refs.append(change.ref)
 
         if update_default_branch and new_named_branch_refs:
diff --git a/hgext3rd/heptapod/tests/git/test_integration.py b/hgext3rd/heptapod/tests/git/test_integration.py
--- a/hgext3rd/heptapod/tests/git/test_integration.py
+++ b/hgext3rd/heptapod/tests/git/test_integration.py
@@ -252,7 +252,8 @@
     # just checking our assumptions
     assert git_repo.branch_titles() == {'branch/default': 'default1'}
 
-    # we need to test the branch masking on a branch that's not the GitLab default
+    # we need to test the branch masking on a branch
+    # that's not the GitLab default (which is protected)
     server.write_commit('foo', branch='other', message='other1')
 
     activate_mirror(server)
@@ -283,7 +284,9 @@
 
     wrapper = LocalRepoWrapper.init(tmpdir.join('repo.hg'), config=config)
     git_repo = GitRepo.init(tmpdir.join('repo.git'))
-    wrapper.write_commit('foo', message="other0", branch=branch_name, topic='initial')
+    wrapper.write_commit('foo', message="other0",
+                         branch=branch_name,
+                         topic='initial')
     wrapper.command('gitlab-mirror')
     # that's what something (maybe Gitaly) does on the GitLab side:
     git_repo.set_symref('HEAD', 'refs/heads/topic/%s/initial' % branch_name)