giteditor.cpp 12.8 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
con's avatar
con committed
2
**
Eike Ziller's avatar
Eike Ziller committed
3 4
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing
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
** 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
Eike Ziller's avatar
Eike Ziller committed
12 13
** a written agreement between you and The Qt Company.  For licensing terms and
** conditions see http://www.qt.io/terms-conditions.  For further information
Eike Ziller's avatar
Eike Ziller committed
14
** use the contact form at http://www.qt.io/contact-us.
15
**
16
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
** Alternatively, this file may be used under the terms of the GNU Lesser
Eike Ziller's avatar
Eike Ziller committed
18 19 20 21 22 23
** 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.
hjk's avatar
hjk committed
24
**
Eike Ziller's avatar
Eike Ziller committed
25 26
** In addition, as a special exception, The Qt Company gives you certain additional
** rights.  These rights are described in The Qt Company LGPL Exception
con's avatar
con committed
27 28
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
29
****************************************************************************/
hjk's avatar
hjk committed
30

con's avatar
con committed
31
#include "giteditor.h"
hjk's avatar
hjk committed
32

con's avatar
con committed
33 34
#include "annotationhighlighter.h"
#include "gitplugin.h"
35
#include "gitclient.h"
36
#include "gitsettings.h"
37 38
#include "gitsubmiteditorwidget.h"
#include "gitconstants.h"
39
#include "githighlighters.h"
con's avatar
con committed
40

41
#include <coreplugin/icore.h>
hjk's avatar
hjk committed
42
#include <utils/qtcassert.h>
43
#include <vcsbase/vcsoutputwindow.h>
44
#include <texteditor/textdocument.h>
45

46 47
#include <QMenu>

48 49 50
#include <QFileInfo>
#include <QRegExp>
#include <QSet>
51
#include <QTemporaryFile>
52
#include <QTextCodec>
53
#include <QDir>
hjk's avatar
hjk committed
54

55 56
#include <QTextCursor>
#include <QTextBlock>
57
#include <QMessageBox>
con's avatar
con committed
58

59
#define CHANGE_PATTERN "[a-f0-9]{7,40}"
con's avatar
con committed
60

61 62
using namespace VcsBase;

