~abentley/bzrtools/bzrtools.dev

0.1.22 by Michael Ellerman
Add __init__.py, put cmd_shelve/unshelve in there.
1
#!/usr/bin/python
0.1.24 by Michael Ellerman
Add plugin description
2
"""Shelf - temporarily set aside changes, then bring them back."""
0.1.22 by Michael Ellerman
Add __init__.py, put cmd_shelve/unshelve in there.
3
0.1.73 by Michael Ellerman
Merge most of the standalone shelf branch. This brings in a few changes which
4
import bzrlib.commands
5
from errors import CommandError
6
from bzrlib.option import Option
7
from patchsource import BzrPatchSource
8
from shelf import Shelf
9
from bzrlib import DEFAULT_IGNORE
0.1.109 by Michael Ellerman
Hijack the bzr command infrastructure to do subcommands for 'shelf'.
10
from bzrlib.help import command_usage
0.1.73 by Michael Ellerman
Merge most of the standalone shelf branch. This brings in a few changes which
11
0.7.2 by Michael Ellerman
Convert from DEFAULT_IGNORES to bzrlib.ignores.add_runtime_ignores().
12
bzrlib.ignores.add_runtime_ignores(['./.shelf'])
0.1.73 by Michael Ellerman
Merge most of the standalone shelf branch. This brings in a few changes which
13
14
class cmd_shelve(bzrlib.commands.Command):
0.1.90 by Michael Ellerman
Update help text to try and be clearer, some stolen from bzrtools.
15
    """Temporarily set aside some changes from the current tree.
0.1.73 by Michael Ellerman
Merge most of the standalone shelf branch. This brings in a few changes which
16
17
    Shelve allows you to temporarily put changes you've made "on the shelf",
18
    ie. out of the way, until a later time when you can bring them back from
19
    the shelf with the 'unshelve' command.
20
0.1.90 by Michael Ellerman
Update help text to try and be clearer, some stolen from bzrtools.
21
    Shelve is intended to help separate several sets of text changes that have
22
    been inappropriately mingled.  If you just want to get rid of all changes
23
    (text and otherwise) and you don't need to restore them later, use revert.
24
25
    By default shelve asks you what you want to shelve, press '?' at the
26
    prompt to get help. To shelve everything run shelve --all.
27
0.1.73 by Michael Ellerman
Merge most of the standalone shelf branch. This brings in a few changes which
28
    If filenames are specified, only the changes to those files will be
29
    shelved, other files will be left untouched.
30
31
    If a revision is specified, changes since that revision will be shelved.
0.1.113 by Michael Ellerman
Support for unshelving in arbitrary order.
32
33
    You can put multiple items on the shelf. Normally each time you run
34
    unshelve the most recently shelved changes will be reinstated. However,
35
    you can also unshelve changes in a different order by explicitly
36
    specifiying which changes to unshelve. This works best when the changes
37
    don't depend on each other.
0.7.3 by Michael Ellerman
Add a reference from 'shelve' help to 'shelf'.
38
39
    While you have patches on the shelf you can view and manipulate them with
40
    the 'shelf' command. Run 'bzr shelf -h' for more info.
0.1.90 by Michael Ellerman
Update help text to try and be clearer, some stolen from bzrtools.
41
    """
0.1.73 by Michael Ellerman
Merge most of the standalone shelf branch. This brings in a few changes which
42
43
    takes_args = ['file*']
0.1.90 by Michael Ellerman
Update help text to try and be clearer, some stolen from bzrtools.
44
    takes_options = ['message', 'revision',
45
            Option('all', help='Shelve all changes without prompting')]
46
0.1.80 by Michael Ellerman
After extensive user testing, ie. me using it, I've decided --pick should be
47
    def run(self, all=False, file_list=None, message=None, revision=None):
0.1.73 by Michael Ellerman
Merge most of the standalone shelf branch. This brings in a few changes which
48
        if revision is not None and revision:
49
            if len(revision) == 1:
50
                revision = revision[0]
51
            else:
52
                raise CommandError("shelve only accepts a single revision "
53
                                  "parameter.")
