use of org.eclipse.jgit.dircache.DirCacheBuilder in project gerrit by GerritCodeReview.
the class MergeUtil method mergeWithConflicts.
// TemporaryBuffer requires calling close before reading.
@SuppressWarnings("resource")
public static ObjectId mergeWithConflicts(RevWalk rw, ObjectInserter ins, DirCache dc, String oursName, RevCommit ours, String theirsName, RevCommit theirs, Map<String, MergeResult<? extends Sequence>> mergeResults) throws IOException {
rw.parseBody(ours);
rw.parseBody(theirs);
String oursMsg = ours.getShortMessage();
String theirsMsg = theirs.getShortMessage();
int nameLength = Math.max(oursName.length(), theirsName.length());
String oursNameFormatted = String.format("%-" + nameLength + "s (%s %s)", oursName, abbreviateName(ours, NAME_ABBREV_LEN), oursMsg.substring(0, Math.min(oursMsg.length(), 60)));
String theirsNameFormatted = String.format("%-" + nameLength + "s (%s %s)", theirsName, abbreviateName(theirs, NAME_ABBREV_LEN), theirsMsg.substring(0, Math.min(theirsMsg.length(), 60)));
MergeFormatter fmt = new MergeFormatter();
Map<String, ObjectId> resolved = new HashMap<>();
for (Map.Entry<String, MergeResult<? extends Sequence>> entry : mergeResults.entrySet()) {
MergeResult<? extends Sequence> p = entry.getValue();
TemporaryBuffer buf = null;
try {
// TODO(dborowitz): Respect inCoreLimit here.
buf = new TemporaryBuffer.LocalFile(null, 10 * 1024 * 1024);
fmt.formatMerge(buf, p, "BASE", oursNameFormatted, theirsNameFormatted, UTF_8);
// Flush file and close for writes, but leave available for reading.
buf.close();
try (InputStream in = buf.openInputStream()) {
resolved.put(entry.getKey(), ins.insert(Constants.OBJ_BLOB, buf.length(), in));
}
} finally {
if (buf != null) {
buf.destroy();
}
}
}
DirCacheBuilder builder = dc.builder();
int cnt = dc.getEntryCount();
for (int i = 0; i < cnt; ) {
DirCacheEntry entry = dc.getEntry(i);
if (entry.getStage() == 0) {
builder.add(entry);
i++;
continue;
}
int next = dc.nextEntry(i);
String path = entry.getPathString();
DirCacheEntry res = new DirCacheEntry(path);
if (resolved.containsKey(path)) {
// For a file with content merge conflict that we produced a result
// above on, collapse the file down to a single stage 0 with just
// the blob content, and a randomly selected mode (the lowest stage,
// which should be the merge base, or ours).
res.setFileMode(entry.getFileMode());
res.setObjectId(resolved.get(path));
} else if (next == i + 1) {
// If there is exactly one stage present, shouldn't be a conflict...
res.setFileMode(entry.getFileMode());
res.setObjectId(entry.getObjectId());
} else if (next == i + 2) {
// Two stages suggests a delete/modify conflict. Pick the higher
// stage as the automatic result.
entry = dc.getEntry(i + 1);
res.setFileMode(entry.getFileMode());
res.setObjectId(entry.getObjectId());
} else {
// 3 stage conflict, no resolve above
// Punt on the 3-stage conflict and show the base, for now.
res.setFileMode(entry.getFileMode());
res.setObjectId(entry.getObjectId());
}
builder.add(res);
i = next;
}
builder.finish();
return dc.writeTree(ins);
}
use of org.eclipse.jgit.dircache.DirCacheBuilder in project gerrit by GerritCodeReview.
the class TreeCreator method readBaseTree.
private DirCache readBaseTree(Repository repository) throws IOException {
try (ObjectReader objectReader = repository.newObjectReader()) {
DirCache dirCache = DirCache.newInCore();
DirCacheBuilder dirCacheBuilder = dirCache.builder();
if (!ObjectId.zeroId().equals(baseTreeId)) {
dirCacheBuilder.addTree(new byte[0], DirCacheEntry.STAGE_0, objectReader, baseTreeId);
}
dirCacheBuilder.finish();
return dirCache;
}
}
use of org.eclipse.jgit.dircache.DirCacheBuilder in project gerrit by GerritCodeReview.
the class VersionedMetaData method readTree.
protected DirCache readTree(RevTree tree) throws IOException, MissingObjectException, IncorrectObjectTypeException {
DirCache dc = DirCache.newInCore();
if (tree != null) {
DirCacheBuilder b = dc.builder();
b.addTree(new byte[0], DirCacheEntry.STAGE_0, reader, tree);
b.finish();
}
return dc;
}
use of org.eclipse.jgit.dircache.DirCacheBuilder in project gerrit by GerritCodeReview.
the class VersionedMetaData method readTree.
protected DirCache readTree(RevTree tree) throws IOException, MissingObjectException, IncorrectObjectTypeException {
DirCache dc = DirCache.newInCore();
if (tree != null) {
DirCacheBuilder b = dc.builder();
b.addTree(new byte[0], DirCacheEntry.STAGE_0, reader, tree);
b.finish();
}
return dc;
}
use of org.eclipse.jgit.dircache.DirCacheBuilder in project egit by eclipse.
the class IndexFileRevisionTest method buildIndex.
private void buildIndex(DirCacheEntry... entries) throws IOException {
DirCache dirCache = repository.lockDirCache();
DirCacheBuilder builder = dirCache.builder();
try {
for (DirCacheEntry entry : entries) builder.add(entry);
builder.commit();
} finally {
dirCache.unlock();
}
}
Aggregations