]> Sergey Matveev's repositories - gerrvim.git/blob - gerrvim2json.pl
Unnecessary quotes on hash keys
[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             "line": 1
58           }
59         ]
60       }
61     }
62
63 =head1 INPUT FORMAT
64
65 Each comment to lines of some file starts with -----BEGIN----- block.
66 After BEGIN word, four parts are comming (space separated):
67
68 =over 4
69
70 =item * Commit's hash. SHA1 in hexadecimal
71
72 =item * Path to file inside repository
73
74 =item * Linenumber where comment begins
75
76 =item * Linenumber where comment ends
77
78 =back
79
80 After BEGIN goes optional text that won't be included in JSON at all. As
81 a rule it is just a copy of code to be commented. It ends with -----END-----.
82
83 Everything between END of one block and BEGIN of another is treated as a
84 comment to the block above. Empty newlines at the end are removed.
85 Optional text before the first BEGIN block is treated as overall review
86 message.
87
88 =cut
89
90 use strict;
91 use warnings;
92
93 use Encode;
94 use JSON;
95
96 my %comments;
97 my $filename = undef;
98 my $linebgn = undef;
99 my $lineend = undef;
100 my $blockn = 0;
101 my $main_message = undef;
102 my $verbatim_block = 0;
103 my @buf;
104
105 sub buf2str {
106     my $r = join "\n", @buf;
107     chomp $r;
108     $r =~ s/\n+$//g;
109     @buf = ();
110     return decode_utf8 $r;
111 };
112
113 sub comment_done {
114     ($comments{$filename} = []) unless defined $comments{$filename};
115     my %c = (message => buf2str);
116     if ($lineend - $linebgn == 1) {
117         $c{line} = $linebgn;
118     } else {
119         $c{range} = {start_line => $linebgn, end_line => $lineend};
120     };
121     push @{$comments{$filename}}, \%c;
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);