hgext/bookflow.py
author Matt Harbison <matt_harbison@yahoo.com>
Thu, 03 Oct 2024 21:08:10 -0400
changeset 51949 a8e1ff9ac257
parent 51863 f4733654f144
child 52022 745409f94f0c
permissions -rw-r--r--
tests: fix a test hang on Windows when setting a debuglock I have no idea why, but running the `hg -R auto-upgrade debuglock --set-lock` command near the end of `test-upgrade-repo.t` hangs the test. It does background the process and `killdaemons.py` runs without error, but control doesn't return to `run-tests.py` until the process is manually killed. I did notice that `$!` in MSYS is *not* the PID of the process that got backgrounded, even when a simple `sleep 60 &` is run in MSYS without the *.t file. When `killdaemons.py` is run manually with the PID in ProcessExplorer, the backgrounded process terminates immediately, and returns control to `run-tests.py`. This looks like it would be a race, but the test waits 10s for the lock file to appear before attempting to kill the process, so there's time. `hg serve` has a `--pid-file` option to write the pid to the file, but this is only a debug command, so I'm not bothering with cluttering the command line.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
40834
9cec7a36bab8 bookflow: new extension for bookmark-based branching
idlsoft <idlsoft@gmail.com>
parents:
diff changeset
     1
"""implements bookmark-based branching (EXPERIMENTAL)
9cec7a36bab8 bookflow: new extension for bookmark-based branching
idlsoft <idlsoft@gmail.com>
parents:
diff changeset
     2
9cec7a36bab8 bookflow: new extension for bookmark-based branching
idlsoft <idlsoft@gmail.com>
parents:
diff changeset
     3
 - Disables creation of new branches (config: enable_branches=False).
9cec7a36bab8 bookflow: new extension for bookmark-based branching
idlsoft <idlsoft@gmail.com>
parents:
diff changeset
     4
 - Requires an active bookmark on commit (config: require_bookmark=True).
9cec7a36bab8 bookflow: new extension for bookmark-based branching
idlsoft <idlsoft@gmail.com>
parents:
diff changeset
     5
 - Doesn't move the active bookmark on update, only on commit.
9cec7a36bab8 bookflow: new extension for bookmark-based branching
idlsoft <idlsoft@gmail.com>
parents:
diff changeset
     6
 - Requires '--rev' for moving an existing bookmark.
9cec7a36bab8 bookflow: new extension for bookmark-based branching
idlsoft <idlsoft@gmail.com>
parents:
diff changeset
     7
 - Protects special bookmarks (config: protect=@).
9cec7a36bab8 bookflow: new extension for bookmark-based branching
idlsoft <idlsoft@gmail.com>
parents:
diff changeset
     8
9cec7a36bab8 bookflow: new extension for bookmark-based branching
idlsoft <idlsoft@gmail.com>
parents:
diff changeset
     9
 flow related commands
9cec7a36bab8 bookflow: new extension for bookmark-based branching
idlsoft <idlsoft@gmail.com>
parents:
diff changeset
    10
9cec7a36bab8 bookflow: new extension for bookmark-based branching
idlsoft <idlsoft@gmail.com>
parents:
diff changeset
    11
    :hg book NAME: create a new bookmark
9cec7a36bab8 bookflow: new extension for bookmark-based branching
idlsoft <idlsoft@gmail.com>
parents:
diff changeset
    12
    :hg book NAME -r REV: move bookmark to revision (fast-forward)
9cec7a36bab8 bookflow: new extension for bookmark-based branching
idlsoft <idlsoft@gmail.com>
parents:
diff changeset
    13
    :hg up|co NAME: switch to bookmark
9cec7a36bab8 bookflow: new extension for bookmark-based branching
idlsoft <idlsoft@gmail.com>
parents:
diff changeset
    14
    :hg push -B .: push active bookmark
9cec7a36bab8 bookflow: new extension for bookmark-based branching
idlsoft <idlsoft@gmail.com>
parents:
diff changeset
    15
"""
9cec7a36bab8 bookflow: new extension for bookmark-based branching
idlsoft <idlsoft@gmail.com>
parents:
diff changeset
    16
51863
f4733654f144 typing: add `from __future__ import annotations` to most files
Matt Harbison <matt_harbison@yahoo.com>
parents: 50775
diff changeset
    17
from __future__ import annotations
f4733654f144 typing: add `from __future__ import annotations` to most files
Matt Harbison <matt_harbison@yahoo.com>
parents: 50775
diff changeset
    18
