/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/shelf.py

Merge serialize-transform into prepare-shelf.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2008 Canonical Ltd
 
2
#
 
3
#    This program is free software; you can redistribute it and/or modify
 
4
#    it under the terms of the GNU General Public License as published by
 
5
#    the Free Software Foundation; either version 2 of the License, or
 
6
#    (at your option) any later version.
 
7
#
 
8
#    This program is distributed in the hope that it will be useful,
 
9
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
#    GNU General Public License for more details.
 
12
#
 
13
#    You should have received a copy of the GNU General Public License
 
14
#    along with this program; if not, write to the Free Software
 
15
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
 
 
18
from bzrlib import (
 
19
    merge3,
 
20
    osutils,
 
21
    pack,
 
22
    transform,
 
23
)
 
24
 
 
25
 
 
26
class ShelfCreator(object):
 
27
    """Create a transform to shelve objects and its inverse."""
 
28
 
 
29
    def __init__(self, work_tree, target_tree, file_list=None):
 
30
        """Constructor.
 
31
 
 
32
        :param work_tree: The working tree to apply changes to
 
33
        :param target_tree: The tree to make the working tree more similar to.
 
34
        :param file_list: The files to make more similar to the target.
 
35
        """
 
36
        self.work_tree = work_tree
 
37
        self.work_transform = transform.TreeTransform(work_tree)
 
38
        self.target_tree = target_tree
 
39
        self.shelf_transform = transform.TransformPreview(self.target_tree)
 
40
        self.renames = {}
 
41
        self.creation = {}
 
42
        self.deletion = {}
 
43
        self.iter_changes = work_tree.iter_changes(self.target_tree,
 
44
                                                   specific_files=file_list)
 
45
 
 
46
    def iter_shelvable(self):
 
47
        """Iterable of tuples describing shelvable changes.
 
48
 
 
49
        As well as generating the tuples, this updates several members.
 
50
        Tuples may be:
 
51
           ('add file', file_id, work_kind, work_path)
 
52
           ('delete file', file_id, target_kind, target_path)
 
53
           ('rename', file_id, target_path, work_path)
 
54
           ('change kind', file_id, target_kind, work_kind, target_path)
 
55
           ('modify text', file_id)
 
56
        """
 
57
        for (file_id, paths, changed, versioned, parents, names, kind,
 
58
             executable) in self.iter_changes:
 
59
            if kind[0] is None or versioned[0] == False:
 
60
                self.creation[file_id] = (kind[1], names[1], parents[1],
 
61
                                          versioned)
 
62
                yield ('add file', file_id, kind[1], paths[1])
 
63
            elif kind[1] is None or versioned[0] == False:
 
64
                self.deletion[file_id] = (kind[0], names[0], parents[0],
 
65
                                          versioned)
 
66
                yield ('delete file', file_id, kind[0], paths[0])
 
67
            else:
 
68
                if names[0] != names[1] or parents[0] != parents[1]:
 
69
                    self.renames[file_id] = (names, parents)
 
70
                    yield ('rename', file_id) + paths
 
71
 
 
72
                if kind[0] != kind [1]:
 
73
                    yield ('change kind', file_id, kind[0], kind[1], paths[0])
 
74
                elif changed:
 
75
                    yield ('modify text', file_id)
 
76
 
 
77
    def shelve_rename(self, file_id):
 
78
        """Shelve a file rename.
 
79
 
 
80
        :param file_id: The file id of the file to shelve the renaming of.
 
81
        """
 
82
        names, parents = self.renames[file_id]
 
83
        w_trans_id = self.work_transform.trans_id_file_id(file_id)
 
84
        work_parent = self.work_transform.trans_id_file_id(parents[0])
 
85
        self.work_transform.adjust_path(names[0], work_parent, w_trans_id)
 
86
 
 
87
        s_trans_id = self.shelf_transform.trans_id_file_id(file_id)
 
88
        shelf_parent = self.shelf_transform.trans_id_file_id(parents[1])
 
89
        self.shelf_transform.adjust_path(names[1], shelf_parent, s_trans_id)
 
90
 
 
91
    def shelve_lines(self, file_id, new_lines):
 
92
        """Shelve text changes to a file, using provided lines.
 
93
 
 
94
        :param file_id: The file id of the file to shelve the text of.
 
95
        :param new_lines: The lines that the file should have due to shelving.
 
96
        """
 
97
        w_trans_id = self.work_transform.trans_id_file_id(file_id)
 
98
        self.work_transform.delete_contents(w_trans_id)
 
99
        self.work_transform.create_file(new_lines, w_trans_id)
 
100
 
 
101
        s_trans_id = self.shelf_transform.trans_id_file_id(file_id)
 
102
        self.shelf_transform.delete_contents(s_trans_id)
 
103
        inverse_lines = self._inverse_lines(new_lines, file_id)
 
104
        self.shelf_transform.create_file(inverse_lines, s_trans_id)
 
105
 
 
106
    @staticmethod
 
