git4idea: IDEADEV-33099: non-git files are now handled more gracefully
[fedora-idea.git] / plugins / git4idea / src / git4idea / vfs / GitVFSListener.java
blob3b7a86c815e28c525fcd879e0e27173f57618c73
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.vfs;
18 import com.intellij.openapi.project.Project;
19 import com.intellij.openapi.vcs.FilePath;
20 import com.intellij.openapi.vcs.VcsException;
21 import com.intellij.openapi.vcs.VcsVFSListener;
22 import com.intellij.openapi.vfs.VirtualFile;
23 import com.intellij.vcsUtil.VcsUtil;
24 import git4idea.GitUtil;
25 import git4idea.GitVcs;
26 import git4idea.commands.GitFileUtils;
27 import git4idea.i18n.GitBundle;
29 import java.util.ArrayList;
30 import java.util.Collection;
31 import java.util.List;
32 import java.util.Map;
34 /**
35 * Git virtual file adapter
37 public class GitVFSListener extends VcsVFSListener {
39 /**
40 * A constructor for listener
42 * @param project a project
43 * @param vcs a vcs for that project
45 public GitVFSListener(final Project project, final GitVcs vcs) {
46 super(project, vcs);
49 /**
50 * {@inheritDoc}
52 protected String getAddTitle() {
53 return GitBundle.getString("vfs.listener.add.title");
56 /**
57 * {@inheritDoc}
59 protected String getSingleFileAddTitle() {
60 return GitBundle.getString("vfs.listener.add.single.title");
63 /**
64 * {@inheritDoc}
66 protected String getSingleFileAddPromptTemplate() {
67 return GitBundle.getString("vfs.listener.add.single.prompt");
70 /**
71 * {@inheritDoc}
73 protected void performAdding(final Collection<VirtualFile> addedFiles, final Map<VirtualFile, VirtualFile> copyFromMap) {
74 Map<VirtualFile, List<VirtualFile>> sortedFiles;
75 try {
76 sortedFiles = GitUtil.sortFilesByGitRoot(addedFiles, true);
78 catch (VcsException e) {
79 ((GitVcs)myVcs).showMessages(e.getMessage());
80 return;
82 // note that copied files are not processed because they are included into added files.
83 for (Map.Entry<VirtualFile, List<VirtualFile>> e : sortedFiles.entrySet()) {
84 try {
85 final VirtualFile root = e.getKey();
86 GitFileUtils.addFiles(myProject, root, e.getValue());
87 GitUtil.refreshFiles(myProject, e.getValue());
89 catch (VcsException ex) {
90 ((GitVcs)myVcs).showMessages(ex.getMessage());
95 /**
96 * Perform adding the files using file paths
98 * @param addedFiles the added files
100 private void performAdding(Collection<FilePath> addedFiles) {
101 Map<VirtualFile, List<FilePath>> sortedFiles;
102 try {
103 sortedFiles = GitUtil.sortFilePathsByGitRoot(addedFiles, true);
105 catch (VcsException e) {
106 ((GitVcs)myVcs).showMessages(e.getMessage());
107 return;
109 // note that copied files are not processed because they are included into added files.
110 for (Map.Entry<VirtualFile, List<FilePath>> e : sortedFiles.entrySet()) {
111 try {
112 final VirtualFile root = e.getKey();
113 GitFileUtils.addPaths(myProject, root, e.getValue());
114 GitUtil.refreshFiles(myProject, e.getValue());
116 catch (VcsException ex) {
117 ((GitVcs)myVcs).showMessages(ex.getMessage());
123 * {@inheritDoc}
125 protected String getDeleteTitle() {
126 return GitBundle.getString("vfs.listener.delete.title");
130 * {@inheritDoc}
132 protected String getSingleFileDeleteTitle() {
133 return GitBundle.getString("vfs.listener.delete.single.title");
137 * {@inheritDoc}
139 protected String getSingleFileDeletePromptTemplate() {
140 return GitBundle.getString("vfs.listener.delete.single.prompt");
144 * {@inheritDoc}
146 protected void performDeletion(final List<FilePath> filesToDelete) {
147 Map<VirtualFile, List<FilePath>> sortedFiles;
148 try {
149 sortedFiles = GitUtil.sortFilePathsByGitRoot(filesToDelete, true);
151 catch (VcsException e) {
152 ((GitVcs)myVcs).showMessages(e.getMessage());
153 return;
155 for (Map.Entry<VirtualFile, List<FilePath>> e : sortedFiles.entrySet()) {
156 try {
157 final VirtualFile root = e.getKey();
158 GitFileUtils.delete(myProject, root, e.getValue());
159 GitUtil.refreshFiles(myProject, e.getValue());
161 catch (VcsException ex) {
162 ((GitVcs)myVcs).showMessages(ex.getMessage());
168 * {@inheritDoc}
170 protected void performMoveRename(final List<MovedFileInfo> movedFiles) {
171 // because git does not tracks moves, the file are just added and deleted.
172 ArrayList<FilePath> added = new ArrayList<FilePath>();
173 ArrayList<FilePath> removed = new ArrayList<FilePath>();
174 for (MovedFileInfo m : movedFiles) {
175 added.add(VcsUtil.getFilePath(m.myNewPath));
176 removed.add(VcsUtil.getFilePath(m.myOldPath));
178 performAdding(added);
179 performDeletion(removed);
183 * {@inheritDoc}
185 protected boolean isDirectoryVersioningSupported() {
186 return false;
190 * {@inheritDoc}
192 @Override
193 protected Collection<FilePath> selectFilePathsToDelete(final List<FilePath> deletedFiles) {
194 // For git asking about vcs delete does not make much sense. The result is practically identical.
195 return deletedFiles;