Example usage for org.eclipse.jgit.diff DiffFormatter close

List of usage examples for org.eclipse.jgit.diff DiffFormatter close

Introduction

In this page you can find the example usage for org.eclipse.jgit.diff DiffFormatter close.

Prototype

@Override
public void close() 

Source Link

Document

Release the internal ObjectReader state.

Usage

From source file:boa.datagen.scm.GitCommit.java

License:Apache License

private void getChangeFiles(final RevCommit parent, final RevCommit rc,
        final HashMap<String, String> rChangedPaths, final HashMap<String, String> rRemovedPaths,
        final HashMap<String, String> rAddedPaths) {
    final DiffFormatter df = new DiffFormatter(NullOutputStream.INSTANCE);
    df.setRepository(repository);// w ww . j  av a 2  s. c o  m
    df.setDiffComparator(RawTextComparator.DEFAULT);
    df.setDetectRenames(true);

    try {
        final AbstractTreeIterator parentIter;
        if (parent == null)
            parentIter = new EmptyTreeIterator();
        else
            parentIter = new CanonicalTreeParser(null, repository.newObjectReader(), parent.getTree());

        for (final DiffEntry diff : df.scan(parentIter,
                new CanonicalTreeParser(null, repository.newObjectReader(), rc.getTree()))) {
            if (diff.getChangeType() == ChangeType.MODIFY || diff.getChangeType() == ChangeType.COPY
                    || diff.getChangeType() == ChangeType.RENAME) {
                if (diff.getOldMode().getObjectType() == Constants.OBJ_BLOB
                        && diff.getNewMode().getObjectType() == Constants.OBJ_BLOB) {
                    String path = diff.getNewPath();
                    rChangedPaths.put(path, diff.getOldPath());
                    filePathGitObjectIds.put(path, diff.getNewId().toObjectId());
                }
            } else if (diff.getChangeType() == ChangeType.ADD) {
                if (diff.getNewMode().getObjectType() == Constants.OBJ_BLOB) {
                    String path = diff.getNewPath();
                    rAddedPaths.put(path, null);
                    filePathGitObjectIds.put(path, diff.getNewId().toObjectId());
                }
            } else if (diff.getChangeType() == ChangeType.DELETE) {
                if (diff.getOldMode().getObjectType() == Constants.OBJ_BLOB) {
                    rRemovedPaths.put(diff.getOldPath(), diff.getOldPath());
                }
            }
        }
    } catch (final IOException e) {
        if (debug)
            System.err.println("Git Error getting commit diffs: " + e.getMessage());
    }
    df.close();
}

From source file:org.eclipse.che.git.impl.jgit.JGitDiffPage.java

License:Open Source License

@Override
public final void writeTo(OutputStream out) throws IOException {
    DiffFormatter formatter = new DiffFormatter(new BufferedOutputStream(out));
    formatter.setRepository(repository);
    List<String> rawFileFilter = request.getFileFilter();
    TreeFilter pathFilter = (rawFileFilter != null && rawFileFilter.size() > 0)
            ? PathFilterGroup.createFromStrings(rawFileFilter)
            : TreeFilter.ALL;/*from ww w.  j  a v  a2  s  .co m*/
    formatter.setPathFilter(AndTreeFilter.create(TreeFilter.ANY_DIFF, pathFilter));

    try {
        String commitA = request.getCommitA();
        String commitB = request.getCommitB();
        boolean cached = request.isCached();

        List<DiffEntry> diff;
        if (commitA == null && commitB == null && !cached) {
            diff = indexToWorkingTree(formatter);
        } else if (commitA != null && commitB == null && !cached) {
            diff = commitToWorkingTree(commitA, formatter);
        } else if (commitB == null) {
            diff = commitToIndex(commitA, formatter);
        } else {
            diff = commitToCommit(commitA, commitB, formatter);
        }

        DiffType type = request.getType();
        if (type == DiffType.NAME_ONLY) {
            writeNames(diff, out);
        } else if (type == DiffType.NAME_STATUS) {
            writeNamesAndStatus(diff, out);
        } else {
            writeRawDiff(diff, formatter);
        }
    } finally {
        formatter.close();
        repository.close();
    }
}

From source file:org.uberfire.java.nio.fs.jgit.util.commands.CustomDiffCommand.java

License:Apache License

/**
 * Executes the {@code Diff} command with all the options and parameters
 * collected by the setter methods (e.g. {@link #setCached(boolean)} of this
 * class. Each instance of this class should only be used for one invocation
 * of the command. Don't call this method twice on an instance.
 * @return a DiffEntry for each path which is different
 *///  w  w  w .j ava  2 s .c  o m
public List<DiffEntry> call() throws GitAPIException {
    final DiffFormatter diffFmt;
    if (out != null && !showNameAndStatusOnly) {
        diffFmt = new DiffFormatter(new BufferedOutputStream(out));
    } else {
        diffFmt = new DiffFormatter(NullOutputStream.INSTANCE);
    }
    diffFmt.setRepository(repo);
    diffFmt.setProgressMonitor(monitor);
    diffFmt.setDetectRenames(true);
    try {
        if (cached) {
            if (oldTree == null) {
                ObjectId head = git.getTreeFromRef(HEAD);
                if (head == null) {
                    throw new NoHeadException(JGitText.get().cannotReadTree);
                }
                CanonicalTreeParser p = new CanonicalTreeParser();
                ObjectReader reader = repo.newObjectReader();
                try {
                    p.reset(reader, head);
                } finally {
                    reader.close();
                }
                oldTree = p;
            }
            newTree = new DirCacheIterator(repo.readDirCache());
        } else {
            if (oldTree == null) {
                oldTree = new DirCacheIterator(repo.readDirCache());
            }
            if (newTree == null) {
                newTree = new FileTreeIterator(repo);
            }
        }

        diffFmt.setPathFilter(pathFilter);

        List<DiffEntry> result = diffFmt.scan(oldTree, newTree);
        if (showNameAndStatusOnly) {
            return result;
        } else {
            if (contextLines >= 0) {
                diffFmt.setContext(contextLines);
            }
            if (destinationPrefix != null) {
                diffFmt.setNewPrefix(destinationPrefix);
            }
            if (sourcePrefix != null) {
                diffFmt.setOldPrefix(sourcePrefix);
            }
            diffFmt.format(result);
            diffFmt.flush();
            return result;
        }
    } catch (IOException e) {
        throw new JGitInternalException(e.getMessage(), e);
    } finally {
        diffFmt.close();
    }
}