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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
|
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2009 Brian McGillion
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
**
** GNU Lesser General Public License Usage
**
** 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, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**************************************************************************/
#include "mercurialclient.h"
#include "constants.h"
#include <vcsbase/command.h>
#include <vcsbase/vcsbaseoutputwindow.h>
#include <vcsbase/vcsbaseplugin.h>
#include <vcsbase/vcsbaseeditor.h>
#include <vcsbase/vcsbaseeditorparameterwidget.h>
#include <utils/synchronousprocess.h>
#include <utils/fileutils.h>
#include <utils/qtcassert.h>
#include <QtCore/QDir>
#include <QtCore/QFileInfo>
#include <QtCore/QTextCodec>
#include <QtCore/QTextStream>
#include <QtCore/QVariant>
namespace Mercurial {
namespace Internal {
MercurialClient::MercurialClient(MercurialSettings *settings) :
VcsBase::VcsBaseClient(settings)
{
}
MercurialSettings *MercurialClient::settings() const
{
return dynamic_cast<MercurialSettings *>(VcsBase::VcsBaseClient::settings());
}
bool MercurialClient::manifestSync(const QString &repository, const QString &relativeFilename)
{
// This only works when called from the repo and outputs paths relative to it.
const QStringList args(QLatin1String("manifest"));
QByteArray output;
vcsFullySynchronousExec(repository, args, &output);
const QDir repositoryDir(repository);
const QFileInfo needle = QFileInfo(repositoryDir, relativeFilename);
const QStringList files = QString::fromLocal8Bit(output).split(QLatin1Char('\n'));
foreach (const QString &fileName, files) {
const QFileInfo managedFile(repositoryDir, fileName);
if (needle == managedFile)
return true;
}
return false;
}
//bool MercurialClient::clone(const QString &directory, const QString &url)
bool MercurialClient::synchronousClone(const QString &workingDir,
const QString &srcLocation,
const QString &dstLocation,
const QStringList &extraOptions)
{
Q_UNUSED(workingDir);
Q_UNUSED(extraOptions);
QDir workingDirectory(srcLocation);
QByteArray output;
const unsigned flags = VcsBase::VcsBasePlugin::SshPasswordPrompt |
VcsBase::VcsBasePlugin::ShowStdOutInLogWindow |
VcsBase::VcsBasePlugin::ShowSuccessMessage;
if (workingDirectory.exists()) {
// Let's make first init
QStringList arguments(QLatin1String("init"));
if (!vcsFullySynchronousExec(workingDirectory.path(), arguments, &output)) {
return false;
}
// Then pull remote repository
arguments.clear();
arguments << QLatin1String("pull") << dstLocation;
const Utils::SynchronousProcessResponse resp1 =
vcsSynchronousExec(workingDirectory.path(), arguments, flags);
if (resp1.result != Utils::SynchronousProcessResponse::Finished) {
return false;
}
// By now, there is no hgrc file -> create it
Utils::FileSaver saver(workingDirectory.path() + QLatin1String("/.hg/hgrc"));
const QString hgrc = QLatin1String("[paths]\ndefault = ") + dstLocation + QLatin1Char('\n');
saver.write(hgrc.toUtf8());
if (!saver.finalize()) {
VcsBase::VcsBaseOutputWindow::instance()->appendError(saver.errorString());
return false;
}
// And last update repository
arguments.clear();
arguments << QLatin1String("update");
const Utils::SynchronousProcessResponse resp2 =
vcsSynchronousExec(workingDirectory.path(), arguments, flags);
return resp2.result == Utils::SynchronousProcessResponse::Finished;
} else {
QStringList arguments(QLatin1String("clone"));
arguments << dstLocation << workingDirectory.dirName();
workingDirectory.cdUp();
const Utils::SynchronousProcessResponse resp =
vcsSynchronousExec(workingDirectory.path(), arguments, flags);
return resp.result == Utils::SynchronousProcessResponse::Finished;
}
}
QString MercurialClient::branchQuerySync(const QString &repositoryRoot)
{
QByteArray output;
if (vcsFullySynchronousExec(repositoryRoot, QStringList(QLatin1String("branch")), &output))
return QTextCodec::codecForLocale()->toUnicode(output).trimmed();
return QLatin1String("Unknown Branch");
}
static inline QString msgParentRevisionFailed(const QString &workingDirectory,
const QString &revision,
const QString &why)
{
return MercurialClient::tr("Unable to find parent revisions of %1 in %2: %3").
arg(revision, QDir::toNativeSeparators(workingDirectory), why);
}
static inline QString msgParseParentsOutputFailed(const QString &output)
{
return MercurialClient::tr("Cannot parse output: %1").arg(output);
}
bool MercurialClient::parentRevisionsSync(const QString &workingDirectory,
const QString &file /* = QString() */,
const QString &revision,
QStringList *parents)
{
parents->clear();
QStringList args;
args << QLatin1String("parents") << QLatin1String("-r") <<revision;
if (!file.isEmpty())
args << file;
QByteArray outputData;
if (!vcsFullySynchronousExec(workingDirectory, args, &outputData))
return false;
QString output = QString::fromLocal8Bit(outputData);
output.remove(QLatin1Char('\r'));
/* Looks like: \code
changeset: 0:031a48610fba
user: ...
\endcode */
// Obtain first line and split by blank-delimited tokens
VcsBase::VcsBaseOutputWindow *outputWindow = VcsBase::VcsBaseOutputWindow::instance();
const QStringList lines = output.split(QLatin1Char('\n'));
if (lines.size() < 1) {
outputWindow->appendSilently(msgParentRevisionFailed(workingDirectory, revision, msgParseParentsOutputFailed(output)));
return false;
}
QStringList changeSets = lines.front().simplified().split(QLatin1Char(' '));
if (changeSets.size() < 2) {
outputWindow->appendSilently(msgParentRevisionFailed(workingDirectory, revision, msgParseParentsOutputFailed(output)));
return false;
}
// Remove revision numbers
const QChar colon = QLatin1Char(':');
const QStringList::iterator end = changeSets.end();
QStringList::iterator it = changeSets.begin();
for (++it; it != end; ++it) {
const int colonIndex = it->indexOf(colon);
if (colonIndex != -1)
parents->push_back(it->mid(colonIndex + 1));
}
return true;
}
// Describe a change using an optional format
bool MercurialClient::shortDescriptionSync(const QString &workingDirectory,
const QString &revision,
const QString &format,
QString *description)
{
description->clear();
QStringList args;
args << QLatin1String("log") << QLatin1String("-r") <<revision;
if (!format.isEmpty())
args << QLatin1String("--template") << format;
QByteArray outputData;
if (!vcsFullySynchronousExec(workingDirectory, args, &outputData))
return false;
*description = QString::fromLocal8Bit(outputData);
description->remove(QLatin1Char('\r'));
if (description->endsWith(QLatin1Char('\n')))
description->truncate(description->size() - 1);
return true;
}
// Default format: "SHA1 (author summmary)"
static const char defaultFormatC[] = "{node} ({author|person} {desc|firstline})";
bool MercurialClient::shortDescriptionSync(const QString &workingDirectory,
const QString &revision,
QString *description)
{
if (!shortDescriptionSync(workingDirectory, revision, QLatin1String(defaultFormatC), description))
return false;
description->remove(QLatin1Char('\n'));
return true;
}
// Convenience to format a list of changes
bool MercurialClient::shortDescriptionsSync(const QString &workingDirectory, const QStringList &revisions,
QStringList *descriptions)
{
descriptions->clear();
foreach(const QString &revision, revisions) {
QString description;
if (!shortDescriptionSync(workingDirectory, revision, &description))
return false;
descriptions->push_back(description);
}
return true;
}
QString MercurialClient::vcsGetRepositoryURL(const QString &directory)
{
QByteArray output;
QStringList arguments(QLatin1String("showconfig"));
arguments << QLatin1String("paths.default");
if (vcsFullySynchronousExec(directory, arguments, &output))
return QString::fromLocal8Bit(output);
return QString();
}
void MercurialClient::incoming(const QString &repositoryRoot, const QString &repository)
{
QStringList args;
args << QLatin1String("incoming") << QLatin1String("-g") << QLatin1String("-p");
if (!repository.isEmpty())
args.append(repository);
QString id = repositoryRoot;
if (!repository.isEmpty()) {
id += QDir::separator();
id += repository;
}
const QString kind = QLatin1String(Constants::DIFFLOG);
const QString title = tr("Hg incoming %1").arg(id);
VcsBase::VcsBaseEditorWidget *editor = createVcsEditor(kind, title, repositoryRoot,
true, "incoming", id);
VcsBase::Command *cmd = createCommand(repository, editor);
if (!repository.isEmpty() && VcsBase::VcsBasePlugin::isSshPromptConfigured())
cmd->setUnixTerminalDisabled(true);
enqueueJob(cmd, args);
}
void MercurialClient::outgoing(const QString &repositoryRoot)
{
QStringList args;
args << QLatin1String("outgoing") << QLatin1String("-g") << QLatin1String("-p");
const QString kind = QLatin1String(Constants::DIFFLOG);
const QString title = tr("Hg outgoing %1").
arg(QDir::toNativeSeparators(repositoryRoot));
VcsBase::VcsBaseEditorWidget *editor = createVcsEditor(kind, title, repositoryRoot, true,
"outgoing", repositoryRoot);
VcsBase::Command *cmd = createCommand(repositoryRoot, editor);
cmd->setUnixTerminalDisabled(VcsBase::VcsBasePlugin::isSshPromptConfigured());
enqueueJob(cmd, args);
}
void MercurialClient::annotate(const QString &workingDir, const QString &file,
const QString revision, int lineNumber,
const QStringList &extraOptions)
{
QStringList args(extraOptions);
args << QLatin1String("-u") << QLatin1String("-c") << QLatin1String("-d");
VcsBaseClient::annotate(workingDir, file, revision, lineNumber, args);
}
void MercurialClient::commit(const QString &repositoryRoot, const QStringList &files,
const QString &commitMessageFile,
const QStringList &extraOptions)
{
QStringList args(extraOptions);
args << QLatin1String("--noninteractive") << QLatin1String("-l") << commitMessageFile << QLatin1String("-A");
VcsBaseClient::commit(repositoryRoot, files, commitMessageFile, args);
}
void MercurialClient::diff(const QString &workingDir, const QStringList &files,
const QStringList &extraOptions)
{
QStringList args(extraOptions);
args << QLatin1String("-g") << QLatin1String("-p") << QLatin1String("-U 8");
VcsBaseClient::diff(workingDir, files, args);
}
void MercurialClient::import(const QString &repositoryRoot, const QStringList &files,
const QStringList &extraOptions)
{
VcsBaseClient::import(repositoryRoot, files,
QStringList(extraOptions) << QLatin1String("--no-commit"));
}
void MercurialClient::revertAll(const QString &workingDir, const QString &revision,
const QStringList &extraOptions)
{
VcsBaseClient::revertAll(workingDir, revision,
QStringList(extraOptions) << QLatin1String("--all"));
}
void MercurialClient::view(const QString &source, const QString &id,
const QStringList &extraOptions)
{
QStringList args;
args << QLatin1String("log") << QLatin1String("-p") << QLatin1String("-g");
VcsBaseClient::view(source, id, args << extraOptions);
}
QString MercurialClient::findTopLevelForFile(const QFileInfo &file) const
{
const QString repositoryCheckFile = QLatin1String(Constants::MECURIALREPO) + QLatin1String("/requires");
return file.isDir() ?
VcsBase::VcsBasePlugin::findRepositoryForDirectory(file.absoluteFilePath(), repositoryCheckFile) :
VcsBase::VcsBasePlugin::findRepositoryForDirectory(file.absolutePath(), repositoryCheckFile);
}
QString MercurialClient::vcsEditorKind(VcsCommand cmd) const
{
switch (cmd)
{
case AnnotateCommand : return QLatin1String(Constants::ANNOTATELOG);
case DiffCommand : return QLatin1String(Constants::DIFFLOG);
case LogCommand : return QLatin1String(Constants::FILELOG);
default : return QLatin1String("");
}
return QLatin1String("");
}
QStringList MercurialClient::revisionSpec(const QString &revision) const
{
QStringList args;
if (!revision.isEmpty())
args << QLatin1String("-r") << revision;
return args;
}
MercurialClient::StatusItem MercurialClient::parseStatusLine(const QString &line) const
{
StatusItem item;
if (!line.isEmpty())
{
if (line.startsWith(QLatin1Char('M')))
item.flags = QLatin1String("Modified");
else if (line.startsWith(QLatin1Char('A')))
item.flags = QLatin1String("Added");
else if (line.startsWith(QLatin1Char('R')))
item.flags = QLatin1String("Removed");
else if (line.startsWith(QLatin1Char('!')))
item.flags = QLatin1String("Deleted");
else if (line.startsWith(QLatin1Char('?')))
item.flags = QLatin1String("Untracked");
else
return item;
//the status line should be similar to "M file_with_changes"
//so just should take the file name part and store it
item.file = line.mid(2);
}
return item;
}
// Collect all parameters required for a diff to be able to associate them
// with a diff editor and re-run the diff with parameters.
struct MercurialDiffParameters
{
QString workingDir;
QStringList files;
QStringList extraOptions;
};
// Parameter widget controlling whitespace diff mode, associated with a parameter
class MercurialDiffParameterWidget : public VcsBase::VcsBaseEditorParameterWidget
{
Q_OBJECT
public:
MercurialDiffParameterWidget(MercurialClient *client,
const MercurialDiffParameters &p, QWidget *parent = 0) :
VcsBase::VcsBaseEditorParameterWidget(parent), m_client(client), m_params(p)
{
mapSetting(addToggleButton(QLatin1String("-w"), tr("Ignore whitespace")),
client->settings()->boolPointer(MercurialSettings::diffIgnoreWhiteSpaceKey));
mapSetting(addToggleButton(QLatin1String("-B"), tr("Ignore blank lines")),
client->settings()->boolPointer(MercurialSettings::diffIgnoreBlankLinesKey));
}
void executeCommand()
{
m_client->diff(m_params.workingDir, m_params.files, m_params.extraOptions);
}
private:
MercurialClient *m_client;
const MercurialDiffParameters m_params;
};
VcsBase::VcsBaseEditorParameterWidget *MercurialClient::createDiffEditor(
const QString &workingDir, const QStringList &files, const QStringList &extraOptions)
{
MercurialDiffParameters parameters;
parameters.workingDir = workingDir;
parameters.files = files;
parameters.extraOptions = extraOptions;
return new MercurialDiffParameterWidget(this, parameters);
}
} // namespace Internal
} // namespace Mercurial
#include "mercurialclient.moc"
|