Gerrit: Improve server encapsulation

Change-Id: If78fa05e0b328a86e699ba79673999650b05a7ef
Reviewed-by: Tobias Hunger <tobias.hunger@qt.io>
This commit is contained in:
Orgad Shaneh
2017-02-23 12:54:20 +02:00
committed by Orgad Shaneh
parent 71c2352e4b
commit da3f9379c1
3 changed files with 41 additions and 33 deletions

View File

@@ -31,6 +31,7 @@
#include <QDebug>
#include <QFileInfo>
#include <QRegularExpression>
#include <QSettings>
#include <QStandardPaths>
@@ -118,6 +119,38 @@ QString GerritServer::url() const
return res;
}
bool GerritServer::fillFromRemote(const QString &remote, const QString &defaultUser)
{
static const QRegularExpression remotePattern(
"^(?:(?<protocol>[^:]+)://)?(?:(?<user>[^@]+)@)?(?<host>[^:/]+)(?::(?<port>\\d+))?");
// Skip local remotes (refer to the root or relative path)
if (remote.isEmpty() || remote.startsWith('/') || remote.startsWith('.'))
return false;
// On Windows, local paths typically starts with <drive>:
if (Utils::HostOsInfo::isWindowsHost() && remote[1] == ':')
return false;
QRegularExpressionMatch match = remotePattern.match(remote);
if (!match.hasMatch())
return false;
const QString protocol = match.captured("protocol");
if (protocol == "https")
type = GerritServer::Https;
else if (protocol == "http")
type = GerritServer::Http;
else if (protocol.isEmpty() || protocol == "ssh")
type = GerritServer::Ssh;
else
return false;
const QString userName = match.captured("user");
user = userName.isEmpty() ? defaultUser : userName;
host = match.captured("host");
port = match.captured("port").toUShort();
if (host.contains("github.com")) // Clearly not gerrit
return false;
return true;
}
bool GerritParameters::equals(const GerritParameters &rhs) const
{
return server == rhs.server && ssh == rhs.ssh && https == rhs.https;