~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/commands.py

  • Committer: mbp at sourcefrog
  • Date: 2005-04-04 13:10:26 UTC
  • Revision ID: mbp@sourcefrog.net-20050404131026-628553cc03687658
new 'renames' command

Show diffs side-by-side

added added

removed removed

Lines of Context:
45
45
       Make files versioned.
46
46
  bzr log
47
47
       Show revision history.
48
 
  bzr diff [FILE...]
 
48
  bzr diff
49
49
       Show changes from last revision to working copy.
50
50
  bzr commit -m 'MESSAGE'
51
51
       Store current state as new revision.
62
62
 
63
63
 
64
64
 
65
 
import sys, os, time, types, shutil, tempfile, traceback, fnmatch, difflib, os.path
 
65
import sys, os, random, time, sha, sets, types, re, shutil, tempfile
 
66
import traceback, socket, fnmatch, difflib
 
67
from os import path
66
68
from sets import Set
67
69
from pprint import pprint
68
70
from stat import *
69
71
from glob import glob
70
 
from inspect import getdoc
71
72
 
72
73
import bzrlib
73
74
from bzrlib.store import ImmutableStore
74
75
from bzrlib.trace import mutter, note, log_error
75
 
from bzrlib.errors import bailout, BzrError, BzrCheckError
 
76
from bzrlib.errors import bailout, BzrError
76
77
from bzrlib.osutils import quotefn, pumpfile, isdir, isfile
77
78
from bzrlib.tree import RevisionTree, EmptyTree, WorkingTree, Tree
78
79
from bzrlib.revision import Revision
126
127
 
127
128
    :todo: Don't show unchanged files unless ``--all`` is given?
128
129
    """
129
 
    #import bzrlib.status
130
 
    #bzrlib.status.tree_status(Branch('.'))
131
130
    Branch('.').show_status(show_all=all)
132
131
 
133
132
 
197
196
 
198
197
 
199
198
 
200
 
# TODO: Maybe a 'mv' command that has the combined move/rename
201
 
# special behaviour of Unix?
202
 
 
203
 
def cmd_move(source_list, dest):
204
 
    b = Branch('.')
205
 
 
206
 
    b.move([b.relpath(s) for s in source_list], b.relpath(dest))
207
 
 
208
 
 
209
 
 
210
 
def cmd_rename(from_name, to_name):
211
 
    """Change the name of an entry.
212
 
 
213
 
    usage: bzr rename FROM_NAME TO_NAME
214
 
 
215
 
    examples:
216
 
      bzr rename frob.c frobber.c
217
 
      bzr rename src/frob.c lib/frob.c
218
 
 
219
 
    It is an error if the destination name exists.
220
 
 
221
 
    See also the 'move' command, which moves files into a different
222
 
    directory without changing their name.
223
 
 
224
 
    TODO: Some way to rename multiple files without invoking bzr for each
225
 
    one?"""
226
 
    b = Branch('.')
227
 
    b.rename_one(b.relpath(from_name), b.relpath(to_name))
228
 
    
 
199
def cmd_mv(source_list, dest):
 
200
    b = Branch('.')
 
201
 
 
202
    b.rename([b.relpath(s) for s in source_list], b.relpath(dest))
229
203
 
230
204
 
231
205
 
232
206
def cmd_renames(dir='.'):
233
207
    """Show list of renamed files.
234
208
 
235
 
    usage: bzr renames [BRANCH]
236
 
 
237
 
    TODO: Option to show renames between two historical versions.
238
 
 
239
 
    TODO: Only show renames under dir, rather than in the whole branch.
240
 
    """
 
209
usage: bzr renames [BRANCH]
 
210
 
 
211
TODO: Option to show renames between two historical versions.
 
212
 
 
213
TODO: Only show renames under dir, rather than in the whole branch.
 
214
"""
241
215
    b = Branch(dir)
242
216
    old_inv = b.basis_tree().inventory
243
217
    new_inv = b.read_working_inventory()
252
226
def cmd_info():
253
227
    """info: Show statistical information for this branch
254
228
 
255
 
    usage: bzr info"""
 
229
usage: bzr info"""
256
230
    import info
257
231
    info.show_info(Branch('.'))        
258
232
    
265
239
 
266
240
 
267
241
def cmd_file_id(filename):
268
 
    """Print file_id of a particular file or directory.
