~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/shelf.py

  • Committer: Tarmac
  • Author(s): Vincent Ladeuil
  • Date: 2017-01-30 14:42:05 UTC
  • mfrom: (6620.1.1 trunk)
  • Revision ID: tarmac-20170130144205-r8fh2xpmiuxyozpv
Merge  2.7 into trunk including fix for bug #1657238 [r=vila]

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2008 Canonical Ltd
 
1
# Copyright (C) 2008, 2009, 2010 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
 
17
from __future__ import absolute_import
17
18
 
18
19
import errno
19
20
import re
20
21
 
 
22
from bzrlib.lazy_import import lazy_import
 
23
lazy_import(globals(), """
21
24
from bzrlib import (
 
25
    bencode,
22
26
    errors,
23
27
    merge,
24
28
    merge3,
25
 
    osutils,
26
29
    pack,
27
30
    transform,
28
 
    ui,
29
 
    workingtree,
30
31
)
31
 
from bzrlib.util import bencode
 
32
""")
32
33
 
33
34
 
34
35
class ShelfCreator(object):
37
38
    def __init__(self, work_tree, target_tree, file_list=None):
38
39
        """Constructor.
39
40
 
40
 
        :param work_tree: The working tree to apply changes to
 
41
        :param work_tree: The working tree to apply changes to. This is not
 
42
            required to be locked - a tree_write lock will be taken out.
41
43
        :param target_tree: The tree to make the working tree more similar to.
 
44
            This is not required to be locked - a read_lock will be taken out.
42
45
        :param file_list: The files to make more similar to the target.
43
46
        """
44
47
        self.work_tree = work_tree
63
66
        """Iterable of tuples describing shelvable changes.
64
67
 
65
68
        As well as generating the tuples, this updates several members.
66
 
        Tuples may be:
 
69
        Tuples may be::
 
70
 
67
71
           ('add file', file_id, work_kind, work_path)
68
72
           ('delete file', file_id, target_kind, target_path)
69
73
           ('rename', file_id, target_path, work_path)
73
77
        """
74
78
        for (file_id, paths, changed, versioned, parents, names, kind,
75
79
             executable) in self.iter_changes:
 
80
            # don't shelve add of tree root.  Working tree should never
 
81
            # lack roots, and bzr misbehaves when they do.
 
82
            # FIXME ADHB (2009-08-09): should still shelve adds of tree roots
 
83
            # when a tree root was deleted / renamed.
 
84
            if kind[0] is None and names[1] == '':
 
85
                continue
 
86
            # Also don't shelve deletion of tree root.
 
87
            if kind[1] is None and names[0] == '':
 
88
                continue
76
89
            if kind[0] is None or versioned[0] == False:
77
90
                self.creation[file_id] = (kind[1], names[1], parents[1],
78
91
                                          versioned)
96
109
                elif changed:
97
110
                    yield ('modify text', file_id)
98
111
 
 
112
    def shelve_change(self, change):
 
113
        """Shelve a change in the iter_shelvable format."""
 
114
        if change[0] == 'rename':
 
115
            self.shelve_rename(change[1])
 
116
        elif change[0] == 'delete file':
 
117
            self.shelve_deletion(change[1])
 
118
        elif change[0] == 'add file':
 
119
            self.shelve_creation(change[1])
 
120
        elif change[0] in ('change kind', 'modify text'):
 
121
            self.shelve_content_change(change[1])
 
122
        elif change[0] == 'modify target':
 
123
            self.shelve_modify_target(change[1])
 
124
        else:
 
125
            raise ValueError('Unknown change kind: "%s"' % change[0])
 
126
 
 
127
    def shelve_all(self):
 
128
        """Shelve all changes.
 
129
 
 
130
        :return: True if changes were shelved, False if there were no changes.
 
131
        """
 
132
        change = None
 
133
        for change in self.iter_shelvable():
 
134
            self.shelve_change(change)
 
135
        return change is not None
 
136
 
99
137
    def shelve_rename(self, file_id):
100
138
        """Shelve a file rename.
101
139
 
222
260
        """Shelve changes from working tree."""
223
261
        self.work_transform.apply()
224
262
 
 
263
    @staticmethod
 
264
    def metadata_record(serializer, revision_id, message=None):
 
265
        metadata = {'revision_id': revision_id}
 
266
        if message is not None:
 
267
            metadata['message'] = message.encode('utf-8')
 
268
        return serializer.bytes_record(
 
269
            bencode.bencode(metadata), (('metadata',),))
 
270
 
225
271
    def write_shelf(self, shelf_file, message=None):
226
272
        """Serialize the shelved changes to a file.
227
273
 
230
276
        :return: the filename of the written file.
231
277
        """
232
278
        transform.resolve_conflicts(self.shelf_transform)
 
279
        revision_id = self.target_tree.get_revision_id()
 
280
        return self._write_shelf(shelf_file, self.shelf_transform, revision_id,
 
281
                                 message)
 
282
 
 
283
    @classmethod
 
284
    def _write_shelf(cls, shelf_file, transform, revision_id, message=None):
233
285
        serializer = pack.ContainerSerialiser()
234
286
        shelf_file.write(serializer.begin())
235
 
        metadata = {
236
 
            'revision_id': self.target_tree.get_revision_id(),
237
 
        }
238
 
        if message is not None:
239
 
            metadata['message'] = message.encode('utf-8')
240
 
        shelf_file.write(serializer.bytes_record(
241
 
            bencode.bencode(metadata), (('metadata',),)))
242
 
        for bytes in self.shelf_transform.serialize(serializer):
 
287
        metadata = cls.metadata_record(serializer, revision_id, message)
 
288
        shelf_file.write(metadata)
 
289
        for bytes in transform.serialize(serializer):
243
290
            shelf_file.write(bytes)
244
291
        shelf_file.write(serializer.end())
245
292