Search in sources :

Example 96 with Ref

use of org.eclipse.jgit.lib.Ref in project gerrit by GerritCodeReview.

the class AbstractSubmoduleSubscription method hasSubmodule.

protected boolean hasSubmodule(TestRepository<?> repo, String branch, String submodule) throws Exception {
    submodule = name(submodule);
    Ref branchTip = repo.git().fetch().setRemote("origin").call().getAdvertisedRef("refs/heads/" + branch);
    if (branchTip == null) {
        return false;
    }
    ObjectId commitId = branchTip.getObjectId();
    RevWalk rw = repo.getRevWalk();
    RevCommit c = rw.parseCommit(commitId);
    rw.parseBody(c.getTree());
    RevTree tree = c.getTree();
    try {
        repo.get(tree, submodule);
        return true;
    } catch (AssertionError e) {
        return false;
    }
}
Also used : Ref(org.eclipse.jgit.lib.Ref) ObjectId(org.eclipse.jgit.lib.ObjectId) RevWalk(org.eclipse.jgit.revwalk.RevWalk) RevTree(org.eclipse.jgit.revwalk.RevTree) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 97 with Ref

use of org.eclipse.jgit.lib.Ref in project gerrit by GerritCodeReview.

the class AbstractSubmit method assertCurrentRevision.

protected void assertCurrentRevision(String changeId, int expectedNum, ObjectId expectedId) throws Exception {
    ChangeInfo c = get(changeId, CURRENT_REVISION);
    assertThat(c.currentRevision).isEqualTo(expectedId.name());
    assertThat(c.revisions.get(expectedId.name())._number).isEqualTo(expectedNum);
    try (Repository repo = repoManager.openRepository(new Project.NameKey(c.project))) {
        String refName = new PatchSet.Id(new Change.Id(c._number), expectedNum).toRefName();
        Ref ref = repo.exactRef(refName);
        assertThat(ref).named(refName).isNotNull();
        assertThat(ref.getObjectId()).isEqualTo(expectedId);
    }
}
Also used : Project(com.google.gerrit.reviewdb.client.Project) InMemoryRepository(org.eclipse.jgit.internal.storage.dfs.InMemoryRepository) TestRepository(org.eclipse.jgit.junit.TestRepository) Repository(org.eclipse.jgit.lib.Repository) Ref(org.eclipse.jgit.lib.Ref) ChangeInfo(com.google.gerrit.extensions.common.ChangeInfo) PatchSet(com.google.gerrit.reviewdb.client.PatchSet) ObjectId(org.eclipse.jgit.lib.ObjectId)

Example 98 with Ref

use of org.eclipse.jgit.lib.Ref in project gerrit by GerritCodeReview.

the class CommentsIT method getCommits.

private List<RevCommit> getCommits(Change.Id changeId) throws IOException {
    try (Repository repo = repoManager.openRepository(project);
        RevWalk revWalk = new RevWalk(repo)) {
        Ref metaRef = repo.exactRef(RefNames.changeMetaRef(changeId));
        revWalk.markStart(revWalk.parseCommit(metaRef.getObjectId()));
        return Lists.newArrayList(revWalk);
    }
}
Also used : Repository(org.eclipse.jgit.lib.Repository) Ref(org.eclipse.jgit.lib.Ref) RevWalk(org.eclipse.jgit.revwalk.RevWalk)

Example 99 with Ref

use of org.eclipse.jgit.lib.Ref in project gerrit by GerritCodeReview.

the class PublicKeyStore method load.

private void load() throws IOException {
    reset();
    reader = repo.newObjectReader();
    Ref ref = repo.getRefDatabase().exactRef(REFS_GPG_KEYS);
    if (ref == null) {
        return;
    }
    try (RevWalk rw = new RevWalk(reader)) {
        tip = rw.parseCommit(ref.getObjectId());
        notes = NoteMap.read(reader, tip);
    }
}
Also used : Ref(org.eclipse.jgit.lib.Ref) RevWalk(org.eclipse.jgit.revwalk.RevWalk)

Example 100 with Ref

use of org.eclipse.jgit.lib.Ref in project gerrit by GerritCodeReview.

the class ReceiveCommits method autoCloseChanges.