40834
9cec7a36bab8 bookflow: new extension for bookmark-based branching
idlsoft <idlsoft@gmail.com>
parents:
diff changeset
    19
from mercurial.i18n import _
9cec7a36bab8 bookflow: new extension for bookmark-based branching
idlsoft <idlsoft@gmail.com>
parents:
diff changeset
    20
from mercurial import (
9cec7a36bab8 bookflow: new extension for bookmark-based branching
idlsoft <idlsoft@gmail.com>
parents:
diff changeset
    21
    bookmarks,
9cec7a36bab8 bookflow: new extension for bookmark-based branching
idlsoft <idlsoft@gmail.com>
parents:
diff changeset
    22
    commands,
9cec7a36bab8 bookflow: new extension for bookmark-based branching
idlsoft <idlsoft@gmail.com>
parents:
diff changeset
    23
    error,
9cec7a36bab8 bookflow: new extension for bookmark-based branching
idlsoft <idlsoft@gmail.com>
parents:
diff changeset
    24
    extensions,
9cec7a36bab8 bookflow: new extension for bookmark-based branching
idlsoft <idlsoft@gmail.com>
parents:
diff changeset
    25
    registrar,
9cec7a36bab8 bookflow: new extension for bookmark-based branching
idlsoft <idlsoft@gmail.com>
parents:
diff changeset
    26
)
9cec7a36bab8 bookflow: new extension for bookmark-based branching
idlsoft <idlsoft@gmail.com>
parents:
diff changeset
    27
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    28
MY_NAME = b'bookflow'
40834
9cec7a36bab8 bookflow: new extension for bookmark-based branching
idlsoft <idlsoft@gmail.com>
parents:
diff changeset
    29
9cec7a36bab8 bookflow: new extension for bookmark-based branching
idlsoft <idlsoft@gmail.com>
parents:
diff changeset
    30
configtable = {}
9cec7a36bab8 bookflow: new extension for bookmark-based branching
idlsoft <idlsoft@gmail.com>
parents:
diff changeset
    31
configitem = registrar.configitem(configtable)
9cec7a36bab8 bookflow: new extension for bookmark-based branching
idlsoft <idlsoft@gmail.com>
parents:
diff changeset
    32
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    33
configitem(MY_NAME, b'protect', [b'@'])
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    34
configitem(MY_NAME, b'require-bookmark', True)
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    35
configitem(MY_NAME, b'enable-branches', False)
40834
9cec7a36bab8 bookflow: new extension for bookmark-based branching
idlsoft <idlsoft@gmail.com>
parents:
diff changeset
    36
9cec7a36bab8 bookflow: new extension for bookmark-based branching
idlsoft <idlsoft@gmail.com>
parents:
diff changeset
    37
cmdtable = {}
9cec7a36bab8 bookflow: new extension for bookmark-based branching
idlsoft <idlsoft@gmail.com>
parents:
diff changeset
    38
command = registrar.command(cmdtable)
9cec7a36bab8 bookflow: new extension for bookmark-based branching
idlsoft <idlsoft@gmail.com>
parents:
diff changeset
    39
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41031
diff changeset
    40
40834
9cec7a36bab8 bookflow: new extension for bookmark-based branching
idlsoft <idlsoft@gmail.com>
parents:
diff changeset
    41
def commit_hook(ui, repo, **kwargs):
9cec7a36bab8 bookflow: new extension for bookmark-based branching
idlsoft <idlsoft@gmail.com>
parents:
diff changeset
    42
    active = repo._bookmarks.active
9cec7a36bab8 bookflow: new extension for bookmark-based branching
idlsoft <idlsoft@gmail.com>
parents:
diff changeset
    43
    if active:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    44
        if active in ui.configlist(MY_NAME, b'protect'):
40834
9cec7a36bab8 bookflow: new extension for bookmark-based branching
idlsoft <idlsoft@gmail.com>
parents:
diff changeset
    45
            raise error.Abort(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    46
                _(b'cannot commit, bookmark %s is protected') % active
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41031
diff changeset
    47
            )
40834
9cec7a36bab8 bookflow: new extension for bookmark-based branching
idlsoft <idlsoft@gmail.com>
parents:
diff changeset
    48
        if not cwd_at_bookmark(repo, active):
