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