Search in sources :

Example 11 with DirCacheBuilder

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);
}
Also used : DirCacheBuilder(org.eclipse.jgit.dircache.DirCacheBuilder) DirCacheEntry(org.eclipse.jgit.dircache.DirCacheEntry) ObjectId(org.eclipse.jgit.lib.ObjectId) HashMap(java.util.HashMap) InputStream(java.io.InputStream) MergeResult(org.eclipse.jgit.merge.MergeResult) Sequence(org.eclipse.jgit.diff.Sequence) TemporaryBuffer(org.eclipse.jgit.util.TemporaryBuffer) Map(java.util.Map) HashMap(java.util.HashMap) MergeFormatter(org.eclipse.jgit.merge.MergeFormatter)

Example 12 with DirCacheBuilder

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;
    }
}
Also used : DirCache(org.eclipse.jgit.dircache.DirCache) DirCacheBuilder(org.eclipse.jgit.dircache.DirCacheBuilder) ObjectReader(org.eclipse.jgit.lib.ObjectReader)

Example 13 with DirCacheBuilder

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;
}
Also used : DirCache(org.eclipse.jgit.dircache.DirCache) DirCacheBuilder(org.eclipse.jgit.dircache.DirCacheBuilder)

Example 14 with DirCacheBuilder

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;
}
Also used : DirCache(org.eclipse.jgit.dircache.DirCache) DirCacheBuilder(org.eclipse.jgit.dircache.DirCacheBuilder)

Example 15 with DirCacheBuilder

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();
    }
}
Also used : DirCache(org.eclipse.jgit.dircache.DirCache) DirCacheBuilder(org.eclipse.jgit.dircache.DirCacheBuilder) DirCacheEntry(org.eclipse.jgit.dircache.DirCacheEntry)

Aggregations

DirCacheBuilder (org.eclipse.jgit.dircache.DirCacheBuilder)23 DirCache (org.eclipse.jgit.dircache.DirCache)21 DirCacheEntry (org.eclipse.jgit.dircache.DirCacheEntry)16 ObjectInserter (org.eclipse.jgit.lib.ObjectInserter)9 ObjectId (org.eclipse.jgit.lib.ObjectId)8 IOException (java.io.IOException)7 Repository (org.eclipse.jgit.lib.Repository)6 InputStream (java.io.InputStream)5 RevWalk (org.eclipse.jgit.revwalk.RevWalk)4 IStatus (org.eclipse.core.runtime.IStatus)3 JGitInternalException (org.eclipse.jgit.api.errors.JGitInternalException)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 TreeSet (java.util.TreeSet)2 Nonnull (javax.annotation.Nonnull)2 IFile (org.eclipse.core.resources.IFile)2 CoreException (org.eclipse.core.runtime.CoreException)2 TestProject (org.eclipse.egit.core.test.TestProject)2 TestRepository (org.eclipse.egit.core.test.TestRepository)2 ConcurrentRefUpdateException (org.eclipse.jgit.api.errors.ConcurrentRefUpdateException)2