~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/diff.py

  • Committer: Robert Collins
  • Date: 2006-03-11 13:58:48 UTC
  • mto: (1615.1.2 bzr.mbp.integration)
  • mto: This revision was merged to the branch mainline in revision 1616.
  • Revision ID: robertc@robertcollins.net-20060311135848-789fa616b8da4662
Note potential improvements in knit adds.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
 
17
from bzrlib.delta import compare_trees
 
18
from bzrlib.errors import BzrError
 
19
from bzrlib.symbol_versioning import *
17
20
from bzrlib.trace import mutter
18
 
from bzrlib.errors import BzrError
19
 
from bzrlib.delta import compare_trees
20
21
 
21
22
# TODO: Rather than building a changeset object, we should probably
22
23
# invoke callbacks on an object.  That object can either accumulate a
23
24
# list, write them out directly, etc etc.
24
25
 
25
 
def internal_diff(old_label, oldlines, new_label, newlines, to_file):
 
26
def internal_diff(old_filename, oldlines, new_filename, newlines, to_file):
26
27
    import difflib
27
28
    
28
29
    # FIXME: difflib is wrong if there is no trailing newline.
42
43
        return
43
44
 
44
45
    ud = difflib.unified_diff(oldlines, newlines,
45
 
                              fromfile=old_label, tofile=new_label)
 
46
                              fromfile=old_filename+'\t', 
 
47
                              tofile=new_filename+'\t')
46
48
 
47
49
    ud = list(ud)
48
50
    # work-around for difflib being too smart for its own good
62
64
    print >>to_file
63
65
 
64
66
 
65
 
