summaryrefslogtreecommitdiff
path: root/plugins/svn4idea/src/org/jetbrains/idea/svn/history/CmdHistoryClient.java
blob: 3494b9ac9d28c987b62d0a0fa7be394c11565b9c (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
178
179
180
181
182
183
184
185
186
187
188
189
package org.jetbrains.idea.svn.history;

import com.intellij.openapi.vcs.VcsException;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.idea.svn.api.BaseSvnClient;
import org.jetbrains.idea.svn.commandLine.CommandExecutor;
import org.jetbrains.idea.svn.commandLine.CommandUtil;
import org.jetbrains.idea.svn.commandLine.SvnBindException;
import org.jetbrains.idea.svn.commandLine.SvnCommandName;
import org.tmatesoft.svn.core.*;
import org.tmatesoft.svn.core.wc.SVNRevision;
import org.tmatesoft.svn.core.wc2.SvnTarget;

import javax.xml.bind.JAXBException;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlValue;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author Konstantin Kolosovsky.
 */
public class CmdHistoryClient extends BaseSvnClient implements HistoryClient {

  @Override
  public void doLog(@NotNull SvnTarget target,
                    @NotNull SVNRevision startRevision,
                    @NotNull SVNRevision endRevision,
                    boolean stopOnCopy,
                    boolean discoverChangedPaths,
                    boolean includeMergedRevisions,
                    long limit,
                    @Nullable String[] revisionProperties,
                    @Nullable ISVNLogEntryHandler handler) throws VcsException {
    // TODO: add revision properties parameter if necessary

    List<String> parameters =
      prepareCommand(target, startRevision, endRevision, stopOnCopy, discoverChangedPaths, includeMergedRevisions, limit);

    try {
      CommandExecutor command = execute(myVcs, target, SvnCommandName.log, parameters, null);
      // TODO: handler should be called in parallel with command execution, but this will be in other thread
      // TODO: check if that is ok for current handler implementation
      parseOutput(command, handler);
    }
    catch (SVNException e) {
      throw new SvnBindException(e);
    }
  }

  private static void parseOutput(@NotNull CommandExecutor command, @Nullable ISVNLogEntryHandler handler)
    throws VcsException, SVNException {
    try {
      LogInfo log = CommandUtil.parse(command.getOutput(), LogInfo.class);

      if (handler != null && log != null) {
        for (LogEntry entry : log.entries) {
          iterateRecursively(entry, handler);
        }
      }
    }
    catch (JAXBException e) {
      throw new VcsException(e);
    }
  }

  private static void iterateRecursively(@NotNull LogEntry entry, @NotNull ISVNLogEntryHandler handler) throws SVNException {
    handler.handleLogEntry(entry.toLogEntry());

    for (LogEntry childEntry : entry.childEntries) {
      iterateRecursively(childEntry, handler);
    }

    if (entry.hasChildren()) {
      // empty log entry passed to handler to fully correspond to SVNKit behavior.
      handler.handleLogEntry(SVNLogEntry.EMPTY_ENTRY);
    }
  }

  private static List<String> prepareCommand(@NotNull SvnTarget target,
                                             @NotNull SVNRevision startRevision,
                                             @NotNull SVNRevision endRevision,
                                             boolean stopOnCopy, boolean discoverChangedPaths, boolean includeMergedRevisions, long limit) {
    List<String> parameters = new ArrayList<String>();

    CommandUtil.put(parameters, target);
    parameters.add("--revision");
    parameters.add(startRevision + ":" + endRevision);

    CommandUtil.put(parameters, stopOnCopy, "--stop-on-copy");
    CommandUtil.put(parameters, discoverChangedPaths, "--verbose");
    CommandUtil.put(parameters, includeMergedRevisions, "--use-merge-history");
    if (limit > 0) {
      parameters.add("--limit");
      parameters.add(String.valueOf(limit));
    }
    parameters.add("--xml");

    return parameters;
  }

  @XmlRootElement(name = "log")
  public static class LogInfo {

    @XmlElement(name = "logentry")
    public List<LogEntry> entries = new ArrayList<LogEntry>();
  }

  public static class LogEntry {

    @XmlAttribute(name = "revision")
    public long revision;

    @XmlElement(name = "author")
    public String author;

    @XmlElement(name = "date")
    public Date date;

    @XmlElement(name = "msg")
    public String message;

    @XmlElement(name = "paths")
    public ChangedPaths changedPaths;

    @XmlElement(name = "logentry")
    public List<LogEntry> childEntries = new ArrayList<LogEntry>();

    public boolean hasChildren() {
      return !childEntries.isEmpty();
    }

    public SVNLogEntry toLogEntry() {
      SVNLogEntry entry = new SVNLogEntry(toChangedPathsMap(), revision, author, date, message);

      entry.setHasChildren(hasChildren());

      return entry;
    }

    public Map<String, SVNLogEntryPath> toChangedPathsMap() {
      return changedPaths != null ? changedPaths.toMap() : ContainerUtil.<String, SVNLogEntryPath>newHashMap();
    }
  }

  public static class ChangedPaths {

    @XmlElement(name = "path")
    public List<ChangedPath> changedPaths = new ArrayList<ChangedPath>();

    public Map<String, SVNLogEntryPath> toMap() {
      Map<String, SVNLogEntryPath> changes = ContainerUtil.newHashMap();

      for (ChangedPath path : changedPaths) {
        changes.put(path.path, path.toLogEntryPath());
      }

      return changes;
    }
  }

  public static class ChangedPath {

    @XmlAttribute(name = "kind")
    public String kind;

    @XmlAttribute(name = "action")
    public String action;

    @XmlAttribute(name = "copyfrom-path")
    public String copyFromPath;

    @XmlAttribute(name = "copyfrom-rev")
    public long copyFromRevision;

    @XmlValue
    public String path;

    public SVNLogEntryPath toLogEntryPath() {
      return new SVNLogEntryPath(path, CommandUtil.getStatusChar(action), copyFromPath, copyFromRevision);
    }
  }
}