~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/builtins.py

  • Committer: John Arbash Meinel
  • Date: 2006-04-25 15:05:42 UTC
  • mfrom: (1185.85.85 bzr-encoding)
  • mto: This revision was merged to the branch mainline in revision 1752.
  • Revision ID: john@arbash-meinel.com-20060425150542-c7b518dca9928691
[merge] the old bzr-encoding changes, reparenting them on bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
 
20
20
import errno
21
21
import os
 
22
import codecs
22
23
from shutil import rmtree
23
24
import sys
24
25
 
147
148
    takes_args = ['file*']
148
149
    takes_options = ['all', 'show-ids', 'revision']
149
150
    aliases = ['st', 'stat']
 
151
 
 
152
    encoding_type = 'replace'
150
153
    
151
154
    @display_command
152
155
    def run(self, all=False, show_ids=False, file_list=None, revision=None):
153
 
        tree, file_list = tree_files(file_list)
154
 
            
155
156
        from bzrlib.status import show_tree_status
 
157
 
 
158
        tree, file_list = tree_files(file_list)
 
159
            
156
160
        show_tree_status(tree, show_unchanged=all, show_ids=show_ids,
157
 
                         specific_files=file_list, revision=revision)
 
161
                         specific_files=file_list, revision=revision,
 
162
                         to_file=self.outf)
158
163
 
159
164
 
160
165
class cmd_cat_revision(Command):
176
181
        if revision_id is None and revision is None:
177
182
            raise BzrCommandError('You must supply either --revision or a revision_id')
178
183
        b = WorkingTree.open_containing(u'.')[0].branch
 
184
 
 
185
        # TODO: jam 20060112 should cat-revision always output utf-8?
179
186
        if revision_id is not None:
180
 
            sys.stdout.write(b.repository.get_revision_xml(revision_id))
 
187
            self.outf.write(b.repository.get_revision_xml(revision_id).decode('utf-8'))
181
188
        elif revision is not None:
182
189
            for rev in revision:
183
190
                if rev is None:
184
191
                    raise BzrCommandError('You cannot specify a NULL revision.')
185
192
                revno, rev_id = rev.in_history(b)
186
 
                sys.stdout.write(b.repository.get_revision_xml(rev_id))
 
193
                self.outf.write(b.repository.get_revision_xml(rev_id).decode('utf-8'))
187
194
    
188
195
 
189
196
class cmd_revno(Command):
190
197
    """Show current revision number.
191
198
 
192
 
    This is equal to the number of revisions on this branch."""
 
199
    This is equal to the number of revisions on this branch.
 
200
    """
 
201
 
193
202
    takes_args = ['location?']
 
203
 
194
204
    @display_command
195
205
    def run(self, location=u'.'):
196
 
        print Branch.open_containing(location)[0].revno()
 
206
        self.outf.write(str(Branch.open_containing(location)[0].revno()))
 
207
        self.outf.write('\n')
197
208
 
198
209
 
199
210
class cmd_revision_info(Command):
202
213
    hidden = True
203
214
    takes_args = ['revision_info*']
204
215
    takes_options = ['revision']
 
216
 
205
217
    @display_command
206
218
    def run(self, revision=None, revision_info_list=[]):
207
219
 
252
264
    """
253
265
    takes_args = ['file*']
254
266
    takes_options = ['no-recurse', 'dry-run', 'verbose']
 
267
    encoding_type = 'replace'
255
268
 
256
269
    def run(self, file_list, no_recurse=False, dry_run=False, verbose=False):
257
270
        import bzrlib.add
258
271
 
259
 
        if dry_run:
260
 
            if is_quiet():
261
 
                # This is pointless, but I'd rather not raise an error
262
 
                action = bzrlib.add.add_action_null
263
 
            else:
264
 
                action = bzrlib.add.add_action_print
265
 
        elif is_quiet():
266
 
            action = bzrlib.add.add_action_add
267
 
        else:
268
 
            action = bzrlib.add.add_action_add_and_print
 
272
        action = bzrlib.add.AddAction(to_file=self.outf,
 
273
            should_add=(not dry_run), should_print=(not is_quiet()))
