15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
# TODO: Split the command framework away from the actual commands.
20
# TODO: probably should say which arguments are candidates for glob
21
# expansion on windows and do that at the command level.
22
27
from bzrlib.trace import mutter, note, log_error, warning
136
141
raise BzrCommandError(msg)
144
def get_merge_type(typestring):
145
"""Attempt to find the merge class/factory associated with a string."""
146
from merge import merge_types
148
return merge_types[typestring][0]
150
templ = '%s%%7s: %%s' % (' '*12)
151
lines = [templ % (f[0], f[1][1]) for f in merge_types.iteritems()]
152
type_list = '\n'.join(lines)
153
msg = "No known merge type %s. Supported types are:\n%s" %\
154
(typestring, type_list)
155
raise BzrCommandError(msg)
140
159
def _get_cmd_dict(plugins_override=True):
237
256
class ExternalCommand(Command):
238
257
"""Class to wrap external commands.
240
We cheat a little here, when get_cmd_class() calls us we actually give it back
241
an object we construct that has the appropriate path, help, options etc for the
244
When run_bzr() tries to instantiate that 'class' it gets caught by the __call__
245
method, which we override to call the Command.__init__ method. That then calls
246
our run method which is pretty straight forward.
248
The only wrinkle is that we have to map bzr's dictionary of options and arguments
249
back into command line options and arguments for the script.
259
We cheat a little here, when get_cmd_class() calls us we actually
260
give it back an object we construct that has the appropriate path,
261
help, options etc for the specified command.
263
When run_bzr() tries to instantiate that 'class' it gets caught by
264
the __call__ method, which we override to call the Command.__init__
265
method. That then calls our run method which is pretty straight
268
The only wrinkle is that we have to map bzr's dictionary of options
269
and arguments back into command line options and arguments for the
252
273
def find_command(cls, cmd):
553
class cmd_mv(Command):
554
"""Move or rename a file.
557
bzr mv OLDNAME NEWNAME
558
bzr mv SOURCE... DESTINATION
560
If the last argument is a versioned directory, all the other names
561
are moved into it. Otherwise, there must be exactly two arguments
562
and the file is changed to a new name, which must not already exist.
564
Files cannot be moved between branches.
566
takes_args = ['names*']
567
def run(self, names_list):
568
if len(names_list) < 2:
569
raise BzrCommandError("missing file argument")
570
b = find_branch(names_list[0])
572
rel_names = [b.relpath(x) for x in names_list]
574
if os.path.isdir(names_list[-1]):
575
# move into existing directory
576
b.move(rel_names[:-1], rel_names[-1])
578
if len(names_list) != 2:
579
raise BzrCommandError('to mv multiple files the destination '
580
'must be a versioned directory')
581
b.move(rel_names[0], rel_names[1])
533
586
class cmd_pull(Command):
601
654
takes_args = ['from_location', 'to_location?']
602
655
takes_options = ['revision']
656
aliases = ['get', 'clone']
604
658
def run(self, from_location, to_location=None, revision=None):
606
660
from bzrlib.merge import merge
607
from bzrlib.branch import DivergedBranches, NoSuchRevision, \
661
from bzrlib.branch import DivergedBranches, \
608
662
find_cached_branch, Branch
609
663
from shutil import rmtree
610
664
from meta_store import CachedStore
797
851
If files are listed, only the changes in those files are listed.
798
852
Otherwise, all changes for the tree are listed.
800
TODO: Given two revision arguments, show the difference between them.
802
854
TODO: Allow diff across branches.
804
856
TODO: Option to use external diff command; could be GNU diff, wdiff,
832
889
b = find_branch('.')
834
# TODO: Make show_diff support taking 2 arguments
836
891
if revision is not None:
837
if len(revision) != 1:
838
raise BzrCommandError('bzr diff --revision takes exactly one revision identifier')
839
base_rev = revision[0]
841
show_diff(b, base_rev, specific_files=file_list,
842
external_diff_options=diff_options)
892
if len(revision) == 1:
893
show_diff(b, revision[0], specific_files=file_list,
894
external_diff_options=diff_options)
895
elif len(revision) == 2:
896
show_diff(b, revision[0], specific_files=file_list,
897
external_diff_options=diff_options,
898
revision2=revision[1])
900
raise BzrCommandError('bzr diff --revision takes exactly one or two revision identifiers')
902
show_diff(b, None, specific_files=file_list,
903
external_diff_options=diff_options)
929
989
takes_args = ['filename?']
930
takes_options = ['forward', 'timezone', 'verbose', 'show-ids', 'revision','long', 'message']
990
takes_options = ['forward', 'timezone', 'verbose', 'show-ids', 'revision',
991
'long', 'message', 'short',]
932
993
def run(self, filename=None, timezone='original',
1199
1261
class cmd_commit(Command):
1200
1262
"""Commit changes into a new revision.
1264
If no arguments are given, the entire tree is committed.
1202
1266
If selected files are specified, only changes to those files are
1203
committed. If a directory is specified then its contents are also
1267
committed. If a directory is specified then the directory and everything
1268
within it is committed.
1206
1270
A selected-file commit may fail in some cases where the committed
1207
1271
tree would be invalid, such as trying to commit a file in a
1215
1279
takes_options = ['message', 'file', 'verbose', 'unchanged']
1216
1280
aliases = ['ci', 'checkin']
1282
# TODO: Give better message for -s, --summary, used by tla people
1218
1284
def run(self, message=None, file=None, verbose=True, selected_list=None,
1219
1285
unchanged=False):
1220
1286
from bzrlib.errors import PointlessCommit
1242
1310
message = codecs.open(file, 'rt', bzrlib.user_encoding).read()
1244
1312
b = find_branch('.')
1314
selected_list = [b.relpath(s) for s in selected_list]
1247
1317
b.commit(message, verbose=verbose,
1248
1318
specific_files=selected_list,
1556
class cmd_missing(Command):
1557
"""What is missing in this branch relative to other branch.
1559
takes_args = ['remote?']
1560
aliases = ['mis', 'miss']
1561
# We don't have to add quiet to the list, because
1562
# unknown options are parsed as booleans
1563
takes_options = ['verbose', 'quiet']
1565
def run(self, remote=None, verbose=False, quiet=False):
1566
from bzrlib.branch import find_branch, DivergedBranches
1567
from bzrlib.errors import BzrCommandError
1568
from bzrlib.missing import get_parent, show_missing
1570
if verbose and quiet:
1571
raise BzrCommandError('Cannot pass both quiet and verbose')
1573
b = find_branch('.')
1574
parent = get_parent(b)
1577
raise BzrCommandError("No missing location known or specified.")
1580
print "Using last location: %s" % parent
1582
elif parent is None:
1583
# We only update x-pull if it did not exist, missing should not change the parent
1584
b.controlfile('x-pull', 'wb').write(remote + '\n')
1585
br_remote = find_branch(remote)
1587
return show_missing(b, br_remote, verbose=verbose, quiet=quiet)
1486
1590
class cmd_plugins(Command):
1487
1591
"""List plugins"""
1684
def _parse_master_args(argv):
1685
"""Parse the arguments that always go with the original command.
1686
These are things like bzr --no-plugins, etc.
1688
There are now 2 types of option flags. Ones that come *before* the command,
1689
and ones that come *after* the command.
1690
Ones coming *before* the command are applied against all possible commands.
1691
And are generally applied before plugins are loaded.
1693
The current list are:
1694
--builtin Allow plugins to load, but don't let them override builtin commands,
1695
they will still be allowed if they do not override a builtin.
1696
--no-plugins Don't load any plugins. This lets you get back to official source
1698
--profile Enable the hotspot profile before running the command.
1699
For backwards compatibility, this is also a non-master option.
1700
--version Spit out the version of bzr that is running and exit.
1701
This is also a non-master option.
1702
--help Run help and exit, also a non-master option (I think that should stay, though)
1704
>>> argv, opts = _parse_master_args(['--test'])
1705
Traceback (most recent call last):
1707
BzrCommandError: Invalid master option: 'test'
1708
>>> argv, opts = _parse_master_args(['--version', 'command'])
1711
>>> print opts['version']
1713
>>> argv, opts = _parse_master_args(['--profile', 'command', '--more-options'])
1715
['command', '--more-options']
1716
>>> print opts['profile']
1718
>>> argv, opts = _parse_master_args(['--no-plugins', 'command'])
1721
>>> print opts['no-plugins']
1723
>>> print opts['profile']
1725
>>> argv, opts = _parse_master_args(['command', '--profile'])
1727
['command', '--profile']
1728
>>> print opts['profile']
1731
master_opts = {'builtin':False,
1739
if arg[:2] != '--': # at the first non-option, we return the rest
1741
arg = arg[2:] # Remove '--'
1742
if arg not in master_opts:
1743
# We could say that this is not an error, that we should
1744
# just let it be handled by the main section instead
1745
raise BzrCommandError('Invalid master option: %r' % arg)
1746
argv.pop(0) # We are consuming this entry
1747
master_opts[arg] = True
1748
return argv, master_opts
1752
1790
def run_bzr(argv):
1753
1791
"""Execute a command.
1756
1794
logging and error handling.
1759
The command-line arguments, without the program name.
1797
The command-line arguments, without the program name from argv[0]
1761
1799
Returns a command status or raises an exception.
1801
Special master options: these must come before the command because
1802
they control how the command is interpreted.
1805
Do not load plugin modules at all
1808
Only use builtin commands. (Plugins are still allowed to change
1812
Run under the Python profiler.
1763
1815
argv = [a.decode(bzrlib.user_encoding) for a in argv]
1765
# some options like --builtin and --no-plugins have special effects
1766
argv, master_opts = _parse_master_args(argv)
1767
if not master_opts['no-plugins']:
1817
opt_profile = opt_no_plugins = opt_builtin = False
1819
# --no-plugins is handled specially at a very early stage. We need
1820
# to load plugins before doing other command parsing so that they
1821
# can override commands, but this needs to happen first.
1824
if a == '--profile':
1826
elif a == '--no-plugins':
1827
opt_no_plugins = True
1828
elif a == '--builtin':
1834
if not opt_no_plugins:
1768
1835
from bzrlib.plugin import load_plugins
1771
1838
args, opts = parse_args(argv)
1773
if master_opts.get('help') or 'help' in opts:
1774
1841
from bzrlib.help import help
1785
if args and args[0] == 'builtin':
1786
include_plugins=False
1790
cmd = str(args.pop(0))
1792
1853
print >>sys.stderr, "please try 'bzr help' for help"
1795
plugins_override = not (master_opts['builtin'])
1796
canonical_cmd, cmd_class = get_cmd_class(cmd, plugins_override=plugins_override)
1798
profile = master_opts['profile']
1799
# For backwards compatibility, I would rather stick with --profile being a
1800
# master/global option
1801
if 'profile' in opts:
1856
cmd = str(args.pop(0))
1858
canonical_cmd, cmd_class = \
1859
get_cmd_class(cmd, plugins_override=not opt_builtin)
1805
1861
# check options are reasonable
1806
1862
allowed = cmd_class.takes_options