/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/plugins/fastimport/cmds.py

  • Committer: Jelmer Vernooij
  • Date: 2020-03-22 01:35:14 UTC
  • mfrom: (7490.7.6 work)
  • mto: This revision was merged to the branch mainline in revision 7499.
  • Revision ID: jelmer@jelmer.uk-20200322013514-7vw1ntwho04rcuj3
merge lp:brz/3.1.

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
 
16
16
"""Fastimport/fastexport commands."""
17
17
 
18
 
from __future__ import absolute_import
19
 
 
20
18
from ... import controldir
21
19
from ...commands import Command
22
 
from ...option import Option, ListOption, RegistryOption
23
 
from ...sixish import (
24
 
    text_type,
25
 
    )
 
20
from ...option import Option, RegistryOption
26
21
 
27
22
 
28
23
from . import (
85
80
      bzr fast-import project.fi project.bzr
86
81
 
87
82
    Numerous commands are provided for generating a fast-import file
88
 
    to use as input. 
 
83
    to use as input.
89
84
    To specify standard input as the input stream, use a
90
85
    source name of '-' (instead of project.fi). If the source name
91
86
    ends in '.gz', it is assumed to be compressed in gzip format.
242
237
    _see_also = ['fast-export', 'fast-import-filter', 'fast-import-info']
243
238
    takes_args = ['source', 'destination?']
244
239
    takes_options = ['verbose',
245
 
                    Option('user-map', type=text_type,
246
 
                        help="Path to file containing a map of user-ids.",
247
 
                        ),
248
 
                    Option('info', type=text_type,
249
 
                        help="Path to file containing caching hints.",
250
 
                        ),
251
 
                    Option('trees',
252
 
                        help="Update all working trees, not just trunk's.",
253
 
                        ),
254
 
                    Option('count', type=int,
255
 
                        help="Import this many revisions then exit.",
256
 
                        ),
257
 
                    Option('checkpoint', type=int,
258
 
                        help="Checkpoint automatically every N revisions."
259
 
                             " The default is 10000.",
260
 
                        ),
261
 
                    Option('autopack', type=int,
262
 
                        help="Pack every N checkpoints. The default is 4.",
263
 
                        ),
264
 
                    Option('inv-cache', type=int,
265
 
                        help="Number of inventories to cache.",
266
 
                        ),
267
 
                    RegistryOption.from_kwargs('mode',
268
 
                        'The import algorithm to use.',
269
 
                        title='Import Algorithm',
270
 
                        default='Use the preferred algorithm (inventory deltas).',
271
 
                        classic="Use the original algorithm (mutable inventories).",
272
 
                        experimental="Enable experimental features.",
273
 
                        value_switches=True, enum_switch=False,
274
 
                        ),
275
 
                    Option('import-marks', type=text_type,
276
 
                        help="Import marks from file."
277
 
                        ),
278
 
                    Option('export-marks', type=text_type,
279
 
                        help="Export marks to file."
280
 
                        ),
281
 
                    RegistryOption('format',
282
 
                            help='Specify a format for the created repository. See'
283
 
                                 ' "bzr help formats" for details.',
284
 
                            lazy_registry=('breezy.controldir', 'format_registry'),
285
 
                            converter=lambda name: controldir.format_registry.make_controldir(name),
286
 
                            value_switches=False, title='Repository format'),
 
240
                     Option('user-map', type=str,
 
241
                            help="Path to file containing a map of user-ids.",
 
242
                            ),
 
243
                     Option('info', type=str,
 
244
                            help="Path to file containing caching hints.",
 
245
                            ),
 
246
                     Option('trees',
 
247
                            help="Update all working trees, not just trunk's.",
 
248
                            ),
 
249
                     Option('count', type=int,
 
250
                            help="Import this many revisions then exit.",
 
251
                            ),
 
252
                     Option('checkpoint', type=int,
 
253
                            help="Checkpoint automatically every N revisions."
 
254
                            " The default is 10000.",
 
255
                            ),
 
256
                     Option('autopack', type=int,
 
257
                            help="Pack every N checkpoints. The default is 4.",
 
258
                            ),
 
259
                     Option('inv-cache', type=int,
 
260
                            help="Number of inventories to cache.",
 
261
                            ),
 
262
                     RegistryOption.from_kwargs('mode',
 
263
                                                'The import algorithm to use.',
 
264
                                                title='Import Algorithm',
 
265
                                                default='Use the preferred algorithm (inventory deltas).',
 
266
                                                experimental="Enable experimental features.",
 
267
                                                value_switches=True, enum_switch=False,
 
268
                                                ),
 
269
                     Option('import-marks', type=str,
 
270
                            help="Import marks from file."
 
271
                            ),
 
272
                     Option('export-marks', type=str,
 
273
                            help="Export marks to file."
 
274
                            ),
 
275
                     RegistryOption('format',
 
276
                                    help='Specify a format for the created repository. See'
 
277
                                    ' "bzr help formats" for details.',
 
278
                                    lazy_registry=(
 
279
                                        'breezy.controldir', 'format_registry'),
 
280
                                    converter=lambda name: controldir.format_registry.make_controldir(
 
281
                                        name),
 
282
                                    value_switches=False, title='Repository format'),
287
283
                     ]
 
284
 
288
285
    def run(self, source, destination='.', verbose=False, info=None,
289
 
        trees=False, count=-1, checkpoint=10000, autopack=4, inv_cache=-1,
290
 
        mode=None, import_marks=None, export_marks=None, format=None,
291
 
        user_map=None):
 
286
            trees=False, count=-1, checkpoint=10000, autopack=4, inv_cache=-1,
 
287
            mode=None, import_marks=None, export_marks=None, format=None,
 
288
            user_map=None):
