1
# Copyright (C) 2005 Canonical Ltd
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.
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.
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
17
"""Tree classes, representing directory at point in time.
21
import os.path, os, fnmatch
23
from osutils import pumpfile, compare_files, filesize, quotefn, sha_file, \
24
joinpath, splitpath, appendpath, isdir, isfile, file_kind, fingerprint_file
26
from stat import S_ISREG, S_ISDIR, ST_MODE, ST_SIZE
28
from inventory import Inventory
29
from trace import mutter, note
30
from errors import bailout
36
"""Abstract file tree.
38
There are several subclasses:
40
* `WorkingTree` exists as files on disk editable by the user.
42
* `RevisionTree` is a tree as recorded at some point in the past.
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.
50
It is possible for trees to contain files that are not described
51
in their inventory or vice versa; for this use `filenames()`.
53
Trees can be compared, etc, regardless of whether they are working
54
trees or versioned trees.
57
def has_filename(self, filename):
58
"""True if the tree has given filename."""
59
raise NotImplementedError()
61
def has_id(self, file_id):
62
return self.inventory.has_id(file_id)
65
"""Return set of all ids in this tree."""
66
return self.inventory.id_set()
68
def id2path(self, file_id):
69
return self.inventory.id2path(file_id)
71
def _get_inventory(self):
72
return self._inventory
74
inventory = property(_get_inventory,
75
doc="Inventory of this Tree")
77
def _check_retrieved(self, ie, f):
78
fp = fingerprint_file(f)
81
if ie.text_size != None:
82
if ie.text_size != fp['size']:
83
bailout("mismatched size for file %r in %r" % (ie.file_id, self._store),
84
["inventory expects %d bytes" % ie.text_size,
85
"file is actually %d bytes" % fp['size'],
86
"store is probably damaged/corrupt"])
88
if ie.text_sha1 != fp['sha1']:
89
bailout("wrong SHA-1 for file %r in %r" % (ie.file_id, self._store),
90
["inventory expects %s" % ie.text_sha1,
91
"file is actually %s" % fp['sha1'],
92
"store is probably damaged/corrupt"])
95
def print_file(self, fileid):
96
"""Print file with id `fileid` to stdout."""
98
pumpfile(self.get_file(fileid), sys.stdout)
101
def export(self, dest):
102
"""Export this tree to a new directory.
104
`dest` should not exist, and will be created holding the
105
contents of this tree.
107
TODO: To handle subdirectories we need to create the
110
:note: If the export fails, the destination directory will be
111
left in a half-assed state.
114
mutter('export version %r' % self)
116
for dp, ie in inv.iter_entries():
118
fullpath = appendpath(dest, dp)
119
if kind == 'directory':
122
pumpfile(self.get_file(ie.file_id), file(fullpath, 'wb'))
124
bailout("don't know how to export {%s} of kind %r" % (ie.file_id, kind))
125
mutter(" export {%s} kind %s to %s" % (ie.file_id, kind, fullpath))
129
class RevisionTree(Tree):
130
"""Tree viewing a previous revision.
132
File text can be retrieved from the text store.
134
TODO: Some kind of `__repr__` method, but a good one
135
probably means knowing the branch and revision number,
136
or at least passing a description to the constructor.
139
def __init__(self, store, inv):
141
self._inventory = inv
143
def get_file(self, file_id):
144
ie = self._inventory[file_id]
145
f = self._store[ie.text_id]
146
mutter(" get fileid{%s} from %r" % (file_id, self))
147
self._check_retrieved(ie, f)
150
def get_file_size(self, file_id):
151
return self._inventory[file_id].text_size
153
def get_file_sha1(self, file_id):
154
ie = self._inventory[file_id]
157
def has_filename(self, filename):
158
return bool(self.inventory.path2id(filename))
160
def list_files(self):
161
# The only files returned by this are those from the version
162
for path, entry in self.inventory.iter_entries():
163
yield path, 'V', entry.kind, entry.file_id
166
class EmptyTree(Tree):
168
self._inventory = Inventory()
170
def has_filename(self, filename):
173
def list_files(self):
174
if False: # just to make it a generator
179
######################################################################
182
# TODO: Merge these two functions into a single one that can operate
183
# on either a whole tree or a set of files.
185
# TODO: Return the diff in order by filename, not by category or in
186
# random order. Can probably be done by lock-stepping through the
187
# filenames from both trees.
190
def file_status(filename, old_tree, new_tree):
191
"""Return single-letter status, old and new names for a file.
193
The complexity here is in deciding how to represent renames;
194
many complex cases are possible.
196
old_inv = old_tree.inventory
197
new_inv = new_tree.inventory
198
new_id = new_inv.path2id(filename)
199
old_id = old_inv.path2id(filename)
201
if not new_id and not old_id:
202
# easy: doesn't exist in either; not versioned at all
203
if new_tree.is_ignored(filename):
204
return 'I', None, None
206
return '?', None, None
208
# There is now a file of this name, great.
211
# There is no longer a file of this name, but we can describe
212
# what happened to the file that used to have
213
# this name. There are two possibilities: either it was
214
# deleted entirely, or renamed.
216
if new_inv.has_id(old_id):
217
return 'X', old_inv.id2path(old_id), new_inv.id2path(old_id)
219
return 'D', old_inv.id2path(old_id), None
221
# if the file_id is new in this revision, it is added
222
if new_id and not old_inv.has_id(new_id):
225
# if there used to be a file of this name, but that ID has now
226
# disappeared, it is deleted
227
if old_id and not new_inv.has_id(old_id):
234
def find_renames(old_inv, new_inv):
235
for file_id in old_inv:
236
if file_id not in new_inv:
238
old_name = old_inv.id2path(file_id)
239
new_name = new_inv.id2path(file_id)
240
if old_name != new_name:
241
yield (old_name, new_name)