/
ReportCommitConflictsOp.java
258 lines (229 loc) · 11.3 KB
/
ReportCommitConflictsOp.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
/* Copyright (c) 2013-2016 Boundless and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Distribution License v1.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/edl-v10.html
*
* Contributors:
* Victor Olaya (Boundless) - initial implementation
*/
package org.locationtech.geogig.plumbing.merge;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import org.locationtech.geogig.model.DiffEntry;
import org.locationtech.geogig.model.NodeRef;
import org.locationtech.geogig.model.ObjectId;
import org.locationtech.geogig.model.Ref;
import org.locationtech.geogig.model.RevCommit;
import org.locationtech.geogig.model.RevFeature;
import org.locationtech.geogig.model.RevFeatureType;
import org.locationtech.geogig.model.RevObject;
import org.locationtech.geogig.model.RevObject.TYPE;
import org.locationtech.geogig.model.RevTree;
import org.locationtech.geogig.plumbing.DiffFeature;
import org.locationtech.geogig.plumbing.DiffTree;
import org.locationtech.geogig.plumbing.FindTreeChild;
import org.locationtech.geogig.plumbing.RevObjectParse;
import org.locationtech.geogig.plumbing.diff.AttributeDiff;
import org.locationtech.geogig.plumbing.diff.FeatureDiff;
import org.locationtech.geogig.repository.AbstractGeoGigOp;
import org.locationtech.geogig.repository.Conflict;
import org.locationtech.geogig.repository.Repository;
import org.locationtech.geogig.repository.impl.DepthSearch;
import org.locationtech.geogig.storage.AutoCloseableIterator;
import org.locationtech.geogig.storage.DiffObjectInfo;
import org.locationtech.geogig.storage.ObjectDatabase;
import org.opengis.feature.type.PropertyDescriptor;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
/**
* Used for cherry pick and rebase to see if the changes from a single commit conflict with another
* branch.
* <p>
* Reports conflicts between changes introduced by a given commit and the last commit of the current
* head. That should give information about whether the specified commit can be applied safely on
* the current branch without overwriting changes. It classifies the changes of the commit in
* conflicting or unconflicting, so they can be applied partially
*/
public class ReportCommitConflictsOp extends AbstractGeoGigOp<MergeScenarioReport> {
private RevCommit commit;
private MergeScenarioConsumer consumer;
/**
* @param commit the commit with the changes to apply {@link RevCommit}
*/
public ReportCommitConflictsOp setCommit(RevCommit commit) {
this.commit = commit;
return this;
}
public ReportCommitConflictsOp setConsumer(MergeScenarioConsumer consumer) {
this.consumer = consumer;
return this;
}
@Override
protected MergeScenarioReport _call() {
Preconditions.checkArgument(consumer != null, "No consumer provided.");
MergeScenarioReport report = new MergeScenarioReport();
ObjectId parentCommitId = ObjectId.NULL;
if (commit.getParentIds().size() > 0) {
parentCommitId = commit.getParentIds().get(0);
}
final ObjectId parentTreeId;
final Repository repository = repository();
final ObjectDatabase db = repository.objectDatabase();
if (parentCommitId.isNull()) {
parentTreeId = ObjectId.NULL;
} else {
parentTreeId = repository.getCommit(parentCommitId).getTreeId();
}
// get changes
DiffTree diffCmd = command(DiffTree.class).setOldTree(parentTreeId)
.setNewTree(commit.getTreeId()).setReportTrees(true);
final RevFeatureTypeCache ftCache = new RevFeatureTypeCache(db);
final RevTree workingTree = this.workingTree().getTree();
try (AutoCloseableIterator<DiffObjectInfo<RevObject>> diffObjects = db
.getDiffObjects(diffCmd.call(), RevObject.class)) {
while (diffObjects.hasNext()) {
final DiffObjectInfo<RevObject> diffEntry = diffObjects.next();
final DiffEntry diff = diffEntry.entry();
final java.util.Optional<RevObject> oldObject = diffEntry.oldValue();
final java.util.Optional<RevObject> newObject = diffEntry.newValue();
final String path = diff.path();
final Optional<RevObject> headObj = command(RevObjectParse.class)
.setRefSpec(Ref.HEAD + ":" + path).call();
switch (diff.changeType()) {
case ADDED:
if (headObj.isPresent()) {
TYPE type = newObject.get().getType();
if (TYPE.TREE.equals(type)) {
NodeRef headVersion = command(FindTreeChild.class).setChildPath(path)
.setParent(repository.getOrCreateHeadTree()).call().get();
if (!headVersion.getMetadataId()
.equals(diff.getNewObject().getMetadataId())) {
consumer.conflicted(new Conflict(path, ObjectId.NULL,
diff.getNewObject().getMetadataId(),
headVersion.getMetadataId()));
report.addConflict(path);
}
} else {
if (!headObj.get().getId().equals(diff.newObjectId())) {
consumer.conflicted(new Conflict(path, ObjectId.NULL,
diff.newObjectId(), headObj.get().getId()));
report.addConflict(path);
}
}
} else {
consumer.unconflicted(diff);
report.addUnconflicted(diff);
}
break;
case REMOVED:
if (headObj.isPresent()) {
if (headObj.get().getId().equals(diff.oldObjectId())) {
consumer.unconflicted(diff);
report.addUnconflicted(diff);
} else {
consumer.conflicted(new Conflict(path, diff.oldObjectId(),
ObjectId.NULL, headObj.get().getId()));
report.addConflict(path);
}
}
break;
case MODIFIED:
if (TYPE.TREE.equals(newObject.get().getType())) {
// TODO:see how to do this. For now, we will pass any change as a conflicted
// one
if (!diff.isChange()) {
consumer.unconflicted(diff);
report.addUnconflicted(diff);
}
break;
}
if (!headObj.isPresent()) {
// git reports this as a conflict but does not mark as conflicted, just
// adds the missing file.
// We add it and consider it unconflicted
consumer.unconflicted(diff);
report.addUnconflicted(diff);
break;
}
final RevFeature oldFeature = (RevFeature) oldObject.get();
final RevFeature newFeature = (RevFeature) newObject.get();
final RevFeature headFeature = db.getFeature(headObj.get().getId());
final NodeRef headFeatureRef = new DepthSearch(db).find(workingTree, path)
.get();
final RevFeatureType headFeatureType = ftCache
.get(headFeatureRef.getMetadataId());
ImmutableList<PropertyDescriptor> descriptors = headFeatureType.descriptors();
FeatureDiff featureDiff = DiffFeature.compare(path, oldFeature, newFeature,
ftCache.get(diff.oldMetadataId()), ftCache.get(diff.newMetadataId()));
Iterator<Entry<PropertyDescriptor, AttributeDiff>> iterator = featureDiff
.getDiffs().entrySet().iterator();
boolean ok = true;
while (iterator.hasNext() && ok) {
Entry<PropertyDescriptor, AttributeDiff> entry = iterator.next();
AttributeDiff attrDiff = entry.getValue();
PropertyDescriptor descriptor = entry.getKey();
switch (attrDiff.getType()) {
case ADDED:
if (descriptors.contains(descriptor)) {
ok = false;
}
break;
case REMOVED:
case MODIFIED:
if (!descriptors.contains(descriptor)) {
ok = false;
break;
}
for (int i = 0; i < descriptors.size(); i++) {
if (descriptors.get(i).equals(descriptor)) {
Optional<Object> value = headFeature.get(i);
Optional<Object> newValue = newFeature.get(i);
if (!newValue.equals(value)) { // if it's going to end up
// setting the same value, it
// is
// compatible, so no need to
// check
ok = attrDiff.canBeAppliedOn(value.orElse(null));
break;
}
}
}
default:
// no-op
}
}
if (ok) {
consumer.unconflicted(diff);
report.addUnconflicted(diff);
} else {
consumer.conflicted(new Conflict(path, diff.oldObjectId(),
diff.newObjectId(), headObj.get().getId()));
report.addConflict(path);
}
break;
}
}
}
consumer.finished();
return report;
}
private static class RevFeatureTypeCache {
private ObjectDatabase db;
private Map<ObjectId, RevFeatureType> cache = new HashMap<>();
RevFeatureTypeCache(ObjectDatabase db) {
this.db = db;
}
public RevFeatureType get(ObjectId ftId) {
RevFeatureType type = cache.get(ftId);
if (type == null) {
type = db.getFeatureType(ftId);
cache.put(ftId, type);
}
return type;
}
}
}