Commit da3f9379 authored by Orgad Shaneh's avatar Orgad Shaneh Committed by Orgad Shaneh

Gerrit: Improve server encapsulation

Change-Id: If78fa05e0b328a86e699ba79673999650b05a7ef
Reviewed-by: Tobias Hunger's avatarTobias Hunger <tobias.hunger@qt.io>
parent 71c2352e
......@@ -230,8 +230,6 @@ void GerritDialog::updateRemotes()
if (m_repository.isEmpty() || !QFileInfo(m_repository).isDir())
return;
m_updatingRemotes = true;
static const QRegularExpression sshPattern(
"^(?:(?<protocol>[^:]+)://)?(?:(?<user>[^@]+)@)?(?<host>[^:/]+)(?::(?<port>\\d+))?");
*m_server = m_parameters->server;
QString errorMessage; // Mute errors. We'll just fallback to the defaults
QMap<QString, QString> remotesList =
......@@ -239,35 +237,14 @@ void GerritDialog::updateRemotes()
QMapIterator<QString, QString> mapIt(remotesList);
while (mapIt.hasNext()) {
mapIt.next();
const QString r = mapIt.value();
// Skip local remotes (refer to the root or relative path)
if (r.isEmpty() || r.startsWith('/') || r.startsWith('.'))
continue;
// On Windows, local paths typically starts with <drive>:
if (Utils::HostOsInfo::isWindowsHost() && r[1] == ':')
continue;
GerritServer server;
QRegularExpressionMatch match = sshPattern.match(r);
if (match.hasMatch()) {
const QString protocol = match.captured("protocol");
if (protocol == "https")
server.type = GerritServer::Https;
else if (protocol == "http")
server.type = GerritServer::Http;
else if (protocol.isEmpty() || protocol == "ssh")
server.type = GerritServer::Ssh;
else
continue;
const QString user = match.captured("user");
server.user = user.isEmpty() ? m_parameters->server.user : user;
server.host = match.captured("host");
server.port = match.captured("port").toUShort();
// Only Ssh is currently supported. In order to extend support for http[s],
// we need to move this logic to the model, and attempt connection to each
// remote (do it only on refresh, not on each path change)
if (server.type == GerritServer::Ssh)
addRemote(server, mapIt.key());
}
if (!server.fillFromRemote(mapIt.value(), m_parameters->server.user))
continue;
// Only Ssh is currently supported. In order to extend support for http[s],
// we need to move this logic to the model, and attempt connection to each
// remote (do it only on refresh, not on each path change)
if (server.type == GerritServer::Ssh)
addRemote(server, mapIt.key());
}
addRemote(m_parameters->server, tr("Fallback"));
m_updatingRemotes = false;
......@@ -276,9 +253,6 @@ void GerritDialog::updateRemotes()
void GerritDialog::addRemote(const GerritServer &server, const QString &name)
{
// Clearly not gerrit
if (server.host.contains("github.com"))
return;
for (int i = 0, total = m_ui->remoteComboBox->count(); i < total; ++i) {
const GerritServer s = m_ui->remoteComboBox->itemData(i).value<GerritServer>();
if (s.host == server.host)
......
......@@ -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;
......
......@@ -47,6 +47,7 @@ public:
bool operator==(const GerritServer &other) const;
QString sshHostArgument() const;
QString url() const;
bool fillFromRemote(const QString &remote, const QString &defaultUser);
QString host;
QString user;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment