~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/changeset/commands.py

  • Committer: Aaron Bentley
  • Date: 2006-05-12 21:03:40 UTC
  • mto: (1185.82.108 w-changeset)
  • mto: This revision was merged to the branch mainline in revision 1738.
  • Revision ID: abentley@panoramicfeedback.com-20060512210340-233f3e6769b9ffed
Switch the fetcher

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
"""\
 
3
This is an attempt to take the internal delta object, and represent
 
4
it as a single-file text-only changeset.
 
5
This should have commands for both generating a changeset,
 
6
and for applying a changeset.
 
7
"""
 
8
 
 
9
import sys
 
10
from bzrlib.commands import Command, register_command
 
11
from bzrlib.branch import Branch
 
12
from bzrlib.revisionspec import RevisionSpec
 
13
from bzrlib.option import Option
 
14
from bzrlib.revision import (common_ancestor, MultipleRevisionSources,
 
15
                             get_intervening_revisions, NULL_REVISION)
 
16
import bzrlib.errors as errors
 
17
 
 
18
class cmd_send_changeset(Command):
 
19
    """Send a bundled up changset via mail.
 
20
 
 
21
    If no revision has been specified, the last commited change will
 
22
    be sent.
 
23
 
 
24
    Subject of the mail can be specified by the --message option,
 
25
    otherwise information from the changeset log will be used.
 
26
 
 
27
    A editor will be spawned where the user may enter a description
 
28
    of the changeset.  The description can be read from a file with
 
29
    the --file FILE option.
 
30
    """
 
31
    takes_options = ['revision', 'message', 'file']
 
32
    takes_args = ['to?']
 
33
 
 
34
    def run(self, to=None, message=None, revision=None, file=None):
 
35
        from bzrlib.errors import BzrCommandError
 
36
        from send_changeset import send_changeset
 
37
        
 
38
        if isinstance(revision, (list, tuple)):
 
39
            if len(revision) > 1:
 
40
                raise BzrCommandError('We do not support rollup-changesets yet.')
 
41
            revision = revision[0]
 
42
 
 
43
        b = Branch.open_containing('.')
 
44
 
 
45
        if not to:
 
46
            try:
 
47
                to = b.controlfile('x-send-address', 'rb').read().strip('\n')
 
48
            except:
 
49
                raise BzrCommandError('destination address is not known')
 
50
 
 
51
        if not isinstance(revision, (list, tuple)):
 
52
            revision = [revision]
 
53
 
 
54
        send_changeset(b, revision, to, message, file)
 
55
 
 
56
 
 
57
class cmd_changeset(Command):
 
58
    """Generate a bundled up changeset.
 
59
 
 
60
    This changeset contains all of the meta-information of a
 
61
    diff, rather than just containing the patch information.
 
62
 
 
63
    bzr cset
 
64
        - Changeset for the last commit
 
65
    bzr cset BASE
 
66
        - Changeset to apply the current tree into BASE
 
67
    bzr cset --revision A
 
68
        - Changeset for revision A
 
69
    bzr cset --revision A..B
 
70
        - Changeset to transform A into B
 
71
    bzr cset --revision A..B BASE
 
72
        - Changeset to transform revision A of BASE into revision B
 
73
          of the local tree
 
74
    """
 
75
    takes_options = ['verbose', 'revision']
 
76
    takes_args = ['base?']
 
77
    aliases = ['cset']
 
78
 
 
79
    def run(self, base=None, revision=None):
 
80
        from bzrlib import user_encoding
 
81
        from bzrlib.changeset.serializer import write
 
82
 
 
83
        if base is None:
 
84
            base_branch = None
 
85
        else:
 
86
            base_branch = Branch.open(base)
 
87
 
 
88
        # We don't want to lock the same branch across
 
89
        # 2 different branches
 
90
        target_branch = Branch.open_containing(u'.')[0]
 
91
        if base_branch is not None and target_branch.base == base_branch.base:
 
92
            base_branch = None
 
93
 
 
94
        base_revision = None
 
95
        if revision is None:
 
96
            target_revision = target_branch.last_revision()
 
97
            if base_branch is not None:
 
98
                base_revision = base_branch.last_revision()
 
