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

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

Introduction

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

Prototype

public void setOldPrefix(String prefix) 

Source Link

Document

Set the prefix applied in front of old file paths.

Usage

From source file:com.tenxdev.ovcs.command.DiffCommand.java

License:Open Source License

/**
 * {@inheritDoc}//from ww  w.jav  a2  s .c o  m
 */
@Override
public void execute(final String... args) throws OvcsException {
    if (args.length != 1 && args.length != 2) {
        throw new UsageException(USAGE);
    }
    String targetObject = null;
    if (args.length == 2) {
        targetObject = args[1].toUpperCase(Locale.getDefault());
        if (!targetObject.toLowerCase(Locale.getDefault()).endsWith(".sql")) {
            targetObject = targetObject + ".sql";
        }
    }
    final FileRepository fileRepository = getRepoForCurrentDir();
    try {
        writeChanges(fileRepository);
        final DiffFormatter formatter = new DiffFormatter(System.out);
        formatter.setNewPrefix("new/");
        formatter.setOldPrefix("old/");
        formatter.setRepository(fileRepository);
        if (targetObject != null) {
            formatter.setPathFilter(PathFilter.create(targetObject));
        }
        final AbstractTreeIterator commitTreeIterator = GitUtils.prepareHeadTreeParser(fileRepository);
        final FileTreeIterator workTreeIterator = new FileTreeIterator(fileRepository);
        final List<DiffEntry> diffEntries = formatter.scan(commitTreeIterator, workTreeIterator);

        for (final DiffEntry entry : diffEntries) {
            System.out.println("Entry: " + entry);
            formatter.format(entry);
        }
    } catch (final IOException e) {
        throw new OvcsException("Unable to generate diff: " + e.getMessage(), e);
    } finally {
        fileRepository.close();
    }
}

From source file:org.eclipse.orion.server.gerritfs.DiffCommand.java

License:Eclipse Distribution 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  ww.  j  ava2s  .  c o  m*/
@Override
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);
    if (ignoreWS)
        diffFmt.setDiffComparator(RawTextComparator.WS_IGNORE_ALL);
    diffFmt.setRepository(repo);
    diffFmt.setProgressMonitor(monitor);
    try {
        if (cached) {
            if (oldTree == null) {
                ObjectId head = repo.resolve(HEAD + "^{tree}"); //$NON-NLS-1$
                if (head == null)
                    throw new NoHeadException(JGitText.get().cannotReadTree);
                CanonicalTreeParser p = new CanonicalTreeParser();
                ObjectReader reader = repo.newObjectReader();
                try {
                    p.reset(reader, head);
                } finally {
                    reader.release();
                }
                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.release();
    }
}

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
 *//*www. j  a v  a  2  s  .c  om*/
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();
    }
}