~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/shelf.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2010-09-01 08:02:42 UTC
  • mfrom: (5390.3.3 faster-revert-593560)
  • Revision ID: pqm@pqm.ubuntu.com-20100901080242-esg62ody4frwmy66
(spiv) Avoid repeatedly calling self.target.all_file_ids() in
 InterTree.iter_changes. (Andrew Bennetts)

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
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
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
 
18
18
import errno
19
19
import re
20
20
 
21
21
from bzrlib import (
 
22
    bencode,
22
23
    errors,
23
24
    merge,
24
25
    merge3,
25
 
    osutils,
26
26
    pack,
27
27
    transform,
28
 
    ui,
29
 
    workingtree,
30
28
)
31
 
from bzrlib.util import bencode
32
29
 
33
30
 
34
31
class ShelfCreator(object):
37
34
    def __init__(self, work_tree, target_tree, file_list=None):
38
35
        """Constructor.
39
36
 
40
 
        :param work_tree: The working tree to apply changes to
 
37
        :param work_tree: The working tree to apply changes to. This is not
 
38
            required to be locked - a tree_write lock will be taken out.
41
39
        :param target_tree: The tree to make the working tree more similar to.
 
40
            This is not required to be locked - a read_lock will be taken out.
42
41
        :param file_list: The files to make more similar to the target.
43
42
        """
44
43
        self.work_tree = work_tree
45
44
        self.work_transform = transform.TreeTransform(work_tree)
46
 
        self.target_tree = target_tree
47
 
        self.shelf_transform = transform.TransformPreview(self.target_tree)
48
 
        self.renames = {}
49
 
        self.creation = {}
50
 
        self.deletion = {}
51
 
        self.iter_changes = work_tree.iter_changes(self.target_tree,
52
 
                                                   specific_files=file_list)
 
45
        try:
 
46
            self.target_tree = target_tree
 
47
            self.shelf_transform = transform.TransformPreview(self.target_tree)
 
48
            try:
 
49
                self.renames = {}
 
50
                self.creation = {}
 
51
                self.deletion = {}
 
52
                self.iter_changes = work_tree.iter_changes(
 
53
                    self.target_tree, specific_files=file_list)
 
54
            except:
 
55
                self.shelf_transform.finalize()
 
56
                raise
 
57
        except:
 
58
            self.work_transform.finalize()
 
59
            raise
53
60
 
54
61
    def iter_shelvable(self):
55
62
        """Iterable of tuples describing shelvable changes.
61
68
           ('rename', file_id, target_path, work_path)
62
69
           ('change kind', file_id, target_kind, work_kind, target_path)
63
70
           ('modify text', file_id)
 
71
           ('modify target', file_id, target_target, work_target)
64
72
        """
65
73
        for (file_id, paths, changed, versioned, parents, names, kind,
66
74
             executable) in self.iter_changes:
 
75
            # don't shelve add of tree root.  Working tree should never
 
76
            # lack roots, and bzr misbehaves when they do.
 
77
            # FIXME ADHB (2009-08-09): should still shelve adds of tree roots
 
78
            # when a tree root was deleted / renamed.
 
79
            if kind[0] is None and names[1] == '':
 
80
                continue
67
81
            if kind[0] is None or versioned[0] == False:
68
82
                self.creation[file_id] = (kind[1], names[1], parents[1],
69
83
                                          versioned)
79
93
 
80
94
                if kind[0] != kind [1]:
81
95
                    yield ('change kind', file_id, kind[0], kind[1], paths[0])
 
96
                elif kind[0] == 'symlink':
 
97
                    t_target = self.target_tree.get_symlink_target(file_id)
 
98
                    w_target = self.work_tree.get_symlink_target(file_id)
 
99
                    yield ('modify target', file_id, paths[0], t_target,
 
100
                            w_target)
82
101
                elif changed:
83
102
                    yield ('modify text', file_id)
84
103
 
 
104
    def shelve_change(self, change):
 
105
        """Shelve a change in the iter_shelvable format."""
 
106
        if change[0] == 'rename':
 
107
            self.shelve_rename(change[1])
 
108
        elif change[0] == 'delete file':
 
109
            self.shelve_deletion(change[1])
 
110
        elif change[0] == 'add file':
 
111
            self.shelve_creation(change[1])
 
112
        elif change[0] in ('change kind', 'modify text'):
 
113
            self.shelve_content_change(change[1])
 
114
        elif change[0] == 'modify target':
 
115
            self.shelve_modify_target(change[1])
 
116
        else:
 
117
            raise ValueError('Unknown change kind: "%s"' % change[0])
 
118
 
 
119
    def shelve_all(self):
 
120
        """Shelve all changes."""
 
121
        for change in self.iter_shelvable():
 
122
            self.shelve_change(change)
 
123
 
85
124
    def shelve_rename(self, file_id):
86
125
        """Shelve a file rename.
87
126
 
96
135
        shelf_parent = self.shelf_transform.trans_id_file_id(parents[1])
97
136
        self.shelf_transform.adjust_path(names[1], shelf_parent, s_trans_id)
98
137
 
 
138
    def shelve_modify_target(self, file_id):
 
139
        """Shelve a change of symlink target.
 
140
 
 
141
        :param file_id: The file id of the symlink which changed target.
 
142
        :param new_target: The target that the symlink should have due
 
143
            to shelving.
 
144
        """
 
145
        new_target = self.target_tree.get_symlink_target(file_id)
 
146
        w_trans_id = self.work_transform.trans_id_file_id(file_id)
 
147
        self.work_transform.delete_contents(w_trans_id)
 
148
        self.work_transform.create_symlink(new_target, w_trans_id)
 
149
 
 
150
        old_target = self.work_tree.get_symlink_target(file_id)
 
151
        s_trans_id = self.shelf_transform.trans_id_file_id(file_id)
 
152
        self.shelf_transform.delete_contents(s_trans_id)
 
153
        self.shelf_transform.create_symlink(old_target, s_trans_id)
 
154
 
99
155
    def shelve_lines(self, file_id, new_lines):
100
156
        """Shelve text changes to a file, using provided lines.
101
157
 
264
320
        tt.deserialize(records)
265
321
        return klass(tree, base_tree, tt, metadata.get('message'))
266
322
 
267
 
    def make_merger(self):
 
323
    def make_merger(self, task=None):
268
324
        """Return a merger that can unshelve the changes."""
269
 
        pb = ui.ui_factory.nested_progress_bar()
270
 
        try:
271
 
            target_tree = self.transform.get_preview_tree()
272
 
            merger = merge.Merger.from_uncommitted(self.tree, target_tree, pb,
273
 
                                                   self.base_tree)
274
 
            merger.merge_type = merge.Merge3Merger
275
 
            return merger
276
 
        finally:
277
 
            pb.finished()
 
325
        target_tree = self.transform.get_preview_tree()
 
326
        merger = merge.Merger.from_uncommitted(self.tree, target_tree,
 
327
            task, self.base_tree)
 
328
        merger.merge_type = merge.Merge3Merger
 
329
        return merger
278
330
 
279
331
    def finalize(self):
280
332
        """Release all resources held by this Unshelver."""