hgext/bookflow.py
author Manuel Jacob <me@manueljacob.de>
Mon, 11 Jul 2022 01:51:20 +0200
branchstable
changeset 49378 094a5fa3cf52
parent 48875 6000f5b25c9b
child 50775 3c8a31be81df
permissions -rw-r--r--
procutil: make stream detection in make_line_buffered more correct and strict In make_line_buffered(), we don’t want to wrap the stream if we know that lines get flushed to the underlying raw stream already. Previously, the heuristic was too optimistic. It assumed that any stream which is not an instance of io.BufferedIOBase doesn’t need wrapping. However, there are buffered streams that aren’t instances of io.BufferedIOBase, like Mercurial’s own winstdout. The new logic is different in two ways: First, only for the check, if unwraps any combination of WriteAllWrapper and winstdout. Second, it skips wrapping the stream only if it is an instance of io.RawIOBase (or already wrapped). If it is an instance of io.BufferedIOBase, it gets wrapped. In any other case, the function raises an exception. This ensures that, if an unknown stream is passed or we add another wrapper in the future, we don’t wrap the stream if it’s already line buffered or not wrap the stream if it’s not line buffered. In fact, this was already helpful during development of this change. Without it, I possibly would have forgot that WriteAllWrapper needs to be ignored for the check, leading to unnecessary wrapping if stdout is unbuffered. The alternative would have been to always wrap unknown streams. However, I don’t think that anyone would benefit from being less strict. We can expect streams from the standard library to be subclassing either io.RawIOBase or io.BufferedIOBase, so running Mercurial in the standard way should not regress by this change. Py2exe might replace sys.stdout and sys.stderr, but that currently breaks Mercurial anyway and also these streams don’t claim to be interactive, so this function is not called for them.

"""implements bookmark-based branching (EXPERIMENTAL)

 - Disables creation of new branches (config: enable_branches=False).
 - Requires an active bookmark on commit (config: require_bookmark=True).
 - Doesn't move the active bookmark on update, only on commit.
 - Requires '--rev' for moving an existing bookmark.
 - Protects special bookmarks (config: protect=@).

 flow related commands

    :hg book NAME: create a new bookmark
    :hg book NAME -r REV: move bookmark to revision (fast-forward)
    :hg up|co NAME: switch to bookmark
    :hg push -B .: push active bookmark
"""

from mercurial.i18n import _
from mercurial import (
    bookmarks,
    commands,
    error,
    extensions,
    registrar,
)

MY_NAME = b'bookflow'

configtable = {}
configitem = registrar.configitem(configtable)

configitem(MY_NAME, b'protect', [b'@'])
configitem(MY_NAME, b'require-bookmark', True)
configitem(MY_NAME, b'enable-branches', False)

cmdtable = {}
command = registrar.command(cmdtable)


def commit_hook(ui, repo, **kwargs):
    active = repo._bookmarks.active
    if active:
        if active in ui.configlist(MY_NAME, b'protect'):
            raise error.Abort(
                _(b'cannot commit, bookmark %s is protected') % active
            )
        if not cwd_at_bookmark(repo, active):
            raise error.Abort(
                _(
                    b'cannot commit, working directory out of sync with active bookmark'
                ),
                hint=_(b"run 'hg up %s'") % active,
            )
    elif ui.configbool(MY_NAME, b'require-bookmark', True):
        raise error.Abort(_(b'cannot commit without an active bookmark'))
    return 0


def bookmarks_update(orig, repo, parents, node):
    if len(parents) == 2:
        # called during commit
        return orig(repo, parents, node)
    else:
        # called during update
        return False


def bookmarks_addbookmarks(
    orig, repo, tr, names, rev=None, force=False, inactive=False
):
    if not rev:
        marks = repo._bookmarks
        for name in names:
            if name in marks:
                raise error.Abort(
                    _(
                        b"bookmark %s already exists, to move use the --rev option"
                    )
                    % name
                )
    return orig(repo, tr, names, rev, force, inactive)


def commands_commit(orig, ui, repo, *args, **opts):
    commit_hook(ui, repo)
    return orig(ui, repo, *args, **opts)


def commands_pull(orig, ui, repo, *args, **opts):
    rc = orig(ui, repo, *args, **opts)
    active = repo._bookmarks.active
    if active and not cwd_at_bookmark(repo, active):
        ui.warn(
            _(
                b"working directory out of sync with active bookmark, run "
                b"'hg up %s'"
            )
            % active
        )
    return rc


def commands_branch(orig, ui, repo, label=None, **opts):
    if label and not opts.get('clean') and not opts.get('rev'):
        raise error.Abort(
            _(
                b"creating named branches is disabled and you should use bookmarks"
            ),
            hint=b"see 'hg help bookflow'",
        )
    return orig(ui, repo, label, **opts)


def cwd_at_bookmark(repo, mark):
    mark_id = repo._bookmarks[mark]
    cur_id = repo.lookup(b'.')
    return cur_id == mark_id


def uisetup(ui):
    extensions.wrapfunction(bookmarks, b'update', bookmarks_update)
    extensions.wrapfunction(bookmarks, b'addbookmarks', bookmarks_addbookmarks)
    extensions.wrapcommand(commands.table, b'commit', commands_commit)
    extensions.wrapcommand(commands.table, b'pull', commands_pull)
    if not ui.configbool(MY_NAME, b'enable-branches'):
        extensions.wrapcommand(commands.table, b'branch', commands_branch)