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
1431 dpurdie 822
#                       path            - Path to local workspace
823
#                       modifiedRoot    - Alternate base for files
824
#                       modified        - Files that are allowed to be modified
825
#                                         Relative to the 'path' or 'modifiedRoot'
826
#                                         May be a single file or an array of files
827
#                       allowLocalMods  - Only warn about local mods
828
#                       cmd             - Command name for error reporting
267 dpurdie 829
#
830
# Returns         :
831
#
832
sub SvnWsModified
833
{
834
    my $self = shift;
835
    my %opt = @_;
836
    Debug ("SvnWsModified");
837
    Error ("Odd number of args to SvnWsModified") unless ((@_ % 2) == 0);
838
 
839
    my $cmd = $opt{'cmd'} || 'SvnWsModified';
840
 
841
    #
842
    #   Validate the path
843
    #
844
    SvnValidateWs ($self, $cmd);
845
    my $path = $self->{WS};
1431 dpurdie 846
    my $modifiedRoot = $opt{'modifiedRoot'} || $path;
847
    my $path_length = length ($modifiedRoot);
267 dpurdie 848
    Verbose2 ("Test Workspace for Modifications: $path");
849
 
850
    #
851
    #   Ask subversion if there are any files to be updated
852
    #
853
    $self->SvnCmd ('status', $path, {'error' => "Svn status command error"} );
854
 
855
    #
856
    #   Examine the list of modified files
857
    #
858
    if ( @{$self->{RESULT_LIST}} )
859
    {
860
        #
351 dpurdie 861
        #   Create a hash of files that are allowed to change
267 dpurdie 862
        #   These are files relative to the base of the view
863
        #
864
        #   The svn command has the 'path' prepended, so this
865
        #   will be removed as we process the commands
866
        #
867
        my %allowed;
868
        my @unexpected;
869
 
870
        if ( exists $opt{'modified'}  )
871
        {
872
            $allowed{'/' . $_} = 1 foreach ( ref ($opt{'modified'}) ? @{$opt{'modified'}} : $opt{'modified'}  );
873
        }
874
 
875
        #
876
        #   Process the list of modified files
877
        #   Do this even if we aren't allowed modified files as we
878
        #   still need to examine the status and kill off junk entries
879
        #   ie: ?, I, ! and ~
880
        #
881
        #    First column: Says if item was added, deleted, or otherwise changed
882
        #      ' ' no modifications
883
        #      'A' Added
884
        #      'C' Conflicted
885
        #      'D' Deleted
886
        #      'I' Ignored
887
        #      'M' Modified
888
        #      'R' Replaced
889
        #      'X' item is unversioned, but is used by an externals definition
890
        #      '?' item is not under version control
891
        #      '!' item is missing (removed by non-svn command) or incomplete
892
        #      '~' versioned item obstructed by some item of a different kind
893
        #
894
        foreach my $entry ( @{$self->{RESULT_LIST}} )
895
        {
896
            #
897
            #   Extract filename from line
351 dpurdie 898
            #       First 8 chars are status
267 dpurdie 899
            #       Remove WS path too
900
            #
1329 dpurdie 901
            if ( length $entry >= 8 + $path_length)
902
            {
903
                my $file = substr ( $entry, 8 + $path_length );
904
                next if ( $allowed{$file} );
905
            }
267 dpurdie 906
 
907
            #
908
            #   Examine the first char and rule out funny things
909
            #
910
            my $f1 =  substr ($entry, 0,1 );
911
            next if ( $f1 =~ m{[?I!~]} );
912
            push @unexpected, $entry;
913
        }
385 dpurdie 914
 
915
        if ( @unexpected )
916
        {
917
            if ( $opt{allowLocalMods} ) {
918
                Message ("Workspace contains locally modified files:", @unexpected);
919
            } else {
920
                Error ("Workspace contains unexpected modified files", @unexpected);
921
            }
922
        }
267 dpurdie 923
    }
924
}
925
 
926
#-------------------------------------------------------------------------------
927
# Function        : SvnListPackages
928
#
929
# Description     : Determine a list of packages within the repo
930
#                   This turns out to be a very slow process
931
#                   so don't use it unless you really really need to
932
#
1403 dpurdie 933
# Inputs          : $self       - Instance data
934
#                   $repo       - Name of the repository
935
#                   Last argument may be a hash of options.
936
#                           Progress    - True: Show progress
937
#                           Show        - >1 : display matched Tags and stats
938
#                                         >2 : display Packages
939
#                           Tag         - Enable Tag Matching
940
#                                         Value is the tag to match
267 dpurdie 941
#
1403 dpurdie 942
# Returns         : Ref to an array of all packages
943
#                   Ref to an array of all packahes with matched tag
267 dpurdie 944
#
945
sub SvnListPackages
946
{
1403 dpurdie 947
    #
948
    #   Extract arguments and options
949
    #   If last argument is a hesh, then its a hash of options
950
    #
951
    my $opt;
952
    $opt = pop @_
953
        if (@_ > 0 and UNIVERSAL::isa($_[-1],'HASH'));
267 dpurdie 954
 
1403 dpurdie 955
    my ($self, $repo) = @_;
956
 
957
    my @path_list = '';
267 dpurdie 958
    my @list;
1403 dpurdie 959
    my @mlist;
267 dpurdie 960
    my $scanned = 0;
961
    Debug ("SvnListPackages");
962
    while ( @path_list )
963
    {
964
        my $path = shift @path_list;
1403 dpurdie 965
        if ( $opt->{Progress} )
966
        {
967
            Message ("Reading: " . ( $path || 'RepoRoot') );
968
        }
267 dpurdie 969
        $scanned++;
1403 dpurdie 970
        my ( $ref_files, $ref_dirs, $ref_svn, $found ) = $self->SvnScanPath ( 'Listing Packages', join( '/', $repo, $path) );
267 dpurdie 971
 
972
        #
973
        #   If there are Subversion dirs (ttb) in this directory
974
        #   then this is a package. Add to list
975
        #
976
        push @list, $path if ( @$ref_svn );
977
 
978
        #
979
        #   Add subdirs to the list of paths to explore
980
        #
981
        foreach  ( @$ref_dirs )
982
        {
1403 dpurdie 983
            chop;                                   # Remove trailing '/'
984
            push @path_list, $path ? join('/', $path , $_) : $_; # Extend the path
267 dpurdie 985
        }
986
    }
987
 
1403 dpurdie 988
    if ( $opt->{Tag} )
989
    {
990
        my $tag = $opt->{Tag};
991
        foreach my $path ( sort @list )
992
        {
993
            Message ("Testing: $path") if ( $opt->{Progress} );
994
            if ( $self->SvnTestPath ( 'Listing Packages', join('/', $repo, $path, 'tags', $tag) ) )
995
            {
996
                push @mlist, $path;
997
            }
998
        }
999
    }
1000
 
1001
    if ( $opt->{Show} )
1002
    {
1003
        Message ("Found Tags:", @mlist );
1004
        Message ("Found Packages:", @list ) if  $opt->{Show} > 2;
1005
        Message ("Tags Found: " . scalar @mlist );
1006
        Message ("Packages Found: " . scalar @list );
1007
        Message ("Dirs Scanned: $scanned");
1008
    }
1009
 
1010
    return \@list, \@mlist;
267 dpurdie 1011
}
1012
 
