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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
|
/****************************************************************************
**
** Copyright (C) 2016 Orgad Shaneh <orgads@gmail.com>.
** Contact: https://www.qt.io/licensing/
**
** 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
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#include "gitgrep.h"
#include "gitclient.h"
#include "gitplugin.h"
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/progressmanager/progressmanager.h>
#include <coreplugin/vcsmanager.h>
#include <texteditor/findinfiles.h>
#include <vcsbase/vcscommand.h>
#include <vcsbase/vcsbaseconstants.h>
#include <utils/fancylineedit.h>
#include <utils/filesearch.h>
#include <utils/fileutils.h>
#include <utils/qtcassert.h>
#include <utils/runextensions.h>
#include <utils/synchronousprocess.h>
#include <utils/textfileformat.h>
#include <QCheckBox>
#include <QFuture>
#include <QFutureWatcher>
#include <QHBoxLayout>
#include <QRegularExpressionValidator>
#include <QScopedPointer>
#include <QSettings>
#include <QTextStream>
namespace Git {
namespace Internal {
class GitGrepParameters
{
public:
QString ref;
bool isEnabled = false;
};
using namespace Core;
using namespace Utils;
using VcsBase::VcsCommand;
namespace {
const char EnableGitGrep[] = "EnableGitGrep";
const char GitGrepRef[] = "GitGrepRef";
class GitGrepRunner : public QObject
{
using FutureInterfaceType = QFutureInterface<FileSearchResultList>;
public:
GitGrepRunner(FutureInterfaceType &fi,
const TextEditor::FileFindParameters ¶meters) :
m_fi(fi),
m_parameters(parameters)
{
m_directory = parameters.additionalParameters.toString();
}
void processLine(const QString &line, FileSearchResultList *resultList) const
{
if (line.isEmpty())
return;
static const QLatin1String boldRed("\x1b[1;31m");
static const QLatin1String resetColor("\x1b[m");
FileSearchResult single;
const int lineSeparator = line.indexOf(QChar::Null);
QString filePath = line.left(lineSeparator);
if (!m_ref.isEmpty() && filePath.startsWith(m_ref))
filePath.remove(0, m_ref.length());
single.fileName = m_directory + '/' + filePath;
const int textSeparator = line.indexOf(QChar::Null, lineSeparator + 1);
single.lineNumber = line.mid(lineSeparator + 1, textSeparator - lineSeparator - 1).toInt();
QString text = line.mid(textSeparator + 1);
QVector<QPair<int, int>> matches;
for (;;) {
const int matchStart = text.indexOf(boldRed);
if (matchStart == -1)
break;
const int matchTextStart = matchStart + boldRed.size();
const int matchEnd = text.indexOf(resetColor, matchTextStart);
QTC_ASSERT(matchEnd != -1, break);
const int matchLength = matchEnd - matchTextStart;
matches.append(qMakePair(matchStart, matchLength));
text = text.left(matchStart) + text.mid(matchTextStart, matchLength)
+ text.mid(matchEnd + resetColor.size());
}
single.matchingLine = text;
foreach (auto match, matches) {
single.matchStart = match.first;
single.matchLength = match.second;
resultList->append(single);
}
}
void read(const QString &text)
{
FileSearchResultList resultList;
QString t = text;
QTextStream stream(&t);
while (!stream.atEnd() && !m_fi.isCanceled())
processLine(stream.readLine(), &resultList);
if (!resultList.isEmpty())
m_fi.reportResult(resultList);
}
void exec()
{
QStringList arguments;
arguments << "-c" << "color.grep.match=bold red"
<< "grep" << "-zn"
<< "--no-full-name"
<< "--color=always";
if (!(m_parameters.flags & FindCaseSensitively))
arguments << "-i";
if (m_parameters.flags & FindWholeWords)
arguments << "-w";
if (m_parameters.flags & FindRegularExpression)
arguments << "-P";
else
arguments << "-F";
arguments << m_parameters.text;
GitGrepParameters params = m_parameters.extensionParameters.value<GitGrepParameters>();
if (!params.ref.isEmpty()) {
arguments << params.ref;
m_ref = params.ref + ':';
}
arguments << "--" << m_parameters.nameFilters;
QScopedPointer<VcsCommand> command(GitPlugin::client()->createCommand(m_directory));
command->addFlags(VcsCommand::SilentOutput | VcsCommand::SuppressFailMessage);
command->setProgressiveOutput(true);
QFutureWatcher<FileSearchResultList> watcher;
watcher.setFuture(m_fi.future());
connect(&watcher, &QFutureWatcher<FileSearchResultList>::canceled,
command.data(), &VcsCommand::cancel);
connect(command.data(), &VcsCommand::stdOutText, this, &GitGrepRunner::read);
SynchronousProcessResponse resp = command->runCommand(GitPlugin::client()->vcsBinary(), arguments, 0);
switch (resp.result) {
case SynchronousProcessResponse::TerminatedAbnormally:
case SynchronousProcessResponse::StartFailed:
case SynchronousProcessResponse::Hang:
m_fi.reportCanceled();
break;
case SynchronousProcessResponse::Finished:
case SynchronousProcessResponse::FinishedError:
// When no results are found, git-grep exits with non-zero status.
// Do not consider this as an error.
break;
}
}
static void run(QFutureInterface<FileSearchResultList> &fi,
TextEditor::FileFindParameters parameters)
{
GitGrepRunner runner(fi, parameters);
Core::ProgressTimer progress(fi, 5);
runner.exec();
}
private:
FutureInterfaceType m_fi;
QString m_directory;
QString m_ref;
const TextEditor::FileFindParameters &m_parameters;
};
} // namespace
static bool validateDirectory(const QString &path)
{
static IVersionControl *gitVc = VcsManager::versionControl(VcsBase::Constants::VCS_ID_GIT);
QTC_ASSERT(gitVc, return false);
return gitVc == VcsManager::findVersionControlForDirectory(path, 0);
}
GitGrep::GitGrep()
{
m_widget = new QWidget;
auto layout = new QHBoxLayout(m_widget);
layout->setMargin(0);
m_enabledCheckBox = new QCheckBox(tr("&Use Git Grep"));
m_enabledCheckBox->setToolTip(tr("Use Git Grep for searching. This includes only files "
"that are managed by Git."));
layout->addWidget(m_enabledCheckBox);
m_treeLineEdit = new FancyLineEdit;
m_treeLineEdit->setPlaceholderText(tr("Tree (optional)"));
m_treeLineEdit->setToolTip(tr("Can be HEAD, tag, local or remote branch, or a commit hash.\n"
"Leave empty to search through the file system."));
const QRegularExpression refExpression("[\\S]*");
m_treeLineEdit->setValidator(new QRegularExpressionValidator(refExpression, this));
layout->addWidget(m_treeLineEdit);
TextEditor::FindInFiles *findInFiles = TextEditor::FindInFiles::instance();
QTC_ASSERT(findInFiles, return);
connect(findInFiles, &TextEditor::FindInFiles::pathChanged,
m_widget, [this](const QString &path) {
m_widget->setEnabled(validateDirectory(path));
});
findInFiles->setFindExtension(this);
}
GitGrep::~GitGrep()
{
delete m_widget;
}
QString GitGrep::title() const
{
return tr("Git Grep");
}
QString GitGrep::toolTip() const
{
const QString ref = m_treeLineEdit->text();
if (!ref.isEmpty())
return tr("Ref: %1\n%2").arg(ref);
return QLatin1String("%1");
}
QWidget *GitGrep::widget() const
{
return m_widget;
}
bool GitGrep::isEnabled() const
{
return m_widget->isEnabled() && m_enabledCheckBox->isChecked();
}
bool GitGrep::isEnabled(const TextEditor::FileFindParameters ¶meters) const
{
return parameters.extensionParameters.value<GitGrepParameters>().isEnabled;
}
QVariant GitGrep::parameters() const
{
GitGrepParameters params;
params.isEnabled = isEnabled();
params.ref = m_treeLineEdit->text();
return qVariantFromValue(params);
}
void GitGrep::readSettings(QSettings *settings)
{
m_enabledCheckBox->setChecked(settings->value(EnableGitGrep, false).toBool());
m_treeLineEdit->setText(settings->value(GitGrepRef).toString());
}
void GitGrep::writeSettings(QSettings *settings) const
{
settings->setValue(EnableGitGrep, m_enabledCheckBox->isChecked());
settings->setValue(GitGrepRef, m_treeLineEdit->text());
}
QFuture<FileSearchResultList> GitGrep::executeSearch(
const TextEditor::FileFindParameters ¶meters)
{
return Utils::runAsync(GitGrepRunner::run, parameters);
}
IEditor *GitGrep::openEditor(const SearchResultItem &item,
const TextEditor::FileFindParameters ¶meters)
{
GitGrepParameters params = parameters.extensionParameters.value<GitGrepParameters>();
if (!params.isEnabled || params.ref.isEmpty() || item.path.isEmpty())
return nullptr;
const QString path = QDir::fromNativeSeparators(item.path.first());
QByteArray content;
const QString topLevel = parameters.additionalParameters.toString();
const QString relativePath = QDir(topLevel).relativeFilePath(path);
if (!GitPlugin::client()->synchronousShow(topLevel, params.ref + ":./" + relativePath,
&content, nullptr)) {
return nullptr;
}
if (content.isEmpty())
return nullptr;
QByteArray fileContent;
if (TextFileFormat::readFileUTF8(path, 0, &fileContent, 0) == TextFileFormat::ReadSuccess) {
if (fileContent == content)
return nullptr; // open the file for read/write
}
QString title = tr("Git Show %1:%2").arg(params.ref).arg(relativePath);
IEditor *editor = EditorManager::openEditorWithContents(Id(), &title, content, title,
EditorManager::DoNotSwitchToDesignMode);
editor->gotoLine(item.lineNumber, item.textMarkPos);
editor->document()->setTemporary(true);
return editor;
}
} // Internal
} // Git
Q_DECLARE_METATYPE(Git::Internal::GitGrepParameters)
|