/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
1 by mbp at sourcefrog
import from baz patch-364
1
#! /usr/bin/env python
2
# -*- coding: UTF-8 -*-
3
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
8
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
18
"""Tree classes, representing directory at point in time.
19
"""
20
21
from sets import Set
22
import os.path, os, fnmatch
23
24
from inventory import Inventory
25
from trace import mutter, note
26
from osutils import pumpfile, compare_files, filesize, quotefn, sha_file, \
27
     joinpath, splitpath, appendpath, isdir, isfile, file_kind
28
from errors import bailout
29
import branch
30
from stat import S_ISREG, S_ISDIR, ST_MODE, ST_SIZE
31
32
import bzrlib
33
34
class Tree:
35
    """Abstract file tree.
36
37
    There are several subclasses:
38
    
39
    * `WorkingTree` exists as files on disk editable by the user.
40
41
    * `RevisionTree` is a tree as recorded at some point in the past.
42
43
    * `EmptyTree`
44
45
    Trees contain an `Inventory` object, and also know how to retrieve
46
    file texts mentioned in the inventory, either from a working
47
    directory or from a store.
48
49
    It is possible for trees to contain files that are not described
50
    in their inventory or vice versa; for this use `filenames()`.
51
52
    Trees can be compared, etc, regardless of whether they are working
53
    trees or versioned trees.
54
    """
55
    
56
    def get_file(self, file_id):
57
        """Return an open file-like object for given file id."""
58
        raise NotImplementedError()
59
60
    def has_filename(self, filename):
61
        """True if the tree has given filename."""
62
        raise NotImplementedError()
63
64
    def has_id(self, file_id):
65
        return self.inventory.has_id(file_id)
66
67
    def id_set(self):
68
        """Return set of all ids in this tree."""
69
        return self.inventory.id_set()
70
71
    def id2path(self, file_id):
72
        return self.inventory.id2path(file_id)
73
74
    def _get_inventory(self):
75
        return self._inventory
76
77
    inventory = property(_get_inventory,
78
                         doc="Inventory of this Tree")
79
80
    def _check_retrieved(self, ie, f):
81
        # TODO: Test this check by damaging the store?
82
        if ie.text_size is not None:
83
            fs = filesize(f)
84
            if fs != ie.text_size:
85
                bailout("mismatched size for file %r in %r" % (ie.file_id, self._store),
86
                        ["inventory expects %d bytes" % ie.text_size,
87
                         "file is actually %d bytes" % fs,
88
                         "store is probably damaged/corrupt"])
89
90
        f_hash = sha_file(f)
91
        f.seek(0)
92
        if ie.text_sha1 != f_hash:
93
            bailout("wrong SHA-1 for file %r in %r" % (ie.file_id, self._store),
94
                    ["inventory expects %s" % ie.text_sha1,
95
                     "file is actually %s" % f_hash,
96
                     "store is probably damaged/corrupt"])
97
98
99
    def export(self, dest):
100
        """Export this tree to a new directory.
101
102
        `dest` should not exist, and will be created holding the
103
        contents of this tree.
104
105
        :todo: To handle subdirectories we need to create the
106
               directories first.
107
108
        :note: If the export fails, the destination directory will be
109
               left in a half-assed state.
110
        """
111
        os.mkdir(dest)
112
        mutter('export version %r' % self)
113
        inv = self.inventory
114
        for dp, ie in inv.iter_entries():
115
            kind = ie.kind
116
            fullpath = appendpath(dest, dp)
117
            if kind == 'directory':
118
                os.mkdir(fullpath)
119
            elif kind == 'file':
120
                pumpfile(self.get_file(ie.file_id), file(fullpath, 'wb'))
121
            else:
122
                bailout("don't know how to export {%s} of kind %r", fid, kind)
123
            mutter("  export {%s} kind %s to %s" % (ie.file_id, kind, fullpath))
124
125
126
127
class WorkingTree(Tree):
128
    """Working copy tree.
129
130
    The inventory is held in the `Branch` working-inventory, and the
131
    files are in a directory on disk.
132
133
    It is possible for a `WorkingTree` to have a filename which is
134
    not listed in the Inventory and vice versa.
135
    """
