Subversion Repositories DevTools

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
267 dpurdie 1
########################################################################
2
# Copyright (C) 2008 ERG Limited, All rights reserved
3
#
4
# Module name   : jats.sh
5
# Module type   : Makefile system
6
# Compiler(s)   : n/a
7
# Environment(s): jats
8
#
9
# Description   : JATS Subversion Interface Functions
10
#
11
#                 Requires a subversion client to be present on the machine
12
#                 Does require at least SubVersion 1.5
13
#                 Uses features not available in 1.4
14
#
15
#                 The package currently implements a set of functions
16
#                 There are some intentional limitations:
17
#                   1) Non recursive
18
#                   2) Errors terminate operation
19
#
20
#                 This package contains experimental argument passing
21
#                 processes. Sometimes use a hash of arguments
22
#
23
#......................................................................#
24
 
25
require 5.008_002;
26
use strict;
27
use warnings;
341 dpurdie 28
our $USER;
267 dpurdie 29
use JatsEnv;
30
 
31
package JatsSvn;
32
 
33
use JatsError;
34
use JatsSystem;
35
use JatsSvnCore qw(:All);
36
 
37
use File::Path;             # Instead of FileUtils
38
use File::Basename;
39
use Cwd;
40
 
41
 
42
# automatically export what we need into namespace of caller.
43
use Exporter();
44
our (@ISA, @EXPORT, %EXPORT_TAGS, @EXPORT_OK);
45
@ISA         = qw(Exporter JatsSvnCore);
46
 
47
@EXPORT      = qw(
48
                    NewSession
49
                    NewSessionByWS
50
                    NewSessionByUrl
51
 
52
                    SvnRmView
53
                    SvnIsaSimpleLabel
54
                    SvnComment
55
 
56
                    SvnUserCmd
361 dpurdie 57
 
58
                    SvnPath2Url
369 dpurdie 59
                    SvnPaths
267 dpurdie 60
                );
61
@EXPORT_OK =  qw(
62
                );
63
 
64
%EXPORT_TAGS = (All => [@EXPORT, @EXPORT_OK]);
65
 
66
#
67
#   Global Variables
68
#
69
 
70
#-------------------------------------------------------------------------------
71
# Function        : SvnCo
72
#
73
# Description     : Create a workspace
74
#                   Can be used to extract files, without creating the
75
#                   subversion control files.
76
#
1403 dpurdie 77
# Inputs          : $self                   - Instance data
78
#                   $RepoPath               - Within the repository
79
#                   $Path                   - Local path
80
#                   Hash of Options
81
#                           export          - Bool: Export Only
82
#                           force           - Bool: Force export to overwrite
83
#                           print           - Bool: Don't print files exported
84
#                           pretext=aa      - Text: Display before operation
267 dpurdie 85
#
86
# Returns         : Nothing
87
#
88
sub SvnCo
89
{
1403 dpurdie 90
    my $self = shift;
91
    my $RepoPath = shift;
92
    my $path = shift;
93
    my %opt = @_;
94
 
341 dpurdie 95
    Debug ("SvnCo", $RepoPath, $path);
1403 dpurdie 96
    Error ("SvnCi: Odd number of args") unless ((@_ % 2) == 0);
267 dpurdie 97
 
98
    #
1403 dpurdie 99
    #   Set some defaults
100
    #
101
    my $cmd = $opt{export} ? 'export' : 'checkout';
102
    my $print = exists $opt{print} ? $opt{print} : 1;
103
    $self->{CoText} =  $opt{pretext} || 'Extracting';
104
 
105
    #
267 dpurdie 106
    #   Ensure that the output path does not exist
107
    #   Do not allow the user to create a local work space
108
    #   where one already exists
109
    #
110
    Error ("SvnCo: No PATH specified" ) unless ( $path );
1403 dpurdie 111
    Error ("SvnCo: Target path already exists", "Path: " . $path ) if ( ! $opt{force} && -e $path  );
267 dpurdie 112
 
113
    #
114
    #   Build up the command line
115
    #
1403 dpurdie 116
    my @args = $cmd;
267 dpurdie 117
    push @args, qw( --ignore-externals );
1403 dpurdie 118
    push @args, qw( --force ) if ( $opt{force} );
267 dpurdie 119
    push @args, $RepoPath, $path;
120
 
121
    my @co_list;
122
    if ( $self->SvnCmd ( @args,
123
                            {
124
                                'process' => \&ProcessCo,
125
                                'data' => \@co_list,
126
                                'credentials' => 1,
127
                                'nosavedata' => 1,
1403 dpurdie 128
                                'printdata' => $print,
267 dpurdie 129
                            }
130
                       ) || @co_list )
131
    {
132
        #
133
        #   We have a checkout limitation
134
        #   Delete the workspace and then report the error
135
        #
385 dpurdie 136
        #   Note: For some reason a simple rmtree doesn't work
137
        #         Nor does glob show all the directories
138
        #
267 dpurdie 139
        Verbose2 ("Remove WorkSpace: $path");
140
        rmtree( $path, IsVerbose(3) );
385 dpurdie 141
        rmtree( $path, IsVerbose(3) );
267 dpurdie 142
        Error ("Checking out Workspace", @{$self->{ERROR_LIST}}, @co_list );
143
    }
144
    return;
145
 
146
    #
147
    #   Internal routine to scan each the checkout
148
    #
149
    #   Due to the structure of a SubVersion repository it would be
150
    #   possible for a user to extract the entire repository. This is
151
    #   not good as the repo could be very very large
152
    #
153
    #   Assume that the structure of the repo is such that our
154
    #   user is not allowed to extract a directory tree that contains
155
    #   key paths - such as /tags/ as this would indicate that they are
156
    #   attempting to extract something that is not a package
157
    #
158
    #
159
    sub ProcessCo
160
    {
161
        my $self = shift;
1329 dpurdie 162
        my $data = shift;
163
 
164
        if ( $self->{PRINTDATA} )
267 dpurdie 165
        {
1329 dpurdie 166
            #
167
            #   Pretty display for user
1403 dpurdie 168
            #   Hide some noise, but not much
1329 dpurdie 169
            #
1403 dpurdie 170
            unless ( $data =~ m~^Export complete.~ )
171
            {
172
                Information1 ( $self->{CoText} . ': ' . $data);
173
            }
1329 dpurdie 174
        }
175
 
176
        if (  $data =~ m~((/)(tags|branches|trunk)(/|$))~ )
177
        {
267 dpurdie 178
            my $bad_dir = $1;
179
            push @{$self->{ERROR_LIST}}, "Checkout does not describe the root of a package. Contains: $bad_dir";
180
            return 1;
181
        }
182
 
183
        ##
184
        ##   Limit the size of the WorkSpace
185
        ##   This limit is a bit artificial, but does attempt to
186
        ##   limit views that encompass too much.
187
        ##
188
        #if ( $#{$self->{RESULT_LIST}} > 100 )
189
        #{
190
        #    Warning ("View is too large - DEBUG USE ONLY. WILL BE REMOVED" );
191
        #    push @{$self->{ERROR_LIST}}, "View too large";
192
        #    return 1;
193
        #}
194
    }
195
}
196
 
197
#-------------------------------------------------------------------------------
1403 dpurdie 198
# Function        : SvnSwitch
199
#
200
# Description     : Switches files and directories
201
#
202
# Inputs          : $self               - Instance data
203
#                   $RepoPath           - Within the repository
204
#                   $Path               - Local path
205
#                   Options             - Options
206
#                           --NoPrint   - Don't print files exported
207
#
208
# Returns         : Nothing
209
#
210
sub SvnSwitch
211
{
212
    my ($self, $RepoPath, $path, @opts) = @_;
213
    my $printdata = ! grep (/^--NoPrint/, @opts );
214
    Debug ("SvnSwitch", $RepoPath, $path);
215
 
216
    #
217
    #   Build up the command line
218
    #
219
    my @sw_list;
220
    if ( $self->SvnCmd ( 'switch', $RepoPath, $path,
221
                            {
222
                                'process' => \&ProcessSwitch,
223
                                'data' => \@sw_list,
224
                                'credentials' => 1,
225
                                'nosavedata' => 1,
226
                                'printdata' => $printdata,
227
                            }
228
                       ) || @sw_list )
229
    {
230
        #
231
        #   We have a switch problem
232
        #   Delete the workspace and then report the error
233
        #
234
        #   Note: For some reason a simple rmtree doesn't work
235
        #         Nor does glob show all the directories
236
        #
237
        Verbose2 ("Remove WorkSpace: $path");
238
        rmtree( $path, IsVerbose(3) );
239
        rmtree( $path, IsVerbose(3) );
240
        Error ("Switch elements", @{$self->{ERROR_LIST}}, @sw_list );
241
    }
242
    return;
243
 
244
    #
245
    #   Internal routine to scan each line of the Switch output
246
    #   Use to provide a nice display
247
    #
248
    sub ProcessSwitch
249
    {
250
        my $self = shift;
251
        my $data = shift;
252
 
253
        if ( $self->{PRINTDATA} )
254
        {
255
            #
256
            #   Pretty display for user
257
            #
258
            Information1 ("Switching : $data");
259
        }
260
    }
261
}
262
 
