/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: Richard Wilbur
  • Date: 2016-02-04 19:07:28 UTC
  • mto: This revision was merged to the branch mainline in revision 6618.
  • Revision ID: richard.wilbur@gmail.com-20160204190728-p0zvfii6zase0fw7
Update COPYING.txt from the original http://www.gnu.org/licenses/gpl-2.0.txt  (Only differences were in whitespace.)  Thanks to Petr Stodulka for pointing out the discrepancy.

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