src/topic/__init__.py
author Augie Fackler <augie@google.com>
Wed, 10 Jun 2015 16:13:20 -0400
changeset 1845 24d8053020a2
parent 1844 862cabc132fd
child 1846 0b5b757ca812
permissions -rw-r--r--
constants: extract key for extra into a constant It might be prudent to prefix it, and regardless it was bugging me to have it repeated everywhere.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1839
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
     1
# __init__.py - topic extension
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
     2
#
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
     3
# This software may be used and distributed according to the terms of the
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
     4
# GNU General Public License version 2 or any later version.
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
     5
"""Adds topic branches. Topic branches are lightweight branches which
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
     6
dissappear when changes are finalized.
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
     7
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
     8
This is sort of similar to a bookmark, but it applies to a whole
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
     9
series instead of a single revision.
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    10
"""
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    11
import functools
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    12
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    13
from mercurial import cmdutil
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    14
from mercurial import commands
1844
862cabc132fd topic: add ability to change topic of non-public changes
Augie Fackler <augie@google.com>
parents: 1843
diff changeset
    15
from mercurial import context
1839
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    16
from mercurial import extensions
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    17
from mercurial import namespaces
1842
94bbc18daa99 topic: disallow use of topics without obsolete enabled
Augie Fackler <augie@google.com>
parents: 1841
diff changeset
    18
from mercurial import obsolete
1839
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    19
from mercurial import phases
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    20
from mercurial import util
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    21
1845
24d8053020a2 constants: extract key for extra into a constant
Augie Fackler <augie@google.com>
parents: 1844
diff changeset
    22
from . import constants
1843
0ba067a97d06 revset: add a topic() revset for querying topics
Augie Fackler <augie@google.com>
parents: 1842
diff changeset
    23
from . import revset as topicrevset
0ba067a97d06 revset: add a topic() revset for querying topics
Augie Fackler <augie@google.com>
parents: 1842
diff changeset
    24
1839
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    25
cmdtable = {}
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    26
command = cmdutil.command(cmdtable)
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    27
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    28
def _namemap(repo, name):
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    29
    return [ctx.node() for ctx in
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    30
            repo.set('not public() and extra(topic, %s)', name)]
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    31
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    32
def _nodemap(repo, node):
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    33
    ctx = repo[node]
1845
24d8053020a2 constants: extract key for extra into a constant
Augie Fackler <augie@google.com>
parents: 1844
diff changeset
    34
    t = ctx.extra().get(constants.extrakey, '')
1839
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    35
    if t and ctx.phase() > phases.public:
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    36
        return [t]
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    37
    return []
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    38
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    39
def reposetup(ui, repo):
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    40
    orig = repo.__class__
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    41
    class topicrepo(repo.__class__):
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    42
        def commitctx(self, ctx, error=None):
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    43
            current = self.currenttopic
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    44
            if current:
1845
24d8053020a2 constants: extract key for extra into a constant
Augie Fackler <augie@google.com>
parents: 1844
diff changeset
    45
                ctx.extra()[constants.extrakey] = current
1839
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    46
            return orig.commitctx(self, ctx, error=error)
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    47
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    48
        @property
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    49
        def topics(self):
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    50
            topics = set(['', self.currenttopic])
1841
72a58a5bfb62 topic: use repo.set() where we need a changectx anyway
Augie Fackler <augie@google.com>
parents: 1839
diff changeset
    51
            for c in self.set('not public()'):
1845
24d8053020a2 constants: extract key for extra into a constant
Augie Fackler <augie@google.com>
parents: 1844
diff changeset
    52
                topics.add(c.extra().get(constants.extrakey, ''))
1839
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    53
            topics.remove('')
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    54
            return topics
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    55
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    56
        @property
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    57
        def currenttopic(self):
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    58
            return self.vfs.tryread('topic')
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    59
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    60
    if util.safehasattr(repo, 'names'):
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    61
        repo.names.addnamespace(namespaces.namespace(
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    62
            'topics', 'topic', namemap=_namemap, nodemap=_nodemap))
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    63
    repo.__class__ = topicrepo
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    64
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    65
@command('topics', [
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    66
    ('', 'clear', False, 'clear active topic if any'),
1844
862cabc132fd topic: add ability to change topic of non-public changes
Augie Fackler <augie@google.com>
parents: 1843
diff changeset
    67
    ('', 'change', '', 'revset of existing revisions to change topic'),
1839
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    68
])
1844
862cabc132fd topic: add ability to change topic of non-public changes
Augie Fackler <augie@google.com>
parents: 1843
diff changeset
    69
