File: wb_subversion_diff.py

package info (click to toggle)
svn-workbench 1.5.0-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,400 kB
  • ctags: 1,585
  • sloc: python: 12,163; sh: 74; makefile: 46; ansic: 9
file content (329 lines) | stat: -rw-r--r-- 11,298 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
'''
 ====================================================================
 Copyright (c) 2003-2006 Barry A Scott.  All rights reserved.

 This software is licensed as described in the file LICENSE.txt,
 which you should have received as part of this distribution.

 ====================================================================

    wb_subversion_diff.py

'''
import sys
import os
import tempfile

import pysvn

import wb_shell_commands
import wb_show_diff_frame
import wb_read_file
import wb_diff_frame

debug_diff = False

#
#   Hold the information about each path to be compared
#
class PathInfoForDiff:
    def __init__( self ):
        # peg_path and peg_revision of the starting point for this path
        self.peg_path = None
        self.peg_revision = None
        # path and revision to be compared found starting the peg
        self.path = None
        self.revision = None
        # title for the diff program
        self.title = None

    def copy( self ):
        path_info = PathInfoForDiff()

        path_info.peg_path = self.peg_path
        path_info.peg_revision = self.peg_revision
        path_info.path = self.path
        path_info.revision = self.revision
        path_info.title = self.title
        return path_info

    def pegIsEqual( self, path_info ):
        # is either is has None in the peg fields
        # say it does not match
        if( self.peg_revision is None
        or self.peg_path is None ):
            return False

        if( path_info.peg_revision is None
        or path_info.peg_path is None ):
            return False

        return (self.peg_path == path_info.peg_path
            and self.peg_revision == path_info.peg_revision)

    def printDescription( self, title ):
        print '%s PathInfoForDiff' % title
        print '      peg_path: %r' % self.peg_path
        print '  peg_revision: %r' % self.peg_revision
        print '          path: %r' % self.path
        print '      revision: %r' % self.revision
        print '         title: %r' % self.title

#
#   Diff two files using the mode selected by the user
#
#   The files can be on disk or at any revision
#

def subversionDiffFiles(
        app,
        project_info,
        old_path_info,
        new_path_info ):

    # if revision is None cannot ask svn for the contents
    mode = app.prefs.getDiffTool().diff_tool_mode
    app.log.debug( 'subversionDiffFiles: mode %r' % mode )

    # svn diff only works on versioned files
    if mode == 'svn-diff' and (old_path_info.revision is None or new_path_info.revision is None):
        mode = 'built-in'

    if mode == 'svn-diff':
        # svn will do all the work
        yield app.backgroundProcess

        diff_text = project_info.client_bg.diff(
                tempfile.mktemp(),
                old_path_info.path, old_path_info.revision,
                new_path_info.path, new_path_info.revision )

        yield app.foregroundProcess

        showDiffText( app, diff_text, old_path_info.title, new_path_info.title )

    elif mode == 'external-gui-diff':
        yield app.backgroundProcess

        ok = False
        try:
            old_temp_filename = __getLocalFilename( app, project_info, old_path_info )
            new_temp_filename = __getLocalFilename( app, project_info, new_path_info )

            ok = True

        except IOError, e:
            app.log_error( e )

        except pysvn.ClientError, e:
            app.log_client_error( e )

        yield app.foregroundProcess

        if ok:
            wb_shell_commands.GuiDiffFiles( app,
                             __processExternalCommandOptions( app,
                                app.prefs.getDiffTool().gui_diff_tool_options[:],
                                old_temp_filename, old_path_info.title,
                                new_temp_filename, new_path_info.title ) )

    elif mode == 'external-shell-diff':
        yield app.backgroundProcess

        ok = False
        try:
            old_temp_filename = __getLocalFilename( app, project_info, old_path_info )
            new_temp_filename = __getLocalFilename( app, project_info, new_path_info )

            diff_text = wb_shell_commands.ShellDiffFiles( app,
                            __processExternalCommandOptions( app,
                                app.prefs.getDiffTool().shell_diff_tool_options[:],
                                old_temp_filename, old_path_info.title,
                                new_temp_filename, new_path_info.title ) )

            ok = True

        except IOError, e:
            app.log_error( e )

        except pysvn.ClientError, e:
            app.log_client_error( e )

        yield app.foregroundProcess

        if ok:
            showDiffText( app, diff_text, old_path_info.title, new_path_info.title )
    else:
        ok = False
        yield app.backgroundProcess

        try:
            all_old_lines = __getFileContents( project_info, old_path_info )
            all_new_lines = __getFileContents( project_info, new_path_info )

            ok = True

        except IOError, e:
            app.log_error( e )

        except pysvn.ClientError, e:
            app.log_client_error( e )

        yield app.foregroundProcess

        # built-in
        if ok:
            diff_frame = wb_diff_frame.DiffFrame( app, app.frame,
                                all_old_lines, old_path_info.title, all_new_lines, new_path_info.title )
            # only show if the files could be read
            if diff_frame.isOk():
                diff_frame.showAllFolds( False )
                diff_frame.Show( True )

                app.all_diff_frames.append( diff_frame )

