~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/commands.py

  • Committer: mbp at sourcefrog
  • Date: 2005-03-23 06:25:55 UTC
  • Revision ID: mbp@sourcefrog.net-20050323062555-5489339018d0c043
- import a subset of elementtree for easier installation

Show diffs side-by-side

added added

removed removed

Lines of Context:
63
63
"""
64
64
 
65
65
# not currently working:
66
 
#  bzr check
67
 
#       Run internal consistency checks.
68
66
#  bzr info
69
67
#       Show some information about this branch.
70
68
 
115
113
## TODO: Perhaps make UUIDs predictable in test mode to make it easier
116
114
## to compare output?
117
115
 
118
 
 
119
 
 
120
 
 
121
 
######################################################################
122
 
# check status
 
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
 
 
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
 
123
131
 
124
132
 
125
133
def cmd_status(all=False):
141
149
    Branch('.').get_revision(revision_id).write_xml(sys.stdout)
142
150
 
143
151
 
144
 
def cmd_get_inventory(inventory_id):
145
 
    """Return inventory in XML by hash"""
146
 
    Branch('.').get_inventory(inventory_hash).write_xml(sys.stdout)
147
 
 
148
 
 
149
 
def cmd_get_revision_inventory(revision_id):
150
 
    """Output inventory for a revision."""
151
 
    b = Branch('.')
152
 
    b.get_revision_inventory(revision_id).write_xml(sys.stdout)
153
 
 
154
 
 
155
152
def cmd_get_file_text(text_id):
156
153
    """Get contents of a file by hash."""
157
154
    sf = Branch('.').text_store[text_id]
168
165
    print Branch('.').revno()
169
166
    
170
167
 
 
168
    
171
169
def cmd_add(file_list, verbose=False):
172
 
    """Add specified files.
 
170
    """Add specified files or directories.
 
171
 
 
172
    In non-recursive mode, all the named items are added, regardless
 
173
    of whether they were previously ignored.  A warning is given if
 
174
    any of the named files are already versioned.
 
175
 
 
176
    In recursive mode (the default), files are treated the same way
 
177
    but the behaviour for directories is different.  Directories that
 
178
    are already versioned do not give a warning.  All directories,
 
179
    whether already versioned or not, are searched for files or
 
180
    subdirectories that are neither versioned or ignored, and these
 
181
    are added.  This search proceeds recursively into versioned
 
182
    directories.
 
183
 
 
184
    Therefore simply saying 'bzr add .' will version all files that
 
185
    are currently unknown.
 
186
    """
 
187
    if True:
 
188
        bzrlib.add.smart_add(file_list, verbose)
 
189
    else:
 
190
        # old way
 
191
        assert file_list
 
192
        b = Branch(file_list[0], find_root=True)
 
193
        b.add([b.relpath(f) for f in file_list], verbose=verbose)
 
194
 
173
195
    
174
 
    Fails if the files are already added.
