]> Sergey Matveev's repositories - gerrvim.git/blob - gerrvim2json.pl
0cf9c869e6410a856ec82533e3242399a200945a
[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 =pod
19
20 =head1 DESCRIPTION
21
22 This script converts gerrvim Vim pluging temporary file to JSON suitable
23 to be published in Gerrit. Input file like this:
24
25     Some main review message.
26     It can be multilined.
27
28     -----BEGIN 5e5a5d9ae9339c8f2c5405c8145e61b738953b4e Makefile 27 29-----
29     foo: bar
30         make -C some thing
31     -----END-----
32     You must replace make with $(MAKE).
33
34     And I could write it multiline too.
35
36
37     -----BEGIN 5e5a5d9ae9339c8f2c5405c8145e61b738953b4e Makefile 1 2-----
38     #/usr/bin/make
39     -----END-----
40     Remove that.
41
42 is transformed to JSON like this:
43
44     {
45       "message": "Some main review message.\nIt can be multilined.",
46       "comments": {
47         "Makefile": [
48           {
49             "message": "You must replace make with $(MAKE).\n\nAnd I could write it multiline too.",
50             "range": {
51               "start_line": "27",
52               "end_line": "29"
53             }
54           },
55           {
56             "message": "Remove that.",
57             "range": {
58               "end_line": "2",
59               "start_line": "1"
60             }
61           }
62         ]
63       }
64     }
65
66 =head1 INPUT FORMAT
67
68 Each comment to lines of some file starts with -----BEGIN----- block.
69 After BEGIN word, four parts are comming (space separated):
70
71 =over 4
72
73 =item * Commit's hash. SHA1 in hexadecimal
74
75 =item * Path to file inside repository
76
77 =item * Linenumber where comment begins
78
79 =item * Linenumber where comment ends
80
81 =back
82
83 After BEGIN goes optional text that won't be included in JSON at all. As
84 a rule it is just a copy of code to be commented. It ends with -----END-----.
85
86 Everything between END of one block and BEGIN of another is treated as a
87 comment to the block above. Empty newlines at the end are removed.
88 Optional text before the first BEGIN block is treated as overall review
89 message.
90
91 =cut
92
93 use strict;
94 use warnings;
95
96 use Encode;
97 use JSON;
98
99 my %comments;
100 my $filename = undef;
101 my $linebgn = undef;
102 my $lineend = undef;
103 my $blockn = 0;
104 my $main_message = undef;
105 my $verbatim_block = 0;
106 my @buf;
107
108 sub buf2str {
109     my $r = join "\n", @buf;
110     chomp $r;
111     $r =~ s/\n+$//g;
112     @buf = ();
113     return decode_utf8 $r;
114 };
115
116 sub comment_done {
117     ($comments{$filename} = []) unless defined $comments{$filename};
118     push @{$comments{$filename}}, {
119         range => {start_line => $linebgn, end_line => $lineend},
120         message => buf2str,
121     };
122 }
123
124 while (<>) {
125     chomp;
126     if (/^-{5}BEGIN \w{40} (.*) (\d+) (\d+)-{5}$/) {
127         $verbatim_block = 1;
128         ($main_message = buf2str) unless $blockn;
129         $blockn++;
130         comment_done if defined $filename;
131         $filename = $1;
132         $linebgn = $2;
133         $lineend = $3;
134     };
135     push @buf, $_ unless $verbatim_block;
136     if (/^-{5}END-{5}$/) {
137         $verbatim_block = 0;
138     };
139 };
140 comment_done;
141
142 my %result = (comments => \%comments);
143 ($result{"message"} = $main_message) if $main_message;
144 print encode_json(\%result);