summaryrefslogtreecommitdiff
path: root/plugins/svn4idea/src/org/jetbrains/idea/svn/copy/SvnKitCopyMoveClient.java
blob: 7f6f64c9ff17009173727e1cef613e490ae4cb18 (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
package org.jetbrains.idea.svn.copy;

import com.intellij.openapi.vcs.VcsException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.idea.svn.api.BaseSvnClient;
import org.jetbrains.idea.svn.api.ProgressTracker;
import org.jetbrains.idea.svn.checkin.CommitEventHandler;
import org.jetbrains.idea.svn.commandLine.SvnBindException;
import org.tmatesoft.svn.core.SVNCommitInfo;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.wc.SVNCopyClient;
import org.tmatesoft.svn.core.wc.SVNCopySource;
import org.tmatesoft.svn.core.wc.SVNRevision;
import org.tmatesoft.svn.core.wc2.SvnTarget;

import java.io.File;

/**
 * @author Konstantin Kolosovsky.
 */
public class SvnKitCopyMoveClient extends BaseSvnClient implements CopyMoveClient {

  private static final int INVALID_REVISION = -1;

  @Override
  public void copy(@NotNull File src, @NotNull File dst, boolean makeParents, boolean isMove) throws VcsException {
    final SVNCopySource copySource = new SVNCopySource(isMove ? SVNRevision.UNDEFINED : SVNRevision.WORKING, SVNRevision.WORKING, src);

    try {
      myVcs.getSvnKitManager().createCopyClient().doCopy(new SVNCopySource[]{copySource}, dst, isMove, makeParents, true);
    }
    catch (SVNException e) {
      throw new VcsException(e);
    }
  }

  @Override
  public long copy(@NotNull SvnTarget source,
                   @NotNull SvnTarget destination,
                   @Nullable SVNRevision revision,
                   boolean makeParents,
                   boolean isMove,
                   @NotNull String message,
                   @Nullable CommitEventHandler handler) throws VcsException {

    if (!destination.isURL()) {
      throw new IllegalArgumentException("Only urls are supported as destination " + destination);
    }

    final SVNCopySource copySource = createCopySource(source, revision);
    SVNCopyClient client = myVcs.getSvnKitManager().createCopyClient();
    client.setEventHandler(toEventHandler(handler));

    SVNCommitInfo info;
    try {
      info = client
        .doCopy(new SVNCopySource[]{copySource}, destination.getURL(), isMove, makeParents, true, message, null);
    }
    catch (SVNException e) {
      throw new VcsException(e);
    }

    return info != null ? info.getNewRevision() : INVALID_REVISION;
  }

  @Override
  public void copy(@NotNull SvnTarget source,
                   @NotNull File destination,
                   @Nullable SVNRevision revision,
                   boolean makeParents,
                   @Nullable ProgressTracker handler) throws VcsException {
    SVNCopyClient client = myVcs.getSvnKitManager().createCopyClient();
    client.setEventHandler(toEventHandler(handler));

    try {
      client.doCopy(new SVNCopySource[]{createCopySource(source, revision)}, destination, false, makeParents, true);
    }
    catch (SVNException e) {
      throw new SvnBindException(e);
    }
  }

  @NotNull
  private static SVNCopySource createCopySource(@NotNull SvnTarget source, @Nullable SVNRevision revision) {
    return source.isFile()
           ? new SVNCopySource(source.getPegRevision(), revision, source.getFile())
           : new SVNCopySource(source.getPegRevision(), revision, source.getURL());
  }
}