Commit eb97a287 authored by Waqar Ahmed's avatar Waqar Ahmed
Browse files

Make checkout happen in the background to avoid freezes in UI

parent 24c599aa
......@@ -8,6 +8,7 @@
#include "gitutils.h"
#include <QCoreApplication>
#include <QFutureWatcher>
#include <QKeyEvent>
#include <QLineEdit>
#include <QPainter>
......@@ -16,6 +17,7 @@
#include <QTextDocument>
#include <QTreeView>
#include <QVBoxLayout>
#include <QtConcurrentRun>
#include <KTextEditor/MainWindow>
#include <KTextEditor/Message>
......@@ -191,6 +193,9 @@ BranchesDialog::BranchesDialog(QWidget *parent, KTextEditor::MainWindow *mainWin
m_treeView->setSelectionMode(QTreeView::SingleSelection);
setHidden(true);
checkoutWatcher = new QFutureWatcher<GitUtils::CheckoutResult>();
connect(checkoutWatcher, &QFutureWatcher<GitUtils::CheckoutResult>::finished, this, &BranchesDialog::onCheckoutDone);
}
void BranchesDialog::openDialog()
......@@ -243,18 +248,16 @@ bool BranchesDialog::eventFilter(QObject *obj, QEvent *event)
return QWidget::eventFilter(obj, event);
}
void BranchesDialog::slotReturnPressed()
void BranchesDialog::onCheckoutDone()
{
const auto branch = m_proxyModel->data(m_treeView->currentIndex(), BranchesDialogModel::CheckoutName).toString();
int res = GitUtils::checkoutBranch(m_projectPath, branch);
const GitUtils::CheckoutResult res = checkoutWatcher->result();
auto msgType = KTextEditor::Message::Positive;
QString msgStr = i18n("Branch %1 checked out", branch);
if (res > 0) {
QString msgStr = i18n("Branch %1 checked out", res.branch);
if (res.returnCode > 0) {
msgType = KTextEditor::Message::Warning;
msgStr = i18n("Failed to checkout branch: %1", branch);
msgStr = i18n("Failed to checkout branch: %1", res.branch);
} else {
Q_EMIT branchChanged(branch);
Q_EMIT branchChanged(res.branch);
}
KTextEditor::Message *msg = new KTextEditor::Message(msgStr, msgType);
......@@ -263,6 +266,13 @@ void BranchesDialog::slotReturnPressed()
msg->setAutoHideMode(KTextEditor::Message::Immediate);
msg->setView(m_mainWindow->activeView());
m_mainWindow->activeView()->document()->postMessage(msg);
}
void BranchesDialog::slotReturnPressed()
{
const auto branch = m_proxyModel->data(m_treeView->currentIndex(), BranchesDialogModel::CheckoutName).toString();
QFuture<GitUtils::CheckoutResult> future = QtConcurrent::run(&GitUtils::checkoutBranch, m_projectPath, branch);
checkoutWatcher->setFuture(future);
m_lineEdit->clear();
hide();
......
......@@ -11,12 +11,18 @@ class BranchesDialogModel;
class QAction;
class BranchFilterModel;
class KActionCollection;
template<typename T> class QFutureWatcher;
namespace KTextEditor
{
class MainWindow;
}
namespace GitUtils
{
struct CheckoutResult;
}
class BranchesDialog : public QMenu
{
Q_OBJECT
......@@ -29,6 +35,8 @@ public:
Q_SIGNAL void branchChanged(const QString &branch);
Q_SLOT void onCheckoutDone();
protected:
bool eventFilter(QObject *obj, QEvent *event) override;
......@@ -43,4 +51,5 @@ private:
BranchFilterModel *m_proxyModel;
KTextEditor::MainWindow *m_mainWindow;
QString m_projectPath;
QFutureWatcher<GitUtils::CheckoutResult> *checkoutWatcher;
};
......@@ -34,16 +34,19 @@ QString GitUtils::getCurrentBranchName(const QString &repo)
return QString();
}
int GitUtils::checkoutBranch(const QString &repo, const QString &branch)
GitUtils::CheckoutResult GitUtils::checkoutBranch(const QString &repo, const QString &branch)
{
QProcess git;
git.setWorkingDirectory(repo);
QStringList args{QStringLiteral("checkout"), branch};
git.start(QStringLiteral("git"), args);
CheckoutResult res;
res.branch = branch;
if (git.waitForStarted() && git.waitForFinished(-1)) {
return git.exitCode();
res.returnCode = git.exitCode();
res.error = QString::fromUtf8(git.readAllStandardError());
}
return -1;
return res;
}
QVector<GitUtils::Branch> GitUtils::getAllBranchesAndTags(const QString &repo, RefType ref)
......
......@@ -24,11 +24,17 @@ struct Branch {
int score; // used for scoring when filtering
};
struct CheckoutResult {
QString branch;
QString error;
int returnCode;
};
bool isGitRepo(const QString &repo);
QString getCurrentBranchName(const QString &repo);
int checkoutBranch(const QString &repo, const QString &branch);
CheckoutResult checkoutBranch(const QString &repo, const QString &branch);
/**
* @brief get all local and remote branches
......
Markdown is supported
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