Git: Fix suggested directory for clone

For:
* remote paths with port number
* local repositories

Task-number: QTCREATORBUG-12651
Change-Id: I7fef5c78499291047781ab48a7fef31d52613198
Reviewed-by: Orgad Shaneh <orgads@gmail.com>
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@digia.com>
Reviewed-by: Tobias Hunger <tobias.hunger@digia.com>
This commit is contained in:
Andre Hartmann
2014-07-13 13:12:12 +02:00
committed by Tobias Hunger
parent 79704ebb8a
commit f6bfd75320
2 changed files with 16 additions and 27 deletions

View File

@@ -44,14 +44,12 @@ struct CloneWizardPagePrivate {
const QString mainLinePostfix; const QString mainLinePostfix;
const QString gitPostFix; const QString gitPostFix;
const QString protocolDelimiter;
QCheckBox *recursiveCheckBox; QCheckBox *recursiveCheckBox;
}; };
CloneWizardPagePrivate::CloneWizardPagePrivate() : CloneWizardPagePrivate::CloneWizardPagePrivate() :
mainLinePostfix(QLatin1String("/mainline.git")), mainLinePostfix(QLatin1String("/mainline.git")),
gitPostFix(QLatin1String(".git")), gitPostFix(QLatin1String(".git")),
protocolDelimiter(QLatin1String("://")),
recursiveCheckBox(0) recursiveCheckBox(0)
{ {
} }
@@ -83,33 +81,21 @@ CloneWizardPage::~CloneWizardPage()
QString CloneWizardPage::directoryFromRepository(const QString &urlIn) const QString CloneWizardPage::directoryFromRepository(const QString &urlIn) const
{ {
/* Try to figure out a good directory name from something like:
* 'user@host:qt/qt.git', 'http://host/qt/qt.git' 'local repo'
* ------> 'qt' . */
const QChar slash = QLatin1Char('/'); const QChar slash = QLatin1Char('/');
QString url = urlIn.trimmed().replace(QLatin1Char('\\'), slash); QString url = urlIn.trimmed().replace(QLatin1Char('\\'), slash);
// remove host
const int protocolDelimiterPos = url.indexOf(d->protocolDelimiter); // "://"
const int startRepoSearchPos = protocolDelimiterPos == -1 ? 0 : protocolDelimiterPos + d->protocolDelimiter.size();
int repoPos = url.indexOf(QLatin1Char(':'), startRepoSearchPos);
if (repoPos == -1)
repoPos = url.indexOf(slash, startRepoSearchPos);
if (repoPos != -1)
url.remove(0, repoPos + 1);
// Remove postfixes // Remove postfixes
if (url.endsWith(d->mainLinePostfix)) { if (url.endsWith(d->mainLinePostfix))
url.truncate(url.size() - d->mainLinePostfix.size()); url.truncate(url.size() - d->mainLinePostfix.size());
} else { else if (url.endsWith(d->gitPostFix))
if (url.endsWith(d->gitPostFix))
url.truncate(url.size() - d->gitPostFix.size()); url.truncate(url.size() - d->gitPostFix.size());
}
// Check for equal parts, something like "qt/qt" -> "qt" // extract repository name (last part of path)
const int slashPos = url.indexOf(slash); int startOfRepoName = url.lastIndexOf(slash);
if (slashPos != -1 && slashPos == (url.size() - 1) / 2) { if (startOfRepoName == -1)
if (url.leftRef(slashPos) == url.rightRef(slashPos)) startOfRepoName = url.lastIndexOf(QLatin1Char(':'));
url.truncate(slashPos); url.remove(0, startOfRepoName);
}
// fix invalid characters // fix invalid characters
const QChar dash = QLatin1Char('-'); const QChar dash = QLatin1Char('-');
url.replace(QRegExp(QLatin1String("[^0-9a-zA-Z_.-]")), dash); url.replace(QRegExp(QLatin1String("[^0-9a-zA-Z_.-]")), dash);
@@ -163,7 +149,6 @@ void Git::CloneWizardPage::testDirectoryFromRepository()
QFETCH(QString, repository); QFETCH(QString, repository);
QFETCH(QString, localDirectory); QFETCH(QString, localDirectory);
QEXPECT_FAIL("ssh with port", "QTCREATORBUG-12651", Abort);
QCOMPARE(directoryFromRepository(repository), localDirectory); QCOMPARE(directoryFromRepository(repository), localDirectory);
} }
#endif #endif

View File

@@ -1619,9 +1619,13 @@ void GitPlugin::testCloneWizard_directoryFromRepository_data()
QTest::addColumn<QString>("localDirectory"); QTest::addColumn<QString>("localDirectory");
QTest::newRow("http") << "http://host/qt/qt.git" << "qt"; QTest::newRow("http") << "http://host/qt/qt.git" << "qt";
QTest::newRow("user@host") << "user@host:qt/qt.git" << "qt"; QTest::newRow("without slash") << "user@host:qt.git" << "qt";
QTest::newRow("local repo") << "/home/user/qt-creator.git" << "home-user-qt-creator"; QTest::newRow("mainline.git") << "git://gitorious.org/gitorious/mainline.git" << "gitorious";
QTest::newRow("local repo (Unix)") << "/home/user/qt-creator.git" << "qt-creator";
QTest::newRow("local repo (Windows)") << "c:\\repos\\qt-creator.git" << "qt-creator";
QTest::newRow("ssh with port") << "ssh://host:29418/qt/qt.git" << "qt"; QTest::newRow("ssh with port") << "ssh://host:29418/qt/qt.git" << "qt";
QTest::newRow("invalid chars removed") << "ssh://host/in%va$lid.git" << "in-va-lid";
QTest::newRow("leading dashs removed") << "https://gerrit.local/--leadingDash" << "leadingDash";
} }
#endif #endif