/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
453 by Martin Pool
- Split WorkingTree into its own file
1
# Copyright (C) 2005 Canonical Ltd
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
1185.16.72 by Martin Pool
[merge] from robert and fix up tests
17
"""WorkingTree object and friends.
18
19
A WorkingTree represents the editable working copy of a branch.
20
Operations which represent the WorkingTree are also done here, 
21
such as renaming or adding files.  The WorkingTree has an inventory 
22
which is updated by these operations.  A commit produces a 
23
new revision based on the workingtree and its inventory.
24
25
At the moment every WorkingTree has its own branch.  Remote
26
WorkingTrees aren't supported.
27
28
To get a WorkingTree, call Branch.working_tree():
29
"""
30
31
32
# TODO: Don't allow WorkingTrees to be constructed for remote branches if 
33
# they don't work.
453 by Martin Pool
- Split WorkingTree into its own file
34
956 by Martin Pool
doc
35
# FIXME: I don't know if writing out the cache from the destructor is really a
1185.16.72 by Martin Pool
[merge] from robert and fix up tests
36
# good idea, because destructors are considered poor taste in Python, and it's
37
# not predictable when it will be written out.
38
39
# TODO: Give the workingtree sole responsibility for the working inventory;
40
# remove the variable and references to it from the branch.  This may require
41
# updating the commit code so as to update the inventory within the working
42
# copy, and making sure there's only one WorkingTree for any directory on disk.
43
# At the momenthey may alias the inventory and have old copies of it in memory.
956 by Martin Pool
doc
44
453 by Martin Pool
- Split WorkingTree into its own file
45
import os
1398 by Robert Collins
integrate in Gustavos x-bit patch
46
import stat
1140 by Martin Pool
- lift out import statements within WorkingTree
47
import fnmatch
1457.1.1 by Robert Collins
rather than getting the branch inventory, WorkingTree can use the whole Branch, or make its own.
48
 
1442.1.65 by Robert Collins
Branch.remove has been moved to WorkingTree.
49
from bzrlib.branch import Branch, needs_read_lock, needs_write_lock, quotefn
453 by Martin Pool
- Split WorkingTree into its own file
50
import bzrlib.tree
1457.1.3 by Robert Collins
make Branch.relpath delegate to the working tree.
51
from bzrlib.osutils import appendpath, file_kind, isdir, splitpath, relpath
1464 by Robert Collins
Merged Martin.
52
from bzrlib.errors import BzrCheckError, DivergedBranches, NotVersionedError
1140 by Martin Pool
- lift out import statements within WorkingTree
53
from bzrlib.trace import mutter
453 by Martin Pool
- Split WorkingTree into its own file
54
1399.1.2 by Robert Collins
push kind character creation into InventoryEntry and TreeEntry
55
class TreeEntry(object):
56
    """An entry that implements the minium interface used by commands.
57
58
    This needs further inspection, it may be better to have 
59
    InventoryEntries without ids - though that seems wrong. For now,
60
    this is a parallel hierarchy to InventoryEntry, and needs to become
61
    one of several things: decorates to that hierarchy, children of, or
62
    parents of it.
1399.1.3 by Robert Collins
move change detection for text and metadata from delta to entry.detect_changes
63
    Another note is that these objects are currently only used when there is
64
    no InventoryEntry available - i.e. for unversioned objects.
65
    Perhaps they should be UnversionedEntry et al. ? - RBC 20051003
1399.1.2 by Robert Collins
push kind character creation into InventoryEntry and TreeEntry
66
    """
67
 
68
    def __eq__(self, other):
69
        # yes, this us ugly, TODO: best practice __eq__ style.
70
        return (isinstance(other, TreeEntry)
71
                and other.__class__ == self.__class__)
72
 
73
    def kind_character(self):
74
        return "???"
75
76
77
class TreeDirectory(TreeEntry):
78
    """See TreeEntry. This is a directory in a working tree."""
79
80
    def __eq__(self, other):
81
        return (isinstance(other, TreeDirectory)
82
                and other.__class__ == self.__class__)
83
84
    def kind_character(self):
85
        return "/"
