From 4637d56284be035fb4ac9d6c5c1a24f4ab50d46f Mon Sep 17 00:00:00 2001
From: dt <qtc-committer@nokia.com>
Date: Thu, 18 Jun 2009 14:30:04 +0200
Subject: [PATCH] Don't ask to reload the file, if the user uses git/undo or
 git/revert.

Simply do it.

Task-Nr: 254558
---
 src/plugins/coreplugin/filemanager.cpp      | 30 +++++++++++++++++++++
 src/plugins/coreplugin/filemanager.h        | 19 +++++++++++++
 src/plugins/git/gitclient.cpp               |  1 +
 src/plugins/git/gitplugin.cpp               | 12 ++++++++-
 src/plugins/perforce/perforceplugin.cpp     | 13 ++-------
 src/plugins/subversion/subversionplugin.cpp | 18 +++----------
 6 files changed, 67 insertions(+), 26 deletions(-)

diff --git a/src/plugins/coreplugin/filemanager.cpp b/src/plugins/coreplugin/filemanager.cpp
index c7ae13639ec..e0525edbc89 100644
--- a/src/plugins/coreplugin/filemanager.cpp
+++ b/src/plugins/coreplugin/filemanager.cpp
@@ -602,3 +602,33 @@ QList<IFile *> FileManager::managedFiles(const QString &fileName) const
     }
     return result;
 }
+
+FileChangeBlocker::FileChangeBlocker(const QString &fileName)
+    : m_reload(false)
+{
+    Core::FileManager *fm = Core::ICore::instance()->fileManager();
+    m_files = fm->managedFiles(fileName);
+    foreach (Core::IFile *file, m_files)
+        fm->blockFileChange(file);
+}
+
+FileChangeBlocker::~FileChangeBlocker()
+{
+    Core::IFile::ReloadBehavior tempBehavior = Core::IFile::ReloadAll;
+    Core::FileManager *fm = Core::ICore::instance()->fileManager();
+    foreach (Core::IFile *file, m_files) {
+        if (m_reload)
+            file->modified(&tempBehavior);
+        fm->unblockFileChange(file);
+    }
+}
+
+void FileChangeBlocker::setModifiedReload(bool b)
+{
+    m_reload = b;
+}
+
+bool FileChangeBlocker::modifiedReload() const
+{
+    return m_reload;
+}
diff --git a/src/plugins/coreplugin/filemanager.h b/src/plugins/coreplugin/filemanager.h
index e49541e19c4..558fac55b1e 100644
--- a/src/plugins/coreplugin/filemanager.h
+++ b/src/plugins/coreplugin/filemanager.h
@@ -136,6 +136,25 @@ private:
     bool m_blockActivated;
 };
 
+/*! The FileChangeBlocker blocks all change notifications to all IFile * that
+    match the given filename. And unblocks in the destructor.
+
+    To also reload the IFile in the destructor class set modifiedReload to true
+
+  */
+class CORE_EXPORT FileChangeBlocker
+{
+public:
+    FileChangeBlocker(const QString &fileName);
+    ~FileChangeBlocker();
+    void setModifiedReload(bool reload);
+    bool modifiedReload() const;
+private:
+    QList<IFile *> m_files;
+    bool m_reload;
+    Q_DISABLE_COPY(FileChangeBlocker);
+};
+
 } // namespace Core
 
 #endif // FILEMANAGER_H
diff --git a/src/plugins/git/gitclient.cpp b/src/plugins/git/gitclient.cpp
index b32f4e4653d..74841b74b1b 100644
--- a/src/plugins/git/gitclient.cpp
+++ b/src/plugins/git/gitclient.cpp
@@ -42,6 +42,7 @@
 #include <coreplugin/messagemanager.h>
 #include <coreplugin/progressmanager/progressmanager.h>
 #include <coreplugin/uniqueidmanager.h>
+#include <coreplugin/filemanager.h>
 #include <texteditor/itexteditor.h>
 #include <utils/qtcassert.h>
 #include <vcsbase/vcsbaseeditor.h>
diff --git a/src/plugins/git/gitplugin.cpp b/src/plugins/git/gitplugin.cpp
index 0c8729cd252..595090085cb 100644
--- a/src/plugins/git/gitplugin.cpp
+++ b/src/plugins/git/gitplugin.cpp
@@ -553,7 +553,14 @@ void GitPlugin::undoFileChanges()
     QFileInfo fileInfo = currentFile();
     QString fileName = fileInfo.fileName();
     QString workingDirectory = fileInfo.absolutePath();
-    m_gitClient->checkout(workingDirectory, fileName);
+
+    Core::FileChangeBlocker fcb(fileInfo.filePath());
+    fcb.setModifiedReload(true);
+
+    QString errorMessage;
+    if (!m_gitClient->synchronousCheckout(workingDirectory, QStringList() << fileName, &errorMessage))
+        m_outputWindow->append(errorMessage);
+
 }
 
 void GitPlugin::undoProjectChanges()
@@ -583,6 +590,9 @@ void GitPlugin::unstageFile()
 void GitPlugin::revertFile()
 {
     const QFileInfo fileInfo = currentFile();
+    Core::FileChangeBlocker fcb(fileInfo.filePath());
+    fcb.setModifiedReload(true);
+
     m_gitClient->revert(QStringList(fileInfo.absoluteFilePath()));
 }
 
diff --git a/src/plugins/perforce/perforceplugin.cpp b/src/plugins/perforce/perforceplugin.cpp
index 789f56a60f5..92ac232b832 100644
--- a/src/plugins/perforce/perforceplugin.cpp
+++ b/src/plugins/perforce/perforceplugin.cpp
@@ -444,18 +444,9 @@ void PerforcePlugin::revertCurrentFile()
             return;
     }
 
-    Core::FileManager *fm = Core::ICore::instance()->fileManager();
-    QList<Core::IFile *> files = fm->managedFiles(fileName);
-    foreach (Core::IFile *file, files) {
-        fm->blockFileChange(file);
-    }
+    Core::FileChangeBlocker fcb(fileName);
+    fcb.setModifiedReload(true);
     PerforceResponse result2 = runP4Cmd(QStringList() << QLatin1String("revert") << fileName, QStringList(), CommandToWindow|StdOutToWindow|StdErrToWindow|ErrorToWindow);
-    Core::IFile::ReloadBehavior tempBehavior =
-            Core::IFile::ReloadAll;
-    foreach (Core::IFile *file, files) {
-        file->modified(&tempBehavior);
-        fm->unblockFileChange(file);
-    }
 }
 
 void PerforcePlugin::diffCurrentFile()
diff --git a/src/plugins/subversion/subversionplugin.cpp b/src/plugins/subversion/subversionplugin.cpp
index 64488069912..1c794e84a75 100644
--- a/src/plugins/subversion/subversionplugin.cpp
+++ b/src/plugins/subversion/subversionplugin.cpp
@@ -611,10 +611,8 @@ void SubversionPlugin::revertCurrentFile()
                              QMessageBox::Yes, QMessageBox::No) == QMessageBox::No)
         return;
 
-    Core::FileManager *fm = Core::ICore::instance()->fileManager();
-    QList<Core::IFile *> files = fm->managedFiles(file);
-    foreach (Core::IFile *file, files)
-        fm->blockFileChange(file);
+
+    Core::FileChangeBlocker fcb(file);
 
     // revert
     args.clear();
@@ -622,16 +620,8 @@ void SubversionPlugin::revertCurrentFile()
     args.append(file);
 
     const SubversionResponse revertResponse = runSvn(args, subversionShortTimeOut, true);
-    if (revertResponse.error) {
-        foreach (Core::IFile *file, files)
-            fm->unblockFileChange(file);
-        return;
-    }
-
-    Core::IFile::ReloadBehavior tempBehavior = Core::IFile::ReloadAll;
-    foreach (Core::IFile *file, files) {
-        file->modified(&tempBehavior);
-        fm->unblockFileChange(file);
+    if (!revertResponse.error) {
+        fcb.setModifiedReload(true);
     }
 }
 
-- 
GitLab