[PLUGIN] tags

Johan Rydberg jrydberg at gnu.org
Tue Jun 28 13:20:13 BST 2005


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi

This plugin implements mutable unversioned tags.  It is all quite
simple; just keep a "x-tags" file in .bzr/ that assigns a symbolic
name to a revision.

The plugin adds two commands and alters one; 

 - bzr tag TAG_NAME [--revsion REV] [--update]
   Creates or updates a tag.

 - bzr tags [BRANCH]
   Lists all tags.

 - bzr branch from/@TAG to
   Same as the builtin command, but without --revision option. Also
   parses spec for the from_location.

I know the code isn't very pretty; but I'm still is early stages of
learning python. So bare with me.

~j


===File ~/src/bzr.tags/plugins/tag/__init__.py==============
#!/usr/bin/env python
"""\
This is a plugin for the Bazaar-NG revision control system.
"""

import os
import bzrlib, bzrlib.commands

class cmd_tag(bzrlib.commands.Command):
    """Create a symbol name for a revision.  The symbolic name can be
    used when branching from this branch.

    If revision is not specified using the --revision option, the current
    revision will be used as base for the tag.

    It is possible to re-set a tag using the --update option.
    """
    takes_args = ['tag_name']
    takes_options = ['revision', 'update']

    def run(self, tag_name=None, revision=None, update=None):
        from bzrlib.branch import Branch
        from bzrlib.errors import BzrCommandError
        import os.path

        branch = Branch('.')
        tags = ""

        try:
            fp = branch.controlfile("x-tags", "rb")

            while 1:
                line = fp.readline()
                if not line:
                    break
                name, revno = line.strip('\n').split(' ')
                if name == tag_name:
                    if not update:
                        raise BzrCommandError('tag already exists')
                    continue
                tags = tags + line
            
            fp.close ()
        except:
            pass
                        
        if revision is None:
            revision = b.revno()

        tags += "%s %d" % (tag_name, revision) + '\n'

        branch.controlfile("x-tags", "w").write(tags)

class cmd_tags(bzrlib.commands.Command):
    """List all tags in a branch.  LOCATION can point to any branch
    but will default to the local branch.
    """
    takes_args = ['location?']

    def run(self, location=None):
        from bzrlib.branch import find_branch, Branch

        if location:
            branch = find_branch(location)
        else:
            branch = Branch('.')

        try:
            fp = branch.controlfile("x-tags", "rb")
        except:
            print "branch does not have any tags"
            return

        while 1:
            line = fp.readline()
            if not line:
                break

            name, revno = line.strip('\n').split(' ')
            print "%-6s %s" % (revno, name)

        fp.close()

class cmd_branch(bzrlib.commands.Command):
    """Create a new copy of a branch.

    If the TO_LOCATION is omitted, the last component of the FROM_LOCATION will
    be used.  In other words, "branch ../foo/bar" will attempt to create ./bar.

    To retrieve the branch as of a particular revision, supply revision spec
    parameter, as in "branch foo/bar/@REV 5".  REV may also be a tag.
    """
    takes_args = ['from_location', 'to_location?']

    def run(self, from_location, to_location=None, revision=None):
        def parse_spec(spec):
            if spec is None:
                return [None, None]
            if '/@' in spec:
                parsed = spec.split('/@')
                assert len(parsed) == 2
                if parsed[1] == "":
                    parsed[1] = None
            else:
                parsed = [spec, None]
            return parsed

        parsed = parse_spec(from_location)
        revision = None

        if parsed[1] is not None:
            from bzrlib.errors import BzrCommandError
            from bzrlib.branch import find_branch
            
            # User has specified either a revision number or a tag,
            # check which.
            try:
                revision = int(parsed[1])
            except:
                from_br = find_branch(parsed[0])

                try:
                    fp = from_br.controlfile("x-tags", "rb")
                except:
                    raise BzrCommandError("branch does not have any tags")
            
                while 1:
                    line = fp.readline()
                    if not line:
                        break

                    tag_name, tag_revno = line.strip('\n').split(' ')
                    if tag_name == parsed[1]:
                        revision = int(tag_revno)
                        break

                fp.close()

            if revision is None:
                raise BzrCommandError("invalid tag or revision: %s" % parsed[1])

        bzrlib.commands.cmd_branch({ 'from_location' : parsed[0],
                                     'to_location'   : to_location},
                                   { 'revision'      : revision })

if hasattr(bzrlib.commands, 'register_plugin_command'):
    bzrlib.commands.register_plugin_command(cmd_tag)
    bzrlib.commands.register_plugin_command(cmd_tags)
    bzrlib.commands.register_plugin_command(cmd_branch)
elif hasattr(bzrlib.commands, 'register_command'):
    bzrlib.commands.register_command(cmd_tag)
    bzrlib.commands.register_command(cmd_tags)
    bzrlib.commands.register_command(cmd_branch)

============================================================
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.5 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.8 <http://mailcrypt.sourceforge.net/>

iD8DBQFCwUB63CqIy3K3X2ERAgSDAKDCXUGKefWspgX4lHsdEL5BmKaxSQCdEDqB
hGbkdZYN02BAC4gqd63N5JU=
=cw8l
-----END PGP SIGNATURE-----





More information about the bazaar mailing list