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

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

Introduction

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

Prototype

public MergeAlgorithm() 

Source Link

Document

Creates a new MergeAlgorithm which uses org.eclipse.jgit.diff.HistogramDiff as diff algorithm

Usage

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

License:Apache License

/**
 * Performs a three-way merge.//from w w  w .  j  a v  a 2 s  . co 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);
    }
}

From source file:org.gitreduce.GitReduce.java

License:Apache License

public static int merge(String base, String generated, String edited, Charset charset) throws IOException {
    MergeAlgorithm mergeAlgorithm = new MergeAlgorithm();
    MergeResult<RawText> mergeResult;
    mergeResult = mergeAlgorithm.merge(RawTextComparator.DEFAULT, new RawText(base.getBytes(charset)),
            new RawText(generated.getBytes(charset)), new RawText(edited.getBytes(charset)));
    return mergeResult.containsConflicts() == true ? 1 : 0;
}

From source file:org.openflexo.hannah.IterativeFileGenerator.java

License:Open Source License

/**
 * <p>Ends the generation. It asks to resolve conflicts (if any). By 
 * default conflict are resolved using user modifications.</p>
 * @param callback callback to handle conflicts
 * @throws IOException//from w  w w.j  a  v  a 2  s .  com
 */
public void end(ConflictHandler callback) throws IOException, GitAPIException {
    final Status status = git.status().call();

    // checks if needs commit.
    if (status.isClean() == false) {

        // checks for files to add
        boolean execute = false;
        final AddCommand add = git.add();
        for (String filename : status.getModified()) {
            execute = true;
            add.addFilepattern(filename);
        }

        for (String filename : status.getUntracked()) {
            execute = true;
            add.addFilepattern(filename);
        }
        if (execute)
            add.call();

        // checks for files to remove
        execute = false;
        final RmCommand rm = git.rm();
        for (String filename : status.getMissing()) {
            execute = true;
            rm.addFilepattern(filename);
        }
        if (execute)
            rm.call();

        git.commit().setMessage("Generation").call();
    }

    // checks out master branch
    git.checkout().setName(MASTER).call();

    // merges generation branch with master (resolving conflict with USER).
    final Repository repo = git.getRepository();
    final Ref generationHead = repo.getRef(GENERATION);
    final MergeStatus mergeStatus = git.merge().include(generationHead).call().getMergeStatus();

    // in case of conflicts, uses the resolution mode to choose the outcome
    if (mergeStatus == MergeStatus.CONFLICTING) {
        // maps to stores confliting files
        final Map<String, DirCacheEntry> baseEntry = new LinkedHashMap<String, DirCacheEntry>();
        final Map<String, DirCacheEntry> userEntry = new LinkedHashMap<String, DirCacheEntry>();
        final Map<String, DirCacheEntry> generationEntry = new LinkedHashMap<String, DirCacheEntry>();

        // for all conflicting entry collects base, user and generation entries. 
        final DirCache cache = repo.lockDirCache();
        for (int i = 0; i < cache.getEntryCount(); i++) {
            final DirCacheEntry entry = cache.getEntry(i);
            switch (entry.getStage()) {
            case DirCacheEntry.STAGE_1:
                baseEntry.put(entry.getPathString(), entry);
                break;
            case DirCacheEntry.STAGE_2:
                userEntry.put(entry.getPathString(), entry);
                break;
            case DirCacheEntry.STAGE_3:
                generationEntry.put(entry.getPathString(), entry);
                break;
            }
        }

        // creates list of conflicting files
        final List<ConflictingFile> conflictingFiles = new ArrayList<ConflictingFile>();
        final MergeAlgorithm mergeAlgorithm = new MergeAlgorithm();
        for (final String path : baseEntry.keySet()) {
            final RawText baseText = getRawText(baseEntry.get(path));
            final RawText userText = getRawText(userEntry.get(path));
            final RawText generationText = getRawText(generationEntry.get(path));

            final MergeResult<RawText> result = mergeAlgorithm.merge(RawTextComparator.DEFAULT, baseText,
                    userText, generationText);
            conflictingFiles.add(new ConflictingFile(path, result));

        }
        // unlocks cache.
        cache.unlock();

        // calls the callback
        callback.conflicts(conflictingFiles);

        // prepares the reset command
        final ResetCommand reset = git.reset();

        // applies callback selections
        for (final ConflictingFile conflictingFile : conflictingFiles) {
            final File file = new File(repo.getWorkTree(), conflictingFile.getPath());
            FileUtil.writeFile(file, conflictingFile.getContents(), "UTF-8");

            reset.addPath(conflictingFile.getPath());
        }

        // resets repository state to allows commit.
        reset.call();

        // commit resolutions
        git.commit().setMessage("User/Generation merge conflicts resolutions.").call();
    }

    // renames git repository to hannah
    gitFolder.renameTo(hannahFolder);
}