git4idea: IDEADEV-40669: git init now refreshes directory in order to prevent incorre...
[fedora-idea.git] / plugins / git4idea / src / git4idea / actions / GitInit.java
blob5058ff4427fd06dc03a19d418abb39aa1fc8e9aa
1 /*
2 * Copyright 2000-2008 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package git4idea.actions;
18 import com.intellij.openapi.actionSystem.AnAction;
19 import com.intellij.openapi.actionSystem.AnActionEvent;
20 import com.intellij.openapi.actionSystem.PlatformDataKeys;
21 import com.intellij.openapi.actionSystem.Presentation;
22 import com.intellij.openapi.fileChooser.FileChooser;
23 import com.intellij.openapi.fileChooser.FileChooserDescriptor;
24 import com.intellij.openapi.project.Project;
25 import com.intellij.openapi.ui.Messages;
26 import com.intellij.openapi.vcs.ProjectLevelVcsManager;
27 import com.intellij.openapi.vcs.VcsDirectoryMapping;
28 import com.intellij.openapi.vcs.VcsException;
29 import com.intellij.openapi.vfs.VirtualFile;
30 import git4idea.GitUtil;
31 import git4idea.GitVcs;
32 import git4idea.commands.GitHandler;
33 import git4idea.commands.GitSimpleHandler;
34 import git4idea.i18n.GitBundle;
35 import git4idea.ui.GitUIUtil;
37 import java.util.ArrayList;
38 import java.util.Collections;
39 import java.util.List;
41 /**
42 * Initialize git repository action
44 public class GitInit extends AnAction {
45 /**
46 * {@inheritDoc}
48 public void actionPerformed(final AnActionEvent e) {
49 final Project project = e.getData(PlatformDataKeys.PROJECT);
50 if (project == null) {
51 return;
53 FileChooserDescriptor fcd = new FileChooserDescriptor(false, true, false, false, false, false);
54 fcd.setShowFileSystemRoots(true);
55 fcd.setTitle(GitBundle.getString("init.destination.directory.title"));
56 fcd.setDescription(GitBundle.getString("init.destination.directory.description"));
57 fcd.setHideIgnored(false);
58 final VirtualFile baseDir = project.getBaseDir();
59 final VirtualFile[] files = FileChooser.chooseFiles(project, fcd, baseDir);
60 if (files.length == 0) {
61 return;
63 VirtualFile root = files[0];
64 if (GitUtil.isUnderGit(root)) {
65 Messages.showErrorDialog(project, GitBundle.message("init.error.already.under.git", root.getPresentableUrl()),
66 GitBundle.getString("init.error.title"));
67 return;
69 try {
70 GitSimpleHandler h = new GitSimpleHandler(project, root, GitHandler.INIT);
71 h.setNoSSH(true);
72 h.run();
74 catch (VcsException ex) {
75 GitUIUtil.showOperationError(project, ex, "git init");
76 return;
78 int rc = Messages.showYesNoDialog(project, GitBundle.getString("init.add.root.message"), GitBundle.getString("init.add.root.title"),
79 Messages.getQuestionIcon());
80 if (rc != 0) {
81 return;
83 root.refresh(false, false);
84 final String path = root.equals(baseDir) ? "" : root.getPath();
85 ProjectLevelVcsManager vcs = ProjectLevelVcsManager.getInstance(project);
86 final List<VcsDirectoryMapping> vcsDirectoryMappings = new ArrayList<VcsDirectoryMapping>(vcs.getDirectoryMappings());
87 VcsDirectoryMapping mapping = new VcsDirectoryMapping(path, GitVcs.getInstance(project).getName());
88 for (int i = 0; i < vcsDirectoryMappings.size(); i++) {
89 final VcsDirectoryMapping m = vcsDirectoryMappings.get(i);
90 if (m.getDirectory().equals(path)) {
91 if (m.getVcs().length() == 0) {
92 vcsDirectoryMappings.set(i, mapping);
93 mapping = null;
94 break;
96 else if (m.getVcs().equals(mapping.getVcs())) {
97 mapping = null;
98 break;
102 if (mapping != null) {
103 vcsDirectoryMappings.add(mapping);
105 vcs.setDirectoryMappings(vcsDirectoryMappings);
106 vcs.updateActiveVcss();
107 GitUtil.refreshFiles(project, Collections.singleton(root));
111 * {@inheritDoc}
113 @Override
114 public void update(AnActionEvent e) {
115 final Project project = e.getData(PlatformDataKeys.PROJECT);
116 Presentation presentation = e.getPresentation();
117 if (project == null) {
118 presentation.setEnabled(false);
119 presentation.setVisible(false);
120 return;
122 presentation.setEnabled(true);
123 presentation.setVisible(true);