269
274
 
270
275
        added, ignored = bzrlib.add.smart_add(file_list, not no_recurse, 
271
 
                                              action)
 
276
                                              action=action)
272
277
        if len(ignored) > 0:
273
278
            for glob in sorted(ignored.keys()):
274
279
                match_len = len(ignored[glob])
275
280
                if verbose:
276
281
                    for path in ignored[glob]:
277
 
                        print "ignored %s matching \"%s\"" % (path, glob)
 
282
                        self.outf.write("ignored %s matching \"%s\"\n" 
 
283
                                        % (path, glob))
278
284
                else:
279
 
                    print "ignored %d file(s) matching \"%s\"" % (match_len,
280
 
                                                              glob)
281
 
            print "If you wish to add some of these files, please add them"\
282
 
                " by name."
 
285
                    self.outf.write("ignored %d file(s) matching \"%s\"\n"
 
286
                                    % (match_len, glob))
 
287
            self.outf.write("If you wish to add some of these files,"
 
288
                            " please add them by name.\n")
283
289
 
284
290
 
285
291
class cmd_mkdir(Command):
288
294
    This is equivalent to creating the directory and then adding it.
289
295
    """
290
296
    takes_args = ['dir+']
 
297
    encoding_type = 'replace'
291
298
 
292
299
    def run(self, dir_list):
293
300
        for d in dir_list:
294
301
            os.mkdir(d)
295
302
            wt, dd = WorkingTree.open_containing(d)
296
303
            wt.add([dd])
297
 
            print 'added', d
 
304
            self.outf.write('added ')
 
305
            self.outf.write(d)
 
306
            self.outf.write('\n')
298
307
 
299
308
 
300
309
class cmd_relpath(Command):
304
313
    
305
314
    @display_command
306
315
    def run(self, filename):
 
316
        # TODO: jam 20050106 Can relpath return a munged path if
 
317
        #       sys.stdout encoding cannot represent it?
307
318
        tree, relpath = WorkingTree.open_containing(filename)
308
 
        print relpath
 
319
        self.outf.write(relpath)
 
320
        self.outf.write('\n')
309
321
 
310
322
 
311
323
class cmd_inventory(Command):
334
346
            if kind and kind != entry.kind:
335
347
                continue
336
348
            if show_ids:
337
 
                print '%-50s %s' % (path, entry.file_id)
 
349
                self.outf.write('%-50s %s\n' % (path, entry.file_id))
338
350
            else:
339
 
                print path
 
351
                self.outf.write(path)
 
352
                self.outf.write('\n')
340
353
 
341
354
 
342
355
class cmd_mv(Command):
355
368
    takes_args = ['names*']
356
369
    aliases = ['move', 'rename']
357
370
 
 
371
    encoding_type = 'replace'
 
372
 
358
373
    def run(self, names_list):
359
374
        if len(names_list) < 2:
360
375
            raise BzrCommandError("missing file argument")
363
378
        if os.path.isdir(names_list[-1]):
364
379
            # move into existing directory
365
380
            for pair in tree.move(rel_names[:-1], rel_names[-1]):
366
 
                print "%s => %s" % pair
 
381
                self.outf.write("%s => %s\n" % pair)
367
382
        else:
368
383
            if len(names_list) != 2:
369
384
                raise BzrCommandError('to mv multiple files the destination '
370
385
                                      'must be a versioned directory')
371
386
            tree.rename_one(rel_names[0], rel_names[1])
372
 
            print "%s => %s" % (rel_names[0], rel_names[1])
 
387
            self.outf.write("%s => %s\n" % (rel_names[0], rel_names[1]))
373
388
            
374
389
    
375
390
class cmd_pull(Command):
396
411
    """
397
412
    takes_options = ['remember', 'overwrite', 'revision', 'verbose']
398
413
    takes_args = ['location?']
 
414
    encoding_type = 'replace'
399
415
 
400
416
    def run(self, location=None, remember=False, overwrite=False, revision=None, verbose=False):
401
417
        # FIXME: too much stuff is in the command class
410
426
            if stored_loc is None:
