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.",
68 Each comment to lines of some file starts with -----BEGIN----- block.
69 After BEGIN word, four parts are comming (space separated):
73 =item * Commit's hash. SHA1 in hexadecimal
75 =item * Path to file inside repository
77 =item * Linenumber where comment begins
79 =item * Linenumber where comment ends
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-----.
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
100 my $filename = undef;
104 my $main_message = undef;
105 my $verbatim_block = 0;
109 my $r = join "\n", @buf;
113 return decode_utf8 $r;
117 ($comments{$filename} = []) unless defined $comments{$filename};
118 push @{$comments{$filename}}, {
119 range => {start_line => $linebgn, end_line => $lineend},
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);