9cec7a36bab8 bookflow: new extension for bookmark-based branching
idlsoft <idlsoft@gmail.com>
parents:
diff changeset
    49
            raise error.Abort(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41031
diff changeset
    50
                _(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    51
                    b'cannot commit, working directory out of sync with active bookmark'
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41031
diff changeset
    52
                ),
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    53
                hint=_(b"run 'hg up %s'") % active,
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41031
diff changeset
    54
            )
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    55
    elif ui.configbool(MY_NAME, b'require-bookmark', True):
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    56
        raise error.Abort(_(b'cannot commit without an active bookmark'))
40834
9cec7a36bab8 bookflow: new extension for bookmark-based branching
idlsoft <idlsoft@gmail.com>
parents:
diff changeset
    57
    return 0
9cec7a36bab8 bookflow: new extension for bookmark-based branching
idlsoft <idlsoft@gmail.com>
parents:
diff changeset
    58
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41031
diff changeset
    59
40834
9cec7a36bab8 bookflow: new extension for bookmark-based branching
idlsoft <idlsoft@gmail.com>
parents:
diff changeset
    60
def bookmarks_update(orig, repo, parents, node):
9cec7a36bab8 bookflow: new extension for bookmark-based branching
idlsoft <idlsoft@gmail.com>
parents:
diff changeset
    61
    if len(parents) == 2:
9cec7a36bab8 bookflow: new extension for bookmark-based branching
idlsoft <idlsoft@gmail.com>
parents:
diff changeset
    62
        # called during commit
9cec7a36bab8 bookflow: new extension for bookmark-based branching
idlsoft <idlsoft@gmail.com>
parents:
diff changeset
    63
        return orig(repo, parents, node)
9cec7a36bab8 bookflow: new extension for bookmark-based branching
idlsoft <idlsoft@gmail.com>
parents:
diff changeset
    64
    else:
9cec7a36bab8 bookflow: new extension for bookmark-based branching
idlsoft <idlsoft@gmail.com>
parents:
diff changeset
    65
        # called during update
9cec7a36bab8 bookflow: new extension for bookmark-based branching
idlsoft <idlsoft@gmail.com>
parents:
diff changeset
    66
        return False
9cec7a36bab8 bookflow: new extension for bookmark-based branching
idlsoft <idlsoft@gmail.com>
parents:
diff changeset
    67
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41031
diff changeset
    68
40834
9cec7a36bab8 bookflow: new extension for bookmark-based branching
idlsoft <idlsoft@gmail.com>
parents:
diff changeset
    69
def bookmarks_addbookmarks(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41031
diff changeset
    70
    orig, repo, tr, names, rev=None, force=False, inactive=False
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41031
diff changeset
    71
):
40834
9cec7a36bab8 bookflow: new extension for bookmark-based branching
idlsoft <idlsoft@gmail.com>
parents:
diff changeset
    72
    if not rev:
9cec7a36bab8 bookflow: new extension for bookmark-based branching
idlsoft <idlsoft@gmail.com>
parents:
diff changeset
    73
        marks = repo._bookmarks
9cec7a36bab8 bookflow: new extension for bookmark-based branching
idlsoft <idlsoft@gmail.com>
parents:
diff changeset
    74
        for name in names:
9cec7a36bab8 bookflow: new extension for bookmark-based branching
idlsoft <idlsoft@gmail.com>
parents:
diff changeset
    75
            if name in marks:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41031
diff changeset
    76
                raise error.Abort(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41031
diff changeset
    77
                    _(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    78
                        b"bookmark %s already exists, to move use the --rev option"
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41031
diff changeset
    79
                    )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41031
diff changeset
    80
                    % name
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41031
diff changeset
    81
                )
40834
9cec7a36bab8 bookflow: new extension for bookmark-based branching
idlsoft <idlsoft@gmail.com>
parents:
diff changeset
    82
    return orig(repo, tr, names, rev, force, inactive)
9cec7a36bab8 bookflow: new extension for bookmark-based branching
idlsoft <idlsoft@gmail.com>
parents:
diff changeset
    83
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41031
diff changeset
    84
40834
9cec7a36bab8 bookflow: new extension for bookmark-based branching
idlsoft <idlsoft@gmail.com>
parents:
diff changeset
    85
def commands_commit(orig, ui, repo, *args, **opts):
9cec7a36bab8 bookflow: new extension for bookmark-based branching
idlsoft <idlsoft@gmail.com>
parents:
diff changeset
    86
    commit_hook(ui, repo)
9cec7a36bab8 bookflow: new extension for bookmark-based branching
idlsoft <idlsoft@gmail.com>
parents:
diff changeset
    87
    return orig(ui, repo, *args, **opts)
