~bzr-pqm/bzr/bzr.dev

0.12.12 by Aaron Bentley
Implement shelf creator
1
# Copyright (C) 2008 Aaron Bentley <aaron@aaronbentley.com>
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
0.12.13 by Aaron Bentley
Implement shelving content
18
from cStringIO import StringIO
19
20
from bzrlib import merge3
0.12.12 by Aaron Bentley
Implement shelf creator
21
from bzrlib import transform
22
23
24
class ShelfCreator(object):
25
26
    def __init__(self, work_tree):
27
        self.work_tree = work_tree
28
        self.work_transform = transform.TreeTransform(work_tree)
29
        self.base_tree = work_tree.basis_tree()
30
        self.shelf_transform = transform.TransformPreview(self.base_tree)
31
        self.renames = {}
32
        self.iter_changes = work_tree.iter_changes(self.base_tree)
33
34
    def __iter__(self):
35
        for (file_id, paths, changed, versioned, parents, names, kind,
36
             executable) in self.iter_changes:
37
            if names[0] != names[1] or parents[0] != parents[1]:
38
                self.renames[file_id] = (names, parents)
39
                yield ('rename', file_id) + paths
0.12.13 by Aaron Bentley
Implement shelving content
40
            if changed:
41
                yield ('modify text', file_id)
0.12.12 by Aaron Bentley
Implement shelf creator
42
43
    def shelve_rename(self, file_id):
44
        names, parents = self.renames[file_id]
45
        w_trans_id = self.work_transform.trans_id_file_id(file_id)
46
        work_parent = self.work_transform.trans_id_file_id(parents[0])
47
        self.work_transform.adjust_path(names[0], work_parent, w_trans_id)
48
49
        s_trans_id = self.shelf_transform.trans_id_file_id(file_id)
50
        shelf_parent = self.shelf_transform.trans_id_file_id(parents[1])
51
        self.shelf_transform.adjust_path(names[1], shelf_parent, s_trans_id)
52
0.12.13 by Aaron Bentley
Implement shelving content
53
    def shelve_text(self, file_id, new_text):
54
        s = StringIO()
55
        s.writelines(new_text)
56
        s.seek(0)
57
        new_lines = s.readlines()
58
        w_trans_id = self.work_transform.trans_id_file_id(file_id)
59
        self.work_transform.delete_contents(w_trans_id)
60
        self.work_transform.create_file(new_lines, w_trans_id)
61
62
        s_trans_id = self.shelf_transform.trans_id_file_id(file_id)
63
        self.shelf_transform.delete_contents(s_trans_id)
64
        inverse_lines = self._inverse_lines(new_lines, file_id)
65
        self.shelf_transform.create_file(inverse_lines, s_trans_id)
66
67
    def _inverse_lines(self, new_lines, file_id):
68
        """Produce a version with only those changes removed from new_lines."""
69
        base_lines = self.base_tree.get_file_lines(file_id)
70
        tree_file = self.work_tree.get_file(file_id)
71
        try:
72
            tree_lines = tree_file.readlines()
73
        finally:
74
            tree_file.close()
75
        return merge3.Merge3(new_lines, base_lines, tree_lines).merge_lines()
76
0.12.12 by Aaron Bentley
Implement shelf creator
77
    def finalize(self):
78
        self.work_transform.finalize()
79
        self.shelf_transform.finalize()
0.12.13 by Aaron Bentley
Implement shelving content
80
81
    def transform(self):
82
        self.work_transform.apply()