2
2
"""Shelf - temporarily set aside changes, then bring them back."""
4
4
import bzrlib.commands
6
from bzrlib.errors import BzrCommandError
5
from errors import CommandError
7
6
from bzrlib.option import Option
7
from patchsource import BzrPatchSource
8
8
from shelf import Shelf
9
from bzrlib import DEFAULT_IGNORE
10
from bzrlib.help import command_usage
12
DEFAULT_IGNORE.append('./.shelf')
13
DEFAULT_IGNORE.append('./.bzr-shelf*')
10
15
class cmd_shelve(bzrlib.commands.Command):
11
"""Temporarily set aside some changes to the current working tree.
16
"""Temporarily set aside some changes from the current tree.
13
18
Shelve allows you to temporarily put changes you've made "on the shelf",
14
19
ie. out of the way, until a later time when you can bring them back from
15
20
the shelf with the 'unshelve' command.
17
You can put multiple items on the shelf, each time you run unshelve the
18
most recently shelved changes will be reinstated.
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.
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.
20
29
If filenames are specified, only the changes to those files will be
21
30
shelved, other files will be left untouched.
23
32
If a revision is specified, changes since that revision will be shelved.
25
If you specifiy "--pick" you'll be prompted for each hunk of the diff as
26
to whether you want to shelve it or not. Press "?" at the prompt for help.
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.
28
41
takes_args = ['file*']
29
takes_options = [Option('pick'), 'message', 'revision']
30
def run(self, pick=False, file_list=None, message=None, revision=None):
31
if file_list is not None and len(file_list) > 0:
32
branchdir = file_list[0]
42
takes_options = ['message', 'revision',
43
Option('all', help='Shelve all changes without prompting')]
45
def run(self, all=False, file_list=None, message=None, revision=None):
36
46
if revision is not None and revision:
37
47
if len(revision) == 1:
38
48
revision = revision[0]
40
raise BzrCommandError("shelve only accepts a single revision "
50
raise CommandError("shelve only accepts a single revision "
44
return s.shelve(pick, message, revision, file_list)
53
source = BzrPatchSource(revision, file_list)
54
s = Shelf(source.base)
55
s.shelve(source, all, message)
59
# The following classes are only used as subcommands for 'shelf', they're
60
# not to be registered directly with bzr.
62
class cmd_shelf_list(bzrlib.commands.Command):
63
"""List the patches on the current shelf."""
64
aliases = ['list', 'ls']
69
class cmd_shelf_delete(bzrlib.commands.Command):
70
"""Delete the patch from the current shelf."""
71
aliases = ['delete', 'del']
72
takes_args = ['patch']
74
self.shelf.delete(patch)
77
class cmd_shelf_switch(bzrlib.commands.Command):
78
"""Switch to the other shelf, create it if necessary."""
80
takes_args = ['othershelf']
81
def run(self, othershelf):
82
s = Shelf(self.shelf.base, othershelf)
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)
94
class cmd_shelf_upgrade(bzrlib.commands.Command):
95
"""Upgrade old format shelves."""
101
class cmd_shelf(bzrlib.commands.Command):
102
"""Perform various operations on your shelved patches. See also shelve."""
103
takes_args = ['subcommand', 'args*']
105
subcommands = [cmd_shelf_list, cmd_shelf_delete, cmd_shelf_switch,
106
cmd_shelf_show, cmd_shelf_upgrade]
108
def run(self, subcommand, args_list):
111
cmd = self._get_cmd_object(subcommand)
112
source = BzrPatchSource()
113
s = Shelf(source.base)
115
return cmd.run_argv_aliases(args_list)
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:
122
raise CommandError("Unknown shelf subcommand '%s'" % cmd_name)
125
text = ["%s\n\nSubcommands:\n" % self.__doc__]
127
for cmd_class in self.subcommands:
128
text.extend(self.sub_help(cmd_class) + ['\n'])
132
def sub_help(self, cmd_class):
134
cmd_obj = cmd_class()
137
usage = command_usage(cmd_obj)
138
usage = usage.replace('bzr shelf-', '')
139
text.append('%s%s\n' % (indent, usage))
141
text.append('%s%s\n' % (2 * indent, cmd_class.__doc__))
143
# Somewhat copied from bzrlib.help.help_on_command_options
145
for option_name, option in sorted(cmd_obj.options().items()):
146
if option_name == 'help':
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))
155
if len(option_help) > 0:
156
text.append('%soptions:\n' % (2 * indent))
157
text.extend(option_help)
46
162
class cmd_unshelve(bzrlib.commands.Command):
47
"""Reinstate the most recently shelved changes.
163
"""Restore shelved changes.
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.
48
168
See 'shelve' for more information.
50
takes_options = [Option('pick')]
51
def run(self, pick=False):
53
return s.unshelve(pick)
171
Option('all', help='Unshelve all changes without prompting'),
172
Option('force', help='Force unshelving even if errors occur'),
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)
181
bzrlib.commands.register_command(cmd_shelf)
55
182
bzrlib.commands.register_command(cmd_shelve)
56
183
bzrlib.commands.register_command(cmd_unshelve)