1
#!/usr/bin/env python2.4
 
 
2
# Copyright (C) 2006 by Canonical Ltd
 
 
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.
 
 
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.
 
 
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
 
 
19
from StringIO import StringIO
 
 
20
from subprocess import Popen, PIPE
 
 
21
from tempfile import mkdtemp
 
 
23
from bzrlib.branch import Branch
 
 
24
import bzrlib.patiencediff
 
 
25
from bzrlib.diff import internal_diff
 
 
26
from bzrlib.osutils import pathjoin
 
 
29
def patch(path, patch_lines):
 
 
30
    """Apply a patch to a branch, using patch(1).  URLs may be used."""
 
 
31
    cmd = ['patch', '--quiet', path]
 
 
33
    child_proc = Popen(cmd, stdin=PIPE)
 
 
34
    for line in patch_lines:
 
 
35
        child_proc.stdin.write(line)
 
 
36
    child_proc.stdin.close()
 
 
43
b = Branch.open_containing('.')[0]
 
 
48
    transaction = repo.get_transaction()
 
 
49
    file_list = list(repo.text_store)
 
 
50
    for i, file_id in enumerate(file_list):
 
 
51
        print "%.2f%% %d of %d %s" % ((float(i)/len(file_list) * 100), i,
 
 
52
                                    len(file_list), file_id)
 
 
53
        versioned_file = repo.text_store.get_weave(file_id, transaction)
 
 
55
        for revision_id in versioned_file.versions():
 
 
57
                old_lines = versioned_file.get_lines(last_id)
 
 
58
                new_lines = versioned_file.get_lines(revision_id)
 
 
59
                if ''.join(old_lines) == ''.join(new_lines):
 
 
62
                new_patch = StringIO()
 
 
64
                    internal_diff('old', old_lines, 'new', new_lines, new_patch,
 
 
65
                        sequence_matcher=bzrlib.patiencediff.PatienceSequenceMatcher)
 
 
67
                    file(pathjoin(temp_dir, 'old'), 
 
 
68
                         'wb').write(''.join(old_lines))
 
 
69
                    file(pathjoin(temp_dir, 'new'), 
 
 
70
                         'wb').write(''.join(new_lines))
 
 
71
                    print "temp dir is %s" % temp_dir
 
 
73
                old_patch = StringIO()
 
 
74
                internal_diff('old', old_lines, 'new', new_lines, old_patch,
 
 
75
                              sequence_matcher=difflib.SequenceMatcher)
 
 
76
                old_total += len(old_patch.getvalue())
 
 
77
                new_total += len(new_patch.getvalue())
 
 
79
                file_path = pathjoin(temp_dir, 'file')
 
 
80
                orig_file = file(file_path, 'wb')
 
 
81
                for line in old_lines:
 
 
84
                patch(file_path, new_patch)
 
 
85
                new_file = file(file_path, 'rb')
 
 
86
                assert list(new_file) == new_lines
 
 
88
    print old_total, new_total