VCS[git]: Make version fail warning less obtrusive.

Display correct warning if git is not installed.
Do not display it as error (popping up the log pane) from
IVersionControl.  Introduce separate caching flag to avoid
repeated execution if git is not installed.
This commit is contained in:
Friedemann Kleint
2010-02-16 09:12:37 +01:00
parent 08a1171e28
commit d185889351
3 changed files with 33 additions and 14 deletions

View File

@@ -132,7 +132,8 @@ GitClient::GitClient(GitPlugin* plugin)
m_plugin(plugin), m_plugin(plugin),
m_core(Core::ICore::instance()), m_core(Core::ICore::instance()),
m_repositoryChangedSignalMapper(0), m_repositoryChangedSignalMapper(0),
m_cachedGitVersion(0) m_cachedGitVersion(0),
m_hasCachedGitVersion(false)
{ {
if (QSettings *s = m_core->settings()) { if (QSettings *s = m_core->settings()) {
m_settings.fromSettings(s); m_settings.fromSettings(s);
@@ -1021,16 +1022,24 @@ bool GitClient::synchronousGit(const QString &workingDirectory,
process.setWorkingDirectory(workingDirectory); process.setWorkingDirectory(workingDirectory);
process.setEnvironment(processEnvironment()); process.setEnvironment(processEnvironment());
QStringList args = binary(); QStringList args = binary(); // "cmd /c git" on Windows
const QString executable = args.front(); const QString executable = args.front();
args.pop_front(); args.pop_front();
args.append(gitArguments); args.append(gitArguments);
process.start(executable, args); process.start(executable, args);
process.closeWriteChannel(); process.closeWriteChannel();
if (!process.waitForStarted()) {
if (errorText) {
const QString msg = QString::fromLatin1("Unable to execute '%1': %2:")
.arg(binary().join(QString(QLatin1Char(' '))), process.errorString());
*errorText = msg.toLocal8Bit();
}
return false;
}
if (!process.waitForFinished()) { if (!process.waitForFinished()) {
if (errorText) if (errorText)
*errorText = "Error: Git timed out"; *errorText = "Error: Git timed out.";
process.kill(); process.kill();
return false; return false;
} }
@@ -1556,6 +1565,7 @@ void GitClient::setSettings(const GitSettings &s)
m_settings.toSettings(s); m_settings.toSettings(s);
m_binaryPath = m_settings.gitBinaryPath(); m_binaryPath = m_settings.gitBinaryPath();
m_cachedGitVersion = 0u; m_cachedGitVersion = 0u;
m_hasCachedGitVersion = false;
} }
} }
@@ -1573,16 +1583,20 @@ void GitClient::connectRepositoryChanged(const QString & repository, GitCommand
} }
// determine version as '(major << 16) + (minor << 8) + patch' or 0. // determine version as '(major << 16) + (minor << 8) + patch' or 0.
unsigned GitClient::gitVersion(QString *errorMessage /* = 0 */) unsigned GitClient::gitVersion(bool silent, QString *errorMessage /* = 0 */)
{ {
if (!m_cachedGitVersion) if (!m_hasCachedGitVersion) {
m_cachedGitVersion = synchronousGitVersion(errorMessage); // Do not execute repeatedly if that fails (due to git
// not being installed) until settings are changed.
m_cachedGitVersion = synchronousGitVersion(silent, errorMessage);
m_hasCachedGitVersion = true;
}
return m_cachedGitVersion; return m_cachedGitVersion;
} }
QString GitClient::gitVersionString(QString *errorMessage) QString GitClient::gitVersionString(bool silent, QString *errorMessage)
{ {
if (const unsigned version = gitVersion(errorMessage)) { if (const unsigned version = gitVersion(silent, errorMessage)) {
QString rc; QString rc;
QTextStream(&rc) << (version >> 16) << '.' QTextStream(&rc) << (version >> 16) << '.'
<< (0xFF & (version >> 8)) << '.' << (0xFF & (version >> 8)) << '.'
@@ -1593,7 +1607,7 @@ QString GitClient::gitVersionString(QString *errorMessage)
} }
// determine version as '(major << 16) + (minor << 8) + patch' or 0. // determine version as '(major << 16) + (minor << 8) + patch' or 0.
unsigned GitClient::synchronousGitVersion(QString *errorMessage /* = 0 */) unsigned GitClient::synchronousGitVersion(bool silent, QString *errorMessage /* = 0 */)
{ {
// run git --version // run git --version
QByteArray outputText; QByteArray outputText;
@@ -1603,9 +1617,13 @@ unsigned GitClient::synchronousGitVersion(QString *errorMessage /* = 0 */)
const QString msg = tr("Unable to determine git version: %1").arg(commandOutputFromLocal8Bit(errorText)); const QString msg = tr("Unable to determine git version: %1").arg(commandOutputFromLocal8Bit(errorText));
if (errorMessage) { if (errorMessage) {
*errorMessage = msg; *errorMessage = msg;
} else {
if (silent) {
VCSBase::VCSBaseOutputWindow::instance()->append(msg);
} else { } else {
VCSBase::VCSBaseOutputWindow::instance()->appendError(msg); VCSBase::VCSBaseOutputWindow::instance()->appendError(msg);
} }
}
return 0; return 0;
} }
// cut 'git version 1.6.5.1.sha' // cut 'git version 1.6.5.1.sha'

View File

@@ -147,8 +147,8 @@ public:
QString *branch = 0, QString *errorMessage = 0); QString *branch = 0, QString *errorMessage = 0);
// determine version as '(major << 16) + (minor << 8) + patch' or 0 // determine version as '(major << 16) + (minor << 8) + patch' or 0
// with some smart caching. // with some smart caching.
unsigned gitVersion(QString *errorMessage = 0); unsigned gitVersion(bool silent, QString *errorMessage = 0);
QString gitVersionString(QString *errorMessage = 0); QString gitVersionString(bool silent, QString *errorMessage = 0);
void pull(const QString &workingDirectory); void pull(const QString &workingDirectory);
void push(const QString &workingDirectory); void push(const QString &workingDirectory);
@@ -234,7 +234,7 @@ private:
QByteArray* errorText = 0, QByteArray* errorText = 0,
bool logCommandToWindow = true); bool logCommandToWindow = true);
// determine version as '(major << 16) + (minor << 8) + patch' or 0. // determine version as '(major << 16) + (minor << 8) + patch' or 0.
unsigned synchronousGitVersion(QString *errorMessage = 0); unsigned synchronousGitVersion(bool silent, QString *errorMessage = 0);
enum RevertResult { RevertOk, RevertUnchanged, RevertCanceled, RevertFailed }; enum RevertResult { RevertOk, RevertUnchanged, RevertCanceled, RevertFailed };
RevertResult revertI(QStringList files, bool *isDirectory, QString *errorMessage); RevertResult revertI(QStringList files, bool *isDirectory, QString *errorMessage);
@@ -247,6 +247,7 @@ private:
QString m_binaryPath; QString m_binaryPath;
QSignalMapper *m_repositoryChangedSignalMapper; QSignalMapper *m_repositoryChangedSignalMapper;
unsigned m_cachedGitVersion; unsigned m_cachedGitVersion;
bool m_hasCachedGitVersion;
}; };

View File

@@ -62,7 +62,7 @@ QString GitVersionControl::displayName() const
// Add: Implement using "git add --intent-to-add" starting from 1.6.1 // Add: Implement using "git add --intent-to-add" starting from 1.6.1
static inline bool addOperationSupported() static inline bool addOperationSupported()
{ {
return gitClient()->gitVersion() >= version(1, 6, 1); return gitClient()->gitVersion(true) >= version(1, 6, 1);
} }
bool GitVersionControl::supportsOperation(Operation operation) const bool GitVersionControl::supportsOperation(Operation operation) const