From d51e560d3e82ab7aa7d615e031ed01a9bae10775 Mon Sep 17 00:00:00 2001
From: Sergey Matveev <stargrave@stargrave.org>
Date: Fri, 17 Jul 2015 23:47:26 +0300
Subject: [PATCH] Comments getter and converter to Gerrvim format utility

---
 gerrcommget.sh | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 53 insertions(+)
 create mode 100755 gerrcommget.sh

diff --git a/gerrcommget.sh b/gerrcommget.sh
new file mode 100755
index 0000000..2b96bd2
--- /dev/null
+++ b/gerrcommget.sh
@@ -0,0 +1,53 @@
+#!/bin/sh
+# gerrvim -- Gerrit review's comments preparation helper
+# Copyright (C) 2015 Sergey Matveev <stargrave@stargrave.org>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+[ -n "$PERL" ] || PERL=perl
+[ -n "$CURL" ] || CURL=curl
+[ -n "$GERRUSER" ] || GERRUSER=stargrave
+[ -n "$GERRPASS" ] || GERRPASS=password
+[ -n "$GERRADDR" ] || GERRADDR=http://gerrit.lan
+
+change=$1
+revision=$2
+
+usage()
+{
+    echo Usage: $0 CHANGE REVISION
+    exit 1
+}
+
+[ -n "$change" ] || usage
+[ -n "$revision" ] || usage
+
+$CURL --silent --user $GERRUSER:$GERRPASS \
+    $GERRADDR/changes/$change/revisions/$revision/comments |
+    $PERL -MEncode -MJSON -e '
+<STDIN>; # Skip first Gerrit malformed JSON line
+my @ser = <STDIN>;
+my $deser = decode_json join "", @ser;
+foreach my $f (keys %{$deser}) {
+    foreach my $comment (@{$deser->{$f}}) {
+        print "-----BEGIN R$comment->{id} $f";
+        print " $comment->{range}->{start_line}";
+        print " $comment->{range}->{end_line}-----\n";
+        print "$comment->{author}->{name}:\n";
+        my $m = encode_utf8 $comment->{message};
+        $m =~ s/\\n/\n/g;
+        print "$m\n-----END-----\n\n";
+    };
+};
+'
-- 
2.51.0