70
by mbp at sourcefrog
Prepare for smart recursive add. |
1 |
# Copyright (C) 2005 Canonical Ltd
|
1
by mbp at sourcefrog
import from baz patch-364 |
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 |
"""Tree classes, representing directory at point in time.
|
|
18 |
"""
|
|
19 |
||
20 |
from sets import Set |
|
21 |
import os.path, os, fnmatch |
|
22 |
||
475
by Martin Pool
- rewrite diff using compare_trees() |
23 |
from osutils import pumpfile, filesize, quotefn, sha_file, \ |
157
by mbp at sourcefrog
fix test case breakage |
24 |
joinpath, splitpath, appendpath, isdir, isfile, file_kind, fingerprint_file |
25 |
import errno |
|
26 |
from stat import S_ISREG, S_ISDIR, ST_MODE, ST_SIZE |
|
27 |
||
1
by mbp at sourcefrog
import from baz patch-364 |
28 |
from inventory import Inventory |
29 |
from trace import mutter, note |
|
30 |
from errors import bailout |
|
31 |
import branch |
|
32 |
||
33 |
import bzrlib |
|
34 |
||
35 |
class Tree: |
|
36 |
"""Abstract file tree.
|
|
37 |
||
38 |
There are several subclasses:
|
|
39 |
|
|
40 |
* `WorkingTree` exists as files on disk editable by the user.
|
|
41 |
||
42 |
* `RevisionTree` is a tree as recorded at some point in the past.
|
|
43 |
||
44 |
* `EmptyTree`
|
|
45 |
||
46 |
Trees contain an `Inventory` object, and also know how to retrieve
|
|
47 |
file texts mentioned in the inventory, either from a working
|
|
48 |
directory or from a store.
|
|
49 |
||
50 |
It is possible for trees to contain files that are not described
|
|
51 |
in their inventory or vice versa; for this use `filenames()`.
|
|
52 |
||
53 |
Trees can be compared, etc, regardless of whether they are working
|
|
54 |
trees or versioned trees.
|
|
55 |
"""
|
|
56 |
||
57 |
def has_filename(self, filename): |
|
58 |
"""True if the tree has given filename."""
|
|
59 |
raise NotImplementedError() |
|
60 |
||
61 |
def has_id(self, file_id): |
|
62 |
return self.inventory.has_id(file_id) |
|
63 |
||
462
by Martin Pool
- New form 'file_id in tree' to check if the file is present |
64 |
__contains__ = has_id |
65 |
||
1
by mbp at sourcefrog
import from baz patch-364 |
66 |
def id_set(self): |
67 |
"""Return set of all ids in this tree."""
|
|
68 |
return self.inventory.id_set() |
|
69 |
||
462
by Martin Pool
- New form 'file_id in tree' to check if the file is present |
70 |
def __iter__(self): |
71 |
return iter(self.inventory) |
|
72 |
||
1
by mbp at sourcefrog
import from baz patch-364 |
73 |
def id2path(self, file_id): |
74 |
return self.inventory.id2path(file_id) |
|
75 |
||
76 |
def _get_inventory(self): |
|
77 |
return self._inventory |
|
78 |
||
79 |
inventory = property(_get_inventory, |
|
80 |
doc="Inventory of this Tree") |
|
81 |
||
82 |
def _check_retrieved(self, ie, f): |
|
130
by mbp at sourcefrog
- fixup checks on retrieved files to cope with compression, |
83 |
fp = fingerprint_file(f) |
84 |
f.seek(0) |
|
85 |
||
184
by mbp at sourcefrog
pychecker fixups |
86 |
if ie.text_size != None: |
131
by mbp at sourcefrog
check size and sha1 of files retrieved from the tree |
87 |
if ie.text_size != fp['size']: |
1
by mbp at sourcefrog
import from baz patch-364 |
88 |
bailout("mismatched size for file %r in %r" % (ie.file_id, self._store), |
89 |
["inventory expects %d bytes" % ie.text_size, |
|
130
by mbp at sourcefrog
- fixup checks on retrieved files to cope with compression, |
90 |
"file is actually %d bytes" % fp['size'], |
1
by mbp at sourcefrog
import from baz patch-364 |
91 |
"store is probably damaged/corrupt"]) |
92 |
||
130
by mbp at sourcefrog
- fixup checks on retrieved files to cope with compression, |
93 |
if ie.text_sha1 != fp['sha1']: |
1
by mbp at sourcefrog
import from baz patch-364 |
94 |
bailout("wrong SHA-1 for file %r in %r" % (ie.file_id, self._store), |
95 |
["inventory expects %s" % ie.text_sha1, |
|
130
by mbp at sourcefrog
- fixup checks on retrieved files to cope with compression, |
96 |
"file is actually %s" % fp['sha1'], |
1
by mbp at sourcefrog
import from baz patch-364 |
97 |
"store is probably damaged/corrupt"]) |
98 |
||
99 |
||
176
by mbp at sourcefrog
New cat command contributed by janmar. |
100 |
def print_file(self, fileid): |
101 |
"""Print file with id `fileid` to stdout."""
|
|
102 |
import sys |
|
103 |
pumpfile(self.get_file(fileid), sys.stdout) |
|
104 |
||
105 |
||
106 |
def export(self, dest): |
|
1
by mbp at sourcefrog
import from baz patch-364 |
107 |
"""Export this tree to a new directory.
|
108 |
||
109 |
`dest` should not exist, and will be created holding the
|
|
110 |
contents of this tree.
|
|
111 |
||
254
by Martin Pool
- Doc cleanups from Magnus Therning |
112 |
TODO: To handle subdirectories we need to create the
|
1
by mbp at sourcefrog
import from baz patch-364 |
113 |
directories first.
|
114 |
||
115 |
:note: If the export fails, the destination directory will be
|
|
116 |
left in a half-assed state.
|
|
117 |
"""
|
|
118 |
os.mkdir(dest) |
|
119 |
mutter('export version %r' % self) |
|
120 |
inv = self.inventory |
|
121 |
for dp, ie in inv.iter_entries(): |
|
122 |
kind = ie.kind |
|
123 |
fullpath = appendpath(dest, dp) |
|
124 |
if kind == 'directory': |
|
125 |
os.mkdir(fullpath) |
|
126 |
elif kind == 'file': |
|
127 |
pumpfile(self.get_file(ie.file_id), file(fullpath, 'wb')) |
|
128 |
else: |
|
453
by Martin Pool
- Split WorkingTree into its own file |
129 |
bailout("don't know how to export {%s} of kind %r" % (ie.file_id, kind)) |
1
by mbp at sourcefrog
import from baz patch-364 |
130 |
mutter(" export {%s} kind %s to %s" % (ie.file_id, kind, fullpath)) |
131 |
||
132 |
||
133 |
||
134 |
class RevisionTree(Tree): |
|
135 |
"""Tree viewing a previous revision.
|
|
136 |
||
137 |
File text can be retrieved from the text store.
|
|
138 |
||
254
by Martin Pool
- Doc cleanups from Magnus Therning |
139 |
TODO: Some kind of `__repr__` method, but a good one
|
1
by mbp at sourcefrog
import from baz patch-364 |
140 |
probably means knowing the branch and revision number,
|
141 |
or at least passing a description to the constructor.
|
|
142 |
"""
|
|
143 |
||
144 |
def __init__(self, store, inv): |
|
145 |
self._store = store |
|
146 |
self._inventory = inv |
|
147 |
||
148 |
def get_file(self, file_id): |
|
149 |
ie = self._inventory[file_id] |
|
150 |
f = self._store[ie.text_id] |
|
151 |
mutter(" get fileid{%s} from %r" % (file_id, self)) |
|
131
by mbp at sourcefrog
check size and sha1 of files retrieved from the tree |
152 |
self._check_retrieved(ie, f) |
1
by mbp at sourcefrog
import from baz patch-364 |
153 |
return f |
154 |
||
155 |
def get_file_size(self, file_id): |
|
156 |
return self._inventory[file_id].text_size |
|
157 |
||
158 |
def get_file_sha1(self, file_id): |
|
159 |
ie = self._inventory[file_id] |
|
160 |
return ie.text_sha1 |
|
161 |
||
162 |
def has_filename(self, filename): |
|
163 |
return bool(self.inventory.path2id(filename)) |
|
164 |
||
165 |
def list_files(self): |
|
166 |
# The only files returned by this are those from the version
|
|
167 |
for path, entry in self.inventory.iter_entries(): |
|
168 |
yield path, 'V', entry.kind, entry.file_id |
|
169 |
||
170 |
||
171 |
class EmptyTree(Tree): |
|
172 |
def __init__(self): |
|
173 |
self._inventory = Inventory() |
|
174 |
||
175 |
def has_filename(self, filename): |
|
176 |
return False |
|
177 |
||
178 |
def list_files(self): |
|
179 |
if False: # just to make it a generator |
|
180 |
yield None |
|
181 |
||
182 |
||
183 |
||
184 |
######################################################################
|
|
185 |
# diff
|
|
186 |
||
187 |
# TODO: Merge these two functions into a single one that can operate
|
|
188 |
# on either a whole tree or a set of files.
|
|
189 |
||
190 |
# TODO: Return the diff in order by filename, not by category or in
|
|
191 |
# random order. Can probably be done by lock-stepping through the
|
|
192 |
# filenames from both trees.
|
|
193 |
||
194 |
||
195 |
def file_status(filename, old_tree, new_tree): |
|
196 |
"""Return single-letter status, old and new names for a file.
|
|
197 |
||
198 |
The complexity here is in deciding how to represent renames;
|
|
199 |
many complex cases are possible.
|
|
200 |
"""
|
|
201 |
old_inv = old_tree.inventory |
|
202 |
new_inv = new_tree.inventory |
|
203 |
new_id = new_inv.path2id(filename) |
|
204 |
old_id = old_inv.path2id(filename) |
|
205 |
||
206 |
if not new_id and not old_id: |
|
207 |
# easy: doesn't exist in either; not versioned at all
|
|
208 |
if new_tree.is_ignored(filename): |
|
209 |
return 'I', None, None |
|
210 |
else: |
|
211 |
return '?', None, None |
|
212 |
elif new_id: |
|
213 |
# There is now a file of this name, great.
|
|
214 |
pass
|
|
215 |
else: |
|
216 |
# There is no longer a file of this name, but we can describe
|
|
217 |
# what happened to the file that used to have
|
|
218 |
# this name. There are two possibilities: either it was
|
|
219 |
# deleted entirely, or renamed.
|
|
220 |
assert old_id |
|
221 |
if new_inv.has_id(old_id): |
|
222 |
return 'X', old_inv.id2path(old_id), new_inv.id2path(old_id) |
|
223 |
else: |
|
224 |
return 'D', old_inv.id2path(old_id), None |
|
225 |
||
226 |
# if the file_id is new in this revision, it is added
|
|
227 |
if new_id and not old_inv.has_id(new_id): |
|
228 |
return 'A' |
|
229 |
||
230 |
# if there used to be a file of this name, but that ID has now
|
|
231 |
# disappeared, it is deleted
|
|
232 |
if old_id and not new_inv.has_id(old_id): |
|
233 |
return 'D' |
|
234 |
||
235 |
return 'wtf?' |
|
236 |
||
237 |
||
238 |
||
164
by mbp at sourcefrog
new 'renames' command |
239 |
def find_renames(old_inv, new_inv): |
240 |
for file_id in old_inv: |
|
241 |
if file_id not in new_inv: |
|
242 |
continue
|
|
243 |
old_name = old_inv.id2path(file_id) |
|
244 |
new_name = new_inv.id2path(file_id) |
|
245 |
if old_name != new_name: |
|
246 |
yield (old_name, new_name) |
|
247 |