GitHandlerUtil: moved runInCurrentThread() implementation to GitHandler.
authorKirill Likhodedov <kirill.likhodedov@jetbrains.com>
Wed, 24 Nov 2010 11:39:49 +0000 (14:39 +0300)
committerKirill Likhodedov <kirill.likhodedov@jetbrains.com>
Wed, 24 Nov 2010 11:39:49 +0000 (14:39 +0300)
plugins/git4idea/src/git4idea/commands/GitHandler.java
plugins/git4idea/src/git4idea/commands/GitHandlerUtil.java

index ded88bd0b26e74b098a9f3199bfdd59a5a39f87f..900e9d2cb4b54e5fe32fb10e49945e0ac8e695a6 100644 (file)
@@ -16,6 +16,7 @@
 package git4idea.commands;
 
 import com.intellij.execution.configurations.GeneralCommandLine;
+import com.intellij.openapi.application.ApplicationManager;
 import com.intellij.openapi.diagnostic.Logger;
 import com.intellij.openapi.project.Project;
 import com.intellij.openapi.vcs.FilePath;
@@ -38,7 +39,15 @@ import org.jetbrains.git4idea.ssh.GitSSHService;
 import java.io.File;
 import java.io.OutputStream;
 import java.nio.charset.Charset;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.LinkedBlockingQueue;
 
 /**
  * A handler for git commands
@@ -50,7 +59,7 @@ public abstract class GitHandler {
 
   private final HashSet<Integer> myIgnoredErrorCodes = new HashSet<Integer>(); // Error codes that are ignored for the handler
   private final List<VcsException> myErrors = Collections.synchronizedList(new ArrayList<VcsException>());
-  private static final Logger log = Logger.getInstance(GitHandler.class.getName());
+  private static final Logger LOG = Logger.getInstance(GitHandler.class.getName());
   final GeneralCommandLine myCommandLine;
   @SuppressWarnings({"FieldAccessedSynchronizedAndUnsynchronized"})
   Process myProcess;
@@ -375,8 +384,8 @@ public abstract class GitHandler {
         myVcs.showCommandLine("cd " + myWorkingDirectory);
         myVcs.showCommandLine(printableCommandLine());
       }
-      if (log.isDebugEnabled()) {
-        log.debug("running git: " + myCommandLine.getCommandLineString() + " in " + myWorkingDirectory);
+      if (LOG.isDebugEnabled()) {
+        LOG.debug("running git: " + myCommandLine.getCommandLineString() + " in " + myWorkingDirectory);
       }
       if (!myNoSSHFlag && myProjectSettings.isIdeaSsh()) {
         GitSSHService ssh = GitSSHIdeaService.getInstance();
@@ -599,4 +608,119 @@ public abstract class GitHandler {
   public boolean isLargeCommandLine() {
     return myCommandLine.getCommandLineString().length() > GitFileUtils.FILE_PATH_LIMIT;
   }
+
+  public void runInCurrentThread(Runnable postStartAction) {
+        final GitVcs vcs = GitVcs.getInstance(myProject);
+    if (vcs == null) { return; }
+
+    boolean suspendable = false;
+    switch (myCommand.lockingPolicy()) {
+      case META:
+        // do nothing no locks are taken for metadata
+        break;
+      case READ:
+        vcs.getCommandLock().readLock().lock();
+        break;
+      case WRITE_SUSPENDABLE:
+        suspendable = true;
+        //noinspection fallthrough
+      case WRITE:
+        vcs.getCommandLock().writeLock().lock();
+        break;
+    }
+    try {
+      if (suspendable) {
+        final Object EXIT = new Object();
+        final Object SUSPEND = new Object();
+        final Object RESUME = new Object();
+        final LinkedBlockingQueue<Object> queue = new LinkedBlockingQueue<Object>();
+        Runnable suspend = new Runnable() {
+          public void run() {
+            queue.add(SUSPEND);
+          }
+        };
+        Runnable resume = new Runnable() {
+          public void run() {
+            queue.add(RESUME);
+          }
+        };
+        setSuspendResume(suspend, resume);
+        start();
+        if (isStarted()) {
+          if (postStartAction != null) {
+            postStartAction.run();
+          }
+          ApplicationManager.getApplication().executeOnPooledThread(new Runnable() {
+            public void run() {
+              waitFor();
+              queue.add(EXIT);
+            }
+          });
+          boolean suspended = false;
+          while (true) {
+            Object action;
+            while (true) {
+              try {
+                action = queue.take();
+                break;
+              }
+              catch (InterruptedException e) {
+                if (LOG.isDebugEnabled()) {
+                  LOG.debug("queue.take() is interrupted", e);
+                }
+              }
+            }
+            if (action == EXIT) {
+              if (suspended) {
+                LOG.error("Exiting while RW lock is suspended (reacquiring W-lock command)");
+                vcs.getCommandLock().writeLock().lock();
+              }
+              break;
+            }
+            else if (action == SUSPEND) {
+              if (suspended) {
+                LOG.error("Suspending suspended W-lock (ignoring command)");
+              }
+              else {
+                vcs.getCommandLock().writeLock().unlock();
+                suspended = true;
+              }
+            }
+            else if (action == RESUME) {
+              if (!suspended) {
+                LOG.error("Resuming not suspended W-lock (ignoring command)");
+              }
+              else {
+                vcs.getCommandLock().writeLock().lock();
+                suspended = false;
+              }
+            }
+          }
+        }
+      }
+      else {
+        start();
+        if (isStarted()) {
+          if (postStartAction != null) {
+            postStartAction.run();
+          }
+          waitFor();
+        }
+      }
+    }
+    finally {
+      switch (myCommand.lockingPolicy()) {
+        case META:
+          // do nothing no locks are taken for metadata
+          break;
+        case READ:
+          vcs.getCommandLock().readLock().unlock();
+          break;
+        case WRITE_SUSPENDABLE:
+        case WRITE:
+          vcs.getCommandLock().writeLock().unlock();
+          break;
+      }
+    }
+  }
 }
index 76337c2591970f367712ef3ea42f92dbcbe69385..1a0e6618a31cd9fc32b82a0675bfdb64475f9b34 100644 (file)
@@ -15,7 +15,6 @@
  */
 package git4idea.commands;
 
