Subject: | added Cvs::Command::Add and Cvs::Command::Result classes |
Cvs does not appear to implement the `cvs add` subcommand; this makes it difficult to use
Cvs to add files to an existing repository.
I have written Cvs::Command::Add and Cvs::Result::Add classes to implement this command;
attached is a patch against 0.07. My patch also adds a dependency on File::Type (used to
autodetect whether the '-kb' argument should be passed to `cvs add`).
Subject: | Cvs-Add.patch |
diff -Naur Cvs-0.07-old/lib/Cvs/Command/Add.pm Cvs-0.07/lib/Cvs/Command/Add.pm
--- Cvs-0.07-old/lib/Cvs/Command/Add.pm 1969-12-31 19:00:00.000000000 -0500
+++ Cvs-0.07/lib/Cvs/Command/Add.pm 2007-04-16 14:57:53.000000000 -0400
@@ -0,0 +1,87 @@
+package Cvs::Command::Add;
+
+use strict;
+use Cvs::Result::Add;
+use Cvs::Cvsroot;
+use base qw(Cvs::Command::Base);
+
+use File::Type;
+
+sub init
+{
+ my($self, $file, $param) = @_;
+ $self->SUPER::init(@_) or return;
+
+ $self->default_params
+ (
+ 'binary' => 0,
+ );
+ $self->param($param);
+
+ return $self->error('Mandatory option: file')
+ unless(defined $file);
+
+ $self->command('add');
+
+ # add '-kb' if the file is a binary
+ if ( $self->param->{'binary'} || isBinary( $self, $file ) ) {
+ $self->push_arg( '-kb' );
+ }
+ $self->push_arg($file);
+ $self->go_into_workdir(1);
+
+ my $result = new Cvs::Result::Add;
+ $self->result($result);
+
+ my $main = $self->new_context();
+ $self->initial_context($main);
+
+ return $self;
+}
+
+sub isBinary
+{
+ my( $self, $filename ) = @_;
+ my( @RETURN );
+
+ # fully qualify the filename
+ $filename = ( $self->cvs->working_directory() . '/' . $filename );
+
+ my( $ft ) = File::Type->new();
+
+ return( 0 ) unless ( -r $filename );
+ my( $filetype ) = $ft->mime_type( $filename );
+
+ # if it's text/<whatever> or unidentifiable, return false; otherwise return true
+ unless ( ( $filetype =~ /^text\// ) || ( $filetype eq 'application/octet-stream' ) ) {
+ push( @RETURN, 1 );
+ }
+
+ return wantarray ? @RETURN : join( '', @RETURN );
+
+}
+
+1;
+=pod
+
+=head1 LICENCE
+
+This library is free software; you can redistribute it and/or modify
+it under the terms of the GNU Lesser General Public License as
+published by the Free Software Foundation; either version 2.1 of the
+License, or (at your option) any later version.
+
+This library is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+Lesser General Public License for more details.
+
+You should have received a copy of the GNU Lesser General Public
+License along with this library; if not, write to the Free Software
+Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+USA
+
+=head1 COPYRIGHT
+
+Copyright (C) 2003 - Olivier Poitrey
+
diff -Naur Cvs-0.07-old/lib/Cvs/Result/Add.pm Cvs-0.07/lib/Cvs/Result/Add.pm
--- Cvs-0.07-old/lib/Cvs/Result/Add.pm 1969-12-31 19:00:00.000000000 -0500
+++ Cvs-0.07/lib/Cvs/Result/Add.pm 2007-04-13 11:08:45.000000000 -0400
@@ -0,0 +1,35 @@
+package Cvs::Result::Add;
+
+use strict;
+use base qw(Cvs::Result::Base);
+
+sub init
+{
+ my $self = shift->SUPER::init(@_);
+ return $self;
+}
+
+1;
+=pod
+
+=head1 LICENCE
+
+This library is free software; you can redistribute it and/or modify
+it under the terms of the GNU Lesser General Public License as
+published by the Free Software Foundation; either version 2.1 of the
+License, or (at your option) any later version.
+
+This library is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+Lesser General Public License for more details.
+
+You should have received a copy of the GNU Lesser General Public
+License along with this library; if not, write to the Free Software
+Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+USA
+
+=head1 COPYRIGHT
+
+Copyright (C) 2003 - Olivier Poitrey
+
diff -Naur Cvs-0.07-old/lib/Cvs.pm Cvs-0.07/lib/Cvs.pm
--- Cvs-0.07-old/lib/Cvs.pm 2004-12-08 10:03:26.000000000 -0500
+++ Cvs-0.07/lib/Cvs.pm 2007-04-16 15:04:18.000000000 -0400
@@ -117,6 +117,19 @@
L<Cvs::Result::Checkout>.
+=head2 add
+
+ Cvs::Result::Add = $cvs->add( $filename, { 'binary' => 1 } );
+
+Adds the specified file to the CVS repository specified in the Cvs object.
+
+Cvs::Command::Add uses the L<File::Type> module to guess whether the specified file should
+be checked in as a text file or a binary file; however, if desired, you can override this
+autodetection by passing a true or false value to the C<binary> parameter. The default
+value of C<binary> is false.
+
+L<Cvs::Result::Add>.
+
=head2 update
Cvs::Result::Update = $cvs->update();
diff -Naur Cvs-0.07-old/t/04add.t Cvs-0.07/t/04add.t
--- Cvs-0.07-old/t/04add.t 1969-12-31 19:00:00.000000000 -0500
+++ Cvs-0.07/t/04add.t 2007-04-16 15:16:55.000000000 -0400
@@ -0,0 +1,24 @@
+use strict;
+use Test;
+use Cwd;
+use Data::Dumper;
+
+plan test => 5;
+
+use Cvs;
+ok(1);
+
+require File::Type;
+ok(1);
+
+my $cvs = new Cvs 'cvs-test';
+ok($cvs);
+
+open(FILE, "> $ENV{PWD}/cvs-test/add-test.txt")
+ or die "Cannot open file `$ENV{PWD}/cvs-test/add-test.txt': $!";
+print FILE "$$ Cvs add test - plain text";
+close FILE;
+
+my $add = $cvs->add( 'test.txt' );
+ok( $add->success() );
+ok( ! $add->error() );