Merge branch 'stable-2.7' Conflicts: pom.xml Change-Id: I1aa397364c36a78e5725d59cf586af5d040cc350
diff --git a/BUCK b/BUCK new file mode 100644 index 0000000..1d6a76d --- /dev/null +++ b/BUCK
@@ -0,0 +1,5 @@ +gerrit_plugin( + name = 'commit-message-length-validator', + srcs = glob(['src/main/java/**/*.java']), + resources = glob(['src/main/resources/**/*']), +)
diff --git a/pom.xml b/pom.xml index be8b057..6db3f29 100644 --- a/pom.xml +++ b/pom.xml
@@ -23,7 +23,7 @@ <name>commit-message-length-validator</name> <groupId>com.googlesource.gerrit.plugins.validators</groupId> <packaging>jar</packaging> - <version>2.7</version> + <version>2.8-SNAPSHOT</version> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
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/about.md b/src/main/resources/Documentation/about.md new file mode 100644 index 0000000..04b05da --- /dev/null +++ b/src/main/resources/Documentation/about.md
@@ -0,0 +1,3 @@ +This plugin checks the length of a commit's commit message +subject and message body, and reports warnings or errors to +the git client if the lengths are exceeded.
diff --git a/src/main/resources/Documentation/config.md b/src/main/resources/Documentation/config.md index eee011a..92440e7 100644 --- a/src/main/resources/Documentation/config.md +++ b/src/main/resources/Documentation/config.md
@@ -1,11 +1,7 @@ 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 +The maximum lengths of the subject and message body can be configured in the standard Gerrit config file `gerrit.config`. commitmessage.maxSubjectLength @@ -15,3 +11,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`.