~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/bundle/commands.py

  • Committer: Robert Collins
  • Date: 2010-05-06 23:54:05 UTC
  • mto: This revision was merged to the branch mainline in revision 5223.
  • Revision ID: robertc@robertcollins.net-20100506235405-wii4elupfhzl3jvy
Add __str__ to the new helper classes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python
 
1
# Copyright (C) 2005-2010 Canonical Ltd
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
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
2
16
"""\
3
17
This is an attempt to take the internal delta object, and represent
4
18
it as a single-file text-only changeset.
7
21
"""
8
22
 
9
23
import sys
10
 
 
11
 
from bzrlib.branch import Branch
12
 
from bzrlib.commands import Command, register_command
13
 
import bzrlib.errors as errors
 
24
from cStringIO import StringIO
 
25
 
 
26
from bzrlib.lazy_import import lazy_import
 
27
lazy_import(globals(), """
 
28
from bzrlib import (
 
29
    branch,
 
30
    errors,
 
31
    merge_directive,
 
32
    revision as _mod_revision,
 
33
    urlutils,
 
34
    transport,
 
35
    )
 
36
""")
 
37
 
 
38
from bzrlib.commands import Command
14
39
from bzrlib.option import Option
15
 
from bzrlib.revision import (common_ancestor, MultipleRevisionSources,
16
 
                             NULL_REVISION)
17
40
from bzrlib.trace import note
18
 
from bzrlib import urlutils
19
 
 
20
 
 
21
 
class cmd_send_changeset(Command):
22
 
    """Send a bundled up changset via mail.
23
 
 
24
 
    If no revision has been specified, the last commited change will
25
 
    be sent.
26
 
 
27
 
    Subject of the mail can be specified by the --message option,
28
 
    otherwise information from the changeset log will be used.
29
 
 
30
 
    A editor will be spawned where the user may enter a description
31
 
    of the changeset.  The description can be read from a file with
32
 
    the --file FILE option.
33
 
    """
34
 
    takes_options = ['revision', 'message', 'file']
35
 
    takes_args = ['to?']
36
 
 
37
 
    def run(self, to=None, message=None, revision=None, file=None):
38
 
        from bzrlib.errors import BzrCommandError
39
 
        from send_changeset import send_changeset
40
 
        
41
 
        if isinstance(revision, (list, tuple)):
42
 
            if len(revision) > 1:
43
 
                raise BzrCommandError('We do not support rollup-changesets yet.')
44
 
            revision = revision[0]
45
 
 
46
 
        b = Branch.open_containing('.')
47
 
 
48
 
        if not to:
49
 
            try:
50
 
                to = b.controlfile('x-send-address', 'rb').read().strip('\n')
51
 
            except:
52
 
                raise BzrCommandError('destination address is not known')
53
 
 
54
 
        if not isinstance(revision, (list, tuple)):
55
 
            revision = [revision]
56
 
 
57
 
        send_changeset(b, revision, to, message, file)
58
 
 
59
 
 
60
 
class cmd_bundle_revisions(Command):
61
 
    """Generate a revision bundle.
62
 
 
63
 
    This bundle contains all of the meta-information of a
64
 
    diff, rather than just containing the patch information.
65
 
 
66
 
    You can apply it to another tree using 'bzr merge'.
67
 
 
68
 
    bzr bundle-revisions
69
 
        - Generate a bundle relative to a remembered location
70
 
    bzr bundle-revisions BASE
71
 
        - Bundle to apply the current tree into BASE
72
 
    bzr bundle-revisions --revision A
73
 
        - Bundle to apply revision A to remembered location 
74
 
    bzr bundle-revisions --revision A..B
75
 
        - Bundle to transform A into B
76
 
    """
77
 
    takes_options = ['verbose', 'revision', 'remember',
78
 
                     Option("output", help="write bundle to specified file",
79
 
                            type=unicode)]
80
 
    takes_args = ['base?']
81
 
    aliases = ['bundle']
82
 
 
83
 
    def run(self, base=None, revision=None, output=None, remember=False):
84
 
        from bzrlib import user_encoding
85
 
        from bzrlib.bundle.serializer import write_bundle
86
 
 
87
 
        target_branch = Branch.open_containing(u'.')[0]
88
 
 
89
 
        if base is None:
90
 
            base_specified = False
91
 
        else:
92
 
            base_specified = True
93
 
 
94
 
        if revision is None:
95
 
            target_revision = target_branch.last_revision()
96
 
        elif len(revision) < 3:
97
 
            target_revision = revision[-1].in_history(target_branch).rev_id
98
 
            if len(revision) == 2:
99
 
                if base_specified:
