/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/branchbuilder.py

  • Committer: John Arbash Meinel
  • Date: 2011-04-22 14:12:22 UTC
  • mfrom: (5809 +trunk)
  • mto: This revision was merged to the branch mainline in revision 5836.
  • Revision ID: john@arbash-meinel.com-20110422141222-nx2j0hbkihcb8j16
Merge newer bzr.dev and resolve conflicts.
Try to write some documentation about how the _dirblock_state works.
Fix up the tests so that they pass again.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
"""Utility for create branches with particular contents."""
18
18
 
19
 
from . import (
20
 
    controldir,
 
19
from bzrlib import (
 
20
    bzrdir,
21
21
    commit,
22
22
    errors,
 
23
    memorytree,
23
24
    revision,
24
25
    )
25
26
 
36
37
 
37
38
    For instance:
38
39
 
39
 
    >>> from breezy.transport.memory import MemoryTransport
 
40
    >>> from bzrlib.transport.memory import MemoryTransport
40
41
    >>> builder = BranchBuilder(MemoryTransport("memory:///"))
41
42
    >>> builder.start_series()
42
 
    >>> builder.build_snapshot(None, [
43
 
    ...     ('add', ('', b'root-id', 'directory', '')),
44
 
    ...     ('add', ('filename', b'f-id', 'file', b'content\n'))],
45
 
    ...     revision_id=b'rev-id')
46
 
    b'rev-id'
47
 
    >>> builder.build_snapshot([b'rev-id'],
48
 
    ...     [('modify', ('filename', b'new-content\n'))],
49
 
    ...     revision_id=b'rev2-id')
50
 
    b'rev2-id'
 
43
    >>> builder.build_snapshot('rev-id', None, [
 
44
    ...     ('add', ('', 'root-id', 'directory', '')),
 
45
    ...     ('add', ('filename', 'f-id', 'file', 'content\n'))])
 
46
    'rev-id'
 
47
    >>> builder.build_snapshot('rev2-id', ['rev-id'],
 
48
    ...     [('modify', ('f-id', 'new-content\n'))])
 
49
    'rev2-id'
51
50
    >>> builder.finish_series()
52
51
    >>> branch = builder.get_branch()
53
52
 
65
64
            If the path of the transport does not exist but its parent does
66
65
            it will be created.
67
66
        :param format: Either a BzrDirFormat, or the name of a format in the
68
 
            controldir format registry for the branch to be built.
 
67
            bzrdir format registry for the branch to be built.
69
68
        :param branch: An already constructed branch to use.  This param is
70
69
            mutually exclusive with the transport and format params.
71
70
        """
83
82
            if format is None:
84
83
                format = 'default'
85
84
            if isinstance(format, str):
86
 
                format = controldir.format_registry.make_controldir(format)
87
 
            self._branch = controldir.ControlDir.create_branch_convenience(
 
85
                format = bzrdir.format_registry.make_bzrdir(format)
 
86
            self._branch = bzrdir.BzrDir.create_branch_convenience(
88
87
                transport.base, format=format, force_new_tree=False)
89
88
        self._tree = None
90
89
 
91
 
    def build_commit(self, parent_ids=None, allow_leftmost_as_ghost=False,
92
 
                     **commit_kwargs):
 
90
    def build_commit(self, **commit_kwargs):
93
91
        """Build a commit on the branch.
94
92
 
95
93
        This makes a commit with no real file content for when you only want
98
96
        :param commit_kwargs: Arguments to pass through to commit, such as
99
97
             timestamp.
100
98
        """
101
 
        if parent_ids is not None:
102
 
            if len(parent_ids) == 0:
103
 
                base_id = revision.NULL_REVISION
104
 
            else:
105
 
                base_id = parent_ids[0]
106
 
            if base_id != self._branch.last_revision():
107
 
                self._move_branch_pointer(
108
 
                    base_id, allow_leftmost_as_ghost=allow_leftmost_as_ghost)
109
 
        tree = self._branch.create_memorytree()
110
 
        with tree.lock_write():
111
 
            if parent_ids is not None:
112
 
                tree.set_parent_ids(
113
 
                    parent_ids,
114
 
                    allow_leftmost_as_ghost=allow_leftmost_as_ghost)
 
99
        tree = memorytree.MemoryTree.create_on_branch(self._branch)
 
100
        tree.lock_write()
 
101
        try:
115
102
            tree.add('')
116
103
            return self._do_commit(tree, **commit_kwargs)
 
104
        finally:
 
105
            tree.unlock()
117
106
 
118
107
    def _do_commit(self, tree, message=None, message_callback=None, **kwargs):
119
108
        reporter = commit.NullCommitReporter()
120
109
        if message is None and message_callback is None:
121
110
            message = u'commit %d' % (self._branch.revno() + 1,)
122
111
        return tree.commit(message, message_callback=message_callback,
123
 
                           reporter=reporter, **kwargs)
 
112
            reporter=reporter,
 
113
            **kwargs)
124
114
 
125
115
    def _move_branch_pointer(self, new_revision_id,
126
 
                             allow_leftmost_as_ghost=False):
 
116
        allow_leftmost_as_ghost=False):
127
117
        """Point self._branch to a different revision id."""
128
 
        with self._branch.lock_write():
 
118
        self._branch.lock_write()
 
119
        try:
129
120
            # We don't seem to have a simple set_last_revision(), so we
130
121
            # implement it here.
131
122
            cur_revno, cur_revision_id = self._branch.last_revision_info()
132
123
            try:
133
124
                g = self._branch.repository.get_graph()
134
 
                new_revno = g.find_distance_to_null(
135
 
                    new_revision_id, [(cur_revision_id, cur_revno)])
 
125
                new_revno = g.find_distance_to_null(new_revision_id,
 
126
                    [(cur_revision_id, cur_revno)])
136
127
                self._branch.set_last_revision_info(new_revno, new_revision_id)
137
128
            except errors.GhostRevisionsHaveNoRevno:
138
129
                if not allow_leftmost_as_ghost:
139
130
                    raise
140
131
                new_revno = 1
 
132
        finally:
 
133
            self._branch.unlock()
141
134
        if self._tree is not None:
142
135
            # We are currently processing a series, but when switching branch
143
136
            # pointers, it is easiest to just create a new memory tree.
145
138
            # We are cheating a little bit here, and locking the new tree
146
139
            # before the old tree is unlocked. But that way the branch stays
147
140
            # locked throughout.
148
 
            new_tree = self._branch.create_memorytree()
 
141
            new_tree = memorytree.MemoryTree.create_on_branch(self._branch)
149
142
            new_tree.lock_write()
150
143
            self._tree.unlock()
151
144
            self._tree = new_tree
160
153
        if self._tree is not None:
161
154
            raise AssertionError('You cannot start a new series while a'
162
155
                                 ' series is already going.')
163
 
        self._tree = self._branch.create_memorytree()
 
156
        self._tree = memorytree.MemoryTree.create_on_branch(self._branch)
164
157
        self._tree.lock_write()
165
158
 
166
159
    def finish_series(self):
168
161
        self._tree.unlock()
169
162
        self._tree = None
170
163
 
171
 
    def build_snapshot(self, parent_ids, actions, message=None, timestamp=None,
172
 
                       allow_leftmost_as_ghost=False, committer=None,
173
 
                       timezone=None, message_callback=None, revision_id=None):
 
164
    def build_snapshot(self, revision_id, parent_ids, actions,
 
165
        message=None, timestamp=None, allow_leftmost_as_ghost=False,
 
166
        committer=None, timezone=None, message_callback=None):
174
167
        """Build a commit, shaped in a specific way.
175
168
 
176
 
        Most of the actions are self-explanatory.  'flush' is special action to
177
 
        break a series of actions into discrete steps so that complex changes
178
 
        (such as unversioning a file-id and re-adding it with a different kind)
179
 
        can be expressed in a way that will clearly work.
180
 
 
 
169
        :param revision_id: The handle for the new commit, can be None
181
170
        :param parent_ids: A list of parent_ids to use for the commit.
182
171
            It can be None, which indicates to use the last commit.
183
172
        :param actions: A list of actions to perform. Supported actions are:
184
 
            ('add', ('path', b'file-id', 'kind', b'content' or None))
185
 
            ('modify', ('path', b'new-content'))
186
 
            ('unversion', 'path')
 
173
            ('add', ('path', 'file-id', 'kind', 'content' or None))
 
174
            ('modify', ('file-id', 'new-content'))
 
175
            ('unversion', 'file-id')
187
176
            ('rename', ('orig-path', 'new-path'))
188
 
            ('flush', None)
189
177
        :param message: An optional commit message, if not supplied, a default
190
178
            commit message will be written.
191
179
        :param message_callback: A message callback to use for the commit, as
196
184
        :param committer: An optional username to use for commit
197
185
        :param allow_leftmost_as_ghost: True if the leftmost parent should be
198
186
            permitted to be a ghost.
199
 
        :param revision_id: The handle for the new commit, can be None
200
187
        :return: The revision_id of the new commit
201
188
        """
202
189
        if parent_ids is not None:
205
192
            else:
206
193
                base_id = parent_ids[0]
207
194
            if base_id != self._branch.last_revision():
208
 
                self._move_branch_pointer(
209
 
                    base_id, allow_leftmost_as_ghost=allow_leftmost_as_ghost)
 
195
                self._move_branch_pointer(base_id,
 
196
                    allow_leftmost_as_ghost=allow_leftmost_as_ghost)
210
197
 
211
198
        if self._tree is not None:
212
199
            tree = self._tree
213
200
        else:
214
 
            tree = self._branch.create_memorytree()
215
 
        with tree.lock_write():
 
201
            tree = memorytree.MemoryTree.create_on_branch(self._branch)
 
202
        tree.lock_write()
 
203
        try:
216
204
            if parent_ids is not None:
217
 
                tree.set_parent_ids(
218
 
                    parent_ids,
 
205
                tree.set_parent_ids(parent_ids,
219
206
                    allow_leftmost_as_ghost=allow_leftmost_as_ghost)
220
207
            # Unfortunately, MemoryTree.add(directory) just creates an
221
208
            # inventory entry. And the only public function to create a
222
209
            # directory is MemoryTree.mkdir() which creates the directory, but
223
210
            # also always adds it. So we have to use a multi-pass setup.
224
 
            pending = _PendingActions()
 
211
            to_add_directories = []
 
212
            to_add_files = []
 
213
            to_add_file_ids = []
 
214
            to_add_kinds = []
 
215
            new_contents = {}
 
216
            to_unversion_ids = []
 
217
            to_rename = []
225
218
            for action, info in actions:
226
219
                if action == 'add':
227
220
                    path, file_id, kind, content = info
228
221
                    if kind == 'directory':
229
 
                        pending.to_add_directories.append((path, file_id))
 
222
                        to_add_directories.append((path, file_id))
230
223
                    else:
231
 
                        pending.to_add_files.append(path)
232
 
                        pending.to_add_file_ids.append(file_id)
233
 
                        pending.to_add_kinds.append(kind)
 
224
                        to_add_files.append(path)
 
225
                        to_add_file_ids.append(file_id)
 
226
                        to_add_kinds.append(kind)
234
227
                        if content is not None:
235
 
                            pending.new_contents[path] = content
 
228
                            new_contents[file_id] = content
236
229
                elif action == 'modify':
237
 
                    path, content = info
238
 
                    pending.new_contents[path] = content
 
230
                    file_id, content = info
 
231
                    new_contents[file_id] = content
239
232
                elif action == 'unversion':
240
 
                    pending.to_unversion_paths.add(info)
 
233
                    to_unversion_ids.append(info)
241
234
                elif action == 'rename':
242
235
                    from_relpath, to_relpath = info
243
 
                    pending.to_rename.append((from_relpath, to_relpath))
244
 
                elif action == 'flush':
245
 
                    self._flush_pending(tree, pending)
246
 
                    pending = _PendingActions()
 
236
                    to_rename.append((from_relpath, to_relpath))
247
237
                else:
248
238
                    raise ValueError('Unknown build action: "%s"' % (action,))
249
 
            self._flush_pending(tree, pending)
250
 
            return self._do_commit(
251
 
                tree, message=message, rev_id=revision_id,
 
239
            if to_unversion_ids:
 
240
                tree.unversion(to_unversion_ids)
 
241
            for path, file_id in to_add_directories:
 
242
                if path == '':
 
243
                    # Special case, because the path already exists
 
244
                    tree.add([path], [file_id], ['directory'])
 
245
                else:
 
246
                    tree.mkdir(path, file_id)
 
247
            for from_relpath, to_relpath in to_rename:
 
248
                tree.rename_one(from_relpath, to_relpath)
 
249
            tree.add(to_add_files, to_add_file_ids, to_add_kinds)
 
250
            for file_id, content in new_contents.iteritems():
 
251
                tree.put_file_bytes_non_atomic(file_id, content)
 
252
            return self._do_commit(tree, message=message, rev_id=revision_id,
252
253
                timestamp=timestamp, timezone=timezone, committer=committer,
253
254
                message_callback=message_callback)
254
 
 
255
 
    def _flush_pending(self, tree, pending):
256
 
        """Flush the pending actions in 'pending', i.e. apply them to tree."""
257
 
        for path, file_id in pending.to_add_directories:
258
 
            if path == '':
259
 
                if tree.has_filename(path) \
260
 
                        and path in pending.to_unversion_paths:
261
 
                    # We're overwriting this path, no need to unversion
262
 
                    pending.to_unversion_paths.discard(path)
263
 
                # Special case, because the path already exists
264
 
                tree.add([path], [file_id], ['directory'])
265
 
            else:
266
 
                tree.mkdir(path, file_id)
267
 
        for from_relpath, to_relpath in pending.to_rename:
268
 
            tree.rename_one(from_relpath, to_relpath)
269
 
        if pending.to_unversion_paths:
270
 
            tree.unversion(pending.to_unversion_paths)
271
 
        tree.add(pending.to_add_files, pending.to_add_file_ids,
272
 
                 pending.to_add_kinds)
273
 
        for path, content in pending.new_contents.items():
274
 
            tree.put_file_bytes_non_atomic(path, content)
 
255
        finally:
 
256
            tree.unlock()
275
257
 
276
258
    def get_branch(self):
277
259
        """Return the branch created by the builder."""
278
260
        return self._branch
279
 
 
280
 
 
281
 
class _PendingActions(object):
282
 
    """Pending actions for build_snapshot to take.
283
 
 
284
 
    This is just a simple class to hold a bunch of the intermediate state of
285
 
    build_snapshot in single object.
286
 
    """
287
 
 
288
 
    def __init__(self):
289
 
        self.to_add_directories = []
290
 
        self.to_add_files = []
291
 
        self.to_add_file_ids = []
292
 
        self.to_add_kinds = []
293
 
        self.new_contents = {}
294
 
        self.to_unversion_paths = set()
295
 
        self.to_rename = []