~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/delta.py

  • Committer: Martin Pool
  • Date: 2006-05-05 01:15:53 UTC
  • mto: This revision was merged to the branch mainline in revision 1698.
  • Revision ID: mbp@sourcefrog.net-20060505011553-76b4687a5e1d01e3
[patch] force deletion of trees containing readonly files (alexander)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#! /usr/bin/env python
2
 
# -*- coding: UTF-8 -*-
 
1
# Copyright (C) 2005, 2006 Canonical
3
2
 
4
3
# This program is free software; you can redistribute it and/or modify
5
4
# it under the terms of the GNU General Public License as published by
15
14
# along with this program; if not, write to the Free Software
16
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
16
 
 
17
from bzrlib.inventory import InventoryEntry
18
18
from bzrlib.trace import mutter
19
19
 
20
20
class TreeDelta(object):
27
27
    removed
28
28
        (path, id, kind)
29
29
    renamed
30
 
        (oldpath, newpath, id, kind, text_modified)
 
30
        (oldpath, newpath, id, kind, text_modified, meta_modified)
31
31
    modified
32
 
        (path, id, kind)
 
32
        (path, id, kind, text_modified, meta_modified)
33
33
    unchanged
34
34
        (path, id, kind)
35
35
 
36
36
    Each id is listed only once.
37
37
 
38
38
    Files that are both modified and renamed are listed only in
39
 
    renamed, with the text_modified flag true.
 
39
    renamed, with the text_modified flag true. The text_modified
 
40
    applies either to the the content of the file or the target of the
 
41
    symbolic link, depending of the kind of file.
40
42
 
41
43
    Files are only considered renamed if their name has changed or
42
44
    their parent directory has changed.  Renaming a directory
88
90
 
89
91
    def show(self, to_file, show_ids=False, show_unchanged=False):
90
92
        def show_list(files):
91
 
            for path, fid, kind in files:
 
93
            for item in files:
 
94
                path, fid, kind = item[:3]
 
95
 
92
96
                if kind == 'directory':
93
97
                    path += '/'
94
98
                elif kind == 'symlink':
95
99
                    path += '@'
96
 
                    
 
100
 
 
101
                if len(item) == 5 and item[4]:
 
102
                    path += '*'
 
103
 
97
104
                if show_ids:
98
105
                    print >>to_file, '  %-30s %s' % (path, fid)
99
106
                else:
107
114
            print >>to_file, 'added:'
108
115
            show_list(self.added)
109
116
 
 
117
        extra_modified = []
 
118
 
110
119
        if self.renamed:
111
120
            print >>to_file, 'renamed:'
112
 
            for oldpath, newpath, fid, kind, text_modified in self.renamed:
 
121
            for (oldpath, newpath, fid, kind,
 
122
                 text_modified, meta_modified) in self.renamed:
 
123
                if text_modified or meta_modified:
 
124
                    extra_modified.append((newpath, fid, kind,
 
125
                                           text_modified, meta_modified))
 
126
                if meta_modified:
 
127
                    newpath += '*'
113
128
                if show_ids:
114
129
                    print >>to_file, '  %s => %s %s' % (oldpath, newpath, fid)
115
130
                else:
116
131
                    print >>to_file, '  %s => %s' % (oldpath, newpath)
117
132
                    
118
 
        if self.modified:
 
133
        if self.modified or extra_modified:
119
134
            print >>to_file, 'modified:'
120
135
            show_list(self.modified)
 
136
            show_list(extra_modified)
121
137
            
122
138
        if show_unchanged and self.unchanged:
123
139
            print >>to_file, 'unchanged:'
141
157
 
142
158
    specific_files
143
159
        If true, only check for changes to specified names or
144
 
        files within them.
 
160
        files within them.  Any unversioned files given have no effect
 
161
        (but this might change in the future).
