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

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

Introduction

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

Prototype

public void setContext(int lineCount) 

Source Link

Document

Change the number of lines of context to display.

Usage

From source file:org.eclipse.egit.core.op.CreatePatchOperation.java

License:Open Source License

public void execute(IProgressMonitor monitor) throws CoreException {
    EclipseGitProgressTransformer gitMonitor;
    if (monitor == null)
        gitMonitor = new EclipseGitProgressTransformer(new NullProgressMonitor());
    else/* w  w w .j ava  2s .  c om*/
        gitMonitor = new EclipseGitProgressTransformer(monitor);

    final StringBuilder sb = new StringBuilder();
    final DiffFormatter diffFmt = new DiffFormatter(new ByteArrayOutputStream() {

        @Override
        public synchronized void write(byte[] b, int off, int len) {
            super.write(b, off, len);
            if (currentEncoding == null)
                sb.append(toString());
            else
                try {
                    sb.append(toString(currentEncoding));
                } catch (UnsupportedEncodingException e) {
                    sb.append(toString());
                }
            reset();
        }
    }) {
        private IProject project;

        @Override
        public void format(DiffEntry ent) throws IOException, CorruptObjectException, MissingObjectException {
            // for "workspace patches" add project header each time project changes
            if (DiffHeaderFormat.WORKSPACE == headerFormat) {
                IProject p = getProject(ent);
                if (!p.equals(project)) {
                    project = p;
                    getOutputStream().write(encodeASCII("#P " + project.getName() + "\n")); //$NON-NLS-1$ //$NON-NLS-2$
                }
            }
            super.format(ent);
        }
    };

    diffFmt.setProgressMonitor(gitMonitor);
    diffFmt.setContext(contextLines);

    if (headerFormat != null && headerFormat != DiffHeaderFormat.NONE)
        writeGitPatchHeader(sb);

    diffFmt.setRepository(repository);
    diffFmt.setPathFilter(pathFilter);

    try {
        if (commit != null) {
            RevCommit[] parents = commit.getParents();
            if (parents.length > 1)
                throw new IllegalStateException(CoreText.CreatePatchOperation_cannotCreatePatchForMergeCommit);
            if (parents.length == 0)
                throw new IllegalStateException(CoreText.CreatePatchOperation_cannotCreatePatchForFirstCommit);

            List<DiffEntry> diffs = diffFmt.scan(parents[0].getId(), commit.getId());
            for (DiffEntry ent : diffs) {
                String path;
                if (ChangeType.DELETE.equals(ent.getChangeType()))
                    path = ent.getOldPath();
                else
                    path = ent.getNewPath();
                currentEncoding = CompareCoreUtils.getResourceEncoding(repository, path);
                diffFmt.format(ent);
            }
        } else
            diffFmt.format(new DirCacheIterator(repository.readDirCache()), new FileTreeIterator(repository));
    } catch (IOException e) {
        Activator.logError(CoreText.CreatePatchOperation_patchFileCouldNotBeWritten, e);
    }

    if (DiffHeaderFormat.WORKSPACE == headerFormat)
        updateWorkspacePatchPrefixes(sb, diffFmt);

    // trim newline
    if (sb.charAt(sb.length() - 1) == '\n')
        sb.setLength(sb.length() - 1);

    patchContent = sb.toString();
}

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
 *//*  www .j av a 2 s  .  co 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.gitistics.visitor.commit.TreeWalkVisitorStandard.java

License:Open Source License

private List<DiffEntry> diffCommitWithParent(DiffFormatter formatter, RevCommit commit, RevCommit parent) {
    try {//from   www .  ja v a 2s  . c  o m
        formatter.setContext(0);
        return formatter.scan(parent, commit);
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        formatter.release();
    }
}

From source file:org.gitistics.visitor.commit.TreeWalkVisitorStandard.java

License:Open Source License

private EditList handleEdits(DiffFormatter formatter, RevCommit commit, DiffEntry entry) {
    try {//from   ww w  .  j a  va 2  s.c  o  m
        formatter.setContext(0);
        FileHeader h = formatter.toFileHeader(entry);
        return h.toEditList();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.repodriller.scm.GitRepository.java

License:Apache License

private void setContext(DiffFormatter df) {
    try {/*from w  w  w.j a va 2  s . co m*/
        int context = getSystemProperty("git.diffcontext"); /* TODO: make it into a configuration */
        df.setContext(context);
    } catch (Exception e) {
        return;
    }
}

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
 *///from w ww  .  ja va  2s  .com
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();
    }
}