2 # gerrvim -- Gerrit review's comments preparation helper
3 # Copyright (C) 2015 Sergey Matveev <stargrave@stargrave.org>
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.
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.
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/>.
22 This script converts gerrvim Vim pluging temporary file to JSON suitable
23 to be published in Gerrit. Input file like this:
25 Some main review message.
28 -----BEGIN 5e5a5d9ae9339c8f2c5405c8145e61b738953b4e Makefile 27 29-----
32 You must replace make with $(MAKE).
34 And I could write it multiline too.
37 -----BEGIN 5e5a5d9ae9339c8f2c5405c8145e61b738953b4e Makefile 1 2-----
42 is transformed to JSON like this:
45 "message": "Some main review message.\nIt can be multilined.",
49 "message": "You must replace make with $(MAKE).\n\nAnd I could write it multiline too.",
56 "message": "Remove that.",
65 Each comment to lines of some file starts with -----BEGIN----- block.
66 After BEGIN word, four parts are comming (space separated):
70 =item * Commit's hash. SHA1 in hexadecimal
72 =item * Path to file inside repository
74 =item * Linenumber where comment begins
76 =item * Linenumber where comment ends
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-----.
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
101 my $main_message = undef;
102 my $verbatim_block = 0;
106 my $r = join "\n", @buf;
110 return decode_utf8 $r;
114 ($comments{$filename} = []) unless defined $comments{$filename};
115 my %c = ("message" => buf2str);
116 if ($lineend - $linebgn == 1) {
117 $c{"line"} = $linebgn;
119 $c{"range"} = {start_line => $linebgn, end_line => $lineend};
121 push @{$comments{$filename}}, \%c;
126 if (/^-{5}BEGIN \w{40} (.*) (\d+) (\d+)-{5}$/) {
128 ($main_message = buf2str) unless $blockn;
130 comment_done if defined $filename;
135 push @buf, $_ unless $verbatim_block;
136 if (/^-{5}END-{5}$/) {
142 my %result = (comments => \%comments);
143 ($result{"message"} = $main_message) if $main_message;
144 print encode_json(\%result);