263
#-------------------------------------------------------------------------------
267 dpurdie 264
# Function        : SvnCi
265
#
266
# Description     : Check in the specified WorkSpace
267
#
268
# Inputs          : $self           - Instance data
269
#                   A hash of named arguments
270
#                       comment     - Commit comment
379 dpurdie 271
#                       allowSame   - Allow no change to the workspace
267 dpurdie 272
#
273
# Returns         : Tag of the checkin
274
#
275
sub SvnCi
276
{
277
    my $self = shift;
278
    my %opt = @_;
279
    my $status_url;
379 dpurdie 280
    my $ws_rev;
267 dpurdie 281
 
282
    Debug ("SvnCi");
283
    Error ("SvnCi: Odd number of args") unless ((@_ % 2) == 0);
284
 
285
    #
286
    #   Validate the source path
1329 dpurdie 287
    #   Note: populates %{$self->{InfoWs}} with 'info' data
267 dpurdie 288
    #
289
    my $path = SvnValidateWs ($self, 'SvnCi');
290
 
291
    #
1329 dpurdie 292
    #   Examine %{$self->{InfoWs}}, which has the results of an 'info'
267 dpurdie 293
    #   command the locate the URL.
294
    #
295
    #   This contains the target view space
296
    #   Sanity test. Don't allow Checkin to a /tags/ area
297
    #
1329 dpurdie 298
    $status_url = $self->{InfoWs}{URL};
299
    $ws_rev = $self->{InfoWs}{Revision};
379 dpurdie 300
 
267 dpurdie 301
    Error ("SvnCi: Cannot determine Repositoty URL")
302
        unless ( $status_url );
303
 
304
    Error ("SvnCi: Not allowed to commit to a 'tags' area", "URL: $status_url")
305
        if ( $status_url =~ m~/tags(/|$)~ );
306
 
307
    #
308
    #   Commit
1329 dpurdie 309
    #   Will modify Repo, so kill the cached Info
310
    #   Will only be a real issue if we tag in the same session
267 dpurdie 311
    #
1329 dpurdie 312
    delete $self->{'InfoWs'};
313
    delete $self->{'InfoRepo'};
314
 
267 dpurdie 315
    $self->SvnCmd ( 'commit', $path
316
                    , '-m', SvnComment( $opt{'comment'}, 'Created by SvnCi' ),
317
                    , { 'credentials' => 1,
318
                        'process' => \&ProcessRevNo,
379 dpurdie 319
                        'error' => "SvnCi: Copy Error",
320
                         }
267 dpurdie 321
                        );
379 dpurdie 322
 
323
    #
324
    #   No error and no commit
325
    #   Workspace was not changed, may be allowed
326
    #
327
    if ( ! $self->{REVNO} && $opt{allowSame} )
328
    {
329
        Warning ("SvnCi: Workspace matches Repository. No commit");
330
        $self->{REVNO} = $ws_rev;
331
    }
332
 
267 dpurdie 333
    Error ("SvnCi: Cannot determine Revision Number", @{$self->{RESULT_LIST}})
334
        unless ( $self->{REVNO} );
335
 
336
    #
337
    #   Update the view
338
    #   Doing this so that the local view contains an up to date
339
    #   revision number. If not done, and a 'copy' is done based
340
    #   on this view then the branch information will indicate that
341
    #   the copy is based on an incorrect version.
342
    #   This can be confusing!
343
    #
344
    $self->SvnCmd ( 'update'   , $path
345
                        , '--ignore-externals'
346
                        , { 'credentials' => 1,
347
                            'error' => "SvnCi: Updating WorkSpace" }
348
                        );
349
    #
350
    #   Pass the updated revision number back to the user
351
    #
352
    $self->CalcRmReference($status_url);
379 dpurdie 353
    Message ("Commit Tag is: " . $self->{RMREF} );
267 dpurdie 354
    return $self->{RMREF} ;
355
}
356
 
357
#-------------------------------------------------------------------------------
358
# Function        : SvnCreatePackage
359
#
360
# Description     : Create a package and any associated files
361
#
362
# Inputs          : $self        - Instance data
363
#                   A hash of named arguments
1403 dpurdie 364
#                       package     - Name of the package
365
#                                     May include subdirs
366
#                       new         - True: Must not already exist
367
#                       replace     - True: Replace targets
368
#                       import      - DirTree to import
369
#                       label       - Tag for imported DirTree
370
#                       type        - Import TTB target
371
#                       printdata   - True: Print extracted files (default)
267 dpurdie 372
#
1403 dpurdie 373
#
267 dpurdie 374
# Returns         : Revision of the copy
375
#
376
sub SvnCreatePackage
377
{
378
    my $self = shift;
379
    my %opt = @_;
380
    my $target;
381
 
382
    Debug ("SvnCreatePackage", @_);
383
    Error ("Odd number of args to SvnCreatePackage") unless ((@_ % 2) == 0);
384
    my %dirs = ( 'trunk/'       => 0,
385
                 'tags/'        => 0,
386
                 'branches/'    => 0 );
387
 
388
    #
1403 dpurdie 389
    #   Sanity Tests and defaul values
267 dpurdie 390
    #
391
    my $package = $self->Full || Error ("SvnCreatePackage: No package name provided");
392
    Error ("SvnCreatePackage: Invalid import path") if ( $opt{'import'} && ! -d $opt{'import'} );
393
    Error ("SvnCreatePackage: Tag without Import") if ( $opt{'label'} && ! $opt{'import'} );
394
    $opt{'label'} = SvnIsaSimpleLabel( $opt{'label'} ) if (  $opt{'label'} );
1403 dpurdie 395
    $opt{'printdata'} = 1 unless ( exists $opt{'printdata'} );
267 dpurdie 396
 
397
    #
398
    #   Package path cannot contain any of the keyword paths tags,trunk,branches
399
    #   as this would place a package with a package
400
    #
401
    Error ("Package path contains a reserved word ($1)", "Path: $package")
402
        if (  $package =~ m~/(tags|branches|trunk)(/|$)~ );
403
 
404
    #
405
    #   Package path cannot be pegged, or look like one
406
    #
407
    Error ("Package name contains a Peg ($1)", "Path: $package")
408
        if ( $package =~ m~.*(@\d+)$~ );
409
 
410
    #
411
    #   Determine TTB target
1403 dpurdie 412
    #   The TTB type for branches and tags also conatins the branch or tag
267 dpurdie 413
    #
414
    $opt{'type'} = 'trunk' unless ( $opt{'type'} );
1403 dpurdie 415
    if ( $opt{'type'} =~ m~^(tags|branches|trunk)(/|$)(.*)~ ) {
416
        Error ("SvnCreatePackage: TTB type ($1) must be followed by a path element")
417
            if ( (($1 eq 'tags') or ($1 eq 'branches' )) && ! $3  );
418
        Error ('SvnCreatePackage: TTB type of trunk must not be followed by a path element: ' . $opt{'type'})
419
            if ( ($1 eq 'trunk') && $3  );
420
    } else {
421
        Error ("SvnCreatePackage: Invalid TTB Type: " . $opt{'type'} );
422
    }
267 dpurdie 423
 
424
    #
425
    #   Before we import data we must ensure that the targets do not exist
426
    #   Determine the import target(s)
427
    #
428
    my $import_target;
429
    my $copy_target;
430
 
1403 dpurdie 431
    $self->{DEVBRANCH} = 'trunk';
267 dpurdie 432
    if ( $opt{'import'} )
433
    {
434
        #
435
        #   Primary target
436
        #   trunk, branck or tag
437
        #
438
        $import_target = $package . '/' . $opt{'type'};
1403 dpurdie 439
        $self->{DEVBRANCH} = $opt{'type'} ;
267 dpurdie 440
 
441
        $self->SvnValidateTarget( 'target'    => $import_target,
442
                                  'delete'    => $opt{'replace'},
443
                                  'available' => 1 );
444
 
445
        #
446
        #   Secondary target
1403 dpurdie 447
        #   Are we tagging the import too
267 dpurdie 448
        #
1403 dpurdie 449
        if ( $opt{'label'} )
267 dpurdie 450
        {
451
            $copy_target = $package . '/tags/' . $opt{'label'};
452
            $self->SvnValidateTarget( 'target'    => $copy_target,
453
                                      'delete'    => $opt{'replace'},
454
                                      'available' => 1 );
455
        }
456
    }
457
 
458
    #
459
    #   Probe to see if the package exists
460
    #
461
    my ( $ref_files, $ref_dirs, $ref_svn, $found ) = $self->SvnScanPath ( 'SvnCreatePackage', $package );
462
    if ( @$ref_dirs )
463
    {
464
        Error ("SvnCreatePackage: Package directory exists",
465
               "Cannot create a package here. Unexpected subdirectories:", @$ref_dirs);
466
    }
467
 
468
    if ( @$ref_files )
469
    {
470
        Warning ("SvnCreatePackage: Unexpected files found",
471
               "Path: $package",
472
               "Unexpected files found: @$ref_files");
473
    }
474
 
475
    if ( @$ref_svn )
476
    {
477
        #
478
        #   Do we need a new one
479
        #
480
        Error ("SvnCreatePackage: Package exists: $package") if $opt{'new'};
481
 
482
        #
483
        #   Some subversion files have been found here
484
        #   Create the rest
485
        #   Assume that the directory tree is good
486
        #
487
        #
488
        #   Some, or all, of the required package subdirectories exist
489
        #   Determine the new ones to created so that it can be done
490
        #   in an atomic step
491
        #
492
        delete $dirs{$_} foreach  ( @$ref_svn );
493
        if ( keys %dirs )
494
        {
495
            Warning ("SvnCreatePackage: Not all package subdirs present",
496
                     "Remaining dirs will be created",
497
                     "Found: @$ref_svn") if @$ref_svn;
498
        }
499
        else
500
        {
501
            Warning ("SvnCreatePackage: Package already present");
502
        }
503
    }
504
    #
505
    #   Create package directories that have not been discovered
506
    #       trunk
507
    #       branches
508
    #       tags
509
    #
510
    my @dirs;
511
    push @dirs, $package . '/' . $_ foreach ( keys %dirs );
512
    $target = $package . '/trunk';
513
 
514
    #
515
    #   Create missing directories - if any
516
    #
517
    if ( @dirs )
518
    {
519
        $self->SvnCmd ('mkdir', @dirs
379 dpurdie 520
                       , '-m', $self->Path() . ': Created by SvnCreatePackage'
267 dpurdie 521
                       , '--parents'
385 dpurdie 522
                       , { 'credentials' => 1
523
                           ,'error' => "SvnCreatePackage"
524
                           ,'process' => \&ProcessRevNo
525
                         } );
267 dpurdie 526
    }
527
 
528
    #
529
    #   Import data into the package if required
530
    #   Import data. Possible cases:
531
    #       - Import to trunk - and then tag it
532
    #       - Import to branches
533
    #       - Import to tags
534
    #
535
    if ( $import_target )
536
    {
537
        Verbose ("Importing directory into new package: $opt{'import'}");
538
 
539
        $target = $import_target;
1403 dpurdie 540
        $self->{PRINTDATA} = $opt{'printdata'};
267 dpurdie 541
        $self->SvnCmd ('import', $opt{'import'}
542
                        , $target
543
                        , '-m', 'Import by SvnCreatePackage'
544
                        , '--force'
545
                        , { 'credentials' => 1
546
                           ,'error' => "Import Incomplete"
547
                           ,'process' => \&ProcessRevNo
1403 dpurdie 548
                           ,'printdata' => $opt{'printdata'}
267 dpurdie 549
                          })
550
    }
551
 
552
    #
553
    #   If imported to the trunk AND a label is provided
554
    #   then tag the import as well.
555
    #   A simple URL copy
556
    #
557
    if ( $copy_target )
558
    {
559
        Verbose ("Labeling imported trunk: $opt{'label'} ");
560
        $target = $copy_target;
561
        $self->SvnCmd ('copy'  , $import_target
562
                        , $target
563
                        , '-m', 'Import tagged by SvnCreatePackage'
564
                        , { 'credentials' => 1
565
                          , 'process' => \&ProcessRevNo
566
                          , 'error' => "Import Incomplete" } );
567
    }
568
 
569
    #
1403 dpurdie 570
    #   If we have done very little then we won't know the version
571
    #   of the repo. Need to force it
572
    #
573
    unless ( $self->{REVNO} || $self->{WSREVNO} )
574
    {
575
        $self->SvnInfo( $package, 'InfoRepo' );
576
        $self->{REVNO}  = $self->{'InfoRepo'}{'Last Changed Rev'} || Error ("SvnCreatePackage: Bad info for Repository");
577
    }
578
 
579
 
580
    #
267 dpurdie 581
    #   Pass the updated revision number back to the user
582
    #
583
    $self->CalcRmReference($target);
1403 dpurdie 584
    Message ("Create Package Rm Ref : " . $self->RmRef);
585
    Message ("Create Package Vcs Tag: " . $self->SvnTag);
267 dpurdie 586
    return $self->{RMREF} ;
587
}
588
 
