/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
from trace import mutter
356 by Martin Pool
- pychecker fixes in bzrlib.diff
19
from errors import BzrError
1 by mbp at sourcefrog
import from baz patch-364
20
21
568 by Martin Pool
- start adding support for showing diffs by calling out to
22
def internal_diff(old_label, oldlines, new_label, newlines, to_file):
475 by Martin Pool
- rewrite diff using compare_trees()
23
    import difflib
24
    
25
    # FIXME: difflib is wrong if there is no trailing newline.
26
    # The syntax used by patch seems to be "\ No newline at
27
    # end of file" following the last diff line from that
28
    # file.  This is not trivial to insert into the
29
    # unified_diff output and it might be better to just fix
30
    # or replace that function.
31
32
    # In the meantime we at least make sure the patch isn't
33
    # mangled.
34
35
36
    # Special workaround for Python2.3, where difflib fails if
37
    # both sequences are empty.
38
    if not oldlines and not newlines:
39
        return
40
41
    nonl = False
42
43
    if oldlines and (oldlines[-1][-1] != '\n'):
44
        oldlines[-1] += '\n'
45
        nonl = True
46
    if newlines and (newlines[-1][-1] != '\n'):
47
        newlines[-1] += '\n'
48
        nonl = True
49
568 by Martin Pool
- start adding support for showing diffs by calling out to
50
    ud = difflib.unified_diff(oldlines, newlines,
51
                              fromfile=old_label, tofile=new_label)
475 by Martin Pool
- rewrite diff using compare_trees()
52
53
    # work-around for difflib being too smart for its own good
54
    # if /dev/null is "1,0", patch won't recognize it as /dev/null
55
    if not oldlines:
56
        ud = list(ud)
57
        ud[2] = ud[2].replace('-1,0', '-0,0')
58
    elif not newlines:
59
        ud = list(ud)
60
        ud[2] = ud[2].replace('+1,0', '+0,0')
61
62
    to_file.writelines(ud)
63
    if nonl:
64
        print >>to_file, "\\ No newline at end of file"
65
    print >>to_file
66
67
550 by Martin Pool
- Refactor diff code into one that works purely on
68
568 by Martin Pool
- start adding support for showing diffs by calling out to
69
70
def external_diff(old_label, oldlines, new_label, newlines, to_file):
71
    """Display a diff by calling out to the external diff program."""
72
    import sys
73
    
74
    if to_file != sys.stdout:
75
        raise NotImplementedError("sorry, can't send external diff other than to stdout yet",
76
                                  to_file)
77
78
    from tempfile import NamedTemporaryFile
79
    from os import system
80
81
    oldtmpf = NamedTemporaryFile()
82
    newtmpf = NamedTemporaryFile()
83
84
    try:
85
        # TODO: perhaps a special case for comparing to or from the empty
86
        # sequence; can just use /dev/null on Unix
87
88
        # TODO: if either of the files being compared already exists as a
89
        # regular named file (e.g. in the working directory) then we can
90
        # compare directly to that, rather than copying it.
91
92
        # TODO: Set the labels appropriately
93
94
        oldtmpf.writelines(oldlines)
95
        newtmpf.writelines(newlines)
96
97
        oldtmpf.flush()
98
        newtmpf.flush()
99
100
        system('diff -u --label %s %s --label %s %s' % (old_label, oldtmpf.name, new_label, newtmpf.name))
101
    finally:
102
        oldtmpf.close()                 # and delete
103
        newtmpf.close()
104
    
105
106
107
def diff_file(old_label, oldlines, new_label, newlines, to_file):
108
    if True:
109
        differ = external_diff
110
    else:
111
        differ = internal_diff
112
113
    differ(old_label, oldlines, new_label, newlines, to_file)
114
115
116
483 by Martin Pool
- change 'file_list' to more explanatory 'specific_files'
117
def show_diff(b, revision, specific_files):
475 by Martin Pool
- rewrite diff using compare_trees()
118
    import sys
119
329 by Martin Pool
- refactor command functions into command classes
120
    if revision == None:
121
        old_tree = b.basis_tree()
122
    else:
123
        old_tree = b.revision_tree(b.lookup_revision(revision))
124
        
125
    new_tree = b.working_tree()
126
550 by Martin Pool
- Refactor diff code into one that works purely on
127
    show_diff_trees(old_tree, new_tree, sys.stdout, specific_files)
128
129
130
131
def show_diff_trees(old_tree, new_tree, to_file, specific_files=None):
132
    """Show in text form the changes from one tree to another.
133
134
    to_files
135
        If set, include only changes to these files.
136
    """
