Example usage for org.eclipse.jgit.revwalk RevWalk newFlag

List of usage examples for org.eclipse.jgit.revwalk RevWalk newFlag

Introduction

In this page you can find the example usage for org.eclipse.jgit.revwalk RevWalk newFlag.

Prototype

public RevFlag newFlag(String name) 

Source Link

Document

Create a new flag for application use during walking.

Usage

From source file:com.github.koraktor.mavanagaiata.git.jgit.JGitRepository.java

License:Open Source License

public GitTagDescription describe() throws GitRepositoryException {
    HashMap<RevCommit, String> tagCommits = new HashMap<RevCommit, String>();
    for (Map.Entry<String, RevTag> tag : this.getRawTags().entrySet()) {
        tagCommits.put((RevCommit) tag.getValue().getObject(), tag.getValue().getName());
    }/*from  w  ww  .ja va2s  . c  o m*/

    RevCommit start = this.getCommit(this.getHeadObject());
    RevWalk revWalk = this.getRevWalk();
    RevFlag seenFlag = revWalk.newFlag("SEEN");

    int distance = -1;
    GitTag nextTag = null;
    HashSet<RevCommit> commits = new HashSet<RevCommit>();
    commits.add(start);
    while (!commits.isEmpty()) {
        distance++;
        HashSet<RevCommit> nextCommits = new HashSet<RevCommit>();

        for (RevCommit currentCommit : commits) {
            try {
                revWalk.parseHeaders(currentCommit);
            } catch (IOException e) {
                throw new GitRepositoryException("Unable to parse headers of commit " + currentCommit.getName(),
                        e);
            }

            if (currentCommit.has(seenFlag)) {
                continue;
            }
            currentCommit.add(seenFlag);

            if (tagCommits.containsKey(currentCommit)) {
                nextTag = this.getTags().get(currentCommit.getId().getName());
                break;
            }

            if (currentCommit.getParents() != null) {
                nextCommits.addAll(Arrays.asList(currentCommit.getParents()));
            }
        }

        commits.clear();
        commits.addAll(nextCommits);
    }

    return new GitTagDescription(this, this.getHeadCommit(), nextTag, distance);
}

From source file:com.google.gerrit.server.change.GetRelatedByAncestors.java

License:Apache License

private List<ChangeAndCommit> children(RevisionResource rsrc, RevWalk rw, Map<Change.Id, ChangeData> changes,
        Map<PatchSet.Id, PatchSet> patchSets, Set<Change.Id> added) throws OrmException, IOException {
    // children is a map of parent commit name to PatchSet built on it.
    Multimap<String, PatchSet.Id> children = allChildren(changes.keySet());

    RevFlag seenCommit = rw.newFlag("seenCommit");
    LinkedList<String> q = Lists.newLinkedList();
    seedQueue(rsrc, rw, seenCommit, patchSets, q);

    ProjectControl projectCtl = rsrc.getControl().getProjectControl();
    Set<Change.Id> seenChange = Sets.newHashSet();
    List<ChangeAndCommit> graph = Lists.newArrayList();
    while (!q.isEmpty()) {
        String id = q.remove();/*ww w.j ava  2  s .  c  o m*/

        // For every matching change find the most recent patch set.
        Map<Change.Id, PatchSet.Id> matches = Maps.newHashMap();
        for (PatchSet.Id psId : children.get(id)) {
            PatchSet.Id e = matches.get(psId.getParentKey());
            if ((e == null || e.get() < psId.get()) && isVisible(projectCtl, changes, patchSets, psId)) {
                matches.put(psId.getParentKey(), psId);
            }
        }

        for (Map.Entry<Change.Id, PatchSet.Id> e : matches.entrySet()) {
            ChangeData cd = changes.get(e.getKey());
            PatchSet ps = patchSets.get(e.getValue());
            if (cd == null || ps == null || !seenChange.add(e.getKey())) {
                continue;
            }

            RevCommit c = rw.parseCommit(ObjectId.fromString(ps.getRevision().get()));
            if (!c.has(seenCommit)) {
                c.add(seenCommit);
                q.addFirst(ps.getRevision().get());
                if (added.add(ps.getId().getParentKey())) {
                    rw.parseBody(c);
                    graph.add(new ChangeAndCommit(cd.change(), ps, c));
                }
            }
        }
    }
    Collections.reverse(graph);
    return graph;
}

From source file:com.google.gerrit.server.change.IncludedInResolver.java

License:Apache License

private static RevFlag newFlag(RevWalk rw) {
    return rw.newFlag("CONTAINS_TARGET");
}

From source file:com.google.gerrit.server.query.change.ConflictsPredicate.java

License:Apache License