86
87
88
class TreeFile(TreeEntry):
89
    """See TreeEntry. This is a regular file in a working tree."""
90
91
    def __eq__(self, other):
92
        return (isinstance(other, TreeFile)
93
                and other.__class__ == self.__class__)
94
95
    def kind_character(self):
96
        return ''
97
98
99
class TreeLink(TreeEntry):
100
    """See TreeEntry. This is a symlink in a working tree."""
101
102
    def __eq__(self, other):
103
        return (isinstance(other, TreeLink)
104
                and other.__class__ == self.__class__)
105
106
    def kind_character(self):
107
        return ''
108
109
453 by Martin Pool
- Split WorkingTree into its own file
110
class WorkingTree(bzrlib.tree.Tree):
111
    """Working copy tree.
112
113
    The inventory is held in the `Branch` working-inventory, and the
114
    files are in a directory on disk.
115
116
    It is possible for a `WorkingTree` to have a filename which is
117
    not listed in the Inventory and vice versa.
118
    """
1442.1.65 by Robert Collins
Branch.remove has been moved to WorkingTree.
119
1457.1.1 by Robert Collins
rather than getting the branch inventory, WorkingTree can use the whole Branch, or make its own.
120
    def __init__(self, basedir, branch=None):
121
        """Construct a WorkingTree for basedir.
122
123
        If the branch is not supplied, it is opened automatically.
124
        If the branch is supplied, it must be the branch for this basedir.
125
        (branch.base is not cross checked, because for remote branches that
126
        would be meaningless).
127
        """
866 by Martin Pool
- use new path-based hashcache for WorkingTree- squash mtime/ctime to whole seconds- update and if necessary write out hashcache when WorkingTree object is created.
128
        from bzrlib.hashcache import HashCache
129
        from bzrlib.trace import note, mutter
1185.16.72 by Martin Pool
[merge] from robert and fix up tests
130
        assert isinstance(basedir, basestring), \
131
            "base directory %r is not a string" % basedir
1457.1.1 by Robert Collins
rather than getting the branch inventory, WorkingTree can use the whole Branch, or make its own.
132
        if branch is None:
133
            branch = Branch.open(basedir)
1185.16.72 by Martin Pool
[merge] from robert and fix up tests
134
        assert isinstance(branch, Branch), \
135
            "branch %r is not a Branch" % branch
1457.1.1 by Robert Collins
rather than getting the branch inventory, WorkingTree can use the whole Branch, or make its own.
136
        self._inventory = branch.inventory
137
        self.path2id = self._inventory.path2id
138
        self.branch = branch
453 by Martin Pool
- Split WorkingTree into its own file
139
        self.basedir = basedir
866 by Martin Pool
- use new path-based hashcache for WorkingTree- squash mtime/ctime to whole seconds- update and if necessary write out hashcache when WorkingTree object is created.
140
141
        # update the whole cache up front and write to disk if anything changed;
142
        # in the future we might want to do this more selectively
143
        hc = self._hashcache = HashCache(basedir)
144
        hc.read()
954 by Martin Pool
- separate out code that just scans the hash cache to find files that are possibly
145
        hc.scan()
866 by Martin Pool
- use new path-based hashcache for WorkingTree- squash mtime/ctime to whole seconds- update and if necessary write out hashcache when WorkingTree object is created.
146
147
        if hc.needs_write:
148
            mutter("write hc")
149
            hc.write()
954 by Martin Pool
- separate out code that just scans the hash cache to find files that are possibly
150
            
151
            
152
    def __del__(self):
153
        if self._hashcache.needs_write:
154
            self._hashcache.write()
866 by Martin Pool
- use new path-based hashcache for WorkingTree- squash mtime/ctime to whole seconds- update and if necessary write out hashcache when WorkingTree object is created.
155
453 by Martin Pool
- Split WorkingTree into its own file
156
462 by Martin Pool
- New form 'file_id in tree' to check if the file is present
157
    def __iter__(self):
158
        """Iterate through file_ids for this tree.
159
160
        file_ids are in a WorkingTree if they are in the working inventory
161
        and the working file exists.
162
        """
