/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
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
18
from bzrlib.trace import mutter
19
from bzrlib.errors import BzrError
20
from bzrlib.delta import compare_trees
1 by mbp at sourcefrog
import from baz patch-364
21
767 by Martin Pool
- files are only reported as modified if their name or parent has changed,
22
# TODO: Rather than building a changeset object, we should probably
23
# invoke callbacks on an object.  That object can either accumulate a
24
# list, write them out directly, etc etc.
25
568 by Martin Pool
- start adding support for showing diffs by calling out to
26
def internal_diff(old_label, oldlines, new_label, newlines, to_file):
475 by Martin Pool
- rewrite diff using compare_trees()
27
    import difflib
28
    
29
    # FIXME: difflib is wrong if there is no trailing newline.
30
    # The syntax used by patch seems to be "\ No newline at
31
    # end of file" following the last diff line from that
32
    # file.  This is not trivial to insert into the
33
    # unified_diff output and it might be better to just fix
34
    # or replace that function.
35
36
    # In the meantime we at least make sure the patch isn't
37
    # mangled.
38
39
40
    # Special workaround for Python2.3, where difflib fails if
41
    # both sequences are empty.
42
    if not oldlines and not newlines:
43
        return
44
568 by Martin Pool
- start adding support for showing diffs by calling out to
45
    ud = difflib.unified_diff(oldlines, newlines,
46
                              fromfile=old_label, tofile=new_label)
475 by Martin Pool
- rewrite diff using compare_trees()
47
1092.1.50 by Robert Collins
make diff lsdiff/filterdiff friendly
48
    ud = list(ud)
475 by Martin Pool
- rewrite diff using compare_trees()
49
    # work-around for difflib being too smart for its own good
50
    # if /dev/null is "1,0", patch won't recognize it as /dev/null
51
    if not oldlines:
52
        ud[2] = ud[2].replace('-1,0', '-0,0')
53
    elif not newlines:
54
        ud[2] = ud[2].replace('+1,0', '+0,0')
1092.1.50 by Robert Collins
make diff lsdiff/filterdiff friendly
55
    # work around for difflib emitting random spaces after the label
56
    ud[0] = ud[0][:-2] + '\n'
57
    ud[1] = ud[1][:-2] + '\n'
475 by Martin Pool
- rewrite diff using compare_trees()
58
804 by Martin Pool
Patch from John:
59
    for line in ud:
60
        to_file.write(line)
974.1.5 by Aaron Bentley
Fixed handling of missing newlines in udiffs
61
        if not line.endswith('\n'):
62
            to_file.write("\n\\ No newline at end of file\n")
475 by Martin Pool
- rewrite diff using compare_trees()
63
    print >>to_file
64
65
550 by Martin Pool
- Refactor diff code into one that works purely on
66
568 by Martin Pool
- start adding support for showing diffs by calling out to
67
571 by Martin Pool
- new --diff-options to pass options through to external
68
def external_diff(old_label, oldlines, new_label, newlines, to_file,
69
                  diff_opts):
568 by Martin Pool
- start adding support for showing diffs by calling out to
70
    """Display a diff by calling out to the external diff program."""
71
    import sys
72
    
73
    if to_file != sys.stdout:
74
        raise NotImplementedError("sorry, can't send external diff other than to stdout yet",
75
                                  to_file)
76
581 by Martin Pool
- make sure any bzr output is flushed before
77
    # make sure our own output is properly ordered before the diff
78
    to_file.flush()
79
568 by Martin Pool
- start adding support for showing diffs by calling out to
80
    from tempfile import NamedTemporaryFile
571 by Martin Pool
- new --diff-options to pass options through to external
81
    import os
568 by Martin Pool
- start adding support for showing diffs by calling out to
82
83
    oldtmpf = NamedTemporaryFile()
84
    newtmpf = NamedTemporaryFile()
85
86
    try:
87
        # TODO: perhaps a special case for comparing to or from the empty
