Skip to content

Commit

Permalink
git: Fixed comparisons of git versions.
Browse files Browse the repository at this point in the history
Lexicographical comparison of version QStrings just doesn't work properly in all
cases.
  • Loading branch information
jaakristioja committed May 10, 2019
1 parent d315fd4 commit ae56287
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
42 changes: 39 additions & 3 deletions src/git.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,49 @@ Git::Git(QObject* p) : QObject(p) {
revsFiles.reserve(MAX_DICT_SIZE);
}

int Git::gitVersionCompare(QString lhs, QString rhs) {
lhs = lhs.trimmed();
rhs = rhs.trimmed();

QRegExp versionRegex("\\d+\\.\\d+\\.\\d+(-rc\\d+)?");
if (!versionRegex.exactMatch(lhs)) {
dbp("ASSERT: Incorrect git version given: \"%1\"", lhs);
return -1;
}
if (!versionRegex.exactMatch(rhs)) {
dbp("ASSERT: Incorrect git version given: \"%1\"", rhs);
return -1;
}

lhs.replace("-rc", ".");
rhs.replace("-rc", ".");
QStringList lcs = lhs.split('.');
QStringList rcs = rhs.split('.');

for (int i = 0; i < 3; ++i) {
uint lc = lcs.takeFirst().toUInt();
uint rc = rcs.takeFirst().toUInt();
if (lc != rc)
return lc < rc ? -1 : 1;
}
if (lcs.isEmpty() != rcs.isEmpty()) // -rc present in one only
return lcs.isEmpty() ? 1 : -1;
if (lcs.isEmpty()) { // -rc present in both
uint lc = lcs.takeFirst().toUInt();
uint rc = rcs.takeFirst().toUInt();
if (lc != rc)
return lc < rc ? -1 : 1;
}
return 0;
}

void Git::checkEnvironment() {

QString version;
if (run("git --version", &version)) {

version = version.section(' ', -1, -1).section('.', 0, 2);
if (version < GIT_VERSION_REQUIRED) {
if (gitVersionCompare(version, GIT_VERSION_REQUIRED) < 0) {

// simply send information, the 'not compatible version'
// policy should be implemented upstream
Expand Down Expand Up @@ -2167,7 +2203,7 @@ bool Git::startRevList(SCList args, FileHistory* fh) {
} else
{} // initCmd << QString("--early-output"); currently disabled

if (gitVersion >= "2.10.0")
if (gitVersionCompare(gitVersion, "2.10.0") >= 0)
initCmd << "--no-show-signature";

return startParseProc(initCmd + args, fh, QString());
Expand All @@ -2190,7 +2226,7 @@ bool Git::startUnappliedList() {

QStringList sl(cmd.split(' '));

if (gitVersion >= "2.10.0")
if (gitVersionCompare(gitVersion, "2.10.0") >= 0)
sl << "--no-show-signature";

sl << unAppliedShaList;
Expand Down
1 change: 1 addition & 0 deletions src/git.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ Q_OBJECT
typedef QList<TreeEntry> TreeInfo;

void setDefaultModel(FileHistory* fh) { revData = fh; }
static int gitVersionCompare(QString lhs, QString rhs);
void checkEnvironment();
void userInfo(SList info);
const QStringList getGitConfigList(bool global);
Expand Down

0 comments on commit ae56287

Please sign in to comment.