589
#-------------------------------------------------------------------------------
590
# Function        : SvnRmView
591
#
592
# Description     : Remove a Subversion view
593
#                   Will run sanity checks and only remove the view if
594
#                   all is well
595
#
596
# Inputs          : A hash of named arguments
597
#                       path     - Path to local workspace
598
#                       modified - Array of files that are allowed to be modified
599
#                       force    - True: Force deletion
600
#
601
# Returns         :
602
#
603
sub SvnRmView
604
{
605
    my %opt = @_;
606
    Debug ("SvnRmView");
607
    Error ("Odd number of args to SvnRmView") unless ((@_ % 2) == 0);
608
 
609
    #
610
    #   Sanity test
611
    #
612
    my $path = $opt{'path'} || '';
613
    my $path_length = length ($path);
614
    Verbose2 ("Delete WorkSpace: $path");
615
 
616
    #
617
    #   If the path does not exist then assume that its already deleted
618
    #
619
    unless ( $path && -e $path )
620
    {
621
        Verbose2 ("SvnRmView: Path does not exist");
622
        return;
623
    }
624
 
625
    #
626
    #   Create information about the workspace
627
    #   This will also validate the path
628
    #
361 dpurdie 629
    my $session = NewSessionByWS ( $path, 0, 1 );
267 dpurdie 630
 
631
    #
632
    #   Validate the path
633
    #
634
    $session->SvnValidateWs ($path, 'SvnRmView');
635
 
636
    #
637
    #   Ask subversion if there are any files to be updated
638
    #   Prevent deletion of a view that has modified files
639
    #
640
    unless ( $opt{'force'} )
641
    {
642
        $session->SvnWsModified ( 'cmd' => 'SvnRmView', %opt );
643
    }
644
 
645
    #
646
    #   Now we can delete it
647
    #
648
    Verbose2 ("Remove WorkSpace: $path");
649
    rmtree( $path, IsVerbose(3) );
650
}
651
 
652
 
653
#-------------------------------------------------------------------------------
654
# Function        : SvnCopyWs
655
#
656
# Description     : Copy a workspace to a new place in the repository
657
#                   Effectively 'label' the workspace
658
#
659
#                   It would appear that the 'copy' command is very clever
660
#                   If a version-controlled file has been changed
661
#                   in the source workspace, then it will automatically be
662
#                   copied. This is a trap.
663
#
664
#                   Only allow a 'copy' if there are no modified
665
#                   files in the work space (unless overridden)
666
#
1329 dpurdie 667
#                   Only allow a 'copy' if the local workspace is
668
#                   up to date with respect with the repo. It possible
669
#                   to do a 'commit' and then a 'copy' (tag) and have
670
#                   unexpected results as the workspace has not been
671
#                   updated. This is a trap.
267 dpurdie 672
#
1329 dpurdie 673
#
267 dpurdie 674
# Inputs          : $self        - Instance data
675
#                   A hash of named arguments
676
#                       path     - Path to local workspace
677
#                       target   - Location within the repository to copy to
678
#                       comment  - Commit comment
679
#                       modified - Array of files that are allowed to
680
#                                  be modified in the workspace.
385 dpurdie 681
#                       noswitch        - True: Don't switch to the new URL
682
#                       replace         - True: Delete existing tag if present
683
#                       allowLocalMods  - True: Allow complex tagging
1403 dpurdie 684
#                       noupdatecheck   - True: Do not check that the WS is up to date
267 dpurdie 685
#
686
# Returns         : Revision of the copy
687
#
688
sub SvnCopyWs
689
{
690
    my $self = shift;
691
    my %opt = @_;
385 dpurdie 692
    my $rv;
267 dpurdie 693
    Debug ("SvnCopyWs");
694
    Error ("Odd number of args to SvnCopyWs") unless ((@_ % 2) == 0);
695
    Error ("SvnCopyWs: No Workspace" ) unless ( $self->{WS} );
696
 
697
    #
698
    #   Insert defaults
699
    #
700
    my $target = $opt{target} || Error ("SvnCopyWs: Target not specified" );
701
 
702
    #
703
    #   Validate the source path
704
    #
705
    my $path = SvnValidateWs ($self, 'SvnCopyWs');
706
 
707
    #
708
    #   Validate the target
709
    #   Cannot have a 'peg'
710
    #
711
    Error ("SvnCopyWs: Target contains a Peg: ($1)", $target)
712
        if ( $target =~ m~(@\d+)\s*$~ );
713
 
714
    #
1329 dpurdie 715
    #   Ensure the Workspace is up to date
716
    #       Determine the state of the Repo and the Workspace
717
    #
1403 dpurdie 718
    unless ( $opt{noupdatecheck} )
719
    {
720
        $self->SvnInfo( $self->{WS} , 'InfoWs' );
721
        $self->SvnInfo( $self->FullWs, 'InfoRepo' );
1329 dpurdie 722
 
1403 dpurdie 723
        my $wsLastChangedRev = $self->{'InfoWs'}{'Last Changed Rev'} || Error ("SvnCopyWs: Bad info for Workspace");
724
        my $repoLastChangedRev = $self->{'InfoRepo'}{'Last Changed Rev'} || Error ("SvnCopyWs: Bad info for Repository");
1329 dpurdie 725
 
1403 dpurdie 726
        Verbose("WS Rev  : $wsLastChangedRev");
727
        Verbose("Repo Rev: $repoLastChangedRev");
728
        Error ('SvnCopyWs: The repository has been modified since the workspace was last updated.',
729
               'Possibly caused by a commit without an update.',
730
               'Update the workspace and try again.',
731
               "Last Changed Rev. Repo: $repoLastChangedRev. Ws:$wsLastChangedRev") if ( $repoLastChangedRev > $wsLastChangedRev );
732
    }
1329 dpurdie 733
 
734
    #
267 dpurdie 735
    #   Examine the workspace and ensure that there are no modified
736
    #   files - unless they are expected
737
    #
738
    $self->SvnWsModified ( 'cmd' => 'SvnCopyWs', %opt );
385 dpurdie 739
 
267 dpurdie 740
    #
741
    #   Validate the repository
742
    #   Ensure that the target does not exist
743
    #   The target may be deleted if it exists and allowed by the user
744
    #
745
    $self->SvnValidateTarget ( 'cmd'    => 'SvnCopyWs',
746
                        'target' => $target,
747
                        'delete' => $opt{replace},
748
                        'comment' => 'Deleted by SvnCopyWs'
749
                        );
369 dpurdie 750
 
267 dpurdie 751
    #
752
    #   Copy source to destination
1329 dpurdie 753
    #   Assuming the WorkSpace is up to date then, even though the source is a
754
    #   WorkSpace, the copy does not transfer data from the WorkSpace.
755
    #   It appears as though its all done on the server. This is good - and fast.
267 dpurdie 756
    #
1329 dpurdie 757
    #   If the Workspace is not up to date, then files that SVN thinks have not
758
    #   been transferred will be transferred - hence the need to update after
759
    #   a commit.
760
    #
385 dpurdie 761
    #   Moreover, files that are modified in the local workspace will
762
    #   be copied and checked into the target, but this is not nice.
267 dpurdie 763
    #
385 dpurdie 764
    $rv = $self->SvnCmd ( 'cp'  , $path
267 dpurdie 765
                        , $target
766
                        , '--parents'
767
                        , '-m', SvnComment( $opt{'comment'}, 'Created by SvnCopyWs' ),
768
                        , { 'process' => \&ProcessRevNo,
1403 dpurdie 769
                            'credentials' => 1,
770
                            'printdata' => 1,
771
                             }
385 dpurdie 772
                        );
773
    if ($rv)
267 dpurdie 774
    {
775
        #
776
        #   Error in copy
777
        #   Attempt to delete the target. Don't worry if we can't do that
778
        #
779
        my @err1 = @{$self->{ERROR_LIST}};
780
        $self->SvnCmd ( 'delete'
781
                    , $target
782
                    , '-m', 'Deleted by SvnCopyWs after creation failure'
783
                    , { 'credentials' => 1, }
784
               );
785
        Error ("SvnCopyWs: Copy Error", @err1);
786
    }
787
 
788
    Error ("SvnCopyWs: Cannot determine Revision Number", @{$self->{RESULT_LIST}})
789
        unless ( $self->{REVNO} );
790
 
791
    Verbose2 ("Copy committed as revision: " . $self->{REVNO} );
792
 
793
    unless ( $opt{'noswitch'} )
794
    {
795
        #
796
        #   Switch to the new URL
797
        #   This will link the Workspace with the copy that we have just made
798
        #
799
        $self->SvnCmd ( 'switch', $target
800
                         , $path
801
                         , { 'credentials' => 1,
802
                             'error' => "SvnCopyWs: Cannot switch to new URL" }
803
               );
804
    }
805
 
806
    #
807
    #   Pass the updated revision number back to the user
808
    #
1403 dpurdie 809
    $self->CalcRmReference($target);
353 dpurdie 810
    #Message ("Tag is: " . $self->{RMREF} );
267 dpurdie 811
    return $self->{RMREF} ;
812
}
813
 
