1
# Copyright (C) 2004, 2005, 2006 Canonical Ltd.
1
# -*- coding: UTF-8 -*-
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
11
# GNU General Public License for more details.
13
13
# You should have received a copy of the GNU General Public License
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
24
17
from bzrlib.delta import compare_trees
25
18
from bzrlib.errors import BzrError
26
import bzrlib.errors as errors
28
from bzrlib.patiencediff import unified_diff
29
import bzrlib.patiencediff
30
from bzrlib.symbol_versioning import (deprecated_function,
32
from bzrlib.textfile import check_text_lines
33
from bzrlib.trace import mutter, warning
19
from bzrlib.symbol_versioning import *
20
from bzrlib.trace import mutter
36
22
# TODO: Rather than building a changeset object, we should probably
37
23
# invoke callbacks on an object. That object can either accumulate a
38
24
# list, write them out directly, etc etc.
40
def internal_diff(old_filename, oldlines, new_filename, newlines, to_file,
41
allow_binary=False, sequence_matcher=None,
42
path_encoding='utf8'):
26
def internal_diff(old_filename, oldlines, new_filename, newlines, to_file):
43
29
# FIXME: difflib is wrong if there is no trailing newline.
44
30
# The syntax used by patch seems to be "\ No newline at
45
31
# end of file" following the last diff line from that
333
273
diff_file = internal_diff
335
276
delta = compare_trees(old_tree, new_tree, want_unchanged=False,
336
specific_files=specific_files,
337
extra_trees=extra_trees, require_versioned=True)
277
specific_files=specific_files)
340
280
for path, file_id, kind in delta.removed:
342
print >>to_file, '=== removed %s %r' % (kind, path.encode('utf8'))
343
old_name = '%s%s\t%s' % (old_label, path,
344
_patch_header_date(old_tree, file_id, path))
345
new_name = '%s%s\t%s' % (new_label, path, EPOCH_DATE)
346
old_tree.inventory[file_id].diff(diff_file, old_name, old_tree,
347
new_name, None, None, to_file)
282
print >>to_file, '=== removed %s %r' % (kind, old_label + path)
283
old_tree.inventory[file_id].diff(diff_file, old_label + path, old_tree,
284
DEVNULL, None, None, to_file)
348
285
for path, file_id, kind in delta.added:
350
print >>to_file, '=== added %s %r' % (kind, path.encode('utf8'))
351
old_name = '%s%s\t%s' % (old_label, path, EPOCH_DATE)
352
new_name = '%s%s\t%s' % (new_label, path,
353
_patch_header_date(new_tree, file_id, path))
354
new_tree.inventory[file_id].diff(diff_file, new_name, new_tree,
355
old_name, None, None, to_file,
287
print >>to_file, '=== added %s %r' % (kind, new_label + path)
288
new_tree.inventory[file_id].diff(diff_file, new_label + path, new_tree,
289
DEVNULL, None, None, to_file,
357
291
for (old_path, new_path, file_id, kind,
358
292
text_modified, meta_modified) in delta.renamed:
360
294
prop_str = get_prop_change(meta_modified)
361
295
print >>to_file, '=== renamed %s %r => %r%s' % (
362
kind, old_path.encode('utf8'),
363
new_path.encode('utf8'), prop_str)
364
old_name = '%s%s\t%s' % (old_label, old_path,
365
_patch_header_date(old_tree, file_id,
367
new_name = '%s%s\t%s' % (new_label, new_path,
368
_patch_header_date(new_tree, file_id,
370
_maybe_diff_file_or_symlink(old_name, old_tree, file_id,
296
kind, old_label + old_path, new_label + new_path, prop_str)
297
_maybe_diff_file_or_symlink(old_label, old_path, old_tree, file_id,
298
new_label, new_path, new_tree,
372
299
text_modified, kind, to_file, diff_file)
373
300
for path, file_id, kind, text_modified, meta_modified in delta.modified:
375
302
prop_str = get_prop_change(meta_modified)
376
print >>to_file, '=== modified %s %r%s' % (kind, path.encode('utf8'), prop_str)
377
old_name = '%s%s\t%s' % (old_label, path,
378
_patch_header_date(old_tree, file_id, path))
379
new_name = '%s%s\t%s' % (new_label, path,
380
_patch_header_date(new_tree, file_id, path))
303
print >>to_file, '=== modified %s %r%s' % (kind, old_label + path,
381
305
if text_modified:
382
_maybe_diff_file_or_symlink(old_name, old_tree, file_id,
306
_maybe_diff_file_or_symlink(old_label, path, old_tree, file_id,
307
new_label, path, new_tree,
384
308
True, kind, to_file, diff_file)
386
309
return has_changes
389
def _patch_header_date(tree, file_id, path):
390
"""Returns a timestamp suitable for use in a patch header."""
391
tm = time.gmtime(tree.get_file_mtime(file_id, path))
392
return time.strftime('%Y-%m-%d %H:%M:%S +0000', tm)
395
def _raise_if_nonexistent(paths, old_tree, new_tree):
396
"""Complain if paths are not in either inventory or tree.
398
It's OK with the files exist in either tree's inventory, or
399
if they exist in the tree but are not versioned.
401
This can be used by operations such as bzr status that can accept
402
unknown or ignored files.
404
mutter("check paths: %r", paths)
407
s = old_tree.filter_unversioned_files(paths)
408
s = new_tree.filter_unversioned_files(s)
409
s = [path for path in s if not new_tree.has_filename(path)]
411
raise errors.PathsDoNotExist(sorted(s))
414
312
def get_prop_change(meta_modified):
415
313
if meta_modified: