~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/bundle/commands.py

  • Committer: Aaron Bentley
  • Date: 2007-07-03 23:57:45 UTC
  • mfrom: (2520.5.5 bzr.mpbundle)
  • mto: This revision was merged to the branch mainline in revision 2631.
  • Revision ID: aaron.bentley@utoronto.ca-20070703235745-gj0v09eqi9k8lrur
Merge initial support for 'submit' command

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
"""
22
22
 
23
23
import sys
 
24
from cStringIO import StringIO
24
25
 
25
26
from bzrlib.lazy_import import lazy_import
26
27
lazy_import(globals(), """
27
28
from bzrlib import (
28
29
    branch,
29
30
    errors,
 
31
    merge_directive,
30
32
    revision as _mod_revision,
31
33
    urlutils,
32
34
    transport,
38
40
from bzrlib.trace import note
39
41
 
40
42
 
41
 
class cmd_bundle_revisions(Command):
42
 
    """Generate a revision bundle.
43
 
 
44
 
    This bundle contains all of the meta-information of a
45
 
    diff, rather than just containing the patch information.
46
 
 
47
 
    You can apply it to another tree using 'bzr merge'.
48
 
 
49
 
    bzr bundle-revisions
50
 
        - Generate a bundle relative to a remembered location
51
 
 
52
 
    bzr bundle-revisions BASE
53
 
        - Bundle to apply the current tree into BASE
54
 
 
55
 
    bzr bundle-revisions --revision A
56
 
        - Bundle to apply revision A to remembered location 
57
 
 
58
 
    bzr bundle-revisions --revision A..B
59
 
        - Bundle to transform A into B
60
 
    """
61
 
    takes_options = ['revision', 'remember',
62
 
                     Option("output", help="write bundle to specified file",
63
 
                            type=unicode)]
64
 
    takes_args = ['base?']
65
 
    aliases = ['bundle']
66
 
    encoding_type = 'exact'
67
 
 
68
 
    def run(self, base=None, revision=None, output=None, remember=False):
69
 
        from bzrlib import user_encoding
70
 
        from bzrlib.bundle.serializer import write_bundle
71
 
 
72
 
        target_branch = branch.Branch.open_containing(u'.')[0]
73
 
        target_branch.lock_write()
74
 
        locked = [target_branch]
75
 
 
76
 
        try:
77
 
            if base is None:
78
 
                base_specified = False
79
 
            else:
80
 
                base_specified = True
81
 
 
82
 
            if revision is None:
83
 
                target_revision = target_branch.last_revision()
84
 
            elif len(revision) < 3:
85
 
                target_revision = revision[-1].in_history(target_branch).rev_id
86
 
                if len(revision) == 2:
87
 
                    if base_specified:
88
 
                        raise errors.BzrCommandError(
89
 
                            'Cannot specify base as well as two revision'
90
 
                            ' arguments.')
91
 
                    revspec = revision[0].in_history(target_branch)
92
 
                    base_revision = revspec.rev_id
93
 
            else:
94
 
                raise errors.BzrCommandError('--revision takes 1 or 2 '
95
 
                                             'parameters')
96
 
 
97
 
            if revision is None or len(revision) < 2:
98
 
                submit_branch = target_branch.get_submit_branch()
99
 
                if base is None:
100
 
                    base = submit_branch
101
 
                if base is None:
102
 
                    base = target_branch.get_parent()
103
 
                if base is None:
104
 
                    raise errors.BzrCommandError("No base branch known or"
105
 
                                                 " specified.")
106
 
                elif not base_specified:
107
 
                    # FIXME:
108
 
                    # note() doesn't pay attention to terminal_encoding() so
109
 
                    # we must format with 'ascii' to be safe
110
 
                    note('Using saved location: %s',
111
 
                         urlutils.unescape_for_display(base, 'ascii'))
112
 
                base_branch = branch.Branch.open(base)
113
 
                base_branch.lock_read()
114
 
                locked.append(base_branch)
115
 
                if submit_branch is None or remember:
116
 
                    if base_specified:
117
 
                        target_branch.set_submit_branch(base_branch.base)
118
 
                    elif remember:
119
 
                        raise errors.BzrCommandError(
120
 
                            '--remember requires a branch to be specified.')
121
 
                target_branch.repository.fetch(base_branch.repository,
122
 
                                               base_branch.last_revision())
123
 
                graph = target_branch.repository.get_graph()
124
 
                base_revision = graph.find_unique_lca(
125
 
                    _mod_revision.ensure_null(base_branch.last_revision()),
126
 
                    _mod_revision.ensure_null(target_revision))
127
 
 
128
 
            if output is not None:
129
 
                fileobj = file(output, 'wb')
130
 
            else:
131
 
                fileobj = sys.stdout
132
 
            write_bundle(target_branch.repository, target_revision,
133
 
                         base_revision, fileobj)
134
 
        finally:
135
 
            for item in reversed(locked):
136
 
                item.unlock()
137
 
 
138
 
 
139
43
class cmd_bundle_info(Command):
140
44
    """Output interesting stats about a bundle"""
141
45
 
142
46
    hidden = True
143
47
    takes_args = ['location']
144
 
    takes_options = [Option('verbose', help="output decoded contents",
145
 
                            short_name='v')]
 
48
    takes_options = []
146
49
    encoding_type = 'exact'
147
50
 
148
51
    def run(self, location, verbose=False):
149
52
        from bzrlib.bundle.serializer import read_bundle
 
53
        from bzrlib.bundle import read_mergeable_from_url
150
54
        from bzrlib import osutils
151
55
        term_encoding = osutils.get_terminal_encoding()
152
 
        dirname, basename = urlutils.split(location)
153
 
        bundle_file = transport.get_transport(dirname).get(basename)
154
 
        bundle_info = read_bundle(bundle_file)
 
56
        bundle_info = read_mergeable_from_url(location)
 
57
        if isinstance(bundle_info, merge_directive._BaseMergeDirective):
 
58
            bundle_info = read_bundle(StringIO(bundle_info.get_raw_bundle()))
155
59
        reader_method = getattr(bundle_info, 'get_bundle_reader', None)
156
60
        if reader_method is None:
157
61
            raise errors.BzrCommandError('Bundle format not supported')