Make the max commit message subject and line lengths configurable

Instead of hard-coding the maximum commit message subject and line
lengths to 65 and 70 characters respectively, allow them to be
configured in the site's config file.

If the lengths are not configured, the default values (65 and 70)
are used.

Change-Id: I43dacc2dd281fd521a156b19ec145fe299f658ef
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 a8f2969..8214223 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/validators/CommitMessageLengthValidation.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/validators/CommitMessageLengthValidation.java
@@ -13,22 +13,48 @@
 // limitations under the License.
 package com.googlesource.gerrit.plugins.validators;
 
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.List;
 
+import org.eclipse.jgit.errors.ConfigInvalidException;
 import org.eclipse.jgit.lib.AbbreviatedObjectId;
+import org.eclipse.jgit.lib.Config;
 import org.eclipse.jgit.revwalk.RevCommit;
 
 import com.google.gerrit.extensions.annotations.Listen;
+import com.google.gerrit.server.config.GerritServerConfig;
 import com.google.gerrit.server.events.CommitReceivedEvent;
 import com.google.gerrit.server.git.validators.CommitValidationException;
 import com.google.gerrit.server.git.validators.CommitValidationListener;
 import com.google.gerrit.server.git.validators.CommitValidationMessage;
+import com.google.inject.Inject;
 import com.google.inject.Singleton;
 
 @Listen
 @Singleton
 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 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 Config config;
+  private final int maxSubjectLength;
+  private final int maxLineLength;
+
+  @Inject
+  public CommitMessageLengthValidation(@GerritServerConfig Config gerritConfig)
+      throws ConfigInvalidException, IOException {
+    this.config = gerritConfig;
+    this.maxSubjectLength = config.getInt(
+        COMMIT_MESSAGE_SECTION, null,
+        MAX_SUBJECT_LENGTH_KEY, DEFAULT_MAX_SUBJECT_LENGTH);
+    this.maxLineLength = config.getInt(
+        COMMIT_MESSAGE_SECTION, null,
+        MAX_LINE_LENGTH_KEY, DEFAULT_MAX_LINE_LENGTH);
+  }
 
   @Override
   public List<CommitValidationMessage> onCommitReceived(CommitReceivedEvent receiveEvent)
@@ -37,9 +63,10 @@
     final AbbreviatedObjectId id = commit.abbreviate(7);
     List<CommitValidationMessage> messages = new ArrayList<CommitValidationMessage>();
 
-    if (65 < commit.getShortMessage().length()) {
+    if (this.maxSubjectLength < commit.getShortMessage().length()) {
       messages.add(new CommitValidationMessage("(W) " + id.name() //
-         + ": commit subject >65 characters; use shorter first paragraph", false));
+         + ": commit subject >" + this.maxSubjectLength //
+         + " characters; use shorter first paragraph", false));
     }
 
     int longLineCnt = 0, nonEmptyCnt = 0;
@@ -47,14 +74,15 @@
       if (!line.trim().isEmpty()) {
         nonEmptyCnt++;
       }
-      if (70 < line.length()) {
+      if (this.maxLineLength < line.length()) {
         longLineCnt++;
       }
     }
 
     if (0 < longLineCnt && 33 < longLineCnt * 100 / nonEmptyCnt) {
       messages.add(new CommitValidationMessage("(W) " + id.name() //
-          + ": commit message lines >70 characters; manually wrap lines", false));
+          + ": commit message lines >" + this.maxLineLength //
+          + " characters; manually wrap lines", false));
     }
 
     return messages;
diff --git a/src/main/resources/Documentation/config.md b/src/main/resources/Documentation/config.md
new file mode 100644
index 0000000..b3f4821
--- /dev/null
+++ b/src/main/resources/Documentation/config.md
@@ -0,0 +1,17 @@
+Commit Message Length Configuration
+===================================
+
+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.
+
+The maximum lengths of the subject and message body are can be
+configured in the standard Gerrit config file `config.gerrit`.
+
+commitmessage.maxSubjectLength
+:	Maximum length of the commit message's subject line.  If
+	not specified, defaults to 65.
+
+commitmessage.maxLineLength
+:	Maximum length of a line in the commit message's body.  If
+	not specified, defaults to 70.