292
289
        load_fastimport()
293
290
        from .processors import generic_processor
294
291
        from .helpers import (
316
313
            'export-marks': export_marks,
317
314
            }
318
315
        return _run(source, generic_processor.GenericProcessor,
319
 
                bzrdir=control, params=params, verbose=verbose,
320
 
                user_map=user_map)
 
316
                    bzrdir=control, params=params, verbose=verbose,
 
317
                    user_map=user_map)
321
318
 
322
319
    def _generate_info(self, source):
323
 
        from ...sixish import StringIO
 
320
        from io import StringIO
324
321
        from fastimport import parser
325
322
        from fastimport.errors import ParsingError
326
323
        from ...errors import BzrCommandError
327
 
        from .processors import info_processor
 
324
        from fastimport.processors import info_processor
328
325
        stream = _get_source_stream(source)
329
326
        output = StringIO()
330
327
        try:
341
338
        return lines
342
339
 
343
340
 
344
 
class cmd_fast_import_filter(Command):
345
 
    """Filter a fast-import stream to include/exclude files & directories.
346
 
 
347
 
    This command is useful for splitting a subdirectory or bunch of
348
 
    files out from a project to create a new project complete with history
349
 
    for just those files. It can also be used to create a new project
350
 
    repository that removes all references to files that should not have
351
 
    been committed, e.g. security-related information (like passwords),
352
 
    commercially sensitive material, files with an incompatible license or
353
 
    large binary files like CD images.
354
 
 
355
 
    To specify standard input as the input stream, use a source name
356
 
    of '-'. If the source name ends in '.gz', it is assumed to be
357
 
    compressed in gzip format.
358
 
 
359
 
    :File/directory filtering:
360
 
 
361
 
     This is supported by the -i and -x options. Excludes take precedence
362
 
     over includes.
363
 
 
364
 
     When filtering out a subdirectory (or file), the new stream uses the
365
 
     subdirectory (or subdirectory containing the file) as the root. As
366
 
     fast-import doesn't know in advance whether a path is a file or
367
 
     directory in the stream, you need to specify a trailing '/' on
368
 
     directories passed to the `--includes option`. If multiple files or
369
 
     directories are given, the new root is the deepest common directory.
370
 
 
371
 
     Note: If a path has been renamed, take care to specify the *original*
372
 
     path name, not the final name that it ends up with.
373
 
 
374
 
    :User mapping:
375
 
 
376
 
     Some source repositories store just the user name while Bazaar
377
 
     prefers a full email address. You can adjust user-ids
378
 
     by using the --user-map option. The argument is a
379
 
     text file with lines in the format::
380
 
 
381
 
       old-id = new-id
382
 
 
383
 
     Blank lines and lines beginning with # are ignored.
384
 
     If old-id has the special value '@', then users without an
385
 
     email address will get one created by using the matching new-id
386
 
     as the domain, unless a more explicit address is given for them.
387
 
     For example, given the user-map of::
388
 
 
389
 
       @ = example.com
390
 
       bill = William Jones <bill@example.com>
391
 
 
392
 
     then user-ids are mapped as follows::
393
 
 
394
 
      maria => maria <maria@example.com>
395
 
      bill => William Jones <bill@example.com>
396
 
 
397
 
     .. note::
398
 
 
399
 
        User mapping is supported by both the fast-import and
400
 
        fast-import-filter commands.
401
 
 
402
 
    :History rewriting:
403
 
 
404
 
     By default fast-import-filter does quite aggressive history rewriting.
405
 
     Empty commits (or commits which had all their content filtered out) will
406
 
     be removed, and so are the references to commits not included in the stream.
407
 
 
408
 
     Flag --dont-squash-empty-commits reverses this behavior and makes it possible to
409
 
     use fast-import-filter on incremental streams.
410
 
 
411
 
    :Examples:
412
 
 
413
 
     Create a new project from a library (note the trailing / on the
414
 
     directory name of the library)::
415
 
 
416
 
       front-end | bzr fast-import-filter -i lib/xxx/ > xxx.fi
417
 
       bzr fast-import xxx.fi mylibrary.bzr
418
 
       (lib/xxx/foo is now foo)
419
 
 
420
 
     Create a new repository without a sensitive file::
421
 
 
422
 
       front-end | bzr fast-import-filter -x missile-codes.txt > clean.fi
423
 
       bzr fast-import clean.fi clean.bzr
424
 
    """
