summaryrefslogtreecommitdiff
path: root/plugins/git4idea/src/git4idea/repo/GitUntrackedFilesHolder.java
blob: 0cf82d9a521f45bbc42cdf2c4814d74ee97e6518 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
 * Copyright 2000-2011 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package git4idea.repo;

import com.intellij.openapi.Disposable;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vcs.VcsException;
import com.intellij.openapi.vcs.changes.ChangeListManager;
import com.intellij.openapi.vcs.changes.VcsDirtyScopeManager;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.VirtualFileManager;
import com.intellij.openapi.vfs.newvfs.BulkFileListener;
import com.intellij.openapi.vfs.newvfs.events.*;
import com.intellij.util.messages.MessageBusConnection;
import git4idea.GitUtil;
import git4idea.commands.Git;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.*;

/**
 * <p>
 *   Stores files which are untracked by the Git repository.
 *   Should be updated by calling {@link #add(com.intellij.openapi.vfs.VirtualFile)} and {@link #remove(java.util.Collection)}
 *   whenever the list of unversioned files changes.
 *   Able to get the list of unversioned files from Git.
 * </p>
 * 
 * <p>
 *   This class is used by {@link git4idea.status.GitNewChangesCollector}.
 *   By keeping track of unversioned files in the Git repository we may invoke
 *   <code>'git status --porcelain --untracked-files=no'</code> which gives a significant speed boost: the command gets more than twice
 *   faster, because it doesn't need to seek for untracked files.
 * </p>
 *
 * <p>
 *   "Keeping track" means the following:
 *   <ul>
 *     <li>
 *       Once a file is created, it is added to untracked (by this class).
 *       Once a file is deleted, it is removed from untracked.
 *     </li>
 *     <li>
 *       Once a file is added to the index, it is removed from untracked.
 *       Once it is removed from the index, it is added to untracked.
 *     </li>
 *   </ul>
 * </p>
 * <p>
 *   In some cases (file creation/deletion) the file is not silently added/removed from the list - instead the file is marked as
 *   "possibly untracked" and Git is asked for the exact status of this file.
 *   It is needed, since the file may be created and added to the index independently, and events may race.
 * </p>
 * <p>
 *   Also, if .git/index changes, then a full refresh is initiated. The reason is not only untracked files tracking, but also handling
 *   committing outside IDEA, etc.
 * </p>
 * <p>
 *   Synchronization policy used in this class:<br/>
 *   myDefinitelyUntrackedFiles is accessed under the myDefinitelyUntrackedFiles lock.<br/>
 *   myPossiblyUntrackedFiles and myReady is accessed under the LOCK lock.<br/>
 *   This is done so, because the latter two variables are accessed from the AWT in after() and we don't want to lock the AWT long,
 *   while myDefinitelyUntrackedFiles is modified along with native request to Git.
 * </p>
 *
 * @author Kirill Likhodedov
 */
public class GitUntrackedFilesHolder implements Disposable, BulkFileListener {

  private static final Logger LOG = Logger.getInstance(GitUntrackedFilesHolder.class);

  private final Project myProject;
  private final VirtualFile myRoot;
  private final ChangeListManager myChangeListManager;
  private final VcsDirtyScopeManager myDirtyScopeManager;
  private final GitRepositoryFiles myRepositoryFiles;
  private final Git myGit;

  private final Set<VirtualFile> myDefinitelyUntrackedFiles = new HashSet<VirtualFile>();
  private final Set<VirtualFile> myPossiblyUntrackedFiles = new HashSet<VirtualFile>();
  private boolean myReady;   // if false, total refresh is needed
  private final Object LOCK = new Object();
  private final GitRepositoryManager myRepositoryManager;

  GitUntrackedFilesHolder(@NotNull GitRepository repository) {
    myProject = repository.getProject();
    myRoot = repository.getRoot();
    myChangeListManager = ChangeListManager.getInstance(myProject);
    myDirtyScopeManager = VcsDirtyScopeManager.getInstance(myProject);
    myGit = ServiceManager.getService(Git.class);

    myRepositoryManager = GitUtil.getRepositoryManager(myProject);
    myRepositoryFiles = GitRepositoryFiles.getInstance(repository.getGitDir());
  }

  void setupVfsListener(@NotNull Project project) {
    if (!project.isDisposed()) {
      MessageBusConnection connection = project.getMessageBus().connect(this);
      connection.subscribe(VirtualFileManager.VFS_CHANGES, this);
    }
  }

  @Override
  public void dispose() {
    synchronized (myDefinitelyUntrackedFiles) {
      myDefinitelyUntrackedFiles.clear();
    }
    synchronized (LOCK) {
      myPossiblyUntrackedFiles.clear();
    }
  }

  /**
   * Adds the file to the list of untracked.
   */
  public void add(@NotNull VirtualFile file) {
    synchronized (myDefinitelyUntrackedFiles) {
      myDefinitelyUntrackedFiles.add(file);
    }
  }

  /**
   * Adds several files to the list of untracked.
   */
  public void add(@NotNull Collection<VirtualFile> files) {
    synchronized (myDefinitelyUntrackedFiles) {
      myDefinitelyUntrackedFiles.addAll(files);
    }
  }

  /**
   * Removes several files from untracked.
   */
  public void remove(@NotNull Collection<VirtualFile> files) {
    synchronized (myDefinitelyUntrackedFiles) {
      myDefinitelyUntrackedFiles.removeAll(files);
    }
  }