100
 
                    raise errors.BzrCommandError('Cannot specify base as well'
101
 
                                                 ' as two revision arguments.')
102
 
                base_revision = revision[0].in_history(target_branch).rev_id
103
 
        else:
104
 
            raise errors.BzrCommandError('--revision takes 1 or 2 parameters')
105
 
 
106
 
        if revision is None or len(revision) < 2:
107
 
            submit_branch = target_branch.get_submit_branch()
108
 
            if base is None:
109
 
                base = submit_branch
110
 
            if base is None:
111
 
                base = target_branch.get_parent()
112
 
            if base is None:
113
 
                raise errors.BzrCommandError("No base branch known or"
114
 
                                             " specified.")
115
 
            elif not base_specified:
116
 
                # FIXME:
117
 
                # note() doesn't pay attention to terminal_encoding() so
118
 
                # we must format with 'ascii' to be safe
119
 
                note('Using saved location: %s',
120
 
                     urlutils.unescape_for_display(base, 'ascii'))
121
 
            base_branch = Branch.open(base)
122
 
 
123
 
            # We don't want to lock the same branch across
124
 
            # 2 different branches
125
 
            if target_branch.base == base_branch.base:
126
 
                base_branch = target_branch 
127
 
            if submit_branch is None or remember:
128
 
                if base_specified:
129
 
                    target_branch.set_submit_branch(base_branch.base)
130
 
                elif remember:
131
 
                    raise errors.BzrCommandError('--remember requires a branch'
132
 
                                                 ' to be specified.')
133
 
            target_branch.repository.fetch(base_branch.repository, 
134
 
                                           base_branch.last_revision())
135
 
            base_revision = common_ancestor(base_branch.last_revision(),
136
 
                                            target_revision,
137
 
                                            target_branch.repository)
138
 
 
139
 
 
140
 
        if output is not None:
141
 
            fileobj = file(output, 'wb')
142
 
        else:
143
 
            fileobj = sys.stdout
144
 
        target_branch.repository.lock_read()
145
 
        try:
146
 
            write_bundle(target_branch.repository, target_revision,
147
 
                         base_revision, fileobj)
148
 
        finally:
149
 
            target_branch.repository.unlock()
150
 
 
151
 
 
152
 
class cmd_verify_changeset(Command):
153
 
    """Read a written changeset, and make sure it is valid.
154
 
 
155
 
    """
156
 
    takes_args = ['filename?']
157
 
 
158
 
    def run(self, filename=None):
159
 
        from read_changeset import read_changeset
160
 
        #from bzrlib.xml import serializer_v4
161
 
 
162
 
        b, relpath = Branch.open_containing('.')
163
 
 
164
 
        if filename is None or filename == '-':
165
 
            f = sys.stdin
166
 
        else:
167
 
            f = open(filename, 'U')
168
 
 
169
 
        cset_info, cset_tree = read_changeset(f, b)
170
 
        # print cset_info
171
 
        # print cset_tree
172
 
        #serializer_v4.write(cset_tree.inventory, sys.stdout)
 
41
 
 
42
 
 
43
class cmd_bundle_info(Command):
 
44
    __doc__ = """Output interesting stats about a bundle"""
 
45
 
 
46
    hidden = True
 
47
    takes_args = ['location']
 
48
    takes_options = ['verbose']
 
49
    encoding_type = 'exact'
 
50
 
 
51
    def run(self, location, verbose=False):
 
52
        from bzrlib.bundle.serializer import read_bundle
 
53
        from bzrlib.bundle import read_mergeable_from_url
 
54
        from bzrlib import osutils
 
55
        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')
 
64
        reader_method = getattr(bundle_info, 'get_bundle_reader', None)
 
65
        if reader_method is None:
 
66
            raise errors.BzrCommandError('Bundle format not supported')
 
67
 
 
68
        by_kind = {}
 
69
        file_ids = set()
 
70
        for bytes, parents, repo_kind, revision_id, file_id\
 
71
            in reader_method().iter_records():
 
72
            by_kind.setdefault(repo_kind, []).append(
 
73
                (bytes, parents, repo_kind, revision_id, file_id))
 
74
            if file_id is not None:
 
75
                file_ids.add(file_id)
 
76
        self.outf.write('Records\n')
 
77
        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')
 
84
        nicks = set()
 
85
        committers = set()
 
86
        for revision in bundle_info.real_revisions:
 
87
            if 'branch-nick' in revision.properties:
 
88
                nicks.add(revision.properties['branch-nick'])
 
89
            committers.add(revision.committer)
 
90
 
 
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')))
 
96
        if verbose:
 
97
            self.outf.write('\n')
 
98
            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")
 
103
            self.outf.write(content)
 
104
            self.outf.write('\n')