Warn when a commit message isn't wrapped

Git tools assume the commit message is well structured according to
the standard convention of a subject line, blank line, and the body.
They also assume the message has been manually line wrapped at
shorter than 70 characters, so the message still fits on an 80
character wide terminal.

Encourage this formatting by warning users when a commit they are
uploading for review doesn't meet this criteria.

Change-Id: I6015a7a72e73f9bfffb239f2c6fb34a83352c0a7
Signed-off-by: Shawn O. Pearce <sop@google.com>
This commit is contained in:
Shawn O. Pearce
2010-02-25 14:53:44 -08:00
parent 298f9c38ee
commit c1fe3e4563

View File

@@ -775,6 +775,7 @@ public class ReceiveCommits implements PreReceiveHook, PostReceiveHook {
private void createChange(final RevWalk walk, final RevCommit c) private void createChange(final RevWalk walk, final RevCommit c)
throws OrmException, IOException { throws OrmException, IOException {
walk.parseBody(c); walk.parseBody(c);
warnMalformedMessage(c);
final Account.Id me = currentUser.getAccountId(); final Account.Id me = currentUser.getAccountId();
Change.Key changeKey = new Change.Key("I" + c.name()); Change.Key changeKey = new Change.Key("I" + c.name());
@@ -900,6 +901,7 @@ public class ReceiveCommits implements PreReceiveHook, PostReceiveHook {
throws IOException, OrmException { throws IOException, OrmException {
final RevCommit c = request.newCommit; final RevCommit c = request.newCommit;
rp.getRevWalk().parseBody(c); rp.getRevWalk().parseBody(c);
warnMalformedMessage(c);
final Account.Id me = currentUser.getAccountId(); final Account.Id me = currentUser.getAccountId();
final Set<Account.Id> reviewers = new HashSet<Account.Id>(reviewerId); final Set<Account.Id> reviewers = new HashSet<Account.Id>(reviewerId);
@@ -992,7 +994,7 @@ public class ReceiveCommits implements PreReceiveHook, PostReceiveHook {
reject(request.cmd, "no changes made"); reject(request.cmd, "no changes made");
return null; return null;
} else { } else {
rp.sendMessage("warning: " + change.getKey().abbreviate() + ": " // rp.sendMessage("(W) " + c.abbreviate(repo, 6).name() + ":" //
+ " no files changed, but" // + " no files changed, but" //
+ (!messageEq ? " message updated" : "") // + (!messageEq ? " message updated" : "") //
+ (!messageEq && !parentsEq ? " and" : "") // + (!messageEq && !parentsEq ? " and" : "") //
@@ -1325,6 +1327,28 @@ public class ReceiveCommits implements PreReceiveHook, PostReceiveHook {
return true; return true;
} }
private void warnMalformedMessage(RevCommit c) {
if (65 < c.getShortMessage().length()) {
rp.sendMessage("(W) " + c.abbreviate(repo, 6).name()
+ ": commit subject >65 characters; use shorter first paragraph");
}
int longLineCnt = 0, nonEmptyCnt = 0;
for (String line : c.getFullMessage().split("\n")) {
if (!line.trim().isEmpty()) {
nonEmptyCnt++;
}
if (70 < line.length()) {
longLineCnt++;
}
}
if (0 < longLineCnt && 33 < longLineCnt * 100 / nonEmptyCnt) {
rp.sendMessage("(W) " + c.abbreviate(repo, 6).name()
+ ": commit message lines >70 characters; manually wrap lines");
}
}
private void autoCloseChanges(final ReceiveCommand cmd) { private void autoCloseChanges(final ReceiveCommand cmd) {
final RevWalk rw = rp.getRevWalk(); final RevWalk rw = rp.getRevWalk();
try { try {