9cec7a36bab8 bookflow: new extension for bookmark-based branching
idlsoft <idlsoft@gmail.com>
parents:
diff changeset
    88
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41031
diff changeset
    89
40834
9cec7a36bab8 bookflow: new extension for bookmark-based branching
idlsoft <idlsoft@gmail.com>
parents:
diff changeset
    90
def commands_pull(orig, ui, repo, *args, **opts):
9cec7a36bab8 bookflow: new extension for bookmark-based branching
idlsoft <idlsoft@gmail.com>
parents:
diff changeset
    91
    rc = orig(ui, repo, *args, **opts)
9cec7a36bab8 bookflow: new extension for bookmark-based branching
idlsoft <idlsoft@gmail.com>
parents:
diff changeset
    92
    active = repo._bookmarks.active
9cec7a36bab8 bookflow: new extension for bookmark-based branching
idlsoft <idlsoft@gmail.com>
parents:
diff changeset
    93
    if active and not cwd_at_bookmark(repo, active):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41031
diff changeset
    94
        ui.warn(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41031
diff changeset
    95
            _(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    96
                b"working directory out of sync with active bookmark, run "
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    97
                b"'hg up %s'"
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41031
diff changeset
    98
            )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41031
diff changeset
    99
            % active
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41031
diff changeset
   100
        )
40834
9cec7a36bab8 bookflow: new extension for bookmark-based branching
idlsoft <idlsoft@gmail.com>
parents:
diff changeset
   101
    return rc
9cec7a36bab8 bookflow: new extension for bookmark-based branching
idlsoft <idlsoft@gmail.com>
parents:
diff changeset
   102
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41031
diff changeset
   103
40834
9cec7a36bab8 bookflow: new extension for bookmark-based branching
idlsoft <idlsoft@gmail.com>
parents:
diff changeset
   104
def commands_branch(orig, ui, repo, label=None, **opts):
43506
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43077
diff changeset
   105
    if label and not opts.get('clean') and not opts.get('rev'):
40834
9cec7a36bab8 bookflow: new extension for bookmark-based branching
idlsoft <idlsoft@gmail.com>
parents:
diff changeset
   106
        raise error.Abort(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41031
diff changeset
   107
            _(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   108
                b"creating named branches is disabled and you should use bookmarks"
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41031
diff changeset
   109
            ),
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   110
            hint=b"see 'hg help bookflow'",
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41031
diff changeset
   111
        )
40834
9cec7a36bab8 bookflow: new extension for bookmark-based branching
idlsoft <idlsoft@gmail.com>
parents:
diff changeset
   112
    return orig(ui, repo, label, **opts)
9cec7a36bab8 bookflow: new extension for bookmark-based branching
idlsoft <idlsoft@gmail.com>
parents:
diff changeset
   113
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41031
diff changeset
   114
40834
9cec7a36bab8 bookflow: new extension for bookmark-based branching
idlsoft <idlsoft@gmail.com>
parents:
diff changeset
   115
def cwd_at_bookmark(repo, mark):
9cec7a36bab8 bookflow: new extension for bookmark-based branching
idlsoft <idlsoft@gmail.com>
parents:
diff changeset
   116
    mark_id = repo._bookmarks[mark]
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   117
    cur_id = repo.lookup(b'.')
40834
9cec7a36bab8 bookflow: new extension for bookmark-based branching
idlsoft <idlsoft@gmail.com>
parents:
diff changeset
   118
    return cur_id == mark_id
9cec7a36bab8 bookflow: new extension for bookmark-based branching
idlsoft <idlsoft@gmail.com>
parents:
diff changeset
   119
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41031
diff changeset
   120
40834
9cec7a36bab8 bookflow: new extension for bookmark-based branching
idlsoft <idlsoft@gmail.com>
parents:
diff changeset
   121
def uisetup(ui):
50775
3c8a31be81df wrapfunction: use sysstr instead of bytes as argument in "bookflow"
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48875
diff changeset
   122
    extensions.wrapfunction(bookmarks, 'update', bookmarks_update)
3c8a31be81df wrapfunction: use sysstr instead of bytes as argument in "bookflow"
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48875
diff changeset
   123
    extensions.wrapfunction(bookmarks, 'addbookmarks', bookmarks_addbookmarks)
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   124
    extensions.wrapcommand(commands.table, b'commit', commands_commit)
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   125
    extensions.wrapcommand(commands.table, b'pull', commands_pull)
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   126
    if not ui.configbool(MY_NAME, b'enable-branches'):
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   127
        extensions.wrapcommand(commands.table, b'branch', commands_branch)