| 1 |
"""implements bookmark-based branching (EXPERIMENTAL) |
| 2 |
|
| 3 |
- Disables creation of new branches (config: enable_branches=False). |
| 4 |
- Requires an active bookmark on commit (config: require_bookmark=True). |
| 5 |
- Doesn't move the active bookmark on update, only on commit. |
| 6 |
- Requires '--rev' for moving an existing bookmark. |
| 7 |
- Protects special bookmarks (config: protect=@). |
| 8 |
|
| 9 |
flow related commands |
| 10 |
|
| 11 |
:hg book NAME: create a new bookmark |
| 12 |
:hg book NAME -r REV: move bookmark to revision (fast-forward) |
| 13 |
:hg up|co NAME: switch to bookmark |
| 14 |
:hg push -B .: push active bookmark |
| 15 |
""" |
| 16 |
|
| 17 |
from __future__ import annotations |
| 18 |
|
| 19 |
from mercurial.i18n import _ |
| 20 |
from mercurial import ( |
| 21 |
bookmarks, |
| 22 |
commands, |
| 23 |
error, |
| 24 |
extensions, |
| 25 |
registrar, |
| 26 |
) |
| 27 |
|
| 28 |
MY_NAME = b'bookflow' |
| 29 |
|
| 30 |
configtable = {} |
| 31 |
configitem = registrar.configitem(configtable) |
| 32 |
|
| 33 |
configitem(MY_NAME, b'protect', [b'@']) |
| 34 |
configitem(MY_NAME, b'require-bookmark', True) |
| 35 |
configitem(MY_NAME, b'enable-branches', False) |
| 36 |
|
| 37 |
cmdtable = {} |
| 38 |
command = registrar.command(cmdtable) |
| 39 |
|
| 40 |
|
| 41 |
def commit_hook(ui, repo, **kwargs): |
| 42 |
active = repo._bookmarks.active |
| 43 |
if active: |
| 44 |
if active in ui.configlist(MY_NAME, b'protect'): |
| 45 |
raise error.Abort( |
| 46 |
_(b'cannot commit, bookmark %s is protected') % active |
| 47 |
) |
| 48 |
if not cwd_at_bookmark(repo, active): |
| 49 |
raise error.Abort( |
| 50 |
_( |
| 51 |
b'cannot commit, working directory out of sync with active bookmark' |
| 52 |
), |
| 53 |
hint=_(b"run 'hg up %s'") % active, |
| 54 |
) |
| 55 |
elif ui.configbool(MY_NAME, b'require-bookmark', True): |
| 56 |
raise error.Abort(_(b'cannot commit without an active bookmark')) |
| 57 |
return 0 |
| 58 |
|
| 59 |
|
| 60 |
def bookmarks_update(orig, repo, parents, node): |
| 61 |
if len(parents) == 2: |
| 62 |
# called during commit |
| 63 |
return orig(repo, parents, node) |
| 64 |
else: |
| 65 |
# called during update |
| 66 |
return False |
| 67 |
|
| 68 |
|
| 69 |
def bookmarks_addbookmarks( |
| 70 |
orig, repo, tr, names, rev=None, force=False, inactive=False |
| 71 |
): |
| 72 |
if not rev: |
| 73 |
marks = repo._bookmarks |
| 74 |
for name in names: |
| 75 |
if name in marks: |
| 76 |
raise error.Abort( |
| 77 |
_( |
| 78 |
b"bookmark %s already exists, to move use the --rev option" |
| 79 |
) |
| 80 |
% name |
| 81 |
) |
| 82 |
return orig(repo, tr, names, rev, force, inactive) |
| 83 |
|
| 84 |
|
| 85 |
def commands_commit(orig, ui, repo, *args, **opts): |
| 86 |
commit_hook(ui, repo) |
| 87 |
return orig(ui, repo, *args, **opts) |
| 88 |
|
| 89 |
|
| 90 |
def commands_pull(orig, ui, repo, *args, **opts): |
| 91 |
rc = orig(ui, repo, *args, **opts) |
| 92 |
active = repo._bookmarks.active |
| 93 |
if active and not cwd_at_bookmark(repo, active): |
| 94 |
ui.warn( |
| 95 |
_( |
| 96 |
b"working directory out of sync with active bookmark, run " |
| 97 |
b"'hg up %s'" |
| 98 |
) |
| 99 |
% active |
| 100 |
) |
| 101 |
return rc |
| 102 |
|
| 103 |
|
| 104 |
def commands_branch(orig, ui, repo, label=None, **opts): |
| 105 |
if label and not opts.get('clean') and not opts.get('rev'): |
| 106 |
raise error.Abort( |
| 107 |
_( |
| 108 |
b"creating named branches is disabled and you should use bookmarks" |
| 109 |
), |
| 110 |
hint=b"see 'hg help bookflow'", |
| 111 |
) |
| 112 |
return orig(ui, repo, label, **opts) |
| 113 |
|
| 114 |
|
| 115 |
def cwd_at_bookmark(repo, mark): |
| 116 |
mark_id = repo._bookmarks[mark] |
| 117 |
cur_id = repo.lookup(b'.') |
| 118 |
return cur_id == mark_id |
| 119 |
|
| 120 |
|
| 121 |
def uisetup(ui): |
| 122 |
extensions.wrapfunction(bookmarks, 'update', bookmarks_update) |
| 123 |
extensions.wrapfunction(bookmarks, 'addbookmarks', bookmarks_addbookmarks) |
| 124 |
extensions.wrapcommand(commands.table, b'commit', commands_commit) |
| 125 |
extensions.wrapcommand(commands.table, b'pull', commands_pull) |
| 126 |
if not ui.configbool(MY_NAME, b'enable-branches'): |
| 127 |
extensions.wrapcommand(commands.table, b'branch', commands_branch) |