Skip to content
Snippets Groups Projects
Commit 3c19e092 authored by Georges Racinet's avatar Georges Racinet
Browse files

GitLabStateMaintainer: adopted bookmarks processing method

The only changes are to the uniform "GitLab" terminology
(from "Git").
parent 22996806
No related branches found
No related tags found
1 merge request!50Separation of hg-git and generic parts in "mirroring" code
......@@ -36,7 +36,6 @@
from heptapod.gitlab.prune_reasons import (
HeadPruneReason,
TopicPublished,
AllHeadsBookmarked,
BranchClosed,
BookmarkRemoved,
WildHeadResolved,
......@@ -110,99 +109,6 @@
hg_sha_from_gitlab_sha = GitHandler.map_hg_get
gitlab_sha_from_hg_sha = GitHandler.map_git_get
def heptapod_gate_bookmarks(self, repo, allow, changes):
"""First handling of bookmark changes (refuse or not), return deleted.
:param repo: passed explicitely because filtering may differ from
:attr:`repo`.
:return: iterable of deleted bookmarks in the form
(name, previous Git hexadecimal SHA)
"""
if not changes:
return ()
ui = repo.ui
deleted = []
ui.note(b"HeptapodGitHandler bookmark changes=%r" % changes)
new_bookmarks = []
for name, change in changes.items():
if change[0] is None:
new_bookmarks.append(name)
elif change[1] is None:
deleted.append((name, self.map_git_get(hex(change[0]))))
if new_bookmarks and not allow:
raise error.Abort(_(
b"Creating bookmarks is forbidden by default in Heptapod "
b"(got these new ones: %s). "
b"See https://heptapod.net/pages/faq.html#bookmarks to "
b"learn why and how to partially lift "
b"that restriction" % util.format_bytes_list(new_bookmarks)))
for new_bm_name, (_ign, new_bm_node) in pycompat.iteritems(changes):
new_bm_ctx = repo[new_bm_node]
new_bm_topic = new_bm_ctx.topic()
if new_bm_topic:
raise error.Abort(_(
b"Creating or updating bookmarks on topic "
b"changesets is forbidden"),
hint=b"topic '%s' and bookmark '%s' for changeset %s" % (
new_bm_topic, new_bm_name, new_bm_ctx.hex()))
return deleted
def branchmap_entry_process_bookmarks(self, gitlab_branch,
entry, bookmarked, to_prune,
allow_bookmarks=False):
"""Bookmarks processing for a branchmap entry.
This remove bookmarked heads from the entry, scheduling the prune
for ``gitlab_branch`` into ``to_prune`` if no head remains.
We want to ignore bookmarked changesets because
- we don't want them to appear in the ``wild`` namespace
- the bookmarks are translated directly as Git branches
- no further Git ref would be relevant for them
We don't want to prune the branch if
- bookmarks aren't explicitely allowed,
because this may be a side effect, potentially disturbing, of
an implicit bookmark move during a push.
- ``gitlab_branch`` is the default GitLab branch. That would be
totally unexpected and very problematic for GitLab.
:param entry: the branchmap entry, with closed heads already filtered
out, and given as a list of :class:`changectx` instances.
:param bookmarked_shas: a set of changesets known to be
bookmarked, not restricted to be a subset of ``entry`` and given
as SHAs.
:returns: set of SHAs, or None
"""
hg_shas = {c.hex() for c in entry}
hg_shas.difference_update(bookmarked)
if hg_shas:
return hg_shas
if gitlab_branch == self.get_default_gitlab_branch():
# branchmap entry are in increasing revno order, but this is
# not a good time to make that simplification and performance
# does not matter for this corner case.
# if all entries are obsolete, we are reaching the best
# we can do: just not schedule the prune
rev_ctxs = [(c.rev(), c) for c in entry if not c.obsolete()]
rev_ctxs.sort()
for _r, ctx in reversed(rev_ctxs):
return {ctx.hex()}
if allow_bookmarks:
self.repo.ui.note(
b"HeptapodGitHandler: scheduling prune of Git branch '%s' "
b"because all its potential heads are bookmarked" % (
gitlab_branch))
to_prune[gitlab_branch] = AllHeadsBookmarked()
def validate_obsolescence(self, txn):
"""Abort if the given transaction creates unwanted obsolescence.
......
......@@ -5,6 +5,13 @@
#
# SPDX-License-Identifier: GPL-2.0-or-later
from mercurial import (
error,
)
from mercurial.node import hex
from mercurial.i18n import _
from heptapod import util
from heptapod.gitlab.branch import (
gitlab_branch_from_ref,
)
......@@ -8,6 +15,9 @@
from heptapod.gitlab.branch import (
gitlab_branch_from_ref,
)
from heptapod.gitlab.prune_reasons import (
AllHeadsBookmarked,
)
class GitLabStateMaintainer:
......@@ -42,3 +52,97 @@
def get_default_gitlab_branch(self):
return gitlab_branch_from_ref(self.get_default_gitlab_ref())
def heptapod_gate_bookmarks(self, repo, allow, changes):
"""First handling of bookmark changes (refuse or not), return deleted.
:param repo: passed explicitely because filtering may differ from
:attr:`repo`.
:return: iterable of deleted bookmarks in the form
(name, previous GitLab hexadecimal SHA)
"""
if not changes:
return ()
ui = repo.ui
deleted = []
ui.note(b"heptapod.gitlab.mirror bookmark changes=%r" % changes)
new_bookmarks = []
for name, change in changes.items():
if change[0] is None:
new_bookmarks.append(name)
elif change[1] is None:
deleted.append((name,
self.gitlab_sha_from_hg_sha(hex(change[0]))))
if new_bookmarks and not allow:
raise error.Abort(_(
b"Creating bookmarks is forbidden by default in Heptapod "
b"(got these new ones: %s). "
b"See https://heptapod.net/pages/faq.html#bookmarks to "
b"learn why and how to partially lift "
b"that restriction" % util.format_bytes_list(new_bookmarks)))
for new_bm_name, (_ign, new_bm_node) in changes.items():
new_bm_ctx = repo[new_bm_node]
new_bm_topic = new_bm_ctx.topic()
if new_bm_topic:
raise error.Abort(_(
b"Creating or updating bookmarks on topic "
b"changesets is forbidden"),
hint=b"topic '%s' and bookmark '%s' for changeset %s" % (
new_bm_topic, new_bm_name, new_bm_ctx.hex()))
return deleted
def branchmap_entry_process_bookmarks(self, gitlab_branch,
entry, bookmarked, to_prune,
allow_bookmarks=False):
"""Bookmarks processing for a branchmap entry.
This remove bookmarked heads from the entry, scheduling the prune
for ``gitlab_branch`` into ``to_prune`` if no head remains.
We want to ignore bookmarked changesets because
- we don't want them to appear in the ``wild`` namespace
- the bookmarks are translated directly as GitLab branches
- no further GitLab ref would be relevant for them
We don't want to prune the branch if
- bookmarks aren't explicitely allowed,
because this may be a side effect, potentially disturbing, of
an implicit bookmark move during a push.
- ``gitlab_branch`` is the default GitLab branch. That would be
totally unexpected and very problematic for GitLab.
:param entry: the branchmap entry, with closed heads already filtered
out, and given as a list of :class:`changectx` instances.
:param bookmarked_shas: a set of changesets known to be
bookmarked, not restricted to be a subset of ``entry`` and given
as SHAs.
:returns: set of SHAs, or None
"""
hg_shas = {c.hex() for c in entry}
hg_shas.difference_update(bookmarked)
if hg_shas:
return hg_shas
if gitlab_branch == self.get_default_gitlab_branch():
# branchmap entry are in increasing revno order, but this is
# not a good time to make that simplification and performance
# does not matter for this corner case.
# if all entries are obsolete, we are reaching the best
# we can do: just not schedule the prune
rev_ctxs = [(c.rev(), c) for c in entry if not c.obsolete()]
rev_ctxs.sort()
for _r, ctx in reversed(rev_ctxs):
return {ctx.hex()}
if allow_bookmarks:
self.repo.ui.note(
b"HeptapodGitHandler: scheduling prune of GitLab branch '%s' "
b"because all its potential heads are bookmarked" % (
gitlab_branch))
to_prune[gitlab_branch] = AllHeadsBookmarked()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment