Gerrit: Factor out server settings

Preparing for gerrit determination by git remote.

Task-number: QTCREATORBUG-8381
Change-Id: Ic862210f40124cd2fb4bb4aa125c7b8e534cd764
Reviewed-by: André Hartmann <aha_1980@gmx.de>
This commit is contained in:
Orgad Shaneh
2017-01-14 23:30:16 +02:00
committed by Orgad Shaneh
parent 5035807f89
commit 2e2c3c39af
6 changed files with 70 additions and 34 deletions

View File

@@ -72,7 +72,7 @@ GerritDialog::GerritDialog(const QSharedPointer<GerritParameters> &p,
, m_repositoryChooserLabel(new QLabel(tr("Apply in:") + ' ', this)) , m_repositoryChooserLabel(new QLabel(tr("Apply in:") + ' ', this))
, m_fetchRunning(false) , m_fetchRunning(false)
{ {
setWindowTitle(tr("Gerrit %1@%2").arg(p->user, p->host)); setWindowTitle(tr("Gerrit %1@%2").arg(p->server.user, p->server.host));
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint); setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
QGroupBox *changesGroup = new QGroupBox(tr("Changes")); QGroupBox *changesGroup = new QGroupBox(tr("Changes"));

View File

@@ -87,7 +87,7 @@ QDebug operator<<(QDebug d, const GerritChange &c)
static inline QString defaultUrl(const QSharedPointer<GerritParameters> &p, int gerritNumber) static inline QString defaultUrl(const QSharedPointer<GerritParameters> &p, int gerritNumber)
{ {
QString result = QLatin1String(p->https ? "https://" : "http://"); QString result = QLatin1String(p->https ? "https://" : "http://");
result += p->host; result += p->server.host;
result += '/'; result += '/';
result += QString::number(gerritNumber); result += QString::number(gerritNumber);
return result; return result;
@@ -197,8 +197,8 @@ QString GerritChange::filterString() const
QStringList GerritChange::gitFetchArguments(const QSharedPointer<GerritParameters> &p) const QStringList GerritChange::gitFetchArguments(const QSharedPointer<GerritParameters> &p) const
{ {
QStringList arguments; QStringList arguments;
const QString url = "ssh://" + p->sshHostArgument() const QString url = "ssh://" + p->server.sshHostArgument()
+ ':' + QString::number(p->port) + '/' + ':' + QString::number(p->server.port) + '/'
+ project; + project;
arguments << "fetch" << url << currentPatchSet.ref; arguments << "fetch" << url << currentPatchSet.ref;
return arguments; return arguments;
@@ -258,7 +258,8 @@ QueryContext::QueryContext(const QStringList &queries,
: QObject(parent) : QObject(parent)
, m_queries(queries) , m_queries(queries)
, m_currentQuery(0) , m_currentQuery(0)
, m_baseArguments({ p->ssh, p->portFlag, QString::number(p->port), p->sshHostArgument(), "gerrit" }) , m_baseArguments({ p->ssh, p->portFlag, QString::number(p->server.port),
p->server.sshHostArgument(), "gerrit" })
{ {
connect(&m_process, &QProcess::readyReadStandardError, connect(&m_process, &QProcess::readyReadStandardError,
this, &QueryContext::readyReadStandardError); this, &QueryContext::readyReadStandardError);
@@ -520,13 +521,13 @@ void GerritModel::refresh(const QString &query)
else else
{ {
const QString statusOpenQuery = "status:open"; const QString statusOpenQuery = "status:open";
if (m_parameters->user.isEmpty()) { if (m_parameters->server.user.isEmpty()) {
queries.push_back(statusOpenQuery); queries.push_back(statusOpenQuery);
} else { } else {
// Owned by: // Owned by:
queries.push_back(statusOpenQuery + " owner:" + m_parameters->user); queries.push_back(statusOpenQuery + " owner:" + m_parameters->server.user);
// For Review by: // For Review by:
queries.push_back(statusOpenQuery + " reviewer:" + m_parameters->user); queries.push_back(statusOpenQuery + " reviewer:" + m_parameters->server.user);
} }
} }

View File

@@ -110,20 +110,21 @@ GerritOptionsWidget::GerritOptionsWidget(QWidget *parent)
GerritParameters GerritOptionsWidget::parameters() const GerritParameters GerritOptionsWidget::parameters() const
{ {
GerritParameters result; GerritParameters result;
result.host = m_hostLineEdit->text().trimmed(); result.server = GerritServer(m_hostLineEdit->text().trimmed(),
result.user = m_userLineEdit->text().trimmed(); static_cast<unsigned short>(m_portSpinBox->value()),
m_userLineEdit->text().trimmed(),
GerritServer::Ssh);
result.ssh = m_sshChooser->path(); result.ssh = m_sshChooser->path();
result.port = m_portSpinBox->value();
result.https = m_httpsCheckBox->isChecked(); result.https = m_httpsCheckBox->isChecked();
return result; return result;
} }
void GerritOptionsWidget::setParameters(const GerritParameters &p) void GerritOptionsWidget::setParameters(const GerritParameters &p)
{ {
m_hostLineEdit->setText(p.host); m_hostLineEdit->setText(p.server.host);
m_userLineEdit->setText(p.user); m_userLineEdit->setText(p.server.user);
m_sshChooser->setPath(p.ssh); m_sshChooser->setPath(p.ssh);
m_portSpinBox->setValue(p.port); m_portSpinBox->setValue(p.server.port);
m_httpsCheckBox->setChecked(p.https); m_httpsCheckBox->setChecked(p.https);
} }

View File

@@ -68,6 +68,25 @@ static inline QString detectSsh()
return ssh; return ssh;
} }
GerritServer::GerritServer()
: host(defaultHostC)
, port(defaultPort)
{
}
GerritServer::GerritServer(const QString &host, unsigned short port,
const QString &user, HostType type)
: host(host)
, user(user)
, port(port)
, type(type)
{}
bool GerritServer::operator==(const GerritServer &other) const
{
return host == other.host && user == other.user && port == other.port && type == other.type;
}
void GerritParameters::setPortFlagBySshType() void GerritParameters::setPortFlagBySshType()
{ {
bool isPlink = false; bool isPlink = false;
@@ -79,30 +98,27 @@ void GerritParameters::setPortFlagBySshType()
} }
GerritParameters::GerritParameters() GerritParameters::GerritParameters()
: host(defaultHostC) : https(true)
, port(defaultPort)
, https(true)
, portFlag(defaultPortFlag) , portFlag(defaultPortFlag)
{ {
} }
QString GerritParameters::sshHostArgument() const QString GerritServer::sshHostArgument() const
{ {
return user.isEmpty() ? host : (user + '@' + host); return user.isEmpty() ? host : (user + '@' + host);
} }
bool GerritParameters::equals(const GerritParameters &rhs) const bool GerritParameters::equals(const GerritParameters &rhs) const
{ {
return port == rhs.port && host == rhs.host && user == rhs.user return server == rhs.server && ssh == rhs.ssh && https == rhs.https;
&& ssh == rhs.ssh && https == rhs.https;
} }
void GerritParameters::toSettings(QSettings *s) const void GerritParameters::toSettings(QSettings *s) const
{ {
s->beginGroup(settingsGroupC); s->beginGroup(settingsGroupC);
s->setValue(hostKeyC, host); s->setValue(hostKeyC, server.host);
s->setValue(userKeyC, user); s->setValue(userKeyC, server.user);
s->setValue(portKeyC, port); s->setValue(portKeyC, server.port);
s->setValue(portFlagKeyC, portFlag); s->setValue(portFlagKeyC, portFlag);
s->setValue(sshKeyC, ssh); s->setValue(sshKeyC, ssh);
s->setValue(httpsKeyC, https); s->setValue(httpsKeyC, https);
@@ -119,10 +135,10 @@ void GerritParameters::saveQueries(QSettings *s) const
void GerritParameters::fromSettings(const QSettings *s) void GerritParameters::fromSettings(const QSettings *s)
{ {
const QString rootKey = QLatin1String(settingsGroupC) + '/'; const QString rootKey = QLatin1String(settingsGroupC) + '/';
host = s->value(rootKey + hostKeyC, defaultHostC).toString(); server.host = s->value(rootKey + hostKeyC, defaultHostC).toString();
user = s->value(rootKey + userKeyC, QString()).toString(); server.user = s->value(rootKey + userKeyC, QString()).toString();
ssh = s->value(rootKey + sshKeyC, QString()).toString(); ssh = s->value(rootKey + sshKeyC, QString()).toString();
port = s->value(rootKey + portKeyC, QVariant(int(defaultPort))).toInt(); server.port = s->value(rootKey + portKeyC, QVariant(int(defaultPort))).toInt();
portFlag = s->value(rootKey + portFlagKeyC, defaultPortFlag).toString(); portFlag = s->value(rootKey + portFlagKeyC, defaultPortFlag).toString();
savedQueries = s->value(rootKey + savedQueriesKeyC, QString()).toString() savedQueries = s->value(rootKey + savedQueriesKeyC, QString()).toString()
.split(','); .split(',');
@@ -133,7 +149,7 @@ void GerritParameters::fromSettings(const QSettings *s)
bool GerritParameters::isValid() const bool GerritParameters::isValid() const
{ {
return !host.isEmpty() && !user.isEmpty() && !ssh.isEmpty(); return !server.host.isEmpty() && !server.user.isEmpty() && !ssh.isEmpty();
} }
} // namespace Internal } // namespace Internal

View File

@@ -32,12 +32,32 @@ QT_FORWARD_DECLARE_CLASS(QSettings)
namespace Gerrit { namespace Gerrit {
namespace Internal { namespace Internal {
class GerritServer
{
public:
enum HostType
{
Http,
Https,
Ssh
};
GerritServer();
GerritServer(const QString &host, unsigned short port, const QString &user, HostType type);
bool operator==(const GerritServer &other) const;
QString sshHostArgument() const;
QString host;
QString user;
unsigned short port = 0;
HostType type = Ssh;
};
class GerritParameters class GerritParameters
{ {
public: public:
GerritParameters(); GerritParameters();
QString sshHostArgument() const;
bool isValid() const; bool isValid() const;
bool equals(const GerritParameters &rhs) const; bool equals(const GerritParameters &rhs) const;
void toSettings(QSettings *) const; void toSettings(QSettings *) const;
@@ -45,9 +65,7 @@ public:
void fromSettings(const QSettings *); void fromSettings(const QSettings *);
void setPortFlagBySshType(); void setPortFlagBySshType();
QString host; GerritServer server;
unsigned short port;
QString user;
QString ssh; QString ssh;
QStringList savedQueries; QStringList savedQueries;
bool https; bool https;

View File

@@ -414,7 +414,7 @@ void GerritPlugin::fetch(const QSharedPointer<GerritChange> &change, int mode)
foreach (QString remote, remotes) { foreach (QString remote, remotes) {
if (remote.endsWith(".git")) if (remote.endsWith(".git"))
remote.chop(4); remote.chop(4);
if (remote.contains(m_parameters->host) && remote.endsWith(change->project)) { if (remote.contains(m_parameters->server.host) && remote.endsWith(change->project)) {
verifiedRepository = true; verifiedRepository = true;
break; break;
} }
@@ -426,7 +426,7 @@ void GerritPlugin::fetch(const QSharedPointer<GerritChange> &change, int mode)
QString remote = submoduleData.url; QString remote = submoduleData.url;
if (remote.endsWith(".git")) if (remote.endsWith(".git"))
remote.chop(4); remote.chop(4);
if (remote.contains(m_parameters->host) && remote.endsWith(change->project) if (remote.contains(m_parameters->server.host) && remote.endsWith(change->project)
&& QFile::exists(repository + '/' + submoduleData.dir)) { && QFile::exists(repository + '/' + submoduleData.dir)) {
repository = QDir::cleanPath(repository + '/' + submoduleData.dir); repository = QDir::cleanPath(repository + '/' + submoduleData.dir);
verifiedRepository = true; verifiedRepository = true;
@@ -440,7 +440,7 @@ void GerritPlugin::fetch(const QSharedPointer<GerritChange> &change, int mode)
ICore::mainWindow(), tr("Remote Not Verified"), ICore::mainWindow(), tr("Remote Not Verified"),
tr("Change host %1\nand project %2\n\nwere not verified among remotes" tr("Change host %1\nand project %2\n\nwere not verified among remotes"
" in %3. Select different folder?") " in %3. Select different folder?")
.arg(m_parameters->host, .arg(m_parameters->server.host,
change->project, change->project,
QDir::toNativeSeparators(repository)), QDir::toNativeSeparators(repository)),
QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel, QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel,