]> git.vpit.fr Git - perl/modules/rgit.git/blob - bin/rgit
Allow user-defined policies. Enable interactive failure handling when Term::ReadKey...
[perl/modules/rgit.git] / bin / rgit
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Carp qw/croak/;
7 use Cwd qw/cwd/;
8 use File::Spec::Functions qw/catfile path/;
9 use List::Util qw/first/;
10
11 use App::Rgit::Utils qw/:codes/;
12 use App::Rgit;
13
14 our $VERSION = '0.03';
15
16 BEGIN {
17  if (-t && eval { use Term::ReadKey; 1 }) {
18   *policy = sub {
19    my ($cmd, $conf, $repo, $status) = @_;
20    return NEXT unless $status;
21    print STDERR "git returned $status\n";
22    print STDERR "[a]bort, [i]gnore, [I]gnore all, [r]etry, open [s]hell ?";
23    ReadMode 4;
24    my $key = ReadKey 0;
25    ReadMode 1;
26    print STDERR "\n";
27    my %codes = (
28     'a' => LAST,
29     'i' => NEXT,
30     'I' => NEXT | SAVE,
31     'r' => REDO,
32     's' => LAST,
33    );
34    $key = 'a' unless defined $key;
35    my $code = $codes{$key};
36    $code = $codes{a} unless defined $code;
37    return $code;
38   };
39  } else {
40   *policy = sub {
41    my ($cmd, $conf, $repo, $status) = @_;
42    return NEXT unless $status;
43    print STDERR "git returned $status, aborting\n";
44    return LAST;
45   };
46  }
47 }
48
49 my $cmd = first { !/^-/ } @ARGV;
50 $cmd = ' ' unless defined $cmd;
51
52 my $git = $ENV{GIT_EXEC_PATH};
53 unless (defined $git) {
54  for (path) {
55   my $g = catfile $_, 'git';
56   if (-x $g) {
57    $git = $g;
58    last;
59   }
60  }
61 }
62 croak "Couldn't find any valid git executable" unless defined $git;
63
64 my $root = $ENV{GIT_DIR};
65 $root = cwd unless defined $root;
66
67 exit App::Rgit->new(
68  git    => $git,
69  root   => $root,
70  cmd    => $cmd,
71  args   => \@ARGV,
72  policy => \&policy,
73 )->run;
74
75 __END__
76
77 =head1 NAME
78
79 rgit - Recursively execute a command on all the git repositories in a directory tree.
80
81 =head1 VERSION
82
83 Version 0.03
84
85 =head1 SYNOPSIS
86
87     rgit [GIT_OPTIONS] COMMAND [COMMAND_ARGS]
88
89 =head1 DESCRIPTION
90
91 This utility recursively searches in the current directory (or in the directory given by the C<GIT_DIR> environment variable if it's set) for all git repositories, sort this list by the repository path, C<chdir> into each of them, and executes the specified git command.
92 Moreover, those formats are substuted in the arguments before running the command :
93
94 =over 4
95
96 =item *
97
98 C<^n> with the current repository name.
99
100 =item *
101
102 C<^g> with the relative path to the current repository.
103
104 =item *
105
106 C<^G> with the absolute path to the current repository.
107
108 =item *
109
110 C<^w> with the relative path to the current repository's working directory.
111
112 =item *
113
114 C<^W> with the absolute path to the current repository's working directory.
115
116 =item *
117
118 C<^b> with a "bareified" relative path, i.e. C<^g> if this is a bare repository, and C<^w.git> otherwise.
119
120 =item *
121
122 C<^B> with an absolute version of the "bareified" path.
123
124 =item *
125
126 C<^R> with the absolute path to the current root directory.
127
128 =item *
129
130 C<^^> with a bare C<^>.
131
132 =back
133
134 There are actually a few commands that are only executed once in the current directory : C<daemon>, C<gui>, C<help>, C<init> and C<version>.
135 For any of those, no format substitution is done.
136
137 You can specify which C<git> executable to use with the C<GIT_EXEC_PATH> environment variable.
138
139 =head1 EXAMPLES
140
141 Execute C<git gc> on all the repositories below the current directory :
142
143     rgit gc
144
145 Tag all the repositories with their name :
146
147     rgit tag ^n
148
149 Add a remote to all repositories in "/foo/bar" to their bare counterpart in C<qux> on F<host> :
150
151     GIT_DIR="/foo/bar" rgit remote add host git://host/qux/^b
152
153 =head1 DEPENDENCIES
154
155 The core modules L<Carp>, L<Cwd>, L<Exporter>, L<File::Find>, L<File::Spec::Functions> and L<List::Util>.
156
157 L<Object::Tiny>.
158
159 =head1 AUTHOR
160
161 Vincent Pit, C<< <perl at profvince.com> >>, L<http://profvince.com>.
162    
163 You can contact me by mail or on C<irc.perl.org> (vincent).
164
165 =head1 BUGS
166
167 Please report any bugs or feature requests to C<bug-rgit at rt.cpan.org>, or through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=rgit>.  I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
168
169 =head1 SUPPORT
170
171 You can find documentation for this module with the perldoc command.
172
173     perldoc rgit
174
175 Tests code coverage report is available at L<http://www.profvince.com/perl/cover/rgit>.
176
177 =head1 COPYRIGHT & LICENSE
178
179 Copyright 2008 Vincent Pit, all rights reserved.
180
181 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
182
183 =cut