411
427
                raise BzrCommandError("No pull location known or specified.")
412
428
            else:
413
 
                print "Using saved location: %s" % stored_loc
 
429
                self.outf.write("Using saved location: %s\n" % stored_loc)
414
430
                location = stored_loc
415
431
 
416
432
        if branch_to.get_parent() is None or remember:
437
453
            if old_rh != new_rh:
438
454
                # Something changed
439
455
                from bzrlib.log import show_changed_revisions
440
 
                show_changed_revisions(branch_to, old_rh, new_rh)
 
456
                show_changed_revisions(branch_to, old_rh, new_rh,
 
457
                                       to_file=self.outf)
441
458
 
442
459
 
443
460
class cmd_push(Command):
464
481
    After that, you can omit the location to use the default.  To change the
465
482
    default, use --remember.
466
483
    """
467
 
    takes_options = ['remember', 'overwrite', 
 
484
    takes_options = ['remember', 'overwrite', 'verbose',
468
485
                     Option('create-prefix', 
469
486
                            help='Create the path leading up to the branch '
470
487
                                 'if it does not already exist')]
471
488
    takes_args = ['location?']
 
489
    encoding_type = 'replace'
472
490
 
473
491
    def run(self, location=None, remember=False, overwrite=False,
474
492
            create_prefix=False, verbose=False):
483
501
            if stored_loc is None:
484
502
                raise BzrCommandError("No push location known or specified.")
485
503
            else:
486
 
                print "Using saved location: %s" % stored_loc
 
504
                self.outf.write("Using saved location: %s" % stored_loc)
487
505
                location = stored_loc
488
506
        if br_from.get_push_location() is None or remember:
489
507
            br_from.set_push_location(location)
541
559
            if old_rh != new_rh:
542
560
                # Something changed
543
561
                from bzrlib.log import show_changed_revisions
544
 
                show_changed_revisions(br_to, old_rh, new_rh)
 
562
                show_changed_revisions(br_to, old_rh, new_rh,
 
563
                                       to_file=self.outf)
545
564
 
546
565
 
547
566
class cmd_branch(Command):
618
637
                raise BzrCommandError(msg)
619
638
            if name:
620
639
                branch.control_files.put_utf8('branch-name', name)
621
 
 
622
640
            note('Branched %d revision(s).' % branch.revno())
623
641
        finally:
624
642
            br_from.unlock()
729
747
        renames = list(bzrlib.tree.find_renames(old_inv, new_inv))
730
748
        renames.sort()
731
749
        for old_name, new_name in renames:
732
 
            print "%s => %s" % (old_name, new_name)        
 
750
            self.outf.write("%s => %s\n" % (old_name, new_name))
733
751
 
734
752
 
735
753
class cmd_update(Command):
801
819
    """
802
820
    hidden = True
803
821
    takes_args = ['filename']
 
822
 
804
823
    @display_command
805
824
    def run(self, filename):
806
825
        tree, relpath = WorkingTree.open_containing(filename)
808
827
        if i == None:
809
828
            raise BzrError("%r is not a versioned file" % filename)
810
829
        else:
811
 
            print i
 
830
            self.outf.write(i)
 
831
            self.outf.write('\n')
812
832
 
813
833
 
814
834
class cmd_file_path(Command):
815
835
    """Print path of file_ids to a file or directory.
816
836
 
817
837
    This prints one line for each directory down to the target,
818
 
    starting at the branch root."""
 
838
    starting at the branch root.
 
