~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/delta.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2009-01-15 16:31:04 UTC
  • mfrom: (3941.1.1 bzr.integration)
  • Revision ID: pqm@pqm.ubuntu.com-20090115163104-30i51kcr1zepm2f7
(vila) Allows deltas to be filtered by log formatters

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
class TreeDelta(object):
27
27
    """Describes changes from one tree to another.
28
28
 
29
 
    Contains four lists:
 
29
    Contains seven lists:
30
30
 
31
31
    added
32
32
        (path, id, kind)
34
34
        (path, id, kind)
35
35
    renamed
36
36
        (oldpath, newpath, id, kind, text_modified, meta_modified)
 
37
    kind_changed
 
38
        (path, id, old_kind, new_kind)
37
39
    modified
38
40
        (path, id, kind, text_modified, meta_modified)
39
41
    unchanged
40
42
        (path, id, kind)
41
43
    unversioned
42
 
        (path, kind)
 
44
        (path, None, kind)
43
45
 
44
46
    Each id is listed only once.
45
47
 
107
109
 
108
110
 
109
111
    def show(self, to_file, show_ids=False, show_unchanged=False,
110
 
             short_status=False, indent=''):
111
 
        """output this delta in status-like form to to_file."""
112
 
        def show_list(files, short_status_letter=''):
113
 
            for item in files:
114
 
                path, fid, kind = item[:3]
115
 
 
116
 
                if kind == 'directory':
117
 
                    path += '/'
118
 
                elif kind == 'symlink':
119
 
                    path += '@'
120
 
 
121
 
                if len(item) == 5 and item[4]:
122
 
                    path += '*'
123
 
 
124
 
                if show_ids:
125
 
                    to_file.write(indent + '%s  %-30s %s\n' % (short_status_letter,
126
 
                        path, fid))
 
112
             short_status=False, indent='',
 
113
             filter=None):
 
114
        """Output this delta in status-like form to to_file.
 
115
 
 
116
        :param to_file: A file-like object where the output is displayed.
 
117
 
 
118
        :param show_ids: Output the file ids if True.
 
119
 
 
120
        :param show_unchanged: Output the unchanged files if True.
 
121
 
 
122
        :param short_status: Single-line status if True.
 
123
 
 
124
        :param indent: Added at the beginning of all output lines (for merged
 
125
            revisions).
 
126
 
 
127
        :param filter: A callable receiving a path and a file id and
 
128
            returning True if the path should be displayed.
 
