[hg] Notification about successful push.

HgPushAction: retrieve number of pushed commits.
HgCommandResultNotifier: able to notify about success.
This commit is contained in:
Kirill Likhodedov
2011-04-13 11:53:05 +04:00
parent f5998bc51a
commit 19375d6086
6 changed files with 52 additions and 10 deletions
@@ -12,31 +12,39 @@
// 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);
}
}
@@ -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);
}
});
}
@@ -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)) {
@@ -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);
}
});
}
@@ -12,17 +12,25 @@
// 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;
}
}
@@ -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);
}
});