163
        inv = self._inventory
866 by Martin Pool
- use new path-based hashcache for WorkingTree- squash mtime/ctime to whole seconds- update and if necessary write out hashcache when WorkingTree object is created.
164
        for path, ie in inv.iter_entries():
1092.2.6 by Robert Collins
symlink support updated to work
165
            if bzrlib.osutils.lexists(self.abspath(path)):
866 by Martin Pool
- use new path-based hashcache for WorkingTree- squash mtime/ctime to whole seconds- update and if necessary write out hashcache when WorkingTree object is created.
166
                yield ie.file_id
462 by Martin Pool
- New form 'file_id in tree' to check if the file is present
167
168
453 by Martin Pool
- Split WorkingTree into its own file
169
    def __repr__(self):
170
        return "<%s of %s>" % (self.__class__.__name__,
954 by Martin Pool
- separate out code that just scans the hash cache to find files that are possibly
171
                               getattr(self, 'basedir', None))
453 by Martin Pool
- Split WorkingTree into its own file
172
866 by Martin Pool
- use new path-based hashcache for WorkingTree- squash mtime/ctime to whole seconds- update and if necessary write out hashcache when WorkingTree object is created.
173
174
453 by Martin Pool
- Split WorkingTree into its own file
175
    def abspath(self, filename):
176
        return os.path.join(self.basedir, filename)
177
1457.1.3 by Robert Collins
make Branch.relpath delegate to the working tree.
178
    def relpath(self, abspath):
179
        """Return the local path portion from a given absolute path."""
180
        return relpath(self.basedir, abspath)
181
453 by Martin Pool
- Split WorkingTree into its own file
182
    def has_filename(self, filename):
1092.2.6 by Robert Collins
symlink support updated to work
183
        return bzrlib.osutils.lexists(self.abspath(filename))
453 by Martin Pool
- Split WorkingTree into its own file
184
185
    def get_file(self, file_id):
186
        return self.get_file_byname(self.id2path(file_id))
187
188
    def get_file_byname(self, filename):
189
        return file(self.abspath(filename), 'rb')
190
191
    def _get_store_filename(self, file_id):
192
        ## XXX: badly named; this isn't in the store at all
193
        return self.abspath(self.id2path(file_id))
194
1248 by Martin Pool
- new weave based cleanup [broken]
195
196
    def id2abspath(self, file_id):
197
        return self.abspath(self.id2path(file_id))
198
462 by Martin Pool
- New form 'file_id in tree' to check if the file is present
199
                
1185.12.39 by abentley
Propogated has_or_had_id to Tree
200
    def has_id(self, file_id):
453 by Martin Pool
- Split WorkingTree into its own file
201
        # files that have been deleted are excluded
1185.12.39 by abentley
Propogated has_or_had_id to Tree
202
        inv = self._inventory
866 by Martin Pool
- use new path-based hashcache for WorkingTree- squash mtime/ctime to whole seconds- update and if necessary write out hashcache when WorkingTree object is created.
203
        if not inv.has_id(file_id):
453 by Martin Pool
- Split WorkingTree into its own file
204
            return False
866 by Martin Pool
- use new path-based hashcache for WorkingTree- squash mtime/ctime to whole seconds- update and if necessary write out hashcache when WorkingTree object is created.
205
        path = inv.id2path(file_id)
1092.2.6 by Robert Collins
symlink support updated to work
206
        return bzrlib.osutils.lexists(self.abspath(path))
462 by Martin Pool
- New form 'file_id in tree' to check if the file is present
207
1185.12.39 by abentley
Propogated has_or_had_id to Tree
208
    def has_or_had_id(self, file_id):
209
        if file_id == self.inventory.root.file_id:
210
            return True
211
        return self.inventory.has_id(file_id)
462 by Martin Pool
- New form 'file_id in tree' to check if the file is present
212
213
    __contains__ = has_id
214
    
215
453 by Martin Pool
- Split WorkingTree into its own file
216
    def get_file_size(self, file_id):
1248 by Martin Pool
- new weave based cleanup [broken]
217
        return os.path.getsize(self.id2abspath(file_id))
