From 0a153064e292d6eabc3a7d65989aca89bb87dc86 Mon Sep 17 00:00:00 2001 From: Vladimir Krivosheev Date: Fri, 8 Nov 2013 15:12:27 +0100 Subject: [PATCH] IDEA-100279 git init --- .../src/git4idea/actions/GitInit.java | 68 ++++++++++--------- 1 file changed, 35 insertions(+), 33 deletions(-) diff --git a/plugins/git4idea/src/git4idea/actions/GitInit.java b/plugins/git4idea/src/git4idea/actions/GitInit.java index 04d3ff77ca2e..aec2fb6d5b51 100644 --- a/plugins/git4idea/src/git4idea/actions/GitInit.java +++ b/plugins/git4idea/src/git4idea/actions/GitInit.java @@ -18,7 +18,6 @@ package git4idea.actions; import com.intellij.notification.NotificationType; import com.intellij.openapi.actionSystem.AnActionEvent; import com.intellij.openapi.actionSystem.CommonDataKeys; -import com.intellij.openapi.actionSystem.PlatformDataKeys; import com.intellij.openapi.components.ServiceManager; import com.intellij.openapi.fileChooser.FileChooser; import com.intellij.openapi.fileChooser.FileChooserDescriptor; @@ -33,6 +32,7 @@ import com.intellij.openapi.util.text.StringUtil; import com.intellij.openapi.vcs.ProjectLevelVcsManager; import com.intellij.openapi.vcs.VcsDirectoryMapping; import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.util.Consumer; import com.intellij.vcsUtil.VcsFileUtil; import git4idea.GitUtil; import git4idea.GitVcs; @@ -51,6 +51,7 @@ import java.util.List; */ public class GitInit extends DumbAwareAction { + @Override public void actionPerformed(final AnActionEvent e) { Project project = e.getData(CommonDataKeys.PROJECT); if (project == null) { @@ -65,40 +66,41 @@ public class GitInit extends DumbAwareAction { if (baseDir == null) { baseDir = project.getBaseDir(); } - final VirtualFile root = FileChooser.chooseFile(fcd, project, baseDir); - if (root == null) { - return; - } - if (GitUtil.isUnderGit(root)) { - final int v = Messages.showYesNoDialog(project, - GitBundle.message("init.warning.already.under.git", - StringUtil.escapeXml(root.getPresentableUrl())), - GitBundle.getString("init.warning.title"), - Messages.getWarningIcon()); - if (v != 0) { - return; - } - } + doInit(project, fcd, baseDir, baseDir); + } - Git git = ServiceManager.getService(Git.class); - GitVcs vcs = GitVcs.getInstance(project); - GitCommandResult result = git.init(project, root); - if (!result.success()) { - if (vcs != null && vcs.getExecutableValidator().checkExecutableAndNotifyIfNeeded()) { - GitUIUtil.notify(GitVcs.IMPORTANT_ERROR_NOTIFICATION, project, "Git init failed", result.getErrorOutputAsHtmlString(), - NotificationType.ERROR, null); - } - return; - } + private static void doInit(final Project project, FileChooserDescriptor fcd, VirtualFile baseDir, final VirtualFile finalBaseDir) { + FileChooser.chooseFile(fcd, project, baseDir, new Consumer() { + @Override + public void consume(final VirtualFile root) { + if (GitUtil.isUnderGit(root) && Messages.showYesNoDialog(project, + GitBundle.message("init.warning.already.under.git", + StringUtil.escapeXml(root.getPresentableUrl())), + GitBundle.getString("init.warning.title"), + Messages.getWarningIcon()) != Messages.YES) { + return; + } - if (project.isDefault()) { - return; - } - final String path = root.equals(baseDir) ? "" : root.getPath(); - final Project finalProject = project; - GitVcs.runInBackground(new Task.Backgroundable(finalProject, GitBundle.getString("common.refreshing")) { - public void run(@NotNull ProgressIndicator indicator) { - refreshAndConfigureVcsMappings(finalProject, root, path); + GitCommandResult result = ServiceManager.getService(Git.class).init(project, root); + if (!result.success()) { + GitVcs vcs = GitVcs.getInstance(project); + if (vcs != null && vcs.getExecutableValidator().checkExecutableAndNotifyIfNeeded()) { + GitUIUtil.notify(GitVcs.IMPORTANT_ERROR_NOTIFICATION, project, "Git init failed", result.getErrorOutputAsHtmlString(), + NotificationType.ERROR, null); + } + return; + } + + if (project.isDefault()) { + return; + } + final String path = root.equals(finalBaseDir) ? "" : root.getPath(); + GitVcs.runInBackground(new Task.Backgroundable(project, GitBundle.getString("common.refreshing")) { + @Override + public void run(@NotNull ProgressIndicator indicator) { + refreshAndConfigureVcsMappings(project, root, path); + } + }); } }); }