~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/bundle/commands.py

  • Committer: John Arbash Meinel
  • Date: 2009-03-06 20:42:40 UTC
  • mto: This revision was merged to the branch mainline in revision 4088.
  • Revision ID: john@arbash-meinel.com-20090306204240-mzjavv31z3gu1x7i
Fix a small bug in setup.py when an extension fails to build

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python
 
1
# Copyright (C) 2006 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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.
6
20
and for applying a changeset.
7
21
"""
8
22
 
9
 
from bzrlib.commands import Command, register_command, OPTIONS
10
 
 
11
 
class cmd_send_changeset(Command):
12
 
    """Send a bundled up changset via mail.
13
 
 
14
 
    If no revision has been specified, the last commited change will
15
 
    be sent.
16
 
 
17
 
    Subject of the mail can be specified by the --message option,
18
 
    otherwise information from the changeset log will be used.
19
 
 
20
 
    A editor will be spawned where the user may enter a description
21
 
    of the changeset.  The description can be read from a file with
22
 
    the --file FILE option.
23
 
    """
24
 
    takes_options = ['revision', 'message', 'file']
25
 
    takes_args = ['to?']
26
 
 
27
 
    def run(self, to=None, message=None, revision=None, file=None):
28
 
        from bzrlib import find_branch
29
 
        from bzrlib.errors import BzrCommandError
30
 
        from send_changeset import send_changeset
31
 
        
32
 
        if isinstance(revision, (list, tuple)):
33
 
            if len(revision) > 1:
34
 
                raise BzrCommandError('We do not support rollup-changesets yet.')
35
 
            revision = revision[0]
36
 
 
37
 
        b = find_branch('.')
38
 
 
39
 
        if not to:
40
 
            try:
41
 
                to = b.controlfile('x-send-address', 'rb').read().strip('\n')
42
 
            except:
43
 
                raise BzrCommandError('destination address is not known')
44
 
 
45
 
        if not isinstance(revision, (list, tuple)):
46
 
            revision = [revision]
47
 
 
48
 
        send_changeset(b, revision, to, message, file)
49
 
 
50
 
class cmd_changeset(Command):
51
 
    """Generate a bundled up changeset.
52
 
 
53
 
    This changeset contains all of the meta-information of a
54
 
    diff, rather than just containing the patch information.
55
 
 
56
 
    BASE - This is the target tree with which you want to merge.
57
 
           It will be used as the base for all patches. Anyone
58
 
           wanting to merge the changeset will be required to have BASE
59
 
    TARGET - This is the final revision which is desired to be in the
60
 
             changeset. It defaults to the last committed revision. './@'
61
 
    STARTING-REV-ID - All revisions between STARTING-REV and TARGET will
62
 
                      be bundled up in the changeset. By default this is
63
 
                      chosen as the merge root.
64
 
                      (NOT Implemented yet)
65
 
 
66
 
 
67
 
    If --verbose, renames will be given as an 'add + delete' style patch.