137
329 by Martin Pool
- refactor command functions into command classes
138
    # TODO: Options to control putting on a prefix or suffix, perhaps as a format string
139
    old_label = ''
140
    new_label = ''
141
142
    DEVNULL = '/dev/null'
143
    # Windows users, don't panic about this filename -- it is a
144
    # special signal to GNU patch that the file should be created or
145
    # deleted respectively.
146
147
    # TODO: Generation of pseudo-diffs for added/deleted files could
148
    # be usefully made into a much faster special case.
149
478 by Martin Pool
- put back support for running diff or status on
150
    delta = compare_trees(old_tree, new_tree, want_unchanged=False,
483 by Martin Pool
- change 'file_list' to more explanatory 'specific_files'
151
                          specific_files=specific_files)
475 by Martin Pool
- rewrite diff using compare_trees()
152
153
    for path, file_id, kind in delta.removed:
154
        print '*** removed %s %r' % (kind, path)
155
        if kind == 'file':
568 by Martin Pool
- start adding support for showing diffs by calling out to
156
            diff_file(old_label + path,
157
                      old_tree.get_file(file_id).readlines(),
158
                      DEVNULL, 
159
                      [],
160
                      to_file)
475 by Martin Pool
- rewrite diff using compare_trees()
161
162
    for path, file_id, kind in delta.added:
163
        print '*** added %s %r' % (kind, path)
164
        if kind == 'file':
568 by Martin Pool
- start adding support for showing diffs by calling out to
165
            diff_file(DEVNULL,
166
                      [],
167
                      new_label + path,
168
                      new_tree.get_file(file_id).readlines(),
169
                      to_file)
475 by Martin Pool
- rewrite diff using compare_trees()
170
171
    for old_path, new_path, file_id, kind, text_modified in delta.renamed:
172
        print '*** renamed %s %r => %r' % (kind, old_path, new_path)
173
        if text_modified:
568 by Martin Pool
- start adding support for showing diffs by calling out to
174
            diff_file(old_label + old_path,
175
                      old_tree.get_file(file_id).readlines(),
176
                      new_label + new_path,
177
                      new_tree.get_file(file_id).readlines(),
178
                      to_file)
475 by Martin Pool
- rewrite diff using compare_trees()
179
180
    for path, file_id, kind in delta.modified:
181
        print '*** modified %s %r' % (kind, path)
182
        if kind == 'file':
568 by Martin Pool
- start adding support for showing diffs by calling out to
183
            diff_file(old_label + path,
184
                      old_tree.get_file(file_id).readlines(),
185
                      new_label + path,
186
                      new_tree.get_file(file_id).readlines(),
187
                      to_file)
329 by Martin Pool
- refactor command functions into command classes
188
189
379 by Martin Pool
- Simpler compare_inventories() to possibly replace diff_trees
190
558 by Martin Pool
- All top-level classes inherit from object
191
class TreeDelta(object):
379 by Martin Pool
- Simpler compare_inventories() to possibly replace diff_trees
192
    """Describes changes from one tree to another.
193
194
    Contains four lists:
195
196
    added
475 by Martin Pool
- rewrite diff using compare_trees()
197
        (path, id, kind)
379 by Martin Pool
- Simpler compare_inventories() to possibly replace diff_trees
198
    removed
475 by Martin Pool
- rewrite diff using compare_trees()
199
        (path, id, kind)
379 by Martin Pool
- Simpler compare_inventories() to possibly replace diff_trees
200
    renamed
475 by Martin Pool
- rewrite diff using compare_trees()
201
        (oldpath, newpath, id, kind, text_modified)
379 by Martin Pool
- Simpler compare_inventories() to possibly replace diff_trees
202
    modified
475 by Martin Pool
- rewrite diff using compare_trees()
203
        (path, id, kind)
463 by Martin Pool
- compare_trees() also reports unchanged files
204
    unchanged
475 by Martin Pool
- rewrite diff using compare_trees()
205
        (path, id, kind)
379 by Martin Pool
- Simpler compare_inventories() to possibly replace diff_trees
206
460 by Martin Pool
- new testing command compare-trees
207
    Each id is listed only once.
379 by Martin Pool
- Simpler compare_inventories() to possibly replace diff_trees
208
460 by Martin Pool
- new testing command compare-trees
209
    Files that are both modified and renamed are listed only in
210
    renamed, with the text_modified flag true.
463 by Martin Pool
- compare_trees() also reports unchanged files
211
212
    The lists are normally sorted when the delta is created.
379 by Martin Pool
- Simpler compare_inventories() to possibly replace diff_trees
213
    """
