Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

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

0 comments on commit f2a9a69

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