Example usage for org.eclipse.jgit.merge MergeFormatter MergeFormatter

List of usage examples for org.eclipse.jgit.merge MergeFormatter MergeFormatter

Introduction

In this page you can find the example usage for org.eclipse.jgit.merge MergeFormatter MergeFormatter.

Prototype

MergeFormatter

Source Link

Usage

From source file:com.google.gerrit.server.patch.AutoMerger.java

License:Apache License

/**
 * Perform an auto-merge of the parents of the given merge commit.
 *
 * @return auto-merge commit or {@code null} if an auto-merge commit
 *     couldn't be created. Headers of the returned RevCommit are parsed.
 *//*from   w  w  w  .  ja v a2 s . c o m*/
public RevCommit merge(Repository repo, RevWalk rw, final ObjectInserter ins, RevCommit merge,
        ThreeWayMergeStrategy mergeStrategy) throws IOException {
    rw.parseHeaders(merge);
    String hash = merge.name();
    String refName = RefNames.REFS_CACHE_AUTOMERGE + hash.substring(0, 2) + "/" + hash.substring(2);
    Ref ref = repo.getRefDatabase().exactRef(refName);
    if (ref != null && ref.getObjectId() != null) {
        RevObject obj = rw.parseAny(ref.getObjectId());
        if (obj instanceof RevCommit) {
            return (RevCommit) obj;
        }
        return commit(repo, rw, ins, refName, obj, merge);
    }

    ResolveMerger m = (ResolveMerger) mergeStrategy.newMerger(repo, true);
    DirCache dc = DirCache.newInCore();
    m.setDirCache(dc);
    m.setObjectInserter(new ObjectInserter.Filter() {
        @Override
        protected ObjectInserter delegate() {
            return ins;
        }

        @Override
        public void flush() {
        }

        @Override
        public void close() {
        }
    });

    boolean couldMerge;
    try {
        couldMerge = m.merge(merge.getParents());
    } catch (IOException e) {
        // It is not safe to continue further down in this method as throwing
        // an exception most likely means that the merge tree was not created
        // and m.getMergeResults() is empty. This would mean that all paths are
        // unmerged and Gerrit UI would show all paths in the patch list.
        log.warn("Error attempting automerge " + refName, e);
        return null;
    }

    ObjectId treeId;
    if (couldMerge) {
        treeId = m.getResultTreeId();

    } else {
        RevCommit ours = merge.getParent(0);
        RevCommit theirs = merge.getParent(1);
        rw.parseBody(ours);
        rw.parseBody(theirs);
        String oursMsg = ours.getShortMessage();
        String theirsMsg = theirs.getShortMessage();

        String oursName = String.format("HEAD   (%s %s)", ours.abbreviate(6).name(),
                oursMsg.substring(0, Math.min(oursMsg.length(), 60)));
        String theirsName = String.format("BRANCH (%s %s)", theirs.abbreviate(6).name(),
                theirsMsg.substring(0, Math.min(theirsMsg.length(), 60)));

        MergeFormatter fmt = new MergeFormatter();
        Map<String, MergeResult<? extends Sequence>> r = m.getMergeResults();
        Map<String, ObjectId> resolved = new HashMap<>();
        for (Map.Entry<String, MergeResult<? extends Sequence>> entry : r.entrySet()) {
            MergeResult<? extends Sequence> p = entry.getValue();
            try (TemporaryBuffer buf = new TemporaryBuffer.LocalFile(null, 10 * 1024 * 1024)) {
                fmt.formatMerge(buf, p, "BASE", oursName, theirsName, UTF_8.name());
                buf.close();

                try (InputStream in = buf.openInputStream()) {
                    resolved.put(entry.getKey(), ins.insert(Constants.OBJ_BLOB, buf.length(), in));
                }
            }
        }

        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();
        treeId = dc.writeTree(ins);
    }
    ins.flush();

    return commit(repo, rw, ins, refName, treeId, merge);
}

From source file:com.google.gerrit.server.patch.PatchListLoader.java

License:Apache License

