2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2009-12-11 16:43:39 +01:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2009-12-11 16:43:39 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2009-12-11 16:43:39 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** 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
|
2016-01-15 14:57:40 +01:00
|
|
|
** 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.
|
2009-12-11 16:43:39 +01:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** 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.
|
2010-12-17 16:01:08 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2009-12-11 16:43:39 +01:00
|
|
|
|
|
|
|
|
#include "perforcechecker.h"
|
|
|
|
|
|
|
|
|
|
#include <utils/qtcassert.h>
|
|
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QApplication>
|
|
|
|
|
#include <QCursor>
|
2021-04-30 16:58:38 +02:00
|
|
|
#include <QDir>
|
|
|
|
|
#include <QFileInfo>
|
|
|
|
|
#include <QRegularExpression>
|
|
|
|
|
#include <QTimer>
|
2009-12-11 16:43:39 +01:00
|
|
|
|
|
|
|
|
namespace Perforce {
|
|
|
|
|
namespace Internal {
|
|
|
|
|
|
2016-02-10 14:14:06 +01:00
|
|
|
PerforceChecker::PerforceChecker(QObject *parent) : QObject(parent)
|
2009-12-11 16:43:39 +01:00
|
|
|
{
|
2016-08-03 19:43:54 +03:00
|
|
|
connect(&m_process, &QProcess::errorOccurred, this, &PerforceChecker::slotError);
|
2019-02-26 09:40:49 +01:00
|
|
|
connect(&m_process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
|
2015-01-30 10:19:13 +01:00
|
|
|
this, &PerforceChecker::slotFinished);
|
2009-12-11 16:43:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PerforceChecker::~PerforceChecker()
|
|
|
|
|
{
|
2011-07-20 14:34:58 +02:00
|
|
|
m_process.kill();
|
|
|
|
|
m_process.waitForFinished();
|
2009-12-11 16:43:39 +01:00
|
|
|
resetOverrideCursor();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool PerforceChecker::isRunning() const
|
|
|
|
|
{
|
|
|
|
|
return m_process.state() == QProcess::Running;
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-29 16:52:58 +02:00
|
|
|
bool PerforceChecker::waitForFinished()
|
2015-05-05 12:56:54 +02:00
|
|
|
{
|
2016-04-29 16:52:58 +02:00
|
|
|
return m_process.waitForFinished() || m_process.state() == QProcess::NotRunning;
|
2015-05-05 12:56:54 +02:00
|
|
|
}
|
|
|
|
|
|
2009-12-11 16:43:39 +01:00
|
|
|
void PerforceChecker::resetOverrideCursor()
|
|
|
|
|
{
|
|
|
|
|
if (m_isOverrideCursor) {
|
|
|
|
|
QApplication::restoreOverrideCursor();
|
|
|
|
|
m_isOverrideCursor = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-05 12:56:54 +02:00
|
|
|
void PerforceChecker::start(const QString &binary, const QString &workingDirectory,
|
2009-12-11 16:43:39 +01:00
|
|
|
const QStringList &basicArgs,
|
|
|
|
|
int timeoutMS)
|
|
|
|
|
{
|
|
|
|
|
if (isRunning()) {
|
|
|
|
|
emitFailed(QLatin1String("Internal error: process still running"));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (binary.isEmpty()) {
|
|
|
|
|
emitFailed(tr("No executable specified"));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
m_binary = binary;
|
|
|
|
|
QStringList args = basicArgs;
|
|
|
|
|
args << QLatin1String("client") << QLatin1String("-o");
|
2015-05-05 12:56:54 +02:00
|
|
|
|
|
|
|
|
if (!workingDirectory.isEmpty())
|
|
|
|
|
m_process.setWorkingDirectory(workingDirectory);
|
|
|
|
|
|
2021-04-30 16:58:38 +02:00
|
|
|
m_process.setCommand({m_binary, args});
|
|
|
|
|
m_process.start();
|
2009-12-11 16:43:39 +01:00
|
|
|
m_process.closeWriteChannel();
|
|
|
|
|
// Timeout handling
|
|
|
|
|
m_timeOutMS = timeoutMS;
|
|
|
|
|
m_timedOut = false;
|
|
|
|
|
if (timeoutMS > 0)
|
2015-11-19 16:50:14 +01:00
|
|
|
QTimer::singleShot(m_timeOutMS, this, &PerforceChecker::slotTimeOut);
|
2009-12-11 16:43:39 +01:00
|
|
|
// Cursor
|
|
|
|
|
if (m_useOverideCursor) {
|
|
|
|
|
m_isOverrideCursor = true;
|
|
|
|
|
QApplication::setOverrideCursor(QCursor(Qt::BusyCursor));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PerforceChecker::slotTimeOut()
|
|
|
|
|
{
|
|
|
|
|
if (!isRunning())
|
|
|
|
|
return;
|
|
|
|
|
m_timedOut = true;
|
2021-04-30 16:58:38 +02:00
|
|
|
m_process.stopProcess();
|
|
|
|
|
emitFailed(tr("\"%1\" timed out after %2 ms.").arg(m_binary).arg(m_timeOutMS));
|
2009-12-11 16:43:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PerforceChecker::slotError(QProcess::ProcessError error)
|
|
|
|
|
{
|
|
|
|
|
if (m_timedOut)
|
|
|
|
|
return;
|
|
|
|
|
switch (error) {
|
|
|
|
|
case QProcess::FailedToStart:
|
2010-07-05 09:52:32 +02:00
|
|
|
emitFailed(tr("Unable to launch \"%1\": %2").
|
|
|
|
|
arg(QDir::toNativeSeparators(m_binary), m_process.errorString()));
|
2009-12-11 16:43:39 +01:00
|
|
|
break;
|
|
|
|
|
case QProcess::Crashed: // Handled elsewhere
|
|
|
|
|
case QProcess::Timedout:
|
|
|
|
|
break;
|
|
|
|
|
case QProcess::ReadError:
|
|
|
|
|
case QProcess::WriteError:
|
|
|
|
|
case QProcess::UnknownError:
|
2021-04-30 16:58:38 +02:00
|
|
|
m_process.stopProcess();
|
2009-12-11 16:43:39 +01:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PerforceChecker::slotFinished(int exitCode, QProcess::ExitStatus exitStatus)
|
|
|
|
|
{
|
|
|
|
|
if (m_timedOut)
|
|
|
|
|
return;
|
|
|
|
|
switch (exitStatus) {
|
|
|
|
|
case QProcess::CrashExit:
|
2010-07-05 09:52:32 +02:00
|
|
|
emitFailed(tr("\"%1\" crashed.").arg(QDir::toNativeSeparators(m_binary)));
|
2009-12-11 16:43:39 +01:00
|
|
|
break;
|
|
|
|
|
case QProcess::NormalExit:
|
|
|
|
|
if (exitCode) {
|
|
|
|
|
const QString stdErr = QString::fromLocal8Bit(m_process.readAllStandardError());
|
2010-07-05 09:52:32 +02:00
|
|
|
emitFailed(tr("\"%1\" terminated with exit code %2: %3").
|
|
|
|
|
arg(QDir::toNativeSeparators(m_binary)).arg(exitCode).arg(stdErr));
|
2009-12-11 16:43:39 +01:00
|
|
|
} else {
|
|
|
|
|
parseOutput(QString::fromLocal8Bit(m_process.readAllStandardOutput()));
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-19 13:34:56 +02:00
|
|
|
static inline QString findTerm(const QString& in, const QLatin1String& term)
|
|
|
|
|
{
|
|
|
|
|
QRegularExpression regExp(QString("(\\n|\\r\\n|\\r)%1\\s*(.*)(\\n|\\r\\n|\\r)").arg(term));
|
|
|
|
|
QTC_ASSERT(regExp.isValid(), return QString());
|
|
|
|
|
QRegularExpressionMatch match = regExp.match(in);
|
|
|
|
|
if (match.hasMatch())
|
|
|
|
|
return match.captured(2).trimmed();
|
|
|
|
|
return QString();
|
|
|
|
|
}
|
|
|
|
|
|
2009-12-11 16:43:39 +01:00
|
|
|
// Parse p4 client output for the top level
|
|
|
|
|
static inline QString clientRootFromOutput(const QString &in)
|
|
|
|
|
{
|
2016-05-19 13:34:56 +02:00
|
|
|
QString root = findTerm(in, QLatin1String("Root:"));
|
|
|
|
|
if (!root.isNull()) {
|
|
|
|
|
// Normalize slashes and capitalization of Windows drive letters for caching.
|
|
|
|
|
return QFileInfo(root).absoluteFilePath();
|
|
|
|
|
}
|
2009-12-11 16:43:39 +01:00
|
|
|
return QString();
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-17 10:18:22 +00:00
|
|
|
// When p4 port and p4 user is set a preconfigured Root: is given, which doesn't relate with
|
|
|
|
|
// the current mapped project. In this case "Client:" has the same value as "Host:", which is an
|
|
|
|
|
// invalid case.
|
|
|
|
|
static inline bool clientAndHostAreEqual(const QString &in)
|
|
|
|
|
{
|
|
|
|
|
QString client = findTerm(in, QLatin1String("Client:"));
|
|
|
|
|
QString host = findTerm(in, QLatin1String("Host:"));
|
|
|
|
|
|
|
|
|
|
return client == host;
|
|
|
|
|
}
|
|
|
|
|
|
2009-12-11 16:43:39 +01:00
|
|
|
void PerforceChecker::parseOutput(const QString &response)
|
|
|
|
|
{
|
|
|
|
|
if (!response.contains(QLatin1String("View:")) && !response.contains(QLatin1String("//depot/"))) {
|
|
|
|
|
emitFailed(tr("The client does not seem to contain any mapped files."));
|
|
|
|
|
return;
|
|
|
|
|
}
|
2016-05-19 13:34:56 +02:00
|
|
|
|
2019-05-17 10:18:22 +00:00
|
|
|
if (clientAndHostAreEqual(response)) {
|
|
|
|
|
// Is an invalid case. But not an error. QtC checks cmake install directories for
|
|
|
|
|
// p4 repositories, or the %temp% directory.
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2009-12-11 16:43:39 +01:00
|
|
|
const QString repositoryRoot = clientRootFromOutput(response);
|
|
|
|
|
if (repositoryRoot.isEmpty()) {
|
2010-02-26 14:18:47 +01:00
|
|
|
//: Unable to determine root of the p4 client installation
|
2009-12-11 16:43:39 +01:00
|
|
|
emitFailed(tr("Unable to determine the client root."));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// Check existence. No precise check here, might be a symlink
|
2014-10-24 10:28:28 +02:00
|
|
|
if (QFileInfo::exists(repositoryRoot)) {
|
2009-12-11 16:43:39 +01:00
|
|
|
emitSucceeded(repositoryRoot);
|
|
|
|
|
} else {
|
2010-07-05 09:52:32 +02:00
|
|
|
emitFailed(tr("The repository \"%1\" does not exist.").
|
|
|
|
|
arg(QDir::toNativeSeparators(repositoryRoot)));
|
2009-12-11 16:43:39 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PerforceChecker::emitFailed(const QString &m)
|
|
|
|
|
{
|
|
|
|
|
resetOverrideCursor();
|
|
|
|
|
emit failed(m);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PerforceChecker::emitSucceeded(const QString &m)
|
|
|
|
|
{
|
|
|
|
|
resetOverrideCursor();
|
|
|
|
|
emit succeeded(m);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool PerforceChecker::useOverideCursor() const
|
|
|
|
|
{
|
|
|
|
|
return m_useOverideCursor;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PerforceChecker::setUseOverideCursor(bool v)
|
|
|
|
|
{
|
|
|
|
|
m_useOverideCursor = v;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|