[hg] Notification about successful push.
authorKirill Likhodedov <kirill.likhodedov@jetbrains.com>
Tue, 12 Apr 2011 11:59:17 +0000 (15:59 +0400)
committerKirill Likhodedov <kirill.likhodedov@jetbrains.com>
Wed, 13 Apr 2011 07:53:05 +0000 (11:53 +0400)
HgPushAction: retrieve number of pushed commits.
HgCommandResultNotifier: able to notify about success.

plugins/hg4idea/src/org/zmlx/hg4idea/action/HgCommandResultNotifier.java
plugins/hg4idea/src/org/zmlx/hg4idea/action/HgCreateTagAction.java
plugins/hg4idea/src/org/zmlx/hg4idea/action/HgMqRebaseAction.java
plugins/hg4idea/src/org/zmlx/hg4idea/action/HgPullAction.java
plugins/hg4idea/src/org/zmlx/hg4idea/action/HgPushAction.java
plugins/hg4idea/src/org/zmlx/hg4idea/action/HgSwitchWorkingDirectoryAction.java

index 21b977587f737b78937054f8c1b7c7cd7b969492..a89a642854c770a6132d7c45f6266fcc2e7fe2a3 100644 (file)
 // limitations under the License.
 package org.zmlx.hg4idea.action;
 
+import com.intellij.notification.Notification;
+import com.intellij.notification.NotificationType;
+import com.intellij.notification.Notifications;
 import com.intellij.openapi.project.Project;
 import com.intellij.vcsUtil.VcsUtil;
 import org.apache.commons.lang.StringUtils;