54
55
        source = BzrPatchSource(revision, file_list)
0.1.74 by Michael Ellerman
Adapt to BzrDir changes and deprecation of show_diff().
56
        s = Shelf(source.base)
0.1.80 by Michael Ellerman
After extensive user testing, ie. me using it, I've decided --pick should be
57
        s.shelve(source, all, message)
0.1.73 by Michael Ellerman
Merge most of the standalone shelf branch. This brings in a few changes which
58
        return 0
59
0.1.109 by Michael Ellerman
Hijack the bzr command infrastructure to do subcommands for 'shelf'.
60
61
# The following classes are only used as subcommands for 'shelf', they're
62
# not to be registered directly with bzr.
63
64
class cmd_shelf_list(bzrlib.commands.Command):
65
    """List the patches on the current shelf."""
66
    aliases = ['list', 'ls']
67
    def run(self):
68
        self.shelf.list()
69
70
71
class cmd_shelf_delete(bzrlib.commands.Command):
72
    """Delete the patch from the current shelf."""
73
    aliases = ['delete', 'del']
74
    takes_args = ['patch']
75
    def run(self, patch):
76
        self.shelf.delete(patch)
77
78
79
class cmd_shelf_switch(bzrlib.commands.Command):
80
    """Switch to the other shelf, create it if necessary."""
81
    aliases = ['switch']
0.1.117 by Michael Ellerman
Arg names with hyphens don't seem to work (broke shelf switch).
82
    takes_args = ['othershelf']
83
    def run(self, othershelf):
84
        s = Shelf(self.shelf.base, othershelf)
0.1.109 by Michael Ellerman
Hijack the bzr command infrastructure to do subcommands for 'shelf'.
85
        s.make_default()
86
87
88
class cmd_shelf_show(bzrlib.commands.Command):
0.1.110 by Michael Ellerman
Make the patch argument to 'shelf show' optional.
89
    """Show the contents of the specified or topmost patch."""
0.1.109 by Michael Ellerman
Hijack the bzr command infrastructure to do subcommands for 'shelf'.
90
    aliases = ['show', 'cat', 'display']
0.1.110 by Michael Ellerman
Make the patch argument to 'shelf show' optional.
91
    takes_args = ['patch?']
92
    def run(self, patch=None):
0.1.109 by Michael Ellerman
Hijack the bzr command infrastructure to do subcommands for 'shelf'.
93
        self.shelf.display(patch)
94
95
96
class cmd_shelf_upgrade(bzrlib.commands.Command):
97
    """Upgrade old format shelves."""
98
    aliases = ['upgrade']
99
    def run(self):
100
        self.shelf.upgrade()
101
102
0.1.73 by Michael Ellerman
Merge most of the standalone shelf branch. This brings in a few changes which
103
class cmd_shelf(bzrlib.commands.Command):
0.1.109 by Michael Ellerman
Hijack the bzr command infrastructure to do subcommands for 'shelf'.
104
    """Perform various operations on your shelved patches. See also shelve."""
0.1.73 by Michael Ellerman
Merge most of the standalone shelf branch. This brings in a few changes which
105
    takes_args = ['subcommand', 'args*']
106
0.1.109 by Michael Ellerman
Hijack the bzr command infrastructure to do subcommands for 'shelf'.
107
    subcommands = [cmd_shelf_list, cmd_shelf_delete, cmd_shelf_switch,
108
        cmd_shelf_show, cmd_shelf_upgrade]
109
0.1.73 by Michael Ellerman
Merge most of the standalone shelf branch. This brings in a few changes which
110
    def run(self, subcommand, args_list):
111
        import sys
112
0.1.109 by Michael Ellerman
Hijack the bzr command infrastructure to do subcommands for 'shelf'.
113
        cmd = self._get_cmd_object(subcommand)
0.1.73 by Michael Ellerman
Merge most of the standalone shelf branch. This brings in a few changes which
114
        source = BzrPatchSource()
0.1.74 by Michael Ellerman
Adapt to BzrDir changes and deprecation of show_diff().
115
        s = Shelf(source.base)