con's avatar
con committed
63 64 65
namespace Git {
namespace Internal {

66
GitEditorWidget::GitEditorWidget() :
67
    m_changeNumberPattern(QLatin1String(CHANGE_PATTERN))
con's avatar
con committed
68
{
69
    QTC_ASSERT(m_changeNumberPattern.isValid(), return);
70 71 72 73 74 75 76
    /* Diff format:
        diff --git a/src/plugins/git/giteditor.cpp b/src/plugins/git/giteditor.cpp
        index 40997ff..4e49337 100644
        --- a/src/plugins/git/giteditor.cpp
        +++ b/src/plugins/git/giteditor.cpp
    */
    setDiffFilePattern(QRegExp(QLatin1String("^(?:diff --git a/|index |[+-]{3} (?:/dev/null|[ab]/(.+$)))")));
77
    setLogEntryPattern(QRegExp(QLatin1String("^commit ([0-9a-f]{8})[0-9a-f]{32}")));
78 79
    setAnnotateRevisionTextFormat(tr("&Blame %1"));
    setAnnotatePreviousRevisionTextFormat(tr("Blame &Parent Revision %1"));
con's avatar
con committed
80 81
}

82
QSet<QString> GitEditorWidget::annotationChanges() const
con's avatar
con committed
83 84 85 86 87 88
{
    QSet<QString> changes;
    const QString txt = toPlainText();
    if (txt.isEmpty())
        return changes;
    // Hunt for first change number in annotation: "<change>:"
89
    QRegExp r(QLatin1String("^(" CHANGE_PATTERN ") "));
hjk's avatar
hjk committed
90
    QTC_ASSERT(r.isValid(), return changes);
con's avatar
con committed
91 92
    if (r.indexIn(txt) != -1) {
        changes.insert(r.cap(1));
93
        r.setPattern(QLatin1String("\n(" CHANGE_PATTERN ") "));
hjk's avatar
hjk committed
94
        QTC_ASSERT(r.isValid(), return changes);
con's avatar
con committed
95 96 97 98 99 100 101 102 103
        int pos = 0;
        while ((pos = r.indexIn(txt, pos)) != -1) {
            pos += r.matchedLength();
            changes.insert(r.cap(1));
        }
    }
    return changes;
}

104
QString GitEditorWidget::changeUnderCursor(const QTextCursor &c) const
con's avatar
con committed
105 106 107 108 109 110 111
{
    QTextCursor cursor = c;
    // Any number is regarded as change number.
    cursor.select(QTextCursor::WordUnderCursor);
    if (!cursor.hasSelection())
        return QString();
    const QString change = cursor.selectedText();
112
    if (m_changeNumberPattern.exactMatch(change))
con's avatar
con committed
113 114 115 116
        return change;
    return QString();
}

117
BaseAnnotationHighlighter *GitEditorWidget::createAnnotationHighlighter(const QSet<QString> &changes) const
con's avatar
con committed
118
{
119
    return new GitAnnotationHighlighter(changes);
con's avatar
con committed
120 121
}

122 123 124 125 126
/* Remove the date specification from annotation, which is tabular:
\code
8ca887aa (author               YYYY-MM-DD HH:MM:SS <offset>  <line>)<content>
\endcode */

Orgad Shaneh's avatar
Orgad Shaneh committed
127
static QString removeAnnotationDate(const QString &b)
128
{
129
    if (b.isEmpty())
Orgad Shaneh's avatar
Orgad Shaneh committed
130
        return b;
131

Orgad Shaneh's avatar
Orgad Shaneh committed
132 133
    const QChar space(QLatin1Char(' '));
    const int parenPos = b.indexOf(QLatin1Char(')'));
134
    if (parenPos == -1)
Orgad Shaneh's avatar
Orgad Shaneh committed
135
        return b;
136 137
    int datePos = parenPos;

Orgad Shaneh's avatar
Orgad Shaneh committed
138
    int i = parenPos;
Orgad Shaneh's avatar
Orgad Shaneh committed
139
    while (i >= 0 && b.at(i) != space)
Orgad Shaneh's avatar
Orgad Shaneh committed
140
        --i;
Orgad Shaneh's avatar
Orgad Shaneh committed
141
    while (i >= 0 && b.at(i) == space)
Orgad Shaneh's avatar
Orgad Shaneh committed
142
        --i;
143
    int spaceCount = 0;
Orgad Shaneh's avatar
Orgad Shaneh committed
144 145
    // i is now on timezone. Go back 3 spaces: That is where the date starts.
    while (i >= 0) {
Orgad Shaneh's avatar
Orgad Shaneh committed
146
        if (b.at(i) == space)
147
            ++spaceCount;
Orgad Shaneh's avatar
Orgad Shaneh committed
148 149
        if (spaceCount == 3) {
            datePos = i;
150 151
            break;
        }
Orgad Shaneh's avatar
Orgad Shaneh committed
152
        --i;
153 154
    }
    if (datePos == 0)
Orgad Shaneh's avatar
Orgad Shaneh committed
155
        return b;
156 157

    // Copy over the parts that have not changed into a new byte array
Orgad Shaneh's avatar
Orgad Shaneh committed
158
    QString result;
159
    QTC_ASSERT(b.size() >= parenPos, return result);
160
    int prevPos = 0;
Orgad Shaneh's avatar
Orgad Shaneh committed
161
    int pos = b.indexOf(QLatin1Char('\n'), 0) + 1;
162
    forever {
163
        QTC_CHECK(prevPos < pos);
Orgad Shaneh's avatar
Orgad Shaneh committed
164
        int afterParen = prevPos + parenPos;
Orgad Shaneh's avatar
Orgad Shaneh committed
165 166
        result.append(b.mid(prevPos, datePos));
        result.append(b.mid(afterParen, pos - afterParen));
167
        prevPos = pos;
168
        QTC_CHECK(prevPos != 0);
169
        if (pos == b.size())
170
            break;
171

Orgad Shaneh's avatar
Orgad Shaneh committed
172
        pos = b.indexOf(QLatin1Char('\n'), pos) + 1;
Orgad Shaneh's avatar
Orgad Shaneh committed
173
        if (pos == 0) // indexOf returned -1
174
            pos = b.size();
175
    }
176
    return result;
177 178
}

179
void GitEditorWidget::setPlainTextFiltered(const QString &text)
180
{
181
    QString modText = text;
182
    GitPlugin *plugin = GitPlugin::instance();
183
    // If desired, filter out the date from annotation
184 185
    switch (contentType())
    {
186
    case AnnotateOutput: {
187 188
        const bool omitAnnotationDate = plugin->settings().boolValue(GitSettings::omitAnnotationDateKey);
        if (omitAnnotationDate)
189
            modText = removeAnnotationDate(text);
190 191
        break;
    }
Tobias Hunger's avatar
Tobias Hunger committed
192 193
    default:
        break;
194 195
    }

196
    textDocument()->setPlainText(modText);
197 198
}

199
void GitEditorWidget::commandFinishedGotoLine(bool ok, int exitCode, const QVariant &v)
200
{
201
    reportCommandFinished(ok, exitCode, v);
202 203 204 205 206 207 208
    if (ok && v.type() == QVariant::Int) {
        const int line = v.toInt();
        if (line >= 0)
            gotoLine(line);
    }
}

209
void GitEditorWidget::checkoutChange()
210
{
211 212
    GitPlugin::instance()->gitClient()->stashAndCheckout(
                sourceWorkingDirectory(), m_currentChange);
213 214
}

215
void GitEditorWidget::resetChange()
216
{
217
    const QString workingDir = sourceWorkingDirectory();
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232

    GitClient *client = GitPlugin::instance()->gitClient();
    if (client->gitStatus(workingDir, StatusMode(NoUntracked | NoSubmodules))
            != GitClient::StatusUnchanged) {
        if (QMessageBox::question(
                    Core::ICore::mainWindow(), tr("Reset"),
                    tr("All changes in working directory will be discarded. Are you sure?"),
                    QMessageBox::Yes | QMessageBox::No,
                    QMessageBox::No) == QMessageBox::No) {
            return;
        }
    }
    client->reset(workingDir, QLatin1String("--hard"), m_currentChange);
}

233
void GitEditorWidget::cherryPickChange()
234
{
235 236
    GitPlugin::instance()->gitClient()->synchronousCherryPick(
                sourceWorkingDirectory(), m_currentChange);
237 238
}

239
void GitEditorWidget::revertChange()
240
{
241 242
    GitPlugin::instance()->gitClient()->synchronousRevert(
                sourceWorkingDirectory(), m_currentChange);
243 244
}

245
void GitEditorWidget::applyDiffChunk(const DiffChunk& chunk, bool revert)
246 247 248 249 250
{
    QTemporaryFile patchFile;
    if (!patchFile.open())
        return;

251
    const QString baseDir = workingDirectory();
252 253 254 255 256 257 258 259 260 261 262
    patchFile.write(chunk.header);
    patchFile.write(chunk.chunk);
    patchFile.close();

    GitClient *client = GitPlugin::instance()->gitClient();
    QStringList args = QStringList() << QLatin1String("--cached");
    if (revert)
        args << QLatin1String("--reverse");
    QString errorMessage;
    if (client->synchronousApplyPatch(baseDir, patchFile.fileName(), &errorMessage, args)) {
        if (errorMessage.isEmpty())
263
            VcsOutputWindow::append(tr("Chunk successfully staged"));
264
        else
265
            VcsOutputWindow::append(errorMessage);
266 267 268 269 270
        if (revert)
            emit diffChunkReverted(chunk);
        else
            emit diffChunkApplied(chunk);
    } else {
271
        VcsOutputWindow::appendError(errorMessage);
272 273 274
    }
}

275
void GitEditorWidget::init()
276
{
277
    VcsBaseEditorWidget::init();
278
    Core::Id editorId = textDocument()->id();
279
    if (editorId == Git::Constants::GIT_COMMIT_TEXT_EDITOR_ID)
280
        textDocument()->setSyntaxHighlighter(new GitSubmitHighlighter);
281
    else if (editorId == Git::Constants::GIT_REBASE_EDITOR_ID)
282
        textDocument()->setSyntaxHighlighter(new GitRebaseHighlighter);
283 284
}

285
void GitEditorWidget::addDiffActions(QMenu *menu, const DiffChunk &chunk)
286 287 288 289
{
    menu->addSeparator();

    QAction *stageAction = menu->addAction(tr("Stage Chunk..."));
290 291 292
    connect(stageAction, &QAction::triggered, this, [this, chunk] {
        applyDiffChunk(chunk, false);
    });
293 294

    QAction *unstageAction = menu->addAction(tr("Unstage Chunk..."));
295 296 297
    connect(unstageAction, &QAction::triggered, this, [this, chunk] {
        applyDiffChunk(chunk, true);
    });
298 299
}

300
bool GitEditorWidget::open(QString *errorString, const QString &fileName, const QString &realFileName)
301
{
302
    Core::Id editorId = textDocument()->id();
303 304
    if (editorId == Git::Constants::GIT_COMMIT_TEXT_EDITOR_ID
            || editorId == Git::Constants::GIT_REBASE_EDITOR_ID) {
305
        QFileInfo fi(fileName);
306 307
        const QString gitPath = fi.absolutePath();
        setSource(gitPath);
308
        textDocument()->setCodec(
309
                    GitPlugin::instance()->gitClient()->encoding(gitPath, "i18n.commitEncoding"));
310
    }
311
    return VcsBaseEditorWidget::open(errorString, fileName, realFileName);
312 313
}

314
QString GitEditorWidget::decorateVersion(const QString &revision) const
315 316 317 318 319 320 321 322
{
    const QFileInfo fi(source());
    const QString workingDirectory = fi.absolutePath();

    // Format verbose, SHA1 being first token
    return GitPlugin::instance()->gitClient()->synchronousShortDescription(workingDirectory, revision);
}

323
QStringList GitEditorWidget::annotationPreviousVersions(const QString &revision) const
324 325 326 327 328 329 330 331 332
{
    QStringList revisions;
    QString errorMessage;
    GitClient *client = GitPlugin::instance()->gitClient();
    const QFileInfo fi(source());
    const QString workingDirectory = fi.absolutePath();
    // Get the SHA1's of the file.
    if (!client->synchronousParentRevisions(workingDirectory, QStringList(fi.fileName()),
                                            revision, &revisions, &errorMessage)) {
333
        VcsOutputWindow::appendSilently(errorMessage);
334 335
        return QStringList();
    }
336
    return revisions;
337 338
}

339
bool GitEditorWidget::isValidRevision(const QString &revision) const
340 341 342 343
{
    return GitPlugin::instance()->gitClient()->isValidRevision(revision);
}

344
void GitEditorWidget::addChangeActions(QMenu *menu, const QString &change)
345 346
{
    m_currentChange = change;
347
    if (contentType() != OtherContent) {
348 349 350 351
        menu->addAction(tr("Cherr&y-Pick Change %1").arg(change), this, SLOT(cherryPickChange()));
        menu->addAction(tr("Re&vert Change %1").arg(change), this, SLOT(revertChange()));
        menu->addAction(tr("C&heckout Change %1").arg(change), this, SLOT(checkoutChange()));
        menu->addAction(tr("Hard &Reset to Change %1").arg(change), this, SLOT(resetChange()));
352
    }
353 354
}

355
QString GitEditorWidget::revisionSubject(const QTextBlock &inBlock) const
356 357 358 359 360 361 362 363 364 365 366
{
    for (QTextBlock block = inBlock.next(); block.isValid(); block = block.next()) {
        const QString line = block.text().trimmed();
        if (line.isEmpty()) {
            block = block.next();
            return block.text().trimmed();
        }
    }
    return QString();
}

367
bool GitEditorWidget::supportChangeLinks() const
368 369
{
    return VcsBaseEditorWidget::supportChangeLinks()
370 371
            || (textDocument()->id() == Git::Constants::GIT_COMMIT_TEXT_EDITOR_ID)
            || (textDocument()->id() == Git::Constants::GIT_REBASE_EDITOR_ID);
372 373
}

374
QString GitEditorWidget::fileNameForLine(int line) const
375 376 377 378 379 380 381 382 383 384 385 386 387
{
    // 7971b6e7 share/qtcreator/dumper/dumper.py   (hjk
    QTextBlock block = document()->findBlockByLineNumber(line - 1);
    QTC_ASSERT(block.isValid(), return source());
    static QRegExp renameExp(QLatin1String("^" CHANGE_PATTERN "\\s+([^(]+)"));
    if (renameExp.indexIn(block.text()) != -1) {
        const QString fileName = renameExp.cap(1).trimmed();
        if (!fileName.isEmpty())
            return fileName;
    }
    return source();
}

388
QString GitEditorWidget::sourceWorkingDirectory() const
389 390 391 392 393
{
    const QFileInfo fi(source());
    return fi.isDir() ? fi.absoluteFilePath() : fi.absolutePath();
}

hjk's avatar
hjk committed
394 395
} // namespace Internal
} // namespace Git