88
        # sequence; can just use /dev/null on Unix
89
90
        # TODO: if either of the files being compared already exists as a
91
        # regular named file (e.g. in the working directory) then we can
92
        # compare directly to that, rather than copying it.
93
94
        oldtmpf.writelines(oldlines)
95
        newtmpf.writelines(newlines)
96
97
        oldtmpf.flush()
98
        newtmpf.flush()
99
571 by Martin Pool
- new --diff-options to pass options through to external
100
        if not diff_opts:
101
            diff_opts = []
102
        diffcmd = ['diff',
103
                   '--label', old_label,
104
                   oldtmpf.name,
105
                   '--label', new_label,
106
                   newtmpf.name]
107
108
        # diff only allows one style to be specified; they don't override.
109
        # note that some of these take optargs, and the optargs can be
110
        # directly appended to the options.
111
        # this is only an approximate parser; it doesn't properly understand
112
        # the grammar.
113
        for s in ['-c', '-u', '-C', '-U',
114
                  '-e', '--ed',
115
                  '-q', '--brief',
116
                  '--normal',
117
                  '-n', '--rcs',
118
                  '-y', '--side-by-side',
119
                  '-D', '--ifdef']:
120
            for j in diff_opts:
121
                if j.startswith(s):
122
                    break
123
            else:
124
                continue
125
            break
126
        else:
127
            diffcmd.append('-u')
128
                  
129
        if diff_opts:
130
            diffcmd.extend(diff_opts)
131
132
        rc = os.spawnvp(os.P_WAIT, 'diff', diffcmd)
133
        
134
        if rc != 0 and rc != 1:
135
            # returns 1 if files differ; that's OK
136
            if rc < 0:
137
                msg = 'signal %d' % (-rc)
138
            else:
139
                msg = 'exit code %d' % rc
140
                
141
            raise BzrError('external diff failed with %s; command: %r' % (rc, diffcmd))
568 by Martin Pool
- start adding support for showing diffs by calling out to
142
    finally:
143
        oldtmpf.close()                 # and delete
144
        newtmpf.close()
145
    
146
147
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
148
def show_diff(b, revision, specific_files, external_diff_options=None,
1092.1.47 by Robert Collins
make show_diff redirectable
149
              revision2=None, output=None):
619 by Martin Pool
doc
150
    """Shortcut for showing the diff to the working tree.
151
152
    b
153
        Branch.
154
155
    revision
156
        None for each, or otherwise the old revision to compare against.
157
    
158
    The more general form is show_diff_trees(), where the caller
159
    supplies any two trees.
160
    """
1092.1.47 by Robert Collins
make show_diff redirectable
161
    if output is None:
162
        import sys
163
        output = sys.stdout
475 by Martin Pool
- rewrite diff using compare_trees()
164
329 by Martin Pool
- refactor command functions into command classes
165
    if revision == None:
166
        old_tree = b.basis_tree()
167
    else:
168
        old_tree = b.revision_tree(b.lookup_revision(revision))
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
169
170
    if revision2 == None:
171
        new_tree = b.working_tree()
172
    else:
173
        new_tree = b.revision_tree(b.lookup_revision(revision2))
329 by Martin Pool
- refactor command functions into command classes
174
1092.1.47 by Robert Collins
make show_diff redirectable
175
    show_diff_trees(old_tree, new_tree, output, specific_files,
571 by Martin Pool
- new --diff-options to pass options through to external
176
                    external_diff_options)
177
178
179
180
def show_diff_trees(old_tree, new_tree, to_file, specific_files=None,
181
                    external_diff_options=None):
550 by Martin Pool
- Refactor diff code into one that works purely on
182
    """Show in text form the changes from one tree to another.
183
184
    to_files
185
        If set, include only changes to these files.
571 by Martin Pool
- new --diff-options to pass options through to external
186
187
    external_diff_options
188
        If set, use an external GNU diff and pass these options.
550 by Martin Pool
- Refactor diff code into one that works purely on
189
    """
