summaryrefslogtreecommitdiff
path: root/plugins/svn4idea/src/org/jetbrains/idea/svn/info/SvnInfoStructure.java
blob: 6e1b1db1efa20b011e33533cd1327ecc08f1c63a (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
/*
 * Copyright 2000-2012 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.info;

import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.tmatesoft.svn.core.*;
import org.tmatesoft.svn.core.internal.wc.SVNConflictVersion;
import org.tmatesoft.svn.core.wc.*;
import org.xml.sax.SAXException;

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

/**
 * Created with IntelliJ IDEA.
 * User: Irina.Chernushina
 * Date: 1/27/12
 * Time: 2:11 PM
 */
public class SvnInfoStructure {

  private static final Map<String, SVNConflictAction> ourConflictActions = ContainerUtil.newHashMap();
  private static final Map<String, SVNConflictReason> ourConflictReasons = ContainerUtil.newHashMap();

  static {
    ourConflictActions.put("add", SVNConflictAction.ADD);
    ourConflictActions.put("edit", SVNConflictAction.EDIT);
    ourConflictActions.put("delete", SVNConflictAction.DELETE);
    ourConflictActions.put("replace", SVNConflictAction.REPLACE);

    ourConflictReasons.put("edit", SVNConflictReason.EDITED);
    ourConflictReasons.put("obstruct", SVNConflictReason.OBSTRUCTED);
    ourConflictReasons.put("delete", SVNConflictReason.DELETED);
    ourConflictReasons.put("miss", SVNConflictReason.MISSING);
    ourConflictReasons.put("unversion", SVNConflictReason.UNVERSIONED);
    ourConflictReasons.put("add", SVNConflictReason.ADDED);
    ourConflictReasons.put("replace", SVNConflictReason.REPLACED);
  }

  @Nullable public File myFile;
  public String relativeUrl;
  public SVNURL myUrl;
  public SVNURL myRootURL;
  public long myRevision;
  public SVNNodeKind myKind;
  public String myUuid;
  public long myCommittedRevision;
  public Date myCommittedDate;
  public String myAuthor;
  public String mySchedule;
  public SVNURL myCopyFromURL;
  public long myCopyFromRevision;
  public Date myTextTime;
  public String myPropTime;
  public String myChecksum;
  public String myConflictOld;
  public String myConflictNew;
  public String myConflictWorking;
  public String myPropRejectFile;
  public SVNLockWrapper myLockWrapper;
  public SVNDepth myDepth;
  public String myChangelistName;
  public long myWcSize;
  public Date myCorrectCommittedDate;
  public Date myCorrectTextDate;

  public TreeConflictDescription myTreeConflict;

  public SVNInfo convert() throws SAXException, SVNException {
    return new IdeaSVNInfo(myFile, myUrl, myRootURL, myRevision, myKind, myUuid, myCommittedRevision, myCommittedDate, myAuthor, mySchedule,
                           myCopyFromURL, myCopyFromRevision, myTextTime, myPropTime, myChecksum, myConflictOld, myConflictNew, myConflictWorking,
                           myPropRejectFile, getLock(), myDepth, myChangelistName, myWcSize, createTreeConflict());
  }

  private SVNLock getLock() {
    SVNLock lock = null;

    if (myLockWrapper != null) {
      myLockWrapper.setPath(relativeUrl);
      lock = myLockWrapper.create();
    }

    return lock;
  }

  private SVNTreeConflictDescription createTreeConflict() throws SAXException, SVNException {
    if (myTreeConflict == null) {
      return null;
    }
    else {
      assert myFile != null;

      final SVNConflictAction action = parseConflictAction(myTreeConflict.myAction);
      final SVNConflictReason reason = parseConflictReason(myTreeConflict.myReason);
      SVNOperation operation = SVNOperation.fromString(myTreeConflict.myOperation);
      operation = operation == null ? SVNOperation.NONE : operation;
      return new SVNTreeConflictDescription(myFile, myKind, action, reason, operation,
                                            createVersion(myTreeConflict.mySourceLeft),
                                            createVersion(myTreeConflict.mySourceRight));
    }
  }

  private SVNConflictAction parseConflictAction(@NotNull String actionName) {
    SVNConflictAction action = SVNConflictAction.fromString(actionName);
    action = action != null ? action : ourConflictActions.get(actionName);

    if (action == null) {
      throw new IllegalArgumentException("Unknown conflict action " + actionName);
    }

    return action;
  }

  private SVNConflictReason parseConflictReason(@NotNull String reasonName) throws SAXException {
    SVNConflictReason reason = SVNConflictReason.fromString(reasonName);
    reason = reason != null ? reason : ourConflictReasons.get(reasonName);

    if (reason == null) {
      throw new SAXException("Can not parse conflict reason: " + reasonName);
    }

    return reason;
  }

  private SVNConflictVersion createVersion(final ConflictVersion version) throws SVNException, SAXException {
    return version == null ? null : new SVNConflictVersion(SVNURL.parseURIEncoded(version.myRepoUrl), version.myPathInRepo,
                                                           parseRevision(version.myRevision), SVNNodeKind.parseKind(version.myKind));
  }
  
  private long parseRevision(final String revision) throws SAXException {
    try {
      return Long.parseLong(revision);
    } catch (NumberFormatException e) {
      throw new SAXException(e);
    }
  }

  public static class TreeConflictDescription {
    public String myOperation;
    public String myKind;
    public String myReason;
    public String myVictim;
    public String myAction;

    public ConflictVersion mySourceLeft;
    public ConflictVersion mySourceRight;
  }
  
  public static class ConflictVersion {
    public String myKind;
    public String myPathInRepo;
    public String myRepoUrl;
    public String myRevision;
  }
}