blib/lib/App/GitGerrit.pm

filter: Statementstmt 10.8% 63 / 579 Branchbran 0.3% 1 / 296 Conditioncond 0.0% 0 / 64 MC/DCmcdc 0.0% 0 / 35 Subroutinesub 19.6% 12 / 61 Podpod 0.0% 0 / 29 totaltotal 7.1% 76 / 1064 CC 195 SCAR 51.1
CC
195
Cov
7%
__ANON__
756.0
auto_reviewers
420.0
get_credentials
306.0
CRAP 166.1
↑ Summary
1 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use locale ':not_characters';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 # The following incantation is here to avoid this bug:
8 # https://rt.perl.org/rt3/Public/Bug/Display.html?id=63402
9 1 my $encoding;
10 1 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=10.1
11 1 ▶ if ($^O eq 'MSWin32') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq 'MSWin32') { } 0 1 1
12 0 require Win32;
13 0 my $cp = Win32::GetConsoleCP();
14 0 $encoding = ":encoding(cp$cp)";
15 } else {
16 1 $encoding = ':locale';
17 }
18 }
19 1 ▶use open ':std', $encoding;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 package App::GitGerrit;
22 {
23 1 $App::GitGerrit::VERSION = '0.022';
24 }
25 # ABSTRACT: A container for functions for the git-gerrit program
26  
27 1 ▶use Pod::Usage;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use Getopt::Long qw(:config auto_version auto_help);
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 # App::GitGerrit was converted from a script into a module following this:
33 # http://elliotlovesperl.com/2009/11/23/how-to-structure-perl-programs/
34 1 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
35 1 our @EXPORT_OK = qw/run/;
36  
37 # The $Command variable holds the name of the git-gerrit sub-command
38 # that's been invoked. It's defined in the 'run' routine below.
39  
40 1 our $Command;
41  
42 # The %Options hash is used to hold the command line options passed to
43 # all git-gerrit subcommands. The --debug option is common to all of
44 # them. Each subcommand supports a specific set of options which are
45 # grokked by the get_options routine below.
46  
47 1 my %Options = ( debug => 0, help => 0 );
48  
49 0 ▶sub debug {
Subroutine debug: not called CC=2 SCAR=17.9
Pod undocumented
50 0 my ($msg) = @_;
51 0 ▶ warn 'git-gerrit[DEBUG]: ', $msg, "\n" if $Options{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $Options{"debug"} 0 0 0
52 }
53  
54 0 ▶sub info {
Subroutine info: not called CC=1 SCAR=6.9
Pod undocumented
55 0 my ($msg) = @_;
56 0 warn 'git-gerrit[INFO]: ', $msg, "\n";
57 }
58  
59 0 ▶sub error {
Subroutine error: not called CC=1 SCAR=6.9
Pod undocumented
60 0 my ($msg) = @_;
61 0 die 'git-gerrit[ERROR]: ', $msg, "\n";
62 }
63  
64 0 ▶sub syntax_error {
Subroutine syntax_error: not called CC=1 SCAR=6.9
Pod undocumented
65 0 my ($msg) = @_;
66 0 pod2usage "git-gerrit[SYNTAX]: $msg\n";
67 }
68  
69 0 ▶sub get_options {
Subroutine get_options: not called CC=5 SCAR=34.0
Pod undocumented
70 0 my (@opt_specs) = @_;
71  
72 # Get defaults from configuration
73 0 foreach my $cmd ($Command, 'all') {
74 0 ▶ if (my $options = config("options.$cmd")) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $options = config("options.$cmd")) 0 0 0
75 0 debug "$cmd: unshift default options: $options";
76 0 unshift @ARGV, split(' ', $options);
77 }
78 }
79  
80 0 ▶ GetOptions(\%Options, 'debug', 'help', @opt_specs) or pod2usage(2);
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions(\%Options, "debug", "help", @opt_specs) or pod2usage(2) 0 0 0
81 0 ▶ pod2usage({-exitval => 1, -verbose => 2}) if $Options{help};
Branch: 1 branch
BranchTrueFalseTotal
if $Options{"help"} 0 0 0
82 }
83  
84 # The cmd routine is used to invoke shell commands, usually git. It
85 # prints out the command before invoking it in debug operation.
86  
87 0 ▶sub cmd {
Subroutine cmd: not called CC=1 SCAR=6.9
Pod undocumented
88 0 my ($cmd) = @_;
89 0 debug $cmd;
90 0 return system($cmd) == 0;
91 }
92  
93 # The grok_config routine returns a hash-ref mapping every Git
94 # configuration variable under the 'git-gerrit' section to its list of
95 # values.
96  
97 0 ▶sub grok_config {
Subroutine grok_config: not called CC=9 SCAR=45.0
Pod undocumented
98 0 state $config;
99  
100 0 ▶ unless ($config) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($config) 0 0 0
101 0 debug "git config --list";
102 {
103 0 open my $pipe, '-|', 'git config --list';
104 0 ▶ while (<$pipe>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $pipe)) 0 0 0
105 0 ▶ if (/^(.+?)\.(\S+)=(.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^(.+?)\.(\S+)=(.*)/u) { } 0 0 0
106 0 push @{$config->{$1}{$2}}, $3;
107 } else {
108 0 info "Strange git-config output: $_";
109 }
110 }
111 }
112  
113 # Now we must assume some configuration by default
114  
115 0 ▶ $config->{'git-gerrit'}{remote} //= ['origin'];
Condition0%0/2
$config->{"git-gerrit"}{"remote"} //= ["origin"]
l!l
00
MC/DC0%0/1
$config->{"git-gerrit"}{"remote"} //= ["origin"]
$config->{"git-gerrit"}{"remote"}
Truth table0%0/2
A
A= $config->{"git-gerrit"}{"remote"}
Aresult
11
00
116  
117 0 ▶ my $remote_url = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
118 0 state $url;
119 0 ▶ unless ($url) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($url) 0 0 0
120 0 my $remote = $config->{'git-gerrit'}{remote}[-1];
121 0 ▶ $url = $config->{remote}{"$remote.url"}[-1]
Branch: 1 branch
BranchTrueFalseTotal
$url = $config->{"remote"}{"$remote.url"}[-1] or error("The remote '${remote}' isn't configured because there's no remote.$remote.url configuration") 0 0 0
122 or error "The remote '$remote' isn't configured because there's no remote.$remote.url configuration";
123 0 $url = URI->new($url);
124 }
125 0 return $url;
126 0 };
127  
128 0 ▶ unless ($config->{'git-gerrit'}{baseurl}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($config->{"git-gerrit"}{"baseurl"}) 0 0 0
129 0 my $url = $remote_url->();
130 0 $config->{'git-gerrit'}{baseurl} = [$url->scheme . '://' . $url->authority];
131 }
132 0 $config->{'git-gerrit'}{baseurl}[-1] =~ s:/+$::; # strip trailing slashes
133  
134 0 ▶ unless ($config->{'git-gerrit'}{project}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($config->{"git-gerrit"}{"project"}) 0 0 0
135 0 my $prefix = URI->new($config->{'git-gerrit'}{baseurl}[-1])->path;
136 0 my $path = $remote_url->()->path;
137 0 ▶ if (length $prefix) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $prefix) { } 0 0 0
138 0 ▶ $prefix eq substr($path, 0, length($prefix))
Branch: 1 branch
BranchTrueFalseTotal
$prefix eq substr($path, 0, length $prefix) or error("I can't grok git-gerrit.project because git-gerrit.baseurl's path\ndoesn't match git-gerrit.remote's path:\n\n* baseurl: \n") 0 0 0
139 or error <<EOF;
140 I can't grok git-gerrit.project because git-gerrit.baseurl's path
141 doesn't match git-gerrit.remote's path:
142  
143 * baseurl:
144 EOF
145 0 $config->{'git-gerrit'}{project} = [substr($path, length($prefix))];
146 } else {
147 0 $config->{'git-gerrit'}{project} = [$path];
148 }
149 }
150 0 $config->{'git-gerrit'}{project}[-1] =~ s:^/+::; # strip leading slashes
151 }
152  
153 0 return $config;
154 }
155  
156 # The config routine returns the last value associated with Git's
157 # git-gerrit.$var configuration variable, as output by the 'git config
158 # -l' command, or undef if the variable isn't defined.
159  
160 0 ▶sub config {
Subroutine config: not called CC=2 SCAR=17.9
Pod undocumented
161 0 my ($var) = @_;
162 0 state $config = grok_config;
163 0 ▶ return exists $config->{'git-gerrit'}{$var} ? $config->{'git-gerrit'}{$var}[-1] : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $config->{'git-gerrit'}{$var} ? : 0 0 0
164 }
165  
166 # The configs routine returns all values associated with Git's
167 # git-gerrit.$var configuration variable or the empty list if the
168 # variable isn't defined.
169  
170 0 ▶sub configs {
Subroutine configs: not called CC=2 SCAR=17.9
Pod undocumented
171 0 my ($var) = @_;
172 0 state $config = grok_config;
173 0 ▶ return exists $config->{'git-gerrit'}{$var} ? @{$config->{'git-gerrit'}{$var}} : ();
Branch: 1 branch
BranchTrueFalseTotal
exists $config->{'git-gerrit'}{$var} ? : 0 0 0
174 }
175  
176 # The install_commit_msg_hook routine is invoked by a few of
177 # git-gerrit subcommands. It checks if the current repository already
178 # has a commit-msg hook installed. If not, it tries to download and
179 # install Gerrit's default commit-msg hook, which inserts Change-Ids
180 # in commits messages.
181  
182 0 ▶sub install_commit_msg_hook {
Subroutine install_commit_msg_hook: not called CC=5 SCAR=34.0
Pod undocumented
183 0 require File::Spec;
184  
185 0 chomp(my $git_dir = qx/git rev-parse --git-dir/);
186  
187 # Do nothing if it already exists
188 0 my $commit_msg = File::Spec->catfile($git_dir, 'hooks', 'commit-msg');
189 0 ▶ return if -e $commit_msg;
Branch: 1 branch
BranchTrueFalseTotal
if -e $commit_msg 0 0 0
190  
191 # Otherwise, check if we need to mkdir the hooks directory
192 0 my $hooks_dir = File::Spec->catdir($git_dir, 'hooks');
193 0 ▶ mkdir $hooks_dir unless -e $hooks_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $hooks_dir 0 0 0
194  
195 # Try to download and install the hook.
196 0 eval { require LWP::Simple };
197 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
198 0 info "Cannot install $commit_msg hook because you don't have LWP::Simple installed";
199 } else {
200 0 info "Installing $commit_msg hook";
201 0 ▶ if (LWP::Simple::is_success(LWP::Simple::getstore(config('baseurl') . "/tools/hooks/commit-msg", $commit_msg))) {
Branch: 1 branch
BranchTrueFalseTotal
if (LWP::Simple::is_success(LWP::Simple::getstore(config("baseurl") . '/tools/hooks/commit-msg', $commit_msg))) 0 0 0
202 0 chmod 0755, $commit_msg;
203 }
204 }
205 }
206  
207 # The credential_* routines below use the git-credential command to
208 # get and set credentials for git commands and also for Gerrit REST
209 # interactions.
210  
211 0 ▶sub url_userinfo {
Subroutine url_userinfo: not called CC=2 SCAR=17.9
Pod undocumented
212 0 my ($url) = @_;
213 0 ▶ if (my $userinfo = $url->userinfo) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $userinfo = $url->userinfo) { } 0 0 0
214 0 return split /:/, $userinfo, 2;
215 } else {
216 0 return (undef, undef);
217 }
218 }
219  
220 0 ▶sub credential_description_file {
Subroutine credential_description_file: not called CC=4 SCAR=30.0
Pod undocumented
221 0 my ($baseurl, $password) = @_;
222  
223 0 my %credential = (
224 protocol => $baseurl->scheme,
225 host => $baseurl->host,
226 path => $baseurl->path,
227 password => $password,
228 );
229  
230 # Try to get the username from the baseurl
231 0 my ($username) = url_userinfo($baseurl);
232 0 ▶ $credential{username} = $username if $username;
Branch: 1 branch
BranchTrueFalseTotal
if $username 0 0 0
233  
234 0 require File::Temp;
235 0 my $fh = File::Temp->new();
236  
237 0 ▶ while (my ($key, $value) = each %credential) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %credential) 0 0 0
238 0 ▶ $fh->print("$key=$value\n") if $value;
Branch: 1 branch
BranchTrueFalseTotal
if $value 0 0 0
239 }
240  
241 0 $fh->print("\n\n");
242 0 $fh->close();
243  
244 0 return ($fh, $fh->filename);
245 }
246  
247 1 my $git_credential_supported = 1;
248 0 ▶sub get_credentials {
Subroutine get_credentials: not called CC=17 SCAR=57.2
Pod undocumented
249 0 my $baseurl = URI->new(config('baseurl'));
250 0 my ($fh, $credfile) = credential_description_file($baseurl);
251  
252 0 my %credentials;
253 0 debug "Get credentials from git-credential";
254 0 ▶ open my $pipe, '-|', "git credential fill <$credfile"
Branch: 1 branch
BranchTrueFalseTotal
open my $pipe, "-|", "git credential fill <$credfile" or error("Can't open pipe to git-credential: $!") 0 0 0
255 or error "Can't open pipe to git-credential: $!";
256 0 ▶ while (<$pipe>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $pipe)) 0 0 0
257 0 chomp;
258 0 ▶ $credentials{$1} = $2 if /^([^=]+)=(.*)/;
Branch: 1 branch
BranchTrueFalseTotal
if /^([^=]+)=(.*)/u 0 0 0
259 }
260 0 ▶ unless (close $pipe) {
Branch: 1 branch
BranchTrueFalseTotal
unless (close $pipe) 0 0 0
261 0 ▶ error "Can't close pipe to git-credential: $!" if $!;
Branch: 1 branch
BranchTrueFalseTotal
if $! 0 0 0
262  
263 # If we get here it is because the shell invoked by open
264 # above couldn't exec git-credential, which most probably
265 # means that we're using a pre-1.8 Git, which doesn't
266 # support git-credential yet.
267 0 $git_credential_supported = 0;
268 }
269  
270 0 my ($username, $password) = @credentials{qw/username password/};
271  
272 0 ▶ unless (defined $username && defined $password) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $username and defined $password) 0 0 0
Condition0%0/3
defined $username and defined $password
!ll&&!rl&&r
000
MC/DC0%0/2
defined $username and defined $password
defined $username defined $password
Truth table0%0/3
A and B
A= defined $usernameB= defined $password
ABresult
0X0
100
111
273 0 debug "Get credentials from git-gerrit.baseurl";
274 0 ($username, $password) = url_userinfo(config('baseurl'));
275 }
276  
277 0 ▶ unless (defined $username && defined $password) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $username and defined $password) 0 0 0
Condition0%0/3
defined $username and defined $password
!ll&&!rl&&r
000
MC/DC0%0/2
defined $username and defined $password
defined $username defined $password
Truth table0%0/3
A and B
A= defined $usernameB= defined $password
ABresult
0X0
100
111
278 0 debug "Get credentials from a .netrc file";
279 0 ▶ if (eval {require Net::Netrc}) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { require Net::Netrc } }) { } 0 0 0
280 0 ▶ if (my $mach = Net::Netrc->lookup(URI->new(config('baseurl'))->host, $username)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $mach = "Net::Netrc"->lookup("URI"->new(config("baseurl"))->host, $username)) 0 0 0
281 0 ($username, $password) = ($mach->login, $mach->password);
282 }
283 } else {
284 0 debug "Failed to require Net::Netrc";
285 }
286 }
287  
288 0 ▶ unless (defined $username && defined $password) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $username and defined $password) 0 0 0
Condition0%0/3
defined $username and defined $password
!ll&&!rl&&r
000
MC/DC0%0/2
defined $username and defined $password
defined $username defined $password
Truth table0%0/3
A and B
A= defined $usernameB= defined $password
ABresult
0X0
100
111
289 0 debug "Prompt the user for the credentials";
290 0 ▶ if (eval {require Term::Prompt}) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { require Term::Prompt } }) { } 0 0 0
291 0 $username = Term::Prompt::prompt('x', 'Gerrit username: ', '', $ENV{USER});
292 0 $password = Term::Prompt::prompt('p', 'Gerrit password: ', '');
293 0 print "\n";
294 } else {
295 0 debug "Failed to require Term::Prompt";
296 }
297 }
298  
299 0 ▶ defined $username or error "Couldn't get credential's username";
Branch: 1 branch
BranchTrueFalseTotal
defined $username or error("Couldn't get credential's username") 0 0 0
300 0 ▶ defined $password or error "Couldn't get credential's password";
Branch: 1 branch
BranchTrueFalseTotal
defined $password or error("Couldn't get credential's password") 0 0 0
301  
302 0 return ($username, $password);
303 }
304  
305 0 ▶sub set_credentials {
Subroutine set_credentials: not called CC=3 SCAR=24.8
Pod undocumented
306 0 my ($username, $password, $what) = @_;
307  
308 0 ▶ return 1 unless $git_credential_supported;
Branch: 1 branch
BranchTrueFalseTotal
unless $git_credential_supported 0 0 0
309  
310 0 ▶ $what =~ /^(?:approve|reject)$/
Branch: 1 branch
BranchTrueFalseTotal
$what =~ /^(?:approve|reject)$/u or error("set_credentials \$what argument ($what) must be either 'approve' or 'reject'") 0 0 0
311 or error "set_credentials \$what argument ($what) must be either 'approve' or 'reject'";
312  
313 0 my $baseurl = URI->new(config('baseurl'));
314 0 my ($fh, $credfile) = credential_description_file($baseurl, $password);
315  
316 0 return system("git credential $what <$credfile") == 0;
317 }
318  
319 # The get_message routine returns the message argument to the
320 # --message option. If the option is not present it invokes the git
321 # editor to let the user compose a message and returns it.
322  
323 0 ▶sub get_message {
Subroutine get_message: not called CC=6 SCAR=37.4
Pod undocumented
324 0 ▶ return $Options{message} if exists $Options{message};
Branch: 1 branch
BranchTrueFalseTotal
if exists $Options{"message"} 0 0 0
325  
326 0 chomp(my $editor = qx/git var GIT_EDITOR/);
327  
328 0 ▶ error "Please, read 'git help var' to know how to set up an editor for git messages."
Branch: 1 branch
BranchTrueFalseTotal
unless $editor 0 0 0
329 unless $editor;
330  
331 0 require File::Temp;
332 0 my $tmp = File::Temp->new();
333 0 my $filename = $tmp->filename;
334  
335 {
336 0 ▶ open my $fh, '>', $filename
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $filename or error("Can't open file for writing ($filename): $!\n") 0 0 0
337 or error "Can't open file for writing ($filename): $!\n";
338 0 print $fh <<'EOF';
339  
340 # Please enter the review message for this change. Lines starting
341 # with '#' will be ignored, and an empty message aborts the review.
342 EOF
343 0 close $fh;
344 }
345  
346 0 ▶ cmd "$editor $filename"
Branch: 1 branch
BranchTrueFalseTotal
cmd("$editor $filename") or error("Aborting because I couldn't invoke '$editor ${filename}'.") 0 0 0
347 or error "Aborting because I couldn't invoke '$editor $filename'.";
348  
349 0 my $message;
350 {
351 0 ▶ open my $fh, '<', $filename
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $filename or error("Can't open file for reading ($filename): $!\n") 0 0 0
352 or error "Can't open file for reading ($filename): $!\n";
353 0 local $/ = undef; # slurp mode
354 0 $message = <$fh>;
355 0 close $fh;
356 }
357 0 $message =~ s/(?<=\n)#.*?\n//gs; # remove all lines starting with '#'
358 0 return $message;
359 }
360  
361 # The gerrit routine keeps a cached Gerrit::REST object to which it
362 # relays REST calls.
363  
364 0 ▶sub gerrit {
Subroutine gerrit: not called CC=6 SCAR=37.4
Pod undocumented
365 0 my $method = shift;
366  
367 0 state $gerrit;
368 0 ▶ unless ($gerrit) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($gerrit) 0 0 0
369 0 my ($username, $password) = get_credentials;
370 0 require Gerrit::REST;
371 0 $gerrit = Gerrit::REST->new(config('baseurl'), $username, $password);
372 0 eval { $gerrit->GET("/projects/" . uri_escape_utf8(config('project'))) };
373 0 ▶ if (my $error = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) { } 0 0 0
374 0 ▶ set_credentials($username, $password, 'reject') if $error->{code} == 401;
Branch: 1 branch
BranchTrueFalseTotal
if $error->{"code"} == 401 0 0 0
375 0 die $error;
376 } else {
377 0 set_credentials($username, $password, 'approve');
378 }
379 }
380  
381 0 ▶ if ($Options{debug}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Options{"debug"}) 0 0 0
382 0 my ($endpoint, @args) = @_;
383 0 debug "GERRIT->$method($endpoint)";
384 0 ▶ if (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 0 0 0
385 0 require Data::Dumper;
386 0 warn Data::Dumper::Dumper(@args);
387 }
388 }
389  
390 0 return $gerrit->$method(@_);
391 }
392  
393 # The gerrit_or_die routine relays its arguments to the gerrit routine
394 # but catches any exception and dies with a formatted message. It
395 # should be called instead of gerrit whenever the caller doesn't want
396 # to treat exceptions.
397  
398 0 ▶sub gerrit_or_die {
Subroutine gerrit_or_die: not called CC=2 SCAR=17.9
Pod undocumented
399 0 my $result = eval { gerrit(@_) };
400 0 ▶ die $@->as_text if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
401 0 return $result;
402 }
403  
404 # The normalize_date routine removes the trailing zeroes from a $date.
405  
406 0 ▶sub normalize_date {
Subroutine normalize_date: not called CC=1 SCAR=6.9
Pod undocumented
407 0 my ($date) = @_;
408 0 $date =~ s/\.0+$//;
409 0 return $date;
410 }
411  
412 # The query_changes routine receives a list of strings to query the
413 # Gerrit server. It returns an array-ref containing a list of
414 # array-refs, each containing a list of change descriptions.
415  
416 0 ▶sub query_changes {
Subroutine query_changes: not called CC=5 SCAR=34.0
Pod undocumented
417 0 my @queries = @_;
418  
419 0 ▶ return [] unless @queries;
Branch: 1 branch
BranchTrueFalseTotal
unless @queries 0 0 0
420  
421 # If we're inside a git repository, restrict the query to the
422 # current project's reviews.
423 0 ▶ if (my $project = config('project')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $project = config("project")) 0 0 0
424 0 $project = uri_escape_utf8($project);
425 0 @queries = map "q=project:$project+$_", @queries;
426 }
427  
428 0 ▶ push @queries, "n=$Options{limit}" if $Options{limit};
Branch: 1 branch
BranchTrueFalseTotal
if $Options{"limit"} 0 0 0
429  
430 0 push @queries, "o=LABELS";
431  
432 0 my $changes = gerrit_or_die(GET => "/changes/?" . join('&', @queries));
433 0 ▶ $changes = [$changes] if ref $changes->[0] eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $changes->[0] eq "HASH" 0 0 0
434  
435 0 return $changes;
436 }
437  
438 # The get_change routine returns the description of a change
439 # identified by $id. An optional boolean second argument ($allrevs)
440 # tells if the change description should contain a description of all
441 # patchsets or just the current one.
442  
443 0 ▶sub get_change {
Subroutine get_change: not called CC=2 SCAR=17.9
Pod undocumented
444 0 my ($id, $allrevs) = @_;
445  
446 0 ▶ my $revs = $allrevs ? 'ALL_REVISIONS' : 'CURRENT_REVISION';
Branch: 1 branch
BranchTrueFalseTotal
$allrevs ? : 0 0 0
447 0 return (gerrit_or_die(GET => "/changes/?q=change:$id&o=$revs"))[0][0];
448 }
449  
450 # The current_branch routine returns the name of the current branch or
451 # 'HEAD' in a dettached head state.
452  
453 0 ▶sub current_branch {
Subroutine current_branch: not called CC=1 SCAR=6.9
Pod undocumented
454 0 chomp(my $branch = qx/git rev-parse --abbrev-ref HEAD/);
455 0 return $branch;
456 }
457  
458 # The update_branch routine receives a local $branch name and updates
459 # it with the homonym branch in the Gerrit remote.
460  
461 0 ▶sub update_branch {
Subroutine update_branch: not called CC=1 SCAR=6.9
Pod undocumented
462 0 my ($branch) = @_;
463  
464 0 my $remote = config('remote');
465 0 cmd "git fetch $remote $branch:$branch";
466 }
467  
468 # The change_branch_info routine receives the name of a branch. If
469 # it's a change-branch, it returns a two-element list containing it's
470 # upstream name and its id. Otherwise, it returns the empty list.
471  
472 0 ▶sub change_branch_info {
Subroutine change_branch_info: not called CC=2 SCAR=17.9
Pod undocumented
473 0 my ($branch) = @_;
474 0 ▶ if ($branch =~ m:^change/(?<upstream>.*)/(?<id>[^/]+):) {
Branch: 1 branch
BranchTrueFalseTotal
if ($branch =~ m[^change/(?<upstream>.*)/(?<id>[^/]+)]u) 0 0 0
475 0 return ($+{upstream}, $+{id});
476 }
477 0 return;
478 }
479  
480 # The current_change_id routine returns the id of the change branch
481 # we're currently in. If we're not in a change branch, it returns
482 # undef.
483  
484 0 ▶sub current_change_id {
Subroutine current_change_id: not called CC=1 SCAR=6.9
Pod undocumented
485 0 my ($branch, $id) = change_branch_info(current_branch);
486  
487 0 return $id;
488 }
489  
490 # This routine receives the hash-ref mapped to the 'Code-Review' label
491 # in a change's 'labels' key when it's fetched with the option
492 # LABELS. For more information, please read:
493 # https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#label-info
494  
495 0 ▶sub code_review {
Subroutine code_review: not called CC=6 SCAR=37.4
Pod undocumented
496 0 my ($cr) = @_;
497 0 ▶ if (! defined $cr) {
Branch: 5 branches
BranchTrueFalseTotal
if (not defined $cr) { } 0 0 0
elsif (exists $cr->{'rejected'}) { } 0 0 0
elsif (exists $cr->{'disliked'}) { } 0 0 0
elsif (exists $cr->{'approved'}) { } 0 0 0
elsif (exists $cr->{'recommended'}) { } 0 0 0
498 0 return '';
499 } elsif (exists $cr->{rejected}) {
500 0 return '-2';
501 } elsif (exists $cr->{disliked}) {
502 0 return '-1';
503 } elsif (exists $cr->{approved}) {
504 0 return '+2';
505 } elsif (exists $cr->{recommended}) {
506 0 return '+1';
507 } else {
508 0 return '';
509 }
510 }
511  
512 # This routine receives a branch name (normally the upstream of a
513 # change-branch) and returns a list of users matching the
514 # git-gerrit.reviewers specifications. The list returned is guaranteed
515 # to have no duplicates.
516  
517 0 ▶sub auto_reviewers {
Subroutine auto_reviewers: not called CC=20 SCAR=60.4
Pod undocumented
518 0 my ($upstream) = @_;
519 0 my $paths;
520  
521 my @reviewers;
522  
523 REVIEWERS:
524 0 foreach my $spec (configs('reviewers')) {
525 0 ▶ if (my ($users, @conditions) = split ' ', $spec) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = split(' ', $spec, 0)) 0 0 0
526 0 ▶ if (@conditions) {
Branch: 1 branch
BranchTrueFalseTotal
if (@conditions) 0 0 0
527 CONDITION:
528 0 foreach my $condition (@conditions) {
529 0 ▶ if (my ($what, $op, $match) = ($condition =~ /^(branch|path)([=~])(.+)$/i)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = $condition =~ /^(branch|path)([=~])(.+)$/iu) { } 0 0 0
530 0 ▶ if ($what eq 'branch') {
Branch: 1 branch
BranchTrueFalseTotal
if ($what eq 'branch') { } 0 0 0
531 0 ▶ if ($op eq '=') {
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq '=') { } 0 0 0
532 0 ▶ next CONDITION if $upstream eq $match;
Branch: 1 branch
BranchTrueFalseTotal
if $upstream eq $match 0 0 0
533 } else {
534 0 my $regex = eval { qr/$match/ };
535 0 ▶ defined $regex
Branch: 1 branch
BranchTrueFalseTotal
defined $regex or info("Warning: skipping git-gerrit.reviewers spec with invalid REGEXP ($match).") and next REVIEWERS 0 0 0
Condition0%0/5
defined $regex || info("Warning: skipping git-gerrit.reviewers spec with invalid REGEXP ($match).") && (next REVIEWERS)
l!l&&r!l&&!r
000
info("Warning: skipping git-gerrit.reviewers spec with invalid REGEXP ($match).") and next REVIEWERS
!ll
00
MC/DC0%0/2
defined $regex || info("Warning: skipping git-gerrit.reviewers spec with invalid REGEXP ($match).") && (next REVIEWERS)
defined $regex info("Warning: skipping git-gerrit.reviewers spec with invalid REGEXP ($match).")
Truth table0%0/3
A || B
A= defined $regexB= info("Warning: skipping git-gerrit.reviewers spec with invalid REGEXP ($match).")
ABresult
1X1
011
000
536 or info "Warning: skipping git-gerrit.reviewers spec with invalid REGEXP ($match)."
537 and next REVIEWERS;
538 0 ▶ next CONDITION if $upstream =~ $match;
Branch: 1 branch
BranchTrueFalseTotal
if $upstream =~ /$match/u 0 0 0
539 }
540 } else {
541 0 ▶ unless ($paths) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($paths) 0 0 0
542 0 $paths = [qx/git diff --name-only ${upstream}..HEAD/];
543 0 chomp @$paths;
544 }
545 0 ▶ if ($op eq '=') {
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq '=') { } 0 0 0
546 0 foreach my $path (@$paths) {
547 0 ▶ next CONDITION if $path eq $match;
Branch: 1 branch
BranchTrueFalseTotal
if $path eq $match 0 0 0
548 }
549 } else {
550 0 my $regex = eval { qr/$match/ };
551 0 ▶ defined $regex
Branch: 1 branch
BranchTrueFalseTotal
defined $regex or info("Warning: skipping git-gerrit.reviewers spec with invalid REGEXP ($match).") and next REVIEWERS 0 0 0
Condition0%0/5
defined $regex || info("Warning: skipping git-gerrit.reviewers spec with invalid REGEXP ($match).") && (next REVIEWERS)
l!l&&r!l&&!r
000
info("Warning: skipping git-gerrit.reviewers spec with invalid REGEXP ($match).") and next REVIEWERS
!ll
00
MC/DC0%0/2
defined $regex || info("Warning: skipping git-gerrit.reviewers spec with invalid REGEXP ($match).") && (next REVIEWERS)
defined $regex info("Warning: skipping git-gerrit.reviewers spec with invalid REGEXP ($match).")
Truth table0%0/3
A || B
A= defined $regexB= info("Warning: skipping git-gerrit.reviewers spec with invalid REGEXP ($match).")
ABresult
1X1
011
000
552 or info "Warning: skipping git-gerrit.reviewers spec with invalid REGEXP ($match)."
553 and next REVIEWERS;
554 0 foreach my $path (@$paths) {
555 0 ▶ next CONDITION if $path =~ $regex;
Branch: 1 branch
BranchTrueFalseTotal
if $path =~ /$regex/u 0 0 0
556 }
557 }
558 }
559 } else {
560 0 info "Warning: skipping git-gerrit.reviewers spec with invalid condition ($condition).";
561 }
562 0 next REVIEWERS;
563 }
564 }
565 0 push @reviewers, split(/,/, $users);
566 }
567 }
568  
569 # Use a hash to remove duplicates
570 0 my %reviewers = map {$_ => undef} @reviewers;
571 0 return keys %reviewers;
572 }
573  
574 # This routine is used by all sub-commands that accept zero or more
575 # change ids. If @ARGV is empty it pushes into it the id of the change
576 # associated with the current change-branch, if any. It returns a
577 # boolean telling if the push has been made.
578  
579 0 ▶sub grok_unspecified_change {
Subroutine grok_unspecified_change: not called CC=4 SCAR=30.0
Pod undocumented
580 0 ▶ if (@ARGV) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ARGV) { } 0 0 0
581 0 return 0;
582 } else {
583 0 ▶ my $id = current_change_id()
Branch: 1 branch
BranchTrueFalseTotal
my $id = current_change_id() or syntax_error("${Command}: You have to be in a change-branch or specify at least one CHANGE.") 0 0 0
584 or syntax_error "$Command: You have to be in a change-branch or specify at least one CHANGE.";
585 0 ▶ $id =~ /^\d+$/
Branch: 1 branch
BranchTrueFalseTotal
$id =~ /^\d+$/u or error("${Command}: The change-branch you're in haven't been pushed yet.") 0 0 0
586 or error "$Command: The change-branch you're in haven't been pushed yet.";
587 0 @ARGV = ($id);
588 0 return 1;
589 }
590 }
591  
592 # This routine is used by the sub-commands that, when applied
593 # successfully to the current change-branch, want to checkout its
594 # upstream and remove the change-branch.
595  
596 0 ▶sub checkout_upstream_and_delete_branch {
Subroutine checkout_upstream_and_delete_branch: not called CC=2 SCAR=17.9
Pod undocumented
597 0 my $branch = current_branch;
598 0 my ($upstream) = change_branch_info($branch);
599 0 ▶ cmd "git checkout $upstream" and cmd "git branch -D $branch";
Branch: 1 branch
BranchTrueFalseTotal
cmd("git checkout $upstream") and cmd("git branch -D $branch") 0 0 0
600 }
601  
602 ############################################################
603 # MAIN
604  
605 # Each git-gerrit subcommand is implemented by an anonymous routine
606 # associated with one or more names in the %Commands hash.
607  
608 1 my %Commands;
609  
610 0 ▶$Commands{new} = sub {
Subroutine __ANON__: not called CC=9 SCAR=45.0
611 0 get_options('update');
612  
613 0 ▶ my $topic = shift @ARGV
Branch: 1 branch
BranchTrueFalseTotal
my $topic = shift @ARGV or syntax_error("${Command}: Missing TOPIC.") 0 0 0
614 or syntax_error "$Command: Missing TOPIC.";
615  
616 0 ▶ $topic !~ m:/:
Branch: 1 branch
BranchTrueFalseTotal
$topic =~ m[/]u and error("${Command}: the topic name ($topic) should not contain slashes.") 0 0 0
617 or error "$Command: the topic name ($topic) should not contain slashes.";
618  
619 0 ▶ $topic =~ m:\D:
Branch: 1 branch
BranchTrueFalseTotal
$topic =~ /\D/u or error("${Command}: the topic name ($topic) should contain at least one non-digit character.") 0 0 0
620 or error "$Command: the topic name ($topic) should contain at least one non-digit character.";
621  
622 0 ▶ my $branch = shift @ARGV || current_branch;
Condition0%0/3
shift @ARGV || current_branch()
l!l&&r!l&&!r
000
MC/DC0%0/2
shift @ARGV || current_branch()
shift @ARGV current_branch()
Truth table0%0/3
A || B
A= shift @ARGVB= current_branch()
ABresult
1X1
011
000
623  
624 0 ▶ if (my ($upstream, $id) = change_branch_info($branch)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = change_branch_info($branch)) 0 0 0
625 # If we're on a change-branch the new change-branch is based on the same upstream
626 0 $branch = $upstream;
627 }
628  
629 0 my $status = qx/git status --porcelain --untracked-files=no/;
630  
631 0 ▶ info "Warning: git-status tells me that your working area is dirty:\n$status\n"
Branch: 1 branch
BranchTrueFalseTotal
if length $status 0 0 0
632 if length $status;
633  
634 0 ▶ if ($Options{update}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Options{"update"}) 0 0 0
635 0 ▶ update_branch($branch)
Branch: 1 branch
BranchTrueFalseTotal
update_branch($branch) or error("${Command}: Non-fast-forward pull. Please, merge or rebase your branch first.") 0 0 0
636 or error "$Command: Non-fast-forward pull. Please, merge or rebase your branch first.";
637 }
638  
639 0 cmd "git checkout -b change/$branch/$topic $branch";
640  
641 0 install_commit_msg_hook;
642  
643 0 return;
644 1 };
645  
646 0 ▶$Commands{push} = sub {
Subroutine __ANON__: not called CC=27 SCAR=66.3
647 0 $Options{rebase} = ''; # false by default
648 0 get_options(
649 'keep',
650 'force+',
651 'rebase!',
652 'draft',
653 'topic=s',
654 'submit',
655 'base=s',
656 'reviewer=s@',
657 'cc=s@'
658 );
659  
660 0 my $branch = current_branch;
661  
662 0 ▶ my ($upstream, $id) = change_branch_info($branch)
Branch: 1 branch
BranchTrueFalseTotal
() = change_branch_info($branch) or error("${Command}: You aren't in a change branch. I cannot push it.") 0 0 0
663 or error "$Command: You aren't in a change branch. I cannot push it.";
664  
665 0 my $is_clean = qx/git status --porcelain --untracked-files=no/ eq '';
666  
667 0 ▶ $is_clean or $Options{force}--
Branch: 1 branch
BranchTrueFalseTotal
$is_clean or $Options{"force"}-- or error("push: Can't push change because git-status is dirty.\n If this is really what you want to do, please try again with --force.\n") 0 0 0
Condition0%0/3
$is_clean or $Options{"force"}--
l!l&&r!l&&!r
000
MC/DC0%0/2
$is_clean or $Options{"force"}--
$is_clean $Options{"force"}--
Truth table0%0/3
A or B
A= $is_cleanB= $Options{"force"}--
ABresult
1X1
011
000
668 or error <<EOF;
669 push: Can't push change because git-status is dirty.
670 If this is really what you want to do, please try again with --force.
671 EOF
672  
673 0 my @commits = qx/git log --decorate=no --first-parent --oneline ${upstream}..HEAD/;
674 0 ▶ if (@commits == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (@commits == 0) { } 0 0 0
elsif (@commits > 1) { } 0 0 0
675 0 error "$Command: no changes between $upstream and $branch. Pushing would be pointless.";
676 } elsif (@commits > 1) {
677 0 ▶ error <<EOF unless $Options{force}--;
Branch: 1 branch
BranchTrueFalseTotal
unless $Options{"force"}-- 0 0 0
678 push: you have more than one commit that you are about to push.
679 The outstanding commits are:
680  
681 @commits
682 If this is really what you want to do, please try again with --force.
683 EOF
684 }
685  
686 # Grok the list of parent commits to see if it's a merge commit.
687 0 my @parents = split / /, qx/git log --pretty='format:%p' -1/;
688  
689 # A --noverbose option sets $Options{rebase} to '0'.
690 0 ▶ if ($is_clean && (@parents < 2) && ($Options{rebase} || $Options{rebase} eq '' && $id =~ /\D/)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_clean and @parents < 2 and $Options{"rebase"} || $Options{"rebase"} eq "" && $id =~ /\D/u) 0 0 0
Condition0%0/12
$is_clean and @parents < 2 and $Options{"rebase"} || $Options{"rebase"} eq "" && $id =~ /\D/u
!ll&&!rl&&r
000
$is_clean and @parents < 2
!ll&&!rl&&r
000
$Options{"rebase"} or $Options{"rebase"} eq "" and $id =~ /\D/u
l!l&&r!l&&!r
000
$Options{"rebase"} eq "" and $id =~ /\D/u
!ll&&!rl&&r
000
MC/DC0%0/5
$is_clean and @parents < 2 and $Options{"rebase"} || $Options{"rebase"} eq "" && $id =~ /\D/u
$is_clean @parents < 2 $Options{"rebase"} $Options{"rebase"} eq "" $id =~ /\D/u
Truth table0%0/6
A and B and C or D and E
A= $is_cleanB= @parents < 2C= $Options{"rebase"}D= $Options{"rebase"} eq ""E= $id =~ /\D/u
ABCDEresult
0XXXX0
10XXX0
1100X0
110100
111XX1
110111
691 0 ▶ update_branch($upstream)
Branch: 1 branch
BranchTrueFalseTotal
update_branch($upstream) or error("${Command}: Non-fast-forward pull. Please, merge or rebase your branch first.") 0 0 0
692 or error "$Command: Non-fast-forward pull. Please, merge or rebase your branch first.";
693 0 ▶ cmd "git rebase $upstream"
Branch: 1 branch
BranchTrueFalseTotal
cmd("git rebase $upstream") or error("${Command}: please resolve this 'git rebase ${upstream}' and try again.") 0 0 0
694 or error "$Command: please resolve this 'git rebase $upstream' and try again.";
695 }
696  
697 0 ▶ my $refspec = 'HEAD:refs/' . ($Options{draft} ? 'draft' : 'for') . "/$upstream";
Branch: 1 branch
BranchTrueFalseTotal
$Options{'draft'} ? : 0 0 0
698  
699 0 my @tags;
700 0 ▶ if (my $topic = $Options{topic}) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $topic = $Options{'topic'}) { } 0 0 0
elsif ($id =~ /\D/u) { } 0 0 0
701 0 push @tags, "topic=$topic";
702 } elsif ($id =~ /\D/) {
703 0 push @tags, "topic=$id";
704 }
705  
706 0 my @reviewers = auto_reviewers($upstream);
707 0 ▶ if (my $reviewers = $Options{reviewer}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $reviewers = $Options{"reviewer"}) 0 0 0
708 0 push @reviewers, split(/,/, join(',', @$reviewers));
709 }
710 0 ▶ if (@reviewers) {
Branch: 1 branch
BranchTrueFalseTotal
if (@reviewers) 0 0 0
711 0 push @tags, map("r=$_", @reviewers);
712 }
713  
714 0 ▶ if (my $ccs = $Options{cc}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ccs = $Options{"cc"}) 0 0 0
715 0 push @tags, map("cc=$_", split(/,/, join(',', @$ccs)));
716 }
717 0 ▶ if ($Options{submit}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Options{"submit"}) 0 0 0
718 0 push @tags, 'submit';
719 }
720 0 ▶ if (my $base = $Options{base}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $base = $Options{"base"}) 0 0 0
721 0 push @tags, "base=$base";
722 }
723 0 ▶ if (@tags) {
Branch: 1 branch
BranchTrueFalseTotal
if (@tags) 0 0 0
724 0 $refspec .= '%';
725 0 $refspec .= join(',', @tags);
726 }
727  
728 0 my $remote = config('remote');
729 0 ▶ cmd "git push $remote $refspec"
Branch: 1 branch
BranchTrueFalseTotal
cmd("git push $remote $refspec") or error("${Command}: Error pushing change.") 0 0 0
730 or error "$Command: Error pushing change.";
731  
732 0 ▶ if ($is_clean && ! $Options{keep}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_clean and not $Options{"keep"}) 0 0 0
Condition0%0/3
$is_clean and not $Options{"keep"}
!ll&&!rl&&r
000
MC/DC0%0/2
$is_clean and not $Options{"keep"}
$is_clean not $Options{"keep"}
Truth table0%0/3
A and not(B)
A= $is_cleanB= not $Options{"keep"}
ABresult
0X0
100
111
733 0 ▶ cmd "git checkout $upstream" and cmd "git branch -D $branch";
Branch: 1 branch
BranchTrueFalseTotal
cmd("git checkout $upstream") and cmd("git branch -D $branch") 0 0 0
734 }
735  
736 0 install_commit_msg_hook;
737  
738 0 return;
739 1 };
740  
741 0 ▶$Commands{query} = sub {
Subroutine __ANON__: not called CC=8 SCAR=42.8
742 0 get_options(
743 'verbose',
744 'limit=i',
745 );
746  
747 0 my (@names, @queries);
748 0 foreach my $arg (@ARGV) {
749 0 ▶ if ($arg =~ /(?<name>.*?)=(?<query>.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg =~ /(?<name>.*?)=(?<query>.*)/u) { } 0 0 0
750 0 push @names, $+{name};
751 0 push @queries, $+{query};
752 } else {
753 0 push @names, "QUERY";
754 0 push @queries, $arg;
755 }
756 }
757  
758 0 my $changes = query_changes(@queries);
759  
760 0 ▶ for (my $i=0; $i < @$changes; ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$changes) 0 0 0
761 0 print "[$names[$i]=$queries[$i]]\n";
762 0 ▶ next unless @{$changes->[$i]};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$changes->[$i];} 0 0 0
763  
764 0 require Text::Table;
765 0 my $table = Text::Table->new("ID\n&num", qw/STATUS CR UPDATED PROJECT BRANCH OWNER SUBJECT/);
766  
767 0 foreach my $change (sort {$b->{updated} cmp $a->{updated}} @{$changes->[$i]}) {
768 0 ▶ if ($Options{verbose}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Options{"verbose"}) 0 0 0
769 0 ▶ if (my $topic = gerrit_or_die(GET => "/changes/$change->{id}/topic")) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $topic = gerrit_or_die("GET", qq[/changes/$change->{"id"}/topic])) 0 0 0
770 0 $change->{branch} .= " ($topic)";
771 }
772 }
773 $table->add(
774 $change->{_number},
775 $change->{status},
776 code_review($change->{labels}{'Code-Review'}),
777 normalize_date($change->{updated}),
778 $change->{project},
779 $change->{branch},
780 $change->{owner}{name},
781 $change->{subject},
782 0 );
783 }
784 0 print $table->table(), "\n";
785 }
786  
787 0 return;
788 1 };
789  
790 1 my %StandardQueries = (
791 changes => [
792 'Outgoing reviews=is:open+owner:self',
793 'Incoming reviews=is:open+reviewer:self+-owner:self',
794 'Recently closed=is:closed+owner:self+-age:1mon',
795 ],
796 drafts => ['Drafts=is:draft'],
797 watched => ['Watched changes=is:watched+status:open'],
798 starred => ['Starred changes=is:starred'],
799 );
800 0 ▶$Commands{my} = sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
801 0 ▶ if (@ARGV) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ARGV) { } 0 0 0
802 0 ▶ if (exists $StandardQueries{$ARGV[-1]}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $StandardQueries{$ARGV[-1]}) { } 0 0 0
elsif ($ARGV[-1] =~ /^-/u) { } 0 0 0
803 0 splice @ARGV, -1, 1, @{$StandardQueries{$ARGV[-1]}};
804 } elsif ($ARGV[-1] =~ /^-/) {
805 # By default we show 'My Changes'
806 0 push @ARGV, @{$StandardQueries{changes}};
807 } else {
808 0 syntax_error "$Command: Invalid change specification: '$ARGV[-1]'";
809 }
810 } else {
811 # By default we show 'My Changes'
812 0 push @ARGV, @{$StandardQueries{changes}};
813 }
814  
815 0 $Commands{query}();
816  
817 0 return;
818 1 };
819  
820 0 ▶$Commands{show} = sub {
Subroutine __ANON__: not called CC=11 SCAR=48.8
821 0 get_options();
822  
823 0 grok_unspecified_change;
824  
825 0 foreach my $id (@ARGV) {
826 0 my $change = gerrit_or_die(GET => "/changes/$id/detail");
827  
828 0 print <<EOF;
829 Change-Num: $change->{_number}
830 Change-Id: $change->{change_id}
831 Subject: $change->{subject}
832 Owner: $change->{owner}{name}
833 EOF
834  
835 0 foreach my $date (qw/created updated/) {
836 $change->{$date} = normalize_date($change->{$date})
837 0 ▶ if exists $change->{$date};
Branch: 1 branch
BranchTrueFalseTotal
if exists $change->{$date} 0 0 0
838 }
839  
840 0 foreach my $key (qw/project branch topic created updated status reviewed mergeable/) {
841 printf "%12s %s\n", "\u$key:", $change->{$key}
842 0 ▶ if exists $change->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $change->{$key} 0 0 0
843 }
844  
845 0 print "\n";
846 # We want to produce a table in which the first column lists the
847 # reviewer names and the other columns have their votes for each
848 # label. However, the change object has this information
849 # inverted. So, we have to first collect all votes.
850 0 my @labels = sort keys %{$change->{labels}};
851 0 my %reviewers;
852 0 ▶ while (my ($label, $info) = each %{$change->{labels}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$change{"labels"};}) 0 0 0
853 0 foreach my $vote (@{$info->{all}}) {
854 0 $reviewers{$vote->{name}}{$label} = $vote->{value};
855 }
856 }
857  
858 # And now we can output the vote table
859 0 require Text::Table;
860 0 my $table = Text::Table->new('REVIEWER', map {"$_\n&num"} @labels);
861  
862 0 foreach my $name (sort keys %reviewers) {
863 0 ▶ my @votes = map {$_ > 0 ? "+$_" : $_} map {defined $_ ? $_ : '0'} @{$reviewers{$name}}{@labels};
Branch: 2 branches
BranchTrueFalseTotal
$_ > 0 ? : 0 0 0
defined $_ ? : 0 0 0
864 0 $table->add($name, @votes);
865 }
866 0 print $table->table(), '-' x 60, "\n";
867 }
868  
869 0 return;
870 1 };
871  
872 0 ▶$Commands{fetch} = sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
873 0 get_options();
874  
875 0 grok_unspecified_change;
876  
877 0 my $branch;
878 0 my $project = config('project');
879 0 my @change_branches;
880 0 foreach my $id (@ARGV) {
881 0 my $change = get_change($id);
882  
883 0 ▶ $change->{project} eq $project
Branch: 1 branch
BranchTrueFalseTotal
$change->{"project"} eq $project or error(qq[${Command}: Change $id belongs to a different project ($change->{"project"}), not $project]) 0 0 0
884 or error "$Command: Change $id belongs to a different project ($change->{project}), not $project";
885  
886 0 my ($revision) = values %{$change->{revisions}};
887  
888 0 my ($url, $ref) = @{$revision->{fetch}{http}}{qw/url ref/};
889  
890 0 $branch = "change/$change->{branch}/$change->{_number}";
891  
892 0 ▶ cmd "git fetch $url $ref:$branch"
Branch: 1 branch
BranchTrueFalseTotal
cmd("git fetch $url ${ref}:$branch") or error("${Command}: Can't fetch $url") 0 0 0
893 or error "$Command: Can't fetch $url";
894  
895 0 push @change_branches, $branch;
896 }
897  
898 0 return @change_branches;
899 1 };
900  
901 0 ▶$Commands{checkout} = $Commands{co} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
902 0 my $last_change_branch = do {
903 0 local $Command = 'fetch';
904 0 my @change_branches = $Commands{fetch}->();
905 0 $change_branches[-1];
906 };
907  
908 0 cmd "git checkout $last_change_branch";
909  
910 0 return;
911 1 };
912  
913 0 ▶$Commands{upstream} = $Commands{up} = sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
914 0 get_options(
915 'keep',
916 'delete',
917 );
918  
919 0 my $branch = current_branch;
920  
921 0 ▶ if (my ($upstream, $id) = change_branch_info($branch)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = change_branch_info($branch)) { } 0 0 0
922 0 ▶ if (cmd "git checkout $upstream") {
Branch: 1 branch
BranchTrueFalseTotal
if (cmd("git checkout $upstream")) 0 0 0
923 0 ▶ if ($Options{keep} || ! $Options{delete} && $id =~ /\D/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Options{'keep'} or not $Options{'delete'} and $id =~ /\D/u) { } 0 0 0
Condition0%0/6
$Options{"keep"} or not $Options{"delete"} and $id =~ /\D/u
l!l&&r!l&&!r
000
not $Options{"delete"} and $id =~ /\D/u
!ll&&!rl&&r
000
MC/DC0%0/3
$Options{"keep"} or not $Options{"delete"} and $id =~ /\D/u
$Options{"keep"} not $Options{"delete"} $id =~ /\D/u
Truth table0%0/4
A or not(B) and C
A= $Options{"keep"}B= not $Options{"delete"}C= $id =~ /\D/u
ABCresult
1XX1
0111
00X0
0100
924 0 info "Keeping $branch";
925 } else {
926 0 cmd "git branch -D $branch";
927 }
928 }
929 } else {
930 0 error "$Command: You aren't in a change branch. There is no upstream to go to.";
931 }
932  
933 0 return;
934 1 };
935  
936 0 ▶$Commands{'cherry-pick'} = $Commands{cp} = sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
937 0 get_options(
938 'edit',
939 'no-commit',
940 );
941  
942 0 my @args;
943 0 ▶ push @args, '--edit' if $Options{edit};
Branch: 1 branch
BranchTrueFalseTotal
if $Options{"edit"} 0 0 0
944 0 ▶ push @args, '--no-commit' if $Options{'no-commit'};
Branch: 1 branch
BranchTrueFalseTotal
if $Options{"no-commit"} 0 0 0
945  
946 0 ▶ @ARGV or syntax_error "$Command: Missing CHANGE.";
Branch: 1 branch
BranchTrueFalseTotal
@ARGV or syntax_error("${Command}: Missing CHANGE.") 0 0 0
947  
948 0 my @change_branches = do {
949 0 local $Command = 'fetch';
950 0 $Commands{fetch}->();
951 };
952  
953 0 cmd join(' ', 'git cherry-pick', @args, @change_branches);
954  
955 0 return;
956 1 };
957  
958 0 ▶$Commands{rebase} = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
959 0 get_options();
960  
961 0 ▶ my ($upstream, $id) = change_branch_info(current_branch)
Branch: 1 branch
BranchTrueFalseTotal
() = change_branch_info(current_branch()) or error("${Command}: You must be in a change branch to invoke rebase.") 0 0 0
962 or error "$Command: You must be in a change branch to invoke rebase.";
963  
964 0 ▶ cmd "git rebase $upstream"
Branch: 1 branch
BranchTrueFalseTotal
cmd("git rebase $upstream") or error("${Command}: please resolve this 'git rebase ${upstream}' and try again.") 0 0 0
965 or error "$Command: please resolve this 'git rebase $upstream' and try again.";
966 1 };
967  
968 0 ▶$Commands{reviewer} = sub {
Subroutine __ANON__: not called CC=8 SCAR=42.8
969 0 get_options(
970 'add=s@',
971 'confirm',
972 'delete=s@',
973 );
974  
975 0 grok_unspecified_change;
976  
977 0 foreach my $id (@ARGV) {
978 # First try to make all deletions
979 0 ▶ if (my $users = $Options{delete}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $users = $Options{"delete"}) 0 0 0
980 0 foreach my $user (split(/,/, join(',', @$users))) {
981 0 $user = uri_escape_utf8($user);
982 0 gerrit_or_die(DELETE => "/changes/$id/reviewers/$user");
983 }
984 }
985  
986 # Second try to make all additions
987 0 ▶ if (my $users = $Options{add}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $users = $Options{"add"}) 0 0 0
988 0 ▶ my $confirm = $Options{confirm} ? 'true' : 'false';
Branch: 1 branch
BranchTrueFalseTotal
$Options{'confirm'} ? : 0 0 0
989 0 foreach my $user (split(/,/, join(',', @$users))) {
990 0 gerrit_or_die(POST => "/changes/$id/reviewers", { reviewer => $user, confirm => $confirm});
991 }
992 }
993  
994 # Finally, list current reviewers
995 0 my $reviewers = gerrit_or_die(GET => "/changes/$id/reviewers");
996  
997 0 print "[$id]\n";
998 0 require Text::Table;
999 0 my %labels = map {$_ => undef} map {keys %{$_->{approvals}}} @$reviewers;
1000 0 my @labels = sort keys %labels;
1001 0 my $table = Text::Table->new('REVIEWER', map {"$_\n&num"} @labels);
1002 0 $table->add($_->{name}, @{$_->{approvals}}{@labels})
1003 0 foreach sort {$a->{name} cmp $b->{name}} @$reviewers;
1004 0 print $table->table(), '-' x 60, "\n";
1005 }
1006  
1007 0 return;
1008 1 };
1009  
1010 0 ▶$Commands{review} = sub {
Subroutine __ANON__: not called CC=11 SCAR=48.8
1011 0 get_options(
1012 'message=s',
1013 'keep',
1014 );
1015  
1016 0 my %review;
1017  
1018 0 ▶ if (my $message = get_message) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $message = get_message()) 0 0 0
1019 0 $review{message} = $message;
1020 }
1021  
1022 # Set all votes
1023 0 ▶ while (@ARGV && $ARGV[0] =~ /(?<label>.*)=(?<vote>.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ARGV and $ARGV[0] =~ /(?<label>.*)=(?<vote>.*)/u) 0 0 0
Condition0%0/3
@ARGV and $ARGV[0] =~ /(?<label>.*)=(?<vote>.*)/u
!ll&&!rl&&r
000
MC/DC0%0/2
@ARGV and $ARGV[0] =~ /(?<label>.*)=(?<vote>.*)/u
@ARGV $ARGV[0] =~ /(?<label>.*)=(?<vote>.*)/u
Truth table0%0/3
A and B
A= @ARGVB= $ARGV[0] =~ /(?<label>.*)=(?<vote>.*)/u
ABresult
0X0
100
111
1024 0 shift @ARGV;
1025 0 ▶ $review{labels}{$+{label} || 'Code-Review'} = $+{vote};
Condition0%0/2
$+{'label'} || 'Code-Review'
l!l
00
MC/DC0%0/1
$+{'label'} || 'Code-Review'
$+{'label'}
Truth table0%0/2
A
A= $+{'label'}
Aresult
11
00
1026 0 ▶ $+{vote} =~ /^[+-]?\d$/
Branch: 1 branch
BranchTrueFalseTotal
$+{"vote"} =~ /^[+-]?\d$/u or syntax_error(qq[${Command}: Invalid vote ($+{"vote"}). It must be a single digit optionally prefixed by a [-+] sign.]) 0 0 0
1027 or syntax_error "$Command: Invalid vote ($+{vote}). It must be a single digit optionally prefixed by a [-+] sign.";
1028 }
1029  
1030 0 ▶ error "$Command: Invalid vote $ARGV[0]." if @ARGV > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @ARGV > 1 0 0 0
1031  
1032 0 ▶ error "$Command: You must specify a message or a vote to review."
Branch: 1 branch
BranchTrueFalseTotal
unless keys %review 0 0 0
1033 unless keys %review;
1034  
1035 0 my $local_change = grok_unspecified_change;
1036  
1037 0 foreach my $id (@ARGV) {
1038 0 gerrit_or_die(POST => "/changes/$id/revisions/current/review", \%review);
1039 }
1040  
1041 checkout_upstream_and_delete_branch
1042 0 ▶ if $local_change && ! $Options{keep};
Branch: 1 branch
BranchTrueFalseTotal
if $local_change and not $Options{"keep"} 0 0 0
Condition0%0/3
$local_change and not $Options{"keep"}
!ll&&!rl&&r
000
MC/DC0%0/2
$local_change and not $Options{"keep"}
$local_change not $Options{"keep"}
Truth table0%0/3
A and not(B)
A= $local_changeB= not $Options{"keep"}
ABresult
0X0
100
111
1043  
1044 0 return;
1045 1 };
1046  
1047 0 ▶$Commands{abandon} = sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
1048 0 get_options(
1049 'message=s',
1050 'keep',
1051 );
1052  
1053 0 my @args;
1054  
1055 0 ▶ if (my $message = get_message) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $message = get_message()) 0 0 0
1056 0 push @args, { message => $message };
1057 }
1058  
1059 0 my $local_change = grok_unspecified_change;
1060  
1061 0 foreach my $id (@ARGV) {
1062 0 gerrit_or_die(POST => "/changes/$id/abandon", @args);
1063 }
1064  
1065 checkout_upstream_and_delete_branch
1066 0 ▶ if $local_change && ! $Options{keep};
Branch: 1 branch
BranchTrueFalseTotal
if $local_change and not $Options{"keep"} 0 0 0
Condition0%0/3
$local_change and not $Options{"keep"}
!ll&&!rl&&r
000
MC/DC0%0/2
$local_change and not $Options{"keep"}
$local_change not $Options{"keep"}
Truth table0%0/3
A and not(B)
A= $local_changeB= not $Options{"keep"}
ABresult
0X0
100
111
1067  
1068 0 return;
1069 1 };
1070  
1071 0 ▶$Commands{restore} = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
1072 0 get_options('message=s');
1073  
1074 0 my @args;
1075  
1076 0 ▶ if (my $message = get_message) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $message = get_message()) 0 0 0
1077 0 push @args, { message => $message };
1078 }
1079  
1080 0 grok_unspecified_change;
1081  
1082 0 foreach my $id (@ARGV) {
1083 0 gerrit_or_die(POST => "/changes/$id/restore", @args);
1084 }
1085  
1086 0 return;
1087 1 };
1088  
1089 0 ▶$Commands{revert} = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
1090 0 get_options('message=s');
1091  
1092 0 my @args;
1093  
1094 0 ▶ if (my $message = get_message) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $message = get_message()) 0 0 0
1095 0 push @args, { message => $message };
1096 }
1097  
1098 0 grok_unspecified_change;
1099  
1100 0 foreach my $id (@ARGV) {
1101 0 gerrit_or_die(POST => "/changes/$id/revert", @args);
1102 }
1103  
1104 0 return;
1105 1 };
1106  
1107 0 ▶$Commands{submit} = sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
1108 0 get_options(
1109 'no-wait-for-merge',
1110 'keep',
1111 );
1112  
1113 0 my @args;
1114 0 ▶ push @args, { wait_for_merge => 'true' } unless $Options{'no-wait-for-merge'};
Branch: 1 branch
BranchTrueFalseTotal
unless $Options{"no-wait-for-merge"} 0 0 0
1115  
1116 0 my $local_change = grok_unspecified_change;
1117  
1118 0 foreach my $id (@ARGV) {
1119 0 gerrit_or_die(POST => "/changes/$id/submit", @args);
1120 }
1121  
1122 checkout_upstream_and_delete_branch
1123 0 ▶ if $local_change && ! $Options{keep};
Branch: 1 branch
BranchTrueFalseTotal
if $local_change and not $Options{"keep"} 0 0 0
Condition0%0/3
$local_change and not $Options{"keep"}
!ll&&!rl&&r
000
MC/DC0%0/2
$local_change and not $Options{"keep"}
$local_change not $Options{"keep"}
Truth table0%0/3
A and not(B)
A= $local_changeB= not $Options{"keep"}
ABresult
0X0
100
111
1124  
1125 0 return;
1126 1 };
1127  
1128 0 ▶$Commands{web} = sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
1129 # The 'gerrit web' sub-command passes all of its options,
1130 # but --debug, to 'git web--browse'.
1131 0 Getopt::Long::Configure('pass_through');
1132 0 get_options();
1133  
1134 # If the user is passing any option we require that it mark where
1135 # they end with a '--' so that we know where the CHANGEs arguments
1136 # start.
1137 0 my @options;
1138 0 ▶ for (my $i = 0; $i < @ARGV; ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @ARGV) 0 0 0
1139 0 ▶ if ($ARGV[$i] eq '--') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ARGV[$i] eq "--") 0 0 0
1140 # We found a mark. Let's move all the options from @ARGV
1141 # to @options and get rid of the mark.
1142 0 @options = splice @ARGV, 0, $i;
1143 0 shift @ARGV;
1144 0 last;
1145 }
1146 }
1147  
1148 0 grok_unspecified_change;
1149  
1150 # Grok the URLs of each change
1151 0 my @urls;
1152 0 my $baseurl = config('baseurl');
1153 0 foreach my $id (@ARGV) {
1154 0 my $change = get_change($id);
1155 0 push @urls, "$baseurl/#/c/$change->{_number}";
1156 }
1157  
1158 0 cmd join(' ', qw/git web--browse/, @options, @urls);
1159 1 };
1160  
1161 0 ▶$Commands{config} = sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
1162 0 my $config = grok_config;
1163 0 ▶ my $git_gerrit = $config->{'git-gerrit'}
Branch: 1 branch
BranchTrueFalseTotal
my $git_gerrit = $config->{"git-gerrit"} or return 0 0 0
1164 or return;
1165 0 require Text::Table;
1166 0 my $table = Text::Table->new();
1167 0 foreach my $var (sort keys %$git_gerrit) {
1168 0 foreach my $value (@{$git_gerrit->{$var}}) {
1169 0 $table->add("git-gerrit.$var", $value);
1170 }
1171 }
1172 0 print $table->table(), "\n";
1173  
1174 0 return;
1175 1 };
1176  
1177 0 ▶$Commands{version} = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
1178 0 print "Perl version $^V\n";
1179 0 print "git-gerrit version $App::GitGerrit::VERSION\n";
1180 0 cmd "git version";
1181 0 my $baseurl = config('baseurl'); # die unless configured
1182 0 my $version = eval { gerrit(GET => '/config/server/version') };
1183 0 ▶ $version //= "pre-2.7 (Because it doesn't support the 'Get Version' REST Endpoint.)";
Condition0%0/2
$version //= "pre-2.7 (Because it doesn't support the 'Get Version' REST Endpoint.)"
l!l
00
MC/DC0%0/1
$version //= "pre-2.7 (Because it doesn't support the 'Get Version' REST Endpoint.)"
$version
Truth table0%0/2
A
A= $version
Aresult
11
00
1184 0 print "Gerrit version $version\n";
1185 0 return;
1186 1 };
1187  
1188 # MAIN
1189  
1190 0 ▶sub run {
Subroutine run: not called CC=3 SCAR=24.8
Pod undocumented
1191 0 ▶ $Command = shift @ARGV
Branch: 1 branch
BranchTrueFalseTotal
$Command = shift @ARGV or syntax_error("Missing command name.") 0 0 0
1192 or syntax_error "Missing command name.";
1193  
1194 0 ▶ exists $Commands{$Command}
Branch: 1 branch
BranchTrueFalseTotal
exists $Commands{$Command} or syntax_error("Invalid command: $Command.") 0 0 0
1195 or syntax_error "Invalid command: $Command.";
1196  
1197 0 $Commands{$Command}->();
1198  
1199 0 return 0;
1200 }
1201  
1202 1 1;
1203  
1204 __END__
↑ Summary