190
329 by Martin Pool
- refactor command functions into command classes
191
    # TODO: Options to control putting on a prefix or suffix, perhaps as a format string
192
    old_label = ''
193
    new_label = ''
194
195
    DEVNULL = '/dev/null'
196
    # Windows users, don't panic about this filename -- it is a
197
    # special signal to GNU patch that the file should be created or
198
    # deleted respectively.
199
200
    # TODO: Generation of pseudo-diffs for added/deleted files could
201
    # be usefully made into a much faster special case.
202
571 by Martin Pool
- new --diff-options to pass options through to external
203
    if external_diff_options:
204
        assert isinstance(external_diff_options, basestring)
205
        opts = external_diff_options.split()
206
        def diff_file(olab, olines, nlab, nlines, to_file):
207
            external_diff(olab, olines, nlab, nlines, to_file, opts)
208
    else:
209
        diff_file = internal_diff
210
    
211
478 by Martin Pool
- put back support for running diff or status on
212
    delta = compare_trees(old_tree, new_tree, want_unchanged=False,
483 by Martin Pool
- change 'file_list' to more explanatory 'specific_files'
213
                          specific_files=specific_files)
475 by Martin Pool
- rewrite diff using compare_trees()
214
215
    for path, file_id, kind in delta.removed:
1092.1.50 by Robert Collins
make diff lsdiff/filterdiff friendly
216
        print >>to_file, '=== removed %s %r' % (kind, path)
475 by Martin Pool
- rewrite diff using compare_trees()
217
        if kind == 'file':
568 by Martin Pool
- start adding support for showing diffs by calling out to
218
            diff_file(old_label + path,
219
                      old_tree.get_file(file_id).readlines(),
220
                      DEVNULL, 
221
                      [],
222
                      to_file)
475 by Martin Pool
- rewrite diff using compare_trees()
223
224
    for path, file_id, kind in delta.added:
1092.1.50 by Robert Collins
make diff lsdiff/filterdiff friendly
225
        print >>to_file, '=== added %s %r' % (kind, path)
475 by Martin Pool
- rewrite diff using compare_trees()
226
        if kind == 'file':
568 by Martin Pool
- start adding support for showing diffs by calling out to
227
            diff_file(DEVNULL,
228
                      [],
229
                      new_label + path,
230
                      new_tree.get_file(file_id).readlines(),
231
                      to_file)
475 by Martin Pool
- rewrite diff using compare_trees()
232
233
    for old_path, new_path, file_id, kind, text_modified in delta.renamed:
1092.1.50 by Robert Collins
make diff lsdiff/filterdiff friendly
234
        print >>to_file, '=== renamed %s %r => %r' % (kind, old_path, new_path)
475 by Martin Pool
- rewrite diff using compare_trees()
235
        if text_modified:
568 by Martin Pool
- start adding support for showing diffs by calling out to
236
            diff_file(old_label + old_path,
237
                      old_tree.get_file(file_id).readlines(),
238
                      new_label + new_path,
239
                      new_tree.get_file(file_id).readlines(),
240
                      to_file)
475 by Martin Pool
- rewrite diff using compare_trees()
241
242
    for path, file_id, kind in delta.modified:
1092.1.50 by Robert Collins
make diff lsdiff/filterdiff friendly
243
        print >>to_file, '=== modified %s %r' % (kind, path)
475 by Martin Pool
- rewrite diff using compare_trees()
244
        if kind == 'file':
568 by Martin Pool
- start adding support for showing diffs by calling out to
245
            diff_file(old_label + path,
246
                      old_tree.get_file(file_id).readlines(),
247
                      new_label + path,
248
                      new_tree.get_file(file_id).readlines(),
249
                      to_file)
329 by Martin Pool
- refactor command functions into command classes
250
251
379 by Martin Pool
- Simpler compare_inventories() to possibly replace diff_trees
252
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
253
254