public static RevTree automerge(Repository repo, RevWalk rw, RevCommit b, ThreeWayMergeStrategy mergeStrategy,
        boolean save) throws IOException {
    String hash = b.name();//from  ww  w  .  j a v  a2 s.  c  o m
    String refName = RefNames.REFS_CACHE_AUTOMERGE + hash.substring(0, 2) + "/" + hash.substring(2);
    Ref ref = repo.getRefDatabase().exactRef(refName);
    if (ref != null && ref.getObjectId() != null) {
        return rw.parseTree(ref.getObjectId());
    }

    ResolveMerger m = (ResolveMerger) mergeStrategy.newMerger(repo, true);
    try (ObjectInserter ins = repo.newObjectInserter()) {
        DirCache dc = DirCache.newInCore();
        m.setDirCache(dc);
        m.setObjectInserter(new ObjectInserter.Filter() {
            @Override
            protected ObjectInserter delegate() {
                return ins;
            }

            @Override
            public void flush() {
            }

            @Override
            public void close() {
            }
        });

        boolean couldMerge;
        try {
            couldMerge = m.merge(b.getParents());
        } catch (IOException e) {
            // It is not safe to continue further down in this method as throwing
            // an exception most likely means that the merge tree was not created
            // and m.getMergeResults() is empty. This would mean that all paths are
            // unmerged and Gerrit UI would show all paths in the patch list.
            log.warn("Error attempting automerge " + refName, e);
            return null;
        }

        ObjectId treeId;
        if (couldMerge) {
            treeId = m.getResultTreeId();

        } else {
            RevCommit ours = b.getParent(0);
            RevCommit theirs = b.getParent(1);
            rw.parseBody(ours);
            rw.parseBody(theirs);
            String oursMsg = ours.getShortMessage();
            String theirsMsg = theirs.getShortMessage();

            String oursName = String.format("HEAD   (%s %s)", ours.abbreviate(6).name(),
                    oursMsg.substring(0, Math.min(oursMsg.length(), 60)));
            String theirsName = String.format("BRANCH (%s %s)", theirs.abbreviate(6).name(),
                    theirsMsg.substring(0, Math.min(theirsMsg.length(), 60)));

            MergeFormatter fmt = new MergeFormatter();
            Map<String, MergeResult<? extends Sequence>> r = m.getMergeResults();
            Map<String, ObjectId> resolved = new HashMap<>();
            for (Map.Entry<String, MergeResult<? extends Sequence>> entry : r.entrySet()) {
                MergeResult<? extends Sequence> p = entry.getValue();
                try (TemporaryBuffer buf = new TemporaryBuffer.LocalFile(null, 10 * 1024 * 1024)) {
                    fmt.formatMerge(buf, p, "BASE", oursName, theirsName, "UTF-8");
                    buf.close();

                    try (InputStream in = buf.openInputStream()) {
                        resolved.put(entry.getKey(), ins.insert(Constants.OBJ_BLOB, buf.length(), in));
                    }
                }
            }

            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();
            treeId = dc.writeTree(ins);
        }
        ins.flush();

        if (save) {
            RefUpdate update = repo.updateRef(refName);
            update.setNewObjectId(treeId);
            update.disableRefLog();
            update.forceUpdate();
        }

        return rw.lookupTree(treeId);
    }
}

From source file:com.itemis.maven.plugins.unleash.scm.providers.merge.UnleashGitMerger.java

License:Eclipse Distribution License

/**
 * Writes merged file content to the working tree.
 *
 * @param result//from w  w w.jav  a  2s  .com
 *          the result of the content merge
 * @return the working tree file to which the merged content was written.
 * @throws FileNotFoundException
 * @throws IOException
 */
private File writeMergedFile(MergeResult<RawText> result) throws FileNotFoundException, IOException {
    File workTree = this.db.getWorkTree();
    FS fs = this.db.getFS();
    File of = new File(workTree, this.tw.getPathString());
    File parentFolder = of.getParentFile();
    if (!fs.exists(parentFolder)) {
        parentFolder.mkdirs();
    }
    OutputStream os = null;
    try {
        os = new BufferedOutputStream(new FileOutputStream(of));
        new MergeFormatter().formatMerge(os, result, Arrays.asList(this.commitNames), CHARACTER_ENCODING);
    } finally {
        Closeables.close(os, true);
    }
    return of;
}

From source file:com.itemis.maven.plugins.unleash.scm.providers.merge.UnleashGitMerger.java

License:Eclipse Distribution License

private ObjectId insertMergeResult(MergeResult<RawText> result) throws IOException {
    TemporaryBuffer.LocalFile buf = new TemporaryBuffer.LocalFile(this.db.getDirectory(), 10 << 20);
    try {/*  ww  w  . j  a va  2 s . c  o m*/
        new MergeFormatter().formatMerge(buf, result, Arrays.asList(this.commitNames), CHARACTER_ENCODING);
        buf.close();
        InputStream in = null;
        try {
            in = buf.openInputStream();
            return getObjectInserter().insert(OBJ_BLOB, buf.length(), in);
        } finally {
            Closeables.closeQuietly(in);
        }
    } finally {
        buf.destroy();
    }
}

From source file:org.apache.torque.generator.merge.ThreeWayMerger.java

License:Apache License

/**
 * Performs a three-way merge./*from   w ww  .  j a v  a 2s  .c  o m*/
 *
 * @param base the base from which the other two versions are derived,
 *        not null.
 * @param generated the newly generated text, not null.
 * @param edited the possibly edited text, not null.
 * @param charsetName the name of the character set, not null.
 * @return the merge result, not null.
 *
 * @throws GeneratorException if merging fails.
 */
public String merge(String base, String generated, String edited, String charsetName)
        throws GeneratorException {
    MergeAlgorithm mergeAlgorithm = new MergeAlgorithm();
    MergeResult<RawText> mergeResult;
    try {
        mergeResult = mergeAlgorithm.merge(RawTextComparator.DEFAULT, new RawText(base.getBytes(charsetName)),
                new RawText(generated.getBytes(charsetName)), new RawText(edited.getBytes(charsetName)));
    } catch (UnsupportedEncodingException e) {
        throw new GeneratorException("unknown character set " + charsetName, e);
    }
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    try {
        new MergeFormatter().formatMerge(outputStream, mergeResult, "base", "generated", "edited", charsetName);
    } catch (IOException e) {
        throw new GeneratorException("could nor render merge result", e);
    }
    try {
        String result = new String(outputStream.toByteArray(), charsetName);
        return result;
    } catch (UnsupportedEncodingException e) {
        throw new GeneratorException("unknown character set " + charsetName, e);
    }
}