File: xxcvs

package info (click to toggle)
xxdiff 1%3A4.0.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 4,716 kB
  • ctags: 2,245
  • sloc: cpp: 18,495; python: 6,134; sh: 1,543; ansic: 1,535; perl: 308; lex: 284; yacc: 279; lisp: 250; tcl: 213; makefile: 82
file content (492 lines) | stat: -rw-r--r-- 15,500 bytes parent folder | download | duplicates (8)
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
#!/usr/bin/env python
#******************************************************************************\
#* $Source$
#* $Id: xxcvs 744 2004-01-27 17:49:26Z blais $
#* $Date: 2004-01-27 12:49:26 -0500 (Tue, 27 Jan 2004) $
#*
#* Copyright (C) 2001 Martin Blais <blais@furius.ca>
#*
#* This program is free software; you can redistribute it and/or modify
#* it under the terms of the GNU General Public License as published by
#* the Free Software Foundation; either version 2 of the License, or
#* (at your option) any later version.
#*
#* This program is distributed in the hope that it will be useful,
#* but WITHOUT ANY WARRANTY; without even the implied warranty of
#* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#* GNU General Public License for more details.
#*
#* You should have received a copy of the GNU General Public License
#* along with this program; if not, write to the Free Software
#* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#*
#*****************************************************************************/

"""Wrapper to cvs and xxdiff.

This script forms glue around cvs and xxdiff. It allows one to go through
commonly performed file comparison tasks in relation to cvs use. See the
individual command descriptions to find out what it can do.

Extended syntax for specifying filenames with revision numbers in <args> is
<filename>@@<revision>, where revision can be a revision number or a cvs tag,
or the keyword LATEST to specify the latest revision in the main branch. If
the revision is not specified, it refers to the filein the user's checkout
space.

If you find a task that you perform often and it is not project- or
company-specific, send a suggestion to the author at blais@furius.ca.
"""

__version__ = "$Revision: 744 $"
__author__ = "Martin Blais <blais@furius.ca>"

#===============================================================================
# EXTERNAL DECLARATIONS
#===============================================================================

import sys, getopt, os.path, commands
import re, string
import time
import tempfile
import errno

import quickopts
import cvs

#===============================================================================
# LOCAL DECLARATIONS
#===============================================================================

#-------------------------------------------------------------------------------
#
def getFileContents(file):
    ( name, revision ) = file
    if not revision:
        return None

    #if not Options.debug:
    #    redir = ' 2> /dev/null'
    #else:
    #    redir = ' 2> /tmp/xxdiff.debug_log'
    # Cannot redirect for now because it needs to display password prompt.
    redir = ''
    pipe = os.popen( gopts.cvs + ' update -p -r' + revision + ' ' + \
                     name + redir )
    contents = pipe.read()
    pipe.close()
    return contents

#-------------------------------------------------------------------------------
#
def getFileContentsTmp(file):
    ( name, revision ) = file
    if not revision:
        return None

    # open temp file.
    tfname = tempfile.mktemp( '.xxdiff.tmp' )
    tf = open( tfname, 'w' )
    os.unlink( tfname )

    #if not Options.debug:
    #    redir = ' 2> /dev/null'
    #else:
    #    redir = ' 2> /tmp/xxdiff.debug_log'
    # Cannot redirect for now because it needs to display password prompt.
    redir = ''

    pipe = os.popen( gopts.cvs + ' update ' + \
                     '-p -r' + revision + ' ' + name + redir )
    tf.write( pipe.read() )
    pipe.close()
    return tf


#-------------------------------------------------------------------------------
#
def runDiff(cargs):
    xargs = string.split( gopts.xxdiffargs ) + cargs
    run( gopts.xxdiff, xargs )
    if not gopts.nowait:
        os.wait()



##------------------------------------------------------------------------------
##
#def run(program, args, stdinn):
#    if gopts.debug:
#        print "running:", program, string.join( args )
#    try:
#        command = string.join( [ program ] + args )
#        ( child_stdin, child_stdout ) = os.popen2( command )
#        if stdinn:
#            child_stdin.write( stdinn )
#        #sopts = os.P_NOWAIT
#        #os.spawnv( sopts, program, [ program ] + args )
#
#        child_stdin.close()
#        child_stdout.close()
#    except os.error, e:
#        sys.stderr.write( "Error spawning program:" + e.strerror + "\n" )
#

#-------------------------------------------------------------------------------
#
def run(program, args, stdinn = None):
    command = string.join( [ program ] + filter( lambda x: x, args ) )

    if gopts.debug or gopts.fake:
        print "running:", command
    if gopts.fake:
        return
    try:
        ( child_stdin, child_stdout ) = os.popen2( command )
        if stdinn:
            child_stdin.write( stdinn )
        #sopts = os.P_NOWAIT
        #os.spawnv( sopts, program, [ program ] + args )

        child_stdin.close()
        child_stdout.close()
    except os.error, e:
        sys.stderr.write( "Error spawning program:" + e.strerror + "\n" )