453 by Martin Pool
- Split WorkingTree into its own file
218
219
    def get_file_sha1(self, file_id):
866 by Martin Pool
- use new path-based hashcache for WorkingTree- squash mtime/ctime to whole seconds- update and if necessary write out hashcache when WorkingTree object is created.
220
        path = self._inventory.id2path(file_id)
221
        return self._hashcache.get_sha1(path)
453 by Martin Pool
- Split WorkingTree into its own file
222
1398 by Robert Collins
integrate in Gustavos x-bit patch
223
224
    def is_executable(self, file_id):
225
        if os.name == "nt":
226
            return self._inventory[file_id].executable
227
        else:
228
            path = self._inventory.id2path(file_id)
229
            mode = os.lstat(self.abspath(path)).st_mode
230
            return bool(stat.S_ISREG(mode) and stat.S_IEXEC&mode)
231
1092.2.6 by Robert Collins
symlink support updated to work
232
    def get_symlink_target(self, file_id):
1185.15.10 by Scott James Remnant
Fix WorkingTree.get_symlink_target() to read the absolute path of the
233
        return os.readlink(self.id2abspath(file_id))
453 by Martin Pool
- Split WorkingTree into its own file
234
235
    def file_class(self, filename):
236
        if self.path2id(filename):
237
            return 'V'
238
        elif self.is_ignored(filename):
239
            return 'I'
240
        else:
241
            return '?'
242
243
244
    def list_files(self):
245
        """Recursively list all files as (path, class, kind, id).
246
247
        Lists, but does not descend into unversioned directories.
248
249
        This does not include files that have been deleted in this
250
        tree.
251
252
        Skips the control directory.
253
        """
866 by Martin Pool
- use new path-based hashcache for WorkingTree- squash mtime/ctime to whole seconds- update and if necessary write out hashcache when WorkingTree object is created.
254
        inv = self._inventory
453 by Martin Pool
- Split WorkingTree into its own file
255
256
        def descend(from_dir_relpath, from_dir_id, dp):
257
            ls = os.listdir(dp)
258
            ls.sort()
259
            for f in ls:
260
                ## TODO: If we find a subdirectory with its own .bzr
261
                ## directory, then that is a separate tree and we
262
                ## should exclude it.
263
                if bzrlib.BZRDIR == f:
264
                    continue
265
266
                # path within tree
267
                fp = appendpath(from_dir_relpath, f)
268
269
                # absolute path
270
                fap = appendpath(dp, f)
271
                
272
                f_ie = inv.get_child(from_dir_id, f)
273
                if f_ie:
274
                    c = 'V'
275
                elif self.is_ignored(fp):
276
                    c = 'I'
277
                else:
278
                    c = '?'
279
280
                fk = file_kind(fap)
281
282
                if f_ie:
283
                    if f_ie.kind != fk:
284
                        raise BzrCheckError("file %r entered as kind %r id %r, "
285
                                            "now of kind %r"
286
                                            % (fap, f_ie.kind, f_ie.file_id, fk))
287
1399.1.2 by Robert Collins
push kind character creation into InventoryEntry and TreeEntry
288
                # make a last minute entry
289
                if f_ie:
290
                    entry = f_ie
291
                else:
292
                    if fk == 'directory':
293
                        entry = TreeDirectory()
294
                    elif fk == 'file':
295
                        entry = TreeFile()
296
                    elif fk == 'symlink':
297
                        entry = TreeLink()
298
                    else:
299
                        entry = TreeEntry()
300
                
301
                yield fp, c, fk, (f_ie and f_ie.file_id), entry
453 by Martin Pool
- Split WorkingTree into its own file
302
303
                if fk != 'directory':
304
                    continue
305
306
                if c != 'V':
307
                    # don't descend unversioned directories
308
                    continue
309
                
310
                for ff in descend(fp, f_ie.file_id, fap):
311
                    yield ff
312
313
        for f in descend('', inv.root.file_id, self.basedir):
314
            yield f
315
            
316
317
318
    def unknowns(self):
319
        for subp in self.extras():
