File indexing completed on 2024-04-28 05:49:04

0001 /*
0002     SPDX-FileCopyrightText: 2021 Waqar Ahmed <waqar.17a@gmail.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 #include "branchcheckoutdialog.h"
0007 #include "branchesdialogmodel.h"
0008 
0009 #include <KLocalizedString>
0010 #include <QtConcurrentRun>
0011 
0012 BranchCheckoutDialog::BranchCheckoutDialog(QWidget *mainWindow, QString projectPath)
0013     : BranchesDialog(mainWindow, projectPath)
0014 {
0015     connect(&m_checkoutWatcher, &QFutureWatcher<GitUtils::CheckoutResult>::finished, this, &BranchCheckoutDialog::onCheckoutDone);
0016 }
0017 
0018 BranchCheckoutDialog::~BranchCheckoutDialog()
0019 {
0020     if (m_checkoutWatcher.isRunning()) {
0021         onCheckoutDone();
0022     }
0023 }
0024 
0025 void BranchCheckoutDialog::resetValues()
0026 {
0027     m_checkoutFromBranchName.clear();
0028     m_checkingOutFromBranch = false;
0029     m_lineEdit.setPlaceholderText(i18n("Select branch to checkout. Press 'Esc' to cancel."));
0030 }
0031 
0032 void BranchCheckoutDialog::openDialog()
0033 {
0034     resetValues();
0035     GitUtils::Branch newBranch;
0036     newBranch.name = i18n("Create New Branch");
0037     GitUtils::Branch newBranchFrom;
0038     newBranchFrom.name = i18n("Create New Branch From...");
0039     QList<GitUtils::Branch> branches{newBranch, newBranchFrom};
0040     branches << GitUtils::getAllBranches(m_projectPath);
0041     m_model->refresh(branches, /*checkingOut:*/ true);
0042 
0043     reselectFirst();
0044     updateViewGeometry();
0045     setFocus();
0046     exec();
0047 }
0048 
0049 void BranchCheckoutDialog::onCheckoutDone()
0050 {
0051     const GitUtils::CheckoutResult res = m_checkoutWatcher.result();
0052     bool warn = false;
0053     QString msgStr = i18n("Branch %1 checked out", res.branch);
0054     if (res.returnCode > 0) {
0055         warn = true;
0056         msgStr = i18n("Failed to checkout to branch %1, Error: %2", res.branch, res.error);
0057     }
0058 
0059     sendMessage(msgStr, warn);
0060 }
0061 
0062 void BranchCheckoutDialog::slotReturnPressed(const QModelIndex &index)
0063 {
0064     // we cleared the model to checkout new branch
0065     if (m_model->rowCount() == 0) {
0066         createNewBranch(m_lineEdit.text(), m_checkoutFromBranchName);
0067         return;
0068     }
0069 
0070     if (!index.isValid()) {
0071         clearLineEdit();
0072         hide();
0073         return;
0074     }
0075 
0076     // branch is selected, do actual checkout
0077     if (m_checkingOutFromBranch) {
0078         m_checkingOutFromBranch = false;
0079         const auto fromBranch = index.data(BranchesDialogModel::CheckoutName).toString();
0080         m_checkoutFromBranchName = fromBranch;
0081         m_model->clear();
0082         clearLineEdit();
0083         m_lineEdit.setPlaceholderText(i18n("Enter new branch name. Press 'Esc' to cancel."));
0084         return;
0085     }
0086 
0087     const auto branch = index.data(BranchesDialogModel::CheckoutName).toString();
0088     const auto itemType = (BranchesDialogModel::ItemType)index.data(BranchesDialogModel::ItemTypeRole).toInt();
0089 
0090     if (itemType == BranchesDialogModel::BranchItem) {
0091         QFuture<GitUtils::CheckoutResult> future = QtConcurrent::run(&GitUtils::checkoutBranch, m_projectPath, branch);
0092         m_checkoutWatcher.setFuture(future);
0093     } else if (itemType == BranchesDialogModel::CreateBranch) {
0094         m_model->clear();
0095         m_lineEdit.setPlaceholderText(i18n("Enter new branch name. Press 'Esc' to cancel."));
0096         return;
0097     } else if (itemType == BranchesDialogModel::CreateBranchFrom) {
0098         m_model->clearBranchCreationItems();
0099         clearLineEdit();
0100         m_lineEdit.setPlaceholderText(i18n("Select branch to checkout from. Press 'Esc' to cancel."));
0101         m_checkingOutFromBranch = true;
0102         return;
0103     }
0104 
0105     clearLineEdit();
0106     hide();
0107 }
0108 
0109 void BranchCheckoutDialog::createNewBranch(const QString &branch, const QString &fromBranch)
0110 {
0111     if (branch.isEmpty()) {
0112         clearLineEdit();
0113         hide();
0114         return;
0115     }
0116 
0117     // the branch name might be invalid, let git handle it
0118     const GitUtils::CheckoutResult r = GitUtils::checkoutNewBranch(m_projectPath, branch, fromBranch);
0119     const bool warn = true;
0120     if (r.returnCode == 0) {
0121         sendMessage(i18n("Checked out to new branch: %1", r.branch), !warn);
0122     } else {
0123         sendMessage(i18n("Failed to create new branch. Error \"%1\"", r.error), warn);
0124     }
0125 
0126     clearLineEdit();
0127     hide();
0128 }
0129 
0130 #include "moc_branchcheckoutdialog.cpp"