2013-12-16 16:19:40 +01:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
2015-01-14 18:07:15 +01:00
|
|
|
** Copyright (C) 2015 The Qt Company Ltd.
|
|
|
|
|
** Contact: http://www.qt.io/licensing
|
2013-12-16 16:19:40 +01:00
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator.
|
|
|
|
|
**
|
|
|
|
|
** 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
|
2015-01-14 18:07:15 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms and
|
|
|
|
|
** conditions see http://www.qt.io/terms-conditions. For further information
|
2014-10-01 13:21:18 +02:00
|
|
|
** use the contact form at http://www.qt.io/contact-us.
|
2013-12-16 16:19:40 +01:00
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
2014-10-01 13:21:18 +02:00
|
|
|
** General Public License version 2.1 or version 3 as published by the Free
|
|
|
|
|
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
|
|
|
|
|
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
|
|
|
|
|
** following information to ensure the GNU Lesser General Public License
|
|
|
|
|
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
|
|
|
|
|
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
2013-12-16 16:19:40 +01:00
|
|
|
**
|
2015-01-14 18:07:15 +01:00
|
|
|
** In addition, as a special exception, The Qt Company gives you certain additional
|
|
|
|
|
** rights. These rights are described in The Qt Company LGPL Exception
|
2013-12-16 16:19:40 +01:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
2014-06-24 10:53:03 +03:00
|
|
|
#include "diffeditorconstants.h"
|
2013-12-16 16:19:40 +01:00
|
|
|
#include "diffeditorcontroller.h"
|
2014-07-04 13:55:51 +02:00
|
|
|
#include "diffeditorreloader.h"
|
2013-12-16 16:19:40 +01:00
|
|
|
|
2014-02-13 16:43:28 +01:00
|
|
|
#include <coreplugin/icore.h>
|
|
|
|
|
|
2014-06-24 10:53:03 +03:00
|
|
|
#include <QStringList>
|
|
|
|
|
|
2014-02-13 16:43:28 +01:00
|
|
|
static const char settingsGroupC[] = "DiffEditor";
|
|
|
|
|
static const char contextLineNumbersKeyC[] = "ContextLineNumbers";
|
|
|
|
|
static const char ignoreWhitespaceKeyC[] = "IgnoreWhitespace";
|
|
|
|
|
|
2013-12-16 16:19:40 +01:00
|
|
|
namespace DiffEditor {
|
|
|
|
|
|
|
|
|
|
DiffEditorController::DiffEditorController(QObject *parent)
|
|
|
|
|
: QObject(parent),
|
2015-01-28 13:08:50 +01:00
|
|
|
m_reloader(0),
|
|
|
|
|
m_contextLinesNumber(3),
|
2014-07-28 23:25:49 +03:00
|
|
|
m_diffFileIndex(-1),
|
|
|
|
|
m_chunkIndex(-1),
|
2014-02-13 16:43:28 +01:00
|
|
|
m_descriptionEnabled(false),
|
2014-11-06 13:23:40 +01:00
|
|
|
m_contextLinesNumberEnabled(true),
|
2015-01-28 13:08:50 +01:00
|
|
|
m_ignoreWhitespace(true)
|
2013-12-16 16:19:40 +01:00
|
|
|
{
|
2014-02-13 16:43:28 +01:00
|
|
|
QSettings *s = Core::ICore::settings();
|
|
|
|
|
s->beginGroup(QLatin1String(settingsGroupC));
|
|
|
|
|
m_contextLinesNumber = s->value(QLatin1String(contextLineNumbersKeyC),
|
|
|
|
|
m_contextLinesNumber).toInt();
|
|
|
|
|
m_ignoreWhitespace = s->value(QLatin1String(ignoreWhitespaceKeyC),
|
|
|
|
|
m_ignoreWhitespace).toBool();
|
|
|
|
|
s->endGroup();
|
|
|
|
|
|
2013-12-16 16:19:40 +01:00
|
|
|
clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DiffEditorController::~DiffEditorController()
|
|
|
|
|
{
|
2014-10-24 14:11:12 +02:00
|
|
|
delete m_reloader;
|
2013-12-16 16:19:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString DiffEditorController::clearMessage() const
|
|
|
|
|
{
|
|
|
|
|
return m_clearMessage;
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-13 16:43:28 +01:00
|
|
|
QList<FileData> DiffEditorController::diffFiles() const
|
2013-12-16 16:19:40 +01:00
|
|
|
{
|
2014-02-13 16:43:28 +01:00
|
|
|
return m_diffFiles;
|
2013-12-16 16:19:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString DiffEditorController::workingDirectory() const
|
|
|
|
|
{
|
|
|
|
|
return m_workingDirectory;
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-30 17:04:54 +01:00
|
|
|
QString DiffEditorController::description() const
|
|
|
|
|
{
|
|
|
|
|
return m_description;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool DiffEditorController::isDescriptionEnabled() const
|
|
|
|
|
{
|
|
|
|
|
return m_descriptionEnabled;
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-13 16:43:28 +01:00
|
|
|
int DiffEditorController::contextLinesNumber() const
|
|
|
|
|
{
|
|
|
|
|
return m_contextLinesNumber;
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-06 13:23:40 +01:00
|
|
|
bool DiffEditorController::isContextLinesNumberEnabled() const
|
|
|
|
|
{
|
|
|
|
|
return m_contextLinesNumberEnabled;
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-13 16:43:28 +01:00
|
|
|
bool DiffEditorController::isIgnoreWhitespace() const
|
|
|
|
|
{
|
|
|
|
|
return m_ignoreWhitespace;
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-11 16:27:23 +01:00
|
|
|
// ### fixme: git-specific handling should be done in the git plugin:
|
|
|
|
|
// Remove unexpanded branches and follows-tag, clear indentation
|
|
|
|
|
// and create E-mail
|
|
|
|
|
static void formatGitDescription(QString *description)
|
|
|
|
|
{
|
|
|
|
|
QString result;
|
|
|
|
|
result.reserve(description->size());
|
|
|
|
|
foreach (QString line, description->split(QLatin1Char('\n'))) {
|
|
|
|
|
if (line.startsWith(QLatin1String("commit "))
|
|
|
|
|
|| line.startsWith(QLatin1String("Branches: <Expand>"))) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (line.startsWith(QLatin1String("Author: ")))
|
|
|
|
|
line.replace(0, 8, QStringLiteral("From: "));
|
|
|
|
|
else if (line.startsWith(QLatin1String(" ")))
|
|
|
|
|
line.remove(0, 4);
|
|
|
|
|
result.append(line);
|
|
|
|
|
result.append(QLatin1Char('\n'));
|
|
|
|
|
}
|
|
|
|
|
*description = result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString DiffEditorController::contents() const
|
|
|
|
|
{
|
|
|
|
|
QString result = m_description;
|
|
|
|
|
const int formattingOptions = DiffUtils::GitFormat;
|
|
|
|
|
if (formattingOptions & DiffUtils::GitFormat)
|
|
|
|
|
formatGitDescription(&result);
|
|
|
|
|
|
|
|
|
|
const QString diff = DiffUtils::makePatch(diffFiles(), formattingOptions);
|
|
|
|
|
if (!diff.isEmpty()) {
|
|
|
|
|
if (!result.isEmpty())
|
|
|
|
|
result += QLatin1Char('\n');
|
|
|
|
|
result += diff;
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-28 23:25:49 +03:00
|
|
|
QString DiffEditorController::makePatch(bool revert, bool addPrefix) const
|
2014-02-13 16:43:28 +01:00
|
|
|
{
|
2014-07-28 23:25:49 +03:00
|
|
|
if (m_diffFileIndex < 0 || m_chunkIndex < 0)
|
2014-02-13 16:43:28 +01:00
|
|
|
return QString();
|
|
|
|
|
|
2014-07-28 23:25:49 +03:00
|
|
|
if (m_diffFileIndex >= m_diffFiles.count())
|
2014-02-13 16:43:28 +01:00
|
|
|
return QString();
|
|
|
|
|
|
2014-07-28 23:25:49 +03:00
|
|
|
const FileData fileData = m_diffFiles.at(m_diffFileIndex);
|
|
|
|
|
if (m_chunkIndex >= fileData.chunks.count())
|
2014-02-13 16:43:28 +01:00
|
|
|
return QString();
|
|
|
|
|
|
2014-07-28 23:25:49 +03:00
|
|
|
const ChunkData chunkData = fileData.chunks.at(m_chunkIndex);
|
|
|
|
|
const bool lastChunk = (m_chunkIndex == fileData.chunks.count() - 1);
|
2014-02-13 16:43:28 +01:00
|
|
|
|
|
|
|
|
const QString fileName = revert
|
|
|
|
|
? fileData.rightFileInfo.fileName
|
|
|
|
|
: fileData.leftFileInfo.fileName;
|
|
|
|
|
|
2014-07-28 23:25:49 +03:00
|
|
|
QString leftPrefix, rightPrefix;
|
|
|
|
|
if (addPrefix) {
|
|
|
|
|
leftPrefix = QLatin1String("a/");
|
|
|
|
|
rightPrefix = QLatin1String("b/");
|
|
|
|
|
}
|
2014-02-13 16:43:28 +01:00
|
|
|
return DiffUtils::makePatch(chunkData,
|
2014-07-28 23:25:49 +03:00
|
|
|
leftPrefix + fileName,
|
|
|
|
|
rightPrefix + fileName,
|
2014-02-13 16:43:28 +01:00
|
|
|
lastChunk && fileData.lastChunkAtTheEndOfFile);
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-04 13:55:51 +02:00
|
|
|
DiffEditorReloader *DiffEditorController::reloader() const
|
|
|
|
|
{
|
|
|
|
|
return m_reloader;
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-24 14:11:12 +02:00
|
|
|
// The ownership of reloader is passed to the controller
|
2014-07-04 13:55:51 +02:00
|
|
|
void DiffEditorController::setReloader(DiffEditorReloader *reloader)
|
|
|
|
|
{
|
|
|
|
|
if (m_reloader == reloader)
|
|
|
|
|
return; // nothing changes
|
|
|
|
|
|
2014-10-24 14:11:12 +02:00
|
|
|
delete m_reloader;
|
2014-07-04 13:55:51 +02:00
|
|
|
|
|
|
|
|
m_reloader = reloader;
|
|
|
|
|
|
|
|
|
|
if (m_reloader)
|
2014-07-07 10:23:11 +02:00
|
|
|
m_reloader->setController(this);
|
2014-07-04 13:55:51 +02:00
|
|
|
|
2014-11-06 13:23:40 +01:00
|
|
|
emit reloaderChanged();
|
2014-07-04 13:55:51 +02:00
|
|
|
}
|
|
|
|
|
|
2013-12-16 16:19:40 +01:00
|
|
|
void DiffEditorController::clear()
|
|
|
|
|
{
|
|
|
|
|
clear(tr("No difference"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DiffEditorController::clear(const QString &message)
|
|
|
|
|
{
|
2014-02-13 16:43:28 +01:00
|
|
|
setDescription(QString());
|
|
|
|
|
setDiffFiles(QList<FileData>());
|
2013-12-16 16:19:40 +01:00
|
|
|
m_clearMessage = message;
|
|
|
|
|
emit cleared(message);
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-13 16:43:28 +01:00
|
|
|
void DiffEditorController::setDiffFiles(const QList<FileData> &diffFileList,
|
|
|
|
|
const QString &workingDirectory)
|
2013-12-16 16:19:40 +01:00
|
|
|
{
|
2014-02-13 16:43:28 +01:00
|
|
|
m_diffFiles = diffFileList;
|
2013-12-16 16:19:40 +01:00
|
|
|
m_workingDirectory = workingDirectory;
|
2014-02-13 16:43:28 +01:00
|
|
|
emit diffFilesChanged(diffFileList, workingDirectory);
|
2013-12-16 16:19:40 +01:00
|
|
|
}
|
|
|
|
|
|
2014-01-30 17:04:54 +01:00
|
|
|
void DiffEditorController::setDescription(const QString &description)
|
|
|
|
|
{
|
|
|
|
|
if (m_description == description)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
m_description = description;
|
2014-06-24 10:53:03 +03:00
|
|
|
emit descriptionChanged(m_description);
|
2014-01-30 17:04:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DiffEditorController::setDescriptionEnabled(bool on)
|
|
|
|
|
{
|
|
|
|
|
if (m_descriptionEnabled == on)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
m_descriptionEnabled = on;
|
|
|
|
|
emit descriptionEnablementChanged(on);
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-24 10:53:03 +03:00
|
|
|
void DiffEditorController::branchesForCommitReceived(const QString &output)
|
|
|
|
|
{
|
|
|
|
|
const QString branches = prepareBranchesForCommit(output);
|
|
|
|
|
|
|
|
|
|
m_description.replace(QLatin1String(Constants::EXPAND_BRANCHES), branches);
|
|
|
|
|
emit descriptionChanged(m_description);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DiffEditorController::expandBranchesRequested()
|
|
|
|
|
{
|
2015-01-28 13:45:02 +01:00
|
|
|
emit requestBranchList(m_description.mid(7, 8));
|
2014-06-24 10:53:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString DiffEditorController::prepareBranchesForCommit(const QString &output)
|
|
|
|
|
{
|
|
|
|
|
QString moreBranches;
|
|
|
|
|
QString branches;
|
|
|
|
|
QStringList res;
|
|
|
|
|
foreach (const QString &branch, output.split(QLatin1Char('\n'))) {
|
|
|
|
|
const QString b = branch.mid(2).trimmed();
|
|
|
|
|
if (!b.isEmpty())
|
|
|
|
|
res << b;
|
|
|
|
|
}
|
|
|
|
|
const int branchCount = res.count();
|
|
|
|
|
// If there are more than 20 branches, list first 10 followed by a hint
|
|
|
|
|
if (branchCount > 20) {
|
|
|
|
|
const int leave = 10;
|
|
|
|
|
//: Displayed after the untranslated message "Branches: branch1, branch2 'and %n more'"
|
|
|
|
|
// in git show.
|
|
|
|
|
moreBranches = QLatin1Char(' ') + tr("and %n more", 0, branchCount - leave);
|
|
|
|
|
res.erase(res.begin() + leave, res.end());
|
|
|
|
|
}
|
|
|
|
|
if (!res.isEmpty())
|
|
|
|
|
branches = (QLatin1String("Branches: ") + res.join(QLatin1String(", ")) + moreBranches);
|
|
|
|
|
|
|
|
|
|
return branches;
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-13 16:43:28 +01:00
|
|
|
void DiffEditorController::setContextLinesNumber(int lines)
|
|
|
|
|
{
|
|
|
|
|
const int l = qMax(lines, 1);
|
|
|
|
|
if (m_contextLinesNumber == l)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
m_contextLinesNumber = l;
|
|
|
|
|
|
|
|
|
|
QSettings *s = Core::ICore::settings();
|
|
|
|
|
s->beginGroup(QLatin1String(settingsGroupC));
|
|
|
|
|
s->setValue(QLatin1String(contextLineNumbersKeyC), m_contextLinesNumber);
|
|
|
|
|
s->endGroup();
|
|
|
|
|
|
|
|
|
|
emit contextLinesNumberChanged(l);
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-06 13:23:40 +01:00
|
|
|
void DiffEditorController::setContextLinesNumberEnabled(bool on)
|
|
|
|
|
{
|
|
|
|
|
if (m_contextLinesNumberEnabled == on)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
m_contextLinesNumberEnabled = on;
|
|
|
|
|
emit contextLinesNumberEnablementChanged(on);
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-13 16:43:28 +01:00
|
|
|
void DiffEditorController::setIgnoreWhitespace(bool ignore)
|
|
|
|
|
{
|
|
|
|
|
if (m_ignoreWhitespace == ignore)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
m_ignoreWhitespace = ignore;
|
|
|
|
|
|
|
|
|
|
QSettings *s = Core::ICore::settings();
|
|
|
|
|
s->beginGroup(QLatin1String(settingsGroupC));
|
|
|
|
|
s->setValue(QLatin1String(ignoreWhitespaceKeyC), m_ignoreWhitespace);
|
|
|
|
|
s->endGroup();
|
|
|
|
|
|
|
|
|
|
emit ignoreWhitespaceChanged(ignore);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DiffEditorController::requestReload()
|
|
|
|
|
{
|
2014-07-04 13:55:51 +02:00
|
|
|
if (m_reloader)
|
|
|
|
|
m_reloader->requestReload();
|
2014-02-13 16:43:28 +01:00
|
|
|
}
|
|
|
|
|
|
2014-06-06 14:35:31 +02:00
|
|
|
void DiffEditorController::requestChunkActions(QMenu *menu,
|
|
|
|
|
int diffFileIndex,
|
|
|
|
|
int chunkIndex)
|
|
|
|
|
{
|
2014-07-28 23:25:49 +03:00
|
|
|
m_diffFileIndex = diffFileIndex;
|
|
|
|
|
m_chunkIndex = chunkIndex;
|
|
|
|
|
emit chunkActionsRequested(menu, diffFileIndex >= 0 && chunkIndex >= 0);
|
2014-06-06 14:35:31 +02:00
|
|
|
}
|
|
|
|
|
|
2014-07-11 12:55:31 +02:00
|
|
|
void DiffEditorController::requestSaveState()
|
|
|
|
|
{
|
|
|
|
|
emit saveStateRequested();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DiffEditorController::requestRestoreState()
|
|
|
|
|
{
|
|
|
|
|
emit restoreStateRequested();
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-16 16:19:40 +01:00
|
|
|
} // namespace DiffEditor
|