425
 
    hidden = False
426
 
    _see_also = ['fast-import']
427
 
    takes_args = ['source?']
428
 
    takes_options = ['verbose',
429
 
                    ListOption('include_paths', short_name='i', type=text_type,
430
 
                        help="Only include commits affecting these paths."
431
 
                             " Directories should have a trailing /."
432
 
                        ),
433
 
                    ListOption('exclude_paths', short_name='x', type=text_type,
434
 
                        help="Exclude these paths from commits."
435
 
                        ),
436
 
                    Option('user-map', type=text_type,
437
 
                        help="Path to file containing a map of user-ids.",
438
 
                        ),
439
 
                    Option('dont-squash-empty-commits',
440
 
                        help="Preserve all commits and links between them"
441
 
                        ),
442
 
                     ]
443
 
    encoding_type = 'exact'
444
 
    def run(self, source=None, verbose=False, include_paths=None,
445
 
        exclude_paths=None, user_map=None, dont_squash_empty_commits=False):
446
 
        from ...errors import BzrCommandError
447
 
        load_fastimport()
448
 
        from fastimport.processors import filter_processor
449
 
        params = {
450
 
            'include_paths': include_paths,
451
 
            'exclude_paths': exclude_paths,
452
 
            }
453
 
        if ('squash_empty_commits' in
454
 
                filter_processor.FilterProcessor.known_params):
455
 
            params['squash_empty_commits'] = (not dont_squash_empty_commits)
456
 
        else:
457
 
            if dont_squash_empty_commits:
458
 
                raise BzrCommandError("installed python-fastimport does not "
459
 
                    "support not squashing empty commits. Please install "
460
 
                    " a newer python-fastimport to use "
461
 
                    "--dont-squash-empty-commits")
462
 
 
463
 
        from fastimport.errors import ParsingError
464
 
        from fastimport import parser
465
 
        stream = _get_source_stream(source)
466
 
        user_mapper = _get_user_mapper(user_map)
467
 
        proc = filter_processor.FilterProcessor(params=params, verbose=verbose)
468
 
        p = parser.ImportParser(stream, verbose=verbose, user_mapper=user_mapper)
469
 
        try:
470
 
            return proc.process(p.iter_commands)
471
 
        except ParsingError as e:
472
 
            raise BzrCommandError("%d: Parse error: %s" % (e.lineno, e))
473
 
 
474
 
 
475
 
