| 1 | use 5.008; #utf8 | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use utf8; | ||
| 5 | |||
| 6 | package Git::PurePerl::Walker; | ||
| 7 | |||
| 8 | our $VERSION = '0.004000'; | ||
| 9 | |||
| 10 | # ABSTRACT: Walk over a sequence of commits in a Git::PurePerl repo | ||
| 11 | |||
| 12 | our $AUTHORITY = 'cpan:KENTNL'; # AUTHORITY | ||
| 13 | |||
| 14 | use Moose qw( has ); | ||
| 15 | use Path::Tiny qw(); | ||
| 16 | use Module::Runtime qw( ); | ||
| 17 | use Git::PurePerl::Walker::Types qw( GPPW_Repository GPPW_Methodish GPPW_Method GPPW_OnCommitish GPPW_OnCommit); | ||
| 18 | use namespace::autoclean; | ||
| 19 | |||
| 20 | |||
| 21 | |||
| 22 | |||
| 23 | |||
| 24 | |||
| 25 | |||
| 26 | |||
| 27 | |||
| 28 | |||
| 29 | |||
| 30 | |||
| 31 | |||
| 32 | |||
| 33 | |||
| 34 | has repo => ( | ||
| 35 | isa => GPPW_Repository, | ||
| 36 | is => 'ro', | ||
| 37 | lazy_build => 1, | ||
| 38 | ); | ||
| 39 | |||
| 40 | |||
| 41 | |||
| 42 | |||
| 43 | |||
| 44 | |||
| 45 | |||
| 46 | |||
| 47 | |||
| 48 | |||
| 49 | |||
| 50 | |||
| 51 | |||
| 52 | |||
| 53 | |||
| 54 | |||
| 55 | |||
| 56 | |||
| 57 | |||
| 58 | |||
| 59 | |||
| 60 | |||
| 61 | |||
| 62 | |||
| 63 | |||
| 64 | |||
| 65 | |||
| 66 | |||
| 67 | |||
| 68 | |||
| 69 | |||
| 70 | |||
| 71 | |||
| 72 | |||
| 73 | |||
| 74 | |||
| 75 | |||
| 76 | |||
| 77 | |||
| 78 | |||
| 79 | |||
| 80 | has _method => ( | ||
| 81 | init_arg => 'method', | ||
| 82 | is => 'ro', | ||
| 83 | isa => GPPW_Methodish, | ||
| 84 | required => 1, | ||
| 85 | ); | ||
| 86 | |||
| 87 | |||
| 88 | |||
| 89 | |||
| 90 | |||
| 91 | |||
| 92 | |||
| 93 | |||
| 94 | |||
| 95 | |||
| 96 | has 'method' => ( | ||
| 97 | init_arg => undef, | ||
| 98 | is => 'ro', | ||
| 99 | isa => GPPW_Method, | ||
| 100 | lazy_build => 1, | ||
| 101 | ); | ||
| 102 | |||
| 103 | |||
| 104 | |||
| 105 | |||
| 106 | |||
| 107 | |||
| 108 | |||
| 109 | |||
| 110 | |||
| 111 | |||
| 112 | |||
| 113 | |||
| 114 | |||
| 115 | |||
| 116 | |||
| 117 | |||
| 118 | |||
| 119 | |||
| 120 | |||
| 121 | |||
| 122 | |||
| 123 | |||
| 124 | |||
| 125 | |||
| 126 | |||
| 127 | |||
| 128 | |||
| 129 | |||
| 130 | |||
| 131 | |||
| 132 | |||
| 133 | |||
| 134 | |||
| 135 | |||
| 136 | |||
| 137 | |||
| 138 | |||
| 139 | |||
| 140 | |||
| 141 | |||
| 142 | |||
| 143 | |||
| 144 | |||
| 145 | |||
| 146 | |||
| 147 | |||
| 148 | |||
| 149 | |||
| 150 | |||
| 151 | |||
| 152 | |||
| 153 | |||
| 154 | |||
| 155 | has '_on_commit' => ( | ||
| 156 | init_arg => 'on_commit', | ||
| 157 | required => 1, | ||
| 158 | is => 'ro', | ||
| 159 | isa => GPPW_OnCommitish, | ||
| 160 | ); | ||
| 161 | |||
| 162 | |||
| 163 | |||
| 164 | |||
| 165 | |||
| 166 | |||
| 167 | |||
| 168 | |||
| 169 | |||
| 170 | |||
| 171 | has 'on_commit' => ( | ||
| 172 | init_arg => undef, | ||
| 173 | isa => GPPW_OnCommit, | ||
| 174 | is => 'ro', | ||
| 175 | lazy_build => 1, | ||
| 176 | ); | ||
| 177 | |||
| 178 | |||
| 179 | |||
| 180 | |||
| 181 | |||
| 182 | |||
| 183 | |||
| 184 | |||
| 185 | |||
| 186 | sub BUILD { | ||
| 187 | my ( $self, ) = @_; | ||
| 188 | $self->reset; | ||
| 189 | return $self; | ||
| 190 | } | ||
| 191 | |||
| 192 | |||
| 193 | |||
| 194 | |||
| 195 | |||
| 196 | sub _build_repo { | ||
| 197 | require Git::PurePerl; | ||
| 198 | return Git::PurePerl->new( directory => Path::Tiny->cwd->stringify ); | ||
| 199 | } | ||
| 200 | |||
| 201 | |||
| 202 | |||
| 203 | |||
| 204 | |||
| 205 | sub _build_method { | ||
| 206 | my ($self) = shift; | ||
| 207 | my ($method) = $self->_method; | ||
| 208 | |||
| 209 | if ( not ref $method ) { | ||
| 210 | my $method_name = Module::Runtime::compose_module_name( 'Git::PurePerl::Walker::Method', $method ); | ||
| 211 | Module::Runtime::require_module($method_name); | ||
| 212 | $method = $method_name->new(); | ||
| 213 | } | ||
| 214 | return $method->for_repository( $self->repo ); | ||
| 215 | } | ||
| 216 | |||
| 217 | |||
| 218 | |||
| 219 | |||
| 220 | |||
| 221 | sub _build_on_commit { | ||
| 222 | my ($self) = shift; | ||
| 223 | my ($on_commit) = $self->_on_commit; | ||
| 224 | |||
| 225 | if ( ref $on_commit and 'CODE' eq ref $on_commit ) { | ||
| 226 | my $on_commit_name = 'Git::PurePerl::Walker::OnCommit::CallBack'; | ||
| 227 | my $callback = $on_commit; | ||
| 228 | Module::Runtime::require_module($on_commit_name); | ||
| 229 | $on_commit = $on_commit_name->new( callback => $callback, ); | ||
| 230 | } | ||
| 231 | elsif ( not ref $on_commit ) { | ||
| 232 | my $on_commit_name = 'Git::PurePerl::Walker::OnCommit::' . $on_commit; | ||
| 233 | Module::Runtime::require_module($on_commit_name); | ||
| 234 | $on_commit = $on_commit_name->new(); | ||
| 235 | } | ||
| 236 | return $on_commit->for_repository( $self->repo ); | ||
| 237 | } | ||
| 238 | |||
| 239 | |||
| 240 | |||
| 241 | |||
| 242 | |||
| 243 | |||
| 244 | |||
| 245 | |||
| 246 | |||
| 247 | ## no critic (Subroutines::ProhibitBuiltinHomonyms) | ||
| 248 | sub reset { | ||
| 249 | my $self = shift; | ||
| 250 | $self->method->reset; | ||
| 251 | $self->on_commit->reset; | ||
| 252 | return $self; | ||
| 253 | } | ||
| 254 | |||
| 255 | |||
| 256 | |||
| 257 | |||
| 258 | |||
| 259 | |||
| 260 | |||
| 261 | |||
| 262 | |||
| 263 | |||
| 264 | |||
| 265 | |||
| 266 | |||
| 267 | |||
| 268 | |||
| 269 | |||
| 270 | |||
| 271 | |||
| 272 | |||
| 273 | |||
| 274 | |||
| 275 | |||
| 276 | |||
| 277 | |||
| 278 | |||
| 279 | |||
| 280 | |||
| 281 | |||
| 282 | sub step { | ||
| 283 | my $self = shift; | ||
| 284 | |||
| 285 | $self->on_commit->handle( $self->method->current ); | ||
| 286 | |||
| 287 | if ( not $self->method->has_next ) { | ||
| 288 | return; | ||
| 289 | } | ||
| 290 | |||
| 291 | $self->method->next; | ||
| 292 | |||
| 293 | return 1; | ||
| 294 | } | ||
| 295 | |||
| 296 | |||
| 297 | |||
| 298 | |||
| 299 | |||
| 300 | |||
| 301 | |||
| 302 | |||
| 303 | |||
| 304 | |||
| 305 | |||
| 306 | |||
| 307 | sub step_all { | ||
| 308 | my $self = shift; | ||
| 309 | my $steps = 1; | ||
| 310 | while ( $self->step ) { | ||
| 311 | $steps++; | ||
| 312 | } | ||
| 313 | return $steps; | ||
| 314 | } | ||
| 315 | |||
| 316 | __PACKAGE__->meta->make_immutable; | ||
| 317 | no Moose; | ||
| 318 | |||
| 319 | 1; | ||
| 320 | |||
| 321 | __END__ | ||
| 322 | |||
| 323 | =pod | ||
| 324 | |||
| 325 | =encoding UTF-8 | ||
| 326 | |||
| 327 | =head1 NAME | ||
| 328 | |||
| 329 | Git::PurePerl::Walker - Walk over a sequence of commits in a Git::PurePerl repo | ||
| 330 | |||
| 331 | =head1 VERSION | ||
| 332 | |||
| 333 | version 0.004000 | ||
| 334 | |||
| 335 | =head1 SYNOPSIS | ||
| 336 | |||
| 337 | use Git::PurePerl::Walker; | ||
| 338 | use Git::PurePerl::Walker::Method::FirstParent; | ||
| 339 | |||
| 340 | my $repo = Git::PurePerl->new( ... ); | ||
| 341 | |||
| 342 | my $walker = Git::PurePerl::Walker->new( | ||
| 343 | repo => $repo, | ||
| 344 | method => Git::PurePerl::Walker::Method::FirstParent->new( | ||
| 345 | start => $repo->ref_sha1('refs/heads/master'), | ||
| 346 | ), | ||
| 347 | on_commit => sub { | ||
| 348 | my ( $commit ) = @_; | ||
| 349 | print $commit->sha1; | ||
| 350 | }, | ||
| 351 | ); | ||
| 352 | |||
| 353 | $walker->step_all; | ||
| 354 | |||
| 355 | =head1 CONSTRUCTOR ARGUMENTS | ||
| 356 | |||
| 357 | =head2 repo | ||
| 358 | |||
| 359 | B<Mandatory:> An instance of L<< C<Git::PurePerl>|Git::PurePerl >> representing | ||
| 360 | the repository to work with. | ||
| 361 | |||
| 362 | =head2 method | ||
| 363 | |||
| 364 | B<Mandatory:> either a C<Str> describing a Class Name Suffix, or an C<Object> | ||
| 365 | that C<does> | ||
| 366 | L<< | ||
| 367 | C<Git::PurePerl::B<Walker::Role::Method>>|Git::PurePerl::Walker::Role::Method | ||
| 368 | >>. | ||
| 369 | |||
| 370 | If its a C<Str>, the C<Str> will be expanded as follows: | ||
| 371 | |||
| 372 | ->new( | ||
| 373 | ... | ||
| 374 | method => 'Foo', | ||
| 375 | ... | ||
| 376 | ); | ||
| 377 | |||
| 378 | $className = 'Git::PurePerl::Walker::Method::Foo' | ||
| 379 | |||
| 380 | And the resulting class will be loaded, and instantiated for you. ( Assuming of | ||
| 381 | course, you don't need to pass any fancy args ). | ||
| 382 | |||
| 383 | If you need fancy args, or a class outside the | ||
| 384 | C<Git::PurePerl::B<Walker::Method::>> namespace, constructing the object will | ||
| 385 | have to be your responsibility. | ||
| 386 | |||
| 387 | ->new( | ||
| 388 | ... | ||
| 389 | method => Foo::Class->new(), | ||
| 390 | ... | ||
| 391 | ) | ||
| 392 | |||
| 393 | =head2 on_commit | ||
| 394 | |||
| 395 | B<Mandatory:> either a C<Str> that can be expanded in a way similar to that by | ||
| 396 | L<< C<I<method>>|/method >>, a C<CodeRef>, or an object that C<does> L<< | ||
| 397 | C<Git::PurePerl::B<Walker::Role::OnCommit>>|Git::PurePerl::Walker::Role::OnCommit | ||
| 398 | >>. | ||
| 399 | |||
| 400 | If passed a C<Str> it will be expanded like so: | ||
| 401 | |||
| 402 | ->new( | ||
| 403 | ... | ||
| 404 | on_commit => $str, | ||
| 405 | ... | ||
| 406 | ); | ||
| 407 | |||
| 408 | $class = 'Git::PurePerl::Walker::OnCommit::' . $str; | ||
| 409 | |||
| 410 | And the resulting class loaded and instantiated. | ||
| 411 | |||
| 412 | If passed a C<CodeRef>, | ||
| 413 | L<< | ||
| 414 | C<Git::PurePerl::B<Walker::OnCommit::CallBack>>|Git::PurePerl::Walker::OnCommit::CallBack | ||
| 415 | >> will be loaded and your C<CodeRef> will be passed as an argument. | ||
| 416 | |||
| 417 | ->new( | ||
| 418 | ... | ||
| 419 | on_commit => sub { | ||
| 420 | my ( $commit ) = @_; | ||
| 421 | |||
| 422 | }, | ||
| 423 | ... | ||
| 424 | ); | ||
| 425 | |||
| 426 | If you need anything fancier, or requiring an unusual namespace, you'll want to | ||
| 427 | construct the object yourself. | ||
| 428 | |||
| 429 | ->new( | ||
| 430 | ... | ||
| 431 | on_commit => Foo::Package->new() | ||
| 432 | ... | ||
| 433 | ); | ||
| 434 | |||
| 435 | =head1 METHODS | ||
| 436 | |||
| 437 | =head2 reset | ||
| 438 | |||
| 439 | $walker->reset(); | ||
| 440 | |||
| 441 | Reset the walk routine back to the state it was before you walked. | ||
| 442 | |||
| 443 | =head2 step | ||
| 444 | |||
| 445 | Increments one step forward in the git history, and dispatches the object to the | ||
| 446 | C<OnCommit> handlers. | ||
| 447 | |||
| 448 | If there are more possible steps to take, it will return a true value. | ||
| 449 | |||
| 450 | while ( $walker->step ) { | ||
| 451 | /* Code to execute if walker has more items */ | ||
| 452 | } | ||
| 453 | |||
| 454 | This code is almost identical to: | ||
| 455 | |||
| 456 | while(1) { | ||
| 457 | $walker->on_commit->handle( $walker->method->current ); | ||
| 458 | |||
| 459 | last if not $walker->method->has_next; | ||
| 460 | |||
| 461 | $walker->method->next; | ||
| 462 | |||
| 463 | /* Code to execute if walker has more items */ | ||
| 464 | } | ||
| 465 | |||
| 466 | =head2 step_all | ||
| 467 | |||
| 468 | my $steps = $walker->step_all; | ||
| 469 | |||
| 470 | Mostly a convenience method to iterate until it can iterate no more, but without | ||
| 471 | you needing to wrap it in a while() block. | ||
| 472 | |||
| 473 | Returns the number of steps executed. | ||
| 474 | |||
| 475 | =head1 ATTRIBUTES | ||
| 476 | |||
| 477 | =head2 repo | ||
| 478 | |||
| 479 | =head2 method | ||
| 480 | |||
| 481 | =head2 on_commit | ||
| 482 | |||
| 483 | =head1 ATTRIBUTE GENERATED METHODS | ||
| 484 | |||
| 485 | =head2 repo | ||
| 486 | |||
| 487 | # Getter | ||
| 488 | my $repo = $walker->repo(); | ||
| 489 | |||
| 490 | =head2 method | ||
| 491 | |||
| 492 | # Getter | ||
| 493 | my $method_object = $walker->method(); | ||
| 494 | |||
| 495 | =head2 on_commit | ||
| 496 | |||
| 497 | # Getter | ||
| 498 | my $on_commit_object = $walker->on_commit(); | ||
| 499 | |||
| 500 | =head1 PRIVATE ATTRIBUTES | ||
| 501 | |||
| 502 | =head2 _method | ||
| 503 | |||
| 504 | =head2 _on_commit | ||
| 505 | |||
| 506 | =head1 PRIVATE METHODS | ||
| 507 | |||
| 508 | =head2 _build_repo | ||
| 509 | |||
| 510 | =head2 _build_method | ||
| 511 | |||
| 512 | =head2 _build_on_commit | ||
| 513 | |||
| 514 | =head1 PRIVATE ATTRIBUTE GENERATED METHODS | ||
| 515 | |||
| 516 | =head2 _method | ||
| 517 | |||
| 518 | # Getter | ||
| 519 | my $methodish = $walker->_method(); | ||
| 520 | |||
| 521 | =head2 _on_commit | ||
| 522 | |||
| 523 | # Getter | ||
| 524 | my $on_commitish => $walker->_on_commit(); | ||
| 525 | |||
| 526 | =for Pod::Coverage BUILD | ||
| 527 | |||
| 528 | =head1 AUTHOR | ||
| 529 | |||
| 530 | Kent Fredric <kentnl@cpan.org> | ||
| 531 | |||
| 532 | =head1 COPYRIGHT AND LICENSE | ||
| 533 | |||
| 534 | This software is copyright (c) 2014 by Kent Fredric <kentnl@cpan.org>. | ||
| 535 | |||
| 536 | This is free software; you can redistribute it and/or modify it under | ||
| 537 | the same terms as the Perl 5 programming language system itself. | ||
| 538 | |||
| 539 | =cut |