214
    def __init__(self):
215
        self.added = []
216
        self.removed = []
217
        self.renamed = []
218
        self.modified = []
463 by Martin Pool
- compare_trees() also reports unchanged files
219
        self.unchanged = []
379 by Martin Pool
- Simpler compare_inventories() to possibly replace diff_trees
220
531 by Martin Pool
- new utility TreeDelta.touches_file_id
221
222
    def touches_file_id(self, file_id):
223
        """Return True if file_id is modified by this delta."""
224
        for l in self.added, self.removed, self.modified:
225
            for v in l:
226
                if v[1] == file_id:
227
                    return True
228
        for v in self.renamed:
229
            if v[2] == file_id:
230
                return True
231
        return False
232
            
233
465 by Martin Pool
- Move show_status() out of Branch into a new function in
234
    def show(self, to_file, show_ids=False, show_unchanged=False):
235
        def show_list(files):
475 by Martin Pool
- rewrite diff using compare_trees()
236
            for path, fid, kind in files:
237
                if kind == 'directory':
238
                    path += '/'
239
                elif kind == 'symlink':
240
                    path += '@'
241
                    
465 by Martin Pool
- Move show_status() out of Branch into a new function in
242
                if show_ids:
243
                    print >>to_file, '  %-30s %s' % (path, fid)
244
                else:
245
                    print >>to_file, ' ', path
246
            
460 by Martin Pool
- new testing command compare-trees
247
        if self.removed:
475 by Martin Pool
- rewrite diff using compare_trees()
248
            print >>to_file, 'removed:'
465 by Martin Pool
- Move show_status() out of Branch into a new function in
249
            show_list(self.removed)
250
                
460 by Martin Pool
- new testing command compare-trees
251
        if self.added:
475 by Martin Pool
- rewrite diff using compare_trees()
252
            print >>to_file, 'added:'
465 by Martin Pool
- Move show_status() out of Branch into a new function in
253
            show_list(self.added)
254
460 by Martin Pool
- new testing command compare-trees
255
        if self.renamed:
475 by Martin Pool
- rewrite diff using compare_trees()
256
            print >>to_file, 'renamed:'
257
            for oldpath, newpath, fid, kind, text_modified in self.renamed:
460 by Martin Pool
- new testing command compare-trees
258
                if show_ids:
259
                    print >>to_file, '  %s => %s %s' % (oldpath, newpath, fid)
260
                else:
261
                    print >>to_file, '  %s => %s' % (oldpath, newpath)
465 by Martin Pool
- Move show_status() out of Branch into a new function in
262
                    
460 by Martin Pool
- new testing command compare-trees
263
        if self.modified:
475 by Martin Pool
- rewrite diff using compare_trees()
264
            print >>to_file, 'modified:'
465 by Martin Pool
- Move show_status() out of Branch into a new function in
265
            show_list(self.modified)
266
            
267
        if show_unchanged and self.unchanged:
475 by Martin Pool
- rewrite diff using compare_trees()
268
            print >>to_file, 'unchanged:'
465 by Martin Pool
- Move show_status() out of Branch into a new function in
269
            show_list(self.unchanged)
460 by Martin Pool
- new testing command compare-trees
270
271
272
483 by Martin Pool
- change 'file_list' to more explanatory 'specific_files'
273
def compare_trees(old_tree, new_tree, want_unchanged, specific_files=None):
478 by Martin Pool
- put back support for running diff or status on
274
    """Describe changes from one tree to another.
275
276
    Returns a TreeDelta with details of added, modified, renamed, and
277
    deleted entries.
278
279
    The root entry is specifically exempt.
280
281
    This only considers versioned files.
282
283
    want_unchanged
485 by Martin Pool
- move commit code into its own module
284
        If true, also list files unchanged from one version to
285
        the next.
478 by Martin Pool
- put back support for running diff or status on
286
483 by Martin Pool
- change 'file_list' to more explanatory 'specific_files'
287
    specific_files
485 by Martin Pool
- move commit code into its own module
288
        If true, only check for changes to specified names or
289
        files within them.
478 by Martin Pool
- put back support for running diff or status on
290
    """
485 by Martin Pool
- move commit code into its own module
291
292
    from osutils import is_inside_any
293
    
460 by Martin Pool
- new testing command compare-trees
294
    old_inv = old_tree.inventory
295
    new_inv = new_tree.inventory
296
    delta = TreeDelta()
