Skip to content
Browse files

added dirty_dir attr to Git::Commit and a test for it

  • Loading branch information...
1 parent 5859ad7 commit f2a9a6974d9e3feff7fcb1097161aa8bc1f9695f @apocalypse apocalypse committed with jquelin Dec 13, 2010
View
1 Changes
@@ -1,6 +1,7 @@
Revision history for Dist::Zilla::Plugin::Git
{{$NEXT}}
+ - add dirty_dir to Commit so you can commit an entire dir (Apocalypse)
1.110480 2011-02-17 18:03:06 Europe/Paris
- CommitBuild support for release_message (nigel metheringham)
View
28 lib/Dist/Zilla/Plugin/Git/Commit.pm
@@ -10,6 +10,7 @@ use Git::Wrapper;
use Moose;
use MooseX::Has::Sugar;
use MooseX::Types::Moose qw{ Str };
+use Path::Class::Dir ();
use String::Formatter method_stringf => {
-as => '_format_string',
@@ -34,10 +35,12 @@ with 'Dist::Zilla::Role::Git::DirtyFiles';
has commit_msg => ( ro, isa=>Str, default => 'v%v%n%n%c' );
has time_zone => ( ro, isa=>Str, default => 'local' );
-
+has dirty_dir => ( ro, isa=>'ArrayRef[Str]', default => sub { [] } );
# -- public methods
+sub mvp_multivalue_args { qw( dirty_dir ) }
+
sub after_release {
my $self = shift;
my $git = Git::Wrapper->new('.');
@@ -48,7 +51,22 @@ sub after_release {
# otherwise before_release would have failed, ending the release
# process.
@output = sort { lc $a cmp lc $b } $self->list_dirty_files($git, 1);
- return unless @output;
+
+ # add the files in dirty_dir to be committed
+ if ( @{ $self->dirty_dir } ) {
+ my @untracked_files = $git->ls_files( { others=>1, 'exclude-standard'=>1 } );
+ foreach my $f ( @untracked_files ) {
+ foreach my $path ( @{ $self->dirty_dir } ) {
+ if ( Path::Class::Dir->new( $path )->subsumes( $f ) ) {
+ push( @output, $f );
+ last;
+ }
+ }
+ }
+ }
+
+ # if nothing to commit, we're done!
+ return unless @output;
# write commit message in a temp file
my ($fh, $filename) = tempfile( 'DZP-git.XXXX', UNLINK => 1 );
@@ -99,7 +117,7 @@ sub _get_changes {
__END__
=for Pod::Coverage
- after_release
+ after_release mvp_multivalue_args
=head1 SYNOPSIS
@@ -134,6 +152,10 @@ C<v%v%n%n%c>, meaning the version number and the list of changes.
=item * time_zone - the time zone to use with C<%d>. Can be any
time zone name accepted by DateTime. Defaults to C<local>.
+=item * dirty_dir - a path that will have it's contents checked in if
+it is locally modified. This option may appear multiple times. The default
+list is empty.
+
=back
You can use the following codes in commit_msg:
View
50 t/2-commit-dirtydir.t
@@ -0,0 +1,50 @@
+#!perl
+
+use strict;
+use warnings;
+
+use Dist::Zilla 1.093250;
+use Dist::Zilla::Tester;
+use Git::Wrapper;
+use Path::Class;
+use lib 't/lib';
+use Test::More tests => 3;
+
+# build fake repository
+my $zilla = Dist::Zilla::Tester->from_config({
+ dist_root => dir(qw(t commit-dirtydir)),
+});
+
+chdir $zilla->tempdir->subdir('source');
+system "git init";
+my $git = Git::Wrapper->new('.');
+$git->config( 'user.name' => 'dzp-git test' );
+$git->config( 'user.email' => 'dzp-git@test' );
+$git->add( qw{ dist.ini Changes } );
+$git->commit( { message => 'initial commit' } );
+
+# do a release, with changes and dist.ini updated
+append_to_file('Changes', "\n");
+append_to_file('dist.ini', "\n");
+$zilla->release;
+
+# check if dist.ini and changelog have been committed
+my ($log) = $git->log( 'HEAD' );
+is( $log->message, "v1.23\n\n- foo\n- bar\n- baz\n", 'commit message taken from changelog' );
+
+# check if we committed our tarball
+my @files = $git->ls_files( { cached => 1 } );
+ok( ( grep { $_ =~ /releases/ } @files ), "We committed the tarball" );
+
+# We should have no dirty files uncommitted
+# ignore the "DZP-git.9y5u" temp file, ha!
+@files = $git->ls_files( { others => 1, modified => 1, unmerged => 1 } );
+ok( @files == 1, "No untracked files left" );
+
+sub append_to_file {
+ my ($file, @lines) = @_;
+ open my $fh, '>>', $file or die "can't open $file: $!";
+ print $fh @lines;
+ close $fh;
+}
+
View
6 t/commit-dirtydir/Changes
@@ -0,0 +1,6 @@
+Changes
+
+1.23 2009-11-16 19:15:45 CET
+ - foo
+ - bar
+ - baz
View
12 t/commit-dirtydir/dist.ini
@@ -0,0 +1,12 @@
+name = Foo
+version = 1.23
+author = foobar
+license = Perl_5
+abstract = Test Library
+copyright_holder = foobar
+copyright_year = 2009
+
+[FakeRelease]
+[MyTestArchiver]
+[Git::Commit]
+dirty_dir = releases
View
21 t/lib/Dist/Zilla/Plugin/MyTestArchiver.pm
@@ -0,0 +1,21 @@
+# taken from DZP::ArchiveRelease, thanks CJM!
+package Dist::Zilla::Plugin::MyTestArchiver;
+use Moose;
+use Moose::Autobox;
+use Path::Class::Dir ();
+use File::Copy ();
+
+with 'Dist::Zilla::Role::Releaser';
+
+sub release {
+ my ($self, $tgz) = @_;
+
+ chmod(0444, $tgz);
+ my $dir = 'releases';
+ mkdir $dir or $self->log_fatal( "Unable to create directory $dir: $!" );
+ my $dest = Path::Class::Dir->new( $dir )->file($tgz->basename);
+ File::Copy::move($tgz, $dest) or $self->log_fatal( "Unable to move: $!" );
+ $self->log("Moved $tgz to $dest");
+}
+
+1;

0 comments on commit f2a9a69

Please sign in to comment.
Something went wrong with that request. Please try again.