private static List<Predicate<ChangeData>> predicates(final Arguments args, String value, List<Change> changes)
        throws OrmException {
    List<Predicate<ChangeData>> changePredicates = Lists.newArrayListWithCapacity(changes.size());
    final Provider<ReviewDb> db = args.db;
    for (final Change c : changes) {
        final ChangeDataCache changeDataCache = new ChangeDataCache(c, db, args.changeDataFactory,
                args.projectCache);/*from w  w w.  j a  va 2s  .c o  m*/
        List<String> files = args.changeDataFactory.create(db.get(), c).currentFilePaths();
        List<Predicate<ChangeData>> filePredicates = Lists.newArrayListWithCapacity(files.size());
        for (String file : files) {
            filePredicates.add(new EqualsPathPredicate(ChangeQueryBuilder.FIELD_PATH, file));
        }

        List<Predicate<ChangeData>> predicatesForOneChange = Lists.newArrayListWithCapacity(5);
        predicatesForOneChange.add(not(new LegacyChangeIdPredicate(args.getSchema(), c.getId())));
        predicatesForOneChange.add(new ProjectPredicate(c.getProject().get()));
        predicatesForOneChange.add(new RefPredicate(c.getDest().get()));
        predicatesForOneChange.add(or(filePredicates));
        predicatesForOneChange
                .add(new OperatorPredicate<ChangeData>(ChangeQueryBuilder.FIELD_CONFLICTS, value) {

                    @Override
                    public boolean match(ChangeData object) throws OrmException {
                        Change otherChange = object.change();
                        if (otherChange == null) {
                            return false;
                        }
                        if (!otherChange.getDest().equals(c.getDest())) {
                            return false;
                        }
                        SubmitType submitType = getSubmitType(object);
                        if (submitType == null) {
                            return false;
                        }
                        ObjectId other = ObjectId.fromString(object.currentPatchSet().getRevision().get());
                        ConflictKey conflictsKey = new ConflictKey(changeDataCache.getTestAgainst(), other,
                                submitType, changeDataCache.getProjectState().isUseContentMerge());
                        Boolean conflicts = args.conflictsCache.getIfPresent(conflictsKey);
                        if (conflicts != null) {
                            return conflicts;
                        }
                        try (Repository repo = args.repoManager.openRepository(otherChange.getProject());
                                CodeReviewRevWalk rw = CodeReviewCommit.newRevWalk(repo)) {
                            RevFlag canMergeFlag = rw.newFlag("CAN_MERGE");
                            CodeReviewCommit commit = rw.parseCommit(changeDataCache.getTestAgainst());
                            SubmitStrategy strategy = args.submitStrategyFactory.create(submitType, db.get(),
                                    repo, rw, null, canMergeFlag, getAlreadyAccepted(repo, rw, commit),
                                    otherChange.getDest(), null);
                            CodeReviewCommit otherCommit = rw.parseCommit(other);
                            otherCommit.add(canMergeFlag);
                            conflicts = !strategy.dryRun(commit, otherCommit);
                            args.conflictsCache.put(conflictsKey, conflicts);
                            return conflicts;
                        } catch (MergeException | NoSuchProjectException | IOException e) {
                            throw new IllegalStateException(e);
                        }
                    }

                    @Override
                    public int getCost() {
                        return 5;
                    }

                    private SubmitType getSubmitType(ChangeData cd) throws OrmException {
                        SubmitTypeRecord r = new SubmitRuleEvaluator(cd).getSubmitType();
                        if (r.status != SubmitTypeRecord.Status.OK) {
                            return null;
                        }
                        return r.type;
                    }

                    private Set<RevCommit> getAlreadyAccepted(Repository repo, RevWalk rw, CodeReviewCommit tip)
                            throws MergeException {
                        Set<RevCommit> alreadyAccepted = Sets.newHashSet();

                        if (tip != null) {
                            alreadyAccepted.add(tip);
                        }

                        try {
                            for (ObjectId id : changeDataCache.getAlreadyAccepted(repo)) {
                                try {
                                    alreadyAccepted.add(rw.parseCommit(id));
                                } catch (IncorrectObjectTypeException iote) {
                                    // Not a commit? Skip over it.
                                }
                            }
                        } catch (IOException e) {
                            throw new MergeException("Failed to determine already accepted commits.", e);
                        }

                        return alreadyAccepted;
                    }
                });
        changePredicates.add(and(predicatesForOneChange));
    }
    return changePredicates;
}

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

License:Open Source License

/**
 * Scans given {@code repo} and build list of commits between two given
 * RevCommit objectId's. Each commit contains list of changed resources
 *
 * @param repo/* ww  w.j a  v a  2 s  .  c om*/
 *            repository that should be scanned
 * @param srcId
 *            RevCommit id that git history traverse will start from
 * @param dstId
 *            RevCommit id that git history traverse will end
 * @param pathFilter
 *            path filter definition or {@code null} when all paths should
 *            be included
 * @return list of {@link Commit} object's between {@code srcId} and
 *         {@code dstId}
 * @throws IOException
 */
