summaryrefslogtreecommitdiff
path: root/plugins/svn4idea/src/org/jetbrains/idea/svn/mergeinfo/OneRecursiveShotMergeInfoWorker.java
blob: b17d3317a801f0306398dc10054e5bee7da3fd90 (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
/*
 * Copyright 2000-2010 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.mergeinfo;

import com.intellij.openapi.util.SystemInfo;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.vcs.AreaMap;
import com.intellij.openapi.vcs.VcsException;
import com.intellij.util.PairProcessor;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.idea.svn.SvnPropertyKeys;
import org.jetbrains.idea.svn.api.Depth;
import org.jetbrains.idea.svn.dialogs.MergeContext;
import org.jetbrains.idea.svn.properties.PropertyConsumer;
import org.jetbrains.idea.svn.properties.PropertyData;
import org.jetbrains.idea.svn.properties.PropertyValue;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNMergeRange;
import org.tmatesoft.svn.core.SVNMergeRangeList;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.internal.util.SVNMergeInfoUtil;
import org.tmatesoft.svn.core.internal.util.SVNPathUtil;
import org.tmatesoft.svn.core.wc.SVNRevision;
import org.tmatesoft.svn.core.wc2.SvnTarget;

import java.io.File;
import java.util.Map;

public class OneRecursiveShotMergeInfoWorker implements MergeInfoWorker {

  @NotNull private final MergeContext myMergeContext;

  // subpath [file] (local) to (subpathURL - merged FROM - to ranges list)
  private final AreaMap<String, Map<String, SVNMergeRangeList>> myDataMap;
  private final Object myLock;
  private final String myFromUrlRelative;

  public OneRecursiveShotMergeInfoWorker(@NotNull MergeContext mergeContext) {
    myMergeContext = mergeContext;
    myLock = new Object();

    myDataMap = AreaMap.create(new PairProcessor<String, String>() {
      public boolean process(String parentUrl, String childUrl) {
        if (".".equals(parentUrl)) return true;
        return SVNPathUtil.isAncestor(ensureUrlFromSlash(parentUrl), ensureUrlFromSlash(childUrl));
      }
    });
    final String url = SVNPathUtil.getRelativePath(myMergeContext.getWcInfo().getRepositoryRoot(), myMergeContext.getSourceUrl());
    myFromUrlRelative = ensureUrlFromSlash(url);
  }

  private String ensureUrlFromSlash(final String url) {
    return url.startsWith("/") ? url : "/" + url;
  }
  
  public void prepare() throws VcsException {
    final Depth depth = Depth.allOrEmpty(myMergeContext.getVcs().getSvnConfiguration().isCheckNestedForQuickMerge());
    PropertyConsumer handler = new PropertyConsumer() {
      public void handleProperty(File path, PropertyData property) throws SVNException {
        final String key = keyFromFile(path);
        synchronized (myLock) {
          myDataMap.put(key, SVNMergeInfoUtil
            .parseMergeInfo(new StringBuffer(replaceSeparators(PropertyValue.toString(property.getValue()))), null));
        }
      }

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

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

    File path = new File(myMergeContext.getWcInfo().getPath());

    myMergeContext.getVcs().getFactory(path).createPropertyClient()
      .getProperty(SvnTarget.fromFile(path), SvnPropertyKeys.MERGE_INFO, SVNRevision.WORKING, depth, handler);
  }

  public SvnMergeInfoCache.MergeCheckResult isMerged(final String relativeToRepoURLPath, final long revisionNumber) {
    // should make relative to wc root
    final String relativeToWc = SVNPathUtil.getRelativePath(myFromUrlRelative, ensureUrlFromSlash(relativeToRepoURLPath));
    if (relativeToWc == null) return SvnMergeInfoCache.MergeCheckResult.NOT_EXISTS;

    final InfoProcessor processor = new InfoProcessor(relativeToWc, myFromUrlRelative, revisionNumber);
    synchronized (myLock) {
      myDataMap.getSimiliar(keyFromPath(relativeToWc), processor);
    }
    return SvnMergeInfoCache.MergeCheckResult.getInstance(processor.isMerged());
  }

  private static class InfoProcessor implements PairProcessor<String, Map<String, SVNMergeRangeList>> {
    private final String myWcLevelRelativeSourceUrl;
    private boolean myMerged;
    private final String myFilePathAsked;
    private final long myRevisionAsked;

    public InfoProcessor(final String filePathAsked, final String wcLevelRelativeSourceUrl, final long revisionAsked) {
      myFilePathAsked = filePathAsked;
      myRevisionAsked = revisionAsked;
      myWcLevelRelativeSourceUrl = wcLevelRelativeSourceUrl.startsWith("/") ? wcLevelRelativeSourceUrl : "/" + wcLevelRelativeSourceUrl;
    }

    public boolean isMerged() {
      return myMerged;
    }

    public boolean process(final String relativeFileSubpath, Map<String, SVNMergeRangeList> map) {
      boolean processed = false;
      final boolean self = relativeFileSubpath.equals(myFilePathAsked);

      if (map.isEmpty()) {
        myMerged = false;
        return true;
      }
      for (Map.Entry<String, SVNMergeRangeList> entry : map.entrySet()) {
        String relativeUrl = entry.getKey();

        boolean urlMatches = false;
        if (".".equals(relativeUrl) || "".equals(relativeUrl)) {
          urlMatches = true;
        } else {
          relativeUrl = (relativeUrl.startsWith("/")) ? relativeUrl : "/" + relativeUrl;
          urlMatches = SVNPathUtil.isAncestor(myWcLevelRelativeSourceUrl, relativeUrl);
        }

        if (! urlMatches) continue;
        processed = true;

        final SVNMergeRangeList rangesList = entry.getValue();

        for (SVNMergeRange range : rangesList.getRanges()) {
          // SVN does not include start revision in range
          final long startRevision = range.getStartRevision() + 1;
          final long endRevision = range.getEndRevision();
          final boolean isInheritable = range.isInheritable();
          final boolean inInterval = (myRevisionAsked >= startRevision) && (myRevisionAsked <= endRevision);

          if ((isInheritable || self) && inInterval) {
            myMerged = true;
            break;
          }
        }
        break;
      }

      return processed;
    }
  }

  private String keyFromFile(final File file) {
    final String path = FileUtil.getRelativePath(myMergeContext.getWcInfo().getPath(), file.getAbsolutePath(), File.separatorChar).replace(
      File.separatorChar, '/');
    return keyFromPath(path);
  }

  private static String keyFromPath(final String path) {
    return SystemInfo.isFileSystemCaseSensitive ? path : path.toUpperCase();
  }

  private static String replaceSeparators(final String s) {
    return s.replace("\r\r", "\r").replace('\r', '\n').replace("\n\n", "\n");
  }
}