/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: Breezy landing bot
  • Author(s): Colin Watson
  • Date: 2020-11-16 21:47:08 UTC
  • mfrom: (7521.1.1 remove-lp-workaround)
  • Revision ID: breezy.the.bot@gmail.com-20201116214708-jos209mgxi41oy15
Remove breezy.git workaround for bazaar.launchpad.net.

Merged from https://code.launchpad.net/~cjwatson/brz/remove-lp-workaround/+merge/393710

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