summaryrefslogtreecommitdiff
path: root/plugins/svn4idea/src/org/jetbrains/idea/svn/properties/CmdPropertyClient.java
blob: e855abe1ec2e1ea2bfe921ef8dbc71bc1965a624 (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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package org.jetbrains.idea.svn.properties;

import com.intellij.openapi.vcs.VcsException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.idea.svn.SvnUtil;
import org.jetbrains.idea.svn.api.BaseSvnClient;
import org.jetbrains.idea.svn.api.Depth;
import org.jetbrains.idea.svn.commandLine.CommandExecutor;
import org.jetbrains.idea.svn.commandLine.CommandUtil;
import org.jetbrains.idea.svn.commandLine.SvnCommandName;
import org.jetbrains.idea.svn.info.Info;
import org.tmatesoft.svn.core.*;
import org.tmatesoft.svn.core.wc.ISVNPropertyHandler;
import org.tmatesoft.svn.core.wc.SVNPropertyData;
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.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Konstantin Kolosovsky.
 */
public class CmdPropertyClient extends BaseSvnClient implements PropertyClient {

  @Nullable
  @Override
  public SVNPropertyData getProperty(@NotNull SvnTarget target,
                                     @NotNull String property,
                                     boolean revisionProperty,
                                     @Nullable SVNRevision revision)
    throws VcsException {
    List<String> parameters = new ArrayList<String>();

    parameters.add(property);
    if (!revisionProperty) {
      CommandUtil.put(parameters, target);
      CommandUtil.put(parameters, revision);
    } else {
      // currently revision properties are returned only for file targets
      assertFile(target);

      // "svn propget --revprop" treats '@' symbol at file path end as part of the path - so here we manually omit adding '@' at the end
      CommandUtil.put(parameters, target, false);
      parameters.add("--revprop");
      CommandUtil.put(parameters, resolveRevisionNumber(target.getFile(), revision));
    }
    // always use --xml option here - this allows to determine if property exists with empty value or property does not exist, which
    // is critical for some parts of merge logic
    parameters.add("--xml");

    CommandExecutor command = execute(myVcs, target, SvnCommandName.propget, parameters, null);
    return parseSingleProperty(target, command.getOutput());
  }

  @Override
  public void getProperty(@NotNull SvnTarget target,
                          @NotNull String property,
                          @Nullable SVNRevision revision,
                          @Nullable Depth depth,
                          @Nullable ISVNPropertyHandler handler) throws VcsException {
    List<String> parameters = new ArrayList<String>();

    parameters.add(property);
    fillListParameters(target, revision, depth, parameters, false);

    CommandExecutor command = execute(myVcs, target, SvnCommandName.propget, parameters, null);
    parseOutput(target, command.getOutput(), handler);
  }

  @Override
  public void list(@NotNull SvnTarget target,
                   @Nullable SVNRevision revision,
                   @Nullable Depth depth,
                   @Nullable ISVNPropertyHandler handler) throws VcsException {
    List<String> parameters = new ArrayList<String>();
    fillListParameters(target, revision, depth, parameters, true);

    CommandExecutor command = execute(myVcs, target, SvnCommandName.proplist, parameters, null);
    parseOutput(target, command.getOutput(), handler);
  }

  @Override
  public void setProperty(@NotNull File file,
                          @NotNull String property,
                          @Nullable SVNPropertyValue value,
                          @Nullable Depth depth,
                          boolean force) throws VcsException {
    runSetProperty(SvnTarget.fromFile(file), property, null, depth, value, force);
  }

  @Override
  public void setProperties(@NotNull File file, @NotNull SVNProperties properties) throws VcsException {
    SVNProperties currentProperties = collectPropertiesToDelete(file);
    currentProperties.putAll(properties);

    for (String propertyName : currentProperties.nameSet()) {
      setProperty(file, propertyName, currentProperties.getSVNPropertyValue(propertyName), Depth.EMPTY, true);
    }
  }

  @NotNull
  private SVNProperties collectPropertiesToDelete(@NotNull File file) throws VcsException {
    final SVNProperties result = new SVNProperties();

    list(SvnTarget.fromFile(file), null, Depth.EMPTY, new ISVNPropertyHandler() {
      @Override
      public void handleProperty(File path, SVNPropertyData property) throws SVNException {
        // null indicates property will be deleted
        result.put(property.getName(), (SVNPropertyValue)null);
      }

      @Override
      public void handleProperty(SVNURL url, SVNPropertyData property) throws SVNException {
      }

      @Override
      public void handleProperty(long revision, SVNPropertyData property) throws SVNException {
      }
    });

    return result;
  }

  @Override
  public void setRevisionProperty(@NotNull SvnTarget target,
                                  @NotNull String property,
                                  @NotNull SVNRevision revision,
                                  @Nullable SVNPropertyValue value,
                                  boolean force) throws VcsException {
    runSetProperty(target, property, revision, null, value, force);
  }

  private void runSetProperty(@NotNull SvnTarget target,
                              @NotNull String property,
                              @Nullable SVNRevision revision,
                              @Nullable Depth depth,
                              @Nullable SVNPropertyValue value,
                              boolean force) throws VcsException {
    List<String> parameters = new ArrayList<String>();
    boolean isDelete = value == null;

    parameters.add(property);
    if (revision != null) {
      parameters.add("--revprop");
      CommandUtil.put(parameters, revision);
    }
    if (!isDelete) {
      parameters.add(SVNPropertyValue.getPropertyAsString(value));
      // --force could only be used in "propset" command, but not in "propdel" command
      CommandUtil.put(parameters, force, "--force");
    }
    CommandUtil.put(parameters, target);
    CommandUtil.put(parameters, depth);

    // For some reason, command setting ignore property when working directory equals target directory (like
    // "svn propset svn:ignore *.java . --depth empty") tries to set ignore also on child files and fails with error like
    // "svn: E200009: Cannot set 'svn:ignore' on a file ('...File1.java')". So here we manually force home directory to be used.
    // NOTE: that setting other properties (not svn:ignore) does not cause such error.
    execute(myVcs, target, CommandUtil.getHomeDirectory(), isDelete ? SvnCommandName.propdel : SvnCommandName.propset, parameters,
            null);
  }

  private void fillListParameters(@NotNull SvnTarget target,
                                  @Nullable SVNRevision revision,
                                  @Nullable Depth depth,
                                  @NotNull List<String> parameters,
                                  boolean verbose) {
    CommandUtil.put(parameters, target);
    CommandUtil.put(parameters, revision);
    CommandUtil.put(parameters, depth);
    parameters.add("--xml");
    CommandUtil.put(parameters, verbose, "--verbose");
  }

  private SVNPropertyData parseSingleProperty(SvnTarget target, String output) throws VcsException {
    final SVNPropertyData[] data = new SVNPropertyData[1];
    ISVNPropertyHandler handler = new ISVNPropertyHandler() {
      @Override
      public void handleProperty(File path, SVNPropertyData property) throws SVNException {
        data[0] = property;
      }

      @Override
      public void handleProperty(SVNURL url, SVNPropertyData property) throws SVNException {
        data[0] = property;
      }

      @Override
      public void handleProperty(long revision, SVNPropertyData property) throws SVNException {
        data[0] = property;
      }
    };

    parseOutput(target, output, handler);

    return data[0];
  }

  private static void parseOutput(SvnTarget target, String output, ISVNPropertyHandler handler) throws VcsException {
    try {
      Properties properties = CommandUtil.parse(output, Properties.class);

      if (properties != null) {
        for (Target childInfo : properties.targets) {
          SvnTarget childTarget = SvnUtil.append(target, childInfo.path);
          for (Property property : childInfo.properties) {
            invokeHandler(childTarget, create(property.name, property.value), handler);
          }
        }

        if (properties.revisionProperties != null) {
          for (Property property : properties.revisionProperties.properties) {
            invokeHandler(properties.revisionProperties.revisionNumber(), create(property.name, property.value), handler);
          }
        }
      }
    }
    catch (JAXBException e) {
      throw new VcsException(e);
    }
    catch (SVNException e) {
      throw new VcsException(e);
    }
  }

  private static void invokeHandler(@NotNull SvnTarget target, @Nullable SVNPropertyData data, @Nullable ISVNPropertyHandler handler)
    throws SVNException {
    if (handler != null && data != null) {
      if (target.isFile()) {
        handler.handleProperty(target.getFile(), data);
      } else {
        handler.handleProperty(target.getURL(), data);
      }
    }
  }

  private static void invokeHandler(long revision, @Nullable SVNPropertyData data, @Nullable ISVNPropertyHandler handler)
    throws SVNException {
    if (handler != null && data != null) {
      handler.handleProperty(revision, data);
    }
  }

  @Nullable
  private static SVNPropertyData create(@NotNull String property, @Nullable String value) {
    SVNPropertyData result = null;

    // such behavior is required to compatibility with SVNKit as some logic in merge depends on
    // whether null property data or property data with empty string value is returned
    if (value != null) {
      result = new SVNPropertyData(property, SVNPropertyValue.create(value.trim()), LF_SEPARATOR_OPTIONS);
    }

    return result;
  }

  private SVNRevision resolveRevisionNumber(@NotNull File path, @Nullable SVNRevision revision) throws VcsException {
    long result = revision != null ? revision.getNumber() : -1;

    // base should be resolved manually - could not set revision to BASE to get revision property
    if (SVNRevision.BASE.equals(revision)) {
      Info info = myVcs.getInfo(path, SVNRevision.BASE);

      result = info != null ? info.getRevision().getNumber() : -1;
    }

    if (result == -1) {
      throw new VcsException("Could not determine revision number for file " + path + " and revision " + revision);
    }

    return SVNRevision.create(result);
  }

  @XmlRootElement(name = "properties")
  public static class Properties {

    @XmlElement(name = "target")
    public List<Target> targets = new ArrayList<Target>();

    @XmlElement(name = "revprops")
    public RevisionProperties revisionProperties;
  }

  public static class Target {

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

    @XmlElement(name = "property")
    public List<Property> properties = new ArrayList<Property>();
  }

  public static class RevisionProperties {

    @XmlAttribute(name = "rev")
    public String revision;

    @XmlElement(name = "property")
    public List<Property> properties = new ArrayList<Property>();

    public long revisionNumber() {
      return Long.valueOf(revision);
    }
  }

  public static class Property {
    @XmlAttribute(name = "name")
    public String name;

    @XmlValue
    public String value;
  }
}