#-------------------------------------------------------------------------------
#
def checkEqualFiles(files):
    """Compares filename to make sure that no two of them are the same."""
    # FIXME todo
    pass


        
    
#===============================================================================
# CLASS ExtCmdBase
#===============================================================================

class ExtCmdBase(quickopts.CmdBase):
    """Base class for command classes."""

    def __init__(self):
        quickopts.CmdBase.__init__( self )
        self.child_stdin = None
        
    def getFiles(self, names):
        # get files
        for f in names:
            ( name, revision ) = f
            if revision:
                if not self.child_stdin:
                    self.child_stdin = getFileContents( f )
                    f.append( None )
                else:
                    f.append( getFileContentsTmp( f ) )


#===============================================================================
# CLASS CmdDiff
#===============================================================================

class CmdDiff(ExtCmdBase):

    """The diff subcommand is used simply to launch xxdiff on a set of files.
    Take advantage of the handy extended syntax that this script provides."""

    _name = 'diff'
    _description = "diff files, graphically."

    def __init__(self):
        ExtCmdBase.__init__( self )

    def usage(self):
        print self.__doc__

    def execute(self, args):
        files = map( lambda x: cvs.File(x), args )

        if not args:
            print "No arguments specified."
            print "Type \""+ os.path.basename( sys.argv[0] ),
            print self._name, "--help\" for more info on this command."
            sys.exit( 1 )

        fal = []
        i = 1
        for f in files:
            # FIXME implement this
            if len( f ) > 2:
                if f[2]:
                    fal.append( f[2] )
                else:
                    fal.append( '-' )
                    fal.append( '--title' + repr(i) + '=\"' + f[0] + \
                                ' (CVS-' + f[1] + ')\"' )
            else:
                fal.append( f[0] )
                i=i+1
        
        runDiff( string.split( gopts.xxdiffargs ) + fal, \
                 self.child_stdin )

    
#===============================================================================
# CLASS CmdPrevious
#===============================================================================

class CmdPrevious(ExtCmdBase):

    """(no description yet FIXME.)"""
    
    _name = 'previous'
    _description = "diffs each file with its previous revision."

    def __init__(self):
        ExtCmdBase.__init__( self )
        
    def usage(self):
        print self.__doc__

    def execute(self, args):
        files = map( lambda x: cvs.File(x), args )
        for f in files:
            # First run a status to figure out if this file is up-to-date.
            status = f.getStatus()
            print status
            if status == cvs.File.UP_TO_DATE:
                current = f.getWorkingRevision()
                previous = f.getPreviousRevision()
                if gopts.verbose:
                    print "------------------------------"
                    print "working revision (Up-to-date): ", current
                    print "previous revision:", previous
                    print "------------------------------"

                cargs = [ current, previous ]
                checkEqualFiles( cargs )
                runDiff( cargs )

            elif status == cvs.File.LOCALLY_MODIFIED or \
                 status == cvs.File.NEEDS_MERGE or \
                 status == cvs.File.FILE_HAD_CONFLICTS_ON_MERGE:
                pass
                # FIXME todo: diff local version against ancestor

            else:
                sys.stderr.write( \
                    "Error: file " + f.fullname + " has status " + \
                    cvs.File._statii[status] + "\n" )
                sys.exit( 1 )


#===============================================================================
# CLASS CmdBranch
#===============================================================================

class CmdBranch(ExtCmdBase):

    """FIXME no description yet"""
    
    _name = 'branch'
    _description = "diffs each file with the previous tagged revision."

    def usage(self):
        print self.__doc__

    def __init__(self):
        ExtCmdBase.__init__( self )

    # FIXME todo


#===============================================================================
# CLASS CmdMerge
#===============================================================================

class CmdMerge(ExtCmdBase):

    """ Diffs each file with its ancestor and the latest in the main branch, if
it is different. Diff hunks that can be resolved are automatically selected."""


    _name = 'merge'
    _description = "diff with each ancestor"

    def usage(self):
        print self.__doc__

    def __init__(self):
        ExtCmdBase.__init__( self )
        
    def execute(self, args):
        # FIXME todo
        # Note: I've got this code already done in xxct for ccase
        pass

#===============================================================================
# CLASS CmdConflict
#===============================================================================