175
 
    """
176
 
    Branch('.').add(file_list, verbose=verbose)
 
196
 
 
197
def cmd_relpath(filename):
 
198
    print Branch(filename).relpath(filename)
177
199
 
178
200
 
179
201
def cmd_inventory(revision=None):
194
216
def cmd_info():
195
217
    b = Branch('.')
196
218
    print 'branch format:', b.controlfile('branch-format', 'r').readline().rstrip('\n')
197
 
    print 'revision number:', b.revno()
 
219
 
 
220
    def plural(n, base='', pl=None):
 
221
        if n == 1:
 
222
            return base
 
223
        elif pl is not None:
 
224
            return pl
 
225
        else:
 
226
            return 's'
198
227
 
199
228
    count_version_dirs = 0
200
229
 
212
241
                     ('renamed', 'R'), ('unknown', '?'), ('ignored', 'I'),
213
242
                     ):
214
243
        print '  %5d %s' % (count_status[fs], name)
215
 
    print '  %5d versioned subdirectories' % count_version_dirs
216
 
            
 
244
    print '  %5d versioned subdirector%s' % (count_version_dirs,
 
245
                                             plural(count_version_dirs, 'y', 'ies'))
 
246
 
 
247
    print
 
248
    print 'branch history:'
 
249
    history = b.revision_history()
 
250
    revno = len(history)
 
251
    print '  %5d revision%s' % (revno, plural(revno))
 
252
    committers = Set()
 
253
    for rev in history:
 
254
        committers.add(b.get_revision(rev).committer)
 
255
    print '  %5d committer%s' % (len(committers), plural(len(committers)))
 
256
    if revno > 0:
 
257
        firstrev = b.get_revision(history[0])
 
258
        age = int((time.time() - firstrev.timestamp) / 3600 / 24)
 
259
        print '  %5d day%s old' % (age, plural(age))
 
260
        print '  first revision: %s' % format_date(firstrev.timestamp,
 
261
                                                 firstrev.timezone)
 
262
 
 
263
        lastrev = b.get_revision(history[-1])
 
264
        print '  latest revision: %s' % format_date(lastrev.timestamp,
 
265
                                                    lastrev.timezone)
 
266
        
 
267
    
217
268
 
218
269
 
219
270
def cmd_remove(file_list, verbose=False):
220
 
    Branch('.').remove(file_list, verbose=verbose)
 
271
    b = Branch(file_list[0])
 
272
    b.remove([b.relpath(f) for f in file_list], verbose=verbose)
221
273
 
222
274
 
223
275
 
224
276
def cmd_file_id(filename):
225
 
    i = Branch('.').read_working_inventory().path2id(filename)
 
277
    b = Branch(filename)
 
278
    i = b.inventory.path2id(b.relpath(filename))
226
279
    if i is None:
227
280
        bailout("%s is not a versioned file" % filename)
228
281
    else:
342
395
 
343
396
 
344
397
 
 
398
def cmd_root(filename=None):
 
399
    """Print the branch root."""
 
400
    print bzrlib.branch.find_branch_root(filename)
 
401
    
 
402
 
345
403
def cmd_log(timezone='original'):
346
404
    """Show log of this branch.
347
405
 
408
466
 
409
467
def cmd_uuid():
410
468
    """Print a newly-generated UUID."""
411
 
    print uuid()
 
469
    print bzrlib.osutils.uuid()
412
470
 
413
471
 
414
472
 
417
475
 
418
476
 
419
477
 
420
 
def cmd_commit(message, verbose=False):
 
478
def cmd_commit(message=None, verbose=False):
 
479
    if not message:
 
480
        bailout("please specify a commit message")
421
481
    Branch('.').commit(message, verbose=verbose)
422
482
 
423
483
 
454
514
    print bzrlib.branch._gen_revision_id(time.time())
455
515
 
456
516
 
457
 
def cmd_doctest():
458
 
    """Run internal doctest suite"""
 
517
def cmd_selftest(verbose=False):
 
518
    """Run internal test suite"""
459
519
    ## -v, if present, is seen by doctest; the argument is just here
460
520
    ## so our parser doesn't complain
461
521
 
462
522
    ## TODO: --verbose option
 
523
 
 
524
    failures, tests = 0, 0
463
525
    
464
 
    import bzr, doctest, bzrlib.store
 
526
    import doctest, bzrlib.store, bzrlib.tests
465
527
    bzrlib.trace.verbose = False
466
 
    doctest.testmod(bzr)
467
 
    doctest.testmod(bzrlib.store)
468
 
    doctest.testmod(bzrlib.inventory)
469
 
    doctest.testmod(bzrlib.branch)
470
 
    doctest.testmod(bzrlib.osutils)
471
 
    doctest.testmod(bzrlib.tree)
472
 
 
473
 
    # more strenuous tests;
474
 
    import bzrlib.tests
475
 
    doctest.testmod(bzrlib.tests)
 
528
 
 
529
    for m in bzrlib.store, bzrlib.inventory, bzrlib.branch, bzrlib.osutils, \
 
530
        bzrlib.tree, bzrlib.tests, bzrlib.commands, bzrlib.add:
 
531
        mf, mt = doctest.testmod(m)
 
532
        failures += mf
 
533
        tests += mt
 
534
        print '%-40s %3d tests' % (m.__name__, mt),
 
535
        if mf:
 
536
            print '%3d FAILED!' % mf
 
537
        else:
 
538
            print
 
539
 
 
540
    print '%-40s %3d tests' % ('total', tests),
 
541
    if failures:
 
542
        print '%3d FAILED!' % failures
 
543
    else:
 
544
        print
 
