File: branchmanager.cpp

package info (click to toggle)
kdevelop 4%3A5.6.2-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 57,892 kB
  • sloc: cpp: 278,773; javascript: 3,558; python: 3,385; sh: 1,317; ansic: 689; xml: 273; php: 95; makefile: 40; lisp: 13; sed: 12
file content (288 lines) | stat: -rw-r--r-- 11,574 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/***************************************************************************
 *   Copyright 2008 Evgeniy Ivanov <powerfox@kde.ru>                       *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or         *
 *   modify it under the terms of the GNU General Public License as        *
 *   published by the Free Software Foundation; either version 2 of        *
 *   the License or (at your option) version 3 or any later version        *
 *   accepted by the membership of KDE e.V. (or its successor approved     *
 *   by the membership of KDE e.V.), which shall act as a proxy            *
 *   defined in Section 14 of version 3 of the license.                    *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
 ***************************************************************************/

#include "branchmanager.h"

#include <QInputDialog>

#include <KMessageBox>
#include <KLocalizedString>

#include "../dvcsplugin.h"
#include <vcs/vcsjob.h>
#include <vcs/models/brancheslistmodel.h>
#include "ui_branchmanager.h"
#include "debug.h"
#include "widgets/vcsdiffpatchsources.h"

#include <interfaces/icore.h>
#include <interfaces/iruncontroller.h>
#include <interfaces/iuicontroller.h>
#include <QDialogButtonBox>
#include <QPushButton>
#include <QVBoxLayout>
#include <QSortFilterProxyModel>
#include <KParts/MainWindow>

using namespace KDevelop;

BranchManager::BranchManager(const QString& repository, KDevelop::DistributedVersionControlPlugin* executor, QWidget *parent)
    : QDialog(parent)
    , m_repository(repository)
    , m_dvcPlugin(executor)
{
    setWindowTitle(i18nc("@title:window", "Branch Manager"));

    auto* mainWidget = new QWidget(this);
    auto *mainLayout = new QVBoxLayout(this);
    mainLayout->addWidget(mainWidget);

    m_ui = new Ui::BranchDialogBase;
    auto* w = new QWidget(this);
    m_ui->setupUi(w);
    mainLayout->addWidget(w);

    auto* buttonBox = new QDialogButtonBox(QDialogButtonBox::Close);
    connect(buttonBox, &QDialogButtonBox::accepted, this, &BranchManager::accept);
    connect(buttonBox, &QDialogButtonBox::rejected, this, &BranchManager::reject);
    mainLayout->addWidget(buttonBox);

    m_model = new BranchesListModel(this);
    m_model->initialize(m_dvcPlugin, QUrl::fromLocalFile(repository));

    // Filter Model
    m_filterModel = new QSortFilterProxyModel();
    m_filterModel->setSourceModel(m_model);
    m_filterModel->setFilterWildcard(QString());
    m_filterModel->sort(0, Qt::AscendingOrder);

    //Changes in filter edit trigger filtering
    connect(m_ui->branchFilterEdit,
            &QLineEdit::textChanged,
            m_filterModel,
            &QSortFilterProxyModel::setFilterWildcard);

    m_ui->branchView->setModel(m_filterModel);

    QString branchName = m_model->currentBranch();
    // apply initial selection
    QList< QStandardItem* > items = m_model->findItems(branchName);
    if (!items.isEmpty()) {
        m_ui->branchView->setCurrentIndex(items.first()->index());
    }

    connect(m_ui->newButton, &QPushButton::clicked, this, &BranchManager::createBranch);
    connect(m_ui->deleteButton, &QPushButton::clicked, this, &BranchManager::deleteBranch);
    connect(m_ui->renameButton, &QPushButton::clicked, this, &BranchManager::renameBranch);
    connect(m_ui->checkoutButton, &QPushButton::clicked, this, &BranchManager::checkoutBranch);

    // checkout branch on double-click
    connect(m_ui->branchView, &QListView::doubleClicked, this, &BranchManager::checkoutBranch);

    connect(m_ui->mergeButton, &QPushButton::clicked, this, &BranchManager::mergeBranch);
    connect(m_ui->diffButton, &QPushButton::clicked, this, &BranchManager::diffFromBranch);
}