0.1.109 by Michael Ellerman
Hijack the bzr command infrastructure to do subcommands for 'shelf'.
116
        cmd.shelf = s
117
        return cmd.run_argv_aliases(args_list)
118
119
    def _get_cmd_object(self, cmd_name):
120
        for cmd_class in self.subcommands:
121
            for alias in cmd_class.aliases:
122
                if alias == cmd_name:
123
                    return cmd_class()
124
        raise CommandError("Unknown shelf subcommand '%s'" % cmd_name)
125
126
    def help(self):
0.1.111 by Michael Ellerman
Make help for subcommands more readable, print options in help also.
127
        text = ["%s\n\nSubcommands:\n" % self.__doc__]
0.1.109 by Michael Ellerman
Hijack the bzr command infrastructure to do subcommands for 'shelf'.
128
129
        for cmd_class in self.subcommands:
0.1.111 by Michael Ellerman
Make help for subcommands more readable, print options in help also.
130
            text.extend(self.sub_help(cmd_class) + ['\n'])
131
132
        return ''.join(text)
133
134
    def sub_help(self, cmd_class):
135
        text = []
136
        cmd_obj = cmd_class()
137
        indent = 2 * ' '
138
139
        usage = command_usage(cmd_obj)
140
        usage = usage.replace('bzr shelf-', '')
141
        text.append('%s%s\n' % (indent, usage))
142
143
        text.append('%s%s\n' % (2 * indent, cmd_class.__doc__))
144
145
        # Somewhat copied from bzrlib.help.help_on_command_options
146
        option_help = []
147
        for option_name, option in sorted(cmd_obj.options().items()):
148
            if option_name == 'help':
149
                continue
150
            option_help.append('%s--%s' % (3 * indent, option_name))
151
            if option.type is not None:
152
                option_help.append(' %s' % option.argname.upper())
153
            if option.short_name():
154
                option_help.append(', -%s' % option.short_name())
155
            option_help.append('%s%s\n' % (2 * indent, option.help))
156
157
        if len(option_help) > 0:
158
            text.append('%soptions:\n' % (2 * indent))
159
            text.extend(option_help)
160
161
        return text
0.1.109 by Michael Ellerman
Hijack the bzr command infrastructure to do subcommands for 'shelf'.
162
0.1.73 by Michael Ellerman
Merge most of the standalone shelf branch. This brings in a few changes which
163
164
class cmd_unshelve(bzrlib.commands.Command):
0.1.113 by Michael Ellerman
Support for unshelving in arbitrary order.
165
    """Restore shelved changes.
166
167
    By default the most recently shelved changes are restored. However if you
168
    specify a patch by name those changes will be restored instead.
169
0.1.73 by Michael Ellerman
Merge most of the standalone shelf branch. This brings in a few changes which
170
    See 'shelve' for more information.
171
    """
0.1.91 by Michael Ellerman
Add --force option to unshelve, which runs the shelved changes through
172
    takes_options = [
173
            Option('all', help='Unshelve all changes without prompting'),
174
            Option('force', help='Force unshelving even if errors occur'),
175
    ]
0.1.113 by Michael Ellerman
Support for unshelving in arbitrary order.
176
    takes_args = ['patch?']
177
    def run(self, patch=None, all=False, force=False):
0.1.73 by Michael Ellerman
Merge most of the standalone shelf branch. This brings in a few changes which
178
        source = BzrPatchSource()
0.1.74 by Michael Ellerman
Adapt to BzrDir changes and deprecation of show_diff().
179
        s = Shelf(source.base)
0.1.113 by Michael Ellerman
Support for unshelving in arbitrary order.
180
        s.unshelve(source, patch, all, force)
0.1.73 by Michael Ellerman
Merge most of the standalone shelf branch. This brings in a few changes which
181
        return 0
182
183
bzrlib.commands.register_command(cmd_shelf)
184
bzrlib.commands.register_command(cmd_shelve)
185
bzrlib.commands.register_command(cmd_unshelve)
186
187
def test_suite():
188
    from bzrlib.tests.TestUtil import TestLoader
189
    import tests
190
    return TestLoader().loadTestsFromModule(tests)