class cmd_fast_import_info(Command):
476
 
    """Output information about a fast-import stream.
477
 
 
478
 
    This command reads a fast-import stream and outputs
479
 
    statistics and interesting properties about what it finds.
480
 
    When run in verbose mode, the information is output as a
481
 
    configuration file that can be passed to fast-import to
482
 
    assist it in intelligently caching objects.
483
 
 
484
 
    To specify standard input as the input stream, use a source name
485
 
    of '-'. If the source name ends in '.gz', it is assumed to be
486
 
    compressed in gzip format.
487
 
 
488
 
    :Examples:
489
 
 
490
 
     Display statistics about the import stream produced by front-end::
491
 
 
492
 
      front-end | bzr fast-import-info -
493
 
 
494
 
     Create a hints file for running fast-import on a large repository::
495
 
 
496
 
       front-end | bzr fast-import-info -v - > front-end.cfg
497
 
    """
498
 
    hidden = False
499
 
    _see_also = ['fast-import']
500
 
    takes_args = ['source']
501
 
    takes_options = ['verbose']
502
 
    def run(self, source, verbose=False):
503
 
        load_fastimport()
504
 
        from .processors import info_processor
505
 
        return _run(source, info_processor.InfoProcessor, verbose=verbose)
506
 
 
507
 
 
508
 
class cmd_fast_import_query(Command):
509
 
    """Query a fast-import stream displaying selected commands.
510
 
 
511
 
    To specify standard input as the input stream, use a source name
512
 
    of '-'. If the source name ends in '.gz', it is assumed to be
513
 
    compressed in gzip format.
514
 
 
515
 
    To specify a commit to display, give its mark using the
516
 
    --commit-mark option. The commit will be displayed with
517
 
    file-commands included but with inline blobs hidden.
518
 
 
519
 
    To specify the commands to display, use the -C option one or
520
 
    more times. To specify just some fields for a command, use the
521
 
    syntax::
522
 
 
523
 
      command=field1,...
524
 
 
525
 
    By default, the nominated fields for the nominated commands
526
 
    are displayed tab separated. To see the information in
527
 
    a name:value format, use verbose mode.
528
 
 
529
 
    Note: Binary fields (e.g. data for blobs) are masked out
530
 
    so it is generally safe to view the output in a terminal.
531
 
 
532
 
    :Examples:
533
 
 
534
 
     Show the commit with mark 429::
535
 
 
536
 
      bzr fast-import-query xxx.fi -m429
537
 
 
538
 
     Show all the fields of the reset and tag commands::
539
 
 
540
 
      bzr fast-import-query xxx.fi -Creset -Ctag
541
 
 
542
 
     Show the mark and merge fields of the commit commands::
543
 
 
544
 
      bzr fast-import-query xxx.fi -Ccommit=mark,merge
545
 
    """
546
 
    hidden = True
547
 
    _see_also = ['fast-import', 'fast-import-filter']
548
 
    takes_args = ['source']
549
 
    takes_options = ['verbose',
550
 
                    Option('commit-mark', short_name='m', type=text_type,
551
 
                        help="Mark of the commit to display."
552
 
                        ),
553
 
                    ListOption('commands', short_name='C', type=text_type,
554
 
                        help="Display fields for these commands."
555
 
                        ),
556
 
                     ]
557
 
    def run(self, source, verbose=False, commands=None, commit_mark=None):
558
 
        load_fastimport()
559
 
        from fastimport.processors import query_processor
560
 
        from . import helpers
561
 
        params = helpers.defines_to_dict(commands) or {}
562
 
        if commit_mark:
563
 
            params['commit-mark'] = commit_mark
564
 
        return _run(source, query_processor.QueryProcessor, params=params,
565
 
            verbose=verbose)
