vcsmanager.cpp 15.9 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
con's avatar
con committed
2
**
3
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
con's avatar
con committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
con's avatar
con committed
7
**
hjk's avatar
hjk committed
8
9
10
11
12
13
14
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
15
**
16
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
18
19
20
21
22
23
24
25
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
con's avatar
con committed
26
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
hjk's avatar
hjk committed
29

con's avatar
con committed
30
31
#include "vcsmanager.h"
#include "iversioncontrol.h"
32
#include "icore.h"
33
#include "documentmanager.h"
34
35
36
#include "idocument.h"
#include "infobar.h"

37
38
39
#include <coreplugin/dialogs/addtovcsdialog.h>
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/editormanager/ieditor.h>
con's avatar
con committed
40

41
#include <vcsbase/vcsbaseconstants.h>
con's avatar
con committed
42
#include <extensionsystem/pluginmanager.h>
43
#include <utils/qtcassert.h>
con's avatar
con committed
44

45
46
47
48
#include <QDir>
#include <QString>
#include <QList>
#include <QMap>
con's avatar
con committed
49

50
51
#include <QFileInfo>
#include <QMessageBox>
con's avatar
con committed
52
53
54

namespace Core {

55
56
57
58
typedef QList<IVersionControl *> VersionControlList;

static inline VersionControlList allVersionControls()
{
59
    return ExtensionSystem::PluginManager::getObjects<IVersionControl>();
60
61
}

62
63
64
// ---- VCSManagerPrivate:
// Maintains a cache of top-level directory->version control.

hjk's avatar
hjk committed
65
66
67
class VcsManagerPrivate
{
public:
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
    class VcsInfo {
    public:
        VcsInfo(IVersionControl *vc, const QString &tl) :
            versionControl(vc), topLevel(tl)
        { }

        bool operator == (const VcsInfo &other) const
        {
            return versionControl == other.versionControl &&
                    topLevel == other.topLevel;
        }

        IVersionControl *versionControl;
        QString topLevel;
    };

84
85
86
    VcsManagerPrivate() : m_unconfiguredVcs(0)
    { }

87
88
89
90
91
    ~VcsManagerPrivate()
    {
        qDeleteAll(m_vcsInfoList);
    }

92
    VcsInfo *findInCache(const QString &dir)
93
    {
94
95
96
        QTC_ASSERT(QDir(dir).isAbsolute(), return 0);
        QTC_ASSERT(!dir.endsWith(QLatin1Char('/')), return 0);
        QTC_ASSERT(QDir::fromNativeSeparators(dir) == dir, return 0);
97
98

        const QMap<QString, VcsInfo *>::const_iterator it = m_cachedMatches.constFind(dir);
99
100
101
102
103
104
105
106
        if (it != m_cachedMatches.constEnd())
            return it.value();
        return 0;
    }

