-
Notifications
You must be signed in to change notification settings - Fork 1
/
FileContentReplace.java
274 lines (234 loc) · 10.1 KB
/
FileContentReplace.java
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
package com.github.cstroe.svndumpgui.internal.transform.replace;
import com.github.cstroe.svndumpgui.api.ContentChunk;
import com.github.cstroe.svndumpgui.api.Node;
import com.github.cstroe.svndumpgui.api.NodeHeader;
import com.github.cstroe.svndumpgui.api.Property;
import com.github.cstroe.svndumpgui.api.Revision;
import com.github.cstroe.svndumpgui.internal.ContentChunkImpl;
import com.github.cstroe.svndumpgui.internal.NodeImpl;
import com.github.cstroe.svndumpgui.internal.transform.AbstractRepositoryMutator;
import com.github.cstroe.svndumpgui.internal.utility.Md5;
import com.github.cstroe.svndumpgui.internal.utility.Sha1;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Predicate;
import static java.util.Objects.requireNonNull;
public class FileContentReplace extends AbstractRepositoryMutator {
private final Predicate<Node> nodeMatcher;
private final Function<Node, ContentChunk> contentChunkGenerator;
private boolean nodeMatched = false;
private Map<Integer, Set<Node>> previouslyUpdated = new HashMap<>();
private int currentRevision;
private int chunkCount;
/**
* Helper method to generate a predicate that matches a node,
* given the revision number, node action, and node path.
*/
public static Predicate<Node> nodeMatch(int revision, String action, String path) {
return n ->
n.getRevision().get().getNumber() == revision &&
action.equals(n.get(NodeHeader.ACTION)) &&
path.equals(n.get(NodeHeader.PATH));
}
/**
* Helper method to generate a {@link ContentChunk} from a String.
*/
public static Function<Node, ContentChunk> chunkFromString(String content) {
return n -> new ContentChunkImpl(content.getBytes());
}
/**
* Helper function for creating a FileContentReplace using a
* {@link #nodeMatch(int, String, String) node matcher} and a {@link #chunkFromString(String) string}.
*/
public static FileContentReplace createFCR(int revision,
String action,
String path,
Function<Node, ContentChunk> chunkGenerator) {
return new FileContentReplace(
nodeMatch(revision, action, path),
chunkGenerator
);
}
public FileContentReplace(Predicate<Node> nodeMatcher, Function<Node, ContentChunk> contentChunkGenerator) {
this.nodeMatcher = requireNonNull(nodeMatcher);
this.contentChunkGenerator = requireNonNull(contentChunkGenerator);
}
@Override
public void consume(Revision revision) {
currentRevision = revision.getNumber();
super.consume(revision);
}
@Override
public void consume(Node node) {
final String nodeAction = node.get(NodeHeader.ACTION);
if("delete".equals(nodeAction)) {
// we don't care about deletes
super.consume(node);
return;
}
final String nodeKind = node.get(NodeHeader.KIND);
if("file".equals(nodeKind) && nodeMatcher.test(node)) {
// we found a match for replacement
nodeMatched = true;
chunkCount = 0;
return;
}
// node was not matched, but it might be a copy of a previously replaced node
String copyFromRev = node.get(NodeHeader.COPY_FROM_REV);
if(copyFromRev == null) {
// nope, it's not a copy
super.consume(node);
return;
}
int copyRevision = Integer.parseInt(copyFromRev);
String copyPath = node.get(NodeHeader.COPY_FROM_PATH);
Node previousNode = findPreviouslyUpdatedNode(copyRevision, copyPath);
if (previousNode != null) {
// node was previously matched
recordNodeUpdate(currentRevision, node);
String previousMd5 = previousNode.get(NodeHeader.MD5);
String previousSha1 = previousNode.get(NodeHeader.SHA1);
String previousCopyMd5 = previousNode.get(NodeHeader.SOURCE_MD5);
String previousCopySha1 = previousNode.get(NodeHeader.SOURCE_SHA1);
String currentSourceMd5 = node.get(NodeHeader.SOURCE_MD5);
String currentSourceSha1 = node.get(NodeHeader.SOURCE_SHA1);
String md5 = previousMd5;
if (md5 == null) {
md5 = previousCopyMd5;
}
if (!md5.equals(currentSourceMd5)) {
node.getHeaders().put(NodeHeader.SOURCE_MD5, md5);
}
String sha1 = previousSha1;
if (sha1 == null) {
sha1 = previousCopySha1;
}
if (!sha1.equals(currentSourceSha1)) {
node.getHeaders().put(NodeHeader.SOURCE_SHA1, sha1);
}
super.consume(node);
return;
}
if("dir".equals(nodeKind) && "add".equals(nodeAction)) {
// we might be moving the directory that contains a previously replaced node
Node previouslyContainedNode = findPreviouslyUpdatedNodeFromDirectory(copyRevision, copyPath);
if(previouslyContainedNode != null) {
// yes, we're moving the directory that contains this node
String newDirectoryPath = node.get(NodeHeader.PATH);
Node nodeCopy = new NodeImpl(previouslyContainedNode);
String nodePath = nodeCopy.get(NodeHeader.PATH);
String newPath = newDirectoryPath + nodePath.substring(copyPath.length());
nodeCopy.getHeaders().put(NodeHeader.PATH, newPath);
recordNodeUpdate(currentRevision, nodeCopy);
}
super.consume(node);
return;
}
super.consume(node);
}
@Override
public void consume(ContentChunk chunk) {
if(!nodeMatched) {
super.consume(chunk);
} else {
chunkCount++;
}
}
@Override
public void endChunks() {
if(!nodeMatched) {
super.endChunks();
}
}
@Override
public void endNode(Node node) {
if(!nodeMatched) {
super.endNode(node);
} else {
ContentChunk generatedChunk = requireNonNull(contentChunkGenerator.apply(node));
updateHeaders(node, generatedChunk);
continueNodeConsumption(node, generatedChunk);
recordNodeUpdate(currentRevision, node);
nodeMatched = false;
chunkCount = 0;
}
}
private void recordNodeUpdate(int currentRevision, Node node) {
if(!previouslyUpdated.containsKey(currentRevision)) {
previouslyUpdated.put(currentRevision, new HashSet<>());
}
previouslyUpdated.get(currentRevision).add(node);
}
private Node findPreviouslyUpdatedNode(int copyRevision, String copyPath) {
Set<Node> nodeSet = previouslyUpdated.get(copyRevision);
if(nodeSet != null) {
for(Node previouslyReplacedNode : nodeSet) {
if(copyPath.equals(previouslyReplacedNode.get(NodeHeader.PATH))) {
return previouslyReplacedNode;
}
}
}
return null;
}
private Node findPreviouslyUpdatedNodeFromDirectory(int copyRevision, String directoryPath) {
Set<Node> nodeSet = previouslyUpdated.get(copyRevision);
if(nodeSet != null) {
for(Node previouslyReplacedNode : nodeSet) {
if(previouslyReplacedNode.get(NodeHeader.PATH).startsWith(directoryPath)) {
return previouslyReplacedNode;
}
}
}
return null;
}
private void updateHeaders(Node node, ContentChunk generatedChunk) {
if(node.getHeaders().get(NodeHeader.TEXT_CONTENT_LENGTH) != null) {
node.getHeaders().put(NodeHeader.TEXT_CONTENT_LENGTH, Integer.toString(generatedChunk.getContent().length));
}
String propContentLengthRaw = node.get(NodeHeader.PROP_CONTENT_LENGTH);
long propContentLength = 0;
if(propContentLengthRaw != null) {
propContentLength = Long.parseLong(propContentLengthRaw);
}
if(node.getHeaders().get(NodeHeader.CONTENT_LENGTH) != null) {
node.getHeaders().put(NodeHeader.CONTENT_LENGTH, Long.toString(propContentLength + generatedChunk.getContent().length));
}
if (node.get(NodeHeader.MD5) != null) {
final String md5hash = new Md5().hash(generatedChunk.getContent());
node.getHeaders().put(NodeHeader.MD5, md5hash);
}
if (node.get(NodeHeader.SHA1) != null) {
final String sha1hash = new Sha1().hash(generatedChunk.getContent());
node.getHeaders().put(NodeHeader.SHA1, sha1hash);
}
if(node.get(NodeHeader.COPY_FROM_REV) != null) {
int copyRevision = Integer.parseInt(node.get(NodeHeader.COPY_FROM_REV));
String copyPath = node.get(NodeHeader.COPY_FROM_PATH);
Node previouslyUpdatedNode = findPreviouslyUpdatedNode(copyRevision, copyPath);
if (previouslyUpdatedNode != null) {
if (node.get(NodeHeader.SOURCE_MD5) != null) {
node.getHeaders().put(NodeHeader.SOURCE_MD5, previouslyUpdatedNode.get(NodeHeader.MD5));
}
if (node.get(NodeHeader.SOURCE_SHA1) != null) {
node.getHeaders().put(NodeHeader.SOURCE_SHA1, previouslyUpdatedNode.get(NodeHeader.SHA1));
}
}
}
}
private void continueNodeConsumption(Node node, ContentChunk generatedChunk) {
final String trailingNewLine = node.getProperties().get(Property.TRAILING_NEWLINE_HINT);
node.getProperties().remove(Property.TRAILING_NEWLINE_HINT);
super.consume(node);
if(chunkCount > 0) {
super.consume(generatedChunk);
super.endChunks();
}
if(trailingNewLine != null) {
node.getProperties().put(Property.TRAILING_NEWLINE_HINT, trailingNewLine);
}
super.endNode(node);
}
}