2 * Copyright 2000-2009 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
.progress
.ProgressIndicator
;
19 import com
.intellij
.openapi
.progress
.Task
;
20 import com
.intellij
.openapi
.project
.Project
;
21 import com
.intellij
.openapi
.vcs
.FilePath
;
22 import com
.intellij
.openapi
.vcs
.VcsException
;
23 import com
.intellij
.openapi
.vcs
.VcsVFSListener
;
24 import com
.intellij
.openapi
.vfs
.VirtualFile
;
25 import com
.intellij
.util
.ui
.UIUtil
;
26 import com
.intellij
.vcsUtil
.VcsUtil
;
27 import git4idea
.GitUtil
;
28 import git4idea
.GitVcs
;
29 import git4idea
.commands
.GitFileUtils
;
30 import git4idea
.i18n
.GitBundle
;
31 import org
.jetbrains
.annotations
.NotNull
;
33 import java
.util
.ArrayList
;
34 import java
.util
.Collection
;
35 import java
.util
.List
;
39 * Git virtual file adapter
41 public class GitVFSListener
extends VcsVFSListener
{
44 * A constructor for listener
46 * @param project a project
47 * @param vcs a vcs for that project
49 public GitVFSListener(final Project project
, final GitVcs vcs
) {
56 protected String
getAddTitle() {
57 return GitBundle
.getString("vfs.listener.add.title");
63 protected String
getSingleFileAddTitle() {
64 return GitBundle
.getString("vfs.listener.add.single.title");
70 protected String
getSingleFileAddPromptTemplate() {
71 return GitBundle
.getString("vfs.listener.add.single.prompt");
77 protected void performAdding(final Collection
<VirtualFile
> addedFiles
, final Map
<VirtualFile
, VirtualFile
> copyFromMap
) {
78 final Map
<VirtualFile
, List
<VirtualFile
>> sortedFiles
;
80 sortedFiles
= GitUtil
.sortFilesByGitRoot(addedFiles
, true);
82 catch (VcsException e
) {
83 gitVcs().showMessages(e
.getMessage());
86 gitVcs().runInBackground(new Task
.Backgroundable(myProject
, GitBundle
.getString("add.adding")) {
88 public void run(@NotNull ProgressIndicator indicator
) {
89 // note that copied files are not processed because they are included into added files.
90 for (Map
.Entry
<VirtualFile
, List
<VirtualFile
>> e
: sortedFiles
.entrySet()) {
92 final VirtualFile root
= e
.getKey();
93 indicator
.setText(root
.getPresentableUrl());
94 GitFileUtils
.addFiles(myProject
, root
, e
.getValue());
95 GitUtil
.markFilesDirty(myProject
, e
.getValue());
97 catch (final VcsException ex
) {
98 UIUtil
.invokeLaterIfNeeded(new Runnable() {
100 gitVcs().showMessages(ex
.getMessage());
110 * @return casted vcs instance
112 private GitVcs
gitVcs() {
113 return ((GitVcs
)myVcs
);
117 * Perform adding the files using file paths
119 * @param addedFiles the added files
121 private void performAdding(Collection
<FilePath
> addedFiles
) {
122 final Map
<VirtualFile
, List
<FilePath
>> sortedFiles
;
124 sortedFiles
= GitUtil
.sortFilePathsByGitRoot(addedFiles
, true);
126 catch (VcsException e
) {
127 gitVcs().showMessages(e
.getMessage());
130 gitVcs().runInBackground(new Task
.Backgroundable(myProject
, GitBundle
.getString("add.adding")) {
132 public void run(@NotNull ProgressIndicator indicator
) {
133 for (Map
.Entry
<VirtualFile
, List
<FilePath
>> e
: sortedFiles
.entrySet()) {
135 final VirtualFile root
= e
.getKey();
136 indicator
.setText(root
.getPresentableUrl());
137 GitFileUtils
.addPaths(myProject
, root
, e
.getValue());
138 GitUtil
.markFilesDirty(myProject
, e
.getValue());
140 catch (final VcsException ex
) {
141 UIUtil
.invokeLaterIfNeeded(new Runnable() {
143 gitVcs().showMessages(ex
.getMessage());
155 protected String
getDeleteTitle() {
156 return GitBundle
.getString("vfs.listener.delete.title");
162 protected String
getSingleFileDeleteTitle() {
163 return GitBundle
.getString("vfs.listener.delete.single.title");
169 protected String
getSingleFileDeletePromptTemplate() {
170 return GitBundle
.getString("vfs.listener.delete.single.prompt");
176 protected void performDeletion(final List
<FilePath
> filesToDelete
) {
177 final Map
<VirtualFile
, List
<FilePath
>> sortedFiles
;
179 sortedFiles
= GitUtil
.sortFilePathsByGitRoot(filesToDelete
, true);
181 catch (VcsException e
) {
182 gitVcs().showMessages(e
.getMessage());
185 gitVcs().runInBackground(new Task
.Backgroundable(myProject
, GitBundle
.getString("remove.removing")) {
187 public void run(@NotNull ProgressIndicator indicator
) {
188 for (Map
.Entry
<VirtualFile
, List
<FilePath
>> e
: sortedFiles
.entrySet()) {
190 final VirtualFile root
= e
.getKey();
191 indicator
.setText(root
.getPresentableUrl());
192 GitFileUtils
.delete(myProject
, root
, e
.getValue());
193 GitUtil
.markFilesDirty(myProject
, e
.getValue());
195 catch (final VcsException ex
) {
196 UIUtil
.invokeLaterIfNeeded(new Runnable() {
198 gitVcs().showMessages(ex
.getMessage());
210 protected void performMoveRename(final List
<MovedFileInfo
> movedFiles
) {
211 // because git does not tracks moves, the file are just added and deleted.
212 ArrayList
<FilePath
> added
= new ArrayList
<FilePath
>();
213 ArrayList
<FilePath
> removed
= new ArrayList
<FilePath
>();
214 for (MovedFileInfo m
: movedFiles
) {
215 added
.add(VcsUtil
.getFilePath(m
.myNewPath
));
216 removed
.add(VcsUtil
.getFilePath(m
.myOldPath
));
218 performAdding(added
);
219 performDeletion(removed
);
225 protected boolean isDirectoryVersioningSupported() {
233 protected Collection
<FilePath
> selectFilePathsToDelete(final List
<FilePath
> deletedFiles
) {
234 // For git asking about vcs delete does not make much sense. The result is practically identical.