320
            if not self.is_ignored(subp):
321
                yield subp
322
1185.14.6 by Aaron Bentley
Made iter_conflicts a WorkingTree method
323
    def iter_conflicts(self):
324
        conflicted = set()
325
        for path in (s[0] for s in self.list_files()):
326
            stem = get_conflicted_stem(path)
327
            if stem is None:
328
                continue
329
            if stem not in conflicted:
330
                conflicted.add(stem)
331
                yield stem
453 by Martin Pool
- Split WorkingTree into its own file
332
1442.1.67 by Robert Collins
Factor out the guts of 'pull' from the command into WorkingTree.pull().
333
    @needs_write_lock
1442.1.68 by Robert Collins
'bzr pull' now accepts '--clobber'.
334
    def pull(self, source, remember=False, clobber=False):
1442.1.67 by Robert Collins
Factor out the guts of 'pull' from the command into WorkingTree.pull().
335
        from bzrlib.merge import merge
336
        source.lock_read()
337
        try:
338
            old_revno = self.branch.revno()
339
            old_revision_history = self.branch.revision_history()
340
            try:
341
                self.branch.update_revisions(source)
342
            except DivergedBranches:
1442.1.68 by Robert Collins
'bzr pull' now accepts '--clobber'.
343
                if not clobber:
1442.1.67 by Robert Collins
Factor out the guts of 'pull' from the command into WorkingTree.pull().
344
                    raise
1442.1.68 by Robert Collins
'bzr pull' now accepts '--clobber'.
345
                self.branch.set_revision_history(source.revision_history())
1442.1.67 by Robert Collins
Factor out the guts of 'pull' from the command into WorkingTree.pull().
346
            new_revision_history = self.branch.revision_history()
347
            if new_revision_history != old_revision_history:
348
                merge((self.basedir, -1), (self.basedir, old_revno), check_clean=False)
349
            if self.branch.get_parent() is None or remember:
350
                self.branch.set_parent(source.base)
351
        finally:
352
            source.unlock()
353
453 by Martin Pool
- Split WorkingTree into its own file
354
    def extras(self):
355
        """Yield all unknown files in this WorkingTree.
356
357
        If there are any unknown directories then only the directory is
358
        returned, not all its children.  But if there are unknown files
359
        under a versioned subdirectory, they are returned.
360
361
        Currently returned depth-first, sorted by name within directories.
362
        """
363
        ## TODO: Work from given directory downwards
364
        for path, dir_entry in self.inventory.directories():
365
            mutter("search for unknowns in %r" % path)
366
            dirabs = self.abspath(path)
367
            if not isdir(dirabs):
368
                # e.g. directory deleted
369
                continue
370
371
            fl = []
372
            for subf in os.listdir(dirabs):
373
                if (subf != '.bzr'
374
                    and (subf not in dir_entry.children)):
375
                    fl.append(subf)
376
            
377
            fl.sort()
378
            for subf in fl:
379
                subp = appendpath(path, subf)
380
                yield subp
381
382
383
    def ignored_files(self):
384
        """Yield list of PATH, IGNORE_PATTERN"""
385
        for subp in self.extras():
386
            pat = self.is_ignored(subp)
387
            if pat != None:
388
                yield subp, pat
389
390
391
    def get_ignore_list(self):
392
        """Return list of ignore patterns.
393
394
        Cached in the Tree object after the first call.
395
        """
396
        if hasattr(self, '_ignorelist'):
397
            return self._ignorelist
398
399
        l = bzrlib.DEFAULT_IGNORE[:]
400
        if self.has_filename(bzrlib.IGNORE_FILENAME):
401
            f = self.get_file_byname(bzrlib.IGNORE_FILENAME)
402
            l.extend([line.rstrip("\n\r") for line in f.readlines()])
403
        self._ignorelist = l
404
        return l
405
406
407
    def is_ignored(self, filename):
408
        r"""Check whether the filename matches an ignore pattern.
409
410
        Patterns containing '/' or '\' need to match the whole path;
411
        others match against only the last component.
412
413
        If the file is ignored, returns the pattern which caused it to
414
        be ignored, otherwise None.  So this can simply be used as a
415
        boolean if desired."""