839
    """
819
840
    hidden = True
820
841
    takes_args = ['filename']
 
842
 
821
843
    @display_command
822
844
    def run(self, filename):
823
845
        tree, relpath = WorkingTree.open_containing(filename)
826
848
        if fid == None:
827
849
            raise BzrError("%r is not a versioned file" % filename)
828
850
        for fip in inv.get_idpath(fid):
829
 
            print fip
 
851
            self.outf.write(fip)
 
852
            self.outf.write('\n')
830
853
 
831
854
 
832
855
class cmd_reconcile(Command):
858
881
class cmd_revision_history(Command):
859
882
    """Display list of revision ids on this branch."""
860
883
    hidden = True
 
884
 
861
885
    @display_command
862
886
    def run(self):
863
887
        branch = WorkingTree.open_containing(u'.')[0].branch
864
888
        for patchid in branch.revision_history():
865
 
            print patchid
 
889
            self.outf.write(patchid)
 
890
            self.outf.write('\n')
866
891
 
867
892
 
868
893
class cmd_ancestry(Command):
869
894
    """List all revisions merged into this branch."""
870
895
    hidden = True
 
896
 
871
897
    @display_command
872
898
    def run(self):
873
899
        tree = WorkingTree.open_containing(u'.')[0]
874
900
        b = tree.branch
875
901
        # FIXME. should be tree.last_revision
876
902
        for revision_id in b.repository.get_ancestry(b.last_revision()):
877
 
            print revision_id
 
903
            if revision_id is None:
 
904
                continue
 
905
            self.outf.write(revision_id)
 
906
            self.outf.write('\n')
878
907
 
879
908
 
880
909
class cmd_init(Command):
999
1028
    takes_args = ['file*']
1000
1029
    takes_options = ['revision', 'diff-options']
1001
1030
    aliases = ['di', 'dif']
 
1031
    encoding_type = 'exact'
1002
1032
 
1003
1033
    @display_command
1004
1034
    def run(self, revision=None, file_list=None, diff_options=None):
1047
1077
    # directories with readdir, rather than stating each one.  Same
1048
1078
    # level of effort but possibly much less IO.  (Or possibly not,
1049
1079
    # if the directories are very large...)
 
1080
    takes_options = ['show-ids']
 
1081
 
1050
1082
    @display_command
1051
1083
    def run(self, show_ids=False):
1052
1084
        tree = WorkingTree.open_containing(u'.')[0]
1053
1085
        old = tree.basis_tree()
1054
1086
        for path, ie in old.inventory.iter_entries():
1055
1087
            if not tree.has_id(ie.file_id):
 
1088
                self.outf.write(path)
1056
1089
                if show_ids:
1057
 
                    print '%-50s %s' % (path, ie.file_id)
1058
 
                else:
1059
 
                    print path
 
1090
                    self.outf.write(' ')
 
1091
                    self.outf.write(ie.file_id)
 
1092
                self.outf.write('\n')
1060
1093
 
1061
1094
 
1062
1095
class cmd_modified(Command):
1070
1103
        td = compare_trees(tree.basis_tree(), tree)
1071
1104
 
1072
1105
        for path, id, kind, text_modified, meta_modified in td.modified:
1073
 
            print path
1074
 
 
 
1106
            self.outf.write(path)
 
1107
            self.outf.write('\n')
1075
1108
 
1076
1109
 
1077
1110
class cmd_added(Command):
1088
1121
            path = inv.id2path(file_id)
1089
1122
            if not os.access(bzrlib.osutils.abspath(path), os.F_OK):
1090
1123
                continue
1091
 
            print path
1092
 
                
1093
 
        
 
1124
            self.outf.write(path)
 
1125
            self.outf.write('\n')
 
1126
 
1094
1127
 
1095
1128
class cmd_root(Command):
1096
1129
    """Show the tree root directory.
1102
1135
    def run(self, filename=None):
1103
1136
        """Print the branch root."""
1104
1137
        tree = WorkingTree.open_containing(filename)[0]
1105
 
        print tree.basedir
 
1138
        self.outf.write(tree.basedir)
 
1139
        self.outf.write('\n')
1106
1140
 
1107
1141
 
1108
1142
class cmd_log(Command):
1136
1170
                            type=str),
1137
1171
                     'short',
1138
1172
                     ]
 
1173
    encoding_type = 'replace'
 
1174
 
1139
1175
    @display_command
1140
1176
    def run(self, location=None, timezone='original',
1141
1177
            verbose=False,
1148
1184
            short=False,
1149
1185
            line=False):
1150
1186
        from bzrlib.log import log_formatter, show_log
1151
 
        import codecs
1152
1187
        assert message is None or isinstance(message, basestring), \
1153
1188
            "invalid message argument %r" % message