def subversionDiffDir(
        app,
        project_info,
        old_path_info,
        new_path_info ):

    # if revision is None cannot ask svn for the contents
    mode = app.prefs.getDiffTool().diff_tool_mode
    app.log.debug( 'subversionDiffDir: mode %r' % mode )

    # svn diff only works on versioned files
    if mode == 'svn-diff' and (old_path_info.revision is None or new_path_info.revision is None):
        mode = 'built-in'

    if mode == 'svn-diff' or True:
        # svn will do all the work
        yield app.backgroundProcess

        if debug_diff:
            old_path_info.printDescription( 'old_path_info' )
            new_path_info.printDescription( 'new_path_info' )

        if old_path_info.pegIsEqual( new_path_info ):
            if debug_diff:
                print 'diff_peg:'
                print '   url_or_path %r' % new_path_info.peg_path
                print '  peg_revision %r' % new_path_info.peg_revision
                print 'revision_start %r' % old_path_info.revision
                print '  revision_end %r' % new_path_info.revision
                print '       recurse %r' % True

            diff_text = project_info.client_bg.diff_peg(
                    tmp_path=tempfile.mktemp(),
                    url_or_path=new_path_info.peg_path,
                    peg_revision=new_path_info.peg_revision,
                    revision_start=old_path_info.revision,
                    revision_end=new_path_info.revision,
                    recurse=True )
        else:
            if debug_diff:
                print 'diff:'
                print ' url_or_path %r' % old_path_info.path
                print '   revision1 %r' % old_path_info.revision
                print 'url_or_path2 %r' % new_path_info.path
                print '   revision2 %r' % new_path_info.revision
                print '     recurse %r' % True
            diff_text = project_info.client_bg.diff(
                    tmp_path=tempfile.mktemp(),
                    url_or_path =old_path_info.path,
                    revision1=old_path_info.revision,
                    url_or_path2=new_path_info.path,
                    revision2=new_path_info.revision,
                    recurse=True )

        yield app.foregroundProcess

        showDiffText( app, diff_text, old_path_info.title, new_path_info.title )

def __processExternalCommandOptions( app, options, old_filename, old_title, new_filename, new_title ):
    # must have the left and right files
    if '%nl' not in options:
        options = options + ' %nl'
    if '%nr' not in options:
        options = options + ' %nr'

    quote = "'"
    if sys.platform == 'win32':
        quote = '"'

    # quote all replacements
    for ui_format, replacement in   (('%tl', quote + old_title + quote)
                                    ,('%tr', quote + new_title + quote)
                                    ,('%nl', quote + old_filename + quote)
                                    ,('%nr', quote + new_filename + quote)):
        options = options.replace( ui_format, replacement )

    return options

#
#   throws IOError and pysvn.ClientError
#
def __getFileContents( project_info, path_info ):
    if path_info.revision is None or path_info.revision.kind == pysvn.opt_revision_kind.working:
        all_content_lines = wb_read_file.readFileContentsAsUnicode( path_info.path ).split('\n')

    elif path_info.revision.kind == pysvn.opt_revision_kind.base:
        base_filename = os.path.join(
            project_info.wc_path,
            os.path.dirname( path_info.path ),
            '.svn', 'text-base',
            os.path.basename( path_info.path ) + '.svn-base' )
        all_content_lines = wb_read_file.readFileContentsAsUnicode( base_filename ).split('\n')

    else:
        all_content_lines = project_info.client_bg.cat(
            url_or_path=path_info.path,
            revision=path_info.revision,
            peg_revision=path_info.peg_revision ).split('\n')

    return all_content_lines

#
#   throws IOError and pysvn.ClientError
#
def __getLocalFilename( app, project_info, path_info ):
    rev_description = ''
    if path_info.revision is None or path_info.revision.kind == pysvn.opt_revision_kind.working:
        return path_info.path

    elif path_info.revision.kind == pysvn.opt_revision_kind.base:
        rev_description = 'BASE'
        base_filename = os.path.join(
            project_info.wc_path,
            os.path.dirname( path_info.path ),
            '.svn', 'text-base',
            os.path.basename( path_info.path ) + '.svn-base' )
        all_content = wb_read_file.readFileContentsAsUnicode( base_filename )

    else:
        if path_info.revision.kind == pysvn.opt_revision_kind.head:
            rev_description = 'HEAD'
        else:
           rev_description = 'R%d' % path_info.revision.number

        all_content = project_info.client_bg.cat(
                url_or_path=path_info.peg_path,
                peg_revision=path_info.peg_revision,
                revision=path_info.revision )

    # create a temp file with a name that is based on the original filename
    prefix = '%s-%s-' % (os.path.basename( path_info.path ), rev_description)
    fd, filename = tempfile.mkstemp( prefix=prefix, suffix='.tmp')
    os.write( fd, all_content )
    os.close( fd )

    # keep track of the temp file
    app.all_temp_files.append( filename )

    # return name to caller
    return filename

def showDiffText( app, text, old_title, new_title ):
    show_diff_frame = wb_show_diff_frame.ShowDiffFrame( app, text, old_title, new_title )

    app.all_diff_frames.append( show_diff_frame )