416
417
        # TODO: Use '**' to match directories, and other extended
418
        # globbing stuff from cvs/rsync.
419
420
        # XXX: fnmatch is actually not quite what we want: it's only
421
        # approximately the same as real Unix fnmatch, and doesn't
422
        # treat dotfiles correctly and allows * to match /.
423
        # Eventually it should be replaced with something more
424
        # accurate.
425
        
426
        for pat in self.get_ignore_list():
427
            if '/' in pat or '\\' in pat:
428
                
429
                # as a special case, you can put ./ at the start of a
430
                # pattern; this is good to match in the top-level
431
                # only;
432
                
433
                if (pat[:2] == './') or (pat[:2] == '.\\'):
434
                    newpat = pat[2:]
435
                else:
436
                    newpat = pat
437
                if fnmatch.fnmatchcase(filename, newpat):
438
                    return pat
439
            else:
440
                if fnmatch.fnmatchcase(splitpath(filename)[-1], pat):
441
                    return pat
442
        else:
443
            return None
1185.14.6 by Aaron Bentley
Made iter_conflicts a WorkingTree method
444
1185.12.28 by Aaron Bentley
Removed use of readonly path for executability test
445
    def kind(self, file_id):
446
        return file_kind(self.id2abspath(file_id))
447
1442.1.65 by Robert Collins
Branch.remove has been moved to WorkingTree.
448
    def lock_read(self):
449
        """See Branch.lock_read, and WorkingTree.unlock."""
450
        return self.branch.lock_read()
451
452
    def lock_write(self):
453
        """See Branch.lock_write, and WorkingTree.unlock."""
454
        return self.branch.lock_write()
455
456
    @needs_write_lock
457
    def remove(self, files, verbose=False):
458
        """Remove nominated files from the working inventory..
459
460
        This does not remove their text.  This does not run on XXX on what? RBC
461
462
        TODO: Refuse to remove modified files unless --force is given?
463
464
        TODO: Do something useful with directories.
465
466
        TODO: Should this remove the text or not?  Tough call; not
467
        removing may be useful and the user can just use use rm, and
468
        is the opposite of add.  Removing it is consistent with most
469
        other tools.  Maybe an option.
470
        """
471
        ## TODO: Normalize names
472
        ## TODO: Remove nested loops; better scalability
473
        if isinstance(files, basestring):
474
            files = [files]
475
476
        inv = self.inventory
477
478
        # do this before any modifications
479
        for f in files:
480
            fid = inv.path2id(f)
481
            if not fid:
1185.16.72 by Martin Pool
[merge] from robert and fix up tests
482
                # TODO: Perhaps make this just a warning, and continue?
483
                # This tends to happen when 
484
                raise NotVersionedError(path=f)
1442.1.65 by Robert Collins
Branch.remove has been moved to WorkingTree.
485
            mutter("remove inventory entry %s {%s}" % (quotefn(f), fid))
486
            if verbose:
487
                # having remove it, it must be either ignored or unknown
488
                if self.is_ignored(f):
489
                    new_status = 'I'
490
                else:
491
                    new_status = '?'
492
                show_status(new_status, inv[fid].kind, quotefn(f))
493
            del inv[fid]
494
495
        self.branch._write_inventory(inv)
496
497
    def unlock(self):
498
        """See Branch.unlock.
499
        
500
        WorkingTree locking just uses the Branch locking facilities.
501
        This is current because all working trees have an embedded branch
502
        within them. IF in the future, we were to make branch data shareable
503
        between multiple working trees, i.e. via shared storage, then we 
504
        would probably want to lock both the local tree, and the branch.
505
        """
506
        return self.branch.unlock()
507
508
1185.14.6 by Aaron Bentley
Made iter_conflicts a WorkingTree method
509
CONFLICT_SUFFIXES = ('.THIS', '.BASE', '.OTHER')
510
def get_conflicted_stem(path):
511
    for suffix in CONFLICT_SUFFIXES:
512
        if path.endswith(suffix):
513
            return path[:-len(suffix)]