bzr branch
http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
|
1
by mbp at sourcefrog
import from baz patch-364 |
1 |
#! /usr/bin/python
|
2 |
||
3 |
||
4 |
# Copyright (C) 2004, 2005 by Martin Pool
|
|
5 |
# Copyright (C) 2005 by Canonical Ltd
|
|
6 |
||
7 |
||
8 |
# This program is free software; you can redistribute it and/or modify
|
|
9 |
# it under the terms of the GNU General Public License as published by
|
|
10 |
# the Free Software Foundation; either version 2 of the License, or
|
|
11 |
# (at your option) any later version.
|
|
12 |
||
13 |
# This program is distributed in the hope that it will be useful,
|
|
14 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
15 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
16 |
# GNU General Public License for more details.
|
|
17 |
||
18 |
# You should have received a copy of the GNU General Public License
|
|
19 |
# along with this program; if not, write to the Free Software
|
|
20 |
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
21 |
||
22 |
"""Bazaar-NG -- a free distributed version-control tool
|
|
23 |
||
24 |
**WARNING: THIS IS AN UNSTABLE DEVELOPMENT VERSION**
|
|
25 |
||
26 |
Current limitation include:
|
|
27 |
||
28 |
* Metadata format is not stable yet -- you may need to
|
|
29 |
discard history in the future.
|
|
30 |
||
31 |
* No handling of subdirectories, symlinks or any non-text files.
|
|
32 |
||
33 |
* Insufficient error handling.
|
|
34 |
||
35 |
* Many commands unimplemented or partially implemented.
|
|
36 |
||
37 |
* Space-inefficient storage.
|
|
38 |
||
39 |
* No merge operators yet.
|
|
40 |
||
41 |
Interesting commands::
|
|
42 |
||
43 |
bzr help
|
|
44 |
Show summary help screen
|
|
45 |
bzr version
|
|
46 |
Show software version/licence/non-warranty.
|
|
47 |
bzr init
|
|
48 |
Start versioning the current directory
|
|
49 |
bzr add FILE...
|
|
50 |
Make files versioned.
|
|
51 |
bzr log
|
|
52 |
Show revision history.
|
|
53 |
bzr diff
|
|
54 |
Show changes from last revision to working copy.
|
|
55 |
bzr commit -m 'MESSAGE'
|
|
56 |
Store current state as new revision.
|
|
57 |
bzr export REVNO DESTINATION
|
|
58 |
Export the branch state at a previous version.
|
|
59 |
bzr status
|
|
60 |
Show summary of pending changes.
|
|
61 |
bzr remove FILE...
|
|
62 |
Make a file not versioned.
|
|
63 |
"""
|
|
64 |
||
65 |
# not currently working:
|
|
66 |
# bzr info
|
|
67 |
# Show some information about this branch.
|
|
68 |
||
69 |
||
70 |
||
71 |
__copyright__ = "Copyright 2005 Canonical Development Ltd." |
|
72 |
__author__ = "Martin Pool <mbp@canonical.com>" |
|
73 |
__docformat__ = "restructuredtext en" |
|
74 |
__version__ = '0.0.0' |
|
75 |
||
76 |
||
77 |
import sys, os, random, time, sha, sets, types, re, shutil, tempfile |
|
78 |
import traceback, socket, fnmatch, difflib |
|
79 |
from os import path |
|
80 |
from sets import Set |
|
81 |
from pprint import pprint |
|
82 |
from stat import * |
|
83 |
from glob import glob |
|
84 |
||
85 |
import bzrlib |
|
86 |
from bzrlib.store import ImmutableStore |
|
87 |
from bzrlib.trace import mutter, note, log_error |
|
88 |
from bzrlib.errors import bailout, BzrError |
|
89 |
from bzrlib.osutils import quotefn, pumpfile, isdir, isfile |
|
90 |
from bzrlib.tree import RevisionTree, EmptyTree, WorkingTree, Tree |
|
91 |
from bzrlib.revision import Revision |
|
92 |
from bzrlib import Branch, Inventory, InventoryEntry, ScratchBranch, BZRDIR, \ |
|
93 |
format_date
|
|
94 |
||
95 |
BZR_DIFF_FORMAT = "## Bazaar-NG diff, format 0 ##\n" |
|
96 |
BZR_PATCHNAME_FORMAT = 'cset:sha1:%s' |
|
97 |
||
98 |
## standard representation
|
|
99 |
NONE_STRING = '(none)' |
|
100 |
EMPTY = 'empty' |
|
101 |
||
102 |
||
103 |
## TODO: Perhaps a different version of inventory commands that
|
|
104 |
## returns iterators...
|
|
105 |
||
106 |
## TODO: Perhaps an AtomicFile class that writes to a temporary file and then renames.
|
|
107 |
||
108 |
## TODO: Some kind of locking on branches. Perhaps there should be a
|
|
109 |
## parameter to the branch object saying whether we want a read or
|
|
110 |
## write lock; release it from destructor. Perhaps don't even need a
|
|
111 |
## read lock to look at immutable objects?
|
|
112 |
||
113 |
## TODO: Perhaps make UUIDs predictable in test mode to make it easier
|
|
114 |
## to compare output?
|
|
115 |
||
|
34
by mbp at sourcefrog
doc |
116 |
## TODO: Some kind of global code to generate the right Branch object
|
117 |
## to work on. Almost, but not quite all, commands need one, and it
|
|
118 |
## can be taken either from their parameters or their working
|
|
119 |
## directory.
|
|
120 |
||
|
46
by Martin Pool
todo |
121 |
## TODO: rename command, needed soon: check destination doesn't exist
|
122 |
## either in working copy or tree; move working copy; update
|
|
123 |
## inventory; write out
|
|
124 |
||
125 |
## TODO: move command; check destination is a directory and will not
|
|
126 |
## clash; move it.
|
|
127 |
||
128 |
## TODO: command to show renames, one per line, as to->from
|
|
129 |
||
130 |
||
|
1
by mbp at sourcefrog
import from baz patch-364 |
131 |
|
132 |
||
133 |
def cmd_status(all=False): |
|
134 |
"""Display status summary. |
|
135 |
||
136 |
For each file there is a single line giving its file state and name.
|
|
137 |
The name is that in the current revision unless it is deleted or
|
|
138 |
missing, in which case the old name is shown.
|
|
139 |
||
140 |
:todo: Don't show unchanged files unless ``--all`` is given?
|
|
141 |
"""
|
|
142 |
Branch('.').show_status(show_all=all) |
|
143 |
||
144 |
||
145 |
||
146 |
######################################################################
|
|
147 |
# examining history
|
|
148 |
def cmd_get_revision(revision_id): |
|
149 |
Branch('.').get_revision(revision_id).write_xml(sys.stdout) |
|
150 |
||
151 |
||
152 |
def cmd_get_file_text(text_id): |
|
153 |
"""Get contents of a file by hash.""" |
|
154 |
sf = Branch('.').text_store[text_id] |
|
155 |
pumpfile(sf, sys.stdout) |
|
156 |
||
157 |
||
158 |
||
159 |
######################################################################
|
|
160 |
# commands
|
|
161 |
||
162 |
||
163 |
def cmd_revno(): |
|
164 |
"""Show number of revisions on this branch""" |
|
165 |
print Branch('.').revno() |
|
166 |
||
167 |
||
168 |
def cmd_add(file_list, verbose=False): |
|
169 |
"""Add specified files. |
|
170 |
|
|
171 |
Fails if the files are already added.
|
|
172 |
"""
|
|
|
66
by mbp at sourcefrog
add command uses the path of the first named file |
173 |
assert file_list |
174 |
b = Branch(file_list[0], find_root=True) |
|
|
69
by Martin Pool
handle add, remove, file-id being given filenames that are |
175 |
b.add([b.relpath(f) for f in file_list], verbose=verbose) |
|
1
by mbp at sourcefrog
import from baz patch-364 |
176 |
|
177 |
||
|
68
by mbp at sourcefrog
- new relpath command and function |
178 |
def cmd_relpath(filename): |
179 |
print Branch(filename).relpath(filename) |
|
180 |
||
181 |
||
|
1
by mbp at sourcefrog
import from baz patch-364 |
182 |
def cmd_inventory(revision=None): |
183 |
"""Show inventory of the current working copy.""" |
|
184 |
## TODO: Also optionally show a previous inventory
|
|
185 |
## TODO: Format options
|
|
186 |
b = Branch('.') |
|
187 |
if revision == None: |
|
188 |
inv = b.read_working_inventory() |
|
189 |
else: |
|
190 |
inv = b.get_revision_inventory(b.lookup_revision(revision)) |
|
191 |
||
192 |
for path, entry in inv.iter_entries(): |
|
193 |
print '%-50s %s' % (entry.file_id, path) |
|
194 |
||
195 |
||
196 |
||
197 |
def cmd_info(): |
|
198 |
b = Branch('.') |
|
199 |
print 'branch format:', b.controlfile('branch-format', 'r').readline().rstrip('\n') |
|
|
21
by mbp at sourcefrog
- bzr info: show summary information on branch history |
200 |
|
201 |
def plural(n, base='', pl=None): |
|
202 |
if n == 1: |
|
203 |
return base |
|
204 |
elif pl is not None: |
|
205 |
return pl |
|
206 |
else: |
|
207 |
return 's' |
|
|
18
by mbp at sourcefrog
show count of versioned/unknown/ignored files |
208 |
|
209 |
count_version_dirs = 0 |
|
|
19
by mbp at sourcefrog
more information in info command |
210 |
|
211 |
count_status = {'A': 0, 'D': 0, 'M': 0, 'R': 0, '?': 0, 'I': 0, '.': 0} |
|
212 |
for st_tup in bzrlib.diff_trees(b.basis_tree(), b.working_tree()): |
|
213 |
fs = st_tup[0] |
|
214 |
count_status[fs] += 1 |
|
215 |
if fs not in ['I', '?'] and st_tup[4] == 'directory': |
|
216 |
count_version_dirs += 1 |
|
217 |
||
218 |
print
|
|
219 |
print 'in the working tree:' |
|
220 |
for name, fs in (('unchanged', '.'), |
|
221 |
('modified', 'M'), ('added', 'A'), ('removed', 'D'), |
|
222 |
('renamed', 'R'), ('unknown', '?'), ('ignored', 'I'), |
|
223 |
):
|
|
224 |
print ' %5d %s' % (count_status[fs], name) |
|
|
21
by mbp at sourcefrog
- bzr info: show summary information on branch history |
225 |
print ' %5d versioned subdirector%s' % (count_version_dirs, |
226 |
plural(count_version_dirs, 'y', 'ies')) |
|
227 |
||
228 |
print
|
|
229 |
print 'branch history:' |
|
230 |
history = b.revision_history() |
|
231 |
revno = len(history) |
|
232 |
print ' %5d revision%s' % (revno, plural(revno)) |
|
233 |
committers = Set() |
|
234 |
for rev in history: |
|
235 |
committers.add(b.get_revision(rev).committer) |
|
236 |
print ' %5d committer%s' % (len(committers), plural(len(committers))) |
|
237 |
if revno > 0: |
|
238 |
firstrev = b.get_revision(history[0]) |
|
239 |
age = int((time.time() - firstrev.timestamp) / 3600 / 24) |
|
240 |
print ' %5d day%s old' % (age, plural(age)) |
|
|
22
by mbp at sourcefrog
bzr info: show date of first and latest commit |
241 |
print ' first revision: %s' % format_date(firstrev.timestamp, |
242 |
firstrev.timezone) |
|
243 |
||
244 |
lastrev = b.get_revision(history[-1]) |
|
245 |
print ' latest revision: %s' % format_date(lastrev.timestamp, |
|
246 |
lastrev.timezone) |
|
247 |
||
|
21
by mbp at sourcefrog
- bzr info: show summary information on branch history |
248 |
|
|
1
by mbp at sourcefrog
import from baz patch-364 |
249 |
|
250 |
||
251 |
def cmd_remove(file_list, verbose=False): |
|
|
69
by Martin Pool
handle add, remove, file-id being given filenames that are |
252 |
b = Branch(file_list[0]) |
253 |
b.remove([b.relpath(f) for f in file_list], verbose=verbose) |
|
|
1
by mbp at sourcefrog
import from baz patch-364 |
254 |
|
255 |
||
256 |
||
257 |
def cmd_file_id(filename): |
|
|
69
by Martin Pool
handle add, remove, file-id being given filenames that are |
258 |
b = Branch(filename) |
259 |
i = b.inventory.path2id(b.relpath(filename)) |
|
|
1
by mbp at sourcefrog
import from baz patch-364 |
260 |
if i is None: |
261 |
bailout("%s is not a versioned file" % filename) |
|
262 |
else: |
|
263 |
print i |
|
264 |
||
265 |
||
266 |
def cmd_find_filename(fileid): |
|
267 |
n = find_filename(fileid) |
|
268 |
if n is None: |
|
269 |
bailout("%s is not a live file id" % fileid) |
|
270 |
else: |
|
271 |
print n |
|
272 |
||
273 |
||
274 |
def cmd_revision_history(): |
|
275 |
for patchid in Branch('.').revision_history(): |
|
276 |
print patchid |
|
277 |
||
278 |
||
279 |
||
280 |
def cmd_init(): |
|
281 |
# TODO: Check we're not already in a working directory? At the
|
|
282 |
# moment you'll get an ugly error.
|
|
283 |
||
284 |
# TODO: What if we're in a subdirectory of a branch? Would like
|
|
285 |
# to allow that, but then the parent may need to understand that
|
|
286 |
# the children have disappeared, or should they be versioned in
|
|
287 |
# both?
|
|
288 |
||
289 |
# TODO: Take an argument/option for branch name.
|
|
290 |
Branch('.', init=True) |
|
291 |
||
292 |
||
293 |
def cmd_diff(revision=None): |
|
294 |
"""Show diff from basis to working copy. |
|
295 |
||
296 |
:todo: Take one or two revision arguments, look up those trees,
|
|
297 |
and diff them.
|
|
298 |
||
299 |
:todo: Allow diff across branches.
|
|
300 |
||
301 |
:todo: Mangle filenames in diff to be more relevant.
|
|
302 |
||
303 |
:todo: Shouldn't be in the cmd function.
|
|
304 |
"""
|
|
305 |
||
306 |
b = Branch('.') |
|
307 |
||
308 |
if revision == None: |
|
309 |
old_tree = b.basis_tree() |
|
310 |
else: |
|
311 |
old_tree = b.revision_tree(b.lookup_revision(revision)) |
|
312 |
||
313 |
new_tree = b.working_tree() |
|
314 |
old_inv = old_tree.inventory |
|
315 |
new_inv = new_tree.inventory |
|
316 |
||
317 |
# TODO: Options to control putting on a prefix or suffix, perhaps as a format string
|
|
318 |
old_label = '' |
|
319 |
new_label = '' |
|
320 |
||
321 |
DEVNULL = '/dev/null' |
|
322 |
# Windows users, don't panic about this filename -- it is a
|
|
323 |
# special signal to GNU patch that the file should be created or
|
|
324 |
# deleted respectively.
|
|
325 |
||
326 |
# TODO: Generation of pseudo-diffs for added/deleted files could
|
|
327 |
# be usefully made into a much faster special case.
|
|
328 |
||
329 |
# TODO: Better to return them in sorted order I think.
|
|
330 |
||
331 |
for file_state, fid, old_name, new_name, kind in bzrlib.diff_trees(old_tree, new_tree): |
|
332 |
d = None |
|
333 |
||
334 |
# Don't show this by default; maybe do it if an option is passed
|
|
335 |
# idlabel = ' {%s}' % fid
|
|
336 |
idlabel = '' |
|
337 |
||
338 |
# FIXME: Something about the diff format makes patch unhappy
|
|
339 |
# with newly-added files.
|
|
340 |
||
341 |
def diffit(*a, **kw): |
|
342 |
sys.stdout.writelines(difflib.unified_diff(*a, **kw)) |
|
343 |
print
|
|
344 |
||
345 |
if file_state in ['.', '?', 'I']: |
|
346 |
continue
|
|
347 |
elif file_state == 'A': |
|
348 |
print '*** added %s %r' % (kind, new_name) |
|
349 |
if kind == 'file': |
|
350 |
diffit([], |
|
351 |
new_tree.get_file(fid).readlines(), |
|
352 |
fromfile=DEVNULL, |
|
353 |
tofile=new_label + new_name + idlabel) |
|
354 |
elif file_state == 'D': |
|
355 |
assert isinstance(old_name, types.StringTypes) |
|
356 |
print '*** deleted %s %r' % (kind, old_name) |
|
357 |
if kind == 'file': |
|
358 |
diffit(old_tree.get_file(fid).readlines(), [], |
|
359 |
fromfile=old_label + old_name + idlabel, |
|
360 |
tofile=DEVNULL) |
|
361 |
elif file_state in ['M', 'R']: |
|
362 |
if file_state == 'M': |
|
363 |
assert kind == 'file' |
|
364 |
assert old_name == new_name |
|
365 |
print '*** modified %s %r' % (kind, new_name) |
|
366 |
elif file_state == 'R': |
|
367 |
print '*** renamed %s %r => %r' % (kind, old_name, new_name) |
|
368 |
||
369 |
if kind == 'file': |
|
370 |
diffit(old_tree.get_file(fid).readlines(), |
|
371 |
new_tree.get_file(fid).readlines(), |
|
372 |
fromfile=old_label + old_name + idlabel, |
|
373 |
tofile=new_label + new_name) |
|
374 |
else: |
|
375 |
bailout("can't represent state %s {%s}" % (file_state, fid)) |
|
376 |
||
377 |
||
378 |
||
|
65
by mbp at sourcefrog
rename 'find-branch-root' command to just 'root' |
379 |
def cmd_root(filename=None): |
380 |
"""Print the branch root.""" |
|
|
62
by mbp at sourcefrog
- new find_branch_root function; based on suggestion from aaron |
381 |
print bzrlib.branch.find_branch_root(filename) |
382 |
||
383 |
||
|
13
by mbp at sourcefrog
fix up cmd_log args |
384 |
def cmd_log(timezone='original'): |
|
1
by mbp at sourcefrog
import from baz patch-364 |
385 |
"""Show log of this branch. |
386 |
||
387 |
:todo: Options for utc; to show ids; to limit range; etc.
|
|
388 |
"""
|
|
|
12
by mbp at sourcefrog
new --timezone option for bzr log |
389 |
Branch('.').write_log(show_timezone=timezone) |
|
1
by mbp at sourcefrog
import from baz patch-364 |
390 |
|
391 |
||
392 |
def cmd_ls(revision=None, verbose=False): |
|
393 |
"""List files in a tree. |
|
394 |
||
395 |
:todo: Take a revision or remote path and list that tree instead.
|
|
396 |
"""
|
|
397 |
b = Branch('.') |
|
398 |
if revision == None: |
|
399 |
tree = b.working_tree() |
|
400 |
else: |
|
401 |
tree = b.revision_tree(b.lookup_revision(revision)) |
|
402 |
||
403 |
for fp, fc, kind, fid in tree.list_files(): |
|
404 |
if verbose: |
|
405 |
if kind == 'directory': |
|
406 |
kindch = '/' |
|
407 |
elif kind == 'file': |
|
408 |
kindch = '' |
|
409 |
else: |
|
410 |
kindch = '???' |
|
411 |
||
412 |
print '%-8s %s%s' % (fc, fp, kindch) |
|
413 |
else: |
|
414 |
print fp |
|
415 |
||
416 |
||
417 |
||
418 |
def cmd_unknowns(): |
|
419 |
"""List unknown files""" |
|
420 |
for f in Branch('.').unknowns(): |
|
421 |
print quotefn(f) |
|
422 |
||
423 |
||
424 |
def cmd_lookup_revision(revno): |
|
425 |
try: |
|
426 |
revno = int(revno) |
|
427 |
except ValueError: |
|
428 |
bailout("usage: lookup-revision REVNO", |
|
429 |
["REVNO is a non-negative revision number for this branch"]) |
|
430 |
||
431 |
print Branch('.').lookup_revision(revno) or NONE_STRING |
|
432 |
||
433 |
||
434 |
||
435 |
def cmd_export(revno, dest): |
|
436 |
"""Export past revision to destination directory.""" |
|
437 |
b = Branch('.') |
|
438 |
rh = b.lookup_revision(int(revno)) |
|
439 |
t = b.revision_tree(rh) |
|
440 |
t.export(dest) |
|
441 |
||
442 |
||
443 |
||
444 |
######################################################################
|
|
445 |
# internal/test commands
|
|
446 |
||
447 |
||
448 |
def cmd_uuid(): |
|
449 |
"""Print a newly-generated UUID.""" |
|
|
63
by mbp at sourcefrog
fix up uuid command |
450 |
print bzrlib.osutils.uuid() |
|
1
by mbp at sourcefrog
import from baz patch-364 |
451 |
|
452 |
||
453 |
||
|
8
by mbp at sourcefrog
store committer's timezone in revision and show |
454 |
def cmd_local_time_offset(): |
455 |
print bzrlib.osutils.local_time_offset() |
|
456 |
||
457 |
||
458 |
||
|
57
by mbp at sourcefrog
error if --message is not given for commit |
459 |
def cmd_commit(message=None, verbose=False): |
460 |
if not message: |
|
461 |
bailout("please specify a commit message") |
|
|
1
by mbp at sourcefrog
import from baz patch-364 |
462 |
Branch('.').commit(message, verbose=verbose) |
463 |
||
464 |
||
465 |
def cmd_check(): |
|
466 |
"""Check consistency of the branch.""" |
|
467 |
check() |
|
468 |
||
469 |
||
470 |
def cmd_is(pred, *rest): |
|
471 |
"""Test whether PREDICATE is true.""" |
|
472 |
try: |
|
473 |
cmd_handler = globals()['assert_' + pred.replace('-', '_')] |
|
474 |
except KeyError: |
|
475 |
bailout("unknown predicate: %s" % quotefn(pred)) |
|
476 |
||
477 |
try: |
|
478 |
cmd_handler(*rest) |
|
479 |
except BzrCheckError: |
|
480 |
# by default we don't print the message so that this can
|
|
481 |
# be used from shell scripts without producing noise
|
|
482 |
sys.exit(1) |
|
483 |
||
484 |
||
485 |
def cmd_username(): |
|
486 |
print bzrlib.osutils.username() |
|
487 |
||
488 |
||
489 |
def cmd_user_email(): |
|
490 |
print bzrlib.osutils.user_email() |
|
491 |
||
492 |
||
493 |
def cmd_gen_revision_id(): |
|
494 |
import time |
|
495 |
print bzrlib.branch._gen_revision_id(time.time()) |
|
496 |
||
497 |
||
|
55
by mbp at sourcefrog
bzr selftest shows some counts of tests |
498 |
def cmd_selftest(verbose=False): |
499 |
"""Run internal test suite""" |
|
|
1
by mbp at sourcefrog
import from baz patch-364 |
500 |
## -v, if present, is seen by doctest; the argument is just here
|
501 |
## so our parser doesn't complain
|
|
502 |
||
503 |
## TODO: --verbose option
|
|
|
55
by mbp at sourcefrog
bzr selftest shows some counts of tests |
504 |
|
505 |
failures, tests = 0, 0 |
|
|
1
by mbp at sourcefrog
import from baz patch-364 |
506 |
|
|
55
by mbp at sourcefrog
bzr selftest shows some counts of tests |
507 |
import doctest, bzrlib.store, bzrlib.tests |
|
1
by mbp at sourcefrog
import from baz patch-364 |
508 |
bzrlib.trace.verbose = False |
|
55
by mbp at sourcefrog
bzr selftest shows some counts of tests |
509 |
|
510 |
for m in bzrlib.store, bzrlib.inventory, bzrlib.branch, bzrlib.osutils, \ |
|
|
58
by mbp at sourcefrog
include bzrlib.commands in selftest |
511 |
bzrlib.tree, bzrlib.tests, bzrlib.commands: |
|
55
by mbp at sourcefrog
bzr selftest shows some counts of tests |
512 |
mf, mt = doctest.testmod(m) |
513 |
failures += mf |
|
514 |
tests += mt |
|
515 |
print '%-40s %3d tests' % (m.__name__, mt), |
|
516 |
if mf: |
|
517 |
print '%3d FAILED!' % mf |
|
518 |
else: |
|
519 |
print
|
|
520 |
||
521 |
print '%-40s %3d tests' % ('total', tests), |
|
522 |
if failures: |
|
523 |
print '%3d FAILED!' % failures |
|
524 |
else: |
|
525 |
print
|
|
526 |
||
527 |
||
528 |
||
529 |
# deprecated
|
|
530 |
cmd_doctest = cmd_selftest |
|
|
53
by mbp at sourcefrog
'selftest' command instead of 'doctest' |
531 |
|
532 |
||
|
1
by mbp at sourcefrog
import from baz patch-364 |
533 |
######################################################################
|
534 |
# help
|
|
535 |
||
536 |
||
537 |
def cmd_help(): |
|
538 |
# TODO: Specific help for particular commands
|
|
539 |
print __doc__ |
|
540 |
||
541 |
||
542 |
def cmd_version(): |
|
543 |
print "bzr (bazaar-ng) %s" % __version__ |
|
544 |
print __copyright__ |
|
545 |
print "http://bazaar-ng.org/" |
|
546 |
print
|
|
547 |
print \
|
|
548 |
"""bzr comes with ABSOLUTELY NO WARRANTY. bzr is free software, and
|
|
549 |
you may use, modify and redistribute it under the terms of the GNU
|
|
550 |
General Public License version 2 or later."""
|
|
551 |
||
552 |
||
553 |
def cmd_rocks(): |
|
554 |
"""Statement of optimism.""" |
|
555 |
print "it sure does!" |
|
556 |
||
557 |
||
558 |
||
559 |
######################################################################
|
|
560 |
# main routine
|
|
561 |
||
562 |
||
563 |
# list of all available options; the rhs can be either None for an
|
|
564 |
# option that takes no argument, or a constructor function that checks
|
|
565 |
# the type.
|
|
566 |
OPTIONS = { |
|
567 |
'all': None, |
|
568 |
'help': None, |
|
569 |
'message': unicode, |
|
570 |
'revision': int, |
|
571 |
'show-ids': None, |
|
|
12
by mbp at sourcefrog
new --timezone option for bzr log |
572 |
'timezone': str, |
|
1
by mbp at sourcefrog
import from baz patch-364 |
573 |
'verbose': None, |
574 |
'version': None, |
|
575 |
}
|
|
576 |
||
577 |
SHORT_OPTIONS = { |
|
578 |
'm': 'message', |
|
579 |
'r': 'revision', |
|
580 |
'v': 'verbose', |
|
581 |
}
|
|
582 |
||
583 |
# List of options that apply to particular commands; commands not
|
|
584 |
# listed take none.
|
|
585 |
cmd_options = { |
|
586 |
'add': ['verbose'], |
|
587 |
'commit': ['message', 'verbose'], |
|
588 |
'diff': ['revision'], |
|
589 |
'inventory': ['revision'], |
|
|
12
by mbp at sourcefrog
new --timezone option for bzr log |
590 |
'log': ['show-ids', 'timezone'], |
|
1
by mbp at sourcefrog
import from baz patch-364 |
591 |
'ls': ['revision', 'verbose'], |
|
12
by mbp at sourcefrog
new --timezone option for bzr log |
592 |
'remove': ['verbose'], |
|
1
by mbp at sourcefrog
import from baz patch-364 |
593 |
'status': ['all'], |
594 |
}
|
|
595 |
||
596 |
||
597 |
cmd_args = { |
|
598 |
'init': [], |
|
599 |
'add': ['file+'], |
|
600 |
'commit': [], |
|
601 |
'diff': [], |
|
602 |
'file-id': ['filename'], |
|
|
65
by mbp at sourcefrog
rename 'find-branch-root' command to just 'root' |
603 |
'root': ['filename?'], |
|
68
by mbp at sourcefrog
- new relpath command and function |
604 |
'relpath': ['filename'], |
|
1
by mbp at sourcefrog
import from baz patch-364 |
605 |
'get-file-text': ['text_id'], |
606 |
'get-inventory': ['inventory_id'], |
|
607 |
'get-revision': ['revision_id'], |
|
608 |
'get-revision-inventory': ['revision_id'], |
|
609 |
'log': [], |
|
610 |
'lookup-revision': ['revno'], |
|
611 |
'export': ['revno', 'dest'], |
|
612 |
'remove': ['file+'], |
|
613 |
'status': [], |
|
614 |
}
|
|
615 |
||
616 |
||
617 |
def parse_args(argv): |
|
618 |
"""Parse command line. |
|
619 |
|
|
620 |
Arguments and options are parsed at this level before being passed
|
|
621 |
down to specific command handlers. This routine knows, from a
|
|
622 |
lookup table, something about the available options, what optargs
|
|
623 |
they take, and which commands will accept them.
|
|
624 |
||
|
31
by Martin Pool
fix up parse_args doctest |
625 |
>>> parse_args('--help'.split())
|
|
1
by mbp at sourcefrog
import from baz patch-364 |
626 |
([], {'help': True})
|
|
31
by Martin Pool
fix up parse_args doctest |
627 |
>>> parse_args('--version'.split())
|
|
1
by mbp at sourcefrog
import from baz patch-364 |
628 |
([], {'version': True})
|
|
31
by Martin Pool
fix up parse_args doctest |
629 |
>>> parse_args('status --all'.split())
|
|
1
by mbp at sourcefrog
import from baz patch-364 |
630 |
(['status'], {'all': True})
|
|
31
by Martin Pool
fix up parse_args doctest |
631 |
>>> parse_args('commit --message=biter'.split())
|
|
17
by mbp at sourcefrog
allow --option=ARG syntax |
632 |
(['commit'], {'message': u'biter'})
|
|
1
by mbp at sourcefrog
import from baz patch-364 |
633 |
"""
|
634 |
args = [] |
|
635 |
opts = {} |
|
636 |
||
637 |
# TODO: Maybe handle '--' to end options?
|
|
638 |
||
|
26
by mbp at sourcefrog
fix StopIteration error on python2.3(?) |
639 |
while argv: |
640 |
a = argv.pop(0) |
|
|
1
by mbp at sourcefrog
import from baz patch-364 |
641 |
if a[0] == '-': |
|
17
by mbp at sourcefrog
allow --option=ARG syntax |
642 |
optarg = None |
|
1
by mbp at sourcefrog
import from baz patch-364 |
643 |
if a[1] == '-': |
644 |
mutter(" got option %r" % a) |
|
|
17
by mbp at sourcefrog
allow --option=ARG syntax |
645 |
if '=' in a: |
646 |
optname, optarg = a[2:].split('=', 1) |
|
647 |
else: |
|
648 |
optname = a[2:] |
|
|
1
by mbp at sourcefrog
import from baz patch-364 |
649 |
if optname not in OPTIONS: |
650 |
bailout('unknown long option %r' % a) |
|
651 |
else: |
|
652 |
shortopt = a[1:] |
|
653 |
if shortopt not in SHORT_OPTIONS: |
|
654 |
bailout('unknown short option %r' % a) |
|
655 |
optname = SHORT_OPTIONS[shortopt] |
|
656 |
||
657 |
if optname in opts: |
|
658 |
# XXX: Do we ever want to support this, e.g. for -r?
|
|
659 |
bailout('repeated option %r' % a) |
|
|
17
by mbp at sourcefrog
allow --option=ARG syntax |
660 |
|
|
1
by mbp at sourcefrog
import from baz patch-364 |
661 |
optargfn = OPTIONS[optname] |
662 |
if optargfn: |
|
|
17
by mbp at sourcefrog
allow --option=ARG syntax |
663 |
if optarg == None: |
|
26
by mbp at sourcefrog
fix StopIteration error on python2.3(?) |
664 |
if not argv: |
|
17
by mbp at sourcefrog
allow --option=ARG syntax |
665 |
bailout('option %r needs an argument' % a) |
666 |
else: |
|
|
26
by mbp at sourcefrog
fix StopIteration error on python2.3(?) |
667 |
optarg = argv.pop(0) |
|
17
by mbp at sourcefrog
allow --option=ARG syntax |
668 |
opts[optname] = optargfn(optarg) |
|
1
by mbp at sourcefrog
import from baz patch-364 |
669 |
mutter(" option argument %r" % opts[optname]) |
670 |
else: |
|
|
17
by mbp at sourcefrog
allow --option=ARG syntax |
671 |
if optarg != None: |
672 |
bailout('option %r takes no argument' % optname) |
|
|
1
by mbp at sourcefrog
import from baz patch-364 |
673 |
opts[optname] = True |
674 |
else: |
|
675 |
args.append(a) |
|
676 |
||
677 |
return args, opts |
|
678 |
||
679 |
||
680 |
||
681 |
def _match_args(cmd, args): |
|
682 |
"""Check non-option arguments match required pattern. |
|
683 |
||
684 |
>>> _match_args('status', ['asdasdsadasd'])
|
|
685 |
Traceback (most recent call last):
|
|
686 |
...
|
|
687 |
BzrError: ("extra arguments to command status: ['asdasdsadasd']", [])
|
|
688 |
>>> _match_args('add', ['asdasdsadasd'])
|
|
689 |
{'file_list': ['asdasdsadasd']}
|
|
690 |
>>> _match_args('add', 'abc def gj'.split())
|
|
691 |
{'file_list': ['abc', 'def', 'gj']}
|
|
692 |
"""
|
|
693 |
# match argument pattern
|
|
694 |
argform = cmd_args.get(cmd, []) |
|
695 |
argdict = {} |
|
696 |
# TODO: Need a way to express 'cp SRC... DEST', where it matches
|
|
697 |
# all but one.
|
|
|
26
by mbp at sourcefrog
fix StopIteration error on python2.3(?) |
698 |
|
|
62
by mbp at sourcefrog
- new find_branch_root function; based on suggestion from aaron |
699 |
# step through args and argform, allowing appropriate 0-many matches
|
|
1
by mbp at sourcefrog
import from baz patch-364 |
700 |
for ap in argform: |
701 |
argname = ap[:-1] |
|
702 |
if ap[-1] == '?': |
|
|
62
by mbp at sourcefrog
- new find_branch_root function; based on suggestion from aaron |
703 |
if args: |
704 |
argdict[argname] = args.pop(0) |
|
|
1
by mbp at sourcefrog
import from baz patch-364 |
705 |
elif ap[-1] == '*': |
706 |
assert 0 |
|
707 |
elif ap[-1] == '+': |
|
708 |
if not args: |
|
709 |
bailout("command %r needs one or more %s" |
|
710 |
% (cmd, argname.upper())) |
|
711 |
else: |
|
712 |
argdict[argname + '_list'] = args[:] |
|
713 |
args = [] |
|
714 |
else: |
|
715 |
# just a plain arg
|
|
716 |
argname = ap |
|
717 |
if not args: |
|
718 |
bailout("command %r requires argument %s" |
|
719 |
% (cmd, argname.upper())) |
|
720 |
else: |
|
721 |
argdict[argname] = args.pop(0) |
|
722 |
||
723 |
if args: |
|
724 |
bailout("extra arguments to command %s: %r" |
|
725 |
% (cmd, args)) |
|
726 |
||
727 |
return argdict |
|
728 |
||
729 |
||
730 |
||
731 |
def run_bzr(argv): |
|
732 |
"""Execute a command. |
|
733 |
||
734 |
This is similar to main(), but without all the trappings for
|
|
735 |
logging and error handling.
|
|
736 |
"""
|
|
737 |
try: |
|
|
26
by mbp at sourcefrog
fix StopIteration error on python2.3(?) |
738 |
args, opts = parse_args(argv[1:]) |
|
1
by mbp at sourcefrog
import from baz patch-364 |
739 |
if 'help' in opts: |
740 |
# TODO: pass down other arguments in case they asked for
|
|
741 |
# help on a command name?
|
|
742 |
cmd_help() |
|
743 |
return 0 |
|
744 |
elif 'version' in opts: |
|
745 |
cmd_version() |
|
746 |
return 0 |
|
747 |
cmd = args.pop(0) |
|
748 |
except IndexError: |
|
749 |
log_error('usage: bzr COMMAND\n') |
|
750 |
log_error(' try "bzr help"\n') |
|
751 |
return 1 |
|
752 |
||
753 |
try: |
|
754 |
cmd_handler = globals()['cmd_' + cmd.replace('-', '_')] |
|
755 |
except KeyError: |
|
756 |
bailout("unknown command " + `cmd`) |
|
757 |
||
758 |
# TODO: special --profile option to turn on the Python profiler
|
|
759 |
||
760 |
# check options are reasonable
|
|
761 |
allowed = cmd_options.get(cmd, []) |
|
762 |
for oname in opts: |
|
763 |
if oname not in allowed: |
|
764 |
bailout("option %r is not allowed for command %r" |
|
765 |
% (oname, cmd)) |
|
766 |
||
767 |
cmdargs = _match_args(cmd, args) |
|
768 |
cmdargs.update(opts) |
|
769 |
||
770 |
ret = cmd_handler(**cmdargs) or 0 |
|
771 |
||
772 |
||
773 |
||
774 |
def main(argv): |
|
775 |
## TODO: Handle command-line options; probably know what options are valid for
|
|
776 |
## each command
|
|
777 |
||
778 |
## TODO: If the arguments are wrong, give a usage message rather
|
|
779 |
## than just a backtrace.
|
|
780 |
||
|
59
by mbp at sourcefrog
lift out tracefile creation code |
781 |
bzrlib.trace.create_tracefile(argv) |
782 |
||
|
1
by mbp at sourcefrog
import from baz patch-364 |
783 |
try: |
784 |
ret = run_bzr(argv) |
|
785 |
return ret |
|
786 |
except BzrError, e: |
|
787 |
log_error('bzr: error: ' + e.args[0] + '\n') |
|
788 |
if len(e.args) > 1: |
|
789 |
for h in e.args[1]: |
|
790 |
log_error(' ' + h + '\n') |
|
791 |
return 1 |
|
792 |
except Exception, e: |
|
793 |
log_error('bzr: exception: %s\n' % e) |
|
794 |
log_error(' see .bzr.log for details\n') |
|
795 |
traceback.print_exc(None, bzrlib.trace._tracefile) |
|
796 |
traceback.print_exc(None, sys.stderr) |
|
797 |
return 1 |
|
798 |
||
799 |
# TODO: Maybe nicer handling of IOError?
|
|
800 |
||
801 |
||
802 |
||
803 |
if __name__ == '__main__': |
|
804 |
sys.exit(main(sys.argv)) |
|
805 |
##import profile
|
|
806 |
##profile.run('main(sys.argv)')
|
|
807 |