cvsadd [download]


#!/usr/bin/perl -w
# $Revision: 1.11 $
# $Date: 2006-11-11 15:08:49 $
# Luis Mondesi <lemsx1@gmail.com>
#
# DESCRIPTION: Use this to add a file or series of files to
# CVS in one shot!
#
# USAGE: cvsadd [filename [filename ...]]
#
# LICENSE: GPL
#
# Changelog:
# 2003-09-27 04:53 EDT made silent
# 2005-09-25 18:02 EDT much more efficient
# 2006-01-02 22:49 EST commit after all files are added

=pod

=head1 NAME

cvsadd - adds files to CVS in one whoop

=head1 DESCRIPTION 

    This script is used to quickly add and commit files to CVS

=cut

$|++;
use strict;
use Getopt::Long;
Getopt::Long::Configure('bundling');

my $DEBUG = 0;
my $HELP = 0;
my $INCLUDE_DIRS=0;
my $EXCEPTIONS_REGEX = "(.*\.swp\$|.*\.o\$|.*\.so\$)";
my @FILES = ();
my $CVS_COMMAND = "cvs -q -w -z3 ";

=pod

=head1 SYNOPSIS

B<cvsadd>  
                [-h,--help]
                [-U,--usage]

=head1 OPTIONS

=over 8

=item -h,--help

Prints this help and exits

=item -U,--usage

Prints usage information and exits

=back

=cut


GetOptions(
    # flags
#    'v|version'         =>  \$PVERSION,
    'h|U|usage|help'    =>  \$HELP,
    'D|debug'           =>  \$DEBUG,
    'd|include-dirs'    =>  \$INCLUDE_DIRS,
    # strings
    #'o|option=s'       =>  \$NEW_OPTION,
    # numbers
    #'a|another-option=i'      =>  \$NEW_ANOTHER_OPTION,
);

sub _usage
{
    use Pod::Usage;
    pod2usage(1);
}

if ($HELP)
{
    _usage();
    exit 0;    # never reaches here
}

sub is_image
{
    # returns 1 if true
    my $file = shift;
    print STDERR ( "is_image() called: $file\n" ) if ( $DEBUG );
    my $file_t = qx/file "$file"/;
    if ( $file_t =~ m/(\bGIF\b|\bPNG\b|\bJPEG\b)/i )
    {
        return 1;
    }
    print STDERR ( "not image file\n" ) if ( $DEBUG );
    return 0;
}

sub is_binary
{
    # returns 1 if true
    my $file = shift;
    print STDERR ( "is_binary() called: $file\n" ) if ( $DEBUG );
    my $file_t = qx/file "$file"/;
    if ( $file_t =~ m/(text\s+executable|\btext\b)/i )
    {
        return 0;
    }
    print STDERR ( "binary file\n" ) if ( $DEBUG );
    return 1;
}

if ( defined($ARGV[0]) and -f $ARGV[0] )
{
    @FILES = @ARGV;    
} else {
    my $addlist = qx/$CVS_COMMAND update/;
    for my $i ( split(/\n/,$addlist) )
    {
        die ("Modifed file detected. Please commit these files before continuing. $i\n") 
            if ( $i =~ /^\s*M\s+/ );
        next if ($i !~ /^\s*\?/g); # if not a new file, keep on
        $i =~ s/^\s*\?\s*//;    # remove ? from the beginning of the line
        push(@FILES,$i);        # files will be tested later
    }
}

for my $file (@FILES)
{
    if (    ! -f $file
            and ! is_image($file)
            and ( is_binary($file) or $file =~ /$EXCEPTIONS_REGEX/ )
    )
    {
        unless ( $INCLUDE_DIRS )
        {
            print STDERR ("Skipping file «$file»\n");
            next;
        }
    }
    system("$CVS_COMMAND add $file 2> /dev/null");
    print STDERR ("Error adding «$file» to the repository\n")
        if ( $? != 0 );
}
# now commit
if ( $? == 0 )
{
    system( "$CVS_COMMAND commit -m 'first commit'" );
} 

=pod

=head1 AUTHORS

Luis Mondesi <lemsx1@gmail.com>

=cut