Commit 032d52d6 authored by Waqar Ahmed's avatar Waqar Ahmed
Browse files

Implement new branch from branch checkout

parent 1164d2c6
......@@ -197,6 +197,13 @@ BranchesDialog::BranchesDialog(QWidget *parent, KTextEditor::MainWindow *mainWin
connect(&m_checkoutWatcher, &QFutureWatcher<GitUtils::CheckoutResult>::finished, this, &BranchesDialog::onCheckoutDone);
}
void BranchesDialog::resetValues()
{
m_checkoutBranchName.clear();
m_checkingOutFromBranch = false;
m_lineEdit->setPlaceholderText(i18n("Select branch to checkout. Press 'Esc' to cancel."));
}
void BranchesDialog::openDialog()
{
GitUtils::Branch newBranch;
......@@ -269,12 +276,30 @@ void BranchesDialog::onCheckoutDone()
void BranchesDialog::slotReturnPressed()
{
// we cleared the model to checkout new branch
if (m_model->rowCount() == 0) {
// we are checking out from a branch
// restore model and return for branch selection
if (m_checkingOutFromBranch) {
qWarning() << "Got name: " << m_lineEdit->text();
m_checkoutBranchName = m_lineEdit->text();
m_lineEdit->clear();
m_model->restoreForCheckout();
m_lineEdit->setPlaceholderText(i18n("Select branch to checkout from. Press 'Esc' to cancel."));
return;
}
createNewBranch(m_lineEdit->text());
;
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();
qWarning() << "Checking out from branch: " << fromBranch;
return createNewBranch(m_checkoutBranchName, fromBranch);
}
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();
......@@ -286,6 +311,10 @@ void BranchesDialog::slotReturnPressed()
m_lineEdit->setPlaceholderText(i18n("Enter new branch name. Press 'Esc' to cancel."));
return;
} else if (itemType == BranchesDialogModel::CreateBranchFrom) {
m_model->saveForCheckout();
m_lineEdit->setPlaceholderText(i18n("Enter new branch name. Press 'Esc' to cancel."));
m_checkingOutFromBranch = true;
return;
}
m_lineEdit->clear();
......@@ -308,15 +337,16 @@ void BranchesDialog::sendMessage(const QString &message, bool warn)
m_mainWindow->activeView()->document()->postMessage(msg);
}
void BranchesDialog::createNewBranch(const QString &branch)
void BranchesDialog::createNewBranch(const QString &branch, const QString &fromBranch)
{
if (branch.isEmpty()) {
m_lineEdit->clear();
hide();
return;
}
// the branch name might be invalid, let git handle it
const GitUtils::CheckoutResult r = GitUtils::checkoutNewBranch(m_projectPath, branch);
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);
......
......@@ -45,8 +45,9 @@ private Q_SLOTS:
void onCheckoutDone();
private:
void resetValues();
void sendMessage(const QString &message, bool warn);
void createNewBranch(const QString &branch);
void createNewBranch(const QString &branch, const QString &fromBranch = QString());
private:
QTreeView *m_treeView;
......@@ -56,4 +57,6 @@ private:
KTextEditor::MainWindow *m_mainWindow;
QString m_projectPath;
QFutureWatcher<GitUtils::CheckoutResult> m_checkoutWatcher;
QString m_checkoutBranchName;
bool m_checkingOutFromBranch = false;
};
......@@ -59,6 +59,9 @@ QVariant BranchesDialogModel::data(const QModelIndex &idx, int role) const
void BranchesDialogModel::refresh(QVector<GitUtils::Branch> branches)
{
// clear
QVector<Branch>().swap(m_savedDuringCheckout);
Branch create{branches.at(0).name, {}, {}, 0, 0, ItemType::CreateBranch};
Branch createFrom{branches.at(1).name, {}, {}, 0, 1, ItemType::CreateBranchFrom};
......@@ -70,6 +73,29 @@ void BranchesDialogModel::refresh(QVector<GitUtils::Branch> branches)
beginResetModel();
m_modelEntries = std::move(temp);
// m_modelEntries = std::move(branches);
endResetModel();
}
void BranchesDialogModel::clear()
{
beginResetModel();
QVector<Branch>().swap(m_modelEntries);
endResetModel();
}
void BranchesDialogModel::saveForCheckout()
{
beginResetModel();
// get rid of "create branch ..." items
m_modelEntries.removeFirst();
m_modelEntries.removeFirst();
m_savedDuringCheckout.swap(m_modelEntries);
endResetModel();
}
void BranchesDialogModel::restoreForCheckout()
{
beginResetModel();
m_modelEntries.swap(m_savedDuringCheckout);
endResetModel();
}
......@@ -25,12 +25,9 @@ public:
int columnCount(const QModelIndex &parent) const override;
QVariant data(const QModelIndex &idx, int role) const override;
void refresh(QVector<GitUtils::Branch> branches);
void clear()
{
beginResetModel();
QVector<Branch>().swap(m_modelEntries);
endResetModel();
}
void clear();
void saveForCheckout();
void restoreForCheckout();
bool setData(const QModelIndex &index, const QVariant &value, int role) override
{
......@@ -53,8 +50,9 @@ private:
int dateSort;
ItemType itemType;
};
QVector<BranchesDialogModel::Branch> m_modelEntries;
// QVector<GitUtils::Branch> m_modelEntries;
QVector<BranchesDialogModel::Branch> m_savedDuringCheckout;
};
#endif
......@@ -35,12 +35,26 @@ struct CheckoutResult {
int returnCode;
};
/**
* @brief check if @p repo is a git repo
* @param repo is path to the repo
* @return
*/
bool isGitRepo(const QString &repo);
/**
* @brief get name of current branch in @p repo
*/
QString getCurrentBranchName(const QString &repo);
/**
* @brief checkout to @p branch in @p repo
*/
CheckoutResult checkoutBranch(const QString &repo, const QString &branch);
/**
* @brief checkout to new @p branch in @p repo from @p fromBranch
*/
CheckoutResult checkoutNewBranch(const QString &repo, const QString &newBranch, const QString &fromBranch = QString());
/**
......@@ -53,4 +67,6 @@ QVector<Branch> getAllBranches(const QString &repo);
QVector<Branch> getAllBranchesAndTags(const QString &repo, RefType ref = RefType::All);
}
Q_DECLARE_TYPEINFO(GitUtils::Branch, Q_MOVABLE_TYPE);
#endif // GITUTILS_H
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