136
    def __init__(self, basedir, inv):
137
        self._inventory = inv
138
        self.basedir = basedir
139
        self.path2id = inv.path2id
140
141
    def __repr__(self):
142
        return "<%s of %s>" % (self.__class__.__name__,
143
                               self.basedir)
144
145
    def _rel(self, filename):
146
        return os.path.join(self.basedir, filename)
147
148
    def has_filename(self, filename):
149
        return os.path.exists(self._rel(filename))
150
151
    def get_file(self, file_id):
152
        return file(self._get_store_filename(file_id), 'rb')
153
154
    def _get_store_filename(self, file_id):
155
        return self._rel(self.id2path(file_id))
156
157
    def get_file_size(self, file_id):
158
        return os.stat(self._get_store_filename(file_id))[ST_SIZE]
159
160
    def get_file_sha1(self, file_id):
161
        f = self.get_file(file_id)
162
        return sha_file(f)
163
164
165
    def file_class(self, filename):
166
        if self.path2id(filename):
167
            return 'V'
168
        elif self.is_ignored(filename):
169
            return 'I'
170
        else:
171
            return '?'
172
173
174
    def file_kind(self, filename):
175
        if isfile(self._rel(filename)):
176
            return 'file'
177
        elif isdir(self._rel(filename)):
178
            return 'directory'
179
        else:
180
            return 'unknown'
181
182
183
    def list_files(self):
184
        """Recursively list all files as (path, class, kind, id).
185
186
        Lists, but does not descend into unversioned directories.
187
188
        This does not include files that have been deleted in this
189
        tree.
190
191
        Skips the control directory.
192
        """
193
        inv = self.inventory
194
195
        def descend(from_dir, from_dir_id, dp):
196
            ls = os.listdir(dp)
197
            ls.sort()
198
            for f in ls:
199
                if bzrlib.BZRDIR == f:
200
                    continue
201
202
                # path within tree
203
                fp = appendpath(from_dir, f)
204
205
                # absolute path
206
                fap = appendpath(dp, f)
207
                
208
                f_ie = inv.get_child(from_dir_id, f)
209
                if f_ie:
210
                    c = 'V'
211
                elif self.is_ignored(fp):
212
                    c = 'I'
213
                else:
214
                    c = '?'
215
216
                fk = file_kind(fap)
217
218
                if f_ie:
219
                    if f_ie.kind != fk:
220
                        bailout("file %r entered as kind %r id %r, now of kind %r"
221
                                % (fap, f_ie.kind, f_ie.file_id, fk))
222
223
                yield fp, c, fk, (f_ie and f_ie.file_id)
224
225
                if fk != 'directory':
226
                    continue
227
228
                if c != 'V':
229
                    # don't descend unversioned directories
230
                    continue
231
                
232
                for ff in descend(fp, f_ie.file_id, fap):
233
                    yield ff
234
235
        for f in descend('', None, self.basedir):
236
            yield f
237
            
238
239
240
    def unknowns(self, path='', dir_id=None):
241
        """Yield names of unknown files in this WorkingTree.
242
243
        If there are any unknown directories then only the directory is
244
        returned, not all its children.  But if there are unknown files
245
        under a versioned subdirectory, they are returned.
246
247
        Currently returned depth-first, sorted by name within directories.
248
        """
249
        for fpath, fclass, fkind, fid in self.list_files():
250
            if fclass == '?':
251
                yield fpath
252
                
253
254
    def ignored_files(self):
255
        for fpath, fclass, fkind, fid in self.list_files():
256
            if fclass == 'I':
257
                yield fpath
258
259
260
    def is_ignored(self, filename):
4 by mbp at sourcefrog
match ignore patterns against only the last path component
261
        """Check whether the filename matches an ignore pattern.
262
263
        Patterns containing '/' need to match the whole path; others
264
        match against only the last component."""