68
 
    """
 
23
import sys
 
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
 
39
from bzrlib.option import Option
 
40
from bzrlib.trace import note
 
41
 
 
42
 
 
43
class cmd_bundle_info(Command):
 
44
    """Output interesting stats about a bundle"""
 
45
 
 
46
    hidden = True
 
47
    takes_args = ['location']
69
48
    takes_options = ['verbose']
70
 
    takes_args = ['base?', 'target?', 'starting-rev-id?']
71
 
    aliases = ['cset']
72
 
 
73
 
    def run(self, base=None, target=None, starting_rev_id=None, verbose=False):
74
 
        from bzrlib.branch import find_branch
75
 
        from bzrlib.commands import parse_spec
76
 
        from bzrlib.errors import BzrCommandError
77
 
        from bzrlib import user_encoding
78
 
        import gen_changeset
79
 
        import sys
80
 
        import codecs
81
 
 
82
 
        if target is None:
83
 
            target = './@'
84
 
        b_target_path, target_revno = parse_spec(target)
85
 
        target_branch = find_branch(b_target_path)
86
 
        if target_revno is None or target_revno == -1:
87
 
            target_rev_id = target_branch.last_patch()
88
 
        else:
89
 
            target_rev_id = target_branch.lookup_revision(target_revno)
90
 
 
91
 
        if base is None:
92
 
            base_branch = target_branch
93
 
            target_rev = target_branch.get_revision(target_rev_id)
94
 
            base_rev_id = target_rev.parents[0].revision_id
95
 
        else:
96
 
            base_path, base_revno = parse_spec(base)
97
 
            base_branch = find_branch(base_path)
98
 
            if base_revno is None or base_revno == -1:
99
 
                base_rev_id = base_branch.last_patch()
100
 
            else:
101
 
                base_rev_id = base_branch.lookup_revision(base_revno)
102
 
 
103
 
        # outf = codecs.getwriter(user_encoding)(sys.stdout,
104
 
        #         errors='replace')
105
 
 
106
 
        if starting_rev_id is not None:
107
 
            raise BzrCommandError('Specifying the STARTING-REV-ID'
108
 
                    ' not yet supported')
109
 
 
110
 
        gen_changeset.show_changeset(base_branch, base_rev_id,
111
 
                target_branch, target_rev_id,
112
 
                starting_rev_id,
113
 
                to_file=sys.stdout, include_full_diff=verbose)
114
 
 
115
 
class cmd_verify_changeset(Command):
116
 
    """Read a written changeset, and make sure it is valid.
117
 
 
118
 
    """
119
 
    takes_args = ['filename?']
120
 
 
121
 
    def run(self, filename=None):
122
 
        import sys
123
 
        from read_changeset import read_changeset
124
 
        from bzrlib.branch import find_branch
125
 
        from bzrlib.xml import pack_xml
126
 
 
127
 
        b = find_branch('.')
128
 
 
129
 
        if filename is None or filename == '-':
130
 
            f = sys.stdin
131
 
        else:
132
 
            f = open(filename, 'U')
133
 
 
134
 
        cset_info, cset_tree = read_changeset(f, b)
135
 
        print cset_info
136
 
        print cset_tree
137
 
        pack_xml(cset_tree.inventory, sys.stdout)
138
 
 
139
 
 
140
 
 
141
 
class cmd_apply_changeset(Command):
142
 
    """Read in the given changeset, and apply it to the
143
 
    current tree.
144
 
 
145
 
    """
146
 
    takes_args = ['filename?']
147
 
    takes_options = ['reverse', 'auto-commit']
148
 
 
149
 
    def run(self, filename=None, reverse=False, auto_commit=False):
150
 
        from bzrlib.branch import find_branch
151
 
        import sys
152
 
        import apply_changeset
153
 
 
154
 
        b = find_branch('.') # Make sure we are in a branch
155
 
        if filename is None or filename == '-':
156
 
            f = sys.stdin
157
 
        else:
158
 
            # Actually, we should not use Universal newlines
159
 
            # as this potentially modifies the patch.
160
 
            # though it seems mailers save attachments with their
161
 
            # own format of the files.
162
 
            f = open(filename, 'rb')
163
 
 
164
 
        apply_changeset.apply_changeset(b, f, reverse=reverse,
165
 
                auto_commit=auto_commit)
166
 
 
167
 
register_command(cmd_changeset)
168
 
register_command(cmd_verify_changeset)
169
 
register_command(cmd_apply_changeset)
170
 
register_command(cmd_send_changeset)
171
 
 
172
 
OPTIONS['reverse'] = None
173
 
OPTIONS['auto-commit'] = None
174
 
 
175
 
def test_suite():
176
 
    from doctest import DocTestSuite
177
 
    from unittest import TestSuite, TestLoader
178
 
    import testchangeset
179
 
    import common
180
 
 
181
 
    suite = TestSuite()
182
 
 
183
 
    suite.addTest(TestLoader().loadTestsFromModule(testchangeset))
184
 
    suite.addTest(DocTestSuite(common))
185
 
 
186
 
    return suite
187
 
 
188
 
 
 
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')