From 8c92f40db863d345e0f9cbe15de84fd369c9a5e8 Mon Sep 17 00:00:00 2001 From: Andre Hartmann Date: Mon, 21 Dec 2020 14:36:06 +0100 Subject: [PATCH] VCS: Add commit message verification Help writing good commit messages by verifying that the complete message is not too short, that the first line (subject) is not too long and that the subject is separated from the rest of the commit by an empty line. Change-Id: I296c036433767f2508630f409c0e4b4c241391d1 Reviewed-by: Leena Miettinen Reviewed-by: Orgad Shaneh --- src/plugins/vcsbase/submiteditorwidget.cpp | 51 ++++++++++++++++++++++ src/plugins/vcsbase/submiteditorwidget.h | 1 + src/plugins/vcsbase/submiteditorwidget.ui | 10 +++++ 3 files changed, 62 insertions(+) diff --git a/src/plugins/vcsbase/submiteditorwidget.cpp b/src/plugins/vcsbase/submiteditorwidget.cpp index ad15a8ca9b0..38ffe003d59 100644 --- a/src/plugins/vcsbase/submiteditorwidget.cpp +++ b/src/plugins/vcsbase/submiteditorwidget.cpp @@ -29,6 +29,7 @@ #include "ui_submiteditorwidget.h" #include +#include #include #include @@ -503,9 +504,59 @@ void SubmitEditorWidget::hideDescription() setDescriptionMandatory(false); } +void VcsBase::SubmitEditorWidget::verifyDescription() +{ + auto fontColor = [](Utils::Theme::Color color) { + return QString("") + .arg(Utils::creatorTheme()->color(color).name()); + }; + const QString hint = fontColor(Utils::Theme::OutputPanes_TestWarnTextColor); + const QString warning = fontColor(Utils::Theme::TextColorError); + + const QChar newLine = '\n'; + const int descriptionLength = d->m_description.length(); + int subjectLength = d->m_description.indexOf(newLine); + int secondLineLength = 0; + if (subjectLength >= 0) { + const int secondLineStart = subjectLength + 1; + secondLineLength = d->m_description.indexOf(newLine, secondLineStart) + - secondLineStart; + } else { + subjectLength = descriptionLength; + } + + enum { MaxSubjectLength = 72, WarningSubjectLength = 55 }; + QStringList hints; + QStringList toolTips; + if (descriptionLength < 20) + hints.append(warning + tr("Warning: The commit message is very short.")); + + if (subjectLength > MaxSubjectLength) + hints.append(warning + tr("Warning: The commit subject is too long.")); + else if (subjectLength > WarningSubjectLength) + hints.append(hint + tr("Hint: Aim for a shorter commit subject.")); + + if (secondLineLength > 0) + hints.append(hint + tr("Hint: The second line of a commit message should be empty.")); + + d->m_ui.descriptionHint->setText(hints.join("
")); + if (!d->m_ui.descriptionHint->text().isEmpty()) { + d->m_ui.descriptionHint->setToolTip( + tr("

Writing good commit messages

" + "
    " + "
  • Avoid very short commit messages.
  • " + "
  • Consider the first line as subject (like in email) " + "and keep it shorter than %1 characters.
  • " + "
  • After an empty second line, a longer description can be added.
  • " + "
  • Describe why the change was done, not how it was done.
  • " + "
").arg(MaxSubjectLength)); + } +} + void SubmitEditorWidget::descriptionTextChanged() { d->m_description = cleanupDescription(d->m_ui.description->toPlainText()); + verifyDescription(); wrapDescription(); trimDescription(); // append field entries diff --git a/src/plugins/vcsbase/submiteditorwidget.h b/src/plugins/vcsbase/submiteditorwidget.h index e19ab65c4de..ceec8c3d340 100644 --- a/src/plugins/vcsbase/submiteditorwidget.h +++ b/src/plugins/vcsbase/submiteditorwidget.h @@ -137,6 +137,7 @@ private: int checkedFilesCount() const; void wrapDescription(); void trimDescription(); + void verifyDescription(); SubmitEditorWidgetPrivate *d; }; diff --git a/src/plugins/vcsbase/submiteditorwidget.ui b/src/plugins/vcsbase/submiteditorwidget.ui index 9479930ec1e..b1ff57522c7 100644 --- a/src/plugins/vcsbase/submiteditorwidget.ui +++ b/src/plugins/vcsbase/submiteditorwidget.ui @@ -90,6 +90,16 @@ true + + + + + + + true + + +