Subversion Repositories DevTools

Rev

Rev 2026 | Blame | Compare with Previous | Last modification | View Log | RSS feed

#! perl
########################################################################
# Copyright ( C ) 2005 ERG Limited, All rights reserved
#
# Module name   : jats.sh
# Module type   : Makefile system
# Compiler(s)   : n/a
# Environment(s): jats
#
# Description   : Process a hijacked file(s)
#                 Check it in on a branch
#
# Usage:
#
# Version   Who      Date        Description
#
#......................................................................#

require 5.006_001;
use strict;
use warnings;
#use Data::Dumper;
use Getopt::Long;
use Pod::Usage;                         # required for help support

#
#   Config items
#
my $branch  = "ddp_test_branch";        # Branch to checkout the file in
                                        # Use one name for lots of work

my $opt_verbose = 0;                    # Verbosity
my $opt_help = 0;
my $opt_manual;
my $pname = "HIJACK_CI";                # Utility name for display messages
my $VERSION = "1.0.0";

#
#   Globals
#
my @error_list;
my $last_result;

#-------------------------------------------------------------------------------
# Function        : Main Entry
#
# Description     : This small program will process hijacked files in a
#                   static view. It will create a branch for the file and then
#                   check the file in on the branch
#
#
# Inputs          : None
#
# Returns         : Even less.
#

my $result = GetOptions (
                "help+"     => \$opt_help,          # flag, multiple use allowed
                "manual"    => \$opt_manual,        # flag
                "verbose+"  => \$opt_verbose,       # flag
                );

#
#   Process help and manual options
#
pod2usage(-verbose => 0, -message => "Version: $VERSION")  if ($opt_help == 1  || ! $result);
pod2usage(-verbose => 1)  if ($opt_help == 2 );
pod2usage(-verbose => 2)  if ($opt_manual || ($opt_help > 2));

error ("No files to process")
    unless ( $#ARGV >= 0  );

#
#   Process files one by one
#
foreach  ( @ARGV )
{
    process_file( $_ );
}
exit;


#-------------------------------------------------------------------------------
# Function        : process_file
#
# Description     : Process one file
#
# Inputs          : A single file
#
# Returns         :
#
sub process_file
{
    my ($file) = @_;
    #
    #   Sanity test
    #
    unless ( -f $file )
    {
        Warning ("File not found: $file");
        return 1;
    }

    #
    #   Ensure that the file is hijacked
    #
    ClearCmd( "ls  $file" );
    error ("Program Terminated") if ( @error_list );

    unless ( $last_result =~ m~\[.*hijacked.*\]~ )
    {
        Warning ("File is not hijacked: $file");
        return 1;
    }

    #
    #   remove any .keep file
    #   Its easier than trying to detect which file ClearCase will create
    #
    if ( -f "$file.keep"  )
    {
        unlink ( "$file.keep" ) || error ("Cannot delete $file.keep");
    }

    #
    #   Status the version of the file in the view
    #   Use to determine vital file info
    #
    ClearCmd( "ls  -short $file" );
    error ("Program Terminated") if ( @error_list );

    $last_result =~ tr~\\/~/~s;
    my $full_path = $last_result;

    my $branch_path = $last_result;
       $branch_path =~ s~[/][^/]*$~~;

    my $root_branch = $branch_path;
       $root_branch =~ s~.*@/~~;

    my $is_on_branch = 0;
       $is_on_branch = 1 if ( $root_branch =~ m~/$branch~  ) ;

    my $target_branch = $is_on_branch ? $root_branch : "$root_branch/$branch";

    vprint ("Full Path        : $full_path");
    vprint ("BranchPath       : $branch_path");
    vprint ("RootPath         : $root_branch");
    vprint ("TgtBranch        : $target_branch");
    vprint ("Is Branched      : $is_on_branch");

    #
    #   Branch the file if it has not been branched
    #
    unless ( $is_on_branch )
    {

        #
        #   Ensure that the required branch exists in this VOB
        #
        ClearCmd ("lstype -short brtype:$branch" );
        if ( $last_result ne $branch )
        {
            vprint ("Create the branch in this VOB" );
            ClearCmd ("mkbrtype -c \"A development branch\" $branch" );
            error ("Program Terminated") if ( @error_list );
        }

        vprint ("Test. Has the file been branched" );
        if ( ClearCmd( "describe -short ${branch_path}/${branch}/0" ) )
        {
            vprint ("Create the initial branch point" );
            ClearCmd( "mkbranch -nco -nc -nwarn $branch $full_path" );
        }
    }

    #
    #   Checkout the file on the branch
    #   Don't checkout any data - its not needed
    #   Since its a hijacked file the file will be renamed to a .keep file
    #
    vprint ("Checkout the file on a branch" );
    ClearCmd ("co -nc -nq -ndata -nwarn -branch \"$target_branch\" $full_path");
    error ("Program Terminated") if ( @error_list );

    #
    #   Checkin the hijacked file
    #   Checkin the .keep file directly
    #
    ClearCmd ("ci -c \"Hijacked file check in\" -identical -from \"$file.keep\" \"$file\"");
    error ("Program Terminated") if ( @error_list );
}

#-------------------------------------------------------------------------------
# Function        : ClearCmd
#
# Description     : Similar to the system command
#                   Does allow standard output and standard error to be captured
#                   to a log file
#
#                   Used since I was having problems with calling other programs
#                   and control-C. It could hang the terminal session.
#
# Inputs          :
#
# Returns         :
#
sub ClearCmd
{
    my( $cmd ) = @_;
    vprint2( "cleartool $cmd" );

        @error_list = ();
        open(CMD, "cleartool $cmd  2>&1 |")    || error( "can't run command: $!" );
        while (<CMD>)
        {
            chomp;
            $last_result = $_;
            $last_result =~ tr~\\/~/~s;
            vprint ( "cleartool resp:" . $_);
            push @error_list, $_ if ( m~Error:~ );
        }
        close(CMD);

    vprint2( "Exit Status: $?" );
    return $? / 256;
}

#-------------------------------------------------------------------------------
# FUNCTION        : error
#
# DESCRIPTION     : Display an error message and exit
#
# INPUTS          : Arguments to be displayed
#
# RETURNS         : This function does not return
#
sub error
{
    print STDERR "[$pname] ERROR: @_\n" ;
    foreach ( @error_list )
    {
        print "$_\n";
    }
    exit 1;
}

sub Warning
{
    print STDERR "[$pname] WARNING: @_\n" ;
}


#-------------------------------------------------------------------------------
# Function        : vprint
#
# Description     : Print if verbose mode is enabled
#
# Inputs          :
#
# Returns         :
#

sub vprint
{
    print STDERR "[$pname] @_\n"
        if ( $opt_verbose );
}
sub vprint2
{
    print STDERR "[$pname] @_\n"
        if ( $opt_verbose > 1 );
}


#-------------------------------------------------------------------------------
#   Documentation
#

=pod

=head1 NAME

xxxxx - Save a hijacked file on a private branch

=head1 SYNOPSIS

  xxxxxx [options] files...

 Options:
    -help               - brief help message
    -help -help         - Detailed help message
    -man                - Full documentation
    -verbose            - Verbose operation

=head1 OPTIONS

=over 8

=item B<-help>

Print a brief help message and exits.

=item B<-help -help>

Print a detailed help message with an explanation for each option.

=item B<-man>

Prints the manual page and exits.

=item B<-verbose>

Increases program output. This option may be specified mutiple times

=back

=head1 DESCRIPTION

This small program will process hijacked files in a static view. It will create
a branch for the file and then check the file in on the branch

=cut