def topics(ui, repo, topic=None, clear=False, change=None):
1839
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
    70
    """View current topic, set current topic, or see all topics."""
1842
94bbc18daa99 topic: disallow use of topics without obsolete enabled
Augie Fackler <augie@google.com>
parents: 1841
diff changeset
    71
    if not obsolete.isenabled(repo, obsolete.createmarkersopt):
94bbc18daa99 topic: disallow use of topics without obsolete enabled
Augie Fackler <augie@google.com>
parents: 1841
diff changeset
    72
        raise util.Abort('current reality means you should only '
94bbc18daa99 topic: disallow use of topics without obsolete enabled
Augie Fackler <augie@google.com>
parents: 1841
diff changeset
    73
                         'use topic with obsolete enabled')
1844
862cabc132fd topic: add ability to change topic of non-public changes
Augie Fackler <augie@google.com>
parents: 1843
diff changeset
    74
    if change:
862cabc132fd topic: add ability to change topic of non-public changes
Augie Fackler <augie@google.com>
parents: 1843
diff changeset
    75
        if topic is None and not clear:
862cabc132fd topic: add ability to change topic of non-public changes
Augie Fackler <augie@google.com>
parents: 1843
diff changeset
    76
            raise util.Abort('changing topic requires a topic name or --clear')
862cabc132fd topic: add ability to change topic of non-public changes
Augie Fackler <augie@google.com>
parents: 1843
diff changeset
    77
        if any(not c.mutable() for c in repo.set('%r and public()', change)):
862cabc132fd topic: add ability to change topic of non-public changes
Augie Fackler <augie@google.com>
parents: 1843
diff changeset
    78
            raise util.Abort("can't change topic of a public change")
862cabc132fd topic: add ability to change topic of non-public changes
Augie Fackler <augie@google.com>
parents: 1843
diff changeset
    79
        rewrote = 0
862cabc132fd topic: add ability to change topic of non-public changes
Augie Fackler <augie@google.com>
parents: 1843
diff changeset
    80
        needevolve = False
862cabc132fd topic: add ability to change topic of non-public changes
Augie Fackler <augie@google.com>
parents: 1843
diff changeset
    81
        for c in repo.set('%r', change):
862cabc132fd topic: add ability to change topic of non-public changes
Augie Fackler <augie@google.com>
parents: 1843
diff changeset
    82
            def filectxfn(repo, ctx, path):
862cabc132fd topic: add ability to change topic of non-public changes
Augie Fackler <augie@google.com>
parents: 1843
diff changeset
    83
                try:
862cabc132fd topic: add ability to change topic of non-public changes
Augie Fackler <augie@google.com>
parents: 1843
diff changeset
    84
                    return c[path]
862cabc132fd topic: add ability to change topic of non-public changes
Augie Fackler <augie@google.com>
parents: 1843
diff changeset
    85
                except error.ManifestLookupError:
862cabc132fd topic: add ability to change topic of non-public changes
Augie Fackler <augie@google.com>
parents: 1843
diff changeset
    86
                    return None
862cabc132fd topic: add ability to change topic of non-public changes
Augie Fackler <augie@google.com>
parents: 1843
diff changeset
    87
            fixedextra = dict(c.extra())
862cabc132fd topic: add ability to change topic of non-public changes
Augie Fackler <augie@google.com>
parents: 1843
diff changeset
    88
            newtopic = None if clear else topic
1845
24d8053020a2 constants: extract key for extra into a constant
Augie Fackler <augie@google.com>
parents: 1844
diff changeset
    89
            if fixedextra.get(constants.extrakey, None) == topic:
1844
862cabc132fd topic: add ability to change topic of non-public changes
Augie Fackler <augie@google.com>
parents: 1843
diff changeset
    90
                continue
1845
24d8053020a2 constants: extract key for extra into a constant
Augie Fackler <augie@google.com>
parents: 1844
diff changeset
    91
            if clear and constants.extrakey in fixedextra:
24d8053020a2 constants: extract key for extra into a constant
Augie Fackler <augie@google.com>
parents: 1844
diff changeset
    92
                del fixedextra[constants.extrakey]
1844
862cabc132fd topic: add ability to change topic of non-public changes
Augie Fackler <augie@google.com>
parents: 1843
diff changeset
    93
            else:
1845
24d8053020a2 constants: extract key for extra into a constant
Augie Fackler <augie@google.com>
parents: 1844
diff changeset
    94
                fixedextra[constants.extrakey] = topic
1844
862cabc132fd topic: add ability to change topic of non-public changes
Augie Fackler <augie@google.com>
parents: 1843
diff changeset
    95
            c.parents()
862cabc132fd topic: add ability to change topic of non-public changes
Augie Fackler <augie@google.com>
parents: 1843
diff changeset
    96
            mc = context.memctx(
862cabc132fd topic: add ability to change topic of non-public changes
Augie Fackler <augie@google.com>
parents: 1843
diff changeset
    97
                repo, (c.p1().node(), c.p2().node()), c.description(),
862cabc132fd topic: add ability to change topic of non-public changes
Augie Fackler <augie@google.com>
parents: 1843
diff changeset
    98
                c.files(), filectxfn,
862cabc132fd topic: add ability to change topic of non-public changes
Augie Fackler <augie@google.com>
parents: 1843
diff changeset
    99
                user=c.user(), date=c.date(), extra=fixedextra)
862cabc132fd topic: add ability to change topic of non-public changes
Augie Fackler <augie@google.com>
parents: 1843
diff changeset
   100
            newnode = repo.commitctx(mc)
862cabc132fd topic: add ability to change topic of non-public changes
Augie Fackler <augie@google.com>
parents: 1843
diff changeset
   101
            needevolve = needevolve or (len(c.children()) > 0)
862cabc132fd topic: add ability to change topic of non-public changes
Augie Fackler <augie@google.com>
parents: 1843
diff changeset
   102
            obsolete.createmarkers(repo, [(c, (repo[newnode],))])
862cabc132fd topic: add ability to change topic of non-public changes
Augie Fackler <augie@google.com>
parents: 1843
diff changeset
   103
            rewrote += 1
862cabc132fd topic: add ability to change topic of non-public changes
Augie Fackler <augie@google.com>
parents: 1843
diff changeset
   104
        ui.status('changed topic on %d changes\n' % rewrote)
862cabc132fd topic: add ability to change topic of non-public changes
Augie Fackler <augie@google.com>
parents: 1843
diff changeset
   105
        if needevolve:
862cabc132fd topic: add ability to change topic of non-public changes
Augie Fackler <augie@google.com>
parents: 1843
diff changeset
   106
            evolvetarget = 'topic(%s)' % topic if topic else 'not topic()'
862cabc132fd topic: add ability to change topic of non-public changes
Augie Fackler <augie@google.com>
parents: 1843
diff changeset
   107
            ui.status('please run hg evolve --rev "%s" now\n' % evolvetarget)
1839
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
   108
    if clear:
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
   109
        if repo.vfs.exists('topic'):
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
   110
            repo.vfs.unlink('topic')
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
   111
        return
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
   112
    if topic is not None:
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
   113
        with repo.vfs.open('topic', 'w') as f:
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
   114
            f.write(topic)
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
   115
        return
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
   116
    current = repo.currenttopic
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
   117
    for t in sorted(repo.topics):
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
   118
        marker = '*' if t == current else ' '
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
   119
        ui.write(' %s %s\n' % (marker, t))
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
   120
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
   121
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
   122
def updatewrap(orig, ui, repo, *args, **kwargs):
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
   123
    ret = orig(ui, repo, *args, **kwargs)
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
   124
    pctx = repo['.']
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
   125
    if pctx.phase() == phases.public and repo.vfs.exists('topic'):
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
   126
        repo.vfs.unlink('topic')
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
   127
    else:
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
   128
        # inherit the topic of the parent revision
1845
24d8053020a2 constants: extract key for extra into a constant
Augie Fackler <augie@google.com>
parents: 1844
diff changeset
   129
        t = pctx.extra().get(constants.extrakey, '')
1839
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
   130
        if t and pctx.phase() > phases.public:
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
   131
            with repo.vfs.open('topic', 'w') as f:
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
   132
                f.write(t)
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
   133
    return ret
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
   134
1bc5e62fc0c7 Initial dumb version of topics.
Augie Fackler <augie@google.com>
parents:
diff changeset
   135
extensions.wrapcommand(commands.table, 'update', updatewrap)
1843
0ba067a97d06 revset: add a topic() revset for querying topics
Augie Fackler <augie@google.com>
parents: 1842
diff changeset
   136
topicrevset.modsetup()