Example usage for org.eclipse.jgit.diff RenameDetector compute

List of usage examples for org.eclipse.jgit.diff RenameDetector compute

Introduction

In this page you can find the example usage for org.eclipse.jgit.diff RenameDetector compute.

Prototype

public List<DiffEntry> compute() throws IOException 

Source Link

Document

Detect renames in the current file set.

Usage

From source file:com.microsoft.gittf.core.tasks.pendDiff.PendDifferenceTask.java

License:Open Source License

/**
 * Creates the CheckinAnalysisChangeCollection analysis object that includes
 * the list of pending changes needed that map to the differences between
 * the fromCommit and toCommit//ww  w. ja  v  a  2s .  com
 * 
 * @param repository
 *        the git repository
 * @param fromRootTree
 *        the from commit tree
 * @param toRootTree
 *        the to commit tree
 * @param renameMode
 *        the rename mode to use when generating the analysis
 * @param progressMonitor
 *        the progress monitor to use to report progress
 * @return
 * @throws Exception
 */
public static CheckinAnalysisChangeCollection analyzeDifferences(Repository repository, RevObject fromRootTree,
        RevObject toRootTree, RenameMode renameMode, final TaskProgressMonitor progressMonitor)
        throws Exception {
    Check.notNull(fromRootTree, "fromRootTree"); //$NON-NLS-1$
    Check.notNull(toRootTree, "toRootTree"); //$NON-NLS-1$
    Check.notNull(progressMonitor, "progressMonitor"); //$NON-NLS-1$

    progressMonitor.beginTask(Messages.getString("PendDifferencesTask.AnalyzingCommits"), //$NON-NLS-1$
            TaskProgressMonitor.INDETERMINATE, TaskProgressDisplay.DISPLAY_SUBTASK_DETAIL);

    /* Init the analysis object */
    final CheckinAnalysisChangeCollection analysis = new CheckinAnalysisChangeCollection(repository,
            fromRootTree, toRootTree);

    log.debug("Walking thru the git-repository tree.");

    /* Init the tree walker object */
    final TreeWalk treeWalker = new NameConflictTreeWalk(repository);
    final RenameDetector repositoryRenameDetector = new RenameDetector(repository);

    try {
        treeWalker.setRecursive(true);

        treeWalker.addTree(fromRootTree);
        treeWalker.addTree(toRootTree);

        treeWalker.setFilter(TreeFilter.ANY_DIFF);

        List<DiffEntry> treeDifferences = DiffEntry.scan(treeWalker);

        log.debug("The number of differences found: " + treeDifferences.size());

        /*
         * If we need to detect file renames then use the rename detector to
         * analayze the differences first
         */
        if (renameMode != RenameMode.NONE) {
            repositoryRenameDetector.addAll(treeDifferences);
            treeDifferences = repositoryRenameDetector.compute();
        }

        /*
         * If the rename mode is either none or file only then we do not
         * need to detect folder deletes as well, since deleting empty
         * folders that have items that have been renamed out is not
         * supported in TFS
         */
        if (renameMode != RenameMode.ALL) {
            analysis.setProcessDeletedFolders(false);
        }

        Map<String, DiffEntry> deleteChanges = new HashMap<String, DiffEntry>();
        Map<String, DiffEntry> addChanges = new HashMap<String, DiffEntry>();

        for (DiffEntry change : treeDifferences) {
            switch (change.getChangeType()) {
            case ADD:
                addChanges.put(change.getNewPath().toUpperCase(), change);
                break;

            case DELETE:
                deleteChanges.put(change.getOldPath().toUpperCase(), change);
                break;
            }
        }

        /* Append each change in to the analysis object */
        for (DiffEntry change : treeDifferences) {
            switch (change.getChangeType()) {
            case ADD:
            case COPY:
                if (!isCaseSensitiveRename(change, deleteChanges, addChanges)) {
                    analysis.pendAdd(new AddChange(change.getNewPath(),
                            CommitUtil.resolveAbbreviatedId(repository, change.getNewId())));
                    analysis.pendPropertyIfChanged(new PropertyChange(change.getNewPath(),
                            CommitUtil.resolveAbbreviatedId(repository, change.getNewId()),
                            change.getNewMode()));
                }
                break;

            case DELETE:
                if (isCaseSensitiveRename(change, deleteChanges, addChanges)) {
                    DiffEntry addChange = addChanges.get(change.getOldPath().toUpperCase());

                    analysis.pendRename(new RenameChange(change.getOldPath(), addChange.getNewPath(),
                            CommitUtil.resolveAbbreviatedId(repository, addChange.getNewId()),
                            !change.getOldId().equals(addChange.getNewId())));
                    analysis.pendPropertyIfChanged(new PropertyChange(addChange.getNewPath(),
                            CommitUtil.resolveAbbreviatedId(repository, addChange.getNewId()),
                            change.getOldMode(), addChange.getNewMode()));
                } else {
                    analysis.pendDelete(new DeleteChange(change.getOldPath(), change.getOldMode()));
                }
                break;

            case MODIFY:
                analysis.pendEdit(new EditChange(change.getNewPath(),
                        CommitUtil.resolveAbbreviatedId(repository, change.getNewId())));
                analysis.pendPropertyIfChanged(new PropertyChange(change.getNewPath(),
                        CommitUtil.resolveAbbreviatedId(repository, change.getNewId()), change.getOldMode(),
                        change.getNewMode()));
                break;

            case RENAME:
                analysis.pendRename(new RenameChange(change.getOldPath(), change.getNewPath(),
                        CommitUtil.resolveAbbreviatedId(repository, change.getNewId()),
                        !change.getOldId().equals(change.getNewId())));
                analysis.pendPropertyIfChanged(new PropertyChange(change.getNewPath(),
                        CommitUtil.resolveAbbreviatedId(repository, change.getNewId()), change.getOldMode(),
                        change.getNewMode()));
            }
        }
    } finally {
        if (treeWalker != null) {
            treeWalker.release();
        }

        progressMonitor.endTask();
    }

    log.debug("Walk thru the git-repository tree finished.");

    return analysis;
}

From source file:de.andreasgiemza.jgeagle.repo.rcs.JGit.java

License:Open Source License

/**
 *
 * Based on http://stackoverflow.com/a/11504177/2246865 by OneWorld
 *
 * @param follow// w  w w  .  ja  va 2  s.com
 * @param start
 * @param path
 * @return
 * @throws IOException
 * @throws GitAPIException
 */
private String getRenamedPath(boolean follow, RevCommit start, String path)
        throws IOException, GitAPIException {
    if (!follow) {
        return null;
    }

    Iterable<RevCommit> allCommitsLater = git.log().add(start).call();

    for (RevCommit commit : allCommitsLater) {
        TreeWalk tw = new TreeWalk(repository);
        tw.addTree(commit.getTree());
        tw.addTree(start.getTree());
        tw.setRecursive(true);
        RenameDetector rd = new RenameDetector(repository);
        rd.addAll(DiffEntry.scan(tw));
        List<DiffEntry> files = rd.compute();

        for (DiffEntry deffE : files) {
            if ((deffE.getChangeType() == DiffEntry.ChangeType.RENAME
                    || deffE.getChangeType() == DiffEntry.ChangeType.COPY)
                    && deffE.getNewPath().contains(path)) {
                return deffE.getOldPath();
            }
        }
    }

    return null;
}