Commit 334f8960 authored by Waqar Ahmed's avatar Waqar Ahmed Committed by Christoph Cullmann
Browse files

Git: Maintain a recent push/pull command history

Very annoying to change the command every time if you are in a
non-github-like git workflow (e.g., gerrit).

Also, use mono font in the dialog since thats what is normally used for
commands and is more readable.
parent af2457bb
Pipeline #93211 passed with stage
in 4 minutes and 15 seconds
......@@ -174,14 +174,14 @@ GitWidget::GitWidget(KateProject *project, KTextEditor::MainWindow *mainWindow,
m_pushBtn = toolButton(QStringLiteral("vcs-push"), i18n("Git push"));
connect(m_pushBtn, &QToolButton::clicked, this, [this]() {
PushPullDialog ppd(m_mainWin->window(), m_gitPath);
PushPullDialog ppd(m_mainWin, m_gitPath);
connect(&ppd, &PushPullDialog::runGitCommand, this, &GitWidget::runPushPullCmd);
ppd.openDialog(PushPullDialog::Push);
});
m_pullBtn = toolButton(QStringLiteral("vcs-pull"), i18n("Git pull"));
connect(m_pullBtn, &QToolButton::clicked, this, [this]() {
PushPullDialog ppd(m_mainWin->window(), m_gitPath);
PushPullDialog ppd(m_mainWin, m_gitPath);
connect(&ppd, &PushPullDialog::runGitCommand, this, &GitWidget::runPushPullCmd);
ppd.openDialog(PushPullDialog::Pull);
});
......
......@@ -6,23 +6,95 @@
#include "pushpulldialog.h"
#include <QProcess>
#include <QStandardItemModel>
PushPullDialog::PushPullDialog(QWidget *mainWindow, const QString &repoPath)
: QuickDialog(nullptr, mainWindow)
#include <KConfigGroup>
#include <KSharedConfig>
#include <KTextEditor/ConfigInterface>
#include <KTextEditor/MainWindow>
#include <KTextEditor/View>
PushPullDialog::PushPullDialog(KTextEditor::MainWindow *mainWindow, const QString &repoPath)
: QuickDialog(nullptr, mainWindow->window())
, m_repo(repoPath)
{
auto ciface = qobject_cast<KTextEditor::ConfigInterface *>(mainWindow->activeView());
Q_ASSERT(ciface);
m_lineEdit.setFont(ciface->configValue(QStringLiteral("font")).value<QFont>());
loadLastExecutedCommands();
}
void PushPullDialog::openDialog(PushPullDialog::Mode m)
{
if (m == Push) {
m_lineEdit.setText(buildPushString());
} else if (m == Pull) {
m_lineEdit.setText(buildPullString());
// build the string
QString builtString = m == Push ? buildPushString() : buildPullString();
// find if we have a last executed push/pull command
QString lastCmd = getLastPushPullCmd(m);
QStringList lastExecCmds = m_lastExecutedCommands;
if (!lastExecCmds.contains(builtString)) {
lastExecCmds.push_front(builtString);
}
// if found, bring it up
if (!lastCmd.isEmpty()) {
lastExecCmds.removeAll(lastCmd);
lastExecCmds.push_front(lastCmd);
}
auto *model = new QStandardItemModel(this);
m_treeView.setModel(model);
auto monoFont = m_lineEdit.font();
for (const QString &cmd : std::as_const(lastExecCmds)) {
auto *item = new QStandardItem(cmd);
item->setFont(monoFont);
model->appendRow(item);
}
connect(m_treeView.selectionModel(), &QItemSelectionModel::currentChanged, this, [this](const QModelIndex &current, const QModelIndex &) {
m_lineEdit.setText(current.data().toString());
});
m_treeView.setCurrentIndex(model->index(0, 0));
exec();
}
QString PushPullDialog::getLastPushPullCmd(Mode m) const
{
const QString cmdToFind = m == Push ? QStringLiteral("git push") : QStringLiteral("git pull");
QString found;
for (const auto &cmd : m_lastExecutedCommands) {
if (cmd.startsWith(cmdToFind)) {
found = cmd;
break;
}
}
return found;
}
void PushPullDialog::loadLastExecutedCommands()
{
KConfigGroup config(KSharedConfig::openConfig(), "kategit");
m_lastExecutedCommands = config.readEntry("lastExecutedGitCmds", QStringList());
}
void PushPullDialog::saveCommand(const QString &command)
{
KConfigGroup config(KSharedConfig::openConfig(), "kategit");
QStringList cmds = m_lastExecutedCommands;
cmds.removeAll(command);
cmds.push_front(command);
while (cmds.size() > 8) {
cmds.pop_back();
}
config.writeEntry("lastExecutedGitCmds", cmds);
}
/**
* This is not for display, hence not reusing gitutils here
*/
......@@ -94,6 +166,7 @@ void PushPullDialog::slotReturnPressed()
if (!m_lineEdit.text().isEmpty()) {
auto args = m_lineEdit.text().split(QLatin1Char(' '));
if (args.first() == QStringLiteral("git")) {
saveCommand(m_lineEdit.text());
args.pop_front();
Q_EMIT runGitCommand(args);
}
......
......@@ -12,7 +12,7 @@ class PushPullDialog : public QuickDialog
{
Q_OBJECT
public:
PushPullDialog(QWidget *mainWindow, const QString &repo);
PushPullDialog(KTextEditor::MainWindow *mainWindow, const QString &repo);
enum Mode { Push, Pull };
void openDialog(Mode m);
......@@ -22,8 +22,13 @@ public:
private:
QString buildPushString();
QString buildPullString();
void loadLastExecutedCommands();
void saveCommand(const QString &command);
QString getLastPushPullCmd(Mode m) const;
QString m_repo;
QStringList m_lastExecutedCommands;
protected Q_SLOTS:
void slotReturnPressed() override;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment