~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to __init__.py

  • Committer: Michael Ellerman
  • Date: 2006-05-30 17:06:19 UTC
  • mto: (0.3.1 shelf-dev)
  • mto: This revision was merged to the branch mainline in revision 393.
  • Revision ID: michael@ellerman.id.au-20060530170619-1de7e5859cbfb801
Hijack the bzr command infrastructure to do subcommands for 'shelf'.

This is actually pretty neat considering it wasn't designed to do this.
The only wrinkles are we have to do the help by hand, and it's a little
ugly. Also when we get bad input for a shelf subcommand 'foo', the error
refers to the command as "shelf-foo", but that's good enough IMHO.

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
from patchsource import BzrPatchSource
8
8
from shelf import Shelf
9
9
from bzrlib import DEFAULT_IGNORE
 
10
from bzrlib.help import command_usage
10
11
 
11
12
DEFAULT_IGNORE.append('./.shelf')
12
13
DEFAULT_IGNORE.append('./.bzr-shelf*')
51
52
        s.shelve(source, all, message)
52
53
        return 0
53
54
 
 
55
 
 
56
# The following classes are only used as subcommands for 'shelf', they're
 
57
# not to be registered directly with bzr.
 
58
 
 
59
class cmd_shelf_list(bzrlib.commands.Command):
 
60
    """List the patches on the current shelf."""
 
61
    aliases = ['list', 'ls']
 
62
    def run(self):
 
63
        self.shelf.list()
 
64
 
 
65
 
 
66
class cmd_shelf_delete(bzrlib.commands.Command):
 
67
    """Delete the patch from the current shelf."""
 
68
    aliases = ['delete', 'del']
 
69
    takes_args = ['patch']
 
70
    def run(self, patch):
 
71
        self.shelf.delete(patch)
 
72
 
 
73
 
 
74
class cmd_shelf_switch(bzrlib.commands.Command):
 
75
    """Switch to the other shelf, create it if necessary."""
 
76
    aliases = ['switch']
 
77
    takes_args = ['other-shelf']
 
78
    def run(self, other_shelf):
 
79
        s = Shelf(self.shelf.base, other_shelf)
 
80
        s.make_default()
 
81
 
 
82
 
 
83
class cmd_shelf_show(bzrlib.commands.Command):
 
84
    """Show the contents of the specified patch."""
 
85
    aliases = ['show', 'cat', 'display']
 
86
    takes_args = ['patch']
 
87
    def run(self, patch):
 
88
        self.shelf.display(patch)
 
89
 
 
90
 
 
91
class cmd_shelf_upgrade(bzrlib.commands.Command):
 
92
    """Upgrade old format shelves."""
 
93
    aliases = ['upgrade']
 
94
    def run(self):
 
95
        self.shelf.upgrade()
 
96
 
 
97
 
54
98
class cmd_shelf(bzrlib.commands.Command):
55
 
    """Perform various operations on your shelved patches. See also shelve.
56
 
 
57
 
    Subcommands:
58
 
        list   (ls)           List the patches on the current shelf.
59
 
        delete (del) <patch>  Delete a patch from the current shelf.
60
 
        switch       <shelf>  Switch to the named shelf, create it if necessary.
61
 
        show         <patch>  Show the contents of the specified patch.
62
 
        upgrade               Upgrade old format shelves.
63
 
    """
 
99
    """Perform various operations on your shelved patches. See also shelve."""
64
100
    takes_args = ['subcommand', 'args*']
65
101
 
 
102
    subcommands = [cmd_shelf_list, cmd_shelf_delete, cmd_shelf_switch,
 
103
        cmd_shelf_show, cmd_shelf_upgrade]
 
104
 
66
105
    def run(self, subcommand, args_list):
67
106
        import sys
68
107
 
 
108
        cmd = self._get_cmd_object(subcommand)
69
109
        source = BzrPatchSource()
70
110
        s = Shelf(source.base)
71
 
 
72
 
        if subcommand == 'ls' or subcommand == 'list':
73
 
            self.__check_no_args(args_list, "shelf list takes no arguments!")
74
 
            s.list()
75
 
        elif subcommand == 'delete' or subcommand == 'del':
76
 
            self.__check_one_arg(args_list, "shelf delete takes one argument!")
77
 
            s.delete(args_list[0])
78
 
        elif subcommand == 'switch':
79
 
            self.__check_one_arg(args_list, "shelf switch takes one argument!")
80
 
            s = Shelf(source.base, args_list[0])
81
 
            s.make_default()
82
 
        elif subcommand == 'show':
83
 
            self.__check_one_arg(args_list, "shelf show takes one argument!")
84
 
            s.display(args_list[0])
85
 
        elif subcommand == 'upgrade':
86
 
            self.__check_no_args(args_list, "shelf upgrade takes no arguments!")
87
 
            s.upgrade()
88
 
        else:
89
 
            raise CommandError("Unknown shelf subcommand '%s'" % subcommand)
90
 
 
91
 
    def __check_one_arg(self, args, msg):
92
 
        if args is None or len(args) != 1:
93
 
            raise CommandError(msg)
94
 
 
95
 
    def __check_no_args(self, args, msg):
96
 
        if args is not None:
97
 
            raise CommandError(msg)
 
111
        cmd.shelf = s
 
112
        return cmd.run_argv_aliases(args_list)
 
113
 
 
114
    def _get_cmd_object(self, cmd_name):
 
115
        for cmd_class in self.subcommands:
 
116
            for alias in cmd_class.aliases:
 
117
                if alias == cmd_name:
 
118
                    return cmd_class()
 
119
        raise CommandError("Unknown shelf subcommand '%s'" % cmd_name)
 
120
 
 
121
    def help(self):
 
122
        lines = ["%s\n\nSubcommands:" % self.__doc__]
 
123
 
 
124
        sub_help = []
 
125
        maxlen = 0
 
126
        for cmd_class in self.subcommands:
 
127
            usage = command_usage(cmd_class())
 
128
            usage = usage.replace('bzr shelf-', '')
 
129
            sub_help.append((usage, cmd_class.__doc__))
 
130
            maxlen = max(maxlen, len(usage))
 
131
 
 
132
        for usage, doc in sub_help:
 
133
            lines.append("  %-*s  %s" % (maxlen, usage, doc))
 
134
 
 
135
        return '\n'.join(lines)
 
136
 
98
137
 
99
138
class cmd_unshelve(bzrlib.commands.Command):
100
139
    """Restore the most recently shelved changes to the current tree.