/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-06-14 17:59:16 UTC
  • mto: This revision was merged to the branch mainline in revision 7065.
  • Revision ID: jelmer@jelmer.uk-20180614175916-a2e2xh5k533guq1x
Move breezy.plugins.git to breezy.git.

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):
41
47
    >>> builder.start_series()
42
48
    >>> builder.build_snapshot(None, [
43
49
    ...     ('add', ('', b'root-id', 'directory', '')),
44
 
    ...     ('add', ('filename', b'f-id', 'file', b'content\n'))],
 
50
    ...     ('add', ('filename', b'f-id', 'file', 'content\n'))],
45
51
    ...     revision_id=b'rev-id')
46
 
    b'rev-id'
47
 
    >>> builder.build_snapshot([b'rev-id'],
48
 
    ...     [('modify', ('filename', b'new-content\n'))],
 
52
    'rev-id'
 
53
    >>> builder.build_snapshot(['rev-id'],
 
54
    ...     [('modify', (b'f-id', 'new-content\n'))],
49
55
    ...     revision_id=b'rev2-id')
50
 
    b'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)
 
113
                self._move_branch_pointer(base_id,
 
114
                    allow_leftmost_as_ghost=allow_leftmost_as_ghost)
109
115
        tree = self._branch.create_memorytree()
110
116
        with tree.lock_write():
111
117
            if parent_ids is not None:
112
 
                tree.set_parent_ids(
113
 
                    parent_ids,
 
118
                tree.set_parent_ids(parent_ids,
114
119
                    allow_leftmost_as_ghost=allow_leftmost_as_ghost)
115
120
            tree.add('')
116
121
            return self._do_commit(tree, **commit_kwargs)
120
125
        if message is None and message_callback is None:
121
126
            message = u'commit %d' % (self._branch.revno() + 1,)
122
127
        return tree.commit(message, message_callback=message_callback,
123
 
                           reporter=reporter, **kwargs)
 
128
            reporter=reporter,
 
129
            **kwargs)
124
130
 
125
131
    def _move_branch_pointer(self, new_revision_id,
126
 
                             allow_leftmost_as_ghost=False):
 
132
        allow_leftmost_as_ghost=False):
127
133
        """Point self._branch to a different revision id."""
128
134
        with self._branch.lock_write():
129
135
            # We don't seem to have a simple set_last_revision(), so we
131
137
            cur_revno, cur_revision_id = self._branch.last_revision_info()
132
138
            try:
133
139
                g = self._branch.repository.get_graph()
134
 
                new_revno = g.find_distance_to_null(
135
 
                    new_revision_id, [(cur_revision_id, cur_revno)])
 
140
                new_revno = g.find_distance_to_null(new_revision_id,
 
141
                    [(cur_revision_id, cur_revno)])
136
142
                self._branch.set_last_revision_info(new_revno, new_revision_id)
137
143
            except errors.GhostRevisionsHaveNoRevno:
138
144
                if not allow_leftmost_as_ghost:
169
175
        self._tree = None
170
176
 
171
177
    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
            allow_leftmost_as_ghost=False, committer=None, timezone=None,
 
179
            message_callback=None, revision_id=None):
174
180
        """Build a commit, shaped in a specific way.
175
181
 
176
182
        Most of the actions are self-explanatory.  'flush' is special action to
181
187
        :param parent_ids: A list of parent_ids to use for the commit.
182
188
            It can be None, which indicates to use the last commit.
183
189
        :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'))
 
190
            ('add', ('path', 'file-id', 'kind', 'content' or None))
 
191
            ('modify', ('path', 'new-content'))
186
192
            ('unversion', 'path')
187
193
            ('rename', ('orig-path', 'new-path'))
188
194
            ('flush', None)
205
211
            else:
206
212
                base_id = parent_ids[0]
207
213
            if base_id != self._branch.last_revision():
208
 
                self._move_branch_pointer(
209
 
                    base_id, allow_leftmost_as_ghost=allow_leftmost_as_ghost)
 
214
                self._move_branch_pointer(base_id,
 
215
                    allow_leftmost_as_ghost=allow_leftmost_as_ghost)
210
216
 
211
217
        if self._tree is not None:
212
218
            tree = self._tree
214
220
            tree = self._branch.create_memorytree()
215
221
        with tree.lock_write():
216
222
            if parent_ids is not None:
217
 
                tree.set_parent_ids(
218
 
                    parent_ids,
 
223
                tree.set_parent_ids(parent_ids,
219
224
                    allow_leftmost_as_ghost=allow_leftmost_as_ghost)
220
225
            # Unfortunately, MemoryTree.add(directory) just creates an
221
226
            # inventory entry. And the only public function to create a
247
252
                else:
248
253
                    raise ValueError('Unknown build action: "%s"' % (action,))
249
254
            self._flush_pending(tree, pending)
250
 
            return self._do_commit(
251
 
                tree, message=message, rev_id=revision_id,
 
255
            return self._do_commit(tree, message=message, rev_id=revision_id,
252
256
                timestamp=timestamp, timezone=timezone, committer=committer,
253
257
                message_callback=message_callback)
254
258
 
255
259
    def _flush_pending(self, tree, pending):
256
 
        """Flush the pending actions in 'pending', i.e. apply them to tree."""
 
260
        """Flush the pending actions in 'pending', i.e. apply them to 'tree'."""
257
261
        for path, file_id in pending.to_add_directories:
258
262
            if path == '':
259
 
                if tree.has_filename(path) \
260
 
                        and path in pending.to_unversion_paths:
 
263
                if tree.has_filename(path) and path in pending.to_unversion_paths:
261
264
                    # We're overwriting this path, no need to unversion
262
265
                    pending.to_unversion_paths.discard(path)
263
266
                # Special case, because the path already exists
268
271
            tree.rename_one(from_relpath, to_relpath)
269
272
        if pending.to_unversion_paths:
270
273
            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.add(pending.to_add_files, pending.to_add_file_ids, pending.to_add_kinds)
 
275
        for path, content in viewitems(pending.new_contents):
274
276
            tree.put_file_bytes_non_atomic(path, content)
275
277
 
276
278
    def get_branch(self):
293
295
        self.new_contents = {}
294
296
        self.to_unversion_paths = set()
295
297
        self.to_rename = []
 
298