~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 (
22
25
    bencode,
23
26
    errors,
24
27
    merge,
25
28
    merge3,
26
 
    osutils,
27
29
    pack,
28
30
    transform,
29
 
    ui,
30
 
    workingtree,
31
31
)
 
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)
104
117
            self.shelve_deletion(change[1])
105
118
        elif change[0] == 'add file':
106
119
            self.shelve_creation(change[1])
107
 
        elif change[0] == 'change kind':
 
120
        elif change[0] in ('change kind', 'modify text'):
108
121
            self.shelve_content_change(change[1])
109
122
        elif change[0] == 'modify target':
110
123
            self.shelve_modify_target(change[1])
111
124
        else:
112
125
            raise ValueError('Unknown change kind: "%s"' % change[0])
113
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
 
114
137
    def shelve_rename(self, file_id):
115
138
        """Shelve a file rename.
116
139
 
237
260
        """Shelve changes from working tree."""
238
261
        self.work_transform.apply()
239
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
 
240
271
    def write_shelf(self, shelf_file, message=None):
241
272
        """Serialize the shelved changes to a file.
242
273
 
245
276
        :return: the filename of the written file.
246
277
        """
247
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):
248
285
        serializer = pack.ContainerSerialiser()
249
286
        shelf_file.write(serializer.begin())
250
 
        metadata = {
251
 
            'revision_id': self.target_tree.get_revision_id(),
252
 
        }
253
 
        if message is not None:
254
 
            metadata['message'] = message.encode('utf-8')
255
 
        shelf_file.write(serializer.bytes_record(
256
 
            bencode.bencode(metadata), (('metadata',),)))
257
 
        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):
258
290
            shelf_file.write(bytes)
259
291
        shelf_file.write(serializer.end())
260
292