-
-
Notifications
You must be signed in to change notification settings - Fork 149
Add support for ${author} and ${branch} in templates #792
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -277,7 +277,17 @@ CommitEditor::CommitEditor(const git::Repository &repo, QWidget *parent) | |
| } | ||
| } | ||
| } | ||
| applyTemplate(t, files); | ||
|
|
||
| RepoView *view = RepoView::parentView(this); | ||
| git::Reference head = view ? view->repo().head() : git::Reference(); | ||
| git::Branch headBranch = head; | ||
|
|
||
| QString branchName = git::Branch(head).name(); | ||
| git::Config config = | ||
| view ? view->repo().gitConfig() : git::Config::global(); | ||
| QString author = config.value<QString>("user.name").toHtmlEscaped(); | ||
|
|
||
| applyTemplate(t, author, branchName, files); | ||
| }); | ||
|
|
||
| QLabel *label = new QLabel(tr("<b>Commit Message:</b>"), this); | ||
|
|
@@ -605,9 +615,11 @@ QString CommitEditor::createFileList(const QStringList &list, int maxFiles) { | |
| return msg; | ||
| } | ||
|
|
||
| void CommitEditor::setMessage(const QStringList &files) { | ||
| void CommitEditor::setMessage(const QString author, const QString branchName, | ||
| const QStringList &files) { | ||
| if (mTemplate->templates().count() > 0) { | ||
| applyTemplate(mTemplate->templates().first().value, files); | ||
| applyTemplate(mTemplate->templates().first().value, author, branchName, | ||
| files); | ||
| } else { | ||
| QString msg = createFileList(files, 3); | ||
| if (!msg.isEmpty()) | ||
|
|
@@ -634,49 +646,97 @@ void CommitEditor::setDiff(const git::Diff &diff) { | |
| } | ||
|
|
||
| // public slots | ||
| void CommitEditor::applyTemplate(const QString &t, const QStringList &files) { | ||
| void CommitEditor::applyTemplate(const QString &t, const QString &author, | ||
| const QString &branchName, | ||
| const QStringList &files) { | ||
|
|
||
| QString templ = t; | ||
| templ = applyAuthorTemplate(templ, author); | ||
| templ = applyBranchTemplate(templ, branchName); | ||
| templ = applyFileTemplate(templ, files); | ||
|
|
||
| auto index = templ.indexOf(TemplateButton::cursorPositionString); | ||
| if (index < 0) { | ||
| index = templ.length(); | ||
| } | ||
|
|
||
| templ.replace(TemplateButton::cursorPositionString, ""); | ||
| mMessage->setText(templ); | ||
| auto cursor = mMessage->textCursor(); | ||
| cursor.setPosition(index); | ||
| mMessage->setTextCursor(cursor); | ||
| } | ||
|
|
||
| void CommitEditor::applyTemplate(const QString &t, const QString &author, | ||
| const QString &branchName) { | ||
| applyTemplate(t, author, branchName, {}); | ||
| } | ||
|
|
||
| QString CommitEditor::applyAuthorTemplate(const QString &t, | ||
| const QString author) { | ||
| QString templateText = t; | ||
| //${author} | ||
| QString pattern = TemplateButton::authorPattern; | ||
| pattern.replace("{", "\\{"); | ||
| pattern.replace("}", "\\}"); | ||
| pattern.replace("$", "\\$"); | ||
| QRegularExpression re(pattern); | ||
| QRegularExpressionMatch match = re.match(templateText); | ||
| if (match.hasMatch()) { | ||
| const auto matchComplete = match.captured(0); | ||
| templateText.replace(matchComplete, author); | ||
| } | ||
| return templateText; | ||
| } | ||
|
|
||
| QString CommitEditor::applyBranchTemplate(const QString &t, | ||
| const QString branchName) { | ||
| QString templateText = t; | ||
| //${branch(:.*){0,1}} | ||
| QString pattern = TemplateButton::branchPattern; | ||
| pattern = pattern.replace(QRegularExpression("^\\$\\{"), "\\$\\{"); | ||
| pattern = pattern.replace(QRegularExpression("\\}$"), "\\}"); | ||
| QRegularExpression re(pattern); | ||
| QRegularExpressionMatch match = re.match(templateText); | ||
| if (match.hasMatch()) { | ||
| const auto matchComplete = match.captured(0); | ||
| QString branchPart = branchName; | ||
| int idx = matchComplete.indexOf(':'); | ||
| if (idx > 0) { | ||
| QRegularExpression branchRegex( | ||
| "(" + matchComplete.mid(idx + 1, matchComplete.length() - idx - 2) + | ||
| ")"); | ||
| match = branchRegex.match(branchName); | ||
| branchPart = match.captured(1); | ||
| } | ||
| templateText.replace(matchComplete, branchPart); | ||
| } | ||
| return templateText; | ||
| } | ||
|
|
||
| QString CommitEditor::applyFileTemplate(const QString &t, | ||
| const QStringList &files) { | ||
| QString templateText = t; | ||
| //${file:} | ||
| QString pattern = TemplateButton::filesPosition; | ||
| pattern.replace("{", "\\{"); | ||
| pattern.replace("}", "\\}"); | ||
| pattern.replace("$", "\\$"); | ||
| QRegularExpression re(pattern); | ||
| QRegularExpressionMatch match = re.match(templ); | ||
| int start = -1; | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't understand why were we calculating the offset instead of simply replacing all the variables and then checking where the placeholder for the cursor was left after all the substitutions.
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry for the late response. How do you know afterwards where to set the cursor? |
||
| int offset = 0; | ||
| QRegularExpressionMatch match = re.match(templateText); | ||
| if (match.hasMatch()) { | ||
| start = match.capturedStart(0); | ||
| int origLength = match.capturedLength(0); | ||
| const auto matchComplete = match.captured(0); | ||
| bool ok; | ||
| const auto number = match.captured(1).toInt(&ok); | ||
|
|
||
| if (ok) { | ||
| const QString filesStr = createFileList(files, number); | ||
| templ.replace(matchComplete, filesStr); | ||
| offset = filesStr.length() - origLength; | ||
| templateText.replace(matchComplete, filesStr); | ||
| } | ||
| } | ||
|
|
||
| auto index = t.indexOf(TemplateButton::cursorPositionString); | ||
| if (index < 0) | ||
| index = templ.length(); | ||
| else if (start > 0 && index > start) { | ||
| // offset, because fileStr has different length than matchComplete | ||
| index += offset; | ||
| } | ||
|
|
||
| templ.replace(TemplateButton::cursorPositionString, ""); | ||
| mMessage->setText(templ); | ||
| auto cursor = mMessage->textCursor(); | ||
| cursor.setPosition(index); | ||
| mMessage->setTextCursor(cursor); | ||
| return templateText; | ||
| } | ||
|
|
||
| void CommitEditor::applyTemplate(const QString &t) { applyTemplate(t, {}); } | ||
|
|
||
| void CommitEditor::updateButtons(bool yieldFocus) { | ||
| RepoView *view = RepoView::parentView(this); | ||
| if (!view || !view->repo().isValid()) { | ||
|
|
@@ -720,6 +780,10 @@ void CommitEditor::updateButtons(bool yieldFocus) { | |
| git::Reference head = view ? view->repo().head() : git::Reference(); | ||
| git::Branch headBranch = head; | ||
|
|
||
| QString branchName = git::Branch(head).name(); | ||
| git::Config config = view ? view->repo().gitConfig() : git::Config::global(); | ||
| QString author = config.value<QString>("user.name").toHtmlEscaped(); | ||
|
|
||
| mMergeAbort->setText(tr("Abort %1").arg(text)); | ||
| mMergeAbort->setVisible(headBranch.isValid() && merging); | ||
|
|
||
|
|
@@ -763,7 +827,7 @@ void CommitEditor::updateButtons(bool yieldFocus) { | |
| QSignalBlocker blocker(mMessage); | ||
| (void)blocker; | ||
|
|
||
| setMessage(files); | ||
| setMessage(author, branchName, files); | ||
| if (yieldFocus && !mMessage->toPlainText().isEmpty()) | ||
| mMessage->setFocus(); | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was not sure if there was an easier way to get the author and branch name at this point
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question. Because of the possibility to temporarly changing the username amd email, I think you need to take the one from the repoview
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When you change temporarly the username (click on the
Authorlink above the commit message), still the old name is used.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@esteban-aliverti can you have a look into it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@esteban-aliverti