private void autoCloseChanges(final ReceiveCommand cmd) {
    logDebug("Starting auto-closing of changes");
    String refName = cmd.getRefName();
    checkState(!MagicBranch.isMagicBranch(refName), "shouldn't be auto-closing changes on magic branch %s", refName);
    // insertChangesAndPatchSets.
    try (BatchUpdate bu = batchUpdateFactory.create(db, projectControl.getProject().getNameKey(), user, TimeUtil.nowTs());
        ObjectInserter ins = repo.newObjectInserter();
        ObjectReader reader = ins.newReader();
        RevWalk rw = new RevWalk(reader)) {
        bu.setRepository(repo, rw, ins).updateChangesInParallel();
        bu.setRequestId(receiveId);
        // TODO(dborowitz): Teach BatchUpdate to ignore missing changes.
        RevCommit newTip = rw.parseCommit(cmd.getNewId());
        Branch.NameKey branch = new Branch.NameKey(project.getNameKey(), refName);
        rw.reset();
        rw.markStart(newTip);
        if (!ObjectId.zeroId().equals(cmd.getOldId())) {
            rw.markUninteresting(rw.parseCommit(cmd.getOldId()));
        }
        ListMultimap<ObjectId, Ref> byCommit = changeRefsById();
        Map<Change.Key, ChangeNotes> byKey = null;
        List<ReplaceRequest> replaceAndClose = new ArrayList<>();
        int existingPatchSets = 0;
        int newPatchSets = 0;
        COMMIT: for (RevCommit c; (c = rw.next()) != null; ) {
            rw.parseBody(c);
            for (Ref ref : byCommit.get(c.copy())) {
                existingPatchSets++;
                PatchSet.Id psId = PatchSet.Id.fromRef(ref.getName());
                bu.addOp(psId.getParentKey(), mergedByPushOpFactory.create(requestScopePropagator, psId, refName));
                continue COMMIT;
            }
            for (String changeId : c.getFooterLines(CHANGE_ID)) {
                if (byKey == null) {
                    byKey = openChangesByBranch(branch);
                }
                ChangeNotes onto = byKey.get(new Change.Key(changeId.trim()));
                if (onto != null) {
                    newPatchSets++;
                    // Hold onto this until we're done with the walk, as the call to
                    // req.validate below calls isMergedInto which resets the walk.
                    ReplaceRequest req = new ReplaceRequest(onto.getChangeId(), c, cmd, false);
                    req.notes = onto;
                    replaceAndClose.add(req);
                    continue COMMIT;
                }
            }
        }
        for (final ReplaceRequest req : replaceAndClose) {
            Change.Id id = req.notes.getChangeId();
            if (!req.validate(true)) {
                logDebug("Not closing {} because validation failed", id);
                continue;
            }
            req.addOps(bu, null);
            bu.addOp(id, mergedByPushOpFactory.create(requestScopePropagator, req.psId, refName).setPatchSetProvider(new Provider<PatchSet>() {

                @Override
                public PatchSet get() {
                    return req.replaceOp.getPatchSet();
                }
            }));
            bu.addOp(id, new ChangeProgressOp(closeProgress));
        }
        logDebug("Auto-closing {} changes with existing patch sets and {} with new patch sets", existingPatchSets, newPatchSets);
        bu.execute();
    } catch (RestApiException e) {
        logError("Can't insert patchset", e);
    } catch (IOException | OrmException | UpdateException | PermissionBackendException e) {
        logError("Can't scan for changes to close", e);
    }
}
Also used : ArrayList(java.util.ArrayList) ChangeNotes(com.google.gerrit.server.notedb.ChangeNotes) BatchUpdate(com.google.gerrit.server.update.BatchUpdate) ObjectInserter(org.eclipse.jgit.lib.ObjectInserter) OrmException(com.google.gwtorm.server.OrmException) MagicBranch(com.google.gerrit.server.util.MagicBranch) Branch(com.google.gerrit.reviewdb.client.Branch) ObjectReader(org.eclipse.jgit.lib.ObjectReader) UpdateException(com.google.gerrit.server.update.UpdateException) RevCommit(org.eclipse.jgit.revwalk.RevCommit) ObjectId(org.eclipse.jgit.lib.ObjectId) PermissionBackendException(com.google.gerrit.server.permissions.PermissionBackendException) Change(com.google.gerrit.reviewdb.client.Change) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) Provider(com.google.inject.Provider) Ref(org.eclipse.jgit.lib.Ref) RequestId(com.google.gerrit.server.util.RequestId) ObjectId(org.eclipse.jgit.lib.ObjectId) RevId(com.google.gerrit.reviewdb.client.RevId) RestApiException(com.google.gerrit.extensions.restapi.RestApiException)

Aggregations

Ref (org.eclipse.jgit.lib.Ref)137 IOException (java.io.IOException)55 RevCommit (org.eclipse.jgit.revwalk.RevCommit)45 ObjectId (org.eclipse.jgit.lib.ObjectId)38 RevWalk (org.eclipse.jgit.revwalk.RevWalk)37 Repository (org.eclipse.jgit.lib.Repository)35 Change (com.google.gerrit.reviewdb.client.Change)22 Test (org.junit.Test)20 ArrayList (java.util.ArrayList)18 Git (org.eclipse.jgit.api.Git)18 File (java.io.File)16 PatchSet (com.google.gerrit.reviewdb.client.PatchSet)12 OrmException (com.google.gwtorm.server.OrmException)12 Map (java.util.Map)12 PersonIdent (org.eclipse.jgit.lib.PersonIdent)12 Status (org.eclipse.jgit.api.Status)11 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)11 HashMap (java.util.HashMap)10 Account (com.google.gerrit.reviewdb.client.Account)9 Branch (com.google.gerrit.reviewdb.client.Branch)9