File indexing completed on 2025-01-05 05:14:50

0001 /*
0002 SPDX-FileCopyrightText: 2021 Hamed Masafi <hamed.masfi@gmail.com>
0003 
0004 SPDX-License-Identifier: GPL-3.0-or-later
0005 */
0006 
0007 #include "switchbranchdialog.h"
0008 #include "commands/commandswitchbranch.h"
0009 #include "gitmanager.h"
0010 #include "libkommitwidgets_appdebug.h"
0011 
0012 #include <QDebug>
0013 
0014 #include <KLocalizedString>
0015 #include <KMessageBox>
0016 
0017 #define BRANCH_TYPE_LOCAL 1
0018 #define BRANCH_TYPE_REMOTE 2
0019 
0020 SwitchBranchDialog::SwitchBranchDialog(Git::Manager *git, QWidget *parent)
0021     : AppDialog(git, parent)
0022 {
0023     setupUi(this);
0024 
0025     _existingLocalBranches = git->branchesNames(Git::Manager::BranchType::LocalBranch);
0026     for (const auto &b : std::as_const(_existingLocalBranches))
0027         comboBoxBranchSelect->addItem(b, BRANCH_TYPE_LOCAL);
0028     _existingRemoteBranches = git->branchesNames(Git::Manager::BranchType::RemoteBranch);
0029     for (const auto &b : std::as_const(_existingRemoteBranches))
0030         comboBoxBranchSelect->addItem(b, BRANCH_TYPE_REMOTE);
0031 
0032     comboBoxTags->addItems(git->tagsNames());
0033     // TODO: comboBoxTags->setModel(git->tagsModel());
0034 
0035     radioButtonTag->setEnabled(comboBoxTags->count());
0036 
0037     connect(buttonBox, &QDialogButtonBox::accepted, this, &SwitchBranchDialog::slotButtonBoxAccepted);
0038 }
0039 
0040 Git::CommandSwitchBranch *SwitchBranchDialog::command() const
0041 {
0042     auto cmd = new Git::CommandSwitchBranch(mGit);
0043 
0044     if (radioButtonExistingBranch->isChecked()) {
0045         if (comboBoxBranchSelect->currentData().toInt() == BRANCH_TYPE_LOCAL) {
0046             cmd->setTarget(comboBoxBranchSelect->currentText());
0047             cmd->setMode(Git::CommandSwitchBranch::ExistingBranch);
0048         } else {
0049             const auto n = comboBoxBranchSelect->currentText().indexOf(QLatin1Char('/'));
0050             if (n == -1) {
0051                 qCWarning(KOMMIT_WIDGETS_LOG, "The branch name %s is invalid", qPrintable(comboBoxBranchSelect->currentText()));
0052                 cmd->deleteLater();
0053                 return nullptr;
0054             }
0055 
0056             cmd->setRemoteBranch(comboBoxBranchSelect->currentText().mid(0, n));
0057             cmd->setTarget(comboBoxBranchSelect->currentText().mid(n + 1));
0058             cmd->setMode(Git::CommandSwitchBranch::RemoteBranch);
0059         }
0060     } else if (radioButtonTag->isChecked()) {
0061         cmd->setMode(Git::CommandSwitchBranch::Tag);
0062         cmd->setTarget(comboBoxTags->currentText());
0063     } else {
0064         cmd->setMode(Git::CommandSwitchBranch::NewBranch);
0065         cmd->setTarget(lineEditNewBranchName->text());
0066     }
0067 
0068     return cmd;
0069 }
0070 
0071 void SwitchBranchDialog::slotButtonBoxAccepted()
0072 {
0073     if (radioButtonExistingBranch->isChecked())
0074         if (_existingLocalBranches.contains(lineEditNewBranchName->text()) || _existingRemoteBranches.contains(lineEditNewBranchName->text())) {
0075             KMessageBox::error(this, i18n("The branch already exists"));
0076             return;
0077         }
0078     accept();
0079 }
0080 
0081 #include "moc_switchbranchdialog.cpp"