1013
#-------------------------------------------------------------------------------
1014
# Function        : ListLabels
1015
#
1016
# Description     : List labels within a given package
1017
#
1018
# Inputs          : $self               - Instance data
1019
#                   $path               - path to label source
1020
#
1021
# Returns         : Ref to an array
1022
#
1023
sub ListLabels
1024
{
1025
    my ($self, $path) = @_;
1026
    Debug ("ListLabels");
1027
 
1028
    my ( $ref_files, $ref_dirs, $ref_svn, $found ) = $self->SvnScanPath ( 'Listing Versions', $path );
1029
 
1030
    Error ("List: Path not found: $path") unless ( $found );
1031
 
1032
    #
1033
    #   Dont report files - just directories
1034
    #
1035
    return $ref_dirs;
1036
}
1037
 
1038
 
1039
#-------------------------------------------------------------------------------
1040
# Function        : SvnLocateWsRoot
1041
#
1042
# Description     : Given a WorkSpace, determine the root of the work space
1043
#                   This is not as simple as you might think
1044
#
1045
#                   Algorithm
1046
#                       svn ls ..
1047
#                       Am I in the parent directory
1048
#                       Repeat
1049
#
369 dpurdie 1050
#                   Updates 'WS' and 'WSURL'
1051
#
267 dpurdie 1052
# Inputs          : $self               - Instance data
1053
#                   $test               - True: Don't die on error
1054
#
1055
# Returns         : Root of workspace as an absolute address
1056
#                   Will not return if there is an error
1057
#
1058
sub SvnLocateWsRoot
1059
{
1060
    my ($self, $test) = @_;
1061
    my @path;
1062
    my $path = $self->{WS};
1403 dpurdie 1063
    my $found;
267 dpurdie 1064
 
1065
    Debug ("SvnLocateWsRoot");
1066
    Error ("SvnLocateWsRoot: No Workspace") unless ( $path  );
1067
    Verbose2 ("SvnLocateWsRoot: Start in $path");
1068
 
1069
    #
1070
    #   Validate the source path
1071
    #
1072
    if ( SvnValidateWs ($self, 'SvnLocateWsRoot', $test) )
1073
    {
1074
        return undef;
1075
    }
1076
 
1077
    #
1403 dpurdie 1078
    #   Under Subversion 1.7 the process is a lot easier
267 dpurdie 1079
    #
1403 dpurdie 1080
    if ( exists $self->{'InfoWs'}{'Working Copy Root Path'} )
1081
    {
1082
        #
1083
        #   WS is now known
1084
        #
1085
        $self->{WS} = $self->{'InfoWs'}{'Working Copy Root Path'};
267 dpurdie 1086
 
1403 dpurdie 1087
        #
1088
        #   Calculate WSURL
1089
        #
1090
        $self->{WSURL} = join('/', $self->{PKGROOT}, $self->{DEVBRANCH});
1091
        $found = 1;
1092
    }
1093
    else
267 dpurdie 1094
    {
1403 dpurdie 1095
        # Preversion 1.7
1096
        Warning ("Using svn < 1.7. This is not recommended");
267 dpurdie 1097
 
1403 dpurdie 1098
        #
1099
        #   Need to sanitize the users path to ensure that the following
1100
        #   algorithm works. Need:
1101
        #       1) Absolute Path
1102
        #       2) Not ending in '/'
1103
        #
369 dpurdie 1104
 
267 dpurdie 1105
        #
1403 dpurdie 1106
        #   If we have a relative path then prepend the current directory
1107
        #   An absolute path is:
1108
        #           /aaa/aa/aa
1109
        #       or  c:/aaa/aa/aa
267 dpurdie 1110
        #
1403 dpurdie 1111
        $path = getcwd() . '/' . $path
1112
            unless ( $path =~ m~^/|\w:/~  );
267 dpurdie 1113
 
1114
        #
1403 dpurdie 1115
        #   Walk the bits and remove ".." directories
1116
        #       Done by pushing non-.. elements and poping last entry for .. elements.
1117
        #   Have a leading "/" which is good.
267 dpurdie 1118
        #
1403 dpurdie 1119
        #   Create a array of directories in the path
1120
        #   Split on one or more \ or / separators
267 dpurdie 1121
        #
1403 dpurdie 1122
        foreach ( split /[\\\/]+/ , $path )
267 dpurdie 1123
        {
1403 dpurdie 1124
            next if ( $_ eq '.' );
1125
            unless ( $_ eq '..' )
1126
            {
1127
                push @path, $_;
1128
            }
1129
            else
1130
            {
1131
                Error ("SvnLocateWsRoot: Bad Pathname: $path")
1132
                    if ( $#path <= 0 );
1133
                pop @path;
1134
            }
267 dpurdie 1135
        }
1136
 
1137
        #
1403 dpurdie 1138
        #   Need to adjust the WSURL too
1139
        #   Break into parts and pop them off as we go
1140
        #   Add a dummy one to allow for the first iteration
267 dpurdie 1141
        #
1403 dpurdie 1142
        my @wsurl = (split (/[\\\/]+/ , $self->{WSURL}), 'Dummy');
369 dpurdie 1143
 
1403 dpurdie 1144
        Verbose2 ("Clean absolute path elements: @path");
1145
        PATH_LOOP:
1146
        while ( @path )
1147
        {
1148
            #
1149
            #   This directory element. Append / to assist in compare
1150
            #   Determine parent path
1151
            #
1152
            my $name = pop (@path) . '/';
1153
            my $parent = join ('/', @path );
1154
            pop @wsurl;
369 dpurdie 1155
 
1403 dpurdie 1156
            #
1157
            #   Examine the parent directory
1158
            #   Get a list of all elements in the parent
1159
            #   Need to ensure that this directory is one of them
1160
            #
1161
            #   Ignore any errors - assume that they are because the
1162
            #   parent is not a part of the work space. This will terminate the
1163
            #   search.
1164
            #
1165
            $self->SvnCmd ('list', $parent, '--depth', 'immediates' );
1166
            foreach my $entry ( @{$self->{RESULT_LIST}} )
1167
            {
1168
                next PATH_LOOP
1169
                    if ( $entry eq $name );
1170
            }
1171
 
1172
            #
1173
            #   Didn't find 'dir' in directory svn listing of parent
1174
            #   This parent is not a part of the same WorkSpace as 'dir'
1175
            #   We have a winner.
1176
            #
1177
            chop $name;                         #   Chop the '/' previously added
1178
            $self->{WS} = $parent . '/' . $name;
1179
 
1180
            #
1181
            #   Reform the WSURL. Elements have been removed as we tested up the
1182
            #   path
1183
            #
1184
            $self->{WSURL} = join '/', @wsurl;
1185
            $found = 1;
1186
            last;
1187
        }
267 dpurdie 1188
    }
1189
 
1190
    #
1191
    #   Shouldn't get this far
1192
    #
1403 dpurdie 1193
    Error ("SvnLocateWsRoot: Root not found")
1194
        unless ( $found );
1195
 
1196
    #
1197
    #   Refresh Info
1198
    #   Must kill cached copy
1199
    #
1200
    delete $self->{'InfoWs'};
1201
    $self->SvnInfo($self->{WS}, 'InfoWs');
1202
    return $self->{WS};
1203
 
267 dpurdie 1204
}
1205
 
1206
#-------------------------------------------------------------------------------
1207
# Function        : SvnValidateWs
1208
#
1209
# Description     : Validate the path to a working store
1210
#
1211
# Inputs          : $self           - Instance data
1212
#                   $user           - Optional prefix for error messages
1213
#                   $test           - True: Just test, Else Error
1214
#
1215
# Returns         : Will not return if not a workspace
1216
#                   Returns the users path
1329 dpurdie 1217
#                   Populates the hash: $self->{InfoWs}
267 dpurdie 1218
#
1219
sub SvnValidateWs
1220
{
1221
    my ($self, $user, $test) = @_;
1222
    Debug ("SvnValidateWs");
1223
 
1224
    $user = "Invalid Subversion Workspace" unless ( $user );
1329 dpurdie 1225
    my $path = $self->{WS};
267 dpurdie 1226
 
1227
    #
1329 dpurdie 1228
    #   Only validate it once
267 dpurdie 1229
    #
1230
    return $path if ( $self->{WS_VALIDATED} );
1231
 
1232
    #
1233
    #   Validate the source path
1234
    #   Must exist and must be a directory
1235
    #
1236
    if ( ! $path ) {
1237
        @{$self->{ERROR_LIST}} = "$user: No path specified";
1238
 
1239
    } elsif ( ! -e $path ) {
1240
        @{$self->{ERROR_LIST}} = "$user: Path does not exist: $path";
1241
 
1242
    } elsif ( ! -d $path ) {
1243
        @{$self->{ERROR_LIST}} = "$user: Path is not a directory";
1244
    } else {
1245
        #
1246
        #   Determine the source path is an fact a view
1247
        #   The info command can do this. Use depth empty to limit the work done
1248
        #
1329 dpurdie 1249
        $self->SvnInfo($path, 'InfoWs');
267 dpurdie 1250
 
1251
        #
1252
        #   Error. Prepend nice message
1253
        #
1254
        unshift @{$self->{ERROR_LIST}}, "$user: Path is not a WorkSpace: $path"
1255
            if ( @{$self->{ERROR_LIST}} );
1256
    }
1257
 
1258
    #
1259
    #   Figure out what to do
1260
    #
1261
    if ( $test )
1262
    {
1263
        return @{$self->{ERROR_LIST}};
1264
    }
1265
    else
1266
    {
1267
        Error @{$self->{ERROR_LIST}} if @{$self->{ERROR_LIST}};
1268
        $self->{WS_VALIDATED} = 1;
1269
        return $path;
1270
    }
1271
}
1272
 
1273
#-------------------------------------------------------------------------------
1274
# Function        : SvnValidatePackageRoot
1275
#
1276
# Description     : Validate a package root
1277
#
1278
# Inputs          : $self           - Instance data
1279
#
1280
# Returns         : Will only return if valid
1281
#                   Returns a cleaned package root
1282
#
1283
sub SvnValidatePackageRoot
1284
{
379 dpurdie 1285
    my ($self, $warning_only) = @_;
267 dpurdie 1286
    Debug ("SvnValidatePackageRoot");
1287
    my $url = $self->Full || Error ("SvnValidatePackageRoot: No URL");
1288
 
1289
    Error ("Package path contains a reserved word ($self->{TAGTYPE})", "Path: $url")
1290
        if (  $self->{TAGTYPE} );
1291
 
1292
    Error ("Package name contains a Peg ($self->{PEG})", "Path: $url")
1293
        if ( $self->{PEG} );
1294
 
1295
    #
1296
    #   Ensure that the target path does exist
1297
    #   Moreover it needs to be a directory and it should have a
1298
    #   a ttb structure
1299
    #
1300
    my ( $ref_files, $ref_dirs, $ref_svn, $found ) = $self->SvnScanPath ( 'Package Base Test', $url );
1301
 
1302
    #
379 dpurdie 1303
    #   Only looking for package path
1304
    #
1305
    if ( !$found && $warning_only )
1306
    {
1307
        return $url;
1308
    }
1309
 
1310
    #
267 dpurdie 1311
    #   Examine the results to see if we have a valid package base
1312
    #
1313
    Error ("Package Base Test: Not a valid package") unless ( $found );
1314
 
1315
    #
1316
    #   Extra bits found
1317
    #   Its not the root of a package
1318
    #
1319
    if ( @$ref_files )
1320
    {
1321
        Warning ("Package Base Test: Files exists",
1322
               "Unexpected files found:", @$ref_files );
1323
    }
1324
 
1325
    #
1326
    #   Need a truck directory
1327
    #   If we don't have a truck we don't have a package
1328
    #
1329
    my $trunk_found = grep ( /trunk\//, @$ref_svn );
1330
    Error ("Invalid Package Base. Does not contain a 'trunk' directory")
1331
        unless ( $trunk_found );
1332
 
1333
    return $url;
1334
}
1335
 
1336
 
1337
#-------------------------------------------------------------------------------
1338
# Function        : SvnIsaSimpleLabel
1339
#
1340
# Description     : Check a label
1341
#                       Must not contain a PEG
1342
#                       Must not contain invalid characters (@ or /)
1343
#                       Must not contain a :: sequence (will confuse other tools)
1344
#                       Handle special label of TIMESTAMP
1345
#
1346
# Inputs          : $label          - to test
1347
#
1348
# Returns         : Will not return on error
1349
#                   Returns label on success
1350
#
1351
sub SvnIsaSimpleLabel
1352
{
1353
    my ($label) = @_;
1354
    Debug ("SvnIsaSimpleLabel, $label");
1355
 
1356
    Error ("No label provided") unless ( $label );
1357
    Error ("Invalid label. Peg (\@nnn) is not allowed: \"$label\"" ) if ( $label =~ m~@\d+$~ );
1358
    Error ("Invalid label. Package Path is not allowed: \"$label\"" ) if ( $label =~ m~/~ );
383 dpurdie 1359
    Error ("Invalid label. Invalid Start Character: \"$label\"" ) unless ( $label =~ m~^[0-9a-zA-Z]~ );
1360
    Error ("Invalid label. Invalid End Character: \"$label\"" ) unless ( $label =~ m~[0-9a-zA-Z]$~ );
267 dpurdie 1361
    Error ("Invalid label. Invalid Characters: \"$label\"" ) unless ( $label =~ m~^[-.:0-9a-zA-Z_]+$~ );
1362
    Error ("Invalid label. Double :: not allowed: \"$label\"" ) if ( $label =~m~::~ );
1363
 
1364
    #
1365
    #   Allow for a label of TIMESTAMP and have it expand
383 dpurdie 1366
    #   Create a label based on users name and a date-time that can be sorted
267 dpurdie 1367
    #
1368
    if ( $label eq 'TIMESTAMP' )
1369
    {
341 dpurdie 1370
        ::EnvImport ('USER' );
1371
        my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
1372
        $label = sprintf("%s_%4.4u.%2.2u.%2.2u.%2.2u%2.2u%2.2u",
1373
            $::USER, $year+1900, $mon+1, $mday, $hour, $min, $sec );
267 dpurdie 1374
    }
1375
    return $label;
1376
}
1377
 
1378
#-------------------------------------------------------------------------------
1379
# Function        : NewSession
1380
#
1381
# Description     : Create a new empty SvnSession Class
1382
#
1383
# Inputs          : None
1384
#
1385
# Returns         : Class
1386
#
1387
sub NewSession
1388
{
1389
    Debug ("NewSession");
1390
    my $self  = SvnSession();
1391
 
1392
    #
1393
    #   Document Class Variables
1394
    #
1395
    $self->{URL} = '';                  # Repo URL prefix
1396
    $self->{WS}  = '';                  # Users WorkSpace
1397
    $self->{PROTOCOL} = '';             # Named Access Protocol
1398
    $self->{PKGROOT} = '';              # Package root
1399
 
1400
    #
1401
    #   Create a class
1402
    #   Bless my self
1403
    #
1404
    bless ($self, __PACKAGE__);
1405
    return $self;
1406
}
1407
 
1408
#-------------------------------------------------------------------------------
1409
# Function        : NewSessionByWS
1410
#
1411
# Description     : Establish a new SVN Session based on a Workspace
1412
#                   Given a workspace path determine the SvnServer and other
1413
#                   relevent information.
1414
#
1415
#                   Requires some rules
1416
#                       * The package is rooted within a 'ttb'
1417
#
1418
# Inputs          : $path                   - Path to WorkSpace
1419
#                   $test                   - No Error on no WS
361 dpurdie 1420
#                   $slack                  - Less stringent
267 dpurdie 1421
#
1422
# Returns         : Ref to Session Information
1423
#
1424
sub NewSessionByWS
1425
{
361 dpurdie 1426
    my ($path, $test, $slack) = @_;
267 dpurdie 1427
    Debug ("NewSessionByWS", @_);
1428
 
1429
    #
1430
    #   Create a basic Session
1431
    #   Populate it with information that is known
1432
    #
1433
    my $self = NewSession();
1434
    $self->{WS} = $path;
1435
 
1436
    #
1437
    #   Validate the path provided
1329 dpurdie 1438
    #   In the process populate $self->{InfoWs} with info about the workspace.
267 dpurdie 1439
    #
1440
    if ($self->SvnValidateWs ( undef, 1) )
1441
    {
1442
        return $self if ( $test );
1443
        Error ( @{$self->{ERROR_LIST}} );
1444
    }
1445
 
1446
    #
1447
    #   Extract useful info
1448
    #       URL: svn://auperaws996vm21/test/MixedView/trunk
1449
    #       Repository Root: svn://auperaws996vm21/test
1450
    #
1329 dpurdie 1451
    my $url = $self->{'InfoWs'}{'URL'};
1452
    my $reporoot = $self->{'InfoWs'}{'Repository Root'};
1453
    my $repoVersion = $self->{'InfoWs'}{'Revision'};
1403 dpurdie 1454
    my $devBranch;
267 dpurdie 1455
 
1456
    Error ("JatsSvn Internal error. Can't parse info")
1457
        unless ( $url && $reporoot );
1458
 
1459
    #
1460
    #   Need the length of the path to the repository
1461
    #   but not the name of the repostory itself.
1462
    #
1463
    #   Remove that from the head of the URL to give a
1464
    #   path within the repository, that includes the repos name
1465
    #
1466
    $reporoot = (fileparse( $reporoot ))[1];
1467
    $url = substr ($url, length ($reporoot));
1468
    $self->{WSURL} = $url;
1469
    chop $reporoot;
1470
 
1471
    Verbose2 ("SvnLocatePackageRoot: $reporoot, $url" );
1472
 
1473
    #
1474
    #   Remove anything after a ttb ( truck, tags, branch ) element
1475
    #   This will be the root of the package within the repo
1476
    #
1477
    if (  $url =~ m~(.+)/((tags|branches|trunk)(/|$).*)~ )
1478
    {
1479
        $url = $1;
1480
        $self->{WSTYPE} = $3;
1403 dpurdie 1481
        if ( $3 eq 'trunk' ) {
1482
            $devBranch = $3;
1483
        } elsif ( $3 eq 'branches' ) {
1484
            $devBranch = $2;
1485
        }
267 dpurdie 1486
    }
1487
    else
1488
    {
361 dpurdie 1489
        #
1490
        #   If we are being slack (ie deleting the workspace)
1491
        #   Then generate a warning, not an error
1492
        #
1493
        my $fnc = $slack ? \&Warning : \&Error;
1494
        $fnc->("SvnLocatePackageRoot. Non standard repository format",
1495
               "Url must contain 'tags' or 'branches' or 'trunk'",
267 dpurdie 1496
               "Url: $url");
361 dpurdie 1497
        $self->{WSTYPE} = 'trunk';
267 dpurdie 1498
    }
1499
 
1500
    #
1501
    #   Insert known information
1502
    #
1503
    $self->{URL} = $reporoot . '/';
1504
    $self->{PKGROOT} = $url;
369 dpurdie 1505
    $self->{WSREVNO} = $repoVersion;
1403 dpurdie 1506
    $self->{DEVBRANCH} = $devBranch;
267 dpurdie 1507
 
1508
    #
1509
    #   Create useful information
1510
    #
1511
    SplitPackageUrl($self);
1512
    return $self;
1513
}
1514
 
1515
#-------------------------------------------------------------------------------
1516
# Function        : NewSessionByUrl
1517
#
1518
# Description     : Establish a new SVN Session based on a user URL
1519
#
1520
# Inputs          : $uurl                   - Users URL
361 dpurdie 1521
#                   $ttb_test               - Test and warn for TTB structure
267 dpurdie 1522
#                   $session                - Optional: Existing session
1523
#
1524
# Returns         : Ref to Session Information
1525
#
1526
sub NewSessionByUrl
1527
{
361 dpurdie 1528
    my ($uurl, $ttb_test, $self ) = @_;
267 dpurdie 1529
    Debug ("NewSessionByUrl", @_);
1530
    Error ("No Repostory Path specified") unless ( $uurl );
1531
 
1532
    #
1533
    #   Create a basic Session
1534
    #   Populate it with information that is known
1535
    #
1536
    $self = NewSession() unless ( $self );
1537
 
1538
    #
369 dpurdie 1539
    #   Examine the URL and convert a Repository Path into a URL
353 dpurdie 1540
    #   as provided by configuration information within the environment
267 dpurdie 1541
    #
361 dpurdie 1542
    ($self->{URL}, $self->{PKGROOT} ) = SvnPath2Url ($uurl);
1543
 
1544
    #
1545
    #   Create useful information
1546
    #
1547
    SplitPackageUrl($self);
1548
 
1549
    #
1550
    #   Warn of non-standard URLs
1551
    #   These may create problems latter
1552
    #
1553
    if ( $ttb_test )
1554
    {
1555
        Warning("Non standard repository format",
1556
                 "Url should contain 'tags' or 'branches' or 'trunk'",
1557
                 "Url: $self->{PKGROOT}") unless $self->{TAGTYPE};
1558
    }
1559
 
1560
    return $self;
1561
}
1562
 
1563
#-------------------------------------------------------------------------------
1564
# Function        : SvnPath2Url
1565
#
1566
# Description     : Convert a repository path to a Full Url
1567
#                   Also handles Full Url
1568
#
1569
# Inputs          : $rpath             - Repository Path
1570
#                                        May be a full URL
1571
#
1572
# Returns         : List context
1573
#                   Two items that can be joined
1574
#                   URL                - URL
1575
#                   PKG_ROOT           - Package Root
1576
#
1577
#                   Scalar context: Joined URL and Package Root
1578
#                                   Fully formed URL
1579
#
1580
sub SvnPath2Url
1581
{
1582
    my ($rpath) = @_;
1583
    my $processed = 0;
1584
    my $url;
1585
    my $pkgroot;
1586
 
1587
    #
1588
    #   Examine the argument and convert a Repository Path into a URL
1589
    #   as provided by configuration information within the environment
1590
    #
1591
    $rpath =~ m~(.+?)/(.*)~;
1592
    my $fe = $1 || $rpath;
353 dpurdie 1593
    my $rest = $2 || '';
1594
    if ( $SVN_URLS{$fe} )
267 dpurdie 1595
    {
361 dpurdie 1596
        $url = $SVN_URLS{$fe};
1597
        $pkgroot = $rest;
353 dpurdie 1598
        $processed = 1;
341 dpurdie 1599
    }
1600
 
353 dpurdie 1601
    if ( ! $processed )
341 dpurdie 1602
    {
267 dpurdie 1603
        #
353 dpurdie 1604
        #   Examine the URL and determine if we have a FULL Url or
1605
        #   a path within the 'default' server
1606
        #
1607
        foreach my $key ( @SVN_URLS_LIST )
1608
        {
361 dpurdie 1609
            if ( $rpath =~ m~^$SVN_URLS{$key}(.*)~ )
353 dpurdie 1610
            {
361 dpurdie 1611
                $url = $SVN_URLS{$key};
1612
                $pkgroot = $1;
353 dpurdie 1613
                $processed = 1;
1614
                last;
1615
            }
1616
        }
1617
    }
267 dpurdie 1618
 
353 dpurdie 1619
    #
1620
    #   Last attempt
1621
    #   Treat as a raw URL - some operations won't be allowed
1622
    #
1623
    if ( ! $processed )
267 dpurdie 1624
    {
361 dpurdie 1625
        if ( $rpath =~ m~^((file|http|https|svn):///?([^/]+)/)(.+)~ )
353 dpurdie 1626
        {
1627
            #       http://server/
1628
            #       https://server/
1629
            #       svn://server/
1630
            #       file://This/Isa/Bad/Guess
1631
            #
361 dpurdie 1632
            $url = $1;
1633
            $pkgroot = $4;
353 dpurdie 1634
        }
369 dpurdie 1635
        elsif ($SVN_URLS{''} )
353 dpurdie 1636
        {
1329 dpurdie 1637
            if ( exists $ENV{'GBE_ABT'} && $ENV{'GBE_ABT'})
1638
            {
1639
                Error ("Attempt to use default repository within automated build", "Path: " . $rpath);
1640
            }
361 dpurdie 1641
            $url = $SVN_URLS{''};
1642
            $pkgroot = $rpath;
353 dpurdie 1643
        }
1644
        else
1645
        {
1646
            #
1647
            #   User default (site configured) Repo Root
1648
            #
1649
            Error ("No site repository configured for : $fe",
1650
                   "Configure GBE_SVN_URL_" . uc($fe) );
1651
        }
267 dpurdie 1652
    }
1653
 
1654
    #
361 dpurdie 1655
    #   May want two elements, may want one
267 dpurdie 1656
    #
361 dpurdie 1657
    return $url, $pkgroot if ( wantarray );
1658
    return $url . $pkgroot;
267 dpurdie 1659
}
1660
 
369 dpurdie 1661
#-------------------------------------------------------------------------------
1662
# Function        : SvnPaths
1663
#
1664
# Description     : Extract SVN path conversion information
1665
#
1666
# Inputs          : Nothing
1667
#
1668
# Returns         : Two refs
1669
#                   Hash of SVN URLS
1670
#                   Array for search order
1671
#
1672
sub SvnPaths
1673
{
1674
    return \%SVN_URLS, \@SVN_URLS_LIST;
1675
}
361 dpurdie 1676
 
267 dpurdie 1677
#-------------------------------------------------------------------------------
1678
# Function        : SplitPackageUrl
1679
#
1403 dpurdie 1680
# Description     : Split the package URL into a few useful bits
267 dpurdie 1681
#
1682
# Inputs          : $self           - Instance data
1683
#
1684
# Returns         : Nothing
1685
#
1686
sub SplitPackageUrl
1687
{
1688
    my ($self) = @_;
353 dpurdie 1689
    Debug ("SplitPackageUrl", $self->{URL}, $self->{PKGROOT});
267 dpurdie 1690
 
1691
    #
1692
    #   Remove any protocol that may be present
1693
    #       http://server/
341 dpurdie 1694
    #       https://server/
267 dpurdie 1695
    #       svn://server/
1696
    #       file://This/Isa/Bad/Guess
1697
    #
341 dpurdie 1698
    if ( $self->{URL} =~ m~^(file|http|https|svn)://([^/]+)~ )
267 dpurdie 1699
    {
1700
        $self->{PROTOCOL} = $1;
1701
        $self->{SERVER} = $2;
1702
    }
1703
 
1704
    if ( $self->{PKGROOT} =~ m~(.*)(@\d+)$~ )
1705
    {
1706
        $self->{PEG} = $2;
1707
    }
1708
 
1709
    #
1710
    #   Determine TTB type
1711
    #   Need to handle
1712
    #       .../trunk
1713
    #       .../trunk@nnnnn
1714
    #       .../tags/version@nnnnn
1715
    #       .../branches/version@nnnnn
1716
    #
1717
    #
1718
    if (  $self->{PKGROOT} =~ m~/?(.*)/(tags|branches|trunk)(/|$|@)(.*)$~ )
1719
    {
1720
        $self->{PATH}         = $1;
1721
        $self->{TAGTYPE}      = $2;
1722
        $self->{VERSION}      = $4;
1723
    }
1724
    else
1725
    {
1726
        $self->{PATH} = $self->{PKGROOT};
1727
    }
1728
 
1729
    DebugDumpData ('SplitPackageUrl', $self ) if ( IsDebug(2) );
1730
}
1731
 
1732
#-------------------------------------------------------------------------------
1733
# Function        : Full
1734
#                   FullWs
1735
#                   Repo
1736
#                   Peg
1737
#                   Type
1738
#                   WsType
1739
#                   Path
1740
#                   Version
1741
#                   RmRef
385 dpurdie 1742
#                   RmPath
267 dpurdie 1743
#
1744
# Description     : Accessor functions
1745
#
1746
# Inputs          : $self       - Instance data
1747
#                                 self (is $_[0])
1748
#
1749
# Returns         : Data Item
1750
#
369 dpurdie 1751
sub Full        { return $_[0]->{URL} . $_[0]->{PKGROOT} ; }
1752
sub FullWs      { return $_[0]->{URL} . $_[0]->{WSURL} ; }
1753
sub FullWsRev   { return $_[0]->{URL} . $_[0]->{WSURL} . '@' . $_[0]->{WSREVNO} ; }
1403 dpurdie 1754
sub FullPath    { return $_[0]->{URL} . $_[0]->{PATH} ; }
369 dpurdie 1755
sub Peg         { return $_[0]->{PEG} ; }
1403 dpurdie 1756
sub DevBranch   { return $_[0]->{DEVBRANCH} || '' ; }
369 dpurdie 1757
sub Type        { return $_[0]->{TAGTYPE} || '' ; }
1758
sub WsType      { return $_[0]->{WSTYPE}  || '' ; }
1759
sub Path        { return $_[0]->{PATH} ; }
1760
sub Version     { return $_[0]->{VERSION} ; }
1761
sub RmRef       { return $_[0]->{RMREF} ; }
385 dpurdie 1762
sub RmPath      { my $path = $_[0]->{RMREF}; $path =~ s~@.*?$~~ ;return  $path; }
1403 dpurdie 1763
sub SvnTag      { return $_[0]->{SVNTAG} || '' ; }
267 dpurdie 1764
 
1765
#-------------------------------------------------------------------------------
1766
# Function        : Print
1767
#
1768
# Description     : Debug display the URL
1769
#
1770
# Inputs          : $self           - Instance data
1771
#                   $header
1772
#                   $indent
1773
#
1774
# Returns         : Nothing
1775
#
1776
sub Print
1777
{
1778
    my ($self, $header, $indent) = @_;
1779
    print "$header\n" if $header;
1780
    $indent = 4 unless ( defined $indent );
1781
    $indent = ' ' x $indent;
1782
 
1783
 
1403 dpurdie 1784
    print $indent . "PROTOCOL :" . $self->{PROTOCOL} . "\n";
1785
    print $indent . "SERVER   :" . $self->{SERVER} . "\n";
1786
    print $indent . "URL      :" . $self->{URL} . "\n";
1787
    print $indent . "PKGROOT  :" . $self->{PKGROOT} . "\n";
1788
    print $indent . "PATH     :" . $self->{PATH} . "\n";
1789
    print $indent . "TAGTYPE  :" . ($self->{TAGTYPE} || '') . "\n";
1790
    print $indent . "VERSION  :" . ($self->{VERSION} || '') . "\n";
1791
    print $indent . "PEG      :" . ($self->{PEG} || '') . "\n";
1792
    print $indent . "DEVBRANCH:" . ($self->{DEVBRANCH} || '') . "\n";
1793
    print $indent . "SVNTAG   :" . ($self->{SVNTAG} || '') . "\n";
1794
#    print $indent . "FULL    :" . $self->Full . "\n";
1795
 
1796
    print $indent . "Full         :" . $self->Full . "\n";
1797
#    print $indent . "FullWs       :" . $self->FullWs    . "\n";
1798
#    print $indent . "FullWsRev    :" . $self->FullWsRev . "\n";
1799
    print $indent . "FullPath     :" . $self->FullPath  . "\n";
1800
    print $indent . "Peg          :" . $self->Peg       . "\n";
1801
    print $indent . "DevBranch    :" . $self->DevBranch . "\n";
1802
    print $indent . "Type         :" . $self->Type      . "\n";
1803
    print $indent . "WsType       :" . $self->WsType    . "\n";
1804
    print $indent . "Path         :" . $self->Path      . "\n";
1805
    print $indent . "Version      :" . $self->Version   . "\n";
1806
    print $indent . "RmRef        :" . ($self->RmRef || '') . "\n";
1807
#    print $indent . "RmPath       :" . ($self->RmPath|| '') . "\n";
267 dpurdie 1808
}
1809
 
1810
#-------------------------------------------------------------------------------
1811
# Function        : BranchName
1812
#
1813
# Description     : Create a full URL to a branch or tag based on the
1814
#                   current entry
1815
#
1816
#                   URL must have a TTB format
1817
#
1818
# Inputs          : $self           - Instance data
1819
#                   $branch         - Name of the branch
1820
#                   $type           - Optional branch type
1821
#
1822
# Returns         : Full URL name to the new branch
1823
#
1824
sub BranchName
1825
{
1826
    my ($self, $branch, $type ) = @_;
1827
    Debug ( "BranchName", $branch );
1828
 
1829
    $type = 'branches' unless ( $type );
1830
    my $root = $self->{PKGROOT};
1831
 
1832
    $root =~ s~/(tags|branches|trunk)(/|$|@).*~~;
1833
 
1834
    return $self->{URL} . $root . '/' . $type . '/' . $branch;
1835
}
1836
 
379 dpurdie 1837
#-------------------------------------------------------------------------------
1838
# Function        : setRepoProperty
1839
#
1840
# Description     : Sets a Repository property
1841
#                   This may well fail unless the Repo is setup to allow such
1842
#                   chnages and the user is allowed to make such changes
1843
#
1844
# Inputs          : $name
1845
#                   $value
1403 dpurdie 1846
#                   $allowError     - Support for bad repositories
379 dpurdie 1847
#
1403 dpurdie 1848
# Returns         : 0 - Change made
1849
#                   Will not return on error
379 dpurdie 1850
#
1851
sub setRepoProperty
1852
{
1403 dpurdie 1853
    my ($self, $name, $value, $allowError ) = @_;
1854
    my $retval = 0;
1855
 
379 dpurdie 1856
    Debug ( "setRepoProperty", $name, $value );
1857
    #
1858
    #   Ensure that the Repo version is known
1859
    #   This should be set by a previous operation
1860
    #
1861
    unless ( defined $self->{REVNO} )
1862
    {
1863
        Error ("setRepoProperty. Release Revision Number not known");
1864
    }
1865
 
1866
    #
1867
    #   Execute the command
1868
    #
1869
    if ( $self->SvnCmd ( 'propset' , $name, '--revprop', '-r',  $self->{REVNO}, $value, $self->Full,
1870
                            {
1871
                                'credentials' => 1,
1872
                                'nosavedata' => 1,
1873
                            }
1874
                       ) )
1875
    {
1876
        #
1877
        #   Property NOT set
1878
        #
1403 dpurdie 1879
        if ( $allowError )
1880
        {
1881
            Warning ("setRepoProperty: $name - FAILED");
1882
            $retval = 1;
1883
        }
1884
        else
1885
        {
1886
            Error ("setRepoProperty: $name - FAILED");
1887
        }
379 dpurdie 1888
    }
1403 dpurdie 1889
 
1890
    return $retval;
379 dpurdie 1891
}
1892
 
1403 dpurdie 1893
#-------------------------------------------------------------------------------
1894
# Function        : backTrackSvnLabel
1895
#
1896
# Description     : Examine a Svn Tag and backtrack until we find the branch
1897
#                   that was used to create the label
1898
#
1899
# Inputs          : $self                   - Instance Data
1900
#                   $src_label              - Label to process
1901
#                                             Label within the current instance
1902
#                   A hash of named arguments
1903
#                       data                - Scalar ref. Hash of good stuff returned
1904
#                       printdata           - Print RAW svn data
1905
#                       onlysimple          - Do not do exhaustive scan
1906
#                       savedevbranch       - Save Dev Branch in session
1907
#
1908
# Returns         : Branch from which the label was taken
1909
#                   or the label prefixed with 'tags'.
1910
#
1911
sub backTrackSvnLabel
1912
{
1913
    my $self = shift;
1914
    my $src_label = shift;
1915
    my %opt = @_;
1916
    my $branch;
1917
 
1918
    Debug ("backTrackSvnLabel");
1919
    Error ("backTrackSvnLabel: Odd number of args") unless ((@_ % 2) == 0);
1920
 
1921
    #
1922
    #   May need to read and process data twice
1923
    #   First   - stop on copy. May it fast
1924
    #   Second  - all the log.
1925
 
1926
    #
1927
    #   extract data
1928
    #
1929
    foreach my $mode ( '--stop-on-copy', '' )
1930
    {
1931
        #   Init stored data
1932
        #   Used to communicate with callback function(s)
1933
        #
1934
        Information ("backTrackSvnLabel: Performing exhaustive search") unless $mode;
1935
        $self->{btData} = ();
1936
        $self->{btData}{results}{base} = $self->FullPath();
1937
        $self->{btData}{results}{label} = $src_label;
1938
        $self->{btData}{results}{changeSets} = 0;
1939
        $self->{btData}{results}{distance} = 0;
1940
 
1941
        #
1942
        #   Linux does not handle empty arguments in the same
1943
        #   manner as windows. Solution: pass an array
1944
        #
1945
        my @mode;
1946
        push @mode, $mode if ( $mode);
1947
        my $spath = $self->FullPath() . '/' . $src_label;
1948
 
1949
        Verbose2("backTrackSvnLabel. Log from $spath");
1950
        $self->SvnCmd ( 'log', '-v', '--xml', '-q'
1951
                        , @mode
1952
                        , $spath
1953
                        , { 'credentials' => 1,
1954
                            'process' => \&ProcessBackTrack,
1955
                            'printdata' => $opt{printdata},
1956
                            'nosavedata' => 1,
1957
                             }
1958
                            );
1959
 
1960
        last if ( $self->{btData}{good} );
1961
        last if ( $opt{onlysimple} );
1962
    }
1963
 
1964
    #
1965
    #   Did not backtrack to a branch (or trunk)
1966
    #   Return the users label
1967
    #
1968
    unless ( $self->{btData}{good} )
1969
    {
1970
        $branch = $src_label;
1971
    }
1972
    else
1973
    {
1974
        $branch = $self->{btData}{results}{devBranch};
1975
        if ( $opt{savedevbranch} )
1976
        {
1977
            $self->{btData}{results}{devBranch} =~ m~^(.*?)(@|$)~;
1978
            $self->{DEVBRANCH} = $1;
1979
        }
1980
 
1981
    }
1982
 
1983
    #
1984
    #   Return data to the user
1985
    #
1986
    if ( my $refData = $opt{data} )
1987
    {
1988
        Error ('Internal: backTrackSvnLabel. Arg to "data" must be ref to a scalar')
1989
            unless ( ref($refData) eq 'SCALAR' );
1990
        $$refData = $self->{btData}{results};
1991
    }
1992
 
1993
    #
1994
    #   Clean up the data
1995
    #
1996
    delete $self->{btData};
1997
    return $branch;
1998
}
1999
 
2000
#-------------------------------------------------------------------------------
2001
# Function        : ProcessBackTrack
2002
#
2003
# Description     :
2004
#                   Parse
2005
#                       <logentry
2006
#                          revision="24272">
2007
#                       <author>bivey</author>
2008
#                       <date>2005-07-25T15:45:35.000000Z</date>
2009
#                       <paths>
2010
#                       <path
2011
#                          prop-mods="false"
2012
#                          text-mods="false"
2013
#                          kind="dir"
2014
#                          copyfrom-path="/enqdef/branches/Stockholm"
2015
#                          copyfrom-rev="24271"
2016
#                          action="A">/enqdef/tags/enqdef_24.0.1.sls</path>
2017
#                       </paths>
2018
#                       <msg>COTS/enqdef: Tagged by Jats Svn Import</msg>
2019
#                       </logentry>
2020
#
2021
#
2022
#                   Uses:   $self->{btData}     - Scratch Data
2023
#
2024
# Inputs          : $self           - Class Data
2025
#                   $line           - Input data to parse
2026
#
2027
# Returns         : 0 - Do not terminate input command
2028
#
2029
sub  ProcessBackTrack
2030
{
2031
    my ($self, $line ) = @_;
2032
    Message ( $line ) if $self->{PRINTDATA};
2033
 
2034
    $line =~ s~\s+$~~;
2035
    next unless ( $line );
2036
#    Debug0('', $line);
2037
 
2038
    my $workSpace =  \%{$self->{btData}};
2039
    if ( $line =~ m~<logentry$~ ) {
2040
        $workSpace->{mode} = 'l';
2041
        $workSpace->{rev} = 0;
2042
        $workSpace->{changesSeen} = 0;
2043
 
2044
    } elsif ( $line =~ m~</logentry>$~ ) {
2045
        $workSpace->{mode} = '';
2046
 
2047
        #
2048
        #   End of a <logenty>
2049
        #   See if we have a result - a dev branch not copied from a tag
2050
        #
2051
        if ( exists $workSpace->{devBranch} )
2052
        {
2053
            $workSpace->{results}{distance}++;
2054
            $workSpace->{devBranch} =~ m~/((tags|branches|trunk)(/|\@).*)~;
2055
            my $devBranch = $1;
2056
 
2057
            push @{$workSpace->{results}{paths}}, $devBranch;
2058
            unless ( $devBranch =~ m ~^tags~ )
2059
            {
2060
                $workSpace->{results}{devBranch} = $devBranch;
2061
                $workSpace->{results}{isaBranch} = 1;
2062
                $workSpace->{good} = 1;
2063
                return 1;
2064
            }
2065
        }
2066
 
2067
    } elsif ( $line =~ m~<path$~ ) {
2068
        $workSpace->{mode} = 'p';
2069
        Error ('Path without Rev') unless ( $workSpace->{rev} );
2070
 
2071
    } elsif ( $line =~ m~</paths>$~ ) {
2072
        $workSpace->{mode} = '';
2073
    }
2074
    return 0 unless ( $workSpace->{mode} );
2075
 
2076
    if ( $workSpace->{mode} eq 'l' )
2077
    {
2078
        #
2079
        #   Processing logentry data
2080
        #       Only need the rev
2081
        #
2082
        $workSpace->{rev} = $1
2083
            if ( $line =~ m~revision=\"(\d+)\"~ );
2084
 
2085
    } elsif ( $workSpace->{mode} eq 'p' ) {
2086
        #
2087
        #   Processing Paths
2088
        #       Entries appear to be in a random order
2089
        #       Not always the same order
2090
        #
2091
        my $end = 0;
2092
        if ( $line =~ s~\s*(.+?)="(.+)">(.*)</path>$~~ )
2093
        {
2094
            #
2095
            #   Last entry has two items
2096
            #       Attribute
2097
            #       Data Item
2098
            #
2099
            $end = 1;
2100
            $workSpace->{path}{$1} = $2;
2101
            $workSpace->{path}{DATA} = $3;
2102
        }
2103
        elsif ($line =~ m~\s*(.*?)="(.*)"~ )
2104
        {
2105
            $workSpace->{path}{$1} = $2;
2106
        }
2107
#        else
2108
#        {
2109
#            Warning ("Cannot decode XML log: $line");
2110
#        }
2111
 
2112
        if ( $end )
2113
        {
2114
            #
2115
            #   If the Repo is created by a pre 1.6 SVN, then kind will be
2116
            #   empty. Have a guess.
2117
            #
2118
            if ( $workSpace->{path}{'kind'} eq '' )
2119
            {
2120
                if ( exists $workSpace->{path}{'copyfrom-path'} ) {
2121
                    $workSpace->{path}{'kind'} = 'dir';
2122
                } else {
2123
                    $workSpace->{path}{'kind'} = 'file';
2124
                }
2125
            }
2126
 
2127
            if ( $workSpace->{path}{'kind'} eq 'dir' &&  exists $workSpace->{path}{'copyfrom-path'} )
2128
            {
2129
                my $srev = $workSpace->{path}{'copyfrom-rev'};
2130
                my $from = $workSpace->{path}{'copyfrom-path'};
2131
                if ( $from =~ m~/trunk$~ || $from =~ m~/branches/[^/]+$~ )
2132
                {
2133
                    $workSpace->{devBranch} = $from . '@' . $srev;
2134
                }
2135
            }
2136
 
2137
            elsif ( $workSpace->{path}{'kind'} eq 'file' )
2138
            {
2139
                #
2140
                #   Track files that have been changed between tag and branch
2141
                #   The log is presented as newest first
2142
                #   The files have a tag-name component.
2143
                #       Remove the tag name - so that we can compare files
2144
                #       Save the first instance of changed files
2145
                #           Others will be in older versions
2146
                #           and thus of no interest
2147
                #
2148
                #   Count the chnage sets that have changes
2149
                #   Having changes in multiple change sets indicates
2150
                #   development on a /tags/ - which is BAD
2151
                #
2152
                $workSpace->{path}{'DATA'} =~ m~(.+)/((tags|branches|trunk)(/|$).*)~;
2153
                my $file =  $2;
2154
                my $full = $file;
2155
                $file =~ s~^tags/(.+?)/~~;
2156
 
2157
                if ( ! exists $workSpace->{files}{$file}  )
2158
                {
2159
                    push @{$workSpace->{results}{files}}, $full . '@' . $workSpace->{rev};
2160
                }
2161
                $workSpace->{files}{$file}++;
2162
                $workSpace->{firstFile} = $file unless ( defined $workSpace->{firstFile} );
2163
 
2164
                unless ( $workSpace->{changesSeen} )
2165
                {
2166
                    unless( $workSpace->{firstFile} eq $file )
2167
                    {
2168
                        $workSpace->{results}{changeSets}++;
2169
                        $workSpace->{changesSeen}++;
2170
                    }
2171
                }
2172
 
2173
                if ( scalar keys %{$workSpace->{files}} > 1 )
2174
                {
2175
                    $workSpace->{results}{multipleChanges} = 1;
2176
                    Verbose ("backTrackSvnLabel: Changes in multiple versions");
2177
                }
2178
            }
2179
 
2180
            delete $workSpace->{path};
2181
        }
2182
    }
2183
 
2184
    #
2185
    #   Return 0 to keep on going
2186
    return 0;
2187
}
2188
 
267 dpurdie 2189
#------------------------------------------------------------------------------
2190
1;