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