99
        elif len(revision) == 1:
 
100
            target_revision = revision[0].in_history(target_branch).rev_id
 
101
            if base_branch is not None:
 
102
                base_revision = base_branch.last_revision()
 
103
        elif len(revision) == 2:
 
104
            target_revision = revision[0].in_history(target_branch).rev_id
 
105
            if base_branch is not None:
 
106
                base_revision = revision[1].in_history(base_branch).rev_id
 
107
            else:
 
108
                base_revision = revision[1].in_history(target_branch).rev_id
 
109
        else:
 
110
            raise errors.BzrCommandError('--revision takes 1 or 2 parameters')
 
111
 
 
112
        if base_revision is None:
 
113
            rev = target_branch.repository.get_revision(target_revision)
 
114
            if rev.parent_ids:
 
115
                base_revision = rev.parent_ids[0]
 
116
            else:
 
117
                base_revision = NULL_REVISION
 
118
 
 
119
        if base_branch is not None:
 
120
            target_branch.repository.fetch(base_branch.repository, 
 
121
                                           revision_id=base_revision)
 
122
            del base_branch
 
123
        revision_id_list = get_intervening_revisions(base_revision,
 
124
                                                     target_revision,
 
125
                                                     target_branch.repository,
 
126
                                             target_branch.revision_history())
 
127
                
 
128
        write(target_branch.repository, revision_id_list, sys.stdout)
 
129
 
 
130
 
 
131
class cmd_verify_changeset(Command):
 
132
    """Read a written changeset, and make sure it is valid.
 
133
 
 
134
    """
 
135
    takes_args = ['filename?']
 
136
 
 
137
    def run(self, filename=None):
 
138
        from read_changeset import read_changeset
 
139
        #from bzrlib.xml import serializer_v4
 
140
 
 
141
        b, relpath = Branch.open_containing('.')
 
142
 
 
143
        if filename is None or filename == '-':
 
144
            f = sys.stdin
 
145
        else:
 
146
            f = open(filename, 'U')
 
147
 
 
148
        cset_info, cset_tree = read_changeset(f, b)
 
149
        # print cset_info
 
150
        # print cset_tree
 
151
        #serializer_v4.write(cset_tree.inventory, sys.stdout)
 
152
 
 
153
 
 
154
 
 
155
class cmd_apply_changeset(Command):
 
156
    """Read in the given changeset, and apply it to the
 
157
    current tree.
 
158
 
 
159
    """
 
160
    takes_args = ['filename?']
 
161
    takes_options = [Option('reverse'), Option('auto-commit')]
 
162
 
 
163
    def run(self, filename=None, reverse=False, auto_commit=False):
 
164
        import apply_changeset
 
165
 
 
166
        b, relpath = Branch.open_containing('.') # Make sure we are in a branch
 
167
        if filename is None or filename == '-':
 
168
            f = sys.stdin
 
169
        else:
 
170
            # Actually, we should not use Universal newlines
 
171
            # as this potentially modifies the patch.
 
172
            # though it seems mailers save attachments with their
 
173
            # own format of the files.
 
174
            f = open(filename, 'rb')
 
175
 
 
176
        apply_changeset.apply_changeset(b, f, reverse=reverse,
 
177
                auto_commit=auto_commit)
 
178
 
 
179
 
 
180
register_command(cmd_changeset)
 
181
register_command(cmd_verify_changeset)
 
182
register_command(cmd_apply_changeset)
 
183
register_command(cmd_send_changeset)
 
184
 
 
185
#OPTIONS['reverse'] = None
 
186
#OPTIONS['auto-commit'] = None
 
187
 
 
188
def test_suite():
 
189
    from doctest import DocTestSuite
 
190
    from unittest import TestSuite, TestLoader
 
191
    import test_changeset
 
192
    import common
 
193
    import patches
 
194
 
 
195
    suite = TestSuite()
 
196
 
 
197
    suite.addTest(TestLoader().loadTestsFromModule(test_changeset))
 
198
    suite.addTest(TestLoader().loadTestsFromModule(patches))
 
199
    suite.addTest(DocTestSuite(common))
 
200
 
 
201
    return suite
 
202
 
 
203