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

Create a base branch dialog


Signed-off-by: Waqar Ahmed's avatarWaqar Ahmed <waqar.17a@gmail.com>
parent cdbc4e53
......@@ -154,8 +154,8 @@ private:
BranchesDialog::BranchesDialog(QWidget *window, KateProjectPluginView *pluginView, QString projectPath)
: QuickDialog(nullptr, window)
, m_pluginView(pluginView)
, m_projectPath(projectPath)
, m_pluginView(pluginView)
{
m_model = new BranchesDialogModel(this);
m_proxyModel = new BranchFilterModel(this);
......@@ -165,36 +165,16 @@ BranchesDialog::BranchesDialog(QWidget *window, KateProjectPluginView *pluginVie
auto delegate = new StyleDelegate(this);
connect(&m_lineEdit, &QLineEdit::textChanged, this, [this, delegate](const QString &s) {
m_proxyModel->setFilterString(s);
static_cast<BranchFilterModel *>(m_proxyModel)->setFilterString(s);
delegate->setFilterString(s);
});
connect(&m_checkoutWatcher, &QFutureWatcher<GitUtils::CheckoutResult>::finished, this, &BranchesDialog::onCheckoutDone);
}
BranchesDialog::~BranchesDialog()
{
if (m_checkoutWatcher.isRunning()) {
onCheckoutDone();
}
}
void BranchesDialog::resetValues()
void BranchesDialog::openDialog(GitUtils::RefType r)
{
m_checkoutBranchName.clear();
m_checkingOutFromBranch = false;
m_lineEdit.setPlaceholderText(i18n("Select branch to checkout. Press 'Esc' to cancel."));
}
m_lineEdit.setPlaceholderText(i18n("Select Branch..."));
void BranchesDialog::openDialog()
{
resetValues();
GitUtils::Branch newBranch;
newBranch.name = i18n("Create New Branch");
GitUtils::Branch newBranchFrom;
newBranchFrom.name = i18n("Create New Branch From...");
QVector<GitUtils::Branch> branches{newBranch, newBranchFrom};
/*QVector<GitUtils::Branch> */ branches << GitUtils::getAllBranches(m_projectPath);
QVector<GitUtils::Branch> branches = GitUtils::getAllBranchesAndTags(m_projectPath, r);
m_model->refresh(branches);
reselectFirst();
......@@ -203,55 +183,14 @@ void BranchesDialog::openDialog()
exec();
}
void BranchesDialog::onCheckoutDone()
{
const GitUtils::CheckoutResult res = m_checkoutWatcher.result();
auto msgType = KTextEditor::Message::Positive;
QString msgStr = i18n("Branch %1 checked out", res.branch);
if (res.returnCode > 0) {
msgType = KTextEditor::Message::Warning;
msgStr = i18n("Failed to checkout to branch %1, Error: %2", res.branch, res.error);
}
sendMessage(msgStr, msgType == KTextEditor::Message::Warning);
}
void BranchesDialog::slotReturnPressed()
{
// we cleared the model to checkout new branch
if (m_model->rowCount() == 0) {
createNewBranch(m_lineEdit.text(), m_checkoutBranchName);
return;
}
// branch is selected, do actual checkout
if (m_checkingOutFromBranch) {
m_checkingOutFromBranch = false;
const auto fromBranch = m_proxyModel->data(m_treeView.currentIndex(), BranchesDialogModel::CheckoutName).toString();
m_checkoutBranchName = fromBranch;
m_model->clear();
clearLineEdit();
m_lineEdit.setPlaceholderText(i18n("Enter new branch name. Press 'Esc' to cancel."));
return;
}
const auto branch = m_proxyModel->data(m_treeView.currentIndex(), BranchesDialogModel::CheckoutName).toString();
const auto itemType = (BranchesDialogModel::ItemType)m_proxyModel->data(m_treeView.currentIndex(), BranchesDialogModel::ItemTypeRole).toInt();
Q_ASSERT(itemType == BranchesDialogModel::BranchItem);
if (itemType == BranchesDialogModel::BranchItem) {
QFuture<GitUtils::CheckoutResult> future = QtConcurrent::run(&GitUtils::checkoutBranch, m_projectPath, branch);
m_checkoutWatcher.setFuture(future);
} else if (itemType == BranchesDialogModel::CreateBranch) {
m_model->clear();
m_lineEdit.setPlaceholderText(i18n("Enter new branch name. Press 'Esc' to cancel."));
return;
} else if (itemType == BranchesDialogModel::CreateBranchFrom) {
m_model->clearBranchCreationItems();
clearLineEdit();
m_lineEdit.setPlaceholderText(i18n("Select branch to checkout from. Press 'Esc' to cancel."));
m_checkingOutFromBranch = true;
return;
}
m_branch = branch;
Q_EMIT branchSelected(branch);
clearLineEdit();
hide();
......@@ -273,24 +212,3 @@ void BranchesDialog::sendMessage(const QString &plainText, bool warn)
genericMessage.insert(QStringLiteral("text"), plainText);
Q_EMIT m_pluginView->message(genericMessage);
}
void BranchesDialog::createNewBranch(const QString &branch, const QString &fromBranch)
{
if (branch.isEmpty()) {
clearLineEdit();
hide();
return;
}
// the branch name might be invalid, let git handle it
const GitUtils::CheckoutResult r = GitUtils::checkoutNewBranch(m_projectPath, branch, fromBranch);
const bool warn = true;
if (r.returnCode == 0) {
sendMessage(i18n("Checked out to new branch: %1", r.branch), !warn);
} else {
sendMessage(i18n("Failed to create new branch. Error \"%1\"", r.error), warn);
}
clearLineEdit();
hide();
}
......@@ -3,6 +3,9 @@
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#ifndef BRANCHES_DIALOG_H
#define BRANCHES_DIALOG_H
#include <QFutureWatcher>
#include <QMenu>
......@@ -27,25 +30,28 @@ class BranchesDialog : public QuickDialog
Q_OBJECT
public:
BranchesDialog(QWidget *window, KateProjectPluginView *pluginView, QString projectPath);
~BranchesDialog();
void openDialog();
void openDialog(GitUtils::RefType r);
void sendMessage(const QString &message, bool warn);
QString branch() const
{
return m_branch;
}
Q_SIGNALS:
void branchSelected(const QString &branch);
private Q_SLOTS:
void slotReturnPressed() override;
void reselectFirst();
void onCheckoutDone();
private:
void resetValues();
void sendMessage(const QString &message, bool warn);
void createNewBranch(const QString &branch, const QString &fromBranch = QString());
protected:
BranchesDialogModel *m_model;
QSortFilterProxyModel *m_proxyModel;
QString m_projectPath;
private:
BranchesDialogModel *m_model;
BranchFilterModel *m_proxyModel;
KateProjectPluginView *m_pluginView;
QString m_projectPath;
QFutureWatcher<GitUtils::CheckoutResult> m_checkoutWatcher;
QString m_checkoutBranchName;
bool m_checkingOutFromBranch = false;
QString m_branch;
};
#endif
......@@ -65,15 +65,18 @@ QVariant BranchesDialogModel::data(const QModelIndex &idx, int role) const
return {};
}
void BranchesDialogModel::refresh(QVector<GitUtils::Branch> branches)
void BranchesDialogModel::refresh(const QVector<GitUtils::Branch> &branches, bool checkingOut)
{
// clear
Branch create{branches.at(0).name, {}, {}, 0, 0, ItemType::CreateBranch};
Branch createFrom{branches.at(1).name, {}, {}, 0, 1, ItemType::CreateBranchFrom};
QVector<Branch> temp{create, createFrom};
QVector<Branch> temp;
if (checkingOut) {
Branch create{branches.at(0).name, {}, {}, 0, 0, ItemType::CreateBranch};
Branch createFrom{branches.at(1).name, {}, {}, 0, 1, ItemType::CreateBranchFrom};
temp.push_back(create);
temp.push_back(createFrom);
}
for (int i = 2; i < branches.size(); ++i) {
int i = checkingOut ? 2 : 0;
for (; i < branches.size(); ++i) {
temp.append({branches.at(i).name, branches.at(i).remote, branches.at(i).type, -1, i, ItemType::BranchItem});
}
......
......@@ -24,7 +24,7 @@ public:
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
int columnCount(const QModelIndex &parent) const override;
QVariant data(const QModelIndex &idx, int role) const override;
void refresh(QVector<GitUtils::Branch> branches);
void refresh(const QVector<GitUtils::Branch> &branches, bool checkingOut = false);
void clear();
void clearBranchCreationItems();
......
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