Skip to content

Commit

Permalink
Merge pull request #667 from Murmele/work/templateManualApply
Browse files Browse the repository at this point in the history
Template: Apply with filenames from diff
  • Loading branch information
Murmele authored Nov 13, 2023
2 parents e7fd8da + 5286ed1 commit bbb6b88
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
36 changes: 28 additions & 8 deletions src/ui/CommitEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,27 @@ CommitEditor::CommitEditor(const git::Repository &repo, QWidget *parent)
mTemplate = new TemplateButton(this);
mTemplate->setText(tr("T"));
connect(mTemplate, &TemplateButton::templateChanged, this,
QOverload<const QString &>::of(&CommitEditor::applyTemplate));
[this](const QString &t) {
QStringList files;
if (mDiff.isValid()) {
int count = mDiff.count();
git::Index index = mDiff.index();
for (int i = 0; i < count; ++i) {
QString name = mDiff.name(i);
switch (index.isStaged(name)) {
case git::Index::PartiallyStaged:
case git::Index::Staged:
files.append(QFileInfo(name).fileName());
break;
case git::Index::Conflicted:
case git::Index::Disabled:
case git::Index::Unstaged:
break;
}
}
}
applyTemplate(t, files);
});

QLabel *label = new QLabel(tr("<b>Commit Message:</b>"), this);

Expand Down Expand Up @@ -585,11 +605,11 @@ QString CommitEditor::createFileList(const QStringList &list, int maxFiles) {
return msg;
}

void CommitEditor::setMessage(const QStringList &list) {
void CommitEditor::setMessage(const QStringList &files) {
if (mTemplate->templates().count() > 0) {
applyTemplate(mTemplate->templates().first().value, list);
applyTemplate(mTemplate->templates().first().value, files);
} else {
QString msg = createFileList(list, 3);
QString msg = createFileList(files, 3);
if (!msg.isEmpty())
msg = QStringLiteral("Update ") + msg;
setMessage(msg);
Expand Down Expand Up @@ -710,7 +730,7 @@ void CommitEditor::updateButtons(bool yieldFocus) {
return;
}

QStringList list;
QStringList files;
int staged = 0;
int partial = 0;
int conflicted = 0;
Expand All @@ -724,12 +744,12 @@ void CommitEditor::updateButtons(bool yieldFocus) {
break;

case git::Index::PartiallyStaged:
list.append(QFileInfo(name).fileName());
files.append(QFileInfo(name).fileName());
++partial;
break;

case git::Index::Staged:
list.append(QFileInfo(name).fileName());
files.append(QFileInfo(name).fileName());
++staged;
break;

Expand All @@ -743,7 +763,7 @@ void CommitEditor::updateButtons(bool yieldFocus) {
QSignalBlocker blocker(mMessage);
(void)blocker;

setMessage(list);
setMessage(files);
if (yieldFocus && !mMessage->toPlainText().isEmpty())
mMessage->setFocus();
}
Expand Down
2 changes: 1 addition & 1 deletion src/ui/CommitEditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class CommitEditor : public QFrame {
void unstage();
bool isUnstageEnabled() const;
static QString createFileList(const QStringList &list, int maxFiles);
void setMessage(const QStringList &list);
void setMessage(const QStringList &files);
void setMessage(const QString &message);
QString message() const;
void setDiff(const git::Diff &diff);
Expand Down
2 changes: 2 additions & 0 deletions src/ui/TemplateDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ TemplateDialog::TemplateDialog(QList<TemplateButton::Template> &templates,
hBox->addItem(spacer);
hBox->addWidget(mRemove);
QVBoxLayout *vBox2 = new QVBoxLayout();
vBox2->addWidget(
new QLabel(tr("First template will be applied automatically"), this));
vBox2->addWidget(mTemplateList);
vBox2->addLayout(hBox);

Expand Down

0 comments on commit bbb6b88

Please sign in to comment.