]> Sergey Matveev's repositories - gerrvim.git/blob - gerrvim2json.pl
Ability to create review comments only with main message
[gerrvim.git] / gerrvim2json.pl
1 #!/usr/bin/env perl
2 # gerrvim -- Gerrit review's comments preparation helper
3 # Copyright (C) 2015 Sergey Matveev <stargrave@stargrave.org>
4 #
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18 use strict;
19 use warnings;
20
21 use Encode;
22 use JSON;
23
24 my %comments;
25 my $blockid;
26 my $filename = undef;
27 my $linebgn = undef;
28 my $lineend = undef;
29 my $blockn = 0;
30 my $main_message = undef;
31 my $verbatim_block = 0;
32 my @buf;
33
34 sub buf2str {
35     my $r = join "\n", @buf;
36     chomp $r;
37     $r =~ s/\n+$//g;
38     @buf = ();
39     return decode_utf8 $r;
40 };
41
42 sub comment_done {
43     ($comments{$filename} = []) unless defined $comments{$filename};
44     my %c = (message => buf2str);
45     if ($lineend - $linebgn == 1) {
46         $c{line} = $linebgn;
47     } else {
48         $c{range} = {start_line => $linebgn, end_line => $lineend};
49     };
50     ($c{in_reply_to} = $1) if ($blockid =~ /^R(.*)$/);
51     push @{$comments{$filename}}, \%c;
52 }
53
54 while (<>) {
55     chomp;
56     if (/^-{5}BEGIN (\w+) (.*) (\d+) (\d+)-{5}$/) {
57         $verbatim_block = 1;
58         ($main_message = buf2str) unless $blockn;
59         $blockn++;
60         comment_done if defined $filename;
61         $blockid = $1;
62         $filename = $2;
63         $linebgn = $3;
64         $lineend = $4;
65     };
66     push @buf, $_ unless $verbatim_block;
67     if (/^-{5}END-{5}$/) {
68         $verbatim_block = 0;
69     };
70 };
71 comment_done if defined $filename;
72 ($main_message = buf2str) if !$blockn;
73
74 my %result = (comments => \%comments);
75 ($result{message} = $main_message) if $main_message;
76 print encode_json(\%result);