summaryrefslogtreecommitdiff
path: root/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/UpdatingChangeListBuilder.java
blob: 93919fcb2bd0ca714867d76f53f91505d9bc9e02 (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
/*
 * Copyright 2000-2009 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 com.intellij.openapi.vcs.changes;

import com.intellij.lifecycle.PeriodicalTasksCloser;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.fileTypes.FileTypeManager;
import com.intellij.openapi.roots.FileIndexFacade;
import com.intellij.openapi.util.Computable;
import com.intellij.openapi.util.Factory;
import com.intellij.openapi.util.Getter;
import com.intellij.openapi.vcs.FilePath;
import com.intellij.openapi.vcs.FilePathImpl;
import com.intellij.openapi.vcs.VcsKey;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;

class UpdatingChangeListBuilder implements ChangelistBuilder {
  private static final Logger LOG = Logger.getInstance("#com.intellij.openapi.vcs.changes.UpdatingChangeListBuilder");
  private final ChangeListWorker myChangeListWorker;
  private final FileHolderComposite myComposite;
  // todo +-
  private final Getter<Boolean> myDisposedGetter;
  private VcsDirtyScope myScope;
  private FoldersCutDownWorker myFoldersCutDownWorker;
  private final IgnoredFilesComponent myIgnoredFilesComponent;
  private final FileIndexFacade myIndex;
  private final ChangeListManagerGate myGate;
  private Factory<JComponent> myAdditionalInfo;

  UpdatingChangeListBuilder(final ChangeListWorker changeListWorker,
                            final FileHolderComposite composite,
                            final Getter<Boolean> disposedGetter,
                            final IgnoredFilesComponent ignoredFilesComponent, final ChangeListManagerGate gate) {
    myChangeListWorker = changeListWorker;
    myComposite = composite;
    myDisposedGetter = disposedGetter;
    myIgnoredFilesComponent = ignoredFilesComponent;
    myGate = gate;
    myIndex = PeriodicalTasksCloser.getInstance().safeGetService(changeListWorker.getProject(), FileIndexFacade.class);
  }

  private void checkIfDisposed() {
    if (myDisposedGetter.get()) throw new ChangeListManagerImpl.DisposedException();
  }

  public void setCurrent(final VcsDirtyScope scope, final FoldersCutDownWorker foldersWorker) {
    myScope = scope;
    myFoldersCutDownWorker = foldersWorker;
  }

  public void processChange(final Change change, VcsKey vcsKey) {
    processChangeInList( change, (ChangeList) null, vcsKey);
  }

  public void processChangeInList(final Change change, @Nullable final ChangeList changeList, final VcsKey vcsKey) {
    checkIfDisposed();

    LOG.debug("[processChangeInList-1] entering, cl name: " + ((changeList == null) ? null: changeList.getName()) +
      " change: " + ChangesUtil.getFilePath(change).getPath());
    final String fileName = ChangesUtil.getFilePath(change).getName();
    if (FileTypeManager.getInstance().isFileIgnored(fileName)) {
      LOG.debug("[processChangeInList-1] file type ignored");
      return;
    }

    ApplicationManager.getApplication().runReadAction(new Runnable() {
      public void run() {
        if (ChangeListManagerImpl.isUnder(change, myScope)) {
          if (changeList != null) {
            LOG.debug("[processChangeInList-1] to add change to cl");
            myChangeListWorker.addChangeToList(changeList.getName(), change, vcsKey);
          } else {
            LOG.debug("[processChangeInList-1] to add to corresponding list");
            myChangeListWorker.addChangeToCorrespondingList(change, vcsKey);
          }
        } else {
          LOG.debug("[processChangeInList-1] not under scope");
        }
      }
    });
  }

  public void processChangeInList(final Change change, final String changeListName, VcsKey vcsKey) {
    checkIfDisposed();

    LocalChangeList list = null;
    if (changeListName != null) {
      list = myChangeListWorker.getCopyByName(changeListName);
      if (list == null) {
        list = myGate.addChangeList(changeListName, null);
      }
    }
    processChangeInList(change, list, vcsKey);
  }

  @Override
  public void removeRegisteredChangeFor(FilePath path) {
    myChangeListWorker.removeRegisteredChangeFor(path);
  }

  private boolean isExcluded(final VirtualFile file) {
    return ApplicationManager.getApplication().runReadAction(new Computable<Boolean>() {
      @Override
      public Boolean compute() {
        checkIfDisposed();
        return myIndex.isExcludedFile(file);
      }
    });
  }

  public void processUnversionedFile(final VirtualFile file) {
    if (LOG.isDebugEnabled()) {
      LOG.debug("processUnversionedFile " + file);
    }
    if (file == null) return;
    checkIfDisposed();
    if (isExcluded(file)) return;
    if (myScope.belongsTo(new FilePathImpl(file))) {
      if (myIgnoredFilesComponent.isIgnoredFile(file)) {
        myComposite.getIgnoredFileHolder().addFile(file);
      }
      else if (myComposite.getIgnoredFileHolder().containsFile(file)) {
        // does not need to add: parent dir is already added
      }
      else {
        myComposite.getVFHolder(FileHolder.HolderType.UNVERSIONED).addFile(file);
      }
      // if a file was previously marked as switched through recursion, remove it from switched list
      myChangeListWorker.removeSwitched(file);
    }
  }

  public void processLocallyDeletedFile(final FilePath file) {
    processLocallyDeletedFile(new LocallyDeletedChange(file));
  }

  public void processLocallyDeletedFile(LocallyDeletedChange locallyDeletedChange) {
    checkIfDisposed();
    final FilePath file = locallyDeletedChange.getPath();
    if (FileTypeManager.getInstance().isFileIgnored(file.getName())) return;
    if (myScope.belongsTo(file)) {
      myChangeListWorker.addLocallyDeleted(locallyDeletedChange);
    }
  }

  public void processModifiedWithoutCheckout(final VirtualFile file) {
    if (file == null) return;
    checkIfDisposed();
    if (isExcluded(file)) return;
    if (myScope.belongsTo(new FilePathImpl(file))) {
      if (LOG.isDebugEnabled()) {
        LOG.debug("processModifiedWithoutCheckout " + file);
      }
      myComposite.getVFHolder(FileHolder.HolderType.MODIFIED_WITHOUT_EDITING).addFile(file);
    }
  }

  public void processIgnoredFile(final VirtualFile file) {
    if (file == null) return;
    checkIfDisposed();
    if (isExcluded(file)) return;
    if (myScope.belongsTo(new FilePathImpl(file))) {
      IgnoredFilesHolder ignoredFilesHolder = myComposite.getIgnoredFileHolder();
      if (ignoredFilesHolder instanceof IgnoredFilesCompositeHolder) {
        IgnoredFilesHolder holder = ((IgnoredFilesCompositeHolder)ignoredFilesHolder).getAppropriateIgnoredHolder();
        if (holder instanceof MapIgnoredFilesHolder) {
          ((MapIgnoredFilesHolder)holder).addByVcsChangeProvider(file);
          return;
        }
      }
      ignoredFilesHolder.addFile(file);
    }
  }

  public void processLockedFolder(final VirtualFile file) {
    if (file == null) return;
    checkIfDisposed();
    if (myScope.belongsTo(new FilePathImpl(file))) {
      if (myFoldersCutDownWorker.addCurrent(file)) {
        myComposite.getVFHolder(FileHolder.HolderType.LOCKED).addFile(file);
      }
    }
  }

  public void processLogicallyLockedFolder(VirtualFile file, LogicalLock logicalLock) {
    if (file == null) return;
    checkIfDisposed();
    if (myScope.belongsTo(new FilePathImpl(file))) {
      ((LogicallyLockedHolder) myComposite.get(FileHolder.HolderType.LOGICALLY_LOCKED)).add(file, logicalLock);
    }
  }

  public void processSwitchedFile(final VirtualFile file, final String branch, final boolean recursive) {
    if (file == null) return;
    checkIfDisposed();
    if (isExcluded(file)) return;
    if (myScope.belongsTo(new FilePathImpl(file))) {
      myChangeListWorker.addSwitched(file, branch, recursive);
    }
  }

  public void processRootSwitch(VirtualFile file, String branch) {
    if (file == null) return;
    checkIfDisposed();
    if (myScope.belongsTo(new FilePathImpl(file))) {
      ((SwitchedFileHolder) myComposite.get(FileHolder.HolderType.ROOT_SWITCH)).addFile(file, branch, false);
    }
  }

  public boolean reportChangesOutsideProject() {
    return false;
  }

  @Override
  public void reportAdditionalInfo(String text) {
    reportAdditionalInfo(ChangesViewManager.createTextStatusFactory(text, true));
  }

  @Override
  public void reportAdditionalInfo(Factory<JComponent> infoComponent) {
    if (myAdditionalInfo == null) {
      myAdditionalInfo = infoComponent;
    }
  }

  public Factory<JComponent> getAdditionalInfo() {
    return myAdditionalInfo;
  }
}