Example usage for org.eclipse.jgit.treewalk TreeWalk idEqual

List of usage examples for org.eclipse.jgit.treewalk TreeWalk idEqual

Introduction

In this page you can find the example usage for org.eclipse.jgit.treewalk TreeWalk idEqual.

Prototype

public boolean idEqual(int nthA, int nthB) 

Source Link

Document

Compare two tree's current ObjectId values for equality.

Usage

From source file:com.beck.ep.team.git.GitListBuilder.java

License:BSD License

public String[] createList(Shell shell, IProject project) throws Exception {
    error = null;//from  www.j a  va  2  s. com
    RepositoryMapping mapping = RepositoryMapping.getMapping(project);
    if (mapping == null) {
        error = "Git Repository not found (project: " + project.getName() + ")";
        return null;
    }
    Repository rep = mapping.getRepository();
    String pjPath = project.getLocation().toFile().getAbsolutePath();
    String repPath = rep.getDirectory().getParentFile().getAbsolutePath();
    int cutPrefixLen = 0;
    int pathDiffLen = pjPath.length() - repPath.length();
    if (pathDiffLen > 0) {
        cutPrefixLen = pathDiffLen;//for repositoy is not in "parent folder of project"
    }

    RevWalk rw = new RevWalk(rep);
    TreeWalk tw = new TreeWalk(rep);

    int ignoreOpt = pref.getInt(GitModeMenuMgr.PREF_TYPE_IGNORE, GitModeMenuMgr.IGNORE_EXCLUDE);
    if (ignoreOpt == GitModeMenuMgr.IGNORE_ONLY) {
        String[] sa = listIgnored(rep, rw, tw, cutPrefixLen);
        if (cutPrefixLen > 0) {
            for (int i = 0; i < sa.length; i++) {
                if (sa[i].length() > cutPrefixLen) {
                    sa[i] = sa[i].substring(cutPrefixLen);
                }
            }
        }
        return sa;
    }

    int mode = pref.getInt(GitModeMenuMgr.PREF_TYPE_MODE, GitModeMenuMgr.MODE_BRANCH_HEAD);
    int sourceMode = pref.getInt(GitModeMenuMgr.PREF_TYPE_SOURCE, GitModeMenuMgr.MODE_WORKING_AREA);
    if (mode == sourceMode) {
        return new String[0];
    }
    int index = addTree(mode, shell, rep, rw, tw);
    if (index == -2) {
        return null;
    }
    int stageIndex = -1;
    boolean excludeUntrackOpt = false;
    if (!pref.getBoolean(GitModeMenuMgr.PREF_TYPE_UNTRACK, true)
            && sourceMode == GitModeMenuMgr.MODE_WORKING_AREA) {
        excludeUntrackOpt = true;
        if (mode == GitModeMenuMgr.MODE_STAGING) {
            stageIndex = 0;
        }
    }

    int idx2 = addTree(sourceMode, shell, rep, rw, tw);
    if (excludeUntrackOpt && stageIndex == -1) {
        addTree(GitModeMenuMgr.MODE_STAGING, shell, rep, rw, tw);
        stageIndex = 2;
    }
    // skip ignored resources
    if (ignoreOpt == GitModeMenuMgr.IGNORE_EXCLUDE && (index != -1 || idx2 != -1)) {
        int pos = (index != -1) ? index : idx2;
        NotIgnoredFilter notIgnoredFilter = new NotIgnoredFilter(pos);//index = tw.addTree(?)
        tw.setFilter(notIgnoredFilter);
    }

    tw.setRecursive(true);
    ArrayList<String> sa = new ArrayList<String>();
    while (tw.next()) {
        if (!tw.idEqual(0, 1)) {
            if (excludeUntrackOpt) {
                if (tw.getTree(stageIndex, AbstractTreeIterator.class) == null) {
                    WorkingTreeIterator t = tw.getTree(1, WorkingTreeIterator.class);
                    if (t != null && !t.isEntryIgnored()) {//file is untrack...
                        continue;
                    }
                }
            }
            String relPath = tw.getPathString();
            if (cutPrefixLen > 0 && relPath.length() > cutPrefixLen) {
                relPath = relPath.substring(cutPrefixLen);
            }
            sa.add(relPath);
        }
    }

    return sa.toArray(new String[sa.size()]);
}

From source file:com.mpdeimos.ct_tests.vcs.GitFileDiff.java

