# HG changeset patch
# User Georges Racinet <georges.racinet@octobus.net>
# Date 1587898154 -7200
#      Sun Apr 26 12:49:14 2020 +0200
# Node ID f425f770945ec2ee1ceacc169f91dc8fc36df530
# Parent  dfa26734ddd23abde7e7768b0ade9ff716a9eb53
Wiki: made `master` Git branch always identical to `branch/default`

GitLab wikis only read from `master`, and that's
pretty hardcoded, down the way to Gitaly, which itself embeds the
Gollum library, instead of just providing raw repo content.

Since we don't want to fork Gitaly, as long as we are using a Git mirror,
it's simpler to tie the `master` and `branch/default` Git branches
together in the case of wikis.

diff --git a/hgext3rd/heptapod/git.py b/hgext3rd/heptapod/git.py
--- a/hgext3rd/heptapod/git.py
+++ b/hgext3rd/heptapod/git.py
@@ -468,8 +468,22 @@
                 before_sha = existing.get(ref, ZERO_SHA)
                 if after_sha and after_sha != before_sha:
                     changes[ref] = GitRefChange(ref, before_sha, after_sha)
+
+        if self.is_wiki():
+            # GitLab wikis are much hardwired onto 'master' in a Git repo
+            on_default = changes.get(git_branch_ref('branch/default'))
+            if on_default is not None:
+                master = git_branch_ref('master')
+                changes[master] = GitRefChange(
+                    master, on_default.before, on_default.after)
+
         return changes
 
+    def is_wiki(self):
+        """Tell whether self.repo is the storage for a GitLab wiki"""
+        return (self.repo.ui.environ.get('GL_REPOSITORY', '')
+                .startswith('wiki-'))
+
     def gitlab_get_hook(self, name):
         hook = self._gl_hooks.get(name)
         if hook is None and gitlab is not None:
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
@@ -146,6 +146,29 @@
     ]
 
 
+def test_wiki(tmpdir, monkeypatch):
+    notifs = []
+    monkeypatch.setattr(gitlab, 'Hook', recording_hook(notifs))
+    repo_path = tmpdir.join('repo.hg')
+    wrapper, base_ctx = make_main_repo(repo_path)
+    wrapper.repo.ui.environ['GL_REPOSITORY'] = 'wiki-251'
+    git_repo = GitRepo.init(tmpdir.join('repo.git'))
+    wrapper.command('gitlab-mirror')
+
+    # for wikis, we duplicate `branch/default` as `master`
+    assert git_repo.branch_titles() == {'branch/default': 'default1',
+                                        'master': 'default1',
+                                        }
+    sha = git_repo.branches()['branch/default']['sha']
+    changes = {'refs/heads/branch/default': (ZERO_SHA, sha),
+               'refs/heads/master': (ZERO_SHA, sha),
+               }
+    assert notifs == [
+        ('pre-receive', changes),
+        ('post-receive', changes),
+    ]
+
+
 def test_tags(tmpdir, monkeypatch):
     notifs = []
     monkeypatch.setattr(gitlab, 'Hook', recording_hook(notifs))