Allow to reject commits when subject or line length exceeds limit

Currently the commit message validator only emits a warning when
the commit message subject or line length exceeds the configured
limit.

Add a new configuration item that will allow the user to specify
that a commit should be rejected with an error if the commit message
subject or line length is too long.

Change-Id: I9a1355eef356fa25ee978607015aa664bf6119a0
diff --git a/src/main/java/com/googlesource/gerrit/plugins/validators/CommitMessageLengthValidation.java b/src/main/java/com/googlesource/gerrit/plugins/validators/CommitMessageLengthValidation.java
index 8214223..5bba706 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/validators/CommitMessageLengthValidation.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/validators/CommitMessageLengthValidation.java
@@ -36,13 +36,16 @@
 public class CommitMessageLengthValidation implements CommitValidationListener {
   private final static int DEFAULT_MAX_SUBJECT_LENGTH = 65;
   private final static int DEFAULT_MAX_LINE_LENGTH = 70;
+  private final static boolean DEFAULT_REJECT_TOO_LONG = false;
   private final static String COMMIT_MESSAGE_SECTION = "commitmessage";
   private final static String MAX_SUBJECT_LENGTH_KEY = "maxSubjectLength";
   private final static String MAX_LINE_LENGTH_KEY = "maxLineLength";
+  private final static String REJECT_TOO_LONG_KEY = "rejectTooLong";
 
   private final Config config;
   private final int maxSubjectLength;
   private final int maxLineLength;
+  private boolean rejectTooLong;
 
   @Inject
   public CommitMessageLengthValidation(@GerritServerConfig Config gerritConfig)
@@ -54,6 +57,21 @@
     this.maxLineLength = config.getInt(
         COMMIT_MESSAGE_SECTION, null,
         MAX_LINE_LENGTH_KEY, DEFAULT_MAX_LINE_LENGTH);
+    this.rejectTooLong = config.getBoolean(
+        COMMIT_MESSAGE_SECTION, REJECT_TOO_LONG_KEY,
+        DEFAULT_REJECT_TOO_LONG);
+  }
+
+  private void onLineTooLong(final AbbreviatedObjectId id,
+      List<CommitValidationMessage> messagesList, final String errorMessage)
+          throws CommitValidationException {
+    final String message = id.name() + ": " + errorMessage;
+    if (rejectTooLong) {
+      messagesList.add(new CommitValidationMessage(message, true));
+      throw new CommitValidationException("Commit length validation failed", messagesList);
+    } else {
+      messagesList.add(new CommitValidationMessage("(W) " + message, false));
+    }
   }
 
   @Override
@@ -64,9 +82,9 @@
     List<CommitValidationMessage> messages = new ArrayList<CommitValidationMessage>();
 
     if (this.maxSubjectLength < commit.getShortMessage().length()) {
-      messages.add(new CommitValidationMessage("(W) " + id.name() //
-         + ": commit subject >" + this.maxSubjectLength //
-         + " characters; use shorter first paragraph", false));
+      onLineTooLong(id, messages,
+          new String("commit subject >" + this.maxSubjectLength
+              + " characters; use shorter first paragraph"));
     }
 
     int longLineCnt = 0, nonEmptyCnt = 0;
@@ -80,9 +98,9 @@
     }
 
     if (0 < longLineCnt && 33 < longLineCnt * 100 / nonEmptyCnt) {
-      messages.add(new CommitValidationMessage("(W) " + id.name() //
-          + ": commit message lines >" + this.maxLineLength //
-          + " characters; manually wrap lines", false));
+      onLineTooLong(id, messages,
+          new String("commit message lines >" + this.maxLineLength
+              + " characters; manually wrap lines"));
     }
 
     return messages;
diff --git a/src/main/resources/Documentation/config.md b/src/main/resources/Documentation/config.md
index eee011a..86bc611 100644
--- a/src/main/resources/Documentation/config.md
+++ b/src/main/resources/Documentation/config.md
@@ -2,10 +2,10 @@
 ===================================
 
 This plugin checks the length of a commit's commit message
-subject and message body, and reports warnings to the git client
-if the lengths are exceeded.
+subject and message body, and reports warnings or errors to
+the git client if the lengths are exceeded.
 
-The maximum lengths of the subject and message body are can be
+The maximum lengths of the subject and message body can be
 configured in the standard Gerrit config file `gerrit.config`.
 
 commitmessage.maxSubjectLength
@@ -15,3 +15,8 @@
 commitmessage.maxLineLength
 :	Maximum length of a line in the commit message's body.  If
 	not specified, defaults to 70.
+
+commitmessage.rejectTooLong
+:	If set to `true`, reject commits whose subject or line
+	length exceeds the maximum allowed length.  If not
+	specified, defaults to `false`.