diff --git a/src/plugins/git/gerrit/gerritserver.cpp b/src/plugins/git/gerrit/gerritserver.cpp index 1c2e8807a45..3c8d028fdf9 100644 --- a/src/plugins/git/gerrit/gerritserver.cpp +++ b/src/plugins/git/gerrit/gerritserver.cpp @@ -113,39 +113,27 @@ QString GerritServer::url(UrlType urlType) const bool GerritServer::fillFromRemote(const QString &remote, const GerritParameters ¶meters) { - static const QRegularExpression remotePattern( - "^(?:(?[^:]+)://)?(?:(?[^@]+)@)?(?[^:/]+)" - "(?::(?\\d+))?:?(?/.*)$"); + const GitRemote r(remote); + if (!r.isValid) + return false; - // 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 : - if (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") + if (r.protocol == "https") type = GerritServer::Https; - else if (protocol == "http") + else if (r.protocol == "http") type = GerritServer::Http; - else if (protocol.isEmpty() || protocol == "ssh") + else if (r.protocol.isEmpty() || r.protocol == "ssh") type = GerritServer::Ssh; else return false; - const QString userName = match.captured("user"); - user.userName = userName.isEmpty() ? parameters.server.user.userName : userName; - host = match.captured("host"); - port = match.captured("port").toUShort(); - if (host.contains("github.com")) // Clearly not gerrit + + user.userName = r.userName.isEmpty() ? parameters.server.user.userName : r.userName; + if (r.host.contains("github.com")) // Clearly not gerrit return false; if (type != GerritServer::Ssh) { curlBinary = parameters.curl; if (curlBinary.isEmpty() || !QFile::exists(curlBinary)) return false; - rootPath = match.captured("path"); + rootPath = r.path; // Strip the last part of the path, which is always the repo name // The rest of the path needs to be inspected to find the root path // (can be http://example.net/review) diff --git a/src/plugins/git/gitclient.cpp b/src/plugins/git/gitclient.cpp index 29c4fd0f243..7fb0790cbfd 100644 --- a/src/plugins/git/gitclient.cpp +++ b/src/plugins/git/gitclient.cpp @@ -3191,6 +3191,39 @@ void GitClient::StashInfo::end() m_stashResult = NotStashed; } +// GitRemote + +GitRemote::GitRemote(const QString &url) +{ + static const QRegularExpression remotePattern( + "^(?:(?[^:]+)://)?(?:(?[^@]+)@)?(?[^:/]+)" + "(?::(?\\d+))?:?(?/.*)$"); + + if (url.isEmpty()) + return; + + // Check for local remotes (refer to the root or relative path) + // On Windows, local paths typically starts with : + if (url.startsWith('/') || url.startsWith('.') + || (HostOsInfo::isWindowsHost() && url[1] == ':')) { + protocol = "file"; + path = QDir::fromNativeSeparators(url); + isValid = QDir(path).exists() || QDir(path + ".git").exists(); + return; + } + + const QRegularExpressionMatch match = remotePattern.match(url); + if (!match.hasMatch()) + return; + + protocol = match.captured("protocol"); + userName = match.captured("user"); + host = match.captured("host"); + port = match.captured("port").toUShort(); + path = match.captured("path"); + isValid = true; +} + } // namespace Internal } // namespace Git diff --git a/src/plugins/git/gitclient.h b/src/plugins/git/gitclient.h index 126461636fe..7563f24531b 100644 --- a/src/plugins/git/gitclient.h +++ b/src/plugins/git/gitclient.h @@ -380,5 +380,17 @@ private: QFutureSynchronizer m_synchronizer; // for commit updates }; +class GitRemote { +public: + GitRemote(const QString &url); + + QString protocol; + QString userName; + QString host; + QString path; + quint16 port = 0; + bool isValid = false; +}; + } // namespace Internal } // namespace Git