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
17
"""Bazaar-NG -- a free distributed version-control tool
20
**WARNING: THIS IS AN UNSTABLE DEVELOPMENT VERSION**
22
* Metadata format is not stable yet -- you may need to
23
discard history in the future.
25
* Many commands unimplemented or partially implemented.
27
* Space-inefficient storage.
29
* No merge operators yet.
36
Show software version/licence/non-warranty.
38
Start versioning the current directory
42
Show revision history.
45
bzr move FROM... DESTDIR
46
Move one or more files to a different directory.
48
Show changes from last revision to working copy.
49
bzr commit -m 'MESSAGE'
50
Store current state as new revision.
51
bzr export [-r REVNO] DESTINATION
52
Export the branch state at a previous version.
54
Show summary of pending changes.
56
Make a file not versioned.
58
Show statistics about this branch.
60
Verify history is stored safely.
61
(for more type 'bzr help commands')
67
import sys, os, time, types, shutil, tempfile, fnmatch, difflib, os.path
19
import sys, os, time, os.path
68
20
from sets import Set
69
from pprint import pprint
74
from bzrlib.store import ImmutableStore
75
23
from bzrlib.trace import mutter, note, log_error
76
24
from bzrlib.errors import bailout, BzrError, BzrCheckError, BzrCommandError
77
25
from bzrlib.osutils import quotefn, pumpfile, isdir, isfile
78
from bzrlib.tree import RevisionTree, EmptyTree, WorkingTree, Tree
26
from bzrlib.tree import RevisionTree, EmptyTree, Tree
79
27
from bzrlib.revision import Revision
80
28
from bzrlib import Branch, Inventory, InventoryEntry, ScratchBranch, BZRDIR, \
83
BZR_DIFF_FORMAT = "## Bazaar-NG diff, format 0 ##\n"
84
BZR_PATCHNAME_FORMAT = 'cset:sha1:%s'
86
## standard representation
87
NONE_STRING = '(none)'
30
from bzrlib import merge
33
def _squish_command_name(cmd):
34
return 'cmd_' + cmd.replace('-', '_')
37
def _unsquish_command_name(cmd):
38
assert cmd.startswith("cmd_")
39
return cmd[4:].replace('_','-')
42
"""Return canonical name and class for all registered commands."""
43
for k, v in globals().iteritems():
44
if k.startswith("cmd_"):
45
yield _unsquish_command_name(k), v
101
47
def get_cmd_class(cmd):
104
cmd = CMD_ALIASES.get(cmd, cmd)
48
"""Return the canonical name and command class for a command.
50
cmd = str(cmd) # not unicode
52
# first look up this command under the specified name
107
cmd_class = globals()['cmd_' + cmd.replace('-', '_')]
54
return cmd, globals()[_squish_command_name(cmd)]
109
raise BzrError("unknown command %r" % cmd)
111
return cmd, cmd_class
58
# look for any command which claims this as an alias
59
for cmdname, cmdclass in get_all_cmds():
60
if cmd in cmdclass.aliases:
61
return cmdname, cmdclass
63
cmdclass = ExternalCommand.find_command(cmd)
67
raise BzrCommandError("unknown command %r" % cmd)
155
110
This is invoked with the options and arguments bound to
156
111
keyword parameters.
158
Return True if the command was successful, False if not.
113
Return 0 or None if the command was successful, or a shell
119
class ExternalCommand(Command):
120
"""Class to wrap external commands.
122
We cheat a little here, when get_cmd_class() calls us we actually give it back
123
an object we construct that has the appropriate path, help, options etc for the
126
When run_bzr() tries to instantiate that 'class' it gets caught by the __call__
127
method, which we override to call the Command.__init__ method. That then calls
128
our run method which is pretty straight forward.
130
The only wrinkle is that we have to map bzr's dictionary of options and arguments
131
back into command line options and arguments for the script.
134
def find_command(cls, cmd):
135
bzrpath = os.environ.get('BZRPATH', '')
137
for dir in bzrpath.split(':'):
138
path = os.path.join(dir, cmd)
139
if os.path.isfile(path):
140
return ExternalCommand(path)
144
find_command = classmethod(find_command)
146
def __init__(self, path):
149
# TODO: If either of these fail, we should detect that and
150
# assume that path is not really a bzr plugin after all.
152
pipe = os.popen('%s --bzr-usage' % path, 'r')
153
self.takes_options = pipe.readline().split()
154
self.takes_args = pipe.readline().split()
157
pipe = os.popen('%s --bzr-help' % path, 'r')
158
self.__doc__ = pipe.read()
161
def __call__(self, options, arguments):
162
Command.__init__(self, options, arguments)
165
def run(self, **kargs):
173
if OPTIONS.has_key(name):
175
opts.append('--%s' % name)
176
if value is not None and value is not True:
177
opts.append(str(value))
179
# it's an arg, or arg list
180
if type(value) is not list:
186
self.status = os.spawnv(os.P_WAIT, self.path, [self.path] + opts + args)
164
190
class cmd_status(Command):
165
191
"""Display status summary.
167
For each file there is a single line giving its file state and name.
168
The name is that in the current revision unless it is deleted or
169
missing, in which case the old name is shown.
193
This reports on versioned and unknown files, reporting them
194
grouped by state. Possible states are:
197
Versioned in the working copy but not in the previous revision.
200
Versioned in the previous revision but removed or deleted
204
Path of this file changed from the previous revision;
205
the text may also have changed. This includes files whose
206
parent directory was renamed.
209
Text has changed since the previous revision.
212
Nothing about this file has changed since the previous revision.
213
Only shown with --all.
216
Not versioned and not matching an ignore pattern.
218
To see ignored files use 'bzr ignored'. For details in the
219
changes to file texts, use 'bzr diff'.
221
If no arguments are specified, the status of the entire working
222
directory is shown. Otherwise, only the status of the specified
223
files or directories is reported. If a directory is given, status
224
is reported for everything inside that directory.
171
takes_options = ['all']
226
takes_args = ['file*']
227
takes_options = ['all', 'show-ids']
228
aliases = ['st', 'stat']
173
def run(self, all=False):
174
#import bzrlib.status
175
#bzrlib.status.tree_status(Branch('.'))
176
Branch('.').show_status(show_all=all)
230
def run(self, all=False, show_ids=False, file_list=None):
232
b = Branch(file_list[0], lock_mode='r')
233
file_list = [b.relpath(x) for x in file_list]
234
# special case: only one path was given and it's the root
236
if file_list == ['']:
239
b = Branch('.', lock_mode='r')
241
status.show_status(b, show_unchanged=all, show_ids=show_ids,
242
specific_files=file_list)
179
245
class cmd_cat_revision(Command):
527
class cmd_modified(Command):
528
"""List files modified in working tree."""
533
inv = b.read_working_inventory()
534
sc = statcache.update_cache(b, inv)
535
basis = b.basis_tree()
536
basis_inv = basis.inventory
538
# We used to do this through iter_entries(), but that's slow
539
# when most of the files are unmodified, as is usually the
540
# case. So instead we iterate by inventory entry, and only
541
# calculate paths as necessary.
543
for file_id in basis_inv:
544
cacheentry = sc.get(file_id)
545
if not cacheentry: # deleted
547
ie = basis_inv[file_id]
548
if cacheentry[statcache.SC_SHA1] != ie.text_sha1:
549
path = inv.id2path(file_id)
554
class cmd_added(Command):
555
"""List files added in working tree."""
559
wt = b.working_tree()
560
basis_inv = b.basis_tree().inventory
563
if file_id in basis_inv:
565
path = inv.id2path(file_id)
566
if not os.access(b.abspath(path), os.F_OK):
451
572
class cmd_root(Command):
452
573
"""Show the tree root directory.
456
577
takes_args = ['filename?']
457
578
def run(self, filename=None):
458
579
"""Print the branch root."""
459
print bzrlib.branch.find_branch_root(filename)
580
from branch import find_branch
581
b = find_branch(filename)
582
print getattr(b, 'base', None) or getattr(b, 'baseurl')
463
585
class cmd_log(Command):
464
586
"""Show log of this branch.
466
TODO: Options to show ids; to limit range; etc.
588
TODO: Option to limit range.
590
TODO: Perhaps show most-recent first with an option for last.
468
takes_options = ['timezone', 'verbose']
469
def run(self, timezone='original', verbose=False):
470
Branch('.').write_log(show_timezone=timezone, verbose=verbose)
592
takes_args = ['filename?']
593
takes_options = ['timezone', 'verbose', 'show-ids']
594
def run(self, filename=None, timezone='original', verbose=False, show_ids=False):
595
from branch import find_branch
596
b = find_branch((filename or '.'), lock_mode='r')
598
filename = b.relpath(filename)
599
bzrlib.show_log(b, filename,
600
show_timezone=timezone,
606
class cmd_touching_revisions(Command):
607
"""Return revision-ids which affected a particular file."""
609
takes_args = ["filename"]
610
def run(self, filename):
611
b = Branch(filename, lock_mode='r')
612
inv = b.read_working_inventory()
613
file_id = inv.path2id(b.relpath(filename))
614
for revno, revision_id, what in bzrlib.log.find_touching_revisions(b, file_id):
615
print "%6d %s" % (revno, what)
473
618
class cmd_ls(Command):
509
654
class cmd_ignore(Command):
510
"""Ignore a command or pattern"""
655
"""Ignore a command or pattern
657
To remove patterns from the ignore list, edit the .bzrignore file.
659
If the pattern contains a slash, it is compared to the whole path
660
from the branch root. Otherwise, it is comapred to only the last
661
component of the path.
663
Ignore patterns are case-insensitive on case-insensitive systems.
665
Note: wildcards must be quoted from the shell on Unix.
668
bzr ignore ./Makefile
511
671
takes_args = ['name_pattern']
513
673
def run(self, name_pattern):
674
from bzrlib.atomicfile import AtomicFile
516
# XXX: This will fail if it's a hardlink; should use an AtomicFile class.
517
f = open(b.abspath('.bzrignore'), 'at')
518
f.write(name_pattern + '\n')
678
ifn = b.abspath('.bzrignore')
680
# FIXME: probably doesn't handle non-ascii patterns
682
if os.path.exists(ifn):
683
f = b.controlfile(ifn, 'rt')
689
if igns and igns[-1] != '\n':
691
igns += name_pattern + '\n'
693
f = AtomicFile(ifn, 'wt')
521
697
inv = b.working_tree().inventory
522
698
if inv.path2id('.bzrignore'):
596
775
class cmd_commit(Command):
597
776
"""Commit changes into a new revision.
599
TODO: Commit only selected files.
778
If selected files are specified, only changes to those files are
779
committed. If a directory is specified then its contents are also
782
A selected-file commit may fail in some cases where the committed
783
tree would be invalid, such as trying to commit a file in a
784
newly-added directory that is not itself committed.
601
786
TODO: Run hooks on tree to-be-committed, and after commit.
603
788
TODO: Strict commit that fails if there are unknown or deleted files.
605
takes_options = ['message', 'verbose']
607
def run(self, message=None, verbose=False):
609
raise BzrCommandError("please specify a commit message")
610
Branch('.').commit(message, verbose=verbose)
790
takes_args = ['selected*']
791
takes_options = ['message', 'file', 'verbose']
792
aliases = ['ci', 'checkin']
794
def run(self, message=None, file=None, verbose=False, selected_list=None):
795
from bzrlib.commit import commit
797
## Warning: shadows builtin file()
798
if not message and not file:
799
raise BzrCommandError("please specify a commit message",
800
["use either --message or --file"])
801
elif message and file:
802
raise BzrCommandError("please specify either --message or --file")
806
message = codecs.open(file, 'rt', bzrlib.user_encoding).read()
809
commit(b, message, verbose=verbose, specific_files=selected_list)
613
812
class cmd_check(Command):
697
915
For a list of all available commands, say 'bzr help commands'."""
698
916
takes_args = ['topic?']
700
919
def run(self, topic=None):
704
def help(topic=None):
707
elif topic == 'commands':
710
help_on_command(topic)
713
def help_on_command(cmdname):
714
cmdname = str(cmdname)
716
from inspect import getdoc
717
topic, cmdclass = get_cmd_class(cmdname)
719
doc = getdoc(cmdclass)
721
raise NotImplementedError("sorry, no detailed help yet for %r" % cmdname)
724
short, rest = doc.split('\n', 1)
729
print 'usage: bzr ' + topic,
730
for aname in cmdclass.takes_args:
731
aname = aname.upper()
732
if aname[-1] in ['$', '+']:
733
aname = aname[:-1] + '...'
734
elif aname[-1] == '?':
735
aname = '[' + aname[:-1] + ']'
736
elif aname[-1] == '*':
737
aname = '[' + aname[:-1] + '...]'
744
help_on_option(cmdclass.takes_options)
747
def help_on_option(options):
755
for shortname, longname in SHORT_OPTIONS.items():
757
l += ', -' + shortname
763
"""List all commands"""
767
for k, v in globals().items():
768
if k.startswith('cmd_'):
769
accu.append((k[4:].replace('_','-'), v))
771
for cmdname, cmdclass in accu:
775
help = inspect.getdoc(cmdclass)
777
print " " + help.split('\n', 1)[0]
924
class cmd_update_stat_cache(Command):
925
"""Update stat-cache mapping inodes to SHA-1 hashes.
932
statcache.update_cache(b.base, b.read_working_inventory())
780
935
######################################################################
1026
1184
msg = 'assertion failed'
1028
1186
msg += ': ' + str(e)
1029
_report_exception(e, msg)
1187
_report_exception(msg)
1031
1189
except KeyboardInterrupt, e:
1032
_report_exception(e, 'interrupted', quiet=True)
1190
_report_exception('interrupted', quiet=True)
1034
1192
except Exception, e:
1036
if isinstance(e, IOError) and e.errno == errno.EPIPE:
1194
if (isinstance(e, IOError)
1195
and hasattr(e, 'errno')
1196
and e.errno == errno.EPIPE):
1038
1198
msg = 'broken pipe'
1040
1200
msg = str(e).rstrip('\n')
1041
_report_exception(e, msg, quiet)
1201
_report_exception(msg, quiet)
1044
1204
bzrlib.trace.close_trace()