Transform to HgAction, get rid of HgBuilderCommands.
HgPusher to show dialog and perform push, to be able to call it without action.
--- /dev/null
+/*
+ * Copyright 2000-2011 JetBrains s.r.o.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.zmlx.hg4idea;
+
+import com.intellij.openapi.diagnostic.Logger;
+import com.intellij.openapi.project.Project;
+import com.intellij.openapi.util.text.StringUtil;
+import com.intellij.openapi.vcs.ProjectLevelVcsManager;
+import org.jetbrains.annotations.Nullable;
+import org.zmlx.hg4idea.action.HgCommandResultNotifier;
+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.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * @author Kirill Likhodedov
+ */
+public class HgPusher {
+
+ private static final Logger LOG = Logger.getInstance(HgPusher.class);
+ private static Pattern PUSH_COMMITS_PATTERN = Pattern.compile(".*added (\\d+) changesets.*");
+
+ private final Project myProject;
+ private final ProjectLevelVcsManager myVcsManager;
+
+ public HgPusher(Project project) {
+ myProject = project;
+ myVcsManager = ProjectLevelVcsManager.getInstance(project);
+ }
+
+ public void showDialogAndPush() {
+ HgPushDialog dialog = new HgPushDialog(myProject);
+ dialog.setRoots(HgUtil.getHgRepositories(myProject));
+ dialog.show();
+ if (dialog.isOK()) {
+ push(myProject, dialog);
+ }
+ }
+
+ private static void push(final Project project, HgPushDialog dialog) {
+ final HgPushCommand command = new HgPushCommand(project, dialog.getRepository(), dialog.getTarget());
+ command.setRevision(dialog.getRevision());
+ command.setForce(dialog.isForce());
+ command.setBranch(dialog.getBranch());
+ command.execute(new HgCommandResultHandler() {
+ @Override
+ public void process(@Nullable HgCommandResult 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;
+ }
+
+}
import com.intellij.openapi.util.ShutDownTracker;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.text.StringUtil;
-import com.intellij.openapi.vcs.FilePath;
-import com.intellij.openapi.vcs.FileStatus;
-import com.intellij.openapi.vcs.VcsException;
+import com.intellij.openapi.vcs.*;
import com.intellij.openapi.vcs.changes.Change;
import com.intellij.openapi.vcs.changes.ChangeListManager;
import com.intellij.openapi.vcs.changes.ContentRevision;
return filePath;
}
}
+
+ /**
+ * Returns all HG roots in the project.
+ */
+ public static @NotNull List<VirtualFile> getHgRepositories(@NotNull Project project) {
+ final List<VirtualFile> repos = new LinkedList<VirtualFile>();
+ for (VcsRoot root : ProjectLevelVcsManager.getInstance(project).getAllVcsRoots()) {
+ if (HgVcs.VCS_NAME.equals(root.vcs.getName())) {
+ repos.add(root.path);
+ }
+ }
+ return repos;
+ }
+
}
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
-import com.intellij.openapi.vcs.ProjectLevelVcsManager;
-import com.intellij.openapi.vcs.VcsRoot;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.vcsUtil.VcsUtil;
import org.jetbrains.annotations.Nullable;
import org.zmlx.hg4idea.HgUtil;
-import org.zmlx.hg4idea.HgVcs;
import org.zmlx.hg4idea.execution.HgCommandException;
import java.lang.reflect.InvocationTargetException;
import java.util.Collection;
-import java.util.LinkedList;
-import java.util.List;
abstract class HgAbstractGlobalAction extends AnAction {
return;
}
- HgGlobalCommand command = getHgGlobalCommandBuilder(project).build(findRepos(project));
+ HgGlobalCommand command = getHgGlobalCommandBuilder(project).build(HgUtil.getHgRepositories(project));
if (command == null) {
return;
}
Project project = PlatformDataKeys.PROJECT.getData(dataContext);
if (project == null) {
presentation.setEnabled(false);
- return;
- }
- }
-
- private List<VirtualFile> findRepos(Project project) {
- List<VirtualFile> repos = new LinkedList<VirtualFile>();
- VcsRoot[] roots = ProjectLevelVcsManager.getInstance(project).getAllVcsRoots();
- for (VcsRoot root : roots) {
- if (HgVcs.VCS_NAME.equals(root.vcs.getName())) {
- repos.add(root.path);
- }
}
- return repos;
}
protected interface HgGlobalCommand {
--- /dev/null
+/*
+ * Copyright 2000-2011 JetBrains s.r.o.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.zmlx.hg4idea.action;
+
+import com.intellij.openapi.actionSystem.*;
+import com.intellij.openapi.project.Project;
+import com.intellij.openapi.vcs.ProjectLevelVcsManager;
+import com.intellij.openapi.vcs.VcsRoot;
+import com.intellij.openapi.vfs.VirtualFile;
+import org.zmlx.hg4idea.HgVcs;
+
+import java.util.LinkedList;
+import java.util.List;
+
+/**
+ * @author Kirill Likhodedov
+ */
+public abstract class HgAction extends AnAction {
+ @Override
+ public void actionPerformed(AnActionEvent event) {
+ final DataContext dataContext = event.getDataContext();
+ final Project project = PlatformDataKeys.PROJECT.getData(dataContext);
+ if (project == null) {
+ return;
+ }
+
+ execute(project);
+ }
+
+ @Override
+ public void update(AnActionEvent e) {
+ Presentation presentation = e.getPresentation();
+ final DataContext dataContext = e.getDataContext();
+
+ Project project = PlatformDataKeys.PROJECT.getData(dataContext);
+ if (project == null) {
+ presentation.setEnabled(false);
+ }
+ }
+
+ public abstract void execute(Project project);
+
+ private static List<VirtualFile> findRepos(Project project) {
+ List<VirtualFile> repos = new LinkedList<VirtualFile>();
+ VcsRoot[] roots = ProjectLevelVcsManager.getInstance(project).getAllVcsRoots();
+ for (VcsRoot root : roots) {
+ if (HgVcs.VCS_NAME.equals(root.vcs.getName())) {
+ repos.add(root.path);
+ }
+ }
+ return repos;
+ }
+
+}
import java.util.List;
-final class HgCommandResultNotifier {
+public final class HgCommandResultNotifier {
private final Project myProject;
- HgCommandResultNotifier(Project project) {
+ public HgCommandResultNotifier(Project project) {
myProject = project;
}
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 org.zmlx.hg4idea.HgPusher;
-import java.util.Collection;
-import java.util.List;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-
-public class HgPushAction extends HgAbstractGlobalAction {
+public class HgPushAction extends HgAction {
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() {
- public HgGlobalCommand build(Collection<VirtualFile> repos) {
- HgPushDialog dialog = new HgPushDialog(project);
- dialog.setRoots(repos);
- dialog.show();
- if (dialog.isOK()) {
- return buildCommand(dialog, project);
- }
- return null;
- }
- };
- }
-
- private HgGlobalCommand buildCommand(final HgPushDialog dialog, final Project project) {
- return new HgGlobalCommand() {
- public VirtualFile getRepo() {
- return dialog.getRepository();
- }
-
- public void execute() {
- HgPushCommand command = new HgPushCommand(project, dialog.getRepository(), dialog.getTarget());
- command.setRevision(dialog.getRevision());
- command.setForce(dialog.isForce());
- command.setBranch(dialog.getBranch());
- command.execute(new HgCommandResultHandler() {
- @Override
- public void process(@Nullable HgCommandResult 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 execute(final Project project) {
+ new HgPusher(project).showDialogAndPush();
}
}