    VcsInfo *findUpInCache(const QString &directory)
    {
        VcsInfo *result = 0;
107
        const QChar slash = QLatin1Char('/');
108
109
        // Split the path, trying to find the matching repository. We start from the reverse
        // in order to detected nested repositories correctly (say, a git checkout under SVN).
110
        for (int pos = directory.size() - 1; pos >= 0; pos = directory.lastIndexOf(slash, pos) - 1) {
111
112
113
114
115
116
117
118
            const QString directoryPart = directory.left(pos);
            result = findInCache(directoryPart);
            if (result != 0)
                break;
        }
        return result;
    }

119
120
121
122
123
    void clearCache()
    {
        m_cachedMatches.clear();
    }

Tobias Hunger's avatar
Tobias Hunger committed
124
    void resetCache(const QString &dir)
125
    {
Tobias Hunger's avatar
Tobias Hunger committed
126
127
128
129
        QTC_ASSERT(QDir(dir).isAbsolute(), return);
        QTC_ASSERT(!dir.endsWith(QLatin1Char('/')), return);
        QTC_ASSERT(QDir::fromNativeSeparators(dir) == dir, return);

130
131
132
133
134
135
136
        const QString dirSlash = dir + QLatin1Char('/');
        foreach (const QString &key, m_cachedMatches.keys()) {
            if (key == dir || key.startsWith(dirSlash))
                m_cachedMatches.remove(key);
        }
    }

Tobias Hunger's avatar
Tobias Hunger committed
137
    void cache(IVersionControl *vc, const QString &topLevel, const QString &dir)
138
    {
Tobias Hunger's avatar
Tobias Hunger committed
139
140
141
        QTC_ASSERT(QDir(dir).isAbsolute(), return);
        QTC_ASSERT(!dir.endsWith(QLatin1Char('/')), return);
        QTC_ASSERT(QDir::fromNativeSeparators(dir) == dir, return);
Tobias Hunger's avatar
Tobias Hunger committed
142
143
144
        QTC_ASSERT(dir.startsWith(topLevel + QLatin1Char('/'))
                   || topLevel == dir || topLevel.isEmpty(), return);
        QTC_ASSERT((topLevel.isEmpty() && !vc) || (!topLevel.isEmpty() && vc), return);
Tobias Hunger's avatar
Tobias Hunger committed
145

146
147
148
        VcsInfo *newInfo = new VcsInfo(vc, topLevel);
        bool createdNewInfo(true);
        // Do we have a matching VcsInfo already?
149
        foreach (VcsInfo *i, m_vcsInfoList) {
150
151
152
153
154
155
156
157
158
159
            if (*i == *newInfo) {
                delete newInfo;
                newInfo = i;
                createdNewInfo = false;
                break;
            }
        }
        if (createdNewInfo)
            m_vcsInfoList.append(newInfo);

160
        QString tmpDir = dir;
161
        const QChar slash = QLatin1Char('/');
162
        while (tmpDir.count() >= topLevel.count() && !tmpDir.isEmpty()) {
163
            m_cachedMatches.insert(tmpDir, newInfo);
164
165
166
167
            // if no vc was found, this might mean we're inside a repo internal directory (.git)
            // Cache only input directory, not parents
            if (!vc)
                break;
168
            const int slashPos = tmpDir.lastIndexOf(slash);
169
            if (slashPos >= 0)
170
                tmpDir.truncate(slashPos);
171
            else
172
                tmpDir.clear();
173
174
175
176
177
        }
    }

