/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: 2018-02-18 21:42:57 UTC
  • mto: This revision was merged to the branch mainline in revision 6859.
  • Revision ID: jelmer@jelmer.uk-20180218214257-jpevutp1wa30tz3v
Update TODO to reference Breezy, not Bazaar.

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