145
162
    """
 
163
    # NB: show_status depends on being able to pass in non-versioned files and
 
164
    # report them as unknown
 
165
    old_tree.lock_read()
 
166
    try:
 
167
        new_tree.lock_read()
 
168
        try:
 
169
            return _compare_trees(old_tree, new_tree, want_unchanged,
 
170
                                  specific_files)
 
171
        finally:
 
172
            new_tree.unlock()
 
173
    finally:
 
174
        old_tree.unlock()
 
175
 
 
176
 
 
177
def _compare_trees(old_tree, new_tree, want_unchanged, specific_files):
146
178
 
147
179
    from osutils import is_inside_any
148
180
    
151
183
    delta = TreeDelta()
152
184
    mutter('start compare_trees')
153
185
 
154
 
    # TODO: match for specific files can be rather smarter by finding
155
 
    # the IDs of those files up front and then considering only that.
 
186
    # TODO: Rather than iterating over the whole tree and then filtering, we
 
187
    # could diff just the specified files (if any) and their subtrees.  
 
188
    # Perhaps should take a list of file-ids instead?   Need to indicate any
 
189
    # ids or names which were not found in the trees.
156
190
 
157
191
    for file_id in old_tree:
158
192
        if file_id in new_tree:
162
196
            kind = old_ie.kind
163
197
            assert kind == new_ie.kind
164
198
            
165
 
            assert kind in ('file', 'directory', 'symlink', 'root_directory'), \
 
199
            assert kind in InventoryEntry.known_kinds, \
166
200
                   'invalid file kind %r' % kind
167
201
 
168
202
            if kind == 'root_directory':
173
207
                    and not is_inside_any(specific_files, new_inv.id2path(file_id))):
174
208
                    continue
175
209
 
176
 
            if kind == 'file':
177
 
                old_sha1 = old_tree.get_file_sha1(file_id)
178
 
                new_sha1 = new_tree.get_file_sha1(file_id)
179
 
                text_modified = (old_sha1 != new_sha1)
180
 
            else:
181
 
                ## mutter("no text to check for %r %r" % (file_id, kind))
182
 
                text_modified = False
 
210
            # temporary hack until all entries are populated before clients 
 
211
            # get them
 
212
            old_path = old_inv.id2path(file_id)
 
213
            new_path = new_inv.id2path(file_id)
 
214
            old_ie._read_tree_state(old_path, old_tree)
 
215
            new_ie._read_tree_state(new_path, new_tree)
 
216
            text_modified, meta_modified = new_ie.detect_changes(old_ie)
183
217
 
184
218
            # TODO: Can possibly avoid calculating path strings if the
185
219
            # two files are unchanged and their names and parents are
188
222
            
189
223
            if (old_ie.name != new_ie.name
190
224
                or old_ie.parent_id != new_ie.parent_id):
191
 
                delta.renamed.append((old_inv.id2path(file_id),
192
 
                                      new_inv.id2path(file_id),
 
225
                delta.renamed.append((old_path,
 
226
                                      new_path,
193
227
                                      file_id, kind,
194
 
                                      text_modified))
195
 
            elif text_modified:
196
 
                delta.modified.append((new_inv.id2path(file_id), file_id, kind))
 
228
                                      text_modified, meta_modified))
 
229
            elif text_modified or meta_modified:
 
230
                delta.modified.append((new_path, file_id, kind,
 
231
                                       text_modified, meta_modified))
197
232
            elif want_unchanged:
198
 
                delta.unchanged.append((new_inv.id2path(file_id), file_id, kind))
 
233
                delta.unchanged.append((new_path, file_id, kind))
199
234
        else:
200
235
            kind = old_inv.get_file_kind(file_id)
201
236
            if kind == 'root_directory':
208
243
 
209
244
    mutter('start looking for new files')
210
245
    for file_id in new_inv:
211
 
        if file_id in old_inv:
 
246
        if file_id in old_inv or file_id not in new_tree:
212
247
            continue
213
248
        kind = new_inv.get_file_kind(file_id)
214
249
        if kind == 'root_directory':