License:Apache License

/**
 * Computer file diffs for specified tree walk and commit
 *
 * @param walk/*from   w w  w.  j a va  2 s. co m*/
 * @param commit
 * @return non-null but possibly empty array of file diffs
 * @throws MissingObjectException
 * @throws IncorrectObjectTypeException
 * @throws CorruptObjectException
 * @throws IOException
 */
public static GitFileDiff[] compute(final TreeWalk walk, final RevCommit commit)
        throws MissingObjectException, IncorrectObjectTypeException, CorruptObjectException, IOException {
    final ArrayList<GitFileDiff> r = new ArrayList<GitFileDiff>();

    if (commit.getParentCount() > 0)
        walk.reset(trees(commit));
    else {
        walk.reset();
        walk.addTree(new EmptyTreeIterator());
        walk.addTree(commit.getTree());
    }

    if (walk.getTreeCount() <= 2) {
        List<DiffEntry> entries = DiffEntry.scan(walk);
        for (DiffEntry entry : entries) {
            final GitFileDiff d = new GitFileDiff(commit, entry);
            r.add(d);
        }
    } else { // DiffEntry does not support walks with more than two trees
        final int nTree = walk.getTreeCount();
        final int myTree = nTree - 1;
        while (walk.next()) {
            if (matchAnyParent(walk, myTree))
                continue;

            final GitFileDiffForMerges d = new GitFileDiffForMerges(commit);
            d.path = walk.getPathString();
            int m0 = 0;
            for (int i = 0; i < myTree; i++)
                m0 |= walk.getRawMode(i);
            final int m1 = walk.getRawMode(myTree);
            d.change = ChangeType.MODIFY;
            if (m0 == 0 && m1 != 0)
                d.change = ChangeType.ADD;
            else if (m0 != 0 && m1 == 0)
                d.change = ChangeType.DELETE;
            else if (m0 != m1 && walk.idEqual(0, myTree))
                d.change = ChangeType.MODIFY; // there is no ChangeType.TypeChanged
            d.blobs = new ObjectId[nTree];
            d.modes = new FileMode[nTree];
            for (int i = 0; i < nTree; i++) {
                d.blobs[i] = walk.getObjectId(i);
                d.modes[i] = walk.getFileMode(i);
            }
            r.add(d);
        }

    }

    final GitFileDiff[] tmp = new GitFileDiff[r.size()];
    r.toArray(tmp);
    return tmp;
}

From source file:com.mpdeimos.ct_tests.vcs.GitFileDiff.java

License:Apache License

private static boolean matchAnyParent(final TreeWalk walk, final int myTree) {
    final int m = walk.getRawMode(myTree);
    for (int i = 0; i < myTree; i++)
        if (walk.getRawMode(i) == m && walk.idEqual(i, myTree))
            return true;
    return false;
}

From source file:com.xiplink.jira.git.FileDiff.java

License:Open Source License

static FileDiff[] compute(final TreeWalk walk, final RevCommit commit)
        throws MissingObjectException, IncorrectObjectTypeException, CorruptObjectException, IOException {
    final ArrayList<FileDiff> r = new ArrayList<FileDiff>();

    if (commit.getParentCount() > 0) {
        walk.reset(trees(commit));/*  w  w w.  ja va 2  s  .  co m*/
    } else {
        walk.reset();
        walk.addTree(new EmptyTreeIterator());
        walk.addTree(commit.getTree());
    }

    if (walk.getTreeCount() <= 2) {
        List<DiffEntry> entries = DiffEntry.scan(walk);
        int number = 0;
        for (DiffEntry entry : entries) {
            final FileDiff d = new FileDiff(commit, entry, number);
            r.add(d);
            number++;
        }
    } else { // DiffEntry does not support walks with more than two trees
        final int nTree = walk.getTreeCount();
        final int myTree = nTree - 1;
        while (walk.next()) {
            if (matchAnyParent(walk, myTree)) {
                continue;
            }

            final FileDiffForMerges d = new FileDiffForMerges(commit);
            d.path = walk.getPathString();
            int m0 = 0;
            for (int i = 0; i < myTree; i++) {
                m0 |= walk.getRawMode(i);
            }
            final int m1 = walk.getRawMode(myTree);
            d.change = ChangeType.MODIFY;
            if (m0 == 0 && m1 != 0) {
                d.change = ChangeType.ADD;
            } else if (m0 != 0 && m1 == 0) {
                d.change = ChangeType.DELETE;
            } else if (m0 != m1 && walk.idEqual(0, myTree)) {
                d.change = ChangeType.MODIFY; // there is no ChangeType.TypeChanged
            }
            d.blobs = new ObjectId[nTree];
            d.modes = new FileMode[nTree];
            for (int i = 0; i < nTree; i++) {
                d.blobs[i] = walk.getObjectId(i);
                d.modes[i] = walk.getFileMode(i);
            }
            r.add(d);
        }

    }

    final FileDiff[] tmp = new FileDiff[r.size()];
    r.toArray(tmp);
    return tmp;
}

