Subversion Repositories DevTools

Rev

Rev 7299 | Rev 7304 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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