269
 
 
270
 
    usage: bzr file-id FILE
271
 
 
272
 
    The file_id is assigned when the file is first added and remains the
273
 
    same through all revisions where the file exists, even when it is
274
 
    moved or renamed.
275
 
    """
276
242
    b = Branch(filename)
277
243
    i = b.inventory.path2id(b.relpath(filename))
278
 
    if i == None:
279
 
        bailout("%r is not a versioned file" % filename)
 
244
    if i is None:
 
245
        bailout("%s is not a versioned file" % filename)
280
246
    else:
281
247
        print i
282
248
 
283
249
 
284
 
def cmd_file_id_path(filename):
285
 
    """Print path of file_ids to a file or directory.
286
 
 
287
 
    usage: bzr file-id-path FILE
288
 
 
289
 
    This prints one line for each directory down to the target,
290
 
    starting at the branch root."""
291
 
    b = Branch(filename)
292
 
    inv = b.inventory
293
 
    fid = inv.path2id(b.relpath(filename))
294
 
    if fid == None:
295
 
        bailout("%r is not a versioned file" % filename)
296
 
    for fip in inv.get_idpath(fid):
297
 
        print fip
 
250
def cmd_find_filename(fileid):
 
251
    n = find_filename(fileid)
 
252
    if n is None:
 
253
        bailout("%s is not a live file id" % fileid)
 
254
    else:
 
255
        print n
298
256
 
299
257
 
300
258
def cmd_revision_history():
328
286
    Branch('.', init=True)
329
287
 
330
288
 
331
 
def cmd_diff(revision=None, file_list=None):
 
289
def cmd_diff(revision=None):
332
290
    """bzr diff: Show differences in working tree.
333
291
    
334
 
    usage: bzr diff [-r REV] [FILE...]
335
 
 
336
 
    --revision REV
337
 
         Show changes since REV, rather than predecessor.
338
 
 
339
 
    If files are listed, only the changes in those files are listed.
340
 
    Otherwise, all changes for the tree are listed.
341
 
 
342
 
    TODO: Given two revision arguments, show the difference between them.
343
 
 
344
 
    TODO: Allow diff across branches.
345
 
 
346
 
    TODO: Option to use external diff command; could be GNU diff, wdiff,
347
 
          or a graphical diff.
348
 
 
349
 
    TODO: If a directory is given, diff everything under that.
350
 
 
351
 
    TODO: Selected-file diff is inefficient and doesn't show you deleted files.
 
292
usage: bzr diff [-r REV]
 
293
 
 
294
--revision REV
 
295
    Show changes since REV, rather than predecessor.
 
296
 
 
297
TODO: Given two revision arguments, show the difference between them.
 
298
 
 
299
TODO: Allow diff across branches.
 
300
 
 
301
TODO: Option to use external diff command; could be GNU diff, wdiff,
 
302
or a graphical diff.
 
303
 
 
304
TODO: Diff selected files.
352
305
"""
353
306
 
354
307
    ## TODO: Shouldn't be in the cmd function.
361
314
        old_tree = b.revision_tree(b.lookup_revision(revision))
362
315
        
363
316
    new_tree = b.working_tree()
 
317
    old_inv = old_tree.inventory
 
318
    new_inv = new_tree.inventory
364
319
 
365
320
    # TODO: Options to control putting on a prefix or suffix, perhaps as a format string
366
321
    old_label = ''
375
330
    # be usefully made into a much faster special case.
376
331
 
377
332
    # TODO: Better to return them in sorted order I think.
378
 
 
379
 
    # FIXME: If given a file list, compare only those files rather
380
 
    # than comparing everything and then throwing stuff away.
381
333
    
382
334
    for file_state, fid, old_name, new_name, kind in bzrlib.diff_trees(old_tree, new_tree):
 
335
        d = None
383
336
 
384
 
        if file_list and new_name not in file_list:
385
 
            continue
386
 
        
387
337
        # Don't show this by default; maybe do it if an option is passed
388
338
        # idlabel = '      {%s}' % fid
389
339
        idlabel = ''
437
387
def cmd_deleted(show_ids=False):
438
388
    """List files deleted in the working tree.
439
389
 
440
 
    TODO: Show files deleted since a previous revision, or between two revisions.
 