BranchManager::~BranchManager()
{
    delete m_ui;
}

void BranchManager::createBranch()
{
    const QModelIndex currentBranchIdx = m_ui->branchView->currentIndex();
    if (!currentBranchIdx.isValid()) {
        KMessageBox::messageBox(this, KMessageBox::Error,
                                i18n("You must select a base branch from the list before creating a new branch."));
        return;
    }
    QString baseBranch = currentBranchIdx.data().toString();
    bool branchNameEntered = false;
    QString newBranch = QInputDialog::getText(this, i18nc("@title:window", "New Branch"), i18nc("@label:textbox", "Name of the new branch:"),
            QLineEdit::Normal, QString(), &branchNameEntered);
    if (!branchNameEntered)
        return;

    if (!m_model->findItems(newBranch).isEmpty())
    {
        KMessageBox::messageBox(this, KMessageBox::Sorry,
                                i18n("Branch \"%1\" already exists.\n"
                                        "Please, choose another name.",
                                        newBranch));
    }
    else
        m_model->createBranch(baseBranch, newBranch);
}

void BranchManager::deleteBranch()
{
    QString baseBranch = m_ui->branchView->selectionModel()->selection().indexes().first().data().toString();

    if (baseBranch == m_model->currentBranch())
    {
        KMessageBox::messageBox(this, KMessageBox::Sorry,
                                    i18n("Currently at the branch \"%1\".\n"
                                            "To remove it, please change to another branch.",
                                            baseBranch));
        return;
    }

    int ret = KMessageBox::messageBox(this, KMessageBox::WarningYesNo,
                                      i18n("Are you sure you want to irreversibly remove the branch '%1'?", baseBranch));
    if (ret == KMessageBox::Yes)
        m_model->removeBranch(baseBranch);
}

void BranchManager::renameBranch()
{
    QModelIndex currentIndex = m_ui->branchView->currentIndex();
    if (!currentIndex.isValid())
        return;

    m_ui->branchView->edit(currentIndex);
}

void BranchManager::checkoutBranch()
{
    QString branch = m_ui->branchView->currentIndex().data().toString();
    if (branch == m_model->currentBranch())
    {
        KMessageBox::messageBox(this, KMessageBox::Sorry,
                                i18n("Already on branch \"%1\"\n",
                                        branch));
        return;
    }

    qCDebug(VCS) << "Switching to" << branch << "in" << m_repository;
    KDevelop::VcsJob *branchJob = m_dvcPlugin->switchBranch(QUrl::fromLocalFile(m_repository), branch);
//     connect(branchJob, SIGNAL(finished(KJob*)), m_model, SIGNAL(resetCurrent()));

    ICore::self()->runController()->registerJob(branchJob);
    close();
}

void BranchManager::mergeBranch()
{
    const QModelIndex branchToMergeIdx = m_ui->branchView->currentIndex();

    if (branchToMergeIdx.isValid()) {
        QString branchToMerge = branchToMergeIdx.data().toString();

        if (m_model->findItems(branchToMerge).isEmpty()) {
            KMessageBox::messageBox(this, KMessageBox::Sorry, i18n("Branch \"%1\" doesn't exists.\n"
                                                                   "Please, choose another name.",
                                                                   branchToMerge));
        } else {
            KDevelop::VcsJob* branchJob = m_dvcPlugin->mergeBranch(QUrl::fromLocalFile(m_repository), branchToMerge);
            ICore::self()->runController()->registerJob(branchJob);
            close();
        }
    } else {
        KMessageBox::messageBox(this, KMessageBox::Error,
                                i18n("You must select a branch to merge into current one from the list."));
    }
}