+import org.jetbrains.annotations.Nullable;
+import org.zmlx.hg4idea.HgErrorUtil;
+import org.zmlx.hg4idea.HgVcs;
 import org.zmlx.hg4idea.execution.HgCommandResult;
 
 import java.util.List;
 
 final class HgCommandResultNotifier {
 
-  private final Project project;
+  private final Project myProject;
 
   HgCommandResultNotifier(Project project) {
-    this.project = project;
+    myProject = project;
   }
 
-  public void process(HgCommandResult result) {
+  public void process(HgCommandResult result, @Nullable String successTitle, @Nullable String successDescription) {
     List<String> out = result.getOutputLines();
     List<String> err = result.getErrorLines();
     if (!out.isEmpty()) {
-      VcsUtil.showStatusMessage(project, out.get(out.size() - 1));
+      VcsUtil.showStatusMessage(myProject, out.get(out.size() - 1));
     }
     if (!err.isEmpty()) {
       VcsUtil.showErrorMessage(
-        project, "<html>" + StringUtils.join(err, "<br>") + "</html>", "Error"
+        myProject, "<html>" + StringUtils.join(err, "<br>") + "</html>", "Error"
       );
+    } else if (!HgErrorUtil.isAbort(result) && successTitle != null && successDescription != null) {
+      Notifications.Bus.notify(new Notification(HgVcs.NOTIFICATION_GROUP_ID, successTitle, successDescription, NotificationType.INFORMATION), myProject);
     }
   }
 
index af702917e2e18b091718d761d16dd80789c0090a..ef97b082d72037424fdf25dec8c99bca956d890f 100644 (file)
@@ -49,7 +49,7 @@ public class HgCreateTagAction extends HgAbstractGlobalAction {
         new HgTagCreateCommand(project, dialog.getRepository(), dialog.getTagName()).execute(new HgCommandResultHandler() {
           @Override
           public void process(@Nullable HgCommandResult result) {
-            new HgCommandResultNotifier(project).process(result);
+            new HgCommandResultNotifier(project).process(result, null, null);
           }
         });
       }
index 82b398ca848324ec4e4572fb2451370bdf927950..6757d873c33632c1c7ca9c3b0d711f693216d405 100644 (file)
@@ -64,7 +64,7 @@ public class HgMqRebaseAction extends HgAbstractGlobalAction {
         pullCommand.execute(new HgCommandResultHandler() {
           @Override
           public void process(@Nullable HgCommandResult result) {
-            new HgCommandResultNotifier(project).process(result);
+            new HgCommandResultNotifier(project).process(result, null, null);
 
             String currentBranch = new HgTagBranchCommand(project, repository).getCurrentBranch();
             if (StringUtils.isBlank(currentBranch)) {
index 15d705be9c6c9ea3d149a307a232594302134903..25da45b3e8887913bdd1109800a9e271edcf4df2 100644 (file)
@@ -54,7 +54,7 @@ public class HgPullAction extends HgAbstractGlobalAction {
         command.execute(new HgCommandResultHandler() {
           @Override
           public void process(@Nullable HgCommandResult result) {
-            new HgCommandResultNotifier(project).process(result);
+            new HgCommandResultNotifier(project).process(result, null, null);
           }
         });
       }
index 838623bd217750c6bbef51e3af52a180713eb6ef..b4e70d27d85d1fb0761642500e47f1f5e801ad28 100644 (file)
 // limitations under the License.
 package org.zmlx.hg4idea.action;
 
+import com.intellij.openapi.diagnostic.Logger;
 import com.intellij.openapi.project.Project;
+import com.intellij.openapi.util.text.StringUtil;
 import com.intellij.openapi.vfs.VirtualFile;
 import org.jetbrains.annotations.Nullable;
+import org.zmlx.hg4idea.HgErrorUtil;
 import org.zmlx.hg4idea.command.HgPushCommand;
 import org.zmlx.hg4idea.execution.HgCommandResult;
 import org.zmlx.hg4idea.execution.HgCommandResultHandler;
 import org.zmlx.hg4idea.ui.HgPushDialog;
 
 import java.util.Collection;
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 public class HgPushAction extends HgAbstractGlobalAction {
+  private static final Logger LOG = Logger.getInstance(HgPushAction.class);
+  private static Pattern PUSH_COMMITS_PATTERN = Pattern.compile(".*added (\\d+) changesets.*");
 
   protected HgGlobalCommandBuilder getHgGlobalCommandBuilder(final Project project) {
     return new HgGlobalCommandBuilder() {
@@ -52,11 +60,37 @@ public class HgPushAction extends HgAbstractGlobalAction {
         command.execute(new HgCommandResultHandler() {
           @Override
           public void process(@Nullable HgCommandResult result) {
-            new HgCommandResultNotifier(project).process(result);
+            int commitsNum = getNumberOfPushedCommits(result);
+            String title = null;
+            String description = null;
+            if (commitsNum >= 0) {
+              title = "Pushed successfully";
+              description = "Pushed " + commitsNum + " " + StringUtil.pluralize("commit", commitsNum) + ".";
+            }
+            new HgCommandResultNotifier(project).process(result, title, description);
           }
         });
       }
     };
   }
 
+  private static int getNumberOfPushedCommits(HgCommandResult result) {
+    if (!HgErrorUtil.isAbort(result)) {
+      final List<String> outputLines = result.getOutputLines();
+      for (String outputLine : outputLines) {
+        final Matcher matcher = PUSH_COMMITS_PATTERN.matcher(outputLine.trim());
+        if (matcher.matches()) {
+          try {
+            return Integer.parseInt(matcher.group(1));
+          }
+          catch (NumberFormatException e) {
+            LOG.info("getNumberOfPushedCommits ", e);
+            return -1;
+          }
+        }
+      }
+    }
+    return -1;
+  }
+
 }
index d9d18ff94490a7f7b387bcb030c6236796438c74..da7ea7417ed7e34cb2946570497fdfa2ff4003ff 100644 (file)
@@ -61,7 +61,7 @@ public class HgSwitchWorkingDirectoryAction extends HgAbstractGlobalAction {
           @Override
           public void run() {
             HgCommandResult result = command.execute();
-            new HgCommandResultNotifier(project).process(result);
+            new HgCommandResultNotifier(project).process(result, null, null);
             project.getMessageBus().syncPublisher(HgVcs.BRANCH_TOPIC).update(project);
           }
         });