  /**
   * Returns the list of unversioned files.
   * This method may be slow, if the full-refresh of untracked files is needed.
   * @return untracked files.
   * @throws VcsException if there is an unexpected error during Git execution.
   */
  @NotNull
  public Collection<VirtualFile> retrieveUntrackedFiles() throws VcsException {
    if (isReady()) {
      verifyPossiblyUntrackedFiles();
    } else {
      rescanAll();
    }
    synchronized (myDefinitelyUntrackedFiles) {
      return new ArrayList<VirtualFile>(myDefinitelyUntrackedFiles);
    }
  }

  public void invalidate() {
    synchronized (LOCK) {
      myReady = false;
    }
  }

  /**
   * Resets the list of untracked files after retrieving the full list of them from Git.
   */
  public void rescanAll() throws VcsException {
    Set<VirtualFile> untrackedFiles = myGit.untrackedFiles(myProject, myRoot, null);
    synchronized (myDefinitelyUntrackedFiles) {
      myDefinitelyUntrackedFiles.clear();
      myDefinitelyUntrackedFiles.addAll(untrackedFiles);
    }
    synchronized (LOCK) {
      myPossiblyUntrackedFiles.clear();
      myReady = true;
    }
  }

  /**
   * @return <code>true</code> if untracked files list is initialized and being kept up-to-date, <code>false</code> if full refresh is needed.
   */
  private boolean isReady() {
    synchronized (LOCK) {
      return myReady;
    }
  }

  /**
   * Queries Git to check the status of {@code myPossiblyUntrackedFiles} and moves them to {@code myDefinitelyUntrackedFiles}.
   */
  private void verifyPossiblyUntrackedFiles() throws VcsException {
    Set<VirtualFile> suspiciousFiles = new HashSet<VirtualFile>();
    synchronized (LOCK) {
      suspiciousFiles.addAll(myPossiblyUntrackedFiles);
      myPossiblyUntrackedFiles.clear();
    }

    synchronized (myDefinitelyUntrackedFiles) {
      Set<VirtualFile> untrackedFiles = myGit.untrackedFiles(myProject, myRoot, suspiciousFiles);
      suspiciousFiles.removeAll(untrackedFiles);
      // files that were suspicious (and thus passed to 'git ls-files'), but are not untracked, are definitely tracked.
      @SuppressWarnings("UnnecessaryLocalVariable")
      Set<VirtualFile> trackedFiles  = suspiciousFiles;

      myDefinitelyUntrackedFiles.addAll(untrackedFiles);
      myDefinitelyUntrackedFiles.removeAll(trackedFiles);
    }
  }

  @Override
  public void before(@NotNull List<? extends VFileEvent> events) {
  }

  @Override
  public void after(@NotNull List<? extends VFileEvent> events) {
    boolean allChanged = false;
    Set<VirtualFile> filesToRefresh = new HashSet<VirtualFile>();

    for (VFileEvent event : events) {
      if (allChanged) {
        break;
      }
      String path = event.getPath();
      if (path == null) {
        continue;
      }
      if (totalRefreshNeeded(path)) {
        allChanged = true;
      }
      else {
        VirtualFile affectedFile = getAffectedFile(event);
        if (notIgnored(affectedFile)) {
          filesToRefresh.add(affectedFile);
        }
      }
    }

    // if index has changed, no need to refresh specific files - we get the full status of all files
    if (allChanged) {
      LOG.info(String.format("GitUntrackedFilesHolder: Index has changed, marking %s recursively dirty", myRoot));
      myDirtyScopeManager.dirDirtyRecursively(myRoot);
      synchronized (LOCK) {
        myReady = false;
      }
    } else {
      synchronized (LOCK) {
        myPossiblyUntrackedFiles.addAll(filesToRefresh);
      }
    }
  }

  private boolean totalRefreshNeeded(@NotNull String path) {
    return indexChanged(path) || externallyCommitted(path) || gitignoreChanged(path);
  }

  private boolean indexChanged(@NotNull String path) {
    return myRepositoryFiles.isIndexFile(path);
  }

  private boolean externallyCommitted(@NotNull String path) {
    return myRepositoryFiles.isCommitMessageFile(path);
  }

  private boolean gitignoreChanged(@NotNull String path) {
    // TODO watch file stored in core.excludesfile
    return path.endsWith(".gitignore") || myRepositoryFiles.isExclude(path);
  }

  @Nullable
  private static VirtualFile getAffectedFile(@NotNull VFileEvent event) {
    if (event instanceof VFileCreateEvent || event instanceof VFileDeleteEvent || event instanceof VFileMoveEvent) {
      return event.getFile();
    } else if (event instanceof VFileCopyEvent) {
      VFileCopyEvent copyEvent = (VFileCopyEvent) event;
      return copyEvent.getNewParent().findChild(copyEvent.getNewChildName());
    }
    return null;
  }


  private boolean notIgnored(@Nullable VirtualFile file) {
    return file != null && belongsToThisRepository(file) && !myChangeListManager.isIgnoredFile(file);
  }

  private boolean belongsToThisRepository(VirtualFile file) {
    final GitRepository repository = myRepositoryManager.getRepositoryForFile(file);
    return repository != null && repository.getRoot().equals(myRoot);
  }
  
}