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

  • Committer: Jelmer Vernooij
  • Date: 2017-06-10 16:40:42 UTC
  • mfrom: (6653.6.7 rename-controldir)
  • mto: This revision was merged to the branch mainline in revision 6690.
  • Revision ID: jelmer@jelmer.uk-20170610164042-zrxqgy2htyduvke2
MergeĀ rename-controldirĀ branch.

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 bzrlib import (
20
 
    bzrdir,
 
19
from __future__ import absolute_import
 
20
 
 
21
from . import (
 
22
    controldir,
21
23
    commit,
22
24
    errors,
23
25
    memorytree,
 
26
    revision,
 
27
    )
 
28
from .sixish import (
 
29
    viewitems,
24
30
    )
25
31
 
26
32
 
36
42
 
37
43
    For instance:
38
44
 
39
 
    >>> from bzrlib.transport.memory import MemoryTransport
 
45
    >>> from breezy.transport.memory import MemoryTransport
40
46
    >>> builder = BranchBuilder(MemoryTransport("memory:///"))
41
47
    >>> builder.start_series()
42
48
    >>> builder.build_snapshot('rev-id', None, [
63
69
            If the path of the transport does not exist but its parent does
64
70
            it will be created.
65
71
        :param format: Either a BzrDirFormat, or the name of a format in the
66
 
            bzrdir format registry for the branch to be built.
 
72
            controldir format registry for the branch to be built.
67
73
        :param branch: An already constructed branch to use.  This param is
68
74
            mutually exclusive with the transport and format params.
69
75
        """
81
87
            if format is None:
82
88
                format = 'default'
83
89
            if isinstance(format, str):
84
 
                format = bzrdir.format_registry.make_bzrdir(format)
85
 
            self._branch = bzrdir.BzrDir.create_branch_convenience(
 
90
                format = controldir.format_registry.make_controldir(format)
 
91
            self._branch = controldir.ControlDir.create_branch_convenience(
86
92
                transport.base, format=format, force_new_tree=False)
87
93
        self._tree = None
88
94
 
89
 
    def build_commit(self, **commit_kwargs):
 
95
    def build_commit(self, parent_ids=None, allow_leftmost_as_ghost=False,
 
96
                     **commit_kwargs):
90
97
        """Build a commit on the branch.
91
98
 
92
99
        This makes a commit with no real file content for when you only want
95
102
        :param commit_kwargs: Arguments to pass through to commit, such as
96
103
             timestamp.
97
104
        """
 
105
        if parent_ids is not None:
 
106
            if len(parent_ids) == 0:
 
107
                base_id = revision.NULL_REVISION
 
108
            else:
 
109
                base_id = parent_ids[0]
 
110
            if base_id != self._branch.last_revision():
 
111
                self._move_branch_pointer(base_id,
 
112
                    allow_leftmost_as_ghost=allow_leftmost_as_ghost)
98
113
        tree = memorytree.MemoryTree.create_on_branch(self._branch)
99
114
        tree.lock_write()
100
115
        try:
 
116
            if parent_ids is not None:
 
117
                tree.set_parent_ids(parent_ids,
 
118
                    allow_leftmost_as_ghost=allow_leftmost_as_ghost)
101
119
            tree.add('')
102
120
            return self._do_commit(tree, **commit_kwargs)
103
121
        finally:
165
183
        committer=None, timezone=None, message_callback=None):
166
184
        """Build a commit, shaped in a specific way.
167
185
 
 
186
        Most of the actions are self-explanatory.  'flush' is special action to
 
187
        break a series of actions into discrete steps so that complex changes
 
188
        (such as unversioning a file-id and re-adding it with a different kind)
 
189
        can be expressed in a way that will clearly work.
 
190
 
168
191
        :param revision_id: The handle for the new commit, can be None
169
192
        :param parent_ids: A list of parent_ids to use for the commit.
170
193
            It can be None, which indicates to use the last commit.
173
196
            ('modify', ('file-id', 'new-content'))
174
197
            ('unversion', 'file-id')
175
198
            ('rename', ('orig-path', 'new-path'))
 
199
            ('flush', None)
176
200
        :param message: An optional commit message, if not supplied, a default
177
201
            commit message will be written.
178
202
        :param message_callback: A message callback to use for the commit, as
186
210
        :return: The revision_id of the new commit
187
211
        """
188
212
        if parent_ids is not None:
189
 
            base_id = parent_ids[0]
 
213
            if len(parent_ids) == 0:
 
214
                base_id = revision.NULL_REVISION
 
215
            else:
 
216
                base_id = parent_ids[0]
190
217
            if base_id != self._branch.last_revision():
191
218
                self._move_branch_pointer(base_id,
192
219
                    allow_leftmost_as_ghost=allow_leftmost_as_ghost)
204
231
            # inventory entry. And the only public function to create a
205
232
            # directory is MemoryTree.mkdir() which creates the directory, but
206
233
            # also always adds it. So we have to use a multi-pass setup.
207
 
            to_add_directories = []
208
 
            to_add_files = []
209
 
            to_add_file_ids = []
210
 
            to_add_kinds = []
211
 
            new_contents = {}
212
 
            to_unversion_ids = []
213
 
            to_rename = []
 
234
            pending = _PendingActions()
214
235
            for action, info in actions:
215
236
                if action == 'add':
216
237
                    path, file_id, kind, content = info
217
238
                    if kind == 'directory':
218
 
                        to_add_directories.append((path, file_id))
 
239
                        pending.to_add_directories.append((path, file_id))
219
240
                    else:
220
 
                        to_add_files.append(path)
221
 
                        to_add_file_ids.append(file_id)
222
 
                        to_add_kinds.append(kind)
 
241
                        pending.to_add_files.append(path)
 
242
                        pending.to_add_file_ids.append(file_id)
 
243
                        pending.to_add_kinds.append(kind)
223
244
                        if content is not None:
224
 
                            new_contents[file_id] = content
 
245
                            pending.new_contents[file_id] = content
225
246
                elif action == 'modify':
226
247
                    file_id, content = info
227
 
                    new_contents[file_id] = content
 
248
                    pending.new_contents[file_id] = content
228
249
                elif action == 'unversion':
229
 
                    to_unversion_ids.append(info)
 
250
                    pending.to_unversion_ids.add(info)
230
251
                elif action == 'rename':
231
252
                    from_relpath, to_relpath = info
232
 
                    to_rename.append((from_relpath, to_relpath))
 
253
                    pending.to_rename.append((from_relpath, to_relpath))
 
254
                elif action == 'flush':
 
255
                    self._flush_pending(tree, pending)
 
256
                    pending = _PendingActions()
233
257
                else:
234
258
                    raise ValueError('Unknown build action: "%s"' % (action,))
235
 
            if to_unversion_ids:
236
 
                tree.unversion(to_unversion_ids)
237
 
            for path, file_id in to_add_directories:
238
 
                if path == '':
239
 
                    # Special case, because the path already exists
240
 
                    tree.add([path], [file_id], ['directory'])
241
 
                else:
242
 
                    tree.mkdir(path, file_id)
243
 
            for from_relpath, to_relpath in to_rename:
244
 
                tree.rename_one(from_relpath, to_relpath)
245
 
            tree.add(to_add_files, to_add_file_ids, to_add_kinds)
246
 
            for file_id, content in new_contents.iteritems():
247
 
                tree.put_file_bytes_non_atomic(file_id, content)
 
259
            self._flush_pending(tree, pending)
248
260
            return self._do_commit(tree, message=message, rev_id=revision_id,
249
261
                timestamp=timestamp, timezone=timezone, committer=committer,
250
262
                message_callback=message_callback)
251
263
        finally:
252
264
            tree.unlock()
253
265
 
 
266
    def _flush_pending(self, tree, pending):
 
267
        """Flush the pending actions in 'pending', i.e. apply them to 'tree'."""
 
268
        for path, file_id in pending.to_add_directories:
 
269
            if path == '':
 
270
                old_id = tree.path2id(path)
 
271
                if old_id is not None and old_id in pending.to_unversion_ids:
 
272
                    # We're overwriting this path, no need to unversion
 
273
                    pending.to_unversion_ids.discard(old_id)
 
274
                # Special case, because the path already exists
 
275
                tree.add([path], [file_id], ['directory'])
 
276
            else:
 
277
                tree.mkdir(path, file_id)
 
278
        for from_relpath, to_relpath in pending.to_rename:
 
279
            tree.rename_one(from_relpath, to_relpath)
 
280
        if pending.to_unversion_ids:
 
281
            tree.unversion(pending.to_unversion_ids)
 
282
        tree.add(pending.to_add_files, pending.to_add_file_ids, pending.to_add_kinds)
 
283
        for file_id, content in viewitems(pending.new_contents):
 
284
            tree.put_file_bytes_non_atomic(file_id, content)
 
285
 
254
286
    def get_branch(self):
255
287
        """Return the branch created by the builder."""
256
288
        return self._branch
 
289
 
 
290
 
 
291
class _PendingActions(object):
 
292
    """Pending actions for build_snapshot to take.
 
293
 
 
294
    This is just a simple class to hold a bunch of the intermediate state of
 
295
    build_snapshot in single object.
 
296
    """
 
297
 
 
298
    def __init__(self):
 
299
        self.to_add_directories = []
 
300
        self.to_add_files = []
 
301
        self.to_add_file_ids = []
 
302
        self.to_add_kinds = []
 
303
        self.new_contents = {}
 
304
        self.to_unversion_ids = set()
 
305
        self.to_rename = []
 
306