class CmdConflict(ExtCmdBase):

    """ Takes a file that has been merged with conflicts and splits files
according to the conflicts into two or three individual files and invokes diff
on those. This allows one to resolve the already updated merge conflicts with
xxdiff. If there was only two files in conflict, you can also specify an
ancestor file to include in the graphical diff.  """

    _name = 'conflict'
    _description = "diff by splitting merge conflicts"

    def usage(self):
        print self.__doc__

    def __init__(self):
        ExtCmdBase.__init__( self )
        self.optmap[ 'ancrev' ] = [ 'r', 0, \
 "Specify an ancestor revision to show in the graphical diff." ]
        self.optmap[ 'ancfile' ] = [ 'a', 0, \
 "Specify an ancestor file to show in the graphical diff." ]
        
    def execute(self, args):
        # FIXME todo
        # Note: I've got this code already done in xxct for ccase
        pass

#===============================================================================
# CLASS CmdUpdate
#===============================================================================

class CmdUpdate(ExtCmdBase):

    """ Updates the specified files (or all hierarchy if none specified), and
invoke graphical diff/merge tool to resolve conflicts if any are present.  """

    _name = 'update'
    _description = "updates, with graphical merge where needed."
    
    def usage(self):
        print self.__doc__
        
    def __init__(self):
        ExtCmdBase.__init__( self )
        
    def execute(self, args):
        # FIXME todo
        # first parse output of status or some benine update call to figure out
        # what to merge.
        pass


#===============================================================================
# CLASS CmdCoAll
#===============================================================================

class CmdCoAll(ExtCmdBase):

    """checks out all the revisions of a particular file in the following
format: filename-rXXX, where XXX is the revision number.  """

    _name = 'coall'
    _description = "checks out all revisions of a file."
    
    def usage(self):
        print self.__doc__

    def __init__(self):
        ExtCmdBase.__init__( self )
        self.optmap[ 'dir' ] = [ 'd', 0, \
 "First create a subdirectory file-revisions before extracting revisions." ]

    def execute(self, args):
        for file in args:
            pdir = ''
            if self.opts.dir:
                pdir = file.fullname + "-revisions"
                try:
                    os.mkdir( pdir )
                except:
                    sys.stderr.write( "Error: cannot create directory " + \
                                      pdir + "\n" )
                    sys.exit( 1 )

            revisions = file.getAllRevisions()
            for rev in revisions:
                print "Checking out revision:", rev 
                cvs.run( "update -p -r" + rev + " " + file.fullname + \
                         " > " + pdir + '/' + file.fullname + "-r" + rev )


#===============================================================================
# MAIN
#===============================================================================

# Declare global options
goptmap = {}
goptmap[ 'help' ] = [ 'h', 0, "Prints this help." ]

goptmap[ 'verbose' ] = [ 'v', 0, \
 "Verbose mode. Prints lots of stuff on stdout." ]

goptmap[ 'version' ] = [ 'V', 0, "Prints version." ]

goptmap[ 'debug' ] = [ 'd', 0, "Self debugging utility (do not use)." ]

goptmap[ 'fake' ] = [ 'n', 0, \
 "Print the commands that would be executed, but do not execute them." ]

goptmap[ 'nowait' ] = [ 'w', 0, \
 "Don't wait after spawning child processes." ]

goptmap[ 'cvs' ] = [ 'c', 1, "Specify cvs program location." ]

goptmap[ 'xxdiff' ] = [ 'g', 1, "Specify xxdiff program location." ]

goptmap[ 'xxdiffargs' ] = [ 'A', 1, \
 "Arguments to pass directly to diff program." ]

# Declare subcommands
subcmds = [ CmdDiff(), \
            CmdPrevious(), \
            CmdBranch(), \
            CmdMerge(), \
            CmdConflict(), \
            CmdUpdate(), \
            CmdCoAll() ]
subcmdlist = map( lambda x: x._name, subcmds )

# Initialization
gopts = quickopts.Options()

gopts.cvs = 'cvs'
gopts.xxdiff = 'xxdiff'
gopts.xxdiffargs = ''

# Parsing
subargs = gopts.parse( sys.argv[1:], goptmap, subcmdlist )

if gopts.version:
    quickopts.printVersion( __version__ )
    sys.exit(1)

if not subargs or gopts.help:
    quickopts.printMultiUsage( gopts, __doc__, \
        map( lambda x: [x._name, x._description], subcmds ) )
    sys.exit( 1 )

if subargs[0] not in subcmdlist:
    sys.stderr.write( "Error: unknown subcommand.\n" )
    quickopts.printMultiUsage( gopts, __doc__, \
        map( lambda x: [x._name, x._description], subcmds ) )
    sys.exit( 1 )

subcmd = subargs[0]
subargs = subargs[1:]

# Run command
for sc in subcmds:
    if sc._name == subcmd:
        quickopts.CmdBase.execute( sc, subargs )