// adapted from VCSStandardDiffUpdater
class VCSBranchDiffUpdater : public VCSDiffUpdater {
public:
    VCSBranchDiffUpdater(const QString& repo, const QString& src, KDevelop::DistributedVersionControlPlugin* vcs);
    ~VCSBranchDiffUpdater() override;
    KDevelop::VcsDiff update() const override;
    KDevelop::IBasicVersionControl* vcs() const override { return m_vcs; }
    QUrl url() const override { return QUrl::fromLocalFile(m_repository); }
private:
    const QString m_repository;
    const QString m_src;
    KDevelop::DistributedVersionControlPlugin* m_vcs;
};

VCSBranchDiffUpdater::VCSBranchDiffUpdater(const QString& repo, const QString& src,
        KDevelop::DistributedVersionControlPlugin* vcs)
    : m_repository(repo)
    , m_src(src)
    , m_vcs(vcs)
{
}

VCSBranchDiffUpdater::~VCSBranchDiffUpdater() {
}

VcsDiff VCSBranchDiffUpdater::update() const
{
    VcsRevision srcRev;
    srcRev.setRevisionValue(m_src, KDevelop::VcsRevision::GlobalNumber);
    // see comment in BranchManager::diffFromBranch()
    const auto destRev = VcsRevision::createSpecialRevision(KDevelop::VcsRevision::Working);
    QScopedPointer<VcsJob> diffJob(m_vcs->diff(QUrl::fromLocalFile(m_repository), srcRev, destRev));
    const bool success = diffJob ? diffJob->exec() : false;
    if (!success) {
        KMessageBox::error(nullptr, i18n("Could not create a patch for the current version."));
        return {};
    }

    return diffJob->fetchResults().value<VcsDiff>();
}

void BranchManager::diffFromBranch()
{
    const auto dest = m_model->currentBranch();
    const auto src = m_ui->branchView->currentIndex().data().toString();
    if (src == dest) {
        KMessageBox::messageBox(this, KMessageBox::Information, i18n("Already on branch \"%1\"\n", src));
        return;
    }

    VcsRevision srcRev;
    srcRev.setRevisionValue(src, KDevelop::VcsRevision::GlobalNumber);
    // We have two options here:
    // * create a regular VcsRevision to represent the last commit on the current branch or
    // * create a special branch to reflect the staging area. I chose this one.
    // If the staging area is clean it automatically defaults to the first option.
    const auto destRev = VcsRevision::createSpecialRevision(KDevelop::VcsRevision::Working);
    const auto job = m_dvcPlugin->diff(QUrl::fromLocalFile(m_repository), srcRev, destRev);
    connect(job, &VcsJob::finished, this, &BranchManager::diffJobFinished);
    m_dvcPlugin->core()->runController()->registerJob(job);
}

void BranchManager::diffJobFinished(KJob* job)
{
    auto vcsjob = qobject_cast<KDevelop::VcsJob*>(job);
    Q_ASSERT(vcsjob);

    if (vcsjob->status() != KDevelop::VcsJob::JobSucceeded) {
        KMessageBox::error(ICore::self()->uiController()->activeMainWindow(), vcsjob->errorString(),
                           i18nc("@titlew:indow", "Unable to Retrieve Diff"));
        return;
    }

    auto diff = vcsjob->fetchResults().value<KDevelop::VcsDiff>();
    if(diff.isEmpty()){
        KMessageBox::information(ICore::self()->uiController()->activeMainWindow(),
                                    i18n("There are no committed differences."),
                                    i18nc("@title:window", "VCS Support"));
        return;
    }

    auto patch = new VCSDiffPatchSource(new VCSBranchDiffUpdater(m_repository,
        m_ui->branchView->currentIndex().data().toString(), m_dvcPlugin));
    showVcsDiff(patch);
    close();
}