]> Sergey Matveev's repositories - gerrvim.git/blob - gerrcommget.sh
Comments getter and converter to Gerrvim format utility
[gerrvim.git] / gerrcommget.sh
1 #!/bin/sh
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 [ -n "$PERL" ] || PERL=perl
19 [ -n "$CURL" ] || CURL=curl
20 [ -n "$GERRUSER" ] || GERRUSER=stargrave
21 [ -n "$GERRPASS" ] || GERRPASS=password
22 [ -n "$GERRADDR" ] || GERRADDR=http://gerrit.lan
23
24 change=$1
25 revision=$2
26
27 usage()
28 {
29     echo Usage: $0 CHANGE REVISION
30     exit 1
31 }
32
33 [ -n "$change" ] || usage
34 [ -n "$revision" ] || usage
35
36 $CURL --silent --user $GERRUSER:$GERRPASS \
37     $GERRADDR/changes/$change/revisions/$revision/comments |
38     $PERL -MEncode -MJSON -e '
39 <STDIN>; # Skip first Gerrit malformed JSON line
40 my @ser = <STDIN>;
41 my $deser = decode_json join "", @ser;
42 foreach my $f (keys %{$deser}) {
43     foreach my $comment (@{$deser->{$f}}) {
44         print "-----BEGIN R$comment->{id} $f";
45         print " $comment->{range}->{start_line}";
46         print " $comment->{range}->{end_line}-----\n";
47         print "$comment->{author}->{name}:\n";
48         my $m = encode_utf8 $comment->{message};
49         $m =~ s/\\n/\n/g;
50         print "$m\n-----END-----\n\n";
51     };
52 };
53 '