566
 
 
567
 
 
568
341
class cmd_fast_export(Command):
569
342
    """Generate a fast-import stream from a Bazaar branch.
570
343
 
670
443
    _see_also = ['fast-import', 'fast-import-filter']
671
444
    takes_args = ['source?', 'destination?']
672
445
    takes_options = ['verbose', 'revision',
673
 
                    Option('git-branch', short_name='b', type=text_type,
674
 
                        argname='FILE',
675
 
                        help='Name of the git branch to create (default=master).'
676
 
                        ),
677
 
                    Option('checkpoint', type=int, argname='N',
678
 
                        help="Checkpoint every N revisions (default=10000)."
679
 
                        ),
680
 
                    Option('marks', type=text_type, argname='FILE',
681
 
                        help="Import marks from and export marks to file."
682
 
                        ),
683
 
                    Option('import-marks', type=text_type, argname='FILE',
684
 
                        help="Import marks from file."
685
 
                        ),
686
 
                    Option('export-marks', type=text_type, argname='FILE',
687
 
                        help="Export marks to file."
688
 
                        ),
689
 
                    Option('plain',
690
 
                        help="Exclude metadata to maximise interoperability."
691
 
                        ),
692
 
                    Option('rewrite-tag-names',
693
 
                        help="Replace characters invalid in git with '_'"
694
 
                             " (plain mode only).",
695
 
                        ),
696
 
                    Option('baseline',
697
 
                        help="Export an 'absolute' baseline commit prior to"
698
 
                             "the first relative commit",
699
 
                        ),
700
 
                    Option('no-tags',
701
 
                        help="Don't export tags"
702
 
                        ),
 
446
                     Option('git-branch', short_name='b', type=str,
 
447
                            argname='FILE',
 
448
                            help='Name of the git branch to create (default=master).'
 
449
                            ),
 
450
                     Option('checkpoint', type=int, argname='N',
 
451
                            help="Checkpoint every N revisions (default=10000)."
 
452
                            ),
 
453
                     Option('marks', type=str, argname='FILE',
 
454
                            help="Import marks from and export marks to file."
 
455
                            ),
 
456
                     Option('import-marks', type=str, argname='FILE',
 
457
                            help="Import marks from file."
 
458
                            ),
 
459
                     Option('export-marks', type=str, argname='FILE',
 
460
                            help="Export marks to file."
 
461
                            ),
 
462
                     Option('plain',
 
463
                            help="Exclude metadata to maximise interoperability."
 
464
                            ),
 
465
                     Option('rewrite-tag-names',
 
466
                            help="Replace characters invalid in git with '_'"
 
467
                            " (plain mode only).",
 
468
                            ),
 
469
                     Option('baseline',
 
470
                            help="Export an 'absolute' baseline commit prior to"
 
471
                            "the first relative commit",
 
472
                            ),
 
473
                     Option('no-tags',
 
474
                            help="Don't export tags"
 
475
                            ),
703
476
                     ]
704
477
    encoding_type = 'exact'
 
478
 
705
479
    def run(self, source=None, destination=None, verbose=False,
706
 
        git_branch="master", checkpoint=10000, marks=None,
707
 
        import_marks=None, export_marks=None, revision=None,
708
 
        plain=True, rewrite_tag_names=False, no_tags=False, baseline=False):
 
480
            git_branch="master", checkpoint=10000, marks=None,
 
481
            import_marks=None, export_marks=None, revision=None,
 
482
            plain=True, rewrite_tag_names=False, no_tags=False, baseline=False):
709
483
        load_fastimport()
710
484
        from ...branch import Branch
711
485
        from . import exporter
718
492
            source = "."
719
493
        branch = Branch.open_containing(source)[0]
720
494
        outf = exporter._get_output_stream(destination)
721
 
        exporter = exporter.BzrFastExporter(branch,
722
 
            outf=outf, ref="refs/heads/%s" % git_branch, checkpoint=checkpoint,
723
 
            import_marks_file=import_marks, export_marks_file=export_marks,
724
 
            revision=revision, verbose=verbose, plain_format=plain,
725
 
            rewrite_tags=rewrite_tag_names, no_tags=no_tags, baseline=baseline)
 
495
        exporter = exporter.BzrFastExporter(
 
496
            branch,
 
497
            outf=outf, ref=b"refs/heads/%s" % git_branch.encode('utf-8'),
 
498
            checkpoint=checkpoint, import_marks_file=import_marks,
 
499
            export_marks_file=export_marks, revision=revision, verbose=verbose,
 
500
            plain_format=plain, rewrite_tags=rewrite_tag_names,
 
501
            no_tags=no_tags, baseline=baseline)
726
502
        return exporter.run()