summaryrefslogtreecommitdiff
path: root/plugins/svn4idea/src/org/jetbrains/idea/svn/integrate/PointMerger.java
blob: aabf015f97eeb908b1e535163a4100e86ca0e6d7 (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
/*
 * 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 org.jetbrains.idea.svn.integrate;

import com.intellij.openapi.vcs.VcsException;
import com.intellij.openapi.vcs.changes.Change;
import com.intellij.openapi.vcs.versionBrowser.CommittedChangeList;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.idea.svn.SvnRevisionNumber;
import org.jetbrains.idea.svn.SvnUtil;
import org.jetbrains.idea.svn.SvnVcs;
import org.jetbrains.idea.svn.api.Depth;
import org.jetbrains.idea.svn.copy.CopyMoveClient;
import org.jetbrains.idea.svn.delete.DeleteClient;
import org.jetbrains.idea.svn.history.SvnRepositoryContentRevision;
import org.jetbrains.idea.svn.update.UpdateEventHandler;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNURL;
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.*;

public class PointMerger extends Merger {
  private final List<Change> mySelectedChanges;
  private final SvnVcs myVcs;
  private final UpdateEventHandler myHandler;

  public PointMerger(final SvnVcs vcs,
                     CommittedChangeList selectedChangeList,
                     final File target,
                     final UpdateEventHandler handler,
                     final SVNURL currentBranchUrl,
                     final List<Change> selectedChanges,
                     String branchName) {
    super(vcs, new ArrayList<CommittedChangeList>(Arrays.<CommittedChangeList>asList(selectedChangeList)),
          target, handler, currentBranchUrl, branchName);
    myHandler = handler;
    myVcs = vcs;
    mySelectedChanges = selectedChanges;
    Collections.sort(mySelectedChanges, ChangesComparator.getInstance());
    myLatestProcessed = selectedChangeList;
  }

  public boolean hasNext() {
    return myCount == 0;
  }

  protected void doMerge() throws SVNException, VcsException {
    for (Change change : mySelectedChanges) {
      if (change.getBeforeRevision() == null) {
        add(change);
      } else if (change.getAfterRevision() == null) {
        delete(change);
      } else {
        merge(change);
      }
    }
  }

  private void merge(final Change change) throws SVNException, VcsException {
    final SvnRepositoryContentRevision before = (SvnRepositoryContentRevision) change.getBeforeRevision();
    final SvnRepositoryContentRevision after = (SvnRepositoryContentRevision) change.getAfterRevision();

    final String path = myCurrentBranchUrl.toString();
    final String beforeUrl = before.getFullPath();
    final String afterUrl = after.getFullPath();

    final File afterPath = SvnUtil.fileFromUrl(myTarget, path, afterUrl);

    MergeClient client = myVcs.getFactory(myTarget).createMergeClient();
    SvnTarget source1 = SvnTarget.fromURL(SVNURL.parseURIEncoded(beforeUrl), ((SvnRevisionNumber)before.getRevisionNumber()).getRevision());
    SvnTarget source2 = SvnTarget.fromURL(SVNURL.parseURIEncoded(afterUrl), ((SvnRevisionNumber) after.getRevisionNumber()).getRevision());

    client.merge(source1, source2, afterPath, Depth.FILES, true, mySvnConfig.isMergeDryRun(), false, false, mySvnConfig.getMergeOptions(),
                 myHandler);
  }

  private void delete(final Change change) throws SVNException, VcsException {
    final SvnRepositoryContentRevision before = (SvnRepositoryContentRevision) change.getBeforeRevision();
    final String path = myCurrentBranchUrl.toString();
    final String beforeUrl = before.getFullPath();
    final File beforePath = SvnUtil.fileFromUrl(myTarget, path, beforeUrl);

    DeleteClient client = myVcs.getFactory(myTarget).createDeleteClient();
    client.delete(beforePath, false, mySvnConfig.isMergeDryRun(), myHandler);
  }

  private void add(final Change change) throws SVNException, VcsException {
    final SvnRepositoryContentRevision after = (SvnRepositoryContentRevision) change.getAfterRevision();
    final String path = myCurrentBranchUrl.toString();
    final String afterUrl = after.getFullPath();
    final File afterPath = SvnUtil.fileFromUrl(myTarget, path, afterUrl);

    final SVNRevision revision = ((SvnRevisionNumber)after.getRevisionNumber()).getRevision();
    // todo dry run
    CopyMoveClient client = myVcs.getFactory(myTarget).createCopyMoveClient();
    client.copy(SvnTarget.fromURL(SVNURL.parseURIEncoded(afterUrl), revision), afterPath, revision, true, myHandler);
  }

  private static class ChangesComparator implements Comparator<Change> {
    private final static ChangesComparator ourInstance = new ChangesComparator();

    public static ChangesComparator getInstance() {
      return ourInstance;
    }

    public int compare(final Change o1, final Change o2) {
      final SvnRepositoryContentRevision after1 = (SvnRepositoryContentRevision) o1.getAfterRevision();
      final SvnRepositoryContentRevision after2 = (SvnRepositoryContentRevision) o2.getAfterRevision();

      if (after1 == null) {
        return 1;
      }
      if (after2 == null) {
        return -1;
      }

      final String path1 = after1.getFullPath();
      final String path2 = after2.getFullPath();
      if (path1 == null) {
        return 1;
      }
      if (path2 == null) {
        return -1;
      }

      final String ancestor = SVNPathUtil.getCommonPathAncestor(path1, path2);
      return (path1.equals(ancestor)) ? -1 : 1;
    }
  }

  @Nullable
  @Override
  public File getMergeInfoHolder() {
    return null;
  }
}