19
19
import sys, os, time, os.path
22
23
from bzrlib.trace import mutter, note, log_error
23
24
from bzrlib.errors import bailout, BzrError, BzrCheckError, BzrCommandError
24
25
from bzrlib.osutils import quotefn, pumpfile, isdir, isfile
25
from bzrlib.tree import RevisionTree, EmptyTree, Tree
26
from bzrlib.tree import RevisionTree, EmptyTree, WorkingTree, Tree
26
27
from bzrlib.revision import Revision
27
28
from bzrlib import Branch, Inventory, InventoryEntry, ScratchBranch, BZRDIR, \
29
from bzrlib import merge
32
32
def _squish_command_name(cmd):
152
class ExternalCommand(Command):
153
"""Class to wrap external commands.
155
We cheat a little here, when get_cmd_class() calls us we actually give it back
156
an object we construct that has the appropriate path, help, options etc for the
159
When run_bzr() tries to instantiate that 'class' it gets caught by the __call__
160
method, which we override to call the Command.__init__ method. That then calls
161
our run method which is pretty straight forward.
163
The only wrinkle is that we have to map bzr's dictionary of options and arguments
164
back into command line options and arguments for the script.
167
def find_command(cls, cmd):
168
bzrpath = os.environ.get('BZRPATH', '')
170
for dir in bzrpath.split(':'):
171
path = os.path.join(dir, cmd)
172
if os.path.isfile(path):
173
return ExternalCommand(path)
177
find_command = classmethod(find_command)
179
def __init__(self, path):
182
# TODO: If either of these fail, we should detect that and
183
# assume that path is not really a bzr plugin after all.
185
pipe = os.popen('%s --bzr-usage' % path, 'r')
186
self.takes_options = pipe.readline().split()
187
self.takes_args = pipe.readline().split()
190
pipe = os.popen('%s --bzr-help' % path, 'r')
191
self.__doc__ = pipe.read()
194
def __call__(self, options, arguments):
195
Command.__init__(self, options, arguments)
198
def run(self, **kargs):
206
if OPTIONS.has_key(name):
208
opts.append('--%s' % name)
209
if value is not None and value is not True:
210
opts.append(str(value))
212
# it's an arg, or arg list
213
if type(value) is not list:
219
self.status = os.spawnv(os.P_WAIT, self.path, [self.path] + opts + args)
223
115
class cmd_status(Command):
224
116
"""Display status summary.
226
This reports on versioned and unknown files, reporting them
227
grouped by state. Possible states are:
230
Versioned in the working copy but not in the previous revision.
233
Versioned in the previous revision but removed or deleted
237
Path of this file changed from the previous revision;
238
the text may also have changed. This includes files whose
239
parent directory was renamed.
242
Text has changed since the previous revision.
245
Nothing about this file has changed since the previous revision.
246
Only shown with --all.
249
Not versioned and not matching an ignore pattern.
251
To see ignored files use 'bzr ignored'. For details in the
252
changes to file texts, use 'bzr diff'.
254
If no arguments are specified, the status of the entire working
255
directory is shown. Otherwise, only the status of the specified
256
files or directories is reported. If a directory is given, status
257
is reported for everything inside that directory.
118
For each file there is a single line giving its file state and name.
119
The name is that in the current revision unless it is deleted or
120
missing, in which case the old name is shown.
259
takes_args = ['file*']
260
takes_options = ['all', 'show-ids']
122
takes_options = ['all']
261
123
aliases = ['st', 'stat']
263
def run(self, all=False, show_ids=False, file_list=None):
265
b = Branch(file_list[0], lock_mode='r')
266
file_list = [b.relpath(x) for x in file_list]
267
# special case: only one path was given and it's the root
269
if file_list == ['']:
272
b = Branch('.', lock_mode='r')
274
status.show_status(b, show_unchanged=all, show_ids=show_ids,
275
specific_files=file_list)
125
def run(self, all=False):
126
#import bzrlib.status
127
#bzrlib.status.tree_status(Branch('.'))
128
Branch('.').show_status(show_all=all)
278
131
class cmd_cat_revision(Command):
528
376
def run(self, revision=None, file_list=None):
529
377
from bzrlib.diff import show_diff
530
from bzrlib import find_branch
533
b = find_branch(file_list[0], lock_mode='r')
534
file_list = [b.relpath(f) for f in file_list]
535
if file_list == ['']:
536
# just pointing to top-of-tree
539
b = Branch('.', lock_mode='r')
541
show_diff(b, revision, specific_files=file_list)
379
show_diff(Branch('.'), revision, file_list)
547
382
class cmd_deleted(Command):
570
class cmd_modified(Command):
571
"""List files modified in working tree."""
576
inv = b.read_working_inventory()
577
sc = statcache.update_cache(b, inv)
578
basis = b.basis_tree()
579
basis_inv = basis.inventory
581
# We used to do this through iter_entries(), but that's slow
582
# when most of the files are unmodified, as is usually the
583
# case. So instead we iterate by inventory entry, and only
584
# calculate paths as necessary.
586
for file_id in basis_inv:
587
cacheentry = sc.get(file_id)
588
if not cacheentry: # deleted
590
ie = basis_inv[file_id]
591
if cacheentry[statcache.SC_SHA1] != ie.text_sha1:
592
path = inv.id2path(file_id)
597
class cmd_added(Command):
598
"""List files added in working tree."""
602
wt = b.working_tree()
603
basis_inv = b.basis_tree().inventory
606
if file_id in basis_inv:
608
path = inv.id2path(file_id)
609
if not os.access(b.abspath(path), os.F_OK):
615
404
class cmd_root(Command):
616
405
"""Show the tree root directory.
620
409
takes_args = ['filename?']
621
410
def run(self, filename=None):
622
411
"""Print the branch root."""
623
from branch import find_branch
624
b = find_branch(filename)
625
print getattr(b, 'base', None) or getattr(b, 'baseurl')
412
print bzrlib.branch.find_branch_root(filename)
628
416
class cmd_log(Command):
629
417
"""Show log of this branch.
631
To request a range of logs, you can use the command -r begin:end
632
-r revision requests a specific revision, -r :end or -r begin: are
635
TODO: Make --revision support uuid: and hash: [future tag:] notation.
419
TODO: Options to show ids; to limit range; etc.
639
takes_args = ['filename?']
640
takes_options = ['forward', 'timezone', 'verbose', 'show-ids', 'revision']
642
def run(self, filename=None, timezone='original',
647
from bzrlib import show_log, find_branch
650
direction = (forward and 'forward') or 'reverse'
653
b = find_branch(filename, lock_mode='r')
654
fp = b.relpath(filename)
656
file_id = b.read_working_inventory().path2id(fp)
658
file_id = None # points to branch root
660
b = find_branch('.', lock_mode='r')
664
revision = [None, None]
665
elif isinstance(revision, int):
666
revision = [revision, revision]
671
assert len(revision) == 2
673
mutter('encoding log as %r' % bzrlib.user_encoding)
674
outf = codecs.getwriter(bzrlib.user_encoding)(sys.stdout)
677
show_timezone=timezone,
682
start_revision=revision[0],
683
end_revision=revision[1])
687
class cmd_touching_revisions(Command):
688
"""Return revision-ids which affected a particular file.
690
A more user-friendly interface is "bzr log FILE"."""
692
takes_args = ["filename"]
693
def run(self, filename):
694
b = Branch(filename, lock_mode='r')
695
inv = b.read_working_inventory()
696
file_id = inv.path2id(b.relpath(filename))
697
for revno, revision_id, what in bzrlib.log.find_touching_revisions(b, file_id):
698
print "%6d %s" % (revno, what)
421
takes_options = ['timezone', 'verbose']
422
def run(self, timezone='original', verbose=False):
423
Branch('.', lock_mode='r').write_log(show_timezone=timezone, verbose=verbose)
701
426
class cmd_ls(Command):
737
462
class cmd_ignore(Command):
738
"""Ignore a command or pattern
740
To remove patterns from the ignore list, edit the .bzrignore file.
742
If the pattern contains a slash, it is compared to the whole path
743
from the branch root. Otherwise, it is comapred to only the last
744
component of the path.
746
Ignore patterns are case-insensitive on case-insensitive systems.
748
Note: wildcards must be quoted from the shell on Unix.
751
bzr ignore ./Makefile
463
"""Ignore a command or pattern"""
754
464
takes_args = ['name_pattern']
756
466
def run(self, name_pattern):
757
from bzrlib.atomicfile import AtomicFile
761
ifn = b.abspath('.bzrignore')
763
if os.path.exists(ifn):
766
igns = f.read().decode('utf-8')
772
if igns and igns[-1] != '\n':
774
igns += name_pattern + '\n'
777
f = AtomicFile(ifn, 'wt')
778
f.write(igns.encode('utf-8'))
469
# XXX: This will fail if it's a hardlink; should use an AtomicFile class.
470
f = open(b.abspath('.bzrignore'), 'at')
471
f.write(name_pattern + '\n')
783
474
inv = b.working_tree().inventory
784
475
if inv.path2id('.bzrignore'):
861
550
class cmd_commit(Command):
862
551
"""Commit changes into a new revision.
864
If selected files are specified, only changes to those files are
865
committed. If a directory is specified then its contents are also
868
A selected-file commit may fail in some cases where the committed
869
tree would be invalid, such as trying to commit a file in a
870
newly-added directory that is not itself committed.
553
TODO: Commit only selected files.
872
555
TODO: Run hooks on tree to-be-committed, and after commit.
874
557
TODO: Strict commit that fails if there are unknown or deleted files.
876
takes_args = ['selected*']
877
takes_options = ['message', 'file', 'verbose']
559
takes_options = ['message', 'verbose']
878
560
aliases = ['ci', 'checkin']
880
def run(self, message=None, file=None, verbose=True, selected_list=None):
881
from bzrlib.commit import commit
883
## Warning: shadows builtin file()
884
if not message and not file:
885
raise BzrCommandError("please specify a commit message",
886
["use either --message or --file"])
887
elif message and file:
888
raise BzrCommandError("please specify either --message or --file")
892
message = codecs.open(file, 'rt', bzrlib.user_encoding).read()
895
commit(b, message, verbose=verbose, specific_files=selected_list)
562
def run(self, message=None, verbose=False):
564
raise BzrCommandError("please specify a commit message")
565
Branch('.').commit(message, verbose=verbose)
898
568
class cmd_check(Command):