def external_diff(old_label, oldlines, new_label, newlines, to_file,
 
67
def external_diff(old_filename, oldlines, new_filename, newlines, to_file,
66
68
                  diff_opts):
67
69
    """Display a diff by calling out to the external diff program."""
68
70
    import sys
97
99
        if not diff_opts:
98
100
            diff_opts = []
99
101
        diffcmd = ['diff',
100
 
                   '--label', old_label,
 
102
                   '--label', old_filename+'\t',
101
103
                   oldtmpf.name,
102
 
                   '--label', new_label,
 
104
                   '--label', new_filename+'\t',
103
105
                   newtmpf.name]
104
106
 
105
107
        # diff only allows one style to be specified; they don't override.
140
142
        oldtmpf.close()                 # and delete
141
143
        newtmpf.close()
142
144
 
143
 
def show_diff(b, revision, specific_files, external_diff_options=None,
144
 
              revision2=None, output=None):
 
145
 
 
146
@deprecated_function(zero_eight)
 
147
def show_diff(b, from_spec, specific_files, external_diff_options=None,
 
148
              revision2=None, output=None, b2=None):
145
149
    """Shortcut for showing the diff to the working tree.
146
150
 
 
151
    Please use show_diff_trees instead.
 
152
 
147
153
    b
148
154
        Branch.
149
155
 
150
156
    revision
151
 
        None for each, or otherwise the old revision to compare against.
 
157
        None for 'basis tree', or otherwise the old revision to compare against.
152
158
    
153
159
    The more general form is show_diff_trees(), where the caller
154
160
    supplies any two trees.
157
163
        import sys
158
164
        output = sys.stdout
159
165
 
160
 
    if revision is None:
161
 
        old_tree = b.basis_tree()
 
166
    if from_spec is None:
 
167
        old_tree = b.bzrdir.open_workingtree()
 
168
        if b2 is None:
 
169
            old_tree = old_tree = old_tree.basis_tree()
162
170
    else:
163
 
        old_tree = b.revision_tree(revision.in_history(b).rev_id)
 
171
        old_tree = b.repository.revision_tree(from_spec.in_history(b).rev_id)
164
172
 
165
173
    if revision2 is None:
166
 
        new_tree = b.working_tree()
167
 
    else:
168
 
        new_tree = b.revision_tree(revision2.in_history(b).rev_id)
169
 
 
170
 
    show_diff_trees(old_tree, new_tree, output, specific_files,
171
 
                    external_diff_options)
172
 
 
 
174
        if b2 is None:
 
175
            new_tree = b.bzrdir.open_workingtree()
 
176
        else:
 
177
            new_tree = b2.bzrdir.open_workingtree()
 
178
    else:
 
179
        new_tree = b.repository.revision_tree(revision2.in_history(b).rev_id)
 
180
 
 
181
    return show_diff_trees(old_tree, new_tree, output, specific_files,
 
182
                           external_diff_options)
 
183
 
 
184
 
 
185
def diff_cmd_helper(tree, specific_files, external_diff_options, 
 
186
                    old_revision_spec=None, new_revision_spec=None):
 
187
    """Helper for cmd_diff.
 
188
 
 
189
   tree 
 
190
        A WorkingTree
 
191
 
 
192
    specific_files
 
193
        The specific files to compare, or None
 
194
 
 
195
    external_diff_options
 
196
        If non-None, run an external diff, and pass it these options
 
197
 
 
198
    old_revision_spec
 
199
        If None, use basis tree as old revision, otherwise use the tree for
 
200
        the specified revision. 
 
201
 
 
202
    new_revision_spec
 
203
        If None, use working tree as new revision, otherwise use the tree for
 
204
        the specified revision.
 
205
    
 
206
    The more general form is show_diff_trees(), where the caller
 
207
    supplies any two trees.
 
208
    """
 
209
    import sys
 
210
    output = sys.stdout
 
211
    def spec_tree(spec):
 
212
        revision_id = spec.in_store(tree.branch).rev_id
 
213
        return tree.branch.repository.revision_tree(revision_id)
 
214
    if old_revision_spec is None:
 
215
        old_tree = tree.basis_tree()
 
216
    else:
 
217
        old_tree = spec_tree(old_revision_spec)
 
218
 
 
219
    if new_revision_spec is None:
 
220
        new_tree = tree
 
221
    else:
 
222
        new_tree = spec_tree(new_revision_spec)
 
223
 
 
224
    return show_diff_trees(old_tree, new_tree, sys.stdout, specific_files,
 
225
                           external_diff_options)
173
226
 
174
227
 
175
228
def show_diff_trees(old_tree, new_tree, to_file, specific_files=None,
183
236
        If set, use an external GNU diff and pass these options.
184
237
    """
185
238
 
186
 
    # TODO: Options to control putting on a prefix or suffix, perhaps as a format string
187
 
    old_label = ''
188
 
    new_label = ''
 
239
    old_tree.lock_read()
 
240
    try:
 
241
        new_tree.lock_read()
 
242
        try:
 
243
            return _show_diff_trees(old_tree, new_tree, to_file,
 
244
                                    specific_files, external_diff_options)
 
245
        finally:
 
246
            new_tree.unlock()
 
247
    finally:
 
248
        old_tree.unlock()
 
249
 
 
250
 
 
251
def _show_diff_trees(old_tree, new_tree, to_file,
 
252
                     specific_files, external_diff_options):
 
253
 
 
254
    # TODO: Options to control putting on a prefix or suffix, perhaps
 
255
    # as a format string?
 
256
    old_label = 'a/'
 
257
    new_label = 'b/'
189
258
 
190
259
    DEVNULL = '/dev/null'
191
260
    # Windows users, don't panic about this filename -- it is a
207
276
    delta = compare_trees(old_tree, new_tree, want_unchanged=False,
208
277
                          specific_files=specific_files)
209
278
 
 
279
    has_changes = 0
210
280
    for path, file_id, kind in delta.removed:
211
 
        print >>to_file, '=== removed %s %r' % (kind, path)
 
281
        has_changes = 1
 
282
        print >>to_file, '=== removed %s %r' % (kind, old_label + path)
212
283
        old_tree.inventory[file_id].diff(diff_file, old_label + path, old_tree,
213
284
                                         DEVNULL, None, None, to_file)
214
285
    for path, file_id, kind in delta.added:
215
 
        print >>to_file, '=== added %s %r' % (kind, path)
 
286
        has_changes = 1
 
287
        print >>to_file, '=== added %s %r' % (kind, new_label + path)
216
288
        new_tree.inventory[file_id].diff(diff_file, new_label + path, new_tree,
217
289
                                         DEVNULL, None, None, to_file, 
218
290
                                         reverse=True)
219
291
    for (old_path, new_path, file_id, kind,
220
292
         text_modified, meta_modified) in delta.renamed:
 
293
        has_changes = 1
221
294
        prop_str = get_prop_change(meta_modified)
222
295
        print >>to_file, '=== renamed %s %r => %r%s' % (
223
 
                          kind, old_path, new_path, prop_str)
 
296
                    kind, old_label + old_path, new_label + new_path, prop_str)
224
297
        _maybe_diff_file_or_symlink(old_label, old_path, old_tree, file_id,
225
298
                                    new_label, new_path, new_tree,
226
299
                                    text_modified, kind, to_file, diff_file)
227
300
    for path, file_id, kind, text_modified, meta_modified in delta.modified:
 
301
        has_changes = 1
228
302
        prop_str = get_prop_change(meta_modified)
229
 
        print >>to_file, '=== modified %s %r%s' % (kind, path, prop_str)
 
303
        print >>to_file, '=== modified %s %r%s' % (kind, old_label + path,
 
304
                    prop_str)
230
305
        if text_modified:
231
306
            _maybe_diff_file_or_symlink(old_label, path, old_tree, file_id,
232
307
                                        new_label, path, new_tree,
233
308
                                        True, kind, to_file, diff_file)
 
309
    return has_changes
234
310
    
235
311
 
236
312
def get_prop_change(meta_modified):