[Mercurial] IDEA-55928. Showing error instead of success notification if repository...
authorKirill Likhodedov <kirill.likhodedov@jetbrains.com>
Thu, 15 Jul 2010 09:52:55 +0000 (13:52 +0400)
committerKirill Likhodedov <kirill.likhodedov@jetbrains.com>
Thu, 15 Jul 2010 09:52:55 +0000 (13:52 +0400)
Update mappings only if the repository is created inside the project dir.
Added an ability to create a repo without any project opened, but due to HgCommandService difficulties this options is not activated yet.

plugins/hg4idea/resources/org/zmlx/hg4idea/HgVcsMessages.properties
plugins/hg4idea/src/org/zmlx/hg4idea/action/HgInit.java
plugins/hg4idea/src/org/zmlx/hg4idea/command/HgInitCommand.java
plugins/hg4idea/src/org/zmlx/hg4idea/ui/HgInitDialog.java

index 46fcc80e8e06d77dfe5080dffba2d815f57ae37d..675d06aac6e79a56a6ec9e624ffe8d79628ea1d0 100644 (file)
@@ -69,6 +69,8 @@ hg4idea.init.already.under.hg.option.use.parent=Use parent repository, but keep
 hg4idea.init.already.under.hg.option.create.repo.here=Create new repository here.
 hg4idea.init.created.notification.title=Mercurial repository created
 hg4idea.init.created.notification.description=Repository was created in {0}
+hg4idea.init.error.title=Error creating Mercurial repository
+hg4idea.init.error.description=Couldn''t create a Mercurial repository in {0}
 
 hg4idea.move.progress=Moving files in the VCS...
 
index f9b1b02702c40870e635bc86e5dabb3b30e3b333..dfc698a7a7c9b846078518e606fc0c54bc953a8b 100644 (file)
@@ -6,11 +6,11 @@ import com.intellij.notification.Notifications;
 import com.intellij.openapi.actionSystem.AnActionEvent;
 import com.intellij.openapi.actionSystem.PlatformDataKeys;
 import com.intellij.openapi.actionSystem.Presentation;
-import com.intellij.openapi.diagnostic.Logger;
 import com.intellij.openapi.project.DumbAwareAction;
 import com.intellij.openapi.project.Project;
 import com.intellij.openapi.vcs.ProjectLevelVcsManager;
 import com.intellij.openapi.vcs.VcsDirectoryMapping;
+import com.intellij.openapi.vfs.VfsUtil;
 import com.intellij.openapi.vfs.VirtualFile;
 import org.zmlx.hg4idea.HgUtil;
 import org.zmlx.hg4idea.HgVcs;
