Search in sources :

Example 41 with RevWalk

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

the class GetContent method getMessage.

private String getMessage(ChangeNotes notes) throws OrmException, IOException {
    Change.Id changeId = notes.getChangeId();
    PatchSet ps = psUtil.current(db.get(), notes);
    if (ps == null) {
        throw new NoSuchChangeException(changeId);
    }
    try (Repository git = gitManager.openRepository(notes.getProjectName());
        RevWalk revWalk = new RevWalk(git)) {
        RevCommit commit = revWalk.parseCommit(ObjectId.fromString(ps.getRevision().get()));
        return commit.getFullMessage();
    } catch (RepositoryNotFoundException e) {
        throw new NoSuchChangeException(changeId, e);
    }
}
Also used : Repository(org.eclipse.jgit.lib.Repository) NoSuchChangeException(com.google.gerrit.server.project.NoSuchChangeException) PatchSet(com.google.gerrit.reviewdb.client.PatchSet) Change(com.google.gerrit.reviewdb.client.Change) RepositoryNotFoundException(org.eclipse.jgit.errors.RepositoryNotFoundException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 42 with RevWalk

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

the class GetMergeList method apply.

@Override
public Response<List<CommitInfo>> apply(RevisionResource rsrc) throws BadRequestException, IOException {
    Project.NameKey p = rsrc.getChange().getProject();
    try (Repository repo = repoManager.openRepository(p);
        RevWalk rw = new RevWalk(repo)) {
        String rev = rsrc.getPatchSet().getRevision().get();
        RevCommit commit = rw.parseCommit(ObjectId.fromString(rev));
        rw.parseBody(commit);
        if (uninterestingParent < 1 || uninterestingParent > commit.getParentCount()) {
            throw new BadRequestException("No such parent: " + uninterestingParent);
        }
        if (commit.getParentCount() < 2) {
            return createResponse(rsrc, ImmutableList.<CommitInfo>of());
        }
        List<RevCommit> commits = MergeListBuilder.build(rw, commit, uninterestingParent);
        List<CommitInfo> result = new ArrayList<>(commits.size());
        ChangeJson changeJson = json.noOptions();
        for (RevCommit c : commits) {
            result.add(changeJson.toCommit(rsrc.getControl(), rw, c, addLinks, true));
        }
        return createResponse(rsrc, result);
    }
}
Also used : Project(com.google.gerrit.reviewdb.client.Project) Repository(org.eclipse.jgit.lib.Repository) ArrayList(java.util.ArrayList) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) CommitInfo(com.google.gerrit.extensions.common.CommitInfo) RevWalk(org.eclipse.jgit.revwalk.RevWalk) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 43 with RevWalk

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

the class ConflictsPredicate method listFiles.

public static List<String> listFiles(Change c, Arguments args, ChangeDataCache changeDataCache) throws OrmException {
    try (Repository repo = args.repoManager.openRepository(c.getProject());
        RevWalk rw = new RevWalk(repo)) {
        RevCommit ps = rw.parseCommit(changeDataCache.getTestAgainst());
        if (ps.getParentCount() > 1) {
            String dest = c.getDest().get();
            Ref destBranch = repo.getRefDatabase().getRef(dest);
            destBranch.getObjectId();
            rw.setRevFilter(RevFilter.MERGE_BASE);
            rw.markStart(rw.parseCommit(destBranch.getObjectId()));
            rw.markStart(ps);
            RevCommit base = rw.next();
            // TODO(zivkov): handle the case with multiple merge bases
            List<String> files = new ArrayList<>();
            try (TreeWalk tw = new TreeWalk(repo)) {
                if (base != null) {
                    tw.setFilter(TreeFilter.ANY_DIFF);
                    tw.addTree(base.getTree());
                }
                tw.addTree(ps.getTree());
                tw.setRecursive(true);
                while (tw.next()) {
                    files.add(tw.getPathString());
                }
            }
            return files;
        }
        return args.changeDataFactory.create(args.db.get(), c).currentFilePaths();
    } catch (IOException e) {
        throw new OrmException(e);
    }
}
Also used : Repository(org.eclipse.jgit.lib.Repository) Ref(org.eclipse.jgit.lib.Ref) OrmException(com.google.gwtorm.server.OrmException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) CodeReviewRevWalk(com.google.gerrit.server.git.CodeReviewCommit.CodeReviewRevWalk) RevWalk(org.eclipse.jgit.revwalk.RevWalk) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 44 with RevWalk

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

the class ConflictsPredicate method predicates.