814
#-------------------------------------------------------------------------------
815
# Function        : SvnWsModified
816
#
817
# Description     : Test a Workspace for modified files
818
#                   Allow some files to be modified
819
#
820
# Inputs          : $self           - Instance data
821
#                   A hash of named arguments
822
#                       path        - Path to local workspace
823
#                       modified    - Files that are allowed to be modified
824
#                                     Relative to the 'path'
825
#                                     May be a single file or an array of files
385 dpurdie 826
#                       allowLocalMods - Only warn about local mods
267 dpurdie 827
#                       cmd         - Command name for error reporting
828
#
829
# Returns         :
830
#
831
sub SvnWsModified
832
{
833
    my $self = shift;
834
    my %opt = @_;
835
    Debug ("SvnWsModified");
836
    Error ("Odd number of args to SvnWsModified") unless ((@_ % 2) == 0);
837
 
838
    my $cmd = $opt{'cmd'} || 'SvnWsModified';
839
 
840
    #
841
    #   Validate the path
842
    #
843
    SvnValidateWs ($self, $cmd);
844
    my $path = $self->{WS};
845
    my $path_length = length ($path);
846
    Verbose2 ("Test Workspace for Modifications: $path");
847
 
848
    #
849
    #   Ask subversion if there are any files to be updated
850
    #
851
    $self->SvnCmd ('status', $path, {'error' => "Svn status command error"} );
852
 
853
    #
854
    #   Examine the list of modified files
855
    #
856
    if ( @{$self->{RESULT_LIST}} )
857
    {
858
        #
351 dpurdie 859
        #   Create a hash of files that are allowed to change
267 dpurdie 860
        #   These are files relative to the base of the view
861
        #
862
        #   The svn command has the 'path' prepended, so this
863
        #   will be removed as we process the commands
864
        #
865
        my %allowed;
866
        my @unexpected;
867
 
868
        if ( exists $opt{'modified'}  )
869
        {
870
            $allowed{'/' . $_} = 1 foreach ( ref ($opt{'modified'}) ? @{$opt{'modified'}} : $opt{'modified'}  );
871
        }
872
 
873
        #
874
        #   Process the list of modified files
875
        #   Do this even if we aren't allowed modified files as we
876
        #   still need to examine the status and kill off junk entries
877
        #   ie: ?, I, ! and ~
878
        #
879
        #    First column: Says if item was added, deleted, or otherwise changed
880
        #      ' ' no modifications
881
        #      'A' Added
882
        #      'C' Conflicted
883
        #      'D' Deleted
884
        #      'I' Ignored
885
        #      'M' Modified
886
        #      'R' Replaced
887
        #      'X' item is unversioned, but is used by an externals definition
888
        #      '?' item is not under version control
889
        #      '!' item is missing (removed by non-svn command) or incomplete
890
        #      '~' versioned item obstructed by some item of a different kind
891
        #
892
        foreach my $entry ( @{$self->{RESULT_LIST}} )
893
        {
894
            #
895
            #   Extract filename from line
351 dpurdie 896
            #       First 8 chars are status
267 dpurdie 897
            #       Remove WS path too
898
            #
1329 dpurdie 899
            if ( length $entry >= 8 + $path_length)
900
            {
901
                my $file = substr ( $entry, 8 + $path_length );
902
                next if ( $allowed{$file} );
903
            }
267 dpurdie 904
 
905
            #
906
            #   Examine the first char and rule out funny things
907
            #
908
            my $f1 =  substr ($entry, 0,1 );
909
            next if ( $f1 =~ m{[?I!~]} );
910
            push @unexpected, $entry;
911
        }
385 dpurdie 912
 
913
        if ( @unexpected )
914
        {
915
            if ( $opt{allowLocalMods} ) {
916
                Message ("Workspace contains locally modified files:", @unexpected);
917
            } else {
918
                Error ("Workspace contains unexpected modified files", @unexpected);
919
            }
920
        }
267 dpurdie 921
    }
922
}
923
 
924
#-------------------------------------------------------------------------------
925
# Function        : SvnListPackages
926
#
927
# Description     : Determine a list of packages within the repo
928
#                   This turns out to be a very slow process
929
#                   so don't use it unless you really really need to
930
#
1403 dpurdie 931
# Inputs          : $self       - Instance data
932
#                   $repo       - Name of the repository
933
#                   Last argument may be a hash of options.
934
#                           Progress    - True: Show progress
935
#                           Show        - >1 : display matched Tags and stats
936
#                                         >2 : display Packages
937
#                           Tag         - Enable Tag Matching
938
#                                         Value is the tag to match
267 dpurdie 939
#
1403 dpurdie 940
# Returns         : Ref to an array of all packages
941
#                   Ref to an array of all packahes with matched tag
267 dpurdie 942
#
943
sub SvnListPackages
944
{
1403 dpurdie 945
    #
946
    #   Extract arguments and options
947
    #   If last argument is a hesh, then its a hash of options
948
    #
949
    my $opt;
950
    $opt = pop @_
951
        if (@_ > 0 and UNIVERSAL::isa($_[-1],'HASH'));
267 dpurdie 952
 
1403 dpurdie 953
    my ($self, $repo) = @_;
954
 
955
    my @path_list = '';
267 dpurdie 956
    my @list;
1403 dpurdie 957
    my @mlist;
267 dpurdie 958
    my $scanned = 0;
959
    Debug ("SvnListPackages");
960
    while ( @path_list )
961
    {
962
        my $path = shift @path_list;
1403 dpurdie 963
        if ( $opt->{Progress} )
964
        {
965
            Message ("Reading: " . ( $path || 'RepoRoot') );
966
        }
267 dpurdie 967
        $scanned++;
1403 dpurdie 968
        my ( $ref_files, $ref_dirs, $ref_svn, $found ) = $self->SvnScanPath ( 'Listing Packages', join( '/', $repo, $path) );
267 dpurdie 969
 
970
        #
971
        #   If there are Subversion dirs (ttb) in this directory
972
        #   then this is a package. Add to list
973
        #
974
        push @list, $path if ( @$ref_svn );
975
 
976
        #
977
        #   Add subdirs to the list of paths to explore
978
        #
979
        foreach  ( @$ref_dirs )
980
        {
1403 dpurdie 981
            chop;                                   # Remove trailing '/'
982
            push @path_list, $path ? join('/', $path , $_) : $_; # Extend the path
267 dpurdie 983
        }
984
    }
985
 
1403 dpurdie 986
    if ( $opt->{Tag} )
987
    {
988
        my $tag = $opt->{Tag};
989
        foreach my $path ( sort @list )
990
        {
991
            Message ("Testing: $path") if ( $opt->{Progress} );
992
            if ( $self->SvnTestPath ( 'Listing Packages', join('/', $repo, $path, 'tags', $tag) ) )
993
            {
994
                push @mlist, $path;
995
            }
996
        }
997
    }
998
 
999
    if ( $opt->{Show} )
1000
    {
1001
        Message ("Found Tags:", @mlist );
1002
        Message ("Found Packages:", @list ) if  $opt->{Show} > 2;
1003
        Message ("Tags Found: " . scalar @mlist );
1004
        Message ("Packages Found: " . scalar @list );
1005
        Message ("Dirs Scanned: $scanned");
1006
    }
1007
 
1008
    return \@list, \@mlist;
267 dpurdie 1009
}
1010
 
1011
#-------------------------------------------------------------------------------
1012
# Function        : ListLabels
1013
#
1014
# Description     : List labels within a given package
1015
#
1016
# Inputs          : $self               - Instance data
1017
#                   $path               - path to label source
1018
#
1019
# Returns         : Ref to an array
1020
#
1021
sub ListLabels
1022
{
1023
    my ($self, $path) = @_;
1024
    Debug ("ListLabels");
1025
 
1026
    my ( $ref_files, $ref_dirs, $ref_svn, $found ) = $self->SvnScanPath ( 'Listing Versions', $path );
1027
 
1028
    Error ("List: Path not found: $path") unless ( $found );
1029
 
1030
    #
1031
    #   Dont report files - just directories
1032
    #
1033
    return $ref_dirs;
1034
}
1035
 
1036
 