390
TODO: Show files deleted since a previous revision, or between two revisions.
441
391
    """
442
392
    b = Branch('.')
443
393
    old = b.basis_tree()
465
415
 
466
416
 
467
417
def cmd_load_inventory():
468
 
    """Load inventory for timing purposes"""
469
 
    Branch('.').basis_tree().inventory
470
 
 
471
 
 
472
 
def cmd_dump_inventory():
473
 
    Branch('.').read_working_inventory().write_xml(sys.stdout)
 
418
    inv = Branch('.').basis_tree().inventory
 
419
 
474
420
 
475
421
 
476
422
def cmd_dump_new_inventory():
488
434
    import bzrlib.newinventory
489
435
    inv = Branch('.').basis_tree().inventory
490
436
    bzrlib.newinventory.write_slacker_inventory(inv, sys.stdout)
491
 
 
492
 
 
493
 
 
494
 
def cmd_dump_text_inventory():
495
 
    import bzrlib.textinv
496
 
    inv = Branch('.').basis_tree().inventory
497
 
    bzrlib.textinv.write_text_inventory(inv, sys.stdout)
498
 
 
499
 
 
500
 
def cmd_load_text_inventory():
501
 
    import bzrlib.textinv
502
 
    inv = bzrlib.textinv.read_text_inventory(sys.stdin)
503
 
    print 'loaded %d entries' % len(inv)
504
 
    
 
437
                
505
438
    
506
439
 
507
440
def cmd_root(filename=None):
550
483
 
551
484
 
552
485
 
553
 
def cmd_ignored():
 
486
def cmd_ignored(verbose=True):
554
487
    """List ignored files and the patterns that matched them.
555
488
      """
556
489
    tree = Branch('.').working_tree()
557
 
    for path, file_class, kind, file_id in tree.list_files():
 
490
    for path, file_class, kind, id in tree.list_files():
558
491
        if file_class != 'I':
559
492
            continue
560
493
        ## XXX: Slightly inefficient since this was already calculated
580
513
    t = b.revision_tree(rh)
581
514
    t.export(dest)
582
515
 
583
 
def cmd_cat(revision, filename):
584
 
    """Print file to stdout."""
585
 
    b = Branch('.')
586
 
    b.print_file(b.relpath(filename), int(revision))
587
516
 
588
517
 
589
518
######################################################################
604
533
def cmd_commit(message=None, verbose=False):
605
534
    """Commit changes to a new revision.
606
535
 
607
 
    --message MESSAGE
608
 
        Description of changes in this revision; free form text.
609
 
        It is recommended that the first line be a single-sentence
610
 
        summary.
611
 
    --verbose
612
 
        Show status of changed files,
613
 
 
614
 
    TODO: Commit only selected files.
615
 
 
616
 
    TODO: Run hooks on tree to-be-committed, and after commit.
617
 
 
618
 
    TODO: Strict commit that fails if there are unknown or deleted files.
619
 
    """
 
536
--message MESSAGE
 
537
    Description of changes in this revision; free form text.
 
538
    It is recommended that the first line be a single-sentence
 
539
    summary.
 
540
--verbose
 
541
    Show status of changed files,
 
542
 
 
543
TODO: Commit only selected files.
 
544
 
 
545
TODO: Run hooks on tree to-be-committed, and after commit.
 
546
 
 
547
TODO: Strict commit that fails if there are unknown or deleted files.
 
548
"""
620
549
 
621
550
    if not message:
622
551
        bailout("please specify a commit message")
626
555
def cmd_check(dir='.'):
627
556
    """check: Consistency check of branch history.
628
557
 
629
 
    usage: bzr check [-v] [BRANCH]
630
 
 
631
 
    options:
632
 
      --verbose, -v         Show progress of checking.
633
 
 
634
 
    This command checks various invariants about the branch storage to
635
 
    detect data corruption or bzr bugs.
636
 
    """
 
558
usage: bzr check [-v] [BRANCH]
 
559
 
 
560
options:
 
561
  --verbose, -v         Show progress of checking.
 
562
 
 
563
This command checks various invariants about the branch storage to
 
564
detect data corruption or bzr bugs.
 
