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
;
26 class OneBaseStrategy
extends AutoMatchStrategy
{
27 private boolean mySucceeded
;
28 private MultiMap
<VirtualFile
, FilePatchInProgress
> myVariants
;
29 private boolean myCheckExistingVariants
;
31 OneBaseStrategy(VirtualFile baseDir
) {
34 myVariants
= new MultiMap
<VirtualFile
, FilePatchInProgress
>();
35 myCheckExistingVariants
= false;
39 public void acceptPatch(TextFilePatch patch
, Collection
<VirtualFile
> foundByName
) {
40 if (! mySucceeded
) return;
42 if (foundByName
.isEmpty()) {
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()) {
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;
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
);
81 filePatchInProgress
= new FilePatchInProgress(creation
, null, myVariants
.keySet().iterator().next());
83 myResult
.add(filePatchInProgress
);
87 public boolean succeeded() {
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
);
99 boolean sameStrip
= true;
100 for (FilePatchInProgress patch
: patches
) {
101 if (numStrip
== -1) {
102 numStrip
= patch
.getCurrentStrip();
104 if (numStrip
!= patch
.getCurrentStrip()) {
111 privilegedSurvivor
= new Pair
<VirtualFile
, Collection
<FilePatchInProgress
>>(file
, patches
);
115 if (privilegedSurvivor
== null) {
116 final VirtualFile first
= myVariants
.keySet().iterator().next();
117 privilegedSurvivor
= new Pair
<VirtualFile
, Collection
<FilePatchInProgress
>>(first
, myVariants
.get(first
));
120 myVariants
.put(privilegedSurvivor
.getFirst(), privilegedSurvivor
.getSecond());
122 myResult
.addAll(myVariants
.values());