    QMap<QString, VcsInfo *> m_cachedMatches;
    QList<VcsInfo *> m_vcsInfoList;
178
    IVersionControl *m_unconfiguredVcs;
con's avatar
con committed
179
180
};

hjk's avatar
hjk committed
181
182
static VcsManagerPrivate *d = 0;
static VcsManager *m_instance = 0;
183

hjk's avatar
hjk committed
184
VcsManager::VcsManager(QObject *parent) :
185
   QObject(parent)
con's avatar
con committed
186
{
hjk's avatar
hjk committed
187
    m_instance = this;
188
    d = new VcsManagerPrivate;
con's avatar
con committed
189
190
}

191
192
// ---- VCSManager:

hjk's avatar
hjk committed
193
VcsManager::~VcsManager()
con's avatar
con committed
194
{
hjk's avatar
hjk committed
195
    m_instance = 0;
hjk's avatar
hjk committed
196
    delete d;
con's avatar
con committed
197
198
}

hjk's avatar
hjk committed
199
200
201
202
203
QObject *VcsManager::instance()
{
    return m_instance;
}

hjk's avatar
hjk committed
204
void VcsManager::extensionsInitialized()
205
206
207
208
{
    // Change signal connections
    foreach (IVersionControl *versionControl, allVersionControls()) {
        connect(versionControl, SIGNAL(filesChanged(QStringList)),
209
                DocumentManager::instance(), SIGNAL(filesChangedInternally(QStringList)));
210
        connect(versionControl, SIGNAL(repositoryChanged(QString)),
hjk's avatar
hjk committed
211
                m_instance, SIGNAL(repositoryChanged(QString)));
212
213
214
    }
}

Sami Tikka's avatar
Sami Tikka committed
215
216
217
218
219
static bool longerThanPath(QPair<QString, IVersionControl *> &pair1, QPair<QString, IVersionControl *> &pair2)
{
    return pair1.first.size() > pair2.first.size();
}

220
221
222
223
224
225
226
227
void VcsManager::resetVersionControlForDirectory(const QString &inputDirectory)
{
    if (inputDirectory.isEmpty())
        return;

    const QString directory = QDir(inputDirectory).absolutePath();

    d->resetCache(directory);
hjk's avatar
hjk committed
228
    emit m_instance->repositoryChanged(directory);
229
230
}

231
IVersionControl* VcsManager::findVersionControlForDirectory(const QString &inputDirectory,
232
                                                            QString *topLevelDirectory)
con's avatar
con committed
233
{
234
235
    typedef QPair<QString, IVersionControl *> StringVersionControlPair;
    typedef QList<StringVersionControlPair> StringVersionControlPairs;
236
    if (inputDirectory.isEmpty())
237
        return 0;
238

239
240
    // Make sure we an absolute path:
    const QString directory = QDir(inputDirectory).absolutePath();
241

hjk's avatar
hjk committed
242
    VcsManagerPrivate::VcsInfo *cachedData = d->findInCache(directory);
243
    if (cachedData) {
244
        if (topLevelDirectory)
245
246
            *topLevelDirectory = cachedData->topLevel;
        return cachedData->versionControl;
con's avatar
con committed
247
248
    }

249
    // Nothing: ask the IVersionControls directly.
250
    const VersionControlList versionControls = allVersionControls();
251
    StringVersionControlPairs allThatCanManage;
Sami Tikka's avatar
Sami Tikka committed
252

hjk's avatar
hjk committed
253
    foreach (IVersionControl * versionControl, versionControls) {
Tobias Hunger's avatar
Tobias Hunger committed
254
        QString topLevel;
255
        if (versionControl->managesDirectory(directory, &topLevel))
256
            allThatCanManage.push_back(StringVersionControlPair(topLevel, versionControl));
con's avatar
con committed
257
    }
Sami Tikka's avatar
Sami Tikka committed
258
259
260
261
262

    // To properly find a nested repository (say, git checkout inside SVN),
    // we need to select the version control with the longest toplevel pathname.
    qSort(allThatCanManage.begin(), allThatCanManage.end(), longerThanPath);

263
    if (allThatCanManage.isEmpty()) {
hjk's avatar
hjk committed
264
        d->cache(0, QString(), directory); // register that nothing was found!
265
266

        // report result;
Sami Tikka's avatar
Sami Tikka committed
267
        if (topLevelDirectory)
268
269
            topLevelDirectory->clear();
        return 0;
Sami Tikka's avatar
Sami Tikka committed
270
    }
271
272

    // Register Vcs(s) with the cache
273
    QString tmpDir = QFileInfo(directory).canonicalFilePath();
274
275
276
277
278
279
    // directory might refer to a historical directory which doesn't exist.
    // In this case, don't cache it.
    if (!tmpDir.isEmpty()) {
        const QChar slash = QLatin1Char('/');
        const StringVersionControlPairs::const_iterator cend = allThatCanManage.constEnd();
        for (StringVersionControlPairs::const_iterator i = allThatCanManage.constBegin(); i != cend; ++i) {
280
281
282
            // If topLevel was already cached for another VC, skip this one
            if (tmpDir.count() < i->first.count())
                continue;
283
284
285
286
287
288
            d->cache(i->second, i->first, tmpDir);
            tmpDir = i->first;
            const int slashPos = tmpDir.lastIndexOf(slash);
            if (slashPos >= 0)
                tmpDir.truncate(slashPos);
        }
289
290
291
292
293
    }

    // return result
    if (topLevelDirectory)
        *topLevelDirectory = allThatCanManage.first().first;
294
    IVersionControl *versionControl = allThatCanManage.first().second;
295
296
297
    const bool isVcsConfigured = versionControl->isConfigured();
    if (!isVcsConfigured || d->m_unconfiguredVcs) {
        Id vcsWarning("VcsNotConfiguredWarning");
298
        IDocument *curDocument = EditorManager::currentDocument();
299
300
301
302
        if (isVcsConfigured) {
            if (curDocument && d->m_unconfiguredVcs == versionControl) {
                curDocument->infoBar()->removeInfo(vcsWarning);
                d->m_unconfiguredVcs = 0;
303
            }
304
305
            return versionControl;
        } else {
306
307
            InfoBar *infoBar = curDocument ? curDocument->infoBar() : 0;
            if (infoBar && infoBar->canInfoBeAdded(vcsWarning)) {
308
309
310
311
312
                InfoBarEntry info(vcsWarning,
                                  tr("%1 repository was detected but %1 is not configured.")
                                  .arg(versionControl->displayName()),
                                  InfoBarEntry::GlobalSuppressionEnabled);
                d->m_unconfiguredVcs = versionControl;
hjk's avatar
hjk committed
313
                info.setCustomButtonInfo(tr("Configure"), m_instance, SLOT(configureVcs()));
314
315
316
                infoBar->addInfo(info);
            }
            return 0;
317
318
319
        }
    }
    return versionControl;
con's avatar
con committed
320
321
}

hjk's avatar
hjk committed
322
QStringList VcsManager::repositories(const IVersionControl *vc)
323
324
325
326
327
328
329
330
{
    QStringList result;
    foreach (const VcsManagerPrivate::VcsInfo *vi, d->m_vcsInfoList)
        if (vi->versionControl == vc)
            result.push_back(vi->topLevel);
    return result;
}

hjk's avatar
hjk committed
331
bool VcsManager::promptToDelete(const QString &fileName)
con's avatar
con committed
332
{
333
334
335
336
337
    if (IVersionControl *vc = findVersionControlForDirectory(QFileInfo(fileName).absolutePath()))
        return promptToDelete(vc, fileName);
    return true;
}

hjk's avatar
hjk committed
338
IVersionControl *VcsManager::checkout(const QString &versionControlType,
339
340
341
                                      const QString &directory,
                                      const QByteArray &url)
{
Tobias Hunger's avatar
Tobias Hunger committed
342
343
344
345
    foreach (IVersionControl *versionControl, allVersionControls()) {
        if (versionControl->displayName() == versionControlType
            && versionControl->supportsOperation(Core::IVersionControl::CheckoutOperation)) {
            if (versionControl->vcsCheckout(directory, url)) {
hjk's avatar
hjk committed
346
                d->cache(versionControl, directory, directory);
347
348
349
350
351
352
353
354
                return versionControl;
            }
            return 0;
        }
    }
    return 0;
}

hjk's avatar
hjk committed
355
bool VcsManager::findVersionControl(const QString &versionControlType)
356
357
{
    foreach (IVersionControl * versionControl, allVersionControls()) {
Tobias Hunger's avatar
Tobias Hunger committed
358
        if (versionControl->displayName() == versionControlType)
359
360
361
362
363
            return true;
    }
    return false;
}

hjk's avatar
hjk committed
364
QString VcsManager::repositoryUrl(const QString &directory)
365
366
367
368
369
370
371
372
{
    IVersionControl *vc = findVersionControlForDirectory(directory);

    if (vc && vc->supportsOperation(Core::IVersionControl::GetRepositoryRootOperation))
       return vc->vcsGetRepositoryURL(directory);
    return QString();
}

hjk's avatar
hjk committed
373
bool VcsManager::promptToDelete(IVersionControl *vc, const QString &fileName)
374
{
375
    QTC_ASSERT(vc, return true);
376
    if (!vc->supportsOperation(IVersionControl::DeleteOperation))
377
        return true;
hjk's avatar
hjk committed
378
379
380
    const QString title = tr("Version Control");
    const QString msg = tr("Would you like to remove this file from the version control system (%1)?\n"
                           "Note: This might remove the local file.").arg(vc->displayName());
con's avatar
con committed
381
382
    const QMessageBox::StandardButton button =
        QMessageBox::question(0, title, msg, QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
383
384
385
    if (button != QMessageBox::Yes)
        return true;
    return vc->vcsDelete(fileName);
con's avatar
con committed
386
387
}

Friedemann Kleint's avatar
Friedemann Kleint committed
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
QString VcsManager::msgAddToVcsTitle()
{
    return tr("Add to Version Control");
}

QString VcsManager::msgPromptToAddToVcs(const QStringList &files, const IVersionControl *vc)
{
    return files.size() == 1
        ? tr("Add the file\n%1\nto version control (%2)?")
              .arg(files.front(), vc->displayName())
        : tr("Add the files\n%1\nto version control (%2)?")
              .arg(files.join(QString(QLatin1Char('\n'))), vc->displayName());
}

QString VcsManager::msgAddToVcsFailedTitle()
{
    return tr("Adding to Version Control Failed");
}

QString VcsManager::msgToAddToVcsFailed(const QStringList &files, const IVersionControl *vc)
{
    return files.size() == 1
410
411
        ? tr("Could not add the file\n%1\nto version control (%2)")
              .arg(files.front(), vc->displayName()) + QLatin1Char('\n')
Friedemann Kleint's avatar
Friedemann Kleint committed
412
413
414
415
        : tr("Could not add the following files to version control (%1)\n%2")
              .arg(vc->displayName(), files.join(QString(QLatin1Char('\n'))));
}

416
417
418
419
420
421
void VcsManager::promptToAdd(const QString &directory, const QStringList &fileNames)
{
    IVersionControl *vc = findVersionControlForDirectory(directory);
    if (!vc || !vc->supportsOperation(Core::IVersionControl::AddOperation))
        return;

422
423
424
425
426
427
428
429
430
    QStringList unmanagedFiles;
    QDir dir(directory);
    foreach (const QString &fileName, fileNames) {
        if (!vc->managesFile(directory, dir.relativeFilePath(fileName)))
            unmanagedFiles << fileName;
    }
    if (unmanagedFiles.isEmpty())
        return;

431
    Internal::AddToVcsDialog dlg(Core::ICore::mainWindow(), VcsManager::msgAddToVcsTitle(),
432
                                 unmanagedFiles, vc->displayName());
433
    if (dlg.exec() == QDialog::Accepted) {
434
        QStringList notAddedToVc;
435
        foreach (const QString &file, unmanagedFiles) {
436
437
438
439
440
            if (!vc->vcsAdd(file))
                notAddedToVc << file;
        }

        if (!notAddedToVc.isEmpty()) {
Friedemann Kleint's avatar
Friedemann Kleint committed
441
442
            QMessageBox::warning(Core::ICore::mainWindow(), VcsManager::msgAddToVcsFailedTitle(),
                                 VcsManager::msgToAddToVcsFailed(notAddedToVc, vc));
443
444
445
446
        }
    }
}

447
448
void VcsManager::emitRepositoryChanged(const QString &repository)
{
hjk's avatar
hjk committed
449
    emit m_instance->repositoryChanged(repository);
450
451
}

452
453
454
455
456
void VcsManager::clearVersionControlCache()
{
    QStringList repoList = d->m_cachedMatches.keys();
    d->clearCache();
    foreach (const QString &repo, repoList)
hjk's avatar
hjk committed
457
        emit m_instance->repositoryChanged(repo);
458
459
}

460
461
462
463
464
465
466
void VcsManager::configureVcs()
{
    QTC_ASSERT(d->m_unconfiguredVcs, return);
    ICore::showOptionsDialog(Id(VcsBase::Constants::VCS_SETTINGS_CATEGORY),
                             d->m_unconfiguredVcs->id());
}

hjk's avatar
hjk committed
467
} // namespace Core