public static List<Predicate<ChangeData>> predicates(final Arguments args, String value, List<Change> changes) throws QueryParseException, OrmException {
    int indexTerms = 0;
    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);
        List<String> files = listFiles(c, args, changeDataCache);
        indexTerms += 3 + files.size();
        if (indexTerms > args.indexConfig.maxTerms()) {
            // later on in the query parsing.
            throw new QueryParseException(TOO_MANY_FILES);
        }
        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(c.getId())));
        predicatesForOneChange.add(new ProjectPredicate(c.getProject().get()));
        predicatesForOneChange.add(new RefPredicate(c.getDest().get()));
        predicatesForOneChange.add(or(or(filePredicates), new IsMergePredicate(args, value)));
        predicatesForOneChange.add(new ChangeOperatorPredicate(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;
                }
                SubmitTypeRecord str = object.submitTypeRecord();
                if (!str.isOk()) {
                    return false;
                }
                ObjectId other = ObjectId.fromString(object.currentPatchSet().getRevision().get());
                ConflictKey conflictsKey = new ConflictKey(changeDataCache.getTestAgainst(), other, str.type, 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)) {
                    conflicts = !args.submitDryRun.run(str.type, repo, rw, otherChange.getDest(), changeDataCache.getTestAgainst(), other, getAlreadyAccepted(repo, rw));
                    args.conflictsCache.put(conflictsKey, conflicts);
                    return conflicts;
                } catch (IntegrationException | NoSuchProjectException | IOException e) {
                    throw new OrmException(e);
                }
            }

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

            private Set<RevCommit> getAlreadyAccepted(Repository repo, RevWalk rw) throws IntegrationException {
                try {
                    Set<RevCommit> accepted = new HashSet<>();
                    SubmitDryRun.addCommits(changeDataCache.getAlreadyAccepted(repo), rw, accepted);
                    ObjectId tip = changeDataCache.getTestAgainst();
                    if (tip != null) {
                        accepted.add(rw.parseCommit(tip));
                    }
                    return accepted;
                } catch (OrmException | IOException e) {
                    throw new IntegrationException("Failed to determine already accepted commits.", e);
                }
            }
        });
        changePredicates.add(and(predicatesForOneChange));
    }
    return changePredicates;
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) OrPredicate(com.google.gerrit.server.query.OrPredicate) Predicate(com.google.gerrit.server.query.Predicate) OrmException(com.google.gwtorm.server.OrmException) ReviewDb(com.google.gerrit.reviewdb.server.ReviewDb) IntegrationException(com.google.gerrit.server.git.IntegrationException) ObjectId(org.eclipse.jgit.lib.ObjectId) CodeReviewRevWalk(com.google.gerrit.server.git.CodeReviewCommit.CodeReviewRevWalk) Change(com.google.gerrit.reviewdb.client.Change) CodeReviewRevWalk(com.google.gerrit.server.git.CodeReviewCommit.CodeReviewRevWalk) RevWalk(org.eclipse.jgit.revwalk.RevWalk) QueryParseException(com.google.gerrit.server.query.QueryParseException) Repository(org.eclipse.jgit.lib.Repository) SubmitTypeRecord(com.google.gerrit.common.data.SubmitTypeRecord)

Example 45 with RevWalk

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

the class AbstractQueryChangesTest method newPatchSet.

protected Change newPatchSet(TestRepository<Repo> repo, Change c) throws Exception {
    // Add a new file so the patch set is not a trivial rebase, to avoid default
    // Code-Review label copying.
    int n = c.currentPatchSetId().get() + 1;
    RevCommit commit = repo.parseBody(repo.commit().message("message").add("file" + n, "contents " + n).create());
    ChangeControl ctl = changeControlFactory.controlFor(db, c, user);
    PatchSetInserter inserter = patchSetFactory.create(ctl, new PatchSet.Id(c.getId(), n), commit).setNotify(NotifyHandling.NONE).setFireRevisionCreated(false).setValidate(false);
    try (BatchUpdate bu = updateFactory.create(db, c.getProject(), user, TimeUtil.nowTs());
        ObjectInserter oi = repo.getRepository().newObjectInserter();
        ObjectReader reader = oi.newReader();
        RevWalk rw = new RevWalk(reader)) {
        bu.setRepository(repo.getRepository(), rw, oi);
        bu.addOp(c.getId(), inserter);
        bu.execute();
    }
    return inserter.getChange();
}
Also used : ObjectInserter(org.eclipse.jgit.lib.ObjectInserter) PatchSetInserter(com.google.gerrit.server.change.PatchSetInserter) ChangeControl(com.google.gerrit.server.project.ChangeControl) ObjectReader(org.eclipse.jgit.lib.ObjectReader) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) ObjectId(org.eclipse.jgit.lib.ObjectId) RevWalk(org.eclipse.jgit.revwalk.RevWalk) BatchUpdate(com.google.gerrit.server.update.BatchUpdate) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Aggregations

RevWalk (org.eclipse.jgit.revwalk.RevWalk)487 RevCommit (org.eclipse.jgit.revwalk.RevCommit)292 Repository (org.eclipse.jgit.lib.Repository)205 ObjectId (org.eclipse.jgit.lib.ObjectId)199 IOException (java.io.IOException)160 Ref (org.eclipse.jgit.lib.Ref)124 Test (org.junit.Test)76 ObjectReader (org.eclipse.jgit.lib.ObjectReader)69 ArrayList (java.util.ArrayList)61 ObjectInserter (org.eclipse.jgit.lib.ObjectInserter)55 TreeWalk (org.eclipse.jgit.treewalk.TreeWalk)47 File (java.io.File)39 RevTree (org.eclipse.jgit.revwalk.RevTree)39 TestRepository (org.eclipse.jgit.junit.TestRepository)35 Git (org.eclipse.jgit.api.Git)32 PersonIdent (org.eclipse.jgit.lib.PersonIdent)32 RevObject (org.eclipse.jgit.revwalk.RevObject)32 InMemoryRepository (org.eclipse.jgit.internal.storage.dfs.InMemoryRepository)31 HashMap (java.util.HashMap)30 Map (java.util.Map)27