129
        """
 
130
 
 
131
        def decorate_path(path, kind, meta_modified=None):
 
132
            if kind == 'directory':
 
133
                path += '/'
 
134
            elif kind == 'symlink':
 
135
                path += '@'
 
136
            if meta_modified:
 
137
                path += '*'
 
138
            return path
 
139
 
 
140
        def show_more_renamed(item):
 
141
            (oldpath, file_id, kind,
 
142
             text_modified, meta_modified, newpath) = item
 
143
            dec_new_path = decorate_path(newpath, kind, meta_modified)
 
144
            to_file.write(' => %s' % dec_new_path)
 
145
            if text_modified or meta_modified:
 
146
                extra_modified.append((newpath, file_id, kind,
 
147
                                       text_modified, meta_modified))
 
148
 
 
149
        def show_more_kind_changed(item):
 
150
            (path, file_id, old_kind, new_kind) = item
 
151
            to_file.write(' (%s => %s)' % (old_kind, new_kind))
 
152
 
 
153
        def show_path(path, file_id, kind, meta_modified,
 
154
                      default_format, with_file_id_format):
 
155
            dec_path = decorate_path(path, kind, meta_modified)
 
156
            if show_ids:
 
157
                to_file.write(with_file_id_format % dec_path)
 
158
            else:
 
159
                to_file.write(default_format % dec_path)
 
160
 
 
161
        def show_list(files, long_status_name, short_status_letter,
 
162
                      default_format='%s', with_file_id_format='%-30s',
 
163
                      show_more=None):
 
164
            if files:
 
165
                header_shown = False
 
166
                if short_status:
 
167
                    prefix = short_status_letter
127
168
                else:
128
 
                    to_file.write(indent + '%s  %s\n' % (short_status_letter, path))
129
 
 
130
 
        if self.removed:
131
 
            if not short_status:
132
 
                to_file.write(indent + 'removed:\n')
133
 
                show_list(self.removed)
134
 
            else:
135
 
                show_list(self.removed, 'D')
136
 
 
137
 
        if self.added:
138
 
            if not short_status:
139
 
                to_file.write(indent + 'added:\n')
140
 
                show_list(self.added)
141
 
            else:
142
 
                show_list(self.added, 'A')
143
 
 
 
169
                    prefix = ''
 
170
                prefix = indent + prefix + '  '
 
171
 
 
172
                for item in files:
 
173
                    path, file_id, kind = item[:3]
 
174
                    if (filter is not None and not filter(path, file_id)):
 
175
                        continue
 
176
                    if not header_shown and not short_status:
 
177
                        to_file.write(indent + long_status_name + ':\n')
 
178
                        header_shown = True
 
179
                    meta_modified = None
 
180
                    if len(item) == 5:
 
181
                        meta_modified = item[4]
 
182
 
 
183
                    to_file.write(prefix)
 
184
                    show_path(path, file_id, kind, meta_modified,
 
185
                              default_format, with_file_id_format)
 
186
                    if show_more is not None:
 
187
                        show_more(item)
 
188
                    if show_ids:
 
189
                        to_file.write(' %s' % file_id)
 
190
                    to_file.write('\n')
 
191
 
 
192
        show_list(self.removed, 'removed', 'D')#
 
193
        show_list(self.added, 'added', 'A')
144
194
        extra_modified = []
145
 
 
146
 
        if self.renamed:
147
 
            short_status_letter = 'R'
148
 
            if not short_status:
149
 
                to_file.write(indent + 'renamed:\n')
150
 
                short_status_letter = ''
151
 
            for (oldpath, newpath, fid, kind,
152
 
                 text_modified, meta_modified) in self.renamed:
153
 
                if text_modified or meta_modified:
154
 
                    extra_modified.append((newpath, fid, kind,
155
 
                                           text_modified, meta_modified))
156
 
                if meta_modified:
157
 
                    newpath += '*'
158
 
                if show_ids:
159
 
                    to_file.write(indent + '%s  %s => %s %s\n' % (
160
 
                        short_status_letter, oldpath, newpath, fid))
161
 
                else:
162
 
                    to_file.write(indent + '%s  %s => %s\n' % (
163
 
                        short_status_letter, oldpath, newpath))
164
 
 
165
 
        if self.kind_changed:
166
 
            if short_status:
167
 
                short_status_letter = 'K'
168
 
            else:
169
 
                to_file.write(indent + 'kind changed:\n')
170
 
                short_status_letter = ''
171
 
            for (path, fid, old_kind, new_kind) in self.kind_changed:
172
 
                if show_ids:
173
 
                    suffix = ' '+fid
174
 
                else:
175
 
                    suffix = ''
176
 
                to_file.write(indent + '%s  %s (%s => %s)%s\n' % (
177
 
                    short_status_letter, path, old_kind, new_kind, suffix))
178
 
 
179
 
        if self.modified or extra_modified:
180
 
            short_status_letter = 'M'
181
 
            if not short_status:
182
 
                to_file.write(indent + 'modified:\n')
183
 
                short_status_letter = ''
184
 
            show_list(self.modified, short_status_letter)
185
 
            show_list(extra_modified, short_status_letter)
186
 
            
187
 
        if show_unchanged and self.unchanged:
188
 
            if not short_status:
189
 
                to_file.write(indent + 'unchanged:\n')
190
 
                show_list(self.unchanged)
191
 
            else:
192
 
                show_list(self.unchanged, 'S')
193
 
 
194
 
        if self.unversioned:
195
 
            to_file.write(indent + 'unknown:\n')
196
 
            show_list(self.unversioned)
 
195
        # Reorder self.renamed tuples so that all lists share the same
 
196
        # order for their 3 first fields and that they also begin like
 
197
        # the self.modified tuples
 
198
        renamed = [(p, i, k, tm, mm, np)
 
199
                   for  p, np, i, k, tm, mm  in self.renamed]
 
200
        show_list(renamed, 'renamed', 'R', with_file_id_format='%s',
 
201
                  show_more=show_more_renamed)
 
202
        show_list(self.kind_changed, 'kind changed', 'K',
 
203
                  with_file_id_format='%s',
 
204
                  show_more=show_more_kind_changed)
 
205
        show_list(self.modified + extra_modified, 'modified', 'M')
 
206
        if show_unchanged:
 
207
            show_list(self.unchanged, 'unchanged', 'S')
 
208
 
 
209
        show_list(self.unversioned, 'unknown', ' ')
197
210
 
198
211
    def get_changes_as_text(self, show_ids=False, show_unchanged=False,
199
212
             short_status=False):