summaryrefslogtreecommitdiff
path: root/plugins/svn4idea/src/org/jetbrains/idea/svn/rollback/SvnRollbackEnvironment.java
blob: 7b09461b376670b75613661550a38b59f3c8af9d (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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
/*
 * Copyright 2000-2014 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 org.jetbrains.idea.svn.rollback;

import com.intellij.openapi.util.Couple;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.vcs.*;
import com.intellij.openapi.vcs.changes.*;
import com.intellij.openapi.vcs.rollback.DefaultRollbackEnvironment;
import com.intellij.openapi.vcs.rollback.RollbackProgressListener;
import com.intellij.openapi.vfs.VfsUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.Processor;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.idea.svn.*;
import org.jetbrains.idea.svn.api.Depth;
import org.jetbrains.idea.svn.api.EventAction;
import org.jetbrains.idea.svn.api.ProgressEvent;
import org.jetbrains.idea.svn.api.ProgressTracker;
import org.jetbrains.idea.svn.commandLine.SvnBindException;
import org.jetbrains.idea.svn.info.Info;
import org.jetbrains.idea.svn.properties.PropertiesMap;
import org.jetbrains.idea.svn.properties.PropertyConsumer;
import org.jetbrains.idea.svn.properties.PropertyData;
import org.tmatesoft.svn.core.*;
import org.tmatesoft.svn.core.wc.*;
import org.tmatesoft.svn.core.wc2.SvnTarget;

import java.io.File;
import java.io.IOException;
import java.util.*;

/**
 * @author yole
 */
public class SvnRollbackEnvironment extends DefaultRollbackEnvironment {
  private final SvnVcs mySvnVcs;

  public SvnRollbackEnvironment(SvnVcs svnVcs) {
    mySvnVcs = svnVcs;
  }

  @Override
  public String getRollbackOperationName() {
    return SvnBundle.message("action.name.revert");
  }

  public void rollbackChanges(List<Change> changes, final List<VcsException> exceptions, @NotNull final RollbackProgressListener listener) {
    listener.indeterminate();
    final SvnChangeProvider changeProvider = (SvnChangeProvider) mySvnVcs.getChangeProvider();
    final Collection<List<Change>> collections = SvnUtil.splitChangesIntoWc(mySvnVcs, changes);
    for (List<Change> collection : collections) {
      // to be more sure about nested changes, being or being not reverted
      final List<Change> innerChanges = new ArrayList<Change>(collection);
      Collections.sort(innerChanges, ChangesAfterPathComparator.getInstance());
      //for (Change change : innerChanges) {
        rollbackGroupForWc(innerChanges, exceptions, listener, changeProvider);
      //}
    }
  }

  private void rollbackGroupForWc(List<Change> changes,
                                  final List<VcsException> exceptions,
                                  final RollbackProgressListener listener,
                                  SvnChangeProvider changeProvider) {
    final UnversionedAndNotTouchedFilesGroupCollector collector = new UnversionedAndNotTouchedFilesGroupCollector();

    final ChangesChecker checker = new ChangesChecker(changeProvider, collector);
    checker.gather(changes);
    exceptions.addAll(checker.getExceptions());

    ProgressTracker revertHandler = new ProgressTracker() {
      public void consume(ProgressEvent event) {
        if (event.getAction() == EventAction.REVERT) {
          final File file = event.getFile();
          if (file != null) {
            listener.accept(file);
          }
        }
        if (event.getAction() == EventAction.FAILED_REVERT) {
          exceptions.add(new VcsException("Revert failed"));
        }
      }

      public void checkCancelled() {
        listener.checkCanceled();
      }
    };

    final List<CopiedAsideInfo> fromToModified = new ArrayList<CopiedAsideInfo>();
    final Map<File, PropertiesMap> properties = ContainerUtil.newHashMap();
    moveRenamesToTmp(exceptions, fromToModified, properties, collector);
    // adds (deletes)
    // deletes (adds)
    // modifications
    final Reverter reverter = new Reverter(mySvnVcs, revertHandler, exceptions);
    reverter.revert(checker.getForAdds(), true);
    reverter.revert(checker.getForDeletes(), true);
    final List<File> edits = checker.getForEdits();
    reverter.revert(edits.toArray(new File[edits.size()]), false);

    moveGroup(exceptions, fromToModified, properties);

    final List<Couple<File>> toBeDeleted = collector.getToBeDeleted();
    for (Couple<File> pair : toBeDeleted) {
      if (pair.getFirst().exists()) {
        FileUtil.delete(pair.getSecond());
      }
    }
  }

  private void moveRenamesToTmp(List<VcsException> exceptions,
                                List<CopiedAsideInfo> fromToModified,
                                final Map<File, PropertiesMap> properties,
                                final UnversionedAndNotTouchedFilesGroupCollector collector) {
    final Map<File, ThroughRenameInfo> fromTo = collector.getFromTo();
    try {
      final File tmp = FileUtil.createTempDirectory("forRename", "");
      final PropertyConsumer handler = new PropertyConsumer() {
        @Override
        public void handleProperty(File path, PropertyData property) throws SVNException {
          final ThroughRenameInfo info = collector.findToFile(new FilePathImpl(path, path.isDirectory()), null);
          if (info != null) {
            if (!properties.containsKey(info.getTo())) {
              properties.put(info.getTo(), new PropertiesMap());
            }
            properties.get(info.getTo()).put(property.getName(), property.getValue());
          }
        }

        @Override
        public void handleProperty(SVNURL url, PropertyData property) throws SVNException {
        }

        @Override
        public void handleProperty(long revision, PropertyData property) throws SVNException {
        }
      };

      // copy also directories here - for moving with svn
      // also, maybe still use just patching? -> well-tested thing, only deletion of folders might suffer
      // todo: special case: addition + move. mark it
      for (Map.Entry<File, ThroughRenameInfo> entry : fromTo.entrySet()) {
        final File source = entry.getKey();
        final ThroughRenameInfo info = entry.getValue();
        if (info.isVersioned()) {
          mySvnVcs.getFactory(source).createPropertyClient().list(SvnTarget.fromFile(source), SVNRevision.WORKING, Depth.EMPTY, handler);
        }
        if (source.isDirectory()) {
          if (! FileUtil.filesEqual(info.getTo(), info.getFirstTo())) {
            fromToModified.add(new CopiedAsideInfo(info.getParentImmediateReverted(), info.getTo(), info.getFirstTo(), null));
          }
          continue;
        }
        final File tmpFile = FileUtil.createTempFile(tmp, source.getName(), "", false);
        tmpFile.mkdirs();
        FileUtil.delete(tmpFile);
        FileUtil.copy(source, tmpFile);
        fromToModified.add(new CopiedAsideInfo(info.getParentImmediateReverted(), info.getTo(), info.getFirstTo(), tmpFile));
      }
    }
    catch (IOException e) {
      exceptions.add(new VcsException(e));
    }
    catch(VcsException e) {
      exceptions.add(e);
    }
  }

  private void moveGroup(final List<VcsException> exceptions,
                         List<CopiedAsideInfo> fromTo,
                         Map<File, PropertiesMap> properties) {
    Collections.sort(fromTo, new Comparator<CopiedAsideInfo>() {
      @Override
      public int compare(CopiedAsideInfo o1, CopiedAsideInfo o2) {
        return FileUtil.compareFiles(o1.getTo(), o2.getTo());
      }
    });
    for (CopiedAsideInfo info : fromTo) {
      if (info.getParentImmediateReverted().exists()) {
        // parent successfully renamed/moved
        try {
          final File from = info.getFrom();
          final File target = info.getTo();
          if (from != null && ! FileUtil.filesEqual(from, target) && ! target.exists()) {
            SvnFileSystemListener.moveFileWithSvn(mySvnVcs, from, target);
          }
          final File root = info.getTmpPlace();
          if (root == null) continue;
          if (! root.isDirectory()) {
            if (target.exists()) {
              FileUtil.copy(root, target);
            } else {
              FileUtil.rename(root, target);
            }
          } else {
            FileUtil.processFilesRecursively(root, new Processor<File>() {
              @Override
              public boolean process(File file) {
                if (file.isDirectory()) return true;
                String relativePath = FileUtil.getRelativePath(root.getPath(), file.getPath(), File.separatorChar);
                File newFile = new File(target, relativePath);
                newFile.getParentFile().mkdirs();
                try {
                  if (target.exists()) {
                    FileUtil.copy(file, newFile);
                  } else {
                    FileUtil.rename(file, newFile);
                  }
                }
                catch (IOException e) {
                  exceptions.add(new VcsException(e));
                }
                return true;
              }
            });
          }
        }
        catch (IOException e) {
          exceptions.add(new VcsException(e));
        }
        catch (VcsException e) {
          exceptions.add(e);
        }
      }
    }

    applyProperties(properties, exceptions);
  }

  private void applyProperties(Map<File, PropertiesMap> propertiesMap, final List<VcsException> exceptions) {
    for (Map.Entry<File, PropertiesMap> entry : propertiesMap.entrySet()) {
      File file = entry.getKey();
      try {
        mySvnVcs.getFactory(file).createPropertyClient().setProperties(file, entry.getValue());
      }
      catch (VcsException e) {
        exceptions.add(e);
      }
    }
  }

  private static class Reverter {
    @NotNull private final SvnVcs myVcs;
    private ProgressTracker myHandler;
    private final List<VcsException> myExceptions;

    private Reverter(@NotNull SvnVcs vcs, ProgressTracker handler, List<VcsException> exceptions) {
      myVcs = vcs;
      myHandler = handler;
      myExceptions = exceptions;
    }

    public void revert(final File[] files, final boolean recursive) {
      if (files.length == 0) return;
      try {
        // Files passed here are split into groups by root and working copy format - thus we could determine factory based on first file
        myVcs.getFactory(files[0]).createRevertClient().revert(files, Depth.allOrEmpty(recursive), myHandler);
      }
      catch (VcsException e) {
        processRevertError(e);
      }
    }

    private void processRevertError(@NotNull VcsException e) {
      if (e.getCause() instanceof  SVNException) {
        SVNException cause = (SVNException)e.getCause();

        // skip errors on unversioned resources.
        if (cause.getErrorMessage().getErrorCode() != SVNErrorCode.WC_NOT_DIRECTORY) {
          myExceptions.add(e);
        }
      } else {
        myExceptions.add(e);
      }
    }
  }

  public void rollbackMissingFileDeletion(List<FilePath> filePaths, final List<VcsException> exceptions,
                                                        final RollbackProgressListener listener) {
    List<File> files = ChangesUtil.filePathsToFiles(filePaths);
    for (File file : files) {
      listener.accept(file);
      try {
        revertFileOrDir(file);
      } catch (VcsException e) {
        exceptions.add(e);
      } catch (SVNException e) {
        exceptions.add(new VcsException(e));
      }
    }
  }

  private void revertFileOrDir(File file) throws SVNException, VcsException {
    Info info = mySvnVcs.getInfo(file);
    if (info != null) {
      if (info.isFile()) {
        doRevert(file, false);
      } else {
        if (SVNProperty.SCHEDULE_ADD.equals(info.getSchedule())) {
          doRevert(file, true);
        } else {
          boolean is17OrGreater = is17OrGreaterCopy(file, info);
          if (is17OrGreater) {
            doRevert(file, true);
          } else {
            // do update to restore missing directory.
            mySvnVcs.getSvnKitManager().createUpdateClient().doUpdate(file, SVNRevision.HEAD, true);
          }
        }
      }
    } else {
      throw new VcsException("Can not get 'svn info' for " + file.getPath());
    }
  }

  private void doRevert(@NotNull File path, boolean recursive) throws VcsException {
    mySvnVcs.getFactory(path).createRevertClient().revert(new File[]{path}, Depth.allOrFiles(recursive), null);
  }

  private boolean is17OrGreaterCopy(final File file, final Info info) throws VcsException {
    final RootsToWorkingCopies copies = mySvnVcs.getRootsToWorkingCopies();
    WorkingCopy copy = copies.getMatchingCopy(info.getURL());

    if (copy == null) {
      WorkingCopyFormat format = mySvnVcs.getWorkingCopyFormat(file);

      return format.isOrGreater(WorkingCopyFormat.ONE_DOT_SEVEN);
    } else {
      return copy.is17Copy();
    }
  }

  private static class UnversionedAndNotTouchedFilesGroupCollector extends EmptyChangelistBuilder {
    private final List<Couple<File>> myToBeDeleted;
    private final Map<File, ThroughRenameInfo> myFromTo;
    // created by changes
    private TreeMap<String, File> myRenames;
    private Set<String> myAlsoReverted;

    private UnversionedAndNotTouchedFilesGroupCollector() {
      myFromTo = new HashMap<File, ThroughRenameInfo>();
      myToBeDeleted = new ArrayList<Couple<File>>();
    }

    @Override
    public void processUnversionedFile(final VirtualFile file) {
      toFromTo(file);
    }

    private void markRename(@NotNull final File beforeFile, @NotNull final File afterFile) {
      myToBeDeleted.add(Couple.of(beforeFile, afterFile));
    }

    public ThroughRenameInfo findToFile(@NotNull final FilePath file, @Nullable final File firstTo) {
      final String path = FilePathsHelper.convertPath(file);
      if (myAlsoReverted.contains(path)) return null;
      final NavigableMap<String, File> head = myRenames.headMap(path, true);
      if (head == null || head.isEmpty()) return null;
      for (Map.Entry<String, File> entry : head.descendingMap().entrySet()) {
        if (path.equals(entry.getKey())) return null;
        if (path.startsWith(entry.getKey())) {
          final String convertedBase = FileUtil.toSystemIndependentName(entry.getKey());
          final String convertedChild = FileUtil.toSystemIndependentName(file.getPath());
          final String relativePath = FileUtil.getRelativePath(convertedBase, convertedChild, '/');
          assert relativePath != null;
          return new ThroughRenameInfo(entry.getValue(), new File(entry.getValue(), relativePath), firstTo, file.getIOFile(), firstTo != null);
        }
      }
      return null;
    }

    private void toFromTo(VirtualFile file) {
      final FilePathImpl path = new FilePathImpl(file);
      final ThroughRenameInfo info = findToFile(path, null);
      if (info != null) {
        myFromTo.put(path.getIOFile(), info);
      }
    }

    private void processChangeImpl(final Change change) {
      if (change.getAfterRevision() != null) {
        final FilePath after = change.getAfterRevision().getFile();
        final ThroughRenameInfo info = findToFile(after, change.getBeforeRevision() == null ? null : change.getBeforeRevision().getFile().getIOFile());
        if (info != null) {
          myFromTo.put(after.getIOFile(), info);
        }
      }
    }

    @Override
    public void processChange(Change change, VcsKey vcsKey) {
      processChangeImpl(change);
    }

    @Override
    public void processChangeInList(Change change, @Nullable ChangeList changeList, VcsKey vcsKey) {
      processChangeImpl(change);
    }

    @Override
    public void processChangeInList(Change change, String changeListName, VcsKey vcsKey) {
      processChangeImpl(change);
    }

    @Override
    public void processIgnoredFile(VirtualFile file) {
      // as with unversioned
      toFromTo(file);
    }

    public List<Couple<File>> getToBeDeleted() {
      return myToBeDeleted;
    }

    public Map<File, ThroughRenameInfo> getFromTo() {
      return myFromTo;
    }

    public void setRenamesMap(TreeMap<String, File> renames) {
      myRenames = renames;
    }

    public void setAlsoReverted(Set<String> alsoReverted) {
      myAlsoReverted = alsoReverted;
    }
  }

  private static class CopiedAsideInfo {
    private final File myParentImmediateReverted;
    private final File myTo;
    private final File myFrom;
    private final File myTmpPlace;

    private CopiedAsideInfo(File parentImmediateReverted, File to, File from, File tmpPlace) {
      myParentImmediateReverted = parentImmediateReverted;
      myTo = to;
      myFrom = from;
      myTmpPlace = tmpPlace;
    }

    public File getParentImmediateReverted() {
      return myParentImmediateReverted;
    }

    public File getTo() {
      return myTo;
    }

    public File getFrom() {
      return myFrom;
    }

    public File getTmpPlace() {
      return myTmpPlace;
    }

    @Override
    public String toString() {
      return myFrom + " -> " + myTo;
    }
  }

  private static class ThroughRenameInfo {
    private final File myParentImmediateReverted;
    private final File myTo;
    private final File myFirstTo;
    private final File myFrom;
    private final boolean myVersioned;

    private ThroughRenameInfo(File parentImmediateReverted, File to, File firstTo, File from, boolean versioned) {
      myParentImmediateReverted = parentImmediateReverted;
      myTo = to;
      myFrom = from;
      myVersioned = versioned;
      myFirstTo = firstTo;
    }

    public File getFirstTo() {
      return myFirstTo;
    }

    public boolean isVersioned() {
      return myVersioned;
    }

    public File getParentImmediateReverted() {
      return myParentImmediateReverted;
    }

    public File getTo() {
      return myTo;
    }

    public File getFrom() {
      return myFrom;
    }
  }

  // both adds and deletes
  private static abstract class SuperfluousRemover {
    private final Set<File> myParentPaths;

    private SuperfluousRemover() {
      myParentPaths = new HashSet<File>();
    }

    @Nullable
    protected abstract File accept(final Change change);

    public void check(final File file) {
      for (Iterator<File> iterator = myParentPaths.iterator(); iterator.hasNext();) {
        final File parentPath = iterator.next();
        if (VfsUtil.isAncestor(parentPath, file, true)) {
          return;
        } else if (VfsUtil.isAncestor(file, parentPath, true)) {
          iterator.remove();
          // remove others; dont check for 1st variant any more
          for (; iterator.hasNext();) {
            final File innerParentPath = iterator.next();
            if (VfsUtil.isAncestor(file, innerParentPath, true)) {
              iterator.remove();
            }
          }
          // will be added in the end
        }
      }
      myParentPaths.add(file);
    }

    public Set<File> getParentPaths() {
      return myParentPaths;
    }
  }

  private static class ChangesChecker {
    private final SuperfluousRemover myForAdds;
    private final SuperfluousRemover myForDeletes;
    private final List<File> myForEdits;

    private final SvnChangeProvider myChangeProvider;
    private final UnversionedAndNotTouchedFilesGroupCollector myCollector;

    private final List<VcsException> myExceptions;

    private ChangesChecker(SvnChangeProvider changeProvider, UnversionedAndNotTouchedFilesGroupCollector collector) {
      myChangeProvider = changeProvider;
      myCollector = collector;

      myForAdds = new SuperfluousRemover() {
        @Nullable
        @Override
        protected File accept(Change change) {
          final ContentRevision beforeRevision = change.getBeforeRevision();
          final ContentRevision afterRevision = change.getAfterRevision();
          if (beforeRevision == null || MoveRenameReplaceCheck.check(change)) {
            return afterRevision.getFile().getIOFile();
          }
          return null;
        }
      };

      myForDeletes = new SuperfluousRemover() {
        @Nullable
        @Override
        protected File accept(Change change) {
          final ContentRevision beforeRevision = change.getBeforeRevision();
          final ContentRevision afterRevision = change.getAfterRevision();
          if (afterRevision == null || MoveRenameReplaceCheck.check(change)) {
            return beforeRevision.getFile().getIOFile();
          }
          return null;
        }
      };

      myForEdits = new ArrayList<File>();
      myExceptions = new ArrayList<VcsException>();
    }

    public void gather(final List<Change> changes) {
      final TreeMap<String, File> renames = new TreeMap<String, File>();
      final Set<String> alsoReverted = new HashSet<String>();
      final Map<String, FilePath> files = new HashMap<String, FilePath>();
      for (Change change : changes) {
        final ContentRevision beforeRevision = change.getBeforeRevision();
        final ContentRevision afterRevision = change.getAfterRevision();
        final String key = afterRevision == null ? null : FilePathsHelper.convertWithLastSeparator(afterRevision.getFile());
        if (MoveRenameReplaceCheck.check(change)) {
          final File beforeFile = beforeRevision.getFile().getIOFile();
          renames.put(key, beforeFile);
          files.put(key, afterRevision.getFile());
          myCollector.markRename(beforeFile, afterRevision.getFile().getIOFile());
        } else if (afterRevision != null) {
          alsoReverted.add(key);
        }
      }
      if (! renames.isEmpty()) {
        final ArrayList<String> paths = new ArrayList<String>(renames.keySet());
        if (paths.size() > 1) {
          FilterFilePathStrings.getInstance().doFilter(paths);
        }
        myCollector.setRenamesMap(renames);
        myCollector.setAlsoReverted(alsoReverted);
        for (String path : paths) {
          try {
            myChangeProvider.getChanges(files.get(path), true, myCollector);
          }
          catch (SVNException e) {
            myExceptions.add(new VcsException(e));
          }
          catch (SvnBindException e) {
            myExceptions.add(e);
          }
        }
      }

      for (Change change : changes) {
        final ContentRevision afterRevision = change.getAfterRevision();

        boolean checked = getAddDelete(myForAdds, change);
        checked |= getAddDelete(myForDeletes, change);

        if (! checked) {
          myForEdits.add(afterRevision.getFile().getIOFile());
        }
      }
    }

    private boolean getAddDelete(final SuperfluousRemover superfluousRemover, final Change change) {
      final File file = superfluousRemover.accept(change);
      if (file != null) {
        superfluousRemover.check(file);
        return true;
      }
      return false;
    }

    public File[] getForAdds() {
      return convert(myForAdds.getParentPaths());
    }

    public File[] getForDeletes() {
      return convert(myForDeletes.getParentPaths());
    }

    private File[] convert(final Collection<File> paths) {
      return paths.toArray(new File[paths.size()]);
    }

    public List<VcsException> getExceptions() {
      return myExceptions;
    }

    public List<File> getForEdits() {
      return myForEdits;
    }
  }
}