From source file:com.xiplink.jira.git.FileDiff.java

License:Open Source License

private static boolean matchAnyParent(final TreeWalk walk, final int myTree) {
    final int m = walk.getRawMode(myTree);
    for (int i = 0; i < myTree; i++) {
        if (walk.getRawMode(i) == m && walk.idEqual(i, myTree)) {
            return true;
        }/* w w w . ja  va 2 s .  com*/
    }
    return false;
}

From source file:de.fau.osr.core.vcs.impl.GitBlameOperation.java

License:Open Source License

/**
 * @throws GitAPIException/*from  w  w w.j  a  v  a  2s.  c o  m*/
 * @throws IOException
 * @return An instance of {@link AnnotatedWords} where every word from
 * the committed contents of path is mapped to a deduplicated list of annotations.
 */
public AnnotatedWords wordBlame() throws GitAPIException, IOException {

    /*
     * at the moment, just look at the state at HEAD,
     * could be expanded in the future to
     *    a) parameterize the used commit
     *    b) annotate the working copy instead 
     */
    ObjectId rootId = repo.resolve("HEAD");
    RevWalk walker = new RevWalk(repo);
    RevCommit rootCommit = walker.parseCommit(rootId);
    Function<ObjectId, byte[]> readFunction = id -> {
        try {
            return client.readBlob(id);
        } catch (IOException | GitAPIException e) {
            throw new RuntimeException(e);
        }
    };
    FlatSource source;
    try {
        ObjectId idAtHead = client.fileAtRev(walker, path, rootCommit);
        source = FlatSource.flatten(readFunction, idAtHead);
    } catch (Exception e) {
        throw new FileNotFoundException(path);
    }
    @SuppressWarnings("unchecked")
    List<Object>[] currentBlame = new List[source.size()];
    BlameItem topBlame = new BlameItem(rootCommit, new TreeMap<>(), path);

    /*
     * initially, blame all lines on HEAD
     */
    for (int i = 0; i < currentBlame.length; ++i) {
        currentBlame[i] = new ArrayList<>();
        topBlame.words.put(i, i);
    }

    workQueue.add(topBlame);

    while (!workQueue.isEmpty()) {
        BlameItem cur = workQueue.pollFirst();
        walker.parseCommit(cur.accused);

        ObjectId idAfter = client.fileAtRev(walker, cur.path, cur.accused);
        FlatSource after = FlatSource.flatten(readFunction, idAfter);

        /*
         * pull in custom annotations from putBlame on all suspect lines
         */
        if (putBlame != null) {
            String nameOfAccused = cur.accused.name();
            for (Map.Entry<Integer, Integer> entry : cur.words.entrySet()) {
                Iterator<? extends Object> iterator = putBlame.apply(nameOfAccused,
                        after.getLineByWord(entry.getKey()));
                if (iterator != null)
                    Iterators.addAll(currentBlame[entry.getValue()], iterator);
            }
        }

        RevCommit[] parents = cur.accused.getParents();

        /*
         * found indicates if we found an unmodified copy in a parent,
         * if false, foundLines indicates which lines we were able to blame
         * down in history
         */
        boolean found = false;
        HashSet<Integer> foundLines = new HashSet<>();
        for (RevCommit parent : parents) {
            walker.parseCommit(parent);

            TreeWalk treeWalk = TreeWalk.forPath(repo, cur.path, cur.accused.getTree(), parent.getTree());
            if (treeWalk.idEqual(0, 1)) {
                //the file has not changed between parent and accused

                BlameItem nextItem = cur.shallowClone();
                nextItem.accused = parent;
                push(nextItem);
                found = true;
            } else {
                //the file has changed

                ObjectId idBefore = client.fileAtRev(walker, cur.path, parent);
                if (idBefore == null) {
                    /*
                     * the file does not exist at the same path in parent and accused,
                     * so go look for identical files
                     * 
                     * could be extended to look for similar files, but watch performance!
                     */
                    treeWalk = new TreeWalk(repo);
                    treeWalk.setRecursive(true);
                    treeWalk.addTree(parent.getTree());
                    while (treeWalk.next()) {
                        if (treeWalk.getObjectId(0).equals(idAfter)) {
                            String pathBefore = treeWalk.getPathString();
                            BlameItem nextItem = cur.shallowClone();
                            nextItem.accused = parent;
                            nextItem.path = pathBefore;
                            push(nextItem);
                            found = true;
                            break;
                        }
                    }
                    continue;
                }
                //the file is at the same location in parent

                byte[] byteBefore = client.readBlob(idBefore);
                EditList diff;
                FlatSource before = FlatSource.flatten(readFunction, idBefore);
                diff = diffAlgorithm.diff(RawTextComparator.DEFAULT, before, after);

                /*
                 * The documentation does not state if diff is sorted,
                 * just that the Edits do not overlap, but it is much
                 * more convenient to have it sorted.
                 */
                final Comparator<Edit> compEdit = (d1, d2) -> d1.getBeginB() - d2.getBeginB();
                diff.sort(compEdit);

                BlameItem passedBlame = new BlameItem(parent, new TreeMap<>(), cur.path);

                Iterator<Map.Entry<Integer, Integer>> entryIterator = cur.words.entrySet().iterator();
                Iterator<Edit> editIterator = diff.iterator();
                Map.Entry<Integer, Integer> curEntry = null;
                if (entryIterator.hasNext())
                    curEntry = entryIterator.next();
                Edit curEdit = null;
                if (editIterator.hasNext())
                    curEdit = editIterator.next();
                int offset = 0;

                /*
                 * traverse diff and words simultaneously
                 */
                while (curEntry != null || entryIterator.hasNext()) {
                    if (curEntry == null) {
                        curEntry = entryIterator.next();
                    } else if (curEdit == null && editIterator.hasNext()) {
                        curEdit = editIterator.next();
                    } else if (curEdit != null && curEdit.getBeginB() <= curEntry.getKey()) {
                        if (curEdit.getEndB() > curEntry.getKey()) {
                            /*
                             * curEntry was erased by curEdit
                             */
                            curEntry = null;
                        } else {
                            /*
                             * curEdit introduced an offset before curEntry
                             */
                            offset += (curEdit.getEndA() - curEdit.getBeginA())
                                    - (curEdit.getEndB() - curEdit.getBeginB());
                            curEdit = null;
                        }
                    } else {
                        /*
                         * push curEntry with key corrected by offset
                         */
                        foundLines.add(curEntry.getKey());
                        passedBlame.words.put(curEntry.getKey() + offset, curEntry.getValue());
                        curEntry = null;
                    }
                }
                /*
                 * push the lines we found in parent back to queue
                 */
                push(passedBlame);
            }
        }
        /*
         * If there is not identical parent file, we have to take
         * responsibility for all lines not found in some parent.
         */
        if (!found) {
            for (Map.Entry<Integer, Integer> entry : cur.words.entrySet()) {
                if (!foundLines.contains(entry.getKey()))
                    currentBlame[entry.getValue()].add(cur.accused.getId());
            }
        }

    }

    /*
     * duplicate objects take up unneeded space, clean them up 
     */
    for (int i = 0; i < currentBlame.length; ++i)
        currentBlame[i] = new ArrayList<>(new HashSet<>(currentBlame[i]));

    return new AnnotatedWords(source, currentBlame);
}