107
    def _content_from_tree(tt, tree, file_id):
 
108
        trans_id = tt.trans_id_file_id(file_id)
 
109
        tt.delete_contents(trans_id)
 
110
        transform.create_from_tree(tt, trans_id, tree, file_id)
 
111
 
 
112
    def shelve_content_change(self, file_id):
 
113
        """Shelve a kind change or binary file content change.
 
114
 
 
115
        :param file_id: The file id of the file to shelve the content change
 
116
            of.
 
117
        """
 
118
        self._content_from_tree(self.work_transform, self.target_tree, file_id)
 
119
        self._content_from_tree(self.shelf_transform, self.work_tree, file_id)
 
120
 
 
121
    def shelve_creation(self, file_id):
 
122
        """Shelve creation of a file.
 
123
 
 
124
        This handles content and inventory id.
 
125
        :param file_id: The file_id of the file to shelve creation of.
 
126
        """
 
127
        kind, name, parent, versioned = self.creation[file_id]
 
128
        version = not versioned[0]
 
129
        self._shelve_creation(self.work_tree, file_id, self.work_transform,
 
130
                              self.shelf_transform, kind, name, parent,
 
131
                              version)
 
132
 
 
133
    def shelve_deletion(self, file_id):
 
134
        """Shelve deletion of a file.
 
135
 
 
136
        This handles content and inventory id.
 
137
        :param file_id: The file_id of the file to shelve deletion of.
 
138
        """
 
139
        kind, name, parent, versioned = self.deletion[file_id]
 
140
        existing_path = self.target_tree.id2path(file_id)
 
141
        if not self.work_tree.has_filename(existing_path):
 
142
            existing_path = None
 
143
        version = not versioned[1]
 
144
        self._shelve_creation(self.target_tree, file_id, self.shelf_transform,
 
145
                              self.work_transform, kind, name, parent,
 
146
                              version, existing_path=existing_path)
 
147
 
 
148
    def _shelve_creation(self, tree, file_id, from_transform, to_transform,
 
149
                         kind, name, parent, version, existing_path=None):
 
150
        w_trans_id = from_transform.trans_id_file_id(file_id)
 
151
        if parent is not None and kind is not None:
 
152
            from_transform.delete_contents(w_trans_id)
 
153
        from_transform.unversion_file(w_trans_id)
 
154
 
 
155
        if existing_path is not None:
 
156
            s_trans_id = to_transform.trans_id_tree_path(existing_path)
 
157
        else:
 
158
            s_trans_id = to_transform.trans_id_file_id(file_id)
 
159
        if parent is not None:
 
160
            s_parent_id = to_transform.trans_id_file_id(parent)
 
161
            to_transform.adjust_path(name, s_parent_id, s_trans_id)
 
162
            if existing_path is None:
 
163
                if kind is None:
 
164
                    to_transform.create_file('', s_trans_id)
 
165
                else:
 
166
                    transform.create_from_tree(to_transform, s_trans_id,
 
167
                                               tree, file_id)
 
168
        if version:
 
169
            to_transform.version_file(file_id, s_trans_id)
 
170
 
 
171
    def read_tree_lines(self, tree, file_id):
 
172
        """Read text lines from a tree.
 
173
 
 
174
        (Tree.get_file_lines is not an official API)
 
175
        """
 
176
        return osutils.split_lines(tree.get_file_text(file_id))
 
177
 
 
178
    def _inverse_lines(self, new_lines, file_id):
 
179
        """Produce a version with only those changes removed from new_lines."""
 
180
        target_lines = self.target_tree.get_file_lines(file_id)
 
181
        work_lines = self.read_tree_lines(self.work_tree, file_id)
 
182
        return merge3.Merge3(new_lines, target_lines, work_lines).merge_lines()
 
183
 
 
184
    def finalize(self):
 
185
        """Release all resources used by this ShelfCreator."""
 
186
        self.work_transform.finalize()
 
187
        self.shelf_transform.finalize()
 
188
 
 
189
    def transform(self):
 
190
        """Shelve changes from working tree."""
 
191
        self.work_transform.apply()
 
192
 
 
193
    def make_shelf_filename(self):
 
194
        """Generate a filename for a shelf."""
 
195
        transport = self.work_tree.bzrdir.root_transport.clone('.shelf2')
 
196
        transport.ensure_base()
 
197
        return transport.local_abspath('01')
 
198
 
 
199
    def write_shelf(self):
 
200
        """Serialize the shelved changes to a file.
 
201
 
 
202
        :return: the filename of the written file.
 
203
        """
 
204
        filename = self.make_shelf_filename()
 
205
        shelf_file = open(filename, 'wb')
 
206
        try:
 
207
            serializer = pack.ContainerSerialiser()
 
208
            shelf_file.write(serializer.begin())
 
209
            for bytes in self.shelf_transform.serialize(serializer):
 
210
                shelf_file.write(bytes)
 
211
            shelf_file.write(serializer.end())
 
212
        finally:
 
213
            shelf_file.close()
 
214
        return filename