-import com.intellij.openapi.application.ApplicationManager;
 import com.intellij.openapi.diagnostic.Logger;
 import com.intellij.openapi.progress.ProgressIndicator;
 import com.intellij.openapi.progress.ProgressManager;
@@ -23,16 +22,14 @@ import com.intellij.openapi.progress.Task;
 import com.intellij.openapi.util.Key;
 import com.intellij.openapi.vcs.VcsException;
 import com.intellij.openapi.vfs.VirtualFile;
-import git4idea.GitVcs;
 import git4idea.i18n.GitBundle;
 import git4idea.ui.GitUIUtil;
 import org.jetbrains.annotations.NonNls;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
 
-import java.awt.*;
+import java.awt.EventQueue;
 import java.util.Collection;
-import java.util.concurrent.LinkedBlockingQueue;
 
 /**
  * Handler utilities that allow running handlers with progress indicators
@@ -185,118 +182,7 @@ public class GitHandlerUtil {
    * @param postStartAction an action that is executed
    */
   static void runInCurrentThread(final GitHandler handler, @Nullable final Runnable postStartAction) {
-    final GitVcs vcs = GitVcs.getInstance(handler.myProject);
-    if (vcs == null) { return; }
-
-    boolean suspendable = false;
-    switch (handler.myCommand.lockingPolicy()) {
-      case META:
-        // do nothing no locks are taken for metadata
-        break;
-      case READ:
-        vcs.getCommandLock().readLock().lock();
-        break;
-      case WRITE_SUSPENDABLE:
-        suspendable = true;
-        //noinspection fallthrough
-      case WRITE:
-        vcs.getCommandLock().writeLock().lock();
-        break;
-    }
-    try {
-      if (suspendable) {
-        final Object EXIT = new Object();
-        final Object SUSPEND = new Object();
-        final Object RESUME = new Object();
-        final LinkedBlockingQueue<Object> queue = new LinkedBlockingQueue<Object>();
-        Runnable suspend = new Runnable() {
-          public void run() {
-            queue.add(SUSPEND);
-          }
-        };
-        Runnable resume = new Runnable() {
-          public void run() {
-            queue.add(RESUME);
-          }
-        };
-        handler.setSuspendResume(suspend, resume);
-        handler.start();
-        if (handler.isStarted()) {
-          if (postStartAction != null) {
-            postStartAction.run();
-          }
-          ApplicationManager.getApplication().executeOnPooledThread(new Runnable() {
-            public void run() {
-              handler.waitFor();
-              queue.add(EXIT);
-            }
-          });
-          boolean suspended = false;
-          while (true) {
-            Object action;
-            while (true) {
-              try {
-                action = queue.take();
-                break;
-              }
-              catch (InterruptedException e) {
-                if (LOG.isDebugEnabled()) {
-                  LOG.debug("queue.take() is interrupted", e);
-                }
-              }
-            }
-            if (action == EXIT) {
-              if (suspended) {
-                LOG.error("Exiting while RW lock is suspended (reacquiring W-lock command)");
-                vcs.getCommandLock().writeLock().lock();
-              }
-              break;
-            }
-            else if (action == SUSPEND) {
-              if (suspended) {
-                LOG.error("Suspending suspended W-lock (ignoring command)");
-              }
-              else {
-                vcs.getCommandLock().writeLock().unlock();
-                suspended = true;
-              }
-            }
-            else if (action == RESUME) {
-              if (!suspended) {
-                LOG.error("Resuming not suspended W-lock (ignoring command)");
-              }
-              else {
-                vcs.getCommandLock().writeLock().lock();
-                suspended = false;
-              }
-            }
-          }
-        }
-      }
-      else {
-        handler.start();
-        if (handler.isStarted()) {
-          if (postStartAction != null) {
-            postStartAction.run();
-          }
-          handler.waitFor();
-        }
-      }
-    }
-    finally {
-      switch (handler.myCommand.lockingPolicy()) {
-        case META:
-          // do nothing no locks are taken for metadata
-          break;
-        case READ:
-          vcs.getCommandLock().readLock().unlock();
-          break;
-        case WRITE_SUSPENDABLE:
-        case WRITE:
-          vcs.getCommandLock().writeLock().unlock();
-          break;
-      }
-    }
+    handler.runInCurrentThread(postStartAction);
   }
 
   /**