File indexing completed on 2025-01-05 05:14:47
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 "branchactions.h" 0008 0009 #include <KLocalizedString> 0010 #include <KMessageBox> 0011 0012 #include "libkommitwidgets_appdebug.h" 0013 #include <QDebug> 0014 #include <QInputDialog> 0015 0016 #include <commands/commandmerge.h> 0017 #include <entities/branch.h> 0018 #include <gitmanager.h> 0019 0020 #include "core/kmessageboxhelper.h" 0021 #include "dialogs/fetchdialog.h" 0022 #include "dialogs/filestreedialog.h" 0023 #include "dialogs/mergedialog.h" 0024 #include "dialogs/notedialog.h" 0025 #include "dialogs/runnerdialog.h" 0026 #include "models/branchesmodel.h" 0027 #include "windows/diffwindow.h" 0028 0029 BranchActions::BranchActions(Git::Manager *git, QWidget *parent) 0030 : AbstractActions(git, parent) 0031 { 0032 _actionCreate = addActionHidden(i18n("Create..."), this, &BranchActions::create); 0033 _actionCreate->setIcon(QIcon::fromTheme(QStringLiteral("document-new"))); 0034 0035 _actionFetch = addActionDisabled(i18n("Fetch..."), this, &BranchActions::fetch); 0036 _actionBrowse = addActionDisabled(i18n("Browse..."), this, &BranchActions::browse); 0037 _actionCheckout = addActionDisabled(i18n("Switch"), this, &BranchActions::checkout); 0038 _actionMerge = addActionDisabled(i18n("Merge..."), this, &BranchActions::merge); 0039 _actionDiff = addActionDisabled(i18n("Diff"), this, &BranchActions::diff); 0040 _actionRemove = addActionDisabled(i18n("Remove..."), this, &BranchActions::remove); 0041 _actionNote = addActionDisabled(i18n("Note..."), this, &BranchActions::note); 0042 } 0043 0044 void BranchActions::setBranchName(const QSharedPointer<Git::Branch> &newBranchName) 0045 { 0046 mBranchName = newBranchName; 0047 0048 setActionEnabled(_actionFetch, true); 0049 setActionEnabled(_actionBrowse, true); 0050 setActionEnabled(_actionCheckout, true); 0051 setActionEnabled(_actionMerge, true); 0052 setActionEnabled(_actionDiff, true); 0053 setActionEnabled(_actionRemove, true); 0054 setActionEnabled(_actionNote, true); 0055 } 0056 0057 void BranchActions::setOtherBranch(const QSharedPointer<Git::Branch> &newOtherBranch) 0058 { 0059 mOtherBranch = newOtherBranch; 0060 } 0061 0062 void BranchActions::fetch() 0063 { 0064 FetchDialog d(mGit, mParent); 0065 d.setBranch(mBranchName->name()); 0066 d.exec(); 0067 } 0068 0069 void BranchActions::create() 0070 { 0071 const auto newBranchName = QInputDialog::getText(mParent, i18n("Create new branch"), i18n("Branch name")); 0072 0073 if (!newBranchName.isEmpty()) { 0074 mGit->runGit({QStringLiteral("checkout"), QStringLiteral("-b"), newBranchName}); 0075 mGit->branchesModel()->load(); 0076 } 0077 } 0078 0079 void BranchActions::browse() 0080 { 0081 FilesTreeDialog d(mGit, mBranchName->name(), mParent); 0082 d.exec(); 0083 } 0084 0085 void BranchActions::checkout() 0086 { 0087 RunnerDialog d(mGit, mParent); 0088 d.run({QStringLiteral("checkout"), mBranchName->name()}); 0089 d.exec(); 0090 } 0091 0092 void BranchActions::diff() 0093 { 0094 QString branchToDiff = mOtherBranch->name(); 0095 0096 if (branchToDiff.isEmpty()) { 0097 auto currentBranch = mGit->currentBranch(); 0098 0099 if (currentBranch != branchToDiff) { 0100 branchToDiff = currentBranch; 0101 } else { 0102 auto branches = mGit->branchesNames(Git::Manager::BranchType::LocalBranch); 0103 if (branches.contains(QStringLiteral("master"))) 0104 branchToDiff = QStringLiteral("master"); 0105 else if (branches.contains(QStringLiteral("main"))) 0106 branchToDiff = QStringLiteral("main"); 0107 } 0108 if (branchToDiff.isEmpty()) { 0109 qCWarning(KOMMIT_WIDGETS_LOG()) << "Main branch is not set to diff"; 0110 return; 0111 } 0112 } 0113 0114 auto d = new DiffWindow(mGit, branchToDiff, mBranchName->name()); 0115 d->showModal(); 0116 } 0117 0118 void BranchActions::remove() 0119 { 0120 if (KMessageBoxHelper::removeQuestion(mParent, i18n("Are you sure to remove the selected branch?"), i18n("Remove Branch"))) { 0121 if (!mGit->removeBranch(mBranchName->name())) 0122 KMessageBox::information(mParent, i18n("Unable to remove the selected branch")); 0123 else 0124 mGit->branchesModel()->load(); 0125 } 0126 } 0127 0128 void BranchActions::merge() 0129 { 0130 MergeDialog d{mGit, mBranchName->name(), mParent}; 0131 if (d.exec() == QDialog::Accepted) { 0132 auto cmd = d.command(); 0133 RunnerDialog runner(mGit, mParent); 0134 runner.run(cmd); 0135 runner.exec(); 0136 } 0137 } 0138 0139 void BranchActions::note() 0140 { 0141 NoteDialog d{mGit, mBranchName->name(), mParent}; 0142 d.exec(); 0143 } 0144 0145 #include "moc_branchactions.cpp"