-
-
Notifications
You must be signed in to change notification settings - Fork 117
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
Add support for ${author} and ${branch} in templates #792
Open
esteban-aliverti
wants to merge
1
commit into
Murmele:master
Choose a base branch
from
esteban-aliverti:feature/457_extra_template_variables
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
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. |
||
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(); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
Author
link 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