@@ -23,24 +23,17 @@ import java.util.ArrayList;
 import java.util.List;
 
 /**
- * Action for initializing Mercurial repository. Command "hg init".
+ * Action for initializing a Mercurial repository.
+ * Command "hg init".
  * @author Kirill Likhodedov
  */
 public class HgInit extends DumbAwareAction {
 
-  private static final Logger LOG = Logger.getInstance(HgInit.class.getName());
   private Project myProject;
 
-  public HgInit() {
-  }
-
   @Override
   public void actionPerformed(AnActionEvent e) {
     myProject = e.getData(PlatformDataKeys.PROJECT);
-    if (myProject == null) {
-      LOG.warn("[actionPerformed] project is null");
-      return;
-    }
 
     // provide window to select the root directory
     final HgInitDialog hgInitDialog = new HgInitDialog(myProject);
@@ -53,7 +46,7 @@ public class HgInit extends DumbAwareAction {
       return;
     }
 
-    // check if it the project is not yet under mercurial and provide some options in that case
+    // check if the selected folder is not yet under mercurial and provide some options in that case
     final VirtualFile vcsRoot = HgUtil.getNearestHgRoot(selectedRoot);
     VirtualFile mapRoot = selectedRoot;
     if (vcsRoot != null) {
@@ -67,37 +60,43 @@ public class HgInit extends DumbAwareAction {
       if (dialog.getAnswer() == HgInitAlreadyUnderHgDialog.Answer.USE_PARENT_REPO) {
         mapRoot = vcsRoot;
       } else if (dialog.getAnswer() == HgInitAlreadyUnderHgDialog.Answer.CREATE_REPO_HERE) {
-        createRepository(selectedRoot);
+        if (!createRepository(selectedRoot)) {
+          return;
+        }
       }
     } else { // no parent repository => creating the repository here.
-       createRepository(selectedRoot);
+       if (!createRepository(selectedRoot)){
+         return;
+       }
     }
 
-    // update vcs directory mappings
-    mapRoot.refresh(false, false);
-    final String path = mapRoot.equals(myProject.getBaseDir()) ? "" : mapRoot.getPath();
-    final ProjectLevelVcsManager vcsManager = ProjectLevelVcsManager.getInstance(myProject);
-    final List<VcsDirectoryMapping> vcsDirectoryMappings = new ArrayList<VcsDirectoryMapping>(vcsManager.getDirectoryMappings());
-    VcsDirectoryMapping mapping = new VcsDirectoryMapping(path, HgVcs.VCS_NAME);
-    for (int i = 0; i < vcsDirectoryMappings.size(); i++) {
-      final VcsDirectoryMapping m = vcsDirectoryMappings.get(i);
-      if (m.getDirectory().equals(path)) {
-        if (m.getVcs().length() == 0) {
-          vcsDirectoryMappings.set(i, mapping);
-          mapping = null;
-          break;
-        }
-        else if (m.getVcs().equals(mapping.getVcs())) {
-          mapping = null;
-          break;
+    // update vcs directory mappings if new repository was created inside the current project directory
+    if (myProject != null && myProject.getBaseDir() != null && VfsUtil.isAncestor(myProject.getBaseDir(), mapRoot, false)) {
+      mapRoot.refresh(false, false);
+      final String path = mapRoot.equals(myProject.getBaseDir()) ? "" : mapRoot.getPath();
+      final ProjectLevelVcsManager vcsManager = ProjectLevelVcsManager.getInstance(myProject);
+      final List<VcsDirectoryMapping> vcsDirectoryMappings = new ArrayList<VcsDirectoryMapping>(vcsManager.getDirectoryMappings());
+      VcsDirectoryMapping mapping = new VcsDirectoryMapping(path, HgVcs.VCS_NAME);
+      for (int i = 0; i < vcsDirectoryMappings.size(); i++) {
+        final VcsDirectoryMapping m = vcsDirectoryMappings.get(i);
+        if (m.getDirectory().equals(path)) {
+          if (m.getVcs().length() == 0) {
+            vcsDirectoryMappings.set(i, mapping);
+            mapping = null;
+            break;
+          }
+          else if (m.getVcs().equals(mapping.getVcs())) {
+            mapping = null;
+            break;
+          }
         }
       }
+      if (mapping != null) {
+        vcsDirectoryMappings.add(mapping);
+      }
+      vcsManager.setDirectoryMappings(vcsDirectoryMappings);
+      vcsManager.updateActiveVcss();
     }
-    if (mapping != null) {
-      vcsDirectoryMappings.add(mapping);
-    }
-    vcsManager.setDirectoryMappings(vcsDirectoryMappings);
-    vcsManager.updateActiveVcss();
   }
 
   @Override
@@ -108,13 +107,14 @@ public class HgInit extends DumbAwareAction {
     presentation.setVisible(project != null);
   }
 
-  private void createRepository(VirtualFile selectedRoot) {
-    if ((new HgInitCommand(myProject)).execute(selectedRoot)) {
-      Notifications.Bus.notify(new Notification(HgVcs.NOTIFICATION_GROUP_ID,
-                                              HgVcsMessages.message("hg4idea.init.created.notification.title"),
-                                              HgVcsMessages.message("hg4idea.init.created.notification.description", selectedRoot.getPresentableUrl()),
-                                              NotificationType.INFORMATION), myProject);
-    }
+  private boolean createRepository(VirtualFile selectedRoot) {
+    final boolean succeeded = (new HgInitCommand(myProject)).execute(selectedRoot);
+    Notifications.Bus.notify(new Notification(HgVcs.NOTIFICATION_GROUP_ID,
+                                              HgVcsMessages.message(succeeded ? "hg4idea.init.created.notification.title" : "hg4idea.init.error.title"),
+                                              HgVcsMessages.message(succeeded ? "hg4idea.init.created.notification.description" : "hg4idea.init.error.description", 
+                                                                    selectedRoot.getPresentableUrl()),
+                                              succeeded ? NotificationType.INFORMATION : NotificationType.ERROR), myProject);
+    return succeeded;
   }
   
 }
index 96f738793349fc653ee25435ec20353ef61e0f1a..670ba67983c1f8a2ef4d19d7715c7e5fd55e4aa0 100644 (file)
@@ -4,6 +4,9 @@ import com.intellij.openapi.project.Project;
 import com.intellij.openapi.vfs.VirtualFile;
 import org.jetbrains.annotations.NotNull;
 
+import java.util.ArrayList;
+import java.util.List;
+
 /**
  * Representation of the "hg init"
  */
@@ -16,7 +19,10 @@ public class HgInitCommand {
   }
 
   public boolean execute(@NotNull VirtualFile repositoryRoot) {
-    return HgCommandService.getInstance(myProject).execute(repositoryRoot, "init", null) != null;
+    final List<String> args = new ArrayList<String>(1);
+    args.add(repositoryRoot.getPath());
+    final HgCommandResult result = HgCommandService.getInstance(myProject).execute(null, "init", args);
+    return result != null && !HgErrorUtil.isAbort(result);
   }
 
 }
index 1c7d515dff94766a23c5696b0dc07b4d37fc2370..f3750cbd84fa05b19d23b22eae1c3f0328b4fb43 100644 (file)
@@ -48,15 +48,28 @@ public class HgInitDialog extends DialogWrapper {
   private JRadioButton myCreateRepositoryForTheRadioButton;
   private JRadioButton mySelectWhereToCreateRadioButton;
   private TextFieldWithBrowseButton myTextFieldBrowser;
-  private final Project myProject;
+
+  @Nullable private final Project myProject;
+  private final boolean myShowDialog; // basing on this field, show options or invoke file chooser at once
+  private final FileChooserDescriptor myFileDescriptor;
   private VirtualFile mySelectedDir;
-  private FileChooserDescriptor myFileDescriptor;
-  private boolean myIsProjectBaseDirHgRoot; // basing on this field, show options or invoke file chooser at once
 
-  public HgInitDialog(Project project) {
+  public HgInitDialog(@Nullable Project project) {
     super(project);
     myProject = project;
-    myIsProjectBaseDirHgRoot = HgUtil.isHgRoot(myProject.getBaseDir());
+    // a file chooser instead of dialog will be shown immediately if there is no current project or if current project is already an hg root
+    myShowDialog = (myProject != null && !HgUtil.isHgRoot(myProject.getBaseDir()));
+
+    myFileDescriptor = new FileChooserDescriptor(false, true, false, false, false, false) {
+      public void validateSelectedFiles(VirtualFile[] files) throws Exception {
+        if (HgUtil.isHgRoot(files[0])) {
+          throw new ConfigurationException(HgVcsMessages.message("hg4idea.init.this.is.hg.root", files[0].getPresentableUrl()));
+        }
+        updateEverything();
+      }
+    };
+    myFileDescriptor.setHideIgnored(false);
+    
     init();
   }
 
@@ -64,7 +77,9 @@ public class HgInitDialog extends DialogWrapper {
   protected void init() {
     super.init();
     setTitle(HgVcsMessages.message("hg4idea.init.dialog.title"));
-    mySelectedDir = myProject.getBaseDir();
+    if (myProject != null) {
+      mySelectedDir = myProject.getBaseDir();
+    }
 
     mySelectWhereToCreateRadioButton.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e) {
@@ -84,15 +99,6 @@ public class HgInitDialog extends DialogWrapper {
       }
     });
 
-    myFileDescriptor = new FileChooserDescriptor(false, true, false, false, false, false) {
-      public void validateSelectedFiles(VirtualFile[] files) throws Exception {
-        if (HgUtil.isHgRoot(files[0])) {
-          throw new ConfigurationException(HgVcsMessages.message("hg4idea.init.this.is.hg.root", files[0].getPresentableUrl()));
-        }
-        updateEverything();
-      }
-    };
-    myFileDescriptor.setHideIgnored(false);
     myTextFieldBrowser.addBrowseFolderListener(HgVcsMessages.message("hg4idea.init.destination.directory.title"),
                                                HgVcsMessages.message("hg4idea.init.destination.directory.description"),
                                                myProject, myFileDescriptor);
@@ -103,20 +109,17 @@ public class HgInitDialog extends DialogWrapper {
    */
   @Override
   public void show() {
-    if (myIsProjectBaseDirHgRoot) {
-      final VirtualFile[] files = FileChooser.chooseFiles(myProject, myFileDescriptor, myProject.getBaseDir());
-      mySelectedDir = (files.length == 0 ? null : files[0]);
-    } else {
+    if (myShowDialog) {
       super.show();
+    } else {
+      final VirtualFile[] files = FileChooser.chooseFiles(myProject, myFileDescriptor);
+      mySelectedDir = (files.length == 0 ? null : files[0]);
     }
   }
 
   @Override
   public boolean isOK() {
-    if (myIsProjectBaseDirHgRoot) {
-      return mySelectedDir != null;
-    }
-    return super.isOK();
+    return myShowDialog ? super.isOK() : mySelectedDir != null;
   }
 
   @Nullable
@@ -134,7 +137,7 @@ public class HgInitDialog extends DialogWrapper {
    * enable/disable the 'OK' button, show error text and update mySelectedDir. 
    */
   private void updateEverything() {
-    if (myCreateRepositoryForTheRadioButton.isSelected()) {
+    if (myShowDialog && myCreateRepositoryForTheRadioButton.isSelected()) {
       enableOKAction();
       mySelectedDir = myProject.getBaseDir();
     } else {