475 by Martin Pool
- rewrite diff using compare_trees()
297
    mutter('start compare_trees')
478 by Martin Pool
- put back support for running diff or status on
298
485 by Martin Pool
- move commit code into its own module
299
    # TODO: match for specific files can be rather smarter by finding
300
    # the IDs of those files up front and then considering only that.
478 by Martin Pool
- put back support for running diff or status on
301
462 by Martin Pool
- New form 'file_id in tree' to check if the file is present
302
    for file_id in old_tree:
303
        if file_id in new_tree:
460 by Martin Pool
- new testing command compare-trees
304
            kind = old_inv.get_file_kind(file_id)
475 by Martin Pool
- rewrite diff using compare_trees()
305
            assert kind == new_inv.get_file_kind(file_id)
306
            
460 by Martin Pool
- new testing command compare-trees
307
            assert kind in ('file', 'directory', 'symlink', 'root_directory'), \
308
                   'invalid file kind %r' % kind
477 by Martin Pool
- fix header for listing of unknown files
309
310
            if kind == 'root_directory':
311
                continue
312
            
313
            old_path = old_inv.id2path(file_id)
314
            new_path = new_inv.id2path(file_id)
315
483 by Martin Pool
- change 'file_list' to more explanatory 'specific_files'
316
            if specific_files:
485 by Martin Pool
- move commit code into its own module
317
                if (not is_inside_any(specific_files, old_path) 
318
                    and not is_inside_any(specific_files, new_path)):
478 by Martin Pool
- put back support for running diff or status on
319
                    continue
320
460 by Martin Pool
- new testing command compare-trees
321
            if kind == 'file':
322
                old_sha1 = old_tree.get_file_sha1(file_id)
323
                new_sha1 = new_tree.get_file_sha1(file_id)
324
                text_modified = (old_sha1 != new_sha1)
325
            else:
326
                ## mutter("no text to check for %r %r" % (file_id, kind))
327
                text_modified = False
471 by Martin Pool
- actually avoid reporting unchanged files if not required
328
329
            # TODO: Can possibly avoid calculating path strings if the
330
            # two files are unchanged and their names and parents are
331
            # the same and the parents are unchanged all the way up.
332
            # May not be worthwhile.
460 by Martin Pool
- new testing command compare-trees
333
            
334
            if old_path != new_path:
475 by Martin Pool
- rewrite diff using compare_trees()
335
                delta.renamed.append((old_path, new_path, file_id, kind,
336
                                      text_modified))
460 by Martin Pool
- new testing command compare-trees
337
            elif text_modified:
475 by Martin Pool
- rewrite diff using compare_trees()
338
                delta.modified.append((new_path, file_id, kind))
471 by Martin Pool
- actually avoid reporting unchanged files if not required
339
            elif want_unchanged:
475 by Martin Pool
- rewrite diff using compare_trees()
340
                delta.unchanged.append((new_path, file_id, kind))
460 by Martin Pool
- new testing command compare-trees
341
        else:
566 by Martin Pool
- fix bug in reporting diffs between trees where files have
342
            kind = old_inv.get_file_kind(file_id)
485 by Martin Pool
- move commit code into its own module
343
            old_path = old_inv.id2path(file_id)
344
            if specific_files:
345
                if not is_inside_any(specific_files, old_path):
346
                    continue
347
            delta.removed.append((old_path, file_id, kind))
475 by Martin Pool
- rewrite diff using compare_trees()
348
349
    mutter('start looking for new files')
460 by Martin Pool
- new testing command compare-trees
350
    for file_id in new_inv:
351
        if file_id in old_inv:
352
            continue
478 by Martin Pool
- put back support for running diff or status on
353
        new_path = new_inv.id2path(file_id)
483 by Martin Pool
- change 'file_list' to more explanatory 'specific_files'
354
        if specific_files:
485 by Martin Pool
- move commit code into its own module
355
            if not is_inside_any(specific_files, new_path):
478 by Martin Pool
- put back support for running diff or status on
356
                continue
475 by Martin Pool
- rewrite diff using compare_trees()
357
        kind = new_inv.get_file_kind(file_id)
478 by Martin Pool
- put back support for running diff or status on
358
        delta.added.append((new_path, file_id, kind))
460 by Martin Pool
- new testing command compare-trees
359
            
360
    delta.removed.sort()
361
    delta.added.sort()
362
    delta.renamed.sort()
363
    delta.modified.sort()
474 by Martin Pool
- sort unchanged files
364
    delta.unchanged.sort()
460 by Martin Pool
- new testing command compare-trees
365
366
    return delta