Skip to content

Commit 146c061

Browse files
committed
CommitForm: add support for ammend
We support "ammend" in commit. Its a checkbox which will ammend the last commit and will also pull the last log. Added a "force" push checkbox.
1 parent 5c19d92 commit 146c061

6 files changed

Lines changed: 95 additions & 18 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ else()
5757
include(cmake/CPM.cmake)
5858
include(cmake/icons-breeze.cmake)
5959

60-
CPMAddPackage("gh:diegoiast/qmdilib#483b78f3e3a995452798513620be59f79b95edf4")
60+
CPMAddPackage("gh:diegoiast/qmdilib#76f0bc264c26dfa5ab1a9f41944c66ae68efcb53")
6161
CPMAddPackage("gh:diegoiast/qutepart-cpp#249f082d356736c750023ac41950b6ad01880810")
6262
CPMAddPackage("gh:diegoiast/command-palette-widget#69eb447b61c9d042394a1404c71a0d0b54ac28c0")
6363
CPMAddPackage("gh:diegoiast/KodoTerm#9d016815076a451ad639fa021d46602585e91247")

src/plugins/git/CommitForm.cpp

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,18 @@ CommitForm::CommitForm(const QString &dir, GitPlugin *plugin, QWidget *parent)
321321
connect(ui->pushButton, &QAbstractButton::clicked, this, &CommitForm::pushImpl);
322322
connect(ui->revertSelectedButton, &QAbstractButton::clicked, this,
323323
&CommitForm::revertSelectionImpl);
324+
connect(ui->amendCheckbox, &QCheckBox::toggled, this, [this](bool checked) {
325+
if (checked) {
326+
git->runGit({"-C", repoRoot, "log", "-1", "--pretty=%B"})
327+
.then(this, [this](const std::tuple<QString, int> &res) {
328+
auto [msg, exitCode] = res;
329+
if (exitCode == 0) {
330+
ui->commitMessage->setPlainText(msg.trimmed());
331+
}
332+
});
333+
}
334+
ui->commitButton->setEnabled(checked || !model->checkedEntries().isEmpty());
335+
});
324336
connect(ui->tableView->selectionModel(), &QItemSelectionModel::selectionChanged, this,
325337
[this](const QItemSelection &selected, const QItemSelection &) {
326338
if (selected.indexes().size() == 0) {
@@ -342,7 +354,7 @@ CommitForm::CommitForm(const QString &dir, GitPlugin *plugin, QWidget *parent)
342354

343355
auto hasSelection = !model->checkedEntries().isEmpty();
344356
ui->revertSelectedButton->setEnabled(hasSelection);
345-
ui->commitButton->setEnabled(hasSelection);
357+
ui->commitButton->setEnabled(hasSelection || ui->amendCheckbox->isChecked());
346358
});
347359
connect(model, &QAbstractItemModel::modelReset, this,
348360
[this]() { ui->revertSelectedButton->setEnabled(false); });
@@ -543,26 +555,18 @@ void CommitForm::revertSelectionImpl() {
543555

544556
void CommitForm::commitImpl() {
545557
auto const &checked = model->checkedEntries();
546-
if (checked.isEmpty()) {
558+
if (checked.isEmpty() && !ui->amendCheckbox->isChecked()) {
547559
return;
548560
}
549561

550-
auto args = QStringList{"-C", repoRoot, "add"};
551-
for (auto const &c : checked) {
552-
args.push_back(c.filename);
553-
}
554-
555-
ui->gitOutput->clear();
556-
git->runGit(args).then(this, [this](const std::tuple<QString, int> &res) {
557-
auto [output, exitCode] = res;
558-
if (exitCode != 0) {
559-
ui->gitOutput->setText(output.trimmed());
560-
qDebug() << QString("ExitCode=%1, output=%2").arg(exitCode).arg(output);
561-
return;
562+
auto doCommit = [this]() {
563+
auto commitLogFileName = createTempFileWithContent(ui->commitMessage->toPlainText());
564+
auto commitArgs = QStringList{"-C", repoRoot, "commit"};
565+
if (ui->amendCheckbox->isChecked()) {
566+
commitArgs << "--amend";
562567
}
568+
commitArgs << "-F" << commitLogFileName;
563569

564-
auto commitLogFileName = createTempFileWithContent(ui->commitMessage->toPlainText());
565-
auto commitArgs = QStringList{"-C", repoRoot, "commit", "-F", commitLogFileName};
566570
git->runGit(commitArgs)
567571
.then(this, [this, commitLogFileName](const std::tuple<QString, int> &res2) {
568572
auto [output2, exitCode2] = res2;
@@ -572,10 +576,31 @@ void CommitForm::commitImpl() {
572576
qDebug() << QString("ExitCode=%1, output=%2").arg(exitCode2).arg(output2);
573577
} else {
574578
ui->commitMessage->clear();
579+
ui->amendCheckbox->setChecked(false);
575580
}
576581
updateGitStatus();
577582
});
578-
});
583+
};
584+
585+
if (checked.isEmpty()) {
586+
doCommit();
587+
} else {
588+
auto args = QStringList{"-C", repoRoot, "add"};
589+
for (auto const &c : checked) {
590+
args.push_back(c.filename);
591+
}
592+
593+
ui->gitOutput->clear();
594+
git->runGit(args).then(this, [this, doCommit](const std::tuple<QString, int> &res) {
595+
auto [output, exitCode] = res;
596+
if (exitCode != 0) {
597+
ui->gitOutput->setText(output.trimmed());
598+
qDebug() << QString("ExitCode=%1, output=%2").arg(exitCode).arg(output);
599+
return;
600+
}
601+
doCommit();
602+
});
603+
}
579604
}
580605

581606
void CommitForm::pushImpl() {
@@ -585,6 +610,10 @@ void CommitForm::pushImpl() {
585610
ui->gitOutput->clear();
586611

587612
auto args = QStringList{"-C", repoRoot, "push"};
613+
if (ui->forcePushCheckbox->isChecked()) {
614+
args << "--force";
615+
}
616+
588617
git->runGit(args).then(this, [this, t](const std::tuple<QString, int> &result) {
589618
auto [output, exitCode] = result;
590619
if (exitCode != 0) {
@@ -598,3 +627,5 @@ void CommitForm::pushImpl() {
598627
ui->pushButton->setEnabled(true);
599628
});
600629
};
630+
631+
void CommitForm::setAmend(bool amend) { ui->amendCheckbox->setChecked(amend); }

src/plugins/git/CommitForm.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class CommitForm : public QWidget, public qmdiClient {
3131
void revertSelectionImpl();
3232
void commitImpl();
3333
void pushImpl();
34+
void setAmend(bool amend);
3435

3536
private:
3637
Ui::CommitForm *ui;

src/plugins/git/CommitForm.ui

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,13 @@
7979
</property>
8080
</widget>
8181
</item>
82+
<item>
83+
<widget class="QCheckBox" name="amendCheckbox">
84+
<property name="text">
85+
<string>&amp;Amend</string>
86+
</property>
87+
</widget>
88+
</item>
8289
<item>
8390
<spacer name="horizontalSpacer">
8491
<property name="orientation">
@@ -92,6 +99,13 @@
9299
</property>
93100
</spacer>
94101
</item>
102+
<item>
103+
<widget class="QCheckBox" name="forcePushCheckbox">
104+
<property name="text">
105+
<string>&amp;Force</string>
106+
</property>
107+
</widget>
108+
</item>
95109
<item>
96110
<widget class="QPushButton" name="pushButton">
97111
<property name="toolTip">

src/plugins/git/GitPlugin.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ void GitPlugin::on_client_merged(qmdiHost *host) {
113113
logProject = new QAction(tr("git log project/dir"), this);
114114
revert = new QAction(tr("git revert"), this);
115115
commit = new QAction(tr("git commit"), this);
116+
commitAmend = new QAction(tr("git commit amend"), this);
116117
stash = new QAction(tr("git stash"), this);
117118
branches = new QAction(tr("git branch"), this);
118119

@@ -126,6 +127,8 @@ void GitPlugin::on_client_merged(qmdiHost *host) {
126127
revert->setShortcut(QKeySequence("Ctrl+G, U"));
127128
commit->setToolTip(tr("Record changes to the repository"));
128129
commit->setShortcut(QKeySequence("Ctrl+G, C"));
130+
commitAmend->setToolTip(tr("Amend the last commit"));
131+
commitAmend->setShortcut(QKeySequence("Ctrl+G, A"));
129132
stash->setToolTip(tr("tash away changes to dirty working directory"));
130133
branches->setToolTip(tr("List, create, or delete branches"));
131134

@@ -134,6 +137,7 @@ void GitPlugin::on_client_merged(qmdiHost *host) {
134137
connect(diffFile, &QAction::triggered, this, &GitPlugin::diffFileHandler);
135138
connect(revert, &QAction::triggered, this, &GitPlugin::revertFileHandler);
136139
connect(commit, &QAction::triggered, this, &GitPlugin::commitHandler);
140+
connect(commitAmend, &QAction::triggered, this, &GitPlugin::commitAmendHandler);
137141

138142
auto menuName = "&Git";
139143
host->menus.addActionGroup(menuName, "&Project");
@@ -142,6 +146,7 @@ void GitPlugin::on_client_merged(qmdiHost *host) {
142146
menus[menuName]->addAction(logProject);
143147
menus[menuName]->addAction(revert);
144148
menus[menuName]->addAction(commit);
149+
menus[menuName]->addAction(commitAmend);
145150
menus[menuName]->addAction(stash);
146151
menus[menuName]->addAction(branches);
147152

@@ -410,6 +415,30 @@ void GitPlugin::commitHandler() {
410415
});
411416
}
412417

418+
void GitPlugin::commitAmendHandler() {
419+
auto manager = getManager();
420+
auto client = manager->getMdiServer()->getCurrentClient();
421+
if (!client) {
422+
return;
423+
}
424+
auto filename = client->mdiClientFileName();
425+
if (filename.isEmpty()) {
426+
qDebug() << "Cannot commit on an empty file" << filename;
427+
return;
428+
}
429+
430+
detectRepoRoot(filename).then(this, [this, manager](const std::tuple<QString, int> &res) {
431+
auto [repoRoot, exitCode] = res;
432+
if (exitCode != 0 || repoRoot.isEmpty()) {
433+
qDebug() << "Filename is not in any git repo";
434+
return;
435+
}
436+
auto commitForm = new CommitForm(repoRoot, this, manager);
437+
commitForm->setAmend(true);
438+
mdiServer->addClient(commitForm);
439+
});
440+
}
441+
413442
void GitPlugin::commitDisplayHandler(const QModelIndex &mi) {
414443
auto widget = static_cast<GitCommitDisplay *>(form->container->widget(0));
415444
auto manager = getManager();

src/plugins/git/GitPlugin.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class GitPlugin : public IPlugin {
4848
void deleteBranchHandler();
4949
void commitDisplayHandler(const QModelIndex &mi);
5050
void commitHandler();
51+
void commitAmendHandler();
5152
void logHandler(GitPlugin::GitLog log, const QString &filename);
5253
void on_gitCommitClicked(const QModelIndex &mi);
5354
void on_gitCommitDoubleClicked(const QModelIndex &mi);
@@ -65,6 +66,7 @@ class GitPlugin : public IPlugin {
6566
QAction *logProject = nullptr;
6667
QAction *revert = nullptr;
6768
QAction *commit = nullptr;
69+
QAction *commitAmend = nullptr;
6870
QAction *stash = nullptr;
6971
QAction *branches = nullptr;
7072
QString gitBinary = "git";

0 commit comments

Comments
 (0)