File Coverage

lib/App/TimeTracker/Command/Git.pm
Criterion Covered Total %
statement 17 30 56.6
branch 0 2 0.0
condition n/a
subroutine 6 8 75.0
pod 0 1 0.0
total 23 41 56.1


line stmt bran cond sub pod time code
1             package App::TimeTracker::Command::Git;
2 1     1   568 use strict;
  1         1  
  1         24  
3 1     1   4 use warnings;
  1         2  
  1         24  
4 1     1   15 use 5.010;
  1         3  
5              
6             our $VERSION = "3.000";
7             # ABSTRACT: App::TimeTracker Git plugin
8              
9 1     1   378 use Moose::Role;
  1         378443  
  1         3  
10 1     1   5155 use Git::Repository;
  1         22865  
  1         3  
11              
12             has 'branch' => (
13             is => 'rw',
14             isa => 'Str',
15             documentation => 'Git: Branch name',
16             );
17             has 'merge' => (
18             is => 'ro',
19             isa => 'Bool',
20             documentation => 'Git: Merge after stopping'
21             );
22             has 'no_branch' => (
23             is => 'ro',
24             isa => 'Bool',
25             documentation => 'Git: Do not create a branch',
26             traits => ['Getopt'],
27             cmd_aliases => [qw/nobranch/],
28             );
29             has 'repository' => (
30             is => 'ro',
31             isa => 'Maybe[Git::Repository]',
32             lazy_build => 1
33             );
34              
35             sub _build_repository {
36 0     0     my ($self) = @_;
37 0           my $r;
38 0           eval { $r = Git::Repository->new( work_tree => '.' ); };
  0            
39 0 0         return $r if $r;
40 0           say "Warning: No git repository found";
41 0           return;
42             }
43              
44             after 'cmd_start' => sub {
45             my $self = shift;
46              
47             return unless $self->repository;
48             return unless $self->branch;
49             return if $self->no_branch;
50              
51             my $r = $self->repository;
52             my $branch = $self->branch;
53             my %branches = map { s/^\s+//; $_ => 1 } $r->run('branch');
54              
55             if ( $branches{ '* ' . $branch } ) {
56             say "Already on branch $branch";
57             return;
58             }
59              
60             if ( !$branches{$branch} ) {
61             print $r->command( 'checkout', '-b', $branch )->stderr->getlines;
62             }
63             else {
64             print $r->command( 'checkout', $branch )->stderr->getlines;
65             }
66             };
67              
68             after 'cmd_continue' => sub {
69             my $self = shift;
70              
71             return unless $self->repository;
72             return unless $self->branch;
73             return if $self->no_branch;
74              
75             print $self->repository->command( 'checkout', $self->branch )
76             ->stderr->getlines;
77             };
78              
79             after cmd_stop => sub {
80             my $self = shift;
81             return unless $self->repository;
82             return unless $self->merge;
83              
84             my $r = $self->repository;
85             my $branch = $self->branch;
86             my %branches = map { s/^\s+//; $_ => 1 } $r->run('branch');
87              
88             unless ( $branches{ '* ' . $branch } ) {
89             say "Not in branch $branch, won't merge.";
90             return;
91             }
92             my $tags = join( ', ', map { $_->name } @{ $self->tags } ) || '';
93             $r->command( 'checkout', 'master' );
94             $r->command( "merge", $branch, "--no-ff", '-m',
95             "implemented $branch $tags" );
96             };
97              
98             sub safe_branch_name { # TODO
99 0     0 0   my ($self, $name ) = @_;
100 0           $name =~ s/\W/_/g;
101 0           $name =~ s/_+/_/g;
102 0           $name =~ s/^_//;
103 0           $name =~ s/_$//;
104 0           return $name
105             }
106              
107 1     1   551 no Moose::Role;
  1         2  
  1         10  
108             1;
109              
110             __END__
111              
112             =pod
113              
114             =encoding UTF-8
115              
116             =head1 NAME
117              
118             App::TimeTracker::Command::Git - App::TimeTracker Git plugin
119              
120             =head1 VERSION
121              
122             version 3.000
123              
124             =head1 DESCRIPTION
125              
126             This plugin makes it easier to set up and manage C<git> C<topic
127             branches>. When starting a new task, you can at the same time start a
128             new C<git branch>. Also, when stopping, C<tracker> can merge the
129             C<topic branch> back into C<master>.
130              
131             See http://nvie.com/posts/a-successful-git-branching-model/ for a good example on how to work with topic branches (and much more!)
132              
133             =head1 CONFIGURATION
134              
135             =head2 plugins
136              
137             Add C<Git> to the list of plugins.
138              
139             Of course this plugin will only work if the current project is in fact a git repo...
140              
141             =head1 NEW COMMANDS
142              
143             none
144              
145             =head1 CHANGES TO OTHER COMMANDS
146              
147             =head2 start, continue
148              
149             =head3 New Options
150              
151             =head4 --branch cool_new_feature
152              
153             ~/perl/Your-Project$ tracker start --branch cool_new_feature
154             Started working on Your-Project at 13:35:53
155             Switched to branch 'cool_new_feature'
156              
157             If you pass a branch name via C<--branch>, C<tracker> will create a
158             new branch (unless it already exists) and then switch into this
159             branch.
160              
161             If the branch already existed, it might be out of sync with master. In
162             this case you should do something like C<git merge master> before
163             starting to work.
164              
165             =head4 --nobranch (--no_branch)
166              
167             ~/perl/Your-Project$ tracker start --branch another_featur --no_branch
168              
169             Do not create a new branch, even if C<--branch> is set. This is only useful if another plugin (eg <RT>) automatically sets C<--branch>.
170              
171             =head2 stop
172              
173             =head3 New Options
174              
175             =head4 --merge
176              
177             ~/perl/Your-Project$ tracker stop --merge
178              
179             After, stopping, merge the current branch back into C<master> (using C<--no-ff>.
180              
181             TODO: Turn this into a string option, which should be the name of the
182             branch we want to merge into. Default to C<master> (or something set
183             in config..)
184              
185             =head1 AUTHOR
186              
187             Thomas Klausner <domm@cpan.org>
188              
189             =head1 COPYRIGHT AND LICENSE
190              
191             This software is copyright (c) 2014 - 2019 by Thomas Klausner.
192              
193             This is free software; you can redistribute it and/or modify it under
194             the same terms as the Perl 5 programming language system itself.
195              
196             =cut