From source file:org.eclipse.egit.core.synchronize.StagedChangeCache.java

License:Open Source License

private static boolean shouldIncludeEntry(TreeWalk tw) {
    final int mHead = tw.getRawMode(1);
    final int mCache = tw.getRawMode(0);

    return mHead == MISSING.getBits() // initial add to cache
            || mCache == MISSING.getBits() // removed from cache
            || (mHead != mCache || (mCache != TREE.getBits() && !tw.idEqual(1, 0))); // modified
}

From source file:org.eclipse.egit.ui.internal.decorators.DecoratableResourceAdapter.java

License:Open Source License

private void extractResourceProperties(TreeWalk treeWalk) throws IOException {
    final ContainerTreeIterator workspaceIterator = treeWalk.getTree(T_WORKSPACE, ContainerTreeIterator.class);
    final ResourceEntry resourceEntry = workspaceIterator != null ? workspaceIterator.getResourceEntry() : null;

    if (resourceEntry == null)
        return;/*from   www.ja  va 2  s . com*/

    if (workspaceIterator != null && workspaceIterator.isEntryIgnored()) {
        ignored = true;
        return;
    }

    final int mHead = treeWalk.getRawMode(T_HEAD);
    final int mIndex = treeWalk.getRawMode(T_INDEX);

    if (mHead == FileMode.MISSING.getBits() && mIndex == FileMode.MISSING.getBits())
        return;

    tracked = true;

    if (mHead == FileMode.MISSING.getBits()) {
        staged = Staged.ADDED;
    } else if (mIndex == FileMode.MISSING.getBits()) {
        staged = Staged.REMOVED;
    } else if (mHead != mIndex || (mIndex != FileMode.TREE.getBits() && !treeWalk.idEqual(T_HEAD, T_INDEX))) {
        staged = Staged.MODIFIED;
    } else {
        staged = Staged.NOT_STAGED;
    }

    final DirCacheIterator indexIterator = treeWalk.getTree(T_INDEX, DirCacheIterator.class);
    final DirCacheEntry indexEntry = indexIterator != null ? indexIterator.getDirCacheEntry() : null;

    if (indexEntry == null)
        return;

    if (indexEntry.getStage() > 0)
        conflicts = true;

    if (indexEntry.isAssumeValid()) {
        dirty = false;
        assumeValid = true;
    } else {
        if (workspaceIterator != null && workspaceIterator.isModified(indexEntry, true))
            dirty = true;
    }
}