1 by mbp at sourcefrog
import from baz patch-364
265
        ## TODO: Take them from a file, not hardcoded
266
        ## TODO: Use extended zsh-style globs maybe?
267
        ## TODO: Use '**' to match directories?
4 by mbp at sourcefrog
match ignore patterns against only the last path component
268
        for pat in bzrlib.DEFAULT_IGNORE:
269
            if '/' in pat:
270
                if fnmatch.fnmatchcase(filename, pat):
271
                    return True
272
            else:
273
                if fnmatch.fnmatchcase(splitpath(filename)[-1], pat):
274
                    return True
1 by mbp at sourcefrog
import from baz patch-364
275
        return False
276
        
277
278
        
279
        
280
281
class RevisionTree(Tree):
282
    """Tree viewing a previous revision.
283
284
    File text can be retrieved from the text store.
285
286
    :todo: Some kind of `__repr__` method, but a good one
287
           probably means knowing the branch and revision number,
288
           or at least passing a description to the constructor.
289
    """
290
    
291
    def __init__(self, store, inv):
292
        self._store = store
293
        self._inventory = inv
294
295
    def get_file(self, file_id):
296
        ie = self._inventory[file_id]
297
        f = self._store[ie.text_id]
298
        mutter("  get fileid{%s} from %r" % (file_id, self))
299
        fs = filesize(f)
300
        if ie.text_size is None:
301
            note("warning: no text size recorded on %r" % ie)
302
        self._check_retrieved(ie, f)
303
        return f
304
305
    def get_file_size(self, file_id):
306
        return self._inventory[file_id].text_size
307
308
    def get_file_sha1(self, file_id):
309
        ie = self._inventory[file_id]
310
        return ie.text_sha1
311
312
    def has_filename(self, filename):
313
        return bool(self.inventory.path2id(filename))
314
315
    def list_files(self):
316
        # The only files returned by this are those from the version
317
        for path, entry in self.inventory.iter_entries():
318
            yield path, 'V', entry.kind, entry.file_id
319
320
321
class EmptyTree(Tree):
322
    def __init__(self):
323
        self._inventory = Inventory()
324
325
    def has_filename(self, filename):
326
        return False
327
328
    def list_files(self):
329
        if False:  # just to make it a generator
330
            yield None
331
    
332
333
334
######################################################################
335
# diff
336
337
# TODO: Merge these two functions into a single one that can operate
338
# on either a whole tree or a set of files.
339
340
# TODO: Return the diff in order by filename, not by category or in
341
# random order.  Can probably be done by lock-stepping through the
342
# filenames from both trees.
343
344
345
def file_status(filename, old_tree, new_tree):
346
    """Return single-letter status, old and new names for a file.
347
348
    The complexity here is in deciding how to represent renames;
349
    many complex cases are possible.
350
    """
351
    old_inv = old_tree.inventory
352
    new_inv = new_tree.inventory
353
    new_id = new_inv.path2id(filename)
354
    old_id = old_inv.path2id(filename)
355
356
    if not new_id and not old_id:
357
        # easy: doesn't exist in either; not versioned at all
358
        if new_tree.is_ignored(filename):
359
            return 'I', None, None
360
        else:
361
            return '?', None, None
362
    elif new_id:
363
        # There is now a file of this name, great.
364
        pass
365
    else:
366
        # There is no longer a file of this name, but we can describe
367
        # what happened to the file that used to have
368
        # this name.  There are two possibilities: either it was
369
        # deleted entirely, or renamed.
370
        assert old_id
371
        if new_inv.has_id(old_id):
372
            return 'X', old_inv.id2path(old_id), new_inv.id2path(old_id)
373
        else:
374
            return 'D', old_inv.id2path(old_id), None
375
376
    # if the file_id is new in this revision, it is added
377
    if new_id and not old_inv.has_id(new_id):
378
        return 'A'
379
380
    # if there used to be a file of this name, but that ID has now
381
    # disappeared, it is deleted
382
    if old_id and not new_inv.has_id(old_id):
383
        return 'D'
384
385
    return 'wtf?'
386
387
    
388