~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to __init__.py

  • Committer: Michael Ellerman
  • Date: 2005-11-28 06:24:55 UTC
  • mto: (0.3.1 shelf-dev) (325.1.2 bzrtools)
  • mto: This revision was merged to the branch mainline in revision 334.
  • Revision ID: michael@ellerman.id.au-20051128062455-9da2ff70dd70e65c
Add tests for new shelf layout.

Shelves are now named .bzr/x-shelf/default/0x where x increases.

Also add a test for doing shelve/unshelve multiple times.

Show diffs side-by-side

added added

removed removed

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