1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
10 ** GNU Lesser General Public License Usage
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
31 **************************************************************************/
33 #include "giteditor.h"
35 #include "annotationhighlighter.h"
36 #include "gitconstants.h"
37 #include "gitplugin.h"
38 #include "gitclient.h"
39 #include "gitsettings.h"
40 #include <QtCore/QTextCodec>
42 #include <coreplugin/editormanager/editormanager.h>
43 #include <utils/qtcassert.h>
44 #include <vcsbase/diffhighlighter.h>
45 #include <vcsbase/vcsbaseoutputwindow.h>
46 #include <QtCore/QDebug>
47 #include <QtCore/QDir>
48 #include <QtCore/QFileInfo>
49 #include <QtCore/QRegExp>
50 #include <QtCore/QSet>
51 #include <QtCore/QTextStream>
53 #include <QtGui/QTextCursor>
54 #include <QtGui/QTextEdit>
55 #include <QtGui/QTextBlock>
57 #define CHANGE_PATTERN_8C "[a-f0-9]{7,8}"
58 #define CHANGE_PATTERN_40C "[a-f0-9]{40,40}"
63 // ------------ GitEditor
64 GitEditor::GitEditor(const VCSBase::VCSBaseEditorParameters *type,
66 VCSBase::VCSBaseEditorWidget(type, parent),
67 m_changeNumberPattern8(QLatin1String(CHANGE_PATTERN_8C)),
68 m_changeNumberPattern40(QLatin1String(CHANGE_PATTERN_40C))
70 QTC_ASSERT(m_changeNumberPattern8.isValid(), return);
71 QTC_ASSERT(m_changeNumberPattern40.isValid(), return);
72 setAnnotateRevisionTextFormat(tr("Blame %1"));
73 setAnnotatePreviousRevisionTextFormat(tr("Blame parent revision %1"));
74 if (Git::Constants::debug)
75 qDebug() << "GitEditor::GitEditor" << type->type << type->id;
78 QSet<QString> GitEditor::annotationChanges() const
80 QSet<QString> changes;
81 const QString txt = toPlainText();
84 // Hunt for first change number in annotation: "<change>:"
85 QRegExp r(QLatin1String("^("CHANGE_PATTERN_8C") "));
86 QTC_ASSERT(r.isValid(), return changes);
87 if (r.indexIn(txt) != -1) {
88 changes.insert(r.cap(1));
89 r.setPattern(QLatin1String("\n("CHANGE_PATTERN_8C") "));
90 QTC_ASSERT(r.isValid(), return changes);
92 while ((pos = r.indexIn(txt, pos)) != -1) {
93 pos += r.matchedLength();
94 changes.insert(r.cap(1));
97 if (Git::Constants::debug)
98 qDebug() << "GitEditor::annotationChanges() returns #" << changes.size();
102 QString GitEditor::changeUnderCursor(const QTextCursor &c) const
104 QTextCursor cursor = c;
105 // Any number is regarded as change number.
106 cursor.select(QTextCursor::WordUnderCursor);
107 if (!cursor.hasSelection())
109 const QString change = cursor.selectedText();
110 if (Git::Constants::debug > 1)
111 qDebug() << "GitEditor:::changeUnderCursor:" << change;
112 if (m_changeNumberPattern8.exactMatch(change))
114 if (m_changeNumberPattern40.exactMatch(change))
119 VCSBase::DiffHighlighter *GitEditor::createDiffHighlighter() const
121 const QRegExp filePattern(QLatin1String("^(diff --git a/|index |[+-][+-][+-] [ab]).*$"));
122 return new VCSBase::DiffHighlighter(filePattern);
125 VCSBase::BaseAnnotationHighlighter *GitEditor::createAnnotationHighlighter(const QSet<QString> &changes) const
127 return new GitAnnotationHighlighter(changes);
130 QString GitEditor::fileNameFromDiffSpecification(const QTextBlock &inBlock) const
132 // Check for "+++ b/src/plugins/git/giteditor.cpp" (blame and diff)
134 const QString newFileIndicator = QLatin1String("+++ b/");
135 for (QTextBlock block = inBlock; block.isValid(); block = block.previous()) {
136 QString diffFileName = block.text();
137 if (diffFileName.startsWith(newFileIndicator)) {
138 diffFileName.remove(0, newFileIndicator.size());
139 return findDiffFile(diffFileName, GitPlugin::instance()->versionControl());
145 /* Remove the date specification from annotation, which is tabular:
147 8ca887aa (author YYYY-MM-DD HH:MM:SS <offset> <line>)<content>
150 static void removeAnnotationDate(QString *s)
154 // Get position of date (including blank) and the ')'
155 const QRegExp isoDatePattern(QLatin1String(" \\d{4}-\\d{2}-\\d{2}"));
156 Q_ASSERT(isoDatePattern.isValid());
157 const int datePos = s->indexOf(isoDatePattern);
158 const int parenPos = datePos == -1 ? -1 : s->indexOf(QLatin1Char(')'));
161 // In all lines, remove the bit from datePos .. parenPos;
162 const int dateLength = parenPos - datePos;
163 const QChar newLine = QLatin1Char('\n');
164 for (int pos = 0; pos < s->size(); ) {
165 if (pos + parenPos >s->size()) // Should not happen
167 s->remove(pos + datePos, dateLength);
168 const int nextLinePos = s->indexOf(newLine, pos + datePos);
169 pos = nextLinePos == -1 ? s->size() : nextLinePos + 1;
173 void GitEditor::setPlainTextDataFiltered(const QByteArray &a)
175 // If desired, filter out the date from annotation
176 const bool omitAnnotationDate = contentType() == VCSBase::AnnotateOutput
177 && GitPlugin::instance()->settings().omitAnnotationDate;
178 if (omitAnnotationDate) {
179 QString text = codec()->toUnicode(a);
180 removeAnnotationDate(&text);
187 void GitEditor::commandFinishedGotoLine(bool ok, int /* exitCode */, const QVariant &v)
189 if (ok && v.type() == QVariant::Int) {
190 const int line = v.toInt();
196 QStringList GitEditor::annotationPreviousVersions(const QString &revision) const
198 QStringList revisions;
199 QString errorMessage;
200 GitClient *client = GitPlugin::instance()->gitClient();
201 const QFileInfo fi(source());
202 const QString workingDirectory = fi.absolutePath();
203 // Get the SHA1's of the file.
204 if (!client->synchronousParentRevisions(workingDirectory, QStringList(fi.fileName()),
205 revision, &revisions, &errorMessage)) {
206 VCSBase::VCSBaseOutputWindow::instance()->appendSilently(errorMessage);
207 return QStringList();
209 // Format verbose, SHA1 being first token
210 QStringList descriptions;
211 if (!client->synchronousShortDescriptions(workingDirectory, revisions, &descriptions, &errorMessage)) {
212 VCSBase::VCSBaseOutputWindow::instance()->appendSilently(errorMessage);
213 return QStringList();
218 } // namespace Internal