Example usage for org.eclipse.jgit.api DiffCommand setPathFilter

List of usage examples for org.eclipse.jgit.api DiffCommand setPathFilter

Introduction

In this page you can find the example usage for org.eclipse.jgit.api DiffCommand setPathFilter.

Prototype

public DiffCommand setPathFilter(TreeFilter pathFilter) 

Source Link

Document

Set path filter

Usage

From source file:com.centurylink.mdw.dataaccess.file.VersionControlGit.java

License:Apache License

public GitDiffs getDiffs(String branch, String path) throws Exception {
    fetch();/*w  w  w  . j av  a2  s  .c o  m*/
    GitDiffs diffs = new GitDiffs();
    ObjectId remoteHead = localRepo.resolve("origin/" + branch + "^{tree}");
    if (remoteHead == null)
        throw new IOException("Unable to determine Git Diffs due to missing remote HEAD");
    CanonicalTreeParser newTreeIter = new CanonicalTreeParser();
    newTreeIter.reset(localRepo.newObjectReader(), remoteHead);
    DiffCommand dc = git.diff().setNewTree(newTreeIter);
    if (path != null)
        dc.setPathFilter(PathFilter.create(path));
    dc.setShowNameAndStatusOnly(true);
    for (DiffEntry diff : dc.call()) {
        if (diff.getChangeType() == ChangeType.ADD || diff.getChangeType() == ChangeType.COPY) {
            diffs.add(DiffType.MISSING, diff.getNewPath());
        } else if (diff.getChangeType() == ChangeType.MODIFY) {
            diffs.add(DiffType.DIFFERENT, diff.getNewPath());
        } else if (diff.getChangeType() == ChangeType.DELETE) {
            diffs.add(DiffType.EXTRA, diff.getOldPath());
        } else if (diff.getChangeType() == ChangeType.RENAME) {
            diffs.add(DiffType.MISSING, diff.getNewPath());
            diffs.add(DiffType.EXTRA, diff.getOldPath());
        }
    }
    // we're purposely omitting folders
    Status status = git.status().addPath(path).call();
    for (String untracked : status.getUntracked()) {
        if (!untracked.startsWith(path + "/Archive/"))
            diffs.add(DiffType.EXTRA, untracked);
    }
    for (String added : status.getAdded()) {
        diffs.add(DiffType.EXTRA, added);
    }
    for (String missing : status.getMissing()) {
        diffs.add(DiffType.MISSING, missing);
    }
    for (String removed : status.getRemoved()) {
        diffs.add(DiffType.MISSING, removed);
    }
    for (String changed : status.getChanged()) {
        diffs.add(DiffType.DIFFERENT, changed);
    }
    for (String modified : status.getModified()) {
        diffs.add(DiffType.DIFFERENT, modified);
    }
    for (String conflict : status.getConflicting()) {
        diffs.add(DiffType.DIFFERENT, conflict);
    }
    return diffs;
}

From source file:org.eclipse.emf.compare.git.pgm.internal.app.LogicalDiffApplication.java

License:Open Source License

/**
 * {@inheritDoc}.//from  w ww.  j a  v a  2 s .  co m
 */
@Override
protected Integer performGitCommand() {
    try {
        IWorkspace ws = ResourcesPlugin.getWorkspace();

        // Call JGit diff to get the files involved
        OutputStream out = new ByteArrayOutputStream();
        DiffCommand diffCommand = Git.open(repo.getDirectory()).diff().setOutputStream(out);
        if (commit != null) {
            diffCommand = diffCommand.setOldTree(getTreeIterator(repo, commit));
        }
        if (commitWith != null) {
            diffCommand = diffCommand.setNewTree(getTreeIterator(repo, commitWith));
        }
        if (pathFilter != null) {
            diffCommand = diffCommand.setPathFilter(pathFilter);
        }
        Set<IFile> files = new HashSet<IFile>();
        List<DiffEntry> entries = diffCommand.call();

        for (DiffEntry diffEntry : entries) {
            String path = diffEntry.getOldPath();
            if (path != null) {
                IFile file = ws.getRoot().getFile(new Path(path));
                if (file != null) {
                    files.add(file);
                }
            }
        }

        if (files.isEmpty()) {
            System.out.println("No difference to display.");
            return Returns.COMPLETE.code();
        }

        for (IFile file : files) {
            RemoteResourceMappingContext mergeContext = createSubscriberForComparison(repo, commit, commitWith,
                    file);
            if (!isEMFCompareCompliantFile(mergeContext, file)) {
                diffCommand.setPathFilter(PathFilter.create(file.getProjectRelativePath().toString()));
                diffCommand.call();
                System.out.println(out.toString());
            } else {
                final ResourceMapping[] emfMappings = getResourceMappings(mergeContext, file);
                for (ResourceMapping mapping : emfMappings) {
                    if (mapping instanceof EMFResourceMapping) {
                        /*
                         * These two won't be used by the builder in our case since we retrieve the
                         * already created syncModel from the resource mapping.
                         */
                        final IModelMinimizer minimizer = new IdenticalResourceMinimizer();
                        final NullProgressMonitor nullProgressMonitor = new NullProgressMonitor();

                        mapping.getTraversals(mergeContext, nullProgressMonitor);
                        final SynchronizationModel syncModel = ((EMFResourceMapping) mapping).getLatestModel();
                        minimizer.minimize(syncModel, nullProgressMonitor);
                        final IComparisonScope scope = ComparisonScopeBuilder.create(syncModel,
                                nullProgressMonitor);

                        final Comparison comparison = EMFCompare.builder().build().compare(scope,
                                BasicMonitor.toMonitor(nullProgressMonitor));

                        Resource resource = new XMIResourceImpl();
                        Copier copier = new Copier(false);
                        EObject comparisonCopy = copier.copy(comparison);
                        copier.copyReferences();
                        resource.getContents().add(comparisonCopy);

                        ByteArrayOutputStream baos = new ByteArrayOutputStream();
                        resource.save(baos, null);
                        System.out.println(baos.toString("UTF-8"));//$NON-NLS-1$
                    }
                }
            }
        }
    } catch (Exception e) {
        System.err.println(e.getMessage());
        e.printStackTrace();
    }

    return Returns.COMPLETE.code();
}