/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: Gustav Hartvigsson
  • Date: 2021-01-09 21:36:27 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20210109213627-h1xwcutzy9m7a99b
Added 'Case Preserving Working Tree Use Cases' from Canonical Wiki

* Addod a page from the Canonical Bazaar wiki
  with information on the scmeatics of case
  perserving filesystems an a case insensitive
  filesystem works.
  
  * Needs re-work, but this will do as it is the
    same inforamoton as what was on the linked
    page in the currint documentation.

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