545
 
 
546
 
 
547
 
 
548
# deprecated
 
549
cmd_doctest = cmd_selftest
476
550
 
477
551
 
478
552
######################################################################
545
619
    'commit':                 [],
546
620
    'diff':                   [],
547
621
    'file-id':                ['filename'],
 
622
    'root':                   ['filename?'],
 
623
    'relpath':                ['filename'],
548
624
    'get-file-text':          ['text_id'],
549
625
    'get-inventory':          ['inventory_id'],
550
626
    'get-revision':           ['revision_id'],
565
641
    lookup table, something about the available options, what optargs
566
642
    they take, and which commands will accept them.
567
643
 
568
 
    >>> parse_args('bzr --help'.split())
 
644
    >>> parse_args('--help'.split())
569
645
    ([], {'help': True})
570
 
    >>> parse_args('bzr --version'.split())
 
646
    >>> parse_args('--version'.split())
571
647
    ([], {'version': True})
572
 
    >>> parse_args('bzr status --all'.split())
 
648
    >>> parse_args('status --all'.split())
573
649
    (['status'], {'all': True})
574
 
    >>> parse_args('bzr commit --message=biter'.split())
 
650
    >>> parse_args('commit --message=biter'.split())
575
651
    (['commit'], {'message': u'biter'})
576
652
    """
577
653
    args = []
579
655
 
580
656
    # TODO: Maybe handle '--' to end options?
581
657
 
582
 
    it = iter(argv[1:])
583
 
    while it:
584
 
        a = it.next()
 
658
    while argv:
 
659
        a = argv.pop(0)
585
660
        if a[0] == '-':
586
661
            optarg = None
587
662
            if a[1] == '-':
605
680
            optargfn = OPTIONS[optname]
606
681
            if optargfn:
607
682
                if optarg == None:
608
 
                    if not it:
 
683
                    if not argv:
609
684
                        bailout('option %r needs an argument' % a)
610
685
                    else:
611
 
                        optarg = it.next()
 
686
                        optarg = argv.pop(0)
612
687
                opts[optname] = optargfn(optarg)
613
688
                mutter("    option argument %r" % opts[optname])
614
689
            else:
639
714
    argdict = {}
640
715
    # TODO: Need a way to express 'cp SRC... DEST', where it matches
641
716
    # all but one.
 
717
 
 
718
    # step through args and argform, allowing appropriate 0-many matches
642
719
    for ap in argform:
643
720
        argname = ap[:-1]
644
721
        if ap[-1] == '?':
645
 
            assert 0
 
722
            if args:
 
723
                argdict[argname] = args.pop(0)
646
724
        elif ap[-1] == '*':
647
725
            assert 0
648
726
        elif ap[-1] == '+':
676
754
    logging and error handling.
677
755
    """
678
756
    try:
679
 
        args, opts = parse_args(argv)
 
757
        args, opts = parse_args(argv[1:])
680
758
        if 'help' in opts:
681
759
            # TODO: pass down other arguments in case they asked for
682
760
            # help on a command name?
719
797
    ## TODO: If the arguments are wrong, give a usage message rather
720
798
    ## than just a backtrace.
721
799
 
 
800
    bzrlib.trace.create_tracefile(argv)
 
801
    
722
802
    try:
723
 
        t = bzrlib.trace._tracefile
724
 
        t.write('-' * 60 + '\n')
725
 
        t.write('bzr invoked at %s\n' % format_date(time.time()))
726
 
        t.write('  by %s on %s\n' % (bzrlib.osutils.username(), socket.gethostname()))
727
 
        t.write('  arguments: %r\n' % argv)
728
 
 
729
 
        starttime = os.times()[4]
730
 
 
731
 
        import platform
732
 
        t.write('  platform: %s\n' % platform.platform())
733
 
        t.write('  python: %s\n' % platform.python_version())
734
 
 
735
803
        ret = run_bzr(argv)
736
 
        
737
 
        times = os.times()
738
 
        mutter("finished, %.3fu/%.3fs cpu, %.3fu/%.3fs cum"
739
 
               % times[:4])
740
 
        mutter("    %.3f elapsed" % (times[4] - starttime))
741
 
 
742
804
        return ret
743
805
    except BzrError, e:
744
806
        log_error('bzr: error: ' + e.args[0] + '\n')