VCS: fix for patch with only new files in it
[fedora-idea.git] / platform / vcs-impl / src / com / intellij / openapi / vcs / changes / patch / OneBaseStrategy.java
blob33a1c634b948422657d2b8a9c977f625efcf4253
1 /*
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.
16 package com.intellij.openapi.vcs.changes.patch;
18 import com.intellij.openapi.diff.impl.patch.TextFilePatch;
19 import com.intellij.openapi.util.Pair;
20 import com.intellij.openapi.vfs.VirtualFile;
21 import com.intellij.util.containers.MultiMap;
22 import org.jetbrains.annotations.Nullable;
24 import java.util.*;
26 class OneBaseStrategy extends AutoMatchStrategy {
27 private boolean mySucceeded;
28 private MultiMap<VirtualFile, FilePatchInProgress> myVariants;
29 private boolean myCheckExistingVariants;
31 OneBaseStrategy(VirtualFile baseDir) {
32 super(baseDir);
33 mySucceeded = true;
34 myVariants = new MultiMap<VirtualFile, FilePatchInProgress>();
35 myCheckExistingVariants = false;
38 @Override
39 public void acceptPatch(TextFilePatch patch, Collection<VirtualFile> foundByName) {
40 if (! mySucceeded) return;
41 // a short check
42 if (foundByName.isEmpty()) {
43 mySucceeded = false;
44 return;
46 final List<FilePatchInProgress> results = new LinkedList<FilePatchInProgress>();
47 final Set<VirtualFile> keysToRemove = new HashSet<VirtualFile>(myVariants.keySet());
48 for (VirtualFile file : foundByName) {
49 final FilePatchInProgress filePatchInProgress = processMatch(patch, file);
50 if (filePatchInProgress != null) {
51 final VirtualFile base = filePatchInProgress.getBase();
52 if (myCheckExistingVariants && (! myVariants.containsKey(base))) continue;
53 keysToRemove.remove(base);
54 results.add(filePatchInProgress);
57 if (myCheckExistingVariants) {
58 for (VirtualFile file : keysToRemove) {
59 myVariants.remove(file);
61 if (myVariants.isEmpty()) {
62 mySucceeded = false;
63 return;
66 final Collection<VirtualFile> exactMatch = filterVariants(patch, foundByName);
67 for (FilePatchInProgress filePatchInProgress : results) {
68 filePatchInProgress.setAutoBases(exactMatch);
69 myVariants.putValue(filePatchInProgress.getBase(), filePatchInProgress);
71 myCheckExistingVariants = true;
74 @Override
75 public void processCreation(TextFilePatch creation) {
76 if (! mySucceeded) return;
77 final FilePatchInProgress filePatchInProgress;
78 if (myVariants.isEmpty()) {
79 filePatchInProgress = new FilePatchInProgress(creation, null, myBaseDir);
80 } else {
81 filePatchInProgress = new FilePatchInProgress(creation, null, myVariants.keySet().iterator().next());
83 myResult.add(filePatchInProgress);
86 @Override
87 public boolean succeeded() {
88 return mySucceeded;
91 @Override
92 public void beforeCreations() {
93 if (! mySucceeded) return;
94 if (myVariants.size() > 1) {
95 Pair<VirtualFile, Collection<FilePatchInProgress>> privilegedSurvivor = null;
96 for (VirtualFile file : myVariants.keySet()) {
97 final Collection<FilePatchInProgress> patches = myVariants.get(file);
98 int numStrip = -1;
99 boolean sameStrip = true;
100 for (FilePatchInProgress patch : patches) {
101 if (numStrip == -1) {
102 numStrip = patch.getCurrentStrip();
103 } else {
104 if (numStrip != patch.getCurrentStrip()) {
105 sameStrip = false;
106 break;
110 if (sameStrip) {
111 privilegedSurvivor = new Pair<VirtualFile, Collection<FilePatchInProgress>>(file, patches);
112 break;
115 if (privilegedSurvivor == null) {
116 final VirtualFile first = myVariants.keySet().iterator().next();
117 privilegedSurvivor = new Pair<VirtualFile, Collection<FilePatchInProgress>>(first, myVariants.get(first));
119 myVariants.clear();
120 myVariants.put(privilegedSurvivor.getFirst(), privilegedSurvivor.getSecond());
122 myResult.addAll(myVariants.values());