1
# Copyright (C) 2004, 2005, 2006 Canonical Ltd
1
# Copyright (C) 2004, 2005, 2006 by Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
11
# GNU General Public License for more details.
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
from bzrlib.lazy_import import lazy_import
23
lazy_import(globals(), """
31
from bzrlib.trace import warning
22
import bzrlib.commands
23
from bzrlib.trace import warning, mutter
24
from bzrlib.revisionspec import RevisionSpec
25
from bzrlib.errors import BzrCommandError
34
28
def _parse_revision_str(revstr):
38
32
each revision specifier supplied.
40
34
>>> _parse_revision_str('234')
41
[<RevisionSpec_revno 234>]
35
[<RevisionSpec_int 234>]
42
36
>>> _parse_revision_str('234..567')
43
[<RevisionSpec_revno 234>, <RevisionSpec_revno 567>]
37
[<RevisionSpec_int 234>, <RevisionSpec_int 567>]
44
38
>>> _parse_revision_str('..')
45
39
[<RevisionSpec None>, <RevisionSpec None>]
46
40
>>> _parse_revision_str('..234')
47
[<RevisionSpec None>, <RevisionSpec_revno 234>]
41
[<RevisionSpec None>, <RevisionSpec_int 234>]
48
42
>>> _parse_revision_str('234..')
49
[<RevisionSpec_revno 234>, <RevisionSpec None>]
43
[<RevisionSpec_int 234>, <RevisionSpec None>]
50
44
>>> _parse_revision_str('234..456..789') # Maybe this should be an error
51
[<RevisionSpec_revno 234>, <RevisionSpec_revno 456>, <RevisionSpec_revno 789>]
45
[<RevisionSpec_int 234>, <RevisionSpec_int 456>, <RevisionSpec_int 789>]
52
46
>>> _parse_revision_str('234....789') #Error ?
53
[<RevisionSpec_revno 234>, <RevisionSpec None>, <RevisionSpec_revno 789>]
47
[<RevisionSpec_int 234>, <RevisionSpec None>, <RevisionSpec_int 789>]
54
48
>>> _parse_revision_str('revid:test@other.com-234234')
55
49
[<RevisionSpec_revid revid:test@other.com-234234>]
56
50
>>> _parse_revision_str('revid:test@other.com-234234..revid:test@other.com-234235')
57
51
[<RevisionSpec_revid revid:test@other.com-234234>, <RevisionSpec_revid revid:test@other.com-234235>]
58
52
>>> _parse_revision_str('revid:test@other.com-234234..23')
59
[<RevisionSpec_revid revid:test@other.com-234234>, <RevisionSpec_revno 23>]
53
[<RevisionSpec_revid revid:test@other.com-234234>, <RevisionSpec_int 23>]
60
54
>>> _parse_revision_str('date:2005-04-12')
61
55
[<RevisionSpec_date date:2005-04-12>]
62
56
>>> _parse_revision_str('date:2005-04-12 12:24:33')
66
60
>>> _parse_revision_str('date:2005-04-12,12:24:33')
67
61
[<RevisionSpec_date date:2005-04-12,12:24:33>]
68
62
>>> _parse_revision_str('-5..23')
69
[<RevisionSpec_revno -5>, <RevisionSpec_revno 23>]
63
[<RevisionSpec_int -5>, <RevisionSpec_int 23>]
70
64
>>> _parse_revision_str('-5')
71
[<RevisionSpec_revno -5>]
65
[<RevisionSpec_int -5>]
72
66
>>> _parse_revision_str('123a')
73
67
Traceback (most recent call last):
75
NoSuchRevisionSpec: No namespace registered for string: '123a'
69
BzrError: No namespace registered for string: '123a'
76
70
>>> _parse_revision_str('abc')
77
71
Traceback (most recent call last):
79
NoSuchRevisionSpec: No namespace registered for string: 'abc'
73
BzrError: No namespace registered for string: 'abc'
80
74
>>> _parse_revision_str('branch:../branch2')
81
75
[<RevisionSpec_branch branch:../branch2>]
82
76
>>> _parse_revision_str('branch:../../branch2')
83
77
[<RevisionSpec_branch branch:../../branch2>]
84
78
>>> _parse_revision_str('branch:../../branch2..23')
85
[<RevisionSpec_branch branch:../../branch2>, <RevisionSpec_revno 23>]
79
[<RevisionSpec_branch branch:../../branch2>, <RevisionSpec_int 23>]
87
81
# TODO: Maybe move this into revisionspec.py
82
old_format_re = re.compile('\d*:\d*')
83
m = old_format_re.match(revstr)
89
# split on the first .. that is not followed by a / ?
90
sep = re.compile("\\.\\.(?!/)")
91
for x in sep.split(revstr):
92
revs.append(revisionspec.RevisionSpec.from_string(x or None))
86
warning('Colon separator for revision numbers is deprecated.'
88
for rev in revstr.split(':'):
90
revs.append(RevisionSpec(int(rev)))
92
revs.append(RevisionSpec(None))
94
sep = re.compile("\\.\\.(?!/)")
95
for x in sep.split(revstr):
96
revs.append(RevisionSpec(x or None))
150
154
if option is self:
153
def get_negation_name(self):
154
if self.name.startswith('no-'):
157
return 'no-' + self.name
159
def add_option(self, parser, short_name):
160
"""Add this option to an Optparse parser"""
161
option_strings = ['--%s' % self.name]
162
if short_name is not None:
163
option_strings.append('-%s' % short_name)
166
parser.add_option(action='store_true', dest=self.name,
168
default=OptionParser.DEFAULT_VALUE,
170
negation_strings = ['--%s' % self.get_negation_name()]
171
parser.add_option(action='store_false', dest=self.name,
172
help=optparse.SUPPRESS_HELP, *negation_strings)
174
parser.add_option(action='callback',
175
callback=self._optparse_callback,
176
type='string', metavar=self.argname.upper(),
178
default=OptionParser.DEFAULT_VALUE,
181
def _optparse_callback(self, option, opt, value, parser):
182
setattr(parser.values, self.name, self.type(value))
184
def iter_switches(self):
185
"""Iterate through the list of switches provided by the option
187
:return: an iterator of (name, short_name, argname, help)
189
argname = self.argname
190
if argname is not None:
191
argname = argname.upper()
192
yield self.name, self.short_name(), argname, self.help
195
class OptionParser(optparse.OptionParser):
196
"""OptionParser that raises exceptions instead of exiting"""
198
DEFAULT_VALUE = object()
200
def error(self, message):
201
raise errors.BzrCommandError(message)
204
def get_optparser(options):
205
"""Generate an optparse parser for bzrlib-style options"""
207
parser = OptionParser()
208
parser.remove_option('--help')
209
short_options = dict((k.name, v) for v, k in
210
Option.SHORT_OPTIONS.iteritems())
211
for option in options.itervalues():
212
option.add_option(parser, short_options.get(option.name))
216
158
def _global_option(name, **kwargs):
217
159
"""Register o as a global option."""
264
206
_global_option('kind', type=str)
265
207
_global_option('dry-run',
266
208
help="show what would be done, but don't actually do anything")
267
_global_option('name-from-revision', help='The path name in the old tree.')
270
211
def _global_short(short_name, long_name):