HgPushAction: retrieve number of pushed commits.
HgCommandResultNotifier: able to notify about success.
// 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);
}
}
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);
}
});
}
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)) {
command.execute(new HgCommandResultHandler() {
@Override
public void process(@Nullable HgCommandResult result) {
- new HgCommandResultNotifier(project).process(result);
+ new HgCommandResultNotifier(project).process(result, null, null);
}
});
}
// 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() {
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;
+ }
+
}
@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);
}
});