/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
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:
0.12.14 by Aaron Bentley
Add shelving of created files
37
            if kind[0] is None or versioned[0] == False:
38
                yield ('add file', file_id, kind[1])
39
            else:
40
                if names[0] != names[1] or parents[0] != parents[1]:
41
                    self.renames[file_id] = (names, parents)
42
                    yield ('rename', file_id) + paths
43
                if changed:
44
                    yield ('modify text', file_id)
0.12.12 by Aaron Bentley
Implement shelf creator
45
46
    def shelve_rename(self, file_id):
47
        names, parents = self.renames[file_id]
48
        w_trans_id = self.work_transform.trans_id_file_id(file_id)
49
        work_parent = self.work_transform.trans_id_file_id(parents[0])
50
        self.work_transform.adjust_path(names[0], work_parent, w_trans_id)
51
52
        s_trans_id = self.shelf_transform.trans_id_file_id(file_id)
53
        shelf_parent = self.shelf_transform.trans_id_file_id(parents[1])
54
        self.shelf_transform.adjust_path(names[1], shelf_parent, s_trans_id)
55
0.12.13 by Aaron Bentley
Implement shelving content
56
    def shelve_text(self, file_id, new_text):
57
        s = StringIO()
58
        s.writelines(new_text)
59
        s.seek(0)
60
        new_lines = s.readlines()
61
        w_trans_id = self.work_transform.trans_id_file_id(file_id)
62
        self.work_transform.delete_contents(w_trans_id)
63
        self.work_transform.create_file(new_lines, w_trans_id)
64
65
        s_trans_id = self.shelf_transform.trans_id_file_id(file_id)
66
        self.shelf_transform.delete_contents(s_trans_id)
67
        inverse_lines = self._inverse_lines(new_lines, file_id)
68
        self.shelf_transform.create_file(inverse_lines, s_trans_id)
69
0.12.14 by Aaron Bentley
Add shelving of created files
70
    def shelve_creation(self, file_id, kind):
71
        w_trans_id = self.work_transform.trans_id_file_id(file_id)
72
        self.work_transform.delete_contents(w_trans_id)
0.12.15 by Aaron Bentley
Handle file-id when shelving creation
73
        self.work_transform.unversion_file(w_trans_id)
0.12.14 by Aaron Bentley
Add shelving of created files
74
75
        s_trans_id = self.shelf_transform.trans_id_file_id(file_id)
76
        if kind == 'file':
77
            lines = self.read_tree_lines(file_id)
78
            self.shelf_transform.create_file(lines, s_trans_id)
0.12.16 by Aaron Bentley
Handle shelving directory creation
79
        if kind == 'directory':
80
            self.shelf_transform.create_directory(s_trans_id)
0.12.17 by Aaron Bentley
Handle creating symlinks
81
        if kind == 'symlink':
82
            target = self.work_tree.get_symlink_target(file_id)
83
            self.shelf_transform.create_symlink(target, s_trans_id)
0.12.15 by Aaron Bentley
Handle file-id when shelving creation
84
        self.shelf_transform.version_file(file_id, s_trans_id)
0.12.14 by Aaron Bentley
Add shelving of created files
85
86
    def read_tree_lines(self, file_id):
0.12.13 by Aaron Bentley
Implement shelving content
87
        tree_file = self.work_tree.get_file(file_id)
88
        try:
0.12.14 by Aaron Bentley
Add shelving of created files
89
            return tree_file.readlines()
0.12.13 by Aaron Bentley
Implement shelving content
90
        finally:
91
            tree_file.close()
0.12.14 by Aaron Bentley
Add shelving of created files
92
93
    def _inverse_lines(self, new_lines, file_id):
94
        """Produce a version with only those changes removed from new_lines."""
95
        base_lines = self.base_tree.get_file_lines(file_id)
96
        tree_lines = self.read_tree_lines(file_id)
0.12.13 by Aaron Bentley
Implement shelving content
97
        return merge3.Merge3(new_lines, base_lines, tree_lines).merge_lines()
98
0.12.12 by Aaron Bentley
Implement shelf creator
99
    def finalize(self):
100
        self.work_transform.finalize()
101
        self.shelf_transform.finalize()
0.12.13 by Aaron Bentley
Implement shelving content
102
103
    def transform(self):
104
        self.work_transform.apply()