1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
# Copyright (C) 2008 Aaron Bentley <aaron@aaronbentley.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
from cStringIO import StringIO
from bzrlib import merge3, pack, transform
from bzrlib.plugins.shelf2 import serialize_transform
class ShelfCreator(object):
def __init__(self, work_tree, target_tree):
self.work_tree = work_tree
self.work_transform = transform.TreeTransform(work_tree)
self.target_tree = target_tree
self.shelf_transform = transform.TransformPreview(self.target_tree)
self.renames = {}
self.creation = {}
self.iter_changes = work_tree.iter_changes(self.target_tree)
def __iter__(self):
for (file_id, paths, changed, versioned, parents, names, kind,
executable) in self.iter_changes:
if kind[0] is None or versioned[0] == False:
self.creation[file_id] = (kind[1], names[1], parents[1])
yield ('add file', file_id, kind[1])
else:
if names[0] != names[1] or parents[0] != parents[1]:
self.renames[file_id] = (names, parents)
yield ('rename', file_id) + paths
if changed:
yield ('modify text', file_id)
def shelve_rename(self, file_id):
names, parents = self.renames[file_id]
w_trans_id = self.work_transform.trans_id_file_id(file_id)
work_parent = self.work_transform.trans_id_file_id(parents[0])
self.work_transform.adjust_path(names[0], work_parent, w_trans_id)
s_trans_id = self.shelf_transform.trans_id_file_id(file_id)
shelf_parent = self.shelf_transform.trans_id_file_id(parents[1])
self.shelf_transform.adjust_path(names[1], shelf_parent, s_trans_id)
def shelve_text(self, file_id, new_text):
s = StringIO()
s.writelines(new_text)
s.seek(0)
new_lines = s.readlines()
w_trans_id = self.work_transform.trans_id_file_id(file_id)
self.work_transform.delete_contents(w_trans_id)
self.work_transform.create_file(new_lines, w_trans_id)
s_trans_id = self.shelf_transform.trans_id_file_id(file_id)
self.shelf_transform.delete_contents(s_trans_id)
inverse_lines = self._inverse_lines(new_lines, file_id)
self.shelf_transform.create_file(inverse_lines, s_trans_id)
def shelve_creation(self, file_id):
kind, name, parent = self.creation[file_id]
w_trans_id = self.work_transform.trans_id_file_id(file_id)
if parent is not None:
self.work_transform.delete_contents(w_trans_id)
self.work_transform.unversion_file(w_trans_id)
s_trans_id = self.shelf_transform.trans_id_file_id(file_id)
if parent is not None:
s_parent_id = self.shelf_transform.trans_id_file_id(parent)
self.shelf_transform.adjust_path(name, s_parent_id, s_trans_id)
if kind == 'file':
lines = self.read_tree_lines(file_id)
self.shelf_transform.create_file(lines, s_trans_id)
if kind == 'directory':
self.shelf_transform.create_directory(s_trans_id)
if kind == 'symlink':
target = self.work_tree.get_symlink_target(file_id)
self.shelf_transform.create_symlink(target, s_trans_id)
self.shelf_transform.version_file(file_id, s_trans_id)
def read_tree_lines(self, file_id):
tree_file = self.work_tree.get_file(file_id)
try:
return tree_file.readlines()
finally:
tree_file.close()
def _inverse_lines(self, new_lines, file_id):
"""Produce a version with only those changes removed from new_lines."""
target_lines = self.target_tree.get_file_lines(file_id)
work_lines = self.read_tree_lines(file_id)
return merge3.Merge3(new_lines, target_lines, work_lines).merge_lines()
def finalize(self):
self.work_transform.finalize()
self.shelf_transform.finalize()
def transform(self):
self.work_transform.apply()
def make_shelf_filename(self):
transport = self.work_tree.bzrdir.root_transport.clone('.shelf2')
transport.ensure_base()
return transport.local_abspath('01')
def write_shelf(self):
filename = self.make_shelf_filename()
shelf_file = open(filename, 'wb')
try:
serializer = pack.ContainerSerialiser()
shelf_file.write(serializer.begin())
for bytes in serialize_transform.serialize(
self.shelf_transform, serializer):
shelf_file.write(bytes)
shelf_file.write(serializer.end())
finally:
shelf_file.close()
return filename
|