1037
#-------------------------------------------------------------------------------
1038
# Function        : SvnLocateWsRoot
1039
#
1040
# Description     : Given a WorkSpace, determine the root of the work space
1041
#                   This is not as simple as you might think
1042
#
1043
#                   Algorithm
1044
#                       svn ls ..
1045
#                       Am I in the parent directory
1046
#                       Repeat
1047
#
369 dpurdie 1048
#                   Updates 'WS' and 'WSURL'
1049
#
267 dpurdie 1050
# Inputs          : $self               - Instance data
1051
#                   $test               - True: Don't die on error
1052
#
1053
# Returns         : Root of workspace as an absolute address
1054
#                   Will not return if there is an error
1055
#
1056
sub SvnLocateWsRoot
1057
{
1058
    my ($self, $test) = @_;
1059
    my @path;
1060
    my $path = $self->{WS};
1403 dpurdie 1061
    my $found;
267 dpurdie 1062
 
1063
    Debug ("SvnLocateWsRoot");
1064
    Error ("SvnLocateWsRoot: No Workspace") unless ( $path  );
1065
    Verbose2 ("SvnLocateWsRoot: Start in $path");
1066
 
1067
    #
1068
    #   Validate the source path
1069
    #
1070
    if ( SvnValidateWs ($self, 'SvnLocateWsRoot', $test) )
1071
    {
1072
        return undef;
1073
    }
1074
 
1075
    #
1403 dpurdie 1076
    #   Under Subversion 1.7 the process is a lot easier
267 dpurdie 1077
    #
1403 dpurdie 1078
    if ( exists $self->{'InfoWs'}{'Working Copy Root Path'} )
1079
    {
1080
        #
1081
        #   WS is now known
1082
        #
1083
        $self->{WS} = $self->{'InfoWs'}{'Working Copy Root Path'};
267 dpurdie 1084
 
1403 dpurdie 1085
        #
1086
        #   Calculate WSURL
1087
        #
1088
        $self->{WSURL} = join('/', $self->{PKGROOT}, $self->{DEVBRANCH});
1089
        $found = 1;
1090
    }
1091
    else
267 dpurdie 1092
    {
1403 dpurdie 1093
        # Preversion 1.7
1094
        Warning ("Using svn < 1.7. This is not recommended");
267 dpurdie 1095
 
1403 dpurdie 1096
        #
1097
        #   Need to sanitize the users path to ensure that the following
1098
        #   algorithm works. Need:
1099
        #       1) Absolute Path
1100
        #       2) Not ending in '/'
1101
        #
369 dpurdie 1102
 
267 dpurdie 1103
        #
1403 dpurdie 1104
        #   If we have a relative path then prepend the current directory
1105
        #   An absolute path is:
1106
        #           /aaa/aa/aa
1107
        #       or  c:/aaa/aa/aa
267 dpurdie 1108
        #
1403 dpurdie 1109
        $path = getcwd() . '/' . $path
1110
            unless ( $path =~ m~^/|\w:/~  );
267 dpurdie 1111
 
1112
        #
1403 dpurdie 1113
        #   Walk the bits and remove ".." directories
1114
        #       Done by pushing non-.. elements and poping last entry for .. elements.
1115
        #   Have a leading "/" which is good.
267 dpurdie 1116
        #
1403 dpurdie 1117
        #   Create a array of directories in the path
1118
        #   Split on one or more \ or / separators
267 dpurdie 1119
        #
1403 dpurdie 1120
        foreach ( split /[\\\/]+/ , $path )
267 dpurdie 1121
        {
1403 dpurdie 1122
            next if ( $_ eq '.' );
1123
            unless ( $_ eq '..' )
1124
            {
1125
                push @path, $_;
1126
            }
1127
            else
1128
            {
1129
                Error ("SvnLocateWsRoot: Bad Pathname: $path")
1130
                    if ( $#path <= 0 );
1131
                pop @path;
1132
            }
267 dpurdie 1133
        }
1134
 
1135
        #
1403 dpurdie 1136
        #   Need to adjust the WSURL too
1137
        #   Break into parts and pop them off as we go
1138
        #   Add a dummy one to allow for the first iteration
267 dpurdie 1139
        #
1403 dpurdie 1140
        my @wsurl = (split (/[\\\/]+/ , $self->{WSURL}), 'Dummy');
369 dpurdie 1141
 
1403 dpurdie 1142
        Verbose2 ("Clean absolute path elements: @path");
1143
        PATH_LOOP:
1144
        while ( @path )
1145
        {
1146
            #
1147
            #   This directory element. Append / to assist in compare
1148
            #   Determine parent path
1149
            #
1150
            my $name = pop (@path) . '/';
1151
            my $parent = join ('/', @path );
1152
            pop @wsurl;
369 dpurdie 1153
 
1403 dpurdie 1154
            #
1155
            #   Examine the parent directory
1156
            #   Get a list of all elements in the parent
1157
            #   Need to ensure that this directory is one of them
1158
            #
1159
            #   Ignore any errors - assume that they are because the
1160
            #   parent is not a part of the work space. This will terminate the
1161
            #   search.
1162
            #
1163
            $self->SvnCmd ('list', $parent, '--depth', 'immediates' );
1164
            foreach my $entry ( @{$self->{RESULT_LIST}} )
1165
            {
1166
                next PATH_LOOP
1167
                    if ( $entry eq $name );
1168
            }
1169
 
1170
            #
1171
            #   Didn't find 'dir' in directory svn listing of parent
1172
            #   This parent is not a part of the same WorkSpace as 'dir'
1173
            #   We have a winner.
1174
            #
1175
            chop $name;                         #   Chop the '/' previously added
1176
            $self->{WS} = $parent . '/' . $name;
1177
 
1178
            #
1179
            #   Reform the WSURL. Elements have been removed as we tested up the
1180
            #   path
1181
            #
1182
            $self->{WSURL} = join '/', @wsurl;
1183
            $found = 1;
1184
            last;
1185
        }
267 dpurdie 1186
    }
1187
 
1188
    #
1189
    #   Shouldn't get this far
1190
    #
1403 dpurdie 1191
    Error ("SvnLocateWsRoot: Root not found")
1192
        unless ( $found );
1193
 
1194
    #
1195
    #   Refresh Info
1196
    #   Must kill cached copy
1197
    #
1198
    delete $self->{'InfoWs'};
1199
    $self->SvnInfo($self->{WS}, 'InfoWs');
1200
    return $self->{WS};
1201
 
267 dpurdie 1202
}
1203
 
1204
#-------------------------------------------------------------------------------
1205
# Function        : SvnValidateWs
1206
#
1207
# Description     : Validate the path to a working store
1208
#
1209
# Inputs          : $self           - Instance data
1210
#                   $user           - Optional prefix for error messages
1211
#                   $test           - True: Just test, Else Error
1212
#
1213
# Returns         : Will not return if not a workspace
1214
#                   Returns the users path
1329 dpurdie 1215
#                   Populates the hash: $self->{InfoWs}
267 dpurdie 1216
#
1217
sub SvnValidateWs
1218
{
1219
    my ($self, $user, $test) = @_;
1220
    Debug ("SvnValidateWs");
1221
 
1222
    $user = "Invalid Subversion Workspace" unless ( $user );
1329 dpurdie 1223
    my $path = $self->{WS};
267 dpurdie 1224
 
1225
    #
1329 dpurdie 1226
    #   Only validate it once
267 dpurdie 1227
    #
1228
    return $path if ( $self->{WS_VALIDATED} );
1229
 
1230
    #
1231
    #   Validate the source path
1232
    #   Must exist and must be a directory
1233
    #
1234
    if ( ! $path ) {
1235
        @{$self->{ERROR_LIST}} = "$user: No path specified";
1236
 
1237
    } elsif ( ! -e $path ) {
1238
        @{$self->{ERROR_LIST}} = "$user: Path does not exist: $path";
1239
 
1240
    } elsif ( ! -d $path ) {
1241
        @{$self->{ERROR_LIST}} = "$user: Path is not a directory";
1242
    } else {
1243
        #
1244
        #   Determine the source path is an fact a view
1245
        #   The info command can do this. Use depth empty to limit the work done
1246
        #
1329 dpurdie 1247
        $self->SvnInfo($path, 'InfoWs');
267 dpurdie 1248
 
1249
        #
1250
        #   Error. Prepend nice message
1251
        #
1252
        unshift @{$self->{ERROR_LIST}}, "$user: Path is not a WorkSpace: $path"
1253
            if ( @{$self->{ERROR_LIST}} );
1254
    }
1255
 
1256
    #
1257
    #   Figure out what to do
1258
    #
1259
    if ( $test )
1260
    {
1261
        return @{$self->{ERROR_LIST}};
1262
    }
1263
    else
1264
    {
1265
        Error @{$self->{ERROR_LIST}} if @{$self->{ERROR_LIST}};
1266
        $self->{WS_VALIDATED} = 1;
1267
        return $path;
1268
    }
1269
}
1270
 
1271
#-------------------------------------------------------------------------------
1272
# Function        : SvnValidatePackageRoot
1273
#
1274
# Description     : Validate a package root
1275
#
1276
# Inputs          : $self           - Instance data
1277
#
1278
# Returns         : Will only return if valid
1279
#                   Returns a cleaned package root
1280
#
1281
sub SvnValidatePackageRoot
1282
{
379 dpurdie 1283
    my ($self, $warning_only) = @_;
267 dpurdie 1284
    Debug ("SvnValidatePackageRoot");
1285
    my $url = $self->Full || Error ("SvnValidatePackageRoot: No URL");
1286
 
1287
    Error ("Package path contains a reserved word ($self->{TAGTYPE})", "Path: $url")
1288
        if (  $self->{TAGTYPE} );
1289
 
1290
    Error ("Package name contains a Peg ($self->{PEG})", "Path: $url")
1291
        if ( $self->{PEG} );
1292
 
1293
    #
1294
    #   Ensure that the target path does exist
1295
    #   Moreover it needs to be a directory and it should have a
1296
    #   a ttb structure
1297
    #
1298
    my ( $ref_files, $ref_dirs, $ref_svn, $found ) = $self->SvnScanPath ( 'Package Base Test', $url );
1299
 
1300
    #
379 dpurdie 1301
    #   Only looking for package path
1302
    #
1303
    if ( !$found && $warning_only )
1304
    {
1305
        return $url;
1306
    }
1307
 
1308
    #
267 dpurdie 1309
    #   Examine the results to see if we have a valid package base
1310
    #
1311
    Error ("Package Base Test: Not a valid package") unless ( $found );
1312
 
1313
    #
1314
    #   Extra bits found
1315
    #   Its not the root of a package
1316
    #
1317
    if ( @$ref_files )
1318
    {
1319
        Warning ("Package Base Test: Files exists",
1320
               "Unexpected files found:", @$ref_files );
1321
    }
1322
 
1323
    #
1324
    #   Need a truck directory
1325
    #   If we don't have a truck we don't have a package
1326
    #
1327
    my $trunk_found = grep ( /trunk\//, @$ref_svn );
1328
    Error ("Invalid Package Base. Does not contain a 'trunk' directory")
1329
        unless ( $trunk_found );
1330
 
1331
    return $url;
1332
}
1333
 
1334
 
1335
#-------------------------------------------------------------------------------
1336
# Function        : SvnIsaSimpleLabel
1337
#
1338
# Description     : Check a label
1339
#                       Must not contain a PEG
1340
#                       Must not contain invalid characters (@ or /)
1341
#                       Must not contain a :: sequence (will confuse other tools)
1342
#                       Handle special label of TIMESTAMP
1343
#
1344
# Inputs          : $label          - to test
1345
#
1346
# Returns         : Will not return on error
1347
#                   Returns label on success
1348
#
1349
sub SvnIsaSimpleLabel
1350
{
1351
    my ($label) = @_;
1352
    Debug ("SvnIsaSimpleLabel, $label");
1353
 
1354
    Error ("No label provided") unless ( $label );
1355
    Error ("Invalid label. Peg (\@nnn) is not allowed: \"$label\"" ) if ( $label =~ m~@\d+$~ );
1356
    Error ("Invalid label. Package Path is not allowed: \"$label\"" ) if ( $label =~ m~/~ );
383 dpurdie 1357
    Error ("Invalid label. Invalid Start Character: \"$label\"" ) unless ( $label =~ m~^[0-9a-zA-Z]~ );
1358
    Error ("Invalid label. Invalid End Character: \"$label\"" ) unless ( $label =~ m~[0-9a-zA-Z]$~ );
267 dpurdie 1359
    Error ("Invalid label. Invalid Characters: \"$label\"" ) unless ( $label =~ m~^[-.:0-9a-zA-Z_]+$~ );
1360
    Error ("Invalid label. Double :: not allowed: \"$label\"" ) if ( $label =~m~::~ );
1361
 
1362
    #
1363
    #   Allow for a label of TIMESTAMP and have it expand
383 dpurdie 1364
    #   Create a label based on users name and a date-time that can be sorted
267 dpurdie 1365
    #
1366
    if ( $label eq 'TIMESTAMP' )
1367
    {
341 dpurdie 1368
        ::EnvImport ('USER' );
1369
        my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
1370
        $label = sprintf("%s_%4.4u.%2.2u.%2.2u.%2.2u%2.2u%2.2u",
1371
            $::USER, $year+1900, $mon+1, $mday, $hour, $min, $sec );
267 dpurdie 1372
    }
1373
    return $label;
1374
}
1375
 
1376
#-------------------------------------------------------------------------------
1377
# Function        : NewSession
1378
#
1379
# Description     : Create a new empty SvnSession Class
1380
#
1381
# Inputs          : None
1382
#
1383
# Returns         : Class
1384
#
1385
sub NewSession
1386
{
1387
    Debug ("NewSession");
1388
    my $self  = SvnSession();
1389
 
1390
    #
1391
    #   Document Class Variables
1392
    #
1393
    $self->{URL} = '';                  # Repo URL prefix
1394
    $self->{WS}  = '';                  # Users WorkSpace
1395
    $self->{PROTOCOL} = '';             # Named Access Protocol
1396
    $self->{PKGROOT} = '';              # Package root
1397
 
1398
    #
1399
    #   Create a class
1400
    #   Bless my self
1401
    #
1402
    bless ($self, __PACKAGE__);
1403
    return $self;
1404
}
1405
 
1406
#-------------------------------------------------------------------------------
1407
# Function        : NewSessionByWS
1408
#
1409
# Description     : Establish a new SVN Session based on a Workspace
1410
#                   Given a workspace path determine the SvnServer and other
1411
#                   relevent information.
1412
#
1413
#                   Requires some rules
1414
#                       * The package is rooted within a 'ttb'
1415
#
1416
# Inputs          : $path                   - Path to WorkSpace
1417
#                   $test                   - No Error on no WS
361 dpurdie 1418
#                   $slack                  - Less stringent
267 dpurdie 1419
#
1420
# Returns         : Ref to Session Information
1421
#
1422
sub NewSessionByWS
1423
{
361 dpurdie 1424
    my ($path, $test, $slack) = @_;
267 dpurdie 1425
    Debug ("NewSessionByWS", @_);
1426
 
1427
    #
1428
    #   Create a basic Session
1429
    #   Populate it with information that is known
1430
    #
1431
    my $self = NewSession();
1432
    $self->{WS} = $path;
1433
 
1434
    #
1435
    #   Validate the path provided
1329 dpurdie 1436
    #   In the process populate $self->{InfoWs} with info about the workspace.
267 dpurdie 1437
    #
1438
    if ($self->SvnValidateWs ( undef, 1) )
1439
    {
1440
        return $self if ( $test );
1441
        Error ( @{$self->{ERROR_LIST}} );
1442
    }
1443
 
1444
    #
1445
    #   Extract useful info
1446
    #       URL: svn://auperaws996vm21/test/MixedView/trunk
1447
    #       Repository Root: svn://auperaws996vm21/test
1448
    #
1329 dpurdie 1449
    my $url = $self->{'InfoWs'}{'URL'};
1450
    my $reporoot = $self->{'InfoWs'}{'Repository Root'};
1451
    my $repoVersion = $self->{'InfoWs'}{'Revision'};
1403 dpurdie 1452
    my $devBranch;
267 dpurdie 1453
 
1454
    Error ("JatsSvn Internal error. Can't parse info")
1455
        unless ( $url && $reporoot );
1456
 
1457
    #
1458
    #   Need the length of the path to the repository
1459
    #   but not the name of the repostory itself.
1460
    #
1461
    #   Remove that from the head of the URL to give a
1462
    #   path within the repository, that includes the repos name
1463
    #
1464
    $reporoot = (fileparse( $reporoot ))[1];
1465
    $url = substr ($url, length ($reporoot));
1466
    $self->{WSURL} = $url;
1467
    chop $reporoot;
1468
 
1469
    Verbose2 ("SvnLocatePackageRoot: $reporoot, $url" );
1470
 
1471
    #
1472
    #   Remove anything after a ttb ( truck, tags, branch ) element
1473
    #   This will be the root of the package within the repo
1474
    #
1475
    if (  $url =~ m~(.+)/((tags|branches|trunk)(/|$).*)~ )
1476
    {
1477
        $url = $1;
1478
        $self->{WSTYPE} = $3;
1403 dpurdie 1479
        if ( $3 eq 'trunk' ) {
1480
            $devBranch = $3;
1481
        } elsif ( $3 eq 'branches' ) {
1482
            $devBranch = $2;
1483
        }
267 dpurdie 1484
    }
1485
    else
1486
    {
361 dpurdie 1487
        #
1488
        #   If we are being slack (ie deleting the workspace)
1489
        #   Then generate a warning, not an error
1490
        #
1491
        my $fnc = $slack ? \&Warning : \&Error;
1492
        $fnc->("SvnLocatePackageRoot. Non standard repository format",
1493
               "Url must contain 'tags' or 'branches' or 'trunk'",
267 dpurdie 1494
               "Url: $url");
361 dpurdie 1495
        $self->{WSTYPE} = 'trunk';
267 dpurdie 1496
    }
1497
 
1498
    #
1499
    #   Insert known information
1500
    #
1501
    $self->{URL} = $reporoot . '/';
1502
    $self->{PKGROOT} = $url;
369 dpurdie 1503
    $self->{WSREVNO} = $repoVersion;
1403 dpurdie 1504
    $self->{DEVBRANCH} = $devBranch;
267 dpurdie 1505
 
1506
    #
1507
    #   Create useful information
1508
    #
1509
    SplitPackageUrl($self);
1510
    return $self;
1511
}
1512
 
1513
#-------------------------------------------------------------------------------
1514
# Function        : NewSessionByUrl
1515
#
1516
# Description     : Establish a new SVN Session based on a user URL
1517
#
1518
# Inputs          : $uurl                   - Users URL
361 dpurdie 1519
#                   $ttb_test               - Test and warn for TTB structure
267 dpurdie 1520
#                   $session                - Optional: Existing session
1521
#
1522
# Returns         : Ref to Session Information
1523
#
1524
sub NewSessionByUrl
1525
{
361 dpurdie 1526
    my ($uurl, $ttb_test, $self ) = @_;
267 dpurdie 1527
    Debug ("NewSessionByUrl", @_);
1528
    Error ("No Repostory Path specified") unless ( $uurl );
1529
 
1530
    #
1531
    #   Create a basic Session
1532
    #   Populate it with information that is known
1533
    #
1534
    $self = NewSession() unless ( $self );
1535
 
1536
    #
369 dpurdie 1537
    #   Examine the URL and convert a Repository Path into a URL
353 dpurdie 1538
    #   as provided by configuration information within the environment
267 dpurdie 1539
    #
361 dpurdie 1540
    ($self->{URL}, $self->{PKGROOT} ) = SvnPath2Url ($uurl);
1541
 
1542
    #
1543
    #   Create useful information
1544
    #
1545
    SplitPackageUrl($self);
1546
 
1547
    #
1548
    #   Warn of non-standard URLs
1549
    #   These may create problems latter
1550
    #
1551
    if ( $ttb_test )
1552
    {
1553
        Warning("Non standard repository format",
1554
                 "Url should contain 'tags' or 'branches' or 'trunk'",
1555
                 "Url: $self->{PKGROOT}") unless $self->{TAGTYPE};
1556
    }
1557
 
1558
    return $self;
1559
}
1560
 
1561
#-------------------------------------------------------------------------------
1562
# Function        : SvnPath2Url
1563
#
1564
# Description     : Convert a repository path to a Full Url
1565
#                   Also handles Full Url
1566
#
1567
# Inputs          : $rpath             - Repository Path
1568
#                                        May be a full URL
1569
#
1570
# Returns         : List context
1571
#                   Two items that can be joined
1572
#                   URL                - URL
1573
#                   PKG_ROOT           - Package Root
1574
#
1575
#                   Scalar context: Joined URL and Package Root
1576
#                                   Fully formed URL
1577
#
1578
sub SvnPath2Url
1579
{
1580
    my ($rpath) = @_;
1581
    my $processed = 0;
1582
    my $url;
1583
    my $pkgroot;
1584
 
1585
    #
1586
    #   Examine the argument and convert a Repository Path into a URL
1587
    #   as provided by configuration information within the environment
1588
    #
1589
    $rpath =~ m~(.+?)/(.*)~;
1590
    my $fe = $1 || $rpath;
353 dpurdie 1591
    my $rest = $2 || '';
1592
    if ( $SVN_URLS{$fe} )
267 dpurdie 1593
    {
361 dpurdie 1594
        $url = $SVN_URLS{$fe};
1595
        $pkgroot = $rest;
353 dpurdie 1596
        $processed = 1;
341 dpurdie 1597
    }
1598
 
353 dpurdie 1599
    if ( ! $processed )
341 dpurdie 1600
    {
267 dpurdie 1601
        #
353 dpurdie 1602
        #   Examine the URL and determine if we have a FULL Url or
1603
        #   a path within the 'default' server
1604
        #
1605
        foreach my $key ( @SVN_URLS_LIST )
1606
        {
361 dpurdie 1607
            if ( $rpath =~ m~^$SVN_URLS{$key}(.*)~ )
353 dpurdie 1608
            {
361 dpurdie 1609
                $url = $SVN_URLS{$key};
1610
                $pkgroot = $1;
353 dpurdie 1611
                $processed = 1;
1612
                last;
1613
            }
1614
        }
1615
    }
267 dpurdie 1616
 
353 dpurdie 1617
    #
1618
    #   Last attempt
1619
    #   Treat as a raw URL - some operations won't be allowed
1620
    #
1621
    if ( ! $processed )
267 dpurdie 1622
    {
361 dpurdie 1623
        if ( $rpath =~ m~^((file|http|https|svn):///?([^/]+)/)(.+)~ )
353 dpurdie 1624
        {
1625
            #       http://server/
1626
            #       https://server/
1627
            #       svn://server/
1628
            #       file://This/Isa/Bad/Guess
1629
            #
361 dpurdie 1630
            $url = $1;
1631
            $pkgroot = $4;
353 dpurdie 1632
        }
369 dpurdie 1633
        elsif ($SVN_URLS{''} )
353 dpurdie 1634
        {
1329 dpurdie 1635
            if ( exists $ENV{'GBE_ABT'} && $ENV{'GBE_ABT'})
1636
            {
1637
                Error ("Attempt to use default repository within automated build", "Path: " . $rpath);
1638
            }
361 dpurdie 1639
            $url = $SVN_URLS{''};
1640
            $pkgroot = $rpath;
353 dpurdie 1641
        }
1642
        else
1643
        {
1644
            #
1645
            #   User default (site configured) Repo Root
1646
            #
1647
            Error ("No site repository configured for : $fe",
1648
                   "Configure GBE_SVN_URL_" . uc($fe) );
1649
        }
267 dpurdie 1650
    }
1651
 
1652
    #
361 dpurdie 1653
    #   May want two elements, may want one
267 dpurdie 1654
    #
361 dpurdie 1655
    return $url, $pkgroot if ( wantarray );
1656
    return $url . $pkgroot;
267 dpurdie 1657
}
1658
 
369 dpurdie 1659
#-------------------------------------------------------------------------------
1660
# Function        : SvnPaths
1661
#
1662
# Description     : Extract SVN path conversion information
1663
#
1664
# Inputs          : Nothing
1665
#
1666
# Returns         : Two refs
1667
#                   Hash of SVN URLS
1668
#                   Array for search order
1669
#
1670
sub SvnPaths
1671
{
1672
    return \%SVN_URLS, \@SVN_URLS_LIST;
1673
}
361 dpurdie 1674
 
267 dpurdie 1675
#-------------------------------------------------------------------------------
1676
# Function        : SplitPackageUrl
1677
#
1403 dpurdie 1678
# Description     : Split the package URL into a few useful bits
267 dpurdie 1679
#
1680
# Inputs          : $self           - Instance data
1681
#
1682
# Returns         : Nothing
1683
#
1684
sub SplitPackageUrl
1685
{
1686
    my ($self) = @_;
353 dpurdie 1687
    Debug ("SplitPackageUrl", $self->{URL}, $self->{PKGROOT});
267 dpurdie 1688
 
1689
    #
1690
    #   Remove any protocol that may be present
1691
    #       http://server/
341 dpurdie 1692
    #       https://server/
267 dpurdie 1693
    #       svn://server/
1694
    #       file://This/Isa/Bad/Guess
1695
    #
341 dpurdie 1696
    if ( $self->{URL} =~ m~^(file|http|https|svn)://([^/]+)~ )
267 dpurdie 1697
    {
1698
        $self->{PROTOCOL} = $1;
1699
        $self->{SERVER} = $2;
1700
    }
1701
 
1702
    if ( $self->{PKGROOT} =~ m~(.*)(@\d+)$~ )
1703
    {
1704
        $self->{PEG} = $2;
1705
    }
1706
 
1707
    #
1708
    #   Determine TTB type
1709
    #   Need to handle
1710
    #       .../trunk
1711
    #       .../trunk@nnnnn
1712
    #       .../tags/version@nnnnn
1713
    #       .../branches/version@nnnnn
1714
    #
1715
    #
1716
    if (  $self->{PKGROOT} =~ m~/?(.*)/(tags|branches|trunk)(/|$|@)(.*)$~ )
1717
    {
1718
        $self->{PATH}         = $1;
1719
        $self->{TAGTYPE}      = $2;
1720
        $self->{VERSION}      = $4;
1721
    }
1722
    else
1723
    {
1724
        $self->{PATH} = $self->{PKGROOT};
1725
    }
1726
 
1727
    DebugDumpData ('SplitPackageUrl', $self ) if ( IsDebug(2) );
1728
}
1729
 
1730
#-------------------------------------------------------------------------------
1731
# Function        : Full
1732
#                   FullWs
1733
#                   Repo
1734
#                   Peg
1735
#                   Type
1736
#                   WsType
1737
#                   Path
1738
#                   Version
1739
#                   RmRef
385 dpurdie 1740
#                   RmPath
267 dpurdie 1741
#
1742
# Description     : Accessor functions
1743
#
1744
# Inputs          : $self       - Instance data
1745
#                                 self (is $_[0])
1746
#
1747
# Returns         : Data Item
1748
#
369 dpurdie 1749
sub Full        { return $_[0]->{URL} . $_[0]->{PKGROOT} ; }
1750
sub FullWs      { return $_[0]->{URL} . $_[0]->{WSURL} ; }
1751
sub FullWsRev   { return $_[0]->{URL} . $_[0]->{WSURL} . '@' . $_[0]->{WSREVNO} ; }
1403 dpurdie 1752
sub FullPath    { return $_[0]->{URL} . $_[0]->{PATH} ; }
369 dpurdie 1753
sub Peg         { return $_[0]->{PEG} ; }
1403 dpurdie 1754
sub DevBranch   { return $_[0]->{DEVBRANCH} || '' ; }
369 dpurdie 1755
sub Type        { return $_[0]->{TAGTYPE} || '' ; }
1756
sub WsType      { return $_[0]->{WSTYPE}  || '' ; }
1757
sub Path        { return $_[0]->{PATH} ; }
1758
sub Version     { return $_[0]->{VERSION} ; }
1759
sub RmRef       { return $_[0]->{RMREF} ; }
385 dpurdie 1760
sub RmPath      { my $path = $_[0]->{RMREF}; $path =~ s~@.*?$~~ ;return  $path; }
1403 dpurdie 1761
sub SvnTag      { return $_[0]->{SVNTAG} || '' ; }
267 dpurdie 1762
 
1763
#-------------------------------------------------------------------------------
1764
# Function        : Print
1765
#
1766
# Description     : Debug display the URL
1767
#
1768
# Inputs          : $self           - Instance data
1769
#                   $header
1770
#                   $indent
1771
#
1772
# Returns         : Nothing
1773
#
1774
sub Print
1775
{
1776
    my ($self, $header, $indent) = @_;
1777
    print "$header\n" if $header;
1778
    $indent = 4 unless ( defined $indent );
1779
    $indent = ' ' x $indent;
1780
 
1781
 
1403 dpurdie 1782
    print $indent . "PROTOCOL :" . $self->{PROTOCOL} . "\n";
1783
    print $indent . "SERVER   :" . $self->{SERVER} . "\n";
1784
    print $indent . "URL      :" . $self->{URL} . "\n";
1785
    print $indent . "PKGROOT  :" . $self->{PKGROOT} . "\n";
1786
    print $indent . "PATH     :" . $self->{PATH} . "\n";
1787
    print $indent . "TAGTYPE  :" . ($self->{TAGTYPE} || '') . "\n";
1788
    print $indent . "VERSION  :" . ($self->{VERSION} || '') . "\n";
1789
    print $indent . "PEG      :" . ($self->{PEG} || '') . "\n";
1790
    print $indent . "DEVBRANCH:" . ($self->{DEVBRANCH} || '') . "\n";
1791
    print $indent . "SVNTAG   :" . ($self->{SVNTAG} || '') . "\n";
1792
#    print $indent . "FULL    :" . $self->Full . "\n";
1793
 
1794
    print $indent . "Full         :" . $self->Full . "\n";
1795
#    print $indent . "FullWs       :" . $self->FullWs    . "\n";
1796
#    print $indent . "FullWsRev    :" . $self->FullWsRev . "\n";
1797
    print $indent . "FullPath     :" . $self->FullPath  . "\n";
1798
    print $indent . "Peg          :" . $self->Peg       . "\n";
1799
    print $indent . "DevBranch    :" . $self->DevBranch . "\n";
1800
    print $indent . "Type         :" . $self->Type      . "\n";
1801
    print $indent . "WsType       :" . $self->WsType    . "\n";
1802
    print $indent . "Path         :" . $self->Path      . "\n";
1803
    print $indent . "Version      :" . $self->Version   . "\n";
1804
    print $indent . "RmRef        :" . ($self->RmRef || '') . "\n";
1805
#    print $indent . "RmPath       :" . ($self->RmPath|| '') . "\n";
267 dpurdie 1806
}
1807
 
1808
#-------------------------------------------------------------------------------
1809
# Function        : BranchName
1810
#
1811
# Description     : Create a full URL to a branch or tag based on the
1812
#                   current entry
1813
#
1814
#                   URL must have a TTB format
1815
#
1816
# Inputs          : $self           - Instance data
1817
#                   $branch         - Name of the branch
1818
#                   $type           - Optional branch type
1819
#
1820
# Returns         : Full URL name to the new branch
1821
#
1822
sub BranchName
1823
{
1824
    my ($self, $branch, $type ) = @_;
1825
    Debug ( "BranchName", $branch );
1826
 
1827
    $type = 'branches' unless ( $type );
1828
    my $root = $self->{PKGROOT};
1829
 
1830
    $root =~ s~/(tags|branches|trunk)(/|$|@).*~~;
1831
 
1832
    return $self->{URL} . $root . '/' . $type . '/' . $branch;
1833
}
1834
 
379 dpurdie 1835
#-------------------------------------------------------------------------------
1836
# Function        : setRepoProperty
1837
#
1838
# Description     : Sets a Repository property
1839
#                   This may well fail unless the Repo is setup to allow such
1840
#                   chnages and the user is allowed to make such changes
1841
#
1842
# Inputs          : $name
1843
#                   $value
1403 dpurdie 1844
#                   $allowError     - Support for bad repositories
379 dpurdie 1845
#
1403 dpurdie 1846
# Returns         : 0 - Change made
1847
#                   Will not return on error
379 dpurdie 1848
#
1849
sub setRepoProperty
1850
{
1403 dpurdie 1851
    my ($self, $name, $value, $allowError ) = @_;
1852
    my $retval = 0;
1853
 
379 dpurdie 1854
    Debug ( "setRepoProperty", $name, $value );
1855
    #
1856
    #   Ensure that the Repo version is known
1857
    #   This should be set by a previous operation
1858
    #
1859
    unless ( defined $self->{REVNO} )
1860
    {
1861
        Error ("setRepoProperty. Release Revision Number not known");
1862
    }
1863
 
1864
    #
1865
    #   Execute the command
1866
    #
1867
    if ( $self->SvnCmd ( 'propset' , $name, '--revprop', '-r',  $self->{REVNO}, $value, $self->Full,
1868
                            {
1869
                                'credentials' => 1,
1870
                                'nosavedata' => 1,
1871
                            }
1872
                       ) )
1873
    {
1874
        #
1875
        #   Property NOT set
1876
        #
1403 dpurdie 1877
        if ( $allowError )
1878
        {
1879
            Warning ("setRepoProperty: $name - FAILED");
1880
            $retval = 1;
1881
        }
1882
        else
1883
        {
1884
            Error ("setRepoProperty: $name - FAILED");
1885
        }
379 dpurdie 1886
    }
1403 dpurdie 1887
 
1888
    return $retval;
379 dpurdie 1889
}
1890
 
1403 dpurdie 1891
#-------------------------------------------------------------------------------
1892
# Function        : backTrackSvnLabel
1893
#
1894
# Description     : Examine a Svn Tag and backtrack until we find the branch
1895
#                   that was used to create the label
1896
#
1897
# Inputs          : $self                   - Instance Data
1898
#                   $src_label              - Label to process
1899
#                                             Label within the current instance
1900
#                   A hash of named arguments
1901
#                       data                - Scalar ref. Hash of good stuff returned
1902
#                       printdata           - Print RAW svn data
1903
#                       onlysimple          - Do not do exhaustive scan
1904
#                       savedevbranch       - Save Dev Branch in session
1905
#
1906
# Returns         : Branch from which the label was taken
1907
#                   or the label prefixed with 'tags'.
1908
#
1909
sub backTrackSvnLabel
1910
{
1911
    my $self = shift;
1912
    my $src_label = shift;
1913
    my %opt = @_;
1914
    my $branch;
1915
 
1916
    Debug ("backTrackSvnLabel");
1917
    Error ("backTrackSvnLabel: Odd number of args") unless ((@_ % 2) == 0);
1918
 
1919
    #
1920
    #   May need to read and process data twice
1921
    #   First   - stop on copy. May it fast
1922
    #   Second  - all the log.
1923
 
1924
    #
1925
    #   extract data
1926
    #
1927
    foreach my $mode ( '--stop-on-copy', '' )
1928
    {
1929
        #   Init stored data
1930
        #   Used to communicate with callback function(s)
1931
        #
1932
        Information ("backTrackSvnLabel: Performing exhaustive search") unless $mode;
1933
        $self->{btData} = ();
1934
        $self->{btData}{results}{base} = $self->FullPath();
1935
        $self->{btData}{results}{label} = $src_label;
1936
        $self->{btData}{results}{changeSets} = 0;
1937
        $self->{btData}{results}{distance} = 0;
1938
 
1939
        #
1940
        #   Linux does not handle empty arguments in the same
1941
        #   manner as windows. Solution: pass an array
1942
        #
1943
        my @mode;
1944
        push @mode, $mode if ( $mode);
1945
        my $spath = $self->FullPath() . '/' . $src_label;
1946
 
1947
        Verbose2("backTrackSvnLabel. Log from $spath");
1948
        $self->SvnCmd ( 'log', '-v', '--xml', '-q'
1949
                        , @mode
1950
                        , $spath
1951
                        , { 'credentials' => 1,
1952
                            'process' => \&ProcessBackTrack,
1953
                            'printdata' => $opt{printdata},
1954
                            'nosavedata' => 1,
1955
                             }
1956
                            );
1957
 
1958
        last if ( $self->{btData}{good} );
1959
        last if ( $opt{onlysimple} );
1960
    }
1961
 
1962
    #
1963
    #   Did not backtrack to a branch (or trunk)
1964
    #   Return the users label
1965
    #
1966
    unless ( $self->{btData}{good} )
1967
    {
1968
        $branch = $src_label;
1969
    }
1970
    else
1971
    {
1972
        $branch = $self->{btData}{results}{devBranch};
1973
        if ( $opt{savedevbranch} )
1974
        {
1975
            $self->{btData}{results}{devBranch} =~ m~^(.*?)(@|$)~;
1976
            $self->{DEVBRANCH} = $1;
1977
        }
1978
 
1979
    }
1980
 
1981
    #
1982
    #   Return data to the user
1983
    #
1984
    if ( my $refData = $opt{data} )
1985
    {
1986
        Error ('Internal: backTrackSvnLabel. Arg to "data" must be ref to a scalar')
1987
            unless ( ref($refData) eq 'SCALAR' );
1988
        $$refData = $self->{btData}{results};
1989
    }
1990
 
1991
    #
1992
    #   Clean up the data
1993
    #
1994
    delete $self->{btData};
1995
    return $branch;
1996
}
1997
 
1998
#-------------------------------------------------------------------------------
1999
# Function        : ProcessBackTrack
2000
#
2001
# Description     :
2002
#                   Parse
2003
#                       <logentry
2004
#                          revision="24272">
2005
#                       <author>bivey</author>
2006
#                       <date>2005-07-25T15:45:35.000000Z</date>
2007
#                       <paths>
2008
#                       <path
2009
#                          prop-mods="false"
2010
#                          text-mods="false"
2011
#                          kind="dir"
2012
#                          copyfrom-path="/enqdef/branches/Stockholm"
2013
#                          copyfrom-rev="24271"
2014
#                          action="A">/enqdef/tags/enqdef_24.0.1.sls</path>
2015
#                       </paths>
2016
#                       <msg>COTS/enqdef: Tagged by Jats Svn Import</msg>
2017
#                       </logentry>
2018
#
2019
#
2020
#                   Uses:   $self->{btData}     - Scratch Data
2021
#
2022
# Inputs          : $self           - Class Data
2023
#                   $line           - Input data to parse
2024
#
2025
# Returns         : 0 - Do not terminate input command
2026
#
2027
sub  ProcessBackTrack
2028
{
2029
    my ($self, $line ) = @_;
2030
    Message ( $line ) if $self->{PRINTDATA};
2031
 
2032
    $line =~ s~\s+$~~;
2033
    next unless ( $line );
2034
#    Debug0('', $line);
2035
 
2036
    my $workSpace =  \%{$self->{btData}};
2037
    if ( $line =~ m~<logentry$~ ) {
2038
        $workSpace->{mode} = 'l';
2039
        $workSpace->{rev} = 0;
2040
        $workSpace->{changesSeen} = 0;
2041
 
2042
    } elsif ( $line =~ m~</logentry>$~ ) {
2043
        $workSpace->{mode} = '';
2044
 
2045
        #
2046
        #   End of a <logenty>
2047
        #   See if we have a result - a dev branch not copied from a tag
2048
        #
2049
        if ( exists $workSpace->{devBranch} )
2050
        {
2051
            $workSpace->{results}{distance}++;
2052
            $workSpace->{devBranch} =~ m~/((tags|branches|trunk)(/|\@).*)~;
2053
            my $devBranch = $1;
2054
 
2055
            push @{$workSpace->{results}{paths}}, $devBranch;
2056
            unless ( $devBranch =~ m ~^tags~ )
2057
            {
2058
                $workSpace->{results}{devBranch} = $devBranch;
2059
                $workSpace->{results}{isaBranch} = 1;
2060
                $workSpace->{good} = 1;
2061
                return 1;
2062
            }
2063
        }
2064
 
2065
    } elsif ( $line =~ m~<path$~ ) {
2066
        $workSpace->{mode} = 'p';
2067
        Error ('Path without Rev') unless ( $workSpace->{rev} );
2068
 
2069
    } elsif ( $line =~ m~</paths>$~ ) {
2070
        $workSpace->{mode} = '';
2071
    }
2072
    return 0 unless ( $workSpace->{mode} );
2073
 
2074
    if ( $workSpace->{mode} eq 'l' )
2075
    {
2076
        #
2077
        #   Processing logentry data
2078
        #       Only need the rev
2079
        #
2080
        $workSpace->{rev} = $1
2081
            if ( $line =~ m~revision=\"(\d+)\"~ );
2082
 
2083
    } elsif ( $workSpace->{mode} eq 'p' ) {
2084
        #
2085
        #   Processing Paths
2086
        #       Entries appear to be in a random order
2087
        #       Not always the same order
2088
        #
2089
        my $end = 0;
2090
        if ( $line =~ s~\s*(.+?)="(.+)">(.*)</path>$~~ )
2091
        {
2092
            #
2093
            #   Last entry has two items
2094
            #       Attribute
2095
            #       Data Item
2096
            #
2097
            $end = 1;
2098
            $workSpace->{path}{$1} = $2;
2099
            $workSpace->{path}{DATA} = $3;
2100
        }
2101
        elsif ($line =~ m~\s*(.*?)="(.*)"~ )
2102
        {
2103
            $workSpace->{path}{$1} = $2;
2104
        }
2105
#        else
2106
#        {
2107
#            Warning ("Cannot decode XML log: $line");
2108
#        }
2109
 
2110
        if ( $end )
2111
        {
2112
            #
2113
            #   If the Repo is created by a pre 1.6 SVN, then kind will be
2114
            #   empty. Have a guess.
2115
            #
2116
            if ( $workSpace->{path}{'kind'} eq '' )
2117
            {
2118
                if ( exists $workSpace->{path}{'copyfrom-path'} ) {
2119
                    $workSpace->{path}{'kind'} = 'dir';
2120
                } else {
2121
                    $workSpace->{path}{'kind'} = 'file';
2122
                }
2123
            }
2124
 
2125
            if ( $workSpace->{path}{'kind'} eq 'dir' &&  exists $workSpace->{path}{'copyfrom-path'} )
2126
            {
2127
                my $srev = $workSpace->{path}{'copyfrom-rev'};
2128
                my $from = $workSpace->{path}{'copyfrom-path'};
2129
                if ( $from =~ m~/trunk$~ || $from =~ m~/branches/[^/]+$~ )
2130
                {
2131
                    $workSpace->{devBranch} = $from . '@' . $srev;
2132
                }
2133
            }
2134
 
2135
            elsif ( $workSpace->{path}{'kind'} eq 'file' )
2136
            {
2137
                #
2138
                #   Track files that have been changed between tag and branch
2139
                #   The log is presented as newest first
2140
                #   The files have a tag-name component.
2141
                #       Remove the tag name - so that we can compare files
2142
                #       Save the first instance of changed files
2143
                #           Others will be in older versions
2144
                #           and thus of no interest
2145
                #
2146
                #   Count the chnage sets that have changes
2147
                #   Having changes in multiple change sets indicates
2148
                #   development on a /tags/ - which is BAD
2149
                #
2150
                $workSpace->{path}{'DATA'} =~ m~(.+)/((tags|branches|trunk)(/|$).*)~;
2151
                my $file =  $2;
2152
                my $full = $file;
2153
                $file =~ s~^tags/(.+?)/~~;
2154
 
2155
                if ( ! exists $workSpace->{files}{$file}  )
2156
                {
2157
                    push @{$workSpace->{results}{files}}, $full . '@' . $workSpace->{rev};
2158
                }
2159
                $workSpace->{files}{$file}++;
2160
                $workSpace->{firstFile} = $file unless ( defined $workSpace->{firstFile} );
2161
 
2162
                unless ( $workSpace->{changesSeen} )
2163
                {
2164
                    unless( $workSpace->{firstFile} eq $file )
2165
                    {
2166
                        $workSpace->{results}{changeSets}++;
2167
                        $workSpace->{changesSeen}++;
2168
                    }
2169
                }
2170
 
2171
                if ( scalar keys %{$workSpace->{files}} > 1 )
2172
                {
2173
                    $workSpace->{results}{multipleChanges} = 1;
2174
                    Verbose ("backTrackSvnLabel: Changes in multiple versions");
2175
                }
2176
            }
2177
 
2178
            delete $workSpace->{path};
2179
        }
2180
    }
2181
 
2182
    #
2183
    #   Return 0 to keep on going
2184
    return 0;
2185
}
2186
 
267 dpurdie 2187
#------------------------------------------------------------------------------
2188
1;