565
"""
637
566
    import bzrlib.check
638
567
    bzrlib.check.check(Branch(dir, find_root=False))
639
568
 
662
591
 
663
592
 
664
593
def cmd_gen_revision_id():
 
594
    import time
665
595
    print bzrlib.branch._gen_revision_id(time.time())
666
596
 
667
597
 
668
 
def cmd_selftest():
 
598
def cmd_selftest(verbose=False):
669
599
    """Run internal test suite"""
670
600
    ## -v, if present, is seen by doctest; the argument is just here
671
601
    ## so our parser doesn't complain
715
645
    except KeyError:
716
646
        bailout("no help for %r" % topic)
717
647
 
718
 
    doc = getdoc(cmdfn)
 
648
    doc = cmdfn.__doc__
719
649
    if doc == None:
720
650
        bailout("sorry, no detailed help yet for %r" % topic)
721
651
 
770
700
# listed take none.
771
701
cmd_options = {
772
702
    'add':                    ['verbose'],
773
 
    'cat':                    ['revision'],
774
703
    'commit':                 ['message', 'verbose'],
775
704
    'deleted':                ['show-ids'],
776
705
    'diff':                   ['revision'],
784
713
 
785
714
cmd_args = {
786
715
    'add':                    ['file+'],
787
 
    'cat':                    ['filename'],
788
716
    'commit':                 [],
789
 
    'diff':                   ['file*'],
 
717
    'diff':                   [],
790
718
    'export':                 ['revno', 'dest'],
791
719
    'file-id':                ['filename'],
792
 
    'file-id-path':           ['filename'],
793
720
    'get-file-text':          ['text_id'],
794
721
    'get-inventory':          ['inventory_id'],
795
722
    'get-revision':           ['revision_id'],
798
725
    'init':                   [],
799
726
    'log':                    [],
800
727
    'lookup-revision':        ['revno'],
801
 
    'move':                   ['source$', 'dest'],
 
728
    'mv':                     ['source$', 'dest'],
802
729
    'relpath':                ['filename'],
803
730
    'remove':                 ['file+'],
804
 
    'rename':                 ['from_name', 'to_name'],
805
731
    'renames':                ['dir?'],
806
732
    'root':                   ['filename?'],
807
733
    'status':                 [],
895
821
        if ap[-1] == '?':
896
822
            if args:
897
823
                argdict[argname] = args.pop(0)
898
 
        elif ap[-1] == '*': # all remaining arguments
899
 
            if args:
900
 
                argdict[argname + '_list'] = args[:]
901
 
                args = []
902
 
            else:
903
 
                argdict[argname + '_list'] = None
 
824
        elif ap[-1] == '*':
 
825
            assert 0
904
826
        elif ap[-1] == '+':
905
827
            if not args:
906
828
                bailout("command %r needs one or more %s"
975
897
            bailout("option %r is not allowed for command %r"
976
898
                    % (oname, cmd))
977
899
 
978
 
    # TODO: give an error if there are any mandatory options which are
979
 
    # not specified?  Or maybe there shouldn't be any "mandatory
980
 
    # options" (it is an oxymoron)
981
 
 
982
900
    # mix arguments and options into one dictionary
983
901
    cmdargs = _match_args(cmd, args)
984
902
    for k, v in opts.items():
995
913
        #stats.strip_dirs()
996
914
        stats.sort_stats('time')
997
915
        stats.print_stats(20)
998
 
 
999
 
        return ret
1000
916
    else:
1001
917
        return cmd_handler(**cmdargs) or 0
1002
918
 
1019
935
        if len(e.args) > 1:
1020
936
            for h in e.args[1]:
1021
937
                log_error('  ' + h + '\n')
1022
 
        traceback.print_exc(None, bzrlib.trace._tracefile)
1023
 
        log_error('(see $HOME/.bzr.log for debug information)\n')
1024
938
        return 1
1025
939
    except Exception, e:
1026
940
        log_error('bzr: exception: %s\n' % e)
1027
 
        log_error('(see $HOME/.bzr.log for debug information)\n')
 
941
        log_error('    see .bzr.log for details\n')
1028
942
        traceback.print_exc(None, bzrlib.trace._tracefile)
1029
 
        ## traceback.print_exc(None, sys.stderr)
 
943
        traceback.print_exc(None, sys.stderr)
1030
944
        return 1
1031
945
 
1032
 
    ## TODO: Trap AssertionError
1033
 
 
1034
 
    # TODO: Maybe nicer handling of IOError especially for broken pipe.
 
946
    # TODO: Maybe nicer handling of IOError?
1035
947
 
1036
948
 
1037
949