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.
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
;
35 * Git virtual file adapter
37 public class GitVFSListener
extends VcsVFSListener
{
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
) {
52 protected String
getAddTitle() {
53 return GitBundle
.getString("vfs.listener.add.title");
59 protected String
getSingleFileAddTitle() {
60 return GitBundle
.getString("vfs.listener.add.single.title");
66 protected String
getSingleFileAddPromptTemplate() {
67 return GitBundle
.getString("vfs.listener.add.single.prompt");
73 protected void performAdding(final Collection
<VirtualFile
> addedFiles
, final Map
<VirtualFile
, VirtualFile
> copyFromMap
) {
74 Map
<VirtualFile
, List
<VirtualFile
>> sortedFiles
;
76 sortedFiles
= GitUtil
.sortFilesByGitRoot(addedFiles
, true);
78 catch (VcsException e
) {
79 ((GitVcs
)myVcs
).showMessages(e
.getMessage());
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()) {
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());
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
;
103 sortedFiles
= GitUtil
.sortFilePathsByGitRoot(addedFiles
, true);
105 catch (VcsException e
) {
106 ((GitVcs
)myVcs
).showMessages(e
.getMessage());
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()) {
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());
125 protected String
getDeleteTitle() {
126 return GitBundle
.getString("vfs.listener.delete.title");
132 protected String
getSingleFileDeleteTitle() {
133 return GitBundle
.getString("vfs.listener.delete.single.title");
139 protected String
getSingleFileDeletePromptTemplate() {
140 return GitBundle
.getString("vfs.listener.delete.single.prompt");
146 protected void performDeletion(final List
<FilePath
> filesToDelete
) {
147 Map
<VirtualFile
, List
<FilePath
>> sortedFiles
;
149 sortedFiles
= GitUtil
.sortFilePathsByGitRoot(filesToDelete
, true);
151 catch (VcsException e
) {
152 ((GitVcs
)myVcs
).showMessages(e
.getMessage());
155 for (Map
.Entry
<VirtualFile
, List
<FilePath
>> e
: sortedFiles
.entrySet()) {
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());
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
);
185 protected boolean isDirectoryVersioningSupported() {
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.