public static List<Commit> build(Repository repo, ObjectId srcId, ObjectId dstId, TreeFilter pathFilter)
        throws IOException {
    if (dstId.equals(srcId))
        return new ArrayList<Commit>(0);

    final RevWalk rw = new RevWalk(repo);

    final RevFlag localFlag = rw.newFlag("local"); //$NON-NLS-1$
    final RevFlag remoteFlag = rw.newFlag("remote"); //$NON-NLS-1$
    final RevFlagSet allFlags = new RevFlagSet();
    allFlags.add(localFlag);
    allFlags.add(remoteFlag);
    rw.carry(allFlags);

    RevCommit srcCommit = rw.parseCommit(srcId);
    srcCommit.add(localFlag);
    rw.markStart(srcCommit);
    srcCommit = null; // free not needed resources

    RevCommit dstCommit = rw.parseCommit(dstId);
    dstCommit.add(remoteFlag);
    rw.markStart(dstCommit);
    dstCommit = null; // free not needed resources

    if (pathFilter != null)
        rw.setTreeFilter(pathFilter);

    List<Commit> result = new ArrayList<Commit>();
    for (RevCommit revCommit : rw) {
        if (revCommit.hasAll(allFlags))
            break;

        Commit commit = new Commit();
        commit.shortMessage = revCommit.getShortMessage();
        commit.commitId = AbbreviatedObjectId.fromObjectId(revCommit);
        commit.authorName = revCommit.getAuthorIdent().getName();
        commit.committerName = revCommit.getCommitterIdent().getName();
        commit.commitDate = revCommit.getAuthorIdent().getWhen();

        RevCommit actualCommit, parentCommit;
        if (revCommit.has(localFlag)) {
            actualCommit = revCommit;
            parentCommit = getParentCommit(revCommit);
            commit.direction = RIGHT;
        } else if (revCommit.has(remoteFlag)) {
            actualCommit = getParentCommit(revCommit);
            parentCommit = revCommit;
            commit.direction = LEFT;
        } else
            throw new GitCommitsModelDirectionException();

        commit.children = getChangedObjects(repo, actualCommit, parentCommit, pathFilter, commit.direction);

        if (commit.children != null)
            result.add(commit);
    }
    rw.dispose();

    return result;
}

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

License:Open Source License

private int calculateKindImpl(Repository repo, TreeWalk tw, int srcNth, int dstNth) throws IOException {
    ObjectId srcId = tw.getObjectId(srcNth);
    ObjectId dstId = tw.getObjectId(dstNth);

    if (srcId.equals(zeroId()))
        return INCOMING | ADDITION;
    if (dstId.equals(zeroId()))
        return OUTGOING | ADDITION;
    if (!srcId.equals(dstId)) {
        RevWalk rw = new RevWalk(repo);
        RevFlag srcFlag = rw.newFlag("source"); //$NON-NLS-1$
        RevFlag dstFlag = rw.newFlag("destination"); //$NON-NLS-1$
        initializeRevWalk(rw, srcFlag, dstFlag);

        RevCommit commit = rw.next();/*w  ww.  j av a  2 s .c  o  m*/
        if (commit.has(srcFlag))
            return OUTGOING | CHANGE;
        else if (commit.has(dstFlag))
            return INCOMING | CHANGE;
        else
            return CONFLICTING | CHANGE;
    }

    return IN_SYNC;
}

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

License:Open Source License

GenerateHistoryJob(final GitHistoryPage ghp, Control control, RevWalk walk, ResourceManager resources) {
    super(NLS.bind(UIText.HistoryPage_refreshJob, Activator.getDefault().getRepositoryUtil()
            .getRepositoryName(ghp.getInputInternal().getRepository())));
    page = ghp;//  w w w . j  a  v a  2 s . c  om
    this.walk = walk;
    highlightFlag = walk.newFlag("highlight"); //$NON-NLS-1$
    loadedCommits = new SWTCommitList(control, resources);
    loadedCommits.source(walk);
    trace = GitTraceLocation.HISTORYVIEW.isActive();
}

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

License:Open Source License

private void getChildrenImpl() {
    RevWalk rw = new RevWalk(repo);
    RevFlag localFlag = rw.newFlag("local"); //$NON-NLS-1$
    RevFlag remoteFlag = rw.newFlag("remote"); //$NON-NLS-1$
    RevFlagSet allFlags = new RevFlagSet();
    allFlags.add(localFlag);/*from w  w  w  .  ja  va2 s .  c o  m*/
    allFlags.add(remoteFlag);
    rw.carry(allFlags);
    List<GitModelObjectContainer> result = new ArrayList<GitModelObjectContainer>();

    rw.setRetainBody(true);
    try {
        RevCommit srcCommit = rw.parseCommit(srcRev);
        srcCommit.add(localFlag);
        rw.markStart(srcCommit);

        RevCommit dstCommit = rw.parseCommit(dstRev);
        dstCommit.add(remoteFlag);
        rw.markStart(dstCommit);

        for (RevCommit nextCommit : rw) {
            if (nextCommit.hasAll(allFlags))
                break;

            if (nextCommit.has(localFlag))
                result.add(new GitModelCommit(this, nextCommit, RIGHT));
            else if (nextCommit.has(remoteFlag))
                result.add(new GitModelCommit(this, nextCommit, LEFT));
        }

        if (includeLocal) {
            result.add(new GitModelCache(this, srcCommit));
            result.add(new GitModelWorkingTree(this, srcCommit));
        }
    } catch (IOException e) {
        Activator.logError(e.getMessage(), e);
    }

    childrens = result.toArray(new GitModelObjectContainer[result.size()]);
}