From source file:org.eclipse.egit.ui.internal.history.FileDiff.java

License:Open Source License

static FileDiff[] compute(final TreeWalk walk, final RevCommit commit)
        throws MissingObjectException, IncorrectObjectTypeException, CorruptObjectException, IOException {
    final ArrayList<FileDiff> r = new ArrayList<FileDiff>();

    if (commit.getParentCount() > 0)
        walk.reset(trees(commit));/*ww  w.  jav a  2 s .co m*/
    else {
        walk.reset();
        walk.addTree(new EmptyTreeIterator());
        walk.addTree(commit.getTree());
    }

    if (walk.getTreeCount() <= 2) {
        List<DiffEntry> entries = DiffEntry.scan(walk);
        for (DiffEntry entry : entries) {
            final FileDiff d = new FileDiff(commit, entry);
            r.add(d);
        }
    } else { // DiffEntry does not support walks with more than two trees
        final int nTree = walk.getTreeCount();
        final int myTree = nTree - 1;
        while (walk.next()) {
            if (matchAnyParent(walk, myTree))
                continue;

            final FileDiffForMerges d = new FileDiffForMerges(commit);
            d.path = walk.getPathString();
            int m0 = 0;
            for (int i = 0; i < myTree; i++)
                m0 |= walk.getRawMode(i);
            final int m1 = walk.getRawMode(myTree);
            d.change = ChangeType.MODIFY;
            if (m0 == 0 && m1 != 0)
                d.change = ChangeType.ADD;
            else if (m0 != 0 && m1 == 0)
                d.change = ChangeType.DELETE;
            else if (m0 != m1 && walk.idEqual(0, myTree))
                d.change = ChangeType.MODIFY; // there is no ChangeType.TypeChanged
            d.blobs = new ObjectId[nTree];
            d.modes = new FileMode[nTree];
            for (int i = 0; i < nTree; i++) {
                d.blobs[i] = walk.getObjectId(i);
                d.modes[i] = walk.getFileMode(i);
            }
            r.add(d);
        }

    }

    final FileDiff[] tmp = new FileDiff[r.size()];
    r.toArray(tmp);
    return tmp;
}

From source file:org.eclipse.egit.ui.internal.synchronize.model.GitModelCache.java

License:Open Source License

private boolean shouldIncludeEntry(TreeWalk tw) {
    final int mHead = tw.getRawMode(BASE_NTH);
    final int mCache = tw.getRawMode(REMOTE_NTH);

    return mHead == MISSING.getBits() // initial add to cache
            || mCache == MISSING.getBits() // removed from cache
            || (mHead != mCache || (mCache != TREE.getBits() && !tw.idEqual(BASE_NTH, REMOTE_NTH))); // modified
}