File Coverage

blib/lib/Group/Git/Cmd/Watch.pm
Criterion Covered Total %
statement 30 62 48.3
branch 0 20 0.0
condition 0 9 0.0
subroutine 10 12 83.3
pod 2 2 100.0
total 42 105 40.0


line stmt bran cond sub pod time code
1             package Group::Git::Cmd::Watch;
2              
3             # Created on: 2013-05-06 21:57:07
4             # Create by: Ivan Wills
5             # $Id$
6             # $Revision$, $HeadURL$, $Date$
7             # $Revision$, $Source$, $Date$
8              
9 1     1   751 use Moo::Role;
  1         4  
  1         14  
10 1     1   2870 use strict;
  1         3  
  1         19  
11 1     1   5 use warnings;
  1         2  
  1         30  
12 1     1   5 use version;
  1         3  
  1         8  
13 1     1   69 use Carp;
  1         3  
  1         78  
14 1     1   5 use English qw/ -no_match_vars /;
  1         2  
  1         10  
15 1     1   467 use File::chdir;
  1         13  
  1         85  
16 1     1   5 use Getopt::Alt;
  1         3  
  1         9  
17 1     1   3020 use YAML::Syck qw/LoadFile DumpFile/;
  1         2222  
  1         72  
18 1     1   8 use Types::Standard qw/Bool/;
  1         2  
  1         10  
19              
20             our $VERSION = version->new('0.7.6');
21              
22             requires 'repos';
23             requires 'verbose';
24              
25             my $config;
26             my $opt = Getopt::Alt->new(
27             {
28             help => __PACKAGE__,
29             default => {
30             config => 'group-git-watch.yml',
31             sleep => 60,
32             }
33             },
34             [
35             'show|w',
36             'once|o',
37             'sleep|s=i',
38             'save|v',
39             'all|a',
40             'config|c=s',
41             ]
42             );
43              
44             has watch_run => (
45             is => 'rw',
46             isa => Bool,
47             );
48              
49             sub watch_start {
50 0     0 1   $opt->process;
51 0 0 0       $config = $opt->opt->save && -f $opt->opt->config ? LoadFile($opt->opt->config) : {};
52              
53 0           return;
54             }
55              
56             sub watch {
57 0     0 1   my ($self, $name) = @_;
58 0 0         return unless -d $name;
59              
60 0 0         if ( !$self->runs ) {
61 0           $self->runs(2);
62              
63 0 0         if ($self->watch_run) {
64 0           system @ARGV;
65 0           $self->watch_run(undef);
66             }
67              
68 0 0         sleep $opt->opt->sleep if $self->runs == 1;
69             }
70              
71 0           my $repo = $self->repos->{$name};
72              
73 0           my $dump;
74             {
75 0           local $CWD = $name;
  0            
76 0           my ($id, $out);
77 0           `git fetch --quiet`;
78              
79 0 0         if ($opt->opt->all) {
80 0           ($out) = `git reflog --all`;
81 0           ($id) = $out =~ /^([0-9a-f]+)\s/;
82             }
83             else {
84 0           ($out) = `git show`;
85 0           ($id) = $out =~ /commit\s+([0-9a-f]+)/
86             }
87              
88 0 0 0       if (!$config->{$name} || $config->{$name} ne $id) {
89 0           $config->{$name} = $id;
90 0           $dump = 1;
91              
92 0 0         return "changed" if $opt->opt->show;
93              
94 0 0         if ($opt->opt->once) {
95 0           $self->watch_run(1);
96             }
97             else {
98 0           system @ARGV;
99             }
100             }
101             }
102              
103 0 0 0       if ($dump && $opt->opt->save) {
104 0           DumpFile($opt->opt->config, $config);
105             }
106              
107 0           return;
108             }
109              
110             1;
111              
112             __END__
113              
114             =head1 NAME
115              
116             Group::Git::Cmd::Watch - Watch for changes in repositories and run a command
117              
118             =head1 VERSION
119              
120             This documentation refers to Group::Git::Cmd::Watch version 0.7.6.
121              
122              
123             =head1 SYNOPSIS
124              
125             group-git watch [options] ([--show|-w]|command)
126              
127             Options:
128             -w --show Show when a repository has changed
129             -o --once Run the command only once for each itteration through all
130             repositories when one or more repositories change.
131             -s --sleep[=]seconds
132             Sleep for this number of seconds between each checking if
133             the repositories have changed (Default 60)
134             -v --save Store the state of each repository so if re-run the program
135             changes since the last run are shown.
136             -a --all Check all branches (not just the current branch) for changes
137             -c --config[=]file
138             Use inconjunction with --save to name the file to save to
139             (Default group-git-watch.yml)
140              
141             =head1 DESCRIPTION
142              
143             =head1 SUBROUTINES/METHODS
144              
145             =over 4
146              
147             =item C<watch ($name)>
148              
149             Runs git watch on each directory if the watch message includes:
150              
151             "nothing to commit"
152              
153             The watch is suppressed to keep the output clean. This can be overridden
154             if verbose is set.
155              
156             =item C<watch_start ()>
157              
158             Process the command line arguments for watch
159              
160             =back
161              
162             =head1 DIAGNOSTICS
163              
164             =head1 CONFIGURATION AND ENVIRONMENT
165              
166             =head1 DEPENDENCIES
167              
168             =head1 INCOMPATIBILITIES
169              
170             =head1 BUGS AND LIMITATIONS
171              
172             There are no known bugs in this module.
173              
174             Please report problems to Ivan Wills (ivan.wills@gmail.com).
175              
176             Patches are welcome.
177              
178             =head1 AUTHOR
179              
180             Ivan Wills - (ivan.wills@gmail.com)
181              
182             =head1 LICENSE AND COPYRIGHT
183              
184             Copyright (c) 2013 Ivan Wills (14 Mullion Close, Hornsby Heights, NSW Australia 2077).
185             All rights reserved.
186              
187             This module is free software; you can redistribute it and/or modify it under
188             the same terms as Perl itself. See L<perlartistic>. This program is
189             distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
190             without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
191             PARTICULAR PURPOSE.
192              
193             =cut