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

GitLabRefChange: moved to gitlab.change module

First step in extraction effort of common components between
hg-git based and fully native repos.

Still not fully decided about shortening it to `RefChange`.
parent 2024498a
No related branches found
No related tags found
1 merge request!50Separation of hg-git and generic parts in "mirroring" code
# 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
"""Generic GitLab integration logic.
"""
import attr
ZERO_SHA = b'0' * 40
"""GitLab zero SHA
Used to represent ref/branch creation or deletion.
As of this writing, it is actually equal to Mercurial's hex for null node,
and to the Git zero SHA, but could become different because of different rates
of adoption of SHA-2 in Mercurial, Git, and GitLab.
"""
@attr.s
class GitLabRefChange(object):
"""Represent a change to be performed in a target Git repo.
Public attributes:
- :attr:`ref`: Git ref name
- :attr:`before`, :attr:`after`: Git SHAs
These will be complemented with a system of options, e.g., to specify that
a topic change actually comes with publication, leading to a deferred
removal of the corresponding Git branch once all appropriate treatments are
done, whether this removal is performed from here or by GitLab.
"""
ref = attr.ib()
before = attr.ib()
after = attr.ib()
def is_creation(self):
return self.before == ZERO_SHA and self.after != ZERO_SHA
def export_for_hook(self, handler, native=False):
"""Prepare for actual hook sending.
This depends on the `heptapod.native` boolean config item.
"""
if native:
def hg_sha(git_sha):
if git_sha == ZERO_SHA:
return git_sha
return handler.map_hg_get(git_sha)
before_hg_sha = hg_sha(self.before)
if before_hg_sha is None:
# before is not known to Git, this is a corruption as in the
# old heptapod/hg-git#3 and we must recover by creating the
# Git branch anyway. On the 'after' side, it would mean
# a severe inconsistency on our side
# TODO in a later version we should
# not convert at all, but we still rely as Git being
# a giant cache of previous state of GitLab branches.
before_hg_sha = ZERO_SHA
after_hg_sha = hg_sha(self.after)
if after_hg_sha is None:
raise KeyError(self.after)
return before_hg_sha, after_hg_sha
return self.before, self.after
......@@ -10,8 +10,8 @@
sound too weird once we are in full native mode)
"""
import pytest
from ...git import (
GitRefChange as RefChange, # `as` for potential rename
from ..change import (
GitLabRefChange as RefChange, # `as` for potential rename
ZERO_SHA,
)
......
......@@ -10,5 +10,4 @@
all interesting things to do with Git.
"""
from __future__ import absolute_import
import attr
......@@ -14,5 +13,4 @@
from dulwich.protocol import ZERO_SHA
from dulwich.repo import check_ref_format
from hggit.git_handler import GitHandler
from heptapod import (
......@@ -27,6 +25,10 @@
ref_is_wild_branch,
InvalidGitLabBranch,
)
from heptapod.gitlab.change import (
ZERO_SHA,
GitLabRefChange as GitRefChange,
)
from heptapod.gitlab.hooks import (
PreReceive,
PostReceive,
......@@ -60,6 +62,7 @@
write_gitlab_branches,
)
DOT_BYTE = ord(b'.')
......@@ -67,57 +70,6 @@
pass
@attr.s
class GitRefChange(object):
"""Represent a change to be performed in a target Git repo.
Public attributes:
- :attr:`ref`: Git ref name
- :attr:`before`, :attr:`after`: Git SHAs
These will be complemented with a system of options, e.g., to specify that
a topic change actually comes with publication, leading to a deferred
removal of the corresponding Git branch once all appropriate treatments are
done, whether this removal is performed from here or by GitLab.
"""
ref = attr.ib()
before = attr.ib()
after = attr.ib()
def is_creation(self):
return self.before == ZERO_SHA and self.after != ZERO_SHA
def export_for_hook(self, handler, native=False):
"""Prepare for actual hook sending.
This depends on the `heptapod.native` boolean config item.
"""
if native:
def hg_sha(git_sha):
if git_sha == ZERO_SHA:
return git_sha
return handler.map_hg_get(git_sha)
before_hg_sha = hg_sha(self.before)
if before_hg_sha is None:
# before is not known to Git, this is a corruption as in the
# old heptapod/hg-git#3 and we must recover by creating the
# Git branch anyway. On the 'after' side, it would mean
# a severe inconsistency on our side
# TODO in a later version we should
# not convert at all, but we still rely as Git being
# a giant cache of previous state of GitLab branches.
before_hg_sha = ZERO_SHA
after_hg_sha = hg_sha(self.after)
if after_hg_sha is None:
raise KeyError(self.after)
return before_hg_sha, after_hg_sha
return self.before, self.after
class HeptapodGitHandler(GitHandler):
def __init__(self, *args, **kwargs):
......
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