Search in sources :

Example 1 with RevFlag

use of org.eclipse.jgit.revwalk.RevFlag in project gerrit by GerritCodeReview.

the class SubmitDryRun method run.

public boolean run(SubmitType submitType, Repository repo, CodeReviewRevWalk rw, Branch.NameKey destBranch, ObjectId tip, ObjectId toMerge, Set<RevCommit> alreadyAccepted) throws IntegrationException, NoSuchProjectException, IOException {
    CodeReviewCommit tipCommit = rw.parseCommit(tip);
    CodeReviewCommit toMergeCommit = rw.parseCommit(toMerge);
    RevFlag canMerge = rw.newFlag("CAN_MERGE");
    toMergeCommit.add(canMerge);
    Arguments args = new Arguments(repo, rw, mergeUtilFactory.create(getProject(destBranch)), new MergeSorter(rw, alreadyAccepted, canMerge, ImmutableSet.of(toMergeCommit)));
    switch(submitType) {
        case CHERRY_PICK:
            return CherryPick.dryRun(args, tipCommit, toMergeCommit);
        case FAST_FORWARD_ONLY:
            return FastForwardOnly.dryRun(args, tipCommit, toMergeCommit);
        case MERGE_ALWAYS:
            return MergeAlways.dryRun(args, tipCommit, toMergeCommit);
        case MERGE_IF_NECESSARY:
            return MergeIfNecessary.dryRun(args, tipCommit, toMergeCommit);
        case REBASE_IF_NECESSARY:
            return RebaseIfNecessary.dryRun(args, repo, tipCommit, toMergeCommit);
        case REBASE_ALWAYS:
            return RebaseAlways.dryRun(args, repo, tipCommit, toMergeCommit);
        default:
            String errorMsg = "No submit strategy for: " + submitType;
            log.error(errorMsg);
            throw new IntegrationException(errorMsg);
    }
}
Also used : IntegrationException(com.google.gerrit.server.git.IntegrationException) RevFlag(org.eclipse.jgit.revwalk.RevFlag) MergeSorter(com.google.gerrit.server.git.MergeSorter) CodeReviewCommit(com.google.gerrit.server.git.CodeReviewCommit)

Example 2 with RevFlag

use of org.eclipse.jgit.revwalk.RevFlag in project egit by eclipse.

the class GitCommitsModelCache method build.

