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

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.checkin.CommitInfo;
import org.jetbrains.idea.svn.diff.DiffOptions;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.wc.ISVNAnnotateHandler;
import org.tmatesoft.svn.core.wc.SVNLogClient;
import org.tmatesoft.svn.core.wc.SVNRevision;
import org.tmatesoft.svn.core.wc2.SvnTarget;

import java.io.File;
import java.util.Date;

/**
 * @author Konstantin Kolosovsky.
 */
public class SvnKitAnnotateClient extends BaseSvnClient implements AnnotateClient {

  @Override
  public void annotate(@NotNull SvnTarget target,
                       @NotNull SVNRevision startRevision,
                       @NotNull SVNRevision endRevision,
                       @Nullable SVNRevision pegRevision,
                       boolean includeMergedRevisions,
                       @Nullable DiffOptions diffOptions,
                       @Nullable AnnotationConsumer handler) throws VcsException {
    try {
      SVNLogClient client = myVcs.getSvnKitManager().createLogClient();

      client.setDiffOptions(toDiffOptions(diffOptions));
      if (target.isFile()) {
        client
          .doAnnotate(target.getFile(), pegRevision, startRevision, endRevision, true, includeMergedRevisions, toAnnotateHandler(handler),
                      null);
      }
      else {
        client
          .doAnnotate(target.getURL(), pegRevision, startRevision, endRevision, true, includeMergedRevisions, toAnnotateHandler(handler),
                      null);
      }
    }
    catch (SVNException e) {
      throw new VcsException(e);
    }
  }

  @Nullable
  private static ISVNAnnotateHandler toAnnotateHandler(@Nullable final AnnotationConsumer handler) {
    ISVNAnnotateHandler result = null;

    if (handler != null) {
      result = new ISVNAnnotateHandler() {
        @Override
        public void handleLine(Date date, long revision, String author, String line) throws SVNException {
          // deprecated - not called
        }

        @Override
        public void handleLine(Date date,
                               long revision,
                               String author,
                               String line,
                               Date mergedDate,
                               long mergedRevision,
                               String mergedAuthor,
                               String mergedPath,
                               int lineNumber) throws SVNException {
          if (revision > 0) {
            CommitInfo info = new CommitInfo.Builder(revision, date, author).build();
            CommitInfo mergeInfo = mergedDate != null ? new CommitInfo.Builder(mergedRevision, mergedDate, mergedAuthor).build() : null;

            handler.consume(lineNumber, info, mergeInfo);
          }
        }

        @Override
        public boolean handleRevision(Date date, long revision, String author, File contents) throws SVNException {
          return false;
        }

        @Override
        public void handleEOF() throws SVNException {
        }
      };
    }

    return result;
  }
}