1154
1189
        direction = (forward and 'forward') or 'reverse'
1200
1235
        if rev1 > rev2:
1201
1236
            (rev2, rev1) = (rev1, rev2)
1202
1237
 
1203
 
        mutter('encoding log as %r', bzrlib.user_encoding)
1204
 
 
1205
 
        # use 'replace' so that we don't abort if trying to write out
1206
 
        # in e.g. the default C locale.
1207
 
        outf = codecs.getwriter(bzrlib.user_encoding)(sys.stdout, errors='replace')
1208
 
 
1209
1238
        if (log_format == None):
1210
1239
            default = bzrlib.config.BranchConfig(b).log_format()
1211
1240
            log_format = get_log_format(long=long, short=short, line=line, default=default)
1212
 
 
1213
1241
        lf = log_formatter(log_format,
1214
1242
                           show_ids=show_ids,
1215
 
                           to_file=outf,
 
1243
                           to_file=self.outf,
1216
1244
                           show_timezone=timezone)
1217
1245
 
1218
1246
        show_log(b,
1242
1270
    A more user-friendly interface is "bzr log FILE"."""
1243
1271
    hidden = True
1244
1272
    takes_args = ["filename"]
 
1273
    encoding_type = 'replace'
 
1274
 
1245
1275
    @display_command
1246
1276
    def run(self, filename):
1247
1277
        tree, relpath = WorkingTree.open_containing(filename)
1249
1279
        inv = tree.read_working_inventory()
1250
1280
        file_id = inv.path2id(relpath)
1251
1281
        for revno, revision_id, what in bzrlib.log.find_touching_revisions(b, file_id):
1252
 
            print "%6d %s" % (revno, what)
 
1282
            self.outf.write("%6d %s\n" % (revno, what))
1253
1283
 
1254
1284
 
1255
1285
class cmd_ls(Command):
1288
1318
        if revision is not None:
1289
1319
            tree = tree.branch.repository.revision_tree(
1290
1320
                revision[0].in_history(tree.branch).rev_id)
 
1321
 
1291
1322
        for fp, fc, kind, fid, entry in tree.list_files():
1292
1323
            if fp.startswith(relpath):
1293
1324
                fp = fp[len(relpath):]
1297
1328
                    continue
1298
1329
                if verbose:
1299
1330
                    kindch = entry.kind_character()
1300
 
                    print '%-8s %s%s' % (fc, fp, kindch)
 
1331
                    self.outf.write('%-8s %s%s\n' % (fc, fp, kindch))
1301
1332
                elif null:
1302
 
                    sys.stdout.write(fp)
1303
 
                    sys.stdout.write('\0')
1304
 
                    sys.stdout.flush()
 
1333
                    self.outf.write(fp)
 
1334
                    self.outf.write('\0')
 
1335
                    self.outf.flush()
1305
1336
                else:
1306
 
                    print fp
 
1337
                    self.outf.write(fp)
 
1338
                    self.outf.write('\n')
1307
1339
 
1308
1340
 
1309
1341
class cmd_unknowns(Command):
1312
1344
    def run(self):
1313
1345
        from bzrlib.osutils import quotefn
1314
1346
        for f in WorkingTree.open_containing(u'.')[0].unknowns():
1315
 
            print quotefn(f)
 
1347
            self.outf.write(quotefn(f))
 
1348
            self.outf.write('\n')
1316
1349
 
1317
1350
 
1318
1351
class cmd_ignore(Command):
1535
1568
        from bzrlib.msgeditor import edit_commit_message, \
1536
1569
                make_commit_message_template
1537
1570
        from tempfile import TemporaryFile
1538
 
        import codecs
1539
1571
 
1540
1572
        # TODO: Need a blackbox test for invoking the external editor; may be
1541
1573
        # slightly problematic to run this cross-platform.
1558
1590
            raise BzrCommandError("please specify either --message or --file")
1559
1591
        
1560
1592
        if file:
1561
 
            import codecs
1562
1593
            message = codecs.open(file, 'rt', bzrlib.user_encoding).read()
1563
1594
 
1564
1595
        if message == "":