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