/**
 * Scans given {@code repo} and build list of commits between two given
 * RevCommit objectId's. Each commit contains list of changed resources
 *
 * @param repo
 *            repository that should be scanned
 * @param srcId
 *            commit id that is considered the "local" version (e.g. from
 *            master)
 * @param dstId
 *            commit id that is considered the "remote" version (e.g. from
 *            origin/master)
 * @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);
    try (RevWalk rw = new RevWalk(repo)) {
        // $NON-NLS-1$
        final RevFlag localFlag = rw.newFlag("local");
        // $NON-NLS-1$
        final RevFlag remoteFlag = rw.newFlag("remote");
        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);
        // free not needed resources
        srcCommit = null;
        RevCommit dstCommit = rw.parseCommit(dstId);
        dstCommit.add(remoteFlag);
        rw.markStart(dstCommit);
        // free not needed resources
        dstCommit = null;
        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 parentCommit = getParentCommit(revCommit);
            if (revCommit.has(localFlag))
                // Outgoing
                commit.direction = RIGHT;
            else if (revCommit.has(remoteFlag))
                // Incoming
                commit.direction = LEFT;
            else
                throw new GitCommitsModelDirectionException();
            commit.children = getChangedObjects(repo, revCommit, parentCommit, pathFilter, commit.direction);
            if (commit.children != null)
                result.add(commit);
        }
        rw.dispose();
        return result;
    }
}
Also used : RevCommit(org.eclipse.jgit.revwalk.RevCommit) RevFlagSet(org.eclipse.jgit.revwalk.RevFlagSet) RevFlag(org.eclipse.jgit.revwalk.RevFlag) ArrayList(java.util.ArrayList) RevWalk(org.eclipse.jgit.revwalk.RevWalk) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 3 with RevFlag

use of org.eclipse.jgit.revwalk.RevFlag in project gerrit by GerritCodeReview.

the class SubmitDryRun method run.

public boolean run(@Nullable CurrentUser caller, SubmitType submitType, Repository repo, CodeReviewRevWalk rw, BranchNameKey destBranch, ObjectId tip, ObjectId toMerge, Set<RevCommit> alreadyAccepted) throws NoSuchProjectException, IOException {
    CodeReviewCommit tipCommit = rw.parseCommit(tip);
    CodeReviewCommit toMergeCommit = rw.parseCommit(toMerge);
    RevFlag canMerge = rw.newFlag("CAN_MERGE");
    toMergeCommit.add(canMerge);
    Arguments args = new Arguments(repo, rw, mergeUtilFactory.create(getProject(destBranch)), new MergeSorter(caller, rw, alreadyAccepted, canMerge, queryProvider, ImmutableSet.of(toMergeCommit)));
    switch(submitType) {
        case CHERRY_PICK:
            return CherryPick.dryRun(args, tipCommit, toMergeCommit);
        case FAST_FORWARD_ONLY:
            return FastForwardOnly.dryRun(args, tipCommit, toMergeCommit);
        case MERGE_ALWAYS:
            return MergeAlways.dryRun(args, tipCommit, toMergeCommit);
        case MERGE_IF_NECESSARY:
            return MergeIfNecessary.dryRun(args, tipCommit, toMergeCommit);
        case REBASE_IF_NECESSARY:
            return RebaseIfNecessary.dryRun(args, repo, tipCommit, toMergeCommit);
        case REBASE_ALWAYS:
            return RebaseAlways.dryRun(args, repo, tipCommit, toMergeCommit);
        case INHERIT:
        default:
            String errorMsg = "No submit strategy for: " + submitType;
            logger.atSevere().log("%s", errorMsg);
            throw new StorageException(errorMsg);
    }
}
Also used : RevFlag(org.eclipse.jgit.revwalk.RevFlag) CodeReviewCommit(com.google.gerrit.server.git.CodeReviewCommit) StorageException(com.google.gerrit.exceptions.StorageException)

Example 4 with RevFlag

use of org.eclipse.jgit.revwalk.RevFlag in project gerrit by GerritCodeReview.

the class WalkSorter method sortProject.

private ImmutableList<PatchSetData> sortProject(Project.NameKey project, Collection<ChangeData> in) throws IOException {
    try (Repository repo = repoManager.openRepository(project);
        RevWalk rw = new RevWalk(repo)) {
        rw.setRetainBody(retainBody);
        ListMultimap<RevCommit, PatchSetData> byCommit = byCommit(rw, in);
        if (byCommit.isEmpty()) {
            return ImmutableList.of();
        } else if (byCommit.size() == 1) {
            return ImmutableList.of(byCommit.values().iterator().next());
        }
        // Walk from all patch set SHA-1s, and terminate as soon as we've found
        // everything we're looking for. This is equivalent to just sorting the
        // list of commits by the RevWalk's configured order.
        // 
        // Partially topo sort the list, ensuring no parent is emitted before a
        // direct child that is also in the input set. This preserves the stable,
        // expected sort in the case where many commits share the same timestamp,
        // e.g. a quick rebase. It also avoids JGit's topo sort, which slurps all
        // interesting commits at the beginning, which is a problem since we don't
        // know which commits to mark as uninteresting. Finding a reasonable set
        // of commits to mark uninteresting (the "rootmost" set) is at least as
        // difficult as just implementing this partial topo sort ourselves.
        // 
        // (This is slightly less efficient than JGit's topo sort, which uses a
        // private in-degree field in RevCommit rather than multimaps. We assume
        // the input size is small enough that this is not an issue.)
        Set<RevCommit> commits = byCommit.keySet();
        ListMultimap<RevCommit, RevCommit> children = collectChildren(commits);
        ListMultimap<RevCommit, RevCommit> pending = MultimapBuilder.hashKeys().arrayListValues().build();
        Deque<RevCommit> todo = new ArrayDeque<>();
        RevFlag done = rw.newFlag("done");
        markStart(rw, commits);
        int expected = commits.size();
        int found = 0;
        RevCommit c;
        List<PatchSetData> result = new ArrayList<>(expected);
        while (found < expected && (c = rw.next()) != null) {
            if (!commits.contains(c)) {
                continue;
            }
            todo.clear();
            todo.add(c);
            int i = 0;
            while (!todo.isEmpty()) {
                // Sanity check: we can't pop more than N pending commits, otherwise
                // we have an infinite loop due to programmer error or something.
                checkState(++i <= commits.size(), "Too many pending steps while sorting %s", commits);
                RevCommit t = todo.removeFirst();
                if (t.has(done)) {
                    continue;
                }
                boolean ready = true;
                for (RevCommit child : children.get(t)) {
                    if (!child.has(done)) {
                        pending.put(child, t);
                        ready = false;
                    }
                }
                if (ready) {
                    found += emit(t, byCommit, result, done);
                    todo.addAll(pending.get(t));
                }
            }
        }
        return ImmutableList.copyOf(result);
    }
}
Also used : ArrayList(java.util.ArrayList) RevWalk(org.eclipse.jgit.revwalk.RevWalk) ArrayDeque(java.util.ArrayDeque) Repository(org.eclipse.jgit.lib.Repository) RevFlag(org.eclipse.jgit.revwalk.RevFlag) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Aggregations

RevFlag (org.eclipse.jgit.revwalk.RevFlag)4 CodeReviewCommit (com.google.gerrit.server.git.CodeReviewCommit)2 ArrayList (java.util.ArrayList)2 RevCommit (org.eclipse.jgit.revwalk.RevCommit)2 RevWalk (org.eclipse.jgit.revwalk.RevWalk)2 StorageException (com.google.gerrit.exceptions.StorageException)1 IntegrationException (com.google.gerrit.server.git.IntegrationException)1 MergeSorter (com.google.gerrit.server.git.MergeSorter)1 ArrayDeque (java.util.ArrayDeque)1 Repository (org.eclipse.jgit.lib.Repository)1 RevFlagSet (org.eclipse.jgit.revwalk.RevFlagSet)1