/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 bzrlib/bundle/commands.py

  • Committer: Aaron Bentley
  • Date: 2007-06-20 00:25:11 UTC
  • mto: (2520.5.2 bzr.mpbundle)
  • mto: This revision was merged to the branch mainline in revision 2631.
  • Revision ID: aaron.bentley@utoronto.ca-20070620002511-6mylajxf4mipn7g7
refactor bundle serialization to make write_bundle primary

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2010 Canonical Ltd
 
1
# Copyright (C) 2006 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
"""\
17
17
This is an attempt to take the internal delta object, and represent
18
18
it as a single-file text-only changeset.
21
21
"""
22
22
 
23
23
import sys
24
 
from cStringIO import StringIO
25
24
 
26
25
from bzrlib.lazy_import import lazy_import
27
26
lazy_import(globals(), """
28
27
from bzrlib import (
29
28
    branch,
30
29
    errors,
31
 
    merge_directive,
32
30
    revision as _mod_revision,
33
31
    urlutils,
34
32
    transport,
40
38
from bzrlib.trace import note
41
39
 
42
40
 
 
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
 
43
139
class cmd_bundle_info(Command):
44
 
    __doc__ = """Output interesting stats about a bundle"""
 
140
    """Output interesting stats about a bundle"""
45
141
 
46
142
    hidden = True
47
143
    takes_args = ['location']
48
 
    takes_options = ['verbose']
 
144
    takes_options = [Option('verbose', help="output decoded contents",
 
145
                            short_name='v')]
49
146
    encoding_type = 'exact'
50
147
 
51
148
    def run(self, location, verbose=False):
52
149
        from bzrlib.bundle.serializer import read_bundle
53
 
        from bzrlib.bundle import read_mergeable_from_url
54
150
        from bzrlib import osutils
55
151
        term_encoding = osutils.get_terminal_encoding()
56
 
        bundle_info = read_mergeable_from_url(location)
57
 
        if isinstance(bundle_info, merge_directive.BaseMergeDirective):
58
 
            bundle_file = StringIO(bundle_info.get_raw_bundle())
59
 
            bundle_info = read_bundle(bundle_file)
60
 
        else:
61
 
            if verbose:
62
 
                raise errors.BzrCommandError('--verbose requires a merge'
63
 
                    ' directive')
 
152
        dirname, basename = urlutils.split(location)
 
153
        bundle_file = transport.get_transport(dirname).get(basename)
 
154
        bundle_info = read_bundle(bundle_file)
64
155
        reader_method = getattr(bundle_info, 'get_bundle_reader', None)
65
156
        if reader_method is None:
66
157
            raise errors.BzrCommandError('Bundle format not supported')
73
164
                (bytes, parents, repo_kind, revision_id, file_id))
74
165
            if file_id is not None:
75
166
                file_ids.add(file_id)
76
 
        self.outf.write('Records\n')
 
167
        print >> self.outf, 'Records'
77
168
        for kind, records in sorted(by_kind.iteritems()):
78
 
            multiparent = sum(1 for b, m, k, r, f in records if
79
 
                              len(m.get('parents', [])) > 1)
80
 
            self.outf.write('%s: %d (%d multiparent)\n' % \
81
 
                (kind, len(records), multiparent))
82
 
        self.outf.write('unique files: %d\n' % len(file_ids))
83
 
        self.outf.write('\n')
 
169
            multiparent = sum(1 for b, p, k, r, f in records if len(p) > 1)
 
170
            print >> self.outf, '%s: %d (%d multiparent)' % \
 
171
                (kind, len(records), multiparent)
 
172
        print >> self.outf, 'unique files: %d' % len(file_ids)
 
173
        print >> self.outf
84
174
        nicks = set()
85
175
        committers = set()
86
176
        for revision in bundle_info.real_revisions:
88
178
                nicks.add(revision.properties['branch-nick'])
89
179
            committers.add(revision.committer)
90
180
 
91
 
        self.outf.write('Revisions\n')
92
 
        self.outf.write(('nicks: %s\n'
93
 
            % ', '.join(sorted(nicks))).encode(term_encoding, 'replace'))
94
 
        self.outf.write(('committers: \n%s\n' %
95
 
        '\n'.join(sorted(committers)).encode(term_encoding, 'replace')))
 
181
        print >> self.outf, 'Revisions'
 
182
        print >> self.outf, ('nicks: %s'
 
183
            % ', '.join(sorted(nicks))).encode(term_encoding, 'replace')
 
184
        print >> self.outf, ('committers: \n%s' %
 
185
        '\n'.join(sorted(committers)).encode(term_encoding, 'replace'))
96
186
        if verbose:
97
 
            self.outf.write('\n')
 
187
            print >> self.outf
98
188
            bundle_file.seek(0)
99
 
            line = bundle_file.readline()
100
 
            line = bundle_file.readline()
101
 
            content = bundle_file.read().decode('bz2')
102
 
            self.outf.write("Decoded contents\n")
 
189
            while True:
 
190
                line = bundle_file.readline()
 
191
                assert line != ''
 
192
                if line.rstrip('\n') == '# End of patch':
 
193
                    break
 
194
            content = bundle_file.read().decode('base-64').decode('bz2')
 
195
            print >> self.outf, "Decoded contents"
103
196
            self.outf.write(content)
104
 
            self.outf.write('\n')
 
197
            print >> self.outf